st/mesa: fix glClear with multiple colorbuffers and different formats
[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 "main/glformats.h"
41 #include "program/prog_instruction.h"
42 #include "st_context.h"
43 #include "st_atom.h"
44 #include "st_cb_clear.h"
45 #include "st_cb_fbo.h"
46 #include "st_format.h"
47 #include "st_program.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_shader_tokens.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_format.h"
54 #include "util/u_framebuffer.h"
55 #include "util/u_inlines.h"
56 #include "util/u_simple_shaders.h"
57 #include "util/u_draw_quad.h"
58 #include "util/u_upload_mgr.h"
59
60 #include "cso_cache/cso_context.h"
61
62
63 /**
64 * Do per-context initialization for glClear.
65 */
66 void
67 st_init_clear(struct st_context *st)
68 {
69 memset(&st->clear, 0, sizeof(st->clear));
70
71 st->clear.raster.half_pixel_center = 1;
72 st->clear.raster.bottom_edge_rule = 1;
73 st->clear.raster.depth_clip = 1;
74 }
75
76
77 /**
78 * Free per-context state for glClear.
79 */
80 void
81 st_destroy_clear(struct st_context *st)
82 {
83 if (st->clear.fs) {
84 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
85 st->clear.fs = NULL;
86 }
87 if (st->clear.vs) {
88 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
89 st->clear.vs = NULL;
90 }
91 }
92
93
94 /**
95 * Helper function to set the fragment shaders.
96 */
97 static INLINE void
98 set_fragment_shader(struct st_context *st)
99 {
100 if (!st->clear.fs)
101 st->clear.fs =
102 util_make_fragment_passthrough_shader(st->pipe, TGSI_SEMANTIC_GENERIC,
103 TGSI_INTERPOLATE_CONSTANT,
104 TRUE);
105
106 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
107 }
108
109
110 /**
111 * Helper function to set the vertex shader.
112 */
113 static INLINE void
114 set_vertex_shader(struct st_context *st)
115 {
116 /* vertex shader - still required to provide the linkage between
117 * fragment shader input semantics and vertex_element/buffers.
118 */
119 if (!st->clear.vs)
120 {
121 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
122 TGSI_SEMANTIC_GENERIC };
123 const uint semantic_indexes[] = { 0, 0 };
124 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
125 semantic_names,
126 semantic_indexes);
127 }
128
129 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
130 }
131
132
133 static void
134 set_vertex_shader_layered(struct st_context *st)
135 {
136 struct pipe_context *pipe = st->pipe;
137
138 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) ||
139 !pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER)) {
140 assert(!"Got layered clear, but the VS layer output is unsupported");
141 set_vertex_shader(st);
142 return;
143 }
144
145 if (!st->clear.vs_layered) {
146 st->clear.vs_layered = util_make_layered_clear_vertex_shader(pipe);
147 }
148
149 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs_layered);
150 }
151
152
153 /**
154 * Draw a screen-aligned quadrilateral.
155 * Coords are clip coords with y=0=bottom.
156 */
157 static void
158 draw_quad(struct st_context *st,
159 float x0, float y0, float x1, float y1, GLfloat z,
160 unsigned num_instances,
161 const union pipe_color_union *color)
162 {
163 struct cso_context *cso = st->cso_context;
164 struct pipe_vertex_buffer vb = {0};
165 GLuint i;
166 float (*vertices)[2][4]; /**< vertex pos + color */
167
168 vb.stride = 8 * sizeof(float);
169
170 if (u_upload_alloc(st->uploader, 0, 4 * sizeof(vertices[0]),
171 &vb.buffer_offset, &vb.buffer,
172 (void **) &vertices) != PIPE_OK) {
173 return;
174 }
175
176 /* positions */
177 vertices[0][0][0] = x0;
178 vertices[0][0][1] = y0;
179
180 vertices[1][0][0] = x1;
181 vertices[1][0][1] = y0;
182
183 vertices[2][0][0] = x1;
184 vertices[2][0][1] = y1;
185
186 vertices[3][0][0] = x0;
187 vertices[3][0][1] = y1;
188
189 /* same for all verts: */
190 for (i = 0; i < 4; i++) {
191 vertices[i][0][2] = z;
192 vertices[i][0][3] = 1.0;
193 vertices[i][1][0] = color->f[0];
194 vertices[i][1][1] = color->f[1];
195 vertices[i][1][2] = color->f[2];
196 vertices[i][1][3] = color->f[3];
197 }
198
199 u_upload_unmap(st->uploader);
200
201 /* draw */
202 cso_set_vertex_buffers(cso, cso_get_aux_vertex_buffer_slot(cso), 1, &vb);
203 cso_draw_arrays_instanced(cso, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
204 0, num_instances);
205 pipe_resource_reference(&vb.buffer, NULL);
206 }
207
208
209
210 /**
211 * Do glClear by drawing a quadrilateral.
212 * The vertices of the quad will be computed from the
213 * ctx->DrawBuffer->_X/Ymin/max fields.
214 */
215 static void
216 clear_with_quad(struct gl_context *ctx,
217 GLboolean color, GLboolean depth, GLboolean stencil)
218 {
219 struct st_context *st = st_context(ctx);
220 const struct gl_framebuffer *fb = ctx->DrawBuffer;
221 const GLfloat fb_width = (GLfloat) fb->Width;
222 const GLfloat fb_height = (GLfloat) fb->Height;
223 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
224 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
225 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
226 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
227 unsigned num_layers =
228 util_framebuffer_get_num_layers(&st->state.framebuffer);
229
230 /*
231 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
232 color ? "color, " : "",
233 depth ? "depth, " : "",
234 stencil ? "stencil" : "",
235 x0, y0,
236 x1, y1);
237 */
238
239 cso_save_blend(st->cso_context);
240 cso_save_stencil_ref(st->cso_context);
241 cso_save_depth_stencil_alpha(st->cso_context);
242 cso_save_rasterizer(st->cso_context);
243 cso_save_sample_mask(st->cso_context);
244 cso_save_viewport(st->cso_context);
245 cso_save_fragment_shader(st->cso_context);
246 cso_save_stream_outputs(st->cso_context);
247 cso_save_vertex_shader(st->cso_context);
248 cso_save_geometry_shader(st->cso_context);
249 cso_save_vertex_elements(st->cso_context);
250 cso_save_aux_vertex_buffer_slot(st->cso_context);
251
252 /* blend state: RGBA masking */
253 {
254 struct pipe_blend_state blend;
255 memset(&blend, 0, sizeof(blend));
256 if (color) {
257 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ?
258 ctx->DrawBuffer->_NumColorDrawBuffers : 1;
259 int i;
260
261 blend.independent_blend_enable = num_buffers > 1;
262
263 for (i = 0; i < num_buffers; i++) {
264 if (ctx->Color.ColorMask[i][0])
265 blend.rt[i].colormask |= PIPE_MASK_R;
266 if (ctx->Color.ColorMask[i][1])
267 blend.rt[i].colormask |= PIPE_MASK_G;
268 if (ctx->Color.ColorMask[i][2])
269 blend.rt[i].colormask |= PIPE_MASK_B;
270 if (ctx->Color.ColorMask[i][3])
271 blend.rt[i].colormask |= PIPE_MASK_A;
272 }
273
274 if (st->ctx->Color.DitherFlag)
275 blend.dither = 1;
276 }
277 cso_set_blend(st->cso_context, &blend);
278 }
279
280 /* depth_stencil state: always pass/set to ref value */
281 {
282 struct pipe_depth_stencil_alpha_state depth_stencil;
283 memset(&depth_stencil, 0, sizeof(depth_stencil));
284 if (depth) {
285 depth_stencil.depth.enabled = 1;
286 depth_stencil.depth.writemask = 1;
287 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
288 }
289
290 if (stencil) {
291 struct pipe_stencil_ref stencil_ref;
292 memset(&stencil_ref, 0, sizeof(stencil_ref));
293 depth_stencil.stencil[0].enabled = 1;
294 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
295 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
296 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
297 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
298 depth_stencil.stencil[0].valuemask = 0xff;
299 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
300 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
301 cso_set_stencil_ref(st->cso_context, &stencil_ref);
302 }
303
304 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
305 }
306
307 cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
308 cso_set_stream_outputs(st->cso_context, 0, NULL, 0);
309 cso_set_sample_mask(st->cso_context, ~0);
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 set_fragment_shader(st);
328 cso_set_geometry_shader_handle(st->cso_context, NULL);
329
330 if (num_layers > 1)
331 set_vertex_shader_layered(st);
332 else
333 set_vertex_shader(st);
334
335 /* We can't translate the clear color to the colorbuffer format,
336 * because different colorbuffers may have different formats.
337 */
338
339 /* draw quad matching scissor rect */
340 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, num_layers,
341 (union pipe_color_union*)&ctx->Color.ClearColor);
342
343 /* Restore pipe state */
344 cso_restore_blend(st->cso_context);
345 cso_restore_stencil_ref(st->cso_context);
346 cso_restore_depth_stencil_alpha(st->cso_context);
347 cso_restore_rasterizer(st->cso_context);
348 cso_restore_sample_mask(st->cso_context);
349 cso_restore_viewport(st->cso_context);
350 cso_restore_fragment_shader(st->cso_context);
351 cso_restore_vertex_shader(st->cso_context);
352 cso_restore_geometry_shader(st->cso_context);
353 cso_restore_vertex_elements(st->cso_context);
354 cso_restore_aux_vertex_buffer_slot(st->cso_context);
355 cso_restore_stream_outputs(st->cso_context);
356 }
357
358
359 /**
360 * Return if the scissor must be enabled during the clear.
361 */
362 static INLINE GLboolean
363 is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
364 {
365 return ctx->Scissor.Enabled &&
366 (ctx->Scissor.X > 0 ||
367 ctx->Scissor.Y > 0 ||
368 (unsigned) ctx->Scissor.Width < rb->Width ||
369 (unsigned) ctx->Scissor.Height < rb->Height);
370 }
371
372
373 /**
374 * Return if any of the color channels are masked.
375 */
376 static INLINE GLboolean
377 is_color_masked(struct gl_context *ctx, int i)
378 {
379 return !ctx->Color.ColorMask[i][0] ||
380 !ctx->Color.ColorMask[i][1] ||
381 !ctx->Color.ColorMask[i][2] ||
382 !ctx->Color.ColorMask[i][3];
383 }
384
385
386 /**
387 * Return if any of the stencil bits are masked.
388 */
389 static INLINE GLboolean
390 is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb)
391 {
392 const GLuint stencilMax = 0xff;
393
394 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
395 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
396 }
397
398
399 /**
400 * Called via ctx->Driver.Clear()
401 */
402 static void
403 st_Clear(struct gl_context *ctx, GLbitfield mask)
404 {
405 struct st_context *st = st_context(ctx);
406 struct gl_renderbuffer *depthRb
407 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
408 struct gl_renderbuffer *stencilRb
409 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
410 GLbitfield quad_buffers = 0x0;
411 GLbitfield clear_buffers = 0x0;
412 GLuint i;
413
414 /* This makes sure the pipe has the latest scissor, etc values */
415 st_validate_state( st );
416
417 if (mask & BUFFER_BITS_COLOR) {
418 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
419 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
420
421 if (mask & (1 << b)) {
422 struct gl_renderbuffer *rb
423 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
424 struct st_renderbuffer *strb = st_renderbuffer(rb);
425 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
426
427 if (!strb || !strb->surface)
428 continue;
429
430 if (is_scissor_enabled(ctx, rb) ||
431 is_color_masked(ctx, colormask_index))
432 quad_buffers |= PIPE_CLEAR_COLOR;
433 else
434 clear_buffers |= PIPE_CLEAR_COLOR;
435 }
436 }
437 }
438
439 if (mask & BUFFER_BIT_DEPTH) {
440 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
441
442 if (strb->surface) {
443 if (is_scissor_enabled(ctx, depthRb))
444 quad_buffers |= PIPE_CLEAR_DEPTH;
445 else
446 clear_buffers |= PIPE_CLEAR_DEPTH;
447 }
448 }
449 if (mask & BUFFER_BIT_STENCIL) {
450 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
451
452 if (strb->surface) {
453 if (is_scissor_enabled(ctx, stencilRb) ||
454 is_stencil_masked(ctx, stencilRb))
455 quad_buffers |= PIPE_CLEAR_STENCIL;
456 else
457 clear_buffers |= PIPE_CLEAR_STENCIL;
458 }
459 }
460
461 /*
462 * If we're going to use clear_with_quad() for any reason, use it for
463 * everything possible.
464 */
465 if (quad_buffers) {
466 quad_buffers |= clear_buffers;
467 clear_with_quad(ctx,
468 quad_buffers & PIPE_CLEAR_COLOR,
469 quad_buffers & PIPE_CLEAR_DEPTH,
470 quad_buffers & PIPE_CLEAR_STENCIL);
471 } else if (clear_buffers) {
472 /* We can't translate the clear color to the colorbuffer format,
473 * because different colorbuffers may have different formats.
474 */
475 st->pipe->clear(st->pipe, clear_buffers,
476 (union pipe_color_union*)&ctx->Color.ClearColor,
477 ctx->Depth.Clear, ctx->Stencil.Clear);
478 }
479 if (mask & BUFFER_BIT_ACCUM)
480 _mesa_clear_accum_buffer(ctx);
481 }
482
483
484 void
485 st_init_clear_functions(struct dd_function_table *functions)
486 {
487 functions->Clear = st_Clear;
488 }