Merge remote branch 'origin/master' into gallium_draw_llvm
[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_program.h"
46 #include "st_public.h"
47 #include "st_inlines.h"
48
49 #include "pipe/p_context.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
57 #include "cso_cache/cso_context.h"
58
59
60 /**
61 * Do per-context initialization for glClear.
62 */
63 void
64 st_init_clear(struct st_context *st)
65 {
66 struct pipe_context *pipe = st->pipe;
67
68 memset(&st->clear, 0, sizeof(st->clear));
69
70 st->clear.raster.gl_rasterization_rules = 1;
71
72 /* fragment shader state: color pass-through program */
73 st->clear.fs = util_make_fragment_passthrough_shader(pipe);
74
75 /* vertex shader state: color/position pass-through */
76 {
77 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
78 TGSI_SEMANTIC_COLOR };
79 const uint semantic_indexes[] = { 0, 0 };
80 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
81 semantic_names,
82 semantic_indexes);
83 }
84 }
85
86
87 /**
88 * Free per-context state for glClear.
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 clip coords with y=0=bottom.
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,
140 PIPE_BUFFER_USAGE_VERTEX,
141 max_slots * sizeof(st->clear.vertices));
142 }
143
144 /* positions */
145 st->clear.vertices[0][0][0] = x0;
146 st->clear.vertices[0][0][1] = y0;
147
148 st->clear.vertices[1][0][0] = x1;
149 st->clear.vertices[1][0][1] = y0;
150
151 st->clear.vertices[2][0][0] = x1;
152 st->clear.vertices[2][0][1] = y1;
153
154 st->clear.vertices[3][0][0] = x0;
155 st->clear.vertices[3][0][1] = y1;
156
157 /* same for all verts: */
158 for (i = 0; i < 4; i++) {
159 st->clear.vertices[i][0][2] = z;
160 st->clear.vertices[i][0][3] = 1.0;
161 st->clear.vertices[i][1][0] = color[0];
162 st->clear.vertices[i][1][1] = color[1];
163 st->clear.vertices[i][1][2] = color[2];
164 st->clear.vertices[i][1][3] = color[3];
165 }
166
167 /* put vertex data into vbuf */
168 st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
169 st->clear.vbuf_slot
170 * 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 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
206 /*
207 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
208 color ? "color, " : "",
209 depth ? "depth, " : "",
210 stencil ? "stencil" : "",
211 x0, y0,
212 x1, y1);
213 */
214
215 cso_save_blend(st->cso_context);
216 cso_save_stencil_ref(st->cso_context);
217 cso_save_depth_stencil_alpha(st->cso_context);
218 cso_save_rasterizer(st->cso_context);
219 cso_save_viewport(st->cso_context);
220 cso_save_clip(st->cso_context);
221 cso_save_fragment_shader(st->cso_context);
222 cso_save_vertex_shader(st->cso_context);
223 cso_save_vertex_elements(st->cso_context);
224
225 /* blend state: RGBA masking */
226 {
227 struct pipe_blend_state blend;
228 memset(&blend, 0, sizeof(blend));
229 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
230 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
231 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
232 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
233 if (color) {
234 if (ctx->Color.ColorMask[0][0])
235 blend.rt[0].colormask |= PIPE_MASK_R;
236 if (ctx->Color.ColorMask[0][1])
237 blend.rt[0].colormask |= PIPE_MASK_G;
238 if (ctx->Color.ColorMask[0][2])
239 blend.rt[0].colormask |= PIPE_MASK_B;
240 if (ctx->Color.ColorMask[0][3])
241 blend.rt[0].colormask |= PIPE_MASK_A;
242 if (st->ctx->Color.DitherFlag)
243 blend.dither = 1;
244 }
245 cso_set_blend(st->cso_context, &blend);
246 }
247
248 /* depth_stencil state: always pass/set to ref value */
249 {
250 struct pipe_depth_stencil_alpha_state depth_stencil;
251 memset(&depth_stencil, 0, sizeof(depth_stencil));
252 if (depth) {
253 depth_stencil.depth.enabled = 1;
254 depth_stencil.depth.writemask = 1;
255 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
256 }
257
258 if (stencil) {
259 struct pipe_stencil_ref stencil_ref;
260 memset(&stencil_ref, 0, sizeof(stencil_ref));
261 depth_stencil.stencil[0].enabled = 1;
262 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
263 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
264 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
265 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
266 depth_stencil.stencil[0].valuemask = 0xff;
267 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
268 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
269 cso_set_stencil_ref(st->cso_context, &stencil_ref);
270 }
271
272 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
273 }
274
275 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
276
277 cso_set_rasterizer(st->cso_context, &st->clear.raster);
278
279 /* viewport state: viewport matching window dims */
280 {
281 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
282 struct pipe_viewport_state vp;
283 vp.scale[0] = 0.5f * fb_width;
284 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
285 vp.scale[2] = 1.0f;
286 vp.scale[3] = 1.0f;
287 vp.translate[0] = 0.5f * fb_width;
288 vp.translate[1] = 0.5f * fb_height;
289 vp.translate[2] = 0.0f;
290 vp.translate[3] = 0.0f;
291 cso_set_viewport(st->cso_context, &vp);
292 }
293
294 cso_set_clip(st->cso_context, &st->clear.clip);
295 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
296 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
297
298 /* draw quad matching scissor rect (XXX verify coord round-off) */
299 draw_quad(ctx, x0, y0, x1, y1,
300 (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
301
302 /* Restore pipe state */
303 cso_restore_blend(st->cso_context);
304 cso_restore_stencil_ref(st->cso_context);
305 cso_restore_depth_stencil_alpha(st->cso_context);
306 cso_restore_rasterizer(st->cso_context);
307 cso_restore_viewport(st->cso_context);
308 cso_restore_clip(st->cso_context);
309 cso_restore_fragment_shader(st->cso_context);
310 cso_restore_vertex_shader(st->cso_context);
311 cso_restore_vertex_elements(st->cso_context);
312 }
313
314
315 /**
316 * Determine if we need to clear the depth buffer by drawing a quad.
317 */
318 static INLINE GLboolean
319 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
320 {
321 if (ctx->Scissor.Enabled &&
322 (ctx->Scissor.X != 0 ||
323 ctx->Scissor.Y != 0 ||
324 ctx->Scissor.Width < rb->Width ||
325 ctx->Scissor.Height < rb->Height))
326 return GL_TRUE;
327
328 if (!ctx->Color.ColorMask[0][0] ||
329 !ctx->Color.ColorMask[0][1] ||
330 !ctx->Color.ColorMask[0][2] ||
331 !ctx->Color.ColorMask[0][3])
332 return GL_TRUE;
333
334 return GL_FALSE;
335 }
336
337
338 /**
339 * Determine if we need to clear the combiend depth/stencil buffer by
340 * drawing a quad.
341 */
342 static INLINE GLboolean
343 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
344 {
345 const GLuint stencilMax = 0xff;
346 GLboolean maskStencil
347 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
348
349 assert(rb->Format == MESA_FORMAT_S8 ||
350 rb->Format == MESA_FORMAT_Z24_S8 ||
351 rb->Format == MESA_FORMAT_S8_Z24);
352
353 if (ctx->Scissor.Enabled &&
354 (ctx->Scissor.X != 0 ||
355 ctx->Scissor.Y != 0 ||
356 ctx->Scissor.Width < rb->Width ||
357 ctx->Scissor.Height < rb->Height))
358 return GL_TRUE;
359
360 if (maskStencil)
361 return GL_TRUE;
362
363 return GL_FALSE;
364 }
365
366
367 /**
368 * Determine if we need to clear the depth buffer by drawing a quad.
369 */
370 static INLINE GLboolean
371 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
372 {
373 const struct st_renderbuffer *strb = st_renderbuffer(rb);
374 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
375
376 if (ctx->Scissor.Enabled &&
377 (ctx->Scissor.X != 0 ||
378 ctx->Scissor.Y != 0 ||
379 ctx->Scissor.Width < rb->Width ||
380 ctx->Scissor.Height < rb->Height))
381 return GL_TRUE;
382
383 if (isDS && ctx->DrawBuffer->Visual.stencilBits > 0)
384 return GL_TRUE;
385
386 return GL_FALSE;
387 }
388
389
390 /**
391 * Determine if we need to clear the stencil buffer by drawing a quad.
392 */
393 static INLINE GLboolean
394 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
395 {
396 const struct st_renderbuffer *strb = st_renderbuffer(rb);
397 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
398 const GLuint stencilMax = 0xff;
399 const GLboolean maskStencil
400 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
401
402 assert(rb->Format == MESA_FORMAT_S8 ||
403 rb->Format == MESA_FORMAT_Z24_S8 ||
404 rb->Format == MESA_FORMAT_S8_Z24);
405
406 if (maskStencil)
407 return GL_TRUE;
408
409 if (ctx->Scissor.Enabled &&
410 (ctx->Scissor.X != 0 ||
411 ctx->Scissor.Y != 0 ||
412 ctx->Scissor.Width < rb->Width ||
413 ctx->Scissor.Height < rb->Height))
414 return GL_TRUE;
415
416 /* This is correct, but it is necessary to look at the depth clear
417 * value held in the surface when it comes time to issue the clear,
418 * rather than taking depth and stencil clear values from the
419 * current state.
420 */
421 if (isDS && ctx->DrawBuffer->Visual.depthBits > 0)
422 return GL_TRUE;
423
424 return GL_FALSE;
425 }
426
427
428
429 /**
430 * Called when we need to flush.
431 */
432 void
433 st_flush_clear(struct st_context *st)
434 {
435 /* Release vertex buffer to avoid synchronous rendering if we were
436 * to map it in the next frame.
437 */
438 pipe_buffer_reference(&st->clear.vbuf, NULL);
439 st->clear.vbuf_slot = 0;
440 }
441
442
443
444 /**
445 * Called via ctx->Driver.Clear()
446 */
447 static void
448 st_Clear(GLcontext *ctx, GLbitfield mask)
449 {
450 static const GLbitfield BUFFER_BITS_DS
451 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
452 struct st_context *st = ctx->st;
453 struct gl_renderbuffer *depthRb
454 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
455 struct gl_renderbuffer *stencilRb
456 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
457 GLbitfield quad_buffers = 0x0;
458 GLbitfield clear_buffers = 0x0;
459 GLuint i;
460
461 /* This makes sure the pipe has the latest scissor, etc values */
462 st_validate_state( st );
463
464 if (mask & BUFFER_BITS_COLOR) {
465 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
466 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
467
468 if (mask & (1 << b)) {
469 struct gl_renderbuffer *rb
470 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
471 struct st_renderbuffer *strb;
472
473 assert(rb);
474
475 strb = st_renderbuffer(rb);
476
477 if (!strb->surface)
478 continue;
479
480 if (check_clear_color_with_quad( ctx, rb ))
481 quad_buffers |= PIPE_CLEAR_COLOR;
482 else
483 clear_buffers |= PIPE_CLEAR_COLOR;
484 }
485 }
486 }
487
488 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
489 /* clearing combined depth + stencil */
490 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
491
492 if (strb->surface) {
493 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
494 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
495 else
496 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
497 }
498 }
499 else {
500 /* separate depth/stencil clears */
501 if (mask & BUFFER_BIT_DEPTH) {
502 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
503
504 if (strb->surface) {
505 if (check_clear_depth_with_quad(ctx, depthRb))
506 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
507 else
508 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
509 }
510 }
511 if (mask & BUFFER_BIT_STENCIL) {
512 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
513
514 if (strb->surface) {
515 if (check_clear_stencil_with_quad(ctx, stencilRb))
516 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
517 else
518 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
519 }
520 }
521 }
522
523 /*
524 * If we're going to use clear_with_quad() for any reason, use it for
525 * everything possible.
526 */
527 if (quad_buffers) {
528 quad_buffers |= clear_buffers;
529 clear_with_quad(ctx,
530 quad_buffers & PIPE_CLEAR_COLOR,
531 mask & BUFFER_BIT_DEPTH,
532 mask & BUFFER_BIT_STENCIL);
533 } else if (clear_buffers)
534 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
535 ctx->Depth.Clear, ctx->Stencil.Clear);
536
537 if (mask & BUFFER_BIT_ACCUM)
538 st_clear_accum_buffer(ctx,
539 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
540 }
541
542
543 void
544 st_init_clear_functions(struct dd_function_table *functions)
545 {
546 functions->Clear = st_Clear;
547 }