trim #includes
[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 "st_atom.h"
37 #include "st_context.h"
38 #include "st_cb_clear.h"
39 #include "st_public.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "vf/vf.h"
43
44
45
46 static GLuint
47 color_value(GLuint pipeFormat, const GLfloat color[4])
48 {
49 GLubyte r, g, b, a;
50
51 UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
52 UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
53 UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
54 UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);
55
56 switch (pipeFormat) {
57 case PIPE_FORMAT_U_R8_G8_B8_A8:
58 return (r << 24) | (g << 16) | (b << 8) | a;
59 case PIPE_FORMAT_U_A8_R8_G8_B8:
60 return (a << 24) | (r << 16) | (g << 8) | b;
61 case PIPE_FORMAT_U_R5_G6_B5:
62 return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
63 default:
64 return 0;
65 }
66 }
67
68
69 static GLuint
70 depth_value(GLuint pipeFormat, GLfloat value)
71 {
72 GLuint val;
73 switch (pipeFormat) {
74 case PIPE_FORMAT_U_Z16:
75 val = (GLuint) (value * 0xffffff);
76 break;
77 case PIPE_FORMAT_U_Z32:
78 val = (GLuint) (value * 0xffffffff);
79 break;
80 case PIPE_FORMAT_S8_Z24:
81 /*case PIPE_FORMAT_Z24_S8:*/
82 val = (GLuint) (value * 0xffffff);
83 break;
84 default:
85 assert(0);
86 }
87 return val;
88 }
89
90
91 static GLboolean
92 is_depth_stencil_format(GLuint pipeFormat)
93 {
94 switch (pipeFormat) {
95 case PIPE_FORMAT_S8_Z24:
96 /*case PIPE_FORMAT_Z24_S8:*/
97 return GL_TRUE;
98 default:
99 return GL_FALSE;
100 }
101 }
102
103
104 /**
105 * Draw a screen-aligned quadrilateral.
106 * Coords are window coords.
107 */
108 static void
109 draw_quad(GLcontext *ctx,
110 float x0, float y0, float x1, float y1, GLfloat z,
111 const GLfloat color[4])
112 {
113 static const GLuint attribs[2] = {
114 VF_ATTRIB_POS,
115 VF_ATTRIB_COLOR0
116 };
117 GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
118 GLuint i;
119
120 /* positions */
121 verts[0][0][0] = x0;
122 verts[0][0][1] = y0;
123
124 verts[1][0][0] = x1;
125 verts[1][0][1] = y0;
126
127 verts[2][0][0] = x1;
128 verts[2][0][1] = y1;
129
130 verts[3][0][0] = x0;
131 verts[3][0][1] = y1;
132
133 /* same for all verts: */
134 for (i = 0; i < 4; i++) {
135 verts[i][0][2] = z;
136 verts[i][0][3] = 1.0;
137 verts[i][1][0] = color[0];
138 verts[i][1][1] = color[1];
139 verts[i][1][2] = color[2];
140 verts[i][1][3] = color[3];
141 }
142
143 ctx->st->pipe->draw_vertices(ctx->st->pipe, GL_QUADS,
144 4, (GLfloat *) verts, 2, attribs);
145 }
146
147
148
149 /**
150 * Do glClear by drawing a quadrilateral.
151 */
152 static void
153 clear_with_quad(GLcontext *ctx, GLuint x0, GLuint y0,
154 GLuint x1, GLuint y1,
155 GLboolean color, GLboolean depth, GLboolean stencil)
156 {
157 struct st_context *st = ctx->st;
158 struct pipe_alpha_test_state alpha_test;
159 struct pipe_blend_state blend;
160 struct pipe_depth_state depth_test;
161 struct pipe_stencil_state stencil_test;
162 struct pipe_setup_state setup;
163
164 /* alpha state: disabled */
165 memset(&alpha_test, 0, sizeof(alpha_test));
166 st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
167
168 /* blend state: RGBA masking */
169 memset(&blend, 0, sizeof(blend));
170 if (color) {
171 if (ctx->Color.ColorMask[0])
172 blend.colormask |= PIPE_MASK_R;
173 if (ctx->Color.ColorMask[1])
174 blend.colormask |= PIPE_MASK_G;
175 if (ctx->Color.ColorMask[2])
176 blend.colormask |= PIPE_MASK_B;
177 if (ctx->Color.ColorMask[3])
178 blend.colormask |= PIPE_MASK_A;
179 if (st->ctx->Color.DitherFlag)
180 blend.dither = 1;
181 }
182 st->pipe->set_blend_state(st->pipe, &blend);
183
184 /* depth state: always pass */
185 memset(&depth_test, 0, sizeof(depth_test));
186 if (depth) {
187 depth_test.enabled = 1;
188 depth_test.writemask = 1;
189 depth_test.func = PIPE_FUNC_ALWAYS;
190 }
191 st->pipe->set_depth_state(st->pipe, &depth_test);
192
193 /* setup state: nothing */
194 memset(&setup, 0, sizeof(setup));
195 if (ctx->Scissor.Enabled)
196 setup.scissor = 1;
197 st->pipe->set_setup_state(st->pipe, &setup);
198
199 /* stencil state: always set to ref value */
200 memset(&stencil_test, 0, sizeof(stencil_test));
201 if (stencil) {
202 stencil_test.front_enabled = 1;
203 stencil_test.front_func = PIPE_FUNC_ALWAYS;
204 stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
205 stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
206 stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
207 stencil_test.ref_value[0] = ctx->Stencil.Clear;
208 stencil_test.value_mask[0] = 0xff;
209 stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
210 }
211 st->pipe->set_stencil_state(st->pipe, &stencil_test);
212
213 /* draw quad matching scissor rect (XXX verify coord round-off) */
214 draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);
215
216 /* Restore pipe state */
217 st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
218 st->pipe->set_blend_state(st->pipe, &st->state.blend);
219 st->pipe->set_depth_state(st->pipe, &st->state.depth);
220 st->pipe->set_setup_state(st->pipe, &st->state.setup);
221 st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
222 /* OR:
223 st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
224 */
225 }
226
227
228 static void
229 clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
230 {
231 if (ctx->Color.ColorMask[0] &&
232 ctx->Color.ColorMask[1] &&
233 ctx->Color.ColorMask[2] &&
234 ctx->Color.ColorMask[3] &&
235 !ctx->Scissor.Enabled)
236 {
237 /* clear whole buffer w/out masking */
238 GLuint clearValue
239 = color_value(rb->surface->format, ctx->Color.ClearColor);
240 ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
241 }
242 else {
243 /* masking or scissoring */
244 clear_with_quad(ctx,
245 ctx->DrawBuffer->_Xmin,
246 ctx->DrawBuffer->_Xmin,
247 ctx->DrawBuffer->_Xmax,
248 ctx->DrawBuffer->_Ymax,
249 GL_TRUE, GL_FALSE, GL_FALSE);
250 }
251 }
252
253
254 static void
255 clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
256 {
257 if (!ctx->Scissor.Enabled) {
258 /* clear whole buffer w/out masking */
259 GLuint clearValue
260 = color_value(rb->surface->format, ctx->Accum.ClearColor);
261 /* Note that clearValue is 32 bits but the accum buffer will
262 * typically be 64bpp...
263 */
264 ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
265 }
266 else {
267 /* scissoring */
268 /* XXX point framebuffer.cbufs[0] at the accum buffer */
269 clear_with_quad(ctx,
270 ctx->DrawBuffer->_Xmin,
271 ctx->DrawBuffer->_Xmin,
272 ctx->DrawBuffer->_Xmax,
273 ctx->DrawBuffer->_Ymax,
274 GL_TRUE, GL_FALSE, GL_FALSE);
275 }
276 }
277
278
279 static void
280 clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
281 {
282 if (!ctx->Scissor.Enabled &&
283 !is_depth_stencil_format(rb->surface->format)) {
284 /* clear whole depth buffer w/out masking */
285 GLuint clearValue = depth_value(rb->surface->format, ctx->Depth.Clear);
286 ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
287 }
288 else {
289 /* masking or scissoring or combined z/stencil buffer */
290 clear_with_quad(ctx,
291 ctx->DrawBuffer->_Xmin,
292 ctx->DrawBuffer->_Xmin,
293 ctx->DrawBuffer->_Xmax,
294 ctx->DrawBuffer->_Ymax,
295 GL_FALSE, GL_TRUE, GL_FALSE);
296 }
297 }
298
299
300 static void
301 clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
302 {
303 const GLuint stencilMax = (1 << rb->StencilBits) - 1;
304 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
305
306 if (!maskStencil && !ctx->Scissor.Enabled &&
307 !is_depth_stencil_format(rb->surface->format)) {
308 /* clear whole stencil buffer w/out masking */
309 GLuint clearValue = ctx->Stencil.Clear;
310 ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
311 }
312 else {
313 /* masking or scissoring */
314 clear_with_quad(ctx,
315 ctx->DrawBuffer->_Xmin,
316 ctx->DrawBuffer->_Xmin,
317 ctx->DrawBuffer->_Xmax,
318 ctx->DrawBuffer->_Ymax,
319 GL_FALSE, GL_FALSE, GL_TRUE);
320 }
321 }
322
323
324 static void
325 clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
326 {
327 const GLuint stencilMax = 1 << rb->StencilBits;
328 GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;
329
330 assert(is_depth_stencil_format(rb->surface->format));
331
332 if (!maskStencil && !ctx->Scissor.Enabled) {
333 /* clear whole buffer w/out masking */
334 GLuint clearValue = depth_value(rb->surface->format, ctx->Depth.Clear);
335
336 switch (rb->surface->format) {
337 case PIPE_FORMAT_S8_Z24:
338 clearValue |= ctx->Stencil.Clear << 24;
339 break;
340 #if 0
341 case PIPE_FORMAT_Z24_S8:
342 clearValue = (clearValue << 8) | clearVal;
343 break;
344 #endif
345 default:
346 assert(0);
347 }
348
349 ctx->st->pipe->clear(ctx->st->pipe, rb->surface, clearValue);
350 }
351 else {
352 /* masking or scissoring */
353 clear_with_quad(ctx,
354 ctx->DrawBuffer->_Xmin,
355 ctx->DrawBuffer->_Xmin,
356 ctx->DrawBuffer->_Xmax,
357 ctx->DrawBuffer->_Ymax,
358 GL_FALSE, GL_TRUE, GL_TRUE);
359 }
360 }
361
362
363
364 /**
365 * Called via ctx->Driver.Clear()
366 * XXX: doesn't pick up the differences between front/back/left/right
367 * clears. Need to sort that out...
368 */
369 static void st_clear(GLcontext *ctx, GLbitfield mask)
370 {
371 static const GLbitfield BUFFER_BITS_DS
372 = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
373 struct st_context *st = ctx->st;
374 struct gl_renderbuffer *depthRb
375 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
376 struct gl_renderbuffer *stencilRb
377 = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
378
379 /* This makes sure the softpipe has the latest scissor, etc values */
380 st_validate_state( st );
381
382 /*
383 * XXX TO-DO:
384 * If we're going to use clear_with_quad() for any reason, use it to
385 * clear as many other buffers as possible.
386 * As it is now, we sometimes call clear_with_quad() three times to clear
387 * color/depth/stencil individually...
388 */
389
390 if (mask & BUFFER_BITS_COLOR) {
391 GLuint b;
392 for (b = 0; b < BUFFER_COUNT; b++) {
393 if (BUFFER_BITS_COLOR & mask & (1 << b)) {
394 clear_color_buffer(ctx,
395 ctx->DrawBuffer->Attachment[b].Renderbuffer);
396 }
397 }
398 }
399
400 if (mask & BUFFER_BIT_ACCUM) {
401 clear_accum_buffer(ctx,
402 ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
403 }
404
405 if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
406 /* clearing combined depth + stencil */
407 clear_depth_stencil_buffer(ctx, depthRb);
408 }
409 else {
410 /* separate depth/stencil clears */
411 if (mask & BUFFER_BIT_DEPTH) {
412 clear_depth_buffer(ctx, depthRb);
413 }
414 if (mask & BUFFER_BIT_STENCIL) {
415 clear_stencil_buffer(ctx, stencilRb);
416 }
417 }
418 }
419
420
421 void st_init_cb_clear( struct st_context *st )
422 {
423 struct dd_function_table *functions = &st->ctx->Driver;
424
425 functions->Clear = st_clear;
426 }
427
428
429 void st_destroy_cb_clear( struct st_context *st )
430 {
431 }
432