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/errors.h"
37 #include "util/glheader.h"
38 #include "main/accum.h"
39 #include "main/formats.h"
40 #include "main/framebuffer.h"
41 #include "main/macros.h"
42 #include "main/glformats.h"
43 #include "program/prog_instruction.h"
44 #include "st_context.h"
45 #include "st_atom.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_clear.h"
48 #include "st_draw.h"
49 #include "st_format.h"
50 #include "st_nir.h"
51 #include "st_program.h"
52 #include "st_util.h"
53
54 #include "pipe/p_context.h"
55 #include "pipe/p_shader_tokens.h"
56 #include "pipe/p_state.h"
57 #include "pipe/p_defines.h"
58 #include "util/format/u_format.h"
59 #include "util/u_inlines.h"
60 #include "util/u_simple_shaders.h"
61
62 #include "cso_cache/cso_context.h"
63
64
65 /**
66 * Do per-context initialization for glClear.
67 */
68 void
st_init_clear(struct st_context * st)69 st_init_clear(struct st_context *st)
70 {
71 memset(&st->clear, 0, sizeof(st->clear));
72
73 st->clear.raster.half_pixel_center = 1;
74 st->clear.raster.bottom_edge_rule = 1;
75 st->clear.raster.depth_clip_near = 1;
76 st->clear.raster.depth_clip_far = 1;
77 }
78
79
80 /**
81 * Free per-context state for glClear.
82 */
83 void
st_destroy_clear(struct st_context * st)84 st_destroy_clear(struct st_context *st)
85 {
86 if (st->clear.fs) {
87 st->pipe->delete_fs_state(st->pipe, st->clear.fs);
88 st->clear.fs = NULL;
89 }
90 if (st->clear.vs) {
91 st->pipe->delete_vs_state(st->pipe, st->clear.vs);
92 st->clear.vs = NULL;
93 }
94 if (st->clear.vs_layered) {
95 st->pipe->delete_vs_state(st->pipe, st->clear.vs_layered);
96 st->clear.vs_layered = NULL;
97 }
98 if (st->clear.gs_layered) {
99 st->pipe->delete_gs_state(st->pipe, st->clear.gs_layered);
100 st->clear.gs_layered = NULL;
101 }
102 }
103
104
105 /**
106 * Helper function to set the clear color fragment shader.
107 */
108 static void
set_clearcolor_fs(struct st_context * st,union pipe_color_union * color)109 set_clearcolor_fs(struct st_context *st, union pipe_color_union *color)
110 {
111 struct pipe_constant_buffer cb = {
112 .user_buffer = color->f,
113 .buffer_size = 4 * sizeof(float),
114 };
115 st->pipe->set_constant_buffer(st->pipe, PIPE_SHADER_FRAGMENT, 0,
116 false, &cb);
117
118 if (!st->clear.fs) {
119 st->clear.fs = st_nir_make_clearcolor_shader(st);
120 }
121
122 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
123 }
124
125 static void *
make_nir_clear_vertex_shader(struct st_context * st,bool layered)126 make_nir_clear_vertex_shader(struct st_context *st, bool layered)
127 {
128 const char *shader_name = layered ? "layered clear VS" : "clear VS";
129 unsigned inputs[] = {
130 VERT_ATTRIB_POS,
131 SYSTEM_VALUE_INSTANCE_ID,
132 };
133 gl_varying_slot outputs[] = {
134 VARYING_SLOT_POS,
135 VARYING_SLOT_LAYER
136 };
137
138 return st_nir_make_passthrough_vs(st, shader_name, layered ? 2 : 1, inputs,
139 outputs, (1 << 1));
140 }
141
142
143 /**
144 * Helper function to set the vertex shader.
145 */
146 static inline void
set_vertex_shader(struct st_context * st)147 set_vertex_shader(struct st_context *st)
148 {
149 /* vertex shader - still required to provide the linkage between
150 * fragment shader input semantics and vertex_element/buffers.
151 */
152 if (!st->clear.vs)
153 st->clear.vs = make_nir_clear_vertex_shader(st, false);
154
155 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
156 cso_set_geometry_shader_handle(st->cso_context, NULL);
157 }
158
159
160 static void
set_vertex_shader_layered(struct st_context * st)161 set_vertex_shader_layered(struct st_context *st)
162 {
163 struct pipe_context *pipe = st->pipe;
164
165 if (!st->screen->caps.vs_instanceid) {
166 assert(!"Got layered clear, but VS instancing is unsupported");
167 set_vertex_shader(st);
168 return;
169 }
170
171 if (!st->clear.vs_layered) {
172 bool vs_layer =
173 st->screen->caps.vs_layer_viewport;
174 if (vs_layer) {
175 st->clear.vs_layered = make_nir_clear_vertex_shader(st, true);
176 } else {
177 st->clear.vs_layered = util_make_layered_clear_helper_vertex_shader(pipe);
178 st->clear.gs_layered = util_make_layered_clear_geometry_shader(pipe);
179 }
180 }
181
182 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs_layered);
183 cso_set_geometry_shader_handle(st->cso_context, st->clear.gs_layered);
184 }
185
186
187 /**
188 * Do glClear by drawing a quadrilateral.
189 * The vertices of the quad will be computed from the
190 * ctx->DrawBuffer->_X/Ymin/max fields.
191 */
192 static void
clear_with_quad(struct gl_context * ctx,unsigned clear_buffers)193 clear_with_quad(struct gl_context *ctx, unsigned clear_buffers)
194 {
195 struct st_context *st = st_context(ctx);
196 struct cso_context *cso = st->cso_context;
197 const struct gl_framebuffer *fb = ctx->DrawBuffer;
198 const GLfloat fb_width = (GLfloat) fb->Width;
199 const GLfloat fb_height = (GLfloat) fb->Height;
200
201 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
202
203 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
204 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
205 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
206 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
207 unsigned num_layers = st->state.fb_num_layers;
208
209 /*
210 printf("%s %s%s%s %f,%f %f,%f\n", __func__,
211 color ? "color, " : "",
212 depth ? "depth, " : "",
213 stencil ? "stencil" : "",
214 x0, y0,
215 x1, y1);
216 */
217
218 cso_save_state(cso, (CSO_BIT_BLEND |
219 CSO_BIT_STENCIL_REF |
220 CSO_BIT_DEPTH_STENCIL_ALPHA |
221 CSO_BIT_RASTERIZER |
222 CSO_BIT_SAMPLE_MASK |
223 CSO_BIT_MIN_SAMPLES |
224 CSO_BIT_VIEWPORT |
225 CSO_BIT_STREAM_OUTPUTS |
226 CSO_BIT_VERTEX_ELEMENTS |
227 (st->active_queries ? CSO_BIT_PAUSE_QUERIES : 0) |
228 CSO_BITS_ALL_SHADERS));
229
230 /* blend state: RGBA masking */
231 {
232 struct pipe_blend_state blend;
233 memset(&blend, 0, sizeof(blend));
234 if (clear_buffers & PIPE_CLEAR_COLOR) {
235 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ?
236 ctx->DrawBuffer->_NumColorDrawBuffers : 1;
237 int i;
238
239 blend.independent_blend_enable = num_buffers > 1;
240 blend.max_rt = num_buffers - 1;
241
242 for (i = 0; i < num_buffers; i++) {
243 if (!(clear_buffers & (PIPE_CLEAR_COLOR0 << i)))
244 continue;
245
246 blend.rt[i].colormask = GET_COLORMASK(ctx->Color.ColorMask, i);
247 }
248
249 if (ctx->Color.DitherFlag)
250 blend.dither = 1;
251 }
252 cso_set_blend(cso, &blend);
253 }
254
255 /* depth_stencil state: always pass/set to ref value */
256 {
257 struct pipe_depth_stencil_alpha_state depth_stencil;
258 memset(&depth_stencil, 0, sizeof(depth_stencil));
259 if (clear_buffers & PIPE_CLEAR_DEPTH) {
260 depth_stencil.depth_enabled = 1;
261 depth_stencil.depth_writemask = 1;
262 depth_stencil.depth_func = PIPE_FUNC_ALWAYS;
263 }
264
265 if (clear_buffers & PIPE_CLEAR_STENCIL) {
266 struct pipe_stencil_ref stencil_ref;
267 memset(&stencil_ref, 0, sizeof(stencil_ref));
268 depth_stencil.stencil[0].enabled = 1;
269 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
270 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
271 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
272 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
273 depth_stencil.stencil[0].valuemask = 0xff;
274 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
275 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
276 cso_set_stencil_ref(cso, stencil_ref);
277 }
278
279 cso_set_depth_stencil_alpha(cso, &depth_stencil);
280 }
281
282 st->util_velems.count = 1;
283 cso_set_vertex_elements(cso, &st->util_velems);
284
285 cso_set_stream_outputs(cso, 0, NULL, NULL, 0);
286 cso_set_sample_mask(cso, ~0);
287 cso_set_min_samples(cso, 1);
288 st->clear.raster.multisample = st->state.fb_num_samples > 1;
289 cso_set_rasterizer(cso, &st->clear.raster);
290
291 /* viewport state: viewport matching window dims */
292 cso_set_viewport_dims(st->cso_context, fb_width, fb_height,
293 _mesa_fb_orientation(fb) == Y_0_TOP);
294
295 /* Set constant buffer */
296 set_clearcolor_fs(st, (union pipe_color_union*)&ctx->Color.ClearColor);
297 cso_set_tessctrl_shader_handle(cso, NULL);
298 cso_set_tesseval_shader_handle(cso, NULL);
299
300 if (num_layers > 1)
301 set_vertex_shader_layered(st);
302 else
303 set_vertex_shader(st);
304
305 /* draw quad matching scissor rect.
306 *
307 * Note: if we're only clearing depth/stencil we still setup vertices
308 * with color, but they'll be ignored.
309 *
310 * We can't translate the clear color to the colorbuffer format,
311 * because different colorbuffers may have different formats.
312 */
313 if (!st_draw_quad(st, x0, y0, x1, y1,
314 ctx->Depth.Clear * 2.0f - 1.0f,
315 0.0f, 0.0f, 0.0f, 0.0f,
316 (const float *) &ctx->Color.ClearColor.f,
317 num_layers)) {
318 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear");
319 }
320
321 /* Restore pipe state */
322 cso_restore_state(cso, 0);
323 ctx->Array.NewVertexElements = true;
324 ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS |
325 ST_NEW_FS_CONSTANTS;
326 }
327
328
329 /**
330 * Return if the scissor must be enabled during the clear.
331 */
332 static inline GLboolean
is_scissor_enabled(struct gl_context * ctx,struct gl_renderbuffer * rb)333 is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
334 {
335 const struct gl_scissor_rect *scissor = &ctx->Scissor.ScissorArray[0];
336
337 return (ctx->Scissor.EnableFlags & 1) &&
338 (scissor->X > 0 ||
339 scissor->Y > 0 ||
340 scissor->X + scissor->Width < (int)rb->Width ||
341 scissor->Y + scissor->Height < (int)rb->Height);
342 }
343
344 /**
345 * Return if window rectangles must be enabled during the clear.
346 */
347 static inline bool
is_window_rectangle_enabled(struct gl_context * ctx)348 is_window_rectangle_enabled(struct gl_context *ctx)
349 {
350 if (ctx->DrawBuffer == ctx->WinSysDrawBuffer)
351 return false;
352 return ctx->Scissor.NumWindowRects > 0 ||
353 ctx->Scissor.WindowRectMode == GL_INCLUSIVE_EXT;
354 }
355
356
357 /**
358 * Return if all of the stencil bits are masked.
359 */
360 static inline GLboolean
is_stencil_disabled(struct gl_context * ctx,struct gl_renderbuffer * rb)361 is_stencil_disabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
362 {
363 const GLuint stencilMax = 0xff;
364
365 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
366 return (ctx->Stencil.WriteMask[0] & stencilMax) == 0;
367 }
368
369
370 /**
371 * Return if any of the stencil bits are masked.
372 */
373 static inline GLboolean
is_stencil_masked(struct gl_context * ctx,struct gl_renderbuffer * rb)374 is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb)
375 {
376 const GLuint stencilMax = 0xff;
377
378 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
379 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
380 }
381
382 void
st_Clear(struct gl_context * ctx,GLbitfield mask)383 st_Clear(struct gl_context *ctx, GLbitfield mask)
384 {
385 struct st_context *st = st_context(ctx);
386 struct gl_renderbuffer *depthRb
387 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
388 struct gl_renderbuffer *stencilRb
389 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
390 GLbitfield quad_buffers = 0x0;
391 GLbitfield clear_buffers = 0x0;
392 bool have_scissor_buffers = false;
393 GLuint i;
394
395 st_flush_bitmap_cache(st);
396 st_invalidate_readpix_cache(st);
397
398 /* This makes sure the pipe has the latest scissor, etc values */
399 st_validate_state(st, ST_PIPELINE_CLEAR_STATE_MASK);
400
401 if (mask & BUFFER_BITS_COLOR) {
402 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
403 gl_buffer_index b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
404
405 if (b != BUFFER_NONE && mask & (1 << b)) {
406 struct gl_renderbuffer *rb
407 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
408 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
409
410 if (!rb || !rb->surface)
411 continue;
412
413 unsigned colormask =
414 GET_COLORMASK(ctx->Color.ColorMask, colormask_index);
415
416 if (!colormask)
417 continue;
418
419 unsigned surf_colormask =
420 util_format_colormask(util_format_description(rb->surface->format));
421
422 bool scissor = is_scissor_enabled(ctx, rb);
423 if ((scissor && !st->can_scissor_clear) ||
424 is_window_rectangle_enabled(ctx) ||
425 ((colormask & surf_colormask) != surf_colormask))
426 quad_buffers |= PIPE_CLEAR_COLOR0 << i;
427 else
428 clear_buffers |= PIPE_CLEAR_COLOR0 << i;
429 have_scissor_buffers |= scissor && st->can_scissor_clear;
430 }
431 }
432 }
433
434 if (mask & BUFFER_BIT_DEPTH) {
435 if (depthRb->surface && ctx->Depth.Mask) {
436 bool scissor = is_scissor_enabled(ctx, depthRb);
437 if ((scissor && !st->can_scissor_clear) ||
438 is_window_rectangle_enabled(ctx))
439 quad_buffers |= PIPE_CLEAR_DEPTH;
440 else
441 clear_buffers |= PIPE_CLEAR_DEPTH;
442 have_scissor_buffers |= scissor && st->can_scissor_clear;
443 }
444 }
445 if (mask & BUFFER_BIT_STENCIL) {
446 if (stencilRb->surface && !is_stencil_disabled(ctx, stencilRb)) {
447 bool scissor = is_scissor_enabled(ctx, stencilRb);
448 if ((scissor && !st->can_scissor_clear) ||
449 is_window_rectangle_enabled(ctx) ||
450 is_stencil_masked(ctx, stencilRb))
451 quad_buffers |= PIPE_CLEAR_STENCIL;
452 else
453 clear_buffers |= PIPE_CLEAR_STENCIL;
454 have_scissor_buffers |= scissor && st->can_scissor_clear;
455 }
456 }
457
458 /* Always clear depth and stencil together.
459 * This can only happen when the stencil writemask is not a full mask.
460 */
461 if (quad_buffers & PIPE_CLEAR_DEPTHSTENCIL &&
462 clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
463 quad_buffers |= clear_buffers & PIPE_CLEAR_DEPTHSTENCIL;
464 clear_buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
465 }
466
467 /* Only use quad-based clearing for the renderbuffers which cannot
468 * use pipe->clear. We want to always use pipe->clear for the other
469 * renderbuffers, because it's likely to be faster.
470 */
471 if (clear_buffers) {
472 const struct gl_scissor_rect *scissor = &ctx->Scissor.ScissorArray[0];
473 struct pipe_scissor_state scissor_state = {
474 .minx = MAX2(scissor->X, 0),
475 .miny = MAX2(scissor->Y, 0),
476 .maxx = MAX2(scissor->X + scissor->Width, 0),
477 .maxy = MAX2(scissor->Y + scissor->Height, 0),
478
479 };
480
481 /* Now invert Y if needed.
482 * Gallium drivers use the convention Y=0=top for surfaces.
483 */
484 if (st->state.fb_orientation == Y_0_TOP) {
485 const struct gl_framebuffer *fb = ctx->DrawBuffer;
486 /* use intermediate variables to avoid uint underflow */
487 GLint miny, maxy;
488 miny = fb->Height - scissor_state.maxy;
489 maxy = fb->Height - scissor_state.miny;
490 scissor_state.miny = MAX2(miny, 0);
491 scissor_state.maxy = MAX2(maxy, 0);
492 }
493 if (have_scissor_buffers) {
494 const struct gl_framebuffer *fb = ctx->DrawBuffer;
495 scissor_state.maxx = MIN2(scissor_state.maxx, fb->Width);
496 scissor_state.maxy = MIN2(scissor_state.maxy, fb->Height);
497 if (scissor_state.minx >= scissor_state.maxx ||
498 scissor_state.miny >= scissor_state.maxy)
499 return;
500 }
501 /* We can't translate the clear color to the colorbuffer format,
502 * because different colorbuffers may have different formats.
503 */
504 st->pipe->clear(st->pipe, clear_buffers, have_scissor_buffers ? &scissor_state : NULL,
505 (union pipe_color_union*)&ctx->Color.ClearColor,
506 ctx->Depth.Clear, ctx->Stencil.Clear);
507 }
508 if (quad_buffers) {
509 clear_with_quad(ctx, quad_buffers);
510 }
511 if (mask & BUFFER_BIT_ACCUM)
512 _mesa_clear_accum_buffer(ctx);
513 }
514
515