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