Silence compiler warnings.
[mesa.git] / src / mesa / pipe / draw / draw_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 "pipe/p_util.h"
36 #include "draw_private.h"
37
38
39
40 struct offset_stage {
41 struct draw_stage stage;
42
43 float scale;
44 float units;
45 };
46
47
48
49 static INLINE struct offset_stage *offset_stage( struct draw_stage *stage )
50 {
51 return (struct offset_stage *) stage;
52 }
53
54
55 static void offset_begin( struct draw_stage *stage )
56 {
57 struct offset_stage *offset = offset_stage(stage);
58 float mrd = 1.0f / 65535.0f; /* XXX this depends on depthbuffer bits! */
59
60 offset->units = stage->draw->setup.offset_units * mrd;
61 offset->scale = stage->draw->setup.offset_scale;
62
63 stage->next->begin( stage->next );
64 }
65
66
67 /**
68 * Offset tri Z. Some hardware can handle this, but not usually when
69 * doing unfilled rendering.
70 */
71 static void do_offset_tri( struct draw_stage *stage,
72 struct prim_header *header )
73 {
74 struct offset_stage *offset = offset_stage(stage);
75 float inv_det = 1.0f / header->det;
76
77 /* Window coords:
78 */
79 float *v0 = header->v[0]->data[0];
80 float *v1 = header->v[1]->data[0];
81 float *v2 = header->v[2]->data[0];
82
83 /* edge vectors e = v0 - v2, f = v1 - v2 */
84 float ex = v0[0] - v2[0];
85 float ey = v0[1] - v2[1];
86 float ez = v0[2] - v2[2];
87 float fx = v1[0] - v2[0];
88 float fy = v1[1] - v2[1];
89 float fz = v1[2] - v2[2];
90
91 /* (a,b) = cross(e,f).xy */
92 float a = ey*fz - ez*fy;
93 float b = ez*fx - ex*fz;
94
95 float dzdx = FABSF(a * inv_det);
96 float dzdy = FABSF(b * inv_det);
97
98 float zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale;
99
100 /*
101 * Note: we're applying the offset and clamping per-vertex.
102 * Ideally, the offset is applied per-fragment prior to fragment shading.
103 */
104 v0[2] = CLAMP(v0[2] + zoffset, 0.0f, 1.0f);
105 v1[2] = CLAMP(v1[2] + zoffset, 0.0f, 1.0f);
106 v2[2] = CLAMP(v2[2] + zoffset, 0.0f, 1.0f);
107
108 stage->next->tri( stage->next, header );
109 }
110
111
112 static void offset_tri( struct draw_stage *stage,
113 struct prim_header *header )
114 {
115 struct prim_header tmp;
116
117 tmp.det = header->det;
118 tmp.v[0] = dup_vert(stage, header->v[0], 0);
119 tmp.v[1] = dup_vert(stage, header->v[1], 1);
120 tmp.v[2] = dup_vert(stage, header->v[2], 2);
121
122 do_offset_tri( stage, &tmp );
123 }
124
125
126
127 static void offset_line( struct draw_stage *stage,
128 struct prim_header *header )
129 {
130 stage->next->line( stage->next, header );
131 }
132
133
134 static void offset_point( struct draw_stage *stage,
135 struct prim_header *header )
136 {
137 stage->next->point( stage->next, header );
138 }
139
140
141 static void offset_end( struct draw_stage *stage )
142 {
143 stage->next->end( stage->next );
144 }
145
146
147 static void offset_reset_stipple_counter( struct draw_stage *stage )
148 {
149 stage->next->reset_stipple_counter( stage->next );
150 }
151
152
153 /**
154 * Create polygon offset drawing stage.
155 */
156 struct draw_stage *draw_offset_stage( struct draw_context *draw )
157 {
158 struct offset_stage *offset = CALLOC_STRUCT(offset_stage);
159
160 draw_alloc_tmps( &offset->stage, 3 );
161
162 offset->stage.draw = draw;
163 offset->stage.next = NULL;
164 offset->stage.begin = offset_begin;
165 offset->stage.point = offset_point;
166 offset->stage.line = offset_line;
167 offset->stage.tri = offset_tri;
168 offset->stage.end = offset_end;
169 offset->stage.reset_stipple_counter = offset_reset_stipple_counter;
170
171 return &offset->stage;
172 }