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