Y invert, clean-up
[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 * The vertices of the quad will be computed from the
202 * ctx->DrawBuffer->_X/Ymin/max fields.
203 */
204 static void
205 clear_with_quad(GLcontext *ctx,
206 GLboolean color, GLboolean depth, GLboolean stencil)
207 {
208 struct st_context *st = ctx->st;
209 const GLfloat x0 = ctx->DrawBuffer->_Xmin;
210 const GLfloat y0 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax;
211 const GLfloat x1 = ctx->DrawBuffer->_Xmax;
212 const GLfloat y1 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin;
213
214 /* alpha state: disabled */
215 {
216 struct pipe_alpha_test_state alpha_test;
217 memset(&alpha_test, 0, sizeof(alpha_test));
218 st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
219 }
220
221 /* blend state: RGBA masking */
222 {
223 struct pipe_blend_state blend;
224 memset(&blend, 0, sizeof(blend));
225 if (color) {
226 if (ctx->Color.ColorMask[0])
227 blend.colormask |= PIPE_MASK_R;
228 if (ctx->Color.ColorMask[1])
229 blend.colormask |= PIPE_MASK_G;
230 if (ctx->Color.ColorMask[2])
231 blend.colormask |= PIPE_MASK_B;
232 if (ctx->Color.ColorMask[3])
233 blend.colormask |= PIPE_MASK_A;
234 if (st->ctx->Color.DitherFlag)
235 blend.dither = 1;
236 }
237 st->pipe->set_blend_state(st->pipe, &blend);
238 }
239
240 /* depth state: always pass */
241 {
242 struct pipe_depth_state depth_test;
243 memset(&depth_test, 0, sizeof(depth_test));
244 if (depth) {
245 depth_test.enabled = 1;
246 depth_test.writemask = 1;
247 depth_test.func = PIPE_FUNC_ALWAYS;
248 }
249 st->pipe->set_depth_state(st->pipe, &depth_test);
250 }
251
252 /* setup state: nothing */
253 {
254 struct pipe_setup_state setup;
255 memset(&setup, 0, sizeof(setup));
256 if (ctx->Scissor.Enabled)
257 setup.scissor = 1;
258 st->pipe->set_setup_state(st->pipe, &setup);
259 }
260
261 /* stencil state: always set to ref value */
262 {
263 struct pipe_stencil_state stencil_test;
264 memset(&stencil_test, 0, sizeof(stencil_test));
265 if (stencil) {
266 stencil_test.front_enabled = 1;
267 stencil_test.front_func = PIPE_FUNC_ALWAYS;
268 stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
269 stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
270 stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
271 stencil_test.ref_value[0] = ctx->Stencil.Clear;
272 stencil_test.value_mask[0] = 0xff;
273 stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
274 }
275 st->pipe->set_stencil_state(st->pipe, &stencil_test);
276 }
277
278 /* fragment shader state: color pass-through program */
279 {
280 static struct st_fragment_program *stfp = NULL;
281 struct pipe_fs_state fs;
282 if (!stfp) {
283 stfp = make_color_shader(st);
284 }
285 memset(&fs, 0, sizeof(fs));
286 fs.inputs_read = stfp->Base.Base.InputsRead;
287 fs.tokens = &stfp->tokens[0];
288 fs.constants = NULL;
289 st->pipe->set_fs_state(st->pipe, &fs);
290 }
291
292 /* draw quad matching scissor rect (XXX verify coord round-off) */
293 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
294
295 /* Restore pipe state */
296 st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
297 st->pipe->set_blend_state(st->pipe, &st->state.blend);
298 st->pipe->set_depth_state(st->pipe, &st->state.depth);
299 st->pipe->set_fs_state(st->pipe, &st->state.fs);
300 st->pipe->set_setup_state(st->pipe, &st->state.setup);
301 st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
302 /* OR:
303 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
304 */
305 }
306
307
308 static void
309 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
310 {
311 struct st_renderbuffer *strb = st_renderbuffer(rb);
312
313 if (ctx->Color.ColorMask[0] &&
314 ctx->Color.ColorMask[1] &&
315 ctx->Color.ColorMask[2] &&
316 ctx->Color.ColorMask[3] &&
317 !ctx->Scissor.Enabled)
318 {
319 /* clear whole buffer w/out masking */
320 GLuint clearValue
321 = color_value(strb->surface->format, ctx->Color.ClearColor);
322 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
323 }
324 else {
325 /* masking or scissoring */
326 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
327 }
328 }
329
330
331 static void
332 clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
333 {
334 struct st_renderbuffer *strb = st_renderbuffer(rb);
335
336 if (!ctx->Scissor.Enabled) {
337 /* clear whole buffer w/out masking */
338 GLuint clearValue
339 = color_value(strb->surface->format, ctx->Accum.ClearColor);
340 /* Note that clearValue is 32 bits but the accum buffer will
341 * typically be 64bpp...
342 */
343 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
344 }
345 else {
346 /* scissoring */
347 /* XXX point framebuffer.cbufs[0] at the accum buffer */
348 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
349 }
350 }
351
352
353 static void
354 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
355 {
356 struct st_renderbuffer *strb = st_renderbuffer(rb);
357
358 if (!ctx->Scissor.Enabled &&
359 !is_depth_stencil_format(strb->surface->format)) {
360 /* clear whole depth buffer w/out masking */
361 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
362 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
363 }
364 else {
365 /* masking or scissoring or combined z/stencil buffer */
366 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
367 }
368 }
369
370
371 static void
372 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
373 {
374 struct st_renderbuffer *strb = st_renderbuffer(rb);
375 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
376 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
377
378 if (!maskStencil && !ctx->Scissor.Enabled &&
379 !is_depth_stencil_format(strb->surface->format)) {
380 /* clear whole stencil buffer w/out masking */
381 GLuint clearValue = ctx->Stencil.Clear;
382 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
383 }
384 else {
385 /* masking or scissoring */
386 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
387 }
388 }
389
390
391 static void
392 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
393 {
394 struct st_renderbuffer *strb = st_renderbuffer(rb);
395 const GLuint stencilMax = 1 << rb->StencilBits;
396 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
397
398 assert(is_depth_stencil_format(strb->surface->format));
399
400 if (!maskStencil && !ctx->Scissor.Enabled) {
401 /* clear whole buffer w/out masking */
402 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
403
404 switch (strb->surface->format) {
405 case PIPE_FORMAT_S8_Z24:
406 clearValue |= ctx->Stencil.Clear << 24;
407 break;
408 #if 0
409 case PIPE_FORMAT_Z24_S8:
410 clearValue = (clearValue << 8) | clearVal;
411 break;
412 #endif
413 default:
414 assert(0);
415 }
416
417 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
418 }
419 else {
420 /* masking or scissoring */
421 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
422 }
423 }
424
425
426
427 /**
428 * Called via ctx->Driver.Clear()
429 * XXX: doesn't pick up the differences between front/back/left/right
430 * clears. Need to sort that out...
431 */
432 static void st_clear(GLcontext *ctx, GLbitfield mask)
433 {
434 static const GLbitfield BUFFER_BITS_DS
435 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
436 struct st_context *st = ctx->st;
437 struct gl_renderbuffer *depthRb
438 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
439 struct gl_renderbuffer *stencilRb
440 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
441
442 /* This makes sure the softpipe has the latest scissor, etc values */
443 st_validate_state( st );
444
445 /*
446 * XXX TO-DO:
447 * If we're going to use clear_with_quad() for any reason, use it to
448 * clear as many other buffers as possible.
449 * As it is now, we sometimes call clear_with_quad() three times to clear
450 * color/depth/stencil individually...
451 */
452
453 if (mask & BUFFER_BITS_COLOR) {
454 GLuint b;
455 for (b = 0; b < BUFFER_COUNT; b++) {
456 if (BUFFER_BITS_COLOR & mask & (1 << b)) {
457 clear_color_buffer(ctx,
458 ctx->DrawBuffer->Attachment[b].Renderbuffer);
459 }
460 }
461 }
462
463 if (mask & BUFFER_BIT_ACCUM) {
464 clear_accum_buffer(ctx,
465 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
466 }
467
468 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
469 /* clearing combined depth + stencil */
470 clear_depth_stencil_buffer(ctx, depthRb);
471 }
472 else {
473 /* separate depth/stencil clears */
474 if (mask & BUFFER_BIT_DEPTH) {
475 clear_depth_buffer(ctx, depthRb);
476 }
477 if (mask & BUFFER_BIT_STENCIL) {
478 clear_stencil_buffer(ctx, stencilRb);
479 }
480 }
481 }
482
483
484 void st_init_clear_functions(struct dd_function_table *functions)
485 {
486 functions->Clear = st_clear;
487 }