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/formats.h"
38 #include "main/macros.h"
39 #include "shader/prog_instruction.h"
40 #include "st_context.h"
42 #include "st_cb_accum.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
46 #include "st_program.h"
47 #include "st_public.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_inlines.h"
51 #include "pipe/p_context.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_format.h"
56 #include "util/u_pack_color.h"
57 #include "util/u_simple_shaders.h"
58 #include "util/u_draw_quad.h"
60 #include "cso_cache/cso_context.h"
64 st_init_clear(struct st_context
*st
)
66 struct pipe_context
*pipe
= st
->pipe
;
68 memset(&st
->clear
.raster
, 0, sizeof(st
->clear
.raster
));
69 st
->clear
.raster
.gl_rasterization_rules
= 1;
71 /* rasterizer state: bypass vertex shader, clipping and viewport */
72 st
->clear
.raster
.bypass_vs_clip_and_viewport
= 1;
74 /* fragment shader state: color pass-through program */
76 util_make_fragment_passthrough_shader(pipe
);
78 /* vertex shader state: color/position pass-through */
80 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
81 TGSI_SEMANTIC_COLOR
};
82 const uint semantic_indexes
[] = { 0, 0 };
83 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
91 st_destroy_clear(struct st_context
*st
)
94 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
98 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
101 if (st
->clear
.vbuf
) {
102 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
103 st
->clear
.vbuf
= NULL
;
109 * Draw a screen-aligned quadrilateral.
110 * Coords are window coords with y=0=bottom. These will be passed
111 * through unmodified to the rasterizer as we have set
112 * rasterizer->bypass_vs_clip_and_viewport.
115 draw_quad(GLcontext
*ctx
,
116 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
117 const GLfloat color
[4])
119 struct st_context
*st
= ctx
->st
;
120 struct pipe_context
*pipe
= st
->pipe
;
122 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
123 * no_flush) updates to buffers where we know there is no conflict
124 * with previous data. Currently using max_slots > 1 will cause
125 * synchronous rendering if the driver flushes its command buffers
126 * between one bitmap and the next. Our flush hook below isn't
127 * sufficient to catch this as the driver doesn't tell us when it
128 * flushes its own command buffers. Until this gets fixed, pay the
129 * price of allocating a new buffer for each bitmap cache-flush to
130 * avoid synchronous rendering.
132 const GLuint max_slots
= 1; /* 1024 / sizeof(st->clear.vertices); */
135 if (st
->clear
.vbuf_slot
>= max_slots
) {
136 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
137 st
->clear
.vbuf_slot
= 0;
140 if (!st
->clear
.vbuf
) {
141 st
->clear
.vbuf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
142 max_slots
* sizeof(st
->clear
.vertices
));
146 st
->clear
.vertices
[0][0][0] = x0
;
147 st
->clear
.vertices
[0][0][1] = y0
;
149 st
->clear
.vertices
[1][0][0] = x1
;
150 st
->clear
.vertices
[1][0][1] = y0
;
152 st
->clear
.vertices
[2][0][0] = x1
;
153 st
->clear
.vertices
[2][0][1] = y1
;
155 st
->clear
.vertices
[3][0][0] = x0
;
156 st
->clear
.vertices
[3][0][1] = y1
;
158 /* same for all verts: */
159 for (i
= 0; i
< 4; i
++) {
160 st
->clear
.vertices
[i
][0][2] = z
;
161 st
->clear
.vertices
[i
][0][3] = 1.0;
162 st
->clear
.vertices
[i
][1][0] = color
[0];
163 st
->clear
.vertices
[i
][1][1] = color
[1];
164 st
->clear
.vertices
[i
][1][2] = color
[2];
165 st
->clear
.vertices
[i
][1][3] = color
[3];
168 /* put vertex data into vbuf */
169 st_no_flush_pipe_buffer_write(st
, st
->clear
.vbuf
,
170 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
171 sizeof(st
->clear
.vertices
),
175 util_draw_vertex_buffer(pipe
,
177 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
178 PIPE_PRIM_TRIANGLE_FAN
,
180 2); /* attribs/vert */
183 st
->clear
.vbuf_slot
++;
189 * Do glClear by drawing a quadrilateral.
190 * The vertices of the quad will be computed from the
191 * ctx->DrawBuffer->_X/Ymin/max fields.
194 clear_with_quad(GLcontext
*ctx
,
195 GLboolean color
, GLboolean depth
, GLboolean stencil
)
197 struct st_context
*st
= ctx
->st
;
198 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
199 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
202 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
203 y0
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
);
204 y1
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
);
207 y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
208 y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
212 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
213 color ? "color, " : "",
214 depth ? "depth, " : "",
215 stencil ? "stencil" : "",
220 cso_save_blend(st
->cso_context
);
221 cso_save_depth_stencil_alpha(st
->cso_context
);
222 cso_save_rasterizer(st
->cso_context
);
223 cso_save_fragment_shader(st
->cso_context
);
224 cso_save_vertex_shader(st
->cso_context
);
226 /* blend state: RGBA masking */
228 struct pipe_blend_state blend
;
229 memset(&blend
, 0, sizeof(blend
));
230 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
231 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
232 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
233 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
235 if (ctx
->Color
.ColorMask
[0])
236 blend
.colormask
|= PIPE_MASK_R
;
237 if (ctx
->Color
.ColorMask
[1])
238 blend
.colormask
|= PIPE_MASK_G
;
239 if (ctx
->Color
.ColorMask
[2])
240 blend
.colormask
|= PIPE_MASK_B
;
241 if (ctx
->Color
.ColorMask
[3])
242 blend
.colormask
|= PIPE_MASK_A
;
243 if (st
->ctx
->Color
.DitherFlag
)
246 cso_set_blend(st
->cso_context
, &blend
);
249 /* depth_stencil state: always pass/set to ref value */
251 struct pipe_depth_stencil_alpha_state depth_stencil
;
252 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
254 depth_stencil
.depth
.enabled
= 1;
255 depth_stencil
.depth
.writemask
= 1;
256 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
260 depth_stencil
.stencil
[0].enabled
= 1;
261 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
262 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
263 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
264 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
265 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
266 depth_stencil
.stencil
[0].valuemask
= 0xff;
267 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
270 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
273 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
275 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
276 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
278 /* draw quad matching scissor rect (XXX verify coord round-off) */
279 draw_quad(ctx
, x0
, y0
, x1
, y1
, (GLfloat
) ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
281 /* Restore pipe state */
282 cso_restore_blend(st
->cso_context
);
283 cso_restore_depth_stencil_alpha(st
->cso_context
);
284 cso_restore_rasterizer(st
->cso_context
);
285 cso_restore_fragment_shader(st
->cso_context
);
286 cso_restore_vertex_shader(st
->cso_context
);
291 * Determine if we need to clear the depth buffer by drawing a quad.
293 static INLINE GLboolean
294 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
296 if (ctx
->Scissor
.Enabled
&&
297 (ctx
->Scissor
.X
!= 0 ||
298 ctx
->Scissor
.Y
!= 0 ||
299 ctx
->Scissor
.Width
< rb
->Width
||
300 ctx
->Scissor
.Height
< rb
->Height
))
303 if (!ctx
->Color
.ColorMask
[0] ||
304 !ctx
->Color
.ColorMask
[1] ||
305 !ctx
->Color
.ColorMask
[2] ||
306 !ctx
->Color
.ColorMask
[3])
313 static INLINE GLboolean
314 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
316 const GLuint stencilMax
= 0xff;
317 GLboolean maskStencil
318 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
320 assert(rb
->Format
== MESA_FORMAT_S8
||
321 rb
->Format
== MESA_FORMAT_Z24_S8
||
322 rb
->Format
== MESA_FORMAT_S8_Z24
);
324 if (ctx
->Scissor
.Enabled
&&
325 (ctx
->Scissor
.X
!= 0 ||
326 ctx
->Scissor
.Y
!= 0 ||
327 ctx
->Scissor
.Width
< rb
->Width
||
328 ctx
->Scissor
.Height
< rb
->Height
))
339 * Determine if we need to clear the depth buffer by drawing a quad.
341 static INLINE GLboolean
342 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
344 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
345 const GLboolean isDS
= util_format_is_depth_and_stencil(strb
->surface
->format
);
347 if (ctx
->Scissor
.Enabled
&&
348 (ctx
->Scissor
.X
!= 0 ||
349 ctx
->Scissor
.Y
!= 0 ||
350 ctx
->Scissor
.Width
< rb
->Width
||
351 ctx
->Scissor
.Height
< rb
->Height
))
355 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
363 * Determine if we need to clear the stencil buffer by drawing a quad.
365 static INLINE GLboolean
366 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
368 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
369 const GLboolean isDS
= util_format_is_depth_and_stencil(strb
->surface
->format
);
370 const GLuint stencilMax
= 0xff;
371 const GLboolean maskStencil
372 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
374 assert(rb
->Format
== MESA_FORMAT_S8
||
375 rb
->Format
== MESA_FORMAT_Z24_S8
||
376 rb
->Format
== MESA_FORMAT_S8_Z24
);
381 if (ctx
->Scissor
.Enabled
&&
382 (ctx
->Scissor
.X
!= 0 ||
383 ctx
->Scissor
.Y
!= 0 ||
384 ctx
->Scissor
.Width
< rb
->Width
||
385 ctx
->Scissor
.Height
< rb
->Height
))
388 /* This is correct, but it is necessary to look at the depth clear
389 * value held in the surface when it comes time to issue the clear,
390 * rather than taking depth and stencil clear values from the
394 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
402 void st_flush_clear( struct st_context
*st
)
404 /* Release vertex buffer to avoid synchronous rendering if we were
405 * to map it in the next frame.
407 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
408 st
->clear
.vbuf_slot
= 0;
414 * Called via ctx->Driver.Clear()
415 * XXX: doesn't pick up the differences between front/back/left/right
416 * clears. Need to sort that out...
418 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
420 static const GLbitfield BUFFER_BITS_DS
421 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
422 struct st_context
*st
= ctx
->st
;
423 struct gl_renderbuffer
*depthRb
424 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
425 struct gl_renderbuffer
*stencilRb
426 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
427 GLbitfield quad_buffers
= 0;
428 GLbitfield clear_buffers
= 0;
431 /* This makes sure the pipe has the latest scissor, etc values */
432 st_validate_state( st
);
434 if (mask
& BUFFER_BITS_COLOR
) {
435 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
436 GLuint b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
438 if (mask
& (1 << b
)) {
439 struct gl_renderbuffer
*rb
440 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
441 struct st_renderbuffer
*strb
;
445 strb
= st_renderbuffer(rb
);
450 if (check_clear_color_with_quad( ctx
, rb
))
451 quad_buffers
|= PIPE_CLEAR_COLOR
;
453 clear_buffers
|= PIPE_CLEAR_COLOR
;
458 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
459 /* clearing combined depth + stencil */
460 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
463 if (check_clear_depth_stencil_with_quad(ctx
, depthRb
))
464 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
466 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
470 /* separate depth/stencil clears */
471 if (mask
& BUFFER_BIT_DEPTH
) {
472 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
475 if (check_clear_depth_with_quad(ctx
, depthRb
))
476 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
478 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
481 if (mask
& BUFFER_BIT_STENCIL
) {
482 struct st_renderbuffer
*strb
= st_renderbuffer(stencilRb
);
485 if (check_clear_stencil_with_quad(ctx
, stencilRb
))
486 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
488 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
494 * If we're going to use clear_with_quad() for any reason, use it for
495 * everything possible.
498 quad_buffers
|= clear_buffers
;
500 quad_buffers
& PIPE_CLEAR_COLOR
,
501 mask
& BUFFER_BIT_DEPTH
,
502 mask
& BUFFER_BIT_STENCIL
);
503 } else if (clear_buffers
)
504 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, clear_buffers
, ctx
->Color
.ClearColor
,
505 ctx
->Depth
.Clear
, ctx
->Stencil
.Clear
);
507 if (mask
& BUFFER_BIT_ACCUM
)
508 st_clear_accum_buffer(ctx
,
509 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
513 void st_init_clear_functions(struct dd_function_table
*functions
)
515 functions
->Clear
= st_clear
;