1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
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"
42 /* The simplest 'middle end' in the new vertex code.
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
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.
60 * For convenience, we provide a helper function to drive the hardware
61 * backend given similar inputs to those required to run the pipeline.
63 * In the case of passthrough mode, many of these actions are disabled
64 * or noops, so we end up doing:
66 * - perform vertex fetch
67 * - drive the hw backend
69 * IE, basically just vertex fetch to post-vs-format vertices,
70 * followed by a call to the backend helper function.
74 struct fetch_emit_middle_end
{
75 struct draw_pt_middle_end base
;
76 struct draw_context
*draw
;
78 struct translate
*translate
;
79 const struct vertex_info
*vinfo
;
81 /* Cache point size somewhere it's address won't change:
85 struct translate_cache
*cache
;
91 static void fetch_emit_prepare( struct draw_pt_middle_end
*middle
,
94 unsigned *max_vertices
)
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
;
101 struct translate_key key
;
104 ok
= draw
->render
->set_primitive( draw
->render
,
111 /* Must do this after set_primitive() above:
113 vinfo
= feme
->vinfo
= draw
->render
->get_vertex_info(draw
->render
);
117 /* Transform from API vertices to HW vertices, skipping the
118 * pipeline_vertex intermediate step.
121 memset(&key
, 0, sizeof(key
));
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
];
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
;
132 switch (vinfo
->attrib
[i
].emit
) {
134 output_format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
135 emit_sz
= 4 * sizeof(unsigned char);
138 output_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
139 emit_sz
= 4 * sizeof(float);
142 output_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
143 emit_sz
= 3 * sizeof(float);
146 output_format
= PIPE_FORMAT_R32G32_FLOAT
;
147 emit_sz
= 2 * sizeof(float);
150 output_format
= PIPE_FORMAT_R32_FLOAT
;
151 emit_sz
= 1 * sizeof(float);
154 input_format
= PIPE_FORMAT_R32_FLOAT
;
155 input_buffer
= draw
->pt
.nr_vertex_buffers
;
157 output_format
= PIPE_FORMAT_R32_FLOAT
;
158 emit_sz
= 1 * sizeof(float);
164 output_format
= PIPE_FORMAT_NONE
;
169 key
.element
[i
].input_format
= input_format
;
170 key
.element
[i
].input_buffer
= input_buffer
;
171 key
.element
[i
].input_offset
= input_offset
;
172 key
.element
[i
].output_format
= output_format
;
173 key
.element
[i
].output_offset
= dst_offset
;
175 dst_offset
+= emit_sz
;
178 key
.nr_elements
= vinfo
->num_attribs
;
179 key
.output_stride
= vinfo
->size
* 4;
181 /* Don't bother with caching at this stage:
183 if (!feme
->translate
||
184 translate_key_compare(&feme
->translate
->key
, &key
) != 0)
186 translate_key_sanitize(&key
);
187 feme
->translate
= translate_cache_find(feme
->cache
,
191 feme
->translate
->set_buffer(feme
->translate
,
192 draw
->pt
.nr_vertex_buffers
,
197 feme
->point_size
= draw
->rasterizer
->point_size
;
199 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
200 feme
->translate
->set_buffer(feme
->translate
,
202 ((char *)draw
->pt
.user
.vbuffer
[i
] +
203 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
204 draw
->pt
.vertex_buffer
[i
].stride
);
207 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
210 /* Return an even number of verts.
211 * This prevents "parity" errors when splitting long triangle strips which
212 * can lead to front/back culling mix-ups.
213 * Every other triangle in a strip has an alternate front/back orientation
214 * so splitting at an odd position can cause the orientation of subsequent
215 * triangles to get reversed.
217 *max_vertices
= *max_vertices
& ~1;
224 static void fetch_emit_run( struct draw_pt_middle_end
*middle
,
225 const unsigned *fetch_elts
,
226 unsigned fetch_count
,
227 const ushort
*draw_elts
,
228 unsigned draw_count
)
230 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
231 struct draw_context
*draw
= feme
->draw
;
234 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
236 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
238 if (fetch_count
>= UNDEFINED_VERTEX_ID
) {
243 draw
->render
->allocate_vertices( draw
->render
,
244 (ushort
)feme
->translate
->key
.output_stride
,
245 (ushort
)fetch_count
);
247 hw_verts
= draw
->render
->map_vertices( draw
->render
);
254 /* Single routine to fetch vertices and emit HW verts.
256 feme
->translate
->run_elts( feme
->translate
,
263 for (i
= 0; i
< fetch_count
; i
++) {
264 debug_printf("\n\nvertex %d:\n", i
);
265 draw_dump_emitted_vertex( feme
->vinfo
,
266 (const uint8_t *)hw_verts
+ feme
->vinfo
->size
* 4 * i
);
270 draw
->render
->unmap_vertices( draw
->render
,
272 (ushort
)(fetch_count
- 1) );
274 /* XXX: Draw arrays path to avoid re-emitting index list again and
277 draw
->render
->draw( draw
->render
,
281 /* Done -- that was easy, wasn't it:
283 draw
->render
->release_vertices( draw
->render
);
288 static void fetch_emit_run_linear( struct draw_pt_middle_end
*middle
,
292 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
293 struct draw_context
*draw
= feme
->draw
;
296 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
298 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
300 if (count
>= UNDEFINED_VERTEX_ID
)
303 if (!draw
->render
->allocate_vertices( draw
->render
,
304 (ushort
)feme
->translate
->key
.output_stride
,
308 hw_verts
= draw
->render
->map_vertices( draw
->render
);
312 /* Single routine to fetch vertices and emit HW verts.
314 feme
->translate
->run( feme
->translate
,
321 for (i
= 0; i
< count
; i
++) {
322 debug_printf("\n\nvertex %d:\n", i
);
323 draw_dump_emitted_vertex( feme
->vinfo
,
324 (const uint8_t *)hw_verts
+ feme
->vinfo
->size
* 4 * i
);
328 draw
->render
->unmap_vertices( draw
->render
, 0, count
- 1 );
330 /* XXX: Draw arrays path to avoid re-emitting index list again and
333 draw
->render
->draw_arrays( draw
->render
, 0, count
);
335 /* Done -- that was easy, wasn't it:
337 draw
->render
->release_vertices( draw
->render
);
346 static boolean
fetch_emit_run_linear_elts( struct draw_pt_middle_end
*middle
,
349 const ushort
*draw_elts
,
350 unsigned draw_count
)
352 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
353 struct draw_context
*draw
= feme
->draw
;
356 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
358 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
360 if (count
>= UNDEFINED_VERTEX_ID
)
363 if (!draw
->render
->allocate_vertices( draw
->render
,
364 (ushort
)feme
->translate
->key
.output_stride
,
368 hw_verts
= draw
->render
->map_vertices( draw
->render
);
372 /* Single routine to fetch vertices and emit HW verts.
374 feme
->translate
->run( feme
->translate
,
379 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
381 /* XXX: Draw arrays path to avoid re-emitting index list again and
384 draw
->render
->draw( draw
->render
,
388 /* Done -- that was easy, wasn't it:
390 draw
->render
->release_vertices( draw
->render
);
398 static void fetch_emit_finish( struct draw_pt_middle_end
*middle
)
403 static void fetch_emit_destroy( struct draw_pt_middle_end
*middle
)
405 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
408 translate_cache_destroy(feme
->cache
);
414 struct draw_pt_middle_end
*draw_pt_fetch_emit( struct draw_context
*draw
)
416 struct fetch_emit_middle_end
*fetch_emit
= CALLOC_STRUCT( fetch_emit_middle_end
);
417 if (fetch_emit
== NULL
)
420 fetch_emit
->cache
= translate_cache_create();
421 if (!fetch_emit
->cache
) {
426 fetch_emit
->base
.prepare
= fetch_emit_prepare
;
427 fetch_emit
->base
.run
= fetch_emit_run
;
428 fetch_emit
->base
.run_linear
= fetch_emit_run_linear
;
429 fetch_emit
->base
.run_linear_elts
= fetch_emit_run_linear_elts
;
430 fetch_emit
->base
.finish
= fetch_emit_finish
;
431 fetch_emit
->base
.destroy
= fetch_emit_destroy
;
433 fetch_emit
->draw
= draw
;
435 return &fetch_emit
->base
;