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