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.
216 * XXX need to emit clip coords when using a vertex program!
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 const GLfloat x0
= ctx
->DrawBuffer
->_Xmin
;
269 const GLfloat y0
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymax
;
270 const GLfloat x1
= ctx
->DrawBuffer
->_Xmax
;
271 const GLfloat y1
= ctx
->DrawBuffer
->Height
- ctx
->DrawBuffer
->_Ymin
;
273 /* alpha state: disabled */
275 struct pipe_alpha_test_state alpha_test
;
276 memset(&alpha_test
, 0, sizeof(alpha_test
));
277 st
->pipe
->set_alpha_test_state(st
->pipe
, &alpha_test
);
280 /* blend state: RGBA masking */
282 struct pipe_blend_state blend
;
283 memset(&blend
, 0, sizeof(blend
));
285 if (ctx
->Color
.ColorMask
[0])
286 blend
.colormask
|= PIPE_MASK_R
;
287 if (ctx
->Color
.ColorMask
[1])
288 blend
.colormask
|= PIPE_MASK_G
;
289 if (ctx
->Color
.ColorMask
[2])
290 blend
.colormask
|= PIPE_MASK_B
;
291 if (ctx
->Color
.ColorMask
[3])
292 blend
.colormask
|= PIPE_MASK_A
;
293 if (st
->ctx
->Color
.DitherFlag
)
296 st
->pipe
->set_blend_state(st
->pipe
, &blend
);
299 /* depth state: always pass */
301 struct pipe_depth_state depth_test
;
302 memset(&depth_test
, 0, sizeof(depth_test
));
304 depth_test
.enabled
= 1;
305 depth_test
.writemask
= 1;
306 depth_test
.func
= PIPE_FUNC_ALWAYS
;
308 st
->pipe
->set_depth_state(st
->pipe
, &depth_test
);
311 /* setup state: nothing */
313 struct pipe_setup_state setup
;
314 memset(&setup
, 0, sizeof(setup
));
315 if (ctx
->Scissor
.Enabled
)
317 st
->pipe
->set_setup_state(st
->pipe
, &setup
);
320 /* stencil state: always set to ref value */
322 struct pipe_stencil_state stencil_test
;
323 memset(&stencil_test
, 0, sizeof(stencil_test
));
325 stencil_test
.front_enabled
= 1;
326 stencil_test
.front_func
= PIPE_FUNC_ALWAYS
;
327 stencil_test
.front_fail_op
= PIPE_STENCIL_OP_REPLACE
;
328 stencil_test
.front_zpass_op
= PIPE_STENCIL_OP_REPLACE
;
329 stencil_test
.front_zfail_op
= PIPE_STENCIL_OP_REPLACE
;
330 stencil_test
.ref_value
[0] = ctx
->Stencil
.Clear
;
331 stencil_test
.value_mask
[0] = 0xff;
332 stencil_test
.write_mask
[0] = ctx
->Stencil
.WriteMask
[0] & 0xff;
334 st
->pipe
->set_stencil_state(st
->pipe
, &stencil_test
);
337 /* fragment shader state: color pass-through program */
339 static struct st_fragment_program
*stfp
= NULL
;
340 struct pipe_shader_state fs
;
342 stfp
= make_color_shader(st
);
344 memset(&fs
, 0, sizeof(fs
));
345 fs
.inputs_read
= stfp
->Base
.Base
.InputsRead
;
346 fs
.tokens
= &stfp
->tokens
[0];
348 st
->pipe
->set_fs_state(st
->pipe
, &fs
);
351 /* vertex shader state: color/position pass-through */
353 static struct st_vertex_program
*stvp
= NULL
;
354 struct pipe_shader_state vs
;
356 stvp
= make_vertex_shader(st
);
358 memset(&vs
, 0, sizeof(vs
));
359 vs
.inputs_read
= stvp
->Base
.Base
.InputsRead
;
360 vs
.outputs_written
= stvp
->Base
.Base
.OutputsWritten
;
361 vs
.tokens
= &stvp
->tokens
[0];
363 st
->pipe
->set_vs_state(st
->pipe
, &vs
);
366 /* draw quad matching scissor rect (XXX verify coord round-off) */
367 draw_quad(ctx
, x0
, y0
, x1
, y1
, ctx
->Depth
.Clear
, ctx
->Color
.ClearColor
);
369 /* Restore pipe state */
370 st
->pipe
->set_alpha_test_state(st
->pipe
, &st
->state
.alpha_test
);
371 st
->pipe
->set_blend_state(st
->pipe
, &st
->state
.blend
);
372 st
->pipe
->set_depth_state(st
->pipe
, &st
->state
.depth
);
373 st
->pipe
->set_fs_state(st
->pipe
, &st
->state
.fs
);
374 st
->pipe
->set_vs_state(st
->pipe
, &st
->state
.vs
);
375 st
->pipe
->set_setup_state(st
->pipe
, &st
->state
.setup
);
376 st
->pipe
->set_stencil_state(st
->pipe
, &st
->state
.stencil
);
378 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
384 clear_color_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
386 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
388 if (ctx
->Color
.ColorMask
[0] &&
389 ctx
->Color
.ColorMask
[1] &&
390 ctx
->Color
.ColorMask
[2] &&
391 ctx
->Color
.ColorMask
[3] &&
392 !ctx
->Scissor
.Enabled
)
394 /* clear whole buffer w/out masking */
396 = color_value(strb
->surface
->format
, ctx
->Color
.ClearColor
);
397 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
400 /* masking or scissoring */
401 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
407 clear_accum_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
409 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
411 if (!ctx
->Scissor
.Enabled
) {
412 /* clear whole buffer w/out masking */
414 = color_value(strb
->surface
->format
, ctx
->Accum
.ClearColor
);
415 /* Note that clearValue is 32 bits but the accum buffer will
416 * typically be 64bpp...
418 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
422 /* XXX point framebuffer.cbufs[0] at the accum buffer */
423 clear_with_quad(ctx
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
429 clear_depth_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
431 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
432 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
434 assert(strb
->surface
->format
);
436 if (ctx
->Scissor
.Enabled
||
437 (isDS
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0)) {
438 /* scissoring or we have a combined depth/stencil buffer */
439 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
442 /* simple clear of whole buffer */
443 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
444 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
450 clear_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
452 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
453 const GLboolean isDS
= is_depth_stencil_format(strb
->surface
->format
);
454 const GLuint stencilMax
= (1 << rb
->StencilBits
) - 1;
455 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
458 ctx
->Scissor
.Enabled
||
459 (isDS
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0)) {
460 /* masking or scissoring or combined depth/stencil buffer */
461 clear_with_quad(ctx
, GL_FALSE
, GL_FALSE
, GL_TRUE
);
464 /* simple clear of whole buffer */
465 GLuint clearValue
= ctx
->Stencil
.Clear
;
466 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
472 clear_depth_stencil_buffer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
474 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
475 const GLuint stencilMax
= 1 << rb
->StencilBits
;
476 GLboolean maskStencil
= ctx
->Stencil
.WriteMask
[0] != stencilMax
;
478 assert(is_depth_stencil_format(strb
->surface
->format
));
480 if (!maskStencil
&& !ctx
->Scissor
.Enabled
) {
481 /* clear whole buffer w/out masking */
482 GLuint clearValue
= depth_value(strb
->surface
->format
, ctx
->Depth
.Clear
);
484 switch (strb
->surface
->format
) {
485 case PIPE_FORMAT_S8_Z24
:
486 clearValue
|= ctx
->Stencil
.Clear
<< 24;
489 case PIPE_FORMAT_Z24_S8
:
490 clearValue
= (clearValue
<< 8) | clearVal
;
497 ctx
->st
->pipe
->clear(ctx
->st
->pipe
, strb
->surface
, clearValue
);
500 /* masking or scissoring */
501 clear_with_quad(ctx
, GL_FALSE
, GL_TRUE
, GL_TRUE
);
508 * Called via ctx->Driver.Clear()
509 * XXX: doesn't pick up the differences between front/back/left/right
510 * clears. Need to sort that out...
512 static void st_clear(GLcontext
*ctx
, GLbitfield mask
)
514 static const GLbitfield BUFFER_BITS_DS
515 = (BUFFER_BIT_DEPTH
| BUFFER_BIT_STENCIL
);
516 struct st_context
*st
= ctx
->st
;
517 struct gl_renderbuffer
*depthRb
518 = ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
519 struct gl_renderbuffer
*stencilRb
520 = ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
522 /* This makes sure the softpipe has the latest scissor, etc values */
523 st_validate_state( st
);
527 * If we're going to use clear_with_quad() for any reason, use it to
528 * clear as many other buffers as possible.
529 * As it is now, we sometimes call clear_with_quad() three times to clear
530 * color/depth/stencil individually...
533 if (mask
& BUFFER_BITS_COLOR
) {
535 for (b
= 0; b
< BUFFER_COUNT
; b
++) {
536 if (BUFFER_BITS_COLOR
& mask
& (1 << b
)) {
537 struct gl_renderbuffer
*rb
538 = ctx
->DrawBuffer
->Attachment
[b
].Renderbuffer
;
540 clear_color_buffer(ctx
, rb
);
545 if (mask
& BUFFER_BIT_ACCUM
) {
546 clear_accum_buffer(ctx
,
547 ctx
->DrawBuffer
->Attachment
[BUFFER_ACCUM
].Renderbuffer
);
550 if ((mask
& BUFFER_BITS_DS
) == BUFFER_BITS_DS
&& depthRb
== stencilRb
) {
551 /* clearing combined depth + stencil */
552 clear_depth_stencil_buffer(ctx
, depthRb
);
555 /* separate depth/stencil clears */
556 if (mask
& BUFFER_BIT_DEPTH
) {
557 clear_depth_buffer(ctx
, depthRb
);
559 if (mask
& BUFFER_BIT_STENCIL
) {
560 clear_stencil_buffer(ctx
, stencilRb
);
566 void st_init_clear_functions(struct dd_function_table
*functions
)
568 functions
->Clear
= st_clear
;