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