Implement new draw_vertices() path for simple vertex array drawing, use it for glClear.
[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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "glheader.h"
35 #include "macros.h"
36 #include "enums.h"
37 #include "st_atom.h"
38 #include "st_context.h"
39 #include "st_cb_clear.h"
40 #include "st_public.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "vf/vf.h"
44
45
46 /**
47 * Draw a screen-aligned quadrilateral.
48 * Coords are window coords.
49 */
50 static void
51 draw_quad(GLcontext *ctx,
52 float x0, float y0, float x1, float y1, GLfloat z,
53 const GLfloat color[4])
54 {
55 static const GLuint attribs[2] = {
56 VF_ATTRIB_POS,
57 VF_ATTRIB_COLOR0
58 };
59 GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
60 GLuint i;
61
62 /* positions */
63 verts[0][0][0] = x0;
64 verts[0][0][1] = y0;
65
66 verts[1][0][0] = x1;
67 verts[1][0][1] = y0;
68
69 verts[2][0][0] = x1;
70 verts[2][0][1] = y1;
71
72 verts[3][0][0] = x0;
73 verts[3][0][1] = y1;
74
75 /* same for all verts: */
76 for (i = 0; i < 4; i++) {
77 verts[i][0][2] = z;
78 verts[i][0][3] = 1.0;
79 verts[i][1][0] = color[0];
80 verts[i][1][1] = color[1];
81 verts[i][1][2] = color[2];
82 verts[i][1][3] = color[3];
83 }
84
85 ctx->st->pipe->draw_vertices(ctx->st->pipe, GL_QUADS,
86 4, (GLfloat *) verts, 2, attribs);
87 }
88
89
90
91 /**
92 * Do glClear by drawing a quadrilateral.
93 */
94 static void
95 clear_with_quad(GLcontext *ctx,
96 GLboolean color, GLboolean depth, GLboolean stencil)
97 {
98 struct st_context *st = ctx->st;
99 struct pipe_blend_state blend;
100 struct pipe_depth_state depth_test;
101 struct pipe_stencil_state stencil_test;
102
103 /* depth state: always pass */
104 memset(&depth_test, 0, sizeof(depth));
105 if (depth) {
106 depth_test.enabled = 1;
107 depth_test.writemask = 1;
108 depth_test.func = PIPE_FUNC_ALWAYS;
109 }
110 st->pipe->set_depth_state(st->pipe, &depth_test);
111
112 /* stencil state: always set to ref value */
113 memset(&stencil_test, 0, sizeof(stencil));
114 if (stencil) {
115 stencil_test.front_enabled = 1;
116 stencil_test.front_func = PIPE_FUNC_ALWAYS;
117 stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
118 stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
119 stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
120 stencil_test.ref_value[0] = ctx->Stencil.Clear;
121 stencil_test.value_mask[0] = 0xff;
122 stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0];
123 }
124 st->pipe->set_stencil_state(st->pipe, &stencil_test);
125
126 /* blend state: RGBA masking */
127 memset(&blend, 0, sizeof(blend));
128 if (color) {
129 if (ctx->Color.ColorMask[0])
130 blend.colormask |= PIPE_MASK_R;
131 if (ctx->Color.ColorMask[1])
132 blend.colormask |= PIPE_MASK_G;
133 if (ctx->Color.ColorMask[2])
134 blend.colormask |= PIPE_MASK_B;
135 if (ctx->Color.ColorMask[3])
136 blend.colormask |= PIPE_MASK_A;
137 if (st->ctx->Color.DitherFlag)
138 blend.dither = 1;
139 }
140 st->pipe->set_blend_state(st->pipe, &blend);
141
142 draw_quad(ctx,
143 ctx->Scissor.X, ctx->Scissor.Y,
144 ctx->Scissor.X + ctx->Scissor.Width,
145 ctx->Scissor.Y + ctx->Scissor.Height,
146 ctx->Depth.Clear, ctx->Color.ClearColor);
147
148 /* Restore GL state */
149 st->pipe->set_blend_state(st->pipe, &st->state.blend);
150 st->pipe->set_depth_state(st->pipe, &st->state.depth);
151 st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
152 /* OR:
153 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
154 */
155 }
156
157
158
159 /**
160 * Called via ctx->Driver.Clear()
161 * XXX: doesn't pick up the differences between front/back/left/right
162 * clears. Need to sort that out...
163 */
164 static void st_clear(GLcontext *ctx, GLbitfield mask)
165 {
166 struct st_context *st = ctx->st;
167 GLboolean color = (mask & BUFFER_BITS_COLOR) ? GL_TRUE : GL_FALSE;
168 GLboolean depth = (mask & BUFFER_BIT_DEPTH) ? GL_TRUE : GL_FALSE;
169 GLboolean stencil = (mask & BUFFER_BIT_STENCIL) ? GL_TRUE : GL_FALSE;
170 GLboolean accum = (mask & BUFFER_BIT_ACCUM) ? GL_TRUE : GL_FALSE;
171
172 GLboolean maskColor, maskStencil;
173 GLboolean fullscreen = !ctx->Scissor.Enabled;
174 GLuint stencilMax = stencil ? (1 << ctx->DrawBuffer->_StencilBuffer->StencilBits) : 0;
175
176 /* This makes sure the softpipe has the latest scissor, etc values */
177 st_validate_state( st );
178
179 maskColor = color && st->state.blend.colormask != PIPE_MASK_RGBA;
180 maskStencil = stencil && ctx->Stencil.WriteMask[0] != stencilMax;
181
182 if (fullscreen && !maskColor && !maskStencil) {
183 /* pipe->clear() should clear a particular surface, so that we
184 * can iterate over render buffers at this level and clear the
185 * ones GL is asking for.
186 *
187 * Will probably need something like pipe->clear_z_stencil() to
188 * cope with the special case of paired and unpaired z/stencil
189 * buffers, though could perhaps deal with them explicitly at
190 * this level.
191 */
192 st->pipe->clear(st->pipe, color, depth, stencil);
193
194 /* And here we would do a clear on whatever surface we are using
195 * to implement accum buffers:
196 */
197 assert(!accum);
198 }
199 else {
200 clear_with_quad(ctx, color, depth, stencil);
201 }
202 }
203
204
205 void st_init_cb_clear( struct st_context *st )
206 {
207 struct dd_function_table *functions = &st->ctx->Driver;
208
209 functions->Clear = st_clear;
210 }
211
212
213 void st_destroy_cb_clear( struct st_context *st )
214 {
215 }
216