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