mesa: rewrite accum buffer support
[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 "program/prog_instruction.h"
41 #include "st_context.h"
42 #include "st_atom.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
45 #include "st_format.h"
46 #include "st_program.h"
47
48 #include "pipe/p_context.h"
49 #include "pipe/p_shader_tokens.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_screen *pscreen = st->pipe->screen;
67
68 memset(&st->clear, 0, sizeof(st->clear));
69
70 st->clear.raster.gl_rasterization_rules = 1;
71 st->clear.enable_ds_separate = pscreen->get_param(pscreen, PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE);
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 if (st->clear.vbuf) {
90 pipe_resource_reference(&st->clear.vbuf, NULL);
91 st->clear.vbuf = NULL;
92 }
93 }
94
95
96 /**
97 * Helper function to set the fragment shaders.
98 */
99 static INLINE void
100 set_fragment_shader(struct st_context *st)
101 {
102 if (!st->clear.fs)
103 st->clear.fs = util_make_fragment_passthrough_shader(st->pipe);
104
105 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
106 }
107
108
109 /**
110 * Helper function to set the vertex shader.
111 */
112 static INLINE void
113 set_vertex_shader(struct st_context *st)
114 {
115 /* vertex shader - still required to provide the linkage between
116 * fragment shader input semantics and vertex_element/buffers.
117 */
118 if (!st->clear.vs)
119 {
120 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
121 TGSI_SEMANTIC_COLOR };
122 const uint semantic_indexes[] = { 0, 0 };
123 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
124 semantic_names,
125 semantic_indexes);
126 }
127
128 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
129 }
130
131
132 /**
133 * Draw a screen-aligned quadrilateral.
134 * Coords are clip coords with y=0=bottom.
135 */
136 static void
137 draw_quad(struct st_context *st,
138 float x0, float y0, float x1, float y1, GLfloat z,
139 const union pipe_color_union *color)
140 {
141 struct pipe_context *pipe = st->pipe;
142
143 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
144 * no_flush) updates to buffers where we know there is no conflict
145 * with previous data. Currently using max_slots > 1 will cause
146 * synchronous rendering if the driver flushes its command buffers
147 * between one bitmap and the next. Our flush hook below isn't
148 * sufficient to catch this as the driver doesn't tell us when it
149 * flushes its own command buffers. Until this gets fixed, pay the
150 * price of allocating a new buffer for each bitmap cache-flush to
151 * avoid synchronous rendering.
152 */
153 const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
154 GLuint i;
155
156 if (st->clear.vbuf_slot >= max_slots) {
157 pipe_resource_reference(&st->clear.vbuf, NULL);
158 st->clear.vbuf_slot = 0;
159 }
160
161 if (!st->clear.vbuf) {
162 st->clear.vbuf = pipe_buffer_create(pipe->screen,
163 PIPE_BIND_VERTEX_BUFFER,
164 PIPE_USAGE_STREAM,
165 max_slots * sizeof(st->clear.vertices));
166 }
167
168 if (!st->clear.vbuf) {
169 /* ran out of memory */
170 return;
171 }
172
173 /* positions */
174 st->clear.vertices[0][0][0] = x0;
175 st->clear.vertices[0][0][1] = y0;
176
177 st->clear.vertices[1][0][0] = x1;
178 st->clear.vertices[1][0][1] = y0;
179
180 st->clear.vertices[2][0][0] = x1;
181 st->clear.vertices[2][0][1] = y1;
182
183 st->clear.vertices[3][0][0] = x0;
184 st->clear.vertices[3][0][1] = y1;
185
186 /* same for all verts: */
187 for (i = 0; i < 4; i++) {
188 st->clear.vertices[i][0][2] = z;
189 st->clear.vertices[i][0][3] = 1.0;
190 st->clear.vertices[i][1][0] = color->f[0];
191 st->clear.vertices[i][1][1] = color->f[1];
192 st->clear.vertices[i][1][2] = color->f[2];
193 st->clear.vertices[i][1][3] = color->f[3];
194 }
195
196 /* put vertex data into vbuf */
197 pipe_buffer_write_nooverlap(st->pipe, st->clear.vbuf,
198 st->clear.vbuf_slot
199 * sizeof(st->clear.vertices),
200 sizeof(st->clear.vertices),
201 st->clear.vertices);
202
203 /* draw */
204 util_draw_vertex_buffer(pipe,
205 st->cso_context,
206 st->clear.vbuf,
207 st->clear.vbuf_slot * sizeof(st->clear.vertices),
208 PIPE_PRIM_TRIANGLE_FAN,
209 4, /* verts */
210 2); /* attribs/vert */
211
212 /* Increment slot */
213 st->clear.vbuf_slot++;
214 }
215
216
217
218 /**
219 * Do glClear by drawing a quadrilateral.
220 * The vertices of the quad will be computed from the
221 * ctx->DrawBuffer->_X/Ymin/max fields.
222 */
223 static void
224 clear_with_quad(struct gl_context *ctx,
225 GLboolean color, GLboolean depth, GLboolean stencil)
226 {
227 struct st_context *st = st_context(ctx);
228 const struct gl_framebuffer *fb = ctx->DrawBuffer;
229 const GLfloat fb_width = (GLfloat) fb->Width;
230 const GLfloat fb_height = (GLfloat) fb->Height;
231 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
232 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
233 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
234 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
235 union pipe_color_union clearColor;
236
237 /*
238 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
239 color ? "color, " : "",
240 depth ? "depth, " : "",
241 stencil ? "stencil" : "",
242 x0, y0,
243 x1, y1);
244 */
245
246 cso_save_blend(st->cso_context);
247 cso_save_stencil_ref(st->cso_context);
248 cso_save_depth_stencil_alpha(st->cso_context);
249 cso_save_rasterizer(st->cso_context);
250 cso_save_viewport(st->cso_context);
251 cso_save_clip(st->cso_context);
252 cso_save_fragment_shader(st->cso_context);
253 cso_save_vertex_shader(st->cso_context);
254 cso_save_geometry_shader(st->cso_context);
255 cso_save_vertex_elements(st->cso_context);
256 cso_save_vertex_buffers(st->cso_context);
257
258 /* blend state: RGBA masking */
259 {
260 struct pipe_blend_state blend;
261 memset(&blend, 0, sizeof(blend));
262 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
263 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
264 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
265 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
266 if (color) {
267 if (ctx->Color.ColorMask[0][0])
268 blend.rt[0].colormask |= PIPE_MASK_R;
269 if (ctx->Color.ColorMask[0][1])
270 blend.rt[0].colormask |= PIPE_MASK_G;
271 if (ctx->Color.ColorMask[0][2])
272 blend.rt[0].colormask |= PIPE_MASK_B;
273 if (ctx->Color.ColorMask[0][3])
274 blend.rt[0].colormask |= PIPE_MASK_A;
275 if (st->ctx->Color.DitherFlag)
276 blend.dither = 1;
277 }
278 cso_set_blend(st->cso_context, &blend);
279 }
280
281 /* depth_stencil state: always pass/set to ref value */
282 {
283 struct pipe_depth_stencil_alpha_state depth_stencil;
284 memset(&depth_stencil, 0, sizeof(depth_stencil));
285 if (depth) {
286 depth_stencil.depth.enabled = 1;
287 depth_stencil.depth.writemask = 1;
288 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
289 }
290
291 if (stencil) {
292 struct pipe_stencil_ref stencil_ref;
293 memset(&stencil_ref, 0, sizeof(stencil_ref));
294 depth_stencil.stencil[0].enabled = 1;
295 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
296 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
297 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
298 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
299 depth_stencil.stencil[0].valuemask = 0xff;
300 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
301 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
302 cso_set_stencil_ref(st->cso_context, &stencil_ref);
303 }
304
305 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
306 }
307
308 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
309
310 cso_set_rasterizer(st->cso_context, &st->clear.raster);
311
312 /* viewport state: viewport matching window dims */
313 {
314 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
315 struct pipe_viewport_state vp;
316 vp.scale[0] = 0.5f * fb_width;
317 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
318 vp.scale[2] = 1.0f;
319 vp.scale[3] = 1.0f;
320 vp.translate[0] = 0.5f * fb_width;
321 vp.translate[1] = 0.5f * fb_height;
322 vp.translate[2] = 0.0f;
323 vp.translate[3] = 0.0f;
324 cso_set_viewport(st->cso_context, &vp);
325 }
326
327 cso_set_clip(st->cso_context, &st->clear.clip);
328 set_fragment_shader(st);
329 set_vertex_shader(st);
330 cso_set_geometry_shader_handle(st->cso_context, NULL);
331
332 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
333 st_translate_color(ctx->Color.ClearColor.f,
334 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
335 clearColor.f);
336 }
337
338 /* draw quad matching scissor rect */
339 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, &clearColor);
340
341 /* Restore pipe state */
342 cso_restore_blend(st->cso_context);
343 cso_restore_stencil_ref(st->cso_context);
344 cso_restore_depth_stencil_alpha(st->cso_context);
345 cso_restore_rasterizer(st->cso_context);
346 cso_restore_viewport(st->cso_context);
347 cso_restore_clip(st->cso_context);
348 cso_restore_fragment_shader(st->cso_context);
349 cso_restore_vertex_shader(st->cso_context);
350 cso_restore_geometry_shader(st->cso_context);
351 cso_restore_vertex_elements(st->cso_context);
352 cso_restore_vertex_buffers(st->cso_context);
353 }
354
355
356 /**
357 * Determine if we need to clear the depth buffer by drawing a quad.
358 */
359 static INLINE GLboolean
360 check_clear_color_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
361 {
362 if (ctx->Scissor.Enabled &&
363 (ctx->Scissor.X != 0 ||
364 ctx->Scissor.Y != 0 ||
365 ctx->Scissor.Width < rb->Width ||
366 ctx->Scissor.Height < rb->Height))
367 return GL_TRUE;
368
369 if (!ctx->Color.ColorMask[0][0] ||
370 !ctx->Color.ColorMask[0][1] ||
371 !ctx->Color.ColorMask[0][2] ||
372 !ctx->Color.ColorMask[0][3])
373 return GL_TRUE;
374
375 return GL_FALSE;
376 }
377
378
379 /**
380 * Determine if we need to clear the combiend depth/stencil buffer by
381 * drawing a quad.
382 */
383 static INLINE GLboolean
384 check_clear_depth_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
385 {
386 const GLuint stencilMax = 0xff;
387 GLboolean maskStencil
388 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
389
390 assert(rb->Format == MESA_FORMAT_S8 ||
391 rb->Format == MESA_FORMAT_Z24_S8 ||
392 rb->Format == MESA_FORMAT_S8_Z24 ||
393 rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
394
395 if (ctx->Scissor.Enabled &&
396 (ctx->Scissor.X != 0 ||
397 ctx->Scissor.Y != 0 ||
398 ctx->Scissor.Width < rb->Width ||
399 ctx->Scissor.Height < rb->Height))
400 return GL_TRUE;
401
402 if (maskStencil)
403 return GL_TRUE;
404
405 return GL_FALSE;
406 }
407
408
409 /**
410 * Determine if we need to clear the depth buffer by drawing a quad.
411 */
412 static INLINE GLboolean
413 check_clear_depth_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
414 boolean ds_separate)
415 {
416 const struct st_renderbuffer *strb = st_renderbuffer(rb);
417 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
418
419 if (ctx->Scissor.Enabled &&
420 (ctx->Scissor.X != 0 ||
421 ctx->Scissor.Y != 0 ||
422 ctx->Scissor.Width < rb->Width ||
423 ctx->Scissor.Height < rb->Height))
424 return GL_TRUE;
425
426 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.stencilBits > 0)
427 return GL_TRUE;
428
429 return GL_FALSE;
430 }
431
432
433 /**
434 * Determine if we need to clear the stencil buffer by drawing a quad.
435 */
436 static INLINE GLboolean
437 check_clear_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
438 boolean ds_separate)
439 {
440 const struct st_renderbuffer *strb = st_renderbuffer(rb);
441 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
442 const GLuint stencilMax = 0xff;
443 const GLboolean maskStencil
444 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
445
446 assert(rb->Format == MESA_FORMAT_S8 ||
447 rb->Format == MESA_FORMAT_Z24_S8 ||
448 rb->Format == MESA_FORMAT_S8_Z24 ||
449 rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
450
451 if (maskStencil)
452 return GL_TRUE;
453
454 if (ctx->Scissor.Enabled &&
455 (ctx->Scissor.X != 0 ||
456 ctx->Scissor.Y != 0 ||
457 ctx->Scissor.Width < rb->Width ||
458 ctx->Scissor.Height < rb->Height))
459 return GL_TRUE;
460
461 /* This is correct, but it is necessary to look at the depth clear
462 * value held in the surface when it comes time to issue the clear,
463 * rather than taking depth and stencil clear values from the
464 * current state.
465 */
466 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.depthBits > 0)
467 return GL_TRUE;
468
469 return GL_FALSE;
470 }
471
472
473
474 /**
475 * Called when we need to flush.
476 */
477 void
478 st_flush_clear(struct st_context *st)
479 {
480 /* Release vertex buffer to avoid synchronous rendering if we were
481 * to map it in the next frame.
482 */
483 pipe_resource_reference(&st->clear.vbuf, NULL);
484 st->clear.vbuf_slot = 0;
485 }
486
487
488
489 /**
490 * Called via ctx->Driver.Clear()
491 */
492 static void
493 st_Clear(struct gl_context *ctx, GLbitfield mask)
494 {
495 static const GLbitfield BUFFER_BITS_DS
496 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
497 struct st_context *st = st_context(ctx);
498 struct gl_renderbuffer *depthRb
499 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
500 struct gl_renderbuffer *stencilRb
501 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
502 GLbitfield quad_buffers = 0x0;
503 GLbitfield clear_buffers = 0x0;
504 GLuint i;
505
506 /* This makes sure the pipe has the latest scissor, etc values */
507 st_validate_state( st );
508
509 if (mask & BUFFER_BITS_COLOR) {
510 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
511 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
512
513 if (mask & (1 << b)) {
514 struct gl_renderbuffer *rb
515 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
516 struct st_renderbuffer *strb = st_renderbuffer(rb);
517
518 if (!strb || !strb->surface)
519 continue;
520
521 if (check_clear_color_with_quad( ctx, rb ))
522 quad_buffers |= PIPE_CLEAR_COLOR;
523 else
524 clear_buffers |= PIPE_CLEAR_COLOR;
525 }
526 }
527 }
528
529 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
530 /* clearing combined depth + stencil */
531 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
532
533 if (strb->surface) {
534 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
535 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
536 else
537 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
538 }
539 }
540 else {
541 /* separate depth/stencil clears */
542 /* I don't think truly separate buffers are actually possible in gallium or hw? */
543 if (mask & BUFFER_BIT_DEPTH) {
544 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
545
546 if (strb->surface) {
547 if (check_clear_depth_with_quad(ctx, depthRb,
548 st->clear.enable_ds_separate))
549 quad_buffers |= PIPE_CLEAR_DEPTH;
550 else
551 clear_buffers |= PIPE_CLEAR_DEPTH;
552 }
553 }
554 if (mask & BUFFER_BIT_STENCIL) {
555 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
556
557 if (strb->surface) {
558 if (check_clear_stencil_with_quad(ctx, stencilRb,
559 st->clear.enable_ds_separate))
560 quad_buffers |= PIPE_CLEAR_STENCIL;
561 else
562 clear_buffers |= PIPE_CLEAR_STENCIL;
563 }
564 }
565 }
566
567 /*
568 * If we're going to use clear_with_quad() for any reason, use it for
569 * everything possible.
570 */
571 if (quad_buffers) {
572 quad_buffers |= clear_buffers;
573 clear_with_quad(ctx,
574 quad_buffers & PIPE_CLEAR_COLOR,
575 quad_buffers & PIPE_CLEAR_DEPTH,
576 quad_buffers & PIPE_CLEAR_STENCIL);
577 } else if (clear_buffers) {
578 /* driver cannot know it can clear everything if the buffer
579 * is a combined depth/stencil buffer but this wasn't actually
580 * required from the visual. Hence fix this up to avoid potential
581 * read-modify-write in the driver.
582 */
583 union pipe_color_union clearColor;
584
585 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) &&
586 ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
587 (depthRb == stencilRb) &&
588 (ctx->DrawBuffer->Visual.depthBits == 0 ||
589 ctx->DrawBuffer->Visual.stencilBits == 0))
590 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
591
592 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
593 st_translate_color(ctx->Color.ClearColor.f,
594 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
595 clearColor.f);
596 }
597
598 st->pipe->clear(st->pipe, clear_buffers, &clearColor,
599 ctx->Depth.Clear, ctx->Stencil.Clear);
600 }
601 if (mask & BUFFER_BIT_ACCUM)
602 _mesa_clear_accum_buffer(ctx);
603 }
604
605
606 void
607 st_init_clear_functions(struct dd_function_table *functions)
608 {
609 functions->Clear = st_Clear;
610 }