Start to remove the temporary draw_vb() and draw_vertices() code.
[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.
216 * XXX need to emit clip coords when using a vertex program!
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 const GLfloat x0 = ctx->DrawBuffer->_Xmin;
269 const GLfloat y0 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax;
270 const GLfloat x1 = ctx->DrawBuffer->_Xmax;
271 const GLfloat y1 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin;
272
273 /* alpha state: disabled */
274 {
275 struct pipe_alpha_test_state alpha_test;
276 memset(&alpha_test, 0, sizeof(alpha_test));
277 st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
278 }
279
280 /* blend state: RGBA masking */
281 {
282 struct pipe_blend_state blend;
283 memset(&blend, 0, sizeof(blend));
284 if (color) {
285 if (ctx->Color.ColorMask[0])
286 blend.colormask |= PIPE_MASK_R;
287 if (ctx->Color.ColorMask[1])
288 blend.colormask |= PIPE_MASK_G;
289 if (ctx->Color.ColorMask[2])
290 blend.colormask |= PIPE_MASK_B;
291 if (ctx->Color.ColorMask[3])
292 blend.colormask |= PIPE_MASK_A;
293 if (st->ctx->Color.DitherFlag)
294 blend.dither = 1;
295 }
296 st->pipe->set_blend_state(st->pipe, &blend);
297 }
298
299 /* depth state: always pass */
300 {
301 struct pipe_depth_state depth_test;
302 memset(&depth_test, 0, sizeof(depth_test));
303 if (depth) {
304 depth_test.enabled = 1;
305 depth_test.writemask = 1;
306 depth_test.func = PIPE_FUNC_ALWAYS;
307 }
308 st->pipe->set_depth_state(st->pipe, &depth_test);
309 }
310
311 /* setup state: nothing */
312 {
313 struct pipe_setup_state setup;
314 memset(&setup, 0, sizeof(setup));
315 if (ctx->Scissor.Enabled)
316 setup.scissor = 1;
317 st->pipe->set_setup_state(st->pipe, &setup);
318 }
319
320 /* stencil state: always set to ref value */
321 {
322 struct pipe_stencil_state stencil_test;
323 memset(&stencil_test, 0, sizeof(stencil_test));
324 if (stencil) {
325 stencil_test.front_enabled = 1;
326 stencil_test.front_func = PIPE_FUNC_ALWAYS;
327 stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
328 stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
329 stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
330 stencil_test.ref_value[0] = ctx->Stencil.Clear;
331 stencil_test.value_mask[0] = 0xff;
332 stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
333 }
334 st->pipe->set_stencil_state(st->pipe, &stencil_test);
335 }
336
337 /* fragment shader state: color pass-through program */
338 {
339 static struct st_fragment_program *stfp = NULL;
340 struct pipe_shader_state fs;
341 if (!stfp) {
342 stfp = make_color_shader(st);
343 }
344 memset(&fs, 0, sizeof(fs));
345 fs.inputs_read = stfp->Base.Base.InputsRead;
346 fs.tokens = &stfp->tokens[0];
347 fs.constants = NULL;
348 st->pipe->set_fs_state(st->pipe, &fs);
349 }
350
351 /* vertex shader state: color/position pass-through */
352 {
353 static struct st_vertex_program *stvp = NULL;
354 struct pipe_shader_state vs;
355 if (!stvp) {
356 stvp = make_vertex_shader(st);
357 }
358 memset(&vs, 0, sizeof(vs));
359 vs.inputs_read = stvp->Base.Base.InputsRead;
360 vs.outputs_written = stvp->Base.Base.OutputsWritten;
361 vs.tokens = &stvp->tokens[0];
362 vs.constants = NULL;
363 st->pipe->set_vs_state(st->pipe, &vs);
364 }
365
366 /* draw quad matching scissor rect (XXX verify coord round-off) */
367 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
368
369 /* Restore pipe state */
370 st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
371 st->pipe->set_blend_state(st->pipe, &st->state.blend);
372 st->pipe->set_depth_state(st->pipe, &st->state.depth);
373 st->pipe->set_fs_state(st->pipe, &st->state.fs);
374 st->pipe->set_vs_state(st->pipe, &st->state.vs);
375 st->pipe->set_setup_state(st->pipe, &st->state.setup);
376 st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
377 /* OR:
378 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
379 */
380 }
381
382
383 static void
384 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
385 {
386 struct st_renderbuffer *strb = st_renderbuffer(rb);
387
388 if (ctx->Color.ColorMask[0] &&
389 ctx->Color.ColorMask[1] &&
390 ctx->Color.ColorMask[2] &&
391 ctx->Color.ColorMask[3] &&
392 !ctx->Scissor.Enabled)
393 {
394 /* clear whole buffer w/out masking */
395 GLuint clearValue
396 = color_value(strb->surface->format, ctx->Color.ClearColor);
397 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
398 }
399 else {
400 /* masking or scissoring */
401 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
402 }
403 }
404
405
406 static void
407 clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
408 {
409 struct st_renderbuffer *strb = st_renderbuffer(rb);
410
411 if (!ctx->Scissor.Enabled) {
412 /* clear whole buffer w/out masking */
413 GLuint clearValue
414 = color_value(strb->surface->format, ctx->Accum.ClearColor);
415 /* Note that clearValue is 32 bits but the accum buffer will
416 * typically be 64bpp...
417 */
418 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
419 }
420 else {
421 /* scissoring */
422 /* XXX point framebuffer.cbufs[0] at the accum buffer */
423 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
424 }
425 }
426
427
428 static void
429 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
430 {
431 struct st_renderbuffer *strb = st_renderbuffer(rb);
432 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
433
434 assert(strb->surface->format);
435
436 if (ctx->Scissor.Enabled ||
437 (isDS && ctx->DrawBuffer->Visual.stencilBits > 0)) {
438 /* scissoring or we have a combined depth/stencil buffer */
439 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
440 }
441 else {
442 /* simple clear of whole buffer */
443 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
444 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
445 }
446 }
447
448
449 static void
450 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
451 {
452 struct st_renderbuffer *strb = st_renderbuffer(rb);
453 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
454 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
455 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
456
457 if (maskStencil ||
458 ctx->Scissor.Enabled ||
459 (isDS && ctx->DrawBuffer->Visual.depthBits > 0)) {
460 /* masking or scissoring or combined depth/stencil buffer */
461 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
462 }
463 else {
464 /* simple clear of whole buffer */
465 GLuint clearValue = ctx->Stencil.Clear;
466 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
467 }
468 }
469
470
471 static void
472 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
473 {
474 struct st_renderbuffer *strb = st_renderbuffer(rb);
475 const GLuint stencilMax = 1 << rb->StencilBits;
476 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
477
478 assert(is_depth_stencil_format(strb->surface->format));
479
480 if (!maskStencil && !ctx->Scissor.Enabled) {
481 /* clear whole buffer w/out masking */
482 GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
483
484 switch (strb->surface->format) {
485 case PIPE_FORMAT_S8_Z24:
486 clearValue |= ctx->Stencil.Clear << 24;
487 break;
488 #if 0
489 case PIPE_FORMAT_Z24_S8:
490 clearValue = (clearValue << 8) | clearVal;
491 break;
492 #endif
493 default:
494 assert(0);
495 }
496
497 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
498 }
499 else {
500 /* masking or scissoring */
501 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
502 }
503 }
504
505
506
507 /**
508 * Called via ctx->Driver.Clear()
509 * XXX: doesn't pick up the differences between front/back/left/right
510 * clears. Need to sort that out...
511 */
512 static void st_clear(GLcontext *ctx, GLbitfield mask)
513 {
514 static const GLbitfield BUFFER_BITS_DS
515 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
516 struct st_context *st = ctx->st;
517 struct gl_renderbuffer *depthRb
518 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
519 struct gl_renderbuffer *stencilRb
520 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
521
522 /* This makes sure the softpipe has the latest scissor, etc values */
523 st_validate_state( st );
524
525 /*
526 * XXX TO-DO:
527 * If we're going to use clear_with_quad() for any reason, use it to
528 * clear as many other buffers as possible.
529 * As it is now, we sometimes call clear_with_quad() three times to clear
530 * color/depth/stencil individually...
531 */
532
533 if (mask & BUFFER_BITS_COLOR) {
534 GLuint b;
535 for (b = 0; b < BUFFER_COUNT; b++) {
536 if (BUFFER_BITS_COLOR & mask & (1 << b)) {
537 struct gl_renderbuffer *rb
538 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
539 assert(rb);
540 clear_color_buffer(ctx, rb);
541 }
542 }
543 }
544
545 if (mask & BUFFER_BIT_ACCUM) {
546 clear_accum_buffer(ctx,
547 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
548 }
549
550 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
551 /* clearing combined depth + stencil */
552 clear_depth_stencil_buffer(ctx, depthRb);
553 }
554 else {
555 /* separate depth/stencil clears */
556 if (mask & BUFFER_BIT_DEPTH) {
557 clear_depth_buffer(ctx, depthRb);
558 }
559 if (mask & BUFFER_BIT_STENCIL) {
560 clear_stencil_buffer(ctx, stencilRb);
561 }
562 }
563 }
564
565
566 void st_init_clear_functions(struct dd_function_table *functions)
567 {
568 functions->Clear = st_clear;
569 }