st/mesa: more fixes for stencil ref change.
[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_program.h"
46 #include "st_public.h"
47 #include "st_inlines.h"
48
49 #include "pipe/p_context.h"
50 #include "util/u_inlines.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_format.h"
54 #include "util/u_simple_shaders.h"
55 #include "util/u_draw_quad.h"
56
57 #include "cso_cache/cso_context.h"
58
59
60 void
61 st_init_clear(struct st_context *st)
62 {
63 struct pipe_context *pipe = st->pipe;
64
65 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
66 st->clear.raster.gl_rasterization_rules = 1;
67
68 /* rasterizer state: bypass vertex shader, clipping and viewport */
69 st->clear.raster.bypass_vs_clip_and_viewport = 1;
70
71 /* fragment shader state: color pass-through program */
72 st->clear.fs =
73 util_make_fragment_passthrough_shader(pipe);
74
75 /* vertex shader state: color/position pass-through */
76 {
77 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
78 TGSI_SEMANTIC_COLOR };
79 const uint semantic_indexes[] = { 0, 0 };
80 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
81 semantic_names,
82 semantic_indexes);
83 }
84 }
85
86
87 void
88 st_destroy_clear(struct st_context *st)
89 {
90 if (st->clear.fs) {
91 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
92 st->clear.fs = NULL;
93 }
94 if (st->clear.vs) {
95 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
96 st->clear.vs = NULL;
97 }
98 if (st->clear.vbuf) {
99 pipe_buffer_reference(&st->clear.vbuf, NULL);
100 st->clear.vbuf = NULL;
101 }
102 }
103
104
105 /**
106 * Draw a screen-aligned quadrilateral.
107 * Coords are window coords with y=0=bottom. These will be passed
108 * through unmodified to the rasterizer as we have set
109 * rasterizer->bypass_vs_clip_and_viewport.
110 */
111 static void
112 draw_quad(GLcontext *ctx,
113 float x0, float y0, float x1, float y1, GLfloat z,
114 const GLfloat color[4])
115 {
116 struct st_context *st = ctx->st;
117 struct pipe_context *pipe = st->pipe;
118
119 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
120 * no_flush) updates to buffers where we know there is no conflict
121 * with previous data. Currently using max_slots > 1 will cause
122 * synchronous rendering if the driver flushes its command buffers
123 * between one bitmap and the next. Our flush hook below isn't
124 * sufficient to catch this as the driver doesn't tell us when it
125 * flushes its own command buffers. Until this gets fixed, pay the
126 * price of allocating a new buffer for each bitmap cache-flush to
127 * avoid synchronous rendering.
128 */
129 const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
130 GLuint i;
131
132 if (st->clear.vbuf_slot >= max_slots) {
133 pipe_buffer_reference(&st->clear.vbuf, NULL);
134 st->clear.vbuf_slot = 0;
135 }
136
137 if (!st->clear.vbuf) {
138 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
139 max_slots * sizeof(st->clear.vertices));
140 }
141
142 /* positions */
143 st->clear.vertices[0][0][0] = x0;
144 st->clear.vertices[0][0][1] = y0;
145
146 st->clear.vertices[1][0][0] = x1;
147 st->clear.vertices[1][0][1] = y0;
148
149 st->clear.vertices[2][0][0] = x1;
150 st->clear.vertices[2][0][1] = y1;
151
152 st->clear.vertices[3][0][0] = x0;
153 st->clear.vertices[3][0][1] = y1;
154
155 /* same for all verts: */
156 for (i = 0; i < 4; i++) {
157 st->clear.vertices[i][0][2] = z;
158 st->clear.vertices[i][0][3] = 1.0;
159 st->clear.vertices[i][1][0] = color[0];
160 st->clear.vertices[i][1][1] = color[1];
161 st->clear.vertices[i][1][2] = color[2];
162 st->clear.vertices[i][1][3] = color[3];
163 }
164
165 /* put vertex data into vbuf */
166 st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
167 st->clear.vbuf_slot * sizeof(st->clear.vertices),
168 sizeof(st->clear.vertices),
169 st->clear.vertices);
170
171 /* draw */
172 util_draw_vertex_buffer(pipe,
173 st->clear.vbuf,
174 st->clear.vbuf_slot * sizeof(st->clear.vertices),
175 PIPE_PRIM_TRIANGLE_FAN,
176 4, /* verts */
177 2); /* attribs/vert */
178
179 /* Increment slot */
180 st->clear.vbuf_slot++;
181 }
182
183
184
185 /**
186 * Do glClear by drawing a quadrilateral.
187 * The vertices of the quad will be computed from the
188 * ctx->DrawBuffer->_X/Ymin/max fields.
189 */
190 static void
191 clear_with_quad(GLcontext *ctx,
192 GLboolean color, GLboolean depth, GLboolean stencil)
193 {
194 struct st_context *st = ctx->st;
195 struct pipe_stencil_ref stencil_ref;
196 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
197 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
198 GLfloat y0, y1;
199
200 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
201 y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
202 y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
203 }
204 else {
205 y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
206 y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
207 }
208
209 /*
210 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
211 color ? "color, " : "",
212 depth ? "depth, " : "",
213 stencil ? "stencil" : "",
214 x0, y0,
215 x1, y1);
216 */
217
218 cso_save_blend(st->cso_context);
219 cso_save_depth_stencil_alpha(st->cso_context);
220 cso_save_rasterizer(st->cso_context);
221 cso_save_fragment_shader(st->cso_context);
222 cso_save_vertex_shader(st->cso_context);
223
224 /* blend state: RGBA masking */
225 {
226 struct pipe_blend_state blend;
227 memset(&blend, 0, sizeof(blend));
228 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
229 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
230 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
231 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
232 if (color) {
233 if (ctx->Color.ColorMask[0][0])
234 blend.rt[0].colormask |= PIPE_MASK_R;
235 if (ctx->Color.ColorMask[0][1])
236 blend.rt[0].colormask |= PIPE_MASK_G;
237 if (ctx->Color.ColorMask[0][2])
238 blend.rt[0].colormask |= PIPE_MASK_B;
239 if (ctx->Color.ColorMask[0][3])
240 blend.rt[0].colormask |= PIPE_MASK_A;
241 if (st->ctx->Color.DitherFlag)
242 blend.dither = 1;
243 }
244 cso_set_blend(st->cso_context, &blend);
245 }
246
247 /* depth_stencil state: always pass/set to ref value */
248 {
249 struct pipe_depth_stencil_alpha_state depth_stencil;
250 memset(&depth_stencil, 0, sizeof(depth_stencil));
251 memset(&stencil_ref, 0, sizeof(stencil_ref));
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].valuemask = 0xff;
265 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
266 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
267 cso_set_stencil_ref(st->cso_context, &stencil_ref);
268 }
269
270 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
271 }
272
273 cso_set_rasterizer(st->cso_context, &st->clear.raster);
274
275 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
276 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
277
278 /* draw quad matching scissor rect (XXX verify coord round-off) */
279 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
280
281 /* Restore pipe state */
282 cso_restore_blend(st->cso_context);
283 cso_restore_depth_stencil_alpha(st->cso_context);
284 cso_restore_rasterizer(st->cso_context);
285 cso_restore_fragment_shader(st->cso_context);
286 cso_restore_vertex_shader(st->cso_context);
287
288 /* cannot restore stencil ref. Try to reconstruct? */
289 if (stencil) {
290 if (ctx->Stencil.Enabled && ctx->DrawBuffer->Visual.stencilBits > 0) {
291 stencil_ref.ref_value[0] = ctx->Stencil.Ref[0] & 0xff;
292 if (ctx->Stencil._TestTwoSide) {
293 const GLuint back = ctx->Stencil._BackFace;
294 stencil_ref.ref_value[1] = ctx->Stencil.Ref[back] & 0xff;
295 }
296 else {
297 stencil_ref.ref_value[1] = stencil_ref.ref_value[0];
298 }
299 }
300 cso_set_stencil_ref(st->cso_context, &stencil_ref);
301 }
302
303 }
304
305
306 /**
307 * Determine if we need to clear the depth buffer by drawing a quad.
308 */
309 static INLINE GLboolean
310 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
311 {
312 if (ctx->Scissor.Enabled &&
313 (ctx->Scissor.X != 0 ||
314 ctx->Scissor.Y != 0 ||
315 ctx->Scissor.Width < rb->Width ||
316 ctx->Scissor.Height < rb->Height))
317 return TRUE;
318
319 if (!ctx->Color.ColorMask[0][0] ||
320 !ctx->Color.ColorMask[0][1] ||
321 !ctx->Color.ColorMask[0][2] ||
322 !ctx->Color.ColorMask[0][3])
323 return TRUE;
324
325 return FALSE;
326 }
327
328
329 static INLINE GLboolean
330 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
331 {
332 const GLuint stencilMax = 0xff;
333 GLboolean maskStencil
334 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
335
336 assert(rb->Format == MESA_FORMAT_S8 ||
337 rb->Format == MESA_FORMAT_Z24_S8 ||
338 rb->Format == MESA_FORMAT_S8_Z24);
339
340 if (ctx->Scissor.Enabled &&
341 (ctx->Scissor.X != 0 ||
342 ctx->Scissor.Y != 0 ||
343 ctx->Scissor.Width < rb->Width ||
344 ctx->Scissor.Height < rb->Height))
345 return TRUE;
346
347 if (maskStencil)
348 return TRUE;
349
350 return FALSE;
351 }
352
353
354 /**
355 * Determine if we need to clear the depth buffer by drawing a quad.
356 */
357 static INLINE GLboolean
358 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
359 {
360 const struct st_renderbuffer *strb = st_renderbuffer(rb);
361 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
362
363 if (ctx->Scissor.Enabled &&
364 (ctx->Scissor.X != 0 ||
365 ctx->Scissor.Y != 0 ||
366 ctx->Scissor.Width < rb->Width ||
367 ctx->Scissor.Height < rb->Height))
368 return TRUE;
369
370 if (isDS &&
371 ctx->DrawBuffer->Visual.stencilBits > 0)
372 return TRUE;
373
374 return FALSE;
375 }
376
377
378 /**
379 * Determine if we need to clear the stencil buffer by drawing a quad.
380 */
381 static INLINE GLboolean
382 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
383 {
384 const struct st_renderbuffer *strb = st_renderbuffer(rb);
385 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
386 const GLuint stencilMax = 0xff;
387 const GLboolean maskStencil
388 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
389
390 assert(rb->Format == MESA_FORMAT_S8 ||
391 rb->Format == MESA_FORMAT_Z24_S8 ||
392 rb->Format == MESA_FORMAT_S8_Z24);
393
394 if (maskStencil)
395 return TRUE;
396
397 if (ctx->Scissor.Enabled &&
398 (ctx->Scissor.X != 0 ||
399 ctx->Scissor.Y != 0 ||
400 ctx->Scissor.Width < rb->Width ||
401 ctx->Scissor.Height < rb->Height))
402 return TRUE;
403
404 /* This is correct, but it is necessary to look at the depth clear
405 * value held in the surface when it comes time to issue the clear,
406 * rather than taking depth and stencil clear values from the
407 * current state.
408 */
409 if (isDS &&
410 ctx->DrawBuffer->Visual.depthBits > 0)
411 return TRUE;
412
413 return FALSE;
414 }
415
416
417
418 void st_flush_clear( struct st_context *st )
419 {
420 /* Release vertex buffer to avoid synchronous rendering if we were
421 * to map it in the next frame.
422 */
423 pipe_buffer_reference(&st->clear.vbuf, NULL);
424 st->clear.vbuf_slot = 0;
425 }
426
427
428
429 /**
430 * Called via ctx->Driver.Clear()
431 * XXX: doesn't pick up the differences between front/back/left/right
432 * clears. Need to sort that out...
433 */
434 static void st_clear(GLcontext *ctx, GLbitfield mask)
435 {
436 static const GLbitfield BUFFER_BITS_DS
437 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
438 struct st_context *st = ctx->st;
439 struct gl_renderbuffer *depthRb
440 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
441 struct gl_renderbuffer *stencilRb
442 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
443 GLbitfield quad_buffers = 0;
444 GLbitfield clear_buffers = 0;
445 GLuint i;
446
447 /* This makes sure the pipe has the latest scissor, etc values */
448 st_validate_state( st );
449
450 if (mask & BUFFER_BITS_COLOR) {
451 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
452 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
453
454 if (mask & (1 << b)) {
455 struct gl_renderbuffer *rb
456 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
457 struct st_renderbuffer *strb;
458
459 assert(rb);
460
461 strb = st_renderbuffer(rb);
462
463 if (!strb->surface)
464 continue;
465
466 if (check_clear_color_with_quad( ctx, rb ))
467 quad_buffers |= PIPE_CLEAR_COLOR;
468 else
469 clear_buffers |= PIPE_CLEAR_COLOR;
470 }
471 }
472 }
473
474 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
475 /* clearing combined depth + stencil */
476 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
477
478 if (strb->surface) {
479 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
480 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
481 else
482 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
483 }
484 }
485 else {
486 /* separate depth/stencil clears */
487 if (mask & BUFFER_BIT_DEPTH) {
488 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
489
490 if (strb->surface) {
491 if (check_clear_depth_with_quad(ctx, depthRb))
492 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
493 else
494 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
495 }
496 }
497 if (mask & BUFFER_BIT_STENCIL) {
498 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
499
500 if (strb->surface) {
501 if (check_clear_stencil_with_quad(ctx, stencilRb))
502 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
503 else
504 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
505 }
506 }
507 }
508
509 /*
510 * If we're going to use clear_with_quad() for any reason, use it for
511 * everything possible.
512 */
513 if (quad_buffers) {
514 quad_buffers |= clear_buffers;
515 clear_with_quad(ctx,
516 quad_buffers & PIPE_CLEAR_COLOR,
517 mask & BUFFER_BIT_DEPTH,
518 mask & BUFFER_BIT_STENCIL);
519 } else if (clear_buffers)
520 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
521 ctx->Depth.Clear, ctx->Stencil.Clear);
522
523 if (mask & BUFFER_BIT_ACCUM)
524 st_clear_accum_buffer(ctx,
525 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
526 }
527
528
529 void st_init_clear_functions(struct dd_function_table *functions)
530 {
531 functions->Clear = st_clear;
532 }