1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
29 * \brief polygon offset state
31 * \author Keith Whitwell <keithw@vmware.com>
35 #include "util/u_format.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "draw_pipe.h"
43 struct draw_stage stage
;
52 static inline struct offset_stage
*offset_stage( struct draw_stage
*stage
)
54 return (struct offset_stage
*) stage
;
62 * Offset tri Z. Some hardware can handle this, but not usually when
63 * doing unfilled rendering.
65 static void do_offset_tri( struct draw_stage
*stage
,
66 struct prim_header
*header
)
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
;
74 float *v0
= header
->v
[0]->data
[pos
];
75 float *v1
= header
->v
[1]->data
[pos
];
76 float *v2
= header
->v
[2]->data
[pos
];
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];
86 /* (a,b) = cross(e,f).xy */
87 float a
= ey
*fz
- ez
*fy
;
88 float b
= ez
*fx
- ex
*fz
;
90 float dzdx
= fabsf(a
* inv_det
);
91 float dzdy
= fabsf(b
* inv_det
);
95 mult
= MAX2(dzdx
, dzdy
) * offset
->scale
;
97 if (stage
->draw
->floating_point_depth
) {
100 maxz
.f
= MAX3(v0
[2], v1
[2], v2
[2]);
101 /* just do the math directly on shifted number */
102 maxz
.ui
&= 0xff << 23;
104 /* Clamping to zero means mrd will be zero for very small numbers,
105 * but specs do not indicate this should be prevented by clamping
106 * mrd to smallest normal number instead. */
107 maxz
.i
= MAX2(maxz
.i
, 0);
109 bias
= offset
->units
* maxz
.f
;
110 zoffset
= bias
+ mult
;
112 zoffset
= offset
->units
+ mult
;
116 zoffset
= (offset
->clamp
< 0.0f
) ? MAX2(zoffset
, offset
->clamp
) :
117 MIN2(zoffset
, offset
->clamp
);
120 * Note: we're applying the offset and clamping per-vertex.
121 * Ideally, the offset is applied per-fragment prior to fragment shading.
123 v0
[2] = CLAMP(v0
[2] + zoffset
, 0.0f
, 1.0f
);
124 v1
[2] = CLAMP(v1
[2] + zoffset
, 0.0f
, 1.0f
);
125 v2
[2] = CLAMP(v2
[2] + zoffset
, 0.0f
, 1.0f
);
127 stage
->next
->tri( stage
->next
, header
);
131 static void offset_tri( struct draw_stage
*stage
,
132 struct prim_header
*header
)
134 struct prim_header tmp
;
136 tmp
.det
= header
->det
;
137 tmp
.flags
= header
->flags
;
138 tmp
.pad
= header
->pad
;
139 tmp
.v
[0] = dup_vert(stage
, header
->v
[0], 0);
140 tmp
.v
[1] = dup_vert(stage
, header
->v
[1], 1);
141 tmp
.v
[2] = dup_vert(stage
, header
->v
[2], 2);
143 do_offset_tri( stage
, &tmp
);
147 static void offset_first_tri( struct draw_stage
*stage
,
148 struct prim_header
*header
)
150 struct offset_stage
*offset
= offset_stage(stage
);
151 const struct pipe_rasterizer_state
*rast
= stage
->draw
->rasterizer
;
152 unsigned fill_mode
= rast
->fill_front
;
155 if (rast
->fill_back
!= rast
->fill_front
) {
156 /* Need to check for back-facing triangle */
157 boolean ccw
= header
->det
< 0.0f
;
158 if (ccw
!= rast
->front_ccw
)
159 fill_mode
= rast
->fill_back
;
162 /* Now determine if we need to do offsetting for the point/line/fill mode */
164 case PIPE_POLYGON_MODE_FILL
:
165 do_offset
= rast
->offset_tri
;
167 case PIPE_POLYGON_MODE_LINE
:
168 do_offset
= rast
->offset_line
;
170 case PIPE_POLYGON_MODE_POINT
:
171 do_offset
= rast
->offset_point
;
174 assert(!"invalid fill_mode in offset_first_tri()");
175 do_offset
= rast
->offset_tri
;
179 offset
->scale
= rast
->offset_scale
;
180 offset
->clamp
= rast
->offset_clamp
;
183 * If depth is floating point, depth bias is calculated with respect
184 * to the primitive's maximum Z value. Retain the original depth bias
185 * value until that stage.
187 if (stage
->draw
->floating_point_depth
) {
188 offset
->units
= (float) rast
->offset_units
;
190 offset
->units
= (float) (rast
->offset_units
* stage
->draw
->mrd
);
194 offset
->scale
= 0.0f
;
195 offset
->clamp
= 0.0f
;
196 offset
->units
= 0.0f
;
200 stage
->tri
= offset_tri
;
201 stage
->tri( stage
, header
);
207 static void offset_flush( struct draw_stage
*stage
,
210 stage
->tri
= offset_first_tri
;
211 stage
->next
->flush( stage
->next
, flags
);
215 static void offset_reset_stipple_counter( struct draw_stage
*stage
)
217 stage
->next
->reset_stipple_counter( stage
->next
);
221 static void offset_destroy( struct draw_stage
*stage
)
223 draw_free_temp_verts( stage
);
229 * Create polygon offset drawing stage.
231 struct draw_stage
*draw_offset_stage( struct draw_context
*draw
)
233 struct offset_stage
*offset
= CALLOC_STRUCT(offset_stage
);
237 offset
->stage
.draw
= draw
;
238 offset
->stage
.name
= "offset";
239 offset
->stage
.next
= NULL
;
240 offset
->stage
.point
= draw_pipe_passthrough_point
;
241 offset
->stage
.line
= draw_pipe_passthrough_line
;
242 offset
->stage
.tri
= offset_first_tri
;
243 offset
->stage
.flush
= offset_flush
;
244 offset
->stage
.reset_stipple_counter
= offset_reset_stipple_counter
;
245 offset
->stage
.destroy
= offset_destroy
;
247 if (!draw_alloc_temp_verts( &offset
->stage
, 3 ))
250 return &offset
->stage
;
254 offset
->stage
.destroy( &offset
->stage
);