comments
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "main/glheader.h"
35 #include "main/macros.h"
36 #include "shader/prog_instruction.h"
37 #include "st_atom.h"
38 #include "st_context.h"
39 #include "st_cb_clear.h"
40 #include "st_cb_fbo.h"
41 #include "st_program.h"
42 #include "st_public.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
46 #include "vf/vf.h"
47
48
49
50
51 static GLuint
52 color_value(GLuint pipeFormat, const GLfloat color[4])
53 {
54 GLubyte r, g, b, a;
55
56 UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
57 UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
58 UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
59 UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);
60
61 switch (pipeFormat) {
62 case PIPE_FORMAT_U_R8_G8_B8_A8:
63 return (r << 24) | (g << 16) | (b << 8) | a;
64 case PIPE_FORMAT_U_A8_R8_G8_B8:
65 return (a << 24) | (r << 16) | (g << 8) | b;
66 case PIPE_FORMAT_U_R5_G6_B5:
67 return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
68 default:
69 return 0;
70 }
71 }
72
73
74 static GLuint
75 depth_value(GLuint pipeFormat, GLfloat value)
76 {
77 GLuint val;
78 switch (pipeFormat) {
79 case PIPE_FORMAT_U_Z16:
80 val = (GLuint) (value * 0xffffff);
81 break;
82 case PIPE_FORMAT_U_Z32:
83 val = (GLuint) (value * 0xffffffff);
84 break;
85 case PIPE_FORMAT_S8_Z24:
86 /*case PIPE_FORMAT_Z24_S8:*/
87 val = (GLuint) (value * 0xffffff);
88 break;
89 default:
90 assert(0);
91 }
92 return val;
93 }
94
95
96 static GLboolean
97 is_depth_stencil_format(GLuint pipeFormat)
98 {
99 switch (pipeFormat) {
100 case PIPE_FORMAT_S8_Z24:
101 /*case PIPE_FORMAT_Z24_S8:*/
102 return GL_TRUE;
103 default:
104 return GL_FALSE;
105 }
106 }
107
108
109
110 /**
111 * Create a simple fragment shader that just passes through the fragment color.
112 */
113 static struct st_fragment_program *
114 make_color_shader(struct st_context *st)
115 {
116 GLcontext *ctx = st->ctx;
117 struct st_fragment_program *stfp;
118 struct gl_program *p;
119 GLboolean b;
120
121 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
122 if (!p)
123 return NULL;
124
125 p->NumInstructions = 2;
126 p->Instructions = _mesa_alloc_instructions(2);
127 if (!p->Instructions) {
128 ctx->Driver.DeleteProgram(ctx, p);
129 return NULL;
130 }
131 _mesa_init_instructions(p->Instructions, 2);
132 /* MOV result.color, fragment.color; */
133 p->Instructions[0].Opcode = OPCODE_MOV;
134 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
135 p->Instructions[0].DstReg.Index = FRAG_RESULT_COLR;
136 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
137 p->Instructions[0].SrcReg[0].Index = FRAG_ATTRIB_COL0;
138 /* END; */
139 p->Instructions[1].Opcode = OPCODE_END;
140
141 p->InputsRead = FRAG_BIT_COL0;
142 p->OutputsWritten = (1 << FRAG_RESULT_COLR);
143
144 stfp = (struct st_fragment_program *) p;
145 /* compile into tgsi format */
146 b = tgsi_mesa_compile_fp_program(&stfp->Base,
147 stfp->tokens, ST_FP_MAX_TOKENS);
148 assert(b);
149
150 return stfp;
151 }
152
153
154 /**
155 * Draw a screen-aligned quadrilateral.
156 * Coords are window coords.
157 */
158 static void
159 draw_quad(GLcontext *ctx,
160 float x0, float y0, float x1, float y1, GLfloat z,
161 const GLfloat color[4])
162 {
163 static const GLuint attribs[2] = {
164 VF_ATTRIB_POS,
165 VF_ATTRIB_COLOR0
166 };
167 GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
168 GLuint i;
169
170 /* positions */
171 verts[0][0][0] = x0;
172 verts[0][0][1] = y0;
173
174 verts[1][0][0] = x1;
175 verts[1][0][1] = y0;
176
177 verts[2][0][0] = x1;
178 verts[2][0][1] = y1;
179
180 verts[3][0][0] = x0;
181 verts[3][0][1] = y1;
182
183 /* same for all verts: */
184 for (i = 0; i < 4; i++) {
185 verts[i][0][2] = z;
186 verts[i][0][3] = 1.0;
187 verts[i][1][0] = color[0];
188 verts[i][1][1] = color[1];
189 verts[i][1][2] = color[2];
190 verts[i][1][3] = color[3];
191 }
192
193 ctx->st->pipe->draw_vertices(ctx->st->pipe, GL_QUADS,
194 4, (GLfloat *) verts, 2, attribs);
195 }
196
197
198
199 /**
200 * Do glClear by drawing a quadrilateral.
201 */
202 static void
203 clear_with_quad(GLcontext *ctx, GLuint x0, GLuint y0,
204 GLuint x1, GLuint y1,
205 GLboolean color, GLboolean depth, GLboolean stencil)
206 {
207 static struct st_fragment_program *stfp = NULL;
208 struct st_context *st = ctx->st;
209 struct pipe_alpha_test_state alpha_test;
210 struct pipe_blend_state blend;
211 struct pipe_depth_state depth_test;
212 struct pipe_stencil_state stencil_test;
213 struct pipe_setup_state setup;
214 struct pipe_fs_state fs;
215
216 /* alpha state: disabled */
217 memset(&alpha_test, 0, sizeof(alpha_test));
218 st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
219
220 /* blend state: RGBA masking */
221 memset(&blend, 0, sizeof(blend));
222 if (color) {
223 if (ctx->Color.ColorMask[0])
224 blend.colormask |= PIPE_MASK_R;
225 if (ctx->Color.ColorMask[1])
226 blend.colormask |= PIPE_MASK_G;
227 if (ctx->Color.ColorMask[2])
228 blend.colormask |= PIPE_MASK_B;
229 if (ctx->Color.ColorMask[3])
230 blend.colormask |= PIPE_MASK_A;
231 if (st->ctx->Color.DitherFlag)
232 blend.dither = 1;
233 }
234 st->pipe->set_blend_state(st->pipe, &blend);
235
236 /* depth state: always pass */
237 memset(&depth_test, 0, sizeof(depth_test));
238 if (depth) {
239 depth_test.enabled = 1;
240 depth_test.writemask = 1;
241 depth_test.func = PIPE_FUNC_ALWAYS;
242 }
243 st->pipe->set_depth_state(st->pipe, &depth_test);
244
245 /* setup state: nothing */
246 memset(&setup, 0, sizeof(setup));
247 if (ctx->Scissor.Enabled)
248 setup.scissor = 1;
249 st->pipe->set_setup_state(st->pipe, &setup);
250
251 /* stencil state: always set to ref value */
252 memset(&stencil_test, 0, sizeof(stencil_test));
253 if (stencil) {
254 stencil_test.front_enabled = 1;
255 stencil_test.front_func = PIPE_FUNC_ALWAYS;
256 stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
257 stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
258 stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
259 stencil_test.ref_value[0] = ctx->Stencil.Clear;
260 stencil_test.value_mask[0] = 0xff;
261 stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
262 }
263 st->pipe->set_stencil_state(st->pipe, &stencil_test);
264
265 /* fragment shader state: color pass-through program */
266 if (!stfp) {
267 stfp = make_color_shader(st);
268 }
269 memset(&fs, 0, sizeof(fs));
270 fs.inputs_read = stfp->Base.Base.InputsRead;
271 fs.tokens = &stfp->tokens[0];
272 fs.constants = NULL;
273 st->pipe->set_fs_state(st->pipe, &fs);
274
275 /* draw quad matching scissor rect (XXX verify coord round-off) */
276 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
277
278 /* Restore pipe state */
279 st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
280 st->pipe->set_blend_state(st->pipe, &st->state.blend);
281 st->pipe->set_depth_state(st->pipe, &st->state.depth);
282 st->pipe->set_fs_state(st->pipe, &st->state.fs);
283 st->pipe->set_setup_state(st->pipe, &st->state.setup);
284 st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
285 /* OR:
286 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
287 */
288 }
289
290
291 static void
292 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
293 {
294 struct st_renderbuffer *strb = st_renderbuffer(rb);
295
296 if (ctx->Color.ColorMask[0] &&
297 ctx->Color.ColorMask[1] &&
298 ctx->Color.ColorMask[2] &&
299 ctx->Color.ColorMask[3] &&
300 !ctx->Scissor.Enabled)
301 {
302 /* clear whole buffer w/out masking */
303 GLuint clearValue
304 = color_value(strb->surface->format, ctx->Color.ClearColor);
305 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
306 }
307 else {
308 /* masking or scissoring */
309 clear_with_quad(ctx,
310 ctx->DrawBuffer->_Xmin,
311 ctx->DrawBuffer->_Xmin,
312 ctx->DrawBuffer->_Xmax,
313 ctx->DrawBuffer->_Ymax,
314 GL_TRUE, GL_FALSE, GL_FALSE);
315 }
316 }
317
318
319 static void
320 clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
321 {
322 struct st_renderbuffer *strb = st_renderbuffer(rb);
323
324 if (!ctx->Scissor.Enabled) {
325 /* clear whole buffer w/out masking */
326 GLuint clearValue
327 = color_value(strb->surface->format, ctx->Accum.ClearColor);
328 /* Note that clearValue is 32 bits but the accum buffer will
329 * typically be 64bpp...
330 */
331 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
332 }
333 else {
334 /* scissoring */
335 /* XXX point framebuffer.cbufs[0] at the accum buffer */
336 clear_with_quad(ctx,
337 ctx->DrawBuffer->_Xmin,
338 ctx->DrawBuffer->_Xmin,
339 ctx->DrawBuffer->_Xmax,
340 ctx->DrawBuffer->_Ymax,
341 GL_TRUE, GL_FALSE, GL_FALSE);
342 }
343 }
344
345
346 static void
347 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
348 {
349 struct st_renderbuffer *strb = st_renderbuffer(rb);
350
351 if (!ctx->Scissor.Enabled &&
352 !is_depth_stencil_format(strb->surface->format)) {
353 /* clear whole depth buffer w/out masking */
354 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
355 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
356 }
357 else {
358 /* masking or scissoring or combined z/stencil buffer */
359 clear_with_quad(ctx,
360 ctx->DrawBuffer->_Xmin,
361 ctx->DrawBuffer->_Xmin,
362 ctx->DrawBuffer->_Xmax,
363 ctx->DrawBuffer->_Ymax,
364 GL_FALSE, GL_TRUE, GL_FALSE);
365 }
366 }
367
368
369 static void
370 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
371 {
372 struct st_renderbuffer *strb = st_renderbuffer(rb);
373 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
374 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
375
376 if (!maskStencil && !ctx->Scissor.Enabled &&
377 !is_depth_stencil_format(strb->surface->format)) {
378 /* clear whole stencil buffer w/out masking */
379 GLuint clearValue = ctx->Stencil.Clear;
380 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
381 }
382 else {
383 /* masking or scissoring */
384 clear_with_quad(ctx,
385 ctx->DrawBuffer->_Xmin,
386 ctx->DrawBuffer->_Xmin,
387 ctx->DrawBuffer->_Xmax,
388 ctx->DrawBuffer->_Ymax,
389 GL_FALSE, GL_FALSE, GL_TRUE);
390 }
391 }
392
393
394 static void
395 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
396 {
397 struct st_renderbuffer *strb = st_renderbuffer(rb);
398 const GLuint stencilMax = 1 << rb->StencilBits;
399 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
400
401 assert(is_depth_stencil_format(strb->surface->format));
402
403 if (!maskStencil && !ctx->Scissor.Enabled) {
404 /* clear whole buffer w/out masking */
405 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
406
407 switch (strb->surface->format) {
408 case PIPE_FORMAT_S8_Z24:
409 clearValue |= ctx->Stencil.Clear << 24;
410 break;
411 #if 0
412 case PIPE_FORMAT_Z24_S8:
413 clearValue = (clearValue << 8) | clearVal;
414 break;
415 #endif
416 default:
417 assert(0);
418 }
419
420 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
421 }
422 else {
423 /* masking or scissoring */
424 clear_with_quad(ctx,
425 ctx->DrawBuffer->_Xmin,
426 ctx->DrawBuffer->_Xmin,
427 ctx->DrawBuffer->_Xmax,
428 ctx->DrawBuffer->_Ymax,
429 GL_FALSE, GL_TRUE, GL_TRUE);
430 }
431 }
432
433
434
435 /**
436 * Called via ctx->Driver.Clear()
437 * XXX: doesn't pick up the differences between front/back/left/right
438 * clears. Need to sort that out...
439 */
440 static void st_clear(GLcontext *ctx, GLbitfield mask)
441 {
442 static const GLbitfield BUFFER_BITS_DS
443 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
444 struct st_context *st = ctx->st;
445 struct gl_renderbuffer *depthRb
446 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
447 struct gl_renderbuffer *stencilRb
448 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
449
450 /* This makes sure the softpipe has the latest scissor, etc values */
451 st_validate_state( st );
452
453 /*
454 * XXX TO-DO:
455 * If we're going to use clear_with_quad() for any reason, use it to
456 * clear as many other buffers as possible.
457 * As it is now, we sometimes call clear_with_quad() three times to clear
458 * color/depth/stencil individually...
459 */
460
461 if (mask & BUFFER_BITS_COLOR) {
462 GLuint b;
463 for (b = 0; b < BUFFER_COUNT; b++) {
464 if (BUFFER_BITS_COLOR & mask & (1 << b)) {
465 clear_color_buffer(ctx,
466 ctx->DrawBuffer->Attachment[b].Renderbuffer);
467 }
468 }
469 }
470
471 if (mask & BUFFER_BIT_ACCUM) {
472 clear_accum_buffer(ctx,
473 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
474 }
475
476 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
477 /* clearing combined depth + stencil */
478 clear_depth_stencil_buffer(ctx, depthRb);
479 }
480 else {
481 /* separate depth/stencil clears */
482 if (mask & BUFFER_BIT_DEPTH) {
483 clear_depth_buffer(ctx, depthRb);
484 }
485 if (mask & BUFFER_BIT_STENCIL) {
486 clear_stencil_buffer(ctx, stencilRb);
487 }
488 }
489 }
490
491
492 void st_init_clear_functions(struct dd_function_table *functions)
493 {
494 functions->Clear = st_clear;
495 }