make llvm draw paths compile with the latest changes
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_wide_point.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 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31 #include "pipe/p_util.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "draw_vs.h"
35 #include "draw_pipe.h"
36
37
38 struct widepoint_stage {
39 struct draw_stage stage;
40
41 float half_point_size;
42 float point_size_min;
43 float point_size_max;
44
45 float xbias;
46 float ybias;
47
48 uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
49 uint texcoord_mode[PIPE_MAX_SHADER_OUTPUTS];
50 uint num_texcoords;
51
52 int psize_slot;
53 };
54
55
56
57 static INLINE struct widepoint_stage *
58 widepoint_stage( struct draw_stage *stage )
59 {
60 return (struct widepoint_stage *)stage;
61 }
62
63
64 static void passthrough_point( struct draw_stage *stage,
65 struct prim_header *header )
66 {
67 stage->next->point( stage->next, header );
68 }
69
70 static void widepoint_line( struct draw_stage *stage,
71 struct prim_header *header )
72 {
73 stage->next->line(stage->next, header);
74 }
75
76 static void widepoint_tri( struct draw_stage *stage,
77 struct prim_header *header )
78 {
79 stage->next->tri(stage->next, header);
80 }
81
82
83 /**
84 * Set the vertex texcoords for sprite mode.
85 * Coords may be left untouched or set to a right-side-up or upside-down
86 * orientation.
87 */
88 static void set_texcoords(const struct widepoint_stage *wide,
89 struct vertex_header *v, const float tc[4])
90 {
91 uint i;
92 for (i = 0; i < wide->num_texcoords; i++) {
93 if (wide->texcoord_mode[i] != PIPE_SPRITE_COORD_NONE) {
94 uint j = wide->texcoord_slot[i];
95 v->data[j][0] = tc[0];
96 if (wide->texcoord_mode[i] == PIPE_SPRITE_COORD_LOWER_LEFT)
97 v->data[j][1] = 1.0f - tc[1];
98 else
99 v->data[j][1] = tc[1];
100 v->data[j][2] = tc[2];
101 v->data[j][3] = tc[3];
102 }
103 }
104 }
105
106
107 /* If there are lots of sprite points (and why wouldn't there be?) it
108 * would probably be more sensible to change hardware setup to
109 * optimize this rather than doing the whole thing in software like
110 * this.
111 */
112 static void widepoint_point( struct draw_stage *stage,
113 struct prim_header *header )
114 {
115 const struct widepoint_stage *wide = widepoint_stage(stage);
116 const boolean sprite = (boolean) stage->draw->rasterizer->point_sprite;
117 float half_size;
118 float left_adj, right_adj, bot_adj, top_adj;
119
120 struct prim_header tri;
121
122 /* four dups of original vertex */
123 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
124 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
125 struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
126 struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
127
128 float *pos0 = v0->data[0];
129 float *pos1 = v1->data[0];
130 float *pos2 = v2->data[0];
131 float *pos3 = v3->data[0];
132
133 /* point size is either per-vertex or fixed size */
134 if (wide->psize_slot >= 0) {
135 half_size = header->v[0]->data[wide->psize_slot][0];
136
137 /* XXX: temporary -- do this in the vertex shader??
138 */
139 half_size = CLAMP(half_size,
140 wide->point_size_min,
141 wide->point_size_max);
142
143 half_size *= 0.5f;
144 }
145 else {
146 half_size = wide->half_point_size;
147 }
148
149 left_adj = -half_size + wide->xbias;
150 right_adj = half_size + wide->xbias;
151 bot_adj = half_size + wide->ybias;
152 top_adj = -half_size + wide->ybias;
153
154 pos0[0] += left_adj;
155 pos0[1] += top_adj;
156
157 pos1[0] += left_adj;
158 pos1[1] += bot_adj;
159
160 pos2[0] += right_adj;
161 pos2[1] += top_adj;
162
163 pos3[0] += right_adj;
164 pos3[1] += bot_adj;
165
166 if (sprite) {
167 static const float tex00[4] = { 0, 0, 0, 1 };
168 static const float tex01[4] = { 0, 1, 0, 1 };
169 static const float tex11[4] = { 1, 1, 0, 1 };
170 static const float tex10[4] = { 1, 0, 0, 1 };
171 set_texcoords( wide, v0, tex00 );
172 set_texcoords( wide, v1, tex01 );
173 set_texcoords( wide, v2, tex10 );
174 set_texcoords( wide, v3, tex11 );
175 }
176
177 tri.det = header->det; /* only the sign matters */
178 tri.v[0] = v0;
179 tri.v[1] = v2;
180 tri.v[2] = v3;
181 stage->next->tri( stage->next, &tri );
182
183 tri.v[0] = v0;
184 tri.v[1] = v3;
185 tri.v[2] = v1;
186 stage->next->tri( stage->next, &tri );
187 }
188
189
190 static void widepoint_first_point( struct draw_stage *stage,
191 struct prim_header *header )
192 {
193 struct widepoint_stage *wide = widepoint_stage(stage);
194 struct draw_context *draw = stage->draw;
195
196 wide->half_point_size = 0.5f * draw->rasterizer->point_size;
197 wide->point_size_min = draw->rasterizer->point_size_min;
198 wide->point_size_max = draw->rasterizer->point_size_max;
199 wide->xbias = 0.0;
200 wide->ybias = 0.0;
201
202 if (draw->rasterizer->gl_rasterization_rules) {
203 wide->xbias = 0.125;
204 }
205
206 /* XXX we won't know the real size if it's computed by the vertex shader! */
207 if ((draw->rasterizer->point_size > draw->pipeline.wide_point_threshold) ||
208 (draw->rasterizer->point_sprite && draw->pipeline.point_sprite)) {
209 stage->point = widepoint_point;
210 }
211 else {
212 stage->point = passthrough_point;
213 }
214
215 if (draw->rasterizer->point_sprite) {
216 /* find vertex shader texcoord outputs */
217 const struct draw_vertex_shader *vs = draw->vertex_shader;
218 uint i, j = 0;
219 for (i = 0; i < vs->info.num_outputs; i++) {
220 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
221 wide->texcoord_slot[j] = i;
222 wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
223 j++;
224 }
225 }
226 wide->num_texcoords = j;
227 }
228
229 wide->psize_slot = -1;
230 if (draw->rasterizer->point_size_per_vertex) {
231 /* find PSIZ vertex output */
232 const struct draw_vertex_shader *vs = draw->vertex_shader;
233 uint i;
234 for (i = 0; i < vs->info.num_outputs; i++) {
235 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
236 wide->psize_slot = i;
237 break;
238 }
239 }
240 }
241
242 stage->point( stage, header );
243 }
244
245
246 static void widepoint_flush( struct draw_stage *stage, unsigned flags )
247 {
248 stage->point = widepoint_first_point;
249 stage->next->flush( stage->next, flags );
250 }
251
252
253 static void widepoint_reset_stipple_counter( struct draw_stage *stage )
254 {
255 stage->next->reset_stipple_counter( stage->next );
256 }
257
258
259 static void widepoint_destroy( struct draw_stage *stage )
260 {
261 draw_free_temp_verts( stage );
262 FREE( stage );
263 }
264
265
266 struct draw_stage *draw_wide_point_stage( struct draw_context *draw )
267 {
268 struct widepoint_stage *wide = CALLOC_STRUCT(widepoint_stage);
269
270 draw_alloc_temp_verts( &wide->stage, 4 );
271
272 wide->stage.draw = draw;
273 wide->stage.next = NULL;
274 wide->stage.point = widepoint_first_point;
275 wide->stage.line = widepoint_line;
276 wide->stage.tri = widepoint_tri;
277 wide->stage.flush = widepoint_flush;
278 wide->stage.reset_stipple_counter = widepoint_reset_stipple_counter;
279 wide->stage.destroy = widepoint_destroy;
280
281 return &wide->stage;
282 }