8071bc77b80f76f1cfc2dacbbc12b2c4487e93a1
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_offset.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 polygon offset state
30 *
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 * \author Brian Paul
33 */
34
35 #include "util/u_format.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "draw_pipe.h"
39
40
41
42 struct offset_stage {
43 struct draw_stage stage;
44
45 float scale;
46 float units;
47 float clamp;
48 };
49
50
51
52 static INLINE struct offset_stage *offset_stage( struct draw_stage *stage )
53 {
54 return (struct offset_stage *) stage;
55 }
56
57
58
59
60
61 /**
62 * Offset tri Z. Some hardware can handle this, but not usually when
63 * doing unfilled rendering.
64 */
65 static void do_offset_tri( struct draw_stage *stage,
66 struct prim_header *header )
67 {
68 const unsigned pos = draw_current_shader_position_output(stage->draw);
69 struct offset_stage *offset = offset_stage(stage);
70 float inv_det = 1.0f / header->det;
71
72 /* Window coords:
73 */
74 float *v0 = header->v[0]->data[pos];
75 float *v1 = header->v[1]->data[pos];
76 float *v2 = header->v[2]->data[pos];
77
78 /* edge vectors e = v0 - v2, f = v1 - v2 */
79 float ex = v0[0] - v2[0];
80 float ey = v0[1] - v2[1];
81 float ez = v0[2] - v2[2];
82 float fx = v1[0] - v2[0];
83 float fy = v1[1] - v2[1];
84 float fz = v1[2] - v2[2];
85
86 /* (a,b) = cross(e,f).xy */
87 float a = ey*fz - ez*fy;
88 float b = ez*fx - ex*fz;
89
90 float dzdx = fabsf(a * inv_det);
91 float dzdy = fabsf(b * inv_det);
92
93 float zoffset, maxz, bias, mult;
94
95 mult = MAX2(dzdx, dzdy) * offset->scale;
96
97 if (stage->draw->floating_point_depth) {
98 maxz = MAX3(v0[2], v1[2], v2[2]);
99
100 /**
101 * XXX: TODO optimize this to quickly resolve a pow2 number through
102 * an exponent only operation.
103 */
104 bias = offset->units * util_fast_exp2(util_get_float32_exponent(maxz) - 23);
105 zoffset = bias + mult;
106 } else {
107 zoffset = offset->units + mult;
108 }
109
110 if (offset->clamp)
111 zoffset = (offset->clamp < 0.0f) ? MAX2(zoffset, offset->clamp) :
112 MIN2(zoffset, offset->clamp);
113
114 /*
115 * Note: we're applying the offset and clamping per-vertex.
116 * Ideally, the offset is applied per-fragment prior to fragment shading.
117 */
118 v0[2] = CLAMP(v0[2] + zoffset, 0.0f, 1.0f);
119 v1[2] = CLAMP(v1[2] + zoffset, 0.0f, 1.0f);
120 v2[2] = CLAMP(v2[2] + zoffset, 0.0f, 1.0f);
121
122 stage->next->tri( stage->next, header );
123 }
124
125
126 static void offset_tri( struct draw_stage *stage,
127 struct prim_header *header )
128 {
129 struct prim_header tmp;
130
131 tmp.det = header->det;
132 tmp.flags = header->flags;
133 tmp.pad = header->pad;
134 tmp.v[0] = dup_vert(stage, header->v[0], 0);
135 tmp.v[1] = dup_vert(stage, header->v[1], 1);
136 tmp.v[2] = dup_vert(stage, header->v[2], 2);
137
138 do_offset_tri( stage, &tmp );
139 }
140
141
142 static void offset_first_tri( struct draw_stage *stage,
143 struct prim_header *header )
144 {
145 struct offset_stage *offset = offset_stage(stage);
146 const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;
147 unsigned fill_mode = rast->fill_front;
148 boolean do_offset;
149
150 if (rast->fill_back != rast->fill_front) {
151 /* Need to check for back-facing triangle */
152 boolean ccw = header->det < 0.0f;
153 if (ccw != rast->front_ccw)
154 fill_mode = rast->fill_back;
155 }
156
157 /* Now determine if we need to do offsetting for the point/line/fill mode */
158 switch (fill_mode) {
159 case PIPE_POLYGON_MODE_FILL:
160 do_offset = rast->offset_tri;
161 break;
162 case PIPE_POLYGON_MODE_LINE:
163 do_offset = rast->offset_line;
164 break;
165 case PIPE_POLYGON_MODE_POINT:
166 do_offset = rast->offset_point;
167 break;
168 default:
169 assert(!"invalid fill_mode in offset_first_tri()");
170 do_offset = rast->offset_tri;
171 }
172
173 if (do_offset) {
174 offset->scale = rast->offset_scale;
175 offset->clamp = rast->offset_clamp;
176
177 /*
178 * If depth is floating point, depth bias is calculated with respect
179 * to the primitive's maximum Z value. Retain the original depth bias
180 * value until that stage.
181 */
182 if (stage->draw->floating_point_depth) {
183 offset->units = (float) rast->offset_units;
184 } else {
185 offset->units = (float) (rast->offset_units * stage->draw->mrd);
186 }
187 }
188 else {
189 offset->scale = 0.0f;
190 offset->clamp = 0.0f;
191 offset->units = 0.0f;
192 }
193
194
195 stage->tri = offset_tri;
196 stage->tri( stage, header );
197 }
198
199
200
201
202 static void offset_flush( struct draw_stage *stage,
203 unsigned flags )
204 {
205 stage->tri = offset_first_tri;
206 stage->next->flush( stage->next, flags );
207 }
208
209
210 static void offset_reset_stipple_counter( struct draw_stage *stage )
211 {
212 stage->next->reset_stipple_counter( stage->next );
213 }
214
215
216 static void offset_destroy( struct draw_stage *stage )
217 {
218 draw_free_temp_verts( stage );
219 FREE( stage );
220 }
221
222
223 /**
224 * Create polygon offset drawing stage.
225 */
226 struct draw_stage *draw_offset_stage( struct draw_context *draw )
227 {
228 struct offset_stage *offset = CALLOC_STRUCT(offset_stage);
229 if (offset == NULL)
230 goto fail;
231
232 offset->stage.draw = draw;
233 offset->stage.name = "offset";
234 offset->stage.next = NULL;
235 offset->stage.point = draw_pipe_passthrough_point;
236 offset->stage.line = draw_pipe_passthrough_line;
237 offset->stage.tri = offset_first_tri;
238 offset->stage.flush = offset_flush;
239 offset->stage.reset_stipple_counter = offset_reset_stipple_counter;
240 offset->stage.destroy = offset_destroy;
241
242 if (!draw_alloc_temp_verts( &offset->stage, 3 ))
243 goto fail;
244
245 return &offset->stage;
246
247 fail:
248 if (offset)
249 offset->stage.destroy( &offset->stage );
250
251 return NULL;
252 }