Tizen 2.0 Release
[profile/ivi/osmesa.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_math.h"
36 #include "util/u_memory.h"
37 #include "draw_pipe.h"
38
39
40
41 struct offset_stage {
42    struct draw_stage stage;
43
44    float scale;
45    float units;
46 };
47
48
49
50 static INLINE struct offset_stage *offset_stage( struct draw_stage *stage )
51 {
52    return (struct offset_stage *) stage;
53 }
54
55
56
57
58
59 /**
60  * Offset tri Z.  Some hardware can handle this, but not usually when
61  * doing unfilled rendering.
62  */
63 static void do_offset_tri( struct draw_stage *stage,
64                            struct prim_header *header )
65 {
66    const unsigned pos = draw_current_shader_position_output(stage->draw);
67    struct offset_stage *offset = offset_stage(stage);   
68    float inv_det = 1.0f / header->det;
69
70    /* Window coords:
71     */
72    float *v0 = header->v[0]->data[pos];
73    float *v1 = header->v[1]->data[pos];
74    float *v2 = header->v[2]->data[pos];
75
76    /* edge vectors e = v0 - v2, f = v1 - v2 */
77    float ex = v0[0] - v2[0];
78    float ey = v0[1] - v2[1];
79    float ez = v0[2] - v2[2];
80    float fx = v1[0] - v2[0];
81    float fy = v1[1] - v2[1];
82    float fz = v1[2] - v2[2];
83
84    /* (a,b) = cross(e,f).xy */
85    float a = ey*fz - ez*fy;
86    float b = ez*fx - ex*fz;
87
88    float dzdx = fabsf(a * inv_det);
89    float dzdy = fabsf(b * inv_det);
90
91    float zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale;
92
93    /*
94     * Note: we're applying the offset and clamping per-vertex.
95     * Ideally, the offset is applied per-fragment prior to fragment shading.
96     */
97    v0[2] = CLAMP(v0[2] + zoffset, 0.0f, 1.0f);
98    v1[2] = CLAMP(v1[2] + zoffset, 0.0f, 1.0f);
99    v2[2] = CLAMP(v2[2] + zoffset, 0.0f, 1.0f);
100
101    stage->next->tri( stage->next, header );
102 }
103
104
105 static void offset_tri( struct draw_stage *stage,
106                         struct prim_header *header )
107 {
108    struct prim_header tmp;
109
110    tmp.det = header->det;
111    tmp.flags = header->flags;
112    tmp.pad = header->pad;
113    tmp.v[0] = dup_vert(stage, header->v[0], 0);
114    tmp.v[1] = dup_vert(stage, header->v[1], 1);
115    tmp.v[2] = dup_vert(stage, header->v[2], 2);
116
117    do_offset_tri( stage, &tmp );
118 }
119
120
121 static void offset_first_tri( struct draw_stage *stage, 
122                               struct prim_header *header )
123 {
124    struct offset_stage *offset = offset_stage(stage);
125
126    offset->units = (float) (stage->draw->rasterizer->offset_units * stage->draw->mrd);
127    offset->scale = stage->draw->rasterizer->offset_scale;
128
129    stage->tri = offset_tri;
130    stage->tri( stage, header );
131 }
132
133
134
135
136 static void offset_flush( struct draw_stage *stage,
137                           unsigned flags )
138 {
139    stage->tri = offset_first_tri;
140    stage->next->flush( stage->next, flags );
141 }
142
143
144 static void offset_reset_stipple_counter( struct draw_stage *stage )
145 {
146    stage->next->reset_stipple_counter( stage->next );
147 }
148
149
150 static void offset_destroy( struct draw_stage *stage )
151 {
152    draw_free_temp_verts( stage );
153    FREE( stage );
154 }
155
156
157 /**
158  * Create polygon offset drawing stage.
159  */
160 struct draw_stage *draw_offset_stage( struct draw_context *draw )
161 {
162    struct offset_stage *offset = CALLOC_STRUCT(offset_stage);
163    if (offset == NULL)
164       goto fail;
165
166    offset->stage.draw = draw;
167    offset->stage.name = "offset";
168    offset->stage.next = NULL;
169    offset->stage.point = draw_pipe_passthrough_point;
170    offset->stage.line = draw_pipe_passthrough_line;
171    offset->stage.tri = offset_first_tri;
172    offset->stage.flush = offset_flush;
173    offset->stage.reset_stipple_counter = offset_reset_stipple_counter;
174    offset->stage.destroy = offset_destroy;
175
176    if (!draw_alloc_temp_verts( &offset->stage, 3 ))
177       goto fail;
178
179    return &offset->stage;
180
181 fail:
182    if (offset)
183       offset->stage.destroy( &offset->stage );
184
185    return NULL;
186 }