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.
201 * The vertices of the quad will be computed from the
202 * ctx->DrawBuffer->_X/Ymin/max fields.
205 clear_with_quad(GLcontext
*ctx
,
206 GLboolean color
, GLboolean depth
, GLboolean stencil
)
208 struct st_context
*st
= ctx
->st
;
209 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
210 const GLfloat y0
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
;
211 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
212 const GLfloat y1
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
;
214 /* alpha state: disabled */
216 struct pipe_alpha_test_state alpha_test
;
217 memset(&alpha_test
, 0, sizeof(alpha_test
));
218 st
->pipe
->set_alpha_test_state(st
->pipe
, &alpha_test
);
221 /* blend state: RGBA masking */
223 struct pipe_blend_state blend
;
224 memset(&blend
, 0, sizeof(blend
));
226 if (ctx
->Color
.ColorMask
[0])
227 blend
.colormask
|= PIPE_MASK_R
;
228 if (ctx
->Color
.ColorMask
[1])
229 blend
.colormask
|= PIPE_MASK_G
;
230 if (ctx
->Color
.ColorMask
[2])
231 blend
.colormask
|= PIPE_MASK_B
;
232 if (ctx
->Color
.ColorMask
[3])
233 blend
.colormask
|= PIPE_MASK_A
;
234 if (st
->ctx
->Color
.DitherFlag
)
237 st
->pipe
->set_blend_state(st
->pipe
, &blend
);
240 /* depth state: always pass */
242 struct pipe_depth_state depth_test
;
243 memset(&depth_test
, 0, sizeof(depth_test
));
245 depth_test
.enabled
= 1;
246 depth_test
.writemask
= 1;
247 depth_test
.func
= PIPE_FUNC_ALWAYS
;
249 st
->pipe
->set_depth_state(st
->pipe
, &depth_test
);
252 /* setup state: nothing */
254 struct pipe_setup_state setup
;
255 memset(&setup
, 0, sizeof(setup
));
256 if (ctx
->Scissor
.Enabled
)
258 st
->pipe
->set_setup_state(st
->pipe
, &setup
);
261 /* stencil state: always set to ref value */
263 struct pipe_stencil_state stencil_test
;
264 memset(&stencil_test
, 0, sizeof(stencil_test
));
266 stencil_test
.front_enabled
= 1;
267 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
268 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
269 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
270 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
271 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
272 stencil_test
.value_mask
[0] = 0xff;
273 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
275 st
->pipe
->set_stencil_state(st
->pipe
, &stencil_test
);
278 /* fragment shader state: color pass-through program */
280 static struct st_fragment_program
*stfp
= NULL
;
281 struct pipe_fs_state fs
;
283 stfp
= make_color_shader(st
);
285 memset(&fs
, 0, sizeof(fs
));
286 fs
.inputs_read
= stfp
->Base
.Base
.InputsRead
;
287 fs
.tokens
= &stfp
->tokens
[0];
289 st
->pipe
->set_fs_state(st
->pipe
, &fs
);
292 /* draw quad matching scissor rect (XXX verify coord round-off) */
293 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
295 /* Restore pipe state */
296 st
->pipe
->set_alpha_test_state(st
->pipe
, &st
->state
.alpha_test
);
297 st
->pipe
->set_blend_state(st
->pipe
, &st
->state
.blend
);
298 st
->pipe
->set_depth_state(st
->pipe
, &st
->state
.depth
);
299 st
->pipe
->set_fs_state(st
->pipe
, &st
->state
.fs
);
300 st
->pipe
->set_setup_state(st
->pipe
, &st
->state
.setup
);
301 st
->pipe
->set_stencil_state(st
->pipe
, &st
->state
.stencil
);
303 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
309 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
311 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
313 if (ctx
->Color
.ColorMask
[0] &&
314 ctx
->Color
.ColorMask
[1] &&
315 ctx
->Color
.ColorMask
[2] &&
316 ctx
->Color
.ColorMask
[3] &&
317 !ctx
->Scissor
.Enabled
)
319 /* clear whole buffer w/out masking */
321 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
322 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
325 /* masking or scissoring */
326 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
332 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
334 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
336 if (!ctx
->Scissor
.Enabled
) {
337 /* clear whole buffer w/out masking */
339 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
340 /* Note that clearValue is 32 bits but the accum buffer will
341 * typically be 64bpp...
343 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
347 /* XXX point framebuffer.cbufs[0] at the accum buffer */
348 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
354 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
356 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
358 if (!ctx
->Scissor
.Enabled
&&
359 !is_depth_stencil_format(strb
->surface
->format
)) {
360 /* clear whole depth buffer w/out masking */
361 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
362 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
365 /* masking or scissoring or combined z/stencil buffer */
366 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
372 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
374 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
375 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
376 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
378 if (!maskStencil
&& !ctx
->Scissor
.Enabled
&&
379 !is_depth_stencil_format(strb
->surface
->format
)) {
380 /* clear whole stencil buffer w/out masking */
381 GLuint clearValue
= ctx
->Stencil
.Clear
;
382 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
385 /* masking or scissoring */
386 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
392 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
394 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
395 const GLuint stencilMax
= 1 << rb
->StencilBits
;
396 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
398 assert(is_depth_stencil_format(strb
->surface
->format
));
400 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
401 /* clear whole buffer w/out masking */
402 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
404 switch (strb
->surface
->format
) {
405 case PIPE_FORMAT_S8_Z24
:
406 clearValue
|= ctx
->Stencil
.Clear
<< 24;
409 case PIPE_FORMAT_Z24_S8
:
410 clearValue
= (clearValue
<< 8) | clearVal
;
417 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
420 /* masking or scissoring */
421 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
428 * Called via ctx->Driver.Clear()
429 * XXX: doesn't pick up the differences between front/back/left/right
430 * clears. Need to sort that out...
432 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
434 static const GLbitfield BUFFER_BITS_DS
435 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
436 struct st_context
*st
= ctx
->st
;
437 struct gl_renderbuffer
*depthRb
438 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
439 struct gl_renderbuffer
*stencilRb
440 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
442 /* This makes sure the softpipe has the latest scissor, etc values */
443 st_validate_state( st
);
447 * If we're going to use clear_with_quad() for any reason, use it to
448 * clear as many other buffers as possible.
449 * As it is now, we sometimes call clear_with_quad() three times to clear
450 * color/depth/stencil individually...
453 if (mask
& BUFFER_BITS_COLOR
) {
455 for (b
= 0; b
< BUFFER_COUNT
; b
++) {
456 if (BUFFER_BITS_COLOR
& mask
& (1 << b
)) {
457 clear_color_buffer(ctx
,
458 ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
);
463 if (mask
& BUFFER_BIT_ACCUM
) {
464 clear_accum_buffer(ctx
,
465 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
468 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
469 /* clearing combined depth + stencil */
470 clear_depth_stencil_buffer(ctx
, depthRb
);
473 /* separate depth/stencil clears */
474 if (mask
& BUFFER_BIT_DEPTH
) {
475 clear_depth_buffer(ctx
, depthRb
);
477 if (mask
& BUFFER_BIT_STENCIL
) {
478 clear_stencil_buffer(ctx
, stencilRb
);
484 void st_init_clear_functions(struct dd_function_table
*functions
)
486 functions
->Clear
= st_clear
;