gallium: split depth_clip into depth_clip_near & depth_clip_far
[mesa.git] / src / mesa / state_tracker / st_cb_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
32 * Brian Paul
33 * Michel Dänzer
34 */
35
36 #include "main/errors.h"
37 #include "main/glheader.h"
38 #include "main/accum.h"
39 #include "main/formats.h"
40 #include "main/framebuffer.h"
41 #include "main/macros.h"
42 #include "main/glformats.h"
43 #include "program/prog_instruction.h"
44 #include "st_context.h"
45 #include "st_atom.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_clear.h"
48 #include "st_cb_fbo.h"
49 #include "st_draw.h"
50 #include "st_format.h"
51 #include "st_program.h"
52
53 #include "pipe/p_context.h"
54 #include "pipe/p_shader_tokens.h"
55 #include "pipe/p_state.h"
56 #include "pipe/p_defines.h"
57 #include "util/u_format.h"
58 #include "util/u_inlines.h"
59 #include "util/u_simple_shaders.h"
60
61 #include "cso_cache/cso_context.h"
62
63
64 /**
65 * Do per-context initialization for glClear.
66 */
67 void
68 st_init_clear(struct st_context *st)
69 {
70 memset(&st->clear, 0, sizeof(st->clear));
71
72 st->clear.raster.half_pixel_center = 1;
73 st->clear.raster.bottom_edge_rule = 1;
74 st->clear.raster.depth_clip_near = 1;
75 st->clear.raster.depth_clip_far = 1;
76 }
77
78
79 /**
80 * Free per-context state for glClear.
81 */
82 void
83 st_destroy_clear(struct st_context *st)
84 {
85 if (st->clear.fs) {
86 cso_delete_fragment_shader(st->cso_context, st->clear.fs);
87 st->clear.fs = NULL;
88 }
89 if (st->clear.vs) {
90 cso_delete_vertex_shader(st->cso_context, st->clear.vs);
91 st->clear.vs = NULL;
92 }
93 if (st->clear.vs_layered) {
94 cso_delete_vertex_shader(st->cso_context, st->clear.vs_layered);
95 st->clear.vs_layered = NULL;
96 }
97 if (st->clear.gs_layered) {
98 cso_delete_geometry_shader(st->cso_context, st->clear.gs_layered);
99 st->clear.gs_layered = NULL;
100 }
101 }
102
103
104 /**
105 * Helper function to set the fragment shaders.
106 */
107 static inline void
108 set_fragment_shader(struct st_context *st)
109 {
110 if (!st->clear.fs)
111 st->clear.fs =
112 util_make_fragment_passthrough_shader(st->pipe, TGSI_SEMANTIC_GENERIC,
113 TGSI_INTERPOLATE_CONSTANT,
114 TRUE);
115
116 cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
117 }
118
119
120 /**
121 * Helper function to set the vertex shader.
122 */
123 static inline void
124 set_vertex_shader(struct st_context *st)
125 {
126 /* vertex shader - still required to provide the linkage between
127 * fragment shader input semantics and vertex_element/buffers.
128 */
129 if (!st->clear.vs)
130 {
131 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
132 TGSI_SEMANTIC_GENERIC };
133 const uint semantic_indexes[] = { 0, 0 };
134 st->clear.vs = util_make_vertex_passthrough_shader(st->pipe, 2,
135 semantic_names,
136 semantic_indexes,
137 FALSE);
138 }
139
140 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
141 cso_set_geometry_shader_handle(st->cso_context, NULL);
142 }
143
144
145 static void
146 set_vertex_shader_layered(struct st_context *st)
147 {
148 struct pipe_context *pipe = st->pipe;
149
150 if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID)) {
151 assert(!"Got layered clear, but VS instancing is unsupported");
152 set_vertex_shader(st);
153 return;
154 }
155
156 if (!st->clear.vs_layered) {
157 bool vs_layer =
158 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
159 if (vs_layer) {
160 st->clear.vs_layered = util_make_layered_clear_vertex_shader(pipe);
161 } else {
162 st->clear.vs_layered = util_make_layered_clear_helper_vertex_shader(pipe);
163 st->clear.gs_layered = util_make_layered_clear_geometry_shader(pipe);
164 }
165 }
166
167 cso_set_vertex_shader_handle(st->cso_context, st->clear.vs_layered);
168 cso_set_geometry_shader_handle(st->cso_context, st->clear.gs_layered);
169 }
170
171
172 /**
173 * Do glClear by drawing a quadrilateral.
174 * The vertices of the quad will be computed from the
175 * ctx->DrawBuffer->_X/Ymin/max fields.
176 */
177 static void
178 clear_with_quad(struct gl_context *ctx, unsigned clear_buffers)
179 {
180 struct st_context *st = st_context(ctx);
181 struct cso_context *cso = st->cso_context;
182 const struct gl_framebuffer *fb = ctx->DrawBuffer;
183 const GLfloat fb_width = (GLfloat) fb->Width;
184 const GLfloat fb_height = (GLfloat) fb->Height;
185
186 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
187
188 const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
189 const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
190 const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
191 const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
192 unsigned num_layers = st->state.fb_num_layers;
193
194 /*
195 printf("%s %s%s%s %f,%f %f,%f\n", __func__,
196 color ? "color, " : "",
197 depth ? "depth, " : "",
198 stencil ? "stencil" : "",
199 x0, y0,
200 x1, y1);
201 */
202
203 cso_save_state(cso, (CSO_BIT_BLEND |
204 CSO_BIT_STENCIL_REF |
205 CSO_BIT_DEPTH_STENCIL_ALPHA |
206 CSO_BIT_RASTERIZER |
207 CSO_BIT_SAMPLE_MASK |
208 CSO_BIT_MIN_SAMPLES |
209 CSO_BIT_VIEWPORT |
210 CSO_BIT_STREAM_OUTPUTS |
211 CSO_BIT_VERTEX_ELEMENTS |
212 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
213 CSO_BIT_PAUSE_QUERIES |
214 CSO_BITS_ALL_SHADERS));
215
216 /* blend state: RGBA masking */
217 {
218 struct pipe_blend_state blend;
219 memset(&blend, 0, sizeof(blend));
220 if (clear_buffers & PIPE_CLEAR_COLOR) {
221 int num_buffers = ctx->Extensions.EXT_draw_buffers2 ?
222 ctx->DrawBuffer->_NumColorDrawBuffers : 1;
223 int i;
224
225 blend.independent_blend_enable = num_buffers > 1;
226
227 for (i = 0; i < num_buffers; i++) {
228 if (!(clear_buffers & (PIPE_CLEAR_COLOR0 << i)))
229 continue;
230
231 blend.rt[i].colormask = GET_COLORMASK(ctx->Color.ColorMask, i);
232 }
233
234 if (ctx->Color.DitherFlag)
235 blend.dither = 1;
236 }
237 cso_set_blend(cso, &blend);
238 }
239
240 /* depth_stencil state: always pass/set to ref value */
241 {
242 struct pipe_depth_stencil_alpha_state depth_stencil;
243 memset(&depth_stencil, 0, sizeof(depth_stencil));
244 if (clear_buffers & PIPE_CLEAR_DEPTH) {
245 depth_stencil.depth.enabled = 1;
246 depth_stencil.depth.writemask = 1;
247 depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
248 }
249
250 if (clear_buffers & PIPE_CLEAR_STENCIL) {
251 struct pipe_stencil_ref stencil_ref;
252 memset(&stencil_ref, 0, sizeof(stencil_ref));
253 depth_stencil.stencil[0].enabled = 1;
254 depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
255 depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
256 depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
257 depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
258 depth_stencil.stencil[0].valuemask = 0xff;
259 depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
260 stencil_ref.ref_value[0] = ctx->Stencil.Clear;
261 cso_set_stencil_ref(cso, &stencil_ref);
262 }
263
264 cso_set_depth_stencil_alpha(cso, &depth_stencil);
265 }
266
267 cso_set_vertex_elements(cso, 2, st->util_velems);
268 cso_set_stream_outputs(cso, 0, NULL, NULL);
269 cso_set_sample_mask(cso, ~0);
270 cso_set_min_samples(cso, 1);
271 cso_set_rasterizer(cso, &st->clear.raster);
272
273 /* viewport state: viewport matching window dims */
274 cso_set_viewport_dims(st->cso_context, fb_width, fb_height,
275 st_fb_orientation(fb) == Y_0_TOP);
276
277 set_fragment_shader(st);
278 cso_set_tessctrl_shader_handle(cso, NULL);
279 cso_set_tesseval_shader_handle(cso, NULL);
280
281 if (num_layers > 1)
282 set_vertex_shader_layered(st);
283 else
284 set_vertex_shader(st);
285
286 /* draw quad matching scissor rect.
287 *
288 * Note: if we're only clearing depth/stencil we still setup vertices
289 * with color, but they'll be ignored.
290 *
291 * We can't translate the clear color to the colorbuffer format,
292 * because different colorbuffers may have different formats.
293 */
294 if (!st_draw_quad(st, x0, y0, x1, y1,
295 ctx->Depth.Clear * 2.0f - 1.0f,
296 0.0f, 0.0f, 0.0f, 0.0f,
297 (const float *) &ctx->Color.ClearColor.f,
298 num_layers)) {
299 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear");
300 }
301
302 /* Restore pipe state */
303 cso_restore_state(cso);
304 }
305
306
307 /**
308 * Return if the scissor must be enabled during the clear.
309 */
310 static inline GLboolean
311 is_scissor_enabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
312 {
313 const struct gl_scissor_rect *scissor = &ctx->Scissor.ScissorArray[0];
314
315 return (ctx->Scissor.EnableFlags & 1) &&
316 (scissor->X > 0 ||
317 scissor->Y > 0 ||
318 scissor->X + scissor->Width < (int)rb->Width ||
319 scissor->Y + scissor->Height < (int)rb->Height);
320 }
321
322 /**
323 * Return if window rectangles must be enabled during the clear.
324 */
325 static inline bool
326 is_window_rectangle_enabled(struct gl_context *ctx)
327 {
328 if (ctx->DrawBuffer == ctx->WinSysDrawBuffer)
329 return false;
330 return ctx->Scissor.NumWindowRects > 0 ||
331 ctx->Scissor.WindowRectMode == GL_INCLUSIVE_EXT;
332 }
333
334
335 /**
336 * Return if all of the stencil bits are masked.
337 */
338 static inline GLboolean
339 is_stencil_disabled(struct gl_context *ctx, struct gl_renderbuffer *rb)
340 {
341 const GLuint stencilMax = 0xff;
342
343 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
344 return (ctx->Stencil.WriteMask[0] & stencilMax) == 0;
345 }
346
347
348 /**
349 * Return if any of the stencil bits are masked.
350 */
351 static inline GLboolean
352 is_stencil_masked(struct gl_context *ctx, struct gl_renderbuffer *rb)
353 {
354 const GLuint stencilMax = 0xff;
355
356 assert(_mesa_get_format_bits(rb->Format, GL_STENCIL_BITS) > 0);
357 return (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
358 }
359
360
361 /**
362 * Called via ctx->Driver.Clear()
363 */
364 static void
365 st_Clear(struct gl_context *ctx, GLbitfield mask)
366 {
367 struct st_context *st = st_context(ctx);
368 struct gl_renderbuffer *depthRb
369 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
370 struct gl_renderbuffer *stencilRb
371 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
372 GLbitfield quad_buffers = 0x0;
373 GLbitfield clear_buffers = 0x0;
374 GLuint i;
375
376 st_flush_bitmap_cache(st);
377 st_invalidate_readpix_cache(st);
378
379 /* This makes sure the pipe has the latest scissor, etc values */
380 st_validate_state(st, ST_PIPELINE_CLEAR);
381
382 if (mask & BUFFER_BITS_COLOR) {
383 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
384 gl_buffer_index b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
385
386 if (b != BUFFER_NONE && mask & (1 << b)) {
387 struct gl_renderbuffer *rb
388 = ctx->DrawBuffer->Attachment[b].Renderbuffer;
389 struct st_renderbuffer *strb = st_renderbuffer(rb);
390 int colormask_index = ctx->Extensions.EXT_draw_buffers2 ? i : 0;
391
392 if (!strb || !strb->surface)
393 continue;
394
395 if (!GET_COLORMASK(ctx->Color.ColorMask, colormask_index))
396 continue;
397
398 if (is_scissor_enabled(ctx, rb) ||
399 is_window_rectangle_enabled(ctx) ||
400 GET_COLORMASK(ctx->Color.ColorMask, colormask_index) != 0xf)
401 quad_buffers |= PIPE_CLEAR_COLOR0 << i;
402 else
403 clear_buffers |= PIPE_CLEAR_COLOR0 << i;
404 }
405 }
406 }
407
408 if (mask & BUFFER_BIT_DEPTH) {
409 struct st_renderbuffer *strb = st_renderbuffer(depthRb);
410
411 if (strb->surface && ctx->Depth.Mask) {
412 if (is_scissor_enabled(ctx, depthRb) ||
413 is_window_rectangle_enabled(ctx))
414 quad_buffers |= PIPE_CLEAR_DEPTH;
415 else
416 clear_buffers |= PIPE_CLEAR_DEPTH;
417 }
418 }
419 if (mask & BUFFER_BIT_STENCIL) {
420 struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
421
422 if (strb->surface && !is_stencil_disabled(ctx, stencilRb)) {
423 if (is_scissor_enabled(ctx, stencilRb) ||
424 is_window_rectangle_enabled(ctx) ||
425 is_stencil_masked(ctx, stencilRb))
426 quad_buffers |= PIPE_CLEAR_STENCIL;
427 else
428 clear_buffers |= PIPE_CLEAR_STENCIL;
429 }
430 }
431
432 /* Always clear depth and stencil together.
433 * This can only happen when the stencil writemask is not a full mask.
434 */
435 if (quad_buffers & PIPE_CLEAR_DEPTHSTENCIL &&
436 clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
437 quad_buffers |= clear_buffers & PIPE_CLEAR_DEPTHSTENCIL;
438 clear_buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
439 }
440
441 /* Only use quad-based clearing for the renderbuffers which cannot
442 * use pipe->clear. We want to always use pipe->clear for the other
443 * renderbuffers, because it's likely to be faster.
444 */
445 if (quad_buffers) {
446 clear_with_quad(ctx, quad_buffers);
447 }
448 if (clear_buffers) {
449 /* We can't translate the clear color to the colorbuffer format,
450 * because different colorbuffers may have different formats.
451 */
452 st->pipe->clear(st->pipe, clear_buffers,
453 (union pipe_color_union*)&ctx->Color.ClearColor,
454 ctx->Depth.Clear, ctx->Stencil.Clear);
455 }
456 if (mask & BUFFER_BIT_ACCUM)
457 _mesa_clear_accum_buffer(ctx);
458 }
459
460
461 void
462 st_init_clear_functions(struct dd_function_table *functions)
463 {
464 functions->Clear = st_Clear;
465 }