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