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_accum.h"
40 #include "st_cb_clear.h"
41 #include "st_cb_fbo.h"
43 #include "st_program.h"
44 #include "st_public.h"
45 #include "st_mesa_to_tgsi.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"
51 #include "util/u_pack_color.h"
53 #include "cso_cache/cso_context.h"
56 /* XXX for testing draw module vertex passthrough: */
57 #define TEST_DRAW_PASSTHROUGH 0
61 is_depth_stencil_format(enum pipe_format pipeFormat
)
64 case PIPE_FORMAT_S8Z24_UNORM
:
65 case PIPE_FORMAT_Z24S8_UNORM
:
75 * Create a simple fragment shader that just passes through the fragment color.
77 static struct st_fragment_program
*
78 make_frag_shader(struct st_context
*st
)
80 GLcontext
*ctx
= st
->ctx
;
81 struct st_fragment_program
*stfp
;
83 GLuint interpMode
[16];
87 for (i
= 0; i
< 16; i
++)
88 interpMode
[i
] = TGSI_INTERPOLATE_LINEAR
;
90 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
94 p
->NumInstructions
= 2;
95 p
->Instructions
= _mesa_alloc_instructions(2);
96 if (!p
->Instructions
) {
97 ctx
->Driver
.DeleteProgram(ctx
, p
);
100 _mesa_init_instructions(p
->Instructions
, 2);
101 /* MOV result.color, fragment.color; */
102 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
103 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
104 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
105 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
106 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
108 p
->Instructions
[1].Opcode
= OPCODE_END
;
110 p
->InputsRead
= FRAG_BIT_COL0
;
111 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
113 stfp
= (struct st_fragment_program
*) p
;
114 st_translate_fragment_program(st
, stfp
, NULL
);
121 * Create a simple vertex shader that just passes through the
122 * vertex position and color.
124 static struct st_vertex_program
*
125 make_vertex_shader(struct st_context
*st
)
127 GLcontext
*ctx
= st
->ctx
;
128 struct st_vertex_program
*stvp
;
129 struct gl_program
*p
;
131 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
135 p
->NumInstructions
= 3;
136 p
->Instructions
= _mesa_alloc_instructions(3);
137 if (!p
->Instructions
) {
138 ctx
->Driver
.DeleteProgram(ctx
, p
);
141 _mesa_init_instructions(p
->Instructions
, 3);
142 /* MOV result.pos, vertex.pos; */
143 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
144 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
145 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
146 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
147 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
148 /* MOV result.color, vertex.color; */
149 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
150 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
151 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_COL0
;
152 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
153 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
155 p
->Instructions
[2].Opcode
= OPCODE_END
;
157 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
158 p
->OutputsWritten
= ((1 << VERT_RESULT_COL0
) |
159 (1 << VERT_RESULT_HPOS
));
161 stvp
= (struct st_vertex_program
*) p
;
162 st_translate_vertex_program(st
, stvp
, NULL
);
173 * Draw a screen-aligned quadrilateral.
174 * Coords are window coords with y=0=bottom. These coords will be transformed
175 * by the vertex shader and viewport transform (which will flip Y if needed).
178 draw_quad(GLcontext
*ctx
,
179 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
180 const GLfloat color
[4])
182 GLfloat verts
[4][2][4]; /* four verts, two attribs, XYZW */
185 #if TEST_DRAW_PASSTHROUGH
186 /* invert Y coords (may be off by one pixel) */
187 y0
= ctx
->DrawBuffer
->Height
- y0
;
188 y1
= ctx
->DrawBuffer
->Height
- y1
;
204 /* same for all verts: */
205 for (i
= 0; i
< 4; i
++) {
207 verts
[i
][0][3] = 1.0;
208 verts
[i
][1][0] = color
[0];
209 verts
[i
][1][1] = color
[1];
210 verts
[i
][1][2] = color
[2];
211 verts
[i
][1][3] = color
[3];
214 st_draw_vertices(ctx
, PIPE_PRIM_POLYGON
, 4, (float *) verts
, 2,
215 #if TEST_DRAW_PASSTHROUGH
226 * Do glClear by drawing a quadrilateral.
227 * The vertices of the quad will be computed from the
228 * ctx->DrawBuffer->_X/Ymin/max fields.
231 clear_with_quad(GLcontext
*ctx
,
232 GLboolean color
, GLboolean depth
, GLboolean stencil
)
234 struct st_context
*st
= ctx
->st
;
235 struct pipe_context
*pipe
= st
->pipe
;
236 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
237 const GLfloat y0
= ctx
->DrawBuffer
->_Ymin
;
238 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
239 const GLfloat y1
= ctx
->DrawBuffer
->_Ymax
;
242 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
243 color ? "color, " : "",
244 depth ? "depth, " : "",
245 stencil ? "stencil" : "",
250 /* blend state: RGBA masking */
252 struct pipe_blend_state blend
;
253 memset(&blend
, 0, sizeof(blend
));
255 if (ctx
->Color
.ColorMask
[0])
256 blend
.colormask
|= PIPE_MASK_R
;
257 if (ctx
->Color
.ColorMask
[1])
258 blend
.colormask
|= PIPE_MASK_G
;
259 if (ctx
->Color
.ColorMask
[2])
260 blend
.colormask
|= PIPE_MASK_B
;
261 if (ctx
->Color
.ColorMask
[3])
262 blend
.colormask
|= PIPE_MASK_A
;
263 if (st
->ctx
->Color
.DitherFlag
)
266 cso_set_blend(st
->cso_context
, &blend
);
269 /* depth_stencil state: always pass/set to ref value */
271 struct pipe_depth_stencil_alpha_state depth_stencil
;
272 memset(&depth_stencil
, 0, sizeof(depth_stencil
));
274 depth_stencil
.depth
.enabled
= 1;
275 depth_stencil
.depth
.writemask
= 1;
276 depth_stencil
.depth
.func
= PIPE_FUNC_ALWAYS
;
280 depth_stencil
.stencil
[0].enabled
= 1;
281 depth_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
282 depth_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
283 depth_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
284 depth_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
285 depth_stencil
.stencil
[0].ref_value
= ctx
->Stencil
.Clear
;
286 depth_stencil
.stencil
[0].value_mask
= 0xff;
287 depth_stencil
.stencil
[0].write_mask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
290 cso_set_depth_stencil_alpha(st
->cso_context
, &depth_stencil
);
293 /* rasterizer state: nothing */
295 struct pipe_rasterizer_state raster
;
296 memset(&raster
, 0, sizeof(raster
));
298 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
299 * that matches the scissor bounds.
301 if (ctx
->Scissor
.Enabled
)
304 #if TEST_DRAW_PASSTHROUGH
305 raster
.bypass_clipping
= 1;
306 raster
.bypass_vs
= 1;
308 cso_set_rasterizer(st
->cso_context
, &raster
);
311 /* fragment shader state: color pass-through program */
313 static struct st_fragment_program
*stfp
= NULL
;
315 stfp
= make_frag_shader(st
);
317 pipe
->bind_fs_state(pipe
, stfp
->driver_shader
);
320 #if !TEST_DRAW_PASSTHROUGH
321 /* vertex shader state: color/position pass-through */
323 static struct st_vertex_program
*stvp
= NULL
;
325 stvp
= make_vertex_shader(st
);
327 pipe
->bind_vs_state(pipe
, stvp
->driver_shader
);
331 #if !TEST_DRAW_PASSTHROUGH
332 /* viewport state: viewport matching window dims */
334 const float width
= ctx
->DrawBuffer
->Width
;
335 const float height
= ctx
->DrawBuffer
->Height
;
336 struct pipe_viewport_state vp
;
337 vp
.scale
[0] = 0.5 * width
;
338 vp
.scale
[1] = -0.5 * height
;
341 vp
.translate
[0] = 0.5 * width
;
342 vp
.translate
[1] = 0.5 * height
;
343 vp
.translate
[2] = 0.0;
344 vp
.translate
[3] = 0.0;
345 pipe
->set_viewport_state(pipe
, &vp
);
349 /* draw quad matching scissor rect (XXX verify coord round-off) */
350 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
353 /* Can't depend on old state objects still existing -- may have
354 * been deleted to make room in the hash, etc. (Should get
357 st_invalidate_state(ctx
, _NEW_COLOR
| _NEW_DEPTH
| _NEW_STENCIL
);
359 /* Restore pipe state */
360 cso_set_blend(st
->cso_context
, &st
->state
.blend
);
361 cso_set_depth_stencil_alpha(st
->cso_context
, &st
->state
.depth_stencil
);
362 cso_set_rasterizer(st
->cso_context
, &st
->state
.rasterizer
);
363 pipe
->bind_fs_state(pipe
, st
->fp
->driver_shader
);
364 pipe
->bind_vs_state(pipe
, st
->vp
->driver_shader
);
366 pipe
->set_viewport_state(pipe
, &st
->state
.viewport
);
371 * Determine if we need to clear the depth buffer by drawing a quad.
373 static INLINE GLboolean
374 check_clear_color_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
376 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
378 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
381 if (ctx
->Scissor
.Enabled
)
384 if (!ctx
->Color
.ColorMask
[0] ||
385 !ctx
->Color
.ColorMask
[1] ||
386 !ctx
->Color
.ColorMask
[2] ||
387 !ctx
->Color
.ColorMask
[3])
394 static INLINE GLboolean
395 check_clear_depth_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
397 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
398 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
399 GLboolean maskStencil
400 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
402 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
405 if (ctx
->Scissor
.Enabled
)
416 * Determine if we need to clear the depth buffer by drawing a quad.
418 static INLINE GLboolean
419 check_clear_depth_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
421 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
422 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
424 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
427 if (ctx
->Scissor
.Enabled
)
431 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
432 ctx
->DrawBuffer
->Visual
.stencilBits
> 0)
440 * Determine if we need to clear the stencil buffer by drawing a quad.
442 static INLINE GLboolean
443 check_clear_stencil_with_quad(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
445 const struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
446 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
447 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
448 const GLboolean maskStencil
449 = (ctx
->Stencil
.WriteMask
[0] & stencilMax
) != stencilMax
;
451 if (strb
->surface
->status
== PIPE_SURFACE_STATUS_UNDEFINED
)
457 if (ctx
->Scissor
.Enabled
)
460 /* This is correct, but it is necessary to look at the depth clear
461 * value held in the surface when it comes time to issue the clear,
462 * rather than taking depth and stencil clear values from the
466 strb
->surface
->status
== PIPE_SURFACE_STATUS_DEFINED
&&
467 ctx
->DrawBuffer
->Visual
.depthBits
> 0)
476 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
478 if (check_clear_color_with_quad( ctx
, rb
)) {
479 /* masking or scissoring */
480 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
483 /* clear whole buffer w/out masking */
484 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
486 util_pack_color(ctx
->Color
.ClearColor
, strb
->surface
->format
, &clearValue
);
487 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
493 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
495 if (check_clear_depth_with_quad(ctx
, rb
)) {
496 /* scissoring or we have a combined depth/stencil buffer */
497 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
500 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
502 /* simple clear of whole buffer */
503 uint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
504 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
510 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
512 if (check_clear_stencil_with_quad(ctx
, rb
)) {
513 /* masking or scissoring or combined depth/stencil buffer */
514 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
517 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
519 /* simple clear of whole buffer */
520 GLuint clearValue
= ctx
->Stencil
.Clear
;
522 switch (strb
->surface
->format
) {
523 case PIPE_FORMAT_S8Z24_UNORM
:
527 ; /* no-op, stencil value is in least significant bits */
530 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
536 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
539 if (check_clear_depth_stencil_with_quad(ctx
, rb
)) {
540 /* masking or scissoring */
541 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
544 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
546 /* clear whole buffer w/out masking */
547 GLuint clearValue
= util_pack_z(strb
->surface
->format
, ctx
->Depth
.Clear
);
549 switch (strb
->surface
->format
) {
550 case PIPE_FORMAT_S8Z24_UNORM
:
551 clearValue
|= ctx
->Stencil
.Clear
<< 24;
553 case PIPE_FORMAT_Z24S8_UNORM
:
554 clearValue
|= ctx
->Stencil
.Clear
;
560 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
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 st_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
;