gallium: implement CSO save/restore functions for use by meta operations (blit, gen...
[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 cso_save_blend(st->cso_context);
251 cso_save_depth_stencil_alpha(st->cso_context);
252 cso_save_rasterizer(st->cso_context);
253 cso_save_viewport(st->cso_context);
254
255 /* blend state: RGBA masking */
256 {
257 struct pipe_blend_state blend;
258 memset(&blend, 0, sizeof(blend));
259 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
260 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
261 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
262 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
263 if (color) {
264 if (ctx->Color.ColorMask[0])
265 blend.colormask |= PIPE_MASK_R;
266 if (ctx->Color.ColorMask[1])
267 blend.colormask |= PIPE_MASK_G;
268 if (ctx->Color.ColorMask[2])
269 blend.colormask |= PIPE_MASK_B;
270 if (ctx->Color.ColorMask[3])
271 blend.colormask |= PIPE_MASK_A;
272 if (st->ctx->Color.DitherFlag)
273 blend.dither = 1;
274 }
275 cso_set_blend(st->cso_context, &blend);
276 }
277
278 /* depth_stencil state: always pass/set to ref value */
279 {
280 struct pipe_depth_stencil_alpha_state depth_stencil;
281 memset(&depth_stencil, 0, sizeof(depth_stencil));
282 if (depth) {
283 depth_stencil.depth.enabled = 1;
284 depth_stencil.depth.writemask = 1;
285 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
286 }
287
288 if (stencil) {
289 depth_stencil.stencil[0].enabled = 1;
290 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
291 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
292 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
293 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
294 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
295 depth_stencil.stencil[0].value_mask = 0xff;
296 depth_stencil.stencil[0].write_mask = ctx->Stencil.WriteMask[0] & 0xff;
297 }
298
299 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
300 }
301
302 /* rasterizer state: nothing */
303 {
304 struct pipe_rasterizer_state raster;
305 memset(&raster, 0, sizeof(raster));
306 #if TEST_DRAW_PASSTHROUGH
307 raster.bypass_clipping = 1;
308 raster.bypass_vs = 1;
309 #endif
310 cso_set_rasterizer(st->cso_context, &raster);
311 }
312
313 /* fragment shader state: color pass-through program */
314 {
315 static struct st_fragment_program *stfp = NULL;
316 if (!stfp) {
317 stfp = make_frag_shader(st);
318 }
319 pipe->bind_fs_state(pipe, stfp->driver_shader);
320 }
321
322 #if !TEST_DRAW_PASSTHROUGH
323 /* vertex shader state: color/position pass-through */
324 {
325 static struct st_vertex_program *stvp = NULL;
326 if (!stvp) {
327 stvp = make_vertex_shader(st);
328 }
329 pipe->bind_vs_state(pipe, stvp->driver_shader);
330 }
331 #endif
332
333 #if !TEST_DRAW_PASSTHROUGH
334 /* viewport state: viewport matching window dims */
335 {
336 const float width = ctx->DrawBuffer->Width;
337 const float height = ctx->DrawBuffer->Height;
338 struct pipe_viewport_state vp;
339 vp.scale[0] = 0.5 * width;
340 vp.scale[1] = -0.5 * height;
341 vp.scale[2] = 1.0;
342 vp.scale[3] = 1.0;
343 vp.translate[0] = 0.5 * width;
344 vp.translate[1] = 0.5 * height;
345 vp.translate[2] = 0.0;
346 vp.translate[3] = 0.0;
347 cso_set_viewport(st->cso_context, &vp);
348 }
349 #endif
350
351 /* draw quad matching scissor rect (XXX verify coord round-off) */
352 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
353
354 /* Restore pipe state */
355 cso_restore_blend(st->cso_context);
356 cso_restore_depth_stencil_alpha(st->cso_context);
357 cso_restore_rasterizer(st->cso_context);
358 cso_restore_viewport(st->cso_context);
359 /* these don't go through cso yet */
360 pipe->bind_fs_state(pipe, st->fp->driver_shader);
361 pipe->bind_vs_state(pipe, st->vp->driver_shader);
362 }
363
364
365 /**
366 * Determine if we need to clear the depth buffer by drawing a quad.
367 */
368 static INLINE GLboolean
369 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
370 {
371 const struct st_renderbuffer *strb = st_renderbuffer(rb);
372
373 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
374 return FALSE;
375
376 if (ctx->Scissor.Enabled)
377 return TRUE;
378
379 if (!ctx->Color.ColorMask[0] ||
380 !ctx->Color.ColorMask[1] ||
381 !ctx->Color.ColorMask[2] ||
382 !ctx->Color.ColorMask[3])
383 return TRUE;
384
385 return FALSE;
386 }
387
388
389 static INLINE GLboolean
390 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
391 {
392 const struct st_renderbuffer *strb = st_renderbuffer(rb);
393 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
394 GLboolean maskStencil
395 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
396
397 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
398 return FALSE;
399
400 if (ctx->Scissor.Enabled)
401 return TRUE;
402
403 if (maskStencil)
404 return TRUE;
405
406 return FALSE;
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
419 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
420 return FALSE;
421
422 if (ctx->Scissor.Enabled)
423 return TRUE;
424
425 if (isDS &&
426 strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
427 ctx->DrawBuffer->Visual.stencilBits > 0)
428 return TRUE;
429
430 return FALSE;
431 }
432
433
434 /**
435 * Determine if we need to clear the stencil buffer by drawing a quad.
436 */
437 static INLINE GLboolean
438 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
439 {
440 const struct st_renderbuffer *strb = st_renderbuffer(rb);
441 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
442 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
443 const GLboolean maskStencil
444 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
445
446 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
447 return FALSE;
448
449 if (maskStencil)
450 return TRUE;
451
452 if (ctx->Scissor.Enabled)
453 return TRUE;
454
455 /* This is correct, but it is necessary to look at the depth clear
456 * value held in the surface when it comes time to issue the clear,
457 * rather than taking depth and stencil clear values from the
458 * current state.
459 */
460 if (isDS &&
461 strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
462 ctx->DrawBuffer->Visual.depthBits > 0)
463 return TRUE;
464
465 return FALSE;
466 }
467
468
469
470 static void
471 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
472 {
473 if (check_clear_color_with_quad( ctx, rb )) {
474 /* masking or scissoring */
475 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
476 }
477 else {
478 /* clear whole buffer w/out masking */
479 struct st_renderbuffer *strb = st_renderbuffer(rb);
480 uint clearValue;
481 util_pack_color(ctx->Color.ClearColor, strb->surface->format, &clearValue);
482 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
483 }
484 }
485
486
487 static void
488 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
489 {
490 if (check_clear_depth_with_quad(ctx, rb)) {
491 /* scissoring or we have a combined depth/stencil buffer */
492 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
493 }
494 else {
495 struct st_renderbuffer *strb = st_renderbuffer(rb);
496
497 /* simple clear of whole buffer */
498 uint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
499 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
500 }
501 }
502
503
504 static void
505 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
506 {
507 if (check_clear_stencil_with_quad(ctx, rb)) {
508 /* masking or scissoring or combined depth/stencil buffer */
509 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
510 }
511 else {
512 struct st_renderbuffer *strb = st_renderbuffer(rb);
513
514 /* simple clear of whole buffer */
515 GLuint clearValue = ctx->Stencil.Clear;
516
517 switch (strb->surface->format) {
518 case PIPE_FORMAT_S8Z24_UNORM:
519 clearValue <<= 24;
520 break;
521 default:
522 ; /* no-op, stencil value is in least significant bits */
523 }
524
525 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
526 }
527 }
528
529
530 static void
531 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
532 {
533
534 if (check_clear_depth_stencil_with_quad(ctx, rb)) {
535 /* masking or scissoring */
536 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
537 }
538 else {
539 struct st_renderbuffer *strb = st_renderbuffer(rb);
540
541 /* clear whole buffer w/out masking */
542 GLuint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
543
544 switch (strb->surface->format) {
545 case PIPE_FORMAT_S8Z24_UNORM:
546 clearValue |= ctx->Stencil.Clear << 24;
547 break;
548 case PIPE_FORMAT_Z24S8_UNORM:
549 clearValue |= ctx->Stencil.Clear;
550 break;
551 default:
552 assert(0);
553 }
554
555 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
556 }
557 }
558
559
560
561 /**
562 * Called via ctx->Driver.Clear()
563 * XXX: doesn't pick up the differences between front/back/left/right
564 * clears. Need to sort that out...
565 */
566 static void st_clear(GLcontext *ctx, GLbitfield mask)
567 {
568 static const GLbitfield BUFFER_BITS_DS
569 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
570 struct st_context *st = ctx->st;
571 struct gl_renderbuffer *depthRb
572 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
573 struct gl_renderbuffer *stencilRb
574 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
575 GLbitfield cmask = mask & BUFFER_BITS_COLOR;
576
577 /* This makes sure the softpipe has the latest scissor, etc values */
578 st_validate_state( st );
579
580 /*
581 * XXX TO-DO:
582 * If we're going to use clear_with_quad() for any reason, use it to
583 * clear as many other buffers as possible.
584 * As it is now, we sometimes call clear_with_quad() three times to clear
585 * color/depth/stencil individually...
586 */
587
588 if (cmask) {
589 GLuint b;
590 for (b = 0; cmask; b++) {
591 if (cmask & (1 << b)) {
592 struct gl_renderbuffer *rb
593 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
594 assert(rb);
595 clear_color_buffer(ctx, rb);
596 cmask &= ~(1 << b); /* turn off bit */
597 }
598 assert(b < BUFFER_COUNT);
599 }
600 }
601
602 if (mask & BUFFER_BIT_ACCUM) {
603 st_clear_accum_buffer(ctx,
604 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
605 }
606
607 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
608 /* clearing combined depth + stencil */
609 clear_depth_stencil_buffer(ctx, depthRb);
610 }
611 else {
612 /* separate depth/stencil clears */
613 if (mask & BUFFER_BIT_DEPTH) {
614 clear_depth_buffer(ctx, depthRb);
615 }
616 if (mask & BUFFER_BIT_STENCIL) {
617 clear_stencil_buffer(ctx, stencilRb);
618 }
619 }
620 }
621
622
623 void st_init_clear_functions(struct dd_function_table *functions)
624 {
625 functions->Clear = st_clear;
626 }