Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch_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 #include "util/u_memory.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vbuf.h"
37 #include "draw/draw_vertex.h"
38 #include "draw/draw_pt.h"
39 #include "translate/translate.h"
40 #include "translate/translate_cache.h"
41
42 /* The simplest 'middle end' in the new vertex code.
43 *
44 * The responsibilities of a middle end are to:
45 * - perform vertex fetch using
46 * - draw vertex element/buffer state
47 * - a list of fetch indices we received as an input
48 * - run the vertex shader
49 * - cliptest,
50 * - clip coord calculation
51 * - viewport transformation
52 * - if necessary, run the primitive pipeline, passing it:
53 * - a linear array of vertex_header vertices constructed here
54 * - a set of draw indices we received as an input
55 * - otherwise, drive the hw backend,
56 * - allocate space for hardware format vertices
57 * - translate the vertex-shader output vertices to hw format
58 * - calling the backend draw functions.
59 *
60 * For convenience, we provide a helper function to drive the hardware
61 * backend given similar inputs to those required to run the pipeline.
62 *
63 * In the case of passthrough mode, many of these actions are disabled
64 * or noops, so we end up doing:
65 *
66 * - perform vertex fetch
67 * - drive the hw backend
68 *
69 * IE, basically just vertex fetch to post-vs-format vertices,
70 * followed by a call to the backend helper function.
71 */
72
73
74 struct fetch_emit_middle_end {
75 struct draw_pt_middle_end base;
76 struct draw_context *draw;
77
78 struct translate *translate;
79 const struct vertex_info *vinfo;
80
81 /* Cache point size somewhere it's address won't change:
82 */
83 float point_size;
84
85 struct translate_cache *cache;
86 };
87
88
89
90
91 static void fetch_emit_prepare( struct draw_pt_middle_end *middle,
92 unsigned prim,
93 unsigned opt,
94 unsigned *max_vertices )
95 {
96 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
97 struct draw_context *draw = feme->draw;
98 const struct vertex_info *vinfo;
99 unsigned i, dst_offset;
100 boolean ok;
101 struct translate_key key;
102
103
104 ok = draw->render->set_primitive( draw->render,
105 prim );
106 if (!ok) {
107 assert(0);
108 return;
109 }
110
111 /* Must do this after set_primitive() above:
112 */
113 vinfo = feme->vinfo = draw->render->get_vertex_info(draw->render);
114
115
116
117 /* Transform from API vertices to HW vertices, skipping the
118 * pipeline_vertex intermediate step.
119 */
120 dst_offset = 0;
121 memset(&key, 0, sizeof(key));
122
123 for (i = 0; i < vinfo->num_attribs; i++) {
124 const struct pipe_vertex_element *src = &draw->pt.vertex_element[vinfo->attrib[i].src_index];
125
126 unsigned emit_sz = 0;
127 unsigned input_format = src->src_format;
128 unsigned input_buffer = src->vertex_buffer_index;
129 unsigned input_offset = src->src_offset;
130 unsigned output_format;
131
132 switch (vinfo->attrib[i].emit) {
133 case EMIT_4F:
134 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
135 emit_sz = 4 * sizeof(float);
136 break;
137 case EMIT_3F:
138 output_format = PIPE_FORMAT_R32G32B32_FLOAT;
139 emit_sz = 3 * sizeof(float);
140 break;
141 case EMIT_2F:
142 output_format = PIPE_FORMAT_R32G32_FLOAT;
143 emit_sz = 2 * sizeof(float);
144 break;
145 case EMIT_1F:
146 output_format = PIPE_FORMAT_R32_FLOAT;
147 emit_sz = 1 * sizeof(float);
148 break;
149 case EMIT_1F_PSIZE:
150 input_format = PIPE_FORMAT_R32_FLOAT;
151 input_buffer = draw->pt.nr_vertex_buffers;
152 input_offset = 0;
153 output_format = PIPE_FORMAT_R32_FLOAT;
154 emit_sz = 1 * sizeof(float);
155 break;
156 default:
157 assert(0);
158 output_format = PIPE_FORMAT_NONE;
159 emit_sz = 0;
160 continue;
161 }
162
163 key.element[i].input_format = input_format;
164 key.element[i].input_buffer = input_buffer;
165 key.element[i].input_offset = input_offset;
166 key.element[i].output_format = output_format;
167 key.element[i].output_offset = dst_offset;
168
169 dst_offset += emit_sz;
170 }
171
172 key.nr_elements = vinfo->num_attribs;
173 key.output_stride = vinfo->size * 4;
174
175 /* Don't bother with caching at this stage:
176 */
177 if (!feme->translate ||
178 translate_key_compare(&feme->translate->key, &key) != 0)
179 {
180 translate_key_sanitize(&key);
181 feme->translate = translate_cache_find(feme->cache,
182 &key);
183
184
185 feme->translate->set_buffer(feme->translate,
186 draw->pt.nr_vertex_buffers,
187 &feme->point_size,
188 0);
189 }
190
191 feme->point_size = draw->rasterizer->point_size;
192
193 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
194 feme->translate->set_buffer(feme->translate,
195 i,
196 ((char *)draw->pt.user.vbuffer[i] +
197 draw->pt.vertex_buffer[i].buffer_offset),
198 draw->pt.vertex_buffer[i].pitch );
199 }
200
201 *max_vertices = (draw->render->max_vertex_buffer_bytes /
202 (vinfo->size * 4));
203
204 /* Return an even number of verts.
205 * This prevents "parity" errors when splitting long triangle strips which
206 * can lead to front/back culling mix-ups.
207 * Every other triangle in a strip has an alternate front/back orientation
208 * so splitting at an odd position can cause the orientation of subsequent
209 * triangles to get reversed.
210 */
211 *max_vertices = *max_vertices & ~1;
212 }
213
214
215
216
217
218 static void fetch_emit_run( struct draw_pt_middle_end *middle,
219 const unsigned *fetch_elts,
220 unsigned fetch_count,
221 const ushort *draw_elts,
222 unsigned draw_count )
223 {
224 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
225 struct draw_context *draw = feme->draw;
226 void *hw_verts;
227
228 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
229 */
230 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
231
232 if (fetch_count >= UNDEFINED_VERTEX_ID) {
233 assert(0);
234 return;
235 }
236
237 hw_verts = draw->render->allocate_vertices( draw->render,
238 (ushort)feme->translate->key.output_stride,
239 (ushort)fetch_count );
240 if (!hw_verts) {
241 assert(0);
242 return;
243 }
244
245
246 /* Single routine to fetch vertices and emit HW verts.
247 */
248 feme->translate->run_elts( feme->translate,
249 fetch_elts,
250 fetch_count,
251 hw_verts );
252
253 if (0) {
254 unsigned i;
255 for (i = 0; i < fetch_count; i++) {
256 debug_printf("\n\nvertex %d:\n", i);
257 draw_dump_emitted_vertex( feme->vinfo,
258 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
259 }
260 }
261
262 /* XXX: Draw arrays path to avoid re-emitting index list again and
263 * again.
264 */
265 draw->render->draw( draw->render,
266 draw_elts,
267 draw_count );
268
269 /* Done -- that was easy, wasn't it:
270 */
271 draw->render->release_vertices( draw->render,
272 hw_verts,
273 feme->translate->key.output_stride,
274 fetch_count );
275
276 }
277
278
279 static void fetch_emit_run_linear( struct draw_pt_middle_end *middle,
280 unsigned start,
281 unsigned count )
282 {
283 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
284 struct draw_context *draw = feme->draw;
285 void *hw_verts;
286
287 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
288 */
289 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
290
291 if (count >= UNDEFINED_VERTEX_ID) {
292 assert(0);
293 return;
294 }
295
296 hw_verts = draw->render->allocate_vertices( draw->render,
297 (ushort)feme->translate->key.output_stride,
298 (ushort)count );
299 if (!hw_verts) {
300 assert(0);
301 return;
302 }
303
304 /* Single routine to fetch vertices and emit HW verts.
305 */
306 feme->translate->run( feme->translate,
307 start,
308 count,
309 hw_verts );
310
311 if (0) {
312 unsigned i;
313 for (i = 0; i < count; i++) {
314 debug_printf("\n\nvertex %d:\n", i);
315 draw_dump_emitted_vertex( feme->vinfo,
316 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i );
317 }
318 }
319
320 /* XXX: Draw arrays path to avoid re-emitting index list again and
321 * again.
322 */
323 draw->render->draw_arrays( draw->render,
324 0, /*start*/
325 count );
326
327 /* Done -- that was easy, wasn't it:
328 */
329 draw->render->release_vertices( draw->render,
330 hw_verts,
331 feme->translate->key.output_stride,
332 count );
333
334 }
335
336
337 static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle,
338 unsigned start,
339 unsigned count,
340 const ushort *draw_elts,
341 unsigned draw_count )
342 {
343 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
344 struct draw_context *draw = feme->draw;
345 void *hw_verts;
346
347 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
348 */
349 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
350
351 if (count >= UNDEFINED_VERTEX_ID)
352 return FALSE;
353
354 hw_verts = draw->render->allocate_vertices( draw->render,
355 (ushort)feme->translate->key.output_stride,
356 (ushort)count );
357 if (!hw_verts)
358 return FALSE;
359
360 /* Single routine to fetch vertices and emit HW verts.
361 */
362 feme->translate->run( feme->translate,
363 start,
364 count,
365 hw_verts );
366
367 /* XXX: Draw arrays path to avoid re-emitting index list again and
368 * again.
369 */
370 draw->render->draw( draw->render,
371 draw_elts,
372 draw_count );
373
374 /* Done -- that was easy, wasn't it:
375 */
376 draw->render->release_vertices( draw->render,
377 hw_verts,
378 feme->translate->key.output_stride,
379 count );
380
381 return TRUE;
382 }
383
384
385
386
387 static void fetch_emit_finish( struct draw_pt_middle_end *middle )
388 {
389 /* nothing to do */
390 }
391
392 static void fetch_emit_destroy( struct draw_pt_middle_end *middle )
393 {
394 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle;
395
396 if (feme->cache)
397 translate_cache_destroy(feme->cache);
398
399 FREE(middle);
400 }
401
402
403 struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw )
404 {
405 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end );
406 if (fetch_emit == NULL)
407 return NULL;
408
409 fetch_emit->cache = translate_cache_create();
410 if (!fetch_emit->cache) {
411 FREE(fetch_emit);
412 return NULL;
413 }
414
415 fetch_emit->base.prepare = fetch_emit_prepare;
416 fetch_emit->base.run = fetch_emit_run;
417 fetch_emit->base.run_linear = fetch_emit_run_linear;
418 fetch_emit->base.run_linear_elts = fetch_emit_run_linear_elts;
419 fetch_emit->base.finish = fetch_emit_finish;
420 fetch_emit->base.destroy = fetch_emit_destroy;
421
422 fetch_emit->draw = draw;
423
424 return &fetch_emit->base;
425 }
426