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