draw: Pass-through pipe_buffer::max_index to translate.
[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 "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_private.h"
38 #include "draw/draw_vbuf.h"
39 #include "draw/draw_vertex.h"
40 #include "draw/draw_pt.h"
41 #include "draw/draw_vs.h"
42
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 unsigned *max_vertices )
75 {
76 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
77 struct draw_context *draw = fse->draw;
78 unsigned num_vs_inputs = draw->vs.vertex_shader->info.num_inputs;
79 const struct vertex_info *vinfo;
80 unsigned i;
81 unsigned nr_vbs = 0;
82
83
84 if (!draw->render->set_primitive( draw->render,
85 prim )) {
86 assert(0);
87 return;
88 }
89
90 /* Must do this after set_primitive() above:
91 */
92 fse->vinfo = vinfo = draw->render->get_vertex_info(draw->render);
93
94
95
96 fse->key.output_stride = vinfo->size * 4;
97 fse->key.nr_outputs = vinfo->num_attribs;
98 fse->key.nr_inputs = num_vs_inputs;
99
100 fse->key.nr_elements = MAX2(fse->key.nr_outputs, /* outputs - translate to hw format */
101 fse->key.nr_inputs); /* inputs - fetch from api format */
102
103 fse->key.viewport = !draw->identity_viewport;
104 fse->key.clip = !draw->bypass_clipping;
105 fse->key.const_vbuffers = 0;
106
107 memset(fse->key.element, 0,
108 fse->key.nr_elements * sizeof(fse->key.element[0]));
109
110 for (i = 0; i < num_vs_inputs; i++) {
111 const struct pipe_vertex_element *src = &draw->pt.vertex_element[i];
112 fse->key.element[i].in.format = src->src_format;
113
114 /* Consider ignoring these, ie make generated programs
115 * independent of this state:
116 */
117 fse->key.element[i].in.buffer = src->vertex_buffer_index;
118 fse->key.element[i].in.offset = src->src_offset;
119 nr_vbs = MAX2(nr_vbs, src->vertex_buffer_index + 1);
120 }
121
122 for (i = 0; i < 5 && i < nr_vbs; i++) {
123 if (draw->pt.vertex_buffer[i].stride == 0)
124 fse->key.const_vbuffers |= (1<<i);
125 }
126
127 if (0) debug_printf("%s: lookup const_vbuffers: %x\n", __FUNCTION__, fse->key.const_vbuffers);
128
129 {
130 unsigned dst_offset = 0;
131
132 for (i = 0; i < vinfo->num_attribs; i++) {
133 unsigned emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
134
135 /* doesn't handle EMIT_OMIT */
136 assert(emit_sz != 0);
137
138 /* The elements in the key correspond to vertex shader output
139 * numbers, not to positions in the hw vertex description --
140 * that's handled by the output_offset field.
141 */
142 fse->key.element[i].out.format = vinfo->attrib[i].emit;
143 fse->key.element[i].out.vs_output = vinfo->attrib[i].src_index;
144 fse->key.element[i].out.offset = dst_offset;
145
146 dst_offset += emit_sz;
147 assert(fse->key.output_stride >= dst_offset);
148 }
149 }
150
151
152 fse->active = draw_vs_lookup_varient( draw->vs.vertex_shader,
153 &fse->key );
154
155 if (!fse->active) {
156 assert(0);
157 return ;
158 }
159
160 if (0) debug_printf("%s: found const_vbuffers: %x\n", __FUNCTION__,
161 fse->active->key.const_vbuffers);
162
163 /* Now set buffer pointers:
164 */
165 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
166 fse->active->set_buffer( fse->active,
167 i,
168 ((const ubyte *) draw->pt.user.vbuffer[i] +
169 draw->pt.vertex_buffer[i].buffer_offset),
170 draw->pt.vertex_buffer[i].stride,
171 draw->pt.vertex_buffer[i].max_index );
172 }
173
174 *max_vertices = (draw->render->max_vertex_buffer_bytes /
175 (vinfo->size * 4));
176
177 /* Return an even number of verts.
178 * This prevents "parity" errors when splitting long triangle strips which
179 * can lead to front/back culling mix-ups.
180 * Every other triangle in a strip has an alternate front/back orientation
181 * so splitting at an odd position can cause the orientation of subsequent
182 * triangles to get reversed.
183 */
184 *max_vertices = *max_vertices & ~1;
185
186 /* Probably need to do this somewhere (or fix exec shader not to
187 * need it):
188 */
189 if (1) {
190 struct draw_vertex_shader *vs = draw->vs.vertex_shader;
191 vs->prepare(vs, draw);
192 }
193 }
194
195
196
197 static void fse_run_linear( struct draw_pt_middle_end *middle,
198 unsigned start,
199 unsigned count )
200 {
201 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
202 struct draw_context *draw = fse->draw;
203 char *hw_verts;
204
205 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
206 */
207 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
208
209 if (count >= UNDEFINED_VERTEX_ID)
210 goto fail;
211
212 if (!draw->render->allocate_vertices( draw->render,
213 (ushort)fse->key.output_stride,
214 (ushort)count ))
215 goto fail;
216
217 hw_verts = draw->render->map_vertices( draw->render );
218 if (!hw_verts)
219 goto fail;
220
221 /* Single routine to fetch vertices, run shader and emit HW verts.
222 * Clipping is done elsewhere -- either by the API or on hardware,
223 * or for some other reason not required...
224 */
225 fse->active->run_linear( fse->active,
226 start, count,
227 hw_verts );
228
229
230 if (0) {
231 unsigned i;
232 for (i = 0; i < count; i++) {
233 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__, i,
234 fse->key.output_stride,
235 fse->key.output_stride * i);
236
237 draw_dump_emitted_vertex( fse->vinfo,
238 (const uint8_t *)hw_verts + fse->key.output_stride * i );
239 }
240 }
241
242 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
243
244 /* Draw arrays path to avoid re-emitting index list again and
245 * again.
246 */
247 draw->render->draw_arrays( draw->render,
248 0,
249 count );
250
251
252 draw->render->release_vertices( draw->render );
253
254 return;
255
256 fail:
257 assert(0);
258 return;
259 }
260
261
262 static void
263 fse_run(struct draw_pt_middle_end *middle,
264 const unsigned *fetch_elts,
265 unsigned fetch_count,
266 const ushort *draw_elts,
267 unsigned draw_count )
268 {
269 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
270 struct draw_context *draw = fse->draw;
271 void *hw_verts;
272
273 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
274 */
275 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
276
277 if (fetch_count >= UNDEFINED_VERTEX_ID)
278 goto fail;
279
280 if (!draw->render->allocate_vertices( draw->render,
281 (ushort)fse->key.output_stride,
282 (ushort)fetch_count ))
283 goto fail;
284
285 hw_verts = draw->render->map_vertices( draw->render );
286 if (!hw_verts)
287 goto fail;
288
289
290 /* Single routine to fetch vertices, run shader and emit HW verts.
291 */
292 fse->active->run_elts( fse->active,
293 fetch_elts,
294 fetch_count,
295 hw_verts );
296
297
298 if (0) {
299 unsigned i;
300 for (i = 0; i < fetch_count; i++) {
301 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
302 draw_dump_emitted_vertex( fse->vinfo,
303 (const uint8_t *)hw_verts +
304 fse->key.output_stride * i );
305 }
306 }
307
308 draw->render->unmap_vertices( draw->render, 0, (ushort)(fetch_count - 1) );
309
310 draw->render->draw( draw->render,
311 draw_elts,
312 draw_count );
313
314
315 draw->render->release_vertices( draw->render );
316 return;
317
318 fail:
319 assert(0);
320 return;
321 }
322
323
324
325 static boolean fse_run_linear_elts( struct draw_pt_middle_end *middle,
326 unsigned start,
327 unsigned count,
328 const ushort *draw_elts,
329 unsigned draw_count )
330 {
331 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
332 struct draw_context *draw = fse->draw;
333 char *hw_verts;
334
335 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
336 */
337 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
338
339 if (count >= UNDEFINED_VERTEX_ID)
340 return FALSE;
341
342 if (!draw->render->allocate_vertices( draw->render,
343 (ushort)fse->key.output_stride,
344 (ushort)count ))
345 return FALSE;
346
347 hw_verts = draw->render->map_vertices( draw->render );
348 if (!hw_verts)
349 return FALSE;
350
351 /* Single routine to fetch vertices, run shader and emit HW verts.
352 * Clipping is done elsewhere -- either by the API or on hardware,
353 * or for some other reason not required...
354 */
355 fse->active->run_linear( fse->active,
356 start, count,
357 hw_verts );
358
359
360 draw->render->draw( draw->render,
361 draw_elts,
362 draw_count );
363
364
365 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
366
367 draw->render->release_vertices( draw->render );
368
369 return TRUE;
370 }
371
372
373
374 static void fse_finish( struct draw_pt_middle_end *middle )
375 {
376 }
377
378
379 static void
380 fse_destroy( struct draw_pt_middle_end *middle )
381 {
382 FREE(middle);
383 }
384
385 struct draw_pt_middle_end *draw_pt_middle_fse( struct draw_context *draw )
386 {
387 struct fetch_shade_emit *fse = CALLOC_STRUCT(fetch_shade_emit);
388 if (!fse)
389 return NULL;
390
391 fse->base.prepare = fse_prepare;
392 fse->base.run = fse_run;
393 fse->base.run_linear = fse_run_linear;
394 fse->base.run_linear_elts = fse_run_linear_elts;
395 fse->base.finish = fse_finish;
396 fse->base.destroy = fse_destroy;
397 fse->draw = draw;
398
399 return &fse->base;
400 }