softpipe: rename sp_quad.[ch] -> sp_quad_pipe.[ch]
[mesa.git] / src / gallium / drivers / softpipe / sp_quad_depth_test.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 * \brief Quad depth testing
30 */
31
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "sp_context.h"
35 #include "sp_headers.h"
36 #include "sp_surface.h"
37 #include "sp_quad_pipe.h"
38 #include "sp_tile_cache.h"
39
40
41 /**
42 * Do depth testing for a quad.
43 * Not static since it's used by the stencil code.
44 */
45
46 /*
47 * To increase efficiency, we should probably have multiple versions
48 * of this function that are specifically for Z16, Z32 and FP Z buffers.
49 * Try to effectively do that with codegen...
50 */
51
52 void
53 sp_depth_test_quad(struct quad_stage *qs, struct quad_header *quad)
54 {
55 struct softpipe_context *softpipe = qs->softpipe;
56 struct pipe_surface *ps = softpipe->framebuffer.zsbuf;
57 const enum pipe_format format = ps->format;
58 unsigned bzzzz[QUAD_SIZE]; /**< Z values fetched from depth buffer */
59 unsigned qzzzz[QUAD_SIZE]; /**< Z values from the quad */
60 unsigned zmask = 0;
61 unsigned j;
62 struct softpipe_cached_tile *tile
63 = sp_get_cached_tile(softpipe, softpipe->zsbuf_cache, quad->input.x0, quad->input.y0);
64
65 assert(ps); /* shouldn't get here if there's no zbuffer */
66
67 /*
68 * Convert quad's float depth values to int depth values (qzzzz).
69 * If the Z buffer stores integer values, we _have_ to do the depth
70 * compares with integers (not floats). Otherwise, the float->int->float
71 * conversion of Z values (which isn't an identity function) will cause
72 * Z-fighting errors.
73 *
74 * Also, get the zbuffer values (bzzzz) from the cached tile.
75 */
76 switch (format) {
77 case PIPE_FORMAT_Z16_UNORM:
78 {
79 float scale = 65535.0;
80
81 for (j = 0; j < QUAD_SIZE; j++) {
82 qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
83 }
84
85 for (j = 0; j < QUAD_SIZE; j++) {
86 int x = quad->input.x0 % TILE_SIZE + (j & 1);
87 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
88 bzzzz[j] = tile->data.depth16[y][x];
89 }
90 }
91 break;
92 case PIPE_FORMAT_Z32_UNORM:
93 {
94 double scale = (double) (uint) ~0UL;
95
96 for (j = 0; j < QUAD_SIZE; j++) {
97 qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
98 }
99
100 for (j = 0; j < QUAD_SIZE; j++) {
101 int x = quad->input.x0 % TILE_SIZE + (j & 1);
102 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
103 bzzzz[j] = tile->data.depth32[y][x];
104 }
105 }
106 break;
107 case PIPE_FORMAT_X8Z24_UNORM:
108 /* fall-through */
109 case PIPE_FORMAT_S8Z24_UNORM:
110 {
111 float scale = (float) ((1 << 24) - 1);
112
113 for (j = 0; j < QUAD_SIZE; j++) {
114 qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
115 }
116
117 for (j = 0; j < QUAD_SIZE; j++) {
118 int x = quad->input.x0 % TILE_SIZE + (j & 1);
119 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
120 bzzzz[j] = tile->data.depth32[y][x] & 0xffffff;
121 }
122 }
123 break;
124 case PIPE_FORMAT_Z24X8_UNORM:
125 /* fall-through */
126 case PIPE_FORMAT_Z24S8_UNORM:
127 {
128 float scale = (float) ((1 << 24) - 1);
129
130 for (j = 0; j < QUAD_SIZE; j++) {
131 qzzzz[j] = (unsigned) (quad->output.depth[j] * scale);
132 }
133
134 for (j = 0; j < QUAD_SIZE; j++) {
135 int x = quad->input.x0 % TILE_SIZE + (j & 1);
136 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
137 bzzzz[j] = tile->data.depth32[y][x] >> 8;
138 }
139 }
140 break;
141 default:
142 assert(0);
143 }
144
145 switch (softpipe->depth_stencil->depth.func) {
146 case PIPE_FUNC_NEVER:
147 /* zmask = 0 */
148 break;
149 case PIPE_FUNC_LESS:
150 /* Note this is pretty much a single sse or cell instruction.
151 * Like this: quad->mask &= (quad->outputs.depth < zzzz);
152 */
153 for (j = 0; j < QUAD_SIZE; j++) {
154 if (qzzzz[j] < bzzzz[j])
155 zmask |= 1 << j;
156 }
157 break;
158 case PIPE_FUNC_EQUAL:
159 for (j = 0; j < QUAD_SIZE; j++) {
160 if (qzzzz[j] == bzzzz[j])
161 zmask |= 1 << j;
162 }
163 break;
164 case PIPE_FUNC_LEQUAL:
165 for (j = 0; j < QUAD_SIZE; j++) {
166 if (qzzzz[j] <= bzzzz[j])
167 zmask |= (1 << j);
168 }
169 break;
170 case PIPE_FUNC_GREATER:
171 for (j = 0; j < QUAD_SIZE; j++) {
172 if (qzzzz[j] > bzzzz[j])
173 zmask |= (1 << j);
174 }
175 break;
176 case PIPE_FUNC_NOTEQUAL:
177 for (j = 0; j < QUAD_SIZE; j++) {
178 if (qzzzz[j] != bzzzz[j])
179 zmask |= (1 << j);
180 }
181 break;
182 case PIPE_FUNC_GEQUAL:
183 for (j = 0; j < QUAD_SIZE; j++) {
184 if (qzzzz[j] >= bzzzz[j])
185 zmask |= (1 << j);
186 }
187 break;
188 case PIPE_FUNC_ALWAYS:
189 zmask = MASK_ALL;
190 break;
191 default:
192 assert(0);
193 }
194
195 quad->inout.mask &= zmask;
196
197 if (softpipe->depth_stencil->depth.writemask) {
198
199 /* This is also efficient with sse / spe instructions:
200 */
201 for (j = 0; j < QUAD_SIZE; j++) {
202 if (quad->inout.mask & (1 << j)) {
203 bzzzz[j] = qzzzz[j];
204 }
205 }
206
207 /* put updated Z values back into cached tile */
208 switch (format) {
209 case PIPE_FORMAT_Z16_UNORM:
210 for (j = 0; j < QUAD_SIZE; j++) {
211 int x = quad->input.x0 % TILE_SIZE + (j & 1);
212 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
213 tile->data.depth16[y][x] = (ushort) bzzzz[j];
214 }
215 break;
216 case PIPE_FORMAT_X8Z24_UNORM:
217 /* fall-through */
218 /* (yes, this falls through to a different case than above) */
219 case PIPE_FORMAT_Z32_UNORM:
220 for (j = 0; j < QUAD_SIZE; j++) {
221 int x = quad->input.x0 % TILE_SIZE + (j & 1);
222 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
223 tile->data.depth32[y][x] = bzzzz[j];
224 }
225 break;
226 case PIPE_FORMAT_S8Z24_UNORM:
227 for (j = 0; j < QUAD_SIZE; j++) {
228 int x = quad->input.x0 % TILE_SIZE + (j & 1);
229 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
230 uint s8z24 = tile->data.depth32[y][x];
231 s8z24 = (s8z24 & 0xff000000) | bzzzz[j];
232 tile->data.depth32[y][x] = s8z24;
233 }
234 break;
235 case PIPE_FORMAT_Z24S8_UNORM:
236 for (j = 0; j < QUAD_SIZE; j++) {
237 int x = quad->input.x0 % TILE_SIZE + (j & 1);
238 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
239 uint z24s8 = tile->data.depth32[y][x];
240 z24s8 = (z24s8 & 0xff) | (bzzzz[j] << 8);
241 tile->data.depth32[y][x] = z24s8;
242 }
243 break;
244 case PIPE_FORMAT_Z24X8_UNORM:
245 for (j = 0; j < QUAD_SIZE; j++) {
246 int x = quad->input.x0 % TILE_SIZE + (j & 1);
247 int y = quad->input.y0 % TILE_SIZE + (j >> 1);
248 tile->data.depth32[y][x] = bzzzz[j] << 8;
249 }
250 break;
251 default:
252 assert(0);
253 }
254 }
255 }
256
257
258 static void
259 depth_test_quad(struct quad_stage *qs, struct quad_header *quad)
260 {
261 sp_depth_test_quad(qs, quad);
262
263 if (quad->inout.mask)
264 qs->next->run(qs->next, quad);
265 }
266
267
268 static void depth_test_begin(struct quad_stage *qs)
269 {
270 qs->next->begin(qs->next);
271 }
272
273
274 static void depth_test_destroy(struct quad_stage *qs)
275 {
276 FREE( qs );
277 }
278
279
280 struct quad_stage *sp_quad_depth_test_stage( struct softpipe_context *softpipe )
281 {
282 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
283
284 stage->softpipe = softpipe;
285 stage->begin = depth_test_begin;
286 stage->run = depth_test_quad;
287 stage->destroy = depth_test_destroy;
288
289 return stage;
290 }