draw: share machine
[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 //return TRUE;
193 }
194
195
196
197
198
199
200
201 static void fse_run_linear( struct draw_pt_middle_end *middle,
202 unsigned start,
203 unsigned count )
204 {
205 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
206 struct draw_context *draw = fse->draw;
207 unsigned alloc_count = align(count, 4);
208 char *hw_verts;
209
210 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
211 */
212 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
213
214 hw_verts = draw->render->allocate_vertices( draw->render,
215 (ushort)fse->key.output_stride,
216 (ushort)alloc_count );
217
218 if (!hw_verts) {
219 assert(0);
220 return;
221 }
222
223 /* Single routine to fetch vertices, run shader and emit HW verts.
224 * Clipping is done elsewhere -- either by the API or on hardware,
225 * or for some other reason not required...
226 */
227 fse->active->run_linear( fse->active,
228 start, count,
229 hw_verts );
230
231 /* Draw arrays path to avoid re-emitting index list again and
232 * again.
233 */
234 draw->render->draw_arrays( draw->render,
235 0,
236 count );
237
238 if (0) {
239 unsigned i;
240 for (i = 0; i < count; i++) {
241 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__, i,
242 fse->key.output_stride,
243 fse->key.output_stride * i);
244
245 draw_dump_emitted_vertex( fse->vinfo,
246 (const uint8_t *)hw_verts + fse->key.output_stride * i );
247 }
248 }
249
250
251 draw->render->release_vertices( draw->render,
252 hw_verts,
253 fse->key.output_stride,
254 count );
255 }
256
257
258 static void
259 fse_run(struct draw_pt_middle_end *middle,
260 const unsigned *fetch_elts,
261 unsigned fetch_count,
262 const ushort *draw_elts,
263 unsigned draw_count )
264 {
265 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
266 struct draw_context *draw = fse->draw;
267 unsigned alloc_count = align(fetch_count, 4);
268 void *hw_verts;
269
270 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
271 */
272 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
273
274 hw_verts = draw->render->allocate_vertices( draw->render,
275 (ushort)fse->key.output_stride,
276 (ushort)alloc_count );
277 if (!hw_verts) {
278 assert(0);
279 return;
280 }
281
282
283 /* Single routine to fetch vertices, run shader and emit HW verts.
284 */
285 fse->active->run_elts( fse->active,
286 fetch_elts,
287 fetch_count,
288 hw_verts );
289
290 draw->render->draw( draw->render,
291 draw_elts,
292 draw_count );
293
294 if (0) {
295 unsigned i;
296 for (i = 0; i < fetch_count; i++) {
297 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
298 draw_dump_emitted_vertex( fse->vinfo,
299 (const uint8_t *)hw_verts +
300 fse->key.output_stride * i );
301 }
302 }
303
304
305 draw->render->release_vertices( draw->render,
306 hw_verts,
307 fse->key.output_stride,
308 fetch_count );
309
310 }
311
312
313 static void fse_finish( struct draw_pt_middle_end *middle )
314 {
315 }
316
317
318 static void
319 fse_destroy( struct draw_pt_middle_end *middle )
320 {
321 FREE(middle);
322 }
323
324 struct draw_pt_middle_end *draw_pt_middle_fse( struct draw_context *draw )
325 {
326 struct fetch_shade_emit *fse = CALLOC_STRUCT(fetch_shade_emit);
327 if (!fse)
328 return NULL;
329
330 fse->base.prepare = fse_prepare;
331 fse->base.run = fse_run;
332 fse->base.run_linear = fse_run_linear;
333 fse->base.finish = fse_finish;
334 fse->base.destroy = fse_destroy;
335 fse->draw = draw;
336
337 return &fse->base;
338 }