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