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