fix softpipe_clear() to handle ps->offset!=0 (such as when rendering to texture and...
[mesa.git] / src / mesa / pipe / softpipe / sp_quad_depth_test.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul 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 "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \brief Quad depth testing
27 */
28
29 #include "pipe/p_defines.h"
30 #include "pipe/p_util.h"
31 #include "sp_context.h"
32 #include "sp_headers.h"
33 #include "sp_surface.h"
34 #include "sp_quad.h"
35
36
37 /**
38 * Do depth testing for a quad.
39 * Not static since it's used by the stencil code.
40 */
41 void
42 sp_depth_test_quad(struct quad_stage *qs, struct quad_header *quad)
43 {
44 struct softpipe_context *softpipe = qs->softpipe;
45 struct softpipe_surface *sps = softpipe_surface(softpipe->framebuffer.zbuf);
46 unsigned bzzzz[QUAD_SIZE]; /**< Z values fetched from depth buffer */
47 unsigned qzzzz[QUAD_SIZE]; /**< Z values from the quad */
48 unsigned zmask = 0;
49 unsigned j;
50 float scale;
51
52 assert(sps); /* shouldn't get here if there's no zbuffer */
53
54 /*
55 * To increase efficiency, we should probably have multiple versions
56 * of this function that are specifically for Z16, Z32 and FP Z buffers.
57 * Try to effectively do that with codegen...
58 */
59 if (sps->surface.format == PIPE_FORMAT_U_Z16)
60 scale = 65535.0;
61 else if (sps->surface.format == PIPE_FORMAT_S8_Z24)
62 scale = (float) ((1 << 24) - 1);
63 else
64 assert(0); /* XXX fix this someday */
65
66 /*
67 * Convert quad's float depth values to int depth values.
68 * If the Z buffer stores integer values, we _have_ to do the depth
69 * compares with integers (not floats). Otherwise, the float->int->float
70 * conversion of Z values (which isn't an identity function) will cause
71 * Z-fighting errors.
72 */
73 for (j = 0; j < QUAD_SIZE; j++) {
74 qzzzz[j] = (unsigned) (quad->outputs.depth[j] * scale);
75 }
76
77 /* get zquad from zbuffer */
78 sps->read_quad_z(sps, quad->x0, quad->y0, bzzzz);
79
80 switch (softpipe->depth_stencil->depth.func) {
81 case PIPE_FUNC_NEVER:
82 /* zmask = 0 */
83 break;
84 case PIPE_FUNC_LESS:
85 /* Note this is pretty much a single sse or cell instruction.
86 * Like this: quad->mask &= (quad->outputs.depth < zzzz);
87 */
88 for (j = 0; j < QUAD_SIZE; j++) {
89 if (qzzzz[j] < bzzzz[j])
90 zmask |= 1 << j;
91 }
92 break;
93 case PIPE_FUNC_EQUAL:
94 for (j = 0; j < QUAD_SIZE; j++) {
95 if (qzzzz[j] == bzzzz[j])
96 zmask |= 1 << j;
97 }
98 break;
99 case PIPE_FUNC_LEQUAL:
100 for (j = 0; j < QUAD_SIZE; j++) {
101 if (qzzzz[j] <= bzzzz[j])
102 zmask |= (1 << j);
103 }
104 break;
105 case PIPE_FUNC_GREATER:
106 for (j = 0; j < QUAD_SIZE; j++) {
107 if (qzzzz[j] > bzzzz[j])
108 zmask |= (1 << j);
109 }
110 break;
111 case PIPE_FUNC_NOTEQUAL:
112 for (j = 0; j < QUAD_SIZE; j++) {
113 if (qzzzz[j] != bzzzz[j])
114 zmask |= (1 << j);
115 }
116 break;
117 case PIPE_FUNC_GEQUAL:
118 for (j = 0; j < QUAD_SIZE; j++) {
119 if (qzzzz[j] >= bzzzz[j])
120 zmask |= (1 << j);
121 }
122 break;
123 case PIPE_FUNC_ALWAYS:
124 zmask = MASK_ALL;
125 break;
126 default:
127 abort();
128 }
129
130 quad->mask &= zmask;
131
132 if (softpipe->depth_stencil->depth.writemask) {
133
134 /* This is also efficient with sse / spe instructions:
135 */
136 for (j = 0; j < QUAD_SIZE; j++) {
137 if (quad->mask & (1 << j)) {
138 bzzzz[j] = qzzzz[j];
139 }
140 }
141
142 /* write updated zquad to zbuffer */
143 sps->write_quad_z(sps, quad->x0, quad->y0, bzzzz);
144 }
145 }
146
147
148 static void
149 depth_test_quad(struct quad_stage *qs, struct quad_header *quad)
150 {
151 sp_depth_test_quad(qs, quad);
152
153 if (quad->mask)
154 qs->next->run(qs->next, quad);
155 }
156
157
158 static void depth_test_begin(struct quad_stage *qs)
159 {
160 if (qs->next)
161 qs->next->begin(qs->next);
162 }
163
164
165 struct quad_stage *sp_quad_depth_test_stage( struct softpipe_context *softpipe )
166 {
167 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
168
169 stage->softpipe = softpipe;
170 stage->begin = depth_test_begin;
171 stage->run = depth_test_quad;
172
173 return stage;
174 }