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