• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   */
32 
33 
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_private.h"
38 #include "draw/draw_vbuf.h"
39 #include "draw/draw_vertex.h"
40 #include "draw/draw_pt.h"
41 #include "draw/draw_vs.h"
42 
43 
44 struct fetch_shade_emit;
45 
46 
47 /* Prototype fetch, shade, emit-hw-verts all in one go.
48  */
49 struct fetch_shade_emit {
50    struct draw_pt_middle_end base;
51    struct draw_context *draw;
52 
53    /* Temporaries:
54     */
55    const float *constants;
56    unsigned pitch[PIPE_MAX_ATTRIBS];
57    const ubyte *src[PIPE_MAX_ATTRIBS];
58    unsigned prim;
59 
60    struct draw_vs_variant_key key;
61    struct draw_vs_variant *active;
62 
63    const struct vertex_info *vinfo;
64 };
65 
66 
67 
68 static void
fse_prepare(struct draw_pt_middle_end * middle,enum pipe_prim_type prim,unsigned opt,unsigned * max_vertices)69 fse_prepare(struct draw_pt_middle_end *middle,
70             enum pipe_prim_type prim,
71             unsigned opt,
72             unsigned *max_vertices)
73 {
74    struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
75    struct draw_context *draw = fse->draw;
76    unsigned num_vs_inputs = draw->vs.vertex_shader->info.num_inputs;
77    const struct vertex_info *vinfo;
78    unsigned nr_vbs = 0;
79 
80    /* Can't support geometry shader on this path.
81     */
82    assert(!draw->gs.geometry_shader);
83 
84    draw->render->set_primitive(draw->render, prim);
85 
86    /* Must do this after set_primitive() above:
87     */
88    fse->vinfo = vinfo = draw->render->get_vertex_info(draw->render);
89 
90    fse->key.output_stride = vinfo->size * 4;
91    fse->key.nr_outputs = vinfo->num_attribs;
92    fse->key.nr_inputs = num_vs_inputs;
93 
94    fse->key.nr_elements = MAX2(fse->key.nr_outputs,     /* outputs - translate to hw format */
95                                fse->key.nr_inputs);     /* inputs - fetch from api format */
96 
97    fse->key.viewport = !draw->bypass_viewport;
98    fse->key.clip = draw->clip_xy || draw->clip_z || draw->clip_user;
99    fse->key.const_vbuffers = 0;
100 
101    memset(fse->key.element, 0,
102           fse->key.nr_elements * sizeof(fse->key.element[0]));
103 
104    for (unsigned i = 0; i < num_vs_inputs; i++) {
105       const struct pipe_vertex_element *src = &draw->pt.vertex_element[i];
106       fse->key.element[i].in.format = src->src_format;
107 
108       /* Consider ignoring these, ie make generated programs
109        * independent of this state:
110        */
111       fse->key.element[i].in.buffer = src->vertex_buffer_index;
112       fse->key.element[i].in.offset = src->src_offset;
113       nr_vbs = MAX2(nr_vbs, src->vertex_buffer_index + 1);
114    }
115 
116    for (unsigned i = 0; i < 5 && i < nr_vbs; i++) {
117       if (draw->pt.vertex_buffer[i].stride == 0)
118          fse->key.const_vbuffers |= (1<<i);
119    }
120 
121    if (0) debug_printf("%s: lookup const_vbuffers: %x\n",
122                        __FUNCTION__, fse->key.const_vbuffers);
123 
124    {
125       unsigned dst_offset = 0;
126 
127       for (unsigned i = 0; i < vinfo->num_attribs; i++) {
128          unsigned emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);
129 
130          /* doesn't handle EMIT_OMIT */
131          assert(emit_sz != 0);
132 
133          /* The elements in the key correspond to vertex shader output
134           * numbers, not to positions in the hw vertex description --
135           * that's handled by the output_offset field.
136           */
137          fse->key.element[i].out.format = vinfo->attrib[i].emit;
138          fse->key.element[i].out.vs_output = vinfo->attrib[i].src_index;
139          fse->key.element[i].out.offset = dst_offset;
140 
141          dst_offset += emit_sz;
142          assert(fse->key.output_stride >= dst_offset);
143       }
144    }
145 
146    fse->active = draw_vs_lookup_variant(draw->vs.vertex_shader, &fse->key);
147 
148    if (!fse->active) {
149       assert(0);
150       return ;
151    }
152 
153    if (0) debug_printf("%s: found const_vbuffers: %x\n", __FUNCTION__,
154                        fse->active->key.const_vbuffers);
155 
156    /* Now set buffer pointers:
157     */
158    for (unsigned i = 0; i < draw->pt.nr_vertex_buffers; i++) {
159       fse->active->set_buffer(fse->active,
160                               i,
161                               ((const ubyte *) draw->pt.user.vbuffer[i].map +
162                                draw->pt.vertex_buffer[i].buffer_offset),
163                               draw->pt.vertex_buffer[i].stride,
164                               draw->pt.max_index);
165    }
166 
167    *max_vertices = (draw->render->max_vertex_buffer_bytes /
168                     (vinfo->size * 4));
169 
170    /* Probably need to do this somewhere (or fix exec shader not to
171     * need it):
172     */
173    if (1) {
174       struct draw_vertex_shader *vs = draw->vs.vertex_shader;
175       vs->prepare(vs, draw);
176    }
177 }
178 
179 
180 static void
fse_bind_parameters(struct draw_pt_middle_end * middle)181 fse_bind_parameters(struct draw_pt_middle_end *middle)
182 {
183    /* No-op? */
184 }
185 
186 
187 static void
fse_run_linear(struct draw_pt_middle_end * middle,unsigned start,unsigned count,unsigned prim_flags)188 fse_run_linear(struct draw_pt_middle_end *middle,
189                unsigned start,
190                unsigned count,
191                unsigned prim_flags)
192 {
193    struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
194    struct draw_context *draw = fse->draw;
195    char *hw_verts;
196 
197    /* XXX: need to flush to get prim_vbuf.c to release its allocation??
198     */
199    draw_do_flush(draw, DRAW_FLUSH_BACKEND);
200 
201    if (!draw->render->allocate_vertices(draw->render,
202                                         (ushort) fse->key.output_stride,
203                                         (ushort) count))
204       goto fail;
205 
206    hw_verts = draw->render->map_vertices(draw->render);
207    if (!hw_verts)
208       goto fail;
209 
210    /* Single routine to fetch vertices, run shader and emit HW verts.
211     * Clipping is done elsewhere -- either by the API or on hardware,
212     * or for some other reason not required...
213     */
214    fse->active->run_linear(fse->active, start, count, hw_verts);
215 
216    if (0) {
217       for (unsigned i = 0; i < count; i++) {
218          debug_printf("\n\n%s vertex %d: (stride %d, offset %d)\n",
219                       __FUNCTION__, i,
220                       fse->key.output_stride,
221                       fse->key.output_stride * i);
222 
223          draw_dump_emitted_vertex(fse->vinfo,
224                                   (const uint8_t *) hw_verts
225                                   + fse->key.output_stride * i);
226       }
227    }
228 
229    draw->render->unmap_vertices(draw->render, 0, (ushort) (count - 1));
230 
231    /* Draw arrays path to avoid re-emitting index list again and
232     * again.
233     */
234    draw->render->draw_arrays(draw->render, 0, count);
235 
236    draw->render->release_vertices(draw->render);
237 
238    return;
239 
240 fail:
241    debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
242    return;
243 }
244 
245 
246 static void
fse_run(struct draw_pt_middle_end * middle,const unsigned * fetch_elts,unsigned fetch_count,const ushort * draw_elts,unsigned draw_count,unsigned prim_flags)247 fse_run(struct draw_pt_middle_end *middle,
248         const unsigned *fetch_elts,
249         unsigned fetch_count,
250         const ushort *draw_elts,
251         unsigned draw_count,
252         unsigned prim_flags)
253 {
254    struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
255    struct draw_context *draw = fse->draw;
256    void *hw_verts;
257 
258    /* XXX: need to flush to get prim_vbuf.c to release its allocation??
259     */
260    draw_do_flush(draw, DRAW_FLUSH_BACKEND);
261 
262    if (!draw->render->allocate_vertices(draw->render,
263                                         (ushort) fse->key.output_stride,
264                                         (ushort) fetch_count))
265       goto fail;
266 
267    hw_verts = draw->render->map_vertices(draw->render);
268    if (!hw_verts)
269       goto fail;
270 
271    /* Single routine to fetch vertices, run shader and emit HW verts.
272     */
273    fse->active->run_elts(fse->active, fetch_elts, fetch_count, hw_verts);
274 
275    if (0) {
276       for (unsigned i = 0; i < fetch_count; i++) {
277          debug_printf("\n\n%s vertex %d:\n", __FUNCTION__, i);
278          draw_dump_emitted_vertex(fse->vinfo,
279                                   (const uint8_t *)hw_verts +
280                                   fse->key.output_stride * i);
281       }
282    }
283 
284    draw->render->unmap_vertices(draw->render, 0, (ushort)(fetch_count - 1));
285 
286    draw->render->draw_elements(draw->render, draw_elts, draw_count);
287 
288    draw->render->release_vertices(draw->render);
289    return;
290 
291 fail:
292    debug_warn_once("allocate or map of vertex buffer failed (out of memory?)");
293    return;
294 }
295 
296 
297 static boolean
fse_run_linear_elts(struct draw_pt_middle_end * middle,unsigned start,unsigned count,const ushort * draw_elts,unsigned draw_count,unsigned prim_flags)298 fse_run_linear_elts(struct draw_pt_middle_end *middle,
299                     unsigned start,
300                     unsigned count,
301                     const ushort *draw_elts,
302                     unsigned draw_count,
303                     unsigned prim_flags)
304 {
305    struct fetch_shade_emit *fse = (struct fetch_shade_emit *)middle;
306    struct draw_context *draw = fse->draw;
307    char *hw_verts;
308 
309    /* XXX: need to flush to get prim_vbuf.c to release its allocation??
310     */
311    draw_do_flush(draw, DRAW_FLUSH_BACKEND);
312 
313    if (!draw->render->allocate_vertices(draw->render,
314                                         (ushort) fse->key.output_stride,
315                                         (ushort) count))
316       return FALSE;
317 
318    hw_verts = draw->render->map_vertices(draw->render);
319    if (!hw_verts)
320       return FALSE;
321 
322    /* Single routine to fetch vertices, run shader and emit HW verts.
323     * Clipping is done elsewhere -- either by the API or on hardware,
324     * or for some other reason not required...
325     */
326    fse->active->run_linear(fse->active, start, count, hw_verts);
327 
328    draw->render->draw_elements(draw->render, draw_elts, draw_count);
329 
330    draw->render->unmap_vertices(draw->render, 0, (ushort)(count - 1));
331 
332    draw->render->release_vertices(draw->render);
333 
334    return TRUE;
335 }
336 
337 
338 static void
fse_finish(struct draw_pt_middle_end * middle)339 fse_finish(struct draw_pt_middle_end *middle)
340 {
341 }
342 
343 
344 static void
fse_destroy(struct draw_pt_middle_end * middle)345 fse_destroy(struct draw_pt_middle_end *middle)
346 {
347    FREE(middle);
348 }
349 
350 
351 struct draw_pt_middle_end *
draw_pt_middle_fse(struct draw_context * draw)352 draw_pt_middle_fse(struct draw_context *draw)
353 {
354    struct fetch_shade_emit *fse = CALLOC_STRUCT(fetch_shade_emit);
355    if (!fse)
356       return NULL;
357 
358    fse->base.prepare = fse_prepare;
359    fse->base.bind_parameters = fse_bind_parameters;
360    fse->base.run = fse_run;
361    fse->base.run_linear = fse_run_linear;
362    fse->base.run_linear_elts = fse_run_linear_elts;
363    fse->base.finish = fse_finish;
364    fse->base.destroy = fse_destroy;
365    fse->draw = draw;
366 
367    return &fse->base;
368 }
369