f0c30b65ba2b1fc34e38ef59352a4367c35b476c
[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/accum.h"
38 #include "main/formats.h"
39 #include "main/macros.h"
40 #include "program/prog_instruction.h"
41 #include "st_context.h"
42 #include "st_atom.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
45 #include "st_format.h"
46 #include "st_program.h"
47
48 #include "pipe/p_context.h"
49 #include "pipe/p_shader_tokens.h"
50 #include "pipe/p_state.h"
51 #include "pipe/p_defines.h"
52 #include "util/u_format.h"
53 #include "util/u_inlines.h"
54 #include "util/u_simple_shaders.h"
55 #include "util/u_draw_quad.h"
56 #include "util/u_upload_mgr.h"
57
58 #include "cso_cache/cso_context.h"
59
60
61 /**
62 * Do per-context initialization for glClear.
63 */
64 void
65 st_init_clear(struct st_context *st)
66 {
67 struct pipe_screen *pscreen = st->pipe->screen;
68
69 memset(&st->clear, 0, sizeof(st->clear));
70
71 st->clear.raster.gl_rasterization_rules = 1;
72 st->clear.raster.depth_clip = 1;
73 st->clear.enable_ds_separate = pscreen->get_param(pscreen, PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE);
74 }
75
76
77 /**
78 * Free per-context state for glClear.
79 */
80 void
81 st_destroy_clear(struct st_context *st)
82 {
83 if (st->clear.fs) {
84 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
85 st->clear.fs = NULL;
86 }
87 if (st->clear.vs) {
88 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
89 st->clear.vs = NULL;
90 }
91 }
92
93
94 /**
95 * Helper function to set the fragment shaders.
96 */
97 static INLINE void
98 set_fragment_shader(struct st_context *st)
99 {
100 if (!st->clear.fs)
101 st->clear.fs = util_make_fragment_passthrough_shader(st->pipe);
102
103 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
104 }
105
106
107 /**
108 * Helper function to set the vertex shader.
109 */
110 static INLINE void
111 set_vertex_shader(struct st_context *st)
112 {
113 /* vertex shader - still required to provide the linkage between
114 * fragment shader input semantics and vertex_element/buffers.
115 */
116 if (!st->clear.vs)
117 {
118 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
119 TGSI_SEMANTIC_COLOR };
120 const uint semantic_indexes[] = { 0, 0 };
121 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
122 semantic_names,
123 semantic_indexes);
124 }
125
126 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
127 }
128
129
130 /**
131 * Draw a screen-aligned quadrilateral.
132 * Coords are clip coords with y=0=bottom.
133 */
134 static void
135 draw_quad(struct st_context *st,
136 float x0, float y0, float x1, float y1, GLfloat z,
137 const union pipe_color_union *color)
138 {
139 struct pipe_context *pipe = st->pipe;
140 struct pipe_resource *vbuf = NULL;
141 GLuint i, offset;
142
143 /* positions */
144 st->clear.vertices[0][0][0] = x0;
145 st->clear.vertices[0][0][1] = y0;
146
147 st->clear.vertices[1][0][0] = x1;
148 st->clear.vertices[1][0][1] = y0;
149
150 st->clear.vertices[2][0][0] = x1;
151 st->clear.vertices[2][0][1] = y1;
152
153 st->clear.vertices[3][0][0] = x0;
154 st->clear.vertices[3][0][1] = y1;
155
156 /* same for all verts: */
157 for (i = 0; i < 4; i++) {
158 st->clear.vertices[i][0][2] = z;
159 st->clear.vertices[i][0][3] = 1.0;
160 st->clear.vertices[i][1][0] = color->f[0];
161 st->clear.vertices[i][1][1] = color->f[1];
162 st->clear.vertices[i][1][2] = color->f[2];
163 st->clear.vertices[i][1][3] = color->f[3];
164 }
165
166 u_upload_data(st->uploader, 0, sizeof(st->clear.vertices),
167 st->clear.vertices, &offset, &vbuf);
168 if (!vbuf) {
169 return;
170 }
171 u_upload_unmap(st->uploader);
172
173 /* draw */
174 util_draw_vertex_buffer(pipe,
175 st->cso_context,
176 vbuf, offset,
177 PIPE_PRIM_TRIANGLE_FAN,
178 4, /* verts */
179 2); /* attribs/vert */
180
181 pipe_resource_reference(&vbuf, NULL);
182 }
183
184
185
186 /**
187 * Do glClear by drawing a quadrilateral.
188 * The vertices of the quad will be computed from the
189 * ctx->DrawBuffer->_X/Ymin/max fields.
190 */
191 static void
192 clear_with_quad(struct gl_context *ctx,
193 GLboolean color, GLboolean depth, GLboolean stencil)
194 {
195 struct st_context *st = st_context(ctx);
196 const struct gl_framebuffer *fb = ctx->DrawBuffer;
197 const GLfloat fb_width = (GLfloat) fb->Width;
198 const GLfloat fb_height = (GLfloat) fb->Height;
199 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
200 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
201 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
202 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
203 union pipe_color_union clearColor;
204
205 /*
206 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
207 color ? "color, " : "",
208 depth ? "depth, " : "",
209 stencil ? "stencil" : "",
210 x0, y0,
211 x1, y1);
212 */
213
214 cso_save_blend(st->cso_context);
215 cso_save_stencil_ref(st->cso_context);
216 cso_save_depth_stencil_alpha(st->cso_context);
217 cso_save_rasterizer(st->cso_context);
218 cso_save_viewport(st->cso_context);
219 cso_save_fragment_shader(st->cso_context);
220 cso_save_stream_outputs(st->cso_context);
221 cso_save_vertex_shader(st->cso_context);
222 cso_save_geometry_shader(st->cso_context);
223 cso_save_vertex_elements(st->cso_context);
224 cso_save_vertex_buffers(st->cso_context);
225
226 /* blend state: RGBA masking */
227 {
228 struct pipe_blend_state blend;
229 memset(&blend, 0, sizeof(blend));
230 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
231 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
232 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
233 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
234 if (color) {
235 if (ctx->Color.ColorMask[0][0])
236 blend.rt[0].colormask |= PIPE_MASK_R;
237 if (ctx->Color.ColorMask[0][1])
238 blend.rt[0].colormask |= PIPE_MASK_G;
239 if (ctx->Color.ColorMask[0][2])
240 blend.rt[0].colormask |= PIPE_MASK_B;
241 if (ctx->Color.ColorMask[0][3])
242 blend.rt[0].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 struct pipe_stencil_ref stencil_ref;
261 memset(&stencil_ref, 0, sizeof(stencil_ref));
262 depth_stencil.stencil[0].enabled = 1;
263 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
264 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
265 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
266 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
267 depth_stencil.stencil[0].valuemask = 0xff;
268 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
269 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
270 cso_set_stencil_ref(st->cso_context, &stencil_ref);
271 }
272
273 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
274 }
275
276 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
277 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
278
279 cso_set_rasterizer(st->cso_context, &st->clear.raster);
280
281 /* viewport state: viewport matching window dims */
282 {
283 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
284 struct pipe_viewport_state vp;
285 vp.scale[0] = 0.5f * fb_width;
286 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
287 vp.scale[2] = 1.0f;
288 vp.scale[3] = 1.0f;
289 vp.translate[0] = 0.5f * fb_width;
290 vp.translate[1] = 0.5f * fb_height;
291 vp.translate[2] = 0.0f;
292 vp.translate[3] = 0.0f;
293 cso_set_viewport(st->cso_context, &vp);
294 }
295
296 set_fragment_shader(st);
297 set_vertex_shader(st);
298 cso_set_geometry_shader_handle(st->cso_context, NULL);
299
300 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
301 st_translate_color(ctx->Color.ClearColor.f,
302 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
303 clearColor.f);
304 }
305
306 /* draw quad matching scissor rect */
307 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, &clearColor);
308
309 /* Restore pipe state */
310 cso_restore_blend(st->cso_context);
311 cso_restore_stencil_ref(st->cso_context);
312 cso_restore_depth_stencil_alpha(st->cso_context);
313 cso_restore_rasterizer(st->cso_context);
314 cso_restore_viewport(st->cso_context);
315 cso_restore_fragment_shader(st->cso_context);
316 cso_restore_vertex_shader(st->cso_context);
317 cso_restore_geometry_shader(st->cso_context);
318 cso_restore_vertex_elements(st->cso_context);
319 cso_restore_vertex_buffers(st->cso_context);
320 cso_restore_stream_outputs(st->cso_context);
321 }
322
323
324 /**
325 * Determine if we need to clear the depth buffer by drawing a quad.
326 */
327 static INLINE GLboolean
328 check_clear_color_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
329 {
330 if (ctx->Scissor.Enabled &&
331 (ctx->Scissor.X != 0 ||
332 ctx->Scissor.Y != 0 ||
333 ctx->Scissor.Width < rb->Width ||
334 ctx->Scissor.Height < rb->Height))
335 return GL_TRUE;
336
337 if (!ctx->Color.ColorMask[0][0] ||
338 !ctx->Color.ColorMask[0][1] ||
339 !ctx->Color.ColorMask[0][2] ||
340 !ctx->Color.ColorMask[0][3])
341 return GL_TRUE;
342
343 return GL_FALSE;
344 }
345
346
347 /**
348 * Determine if we need to clear the combiend depth/stencil buffer by
349 * drawing a quad.
350 */
351 static INLINE GLboolean
352 check_clear_depth_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
353 {
354 const GLuint stencilMax = 0xff;
355 GLboolean maskStencil
356 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
357
358 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
359
360 if (ctx->Scissor.Enabled &&
361 (ctx->Scissor.X != 0 ||
362 ctx->Scissor.Y != 0 ||
363 ctx->Scissor.Width < rb->Width ||
364 ctx->Scissor.Height < rb->Height))
365 return GL_TRUE;
366
367 if (maskStencil)
368 return GL_TRUE;
369
370 return GL_FALSE;
371 }
372
373
374 /**
375 * Determine if we need to clear the depth buffer by drawing a quad.
376 */
377 static INLINE GLboolean
378 check_clear_depth_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
379 boolean ds_separate)
380 {
381 const struct st_renderbuffer *strb = st_renderbuffer(rb);
382 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
383
384 if (ctx->Scissor.Enabled &&
385 (ctx->Scissor.X != 0 ||
386 ctx->Scissor.Y != 0 ||
387 ctx->Scissor.Width < rb->Width ||
388 ctx->Scissor.Height < rb->Height))
389 return GL_TRUE;
390
391 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.stencilBits > 0)
392 return GL_TRUE;
393
394 return GL_FALSE;
395 }
396
397
398 /**
399 * Determine if we need to clear the stencil buffer by drawing a quad.
400 */
401 static INLINE GLboolean
402 check_clear_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
403 boolean ds_separate)
404 {
405 const struct st_renderbuffer *strb = st_renderbuffer(rb);
406 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
407 const GLuint stencilMax = 0xff;
408 const GLboolean maskStencil
409 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
410
411 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
412
413 if (maskStencil)
414 return GL_TRUE;
415
416 if (ctx->Scissor.Enabled &&
417 (ctx->Scissor.X != 0 ||
418 ctx->Scissor.Y != 0 ||
419 ctx->Scissor.Width < rb->Width ||
420 ctx->Scissor.Height < rb->Height))
421 return GL_TRUE;
422
423 /* This is correct, but it is necessary to look at the depth clear
424 * value held in the surface when it comes time to issue the clear,
425 * rather than taking depth and stencil clear values from the
426 * current state.
427 */
428 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.depthBits > 0)
429 return GL_TRUE;
430
431 return GL_FALSE;
432 }
433
434
435 /**
436 * Called via ctx->Driver.Clear()
437 */
438 static void
439 st_Clear(struct gl_context *ctx, GLbitfield mask)
440 {
441 static const GLbitfield BUFFER_BITS_DS
442 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
443 struct st_context *st = st_context(ctx);
444 struct gl_renderbuffer *depthRb
445 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
446 struct gl_renderbuffer *stencilRb
447 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
448 GLbitfield quad_buffers = 0x0;
449 GLbitfield clear_buffers = 0x0;
450 GLuint i;
451
452 /* This makes sure the pipe has the latest scissor, etc values */
453 st_validate_state( st );
454
455 if (mask & BUFFER_BITS_COLOR) {
456 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
457 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
458
459 if (mask & (1 << b)) {
460 struct gl_renderbuffer *rb
461 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
462 struct st_renderbuffer *strb = st_renderbuffer(rb);
463
464 if (!strb || !strb->surface)
465 continue;
466
467 if (check_clear_color_with_quad( ctx, rb ))
468 quad_buffers |= PIPE_CLEAR_COLOR;
469 else
470 clear_buffers |= PIPE_CLEAR_COLOR;
471 }
472 }
473 }
474
475 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
476 /* clearing combined depth + stencil */
477 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
478
479 if (strb->surface) {
480 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
481 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
482 else
483 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
484 }
485 }
486 else {
487 /* separate depth/stencil clears */
488 /* I don't think truly separate buffers are actually possible in gallium or hw? */
489 if (mask & BUFFER_BIT_DEPTH) {
490 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
491
492 if (strb->surface) {
493 if (check_clear_depth_with_quad(ctx, depthRb,
494 st->clear.enable_ds_separate))
495 quad_buffers |= PIPE_CLEAR_DEPTH;
496 else
497 clear_buffers |= PIPE_CLEAR_DEPTH;
498 }
499 }
500 if (mask & BUFFER_BIT_STENCIL) {
501 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
502
503 if (strb->surface) {
504 if (check_clear_stencil_with_quad(ctx, stencilRb,
505 st->clear.enable_ds_separate))
506 quad_buffers |= PIPE_CLEAR_STENCIL;
507 else
508 clear_buffers |= PIPE_CLEAR_STENCIL;
509 }
510 }
511 }
512
513 /*
514 * If we're going to use clear_with_quad() for any reason, use it for
515 * everything possible.
516 */
517 if (quad_buffers) {
518 quad_buffers |= clear_buffers;
519 clear_with_quad(ctx,
520 quad_buffers & PIPE_CLEAR_COLOR,
521 quad_buffers & PIPE_CLEAR_DEPTH,
522 quad_buffers & PIPE_CLEAR_STENCIL);
523 } else if (clear_buffers) {
524 /* driver cannot know it can clear everything if the buffer
525 * is a combined depth/stencil buffer but this wasn't actually
526 * required from the visual. Hence fix this up to avoid potential
527 * read-modify-write in the driver.
528 */
529 union pipe_color_union clearColor;
530
531 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) &&
532 ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
533 (depthRb == stencilRb) &&
534 (ctx->DrawBuffer->Visual.depthBits == 0 ||
535 ctx->DrawBuffer->Visual.stencilBits == 0))
536 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
537
538 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
539 st_translate_color(ctx->Color.ClearColor.f,
540 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
541 clearColor.f);
542 }
543
544 st->pipe->clear(st->pipe, clear_buffers, &clearColor,
545 ctx->Depth.Clear, ctx->Stencil.Clear);
546 }
547 if (mask & BUFFER_BIT_ACCUM)
548 _mesa_clear_accum_buffer(ctx);
549 }
550
551
552 void
553 st_init_clear_functions(struct dd_function_table *functions)
554 {
555 functions->Clear = st_Clear;
556 }