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 GLcontext
*ctx
= st
->ctx
;
125 struct st_fragment_program
*stfp
;
126 struct gl_program
*p
;
127 GLuint interpMode
[16];
131 for (i
= 0; i
< 16; i
++)
132 interpMode
[i
] = TGSI_INTERPOLATE_LINEAR
;
134 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
138 p
->NumInstructions
= 2;
139 p
->Instructions
= _mesa_alloc_instructions(2);
140 if (!p
->Instructions
) {
141 ctx
->Driver
.DeleteProgram(ctx
, p
);
144 _mesa_init_instructions(p
->Instructions
, 2);
145 /* MOV result.color, fragment.color; */
146 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
147 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
148 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
149 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
150 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
152 p
->Instructions
[1].Opcode
= OPCODE_END
;
154 p
->InputsRead
= FRAG_BIT_COL0
;
155 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
157 stfp
= (struct st_fragment_program
*) p
;
158 st_translate_fragment_program(st
, stfp
, NULL
,
159 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
166 * Create a simple vertex shader that just passes through the
167 * vertex position and color.
169 static struct st_vertex_program
*
170 make_vertex_shader(struct st_context
*st
)
172 GLcontext
*ctx
= st
->ctx
;
173 struct st_vertex_program
*stvp
;
174 struct gl_program
*p
;
176 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
180 p
->NumInstructions
= 3;
181 p
->Instructions
= _mesa_alloc_instructions(3);
182 if (!p
->Instructions
) {
183 ctx
->Driver
.DeleteProgram(ctx
, p
);
186 _mesa_init_instructions(p
->Instructions
, 3);
187 /* MOV result.pos, vertex.pos; */
188 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
189 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
190 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
191 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
192 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
193 /* MOV result.color, vertex.color; */
194 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
195 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
196 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_COL0
;
197 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
198 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
200 p
->Instructions
[2].Opcode
= OPCODE_END
;
202 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
203 p
->OutputsWritten
= ((1 << VERT_RESULT_COL0
) |
204 (1 << VERT_RESULT_HPOS
));
206 stvp
= (struct st_vertex_program
*) p
;
207 st_translate_vertex_program(st
, stvp
, NULL
,
208 stvp
->tokens
, ST_MAX_SHADER_TOKENS
);
217 * Draw a screen-aligned quadrilateral.
218 * Coords are window coords with y=0=bottom. These coords will be transformed
219 * by the vertex shader and viewport transform (which will flip Y if needed).
222 draw_quad(GLcontext
*ctx
,
223 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
224 const GLfloat color
[4])
226 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
242 /* same for all verts: */
243 for (i
= 0; i
< 4; i
++) {
245 verts
[i
][0][3] = 1.0;
246 verts
[i
][1][0] = color
[0];
247 verts
[i
][1][1] = color
[1];
248 verts
[i
][1][2] = color
[2];
249 verts
[i
][1][3] = color
[3];
252 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2);
258 * Do glClear by drawing a quadrilateral.
259 * The vertices of the quad will be computed from the
260 * ctx->DrawBuffer->_X/Ymin/max fields.
263 clear_with_quad(GLcontext
*ctx
,
264 GLboolean color
, GLboolean depth
, GLboolean stencil
)
266 struct st_context
*st
= ctx
->st
;
267 struct pipe_context
*pipe
= st
->pipe
;
268 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
269 const GLfloat y0
= ctx
->DrawBuffer
->_Ymin
;
270 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
271 const GLfloat y1
= ctx
->DrawBuffer
->_Ymax
;
273 /* alpha state: disabled */
275 struct pipe_alpha_test_state alpha_test
;
276 const struct cso_alpha_test
*cso
;
277 memset(&alpha_test
, 0, sizeof(alpha_test
));
278 cso
= st_cached_alpha_test_state(st
, &alpha_test
);
279 pipe
->bind_alpha_test_state(pipe
, cso
->data
);
282 /* blend state: RGBA masking */
284 struct pipe_blend_state blend
;
285 const struct cso_blend
*cso
;
286 memset(&blend
, 0, sizeof(blend
));
288 if (ctx
->Color
.ColorMask
[0])
289 blend
.colormask
|= PIPE_MASK_R
;
290 if (ctx
->Color
.ColorMask
[1])
291 blend
.colormask
|= PIPE_MASK_G
;
292 if (ctx
->Color
.ColorMask
[2])
293 blend
.colormask
|= PIPE_MASK_B
;
294 if (ctx
->Color
.ColorMask
[3])
295 blend
.colormask
|= PIPE_MASK_A
;
296 if (st
->ctx
->Color
.DitherFlag
)
299 cso
= st_cached_blend_state(st
, &blend
);
300 pipe
->bind_blend_state(pipe
, cso
->data
);
303 /* depth_stencil state: always pass/set to ref value */
305 struct pipe_depth_stencil_state depth_stencil
;
306 const struct cso_depth_stencil
*cso
;
307 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
309 depth_stencil
.depth
.enabled
= 1;
310 depth_stencil
.depth
.writemask
= 1;
311 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
315 depth_stencil
.stencil
.front_enabled
= 1;
316 depth_stencil
.stencil
.front_func
= PIPE_FUNC_ALWAYS
;
317 depth_stencil
.stencil
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
318 depth_stencil
.stencil
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
319 depth_stencil
.stencil
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
320 depth_stencil
.stencil
.ref_value
[0] = ctx
->Stencil
.Clear
;
321 depth_stencil
.stencil
.value_mask
[0] = 0xff;
322 depth_stencil
.stencil
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
324 cso
= st_cached_depth_stencil_state(st
, &depth_stencil
);
325 pipe
->bind_depth_stencil_state(pipe
, cso
->data
);
328 /* setup state: nothing */
330 struct pipe_rasterizer_state raster
;
331 const struct cso_rasterizer
*cso
;
332 memset(&raster
, 0, sizeof(raster
));
334 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
335 * that matches the scissor bounds.
337 if (ctx
->Scissor
.Enabled
)
340 cso
= st_cached_rasterizer_state(st
, &raster
);
341 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
344 /* fragment shader state: color pass-through program */
346 static struct st_fragment_program
*stfp
= NULL
;
348 stfp
= make_frag_shader(st
);
350 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
353 /* vertex shader state: color/position pass-through */
355 static struct st_vertex_program
*stvp
= NULL
;
357 stvp
= make_vertex_shader(st
);
359 pipe
->bind_vs_state(pipe
, stvp
->vs
->data
);
362 /* viewport state: viewport matching window dims */
364 const float width
= ctx
->DrawBuffer
->Width
;
365 const float height
= ctx
->DrawBuffer
->Height
;
366 struct pipe_viewport_state vp
;
367 vp
.scale
[0] = 0.5 * width
;
368 vp
.scale
[1] = -0.5 * height
;
371 vp
.translate
[0] = 0.5 * width
;
372 vp
.translate
[1] = 0.5 * height
;
373 vp
.translate
[2] = 0.5;
374 vp
.translate
[3] = 0.0;
375 pipe
->set_viewport_state(pipe
, &vp
);
378 /* draw quad matching scissor rect (XXX verify coord round-off) */
379 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
381 /* Restore pipe state */
382 pipe
->bind_alpha_test_state(pipe
, st
->state
.alpha_test
->data
);
383 pipe
->bind_blend_state(pipe
, st
->state
.blend
->data
);
384 pipe
->bind_depth_stencil_state(pipe
, st
->state
.depth_stencil
->data
);
385 pipe
->bind_fs_state(pipe
, st
->state
.fs
->data
);
386 pipe
->bind_vs_state(pipe
, st
->state
.vs
->data
);
387 pipe
->bind_rasterizer_state(pipe
, st
->state
.rasterizer
->data
);
388 pipe
->set_viewport_state(pipe
, &st
->state
.viewport
);
390 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
396 * Determine if we need to clear the depth buffer by drawing a quad.
398 static INLINE GLboolean
399 check_clear_color_with_quad(GLcontext
*ctx
)
401 return !(ctx
->Color
.ColorMask
[0] &&
402 ctx
->Color
.ColorMask
[1] &&
403 ctx
->Color
.ColorMask
[2] &&
404 ctx
->Color
.ColorMask
[3] &&
405 !ctx
->Scissor
.Enabled
);
410 * Determine if we need to clear the depth buffer by drawing a quad.
412 static INLINE GLboolean
413 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
415 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
416 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
417 return ctx
->Scissor
.Enabled
418 || (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
423 * Determine if we need to clear the stencil buffer by drawing a quad.
425 static INLINE GLboolean
426 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
428 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
429 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
430 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
431 const GLboolean maskStencil
432 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
434 || ctx
->Scissor
.Enabled
435 || (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0);
442 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
444 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
446 if (ctx
->Color
.ColorMask
[0] &&
447 ctx
->Color
.ColorMask
[1] &&
448 ctx
->Color
.ColorMask
[2] &&
449 ctx
->Color
.ColorMask
[3] &&
450 !ctx
->Scissor
.Enabled
)
452 /* clear whole buffer w/out masking */
454 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
455 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
458 /* masking or scissoring */
459 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
465 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
467 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
469 if (!ctx
->Scissor
.Enabled
) {
470 /* clear whole buffer w/out masking */
472 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
473 /* Note that clearValue is 32 bits but the accum buffer will
474 * typically be 64bpp...
476 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
480 /* XXX point framebuffer.cbufs[0] at the accum buffer */
481 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
487 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
489 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
490 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
492 assert(strb
->surface
->format
);
494 if (check_clear_depth_with_quad(ctx
, rb
)) {
495 /* scissoring or we have a combined depth/stencil buffer */
496 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
499 /* simple clear of whole buffer */
500 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
501 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
507 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
509 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
510 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
511 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
512 GLboolean maskStencil
513 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
516 ctx
->Scissor
.Enabled
||
517 (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0)) {
518 /* masking or scissoring or combined depth/stencil buffer */
519 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
522 /* simple clear of whole buffer */
523 GLuint clearValue
= ctx
->Stencil
.Clear
;
524 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
530 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
532 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
533 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
534 GLboolean maskStencil
535 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
537 assert(is_depth_stencil_format(strb
->surface
->format
));
539 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
540 /* clear whole buffer w/out masking */
541 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
543 switch (strb
->surface
->format
) {
544 case PIPE_FORMAT_S8_Z24
:
545 clearValue
|= ctx
->Stencil
.Clear
<< 24;
548 case PIPE_FORMAT_Z24_S8
:
549 clearValue
= (clearValue
<< 8) | clearVal
;
556 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
559 /* masking or scissoring */
560 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
567 * Called via ctx->Driver.Clear()
568 * XXX: doesn't pick up the differences between front/back/left/right
569 * clears. Need to sort that out...
571 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
573 static const GLbitfield BUFFER_BITS_DS
574 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
575 struct st_context
*st
= ctx
->st
;
576 struct gl_renderbuffer
*depthRb
577 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
578 struct gl_renderbuffer
*stencilRb
579 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
580 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
582 /* This makes sure the softpipe has the latest scissor, etc values */
583 st_validate_state( st
);
587 * If we're going to use clear_with_quad() for any reason, use it to
588 * clear as many other buffers as possible.
589 * As it is now, we sometimes call clear_with_quad() three times to clear
590 * color/depth/stencil individually...
595 for (b
= 0; cmask
; b
++) {
596 if (cmask
& (1 << b
)) {
597 struct gl_renderbuffer
*rb
598 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
600 clear_color_buffer(ctx
, rb
);
601 cmask
&= ~(1 << b
); /* turn off bit */
603 assert(b
< BUFFER_COUNT
);
607 if (mask
& BUFFER_BIT_ACCUM
) {
608 clear_accum_buffer(ctx
,
609 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
612 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
613 /* clearing combined depth + stencil */
614 clear_depth_stencil_buffer(ctx
, depthRb
);
617 /* separate depth/stencil clears */
618 if (mask
& BUFFER_BIT_DEPTH
) {
619 clear_depth_buffer(ctx
, depthRb
);
621 if (mask
& BUFFER_BIT_STENCIL
) {
622 clear_stencil_buffer(ctx
, stencilRb
);
628 void st_init_clear_functions(struct dd_function_table
*functions
)
630 functions
->Clear
= st_clear
;