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"
42 #include "st_program.h"
43 #include "st_public.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_state.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_winsys.h"
50 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
58 color_value(GLuint pipeFormat
, const GLfloat color
[4])
62 UNCLAMPED_FLOAT_TO_UBYTE(r
, color
[0]);
63 UNCLAMPED_FLOAT_TO_UBYTE(g
, color
[1]);
64 UNCLAMPED_FLOAT_TO_UBYTE(b
, color
[2]);
65 UNCLAMPED_FLOAT_TO_UBYTE(a
, color
[3]);
68 case PIPE_FORMAT_U_R8_G8_B8_A8
:
69 return (r
<< 24) | (g
<< 16) | (b
<< 8) | a
;
70 case PIPE_FORMAT_U_A8_R8_G8_B8
:
71 return (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
72 case PIPE_FORMAT_U_R5_G6_B5
:
73 return ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | (b
>> 3);
81 depth_value(GLuint pipeFormat
, GLfloat value
)
85 case PIPE_FORMAT_U_Z16
:
86 val
= (GLuint
) (value
* 0xffffff);
88 case PIPE_FORMAT_U_Z32
:
89 val
= (GLuint
) (value
* 0xffffffff);
91 case PIPE_FORMAT_S8_Z24
:
92 /*case PIPE_FORMAT_Z24_S8:*/
93 val
= (GLuint
) (value
* 0xffffff);
103 is_depth_stencil_format(GLuint pipeFormat
)
105 switch (pipeFormat
) {
106 case PIPE_FORMAT_S8_Z24
:
107 /*case PIPE_FORMAT_Z24_S8:*/
117 * Create a simple fragment shader that just passes through the fragment color.
119 static struct st_fragment_program
*
120 make_color_shader(struct st_context
*st
)
122 GLcontext
*ctx
= st
->ctx
;
123 struct st_fragment_program
*stfp
;
124 struct gl_program
*p
;
127 p
= ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
131 p
->NumInstructions
= 2;
132 p
->Instructions
= _mesa_alloc_instructions(2);
133 if (!p
->Instructions
) {
134 ctx
->Driver
.DeleteProgram(ctx
, p
);
137 _mesa_init_instructions(p
->Instructions
, 2);
138 /* MOV result.color, fragment.color; */
139 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
140 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
141 p
->Instructions
[0].DstReg
.Index
= FRAG_RESULT_COLR
;
142 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
143 p
->Instructions
[0].SrcReg
[0].Index
= FRAG_ATTRIB_COL0
;
145 p
->Instructions
[1].Opcode
= OPCODE_END
;
147 p
->InputsRead
= FRAG_BIT_COL0
;
148 p
->OutputsWritten
= (1 << FRAG_RESULT_COLR
);
150 stfp
= (struct st_fragment_program
*) p
;
151 /* compile into tgsi format */
152 b
= tgsi_mesa_compile_fp_program(&stfp
->Base
,
153 stfp
->tokens
, ST_FP_MAX_TOKENS
);
161 * Create a simple vertex shader that just passes through the
162 * vertex position and color.
164 static struct st_vertex_program
*
165 make_vertex_shader(struct st_context
*st
)
167 GLcontext
*ctx
= st
->ctx
;
168 struct st_vertex_program
*stvp
;
169 struct gl_program
*p
;
172 p
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
176 p
->NumInstructions
= 3;
177 p
->Instructions
= _mesa_alloc_instructions(3);
178 if (!p
->Instructions
) {
179 ctx
->Driver
.DeleteProgram(ctx
, p
);
182 _mesa_init_instructions(p
->Instructions
, 3);
183 /* MOV result.pos, vertex.pos; */
184 p
->Instructions
[0].Opcode
= OPCODE_MOV
;
185 p
->Instructions
[0].DstReg
.File
= PROGRAM_OUTPUT
;
186 p
->Instructions
[0].DstReg
.Index
= VERT_RESULT_HPOS
;
187 p
->Instructions
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
188 p
->Instructions
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
189 /* MOV result.color, vertex.color; */
190 p
->Instructions
[1].Opcode
= OPCODE_MOV
;
191 p
->Instructions
[1].DstReg
.File
= PROGRAM_OUTPUT
;
192 p
->Instructions
[1].DstReg
.Index
= VERT_RESULT_COL0
;
193 p
->Instructions
[1].SrcReg
[0].File
= PROGRAM_INPUT
;
194 p
->Instructions
[1].SrcReg
[0].Index
= VERT_ATTRIB_COLOR0
;
196 p
->Instructions
[2].Opcode
= OPCODE_END
;
198 p
->InputsRead
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
199 p
->OutputsWritten
= ((1 << VERT_RESULT_COL0
) |
200 (1 << VERT_RESULT_HPOS
));
202 stvp
= (struct st_vertex_program
*) p
;
203 /* compile into tgsi format */
204 b
= tgsi_mesa_compile_vp_program(&stvp
->Base
,
205 stvp
->tokens
, ST_FP_MAX_TOKENS
);
214 * Draw a screen-aligned quadrilateral.
215 * Coords are window coords with y=0=bottom. These coords will be transformed
216 * by the vertex shader and viewport transform (which will flip Y if needed).
219 draw_quad(GLcontext
*ctx
,
220 float x0
, float y0
, float x1
, float y1
, GLfloat z
,
221 const GLfloat color
[4])
223 static const GLuint attribs
[2] = {
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, attribs
);
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
= ctx
->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 memset(&alpha_test
, 0, sizeof(alpha_test
));
278 pipe
->set_alpha_test_state(pipe
, &alpha_test
);
281 /* blend state: RGBA masking */
283 struct pipe_blend_state blend
;
284 memset(&blend
, 0, sizeof(blend
));
286 if (ctx
->Color
.ColorMask
[0])
287 blend
.colormask
|= PIPE_MASK_R
;
288 if (ctx
->Color
.ColorMask
[1])
289 blend
.colormask
|= PIPE_MASK_G
;
290 if (ctx
->Color
.ColorMask
[2])
291 blend
.colormask
|= PIPE_MASK_B
;
292 if (ctx
->Color
.ColorMask
[3])
293 blend
.colormask
|= PIPE_MASK_A
;
294 if (st
->ctx
->Color
.DitherFlag
)
297 pipe
->set_blend_state(pipe
, &blend
);
300 /* depth state: always pass */
302 struct pipe_depth_state depth_test
;
303 memset(&depth_test
, 0, sizeof(depth_test
));
305 depth_test
.enabled
= 1;
306 depth_test
.writemask
= 1;
307 depth_test
.func
= PIPE_FUNC_ALWAYS
;
309 pipe
->set_depth_state(pipe
, &depth_test
);
312 /* setup state: nothing */
314 struct pipe_setup_state setup
;
315 memset(&setup
, 0, sizeof(setup
));
317 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
318 * that matches the scissor bounds.
320 if (ctx
->Scissor
.Enabled
)
323 pipe
->set_setup_state(pipe
, &setup
);
326 /* stencil state: always set to ref value */
328 struct pipe_stencil_state stencil_test
;
329 memset(&stencil_test
, 0, sizeof(stencil_test
));
331 stencil_test
.front_enabled
= 1;
332 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
333 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
334 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
335 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
336 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
337 stencil_test
.value_mask
[0] = 0xff;
338 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
340 pipe
->set_stencil_state(pipe
, &stencil_test
);
343 /* fragment shader state: color pass-through program */
345 static struct st_fragment_program
*stfp
= NULL
;
346 struct pipe_shader_state fs
;
348 stfp
= make_color_shader(st
);
350 memset(&fs
, 0, sizeof(fs
));
351 fs
.inputs_read
= tgsi_mesa_translate_fragment_input_mask(stfp
->Base
.Base
.InputsRead
);
352 fs
.outputs_written
= tgsi_mesa_translate_fragment_output_mask(stfp
->Base
.Base
.OutputsWritten
);
353 fs
.tokens
= &stfp
->tokens
[0];
354 pipe
->set_fs_state(pipe
, &fs
);
357 /* vertex shader state: color/position pass-through */
359 static struct st_vertex_program
*stvp
= NULL
;
360 struct pipe_shader_state vs
;
362 stvp
= make_vertex_shader(st
);
364 memset(&vs
, 0, sizeof(vs
));
365 vs
.inputs_read
= stvp
->Base
.Base
.InputsRead
;
366 vs
.outputs_written
= stvp
->Base
.Base
.OutputsWritten
;
367 vs
.tokens
= &stvp
->tokens
[0];
368 pipe
->set_vs_state(pipe
, &vs
);
371 /* viewport state: viewport matching window dims */
373 const float width
= ctx
->DrawBuffer
->Width
;
374 const float height
= ctx
->DrawBuffer
->Height
;
375 struct pipe_viewport_state vp
;
376 vp
.scale
[0] = 0.5 * width
;
377 vp
.scale
[1] = -0.5 * height
;
380 vp
.translate
[0] = 0.5 * width
;
381 vp
.translate
[1] = 0.5 * height
;
382 vp
.translate
[2] = 0.5;
383 vp
.translate
[3] = 0.0;
384 pipe
->set_viewport_state(pipe
, &vp
);
387 /* draw quad matching scissor rect (XXX verify coord round-off) */
388 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
390 /* Restore pipe state */
391 pipe
->set_alpha_test_state(pipe
, &st
->state
.alpha_test
);
392 pipe
->set_blend_state(pipe
, &st
->state
.blend
);
393 pipe
->set_depth_state(pipe
, &st
->state
.depth
);
394 pipe
->set_fs_state(pipe
, &st
->state
.fs
);
395 pipe
->set_vs_state(pipe
, &st
->state
.vs
);
396 pipe
->set_setup_state(pipe
, &st
->state
.setup
);
397 pipe
->set_stencil_state(pipe
, &st
->state
.stencil
);
398 pipe
->set_viewport_state(pipe
, &ctx
->st
->state
.viewport
);
400 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
406 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
408 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
410 if (ctx
->Color
.ColorMask
[0] &&
411 ctx
->Color
.ColorMask
[1] &&
412 ctx
->Color
.ColorMask
[2] &&
413 ctx
->Color
.ColorMask
[3] &&
414 !ctx
->Scissor
.Enabled
)
416 /* clear whole buffer w/out masking */
418 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
419 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
422 /* masking or scissoring */
423 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
429 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
431 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
433 if (!ctx
->Scissor
.Enabled
) {
434 /* clear whole buffer w/out masking */
436 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
437 /* Note that clearValue is 32 bits but the accum buffer will
438 * typically be 64bpp...
440 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
444 /* XXX point framebuffer.cbufs[0] at the accum buffer */
445 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
451 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
453 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
454 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
456 assert(strb
->surface
->format
);
458 if (ctx
->Scissor
.Enabled
||
459 (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0)) {
460 /* scissoring or we have a combined depth/stencil buffer */
461 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
464 /* simple clear of whole buffer */
465 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
466 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
472 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
474 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
475 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
476 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
477 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
480 ctx
->Scissor
.Enabled
||
481 (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0)) {
482 /* masking or scissoring or combined depth/stencil buffer */
483 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
486 /* simple clear of whole buffer */
487 GLuint clearValue
= ctx
->Stencil
.Clear
;
488 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
494 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
496 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
497 const GLuint stencilMax
= 1 << rb
->StencilBits
;
498 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
500 assert(is_depth_stencil_format(strb
->surface
->format
));
502 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
503 /* clear whole buffer w/out masking */
504 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
506 switch (strb
->surface
->format
) {
507 case PIPE_FORMAT_S8_Z24
:
508 clearValue
|= ctx
->Stencil
.Clear
<< 24;
511 case PIPE_FORMAT_Z24_S8
:
512 clearValue
= (clearValue
<< 8) | clearVal
;
519 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
522 /* masking or scissoring */
523 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
530 * Called via ctx->Driver.Clear()
531 * XXX: doesn't pick up the differences between front/back/left/right
532 * clears. Need to sort that out...
534 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
536 static const GLbitfield BUFFER_BITS_DS
537 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
538 struct st_context
*st
= ctx
->st
;
539 struct gl_renderbuffer
*depthRb
540 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
541 struct gl_renderbuffer
*stencilRb
542 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
544 /* This makes sure the softpipe has the latest scissor, etc values */
545 st_validate_state( st
);
549 * If we're going to use clear_with_quad() for any reason, use it to
550 * clear as many other buffers as possible.
551 * As it is now, we sometimes call clear_with_quad() three times to clear
552 * color/depth/stencil individually...
555 if (mask
& BUFFER_BITS_COLOR
) {
557 for (b
= 0; b
< BUFFER_COUNT
; b
++) {
558 if (BUFFER_BITS_COLOR
& mask
& (1 << b
)) {
559 struct gl_renderbuffer
*rb
560 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
562 clear_color_buffer(ctx
, rb
);
567 if (mask
& BUFFER_BIT_ACCUM
) {
568 clear_accum_buffer(ctx
,
569 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
572 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
573 /* clearing combined depth + stencil */
574 clear_depth_stencil_buffer(ctx
, depthRb
);
577 /* separate depth/stencil clears */
578 if (mask
& BUFFER_BIT_DEPTH
) {
579 clear_depth_buffer(ctx
, depthRb
);
581 if (mask
& BUFFER_BIT_STENCIL
) {
582 clear_stencil_buffer(ctx
, stencilRb
);
588 void st_init_clear_functions(struct dd_function_table
*functions
)
590 functions
->Clear
= st_clear
;