1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 #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"
36 #include "util/u_prim.h"
37
38 struct pt_emit {
39 struct draw_context *draw;
40
41 struct translate *translate;
42
43 struct translate_cache *cache;
44 unsigned prim;
45
46 const struct vertex_info *vinfo;
47
48 float zero4[4];
49
50 };
51
52
53 void
draw_pt_emit_prepare(struct pt_emit * emit,unsigned prim,unsigned * max_vertices)54 draw_pt_emit_prepare(struct pt_emit *emit,
55 unsigned prim,
56 unsigned *max_vertices)
57 {
58 struct draw_context *draw = emit->draw;
59 const struct vertex_info *vinfo;
60 unsigned dst_offset;
61 struct translate_key hw_key;
62 unsigned i;
63
64 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
65 */
66 draw_do_flush(draw, DRAW_FLUSH_BACKEND);
67
68 /* XXX: may need to defensively reset this later on as clipping can
69 * clobber this state in the render backend.
70 */
71 emit->prim = prim;
72
73 draw->render->set_primitive(draw->render, emit->prim);
74
75 /* Must do this after set_primitive() above:
76 */
77 emit->vinfo = vinfo = draw->render->get_vertex_info(draw->render);
78
79 /* Translate from pipeline vertices to hw vertices.
80 */
81 dst_offset = 0;
82 for (i = 0; i < vinfo->num_attribs; i++) {
83 unsigned emit_sz = 0;
84 unsigned src_buffer = 0;
85 unsigned output_format;
86 unsigned src_offset = vinfo->attrib[i].src_index * 4 * sizeof(float);
87
88 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
89 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
90
91 /* doesn't handle EMIT_OMIT */
92 assert(emit_sz != 0);
93
94 if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) {
95 src_buffer = 1;
96 src_offset = 0;
97 }
98 else if (vinfo->attrib[i].src_index == DRAW_ATTR_NONEXIST) {
99 /* elements which don't exist will get assigned zeros */
100 src_buffer = 2;
101 src_offset = 0;
102 }
103
104 hw_key.element[i].type = TRANSLATE_ELEMENT_NORMAL;
105 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
106 hw_key.element[i].input_buffer = src_buffer;
107 hw_key.element[i].input_offset = src_offset;
108 hw_key.element[i].instance_divisor = 0;
109 hw_key.element[i].output_format = output_format;
110 hw_key.element[i].output_offset = dst_offset;
111
112 dst_offset += emit_sz;
113 }
114
115 hw_key.nr_elements = vinfo->num_attribs;
116 hw_key.output_stride = vinfo->size * 4;
117
118 if (!emit->translate ||
119 translate_key_compare(&emit->translate->key, &hw_key) != 0) {
120 translate_key_sanitize(&hw_key);
121 emit->translate = translate_cache_find(emit->cache, &hw_key);
122
123 emit->translate->set_buffer(emit->translate, 2, &emit->zero4[0], 0, ~0);
124 }
125
126 if (!vinfo->size)
127 *max_vertices = 0;
128 else
129 *max_vertices = (draw->render->max_vertex_buffer_bytes /
130 (vinfo->size * 4));
131 }
132
133
134 void
draw_pt_emit(struct pt_emit * emit,const struct draw_vertex_info * vert_info,const struct draw_prim_info * prim_info)135 draw_pt_emit(struct pt_emit *emit,
136 const struct draw_vertex_info *vert_info,
137 const struct draw_prim_info *prim_info)
138 {
139 const float (*vertex_data)[4] = (const float (*)[4])vert_info->verts->data;
140 unsigned vertex_count = vert_info->count;
141 unsigned stride = vert_info->stride;
142 const ushort *elts = prim_info->elts;
143 struct draw_context *draw = emit->draw;
144 struct translate *translate = emit->translate;
145 struct vbuf_render *render = draw->render;
146 unsigned start, i;
147 void *hw_verts;
148
149 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
150 */
151 draw_do_flush(draw, DRAW_FLUSH_BACKEND);
152
153 if (vertex_count == 0)
154 return;
155
156 /* XXX: and work out some way to coordinate the render primitive
157 * between vbuf.c and here...
158 */
159 render->set_primitive(draw->render, prim_info->prim);
160
161 render->allocate_vertices(render,
162 (ushort)translate->key.output_stride,
163 (ushort)vertex_count);
164
165 hw_verts = render->map_vertices(render);
166 if (!hw_verts) {
167 debug_warn_once("map of vertex buffer failed (out of memory?)");
168 return;
169 }
170
171 translate->set_buffer(translate,
172 0,
173 vertex_data,
174 stride,
175 ~0);
176
177 translate->set_buffer(translate,
178 1,
179 &draw->rasterizer->point_size,
180 0,
181 ~0);
182
183 /* fetch/translate vertex attribs to fill hw_verts[] */
184 translate->run(translate,
185 0,
186 vertex_count,
187 0,
188 0,
189 hw_verts);
190
191 render->unmap_vertices(render, 0, vertex_count - 1);
192
193 for (start = i = 0;
194 i < prim_info->primitive_count;
195 start += prim_info->primitive_lengths[i], i++)
196 {
197 render->draw_elements(render,
198 elts + start,
199 prim_info->primitive_lengths[i]);
200 }
201
202 render->release_vertices(render);
203 }
204
205
206 void
draw_pt_emit_linear(struct pt_emit * emit,const struct draw_vertex_info * vert_info,const struct draw_prim_info * prim_info)207 draw_pt_emit_linear(struct pt_emit *emit,
208 const struct draw_vertex_info *vert_info,
209 const struct draw_prim_info *prim_info)
210 {
211 const float (*vertex_data)[4] = (const float (*)[4])vert_info->verts->data;
212 unsigned stride = vert_info->stride;
213 unsigned count = vert_info->count;
214 struct draw_context *draw = emit->draw;
215 struct translate *translate = emit->translate;
216 struct vbuf_render *render = draw->render;
217 void *hw_verts;
218 unsigned start, i;
219
220 #if 0
221 debug_printf("Linear emit\n");
222 #endif
223 /* XXX: need to flush to get prim_vbuf.c to release its allocation??
224 */
225 draw_do_flush(draw, DRAW_FLUSH_BACKEND);
226
227 /* XXX: and work out some way to coordinate the render primitive
228 * between vbuf.c and here...
229 */
230 render->set_primitive(draw->render, prim_info->prim);
231
232 if (!render->allocate_vertices(render,
233 (ushort)translate->key.output_stride,
234 (ushort)count))
235 goto fail;
236
237 hw_verts = render->map_vertices(render);
238 if (!hw_verts)
239 goto fail;
240
241 translate->set_buffer(translate, 0,
242 vertex_data, stride, count - 1);
243
244 translate->set_buffer(translate, 1,
245 &draw->rasterizer->point_size,
246 0, ~0);
247
248 translate->run(translate,
249 0,
250 count,
251 0,
252 0,
253 hw_verts);
254
255 if (0) {
256 unsigned i;
257 for (i = 0; i < count; i++) {
258 debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
259 draw_dump_emitted_vertex(emit->vinfo,
260 (const uint8_t *)hw_verts +
261 translate->key.output_stride * i);
262 }
263 }
264
265 render->unmap_vertices(render, 0, count - 1);
266
267 for (start = i = 0;
268 i < prim_info->primitive_count;
269 start += prim_info->primitive_lengths[i], i++)
270 {
271 render->draw_arrays(render,
272 start,
273 prim_info->primitive_lengths[i]);
274 }
275
276 render->release_vertices(render);
277
278 return;
279
280 fail:
281 debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
282 return;
283 }
284
285
286 struct pt_emit *
draw_pt_emit_create(struct draw_context * draw)287 draw_pt_emit_create(struct draw_context *draw)
288 {
289 struct pt_emit *emit = CALLOC_STRUCT(pt_emit);
290 if (!emit)
291 return NULL;
292
293 emit->draw = draw;
294 emit->cache = translate_cache_create();
295 if (!emit->cache) {
296 FREE(emit);
297 return NULL;
298 }
299
300 emit->zero4[0] = emit->zero4[1] = emit->zero4[2] = emit->zero4[3] = 0.0f;
301
302 return emit;
303 }
304
305
306 void
draw_pt_emit_destroy(struct pt_emit * emit)307 draw_pt_emit_destroy(struct pt_emit *emit)
308 {
309 if (emit->cache)
310 translate_cache_destroy(emit->cache);
311
312 FREE(emit);
313 }
314