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