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"
39 #include "st_context.h"
40 #include "st_cb_clear.h"
41 #include "st_cb_fbo.h"
43 #include "st_program.h"
44 #include "st_public.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_state.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_winsys.h"
51 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
59 color_value(GLuint pipeFormat
, const GLfloat color
[4])
63 UNCLAMPED_FLOAT_TO_UBYTE(r
, color
[0]);
64 UNCLAMPED_FLOAT_TO_UBYTE(g
, color
[1]);
65 UNCLAMPED_FLOAT_TO_UBYTE(b
, color
[2]);
66 UNCLAMPED_FLOAT_TO_UBYTE(a
, color
[3]);
69 case PIPE_FORMAT_U_R8_G8_B8_A8
:
70 return (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
71 case PIPE_FORMAT_U_A8_R8_G8_B8
:
72 return (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
73 case PIPE_FORMAT_U_R5_G6_B5
:
74 return ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
82 depth_value(GLuint pipeFormat
, GLfloat value
)
86 case PIPE_FORMAT_U_Z16
:
87 val
= (GLuint
) (value
* 0xffffff);
89 case PIPE_FORMAT_U_Z32
:
90 val
= (GLuint
) (value
* 0xffffffff);
92 case PIPE_FORMAT_S8_Z24
:
93 /*case PIPE_FORMAT_Z24_S8:*/
94 val
= (GLuint
) (value
* 0xffffff);
105 is_depth_stencil_format(GLuint pipeFormat
)
107 switch (pipeFormat
) {
108 case PIPE_FORMAT_S8_Z24
:
109 /*case PIPE_FORMAT_Z24_S8:*/
119 * Create a simple fragment shader that just passes through the fragment color.
121 static struct st_fragment_program
*
122 make_frag_shader(struct st_context
*st
)
124 static const GLuint outputMapping
[] = { 1, 0 };
125 GLcontext
*ctx
= st
->ctx
;
126 struct st_fragment_program
*stfp
;
127 struct gl_program
*p
;
129 GLuint interpMode
[16];
133 for (i
= 0; i
< 16; i
++)
134 interpMode
[i
] = TGSI_INTERPOLATE_LINEAR
;
136 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
140 p
->NumInstructions
= 2;
141 p
->Instructions
= _mesa_alloc_instructions(2);
142 if (!p
->Instructions
) {
143 ctx
->Driver
.DeleteProgram(ctx
, p
);
146 _mesa_init_instructions(p
->Instructions
, 2);
147 /* MOV result.color, fragment.color; */
148 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
149 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
150 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
151 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
152 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
154 p
->Instructions
[1].Opcode
= OPCODE_END
;
156 p
->InputsRead
= FRAG_BIT_COL0
;
157 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
159 stfp
= (struct st_fragment_program
*) p
;
160 /* compile into tgsi format */
161 b
= tgsi_mesa_compile_fp_program(&stfp
->Base
, NULL
, interpMode
,
163 stfp
->tokens
, ST_FP_MAX_TOKENS
);
171 * Create a simple vertex shader that just passes through the
172 * vertex position and color.
174 static struct st_vertex_program
*
175 make_vertex_shader(struct st_context
*st
)
177 /* Map VERT_ATTRIB_POS to 0, VERT_ATTRIB_COLOR0 to 1 */
178 static const GLuint inputMapping
[4] = { 0, 0, 0, 1 };
179 /* Map VERT_RESULT_HPOS to 0, VERT_RESULT_COL0 to 1 */
180 static const GLuint outputMapping
[2] = { 0, 1 };
182 GLcontext
*ctx
= st
->ctx
;
183 struct st_vertex_program
*stvp
;
184 struct gl_program
*p
;
187 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
191 p
->NumInstructions
= 3;
192 p
->Instructions
= _mesa_alloc_instructions(3);
193 if (!p
->Instructions
) {
194 ctx
->Driver
.DeleteProgram(ctx
, p
);
197 _mesa_init_instructions(p
->Instructions
, 3);
198 /* MOV result.pos, vertex.pos; */
199 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
200 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
201 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
202 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
203 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
204 /* MOV result.color, vertex.color; */
205 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
206 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
207 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_COL0
;
208 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
209 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
211 p
->Instructions
[2].Opcode
= OPCODE_END
;
213 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
214 p
->OutputsWritten
= ((1 << VERT_RESULT_COL0
) |
215 (1 << VERT_RESULT_HPOS
));
217 stvp
= (struct st_vertex_program
*) p
;
218 /* compile into tgsi format */
219 b
= tgsi_mesa_compile_vp_program(&stvp
->Base
,
222 stvp
->tokens
, ST_FP_MAX_TOKENS
);
231 * Draw a screen-aligned quadrilateral.
232 * Coords are window coords with y=0=bottom. These coords will be transformed
233 * by the vertex shader and viewport transform (which will flip Y if needed).
236 draw_quad(GLcontext
*ctx
,
237 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
238 const GLfloat color
[4])
240 static const GLuint attribs
[2] = {
244 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
260 /* same for all verts: */
261 for (i
= 0; i
< 4; i
++) {
263 verts
[i
][0][3] = 1.0;
264 verts
[i
][1][0] = color
[0];
265 verts
[i
][1][1] = color
[1];
266 verts
[i
][1][2] = color
[2];
267 verts
[i
][1][3] = color
[3];
270 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2, attribs
);
276 * Do glClear by drawing a quadrilateral.
277 * The vertices of the quad will be computed from the
278 * ctx->DrawBuffer->_X/Ymin/max fields.
281 clear_with_quad(GLcontext
*ctx
,
282 GLboolean color
, GLboolean depth
, GLboolean stencil
)
284 struct st_context
*st
= ctx
->st
;
285 struct pipe_context
*pipe
= ctx
->st
->pipe
;
286 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
287 const GLfloat y0
= ctx
->DrawBuffer
->_Ymin
;
288 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
289 const GLfloat y1
= ctx
->DrawBuffer
->_Ymax
;
291 /* alpha state: disabled */
293 struct pipe_alpha_test_state alpha_test
;
294 memset(&alpha_test
, 0, sizeof(alpha_test
));
295 pipe
->set_alpha_test_state(pipe
, &alpha_test
);
298 /* blend state: RGBA masking */
300 struct pipe_blend_state blend
;
301 const struct pipe_blend_state
*state
;
302 memset(&blend
, 0, sizeof(blend
));
304 if (ctx
->Color
.ColorMask
[0])
305 blend
.colormask
|= PIPE_MASK_R
;
306 if (ctx
->Color
.ColorMask
[1])
307 blend
.colormask
|= PIPE_MASK_G
;
308 if (ctx
->Color
.ColorMask
[2])
309 blend
.colormask
|= PIPE_MASK_B
;
310 if (ctx
->Color
.ColorMask
[3])
311 blend
.colormask
|= PIPE_MASK_A
;
312 if (st
->ctx
->Color
.DitherFlag
)
315 state
= st_cached_blend_state(st
, &blend
);
316 pipe
->bind_blend_state(pipe
, state
);
319 /* depth_stencil state: always pass/set to ref value */
321 struct pipe_depth_stencil_state depth_stencil
;
322 struct pipe_depth_stencil_state
*cached
;
323 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
325 depth_stencil
.depth
.enabled
= 1;
326 depth_stencil
.depth
.writemask
= 1;
327 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
331 depth_stencil
.stencil
.front_enabled
= 1;
332 depth_stencil
.stencil
.front_func
= PIPE_FUNC_ALWAYS
;
333 depth_stencil
.stencil
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
334 depth_stencil
.stencil
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
335 depth_stencil
.stencil
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
336 depth_stencil
.stencil
.ref_value
[0] = ctx
->Stencil
.Clear
;
337 depth_stencil
.stencil
.value_mask
[0] = 0xff;
338 depth_stencil
.stencil
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
341 st_cached_depth_stencil_state(ctx
->st
, &depth_stencil
);
342 pipe
->bind_depth_stencil_state(pipe
, cached
);
345 /* setup state: nothing */
347 struct pipe_rasterizer_state raster
;
348 const struct pipe_rasterizer_state
*cached
;
349 memset(&raster
, 0, sizeof(raster
));
351 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
352 * that matches the scissor bounds.
354 if (ctx
->Scissor
.Enabled
)
357 cached
= st_cached_rasterizer_state(ctx
->st
, &raster
);
358 pipe
->bind_rasterizer_state(pipe
, cached
);
361 /* fragment shader state: color pass-through program */
363 static struct st_fragment_program
*stfp
= NULL
;
364 struct pipe_shader_state fs
;
365 const struct pipe_shader_state
*cached
;
367 stfp
= make_frag_shader(st
);
369 memset(&fs
, 0, sizeof(fs
));
370 fs
.inputs_read
= tgsi_mesa_translate_fragment_input_mask(stfp
->Base
.Base
.InputsRead
);
371 fs
.outputs_written
= tgsi_mesa_translate_fragment_output_mask(stfp
->Base
.Base
.OutputsWritten
);
372 fs
.tokens
= &stfp
->tokens
[0];
373 cached
= st_cached_fs_state(st
, &fs
);
374 pipe
->bind_fs_state(pipe
, cached
);
377 /* vertex shader state: color/position pass-through */
379 static struct st_vertex_program
*stvp
= NULL
;
380 struct pipe_shader_state vs
;
381 const struct pipe_shader_state
*cached
;
383 stvp
= make_vertex_shader(st
);
385 memset(&vs
, 0, sizeof(vs
));
386 vs
.inputs_read
= stvp
->Base
.Base
.InputsRead
;
387 vs
.outputs_written
= stvp
->Base
.Base
.OutputsWritten
;
388 vs
.tokens
= &stvp
->tokens
[0];
389 cached
= st_cached_vs_state(st
, &vs
);
390 pipe
->bind_vs_state(pipe
, cached
);
393 /* viewport state: viewport matching window dims */
395 const float width
= ctx
->DrawBuffer
->Width
;
396 const float height
= ctx
->DrawBuffer
->Height
;
397 struct pipe_viewport_state vp
;
398 vp
.scale
[0] = 0.5 * width
;
399 vp
.scale
[1] = -0.5 * height
;
402 vp
.translate
[0] = 0.5 * width
;
403 vp
.translate
[1] = 0.5 * height
;
404 vp
.translate
[2] = 0.5;
405 vp
.translate
[3] = 0.0;
406 pipe
->set_viewport_state(pipe
, &vp
);
409 /* draw quad matching scissor rect (XXX verify coord round-off) */
410 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
412 /* Restore pipe state */
413 pipe
->set_alpha_test_state(pipe
, &st
->state
.alpha_test
);
414 pipe
->bind_blend_state(pipe
, st
->state
.blend
);
415 pipe
->bind_depth_stencil_state(pipe
, st
->state
.depth_stencil
);
416 pipe
->bind_fs_state(pipe
, st
->state
.fs
);
417 pipe
->bind_vs_state(pipe
, st
->state
.vs
);
418 pipe
->bind_rasterizer_state(pipe
, st
->state
.rasterizer
);
419 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
421 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
427 * Determine if we need to clear the depth buffer by drawing a quad.
429 static INLINE GLboolean
430 check_clear_color_with_quad(GLcontext
*ctx
)
432 return !(ctx
->Color
.ColorMask
[0] &&
433 ctx
->Color
.ColorMask
[1] &&
434 ctx
->Color
.ColorMask
[2] &&
435 ctx
->Color
.ColorMask
[3] &&
436 !ctx
->Scissor
.Enabled
);
441 * Determine if we need to clear the depth buffer by drawing a quad.
443 static INLINE GLboolean
444 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
446 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
447 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
448 return ctx
->Scissor
.Enabled
449 || (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
454 * Determine if we need to clear the stencil buffer by drawing a quad.
456 static INLINE GLboolean
457 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
459 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
460 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
461 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
462 const GLboolean maskStencil
463 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
465 || ctx
->Scissor
.Enabled
466 || (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0);
473 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
475 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
477 if (ctx
->Color
.ColorMask
[0] &&
478 ctx
->Color
.ColorMask
[1] &&
479 ctx
->Color
.ColorMask
[2] &&
480 ctx
->Color
.ColorMask
[3] &&
481 !ctx
->Scissor
.Enabled
)
483 /* clear whole buffer w/out masking */
485 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
486 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
489 /* masking or scissoring */
490 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
496 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
498 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
500 if (!ctx
->Scissor
.Enabled
) {
501 /* clear whole buffer w/out masking */
503 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
504 /* Note that clearValue is 32 bits but the accum buffer will
505 * typically be 64bpp...
507 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
511 /* XXX point framebuffer.cbufs[0] at the accum buffer */
512 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
518 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
520 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
521 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
523 assert(strb
->surface
->format
);
525 if (ctx
->Scissor
.Enabled
||
526 (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0)) {
527 /* scissoring or we have a combined depth/stencil buffer */
528 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
531 /* simple clear of whole buffer */
532 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
533 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
539 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
541 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
542 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
543 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
544 GLboolean maskStencil
545 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
548 ctx
->Scissor
.Enabled
||
549 (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0)) {
550 /* masking or scissoring or combined depth/stencil buffer */
551 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
554 /* simple clear of whole buffer */
555 GLuint clearValue
= ctx
->Stencil
.Clear
;
556 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
562 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
564 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
565 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
566 GLboolean maskStencil
567 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
569 assert(is_depth_stencil_format(strb
->surface
->format
));
571 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
572 /* clear whole buffer w/out masking */
573 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
575 switch (strb
->surface
->format
) {
576 case PIPE_FORMAT_S8_Z24
:
577 clearValue
|= ctx
->Stencil
.Clear
<< 24;
580 case PIPE_FORMAT_Z24_S8
:
581 clearValue
= (clearValue
<< 8) | clearVal
;
588 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
591 /* masking or scissoring */
592 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
599 * Called via ctx->Driver.Clear()
600 * XXX: doesn't pick up the differences between front/back/left/right
601 * clears. Need to sort that out...
603 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
605 static const GLbitfield BUFFER_BITS_DS
606 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
607 struct st_context
*st
= ctx
->st
;
608 struct gl_renderbuffer
*depthRb
609 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
610 struct gl_renderbuffer
*stencilRb
611 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
612 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
614 /* This makes sure the softpipe has the latest scissor, etc values */
615 st_validate_state( st
);
619 * If we're going to use clear_with_quad() for any reason, use it to
620 * clear as many other buffers as possible.
621 * As it is now, we sometimes call clear_with_quad() three times to clear
622 * color/depth/stencil individually...
627 for (b
= 0; cmask
; b
++) {
628 if (cmask
& (1 << b
)) {
629 struct gl_renderbuffer
*rb
630 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
632 clear_color_buffer(ctx
, rb
);
633 cmask
&= ~(1 << b
); /* turn off bit */
635 assert(b
< BUFFER_COUNT
);
639 if (mask
& BUFFER_BIT_ACCUM
) {
640 clear_accum_buffer(ctx
,
641 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
644 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
645 /* clearing combined depth + stencil */
646 clear_depth_stencil_buffer(ctx
, depthRb
);
649 /* separate depth/stencil clears */
650 if (mask
& BUFFER_BIT_DEPTH
) {
651 clear_depth_buffer(ctx
, depthRb
);
653 if (mask
& BUFFER_BIT_STENCIL
) {
654 clear_stencil_buffer(ctx
, stencilRb
);
660 void st_init_clear_functions(struct dd_function_table
*functions
)
662 functions
->Clear
= st_clear
;