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