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_accum.h"
41 #include "st_cb_clear.h"
42 #include "st_cb_fbo.h"
44 #include "st_program.h"
45 #include "st_public.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_state.h"
49 #include "pipe/p_defines.h"
50 #include "pipe/p_winsys.h"
52 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
60 color_value(GLuint pipeFormat
, const GLfloat color
[4])
64 UNCLAMPED_FLOAT_TO_UBYTE(r
, color
[0]);
65 UNCLAMPED_FLOAT_TO_UBYTE(g
, color
[1]);
66 UNCLAMPED_FLOAT_TO_UBYTE(b
, color
[2]);
67 UNCLAMPED_FLOAT_TO_UBYTE(a
, color
[3]);
70 case PIPE_FORMAT_U_R8_G8_B8_A8
:
71 return (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
72 case PIPE_FORMAT_U_A8_R8_G8_B8
:
73 return (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
74 case PIPE_FORMAT_U_R5_G6_B5
:
75 return ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
83 depth_value(GLuint pipeFormat
, GLfloat value
)
87 case PIPE_FORMAT_U_Z16
:
88 val
= (GLuint
) (value
* 0xffffff);
90 case PIPE_FORMAT_U_Z32
:
91 val
= (GLuint
) (value
* 0xffffffff);
93 case PIPE_FORMAT_S8_Z24
:
94 /*case PIPE_FORMAT_Z24_S8:*/
95 val
= (GLuint
) (value
* 0xffffff);
106 is_depth_stencil_format(GLuint pipeFormat
)
108 switch (pipeFormat
) {
109 case PIPE_FORMAT_S8_Z24
:
110 /*case PIPE_FORMAT_Z24_S8:*/
120 * Create a simple fragment shader that just passes through the fragment color.
122 static struct st_fragment_program
*
123 make_frag_shader(struct st_context
*st
)
125 GLcontext
*ctx
= st
->ctx
;
126 struct st_fragment_program
*stfp
;
127 struct gl_program
*p
;
128 GLuint interpMode
[16];
132 for (i
= 0; i
< 16; i
++)
133 interpMode
[i
] = TGSI_INTERPOLATE_LINEAR
;
135 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
139 p
->NumInstructions
= 2;
140 p
->Instructions
= _mesa_alloc_instructions(2);
141 if (!p
->Instructions
) {
142 ctx
->Driver
.DeleteProgram(ctx
, p
);
145 _mesa_init_instructions(p
->Instructions
, 2);
146 /* MOV result.color, fragment.color; */
147 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
148 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
149 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
150 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
151 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
153 p
->Instructions
[1].Opcode
= OPCODE_END
;
155 p
->InputsRead
= FRAG_BIT_COL0
;
156 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
158 stfp
= (struct st_fragment_program
*) p
;
159 st_translate_fragment_program(st
, stfp
, NULL
,
160 stfp
->tokens
, ST_MAX_SHADER_TOKENS
);
167 * Create a simple vertex shader that just passes through the
168 * vertex position and color.
170 static struct st_vertex_program
*
171 make_vertex_shader(struct st_context
*st
)
173 GLcontext
*ctx
= st
->ctx
;
174 struct st_vertex_program
*stvp
;
175 struct gl_program
*p
;
177 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
181 p
->NumInstructions
= 3;
182 p
->Instructions
= _mesa_alloc_instructions(3);
183 if (!p
->Instructions
) {
184 ctx
->Driver
.DeleteProgram(ctx
, p
);
187 _mesa_init_instructions(p
->Instructions
, 3);
188 /* MOV result.pos, vertex.pos; */
189 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
190 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
191 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
192 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
193 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
194 /* MOV result.color, vertex.color; */
195 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
196 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
197 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_COL0
;
198 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
199 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
201 p
->Instructions
[2].Opcode
= OPCODE_END
;
203 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
204 p
->OutputsWritten
= ((1 << VERT_RESULT_COL0
) |
205 (1 << VERT_RESULT_HPOS
));
207 stvp
= (struct st_vertex_program
*) p
;
208 st_translate_vertex_program(st
, stvp
, NULL
,
209 stvp
->tokens
, ST_MAX_SHADER_TOKENS
);
218 * Draw a screen-aligned quadrilateral.
219 * Coords are window coords with y=0=bottom. These coords will be transformed
220 * by the vertex shader and viewport transform (which will flip Y if needed).
223 draw_quad(GLcontext
*ctx
,
224 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
225 const GLfloat color
[4])
227 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
243 /* same for all verts: */
244 for (i
= 0; i
< 4; i
++) {
246 verts
[i
][0][3] = 1.0;
247 verts
[i
][1][0] = color
[0];
248 verts
[i
][1][1] = color
[1];
249 verts
[i
][1][2] = color
[2];
250 verts
[i
][1][3] = color
[3];
253 st_draw_vertices(ctx
, PIPE_PRIM_QUADS
, 4, (float *) verts
, 2);
259 * Do glClear by drawing a quadrilateral.
260 * The vertices of the quad will be computed from the
261 * ctx->DrawBuffer->_X/Ymin/max fields.
264 clear_with_quad(GLcontext
*ctx
,
265 GLboolean color
, GLboolean depth
, GLboolean stencil
)
267 struct st_context
*st
= ctx
->st
;
268 struct pipe_context
*pipe
= st
->pipe
;
269 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
270 const GLfloat y0
= ctx
->DrawBuffer
->_Ymin
;
271 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
272 const GLfloat y1
= ctx
->DrawBuffer
->_Ymax
;
274 /* alpha state: disabled */
276 struct pipe_alpha_test_state alpha_test
;
277 const struct cso_alpha_test
*cso
;
278 memset(&alpha_test
, 0, sizeof(alpha_test
));
279 cso
= st_cached_alpha_test_state(st
, &alpha_test
);
280 pipe
->bind_alpha_test_state(pipe
, cso
->data
);
283 /* blend state: RGBA masking */
285 struct pipe_blend_state blend
;
286 const struct cso_blend
*cso
;
287 memset(&blend
, 0, sizeof(blend
));
289 if (ctx
->Color
.ColorMask
[0])
290 blend
.colormask
|= PIPE_MASK_R
;
291 if (ctx
->Color
.ColorMask
[1])
292 blend
.colormask
|= PIPE_MASK_G
;
293 if (ctx
->Color
.ColorMask
[2])
294 blend
.colormask
|= PIPE_MASK_B
;
295 if (ctx
->Color
.ColorMask
[3])
296 blend
.colormask
|= PIPE_MASK_A
;
297 if (st
->ctx
->Color
.DitherFlag
)
300 cso
= st_cached_blend_state(st
, &blend
);
301 pipe
->bind_blend_state(pipe
, cso
->data
);
304 /* depth_stencil state: always pass/set to ref value */
306 struct pipe_depth_stencil_state depth_stencil
;
307 const struct cso_depth_stencil
*cso
;
308 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
310 depth_stencil
.depth
.enabled
= 1;
311 depth_stencil
.depth
.writemask
= 1;
312 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
316 depth_stencil
.stencil
.front_enabled
= 1;
317 depth_stencil
.stencil
.front_func
= PIPE_FUNC_ALWAYS
;
318 depth_stencil
.stencil
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
319 depth_stencil
.stencil
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
320 depth_stencil
.stencil
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
321 depth_stencil
.stencil
.ref_value
[0] = ctx
->Stencil
.Clear
;
322 depth_stencil
.stencil
.value_mask
[0] = 0xff;
323 depth_stencil
.stencil
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
325 cso
= st_cached_depth_stencil_state(st
, &depth_stencil
);
326 pipe
->bind_depth_stencil_state(pipe
, cso
->data
);
329 /* rasterizer state: nothing */
331 struct pipe_rasterizer_state raster
;
332 const struct cso_rasterizer
*cso
;
333 memset(&raster
, 0, sizeof(raster
));
335 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
336 * that matches the scissor bounds.
338 if (ctx
->Scissor
.Enabled
)
341 cso
= st_cached_rasterizer_state(st
, &raster
);
342 pipe
->bind_rasterizer_state(pipe
, cso
->data
);
345 /* fragment shader state: color pass-through program */
347 static struct st_fragment_program
*stfp
= NULL
;
349 stfp
= make_frag_shader(st
);
351 pipe
->bind_fs_state(pipe
, stfp
->fs
->data
);
354 /* vertex shader state: color/position pass-through */
356 static struct st_vertex_program
*stvp
= NULL
;
358 stvp
= make_vertex_shader(st
);
360 pipe
->bind_vs_state(pipe
, stvp
->vs
->data
);
363 /* viewport state: viewport matching window dims */
365 const float width
= ctx
->DrawBuffer
->Width
;
366 const float height
= ctx
->DrawBuffer
->Height
;
367 struct pipe_viewport_state vp
;
368 vp
.scale
[0] = 0.5 * width
;
369 vp
.scale
[1] = -0.5 * height
;
372 vp
.translate
[0] = 0.5 * width
;
373 vp
.translate
[1] = 0.5 * height
;
374 vp
.translate
[2] = 0.0;
375 vp
.translate
[3] = 0.0;
376 pipe
->set_viewport_state(pipe
, &vp
);
379 /* draw quad matching scissor rect (XXX verify coord round-off) */
380 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
382 /* Restore pipe state */
383 pipe
->bind_alpha_test_state(pipe
, st
->state
.alpha_test
->data
);
384 pipe
->bind_blend_state(pipe
, st
->state
.blend
->data
);
385 pipe
->bind_depth_stencil_state(pipe
, st
->state
.depth_stencil
->data
);
386 pipe
->bind_fs_state(pipe
, st
->state
.fs
->data
);
387 pipe
->bind_vs_state(pipe
, st
->state
.vs
->data
);
388 pipe
->bind_rasterizer_state(pipe
, st
->state
.rasterizer
->data
);
389 pipe
->set_viewport_state(pipe
, &st
->state
.viewport
);
391 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
397 * Determine if we need to clear the depth buffer by drawing a quad.
399 static INLINE GLboolean
400 check_clear_color_with_quad(GLcontext
*ctx
)
402 return !(ctx
->Color
.ColorMask
[0] &&
403 ctx
->Color
.ColorMask
[1] &&
404 ctx
->Color
.ColorMask
[2] &&
405 ctx
->Color
.ColorMask
[3] &&
406 !ctx
->Scissor
.Enabled
);
411 * Determine if we need to clear the depth buffer by drawing a quad.
413 static INLINE GLboolean
414 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
416 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
417 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
418 return ctx
->Scissor
.Enabled
419 || (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
424 * Determine if we need to clear the stencil buffer by drawing a quad.
426 static INLINE GLboolean
427 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
429 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
430 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
431 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
432 const GLboolean maskStencil
433 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
435 || ctx
->Scissor
.Enabled
436 || (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0);
443 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
445 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
447 if (ctx
->Color
.ColorMask
[0] &&
448 ctx
->Color
.ColorMask
[1] &&
449 ctx
->Color
.ColorMask
[2] &&
450 ctx
->Color
.ColorMask
[3] &&
451 !ctx
->Scissor
.Enabled
)
453 /* clear whole buffer w/out masking */
455 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
456 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
459 /* masking or scissoring */
460 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
466 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
468 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
469 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
471 assert(strb
->surface
->format
);
473 if (check_clear_depth_with_quad(ctx
, rb
)) {
474 /* scissoring or we have a combined depth/stencil buffer */
475 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
478 /* simple clear of whole buffer */
479 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
480 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
486 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
488 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
489 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
490 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
491 GLboolean maskStencil
492 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
495 ctx
->Scissor
.Enabled
||
496 (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0)) {
497 /* masking or scissoring or combined depth/stencil buffer */
498 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
501 /* simple clear of whole buffer */
502 GLuint clearValue
= ctx
->Stencil
.Clear
;
503 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
509 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
511 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
512 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
513 GLboolean maskStencil
514 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
516 assert(is_depth_stencil_format(strb
->surface
->format
));
518 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
519 /* clear whole buffer w/out masking */
520 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
522 switch (strb
->surface
->format
) {
523 case PIPE_FORMAT_S8_Z24
:
524 clearValue
|= ctx
->Stencil
.Clear
<< 24;
527 case PIPE_FORMAT_Z24_S8
:
528 clearValue
= (clearValue
<< 8) | clearVal
;
535 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
538 /* masking or scissoring */
539 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
546 * Called via ctx->Driver.Clear()
547 * XXX: doesn't pick up the differences between front/back/left/right
548 * clears. Need to sort that out...
550 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
552 static const GLbitfield BUFFER_BITS_DS
553 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
554 struct st_context
*st
= ctx
->st
;
555 struct gl_renderbuffer
*depthRb
556 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
557 struct gl_renderbuffer
*stencilRb
558 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
559 GLbitfield cmask
= mask
& BUFFER_BITS_COLOR
;
561 /* This makes sure the softpipe has the latest scissor, etc values */
562 st_validate_state( st
);
566 * If we're going to use clear_with_quad() for any reason, use it to
567 * clear as many other buffers as possible.
568 * As it is now, we sometimes call clear_with_quad() three times to clear
569 * color/depth/stencil individually...
574 for (b
= 0; cmask
; b
++) {
575 if (cmask
& (1 << b
)) {
576 struct gl_renderbuffer
*rb
577 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
579 clear_color_buffer(ctx
, rb
);
580 cmask
&= ~(1 << b
); /* turn off bit */
582 assert(b
< BUFFER_COUNT
);
586 if (mask
& BUFFER_BIT_ACCUM
) {
587 st_clear_accum_buffer(ctx
,
588 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
591 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
592 /* clearing combined depth + stencil */
593 clear_depth_stencil_buffer(ctx
, depthRb
);
596 /* separate depth/stencil clears */
597 if (mask
& BUFFER_BIT_DEPTH
) {
598 clear_depth_buffer(ctx
, depthRb
);
600 if (mask
& BUFFER_BIT_STENCIL
) {
601 clear_stencil_buffer(ctx
, stencilRb
);
607 void st_init_clear_functions(struct dd_function_table
*functions
)
609 functions
->Clear
= st_clear
;