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