Set viewport state so viewport matches window dims.
[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 = stfp->Base.Base.InputsRead;
352 fs.tokens = &stfp->tokens[0];
353 fs.constants = NULL;
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 vs.constants = NULL;
369 pipe->set_vs_state(pipe, &vs);
370 }
371
372 /* viewport state: viewport matching window dims */
373 {
374 const float width = ctx->DrawBuffer->Width;
375 const float height = ctx->DrawBuffer->Height;
376 struct pipe_viewport_state vp;
377 vp.scale[0] = 0.5 * width;
378 vp.scale[1] = -0.5 * height;
379 vp.scale[2] = 0.5;
380 vp.scale[3] = 1.0;
381 vp.translate[0] = 0.5 * width;
382 vp.translate[1] = 0.5 * height;
383 vp.translate[2] = 0.5;
384 vp.translate[3] = 0.0;
385 pipe->set_viewport_state(pipe, &vp);
386 }
387
388 /* draw quad matching scissor rect (XXX verify coord round-off) */
389 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
390
391 /* Restore pipe state */
392 pipe->set_alpha_test_state(pipe, &st->state.alpha_test);
393 pipe->set_blend_state(pipe, &st->state.blend);
394 pipe->set_depth_state(pipe, &st->state.depth);
395 pipe->set_fs_state(pipe, &st->state.fs);
396 pipe->set_vs_state(pipe, &st->state.vs);
397 pipe->set_setup_state(pipe, &st->state.setup);
398 pipe->set_stencil_state(pipe, &st->state.stencil);
399 pipe->set_viewport_state(pipe, &ctx->st->state.viewport);
400 /* OR:
401 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
402 */
403 }
404
405
406 static void
407 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
408 {
409 struct st_renderbuffer *strb = st_renderbuffer(rb);
410
411 if (ctx->Color.ColorMask[0] &&
412 ctx->Color.ColorMask[1] &&
413 ctx->Color.ColorMask[2] &&
414 ctx->Color.ColorMask[3] &&
415 !ctx->Scissor.Enabled)
416 {
417 /* clear whole buffer w/out masking */
418 GLuint clearValue
419 = color_value(strb->surface->format, ctx->Color.ClearColor);
420 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
421 }
422 else {
423 /* masking or scissoring */
424 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
425 }
426 }
427
428
429 static void
430 clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
431 {
432 struct st_renderbuffer *strb = st_renderbuffer(rb);
433
434 if (!ctx->Scissor.Enabled) {
435 /* clear whole buffer w/out masking */
436 GLuint clearValue
437 = color_value(strb->surface->format, ctx->Accum.ClearColor);
438 /* Note that clearValue is 32 bits but the accum buffer will
439 * typically be 64bpp...
440 */
441 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
442 }
443 else {
444 /* scissoring */
445 /* XXX point framebuffer.cbufs[0] at the accum buffer */
446 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
447 }
448 }
449
450
451 static void
452 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
453 {
454 struct st_renderbuffer *strb = st_renderbuffer(rb);
455 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
456
457 assert(strb->surface->format);
458
459 if (ctx->Scissor.Enabled ||
460 (isDS && ctx->DrawBuffer->Visual.stencilBits > 0)) {
461 /* scissoring or we have a combined depth/stencil buffer */
462 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
463 }
464 else {
465 /* simple clear of whole buffer */
466 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
467 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
468 }
469 }
470
471
472 static void
473 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
474 {
475 struct st_renderbuffer *strb = st_renderbuffer(rb);
476 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
477 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
478 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
479
480 if (maskStencil ||
481 ctx->Scissor.Enabled ||
482 (isDS && ctx->DrawBuffer->Visual.depthBits > 0)) {
483 /* masking or scissoring or combined depth/stencil buffer */
484 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
485 }
486 else {
487 /* simple clear of whole buffer */
488 GLuint clearValue = ctx->Stencil.Clear;
489 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
490 }
491 }
492
493
494 static void
495 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
496 {
497 struct st_renderbuffer *strb = st_renderbuffer(rb);
498 const GLuint stencilMax = 1 << rb->StencilBits;
499 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
500
501 assert(is_depth_stencil_format(strb->surface->format));
502
503 if (!maskStencil && !ctx->Scissor.Enabled) {
504 /* clear whole buffer w/out masking */
505 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
506
507 switch (strb->surface->format) {
508 case PIPE_FORMAT_S8_Z24:
509 clearValue |= ctx->Stencil.Clear << 24;
510 break;
511 #if 0
512 case PIPE_FORMAT_Z24_S8:
513 clearValue = (clearValue << 8) | clearVal;
514 break;
515 #endif
516 default:
517 assert(0);
518 }
519
520 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
521 }
522 else {
523 /* masking or scissoring */
524 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
525 }
526 }
527
528
529
530 /**
531 * Called via ctx->Driver.Clear()
532 * XXX: doesn't pick up the differences between front/back/left/right
533 * clears. Need to sort that out...
534 */
535 static void st_clear(GLcontext *ctx, GLbitfield mask)
536 {
537 static const GLbitfield BUFFER_BITS_DS
538 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
539 struct st_context *st = ctx->st;
540 struct gl_renderbuffer *depthRb
541 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
542 struct gl_renderbuffer *stencilRb
543 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
544
545 /* This makes sure the softpipe has the latest scissor, etc values */
546 st_validate_state( st );
547
548 /*
549 * XXX TO-DO:
550 * If we're going to use clear_with_quad() for any reason, use it to
551 * clear as many other buffers as possible.
552 * As it is now, we sometimes call clear_with_quad() three times to clear
553 * color/depth/stencil individually...
554 */
555
556 if (mask & BUFFER_BITS_COLOR) {
557 GLuint b;
558 for (b = 0; b < BUFFER_COUNT; b++) {
559 if (BUFFER_BITS_COLOR & mask & (1 << b)) {
560 struct gl_renderbuffer *rb
561 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
562 assert(rb);
563 clear_color_buffer(ctx, rb);
564 }
565 }
566 }
567
568 if (mask & BUFFER_BIT_ACCUM) {
569 clear_accum_buffer(ctx,
570 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
571 }
572
573 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
574 /* clearing combined depth + stencil */
575 clear_depth_stencil_buffer(ctx, depthRb);
576 }
577 else {
578 /* separate depth/stencil clears */
579 if (mask & BUFFER_BIT_DEPTH) {
580 clear_depth_buffer(ctx, depthRb);
581 }
582 if (mask & BUFFER_BIT_STENCIL) {
583 clear_stencil_buffer(ctx, stencilRb);
584 }
585 }
586 }
587
588
589 void st_init_clear_functions(struct dd_function_table *functions)
590 {
591 functions->Clear = st_clear;
592 }