Merge branch 'mesa_7_6_branch'
[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
120 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
121 * no_flush) updates to buffers where we know there is no conflict
122 * with previous data. Currently using max_slots > 1 will cause
123 * synchronous rendering if the driver flushes its command buffers
124 * between one bitmap and the next. Our flush hook below isn't
125 * sufficient to catch this as the driver doesn't tell us when it
126 * flushes its own command buffers. Until this gets fixed, pay the
127 * price of allocating a new buffer for each bitmap cache-flush to
128 * avoid synchronous rendering.
129 */
130 const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
131 GLuint i;
132
133 if (st->clear.vbuf_slot >= max_slots) {
134 pipe_buffer_reference(&st->clear.vbuf, NULL);
135 st->clear.vbuf_slot = 0;
136 }
137
138 if (!st->clear.vbuf) {
139 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
140 max_slots * sizeof(st->clear.vertices));
141 }
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[0];
161 st->clear.vertices[i][1][1] = color[1];
162 st->clear.vertices[i][1][2] = color[2];
163 st->clear.vertices[i][1][3] = color[3];
164 }
165
166 /* put vertex data into vbuf */
167 st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
168 st->clear.vbuf_slot * sizeof(st->clear.vertices),
169 sizeof(st->clear.vertices),
170 st->clear.vertices);
171
172 /* draw */
173 util_draw_vertex_buffer(pipe,
174 st->clear.vbuf,
175 st->clear.vbuf_slot * sizeof(st->clear.vertices),
176 PIPE_PRIM_TRIANGLE_FAN,
177 4, /* verts */
178 2); /* attribs/vert */
179
180 /* Increment slot */
181 st->clear.vbuf_slot++;
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(GLcontext *ctx,
193 GLboolean color, GLboolean depth, GLboolean stencil)
194 {
195 struct st_context *st = ctx->st;
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.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
229 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
230 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
231 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
232 if (color) {
233 if (ctx->Color.ColorMask[0])
234 blend.colormask |= PIPE_MASK_R;
235 if (ctx->Color.ColorMask[1])
236 blend.colormask |= PIPE_MASK_G;
237 if (ctx->Color.ColorMask[2])
238 blend.colormask |= PIPE_MASK_B;
239 if (ctx->Color.ColorMask[3])
240 blend.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 if (depth) {
252 depth_stencil.depth.enabled = 1;
253 depth_stencil.depth.writemask = 1;
254 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
255 }
256
257 if (stencil) {
258 depth_stencil.stencil[0].enabled = 1;
259 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
260 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
261 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
262 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
263 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
264 depth_stencil.stencil[0].valuemask = 0xff;
265 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
266 }
267
268 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
269 }
270
271 cso_set_rasterizer(st->cso_context, &st->clear.raster);
272
273 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
274 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
275
276 /* draw quad matching scissor rect (XXX verify coord round-off) */
277 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
278
279 /* Restore pipe state */
280 cso_restore_blend(st->cso_context);
281 cso_restore_depth_stencil_alpha(st->cso_context);
282 cso_restore_rasterizer(st->cso_context);
283 cso_restore_fragment_shader(st->cso_context);
284 cso_restore_vertex_shader(st->cso_context);
285 }
286
287
288 /**
289 * Determine if we need to clear the depth buffer by drawing a quad.
290 */
291 static INLINE GLboolean
292 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
293 {
294 if (ctx->Scissor.Enabled &&
295 (ctx->Scissor.X != 0 ||
296 ctx->Scissor.Y != 0 ||
297 ctx->Scissor.Width < rb->Width ||
298 ctx->Scissor.Height < rb->Height))
299 return TRUE;
300
301 if (!ctx->Color.ColorMask[0] ||
302 !ctx->Color.ColorMask[1] ||
303 !ctx->Color.ColorMask[2] ||
304 !ctx->Color.ColorMask[3])
305 return TRUE;
306
307 return FALSE;
308 }
309
310
311 static INLINE GLboolean
312 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
313 {
314 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
315 GLboolean maskStencil
316 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
317
318 if (ctx->Scissor.Enabled &&
319 (ctx->Scissor.X != 0 ||
320 ctx->Scissor.Y != 0 ||
321 ctx->Scissor.Width < rb->Width ||
322 ctx->Scissor.Height < rb->Height))
323 return TRUE;
324
325 if (maskStencil)
326 return TRUE;
327
328 return FALSE;
329 }
330
331
332 /**
333 * Determine if we need to clear the depth buffer by drawing a quad.
334 */
335 static INLINE GLboolean
336 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
337 {
338 const struct st_renderbuffer *strb = st_renderbuffer(rb);
339 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
340
341 if (ctx->Scissor.Enabled &&
342 (ctx->Scissor.X != 0 ||
343 ctx->Scissor.Y != 0 ||
344 ctx->Scissor.Width < rb->Width ||
345 ctx->Scissor.Height < rb->Height))
346 return TRUE;
347
348 if (isDS &&
349 ctx->DrawBuffer->Visual.stencilBits > 0)
350 return TRUE;
351
352 return FALSE;
353 }
354
355
356 /**
357 * Determine if we need to clear the stencil buffer by drawing a quad.
358 */
359 static INLINE GLboolean
360 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
361 {
362 const struct st_renderbuffer *strb = st_renderbuffer(rb);
363 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
364 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
365 const GLboolean maskStencil
366 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
367
368 if (maskStencil)
369 return TRUE;
370
371 if (ctx->Scissor.Enabled &&
372 (ctx->Scissor.X != 0 ||
373 ctx->Scissor.Y != 0 ||
374 ctx->Scissor.Width < rb->Width ||
375 ctx->Scissor.Height < rb->Height))
376 return TRUE;
377
378 /* This is correct, but it is necessary to look at the depth clear
379 * value held in the surface when it comes time to issue the clear,
380 * rather than taking depth and stencil clear values from the
381 * current state.
382 */
383 if (isDS &&
384 ctx->DrawBuffer->Visual.depthBits > 0)
385 return TRUE;
386
387 return FALSE;
388 }
389
390
391
392 void st_flush_clear( struct st_context *st )
393 {
394 /* Release vertex buffer to avoid synchronous rendering if we were
395 * to map it in the next frame.
396 */
397 pipe_buffer_reference(&st->clear.vbuf, NULL);
398 st->clear.vbuf_slot = 0;
399 }
400
401
402
403 /**
404 * Called via ctx->Driver.Clear()
405 * XXX: doesn't pick up the differences between front/back/left/right
406 * clears. Need to sort that out...
407 */
408 static void st_clear(GLcontext *ctx, GLbitfield mask)
409 {
410 static const GLbitfield BUFFER_BITS_DS
411 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
412 struct st_context *st = ctx->st;
413 struct gl_renderbuffer *depthRb
414 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
415 struct gl_renderbuffer *stencilRb
416 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
417 GLbitfield quad_buffers = 0;
418 GLbitfield clear_buffers = 0;
419 GLuint i;
420
421 /* This makes sure the pipe has the latest scissor, etc values */
422 st_validate_state( st );
423
424 if (mask & BUFFER_BITS_COLOR) {
425 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
426 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
427
428 if (mask & (1 << b)) {
429 struct gl_renderbuffer *rb
430 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
431 struct st_renderbuffer *strb;
432
433 assert(rb);
434
435 strb = st_renderbuffer(rb);
436
437 if (!strb->surface)
438 continue;
439
440 if (check_clear_color_with_quad( ctx, rb ))
441 quad_buffers |= PIPE_CLEAR_COLOR;
442 else
443 clear_buffers |= PIPE_CLEAR_COLOR;
444 }
445 }
446 }
447
448 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
449 /* clearing combined depth + stencil */
450 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
451
452 if (strb->surface) {
453 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
454 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
455 else
456 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
457 }
458 }
459 else {
460 /* separate depth/stencil clears */
461 if (mask & BUFFER_BIT_DEPTH) {
462 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
463
464 if (strb->surface) {
465 if (check_clear_depth_with_quad(ctx, depthRb))
466 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
467 else
468 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
469 }
470 }
471 if (mask & BUFFER_BIT_STENCIL) {
472 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
473
474 if (strb->surface) {
475 if (check_clear_stencil_with_quad(ctx, stencilRb))
476 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
477 else
478 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
479 }
480 }
481 }
482
483 /*
484 * If we're going to use clear_with_quad() for any reason, use it for
485 * everything possible.
486 */
487 if (quad_buffers) {
488 quad_buffers |= clear_buffers;
489 clear_with_quad(ctx,
490 quad_buffers & PIPE_CLEAR_COLOR,
491 mask & BUFFER_BIT_DEPTH,
492 mask & BUFFER_BIT_STENCIL);
493 } else if (clear_buffers)
494 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
495 ctx->Depth.Clear, ctx->Stencil.Clear);
496
497 if (mask & BUFFER_BIT_ACCUM)
498 st_clear_accum_buffer(ctx,
499 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
500 }
501
502
503 void st_init_clear_functions(struct dd_function_table *functions)
504 {
505 functions->Clear = st_clear;
506 }