gallium: added draw_need_pipeline() predicate function
[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, bot_adj, top_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 const float xbias = 0.0, ybias = -0.125;
128
129 /* point size is either per-vertex or fixed size */
130 if (wide->psize_slot >= 0) {
131 half_size = 0.5f * header->v[0]->data[wide->psize_slot][0];
132 }
133 else {
134 half_size = wide->half_point_size;
135 }
136
137 left_adj = -half_size + xbias;
138 right_adj = half_size + xbias;
139 bot_adj = half_size + ybias;
140 top_adj = -half_size + ybias;
141
142 pos0[0] += left_adj;
143 pos0[1] += top_adj;
144
145 pos1[0] += left_adj;
146 pos1[1] += bot_adj;
147
148 pos2[0] += right_adj;
149 pos2[1] += top_adj;
150
151 pos3[0] += right_adj;
152 pos3[1] += bot_adj;
153
154 if (sprite) {
155 static const float tex00[4] = { 0, 0, 0, 1 };
156 static const float tex01[4] = { 0, 1, 0, 1 };
157 static const float tex11[4] = { 1, 1, 0, 1 };
158 static const float tex10[4] = { 1, 0, 0, 1 };
159 set_texcoords( wide, v0, tex00 );
160 set_texcoords( wide, v1, tex01 );
161 set_texcoords( wide, v2, tex10 );
162 set_texcoords( wide, v3, tex11 );
163 }
164
165 tri.det = header->det; /* only the sign matters */
166 tri.v[0] = v0;
167 tri.v[1] = v2;
168 tri.v[2] = v3;
169 stage->next->tri( stage->next, &tri );
170
171 tri.v[0] = v0;
172 tri.v[1] = v3;
173 tri.v[2] = v1;
174 stage->next->tri( stage->next, &tri );
175 }
176
177
178 static void widepoint_first_point( struct draw_stage *stage,
179 struct prim_header *header )
180 {
181 struct widepoint_stage *wide = widepoint_stage(stage);
182 struct draw_context *draw = stage->draw;
183
184 wide->half_point_size = 0.5f * draw->rasterizer->point_size;
185
186 /* XXX we won't know the real size if it's computed by the vertex shader! */
187 if (draw->rasterizer->point_size > draw->wide_point_threshold) {
188 stage->point = widepoint_point;
189 }
190 else {
191 stage->point = passthrough_point;
192 }
193
194 if (draw->rasterizer->point_sprite) {
195 /* find vertex shader texcoord outputs */
196 const struct draw_vertex_shader *vs = draw->vertex_shader;
197 uint i, j = 0;
198 for (i = 0; i < vs->info.num_outputs; i++) {
199 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
200 wide->texcoord_slot[j] = i;
201 wide->texcoord_mode[j] = draw->rasterizer->sprite_coord_mode[j];
202 j++;
203 }
204 }
205 wide->num_texcoords = j;
206 }
207
208 wide->psize_slot = -1;
209 if (draw->rasterizer->point_size_per_vertex) {
210 /* find PSIZ vertex output */
211 const struct draw_vertex_shader *vs = draw->vertex_shader;
212 uint i;
213 for (i = 0; i < vs->info.num_outputs; i++) {
214 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_PSIZE) {
215 wide->psize_slot = i;
216 break;
217 }
218 }
219 }
220
221 stage->point( stage, header );
222 }
223
224
225 static void widepoint_flush( struct draw_stage *stage, unsigned flags )
226 {
227 stage->point = widepoint_first_point;
228 stage->next->flush( stage->next, flags );
229 }
230
231
232 static void widepoint_reset_stipple_counter( struct draw_stage *stage )
233 {
234 stage->next->reset_stipple_counter( stage->next );
235 }
236
237
238 static void widepoint_destroy( struct draw_stage *stage )
239 {
240 draw_free_temp_verts( stage );
241 FREE( stage );
242 }
243
244
245 struct draw_stage *draw_wide_point_stage( struct draw_context *draw )
246 {
247 struct widepoint_stage *wide = CALLOC_STRUCT(widepoint_stage);
248
249 draw_alloc_temp_verts( &wide->stage, 4 );
250
251 wide->stage.draw = draw;
252 wide->stage.next = NULL;
253 wide->stage.point = widepoint_first_point;
254 wide->stage.line = widepoint_line;
255 wide->stage.tri = widepoint_tri;
256 wide->stage.flush = widepoint_flush;
257 wide->stage.reset_stipple_counter = widepoint_reset_stipple_counter;
258 wide->stage.destroy = widepoint_destroy;
259
260 return &wide->stage;
261 }