draw_pt.c 17.7 KB
Newer Older
1 2
/**************************************************************************
 * 
Jose Fonseca's avatar
Jose Fonseca committed
3
 * Copyright 2007 VMware, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
Jose Fonseca's avatar
Jose Fonseca committed
21
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 23 24 25 26 27 28 29
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

 /*
  * Authors:
Jose Fonseca's avatar
Jose Fonseca committed
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 37
#include "draw/draw_private.h"
#include "draw/draw_pt.h"
38
#include "draw/draw_vbuf.h"
Brian Paul's avatar
Brian Paul committed
39
#include "draw/draw_vs.h"
40
#include "tgsi/tgsi_dump.h"
Michal Krol's avatar
Michal Krol committed
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

Brian Paul's avatar
Brian Paul committed
47 48
DEBUG_GET_ONCE_BOOL_OPTION(draw_fse, "DRAW_FSE", FALSE)
DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse, "DRAW_NO_FSE", FALSE)
49

50
/* Overall we split things into:
Chia-I Wu's avatar
Chia-I Wu committed
51
 *     - frontend -- prepare fetch_elts, draw_elts - eg vsplit
52 53 54 55
 *     - middle   -- fetch, shade, cliptest, viewport
 *     - pipeline -- the prim pipeline: clipping, wide lines, etc 
 *     - backend  -- the vbuf_render provided by the driver.
 */
56
static boolean
57
draw_pt_arrays(struct draw_context *draw,
58 59 60 61 62 63
               unsigned prim,
               unsigned start, 
               unsigned count)
{
   struct draw_pt_front_end *frontend = NULL;
   struct draw_pt_middle_end *middle = NULL;
64
   unsigned opt = 0;
65

66 67 68 69
   /* Sanitize primitive length:
    */
   {
      unsigned first, incr;
70 71 72 73 74 75

      if (prim == PIPE_PRIM_PATCHES) {
         first = draw->pt.vertices_per_patch;
         incr = draw->pt.vertices_per_patch;
      } else
         draw_pt_split_prim(prim, &first, &incr);
76
      count = draw_pt_trim_count(count, first, incr);
77 78 79 80
      if (count < first)
         return TRUE;
   }

81
   if (!draw->force_passthrough) {
82 83 84 85 86 87
      unsigned out_prim = prim;

      if (draw->gs.geometry_shader)
         out_prim = draw->gs.geometry_shader->output_primitive;
      else if (draw->tes.tess_eval_shader)
         out_prim = get_tes_output_prim(draw->tes.tess_eval_shader);
88

89 90 91
      if (!draw->render) {
         opt |= PT_PIPELINE;
      }
92

93 94
      if (draw_need_pipeline(draw,
                             draw->rasterizer,
95
                             out_prim)) {
96 97 98
         opt |= PT_PIPELINE;
      }

99 100 101
      if ((draw->clip_xy ||
           draw->clip_z ||
           draw->clip_user) && !draw->pt.test_fse) {
102 103
         opt |= PT_CLIPTEST;
      }
104

105
      opt |= PT_SHADE;
106
   }
107

108
   if (draw->pt.middle.llvm) {
109 110 111 112 113 114 115 116 117
      middle = draw->pt.middle.llvm;
   } else {
      if (opt == 0)
         middle = draw->pt.middle.fetch_emit;
      else if (opt == PT_SHADE && !draw->pt.no_fse)
         middle = draw->pt.middle.fetch_shade_emit;
      else
         middle = draw->pt.middle.general;
   }
118

119 120
   frontend = draw->pt.frontend;

121
   if (frontend) {
122 123 124 125 126 127 128 129 130 131 132 133
      if (draw->pt.prim != prim || draw->pt.opt != opt) {
         /* In certain conditions switching primitives requires us to flush
          * and validate the different stages. One example is when smooth
          * lines are active but first drawn with triangles and then with
          * lines.
          */
         draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
         frontend = NULL;
      } else if (draw->pt.eltSize != draw->pt.user.eltSize) {
         /* Flush draw state if eltSize changed.
          * This could be improved so only the frontend is flushed since it
          * converts all indices to ushorts and the fetch part of the middle
Brian Paul's avatar
Brian Paul committed
134
          * always prepares both linear and indexed.
135 136 137 138 139
          */
         frontend->flush( frontend, DRAW_FLUSH_STATE_CHANGE );
         frontend = NULL;
      }
   }
140

141 142
   if (!frontend) {
      frontend = draw->pt.front.vsplit;
143

144
      frontend->prepare( frontend, prim, middle, opt );
145

146 147 148 149 150 151
      draw->pt.frontend = frontend;
      draw->pt.eltSize = draw->pt.user.eltSize;
      draw->pt.prim = prim;
      draw->pt.opt = opt;
   }

152 153 154 155 156 157
   if (draw->pt.rebind_parameters) {
      /* update constants, viewport dims, clip planes, etc */
      middle->bind_parameters(middle);
      draw->pt.rebind_parameters = FALSE;
   }

158
   frontend->run( frontend, start, count );
159 160 161 162

   return TRUE;
}

