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