• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    assert(vertex_count <= 65535);
162    render->allocate_vertices(render,
163                              (ushort)translate->key.output_stride,
164                              (ushort)vertex_count);
165 
166    hw_verts = render->map_vertices(render);
167    if (!hw_verts) {
168       debug_warn_once("map of vertex buffer failed (out of memory?)");
169       return;
170    }
171 
172    translate->set_buffer(translate,
173                          0,
174                          vertex_data,
175                          stride,
176                          ~0);
177 
178    translate->set_buffer(translate,
179                          1,
180                          &draw->rasterizer->point_size,
181                          0,
182                          ~0);
183 
184    /* fetch/translate vertex attribs to fill hw_verts[] */
185    translate->run(translate,
186                   0,
187                   vertex_count,
188                   0,
189                   0,
190                   hw_verts);
191 
192    render->unmap_vertices(render, 0, vertex_count - 1);
193 
194    for (start = i = 0;
195         i < prim_info->primitive_count;
196         start += prim_info->primitive_lengths[i], i++)
197    {
198       render->draw_elements(render,
199                             elts + start,
200                             prim_info->primitive_lengths[i]);
201    }
202 
203    render->release_vertices(render);
204 }
205 
206 
207 void
draw_pt_emit_linear(struct pt_emit * emit,const struct draw_vertex_info * vert_info,const struct draw_prim_info * prim_info)208 draw_pt_emit_linear(struct pt_emit *emit,
209                     const struct draw_vertex_info *vert_info,
210                     const struct draw_prim_info *prim_info)
211 {
212    const float (*vertex_data)[4] = (const float (*)[4])vert_info->verts->data;
213    unsigned stride = vert_info->stride;
214    unsigned count = vert_info->count;
215    struct draw_context *draw = emit->draw;
216    struct translate *translate = emit->translate;
217    struct vbuf_render *render = draw->render;
218    void *hw_verts;
219    unsigned start, i;
220 
221 #if 0
222    debug_printf("Linear emit\n");
223 #endif
224    /* XXX: need to flush to get prim_vbuf.c to release its allocation??
225     */
226    draw_do_flush(draw, DRAW_FLUSH_BACKEND);
227 
228    /* XXX: and work out some way to coordinate the render primitive
229     * between vbuf.c and here...
230     */
231    render->set_primitive(draw->render, prim_info->prim);
232 
233    assert(count <= 65535);
234    if (!render->allocate_vertices(render,
235                                   (ushort)translate->key.output_stride,
236                                   (ushort)count))
237       goto fail;
238 
239    hw_verts = render->map_vertices(render);
240    if (!hw_verts)
241       goto fail;
242 
243    translate->set_buffer(translate, 0,
244                          vertex_data, stride, count - 1);
245 
246    translate->set_buffer(translate, 1,
247                          &draw->rasterizer->point_size,
248                          0, ~0);
249 
250    translate->run(translate,
251                   0,
252                   count,
253                   0,
254                   0,
255                   hw_verts);
256 
257    if (0) {
258       unsigned i;
259       for (i = 0; i < count; i++) {
260          debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
261          draw_dump_emitted_vertex(emit->vinfo,
262                                   (const uint8_t *)hw_verts +
263                                   translate->key.output_stride * i);
264       }
265    }
266 
267    render->unmap_vertices(render, 0, count - 1);
268 
269    for (start = i = 0;
270         i < prim_info->primitive_count;
271         start += prim_info->primitive_lengths[i], i++)
272    {
273       render->draw_arrays(render,
274                           start,
275                           prim_info->primitive_lengths[i]);
276    }
277 
278    render->release_vertices(render);
279 
280    return;
281 
282 fail:
283    debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
284    return;
285 }
286 
287 
288 struct pt_emit *
draw_pt_emit_create(struct draw_context * draw)289 draw_pt_emit_create(struct draw_context *draw)
290 {
291    struct pt_emit *emit = CALLOC_STRUCT(pt_emit);
292    if (!emit)
293       return NULL;
294 
295    emit->draw = draw;
296    emit->cache = translate_cache_create();
297    if (!emit->cache) {
298       FREE(emit);
299       return NULL;
300    }
301 
302    emit->zero4[0] = emit->zero4[1] = emit->zero4[2] = emit->zero4[3] = 0.0f;
303 
304    return emit;
305 }
306 
307 
308 void
draw_pt_emit_destroy(struct pt_emit * emit)309 draw_pt_emit_destroy(struct pt_emit *emit)
310 {
311    if (emit->cache)
312       translate_cache_destroy(emit->cache);
313 
314    FREE(emit);
315 }
316