163 164
void draw_pt_flush( struct draw_context *draw, unsigned flags )
{
165 166
   assert(flags);

167 168 169 170
   if (draw->pt.frontend) {
      draw->pt.frontend->flush( draw->pt.frontend, flags );

      /* don't prepare if we only are flushing the backend */
171
      if (flags & DRAW_FLUSH_STATE_CHANGE)
172 173
         draw->pt.frontend = NULL;
   }
174 175 176 177

   if (flags & DRAW_FLUSH_PARAMETER_CHANGE) {
      draw->pt.rebind_parameters = TRUE;
   }
178 179 180
}


181 182 183

boolean draw_pt_init( struct draw_context *draw )
{
184 185
   draw->pt.test_fse = debug_get_option_draw_fse();
   draw->pt.no_fse = debug_get_option_draw_no_fse();
186

Chia-I Wu's avatar
Chia-I Wu committed
187 188
   draw->pt.front.vsplit = draw_pt_vsplit(draw);
   if (!draw->pt.front.vsplit)
189 190
      return FALSE;

191 192 193
   draw->pt.middle.fetch_emit = draw_pt_fetch_emit( draw );
   if (!draw->pt.middle.fetch_emit)
      return FALSE;
194

Keith Whitwell's avatar
Keith Whitwell committed
195 196 197
   draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
   if (!draw->pt.middle.fetch_shade_emit)
      return FALSE;
198

199
   draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
200
   if (!draw->pt.middle.general)
201 202
      return FALSE;

203
#ifdef LLVM_AVAILABLE
204
   if (draw->llvm)
205 206 207
      draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
#endif

208 209 210 211 212 213
   return TRUE;
}


void draw_pt_destroy( struct draw_context *draw )
{
214 215 216 217 218
   if (draw->pt.middle.llvm) {
      draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
      draw->pt.middle.llvm = NULL;
   }

219 220 221 222
   if (draw->pt.middle.general) {
      draw->pt.middle.general->destroy( draw->pt.middle.general );
      draw->pt.middle.general = NULL;
   }
223

224 225 226 227
   if (draw->pt.middle.fetch_emit) {
      draw->pt.middle.fetch_emit->destroy( draw->pt.middle.fetch_emit );
      draw->pt.middle.fetch_emit = NULL;
   }
228

229 230 231 232 233
   if (draw->pt.middle.fetch_shade_emit) {
      draw->pt.middle.fetch_shade_emit->destroy( draw->pt.middle.fetch_shade_emit );
      draw->pt.middle.fetch_shade_emit = NULL;
   }

Chia-I Wu's avatar
Chia-I Wu committed
234 235 236
   if (draw->pt.front.vsplit) {
      draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
      draw->pt.front.vsplit = NULL;
237
   }
238
}
Keith Whitwell's avatar
Keith Whitwell committed
239 240


241 242 243 244 245 246 247 248 249 250 251 252
/**
 * Debug- print the first 'count' vertices.
 */
