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"
45 #include "st_program.h"
46 #include "st_public.h"
47 #include "st_inlines.h"
49 #include "pipe/p_context.h"
50 #include "util/u_inlines.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_format.h"
54 #include "util/u_simple_shaders.h"
55 #include "util/u_draw_quad.h"
57 #include "cso_cache/cso_context.h"
61 st_init_clear(struct st_context
*st
)
63 struct pipe_context
*pipe
= st
->pipe
;
65 memset(&st
->clear
.raster
, 0, sizeof(st
->clear
.raster
));
66 st
->clear
.raster
.gl_rasterization_rules
= 1;
68 /* rasterizer state: bypass vertex shader, clipping and viewport */
69 st
->clear
.raster
.bypass_vs_clip_and_viewport
= 1;
71 /* fragment shader state: color pass-through program */
73 util_make_fragment_passthrough_shader(pipe
);
75 /* vertex shader state: color/position pass-through */
77 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
78 TGSI_SEMANTIC_COLOR
};
79 const uint semantic_indexes
[] = { 0, 0 };
80 st
->clear
.vs
= util_make_vertex_passthrough_shader(pipe
, 2,
88 st_destroy_clear(struct st_context
*st
)
91 cso_delete_fragment_shader(st
->cso_context
, st
->clear
.fs
);
95 cso_delete_vertex_shader(st
->cso_context
, st
->clear
.vs
);
99 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
100 st
->clear
.vbuf
= NULL
;
106 * Draw a screen-aligned quadrilateral.
107 * Coords are window coords with y=0=bottom. These will be passed
108 * through unmodified to the rasterizer as we have set
109 * rasterizer->bypass_vs_clip_and_viewport.
112 draw_quad(GLcontext
*ctx
,
113 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
114 const GLfloat color
[4])
116 struct st_context
*st
= ctx
->st
;
117 struct pipe_context
*pipe
= st
->pipe
;
119 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
120 * no_flush) updates to buffers where we know there is no conflict
121 * with previous data. Currently using max_slots > 1 will cause
122 * synchronous rendering if the driver flushes its command buffers
123 * between one bitmap and the next. Our flush hook below isn't
124 * sufficient to catch this as the driver doesn't tell us when it
125 * flushes its own command buffers. Until this gets fixed, pay the
126 * price of allocating a new buffer for each bitmap cache-flush to
127 * avoid synchronous rendering.
129 const GLuint max_slots
= 1; /* 1024 / sizeof(st->clear.vertices); */
132 if (st
->clear
.vbuf_slot
>= max_slots
) {
133 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
134 st
->clear
.vbuf_slot
= 0;
137 if (!st
->clear
.vbuf
) {
138 st
->clear
.vbuf
= pipe_buffer_create(pipe
->screen
, 32, PIPE_BUFFER_USAGE_VERTEX
,
139 max_slots
* sizeof(st
->clear
.vertices
));
143 st
->clear
.vertices
[0][0][0] = x0
;
144 st
->clear
.vertices
[0][0][1] = y0
;
146 st
->clear
.vertices
[1][0][0] = x1
;
147 st
->clear
.vertices
[1][0][1] = y0
;
149 st
->clear
.vertices
[2][0][0] = x1
;
150 st
->clear
.vertices
[2][0][1] = y1
;
152 st
->clear
.vertices
[3][0][0] = x0
;
153 st
->clear
.vertices
[3][0][1] = y1
;
155 /* same for all verts: */
156 for (i
= 0; i
< 4; i
++) {
157 st
->clear
.vertices
[i
][0][2] = z
;
158 st
->clear
.vertices
[i
][0][3] = 1.0;
159 st
->clear
.vertices
[i
][1][0] = color
[0];
160 st
->clear
.vertices
[i
][1][1] = color
[1];
161 st
->clear
.vertices
[i
][1][2] = color
[2];
162 st
->clear
.vertices
[i
][1][3] = color
[3];
165 /* put vertex data into vbuf */
166 st_no_flush_pipe_buffer_write_nooverlap(st
, st
->clear
.vbuf
,
167 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
168 sizeof(st
->clear
.vertices
),
172 util_draw_vertex_buffer(pipe
,
174 st
->clear
.vbuf_slot
* sizeof(st
->clear
.vertices
),
175 PIPE_PRIM_TRIANGLE_FAN
,
177 2); /* attribs/vert */
180 st
->clear
.vbuf_slot
++;
186 * Do glClear by drawing a quadrilateral.
187 * The vertices of the quad will be computed from the
188 * ctx->DrawBuffer->_X/Ymin/max fields.
191 clear_with_quad(GLcontext
*ctx
,
192 GLboolean color
, GLboolean depth
, GLboolean stencil
)
194 struct st_context
*st
= ctx
->st
;
195 const GLfloat x0
= (GLfloat
) ctx
->DrawBuffer
->_Xmin
;
196 const GLfloat x1
= (GLfloat
) ctx
->DrawBuffer
->_Xmax
;
199 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
200 y0
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
);
201 y1
= (GLfloat
) (ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
);
204 y0
= (GLfloat
) ctx
->DrawBuffer
->_Ymin
;
205 y1
= (GLfloat
) ctx
->DrawBuffer
->_Ymax
;
209 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
210 color ? "color, " : "",
211 depth ? "depth, " : "",
212 stencil ? "stencil" : "",
217 cso_save_blend(st
->cso_context
);
218 cso_save_stencil_ref(st
->cso_context
);
219 cso_save_depth_stencil_alpha(st
->cso_context
);
220 cso_save_rasterizer(st
->cso_context
);
221 cso_save_fragment_shader(st
->cso_context
);
222 cso_save_vertex_shader(st
->cso_context
);
224 /* blend state: RGBA masking */
226 struct pipe_blend_state blend
;
227 memset(&blend
, 0, sizeof(blend
));
228 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
229 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
230 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
231 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
233 if (ctx
->Color
.ColorMask
[0][0])
234 blend
.rt
[0].colormask
|= PIPE_MASK_R
;
235 if (ctx
->Color
.ColorMask
[0][1])
236 blend
.rt
[0].colormask
|= PIPE_MASK_G
;
237 if (ctx
->Color
.ColorMask
[0][2])
238 blend
.rt
[0].colormask
|= PIPE_MASK_B
;
239 if (ctx
->Color
.ColorMask
[0][3])
240 blend
.rt
[0].colormask
|= PIPE_MASK_A
;
241 if (st
->ctx
->Color
.DitherFlag
)
244 cso_set_blend(st
->cso_context
, &blend
);
247 /* depth_stencil state: always pass/set to ref value */
249 struct pipe_depth_stencil_alpha_state depth_stencil
;
250 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
252 depth_stencil
.depth
.enabled
= 1;
253 depth_stencil
.depth
.writemask
= 1;
254 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
258 struct pipe_stencil_ref stencil_ref
;
259 memset(&stencil_ref
, 0, sizeof(stencil_ref
));
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].valuemask
= 0xff;
266 depth_stencil
.stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
267 stencil_ref
.ref_value
[0] = ctx
->Stencil
.Clear
;
268 cso_set_stencil_ref(st
->cso_context
, &stencil_ref
);
271 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
274 cso_set_rasterizer(st
->cso_context
, &st
->clear
.raster
);
276 cso_set_fragment_shader_handle(st
->cso_context
, st
->clear
.fs
);
277 cso_set_vertex_shader_handle(st
->cso_context
, st
->clear
.vs
);
279 /* draw quad matching scissor rect (XXX verify coord round-off) */
280 draw_quad(ctx
, x0
, y0
, x1
, y1
, (GLfloat
) ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
282 /* Restore pipe state */
283 cso_restore_blend(st
->cso_context
);
284 cso_restore_stencil_ref(st
->cso_context
);
285 cso_restore_depth_stencil_alpha(st
->cso_context
);
286 cso_restore_rasterizer(st
->cso_context
);
287 cso_restore_fragment_shader(st
->cso_context
);
288 cso_restore_vertex_shader(st
->cso_context
);
294 * Determine if we need to clear the depth buffer by drawing a quad.
296 static INLINE GLboolean
297 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
299 if (ctx
->Scissor
.Enabled
&&
300 (ctx
->Scissor
.X
!= 0 ||
301 ctx
->Scissor
.Y
!= 0 ||
302 ctx
->Scissor
.Width
< rb
->Width
||
303 ctx
->Scissor
.Height
< rb
->Height
))
306 if (!ctx
->Color
.ColorMask
[0][0] ||
307 !ctx
->Color
.ColorMask
[0][1] ||
308 !ctx
->Color
.ColorMask
[0][2] ||
309 !ctx
->Color
.ColorMask
[0][3])
316 static INLINE GLboolean
317 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
319 const GLuint stencilMax
= 0xff;
320 GLboolean maskStencil
321 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
323 assert(rb
->Format
== MESA_FORMAT_S8
||
324 rb
->Format
== MESA_FORMAT_Z24_S8
||
325 rb
->Format
== MESA_FORMAT_S8_Z24
);
327 if (ctx
->Scissor
.Enabled
&&
328 (ctx
->Scissor
.X
!= 0 ||
329 ctx
->Scissor
.Y
!= 0 ||
330 ctx
->Scissor
.Width
< rb
->Width
||
331 ctx
->Scissor
.Height
< rb
->Height
))
342 * Determine if we need to clear the depth buffer by drawing a quad.
344 static INLINE GLboolean
345 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
347 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
348 const GLboolean isDS
= util_format_is_depth_and_stencil(strb
->surface
->format
);
350 if (ctx
->Scissor
.Enabled
&&
351 (ctx
->Scissor
.X
!= 0 ||
352 ctx
->Scissor
.Y
!= 0 ||
353 ctx
->Scissor
.Width
< rb
->Width
||
354 ctx
->Scissor
.Height
< rb
->Height
))
358 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
366 * Determine if we need to clear the stencil buffer by drawing a quad.
368 static INLINE GLboolean
369 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
371 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
372 const GLboolean isDS
= util_format_is_depth_and_stencil(strb
->surface
->format
);
373 const GLuint stencilMax
= 0xff;
374 const GLboolean maskStencil
375 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
377 assert(rb
->Format
== MESA_FORMAT_S8
||
378 rb
->Format
== MESA_FORMAT_Z24_S8
||
379 rb
->Format
== MESA_FORMAT_S8_Z24
);
384 if (ctx
->Scissor
.Enabled
&&
385 (ctx
->Scissor
.X
!= 0 ||
386 ctx
->Scissor
.Y
!= 0 ||
387 ctx
->Scissor
.Width
< rb
->Width
||
388 ctx
->Scissor
.Height
< rb
->Height
))
391 /* This is correct, but it is necessary to look at the depth clear
392 * value held in the surface when it comes time to issue the clear,
393 * rather than taking depth and stencil clear values from the
397 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
405 void st_flush_clear( struct st_context
*st
)
407 /* Release vertex buffer to avoid synchronous rendering if we were
408 * to map it in the next frame.
410 pipe_buffer_reference(&st
->clear
.vbuf
, NULL
);
411 st
->clear
.vbuf_slot
= 0;
417 * Called via ctx->Driver.Clear()
418 * XXX: doesn't pick up the differences between front/back/left/right
419 * clears. Need to sort that out...
421 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
423 static const GLbitfield BUFFER_BITS_DS
424 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
425 struct st_context
*st
= ctx
->st
;
426 struct gl_renderbuffer
*depthRb
427 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
428 struct gl_renderbuffer
*stencilRb
429 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
430 GLbitfield quad_buffers
= 0;
431 GLbitfield clear_buffers
= 0;
434 /* This makes sure the pipe has the latest scissor, etc values */
435 st_validate_state( st
);
437 if (mask
& BUFFER_BITS_COLOR
) {
438 for (i
= 0; i
< ctx
->DrawBuffer
->_NumColorDrawBuffers
; i
++) {
439 GLuint b
= ctx
->DrawBuffer
->_ColorDrawBufferIndexes
[i
];
441 if (mask
& (1 << b
)) {
442 struct gl_renderbuffer
*rb
443 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
444 struct st_renderbuffer
*strb
;
448 strb
= st_renderbuffer(rb
);
453 if (check_clear_color_with_quad( ctx
, rb
))
454 quad_buffers
|= PIPE_CLEAR_COLOR
;
456 clear_buffers
|= PIPE_CLEAR_COLOR
;
461 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
462 /* clearing combined depth + stencil */
463 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
466 if (check_clear_depth_stencil_with_quad(ctx
, depthRb
))
467 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
469 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
473 /* separate depth/stencil clears */
474 if (mask
& BUFFER_BIT_DEPTH
) {
475 struct st_renderbuffer
*strb
= st_renderbuffer(depthRb
);
478 if (check_clear_depth_with_quad(ctx
, depthRb
))
479 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
481 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
484 if (mask
& BUFFER_BIT_STENCIL
) {
485 struct st_renderbuffer
*strb
= st_renderbuffer(stencilRb
);
488 if (check_clear_stencil_with_quad(ctx
, stencilRb
))
489 quad_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
491 clear_buffers
|= PIPE_CLEAR_DEPTHSTENCIL
;
497 * If we're going to use clear_with_quad() for any reason, use it for
498 * everything possible.
501 quad_buffers
|= clear_buffers
;
503 quad_buffers
& PIPE_CLEAR_COLOR
,
504 mask
& BUFFER_BIT_DEPTH
,
505 mask
& BUFFER_BIT_STENCIL
);
506 } else if (clear_buffers
)
507 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, clear_buffers
, ctx
->Color
.ClearColor
,
508 ctx
->Depth
.Clear
, ctx
->Stencil
.Clear
);
510 if (mask
& BUFFER_BIT_ACCUM
)
511 st_clear_accum_buffer(ctx
,
512 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
516 void st_init_clear_functions(struct dd_function_table
*functions
)
518 functions
->Clear
= st_clear
;