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