Some changed for non-C99 compilers
[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_context.h"
38 #include "st_atom.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_inlines.h"
49 #include "pipe/p_state.h"
50 #include "pipe/p_defines.h"
51 #include "util/u_pack_color.h"
52 #include "util/u_simple_shaders.h"
53 #include "util/u_draw_quad.h"
54
55 #include "cso_cache/cso_context.h"
56
57
58 void
59 st_init_clear(struct st_context *st)
60 {
61 struct pipe_context *pipe = st->pipe;
62
63 /* rasterizer state: bypass clipping */
64 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
65 st->clear.raster.gl_rasterization_rules = 1;
66 st->clear.raster.bypass_clipping = 1;
67
68 /* viewport state: identity since we're drawing in window coords */
69 st->clear.viewport.scale[0] = 1.0;
70 st->clear.viewport.scale[1] = 1.0;
71 st->clear.viewport.scale[2] = 1.0;
72 st->clear.viewport.scale[3] = 1.0;
73 st->clear.viewport.translate[0] = 0.0;
74 st->clear.viewport.translate[1] = 0.0;
75 st->clear.viewport.translate[2] = 0.0;
76 st->clear.viewport.translate[3] = 0.0;
77
78 /* fragment shader state: color pass-through program */
79 st->clear.fs =
80 util_make_fragment_passthrough_shader(pipe, &st->clear.frag_shader);
81
82 /* vertex shader state: color/position pass-through */
83 {
84 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
85 TGSI_SEMANTIC_COLOR };
86 const uint semantic_indexes[] = { 0, 0 };
87 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
88 semantic_names,
89 semantic_indexes,
90 &st->clear.vert_shader);
91 }
92 }
93
94
95 void
96 st_destroy_clear(struct st_context *st)
97 {
98 struct pipe_context *pipe = st->pipe;
99
100 if (st->clear.fs) {
101 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
102 st->clear.fs = NULL;
103 }
104 if (st->clear.vs) {
105 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
106 st->clear.vs = NULL;
107 }
108 if (st->clear.vbuf) {
109 pipe_buffer_destroy(pipe, st->clear.vbuf);
110 st->clear.vbuf = NULL;
111 }
112 }
113
114
115 static GLboolean
116 is_depth_stencil_format(enum pipe_format pipeFormat)
117 {
118 switch (pipeFormat) {
119 case PIPE_FORMAT_S8Z24_UNORM:
120 case PIPE_FORMAT_Z24S8_UNORM:
121 return GL_TRUE;
122 default:
123 return GL_FALSE;
124 }
125 }
126
127
128
129 /**
130 * Draw a screen-aligned quadrilateral.
131 * Coords are window coords with y=0=bottom. These coords will be transformed
132 * by the vertex shader and viewport transform (which will flip Y if needed).
133 */
134 static void
135 draw_quad(GLcontext *ctx,
136 float x0, float y0, float x1, float y1, GLfloat z,
137 const GLfloat color[4])
138 {
139 struct st_context *st = ctx->st;
140 struct pipe_context *pipe = st->pipe;
141 GLuint i;
142 void *buf;
143
144 if (!st->clear.vbuf) {
145 st->clear.vbuf = pipe_buffer_create(pipe, 32, PIPE_BUFFER_USAGE_VERTEX,
146 sizeof(st->clear.vertices));
147 }
148
149 /* positions */
150 st->clear.vertices[0][0][0] = x0;
151 st->clear.vertices[0][0][1] = y0;
152
153 st->clear.vertices[1][0][0] = x1;
154 st->clear.vertices[1][0][1] = y0;
155
156 st->clear.vertices[2][0][0] = x1;
157 st->clear.vertices[2][0][1] = y1;
158
159 st->clear.vertices[3][0][0] = x0;
160 st->clear.vertices[3][0][1] = y1;
161
162 /* same for all verts: */
163 for (i = 0; i < 4; i++) {
164 st->clear.vertices[i][0][2] = z;
165 st->clear.vertices[i][0][3] = 1.0;
166 st->clear.vertices[i][1][0] = color[0];
167 st->clear.vertices[i][1][1] = color[1];
168 st->clear.vertices[i][1][2] = color[2];
169 st->clear.vertices[i][1][3] = color[3];
170 }
171
172 /* put vertex data into vbuf */
173 buf = pipe_buffer_map(pipe, st->clear.vbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
174 memcpy(buf, st->clear.vertices, sizeof(st->clear.vertices));
175 pipe_buffer_unmap(pipe, st->clear.vbuf);
176
177 /* draw */
178 util_draw_vertex_buffer(pipe, st->clear.vbuf,
179 PIPE_PRIM_TRIANGLE_FAN,
180 4, /* verts */
181 2); /* attribs/vert */
182 }
183
184
185
186 /**
187 * Do glClear by drawing a quadrilateral.
188 * The vertices of the quad will be computed from the
189 * ctx->DrawBuffer->_X/Ymin/max fields.
190 */
191 static void
192 clear_with_quad(GLcontext *ctx,
193 GLboolean color, GLboolean depth, GLboolean stencil)
194 {
195 struct st_context *st = ctx->st;
196 const GLfloat x0 = ctx->DrawBuffer->_Xmin;
197 const GLfloat x1 = ctx->DrawBuffer->_Xmax;
198 GLfloat y0, y1;
199
200 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
201 y0 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax;
202 y1 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin;
203 }
204 else {
205 y0 = ctx->DrawBuffer->_Ymin;
206 y1 = ctx->DrawBuffer->_Ymax;
207 }
208
209 /*
210 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
211 color ? "color, " : "",
212 depth ? "depth, " : "",
213 stencil ? "stencil" : "",
214 x0, y0,
215 x1, y1);
216 */
217
218 cso_save_blend(st->cso_context);
219 cso_save_depth_stencil_alpha(st->cso_context);
220 cso_save_rasterizer(st->cso_context);
221 cso_save_viewport(st->cso_context);
222 cso_save_fragment_shader(st->cso_context);
223 cso_save_vertex_shader(st->cso_context);
224
225 /* blend state: RGBA masking */
226 {
227 struct pipe_blend_state blend;
228 memset(&blend, 0, sizeof(blend));
229 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
230 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
231 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
232 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
233 if (color) {
234 if (ctx->Color.ColorMask[0])
235 blend.colormask |= PIPE_MASK_R;
236 if (ctx->Color.ColorMask[1])
237 blend.colormask |= PIPE_MASK_G;
238 if (ctx->Color.ColorMask[2])
239 blend.colormask |= PIPE_MASK_B;
240 if (ctx->Color.ColorMask[3])
241 blend.colormask |= PIPE_MASK_A;
242 if (st->ctx->Color.DitherFlag)
243 blend.dither = 1;
244 }
245 cso_set_blend(st->cso_context, &blend);
246 }
247
248 /* depth_stencil state: always pass/set to ref value */
249 {
250 struct pipe_depth_stencil_alpha_state depth_stencil;
251 memset(&depth_stencil, 0, sizeof(depth_stencil));
252 if (depth) {
253 depth_stencil.depth.enabled = 1;
254 depth_stencil.depth.writemask = 1;
255 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
256 }
257
258 if (stencil) {
259 depth_stencil.stencil[0].enabled = 1;
260 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
261 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
262 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
263 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
264 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
265 depth_stencil.stencil[0].value_mask = 0xff;
266 depth_stencil.stencil[0].write_mask = ctx->Stencil.WriteMask[0] & 0xff;
267 }
268
269 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
270 }
271
272 cso_set_rasterizer(st->cso_context, &st->clear.raster);
273 cso_set_viewport(st->cso_context, &st->clear.viewport);
274
275 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
276 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
277
278 /* draw quad matching scissor rect (XXX verify coord round-off) */
279 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
280
281 /* Restore pipe state */
282 cso_restore_blend(st->cso_context);
283 cso_restore_depth_stencil_alpha(st->cso_context);
284 cso_restore_rasterizer(st->cso_context);
285 cso_restore_viewport(st->cso_context);
286 cso_restore_fragment_shader(st->cso_context);
287 cso_restore_vertex_shader(st->cso_context);
288 }
289
290
291 /**
292 * Determine if we need to clear the depth buffer by drawing a quad.
293 */
294 static INLINE GLboolean
295 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
296 {
297 const struct st_renderbuffer *strb = st_renderbuffer(rb);
298
299 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
300 return FALSE;
301
302 if (ctx->Scissor.Enabled)
303 return TRUE;
304
305 if (!ctx->Color.ColorMask[0] ||
306 !ctx->Color.ColorMask[1] ||
307 !ctx->Color.ColorMask[2] ||
308 !ctx->Color.ColorMask[3])
309 return TRUE;
310
311 return FALSE;
312 }
313
314
315 static INLINE GLboolean
316 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
317 {
318 const struct st_renderbuffer *strb = st_renderbuffer(rb);
319 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
320 GLboolean maskStencil
321 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
322
323 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
324 return FALSE;
325
326 if (ctx->Scissor.Enabled)
327 return TRUE;
328
329 if (maskStencil)
330 return TRUE;
331
332 return FALSE;
333 }
334
335
336 /**
337 * Determine if we need to clear the depth buffer by drawing a quad.
338 */
339 static INLINE GLboolean
340 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
341 {
342 const struct st_renderbuffer *strb = st_renderbuffer(rb);
343 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
344
345 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
346 return FALSE;
347
348 if (ctx->Scissor.Enabled)
349 return TRUE;
350
351 if (isDS &&
352 strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
353 ctx->DrawBuffer->Visual.stencilBits > 0)
354 return TRUE;
355
356 return FALSE;
357 }
358
359
360 /**
361 * Determine if we need to clear the stencil buffer by drawing a quad.
362 */
363 static INLINE GLboolean
364 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
365 {
366 const struct st_renderbuffer *strb = st_renderbuffer(rb);
367 const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
368 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
369 const GLboolean maskStencil
370 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
371
372 if (strb->surface->status == PIPE_SURFACE_STATUS_UNDEFINED)
373 return FALSE;
374
375 if (maskStencil)
376 return TRUE;
377
378 if (ctx->Scissor.Enabled)
379 return TRUE;
380
381 /* This is correct, but it is necessary to look at the depth clear
382 * value held in the surface when it comes time to issue the clear,
383 * rather than taking depth and stencil clear values from the
384 * current state.
385 */
386 if (isDS &&
387 strb->surface->status == PIPE_SURFACE_STATUS_DEFINED &&
388 ctx->DrawBuffer->Visual.depthBits > 0)
389 return TRUE;
390
391 return FALSE;
392 }
393
394
395
396 static void
397 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
398 {
399 if (check_clear_color_with_quad( ctx, rb )) {
400 /* masking or scissoring */
401 clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
402 }
403 else {
404 /* clear whole buffer w/out masking */
405 struct st_renderbuffer *strb = st_renderbuffer(rb);
406 uint clearValue;
407 util_pack_color(ctx->Color.ClearColor, PIPE_FORMAT_A8R8G8B8_UNORM, &clearValue);
408 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
409 }
410 }
411
412
413 static void
414 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
415 {
416 if (check_clear_depth_with_quad(ctx, rb)) {
417 /* scissoring or we have a combined depth/stencil buffer */
418 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
419 }
420 else {
421 struct st_renderbuffer *strb = st_renderbuffer(rb);
422
423 /* simple clear of whole buffer */
424 uint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
425 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
426 }
427 }
428
429
430 static void
431 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
432 {
433 if (check_clear_stencil_with_quad(ctx, rb)) {
434 /* masking or scissoring or combined depth/stencil buffer */
435 clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
436 }
437 else {
438 struct st_renderbuffer *strb = st_renderbuffer(rb);
439
440 /* simple clear of whole buffer */
441 GLuint clearValue = ctx->Stencil.Clear;
442
443 switch (strb->surface->format) {
444 case PIPE_FORMAT_S8Z24_UNORM:
445 clearValue <<= 24;
446 break;
447 default:
448 ; /* no-op, stencil value is in least significant bits */
449 }
450
451 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
452 }
453 }
454
455
456 static void
457 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
458 {
459
460 if (check_clear_depth_stencil_with_quad(ctx, rb)) {
461 /* masking or scissoring */
462 clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
463 }
464 else {
465 struct st_renderbuffer *strb = st_renderbuffer(rb);
466
467 /* clear whole buffer w/out masking */
468 GLuint clearValue = util_pack_z(strb->surface->format, ctx->Depth.Clear);
469
470 switch (strb->surface->format) {
471 case PIPE_FORMAT_S8Z24_UNORM:
472 clearValue |= ctx->Stencil.Clear << 24;
473 break;
474 case PIPE_FORMAT_Z24S8_UNORM:
475 clearValue |= ctx->Stencil.Clear;
476 break;
477 default:
478 assert(0);
479 }
480
481 ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
482 }
483 }
484
485
486
487 /**
488 * Called via ctx->Driver.Clear()
489 * XXX: doesn't pick up the differences between front/back/left/right
490 * clears. Need to sort that out...
491 */
492 static void st_clear(GLcontext *ctx, GLbitfield mask)
493 {
494 static const GLbitfield BUFFER_BITS_DS
495 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
496 struct st_context *st = ctx->st;
497 struct gl_renderbuffer *depthRb
498 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
499 struct gl_renderbuffer *stencilRb
500 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
501 GLbitfield cmask = mask & BUFFER_BITS_COLOR;
502
503 /* This makes sure the softpipe has the latest scissor, etc values */
504 st_validate_state( st );
505
506 /*
507 * XXX TO-DO:
508 * If we're going to use clear_with_quad() for any reason, use it to
509 * clear as many other buffers as possible.
510 * As it is now, we sometimes call clear_with_quad() three times to clear
511 * color/depth/stencil individually...
512 */
513
514 if (cmask) {
515 GLuint b;
516 for (b = 0; cmask; b++) {
517 if (cmask & (1 << b)) {
518 struct gl_renderbuffer *rb
519 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
520 assert(rb);
521 clear_color_buffer(ctx, rb);
522 cmask &= ~(1 << b); /* turn off bit */
523 }
524 assert(b < BUFFER_COUNT);
525 }
526 }
527
528 if (mask & BUFFER_BIT_ACCUM) {
529 st_clear_accum_buffer(ctx,
530 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
531 }
532
533 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
534 /* clearing combined depth + stencil */
535 clear_depth_stencil_buffer(ctx, depthRb);
536 }
537 else {
538 /* separate depth/stencil clears */
539 if (mask & BUFFER_BIT_DEPTH) {
540 clear_depth_buffer(ctx, depthRb);
541 }
542 if (mask & BUFFER_BIT_STENCIL) {
543 clear_stencil_buffer(ctx, stencilRb);
544 }
545 }
546 }
547
548
549 void st_init_clear_functions(struct dd_function_table *functions)
550 {
551 functions->Clear = st_clear;
552 }