Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Keith Whitwell <keith@tungstengraphics.com>
32 * Brian Paul
33 * Michel Dänzer
34 */
35
36 #include "main/glheader.h"
37 #include "main/formats.h"
38 #include "main/macros.h"
39 #include "shader/prog_instruction.h"
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_cb_accum.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
45 #include "st_draw.h"
46 #include "st_program.h"
47 #include "st_public.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_inlines.h"
50
51 #include "pipe/p_context.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_pack_color.h"
56 #include "util/u_simple_shaders.h"
57 #include "util/u_draw_quad.h"
58
59 #include "cso_cache/cso_context.h"
60
61
62 void
63 st_init_clear(struct st_context *st)
64 {
65 struct pipe_context *pipe = st->pipe;
66
67 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
68 st->clear.raster.gl_rasterization_rules = 1;
69
70 /* rasterizer state: bypass vertex shader, clipping and viewport */
71 st->clear.raster.bypass_vs_clip_and_viewport = 1;
72
73 /* fragment shader state: color pass-through program */
74 st->clear.fs =
75 util_make_fragment_passthrough_shader(pipe);
76
77 /* vertex shader state: color/position pass-through */
78 {
79 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
80 TGSI_SEMANTIC_COLOR };
81 const uint semantic_indexes[] = { 0, 0 };
82 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
83 semantic_names,
84 semantic_indexes);
85 }
86 }
87
88
89 void
90 st_destroy_clear(struct st_context *st)
91 {
92 if (st->clear.fs) {
93 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
94 st->clear.fs = NULL;
95 }
96 if (st->clear.vs) {
97 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
98 st->clear.vs = NULL;
99 }
100 if (st->clear.vbuf) {
101 pipe_buffer_reference(&st->clear.vbuf, NULL);
102 st->clear.vbuf = NULL;
103 }
104 }
105
106
107 /**
108 * Draw a screen-aligned quadrilateral.
109 * Coords are window coords with y=0=bottom. These will be passed
110 * through unmodified to the rasterizer as we have set
111 * rasterizer->bypass_vs_clip_and_viewport.
112 */
113 static void
114 draw_quad(GLcontext *ctx,
115 float x0, float y0, float x1, float y1, GLfloat z,
116 const GLfloat color[4])
117 {
118 struct st_context *st = ctx->st;
119 struct pipe_context *pipe = st->pipe;
120
121 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
122 * no_flush) updates to buffers where we know there is no conflict
123 * with previous data. Currently using max_slots > 1 will cause
124 * synchronous rendering if the driver flushes its command buffers
125 * between one bitmap and the next. Our flush hook below isn't
126 * sufficient to catch this as the driver doesn't tell us when it
127 * flushes its own command buffers. Until this gets fixed, pay the
128 * price of allocating a new buffer for each bitmap cache-flush to
129 * avoid synchronous rendering.
130 */
131 const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
132 GLuint i;
133
134 if (st->clear.vbuf_slot >= max_slots) {
135 pipe_buffer_reference(&st->clear.vbuf, NULL);
136 st->clear.vbuf_slot = 0;
137 }
138
139 if (!st->clear.vbuf) {
140 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
141 max_slots * sizeof(st->clear.vertices));
142 }
143
144 /* positions */
145 st->clear.vertices[0][0][0] = x0;
146 st->clear.vertices[0][0][1] = y0;
147
148 st->clear.vertices[1][0][0] = x1;
149 st->clear.vertices[1][0][1] = y0;
150
151 st->clear.vertices[2][0][0] = x1;
152 st->clear.vertices[2][0][1] = y1;
153
154 st->clear.vertices[3][0][0] = x0;
155 st->clear.vertices[3][0][1] = y1;
156
157 /* same for all verts: */
158 for (i = 0; i < 4; i++) {
159 st->clear.vertices[i][0][2] = z;
160 st->clear.vertices[i][0][3] = 1.0;
161 st->clear.vertices[i][1][0] = color[0];
162 st->clear.vertices[i][1][1] = color[1];
163 st->clear.vertices[i][1][2] = color[2];
164 st->clear.vertices[i][1][3] = color[3];
165 }
166
167 /* put vertex data into vbuf */
168 st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
169 st->clear.vbuf_slot * sizeof(st->clear.vertices),
170 sizeof(st->clear.vertices),
171 st->clear.vertices);
172
173 /* draw */
174 util_draw_vertex_buffer(pipe,
175 st->clear.vbuf,
176 st->clear.vbuf_slot * sizeof(st->clear.vertices),
177 PIPE_PRIM_TRIANGLE_FAN,
178 4, /* verts */
179 2); /* attribs/vert */
180
181 /* Increment slot */
182 st->clear.vbuf_slot++;
183 }
184
185
186
187 /**
188 * Do glClear by drawing a quadrilateral.
189 * The vertices of the quad will be computed from the
190 * ctx->DrawBuffer->_X/Ymin/max fields.
191 */
192 static void
193 clear_with_quad(GLcontext *ctx,
194 GLboolean color, GLboolean depth, GLboolean stencil)
195 {
196 struct st_context *st = ctx->st;
197 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
198 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
199 GLfloat y0, y1;
200
201 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
202 y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
203 y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
204 }
205 else {
206 y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
207 y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
208 }
209
210 /*
211 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
212 color ? "color, " : "",
213 depth ? "depth, " : "",
214 stencil ? "stencil" : "",
215 x0, y0,
216 x1, y1);
217 */
218
219 cso_save_blend(st->cso_context);
220 cso_save_depth_stencil_alpha(st->cso_context);
221 cso_save_rasterizer(st->cso_context);
222 cso_save_fragment_shader(st->cso_context);
223 cso_save_vertex_shader(st->cso_context);
224
225 /* blend state: RGBA masking */
226 {
227 struct pipe_blend_state blend;
228 memset(&blend, 0, sizeof(blend));
229 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
230 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
231 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
232 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
233 if (color) {
234 if (ctx->Color.ColorMask[0])
235 blend.colormask |= PIPE_MASK_R;
236 if (ctx->Color.ColorMask[1])
237 blend.colormask |= PIPE_MASK_G;
238 if (ctx->Color.ColorMask[2])
239 blend.colormask |= PIPE_MASK_B;
240 if (ctx->Color.ColorMask[3])
241 blend.colormask |= PIPE_MASK_A;
242 if (st->ctx->Color.DitherFlag)
243 blend.dither = 1;
244 }
245 cso_set_blend(st->cso_context, &blend);
246 }
247
248 /* depth_stencil state: always pass/set to ref value */
249 {
250 struct pipe_depth_stencil_alpha_state depth_stencil;
251 memset(&depth_stencil, 0, sizeof(depth_stencil));
252 if (depth) {
253 depth_stencil.depth.enabled = 1;
254 depth_stencil.depth.writemask = 1;
255 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
256 }
257
258 if (stencil) {
259 depth_stencil.stencil[0].enabled = 1;
260 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
261 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
262 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
263 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
264 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
265 depth_stencil.stencil[0].valuemask = 0xff;
266 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
267 }
268
269 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
270 }
271
272 cso_set_rasterizer(st->cso_context, &st->clear.raster);
273
274 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
275 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
276
277 /* draw quad matching scissor rect (XXX verify coord round-off) */
278 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
279
280 /* Restore pipe state */
281 cso_restore_blend(st->cso_context);
282 cso_restore_depth_stencil_alpha(st->cso_context);
283 cso_restore_rasterizer(st->cso_context);
284 cso_restore_fragment_shader(st->cso_context);
285 cso_restore_vertex_shader(st->cso_context);
286 }
287
288
289 /**
290 * Determine if we need to clear the depth buffer by drawing a quad.
291 */
292 static INLINE GLboolean
293 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
294 {
295 if (ctx->Scissor.Enabled &&
296 (ctx->Scissor.X != 0 ||
297 ctx->Scissor.Y != 0 ||
298 ctx->Scissor.Width < rb->Width ||
299 ctx->Scissor.Height < rb->Height))
300 return TRUE;
301
302 if (!ctx->Color.ColorMask[0] ||
303 !ctx->Color.ColorMask[1] ||
304 !ctx->Color.ColorMask[2] ||
305 !ctx->Color.ColorMask[3])
306 return TRUE;
307
308 return FALSE;
309 }
310
311
312 static INLINE GLboolean
313 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
314 {
315 const GLuint stencilMax = 0xff;
316 GLboolean maskStencil
317 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
318
319 assert(rb->Format == MESA_FORMAT_S8 ||
320 rb->Format == MESA_FORMAT_Z24_S8 ||
321 rb->Format == MESA_FORMAT_S8_Z24);
322
323 if (ctx->Scissor.Enabled &&
324 (ctx->Scissor.X != 0 ||
325 ctx->Scissor.Y != 0 ||
326 ctx->Scissor.Width < rb->Width ||
327 ctx->Scissor.Height < rb->Height))
328 return TRUE;
329
330 if (maskStencil)
331 return TRUE;
332
333 return FALSE;
334 }
335
336
337 /**
338 * Determine if we need to clear the depth buffer by drawing a quad.
339 */
340 static INLINE GLboolean
341 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
342 {
343 const struct st_renderbuffer *strb = st_renderbuffer(rb);
344 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
345
346 if (ctx->Scissor.Enabled &&
347 (ctx->Scissor.X != 0 ||
348 ctx->Scissor.Y != 0 ||
349 ctx->Scissor.Width < rb->Width ||
350 ctx->Scissor.Height < rb->Height))
351 return TRUE;
352
353 if (isDS &&
354 ctx->DrawBuffer->Visual.stencilBits > 0)
355 return TRUE;
356
357 return FALSE;
358 }
359
360
361 /**
362 * Determine if we need to clear the stencil buffer by drawing a quad.
363 */
364 static INLINE GLboolean
365 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
366 {
367 const struct st_renderbuffer *strb = st_renderbuffer(rb);
368 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
369 const GLuint stencilMax = 0xff;
370 const GLboolean maskStencil
371 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
372
373 assert(rb->Format == MESA_FORMAT_S8 ||
374 rb->Format == MESA_FORMAT_Z24_S8 ||
375 rb->Format == MESA_FORMAT_S8_Z24);
376
377 if (maskStencil)
378 return TRUE;
379
380 if (ctx->Scissor.Enabled &&
381 (ctx->Scissor.X != 0 ||
382 ctx->Scissor.Y != 0 ||
383 ctx->Scissor.Width < rb->Width ||
384 ctx->Scissor.Height < rb->Height))
385 return TRUE;
386
387 /* This is correct, but it is necessary to look at the depth clear
388 * value held in the surface when it comes time to issue the clear,
389 * rather than taking depth and stencil clear values from the
390 * current state.
391 */
392 if (isDS &&
393 ctx->DrawBuffer->Visual.depthBits > 0)
394 return TRUE;
395
396 return FALSE;
397 }
398
399
400
401 void st_flush_clear( struct st_context *st )
402 {
403 /* Release vertex buffer to avoid synchronous rendering if we were
404 * to map it in the next frame.
405 */
406 pipe_buffer_reference(&st->clear.vbuf, NULL);
407 st->clear.vbuf_slot = 0;
408 }
409
410
411
412 /**
413 * Called via ctx->Driver.Clear()
414 * XXX: doesn't pick up the differences between front/back/left/right
415 * clears. Need to sort that out...
416 */
417 static void st_clear(GLcontext *ctx, GLbitfield mask)
418 {
419 static const GLbitfield BUFFER_BITS_DS
420 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
421 struct st_context *st = ctx->st;
422 struct gl_renderbuffer *depthRb
423 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
424 struct gl_renderbuffer *stencilRb
425 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
426 GLbitfield quad_buffers = 0;
427 GLbitfield clear_buffers = 0;
428 GLuint i;
429
430 /* This makes sure the pipe has the latest scissor, etc values */
431 st_validate_state( st );
432
433 if (mask & BUFFER_BITS_COLOR) {
434 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
435 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
436
437 if (mask & (1 << b)) {
438 struct gl_renderbuffer *rb
439 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
440 struct st_renderbuffer *strb;
441
442 assert(rb);
443
444 strb = st_renderbuffer(rb);
445
446 if (!strb->surface)
447 continue;
448
449 if (check_clear_color_with_quad( ctx, rb ))
450 quad_buffers |= PIPE_CLEAR_COLOR;
451 else
452 clear_buffers |= PIPE_CLEAR_COLOR;
453 }
454 }
455 }
456
457 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
458 /* clearing combined depth + stencil */
459 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
460
461 if (strb->surface) {
462 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
463 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
464 else
465 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
466 }
467 }
468 else {
469 /* separate depth/stencil clears */
470 if (mask & BUFFER_BIT_DEPTH) {
471 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
472
473 if (strb->surface) {
474 if (check_clear_depth_with_quad(ctx, depthRb))
475 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
476 else
477 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
478 }
479 }
480 if (mask & BUFFER_BIT_STENCIL) {
481 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
482
483 if (strb->surface) {
484 if (check_clear_stencil_with_quad(ctx, stencilRb))
485 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
486 else
487 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
488 }
489 }
490 }
491
492 /*
493 * If we're going to use clear_with_quad() for any reason, use it for
494 * everything possible.
495 */
496 if (quad_buffers) {
497 quad_buffers |= clear_buffers;
498 clear_with_quad(ctx,
499 quad_buffers & PIPE_CLEAR_COLOR,
500 mask & BUFFER_BIT_DEPTH,
501 mask & BUFFER_BIT_STENCIL);
502 } else if (clear_buffers)
503 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
504 ctx->Depth.Clear, ctx->Stencil.Clear);
505
506 if (mask & BUFFER_BIT_ACCUM)
507 st_clear_accum_buffer(ctx,
508 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
509 }
510
511
512 void st_init_clear_functions(struct dd_function_table *functions)
513 {
514 functions->Clear = st_clear;
515 }