1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
27 **************************************************************************/
31 * Keith Whitwell <keith@tungstengraphics.com>
36 #include "main/glheader.h"
37 #include "main/macros.h"
38 #include "shader/prog_instruction.h"
39 #include "st_context.h"
41 #include "st_cb_accum.h"
42 #include "st_cb_clear.h"
43 #include "st_cb_fbo.h"
45 #include "st_program.h"
46 #include "st_public.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "st_inlines.h"
50 #include "pipe/p_context.h"
51 #include "pipe/p_inlines.h"
52 #include "pipe/p_state.h"
53 #include "pipe/p_defines.h"
54 #include "util/u_pack_color.h"
55 #include "util/u_simple_shaders.h"
56 #include "util/u_draw_quad.h"
58 #include "cso_cache/cso_context.h"
62 st_init_clear(struct st_context *st)
64 struct pipe_context *pipe = st->pipe;
66 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
67 st->clear.raster.gl_rasterization_rules = 1;
69 /* rasterizer state: bypass vertex shader, clipping and viewport */
70 st->clear.raster.bypass_vs_clip_and_viewport = 1;
72 /* fragment shader state: color pass-through program */
74 util_make_fragment_passthrough_shader(pipe);
76 /* vertex shader state: color/position pass-through */
78 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
79 TGSI_SEMANTIC_COLOR };
80 const uint semantic_indexes[] = { 0, 0 };
81 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
89 st_destroy_clear(struct st_context *st)
92 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
96 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
100 pipe_buffer_reference(&st->clear.vbuf, NULL);
101 st->clear.vbuf = NULL;
107 * Draw a screen-aligned quadrilateral.
108 * Coords are window coords with y=0=bottom. These will be passed
109 * through unmodified to the rasterizer as we have set
110 * rasterizer->bypass_vs_clip_and_viewport.
113 draw_quad(GLcontext *ctx,
114 float x0, float y0, float x1, float y1, GLfloat z,
115 const GLfloat color[4])
117 struct st_context *st = ctx->st;
118 struct pipe_context *pipe = st->pipe;
119 const GLuint max_slots = 1024 / sizeof(st->clear.vertices);
122 if (st->clear.vbuf_slot >= max_slots) {
123 pipe_buffer_reference(&st->clear.vbuf, NULL);
124 st->clear.vbuf_slot = 0;
127 if (!st->clear.vbuf) {
128 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
129 max_slots * sizeof(st->clear.vertices));
133 st->clear.vertices[0][0][0] = x0;
134 st->clear.vertices[0][0][1] = y0;
136 st->clear.vertices[1][0][0] = x1;
137 st->clear.vertices[1][0][1] = y0;
139 st->clear.vertices[2][0][0] = x1;
140 st->clear.vertices[2][0][1] = y1;
142 st->clear.vertices[3][0][0] = x0;
143 st->clear.vertices[3][0][1] = y1;
145 /* same for all verts: */
146 for (i = 0; i < 4; i++) {
147 st->clear.vertices[i][0][2] = z;
148 st->clear.vertices[i][0][3] = 1.0;
149 st->clear.vertices[i][1][0] = color[0];
150 st->clear.vertices[i][1][1] = color[1];
151 st->clear.vertices[i][1][2] = color[2];
152 st->clear.vertices[i][1][3] = color[3];
155 /* put vertex data into vbuf */
156 st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
157 st->clear.vbuf_slot * sizeof(st->clear.vertices),
158 sizeof(st->clear.vertices),
162 util_draw_vertex_buffer(pipe,
164 st->clear.vbuf_slot * sizeof(st->clear.vertices),
165 PIPE_PRIM_TRIANGLE_FAN,
167 2); /* attribs/vert */
170 st->clear.vbuf_slot++;
176 * Do glClear by drawing a quadrilateral.
177 * The vertices of the quad will be computed from the
178 * ctx->DrawBuffer->_X/Ymin/max fields.
181 clear_with_quad(GLcontext *ctx,
182 GLboolean color, GLboolean depth, GLboolean stencil)
184 struct st_context *st = ctx->st;
185 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
186 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
189 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
190 y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
191 y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
194 y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
195 y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
199 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
200 color ? "color, " : "",
201 depth ? "depth, " : "",
202 stencil ? "stencil" : "",
207 cso_save_blend(st->cso_context);
208 cso_save_depth_stencil_alpha(st->cso_context);
209 cso_save_rasterizer(st->cso_context);
210 cso_save_fragment_shader(st->cso_context);
211 cso_save_vertex_shader(st->cso_context);
213 /* blend state: RGBA masking */
215 struct pipe_blend_state blend;
216 memset(&blend, 0, sizeof(blend));
217 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
218 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
219 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
220 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
222 if (ctx->Color.ColorMask[0])
223 blend.colormask |= PIPE_MASK_R;
224 if (ctx->Color.ColorMask[1])
225 blend.colormask |= PIPE_MASK_G;
226 if (ctx->Color.ColorMask[2])
227 blend.colormask |= PIPE_MASK_B;
228 if (ctx->Color.ColorMask[3])
229 blend.colormask |= PIPE_MASK_A;
230 if (st->ctx->Color.DitherFlag)
233 cso_set_blend(st->cso_context, &blend);
236 /* depth_stencil state: always pass/set to ref value */
238 struct pipe_depth_stencil_alpha_state depth_stencil;
239 memset(&depth_stencil, 0, sizeof(depth_stencil));
241 depth_stencil.depth.enabled = 1;
242 depth_stencil.depth.writemask = 1;
243 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
247 depth_stencil.stencil[0].enabled = 1;
248 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
249 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
250 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
251 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
252 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
253 depth_stencil.stencil[0].valuemask = 0xff;
254 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
257 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
260 cso_set_rasterizer(st->cso_context, &st->clear.raster);
262 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
263 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
265 /* draw quad matching scissor rect (XXX verify coord round-off) */
266 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
268 /* Restore pipe state */
269 cso_restore_blend(st->cso_context);
270 cso_restore_depth_stencil_alpha(st->cso_context);
271 cso_restore_rasterizer(st->cso_context);
272 cso_restore_fragment_shader(st->cso_context);
273 cso_restore_vertex_shader(st->cso_context);
278 * Determine if we need to clear the depth buffer by drawing a quad.
280 static INLINE GLboolean
281 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
283 if (ctx->Scissor.Enabled &&
284 (ctx->Scissor.X != 0 ||
285 ctx->Scissor.Y != 0 ||
286 ctx->Scissor.Width < rb->Width ||
287 ctx->Scissor.Height < rb->Height))
290 if (!ctx->Color.ColorMask[0] ||
291 !ctx->Color.ColorMask[1] ||
292 !ctx->Color.ColorMask[2] ||
293 !ctx->Color.ColorMask[3])
300 static INLINE GLboolean
301 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
303 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
304 GLboolean maskStencil
305 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
307 if (ctx->Scissor.Enabled &&
308 (ctx->Scissor.X != 0 ||
309 ctx->Scissor.Y != 0 ||
310 ctx->Scissor.Width < rb->Width ||
311 ctx->Scissor.Height < rb->Height))
322 * Determine if we need to clear the depth buffer by drawing a quad.
324 static INLINE GLboolean
325 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
327 const struct st_renderbuffer *strb = st_renderbuffer(rb);
328 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
330 if (ctx->Scissor.Enabled &&
331 (ctx->Scissor.X != 0 ||
332 ctx->Scissor.Y != 0 ||
333 ctx->Scissor.Width < rb->Width ||
334 ctx->Scissor.Height < rb->Height))
338 ctx->DrawBuffer->Visual.stencilBits > 0)
346 * Determine if we need to clear the stencil buffer by drawing a quad.
348 static INLINE GLboolean
349 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
351 const struct st_renderbuffer *strb = st_renderbuffer(rb);
352 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
353 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
354 const GLboolean maskStencil
355 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
360 if (ctx->Scissor.Enabled &&
361 (ctx->Scissor.X != 0 ||
362 ctx->Scissor.Y != 0 ||
363 ctx->Scissor.Width < rb->Width ||
364 ctx->Scissor.Height < rb->Height))
367 /* This is correct, but it is necessary to look at the depth clear
368 * value held in the surface when it comes time to issue the clear,
369 * rather than taking depth and stencil clear values from the
373 ctx->DrawBuffer->Visual.depthBits > 0)
381 void st_flush_clear( struct st_context *st )
383 /* Release vertex buffer to avoid synchronous rendering if we were
384 * to map it in the next frame.
386 pipe_buffer_reference(&st->clear.vbuf, NULL);
387 st->clear.vbuf_slot = 0;
393 * Called via ctx->Driver.Clear()
394 * XXX: doesn't pick up the differences between front/back/left/right
395 * clears. Need to sort that out...
397 static void st_clear(GLcontext *ctx, GLbitfield mask)
399 static const GLbitfield BUFFER_BITS_DS
400 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
401 struct st_context *st = ctx->st;
402 struct gl_renderbuffer *depthRb
403 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
404 struct gl_renderbuffer *stencilRb
405 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
406 GLbitfield quad_buffers = 0;
407 GLbitfield clear_buffers = 0;
410 /* This makes sure the pipe has the latest scissor, etc values */
411 st_validate_state( st );
413 if (mask & BUFFER_BITS_COLOR) {
414 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
415 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
417 if (mask & (1 << b)) {
418 struct gl_renderbuffer *rb
419 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
420 struct st_renderbuffer *strb;
424 strb = st_renderbuffer(rb);
429 if (check_clear_color_with_quad( ctx, rb ))
430 quad_buffers |= PIPE_CLEAR_COLOR;
432 clear_buffers |= PIPE_CLEAR_COLOR;
437 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
438 /* clearing combined depth + stencil */
439 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
442 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
443 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
445 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
449 /* separate depth/stencil clears */
450 if (mask & BUFFER_BIT_DEPTH) {
451 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
454 if (check_clear_depth_with_quad(ctx, depthRb))
455 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
457 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
460 if (mask & BUFFER_BIT_STENCIL) {
461 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
464 if (check_clear_stencil_with_quad(ctx, stencilRb))
465 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
467 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
473 * If we're going to use clear_with_quad() for any reason, use it for
474 * everything possible.
477 quad_buffers |= clear_buffers;
479 quad_buffers & PIPE_CLEAR_COLOR,
480 mask & BUFFER_BIT_DEPTH,
481 mask & BUFFER_BIT_STENCIL);
482 } else if (clear_buffers)
483 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
484 ctx->Depth.Clear, ctx->Stencil.Clear);
486 if (mask & BUFFER_BIT_ACCUM)
487 st_clear_accum_buffer(ctx,
488 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
492 void st_init_clear_functions(struct dd_function_table *functions)
494 functions->Clear = st_clear;