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 #include "draw/draw_context.h"
34 #include "draw/draw_gs.h"
35 #include "draw/draw_tess.h"
36 #include "draw/draw_private.h"
37 #include "draw/draw_pt.h"
38 #include "draw/draw_vbuf.h"
39 #include "draw/draw_vs.h"
40 #include "tgsi/tgsi_dump.h"
41 #include "util/u_math.h"
42 #include "util/u_prim.h"
43 #include "util/format/u_format.h"
44 #include "util/u_draw.h"
45 
46 
47 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse, "DRAW_FSE", FALSE)
48 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse, "DRAW_NO_FSE", FALSE)
49 
50 /* Overall we split things into:
51  *     - frontend -- prepare fetch_elts, draw_elts - eg vsplit
52  *     - middle   -- fetch, shade, cliptest, viewport
53  *     - pipeline -- the prim pipeline: clipping, wide lines, etc
54  *     - backend  -- the vbuf_render provided by the driver.
55  */
56 static boolean
draw_pt_arrays(struct draw_context * draw,unsigned prim,unsigned start,unsigned count)57 draw_pt_arrays(struct draw_context *draw,
58                unsigned prim,
59                unsigned start,
60                unsigned count)
61 {
62    struct draw_pt_front_end *frontend = NULL;
63    struct draw_pt_middle_end *middle = NULL;
64    unsigned opt = 0;
65 
66    /* Sanitize primitive length:
67     */
68    {
69       unsigned first, incr;
70 
71       if (prim == PIPE_PRIM_PATCHES) {
72          first = draw->pt.vertices_per_patch;
73          incr = draw->pt.vertices_per_patch;
74       } else
75          draw_pt_split_prim(prim, &first, &incr);
76       count = draw_pt_trim_count(count, first, incr);
77       if (count < first)
78          return TRUE;
79    }
80 
81    if (!draw->force_passthrough) {
82       unsigned out_prim = prim;
83 
84       if (draw->gs.geometry_shader)
85          out_prim = draw->gs.geometry_shader->output_primitive;
86       else if (draw->tes.tess_eval_shader)
87          out_prim = get_tes_output_prim(draw->tes.tess_eval_shader);
88 
89       if (!draw->render) {
90          opt |= PT_PIPELINE;
91       }
92 
93       if (draw_need_pipeline(draw,
94                              draw->rasterizer,
95                              out_prim)) {
96          opt |= PT_PIPELINE;
97       }
98 
99       if ((draw->clip_xy ||
100            draw->clip_z ||
101            draw->clip_user) && !draw->pt.test_fse) {
102          opt |= PT_CLIPTEST;
103       }
104 
105       opt |= PT_SHADE;
106    }
107 
108    if (draw->pt.middle.llvm) {
109       middle = draw->pt.middle.llvm;
110    } else {
111       if (opt == 0)
112          middle = draw->pt.middle.fetch_emit;
113       else if (opt == PT_SHADE && !draw->pt.no_fse)
114          middle = draw->pt.middle.fetch_shade_emit;
115       else
116          middle = draw->pt.middle.general;
117    }
118 
119    frontend = draw->pt.frontend;
120 
121    if (frontend) {
122       if (draw->pt.prim != prim || draw->pt.opt != opt) {
123          /* In certain conditions switching primitives requires us to flush
124           * and validate the different stages. One example is when smooth
125           * lines are active but first drawn with triangles and then with
126           * lines.
127           */
128          draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
129          frontend = NULL;
130       } else if (draw->pt.eltSize != draw->pt.user.eltSize) {
131          /* Flush draw state if eltSize changed.
132           * This could be improved so only the frontend is flushed since it
133           * converts all indices to ushorts and the fetch part of the middle
134           * always prepares both linear and indexed.
135           */
136          frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
137          frontend = NULL;
138       }
139    }
140 
141    if (!frontend) {
142       frontend = draw->pt.front.vsplit;
143 
144       frontend->prepare( frontend, prim, middle, opt );
145 
146       draw->pt.frontend = frontend;
147       draw->pt.eltSize = draw->pt.user.eltSize;
148       draw->pt.prim = prim;
149       draw->pt.opt = opt;
150    }
151 
152    if (draw->pt.rebind_parameters) {
153       /* update constants, viewport dims, clip planes, etc */
154       middle->bind_parameters(middle);
155       draw->pt.rebind_parameters = FALSE;
156    }
157 
158    frontend->run( frontend, start, count );
159 
160    return TRUE;
161 }
162 
draw_pt_flush(struct draw_context * draw,unsigned flags)163 void draw_pt_flush( struct draw_context *draw, unsigned flags )
164 {
165    assert(flags);
166 
167    if (draw->pt.frontend) {
168       draw->pt.frontend->flush( draw->pt.frontend, flags );
169 
170       /* don't prepare if we only are flushing the backend */
171       if (flags & DRAW_FLUSH_STATE_CHANGE)
172          draw->pt.frontend = NULL;
173    }
174 
175    if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
176       draw->pt.rebind_parameters = TRUE;
177    }
178 }
179 
180 
181 
draw_pt_init(struct draw_context * draw)182 boolean draw_pt_init( struct draw_context *draw )
183 {
184    draw->pt.test_fse = debug_get_option_draw_fse();
185    draw->pt.no_fse = debug_get_option_draw_no_fse();
186 
187    draw->pt.front.vsplit = draw_pt_vsplit(draw);
188    if (!draw->pt.front.vsplit)
189       return FALSE;
190 
191    draw->pt.middle.fetch_emit = draw_pt_fetch_emit( draw );
192    if (!draw->pt.middle.fetch_emit)
193       return FALSE;
194 
195    draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
196    if (!draw->pt.middle.fetch_shade_emit)
197       return FALSE;
198 
199    draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
200    if (!draw->pt.middle.general)
201       return FALSE;
202 
203 #ifdef LLVM_AVAILABLE
204    if (draw->llvm)
205       draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
206 #endif
207 
208    return TRUE;
209 }
210 
211 
draw_pt_destroy(struct draw_context * draw)212 void draw_pt_destroy( struct draw_context *draw )
213 {
214    if (draw->pt.middle.llvm) {
215       draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
216       draw->pt.middle.llvm = NULL;
217    }
218 
219    if (draw->pt.middle.general) {
220       draw->pt.middle.general->destroy( draw->pt.middle.general );
221       draw->pt.middle.general = NULL;
222    }
223 
224    if (draw->pt.middle.fetch_emit) {
225       draw->pt.middle.fetch_emit->destroy( draw->pt.middle.fetch_emit );
226       draw->pt.middle.fetch_emit = NULL;
227    }
228 
229    if (draw->pt.middle.fetch_shade_emit) {
230       draw->pt.middle.fetch_shade_emit->destroy( draw->pt.middle.fetch_shade_emit );
231       draw->pt.middle.fetch_shade_emit = NULL;
232    }
233 
234    if (draw->pt.front.vsplit) {
235       draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
236       draw->pt.front.vsplit = NULL;
237    }
238 }
239 
240 
241 /**
242  * Debug- print the first 'count' vertices.
243  */
244 static void
draw_print_arrays(struct draw_context * draw,uint prim,int start,uint count)245 draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
246 {
247    uint i;
248 
249    debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
250                 prim, start, count);
251 
252    for (i = 0; i < count; i++) {
253       uint ii = 0;
254       uint j;
255 
256       if (draw->pt.user.eltSize) {
257          /* indexed arrays */
258 
259          switch (draw->pt.user.eltSize) {
260          case 1:
261             {
262                const ubyte *elem = (const ubyte *) draw->pt.user.elts;
263                ii = elem[start + i];
264             }
265             break;
266          case 2:
267             {
268                const ushort *elem = (const ushort *) draw->pt.user.elts;
269                ii = elem[start + i];
270             }
271             break;
272          case 4:
273             {
274                const uint *elem = (const uint *) draw->pt.user.elts;
275                ii = elem[start + i];
276             }
277             break;
278          default:
279             assert(0);
280             return;
281          }
282          ii += draw->pt.user.eltBias;
283          debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
284                       draw->pt.user.eltBias, ii);
285       }
286       else {
287          /* non-indexed arrays */
288          ii = start + i;
289          debug_printf("Vertex %u:\n", ii);
290       }
291 
292       for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
293          uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
294          ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf].map;
295 
296          if (draw->pt.vertex_element[j].instance_divisor) {
297             ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
298          }
299 
300          ptr += draw->pt.vertex_buffer[buf].buffer_offset;
301          ptr += draw->pt.vertex_buffer[buf].stride * ii;
302          ptr += draw->pt.vertex_element[j].src_offset;
303 
304          debug_printf("  Attr %u: ", j);
305          switch (draw->pt.vertex_element[j].src_format) {
306          case PIPE_FORMAT_R32_FLOAT:
307             {
308                float *v = (float *) ptr;
309                debug_printf("R %f  @ %p\n", v[0], (void *) v);
310             }
311             break;
312          case PIPE_FORMAT_R32G32_FLOAT:
313             {
314                float *v = (float *) ptr;
315                debug_printf("RG %f %f  @ %p\n", v[0], v[1], (void *) v);
316             }
317             break;
318          case PIPE_FORMAT_R32G32B32_FLOAT:
319             {
320                float *v = (float *) ptr;
321                debug_printf("RGB %f %f %f  @ %p\n", v[0], v[1], v[2], (void *) v);
322             }
323             break;
324          case PIPE_FORMAT_R32G32B32A32_FLOAT:
325             {
326                float *v = (float *) ptr;
327                debug_printf("RGBA %f %f %f %f  @ %p\n", v[0], v[1], v[2], v[3],
328                             (void *) v);
329             }
330             break;
331          case PIPE_FORMAT_B8G8R8A8_UNORM:
332             {
333                ubyte *u = (ubyte *) ptr;
334                debug_printf("BGRA %d %d %d %d  @ %p\n", u[0], u[1], u[2], u[3],
335                             (void *) u);
336             }
337             break;
338          case PIPE_FORMAT_A8R8G8B8_UNORM:
339             {
340                ubyte *u = (ubyte *) ptr;
341                debug_printf("ARGB %d %d %d %d  @ %p\n", u[0], u[1], u[2], u[3],
342                             (void *) u);
343             }
344             break;
345          default:
346             debug_printf("other format %s (fix me)\n",
347                      util_format_name(draw->pt.vertex_element[j].src_format));
348          }
349       }
350    }
351 }
352 
353 
354 /** Helper code for below */
355 #define PRIM_RESTART_LOOP(elements) \
356    do { \
357       for (j = 0; j < count; j++) {               \
358          i = draw_overflow_uadd(start, j, MAX_LOOP_IDX);  \
359          if (i < elt_max && elements[i] == info->restart_index) { \
360             if (cur_count > 0) { \
361                /* draw elts up to prev pos */ \
362                draw_pt_arrays(draw, prim, cur_start, cur_count); \
363             } \
364             /* begin new prim at next elt */ \
365             cur_start = i + 1; \
366             cur_count = 0; \
367          } \
368          else { \
369             cur_count++; \
370          } \
371       } \
372       if (cur_count > 0) { \
373          draw_pt_arrays(draw, prim, cur_start, cur_count); \
374       } \
375    } while (0)
376 
377 
378 /**
379  * For drawing prims with primitive restart enabled.
380  * Scan for restart indexes and draw the runs of elements/vertices between
381  * the restarts.
382  */
383 static void
draw_pt_arrays_restart(struct draw_context * draw,const struct pipe_draw_info * info)384 draw_pt_arrays_restart(struct draw_context *draw,
385                        const struct pipe_draw_info *info)
386 {
387    const unsigned prim = info->mode;
388    const unsigned start = info->start;
389    const unsigned count = info->count;
390    const unsigned elt_max = draw->pt.user.eltMax;
391    unsigned i, j, cur_start, cur_count;
392    /* The largest index within a loop using the i variable as the index.
393     * Used for overflow detection */
394    const unsigned MAX_LOOP_IDX = 0xffffffff;
395 
396    assert(info->primitive_restart);
397 
398    if (draw->pt.user.eltSize) {
399       /* indexed prims (draw_elements) */
400       cur_start = start;
401       cur_count = 0;
402 
403       switch (draw->pt.user.eltSize) {
404       case 1:
405          {
406             const ubyte *elt_ub = (const ubyte *) draw->pt.user.elts;
407             PRIM_RESTART_LOOP(elt_ub);
408          }
409          break;
410       case 2:
411          {
412             const ushort *elt_us = (const ushort *) draw->pt.user.elts;
413             PRIM_RESTART_LOOP(elt_us);
414          }
415          break;
416       case 4:
417          {
418             const uint *elt_ui = (const uint *) draw->pt.user.elts;
419             PRIM_RESTART_LOOP(elt_ui);
420          }
421          break;
422       default:
423          assert(0 && "bad eltSize in draw_arrays()");
424       }
425    }
426    else {
427       /* Non-indexed prims (draw_arrays).
428        * Primitive restart should have been handled in gallium frontends.
429        */
430       draw_pt_arrays(draw, prim, start, count);
431    }
432 }
433 
434 
435 /**
436  * Resolve true values within pipe_draw_info.
437  * If we're rendering from transform feedback/stream output
438  * buffers both the count and max_index need to be computed
439  * from the attached stream output target.
440  */
441 static void
resolve_draw_info(const struct pipe_draw_info * raw_info,struct pipe_draw_info * info,struct pipe_vertex_buffer * vertex_buffer)442 resolve_draw_info(const struct pipe_draw_info *raw_info,
443                   struct pipe_draw_info *info,
444                   struct pipe_vertex_buffer *vertex_buffer)
445 {
446    memcpy(info, raw_info, sizeof(struct pipe_draw_info));
447 
448    if (raw_info->count_from_stream_output) {
449       struct draw_so_target *target =
450          (struct draw_so_target *)info->count_from_stream_output;
451       assert(vertex_buffer != NULL);
452       info->count = vertex_buffer->stride == 0 ? 0 :
453                        target->internal_offset / vertex_buffer->stride;
454 
455       /* Stream output draw can not be indexed */
456       debug_assert(!info->index_size);
457       info->max_index = info->count - 1;
458    }
459 }
460 
461 /**
462  * Draw vertex arrays.
463  * This is the main entrypoint into the drawing module.  If drawing an indexed
464  * primitive, the draw_set_indexes() function should have already been called
465  * to specify the element/index buffer information.
466  */
467 void
draw_vbo(struct draw_context * draw,const struct pipe_draw_info * info)468 draw_vbo(struct draw_context *draw,
469          const struct pipe_draw_info *info)
470 {
471    unsigned instance;
472    unsigned index_limit;
473    unsigned count;
474    unsigned fpstate = util_fpstate_get();
475    struct pipe_draw_info resolved_info;
476 
477    if (info->instance_count == 0)
478       return;
479 
480    /* Make sure that denorms are treated like zeros. This is
481     * the behavior required by D3D10. OpenGL doesn't care.
482     */
483    util_fpstate_set_denorms_to_zero(fpstate);
484 
485    resolve_draw_info(info, &resolved_info, &(draw->pt.vertex_buffer[0]));
486    info = &resolved_info;
487 
488    if (info->index_size)
489       assert(draw->pt.user.elts);
490 
491    count = info->count;
492 
493    draw->pt.user.eltBias = info->index_bias;
494    draw->pt.user.min_index = info->min_index;
495    draw->pt.user.max_index = info->max_index;
496    draw->pt.user.eltSize = info->index_size ? draw->pt.user.eltSizeIB : 0;
497    draw->pt.user.drawid = info->drawid;
498 
499    draw->pt.vertices_per_patch = info->vertices_per_patch;
500 
501    if (0)
502       debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
503                    info->mode, info->start, count);
504 
505    if (0)
506       tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
507 
508    if (0) {
509       unsigned int i;
510       debug_printf("Elements:\n");
511       for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
512          debug_printf("  %u: src_offset=%u  inst_div=%u   vbuf=%u  format=%s\n",
513                       i,
514                       draw->pt.vertex_element[i].src_offset,
515                       draw->pt.vertex_element[i].instance_divisor,
516                       draw->pt.vertex_element[i].vertex_buffer_index,
517                       util_format_name(draw->pt.vertex_element[i].src_format));
518       }
519       debug_printf("Buffers:\n");
520       for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
521          debug_printf("  %u: stride=%u offset=%u size=%d ptr=%p\n",
522                       i,
523                       draw->pt.vertex_buffer[i].stride,
524                       draw->pt.vertex_buffer[i].buffer_offset,
525                       (int) draw->pt.user.vbuffer[i].size,
526                       draw->pt.user.vbuffer[i].map);
527       }
528    }
529 
530    if (0)
531       draw_print_arrays(draw, info->mode, info->start, MIN2(count, 20));
532 
533    index_limit = util_draw_max_index(draw->pt.vertex_buffer,
534                                      draw->pt.vertex_element,
535                                      draw->pt.nr_vertex_elements,
536                                      info);
537 #ifdef LLVM_AVAILABLE
538    if (!draw->llvm)
539 #endif
540    {
541       if (index_limit == 0) {
542          /* one of the buffers is too small to do any valid drawing */
543          debug_warning("draw: VBO too small to draw anything\n");
544          util_fpstate_set(fpstate);
545          return;
546       }
547    }
548 
549    /* If we're collecting stats then make sure we start from scratch */
550    if (draw->collect_statistics) {
551       memset(&draw->statistics, 0, sizeof(draw->statistics));
552    }
553 
554    draw->pt.max_index = index_limit - 1;
555    draw->start_index = info->start;
556 
557    /*
558     * TODO: We could use draw->pt.max_index to further narrow
559     * the min_index/max_index hints given by gallium frontends.
560     */
561 
562    for (instance = 0; instance < info->instance_count; instance++) {
563       unsigned instance_idx = instance + info->start_instance;
564       draw->start_instance = info->start_instance;
565       draw->instance_id = instance;
566       /* check for overflow */
567       if (instance_idx < instance ||
568           instance_idx < draw->start_instance) {
569          /* if we overflown just set the instance id to the max */
570          draw->instance_id = 0xffffffff;
571       }
572 
573       draw_new_instance(draw);
574 
575       if (info->primitive_restart) {
576          draw_pt_arrays_restart(draw, info);
577       }
578       else {
579          draw_pt_arrays(draw, info->mode, info->start, count);
580       }
581    }
582 
583    /* If requested emit the pipeline statistics for this run */
584    if (draw->collect_statistics) {
585       draw->render->pipeline_statistics(draw->render, &draw->statistics);
586    }
587    util_fpstate_set(fpstate);
588 }
589