draw: support psize in vs_varient paths
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_shade_emit.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 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "pipe/p_util.h"
35 #include "draw/draw_context.h"
36 #include "draw/draw_private.h"
37 #include "draw/draw_vbuf.h"
38 #include "draw/draw_vertex.h"
39 #include "draw/draw_pt.h"
40 #include "draw/draw_vs.h"
41
42 #include "translate/translate.h"
43
44 struct fetch_shade_emit;
45
46
47 /* Prototype fetch, shade, emit-hw-verts all in one go.
48 */
49 struct fetch_shade_emit {
50 struct draw_pt_middle_end base;
51 struct draw_context *draw;
52
53
54 /* Temporaries:
55 */
56 const float *constants;
57 unsigned pitch[PIPE_MAX_ATTRIBS];
58 const ubyte *src[PIPE_MAX_ATTRIBS];
59 unsigned prim;
60
61 struct draw_vs_varient_key key;
62 struct draw_vs_varient *active;
63
64
65 const struct vertex_info *vinfo;
66 };
67
68
69
70
71 static void fse_prepare( struct draw_pt_middle_end *middle,
72 unsigned prim,
73 unsigned opt )
74 {
75 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
76 struct draw_context *draw = fse->draw;
77 unsigned num_vs_inputs = draw->vs.vertex_shader->info.num_inputs;
78 const struct vertex_info *vinfo;
79 unsigned i;
80
81
82 if (!draw->render->set_primitive( draw->render,
83 prim )) {
84 assert(0);
85 return;
86 }
87
88 /* Must do this after set_primitive() above:
89 */
90 fse->vinfo = vinfo = draw->render->get_vertex_info(draw->render);
91
92
93
94 fse->key.output_stride = vinfo->size * 4;
95 fse->key.nr_outputs = vinfo->num_attribs;
96 fse->key.nr_inputs = num_vs_inputs;
97
98 fse->key.nr_elements = MAX2(fse->key.nr_outputs, /* outputs - translate to hw format */
99 fse->key.nr_inputs); /* inputs - fetch from api format */
100
101 fse->key.viewport = !draw->identity_viewport;
102 fse->key.clip = !draw->bypass_clipping;
103 fse->key.pad = 0;
104
105 memset(fse->key.element, 0,
106 fse->key.nr_elements * sizeof(fse->key.element[0]));
107
108 for (i = 0; i < num_vs_inputs; i++) {
109 const struct pipe_vertex_element *src = &draw->pt.vertex_element[i];
110 fse->key.element[i].in.format = src->src_format;
111
112 /* Consider ignoring these, ie make generated programs
113 * independent of this state:
114 */
115 fse->key.element[i].in.buffer = src->vertex_buffer_index;
116 fse->key.element[i].in.offset = src->src_offset;
117 }
118
119
120 {
121 unsigned dst_offset = 0;
122
123 for (i = 0; i < vinfo->num_attribs; i++) {
124 unsigned emit_sz = 0;
125
126 switch (vinfo->emit[i]) {
127 case EMIT_4F:
128 emit_sz = 4 * sizeof(float);
129 break;
130 case EMIT_3F:
131 emit_sz = 3 * sizeof(float);
132 break;
133 case EMIT_2F:
134 emit_sz = 2 * sizeof(float);
135 break;
136 case EMIT_1F:
137 emit_sz = 1 * sizeof(float);
138 break;
139 case EMIT_1F_PSIZE:
140 emit_sz = 1 * sizeof(float);
141 break;
142 case EMIT_4UB:
143 emit_sz = 4 * sizeof(ubyte);
144 break;
145 default:
146 assert(0);
147 break;
148 }
149
150 /* The elements in the key correspond to vertex shader output
151 * numbers, not to positions in the hw vertex description --
152 * that's handled by the output_offset field.
153 */
154 fse->key.element[i].out.format = vinfo->emit[i];
155 fse->key.element[i].out.vs_output = vinfo->src_index[i];
156 fse->key.element[i].out.offset = dst_offset;
157
158 dst_offset += emit_sz;
159 assert(fse->key.output_stride >= dst_offset);
160 }
161 }
162
163
164 /* Would normally look up a vertex shader and peruse its list of
165 * varients somehow. We omitted that step and put all the
166 * hardcoded "shaders" into an array. We're just making the
167 * assumption that this happens to be a matching shader... ie
168 * you're running isosurf, aren't you?
169 */
170 fse->active = draw_vs_lookup_varient( draw->vs.vertex_shader,
171 &fse->key );
172
173 if (!fse->active) {
174 assert(0);
175 return ;
176 }
177
178 /* Now set buffer pointers:
179 */
180 for (i = 0; i < num_vs_inputs; i++) {
181 unsigned buf = draw->pt.vertex_element[i].vertex_buffer_index;
182
183 fse->active->set_input( fse->active,
184 i,
185
186 ((const ubyte *) draw->pt.user.vbuffer[buf] +
187 draw->pt.vertex_buffer[buf].buffer_offset),
188
189 draw->pt.vertex_buffer[buf].pitch );
190 }
191
192 fse->active->set_constants( fse->active,
193 (const float (*)[4])draw->pt.user.constants );
194
195 fse->active->set_viewport( fse->active,
196 &draw->viewport );
197
198 //return TRUE;
199 }
200
201
202
203
204
205
206
207 static void fse_run_linear( struct draw_pt_middle_end *middle,
208 unsigned start,
209 unsigned count )
210 {
211 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
212 struct draw_context *draw = fse->draw;
213 unsigned alloc_count = align(count, 4);
214 char *hw_verts;
215
216 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
217 */
218 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
219
220 hw_verts = draw->render->allocate_vertices( draw->render,
221 (ushort)fse->key.output_stride,
222 (ushort)alloc_count );
223
224 if (!hw_verts) {
225 assert(0);
226 return;
227 }
228
229 /* Single routine to fetch vertices, run shader and emit HW verts.
230 * Clipping is done elsewhere -- either by the API or on hardware,
231 * or for some other reason not required...
232 */
233 fse->active->run_linear( fse->active,
234 start, count,
235 hw_verts );
236
237 /* Draw arrays path to avoid re-emitting index list again and
238 * again.
239 */
240 draw->render->draw_arrays( draw->render,
241 0,
242 count );
243
244 if (0) {
245 unsigned i;
246 for (i = 0; i < count; i++) {
247 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__, i,
248 fse->key.output_stride,
249 fse->key.output_stride * i);
250
251 draw_dump_emitted_vertex( fse->vinfo,
252 (const uint8_t *)hw_verts + fse->key.output_stride * i );
253 }
254 }
255
256
257 draw->render->release_vertices( draw->render,
258 hw_verts,
259 fse->key.output_stride,
260 count );
261 }
262
263
264 static void
265 fse_run(struct draw_pt_middle_end *middle,
266 const unsigned *fetch_elts,
267 unsigned fetch_count,
268 const ushort *draw_elts,
269 unsigned draw_count )
270 {
271 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
272 struct draw_context *draw = fse->draw;
273 unsigned alloc_count = align(fetch_count, 4);
274 void *hw_verts;
275
276 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
277 */
278 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
279
280 hw_verts = draw->render->allocate_vertices( draw->render,
281 (ushort)fse->key.output_stride,
282 (ushort)alloc_count );
283 if (!hw_verts) {
284 assert(0);
285 return;
286 }
287
288
289 /* Single routine to fetch vertices, run shader and emit HW verts.
290 */
291 fse->active->run_elts( fse->active,
292 fetch_elts,
293 fetch_count,
294 hw_verts );
295
296 draw->render->draw( draw->render,
297 draw_elts,
298 draw_count );
299
300 if (0) {
301 unsigned i;
302 for (i = 0; i < fetch_count; i++) {
303 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
304 draw_dump_emitted_vertex( fse->vinfo,
305 (const uint8_t *)hw_verts +
306 fse->key.output_stride * i );
307 }
308 }
309
310
311 draw->render->release_vertices( draw->render,
312 hw_verts,
313 fse->key.output_stride,
314 fetch_count );
315
316 }
317
318
319 static void fse_finish( struct draw_pt_middle_end *middle )
320 {
321 }
322
323
324 static void
325 fse_destroy( struct draw_pt_middle_end *middle )
326 {
327 FREE(middle);
328 }
329
330 struct draw_pt_middle_end *draw_pt_middle_fse( struct draw_context *draw )
331 {
332 struct fetch_shade_emit *fse = CALLOC_STRUCT(fetch_shade_emit);
333 if (!fse)
334 return NULL;
335
336 fse->base.prepare = fse_prepare;
337 fse->base.run = fse_run;
338 fse->base.run_linear = fse_run_linear;
339 fse->base.finish = fse_finish;
340 fse->base.destroy = fse_destroy;
341 fse->draw = draw;
342
343 return &fse->base;
344 }