Merge branch '7.8'
[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 }
172
173 *max_vertices = (draw->render->max_vertex_buffer_bytes /
174 (vinfo->size * 4));
175
176 /* Return an even number of verts.
177 * This prevents "parity" errors when splitting long triangle strips which
178 * can lead to front/back culling mix-ups.
179 * Every other triangle in a strip has an alternate front/back orientation
180 * so splitting at an odd position can cause the orientation of subsequent
181 * triangles to get reversed.
182 */
183 *max_vertices = *max_vertices & ~1;
184
185 /* Probably need to do this somewhere (or fix exec shader not to
186 * need it):
187 */
188 if (1) {
189 struct draw_vertex_shader *vs = draw->vs.vertex_shader;
190 vs->prepare(vs, draw);
191 }
192 }
193
194
195
196 static void fse_run_linear( struct draw_pt_middle_end *middle,
197 unsigned start,
198 unsigned count )
199 {
200 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
201 struct draw_context *draw = fse->draw;
202 char *hw_verts;
203
204 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
205 */
206 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
207
208 if (count >= UNDEFINED_VERTEX_ID)
209 goto fail;
210
211 if (!draw->render->allocate_vertices( draw->render,
212 (ushort)fse->key.output_stride,
213 (ushort)count ))
214 goto fail;
215
216 hw_verts = draw->render->map_vertices( draw->render );
217 if (!hw_verts)
218 goto fail;
219
220 /* Single routine to fetch vertices, run shader and emit HW verts.
221 * Clipping is done elsewhere -- either by the API or on hardware,
222 * or for some other reason not required...
223 */
224 fse->active->run_linear( fse->active,
225 start, count,
226 hw_verts );
227
228
229 if (0) {
230 unsigned i;
231 for (i = 0; i < count; i++) {
232 debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n", __FUNCTION__, i,
233 fse->key.output_stride,
234 fse->key.output_stride * i);
235
236 draw_dump_emitted_vertex( fse->vinfo,
237 (const uint8_t *)hw_verts + fse->key.output_stride * i );
238 }
239 }
240
241 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
242
243 /* Draw arrays path to avoid re-emitting index list again and
244 * again.
245 */
246 draw->render->draw_arrays( draw->render,
247 0,
248 count );
249
250
251 draw->render->release_vertices( draw->render );
252
253 return;
254
255 fail:
256 assert(0);
257 return;
258 }
259
260
261 static void
262 fse_run(struct draw_pt_middle_end *middle,
263 const unsigned *fetch_elts,
264 unsigned fetch_count,
265 const ushort *draw_elts,
266 unsigned draw_count )
267 {
268 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
269 struct draw_context *draw = fse->draw;
270 void *hw_verts;
271
272 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
273 */
274 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
275
276 if (fetch_count >= UNDEFINED_VERTEX_ID)
277 goto fail;
278
279 if (!draw->render->allocate_vertices( draw->render,
280 (ushort)fse->key.output_stride,
281 (ushort)fetch_count ))
282 goto fail;
283
284 hw_verts = draw->render->map_vertices( draw->render );
285 if (!hw_verts)
286 goto fail;
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
297 if (0) {
298 unsigned i;
299 for (i = 0; i < fetch_count; i++) {
300 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
301 draw_dump_emitted_vertex( fse->vinfo,
302 (const uint8_t *)hw_verts +
303 fse->key.output_stride * i );
304 }
305 }
306
307 draw->render->unmap_vertices( draw->render, 0, (ushort)(fetch_count - 1) );
308
309 draw->render->draw( draw->render,
310 draw_elts,
311 draw_count );
312
313
314 draw->render->release_vertices( draw->render );
315 return;
316
317 fail:
318 assert(0);
319 return;
320 }
321
322
323
324 static boolean fse_run_linear_elts( struct draw_pt_middle_end *middle,
325 unsigned start,
326 unsigned count,
327 const ushort *draw_elts,
328 unsigned draw_count )
329 {
330 struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
331 struct draw_context *draw = fse->draw;
332 char *hw_verts;
333
334 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
335 */
336 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
337
338 if (count >= UNDEFINED_VERTEX_ID)
339 return FALSE;
340
341 if (!draw->render->allocate_vertices( draw->render,
342 (ushort)fse->key.output_stride,
343 (ushort)count ))
344 return FALSE;
345
346 hw_verts = draw->render->map_vertices( draw->render );
347 if (!hw_verts)
348 return FALSE;
349
350 /* Single routine to fetch vertices, run shader and emit HW verts.
351 * Clipping is done elsewhere -- either by the API or on hardware,
352 * or for some other reason not required...
353 */
354 fse->active->run_linear( fse->active,
355 start, count,
356 hw_verts );
357
358
359 draw->render->draw( draw->render,
360 draw_elts,
361 draw_count );
362
363
364 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) );
365
366 draw->render->release_vertices( draw->render );
367
368 return TRUE;
369 }
370
371
372
373 static void fse_finish( struct draw_pt_middle_end *middle )
374 {
375 }
376
377
378 static void
379 fse_destroy( struct draw_pt_middle_end *middle )
380 {
381 FREE(middle);
382 }
383
384 struct draw_pt_middle_end *draw_pt_middle_fse( struct draw_context *draw )
385 {
386 struct fetch_shade_emit *fse = CALLOC_STRUCT(fetch_shade_emit);
387 if (!fse)
388 return NULL;
389
390 fse->base.prepare = fse_prepare;
391 fse->base.run = fse_run;
392 fse->base.run_linear = fse_run_linear;
393 fse->base.run_linear_elts = fse_run_linear_elts;
394 fse->base.finish = fse_finish;
395 fse->base.destroy = fse_destroy;
396 fse->draw = draw;
397
398 return &fse->base;
399 }