1 /**************************************************************************
2 *
3 * Copyright 2007 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 /**
29 * \file
30 * Vertex buffer drawing stage.
31 *
32 * \author Jose Fonseca <jfonseca@vmware.com>
33 * \author Keith Whitwell <keithw@vmware.com>
34 */
35
36
37 #include "util/u_debug.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "draw_vbuf.h"
41 #include "draw_private.h"
42 #include "draw_vertex.h"
43 #include "draw_pipe.h"
44 #include "translate/translate.h"
45 #include "translate/translate_cache.h"
46
47
48 /**
49 * Vertex buffer emit stage.
50 */
51 struct vbuf_stage {
52 struct draw_stage stage; /**< This must be first (base class) */
53
54 struct vbuf_render *render;
55
56 const struct vertex_info *vinfo;
57
58 /** Vertex size in bytes */
59 unsigned vertex_size;
60
61 struct translate *translate;
62
63 /* FIXME: we have no guarantee that 'unsigned' is 32bit */
64
65 /** Vertices in hardware format */
66 unsigned *vertices;
67 unsigned *vertex_ptr;
68 unsigned max_vertices;
69 unsigned nr_vertices;
70
71 /** Indices */
72 ushort *indices;
73 unsigned max_indices;
74 unsigned nr_indices;
75
76 /* Cache point size somewhere its address won't change:
77 */
78 float point_size;
79 float zero4[4];
80
81 struct translate_cache *cache;
82 };
83
84
85 /**
86 * Basically a cast wrapper.
87 */
88 static inline struct vbuf_stage *
vbuf_stage(struct draw_stage * stage)89 vbuf_stage(struct draw_stage *stage)
90 {
91 assert(stage);
92 return (struct vbuf_stage *)stage;
93 }
94
95
96 static void vbuf_flush_vertices(struct vbuf_stage *vbuf);
97 static void vbuf_alloc_vertices(struct vbuf_stage *vbuf);
98
99
100 static inline void
check_space(struct vbuf_stage * vbuf,unsigned nr)101 check_space(struct vbuf_stage *vbuf, unsigned nr)
102 {
103 if (vbuf->nr_vertices + nr > vbuf->max_vertices ||
104 vbuf->nr_indices + nr > vbuf->max_indices) {
105 vbuf_flush_vertices(vbuf);
106 vbuf_alloc_vertices(vbuf);
107 }
108 }
109
110
111 /**
112 * Extract the needed fields from post-transformed vertex and emit
113 * a hardware(driver) vertex.
114 * Recall that the vertices are constructed by the 'draw' module and
115 * have a couple of slots at the beginning (1-dword header, 4-dword
116 * clip pos) that we ignore here. We only use the vertex->data[] fields.
117 */
118 static inline ushort
emit_vertex(struct vbuf_stage * vbuf,struct vertex_header * vertex)119 emit_vertex(struct vbuf_stage *vbuf, struct vertex_header *vertex)
120 {
121 if (vertex->vertex_id == UNDEFINED_VERTEX_ID && vbuf->vertex_ptr) {
122 /* Hmm - vertices are emitted one at a time - better make sure
123 * set_buffer is efficient. Consider a special one-shot mode for
124 * translate.
125 */
126 /* Note: we really do want data[0] here, not data[pos]:
127 */
128 vbuf->translate->set_buffer(vbuf->translate, 0, vertex->data[0], 0, ~0);
129 vbuf->translate->run(vbuf->translate, 0, 1, 0, 0, vbuf->vertex_ptr);
130
131 if (0) draw_dump_emitted_vertex(vbuf->vinfo, (uint8_t *)vbuf->vertex_ptr);
132
133 vbuf->vertex_ptr += vbuf->vertex_size/4;
134 vertex->vertex_id = vbuf->nr_vertices++;
135 }
136
137 return (ushort)vertex->vertex_id;
138 }
139
140
141 static void
vbuf_tri(struct draw_stage * stage,struct prim_header * prim)142 vbuf_tri(struct draw_stage *stage, struct prim_header *prim)
143 {
144 struct vbuf_stage *vbuf = vbuf_stage(stage);
145 unsigned i;
146
147 check_space(vbuf, 3);
148
149 for (i = 0; i < 3; i++) {
150 vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[i]);
151 }
152 }
153
154
155 static void
vbuf_line(struct draw_stage * stage,struct prim_header * prim)156 vbuf_line(struct draw_stage *stage, struct prim_header *prim)
157 {
158 struct vbuf_stage *vbuf = vbuf_stage(stage);
159 unsigned i;
160
161 check_space(vbuf, 2);
162
163 for (i = 0; i < 2; i++) {
164 vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[i]);
165 }
166 }
167
168
169 static void
vbuf_point(struct draw_stage * stage,struct prim_header * prim)170 vbuf_point(struct draw_stage *stage, struct prim_header *prim)
171 {
172 struct vbuf_stage *vbuf = vbuf_stage(stage);
173
174 check_space(vbuf, 1);
175
176 vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[0]);
177 }
178
179
180 /**
181 * Set the prim type for subsequent vertices.
182 * This may result in a new vertex size. The existing vbuffer (if any)
183 * will be flushed if needed and a new one allocated.
184 */
185 static void
vbuf_start_prim(struct vbuf_stage * vbuf,uint prim)186 vbuf_start_prim(struct vbuf_stage *vbuf, uint prim)
187 {
188 struct translate_key hw_key;
189 unsigned dst_offset;
190 unsigned i;
191 const struct vertex_info *vinfo;
192
193 vbuf->render->set_primitive(vbuf->render, prim);
194 if (vbuf->render->set_view_index)
195 vbuf->render->set_view_index(vbuf->render, vbuf->stage.draw->pt.user.viewid);
196
197 /* Must do this after set_primitive() above:
198 *
199 * XXX: need some state managment to track when this needs to be
200 * recalculated. The driver should tell us whether there was a
201 * state change.
202 */
203 vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render);
204 vinfo = vbuf->vinfo;
205 vbuf->vertex_size = vinfo->size * sizeof(float);
206
207 /* Translate from pipeline vertices to hw vertices.
208 */
209 dst_offset = 0;
210
211 for (i = 0; i < vinfo->num_attribs; i++) {
212 unsigned emit_sz = 0;
213 unsigned src_buffer = 0;
214 enum pipe_format output_format;
215 unsigned src_offset = (vinfo->attrib[i].src_index * 4 * sizeof(float));
216
217 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);
218 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
219
220 /* doesn't handle EMIT_OMIT */
221 assert(emit_sz != 0);
222
223 if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) {
224 src_buffer = 1;
225 src_offset = 0;
226 }
227 else if (vinfo->attrib[i].src_index == DRAW_ATTR_NONEXIST) {
228 /* elements which don't exist will get assigned zeros */
229 src_buffer = 2;
230 src_offset = 0;
231 }
232
233 hw_key.element[i].type = TRANSLATE_ELEMENT_NORMAL;
234 hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
235 hw_key.element[i].input_buffer = src_buffer;
236 hw_key.element[i].input_offset = src_offset;
237 hw_key.element[i].instance_divisor = 0;
238 hw_key.element[i].output_format = output_format;
239 hw_key.element[i].output_offset = dst_offset;
240
241 dst_offset += emit_sz;
242 }
243
244 hw_key.nr_elements = vinfo->num_attribs;
245 hw_key.output_stride = vbuf->vertex_size;
246
247 /* Don't bother with caching at this stage:
248 */
249 if (!vbuf->translate ||
250 translate_key_compare(&vbuf->translate->key, &hw_key) != 0) {
251 translate_key_sanitize(&hw_key);
252 vbuf->translate = translate_cache_find(vbuf->cache, &hw_key);
253
254 vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0, ~0);
255 vbuf->translate->set_buffer(vbuf->translate, 2, &vbuf->zero4[0], 0, ~0);
256 }
257
258 vbuf->point_size = vbuf->stage.draw->rasterizer->point_size;
259
260 /* Allocate new buffer?
261 */
262 assert(vbuf->vertices == NULL);
263 vbuf_alloc_vertices(vbuf);
264 }
265
266
267 static void
vbuf_first_tri(struct draw_stage * stage,struct prim_header * prim)268 vbuf_first_tri(struct draw_stage *stage, struct prim_header *prim)
269 {
270 struct vbuf_stage *vbuf = vbuf_stage(stage);
271
272 vbuf_flush_vertices(vbuf);
273 vbuf_start_prim(vbuf, PIPE_PRIM_TRIANGLES);
274 stage->tri = vbuf_tri;
275 stage->tri(stage, prim);
276 }
277
278
279 static void
vbuf_first_line(struct draw_stage * stage,struct prim_header * prim)280 vbuf_first_line(struct draw_stage *stage, struct prim_header *prim)
281 {
282 struct vbuf_stage *vbuf = vbuf_stage(stage);
283
284 vbuf_flush_vertices(vbuf);
285 vbuf_start_prim(vbuf, PIPE_PRIM_LINES);
286 stage->line = vbuf_line;
287 stage->line(stage, prim);
288 }
289
290
291 static void
vbuf_first_point(struct draw_stage * stage,struct prim_header * prim)292 vbuf_first_point(struct draw_stage *stage, struct prim_header *prim)
293 {
294 struct vbuf_stage *vbuf = vbuf_stage(stage);
295
296 vbuf_flush_vertices(vbuf);
297 vbuf_start_prim(vbuf, PIPE_PRIM_POINTS);
298 stage->point = vbuf_point;
299 stage->point(stage, prim);
300 }
301
302
303
304 /**
305 * Flush existing vertex buffer and allocate a new one.
306 */
307 static void
vbuf_flush_vertices(struct vbuf_stage * vbuf)308 vbuf_flush_vertices(struct vbuf_stage *vbuf)
309 {
310 if (vbuf->vertices) {
311 vbuf->render->unmap_vertices(vbuf->render, 0, vbuf->nr_vertices - 1);
312
313 if (vbuf->nr_indices) {
314 vbuf->render->draw_elements(vbuf->render,
315 vbuf->indices,
316 vbuf->nr_indices);
317
318 vbuf->nr_indices = 0;
319 }
320
321 /* Reset temporary vertices ids */
322 if (vbuf->nr_vertices)
323 draw_reset_vertex_ids(vbuf->stage.draw);
324
325 /* Free the vertex buffer */
326 vbuf->render->release_vertices(vbuf->render);
327
328 vbuf->max_vertices = vbuf->nr_vertices = 0;
329 vbuf->vertex_ptr = vbuf->vertices = NULL;
330 }
331
332 /* Reset point/line/tri function pointers.
333 * If (for example) we transition from points to tris and back to points
334 * again, we need to call the vbuf_first_point() function again to flush
335 * the triangles before drawing more points. This can happen when drawing
336 * with front polygon mode = filled and back polygon mode = line or point.
337 */
338 vbuf->stage.point = vbuf_first_point;
339 vbuf->stage.line = vbuf_first_line;
340 vbuf->stage.tri = vbuf_first_tri;
341 }
342
343
344 static void
vbuf_alloc_vertices(struct vbuf_stage * vbuf)345 vbuf_alloc_vertices(struct vbuf_stage *vbuf)
346 {
347 if (vbuf->vertex_ptr) {
348 assert(!vbuf->nr_indices);
349 assert(!vbuf->vertices);
350 }
351
352 /* Allocate a new vertex buffer */
353 vbuf->max_vertices =
354 vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size;
355
356 if (vbuf->max_vertices >= UNDEFINED_VERTEX_ID)
357 vbuf->max_vertices = UNDEFINED_VERTEX_ID - 1;
358
359 /* Must always succeed -- driver gives us a
360 * 'max_vertex_buffer_bytes' which it guarantees it can allocate,
361 * and it will flush itself if necessary to do so. If this does
362 * fail, we are basically without usable hardware.
363 */
364 vbuf->render->allocate_vertices(vbuf->render,
365 (ushort) vbuf->vertex_size,
366 (ushort) vbuf->max_vertices);
367
368 vbuf->vertices = (uint *) vbuf->render->map_vertices(vbuf->render);
369
370 vbuf->vertex_ptr = vbuf->vertices;
371 }
372
373
374 static void
vbuf_flush(struct draw_stage * stage,unsigned flags)375 vbuf_flush(struct draw_stage *stage, unsigned flags)
376 {
377 struct vbuf_stage *vbuf = vbuf_stage(stage);
378
379 vbuf_flush_vertices(vbuf);
380 }
381
382
383 static void
vbuf_reset_stipple_counter(struct draw_stage * stage)384 vbuf_reset_stipple_counter(struct draw_stage *stage)
385 {
386 /* XXX: Need to do something here for hardware with linestipple.
387 */
388 (void) stage;
389 }
390
391
392 static void
vbuf_destroy(struct draw_stage * stage)393 vbuf_destroy(struct draw_stage *stage)
394 {
395 struct vbuf_stage *vbuf = vbuf_stage(stage);
396
397 if (vbuf->indices)
398 align_free(vbuf->indices);
399
400 if (vbuf->render)
401 vbuf->render->destroy(vbuf->render);
402
403 if (vbuf->cache)
404 translate_cache_destroy(vbuf->cache);
405
406 FREE(stage);
407 }
408
409
410 /**
411 * Create a new primitive vbuf/render stage.
412 */
413 struct draw_stage *
draw_vbuf_stage(struct draw_context * draw,struct vbuf_render * render)414 draw_vbuf_stage(struct draw_context *draw, struct vbuf_render *render)
415 {
416 struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage);
417 if (!vbuf)
418 goto fail;
419
420 vbuf->stage.draw = draw;
421 vbuf->stage.name = "vbuf";
422 vbuf->stage.point = vbuf_first_point;
423 vbuf->stage.line = vbuf_first_line;
424 vbuf->stage.tri = vbuf_first_tri;
425 vbuf->stage.flush = vbuf_flush;
426 vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter;
427 vbuf->stage.destroy = vbuf_destroy;
428
429 vbuf->render = render;
430 vbuf->max_indices = MIN2(render->max_indices, UNDEFINED_VERTEX_ID-1);
431
432 vbuf->indices = (ushort *) align_malloc(vbuf->max_indices *
433 sizeof(vbuf->indices[0]),
434 16);
435 if (!vbuf->indices)
436 goto fail;
437
438 vbuf->cache = translate_cache_create();
439 if (!vbuf->cache)
440 goto fail;
441
442 vbuf->vertices = NULL;
443 vbuf->vertex_ptr = vbuf->vertices;
444
445 vbuf->zero4[0] = vbuf->zero4[1] = vbuf->zero4[2] = vbuf->zero4[3] = 0.0f;
446
447 return &vbuf->stage;
448
449 fail:
450 if (vbuf)
451 vbuf_destroy(&vbuf->stage);
452
453 return NULL;
454 }
455