9af56dee2dcfc163928c211686c0d841b1f9a2be
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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_bitmap.h"
45 #include "st_cb_clear.h"
46 #include "st_cb_fbo.h"
47 #include "st_draw.h"
48 #include "st_format.h"
49 #include "st_program.h"
50
51 #include "pipe/p_context.h"
52 #include "pipe/p_shader_tokens.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_format.h"
56 #include "util/u_framebuffer.h"
57 #include "util/u_inlines.h"
58 #include "util/u_simple_shaders.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 if (st->clear.vs_layered) {
92 cso_delete_vertex_shader(st->cso_context, st->clear.vs_layered);
93 st->clear.vs_layered = NULL;
94 }
95 if (st->clear.gs_layered) {
96 cso_delete_geometry_shader(st->cso_context, st->clear.gs_layered);
97 st->clear.gs_layered = NULL;
98 }
99 }
100
101
102 /**
103 * Helper function to set the fragment shaders.
104 */
105 static inline void
106 set_fragment_shader(struct st_context *st)
107 {
108 if (!st->clear.fs)
109 st->clear.fs =
110 util_make_fragment_passthrough_shader(st->pipe, TGSI_SEMANTIC_GENERIC,
111 TGSI_INTERPOLATE_CONSTANT,
112 TRUE);
113
114 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
115 }
116
117
118 /**
119 * Helper function to set the vertex shader.
120 */
121 static inline void
122 set_vertex_shader(struct st_context *st)
123 {
124 /* vertex shader - still required to provide the linkage between
125 * fragment shader input semantics and vertex_element/buffers.
126 */
127 if (!st->clear.vs)
128 {
129 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
130 TGSI_SEMANTIC_GENERIC };
131 const uint semantic_indexes[] = { 0, 0 };
132 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
133 semantic_names,
134 semantic_indexes,
135 FALSE);
136 }
137
138 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
139 cso_set_geometry_shader_handle(st->cso_context, NULL);
140 }
141
142
143 static void
144 set_vertex_shader_layered(struct st_context *st)
145 {
146 struct pipe_context *pipe = st->pipe;
147
148 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID)) {
149 assert(!"Got layered clear, but VS instancing is unsupported");
150 set_vertex_shader(st);
151 return;
152 }
153
154 if (!st->clear.vs_layered) {
155 bool vs_layer =
156 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
157 if (vs_layer) {
158 st->clear.vs_layered = util_make_layered_clear_vertex_shader(pipe);
159 } else {
160 st->clear.vs_layered = util_make_layered_clear_helper_vertex_shader(pipe);
161 st->clear.gs_layered = util_make_layered_clear_geometry_shader(pipe);
162 }
163 }
164
165 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs_layered);
166 cso_set_geometry_shader_handle(st->cso_context, st->clear.gs_layered);
167 }
168
169
170 /**
171 * Do glClear by drawing a quadrilateral.
172 * The vertices of the quad will be computed from the
173 * ctx->DrawBuffer->_X/Ymin/max fields.
174 */
175 static void
176 clear_with_quad(struct gl_context *ctx, unsigned clear_buffers)
177 {
178 struct st_context *st = st_context(ctx);
179 struct cso_context *cso = st->cso_context;
180 const struct gl_framebuffer *fb = ctx->DrawBuffer;
181 const GLfloat fb_width = (GLfloat) fb->Width;
182 const GLfloat fb_height = (GLfloat) fb->Height;
183 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
184 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
185 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
186 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
187 unsigned num_layers =
188 util_framebuffer_get_num_layers(&st->state.framebuffer);
189
190 /*
191 printf("%s %s%s%s %f,%f %f,%f\n", __func__,
192 color ? "color, " : "",
193 depth ? "depth, " : "",
194 stencil ? "stencil" : "",
195 x0, y0,
196 x1, y1);
197 */
198
199 cso_save_blend(cso);
200 cso_save_stencil_ref(cso);
201 cso_save_depth_stencil_alpha(cso);
202 cso_save_rasterizer(cso);
203 cso_save_sample_mask(cso);
204 cso_save_min_samples(cso);
205 cso_save_viewport(cso);
206 cso_save_fragment_shader(cso);
207 cso_save_stream_outputs(cso);
208 cso_save_vertex_shader(cso);
209 cso_save_tessctrl_shader(cso);
210 cso_save_tesseval_shader(cso);
211 cso_save_geometry_shader(cso);
212 cso_save_vertex_elements(cso);
213 cso_save_aux_vertex_buffer_slot(cso);
214
215 /* blend state: RGBA masking */
216 {
217 struct pipe_blend_state blend;
218 memset(&blend, 0, sizeof(blend));
219 if (clear_buffers & PIPE_CLEAR_COLOR) {
220 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ?
221 ctx->DrawBuffer->_NumColorDrawBuffers : 1;
222 int i;
223
224 blend.independent_blend_enable = num_buffers > 1;
225
226 for (i = 0; i < num_buffers; i++) {
227 if (!(clear_buffers & (PIPE_CLEAR_COLOR0 << i)))
228 continue;
229
230 if (ctx->Color.ColorMask[i][0])
231 blend.rt[i].colormask |= PIPE_MASK_R;
232 if (ctx->Color.ColorMask[i][1])
233 blend.rt[i].colormask |= PIPE_MASK_G;
234 if (ctx->Color.ColorMask[i][2])
235 blend.rt[i].colormask |= PIPE_MASK_B;
236 if (ctx->Color.ColorMask[i][3])
237 blend.rt[i].colormask |= PIPE_MASK_A;
238 }
239
240 if (st->ctx->Color.DitherFlag)
241 blend.dither = 1;
242 }
243 cso_set_blend(cso, &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 (clear_buffers & PIPE_CLEAR_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 (clear_buffers & PIPE_CLEAR_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(cso, &stencil_ref);
268 }
269
270 cso_set_depth_stencil_alpha(cso, &depth_stencil);
271 }
272
273 cso_set_vertex_elements(cso, 2, st->util_velems);
274 cso_set_stream_outputs(cso, 0, NULL, NULL);
275 cso_set_sample_mask(cso, ~0);
276 cso_set_min_samples(cso, 1);
277 cso_set_rasterizer(cso, &st->clear.raster);
278
279 /* viewport state: viewport matching window dims */
280 {
281 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
282 struct pipe_viewport_state vp;
283 vp.scale[0] = 0.5f * fb_width;
284 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
285 vp.scale[2] = 0.5f;
286 vp.translate[0] = 0.5f * fb_width;
287 vp.translate[1] = 0.5f * fb_height;
288 vp.translate[2] = 0.5f;
289 cso_set_viewport(cso, &vp);
290 }
291
292 set_fragment_shader(st);
293 cso_set_tessctrl_shader_handle(cso, NULL);
294 cso_set_tesseval_shader_handle(cso, NULL);
295
296 if (num_layers > 1)
297 set_vertex_shader_layered(st);
298 else
299 set_vertex_shader(st);
300
301 /* draw quad matching scissor rect.
302 *
303 * Note: if we're only clearing depth/stencil we still setup vertices
304 * with color, but they'll be ignored.
305 *
306 * We can't translate the clear color to the colorbuffer format,
307 * because different colorbuffers may have different formats.
308 */
309 if (!st_draw_quad(st, x0, y0, x1, y1,
310 ctx->Depth.Clear * 2.0f - 1.0f,
311 0.0f, 0.0f, 0.0f, 0.0f,
312 (const float *) &ctx->Color.ClearColor.f,
313 num_layers)) {
314 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear");
315 }
316
317 /* Restore pipe state */
318 cso_restore_blend(cso);
319 cso_restore_stencil_ref(cso);
320 cso_restore_depth_stencil_alpha(cso);
321 cso_restore_rasterizer(cso);
322 cso_restore_sample_mask(cso);
323 cso_restore_min_samples(cso);
324 cso_restore_viewport(cso);
325 cso_restore_fragment_shader(cso);
326 cso_restore_vertex_shader(cso);
327 cso_restore_tessctrl_shader(cso);
328 cso_restore_tesseval_shader(cso);
329 cso_restore_geometry_shader(cso);
330 cso_restore_vertex_elements(cso);
331 cso_restore_aux_vertex_buffer_slot(cso);
332 cso_restore_stream_outputs(cso);
333 }
334
335
336 /**
337 * Return if the scissor must be enabled during the clear.
338 */
339 static inline GLboolean
340 is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
341 {
342 return (ctx->Scissor.EnableFlags & 1) &&
343 (ctx->Scissor.ScissorArray[0].X > 0 ||
344 ctx->Scissor.ScissorArray[0].Y > 0 ||
345 (unsigned) ctx->Scissor.ScissorArray[0].Width < rb->Width ||
346 (unsigned) ctx->Scissor.ScissorArray[0].Height < rb->Height);
347 }
348
349
350 /**
351 * Return if all of the color channels are masked.
352 */
353 static inline GLboolean
354 is_color_disabled(struct gl_context *ctx, int i)
355 {
356 return !ctx->Color.ColorMask[i][0] &&
357 !ctx->Color.ColorMask[i][1] &&
358 !ctx->Color.ColorMask[i][2] &&
359 !ctx->Color.ColorMask[i][3];
360 }
361
362
363 /**
364 * Return if any of the color channels are masked.
365 */
366 static inline GLboolean
367 is_color_masked(struct gl_context *ctx, int i)
368 {
369 return !ctx->Color.ColorMask[i][0] ||
370 !ctx->Color.ColorMask[i][1] ||
371 !ctx->Color.ColorMask[i][2] ||
372 !ctx->Color.ColorMask[i][3];
373 }
374
375
376 /**
377 * Return if all of the stencil bits are masked.
378 */
379 static inline GLboolean
380 is_stencil_disabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
381 {
382 const GLuint stencilMax = 0xff;
383
384 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
385 return (ctx->Stencil.WriteMask[0] & stencilMax) == 0;
386 }
387
388
389 /**
390 * Return if any of the stencil bits are masked.
391 */
392 static inline GLboolean
393 is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb)
394 {
395 const GLuint stencilMax = 0xff;
396
397 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
398 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
399 }
400
401
402 /**
403 * Called via ctx->Driver.Clear()
404 */
405 static void
406 st_Clear(struct gl_context *ctx, GLbitfield mask)
407 {
408 struct st_context *st = st_context(ctx);
409 struct gl_renderbuffer *depthRb
410 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
411 struct gl_renderbuffer *stencilRb
412 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
413 GLbitfield quad_buffers = 0x0;
414 GLbitfield clear_buffers = 0x0;
415 GLuint i;
416
417 st_flush_bitmap_cache(st);
418
419 /* This makes sure the pipe has the latest scissor, etc values */
420 st_validate_state( st, ST_PIPELINE_RENDER );
421
422 if (mask & BUFFER_BITS_COLOR) {
423 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
424 GLint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
425
426 if (b >= 0 && mask & (1 << b)) {
427 struct gl_renderbuffer *rb
428 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
429 struct st_renderbuffer *strb = st_renderbuffer(rb);
430 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
431
432 if (!strb || !strb->surface)
433 continue;
434
435 if (is_color_disabled(ctx, colormask_index))
436 continue;
437
438 if (is_scissor_enabled(ctx, rb) ||
439 is_color_masked(ctx, colormask_index))
440 quad_buffers |= PIPE_CLEAR_COLOR0 << i;
441 else
442 clear_buffers |= PIPE_CLEAR_COLOR0 << i;
443 }
444 }
445 }
446
447 if (mask & BUFFER_BIT_DEPTH) {
448 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
449
450 if (strb->surface && ctx->Depth.Mask) {
451 if (is_scissor_enabled(ctx, depthRb))
452 quad_buffers |= PIPE_CLEAR_DEPTH;
453 else
454 clear_buffers |= PIPE_CLEAR_DEPTH;
455 }
456 }
457 if (mask & BUFFER_BIT_STENCIL) {
458 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
459
460 if (strb->surface && !is_stencil_disabled(ctx, stencilRb)) {
461 if (is_scissor_enabled(ctx, stencilRb) ||
462 is_stencil_masked(ctx, stencilRb))
463 quad_buffers |= PIPE_CLEAR_STENCIL;
464 else
465 clear_buffers |= PIPE_CLEAR_STENCIL;
466 }
467 }
468
469 /* Always clear depth and stencil together.
470 * This can only happen when the stencil writemask is not a full mask.
471 */
472 if (quad_buffers & PIPE_CLEAR_DEPTHSTENCIL &&
473 clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
474 quad_buffers |= clear_buffers & PIPE_CLEAR_DEPTHSTENCIL;
475 clear_buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
476 }
477
478 /* Only use quad-based clearing for the renderbuffers which cannot
479 * use pipe->clear. We want to always use pipe->clear for the other
480 * renderbuffers, because it's likely to be faster.
481 */
482 if (quad_buffers) {
483 clear_with_quad(ctx, quad_buffers);
484 }
485 if (clear_buffers) {
486 /* We can't translate the clear color to the colorbuffer format,
487 * because different colorbuffers may have different formats.
488 */
489 st->pipe->clear(st->pipe, clear_buffers,
490 (union pipe_color_union*)&ctx->Color.ClearColor,
491 ctx->Depth.Clear, ctx->Stencil.Clear);
492 }
493 if (mask & BUFFER_BIT_ACCUM)
494 _mesa_clear_accum_buffer(ctx);
495 }
496
497
498 void
499 st_init_clear_functions(struct dd_function_table *functions)
500 {
501 functions->Clear = st_Clear;
502 }