mesa: remove a bunch of gl_renderbuffer fields
[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 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Keith Whitwell <keith@tungstengraphics.com>
32 * Brian Paul
33 * Michel Dänzer
34 */
35
36 #include "main/glheader.h"
37 #include "main/formats.h"
38 #include "main/macros.h"
39 #include "shader/prog_instruction.h"
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_cb_accum.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
45 #include "st_draw.h"
46 #include "st_program.h"
47 #include "st_public.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "st_inlines.h"
50
51 #include "pipe/p_context.h"
52 #include "pipe/p_inlines.h"
53 #include "pipe/p_state.h"
54 #include "pipe/p_defines.h"
55 #include "util/u_pack_color.h"
56 #include "util/u_simple_shaders.h"
57 #include "util/u_draw_quad.h"
58
59 #include "cso_cache/cso_context.h"
60
61
62 void
63 st_init_clear(struct st_context *st)
64 {
65 struct pipe_context *pipe = st->pipe;
66
67 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
68 st->clear.raster.gl_rasterization_rules = 1;
69
70 /* rasterizer state: bypass vertex shader, clipping and viewport */
71 st->clear.raster.bypass_vs_clip_and_viewport = 1;
72
73 /* fragment shader state: color pass-through program */
74 st->clear.fs =
75 util_make_fragment_passthrough_shader(pipe);
76
77 /* vertex shader state: color/position pass-through */
78 {
79 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
80 TGSI_SEMANTIC_COLOR };
81 const uint semantic_indexes[] = { 0, 0 };
82 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
83 semantic_names,
84 semantic_indexes);
85 }
86 }
87
88
89 void
90 st_destroy_clear(struct st_context *st)
91 {
92 if (st->clear.fs) {
93 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
94 st->clear.fs = NULL;
95 }
96 if (st->clear.vs) {
97 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
98 st->clear.vs = NULL;
99 }
100 if (st->clear.vbuf) {
101 pipe_buffer_reference(&st->clear.vbuf, NULL);
102 st->clear.vbuf = NULL;
103 }
104 }
105
106
107 /**
108 * Draw a screen-aligned quadrilateral.
109 * Coords are window coords with y=0=bottom. These will be passed
110 * through unmodified to the rasterizer as we have set
111 * rasterizer->bypass_vs_clip_and_viewport.
112 */
113 static void
114 draw_quad(GLcontext *ctx,
115 float x0, float y0, float x1, float y1, GLfloat z,
116 const GLfloat color[4])
117 {
118 struct st_context *st = ctx->st;
119 struct pipe_context *pipe = st->pipe;
120 const GLuint max_slots = 1024 / sizeof(st->clear.vertices);
121 GLuint i;
122
123 if (st->clear.vbuf_slot >= max_slots) {
124 pipe_buffer_reference(&st->clear.vbuf, NULL);
125 st->clear.vbuf_slot = 0;
126 }
127
128 if (!st->clear.vbuf) {
129 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
130 max_slots * sizeof(st->clear.vertices));
131 }
132
133 /* positions */
134 st->clear.vertices[0][0][0] = x0;
135 st->clear.vertices[0][0][1] = y0;
136
137 st->clear.vertices[1][0][0] = x1;
138 st->clear.vertices[1][0][1] = y0;
139
140 st->clear.vertices[2][0][0] = x1;
141 st->clear.vertices[2][0][1] = y1;
142
143 st->clear.vertices[3][0][0] = x0;
144 st->clear.vertices[3][0][1] = y1;
145
146 /* same for all verts: */
147 for (i = 0; i < 4; i++) {
148 st->clear.vertices[i][0][2] = z;
149 st->clear.vertices[i][0][3] = 1.0;
150 st->clear.vertices[i][1][0] = color[0];
151 st->clear.vertices[i][1][1] = color[1];
152 st->clear.vertices[i][1][2] = color[2];
153 st->clear.vertices[i][1][3] = color[3];
154 }
155
156 /* put vertex data into vbuf */
157 st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
158 st->clear.vbuf_slot * sizeof(st->clear.vertices),
159 sizeof(st->clear.vertices),
160 st->clear.vertices);
161
162 /* draw */
163 util_draw_vertex_buffer(pipe,
164 st->clear.vbuf,
165 st->clear.vbuf_slot * sizeof(st->clear.vertices),
166 PIPE_PRIM_TRIANGLE_FAN,
167 4, /* verts */
168 2); /* attribs/vert */
169
170 /* Increment slot */
171 st->clear.vbuf_slot++;
172 }
173
174
175
176 /**
177 * Do glClear by drawing a quadrilateral.
178 * The vertices of the quad will be computed from the
179 * ctx->DrawBuffer->_X/Ymin/max fields.
180 */
181 static void
182 clear_with_quad(GLcontext *ctx,
183 GLboolean color, GLboolean depth, GLboolean stencil)
184 {
185 struct st_context *st = ctx->st;
186 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
187 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
188 GLfloat y0, y1;
189
190 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
191 y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
192 y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
193 }
194 else {
195 y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
196 y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
197 }
198
199 /*
200 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
201 color ? "color, " : "",
202 depth ? "depth, " : "",
203 stencil ? "stencil" : "",
204 x0, y0,
205 x1, y1);
206 */
207
208 cso_save_blend(st->cso_context);
209 cso_save_depth_stencil_alpha(st->cso_context);
210 cso_save_rasterizer(st->cso_context);
211 cso_save_fragment_shader(st->cso_context);
212 cso_save_vertex_shader(st->cso_context);
213
214 /* blend state: RGBA masking */
215 {
216 struct pipe_blend_state blend;
217 memset(&blend, 0, sizeof(blend));
218 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
219 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
220 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
221 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
222 if (color) {
223 if (ctx->Color.ColorMask[0])
224 blend.colormask |= PIPE_MASK_R;
225 if (ctx->Color.ColorMask[1])
226 blend.colormask |= PIPE_MASK_G;
227 if (ctx->Color.ColorMask[2])
228 blend.colormask |= PIPE_MASK_B;
229 if (ctx->Color.ColorMask[3])
230 blend.colormask |= PIPE_MASK_A;
231 if (st->ctx->Color.DitherFlag)
232 blend.dither = 1;
233 }
234 cso_set_blend(st->cso_context, &blend);
235 }
236
237 /* depth_stencil state: always pass/set to ref value */
238 {
239 struct pipe_depth_stencil_alpha_state depth_stencil;
240 memset(&depth_stencil, 0, sizeof(depth_stencil));
241 if (depth) {
242 depth_stencil.depth.enabled = 1;
243 depth_stencil.depth.writemask = 1;
244 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
245 }
246
247 if (stencil) {
248 depth_stencil.stencil[0].enabled = 1;
249 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
250 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
251 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
252 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
253 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
254 depth_stencil.stencil[0].valuemask = 0xff;
255 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
256 }
257
258 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
259 }
260
261 cso_set_rasterizer(st->cso_context, &st->clear.raster);
262
263 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
264 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
265
266 /* draw quad matching scissor rect (XXX verify coord round-off) */
267 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
268
269 /* Restore pipe state */
270 cso_restore_blend(st->cso_context);
271 cso_restore_depth_stencil_alpha(st->cso_context);
272 cso_restore_rasterizer(st->cso_context);
273 cso_restore_fragment_shader(st->cso_context);
274 cso_restore_vertex_shader(st->cso_context);
275 }
276
277
278 /**
279 * Determine if we need to clear the depth buffer by drawing a quad.
280 */
281 static INLINE GLboolean
282 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
283 {
284 if (ctx->Scissor.Enabled &&
285 (ctx->Scissor.X != 0 ||
286 ctx->Scissor.Y != 0 ||
287 ctx->Scissor.Width < rb->Width ||
288 ctx->Scissor.Height < rb->Height))
289 return TRUE;
290
291 if (!ctx->Color.ColorMask[0] ||
292 !ctx->Color.ColorMask[1] ||
293 !ctx->Color.ColorMask[2] ||
294 !ctx->Color.ColorMask[3])
295 return TRUE;
296
297 return FALSE;
298 }
299
300
301 static INLINE GLboolean
302 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
303 {
304 const GLuint stencilMax = 0xff;
305 GLboolean maskStencil
306 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
307
308 assert(rb->Format == MESA_FORMAT_S8 ||
309 rb->Format == MESA_FORMAT_Z24_S8 ||
310 rb->Format == MESA_FORMAT_S8_Z24);
311
312 if (ctx->Scissor.Enabled &&
313 (ctx->Scissor.X != 0 ||
314 ctx->Scissor.Y != 0 ||
315 ctx->Scissor.Width < rb->Width ||
316 ctx->Scissor.Height < rb->Height))
317 return TRUE;
318
319 if (maskStencil)
320 return TRUE;
321
322 return FALSE;
323 }
324
325
326 /**
327 * Determine if we need to clear the depth buffer by drawing a quad.
328 */
329 static INLINE GLboolean
330 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
331 {
332 const struct st_renderbuffer *strb = st_renderbuffer(rb);
333 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
334
335 if (ctx->Scissor.Enabled &&
336 (ctx->Scissor.X != 0 ||
337 ctx->Scissor.Y != 0 ||
338 ctx->Scissor.Width < rb->Width ||
339 ctx->Scissor.Height < rb->Height))
340 return TRUE;
341
342 if (isDS &&
343 ctx->DrawBuffer->Visual.stencilBits > 0)
344 return TRUE;
345
346 return FALSE;
347 }
348
349
350 /**
351 * Determine if we need to clear the stencil buffer by drawing a quad.
352 */
353 static INLINE GLboolean
354 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
355 {
356 const struct st_renderbuffer *strb = st_renderbuffer(rb);
357 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
358 const GLuint stencilMax = 0xff;
359 const GLboolean maskStencil
360 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
361
362 assert(rb->Format == MESA_FORMAT_S8 ||
363 rb->Format == MESA_FORMAT_Z24_S8 ||
364 rb->Format == MESA_FORMAT_S8_Z24);
365
366 if (maskStencil)
367 return TRUE;
368
369 if (ctx->Scissor.Enabled &&
370 (ctx->Scissor.X != 0 ||
371 ctx->Scissor.Y != 0 ||
372 ctx->Scissor.Width < rb->Width ||
373 ctx->Scissor.Height < rb->Height))
374 return TRUE;
375
376 /* This is correct, but it is necessary to look at the depth clear
377 * value held in the surface when it comes time to issue the clear,
378 * rather than taking depth and stencil clear values from the
379 * current state.
380 */
381 if (isDS &&
382 ctx->DrawBuffer->Visual.depthBits > 0)
383 return TRUE;
384
385 return FALSE;
386 }
387
388
389
390 void st_flush_clear( struct st_context *st )
391 {
392 /* Release vertex buffer to avoid synchronous rendering if we were
393 * to map it in the next frame.
394 */
395 pipe_buffer_reference(&st->clear.vbuf, NULL);
396 st->clear.vbuf_slot = 0;
397 }
398
399
400
401 /**
402 * Called via ctx->Driver.Clear()
403 * XXX: doesn't pick up the differences between front/back/left/right
404 * clears. Need to sort that out...
405 */
406 static void st_clear(GLcontext *ctx, GLbitfield mask)
407 {
408 static const GLbitfield BUFFER_BITS_DS
409 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
410 struct st_context *st = ctx->st;
411 struct gl_renderbuffer *depthRb
412 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
413 struct gl_renderbuffer *stencilRb
414 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
415 GLbitfield quad_buffers = 0;
416 GLbitfield clear_buffers = 0;
417 GLuint i;
418
419 /* This makes sure the pipe has the latest scissor, etc values */
420 st_validate_state( st );
421
422 if (mask & BUFFER_BITS_COLOR) {
423 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
424 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
425
426 if (mask & (1 << b)) {
427 struct gl_renderbuffer *rb
428 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
429 struct st_renderbuffer *strb;
430
431 assert(rb);
432
433 strb = st_renderbuffer(rb);
434
435 if (!strb->surface)
436 continue;
437
438 if (check_clear_color_with_quad( ctx, rb ))
439 quad_buffers |= PIPE_CLEAR_COLOR;
440 else
441 clear_buffers |= PIPE_CLEAR_COLOR;
442 }
443 }
444 }
445
446 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
447 /* clearing combined depth + stencil */
448 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
449
450 if (strb->surface) {
451 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
452 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
453 else
454 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
455 }
456 }
457 else {
458 /* separate depth/stencil clears */
459 if (mask & BUFFER_BIT_DEPTH) {
460 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
461
462 if (strb->surface) {
463 if (check_clear_depth_with_quad(ctx, depthRb))
464 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
465 else
466 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
467 }
468 }
469 if (mask & BUFFER_BIT_STENCIL) {
470 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
471
472 if (strb->surface) {
473 if (check_clear_stencil_with_quad(ctx, stencilRb))
474 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
475 else
476 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
477 }
478 }
479 }
480
481 /*
482 * If we're going to use clear_with_quad() for any reason, use it for
483 * everything possible.
484 */
485 if (quad_buffers) {
486 quad_buffers |= clear_buffers;
487 clear_with_quad(ctx,
488 quad_buffers & PIPE_CLEAR_COLOR,
489 mask & BUFFER_BIT_DEPTH,
490 mask & BUFFER_BIT_STENCIL);
491 } else if (clear_buffers)
492 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
493 ctx->Depth.Clear, ctx->Stencil.Clear);
494
495 if (mask & BUFFER_BIT_ACCUM)
496 st_clear_accum_buffer(ctx,
497 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
498 }
499
500
501 void st_init_clear_functions(struct dd_function_table *functions)
502 {
503 functions->Clear = st_clear;
504 }