Merge branch 'softpipe_0_1_branch' of git+ssh://brianp@git.freedesktop.org/git/mesa...
[mesa.git] / src / mesa / pipe / softpipe / sp_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 /* Author:
29 * Brian Paul
30 */
31
32
33 #include "pipe/p_defines.h"
34 #include "sp_clear.h"
35 #include "sp_context.h"
36 #include "sp_surface.h"
37 #include "sp_state.h"
38 #include "colormac.h"
39
40
41 static GLuint
42 color_value(GLuint format, const GLfloat color[4])
43 {
44 GLubyte r, g, b, a;
45
46 UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
47 UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
48 UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
49 UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);
50
51 switch (format) {
52 case PIPE_FORMAT_U_R8_G8_B8_A8:
53 return (r << 24) | (g << 16) | (b << 8) | a;
54 case PIPE_FORMAT_U_A8_R8_G8_B8:
55 return (a << 24) | (r << 16) | (g << 8) | b;
56 case PIPE_FORMAT_U_R5_G6_B5:
57 return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
58 default:
59 return 0;
60 }
61 }
62
63
64 void
65 softpipe_clear(struct pipe_context *pipe, GLboolean color, GLboolean depth,
66 GLboolean stencil, GLboolean accum)
67 {
68 struct softpipe_context *softpipe = softpipe_context(pipe);
69 GLint x, y, w, h;
70
71 softpipe_update_derived(softpipe); /* not needed?? */
72
73 x = 0;
74 y = 0;
75 w = softpipe->framebuffer.cbufs[0]->width;
76 h = softpipe->framebuffer.cbufs[0]->height;
77
78 if (color) {
79 GLuint i;
80 for (i = 0; i < softpipe->framebuffer.num_cbufs; i++) {
81 struct pipe_surface *ps = softpipe->framebuffer.cbufs[i];
82 GLuint clearVal = color_value(ps->format,
83 softpipe->clear_color.color);
84 pipe->region_fill(pipe, ps->region, 0, x, y, w, h, clearVal, ~0);
85 }
86 }
87
88 if (depth && stencil &&
89 softpipe->framebuffer.zbuf == softpipe->framebuffer.sbuf) {
90 /* clear Z and stencil together */
91 struct pipe_surface *ps = softpipe->framebuffer.zbuf;
92 if (ps->format == PIPE_FORMAT_S8_Z24) {
93 GLuint mask = (softpipe->stencil.write_mask[0] << 8) | 0xffffff;
94 GLuint clearVal = (GLuint) (softpipe->depth_test.clear * 0xffffff);
95
96 assert (mask == ~0);
97
98 clearVal |= (softpipe->stencil.clear_value << 24);
99 pipe->region_fill(pipe, ps->region, 0, x, y, w, h, clearVal, ~0);
100 }
101 else {
102 /* XXX Z24_S8 format? */
103 assert(0);
104 }
105 }
106 else {
107 /* separate Z and stencil */
108 if (depth) {
109 struct pipe_surface *ps = softpipe->framebuffer.zbuf;
110 GLuint clearVal;
111
112 switch (ps->format) {
113 case PIPE_FORMAT_U_Z16:
114 clearVal = (GLuint) (softpipe->depth_test.clear * 65535.0);
115 break;
116 case PIPE_FORMAT_U_Z32:
117 clearVal = (GLuint) (softpipe->depth_test.clear * 0xffffffff);
118 break;
119 case PIPE_FORMAT_S8_Z24:
120 clearVal = (GLuint) (softpipe->depth_test.clear * 0xffffff);
121 break;
122 default:
123 assert(0);
124 }
125
126 pipe->region_fill(pipe, ps->region, 0, x, y, w, h, clearVal, ~0);
127 }
128
129 if (stencil) {
130 struct pipe_surface *ps = softpipe->framebuffer.sbuf;
131 GLuint clearVal = softpipe->stencil.clear_value;
132
133 /* If this is not ~0, we shouldn't get here - clear should be
134 * done with geometry instead.
135 */
136 GLuint mask = softpipe->stencil.write_mask[0];
137
138 assert((mask & 0xff) == 0xff);
139
140 switch (ps->format) {
141 case PIPE_FORMAT_S8_Z24:
142 clearVal = clearVal << 24;
143 mask = mask << 24;
144 break;
145 case PIPE_FORMAT_U_S8:
146 /* nothing */
147 break;
148 default:
149 assert(0);
150 }
151
152 pipe->region_fill(pipe, ps->region, 0, x, y, w, h, clearVal, mask);
153 }
154 }
155
156 if (accum) {
157 /* XXX there might be no notion of accum buffers in 'pipe'.
158 * Just implement them with a deep RGBA surface format...
159 */
160 struct pipe_surface *ps = softpipe->framebuffer.abuf;
161 GLuint clearVal = 0x0; /* XXX FIX */
162 GLuint mask = ~0;
163 assert(ps);
164 pipe->region_fill(pipe, ps->region, 0, x, y, w, h, clearVal, mask);
165 }
166 }