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 "draw/draw_gs.h"
40 #include "translate/translate.h"
41 #include "translate/translate_cache.h"
43 /* The simplest 'middle end' in the new vertex code.
45 * The responsibilities of a middle end are to:
46 * - perform vertex fetch using
47 * - draw vertex element/buffer state
48 * - a list of fetch indices we received as an input
49 * - run the vertex shader
51 * - clip coord calculation
52 * - viewport transformation
53 * - if necessary, run the primitive pipeline, passing it:
54 * - a linear array of vertex_header vertices constructed here
55 * - a set of draw indices we received as an input
56 * - otherwise, drive the hw backend,
57 * - allocate space for hardware format vertices
58 * - translate the vertex-shader output vertices to hw format
59 * - calling the backend draw functions.
61 * For convenience, we provide a helper function to drive the hardware
62 * backend given similar inputs to those required to run the pipeline.
64 * In the case of passthrough mode, many of these actions are disabled
65 * or noops, so we end up doing:
67 * - perform vertex fetch
68 * - drive the hw backend
70 * IE, basically just vertex fetch to post-vs-format vertices,
71 * followed by a call to the backend helper function.
75 struct fetch_emit_middle_end
{
76 struct draw_pt_middle_end base
;
77 struct draw_context
*draw
;
79 struct translate
*translate
;
80 const struct vertex_info
*vinfo
;
82 /* Cache point size somewhere it's address won't change:
86 struct translate_cache
*cache
;
92 static void fetch_emit_prepare( struct draw_pt_middle_end
*middle
,
95 unsigned *max_vertices
)
97 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
98 struct draw_context
*draw
= feme
->draw
;
99 const struct vertex_info
*vinfo
;
100 unsigned i
, dst_offset
;
102 struct translate_key key
;
104 unsigned gs_out_prim
= (draw
->gs
.geometry_shader
?
105 draw
->gs
.geometry_shader
->output_primitive
:
110 ok
= draw
->render
->set_primitive( draw
->render
,
117 /* Must do this after set_primitive() above:
119 vinfo
= feme
->vinfo
= draw
->render
->get_vertex_info(draw
->render
);
123 /* Transform from API vertices to HW vertices, skipping the
124 * pipeline_vertex intermediate step.
127 memset(&key
, 0, sizeof(key
));
129 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
130 const struct pipe_vertex_element
*src
= &draw
->pt
.vertex_element
[vinfo
->attrib
[i
].src_index
];
132 unsigned emit_sz
= 0;
133 unsigned input_format
= src
->src_format
;
134 unsigned input_buffer
= src
->vertex_buffer_index
;
135 unsigned input_offset
= src
->src_offset
;
136 unsigned output_format
;
138 output_format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
139 emit_sz
= draw_translate_vinfo_size(vinfo
->attrib
[i
].emit
);
141 if (vinfo
->attrib
[i
].emit
== EMIT_OMIT
)
144 if (vinfo
->attrib
[i
].emit
== EMIT_1F_PSIZE
) {
145 input_format
= PIPE_FORMAT_R32_FLOAT
;
146 input_buffer
= draw
->pt
.nr_vertex_buffers
;
150 key
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
151 key
.element
[i
].input_format
= input_format
;
152 key
.element
[i
].input_buffer
= input_buffer
;
153 key
.element
[i
].input_offset
= input_offset
;
154 key
.element
[i
].instance_divisor
= src
->instance_divisor
;
155 key
.element
[i
].output_format
= output_format
;
156 key
.element
[i
].output_offset
= dst_offset
;
158 dst_offset
+= emit_sz
;
161 key
.nr_elements
= vinfo
->num_attribs
;
162 key
.output_stride
= vinfo
->size
* 4;
164 /* Don't bother with caching at this stage:
166 if (!feme
->translate
||
167 translate_key_compare(&feme
->translate
->key
, &key
) != 0)
169 translate_key_sanitize(&key
);
170 feme
->translate
= translate_cache_find(feme
->cache
,
174 feme
->translate
->set_buffer(feme
->translate
,
175 draw
->pt
.nr_vertex_buffers
,
181 feme
->point_size
= draw
->rasterizer
->point_size
;
183 for (i
= 0; i
< draw
->pt
.nr_vertex_buffers
; i
++) {
184 feme
->translate
->set_buffer(feme
->translate
,
186 ((char *)draw
->pt
.user
.vbuffer
[i
] +
187 draw
->pt
.vertex_buffer
[i
].buffer_offset
),
188 draw
->pt
.vertex_buffer
[i
].stride
,
189 draw
->pt
.vertex_buffer
[i
].max_index
);
192 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
200 static void fetch_emit_run( struct draw_pt_middle_end
*middle
,
201 const unsigned *fetch_elts
,
202 unsigned fetch_count
,
203 const ushort
*draw_elts
,
205 unsigned prim_flags
)
207 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
208 struct draw_context
*draw
= feme
->draw
;
211 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
213 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
215 draw
->render
->allocate_vertices( draw
->render
,
216 (ushort
)feme
->translate
->key
.output_stride
,
217 (ushort
)fetch_count
);
219 hw_verts
= draw
->render
->map_vertices( draw
->render
);
226 /* Single routine to fetch vertices and emit HW verts.
228 feme
->translate
->run_elts( feme
->translate
,
236 for (i
= 0; i
< fetch_count
; i
++) {
237 debug_printf("\n\nvertex %d:\n", i
);
238 draw_dump_emitted_vertex( feme
->vinfo
,
239 (const uint8_t *)hw_verts
+ feme
->vinfo
->size
* 4 * i
);
243 draw
->render
->unmap_vertices( draw
->render
,
245 (ushort
)(fetch_count
- 1) );
247 /* XXX: Draw arrays path to avoid re-emitting index list again and
250 draw
->render
->draw_elements( draw
->render
,
254 /* Done -- that was easy, wasn't it:
256 draw
->render
->release_vertices( draw
->render
);
261 static void fetch_emit_run_linear( struct draw_pt_middle_end
*middle
,
264 unsigned prim_flags
)
266 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
267 struct draw_context
*draw
= feme
->draw
;
270 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
272 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
274 if (!draw
->render
->allocate_vertices( draw
->render
,
275 (ushort
)feme
->translate
->key
.output_stride
,
279 hw_verts
= draw
->render
->map_vertices( draw
->render
);
283 /* Single routine to fetch vertices and emit HW verts.
285 feme
->translate
->run( feme
->translate
,
293 for (i
= 0; i
< count
; i
++) {
294 debug_printf("\n\nvertex %d:\n", i
);
295 draw_dump_emitted_vertex( feme
->vinfo
,
296 (const uint8_t *)hw_verts
+ feme
->vinfo
->size
* 4 * i
);
300 draw
->render
->unmap_vertices( draw
->render
, 0, count
- 1 );
302 /* XXX: Draw arrays path to avoid re-emitting index list again and
305 draw
->render
->draw_arrays( draw
->render
, 0, count
);
307 /* Done -- that was easy, wasn't it:
309 draw
->render
->release_vertices( draw
->render
);
318 static boolean
fetch_emit_run_linear_elts( struct draw_pt_middle_end
*middle
,
321 const ushort
*draw_elts
,
323 unsigned prim_flags
)
325 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
326 struct draw_context
*draw
= feme
->draw
;
329 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
331 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
333 if (!draw
->render
->allocate_vertices( draw
->render
,
334 (ushort
)feme
->translate
->key
.output_stride
,
338 hw_verts
= draw
->render
->map_vertices( draw
->render
);
342 /* Single routine to fetch vertices and emit HW verts.
344 feme
->translate
->run( feme
->translate
,
350 draw
->render
->unmap_vertices( draw
->render
, 0, (ushort
)(count
- 1) );
352 /* XXX: Draw arrays path to avoid re-emitting index list again and
355 draw
->render
->draw_elements( draw
->render
,
359 /* Done -- that was easy, wasn't it:
361 draw
->render
->release_vertices( draw
->render
);
369 static void fetch_emit_finish( struct draw_pt_middle_end
*middle
)
374 static void fetch_emit_destroy( struct draw_pt_middle_end
*middle
)
376 struct fetch_emit_middle_end
*feme
= (struct fetch_emit_middle_end
*)middle
;
379 translate_cache_destroy(feme
->cache
);
385 struct draw_pt_middle_end
*draw_pt_fetch_emit( struct draw_context
*draw
)
387 struct fetch_emit_middle_end
*fetch_emit
= CALLOC_STRUCT( fetch_emit_middle_end
);
388 if (fetch_emit
== NULL
)
391 fetch_emit
->cache
= translate_cache_create();
392 if (!fetch_emit
->cache
) {
397 fetch_emit
->base
.prepare
= fetch_emit_prepare
;
398 fetch_emit
->base
.run
= fetch_emit_run
;
399 fetch_emit
->base
.run_linear
= fetch_emit_run_linear
;
400 fetch_emit
->base
.run_linear_elts
= fetch_emit_run_linear_elts
;
401 fetch_emit
->base
.finish
= fetch_emit_finish
;
402 fetch_emit
->base
.destroy
= fetch_emit_destroy
;
404 fetch_emit
->draw
= draw
;
406 return &fetch_emit
->base
;