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