1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
28 #include "util/u_memory.h"
29 #include "draw/draw_context.h"
30 #include "draw/draw_private.h"
31 #include "draw/draw_vbuf.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_pt.h"
34 #include "translate/translate.h"
35 #include "translate/translate_cache.h"
38 struct draw_context
*draw
;
40 struct translate
*translate
;
42 struct translate_cache
*cache
;
45 const struct vertex_info
*vinfo
;
48 void draw_pt_emit_prepare( struct pt_emit
*emit
,
50 unsigned *max_vertices
)
52 struct draw_context
*draw
= emit
->draw
;
53 const struct vertex_info
*vinfo
;
55 struct translate_key hw_key
;
59 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
61 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
64 /* XXX: may need to defensively reset this later on as clipping can
65 * clobber this state in the render backend.
69 ok
= draw
->render
->set_primitive(draw
->render
, emit
->prim
);
75 /* Must do this after set_primitive() above:
77 emit
->vinfo
= vinfo
= draw
->render
->get_vertex_info(draw
->render
);
80 /* Translate from pipeline vertices to hw vertices.
83 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
85 unsigned src_buffer
= 0;
86 unsigned output_format
;
87 unsigned src_offset
= (vinfo
->attrib
[i
].src_index
* 4 * sizeof(float) );
89 output_format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
90 emit_sz
= draw_translate_vinfo_size(vinfo
->attrib
[i
].emit
);
92 /* doesn't handle EMIT_OMIT */
95 if (vinfo
->attrib
[i
].emit
== EMIT_1F_PSIZE
) {
100 hw_key
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
101 hw_key
.element
[i
].input_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
102 hw_key
.element
[i
].input_buffer
= src_buffer
;
103 hw_key
.element
[i
].input_offset
= src_offset
;
104 hw_key
.element
[i
].instance_divisor
= 0;
105 hw_key
.element
[i
].output_format
= output_format
;
106 hw_key
.element
[i
].output_offset
= dst_offset
;
108 dst_offset
+= emit_sz
;
111 hw_key
.nr_elements
= vinfo
->num_attribs
;
112 hw_key
.output_stride
= vinfo
->size
* 4;
114 if (!emit
->translate
||
115 translate_key_compare(&emit
->translate
->key
, &hw_key
) != 0)
117 translate_key_sanitize(&hw_key
);
118 emit
->translate
= translate_cache_find(emit
->cache
, &hw_key
);
121 *max_vertices
= (draw
->render
->max_vertex_buffer_bytes
/
126 void draw_pt_emit( struct pt_emit
*emit
,
127 const struct draw_vertex_info
*vert_info
,
128 const struct draw_prim_info
*prim_info
)
130 const float (*vertex_data
)[4] = (const float (*)[4])vert_info
->verts
->data
;
131 unsigned vertex_count
= vert_info
->count
;
132 unsigned stride
= vert_info
->stride
;
133 const ushort
*elts
= prim_info
->elts
;
134 struct draw_context
*draw
= emit
->draw
;
135 struct translate
*translate
= emit
->translate
;
136 struct vbuf_render
*render
= draw
->render
;
140 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
142 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
144 if (vertex_count
== 0)
147 /* XXX: and work out some way to coordinate the render primitive
148 * between vbuf.c and here...
150 if (!draw
->render
->set_primitive(draw
->render
, emit
->prim
)) {
155 render
->allocate_vertices(render
,
156 (ushort
)translate
->key
.output_stride
,
157 (ushort
)vertex_count
);
159 hw_verts
= render
->map_vertices( render
);
165 translate
->set_buffer(translate
,
171 translate
->set_buffer(translate
,
173 &draw
->rasterizer
->point_size
,
177 /* fetch/translate vertex attribs to fill hw_verts[] */
178 translate
->run( translate
,
184 render
->unmap_vertices( render
,
189 i
< prim_info
->primitive_count
;
190 start
+= prim_info
->primitive_lengths
[i
], i
++)
192 render
->draw_elements(render
,
194 prim_info
->primitive_lengths
[i
]);
197 render
->release_vertices(render
);
201 void draw_pt_emit_linear(struct pt_emit
*emit
,
202 const struct draw_vertex_info
*vert_info
,
203 const struct draw_prim_info
*prim_info
)
205 const float (*vertex_data
)[4] = (const float (*)[4])vert_info
->verts
->data
;
206 unsigned stride
= vert_info
->stride
;
207 unsigned count
= vert_info
->count
;
208 struct draw_context
*draw
= emit
->draw
;
209 struct translate
*translate
= emit
->translate
;
210 struct vbuf_render
*render
= draw
->render
;
215 debug_printf("Linear emit\n");
217 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
219 draw_do_flush( draw
, DRAW_FLUSH_BACKEND
);
221 /* XXX: and work out some way to coordinate the render primitive
222 * between vbuf.c and here...
224 if (!draw
->render
->set_primitive(draw
->render
, emit
->prim
))
227 if (!render
->allocate_vertices(render
,
228 (ushort
)translate
->key
.output_stride
,
232 hw_verts
= render
->map_vertices( render
);
236 translate
->set_buffer(translate
, 0,
237 vertex_data
, stride
, count
- 1);
239 translate
->set_buffer(translate
, 1,
240 &draw
->rasterizer
->point_size
,
243 translate
->run(translate
,
251 for (i
= 0; i
< count
; i
++) {
252 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__
, i
);
253 draw_dump_emitted_vertex( emit
->vinfo
,
254 (const uint8_t *)hw_verts
+
255 translate
->key
.output_stride
* i
);
259 render
->unmap_vertices( render
, 0, count
- 1 );
262 i
< prim_info
->primitive_count
;
263 start
+= prim_info
->primitive_lengths
[i
], i
++)
265 render
->draw_arrays(render
,
267 prim_info
->primitive_lengths
[i
]);
270 render
->release_vertices(render
);
279 struct pt_emit
*draw_pt_emit_create( struct draw_context
*draw
)
281 struct pt_emit
*emit
= CALLOC_STRUCT(pt_emit
);
286 emit
->cache
= translate_cache_create();
295 void draw_pt_emit_destroy( struct pt_emit
*emit
)
298 translate_cache_destroy(emit
->cache
);