static void
draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
{
   uint i;

   debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
                prim, start, count);

   for (i = 0; i < count; i++) {
253 254
      uint ii = 0;
      uint j;
255

256
      if (draw->pt.user.eltSize) {
257
         /* indexed arrays */
258

259 260 261
         switch (draw->pt.user.eltSize) {
         case 1:
            {
262
               const ubyte *elem = (const ubyte *) draw->pt.user.elts;
263 264 265 266 267
               ii = elem[start + i];
            }
            break;
         case 2:
            {
268
               const ushort *elem = (const ushort *) draw->pt.user.elts;
269 270 271 272 273
               ii = elem[start + i];
            }
            break;
         case 4:
            {
274
               const uint *elem = (const uint *) draw->pt.user.elts;
275 276 277 278 279
               ii = elem[start + i];
            }
            break;
         default:
            assert(0);
Jose Fonseca's avatar
Jose Fonseca committed
280
            return;
281
         }
Jose Fonseca's avatar
Jose Fonseca committed
282 283 284
         ii += draw->pt.user.eltBias;
         debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
                      draw->pt.user.eltBias, ii);
285 286 287 288 289 290 291 292 293
      }
      else {
         /* non-indexed arrays */
         ii = start + i;
         debug_printf("Vertex %u:\n", ii);
      }

      for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
         uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
294
         ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf].map;
295 296 297 298 299

         if (draw->pt.vertex_element[j].instance_divisor) {
            ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
         }

300
         ptr += draw->pt.vertex_buffer[buf].buffer_offset;
301
         ptr += draw->pt.vertex_buffer[buf].stride * ii;
302 303 304 305 306 307 308
         ptr += draw->pt.vertex_element[j].src_offset;

         debug_printf("  Attr %u: ", j);
         switch (draw->pt.vertex_element[j].src_format) {
         case PIPE_FORMAT_R32_FLOAT:
            {
               float *v = (float *) ptr;
Brian Paul's avatar
Brian Paul committed
309
               debug_printf("R %f  @ %p\n", v[0], (void *) v);
310 311 312 313 314
            }
            break;
         case PIPE_FORMAT_R32G32_FLOAT:
            {
               float *v = (float *) ptr;
Brian Paul's avatar
Brian Paul committed
315
               debug_printf("RG %f %f  @ %p\n", v[0], v[1], (void *) v);
316 317 318 319 320
            }
            break;
         case PIPE_FORMAT_R32G32B32_FLOAT:
            {
               float *v = (float *) ptr;
Brian Paul's avatar
Brian Paul committed
321
               debug_printf("RGB %f %f %f  @ %p\n", v[0], v[1], v[2], (void *) v);
322 323 324 325 326
            }
            break;
         case PIPE_FORMAT_R32G32B32A32_FLOAT:
            {
               float *v = (float *) ptr;
Brian Paul's avatar
Brian Paul committed
327
               debug_printf("RGBA %f %f %f %f  @ %p\n", v[0], v[1], v[2], v[3],
328 329 330
                            (void *) v);
            }
            break;
Brian Paul's avatar
Brian Paul committed
331 332 333 334 335 336 337
         case PIPE_FORMAT_B8G8R8A8_UNORM:
            {
               ubyte *u = (ubyte *) ptr;
               debug_printf("BGRA %d %d %d %d  @ %p\n", u[0], u[1], u[2], u[3],
                            (void *) u);
            }
            break;
338 339 340 341 342 343 344
         case PIPE_FORMAT_A8R8G8B8_UNORM:
            {
               ubyte *u = (ubyte *) ptr;
               debug_printf("ARGB %d %d %d %d  @ %p\n", u[0], u[1], u[2], u[3],
                            (void *) u);
            }
            break;
345
         default:
Brian Paul's avatar
Brian Paul committed
346 347
            debug_printf("other format %s (fix me)\n",
                     util_format_name(draw->pt.vertex_element[j].src_format));
348 349 350 351
         }
      }
   }
}
Keith Whitwell's avatar
Keith Whitwell committed
352 353


