gallium: fix line emit order for unfilled tris
[mesa.git] / src / gallium / auxiliary / draw / draw_wide_prims.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_private.h"
35
36
37 struct wide_stage {
38 struct draw_stage stage;
39
40 float half_line_width;
41 float half_point_size;
42
43 uint texcoord_slot[PIPE_MAX_SHADER_OUTPUTS];
44 uint texcoord_mode[PIPE_MAX_SHADER_OUTPUTS];
45 uint num_texcoords;
46
47 int psize_slot;
48 };
49
50
51
52 static INLINE struct wide_stage *wide_stage( struct draw_stage *stage )
53 {
54 return (struct wide_stage *)stage;
55 }
56
57
58 static void passthrough_point( struct draw_stage *stage,
59 struct prim_header *header )
60 {
61 stage->next->point( stage->next, header );
62 }
63
64 static void passthrough_line( struct draw_stage *stage,
65 struct prim_header *header )
66 {
67 stage->next->line(stage->next, header);
68 }
69
70 static void passthrough_tri( struct draw_stage *stage,
71 struct prim_header *header )
72 {
73 stage->next->tri(stage->next, header);
74 }
75
76
77 /**
78 * Draw a wide line by drawing a quad (two triangles).
79 * XXX need to disable polygon stipple.
80 */
81 static void wide_line( struct draw_stage *stage,
82 struct prim_header *header )
83 {
84 const struct wide_stage *wide = wide_stage(stage);
85 const float half_width = wide->half_line_width;
86
87 struct prim_header tri;
88
89 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
90 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
91 struct vertex_header *v2 = dup_vert(stage, header->v[1], 2);
92 struct vertex_header *v3 = dup_vert(stage, header->v[1], 3);
93
94 float *pos0 = v0->data[0];
95 float *pos1 = v1->data[0];
96 float *pos2 = v2->data[0];
97 float *pos3 = v3->data[0];
98
99 const float dx = FABSF(pos0[0] - pos2[0]);
100 const float dy = FABSF(pos0[1] - pos2[1]);
101
102 /*
103 * Draw wide line as a quad (two tris) by "stretching" the line along
104 * X or Y.
105 * We need to tweak coords in several ways to be conformant here.
106 */
107
108 if (dx > dy) {
109 /* x-major line */
110 pos0[1] = pos0[1] - half_width - 0.25f;
111 pos1[1] = pos1[1] + half_width - 0.25f;
112 pos2[1] = pos2[1] - half_width - 0.25f;
113 pos3[1] = pos3[1] + half_width - 0.25f;
114 if (pos0[0] < pos2[0]) {
115 /* left to right line */
116 pos0[0] -= 0.5f;
117 pos1[0] -= 0.5f;
118 pos2[0] -= 0.5f;
119 pos3[0] -= 0.5f;
120 }
121 else {
122 /* right to left line */
123 pos0[0] += 0.5f;
124 pos1[0] += 0.5f;
125 pos2[0] += 0.5f;
126 pos3[0] += 0.5f;
127 }
128 }
129 else {
130 /* y-major line */
131 pos0[0] = pos0[0] - half_width + 0.25f;
132 pos1[0] = pos1[0] + half_width + 0.25f;
133 pos2[0] = pos2[0] - half_width + 0.25f;
134 pos3[0] = pos3[0] + half_width + 0.25f;
135 if (pos0[1] < pos2[1]) {
136 /* top to bottom line */
137 pos0[1] -= 0.5f;
138 pos1[1] -= 0.5f;
139 pos2[1] -= 0.5f;
140 pos3[1] -= 0.5f;
141 }
142 else {
143 /* bottom to top line */
144 pos0[1] += 0.5f;
145 pos1[1] += 0.5f;
146 pos2[1] += 0.5f;
147 pos3[1] += 0.5f;
148 }
149 }
150
151 tri.det = header->det; /* only the sign matters */
152 tri.v[0] = v0;
153 tri.v[1] = v2;
154 tri.v[2] = v3;
155 stage->next->tri( stage->next, &tri );
156
157 tri.v[0] = v0;
158 tri.v[1] = v3;
159 tri.v[2] = v1;
160 stage->next->tri( stage->next, &tri );
161 }
162
163
164 /**
165 * Set the vertex texcoords for sprite mode.
166 * Coords may be left untouched or set to a right-side-up or upside-down
167 * orientation.
168 */
169 static void set_texcoords(const struct wide_stage *wide,
170 struct vertex_header *v, const float tc[4])
171 {
172 uint i;
173 for (i = 0; i < wide->num_texcoords; i++) {
174 if (wide->texcoord_mode[i] != PIPE_SPRITE_COORD_NONE) {
175 uint j = wide->texcoord_slot[i];
176 v->data[j][0] = tc[0];
177 if (wide->texcoord_mode[i] == PIPE_SPRITE_COORD_LOWER_LEFT)
178 v->data[j][1] = 1.0f - tc[1];
179 else
180 v->data[j][1] = tc[1];
181 v->data[j][2] = tc[2];
182 v->data[j][3] = tc[3];
183 }
184 }
185 }
186
187
188 /* If there are lots of sprite points (and why wouldn't there be?) it
189 * would probably be more sensible to change hardware setup to
190 * optimize this rather than doing the whole thing in software like
191 * this.
192 */
193 static void wide_point( struct draw_stage *stage,
194 struct prim_header *header )
195 {
196 const struct wide_stage *wide = wide_stage(stage);
197 const boolean sprite = (boolean) stage->draw->rasterizer->point_sprite;
198 float half_size;
199 float left_adj, right_adj;
200
201 struct prim_header tri;
202
203 /* four dups of original vertex */
204 struct vertex_header *v0 = dup_vert(stage, header->v[0], 0);
205 struct vertex_header *v1 = dup_vert(stage, header->v[0], 1);
206 struct vertex_header *v2 = dup_vert(stage, header->v[0], 2);
207 struct vertex_header *v3 = dup_vert(stage, header->v[0], 3);
208
209 float *pos0 = v0->data[0];
210 float *pos1 = v1->data[0];
211 float *pos2 = v2->data[0];
212 float *pos3 = v3->data[0];
213
214 /* point size is either per-vertex or fixed size */
215 if (wide->psize_slot >= 0) {
216 half_size = 0.5f * header->v[0]->data[wide->psize_slot][0];
217 }
218 else {
219 half_size = wide->half_point_size;
220 }
221
222 left_adj = -half_size; /* + 0.25f;*/
223 right_adj = half_size; /* + 0.25f;*/
224
225 pos0[0] += left_adj;
226 pos0[1] -= half_size;
227
228 pos1[0] += left_adj;
229 pos1[1] += half_size;
230
231 pos2[0] += right_adj;
232 pos2[1] -= half_size;
233
234 pos3[0] += right_adj;
235 pos3[1] += half_size;
236
237 if (sprite) {
238 static const float tex00[4] = { 0, 0, 0, 1 };
239 static const float tex01[4] = { 0, 1, 0, 1 };
240 static const float tex11[4] = { 1, 1, 0, 1 };
241 static const float tex10[4] = { 1, 0, 0, 1 };
242 set_texcoords( wide, v0, tex00 );
243 set_texcoords( wide, v1, tex01 );
244 set_texcoords( wide, v2, tex10 );
245 set_texcoords( wide, v3, tex11 );
246 }
247
248 tri.det = header->det; /* only the sign matters */
249 tri.v[0] = v0;
250 tri.v[1] = v2;
251 tri.v[2] = v3;
252 stage->next->tri( stage->next, &tri );
253
254 tri.v[0] = v0;
255 tri.v[1] = v3;
256 tri.v[2] = v1;
257 stage->next->tri( stage->next, &tri );
258 }
259
260
261 static void wide_first_point( struct draw_stage *stage,
262 struct prim_header *header )
263 {
264 struct wide_stage *wide = wide_stage(stage);
265 struct draw_context *draw = stage->draw;
266
267 wide->half_point_size = 0.5f * draw->rasterizer->point_size;
268
269 /* XXX we won't know the real size if it's computed by the vertex shader! */
270 if (draw->rasterizer->point_size > draw->wide_point_threshold) {
271 stage->point = wide_point;
272 }
273 else {
274 stage->point = passthrough_point;
275 }
276
277 if (draw->rasterizer->point_sprite) {
278 /* find vertex shader texcoord outputs */
279 const struct draw_vertex_shader *vs = draw->vertex_shader;
280 uint i, j = 0;
281 for (i = 0; i < vs->info.num_outputs; i++) {
282 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
283 wide->texcoord_slot[j] = i;
284 wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
285 j++;
286 }
287 }
288 wide->num_texcoords = j;
289 }
290
291 wide->psize_slot = -1;
292
293 if (draw->rasterizer->point_size_per_vertex) {
294 /* find PSIZ vertex output */
295 const struct draw_vertex_shader *vs = draw->vertex_shader;
296 uint i;
297 for (i = 0; i < vs->info.num_outputs; i++) {
298 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
299 wide->psize_slot = i;
300 break;
301 }
302 }
303 }
304
305 stage->point( stage, header );
306 }
307
308
309
310 static void wide_first_line( struct draw_stage *stage,
311 struct prim_header *header )
312 {
313 struct wide_stage *wide = wide_stage(stage);
314 struct draw_context *draw = stage->draw;
315
316 wide->half_line_width = 0.5f * draw->rasterizer->line_width;
317
318 if (draw->rasterizer->line_width != 1.0) {
319 wide->stage.line = wide_line;
320 }
321 else {
322 wide->stage.line = passthrough_line;
323 }
324
325 stage->line( stage, header );
326 }
327
328
329 static void wide_flush( struct draw_stage *stage, unsigned flags )
330 {
331 stage->line = wide_first_line;
332 stage->point = wide_first_point;
333 stage->next->flush( stage->next, flags );
334 }
335
336
337 static void wide_reset_stipple_counter( struct draw_stage *stage )
338 {
339 stage->next->reset_stipple_counter( stage->next );
340 }
341
342
343 static void wide_destroy( struct draw_stage *stage )
344 {
345 draw_free_temp_verts( stage );
346 FREE( stage );
347 }
348
349
350 struct draw_stage *draw_wide_stage( struct draw_context *draw )
351 {
352 struct wide_stage *wide = CALLOC_STRUCT(wide_stage);
353
354 draw_alloc_temp_verts( &wide->stage, 4 );
355
356 wide->stage.draw = draw;
357 wide->stage.next = NULL;
358 wide->stage.point = wide_first_point;
359 wide->stage.line = wide_first_line;
360 wide->stage.tri = passthrough_tri;
361 wide->stage.flush = wide_flush;
362 wide->stage.reset_stipple_counter = wide_reset_stipple_counter;
363 wide->stage.destroy = wide_destroy;
364
365 return &wide->stage;
366 }