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