Merge branch 'master' into asm-shader-rework-1
[profile/ivi/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/macros.h"
38 #include "shader/prog_instruction.h"
39 #include "st_context.h"
40 #include "st_atom.h"
41 #include "st_cb_accum.h"
42 #include "st_cb_clear.h"
43 #include "st_cb_fbo.h"
44 #include "st_draw.h"
45 #include "st_program.h"
46 #include "st_public.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "st_inlines.h"
49
50 #include "pipe/p_context.h"
51 #include "pipe/p_inlines.h"
52 #include "pipe/p_state.h"
53 #include "pipe/p_defines.h"
54 #include "util/u_pack_color.h"
55 #include "util/u_simple_shaders.h"
56 #include "util/u_draw_quad.h"
57
58 #include "cso_cache/cso_context.h"
59
60
61 void
62 st_init_clear(struct st_context *st)
63 {
64    struct pipe_context *pipe = st->pipe;
65
66    memset(&st->clear.raster, 0, sizeof(st->clear.raster));
67    st->clear.raster.gl_rasterization_rules = 1;
68
69    /* rasterizer state: bypass vertex shader, clipping and viewport */
70    st->clear.raster.bypass_vs_clip_and_viewport = 1;
71
72    /* fragment shader state: color pass-through program */
73    st->clear.fs =
74       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 void
89 st_destroy_clear(struct st_context *st)
90 {
91    if (st->clear.fs) {
92       cso_delete_fragment_shader(st->cso_context, st->clear.fs);
93       st->clear.fs = NULL;
94    }
95    if (st->clear.vs) {
96       cso_delete_vertex_shader(st->cso_context, st->clear.vs);
97       st->clear.vs = NULL;
98    }
99    if (st->clear.vbuf) {
100       pipe_buffer_reference(&st->clear.vbuf, NULL);
101       st->clear.vbuf = NULL;
102    }
103 }
104
105
106 /**
107  * Draw a screen-aligned quadrilateral.
108  * Coords are window coords with y=0=bottom.  These will be passed
109  * through unmodified to the rasterizer as we have set
110  * rasterizer->bypass_vs_clip_and_viewport.
111  */
112 static void
113 draw_quad(GLcontext *ctx,
114           float x0, float y0, float x1, float y1, GLfloat z,
115           const GLfloat color[4])
116 {
117    struct st_context *st = ctx->st;
118    struct pipe_context *pipe = st->pipe;
119    const GLuint max_slots = 1024 / sizeof(st->clear.vertices);
120    GLuint i;
121
122    if (st->clear.vbuf_slot >= max_slots) {
123       pipe_buffer_reference(&st->clear.vbuf, NULL);
124       st->clear.vbuf_slot = 0;
125    }
126
127    if (!st->clear.vbuf) {
128       st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
129                                           max_slots * sizeof(st->clear.vertices));
130    }
131
132    /* positions */
133    st->clear.vertices[0][0][0] = x0;
134    st->clear.vertices[0][0][1] = y0;
135
136    st->clear.vertices[1][0][0] = x1;
137    st->clear.vertices[1][0][1] = y0;
138
139    st->clear.vertices[2][0][0] = x1;
140    st->clear.vertices[2][0][1] = y1;
141
142    st->clear.vertices[3][0][0] = x0;
143    st->clear.vertices[3][0][1] = y1;
144
145    /* same for all verts: */
146    for (i = 0; i < 4; i++) {
147       st->clear.vertices[i][0][2] = z;
148       st->clear.vertices[i][0][3] = 1.0;
149       st->clear.vertices[i][1][0] = color[0];
150       st->clear.vertices[i][1][1] = color[1];
151       st->clear.vertices[i][1][2] = color[2];
152       st->clear.vertices[i][1][3] = color[3];
153    }
154
155    /* put vertex data into vbuf */
156    st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
157                                  st->clear.vbuf_slot * sizeof(st->clear.vertices),
158                                  sizeof(st->clear.vertices),
159                                  st->clear.vertices);
160
161    /* draw */
162    util_draw_vertex_buffer(pipe, 
163                            st->clear.vbuf, 
164                            st->clear.vbuf_slot * sizeof(st->clear.vertices),
165                            PIPE_PRIM_TRIANGLE_FAN,
166                            4,  /* verts */
167                            2); /* attribs/vert */
168
169    /* Increment slot */
170    st->clear.vbuf_slot++;
171 }
172
173
174
175 /**
176  * Do glClear by drawing a quadrilateral.
177  * The vertices of the quad will be computed from the
178  * ctx->DrawBuffer->_X/Ymin/max fields.
179  */
180 static void
181 clear_with_quad(GLcontext *ctx,
182                 GLboolean color, GLboolean depth, GLboolean stencil)
183 {
184    struct st_context *st = ctx->st;
185    const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
186    const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
187    GLfloat y0, y1;
188
189    if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
190       y0 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax);
191       y1 = (GLfloat) (ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin);
192    }
193    else {
194       y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
195       y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
196    }
197
198    /*
199    printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 
200           color ? "color, " : "",
201           depth ? "depth, " : "",
202           stencil ? "stencil" : "",
203           x0, y0,
204           x1, y1);
205    */
206
207    cso_save_blend(st->cso_context);
208    cso_save_depth_stencil_alpha(st->cso_context);
209    cso_save_rasterizer(st->cso_context);
210    cso_save_fragment_shader(st->cso_context);
211    cso_save_vertex_shader(st->cso_context);
212
213    /* blend state: RGBA masking */
214    {
215       struct pipe_blend_state blend;
216       memset(&blend, 0, sizeof(blend));
217       blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
218       blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
219       blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
220       blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
221       if (color) {
222          if (ctx->Color.ColorMask[0])
223             blend.colormask |= PIPE_MASK_R;
224          if (ctx->Color.ColorMask[1])
225             blend.colormask |= PIPE_MASK_G;
226          if (ctx->Color.ColorMask[2])
227             blend.colormask |= PIPE_MASK_B;
228          if (ctx->Color.ColorMask[3])
229             blend.colormask |= PIPE_MASK_A;
230          if (st->ctx->Color.DitherFlag)
231             blend.dither = 1;
232       }
233       cso_set_blend(st->cso_context, &blend);
234    }
235
236    /* depth_stencil state: always pass/set to ref value */
237    {
238       struct pipe_depth_stencil_alpha_state depth_stencil;
239       memset(&depth_stencil, 0, sizeof(depth_stencil));
240       if (depth) {
241          depth_stencil.depth.enabled = 1;
242          depth_stencil.depth.writemask = 1;
243          depth_stencil.depth.func = PIPE_FUNC_ALWAYS;
244       }
245
246       if (stencil) {
247          depth_stencil.stencil[0].enabled = 1;
248          depth_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
249          depth_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
250          depth_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
251          depth_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
252          depth_stencil.stencil[0].ref_value = ctx->Stencil.Clear;
253          depth_stencil.stencil[0].valuemask = 0xff;
254          depth_stencil.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
255       }
256
257       cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
258    }
259
260    cso_set_rasterizer(st->cso_context, &st->clear.raster);
261
262    cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
263    cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
264
265    /* draw quad matching scissor rect (XXX verify coord round-off) */
266    draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
267
268    /* Restore pipe state */
269    cso_restore_blend(st->cso_context);
270    cso_restore_depth_stencil_alpha(st->cso_context);
271    cso_restore_rasterizer(st->cso_context);
272    cso_restore_fragment_shader(st->cso_context);
273    cso_restore_vertex_shader(st->cso_context);
274 }
275
276
277 /**
278  * Determine if we need to clear the depth buffer by drawing a quad.
279  */
280 static INLINE GLboolean
281 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
282 {
283    if (ctx->Scissor.Enabled &&
284        (ctx->Scissor.X != 0 ||
285         ctx->Scissor.Y != 0 ||
286         ctx->Scissor.Width < rb->Width ||
287         ctx->Scissor.Height < rb->Height))
288       return TRUE;
289
290    if (!ctx->Color.ColorMask[0] ||
291        !ctx->Color.ColorMask[1] ||
292        !ctx->Color.ColorMask[2] ||
293        !ctx->Color.ColorMask[3])
294       return TRUE;
295
296    return FALSE;
297 }
298
299
300 static INLINE GLboolean
301 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
302 {
303    const GLuint stencilMax = (1 << rb->StencilBits) - 1;
304    GLboolean maskStencil
305       = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
306
307    if (ctx->Scissor.Enabled &&
308        (ctx->Scissor.X != 0 ||
309         ctx->Scissor.Y != 0 ||
310         ctx->Scissor.Width < rb->Width ||
311         ctx->Scissor.Height < rb->Height))
312       return TRUE;
313
314    if (maskStencil)
315       return TRUE;
316
317    return FALSE;
318 }
319
320
321 /**
322  * Determine if we need to clear the depth buffer by drawing a quad.
323  */
324 static INLINE GLboolean
325 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
326 {
327    const struct st_renderbuffer *strb = st_renderbuffer(rb);
328    const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
329
330    if (ctx->Scissor.Enabled &&
331        (ctx->Scissor.X != 0 ||
332         ctx->Scissor.Y != 0 ||
333         ctx->Scissor.Width < rb->Width ||
334         ctx->Scissor.Height < rb->Height))
335       return TRUE;
336
337    if (isDS && 
338        ctx->DrawBuffer->Visual.stencilBits > 0)
339       return TRUE;
340
341    return FALSE;
342 }
343
344
345 /**
346  * Determine if we need to clear the stencil buffer by drawing a quad.
347  */
348 static INLINE GLboolean
349 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
350 {
351    const struct st_renderbuffer *strb = st_renderbuffer(rb);
352    const GLboolean isDS = pf_is_depth_and_stencil(strb->surface->format);
353    const GLuint stencilMax = (1 << rb->StencilBits) - 1;
354    const GLboolean maskStencil
355       = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
356
357    if (maskStencil) 
358       return TRUE;
359
360    if (ctx->Scissor.Enabled &&
361        (ctx->Scissor.X != 0 ||
362         ctx->Scissor.Y != 0 ||
363         ctx->Scissor.Width < rb->Width ||
364         ctx->Scissor.Height < rb->Height))
365       return TRUE;
366
367    /* This is correct, but it is necessary to look at the depth clear
368     * value held in the surface when it comes time to issue the clear,
369     * rather than taking depth and stencil clear values from the
370     * current state.
371     */
372    if (isDS && 
373        ctx->DrawBuffer->Visual.depthBits > 0)
374       return TRUE;
375
376    return FALSE;
377 }
378
379
380
381 void st_flush_clear( struct st_context *st )
382 {
383    /* Release vertex buffer to avoid synchronous rendering if we were
384     * to map it in the next frame.
385     */
386    pipe_buffer_reference(&st->clear.vbuf, NULL);
387    st->clear.vbuf_slot = 0;
388 }
389  
390
391
392 /**
393  * Called via ctx->Driver.Clear()
394  * XXX: doesn't pick up the differences between front/back/left/right
395  * clears.  Need to sort that out...
396  */
397 static void st_clear(GLcontext *ctx, GLbitfield mask)
398 {
399    static const GLbitfield BUFFER_BITS_DS
400       = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
401    struct st_context *st = ctx->st;
402    struct gl_renderbuffer *depthRb
403       = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
404    struct gl_renderbuffer *stencilRb
405       = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
406    GLbitfield quad_buffers = 0;
407    GLbitfield clear_buffers = 0;
408    GLuint i;
409
410    /* This makes sure the pipe has the latest scissor, etc values */
411    st_validate_state( st );
412
413    if (mask & BUFFER_BITS_COLOR) {
414       for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
415          GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
416
417          if (mask & (1 << b)) {
418             struct gl_renderbuffer *rb
419                = ctx->DrawBuffer->Attachment[b].Renderbuffer;
420             struct st_renderbuffer *strb;
421
422             assert(rb);
423
424             strb = st_renderbuffer(rb);
425
426             if (!strb->surface)
427                continue;
428
429             if (check_clear_color_with_quad( ctx, rb ))
430                quad_buffers |= PIPE_CLEAR_COLOR;
431             else
432                clear_buffers |= PIPE_CLEAR_COLOR;
433          }
434       }
435    }
436
437    if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
438       /* clearing combined depth + stencil */
439       struct st_renderbuffer *strb = st_renderbuffer(depthRb);
440
441       if (strb->surface) {
442          if (check_clear_depth_stencil_with_quad(ctx, depthRb))
443             quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
444          else
445             clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
446       }
447    }
448    else {
449       /* separate depth/stencil clears */
450       if (mask & BUFFER_BIT_DEPTH) {
451          struct st_renderbuffer *strb = st_renderbuffer(depthRb);
452
453          if (strb->surface) {
454             if (check_clear_depth_with_quad(ctx, depthRb))
455                quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
456             else
457                clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
458          }
459       }
460       if (mask & BUFFER_BIT_STENCIL) {
461          struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
462
463          if (strb->surface) {
464             if (check_clear_stencil_with_quad(ctx, stencilRb))
465                quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
466             else
467                clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
468          }
469       }
470    }
471
472    /*
473     * If we're going to use clear_with_quad() for any reason, use it for
474     * everything possible.
475     */
476    if (quad_buffers) {
477       quad_buffers |= clear_buffers;
478       clear_with_quad(ctx,
479                       quad_buffers & PIPE_CLEAR_COLOR,
480                       mask & BUFFER_BIT_DEPTH,
481                       mask & BUFFER_BIT_STENCIL);
482    } else if (clear_buffers)
483       ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
484                            ctx->Depth.Clear, ctx->Stencil.Clear);
485
486    if (mask & BUFFER_BIT_ACCUM)
487       st_clear_accum_buffer(ctx,
488                             ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
489 }
490
491
492 void st_init_clear_functions(struct dd_function_table *functions)
493 {
494    functions->Clear = st_clear;
495 }