354 355 356
/** Helper code for below */
#define PRIM_RESTART_LOOP(elements) \
   do { \
357 358
      for (j = 0; j < count; j++) {               \
         i = draw_overflow_uadd(start, j, MAX_LOOP_IDX);  \
359
         if (i < elt_max && elements[i] == info->restart_index) { \
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
            if (cur_count > 0) { \
               /* draw elts up to prev pos */ \
               draw_pt_arrays(draw, prim, cur_start, cur_count); \
            } \
            /* begin new prim at next elt */ \
            cur_start = i + 1; \
            cur_count = 0; \
         } \
         else { \
            cur_count++; \
         } \
      } \
      if (cur_count > 0) { \
         draw_pt_arrays(draw, prim, cur_start, cur_count); \
      } \
   } while (0)


/**
 * For drawing prims with primitive restart enabled.
 * Scan for restart indexes and draw the runs of elements/vertices between
 * the restarts.
 */
static void
draw_pt_arrays_restart(struct draw_context *draw,
                       const struct pipe_draw_info *info)
{
   const unsigned prim = info->mode;
   const unsigned start = info->start;
   const unsigned count = info->count;
390
   const unsigned elt_max = draw->pt.user.eltMax;
391 392 393 394
   unsigned i, j, cur_start, cur_count;
   /* The largest index within a loop using the i variable as the index.
    * Used for overflow detection */
   const unsigned MAX_LOOP_IDX = 0xffffffff;
395 396 397

   assert(info->primitive_restart);

398
   if (draw->pt.user.eltSize) {
399 400 401 402 403 404 405
      /* indexed prims (draw_elements) */
      cur_start = start;
      cur_count = 0;

      switch (draw->pt.user.eltSize) {
      case 1:
         {
406
            const ubyte *elt_ub = (const ubyte *) draw->pt.user.elts;
407 408 409 410 411
            PRIM_RESTART_LOOP(elt_ub);
         }
         break;
      case 2:
         {
412
            const ushort *elt_us = (const ushort *) draw->pt.user.elts;
413 414 415 416 417
            PRIM_RESTART_LOOP(elt_us);
         }
         break;
      case 4:
         {
418
            const uint *elt_ui = (const uint *) draw->pt.user.elts;
419 420 421 422 423 424 425 426 427
            PRIM_RESTART_LOOP(elt_ui);
         }
         break;
      default:
         assert(0 && "bad eltSize in draw_arrays()");
      }
   }
   else {
      /* Non-indexed prims (draw_arrays).
428
       * Primitive restart should have been handled in gallium frontends.
429 430 431 432 433 434
       */
      draw_pt_arrays(draw, prim, start, count);
   }
}


435 436 437 438 439 440 441 442
/**
 * Resolve true values within pipe_draw_info.
 * If we're rendering from transform feedback/stream output
 * buffers both the count and max_index need to be computed
 * from the attached stream output target. 
 */
static void
resolve_draw_info(const struct pipe_draw_info *raw_info,
443 444
                  struct pipe_draw_info *info,
                  struct pipe_vertex_buffer *vertex_buffer)
445 446 447
{
   memcpy(info, raw_info, sizeof(struct pipe_draw_info));

448
   if (raw_info->indirect && raw_info->indirect->count_from_stream_output) {
449
      struct draw_so_target *target =
450
         (struct draw_so_target *)info->indirect->count_from_stream_output;
451
      assert(vertex_buffer != NULL);
452 453
      info->count = vertex_buffer->stride == 0 ? 0 :
                       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
      info->indirect = NULL;
459 460
   }
}
461 462 463 464

/**
 * Draw vertex arrays.
 * This is the main entrypoint into the drawing module.  If drawing an indexed
465 466
 * primitive, the draw_set_indexes() function should have already been called
 * to specify the element/index buffer information.
467 468 469 470 471
 */
