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