1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "main/glheader.h"
35 #include "main/macros.h"
36 #include "shader/prog_instruction.h"
38 #include "st_context.h"
39 #include "st_cb_clear.h"
40 #include "st_cb_fbo.h"
41 #include "st_program.h"
42 #include "st_public.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
52 color_value(GLuint pipeFormat
, const GLfloat color
[4])
56 UNCLAMPED_FLOAT_TO_UBYTE(r
, color
[0]);
57 UNCLAMPED_FLOAT_TO_UBYTE(g
, color
[1]);
58 UNCLAMPED_FLOAT_TO_UBYTE(b
, color
[2]);
59 UNCLAMPED_FLOAT_TO_UBYTE(a
, color
[3]);
62 case PIPE_FORMAT_U_R8_G8_B8_A8
:
63 return (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
64 case PIPE_FORMAT_U_A8_R8_G8_B8
:
65 return (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
66 case PIPE_FORMAT_U_R5_G6_B5
:
67 return ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
75 depth_value(GLuint pipeFormat
, GLfloat value
)
79 case PIPE_FORMAT_U_Z16
:
80 val
= (GLuint
) (value
* 0xffffff);
82 case PIPE_FORMAT_U_Z32
:
83 val
= (GLuint
) (value
* 0xffffffff);
85 case PIPE_FORMAT_S8_Z24
:
86 /*case PIPE_FORMAT_Z24_S8:*/
87 val
= (GLuint
) (value
* 0xffffff);
97 is_depth_stencil_format(GLuint pipeFormat
)
100 case PIPE_FORMAT_S8_Z24
:
101 /*case PIPE_FORMAT_Z24_S8:*/
111 * Create a simple fragment shader that just passes through the fragment color.
113 static struct st_fragment_program
*
114 make_color_shader(struct st_context
*st
)
116 GLcontext
*ctx
= st
->ctx
;
117 struct st_fragment_program
*stfp
;
118 struct gl_program
*p
;
121 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
125 p
->NumInstructions
= 2;
126 p
->Instructions
= _mesa_alloc_instructions(2);
127 if (!p
->Instructions
) {
128 ctx
->Driver
.DeleteProgram(ctx
, p
);
131 _mesa_init_instructions(p
->Instructions
, 2);
132 /* MOV result.color, fragment.color; */
133 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
134 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
135 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
136 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
137 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
139 p
->Instructions
[1].Opcode
= OPCODE_END
;
141 p
->InputsRead
= FRAG_BIT_COL0
;
142 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
144 stfp
= (struct st_fragment_program
*) p
;
145 /* compile into tgsi format */
146 b
= tgsi_mesa_compile_fp_program(&stfp
->Base
,
147 stfp
->tokens
, ST_FP_MAX_TOKENS
);
155 * Draw a screen-aligned quadrilateral.
156 * Coords are window coords.
159 draw_quad(GLcontext
*ctx
,
160 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
161 const GLfloat color
[4])
163 static const GLuint attribs
[2] = {
167 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
183 /* same for all verts: */
184 for (i
= 0; i
< 4; i
++) {
186 verts
[i
][0][3] = 1.0;
187 verts
[i
][1][0] = color
[0];
188 verts
[i
][1][1] = color
[1];
189 verts
[i
][1][2] = color
[2];
190 verts
[i
][1][3] = color
[3];
193 ctx
->st
->pipe
->draw_vertices(ctx
->st
->pipe
, GL_QUADS
,
194 4, (GLfloat
*) verts
, 2, attribs
);
200 * Do glClear by drawing a quadrilateral.
203 clear_with_quad(GLcontext
*ctx
, GLuint x0
, GLuint y0
,
204 GLuint x1
, GLuint y1
,
205 GLboolean color
, GLboolean depth
, GLboolean stencil
)
207 static struct st_fragment_program
*stfp
= NULL
;
208 struct st_context
*st
= ctx
->st
;
209 struct pipe_alpha_test_state alpha_test
;
210 struct pipe_blend_state blend
;
211 struct pipe_depth_state depth_test
;
212 struct pipe_stencil_state stencil_test
;
213 struct pipe_setup_state setup
;
214 struct pipe_fs_state fs
;
216 /* alpha state: disabled */
217 memset(&alpha_test
, 0, sizeof(alpha_test
));
218 st
->pipe
->set_alpha_test_state(st
->pipe
, &alpha_test
);
220 /* blend state: RGBA masking */
221 memset(&blend
, 0, sizeof(blend
));
223 if (ctx
->Color
.ColorMask
[0])
224 blend
.colormask
|= PIPE_MASK_R
;
225 if (ctx
->Color
.ColorMask
[1])
226 blend
.colormask
|= PIPE_MASK_G
;
227 if (ctx
->Color
.ColorMask
[2])
228 blend
.colormask
|= PIPE_MASK_B
;
229 if (ctx
->Color
.ColorMask
[3])
230 blend
.colormask
|= PIPE_MASK_A
;
231 if (st
->ctx
->Color
.DitherFlag
)
234 st
->pipe
->set_blend_state(st
->pipe
, &blend
);
236 /* depth state: always pass */
237 memset(&depth_test
, 0, sizeof(depth_test
));
239 depth_test
.enabled
= 1;
240 depth_test
.writemask
= 1;
241 depth_test
.func
= PIPE_FUNC_ALWAYS
;
243 st
->pipe
->set_depth_state(st
->pipe
, &depth_test
);
245 /* setup state: nothing */
246 memset(&setup
, 0, sizeof(setup
));
247 if (ctx
->Scissor
.Enabled
)
249 st
->pipe
->set_setup_state(st
->pipe
, &setup
);
251 /* stencil state: always set to ref value */
252 memset(&stencil_test
, 0, sizeof(stencil_test
));
254 stencil_test
.front_enabled
= 1;
255 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
256 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
257 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
258 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
259 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
260 stencil_test
.value_mask
[0] = 0xff;
261 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
263 st
->pipe
->set_stencil_state(st
->pipe
, &stencil_test
);
265 /* fragment shader state: color pass-through program */
267 stfp
= make_color_shader(st
);
269 memset(&fs
, 0, sizeof(fs
));
270 fs
.inputs_read
= stfp
->Base
.Base
.InputsRead
;
271 fs
.tokens
= &stfp
->tokens
[0];
273 st
->pipe
->set_fs_state(st
->pipe
, &fs
);
275 /* draw quad matching scissor rect (XXX verify coord round-off) */
276 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
278 /* Restore pipe state */
279 st
->pipe
->set_alpha_test_state(st
->pipe
, &st
->state
.alpha_test
);
280 st
->pipe
->set_blend_state(st
->pipe
, &st
->state
.blend
);
281 st
->pipe
->set_depth_state(st
->pipe
, &st
->state
.depth
);
282 st
->pipe
->set_fs_state(st
->pipe
, &st
->state
.fs
);
283 st
->pipe
->set_setup_state(st
->pipe
, &st
->state
.setup
);
284 st
->pipe
->set_stencil_state(st
->pipe
, &st
->state
.stencil
);
286 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
292 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
294 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
296 if (ctx
->Color
.ColorMask
[0] &&
297 ctx
->Color
.ColorMask
[1] &&
298 ctx
->Color
.ColorMask
[2] &&
299 ctx
->Color
.ColorMask
[3] &&
300 !ctx
->Scissor
.Enabled
)
302 /* clear whole buffer w/out masking */
304 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
305 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
308 /* masking or scissoring */
310 ctx
->DrawBuffer
->_Xmin
,
311 ctx
->DrawBuffer
->_Xmin
,
312 ctx
->DrawBuffer
->_Xmax
,
313 ctx
->DrawBuffer
->_Ymax
,
314 GL_TRUE
, GL_FALSE
, GL_FALSE
);
320 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
322 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
324 if (!ctx
->Scissor
.Enabled
) {
325 /* clear whole buffer w/out masking */
327 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
328 /* Note that clearValue is 32 bits but the accum buffer will
329 * typically be 64bpp...
331 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
335 /* XXX point framebuffer.cbufs[0] at the accum buffer */
337 ctx
->DrawBuffer
->_Xmin
,
338 ctx
->DrawBuffer
->_Xmin
,
339 ctx
->DrawBuffer
->_Xmax
,
340 ctx
->DrawBuffer
->_Ymax
,
341 GL_TRUE
, GL_FALSE
, GL_FALSE
);
347 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
349 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
351 if (!ctx
->Scissor
.Enabled
&&
352 !is_depth_stencil_format(strb
->surface
->format
)) {
353 /* clear whole depth buffer w/out masking */
354 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
355 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
358 /* masking or scissoring or combined z/stencil buffer */
360 ctx
->DrawBuffer
->_Xmin
,
361 ctx
->DrawBuffer
->_Xmin
,
362 ctx
->DrawBuffer
->_Xmax
,
363 ctx
->DrawBuffer
->_Ymax
,
364 GL_FALSE
, GL_TRUE
, GL_FALSE
);
370 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
372 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
373 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
374 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
376 if (!maskStencil
&& !ctx
->Scissor
.Enabled
&&
377 !is_depth_stencil_format(strb
->surface
->format
)) {
378 /* clear whole stencil buffer w/out masking */
379 GLuint clearValue
= ctx
->Stencil
.Clear
;
380 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
383 /* masking or scissoring */
385 ctx
->DrawBuffer
->_Xmin
,
386 ctx
->DrawBuffer
->_Xmin
,
387 ctx
->DrawBuffer
->_Xmax
,
388 ctx
->DrawBuffer
->_Ymax
,
389 GL_FALSE
, GL_FALSE
, GL_TRUE
);
395 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
397 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
398 const GLuint stencilMax
= 1 << rb
->StencilBits
;
399 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
401 assert(is_depth_stencil_format(strb
->surface
->format
));
403 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
404 /* clear whole buffer w/out masking */
405 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
407 switch (strb
->surface
->format
) {
408 case PIPE_FORMAT_S8_Z24
:
409 clearValue
|= ctx
->Stencil
.Clear
<< 24;
412 case PIPE_FORMAT_Z24_S8
:
413 clearValue
= (clearValue
<< 8) | clearVal
;
420 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
423 /* masking or scissoring */
425 ctx
->DrawBuffer
->_Xmin
,
426 ctx
->DrawBuffer
->_Xmin
,
427 ctx
->DrawBuffer
->_Xmax
,
428 ctx
->DrawBuffer
->_Ymax
,
429 GL_FALSE
, GL_TRUE
, GL_TRUE
);
436 * Called via ctx->Driver.Clear()
437 * XXX: doesn't pick up the differences between front/back/left/right
438 * clears. Need to sort that out...
440 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
442 static const GLbitfield BUFFER_BITS_DS
443 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
444 struct st_context
*st
= ctx
->st
;
445 struct gl_renderbuffer
*depthRb
446 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
447 struct gl_renderbuffer
*stencilRb
448 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
450 /* This makes sure the softpipe has the latest scissor, etc values */
451 st_validate_state( st
);
455 * If we're going to use clear_with_quad() for any reason, use it to
456 * clear as many other buffers as possible.
457 * As it is now, we sometimes call clear_with_quad() three times to clear
458 * color/depth/stencil individually...
461 if (mask
& BUFFER_BITS_COLOR
) {
463 for (b
= 0; b
< BUFFER_COUNT
; b
++) {
464 if (BUFFER_BITS_COLOR
& mask
& (1 << b
)) {
465 clear_color_buffer(ctx
,
466 ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
);
471 if (mask
& BUFFER_BIT_ACCUM
) {
472 clear_accum_buffer(ctx
,
473 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
476 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
477 /* clearing combined depth + stencil */
478 clear_depth_stencil_buffer(ctx
, depthRb
);
481 /* separate depth/stencil clears */
482 if (mask
& BUFFER_BIT_DEPTH
) {
483 clear_depth_buffer(ctx
, depthRb
);
485 if (mask
& BUFFER_BIT_STENCIL
) {
486 clear_stencil_buffer(ctx
, stencilRb
);
492 void st_init_clear_functions(struct dd_function_table
*functions
)
494 functions
->Clear
= st_clear
;