mesa: Use pipe_buffer_write_nooverlap where appropriate.
[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_program.h"
46 #include "st_public.h"
47 #include "st_inlines.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_inlines.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_simple_shaders.h"
54 #include "util/u_draw_quad.h"
55
56 #include "cso_cache/cso_context.h"
57
58
59 void
60 st_init_clear(struct st_context *st)
61 {
62 struct pipe_context *pipe = st->pipe;
63
64 memset(&st->clear.raster, 0, sizeof(st->clear.raster));
65 st->clear.raster.gl_rasterization_rules = 1;
66
67 /* rasterizer state: bypass vertex shader, clipping and viewport */
68 st->clear.raster.bypass_vs_clip_and_viewport = 1;
69
70 /* fragment shader state: color pass-through program */
71 st->clear.fs =
72 util_make_fragment_passthrough_shader(pipe);
73
74 /* vertex shader state: color/position pass-through */
75 {
76 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
77 TGSI_SEMANTIC_COLOR };
78 const uint semantic_indexes[] = { 0, 0 };
79 st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
80 semantic_names,
81 semantic_indexes);
82 }
83 }
84
85
86 void
87 st_destroy_clear(struct st_context *st)
88 {
89 if (st->clear.fs) {
90 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
91 st->clear.fs = NULL;
92 }
93 if (st->clear.vs) {
94 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
95 st->clear.vs = NULL;
96 }
97 if (st->clear.vbuf) {
98 pipe_buffer_reference(&st->clear.vbuf, NULL);
99 st->clear.vbuf = NULL;
100 }
101 }
102
103
104 /**
105 * Draw a screen-aligned quadrilateral.
106 * Coords are window coords with y=0=bottom. These will be passed
107 * through unmodified to the rasterizer as we have set
108 * rasterizer->bypass_vs_clip_and_viewport.
109 */
110 static void
111 draw_quad(GLcontext *ctx,
112 float x0, float y0, float x1, float y1, GLfloat z,
113 const GLfloat color[4])
114 {
115 struct st_context *st = ctx->st;
116 struct pipe_context *pipe = st->pipe;
117
118 /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
119 * no_flush) updates to buffers where we know there is no conflict
120 * with previous data. Currently using max_slots > 1 will cause
121 * synchronous rendering if the driver flushes its command buffers
122 * between one bitmap and the next. Our flush hook below isn't
123 * sufficient to catch this as the driver doesn't tell us when it
124 * flushes its own command buffers. Until this gets fixed, pay the
125 * price of allocating a new buffer for each bitmap cache-flush to
126 * avoid synchronous rendering.
127 */
128 const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
129 GLuint i;
130
131 if (st->clear.vbuf_slot >= max_slots) {
132 pipe_buffer_reference(&st->clear.vbuf, NULL);
133 st->clear.vbuf_slot = 0;
134 }
135
136 if (!st->clear.vbuf) {
137 st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
138 max_slots * sizeof(st->clear.vertices));
139 }
140
141 /* positions */
142 st->clear.vertices[0][0][0] = x0;
143 st->clear.vertices[0][0][1] = y0;
144
145 st->clear.vertices[1][0][0] = x1;
146 st->clear.vertices[1][0][1] = y0;
147
148 st->clear.vertices[2][0][0] = x1;
149 st->clear.vertices[2][0][1] = y1;
150
151 st->clear.vertices[3][0][0] = x0;
152 st->clear.vertices[3][0][1] = y1;
153
154 /* same for all verts: */
155 for (i = 0; i < 4; i++) {
156 st->clear.vertices[i][0][2] = z;
157 st->clear.vertices[i][0][3] = 1.0;
158 st->clear.vertices[i][1][0] = color[0];
159 st->clear.vertices[i][1][1] = color[1];
160 st->clear.vertices[i][1][2] = color[2];
161 st->clear.vertices[i][1][3] = color[3];
162 }
163
164 /* put vertex data into vbuf */
165 st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
166 st->clear.vbuf_slot * sizeof(st->clear.vertices),
167 sizeof(st->clear.vertices),
168 st->clear.vertices);
169
170 /* draw */
171 util_draw_vertex_buffer(pipe,
172 st->clear.vbuf,
173 st->clear.vbuf_slot * sizeof(st->clear.vertices),
174 PIPE_PRIM_TRIANGLE_FAN,
175 4, /* verts */
176 2); /* attribs/vert */
177
178 /* Increment slot */
179 st->clear.vbuf_slot++;
180 }
181
182
183
184 /**
185 * Do glClear by drawing a quadrilateral.
186 * The vertices of the quad will be computed from the
187 * ctx->DrawBuffer->_X/Ymin/max fields.
188 */
189 static void
190 clear_with_quad(GLcontext *ctx,
191 GLboolean color, GLboolean depth, GLboolean stencil)
192 {
193 struct st_context *st = ctx->st;
194 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
195 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
196 GLfloat y0, y1;
197
198 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
199 y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
200 y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
201 }
202 else {
203 y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
204 y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
205 }
206
207 /*
208 printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__,
209 color ? "color, " : "",
210 depth ? "depth, " : "",
211 stencil ? "stencil" : "",
212 x0, y0,
213 x1, y1);
214 */
215
216 cso_save_blend(st->cso_context);
217 cso_save_depth_stencil_alpha(st->cso_context);
218 cso_save_rasterizer(st->cso_context);
219 cso_save_fragment_shader(st->cso_context);
220 cso_save_vertex_shader(st->cso_context);
221
222 /* blend state: RGBA masking */
223 {
224 struct pipe_blend_state blend;
225 memset(&blend, 0, sizeof(blend));
226 blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
227 blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
228 blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
229 blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
230 if (color) {
231 if (ctx->Color.ColorMask[0])
232 blend.colormask |= PIPE_MASK_R;
233 if (ctx->Color.ColorMask[1])
234 blend.colormask |= PIPE_MASK_G;
235 if (ctx->Color.ColorMask[2])
236 blend.colormask |= PIPE_MASK_B;
237 if (ctx->Color.ColorMask[3])
238 blend.colormask |= PIPE_MASK_A;
239 if (st->ctx->Color.DitherFlag)
240 blend.dither = 1;
241 }
242 cso_set_blend(st->cso_context, &blend);
243 }
244
245 /* depth_stencil state: always pass/set to ref value */
246 {
247 struct pipe_depth_stencil_alpha_state depth_stencil;
248 memset(&depth_stencil, 0, sizeof(depth_stencil));
249 if (depth) {
250 depth_stencil.depth.enabled = 1;
251 depth_stencil.depth.writemask = 1;
252 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
253 }
254
255 if (stencil) {
256 depth_stencil.stencil[0].enabled = 1;
257 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
258 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
259 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
260 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
261 depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
262 depth_stencil.stencil[0].valuemask = 0xff;
263 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
264 }
265
266 cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
267 }
268
269 cso_set_rasterizer(st->cso_context, &st->clear.raster);
270
271 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
272 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
273
274 /* draw quad matching scissor rect (XXX verify coord round-off) */
275 draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
276
277 /* Restore pipe state */
278 cso_restore_blend(st->cso_context);
279 cso_restore_depth_stencil_alpha(st->cso_context);
280 cso_restore_rasterizer(st->cso_context);
281 cso_restore_fragment_shader(st->cso_context);
282 cso_restore_vertex_shader(st->cso_context);
283 }
284
285
286 /**
287 * Determine if we need to clear the depth buffer by drawing a quad.
288 */
289 static INLINE GLboolean
290 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
291 {
292 if (ctx->Scissor.Enabled &&
293 (ctx->Scissor.X != 0 ||
294 ctx->Scissor.Y != 0 ||
295 ctx->Scissor.Width < rb->Width ||
296 ctx->Scissor.Height < rb->Height))
297 return TRUE;
298
299 if (!ctx->Color.ColorMask[0] ||
300 !ctx->Color.ColorMask[1] ||
301 !ctx->Color.ColorMask[2] ||
302 !ctx->Color.ColorMask[3])
303 return TRUE;
304
305 return FALSE;
306 }
307
308
309 static INLINE GLboolean
310 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
311 {
312 const GLuint stencilMax = 0xff;
313 GLboolean maskStencil
314 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
315
316 assert(rb->Format == MESA_FORMAT_S8 ||
317 rb->Format == MESA_FORMAT_Z24_S8 ||
318 rb->Format == MESA_FORMAT_S8_Z24);
319
320 if (ctx->Scissor.Enabled &&
321 (ctx->Scissor.X != 0 ||
322 ctx->Scissor.Y != 0 ||
323 ctx->Scissor.Width < rb->Width ||
324 ctx->Scissor.Height < rb->Height))
325 return TRUE;
326
327 if (maskStencil)
328 return TRUE;
329
330 return FALSE;
331 }
332
333
334 /**
335 * Determine if we need to clear the depth buffer by drawing a quad.
336 */
337 static INLINE GLboolean
338 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
339 {
340 const struct st_renderbuffer *strb = st_renderbuffer(rb);
341 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
342
343 if (ctx->Scissor.Enabled &&
344 (ctx->Scissor.X != 0 ||
345 ctx->Scissor.Y != 0 ||
346 ctx->Scissor.Width < rb->Width ||
347 ctx->Scissor.Height < rb->Height))
348 return TRUE;
349
350 if (isDS &&
351 ctx->DrawBuffer->Visual.stencilBits > 0)
352 return TRUE;
353
354 return FALSE;
355 }
356
357
358 /**
359 * Determine if we need to clear the stencil buffer by drawing a quad.
360 */
361 static INLINE GLboolean
362 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
363 {
364 const struct st_renderbuffer *strb = st_renderbuffer(rb);
365 const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
366 const GLuint stencilMax = 0xff;
367 const GLboolean maskStencil
368 = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
369
370 assert(rb->Format == MESA_FORMAT_S8 ||
371 rb->Format == MESA_FORMAT_Z24_S8 ||
372 rb->Format == MESA_FORMAT_S8_Z24);
373
374 if (maskStencil)
375 return TRUE;
376
377 if (ctx->Scissor.Enabled &&
378 (ctx->Scissor.X != 0 ||
379 ctx->Scissor.Y != 0 ||
380 ctx->Scissor.Width < rb->Width ||
381 ctx->Scissor.Height < rb->Height))
382 return TRUE;
383
384 /* This is correct, but it is necessary to look at the depth clear
385 * value held in the surface when it comes time to issue the clear,
386 * rather than taking depth and stencil clear values from the
387 * current state.
388 */
389 if (isDS &&
390 ctx->DrawBuffer->Visual.depthBits > 0)
391 return TRUE;
392
393 return FALSE;
394 }
395
396
397
398 void st_flush_clear( struct st_context *st )
399 {
400 /* Release vertex buffer to avoid synchronous rendering if we were
401 * to map it in the next frame.
402 */
403 pipe_buffer_reference(&st->clear.vbuf, NULL);
404 st->clear.vbuf_slot = 0;
405 }
406
407
408
409 /**
410 * Called via ctx->Driver.Clear()
411 * XXX: doesn't pick up the differences between front/back/left/right
412 * clears. Need to sort that out...
413 */
414 static void st_clear(GLcontext *ctx, GLbitfield mask)
415 {
416 static const GLbitfield BUFFER_BITS_DS
417 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
418 struct st_context *st = ctx->st;
419 struct gl_renderbuffer *depthRb
420 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
421 struct gl_renderbuffer *stencilRb
422 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
423 GLbitfield quad_buffers = 0;
424 GLbitfield clear_buffers = 0;
425 GLuint i;
426
427 /* This makes sure the pipe has the latest scissor, etc values */
428 st_validate_state( st );
429
430 if (mask & BUFFER_BITS_COLOR) {
431 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
432 GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
433
434 if (mask & (1 << b)) {
435 struct gl_renderbuffer *rb
436 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
437 struct st_renderbuffer *strb;
438
439 assert(rb);
440
441 strb = st_renderbuffer(rb);
442
443 if (!strb->surface)
444 continue;
445
446 if (check_clear_color_with_quad( ctx, rb ))
447 quad_buffers |= PIPE_CLEAR_COLOR;
448 else
449 clear_buffers |= PIPE_CLEAR_COLOR;
450 }
451 }
452 }
453
454 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
455 /* clearing combined depth + stencil */
456 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
457
458 if (strb->surface) {
459 if (check_clear_depth_stencil_with_quad(ctx, depthRb))
460 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
461 else
462 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
463 }
464 }
465 else {
466 /* separate depth/stencil clears */
467 if (mask & BUFFER_BIT_DEPTH) {
468 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
469
470 if (strb->surface) {
471 if (check_clear_depth_with_quad(ctx, depthRb))
472 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
473 else
474 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
475 }
476 }
477 if (mask & BUFFER_BIT_STENCIL) {
478 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
479
480 if (strb->surface) {
481 if (check_clear_stencil_with_quad(ctx, stencilRb))
482 quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
483 else
484 clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
485 }
486 }
487 }
488
489 /*
490 * If we're going to use clear_with_quad() for any reason, use it for
491 * everything possible.
492 */
493 if (quad_buffers) {
494 quad_buffers |= clear_buffers;
495 clear_with_quad(ctx,
496 quad_buffers & PIPE_CLEAR_COLOR,
497 mask & BUFFER_BIT_DEPTH,
498 mask & BUFFER_BIT_STENCIL);
499 } else if (clear_buffers)
500 ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
501 ctx->Depth.Clear, ctx->Stencil.Clear);
502
503 if (mask & BUFFER_BIT_ACCUM)
504 st_clear_accum_buffer(ctx,
505 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
506 }
507
508
509 void st_init_clear_functions(struct dd_function_table *functions)
510 {
511 functions->Clear = st_clear;
512 }