Fix viewport Z scale/bias to get the right Z values from drawing the quad.
[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_cache.h"
39 #include "st_context.h"
40 #include "st_cb_accum.h"
41 #include "st_cb_clear.h"
42 #include "st_cb_fbo.h"
43 #include "st_draw.h"
44 #include "st_program.h"
45 #include "st_public.h"
46
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
52 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
53
54 #include "vf/vf.h"
55
56
57
58
59 static GLuint
60 color_value(GLuint pipeFormat, const GLfloat color[4])
61 {
62 GLubyte r, g, b, a;
63
64 UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
65 UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
66 UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
67 UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);
68
69 switch (pipeFormat) {
70 case PIPE_FORMAT_U_R8_G8_B8_A8:
71 return (r << 24) | (g << 16) | (b << 8) | a;
72 case PIPE_FORMAT_U_A8_R8_G8_B8:
73 return (a << 24) | (r << 16) | (g << 8) | b;
74 case PIPE_FORMAT_U_R5_G6_B5:
75 return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
76 default:
77 return 0;
78 }
79 }
80
81
82 static GLuint
83 depth_value(GLuint pipeFormat, GLfloat value)
84 {
85 GLuint val;
86 switch (pipeFormat) {
87 case PIPE_FORMAT_U_Z16:
88 val = (GLuint) (value * 0xffffff);
89 break;
90 case PIPE_FORMAT_U_Z32:
91 val = (GLuint) (value * 0xffffffff);
92 break;
93 case PIPE_FORMAT_S8_Z24:
94 /*case PIPE_FORMAT_Z24_S8:*/
95 val = (GLuint) (value * 0xffffff);
96 break;
97 default:
98 val = 0;
99 assert(0);
100 }
101 return val;
102 }
103
104
105 static GLboolean
106 is_depth_stencil_format(GLuint pipeFormat)
107 {
108 switch (pipeFormat) {
109 case PIPE_FORMAT_S8_Z24:
110 /*case PIPE_FORMAT_Z24_S8:*/
111 return GL_TRUE;
112 default:
113 return GL_FALSE;
114 }
115 }
116
117
118
119 /**
120 * Create a simple fragment shader that just passes through the fragment color.
121 */
122 static struct st_fragment_program *
123 make_frag_shader(struct st_context *st)
124 {
125 GLcontext *ctx = st->ctx;
126 struct st_fragment_program *stfp;
127 struct gl_program *p;
128 GLuint interpMode[16];
129 GLuint i;
130
131 /* XXX temporary */
132 for (i = 0; i < 16; i++)
133 interpMode[i] = TGSI_INTERPOLATE_LINEAR;
134
135 p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
136 if (!p)
137 return NULL;
138
139 p->NumInstructions = 2;
140 p->Instructions = _mesa_alloc_instructions(2);
141 if (!p->Instructions) {
142 ctx->Driver.DeleteProgram(ctx, p);
143 return NULL;
144 }
145 _mesa_init_instructions(p->Instructions, 2);
146 /* MOV result.color, fragment.color; */
147 p->Instructions[0].Opcode = OPCODE_MOV;
148 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
149 p->Instructions[0].DstReg.Index = FRAG_RESULT_COLR;
150 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
151 p->Instructions[0].SrcReg[0].Index = FRAG_ATTRIB_COL0;
152 /* END; */
153 p->Instructions[1].Opcode = OPCODE_END;
154
155 p->InputsRead = FRAG_BIT_COL0;
156 p->OutputsWritten = (1 << FRAG_RESULT_COLR);
157
158 stfp = (struct st_fragment_program *) p;
159 st_translate_fragment_program(st, stfp, NULL,
160 stfp->tokens, ST_MAX_SHADER_TOKENS);
161
162 return stfp;
163 }
164
165
166 /**
167 * Create a simple vertex shader that just passes through the
168 * vertex position and color.
169 */
170 static struct st_vertex_program *
171 make_vertex_shader(struct st_context *st)
172 {
173 GLcontext *ctx = st->ctx;
174 struct st_vertex_program *stvp;
175 struct gl_program *p;
176
177 p = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
178 if (!p)
179 return NULL;
180
181 p->NumInstructions = 3;
182 p->Instructions = _mesa_alloc_instructions(3);
183 if (!p->Instructions) {
184 ctx->Driver.DeleteProgram(ctx, p);
185 return NULL;
186 }
187 _mesa_init_instructions(p->Instructions, 3);
188 /* MOV result.pos, vertex.pos; */
189 p->Instructions[0].Opcode = OPCODE_MOV;
190 p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
191 p->Instructions[0].DstReg.Index = VERT_RESULT_HPOS;
192 p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
193 p->Instructions[0].SrcReg[0].Index = VERT_ATTRIB_POS;
194 /* MOV result.color, vertex.color; */
195 p->Instructions[1].Opcode = OPCODE_MOV;
196 p->Instructions[1].DstReg.File = PROGRAM_OUTPUT;
197 p->Instructions[1].DstReg.Index = VERT_RESULT_COL0;
198 p->Instructions[1].SrcReg[0].File = PROGRAM_INPUT;
199 p->Instructions[1].SrcReg[0].Index = VERT_ATTRIB_COLOR0;
200 /* END; */
201 p->Instructions[2].Opcode = OPCODE_END;
202
203 p->InputsRead = VERT_BIT_POS | VERT_BIT_COLOR0;
204 p->OutputsWritten = ((1 << VERT_RESULT_COL0) |
205 (1 << VERT_RESULT_HPOS));
206
207 stvp = (struct st_vertex_program *) p;
208 st_translate_vertex_program(st, stvp, NULL,
209 stvp->tokens, ST_MAX_SHADER_TOKENS);
210 assert(stvp->vs);
211
212 return stvp;
213 }
214
215
216
217 /**
218 * Draw a screen-aligned quadrilateral.
219 * Coords are window coords with y=0=bottom. These coords will be transformed
220 * by the vertex shader and viewport transform (which will flip Y if needed).
221 */
222 static void
223 draw_quad(GLcontext *ctx,
224 float x0, float y0, float x1, float y1, GLfloat z,
225 const GLfloat color[4])
226 {
227 GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
228 GLuint i;
229
230 /* positions */
231 verts[0][0][0] = x0;
232 verts[0][0][1] = y0;
233
234 verts[1][0][0] = x1;
235 verts[1][0][1] = y0;
236
237 verts[2][0][0] = x1;
238 verts[2][0][1] = y1;
239
240 verts[3][0][0] = x0;
241 verts[3][0][1] = y1;
242
243 /* same for all verts: */
244 for (i = 0; i < 4; i++) {
245 verts[i][0][2] = z;
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];
251 }
252
253 st_draw_vertices(ctx, PIPE_PRIM_QUADS, 4, (float *) verts, 2);
254 }
255
256
257
258 /**
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.
262 */
263 static void
264 clear_with_quad(GLcontext *ctx,
265 GLboolean color, GLboolean depth, GLboolean stencil)
266 {
267 struct st_context *st = ctx->st;
268 struct pipe_context *pipe = 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;
273
274 /* alpha state: disabled */
275 {
276 struct pipe_alpha_test_state alpha_test;
277 const struct cso_alpha_test *cso;
278 memset(&alpha_test, 0, sizeof(alpha_test));
279 cso = st_cached_alpha_test_state(st, &alpha_test);
280 pipe->bind_alpha_test_state(pipe, cso->data);
281 }
282
283 /* blend state: RGBA masking */
284 {
285 struct pipe_blend_state blend;
286 const struct cso_blend *cso;
287 memset(&blend, 0, sizeof(blend));
288 if (color) {
289 if (ctx->Color.ColorMask[0])
290 blend.colormask |= PIPE_MASK_R;
291 if (ctx->Color.ColorMask[1])
292 blend.colormask |= PIPE_MASK_G;
293 if (ctx->Color.ColorMask[2])
294 blend.colormask |= PIPE_MASK_B;
295 if (ctx->Color.ColorMask[3])
296 blend.colormask |= PIPE_MASK_A;
297 if (st->ctx->Color.DitherFlag)
298 blend.dither = 1;
299 }
300 cso = st_cached_blend_state(st, &blend);
301 pipe->bind_blend_state(pipe, cso->data);
302 }
303
304 /* depth_stencil state: always pass/set to ref value */
305 {
306 struct pipe_depth_stencil_state depth_stencil;
307 const struct cso_depth_stencil *cso;
308 memset(&depth_stencil, 0, sizeof(depth_stencil));
309 if (depth) {
310 depth_stencil.depth.enabled = 1;
311 depth_stencil.depth.writemask = 1;
312 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
313 }
314
315 if (stencil) {
316 depth_stencil.stencil.front_enabled = 1;
317 depth_stencil.stencil.front_func = PIPE_FUNC_ALWAYS;
318 depth_stencil.stencil.front_fail_op = PIPE_STENCIL_OP_REPLACE;
319 depth_stencil.stencil.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
320 depth_stencil.stencil.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
321 depth_stencil.stencil.ref_value[0] = ctx->Stencil.Clear;
322 depth_stencil.stencil.value_mask[0] = 0xff;
323 depth_stencil.stencil.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
324 }
325 cso = st_cached_depth_stencil_state(st, &depth_stencil);
326 pipe->bind_depth_stencil_state(pipe, cso->data);
327 }
328
329 /* rasterizer state: nothing */
330 {
331 struct pipe_rasterizer_state raster;
332 const struct cso_rasterizer *cso;
333 memset(&raster, 0, sizeof(raster));
334 #if 0
335 /* don't do per-pixel scissor; we'll just draw a PIPE_PRIM_QUAD
336 * that matches the scissor bounds.
337 */
338 if (ctx->Scissor.Enabled)
339 raster.scissor = 1;
340 #endif
341 cso = st_cached_rasterizer_state(st, &raster);
342 pipe->bind_rasterizer_state(pipe, cso->data);
343 }
344
345 /* fragment shader state: color pass-through program */
346 {
347 static struct st_fragment_program *stfp = NULL;
348 if (!stfp) {
349 stfp = make_frag_shader(st);
350 }
351 pipe->bind_fs_state(pipe, stfp->fs->data);
352 }
353
354 /* vertex shader state: color/position pass-through */
355 {
356 static struct st_vertex_program *stvp = NULL;
357 if (!stvp) {
358 stvp = make_vertex_shader(st);
359 }
360 pipe->bind_vs_state(pipe, stvp->vs->data);
361 }
362
363 /* viewport state: viewport matching window dims */
364 {
365 const float width = ctx->DrawBuffer->Width;
366 const float height = ctx->DrawBuffer->Height;
367 struct pipe_viewport_state vp;
368 vp.scale[0] = 0.5 * width;
369 vp.scale[1] = -0.5 * height;
370 vp.scale[2] = 1.0;
371 vp.scale[3] = 1.0;
372 vp.translate[0] = 0.5 * width;
373 vp.translate[1] = 0.5 * height;
374 vp.translate[2] = 0.0;
375 vp.translate[3] = 0.0;
376 pipe->set_viewport_state(pipe, &vp);
377 }
378
379 /* draw quad matching scissor rect (XXX verify coord round-off) */
380 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
381
382 /* Restore pipe state */
383 pipe->bind_alpha_test_state(pipe, st->state.alpha_test->data);
384 pipe->bind_blend_state(pipe, st->state.blend->data);
385 pipe->bind_depth_stencil_state(pipe, st->state.depth_stencil->data);
386 pipe->bind_fs_state(pipe, st->state.fs->data);
387 pipe->bind_vs_state(pipe, st->state.vs->data);
388 pipe->bind_rasterizer_state(pipe, st->state.rasterizer->data);
389 pipe->set_viewport_state(pipe, &st->state.viewport);
390 /* OR:
391 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
392 */
393 }
394
395
396 /**
397 * Determine if we need to clear the depth buffer by drawing a quad.
398 */
399 static INLINE GLboolean
400 check_clear_color_with_quad(GLcontext *ctx)
401 {
402 return !(ctx->Color.ColorMask[0] &&
403 ctx->Color.ColorMask[1] &&
404 ctx->Color.ColorMask[2] &&
405 ctx->Color.ColorMask[3] &&
406 !ctx->Scissor.Enabled);
407 }
408
409
410 /**
411 * Determine if we need to clear the depth buffer by drawing a quad.
412 */
413 static INLINE GLboolean
414 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
415 {
416 const struct st_renderbuffer *strb = st_renderbuffer(rb);
417 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
418 return ctx->Scissor.Enabled
419 || (isDS && ctx->DrawBuffer->Visual.stencilBits > 0);
420 }
421
422
423 /**
424 * Determine if we need to clear the stencil buffer by drawing a quad.
425 */
426 static INLINE GLboolean
427 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
428 {
429 const struct st_renderbuffer *strb = st_renderbuffer(rb);
430 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
431 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
432 const GLboolean maskStencil
433 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
434 return maskStencil
435 || ctx->Scissor.Enabled
436 || (isDS && ctx->DrawBuffer->Visual.depthBits > 0);
437 }
438
439
440
441
442 static void
443 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
444 {
445 struct st_renderbuffer *strb = st_renderbuffer(rb);
446
447 if (ctx->Color.ColorMask[0] &&
448 ctx->Color.ColorMask[1] &&
449 ctx->Color.ColorMask[2] &&
450 ctx->Color.ColorMask[3] &&
451 !ctx->Scissor.Enabled)
452 {
453 /* clear whole buffer w/out masking */
454 GLuint clearValue
455 = color_value(strb->surface->format, ctx->Color.ClearColor);
456 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
457 }
458 else {
459 /* masking or scissoring */
460 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
461 }
462 }
463
464
465 static void
466 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
467 {
468 struct st_renderbuffer *strb = st_renderbuffer(rb);
469 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
470
471 assert(strb->surface->format);
472
473 if (check_clear_depth_with_quad(ctx, rb)) {
474 /* scissoring or we have a combined depth/stencil buffer */
475 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
476 }
477 else {
478 /* simple clear of whole buffer */
479 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
480 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
481 }
482 }
483
484
485 static void
486 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
487 {
488 struct st_renderbuffer *strb = st_renderbuffer(rb);
489 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
490 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
491 GLboolean maskStencil
492 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
493
494 if (maskStencil ||
495 ctx->Scissor.Enabled ||
496 (isDS && ctx->DrawBuffer->Visual.depthBits > 0)) {
497 /* masking or scissoring or combined depth/stencil buffer */
498 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
499 }
500 else {
501 /* simple clear of whole buffer */
502 GLuint clearValue = ctx->Stencil.Clear;
503 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
504 }
505 }
506
507
508 static void
509 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
510 {
511 struct st_renderbuffer *strb = st_renderbuffer(rb);
512 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
513 GLboolean maskStencil
514 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
515
516 assert(is_depth_stencil_format(strb->surface->format));
517
518 if (!maskStencil && !ctx->Scissor.Enabled) {
519 /* clear whole buffer w/out masking */
520 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
521
522 switch (strb->surface->format) {
523 case PIPE_FORMAT_S8_Z24:
524 clearValue |= ctx->Stencil.Clear << 24;
525 break;
526 #if 0
527 case PIPE_FORMAT_Z24_S8:
528 clearValue = (clearValue << 8) | clearVal;
529 break;
530 #endif
531 default:
532 assert(0);
533 }
534
535 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
536 }
537 else {
538 /* masking or scissoring */
539 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
540 }
541 }
542
543
544
545 /**
546 * Called via ctx->Driver.Clear()
547 * XXX: doesn't pick up the differences between front/back/left/right
548 * clears. Need to sort that out...
549 */
550 static void st_clear(GLcontext *ctx, GLbitfield mask)
551 {
552 static const GLbitfield BUFFER_BITS_DS
553 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
554 struct st_context *st = ctx->st;
555 struct gl_renderbuffer *depthRb
556 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
557 struct gl_renderbuffer *stencilRb
558 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
559 GLbitfield cmask = mask & BUFFER_BITS_COLOR;
560
561 /* This makes sure the softpipe has the latest scissor, etc values */
562 st_validate_state( st );
563
564 /*
565 * XXX TO-DO:
566 * If we're going to use clear_with_quad() for any reason, use it to
567 * clear as many other buffers as possible.
568 * As it is now, we sometimes call clear_with_quad() three times to clear
569 * color/depth/stencil individually...
570 */
571
572 if (cmask) {
573 GLuint b;
574 for (b = 0; cmask; b++) {
575 if (cmask & (1 << b)) {
576 struct gl_renderbuffer *rb
577 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
578 assert(rb);
579 clear_color_buffer(ctx, rb);
580 cmask &= ~(1 << b); /* turn off bit */
581 }
582 assert(b < BUFFER_COUNT);
583 }
584 }
585
586 if (mask & BUFFER_BIT_ACCUM) {
587 st_clear_accum_buffer(ctx,
588 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
589 }
590
591 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
592 /* clearing combined depth + stencil */
593 clear_depth_stencil_buffer(ctx, depthRb);
594 }
595 else {
596 /* separate depth/stencil clears */
597 if (mask & BUFFER_BIT_DEPTH) {
598 clear_depth_buffer(ctx, depthRb);
599 }
600 if (mask & BUFFER_BIT_STENCIL) {
601 clear_stencil_buffer(ctx, stencilRb);
602 }
603 }
604 }
605
606
607 void st_init_clear_functions(struct dd_function_table *functions)
608 {
609 functions->Clear = st_clear;
610 }