st/mesa: overhaul vertex setup for clearing, glDrawPixels, glBitmap
[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_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 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 * Draw a screen-aligned quadrilateral.
172 * Coords are clip coords with y=0=bottom.
173 */
174 static void
175 draw_quad(struct st_context *st,
176 float x0, float y0, float x1, float y1, GLfloat z,
177 unsigned num_instances,
178 const union pipe_color_union *color)
179 {
180 struct cso_context *cso = st->cso_context;
181 struct pipe_vertex_buffer vb = {0};
182 struct st_util_vertex *verts;
183
184 vb.stride = sizeof(struct st_util_vertex);
185
186 u_upload_alloc(st->uploader, 0, 4 * sizeof(struct st_util_vertex), 4,
187 &vb.buffer_offset, &vb.buffer, (void **) &verts);
188 if (!vb.buffer) {
189 return;
190 }
191
192 /* Convert Z from [0,1] to [-1,1] range */
193 z = z * 2.0f - 1.0f;
194
195 /* Note: if we're only clearing depth/stencil we still setup vertices
196 * with color, but they'll be ignored.
197 */
198 verts[0].x = x0;
199 verts[0].y = y0;
200 verts[0].z = z;
201 verts[0].r = color->f[0];
202 verts[0].g = color->f[1];
203 verts[0].b = color->f[2];
204 verts[0].a = color->f[3];
205
206 verts[1].x = x1;
207 verts[1].y = y0;
208 verts[1].z = z;
209 verts[1].r = color->f[0];
210 verts[1].g = color->f[1];
211 verts[1].b = color->f[2];
212 verts[1].a = color->f[3];
213
214 verts[2].x = x1;
215 verts[2].y = y1;
216 verts[2].z = z;
217 verts[2].r = color->f[0];
218 verts[2].g = color->f[1];
219 verts[2].b = color->f[2];
220 verts[2].a = color->f[3];
221
222 verts[3].x = x0;
223 verts[3].y = y1;
224 verts[3].z = z;
225 verts[3].r = color->f[0];
226 verts[3].g = color->f[1];
227 verts[3].b = color->f[2];
228 verts[3].a = color->f[3];
229
230 u_upload_unmap(st->uploader);
231
232 /* draw */
233 cso_set_vertex_buffers(cso, cso_get_aux_vertex_buffer_slot(cso), 1, &vb);
234 cso_draw_arrays_instanced(cso, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
235 0, num_instances);
236 pipe_resource_reference(&vb.buffer, NULL);
237 }
238
239
240
241 /**
242 * Do glClear by drawing a quadrilateral.
243 * The vertices of the quad will be computed from the
244 * ctx->DrawBuffer->_X/Ymin/max fields.
245 */
246 static void
247 clear_with_quad(struct gl_context *ctx, unsigned clear_buffers)
248 {
249 struct st_context *st = st_context(ctx);
250 const struct gl_framebuffer *fb = ctx->DrawBuffer;
251 const GLfloat fb_width = (GLfloat) fb->Width;
252 const GLfloat fb_height = (GLfloat) fb->Height;
253 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
254 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
255 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
256 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
257 unsigned num_layers =
258 util_framebuffer_get_num_layers(&st->state.framebuffer);
259
260 /*
261 printf("%s %s%s%s %f,%f %f,%f\n", __func__,
262 color ? "color, " : "",
263 depth ? "depth, " : "",
264 stencil ? "stencil" : "",
265 x0, y0,
266 x1, y1);
267 */
268
269 cso_save_blend(st->cso_context);
270 cso_save_stencil_ref(st->cso_context);
271 cso_save_depth_stencil_alpha(st->cso_context);
272 cso_save_rasterizer(st->cso_context);
273 cso_save_sample_mask(st->cso_context);
274 cso_save_min_samples(st->cso_context);
275 cso_save_viewport(st->cso_context);
276 cso_save_fragment_shader(st->cso_context);
277 cso_save_stream_outputs(st->cso_context);
278 cso_save_vertex_shader(st->cso_context);
279 cso_save_tessctrl_shader(st->cso_context);
280 cso_save_tesseval_shader(st->cso_context);
281 cso_save_geometry_shader(st->cso_context);
282 cso_save_vertex_elements(st->cso_context);
283 cso_save_aux_vertex_buffer_slot(st->cso_context);
284
285 /* blend state: RGBA masking */
286 {
287 struct pipe_blend_state blend;
288 memset(&blend, 0, sizeof(blend));
289 if (clear_buffers & PIPE_CLEAR_COLOR) {
290 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ?
291 ctx->DrawBuffer->_NumColorDrawBuffers : 1;
292 int i;
293
294 blend.independent_blend_enable = num_buffers > 1;
295
296 for (i = 0; i < num_buffers; i++) {
297 if (!(clear_buffers & (PIPE_CLEAR_COLOR0 << i)))
298 continue;
299
300 if (ctx->Color.ColorMask[i][0])
301 blend.rt[i].colormask |= PIPE_MASK_R;
302 if (ctx->Color.ColorMask[i][1])
303 blend.rt[i].colormask |= PIPE_MASK_G;
304 if (ctx->Color.ColorMask[i][2])
305 blend.rt[i].colormask |= PIPE_MASK_B;
306 if (ctx->Color.ColorMask[i][3])
307 blend.rt[i].colormask |= PIPE_MASK_A;
308 }
309
310 if (st->ctx->Color.DitherFlag)
311 blend.dither = 1;
312 }
313 cso_set_blend(st->cso_context, &blend);
314 }
315
316 /* depth_stencil state: always pass/set to ref value */
317 {
318 struct pipe_depth_stencil_alpha_state depth_stencil;
319 memset(&depth_stencil, 0, sizeof(depth_stencil));
320 if (clear_buffers & PIPE_CLEAR_DEPTH) {
321 depth_stencil.depth.enabled = 1;
322 depth_stencil.depth.writemask = 1;
323 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
324 }
325
326 if (clear_buffers & PIPE_CLEAR_STENCIL) {
327 struct pipe_stencil_ref stencil_ref;
328 memset(&stencil_ref, 0, sizeof(stencil_ref));
329 depth_stencil.stencil[0].enabled = 1;
330 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
331 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
332 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
333 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
334 depth_stencil.stencil[0].valuemask = 0xff;
335 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
336 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
337 cso_set_stencil_ref(st->cso_context, &stencil_ref);
338 }
339
340 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
341 }
342
343 cso_set_vertex_elements(st->cso_context, 2, st->util_velems);
344 cso_set_stream_outputs(st->cso_context, 0, NULL, NULL);
345 cso_set_sample_mask(st->cso_context, ~0);
346 cso_set_min_samples(st->cso_context, 1);
347 cso_set_rasterizer(st->cso_context, &st->clear.raster);
348
349 /* viewport state: viewport matching window dims */
350 {
351 const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
352 struct pipe_viewport_state vp;
353 vp.scale[0] = 0.5f * fb_width;
354 vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
355 vp.scale[2] = 0.5f;
356 vp.translate[0] = 0.5f * fb_width;
357 vp.translate[1] = 0.5f * fb_height;
358 vp.translate[2] = 0.5f;
359 cso_set_viewport(st->cso_context, &vp);
360 }
361
362 set_fragment_shader(st);
363 cso_set_tessctrl_shader_handle(st->cso_context, NULL);
364 cso_set_tesseval_shader_handle(st->cso_context, NULL);
365
366 if (num_layers > 1)
367 set_vertex_shader_layered(st);
368 else
369 set_vertex_shader(st);
370
371 /* We can't translate the clear color to the colorbuffer format,
372 * because different colorbuffers may have different formats.
373 */
374
375 /* draw quad matching scissor rect */
376 draw_quad(st, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, num_layers,
377 (union pipe_color_union*)&ctx->Color.ClearColor);
378
379 /* Restore pipe state */
380 cso_restore_blend(st->cso_context);
381 cso_restore_stencil_ref(st->cso_context);
382 cso_restore_depth_stencil_alpha(st->cso_context);
383 cso_restore_rasterizer(st->cso_context);
384 cso_restore_sample_mask(st->cso_context);
385 cso_restore_min_samples(st->cso_context);
386 cso_restore_viewport(st->cso_context);
387 cso_restore_fragment_shader(st->cso_context);
388 cso_restore_vertex_shader(st->cso_context);
389 cso_restore_tessctrl_shader(st->cso_context);
390 cso_restore_tesseval_shader(st->cso_context);
391 cso_restore_geometry_shader(st->cso_context);
392 cso_restore_vertex_elements(st->cso_context);
393 cso_restore_aux_vertex_buffer_slot(st->cso_context);
394 cso_restore_stream_outputs(st->cso_context);
395 }
396
397
398 /**
399 * Return if the scissor must be enabled during the clear.
400 */
401 static inline GLboolean
402 is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
403 {
404 return (ctx->Scissor.EnableFlags & 1) &&
405 (ctx->Scissor.ScissorArray[0].X > 0 ||
406 ctx->Scissor.ScissorArray[0].Y > 0 ||
407 (unsigned) ctx->Scissor.ScissorArray[0].Width < rb->Width ||
408 (unsigned) ctx->Scissor.ScissorArray[0].Height < rb->Height);
409 }
410
411
412 /**
413 * Return if all of the color channels are masked.
414 */
415 static inline GLboolean
416 is_color_disabled(struct gl_context *ctx, int i)
417 {
418 return !ctx->Color.ColorMask[i][0] &&
419 !ctx->Color.ColorMask[i][1] &&
420 !ctx->Color.ColorMask[i][2] &&
421 !ctx->Color.ColorMask[i][3];
422 }
423
424
425 /**
426 * Return if any of the color channels are masked.
427 */
428 static inline GLboolean
429 is_color_masked(struct gl_context *ctx, int i)
430 {
431 return !ctx->Color.ColorMask[i][0] ||
432 !ctx->Color.ColorMask[i][1] ||
433 !ctx->Color.ColorMask[i][2] ||
434 !ctx->Color.ColorMask[i][3];
435 }
436
437
438 /**
439 * Return if all of the stencil bits are masked.
440 */
441 static inline GLboolean
442 is_stencil_disabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
443 {
444 const GLuint stencilMax = 0xff;
445
446 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
447 return (ctx->Stencil.WriteMask[0] & stencilMax) == 0;
448 }
449
450
451 /**
452 * Return if any of the stencil bits are masked.
453 */
454 static inline GLboolean
455 is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb)
456 {
457 const GLuint stencilMax = 0xff;
458
459 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
460 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
461 }
462
463
464 /**
465 * Called via ctx->Driver.Clear()
466 */
467 static void
468 st_Clear(struct gl_context *ctx, GLbitfield mask)
469 {
470 struct st_context *st = st_context(ctx);
471 struct gl_renderbuffer *depthRb
472 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
473 struct gl_renderbuffer *stencilRb
474 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
475 GLbitfield quad_buffers = 0x0;
476 GLbitfield clear_buffers = 0x0;
477 GLuint i;
478
479 st_flush_bitmap_cache(st);
480
481 /* This makes sure the pipe has the latest scissor, etc values */
482 st_validate_state( st, ST_PIPELINE_RENDER );
483
484 if (mask & BUFFER_BITS_COLOR) {
485 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
486 GLint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
487
488 if (b >= 0 && mask & (1 << b)) {
489 struct gl_renderbuffer *rb
490 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
491 struct st_renderbuffer *strb = st_renderbuffer(rb);
492 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
493
494 if (!strb || !strb->surface)
495 continue;
496
497 if (is_color_disabled(ctx, colormask_index))
498 continue;
499
500 if (is_scissor_enabled(ctx, rb) ||
501 is_color_masked(ctx, colormask_index))
502 quad_buffers |= PIPE_CLEAR_COLOR0 << i;
503 else
504 clear_buffers |= PIPE_CLEAR_COLOR0 << i;
505 }
506 }
507 }
508
509 if (mask & BUFFER_BIT_DEPTH) {
510 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
511
512 if (strb->surface && ctx->Depth.Mask) {
513 if (is_scissor_enabled(ctx, depthRb))
514 quad_buffers |= PIPE_CLEAR_DEPTH;
515 else
516 clear_buffers |= PIPE_CLEAR_DEPTH;
517 }
518 }
519 if (mask & BUFFER_BIT_STENCIL) {
520 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
521
522 if (strb->surface && !is_stencil_disabled(ctx, stencilRb)) {
523 if (is_scissor_enabled(ctx, stencilRb) ||
524 is_stencil_masked(ctx, stencilRb))
525 quad_buffers |= PIPE_CLEAR_STENCIL;
526 else
527 clear_buffers |= PIPE_CLEAR_STENCIL;
528 }
529 }
530
531 /* Always clear depth and stencil together.
532 * This can only happen when the stencil writemask is not a full mask.
533 */
534 if (quad_buffers & PIPE_CLEAR_DEPTHSTENCIL &&
535 clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
536 quad_buffers |= clear_buffers & PIPE_CLEAR_DEPTHSTENCIL;
537 clear_buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
538 }
539
540 /* Only use quad-based clearing for the renderbuffers which cannot
541 * use pipe->clear. We want to always use pipe->clear for the other
542 * renderbuffers, because it's likely to be faster.
543 */
544 if (quad_buffers) {
545 clear_with_quad(ctx, quad_buffers);
546 }
547 if (clear_buffers) {
548 /* We can't translate the clear color to the colorbuffer format,
549 * because different colorbuffers may have different formats.
550 */
551 st->pipe->clear(st->pipe, clear_buffers,
552 (union pipe_color_union*)&ctx->Color.ClearColor,
553 ctx->Depth.Clear, ctx->Stencil.Clear);
554 }
555 if (mask & BUFFER_BIT_ACCUM)
556 _mesa_clear_accum_buffer(ctx);
557 }
558
559
560 void
561 st_init_clear_functions(struct dd_function_table *functions)
562 {
563 functions->Clear = st_Clear;
564 }