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