void
draw_vbo(struct draw_context *draw,
         const struct pipe_draw_info *info)
{
472
   unsigned instance;
473
   unsigned index_limit;
474
   unsigned count;
475
   unsigned fpstate = util_fpstate_get();
476 477
   struct pipe_draw_info resolved_info;

478 479 480
   if (info->instance_count == 0)
      return;

481 482 483 484 485
   /* Make sure that denorms are treated like zeros. This is 
    * the behavior required by D3D10. OpenGL doesn't care.
    */
   util_fpstate_set_denorms_to_zero(fpstate);

486
   resolve_draw_info(info, &resolved_info, &(draw->pt.vertex_buffer[0]));
487 488
   info = &resolved_info;

489
   if (info->index_size)
490
      assert(draw->pt.user.elts);
491

492
   count = info->count;
493 494 495 496

   draw->pt.user.eltBias = info->index_bias;
   draw->pt.user.min_index = info->min_index;
   draw->pt.user.max_index = info->max_index;
497
   draw->pt.user.eltSize = info->index_size ? draw->pt.user.eltSizeIB : 0;
498
   draw->pt.user.drawid = info->drawid;
499

500 501
   draw->pt.vertices_per_patch = info->vertices_per_patch;

502
   if (0)
503
      debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
504
                   info->mode, info->start, count);
505 506

   if (0)
507
      tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
508 509 510

   if (0) {
      unsigned int i;
511 512
      debug_printf("Elements:\n");
      for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
Brian Paul's avatar
Brian Paul committed
513 514 515 516 517
         debug_printf("  %u: src_offset=%u  inst_div=%u   vbuf=%u  format=%s\n",
                      i,
                      draw->pt.vertex_element[i].src_offset,
                      draw->pt.vertex_element[i].instance_divisor,
                      draw->pt.vertex_element[i].vertex_buffer_index,
518
                      util_format_name(draw->pt.vertex_element[i].src_format));
519 520 521
      }
      debug_printf("Buffers:\n");
      for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
522
         debug_printf("  %u: stride=%u offset=%u size=%d ptr=%p\n",
Brian Paul's avatar
Brian Paul committed
523
                      i,
524
                      draw->pt.vertex_buffer[i].stride,
525
                      draw->pt.vertex_buffer[i].buffer_offset,
526
                      (int) draw->pt.user.vbuffer[i].size,
527
                      draw->pt.user.vbuffer[i].map);
528 529 530
      }
   }

531
   if (0)
532
      draw_print_arrays(draw, info->mode, info->start, MIN2(count, 20));
533

534 535 536 537
   index_limit = util_draw_max_index(draw->pt.vertex_buffer,
                                     draw->pt.vertex_element,
                                     draw->pt.nr_vertex_elements,
                                     info);
538
#ifdef LLVM_AVAILABLE
539 540 541 542
   if (!draw->llvm)
#endif
   {
      if (index_limit == 0) {
Brian Paul's avatar
Brian Paul committed
543
         /* one of the buffers is too small to do any valid drawing */
544
         debug_warning("draw: VBO too small to draw anything\n");
545
         util_fpstate_set(fpstate);
546 547
         return;
      }
548 549
   }

550 551 552 553 554
   /* If we're collecting stats then make sure we start from scratch */
   if (draw->collect_statistics) {
      memset(&draw->statistics, 0, sizeof(draw->statistics));
   }

555
   draw->pt.max_index = index_limit - 1;
Zack Rusin's avatar
Zack Rusin committed
556
   draw->start_index = info->start;
557

558 559
   /*
    * TODO: We could use draw->pt.max_index to further narrow
560
    * the min_index/max_index hints given by gallium frontends.
561 562
    */

563
   for (instance = 0; instance < info->instance_count; instance++) {
564
      unsigned instance_idx = instance + info->start_instance;
Zack Rusin's avatar
Zack Rusin committed
565
      draw->start_instance = info->start_instance;
566
      draw->instance_id = instance;
567
      /* check for overflow */
568 569
      if (instance_idx < instance ||
          instance_idx < draw->start_instance) {
570 571 572
         /* if we overflown just set the instance id to the max */
         draw->instance_id = 0xffffffff;
      }
573

574 575
      draw_new_instance(draw);

576 577 578 579
      if (info->primitive_restart) {
         draw_pt_arrays_restart(draw, info);
      }
      else {
580
         draw_pt_arrays(draw, info->mode, info->start, count);
581
      }
582
   }
583 584 585 586 587

   /* If requested emit the pipeline statistics for this run */
   if (draw->collect_statistics) {
      draw->render->pipeline_statistics(draw->render, &draw->statistics);
   }
588
   util_fpstate_set(fpstate);
589
}