Merge branch 'glsl-to-tgsi'
[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 "program/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_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 GLfloat color[4])
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 /* positions */
169 st->clear.vertices[0][0][0] = x0;
170 st->clear.vertices[0][0][1] = y0;
171
172 st->clear.vertices[1][0][0] = x1;
173 st->clear.vertices[1][0][1] = y0;
174
175 st->clear.vertices[2][0][0] = x1;
176 st->clear.vertices[2][0][1] = y1;
177
178 st->clear.vertices[3][0][0] = x0;
179 st->clear.vertices[3][0][1] = y1;
180
181 /* same for all verts: */
182 for (i = 0; i < 4; i++) {
183 st->clear.vertices[i][0][2] = z;
184 st->clear.vertices[i][0][3] = 1.0;
185 st->clear.vertices[i][1][0] = color[0];
186 st->clear.vertices[i][1][1] = color[1];
187 st->clear.vertices[i][1][2] = color[2];
188 st->clear.vertices[i][1][3] = color[3];
189 }
190
191 /* put vertex data into vbuf */
192 pipe_buffer_write_nooverlap(st->pipe, st->clear.vbuf,
193 st->clear.vbuf_slot
194 * sizeof(st->clear.vertices),
195 sizeof(st->clear.vertices),
196 st->clear.vertices);
197
198 /* draw */
199 util_draw_vertex_buffer(pipe,
200 st->cso_context,
201 st->clear.vbuf,
202 st->clear.vbuf_slot * sizeof(st->clear.vertices),
203 PIPE_PRIM_TRIANGLE_FAN,
204 4, /* verts */
205 2); /* attribs/vert */
206
207 /* Increment slot */
208 st->clear.vbuf_slot++;
209 }
210
211
212
213 /**
214 * Do glClear by drawing a quadrilateral.
215 * The vertices of the quad will be computed from the
216 * ctx->DrawBuffer->_X/Ymin/max fields.
217 */
218 static void
219 clear_with_quad(struct gl_context *ctx,
220 GLboolean color, GLboolean depth, GLboolean stencil)
221 {
222 struct st_context *st = st_context(ctx);
223 const struct gl_framebuffer *fb = ctx->DrawBuffer;
224 const GLfloat fb_width = (GLfloat) fb->Width;
225 const GLfloat fb_height = (GLfloat) fb->Height;
226 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
227 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
228 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
229 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
230 float clearColor[4];
231
232 /*
233 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
234 color ? "color, " : "",
235 depth ? "depth, " : "",
236 stencil ? "stencil" : "",
237 x0, y0,
238 x1, y1);
239 */
240
241 cso_save_blend(st->cso_context);
242 cso_save_stencil_ref(st->cso_context);
243 cso_save_depth_stencil_alpha(st->cso_context);
244 cso_save_rasterizer(st->cso_context);
245 cso_save_viewport(st->cso_context);
246 cso_save_clip(st->cso_context);
247 cso_save_fragment_shader(st->cso_context);
248 cso_save_vertex_shader(st->cso_context);
249 cso_save_vertex_elements(st->cso_context);
250 cso_save_vertex_buffers(st->cso_context);
251
252 /* blend state: RGBA masking */
253 {
254 struct pipe_blend_state blend;
255 memset(&blend, 0, sizeof(blend));
256 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
257 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
258 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
259 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
260 if (color) {
261 if (ctx->Color.ColorMask[0][0])
262 blend.rt[0].colormask |= PIPE_MASK_R;
263 if (ctx->Color.ColorMask[0][1])
264 blend.rt[0].colormask |= PIPE_MASK_G;
265 if (ctx->Color.ColorMask[0][2])
266 blend.rt[0].colormask |= PIPE_MASK_B;
267 if (ctx->Color.ColorMask[0][3])
268 blend.rt[0].colormask |= PIPE_MASK_A;
269 if (st->ctx->Color.DitherFlag)
270 blend.dither = 1;
271 }
272 cso_set_blend(st->cso_context, &blend);
273 }
274
275 /* depth_stencil state: always pass/set to ref value */
276 {
277 struct pipe_depth_stencil_alpha_state depth_stencil;
278 memset(&depth_stencil, 0, sizeof(depth_stencil));
279 if (depth) {
280 depth_stencil.depth.enabled = 1;
281 depth_stencil.depth.writemask = 1;
282 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
283 }
284
285 if (stencil) {
286 struct pipe_stencil_ref stencil_ref;
287 memset(&stencil_ref, 0, sizeof(stencil_ref));
288 depth_stencil.stencil[0].enabled = 1;
289 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
290 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
291 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
292 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
293 depth_stencil.stencil[0].valuemask = 0xff;
294 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
295 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
296 cso_set_stencil_ref(st->cso_context, &stencil_ref);
297 }
298
299 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
300 }
301
302 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
303
304 cso_set_rasterizer(st->cso_context, &st->clear.raster);
305
306 /* viewport state: viewport matching window dims */
307 {
308 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
309 struct pipe_viewport_state vp;
310 vp.scale[0] = 0.5f * fb_width;
311 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
312 vp.scale[2] = 1.0f;
313 vp.scale[3] = 1.0f;
314 vp.translate[0] = 0.5f * fb_width;
315 vp.translate[1] = 0.5f * fb_height;
316 vp.translate[2] = 0.0f;
317 vp.translate[3] = 0.0f;
318 cso_set_viewport(st->cso_context, &vp);
319 }
320
321 cso_set_clip(st->cso_context, &st->clear.clip);
322 set_fragment_shader(st);
323 set_vertex_shader(st);
324
325 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
326 st_translate_color(ctx->Color.ClearColorUnclamped,
327 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
328 clearColor);
329 }
330
331 /* draw quad matching scissor rect */
332 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, clearColor);
333
334 /* Restore pipe state */
335 cso_restore_blend(st->cso_context);
336 cso_restore_stencil_ref(st->cso_context);
337 cso_restore_depth_stencil_alpha(st->cso_context);
338 cso_restore_rasterizer(st->cso_context);
339 cso_restore_viewport(st->cso_context);
340 cso_restore_clip(st->cso_context);
341 cso_restore_fragment_shader(st->cso_context);
342 cso_restore_vertex_shader(st->cso_context);
343 cso_restore_vertex_elements(st->cso_context);
344 cso_restore_vertex_buffers(st->cso_context);
345 }
346
347
348 /**
349 * Determine if we need to clear the depth buffer by drawing a quad.
350 */
351 static INLINE GLboolean
352 check_clear_color_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
353 {
354 if (ctx->Scissor.Enabled &&
355 (ctx->Scissor.X != 0 ||
356 ctx->Scissor.Y != 0 ||
357 ctx->Scissor.Width < rb->Width ||
358 ctx->Scissor.Height < rb->Height))
359 return GL_TRUE;
360
361 if (!ctx->Color.ColorMask[0][0] ||
362 !ctx->Color.ColorMask[0][1] ||
363 !ctx->Color.ColorMask[0][2] ||
364 !ctx->Color.ColorMask[0][3])
365 return GL_TRUE;
366
367 return GL_FALSE;
368 }
369
370
371 /**
372 * Determine if we need to clear the combiend depth/stencil buffer by
373 * drawing a quad.
374 */
375 static INLINE GLboolean
376 check_clear_depth_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb)
377 {
378 const GLuint stencilMax = 0xff;
379 GLboolean maskStencil
380 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
381
382 assert(rb->Format == MESA_FORMAT_S8 ||
383 rb->Format == MESA_FORMAT_Z24_S8 ||
384 rb->Format == MESA_FORMAT_S8_Z24 ||
385 rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
386
387 if (ctx->Scissor.Enabled &&
388 (ctx->Scissor.X != 0 ||
389 ctx->Scissor.Y != 0 ||
390 ctx->Scissor.Width < rb->Width ||
391 ctx->Scissor.Height < rb->Height))
392 return GL_TRUE;
393
394 if (maskStencil)
395 return GL_TRUE;
396
397 return GL_FALSE;
398 }
399
400
401 /**
402 * Determine if we need to clear the depth buffer by drawing a quad.
403 */
404 static INLINE GLboolean
405 check_clear_depth_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
406 boolean ds_separate)
407 {
408 const struct st_renderbuffer *strb = st_renderbuffer(rb);
409 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
410
411 if (ctx->Scissor.Enabled &&
412 (ctx->Scissor.X != 0 ||
413 ctx->Scissor.Y != 0 ||
414 ctx->Scissor.Width < rb->Width ||
415 ctx->Scissor.Height < rb->Height))
416 return GL_TRUE;
417
418 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.stencilBits > 0)
419 return GL_TRUE;
420
421 return GL_FALSE;
422 }
423
424
425 /**
426 * Determine if we need to clear the stencil buffer by drawing a quad.
427 */
428 static INLINE GLboolean
429 check_clear_stencil_with_quad(struct gl_context *ctx, struct gl_renderbuffer *rb,
430 boolean ds_separate)
431 {
432 const struct st_renderbuffer *strb = st_renderbuffer(rb);
433 const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
434 const GLuint stencilMax = 0xff;
435 const GLboolean maskStencil
436 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
437
438 assert(rb->Format == MESA_FORMAT_S8 ||
439 rb->Format == MESA_FORMAT_Z24_S8 ||
440 rb->Format == MESA_FORMAT_S8_Z24 ||
441 rb->Format == MESA_FORMAT_Z32_FLOAT_X24S8);
442
443 if (maskStencil)
444 return GL_TRUE;
445
446 if (ctx->Scissor.Enabled &&
447 (ctx->Scissor.X != 0 ||
448 ctx->Scissor.Y != 0 ||
449 ctx->Scissor.Width < rb->Width ||
450 ctx->Scissor.Height < rb->Height))
451 return GL_TRUE;
452
453 /* This is correct, but it is necessary to look at the depth clear
454 * value held in the surface when it comes time to issue the clear,
455 * rather than taking depth and stencil clear values from the
456 * current state.
457 */
458 if (!ds_separate && isDS && ctx->DrawBuffer->Visual.depthBits > 0)
459 return GL_TRUE;
460
461 return GL_FALSE;
462 }
463
464
465
466 /**
467 * Called when we need to flush.
468 */
469 void
470 st_flush_clear(struct st_context *st)
471 {
472 /* Release vertex buffer to avoid synchronous rendering if we were
473 * to map it in the next frame.
474 */
475 pipe_resource_reference(&st->clear.vbuf, NULL);
476 st->clear.vbuf_slot = 0;
477 }
478
479
480
481 /**
482 * Called via ctx->Driver.Clear()
483 */
484 static void
485 st_Clear(struct gl_context *ctx, GLbitfield mask)
486 {
487 static const GLbitfield BUFFER_BITS_DS
488 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
489 struct st_context *st = st_context(ctx);
490 struct gl_renderbuffer *depthRb
491 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
492 struct gl_renderbuffer *stencilRb
493 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
494 GLbitfield quad_buffers = 0x0;
495 GLbitfield clear_buffers = 0x0;
496 GLuint i;
497
498 /* This makes sure the pipe has the latest scissor, etc values */
499 st_validate_state( st );
500
501 if (mask & BUFFER_BITS_COLOR) {
502 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
503 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
504
505 if (mask & (1 << b)) {
506 struct gl_renderbuffer *rb
507 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
508 struct st_renderbuffer *strb = st_renderbuffer(rb);
509
510 if (!strb || !strb->surface)
511 continue;
512
513 if (check_clear_color_with_quad( ctx, rb ))
514 quad_buffers |= PIPE_CLEAR_COLOR;
515 else
516 clear_buffers |= PIPE_CLEAR_COLOR;
517 }
518 }
519 }
520
521 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
522 /* clearing combined depth + stencil */
523 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
524
525 if (strb->surface) {
526 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
527 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
528 else
529 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
530 }
531 }
532 else {
533 /* separate depth/stencil clears */
534 /* I don't think truly separate buffers are actually possible in gallium or hw? */
535 if (mask & BUFFER_BIT_DEPTH) {
536 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
537
538 if (strb->surface) {
539 if (check_clear_depth_with_quad(ctx, depthRb,
540 st->clear.enable_ds_separate))
541 quad_buffers |= PIPE_CLEAR_DEPTH;
542 else
543 clear_buffers |= PIPE_CLEAR_DEPTH;
544 }
545 }
546 if (mask & BUFFER_BIT_STENCIL) {
547 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
548
549 if (strb->surface) {
550 if (check_clear_stencil_with_quad(ctx, stencilRb,
551 st->clear.enable_ds_separate))
552 quad_buffers |= PIPE_CLEAR_STENCIL;
553 else
554 clear_buffers |= PIPE_CLEAR_STENCIL;
555 }
556 }
557 }
558
559 /*
560 * If we're going to use clear_with_quad() for any reason, use it for
561 * everything possible.
562 */
563 if (quad_buffers) {
564 quad_buffers |= clear_buffers;
565 clear_with_quad(ctx,
566 quad_buffers & PIPE_CLEAR_COLOR,
567 quad_buffers & PIPE_CLEAR_DEPTH,
568 quad_buffers & PIPE_CLEAR_STENCIL);
569 } else if (clear_buffers) {
570 /* driver cannot know it can clear everything if the buffer
571 * is a combined depth/stencil buffer but this wasn't actually
572 * required from the visual. Hence fix this up to avoid potential
573 * read-modify-write in the driver.
574 */
575 float clearColor[4];
576
577 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) &&
578 ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
579 (depthRb == stencilRb) &&
580 (ctx->DrawBuffer->Visual.depthBits == 0 ||
581 ctx->DrawBuffer->Visual.stencilBits == 0))
582 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
583
584 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
585 st_translate_color(ctx->Color.ClearColor,
586 ctx->DrawBuffer->_ColorDrawBuffers[0]->_BaseFormat,
587 clearColor);
588 }
589
590 st->pipe->clear(st->pipe, clear_buffers, ctx->Color.ClearColorUnclamped,
591 ctx->Depth.Clear, ctx->Stencil.Clear);
592 }
593 if (mask & BUFFER_BIT_ACCUM)
594 st_clear_accum_buffer(ctx,
595 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
596 }
597
598
599 void
600 st_init_clear_functions(struct dd_function_table *functions)
601 {
602 functions->Clear = st_Clear;
603 }