OSDN Git Service

Merge branch '7.8'
[android-x86/external-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/formats.h"
38 #include "main/macros.h"
39 #include "shader/prog_instruction.h"
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_cb_accum.h"
43 #include "st_cb_clear.h"
44 #include "st_cb_fbo.h"
45 #include "st_program.h"
46 #include "st_public.h"
47 #include "st_inlines.h"
48
49 #include "pipe/p_context.h"
50 #include "util/u_inlines.h"
51 #include "pipe/p_state.h"
52 #include "pipe/p_defines.h"
53 #include "util/u_format.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 void
61 st_init_clear(struct st_context *st)
62 {
63    struct pipe_context *pipe = st->pipe;
64
65    memset(&st->clear, 0, sizeof(st->clear));
66
67    st->clear.raster.gl_rasterization_rules = 1;
68
69    /* fragment shader state: color pass-through program */
70    st->clear.fs =
71       util_make_fragment_passthrough_shader(pipe);
72
73    /* vertex shader state: color/position pass-through */
74    {
75       const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
76                                       TGSI_SEMANTIC_COLOR };
77       const uint semantic_indexes[] = { 0, 0 };
78       st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
79                                                          semantic_names,
80                                                          semantic_indexes);
81    }
82 }
83
84
85 void
86 st_destroy_clear(struct st_context *st)
87 {
88    if (st->clear.fs) {
89       cso_delete_fragment_shader(st->cso_context, st->clear.fs);
90       st->clear.fs = NULL;
91    }
92    if (st->clear.vs) {
93       cso_delete_vertex_shader(st->cso_context, st->clear.vs);
94       st->clear.vs = NULL;
95    }
96    if (st->clear.vbuf) {
97       pipe_buffer_reference(&st->clear.vbuf, NULL);
98       st->clear.vbuf = NULL;
99    }
100 }
101
102
103 /**
104  * Draw a screen-aligned quadrilateral.
105  * Coords are clip coords with y=0=bottom.
106  */
107 static void
108 draw_quad(GLcontext *ctx,
109           float x0, float y0, float x1, float y1, GLfloat z,
110           const GLfloat color[4])
111 {
112    struct st_context *st = ctx->st;
113    struct pipe_context *pipe = st->pipe;
114
115    /* XXX: Need to improve buffer_write to allow NO_WAIT (as well as
116     * no_flush) updates to buffers where we know there is no conflict
117     * with previous data.  Currently using max_slots > 1 will cause
118     * synchronous rendering if the driver flushes its command buffers
119     * between one bitmap and the next.  Our flush hook below isn't
120     * sufficient to catch this as the driver doesn't tell us when it
121     * flushes its own command buffers.  Until this gets fixed, pay the
122     * price of allocating a new buffer for each bitmap cache-flush to
123     * avoid synchronous rendering.
124     */
125    const GLuint max_slots = 1; /* 1024 / sizeof(st->clear.vertices); */
126    GLuint i;
127
128    if (st->clear.vbuf_slot >= max_slots) {
129       pipe_buffer_reference(&st->clear.vbuf, NULL);
130       st->clear.vbuf_slot = 0;
131    }
132
133    if (!st->clear.vbuf) {
134       st->clear.vbuf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
135                                           max_slots * sizeof(st->clear.vertices));
136    }
137
138    /* positions */
139    st->clear.vertices[0][0][0] = x0;
140    st->clear.vertices[0][0][1] = y0;
141
142    st->clear.vertices[1][0][0] = x1;
143    st->clear.vertices[1][0][1] = y0;
144
145    st->clear.vertices[2][0][0] = x1;
146    st->clear.vertices[2][0][1] = y1;
147
148    st->clear.vertices[3][0][0] = x0;
149    st->clear.vertices[3][0][1] = y1;
150
151    /* same for all verts: */
152    for (i = 0; i < 4; i++) {
153       st->clear.vertices[i][0][2] = z;
154       st->clear.vertices[i][0][3] = 1.0;
155       st->clear.vertices[i][1][0] = color[0];
156       st->clear.vertices[i][1][1] = color[1];
157       st->clear.vertices[i][1][2] = color[2];
158       st->clear.vertices[i][1][3] = color[3];
159    }
160
161    /* put vertex data into vbuf */
162    st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
163                                            st->clear.vbuf_slot * sizeof(st->clear.vertices),
164                                            sizeof(st->clear.vertices),
165                                            st->clear.vertices);
166
167    /* draw */
168    util_draw_vertex_buffer(pipe, 
169                            st->clear.vbuf, 
170                            st->clear.vbuf_slot * sizeof(st->clear.vertices),
171                            PIPE_PRIM_TRIANGLE_FAN,
172                            4,  /* verts */
173                            2); /* attribs/vert */
174
175    /* Increment slot */
176    st->clear.vbuf_slot++;
177 }
178
179
180
181 /**
182  * Do glClear by drawing a quadrilateral.
183  * The vertices of the quad will be computed from the
184  * ctx->DrawBuffer->_X/Ymin/max fields.
185  */
186 static void
187 clear_with_quad(GLcontext *ctx,
188                 GLboolean color, GLboolean depth, GLboolean stencil)
189 {
190    struct st_context *st = ctx->st;
191    const struct gl_framebuffer *fb = ctx->DrawBuffer;
192    const GLfloat fb_width = (GLfloat) fb->Width;
193    const GLfloat fb_height = (GLfloat) fb->Height;
194    const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin / fb_width * 2.0f - 1.0f;
195    const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax / fb_width * 2.0f - 1.0f;
196    const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin / fb_height * 2.0f - 1.0f;
197    const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax / fb_height * 2.0f - 1.0f;
198
199    /*
200    printf("%s %s%s%s %f,%f %f,%f\n", __FUNCTION__, 
201           color ? "color, " : "",
202           depth ? "depth, " : "",
203           stencil ? "stencil" : "",
204           x0, y0,
205           x1, y1);
206    */
207
208    cso_save_blend(st->cso_context);
209    cso_save_stencil_ref(st->cso_context);
210    cso_save_depth_stencil_alpha(st->cso_context);
211    cso_save_rasterizer(st->cso_context);
212    cso_save_viewport(st->cso_context);
213    cso_save_clip(st->cso_context);
214    cso_save_fragment_shader(st->cso_context);
215    cso_save_vertex_shader(st->cso_context);
216    cso_save_vertex_elements(st->cso_context);
217
218    /* blend state: RGBA masking */
219    {
220       struct pipe_blend_state blend;
221       memset(&blend, 0, sizeof(blend));
222       blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
223       blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
224       blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
225       blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
226       if (color) {
227          if (ctx->Color.ColorMask[0][0])
228             blend.rt[0].colormask |= PIPE_MASK_R;
229          if (ctx->Color.ColorMask[0][1])
230             blend.rt[0].colormask |= PIPE_MASK_G;
231          if (ctx->Color.ColorMask[0][2])
232             blend.rt[0].colormask |= PIPE_MASK_B;
233          if (ctx->Color.ColorMask[0][3])
234             blend.rt[0].colormask |= PIPE_MASK_A;
235          if (st->ctx->Color.DitherFlag)
236             blend.dither = 1;
237       }
238       cso_set_blend(st->cso_context, &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 (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 (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(st->cso_context, &stencil_ref);
263       }
264
265       cso_set_depth_stencil_alpha(st->cso_context, &depth_stencil);
266    }
267
268    cso_set_vertex_elements(st->cso_context, 2, st->velems_util_draw);
269
270    cso_set_rasterizer(st->cso_context, &st->clear.raster);
271
272    /* viewport state: viewport matching window dims */
273    {
274       const GLboolean invert = (st_fb_orientation(fb) == Y_0_TOP);
275       struct pipe_viewport_state vp;
276       vp.scale[0] = 0.5f * fb_width;
277       vp.scale[1] = fb_height * (invert ? -0.5f : 0.5f);
278       vp.scale[2] = 1.0f;
279       vp.scale[3] = 1.0f;
280       vp.translate[0] = 0.5f * fb_width;
281       vp.translate[1] = 0.5f * fb_height;
282       vp.translate[2] = 0.0f;
283       vp.translate[3] = 0.0f;
284       cso_set_viewport(st->cso_context, &vp);
285    }
286
287    cso_set_clip(st->cso_context, &st->clear.clip);
288    cso_set_fragment_shader_handle(st->cso_context, st->clear.fs);
289    cso_set_vertex_shader_handle(st->cso_context, st->clear.vs);
290
291    /* draw quad matching scissor rect (XXX verify coord round-off) */
292    draw_quad(ctx, x0, y0, x1, y1, (GLfloat) ctx->Depth.Clear, ctx->Color.ClearColor);
293
294    /* Restore pipe state */
295    cso_restore_blend(st->cso_context);
296    cso_restore_stencil_ref(st->cso_context);
297    cso_restore_depth_stencil_alpha(st->cso_context);
298    cso_restore_rasterizer(st->cso_context);
299    cso_restore_viewport(st->cso_context);
300    cso_restore_clip(st->cso_context);
301    cso_restore_fragment_shader(st->cso_context);
302    cso_restore_vertex_shader(st->cso_context);
303    cso_restore_vertex_elements(st->cso_context);
304
305 }
306
307
308 /**
309  * Determine if we need to clear the depth buffer by drawing a quad.
310  */
311 static INLINE GLboolean
312 check_clear_color_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
313 {
314    if (ctx->Scissor.Enabled &&
315        (ctx->Scissor.X != 0 ||
316         ctx->Scissor.Y != 0 ||
317         ctx->Scissor.Width < rb->Width ||
318         ctx->Scissor.Height < rb->Height))
319       return TRUE;
320
321    if (!ctx->Color.ColorMask[0][0] ||
322        !ctx->Color.ColorMask[0][1] ||
323        !ctx->Color.ColorMask[0][2] ||
324        !ctx->Color.ColorMask[0][3])
325       return TRUE;
326
327    return FALSE;
328 }
329
330
331 static INLINE GLboolean
332 check_clear_depth_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
333 {
334    const GLuint stencilMax = 0xff;
335    GLboolean maskStencil
336       = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
337
338    assert(rb->Format == MESA_FORMAT_S8 ||
339           rb->Format == MESA_FORMAT_Z24_S8 ||
340           rb->Format == MESA_FORMAT_S8_Z24);
341
342    if (ctx->Scissor.Enabled &&
343        (ctx->Scissor.X != 0 ||
344         ctx->Scissor.Y != 0 ||
345         ctx->Scissor.Width < rb->Width ||
346         ctx->Scissor.Height < rb->Height))
347       return TRUE;
348
349    if (maskStencil)
350       return TRUE;
351
352    return FALSE;
353 }
354
355
356 /**
357  * Determine if we need to clear the depth buffer by drawing a quad.
358  */
359 static INLINE GLboolean
360 check_clear_depth_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
361 {
362    const struct st_renderbuffer *strb = st_renderbuffer(rb);
363    const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
364
365    if (ctx->Scissor.Enabled &&
366        (ctx->Scissor.X != 0 ||
367         ctx->Scissor.Y != 0 ||
368         ctx->Scissor.Width < rb->Width ||
369         ctx->Scissor.Height < rb->Height))
370       return TRUE;
371
372    if (isDS && 
373        ctx->DrawBuffer->Visual.stencilBits > 0)
374       return TRUE;
375
376    return FALSE;
377 }
378
379
380 /**
381  * Determine if we need to clear the stencil buffer by drawing a quad.
382  */
383 static INLINE GLboolean
384 check_clear_stencil_with_quad(GLcontext *ctx, struct gl_renderbuffer *rb)
385 {
386    const struct st_renderbuffer *strb = st_renderbuffer(rb);
387    const GLboolean isDS = util_format_is_depth_and_stencil(strb->surface->format);
388    const GLuint stencilMax = 0xff;
389    const GLboolean maskStencil
390       = (ctx->Stencil.WriteMask[0] & stencilMax) != stencilMax;
391
392    assert(rb->Format == MESA_FORMAT_S8 ||
393           rb->Format == MESA_FORMAT_Z24_S8 ||
394           rb->Format == MESA_FORMAT_S8_Z24);
395
396    if (maskStencil) 
397       return TRUE;
398
399    if (ctx->Scissor.Enabled &&
400        (ctx->Scissor.X != 0 ||
401         ctx->Scissor.Y != 0 ||
402         ctx->Scissor.Width < rb->Width ||
403         ctx->Scissor.Height < rb->Height))
404       return TRUE;
405
406    /* This is correct, but it is necessary to look at the depth clear
407     * value held in the surface when it comes time to issue the clear,
408     * rather than taking depth and stencil clear values from the
409     * current state.
410     */
411    if (isDS && 
412        ctx->DrawBuffer->Visual.depthBits > 0)
413       return TRUE;
414
415    return FALSE;
416 }
417
418
419
420 void st_flush_clear( struct st_context *st )
421 {
422    /* Release vertex buffer to avoid synchronous rendering if we were
423     * to map it in the next frame.
424     */
425    pipe_buffer_reference(&st->clear.vbuf, NULL);
426    st->clear.vbuf_slot = 0;
427 }
428  
429
430
431 /**
432  * Called via ctx->Driver.Clear()
433  * XXX: doesn't pick up the differences between front/back/left/right
434  * clears.  Need to sort that out...
435  */
436 static void st_clear(GLcontext *ctx, GLbitfield mask)
437 {
438    static const GLbitfield BUFFER_BITS_DS
439       = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
440    struct st_context *st = ctx->st;
441    struct gl_renderbuffer *depthRb
442       = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
443    struct gl_renderbuffer *stencilRb
444       = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
445    GLbitfield quad_buffers = 0;
446    GLbitfield clear_buffers = 0;
447    GLuint i;
448
449    /* This makes sure the pipe has the latest scissor, etc values */
450    st_validate_state( st );
451
452    if (mask & BUFFER_BITS_COLOR) {
453       for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
454          GLuint b = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
455
456          if (mask & (1 << b)) {
457             struct gl_renderbuffer *rb
458                = ctx->DrawBuffer->Attachment[b].Renderbuffer;
459             struct st_renderbuffer *strb;
460
461             assert(rb);
462
463             strb = st_renderbuffer(rb);
464
465             if (!strb->surface)
466                continue;
467
468             if (check_clear_color_with_quad( ctx, rb ))
469                quad_buffers |= PIPE_CLEAR_COLOR;
470             else
471                clear_buffers |= PIPE_CLEAR_COLOR;
472          }
473       }
474    }
475
476    if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
477       /* clearing combined depth + stencil */
478       struct st_renderbuffer *strb = st_renderbuffer(depthRb);
479
480       if (strb->surface) {
481          if (check_clear_depth_stencil_with_quad(ctx, depthRb))
482             quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
483          else
484             clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
485       }
486    }
487    else {
488       /* separate depth/stencil clears */
489       if (mask & BUFFER_BIT_DEPTH) {
490          struct st_renderbuffer *strb = st_renderbuffer(depthRb);
491
492          if (strb->surface) {
493             if (check_clear_depth_with_quad(ctx, depthRb))
494                quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
495             else
496                clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
497          }
498       }
499       if (mask & BUFFER_BIT_STENCIL) {
500          struct st_renderbuffer *strb = st_renderbuffer(stencilRb);
501
502          if (strb->surface) {
503             if (check_clear_stencil_with_quad(ctx, stencilRb))
504                quad_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
505             else
506                clear_buffers |= PIPE_CLEAR_DEPTHSTENCIL;
507          }
508       }
509    }
510
511    /*
512     * If we're going to use clear_with_quad() for any reason, use it for
513     * everything possible.
514     */
515    if (quad_buffers) {
516       quad_buffers |= clear_buffers;
517       clear_with_quad(ctx,
518                       quad_buffers & PIPE_CLEAR_COLOR,
519                       mask & BUFFER_BIT_DEPTH,
520                       mask & BUFFER_BIT_STENCIL);
521    } else if (clear_buffers)
522       ctx->st->pipe->clear(ctx->st->pipe, clear_buffers, ctx->Color.ClearColor,
523                            ctx->Depth.Clear, ctx->Stencil.Clear);
524
525    if (mask & BUFFER_BIT_ACCUM)
526       st_clear_accum_buffer(ctx,
527                             ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
528 }
529
530
531 void st_init_clear_functions(struct dd_function_table *functions)
532 {
533    functions->Clear = st_clear;
534 }