u_prim_restart.c 9.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2014 VMware, Inc.
 * 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.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * 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.
 */



#include "u_inlines.h"
29
#include "util/u_memory.h"
30 31
#include "u_prim_restart.h"

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
typedef struct {
  uint32_t count;
  uint32_t primCount;
  uint32_t firstIndex;
  int32_t  baseVertex;
  uint32_t reservedMustBeZero;
} DrawElementsIndirectCommand;

static DrawElementsIndirectCommand
read_indirect_elements(struct pipe_context *context, struct pipe_draw_indirect_info *indirect)
{
   DrawElementsIndirectCommand ret;
   struct pipe_transfer *transfer = NULL;
   void *map = NULL;
   /* we only need the first 3 members */
   unsigned read_size = 3 * sizeof(uint32_t);
   assert(indirect->buffer->width0 > 3 * sizeof(uint32_t));
   map = pipe_buffer_map_range(context, indirect->buffer,
                                   indirect->offset,
                                   read_size,
52
                                   PIPE_MAP_READ,
53 54 55 56 57 58
                                   &transfer);
   assert(map);
   memcpy(&ret, map, read_size);
   pipe_buffer_unmap(context, transfer);
   return ret;
}
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
void
util_translate_prim_restart_data(unsigned index_size,
                                 void *src_map, void *dst_map,
                                 unsigned count, unsigned restart_index)
{
   if (index_size == 1) {
      uint8_t *src = (uint8_t *) src_map;
      uint16_t *dst = (uint16_t *) dst_map;
      unsigned i;
      for (i = 0; i < count; i++) {
         dst[i] = (src[i] == restart_index) ? 0xffff : src[i];
      }
   }
   else if (index_size == 2) {
      uint16_t *src = (uint16_t *) src_map;
      uint16_t *dst = (uint16_t *) dst_map;
      unsigned i;
      for (i = 0; i < count; i++) {
         dst[i] = (src[i] == restart_index) ? 0xffff : src[i];
      }
   }
   else {
      uint32_t *src = (uint32_t *) src_map;
      uint32_t *dst = (uint32_t *) dst_map;
      unsigned i;
      assert(index_size == 4);
      for (i = 0; i < count; i++) {
         dst[i] = (src[i] == restart_index) ? 0xffffffff : src[i];
      }
   }
}

92 93 94 95 96 97 98 99 100
/**
 * Translate an index buffer for primitive restart.
 * Create a new index buffer which is a copy of the original index buffer
 * except that instances of 'restart_index' are converted to 0xffff or
 * 0xffffffff.
 * Also, index buffers using 1-byte indexes are converted to 2-byte indexes.
 */
enum pipe_error
util_translate_prim_restart_ib(struct pipe_context *context,
101 102
                               const struct pipe_draw_info *info,
                               struct pipe_resource **dst_buffer)
103 104 105 106
{
   struct pipe_screen *screen = context->screen;
   struct pipe_transfer *src_transfer = NULL, *dst_transfer = NULL;
   void *src_map = NULL, *dst_map = NULL;
107
   const unsigned src_index_size = info->index_size;
108
   unsigned dst_index_size;
109 110 111
   DrawElementsIndirectCommand indirect;
   unsigned count = info->count;
   unsigned start = info->start;
112 113

   /* 1-byte indexes are converted to 2-byte indexes, 4-byte stays 4-byte */
114
   dst_index_size = MAX2(2, info->index_size);
115 116
   assert(dst_index_size == 2 || dst_index_size == 4);

117
   if (info->indirect && info->indirect->buffer) {
118 119 120 121 122
      indirect = read_indirect_elements(context, info->indirect);
      count = indirect.count;
      start = indirect.firstIndex;
   }

123 124 125
   /* Create new index buffer */
   *dst_buffer = pipe_buffer_create(screen, PIPE_BIND_INDEX_BUFFER,
                                    PIPE_USAGE_STREAM,
126
                                    count * dst_index_size);
127 128 129 130 131
   if (!*dst_buffer)
      goto error;

   /* Map new / dest index buffer */
   dst_map = pipe_buffer_map(context, *dst_buffer,
132
                             PIPE_MAP_WRITE, &dst_transfer);
133 134 135
   if (!dst_map)
      goto error;

136
   if (info->has_user_indices)
137
      src_map = (unsigned char*)info->index.user + start * src_index_size;
138 139 140
   else
      /* Map original / src index buffer */
      src_map = pipe_buffer_map_range(context, info->index.resource,
141 142
                                      start * src_index_size,
                                      count * src_index_size,
143
                                      PIPE_MAP_READ,
144
                                      &src_transfer);
145 146 147
   if (!src_map)
      goto error;

148 149
   util_translate_prim_restart_data(src_index_size, src_map, dst_map,
                                    info->count, info->restart_index);
150

151 152
   if (src_transfer)
      pipe_buffer_unmap(context, src_transfer);
153 154 155 156 157 158 159 160 161 162
   pipe_buffer_unmap(context, dst_transfer);

   return PIPE_OK;

error:
   if (src_transfer)
      pipe_buffer_unmap(context, src_transfer);
   if (dst_transfer)
      pipe_buffer_unmap(context, dst_transfer);
   if (*dst_buffer)
163
      pipe_resource_reference(dst_buffer, NULL);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
   return PIPE_ERROR_OUT_OF_MEMORY;
}


/** Helper structs for util_draw_vbo_without_prim_restart() */

struct range {
   unsigned start, count;
};

struct range_info {
   struct range *ranges;
   unsigned count, max;
};


/**
 * Helper function for util_draw_vbo_without_prim_restart()
 * \return true for success, false if out of memory
 */
static boolean
add_range(struct range_info *info, unsigned start, unsigned count)
{
   if (info->max == 0) {
      info->max = 10;
      info->ranges = MALLOC(info->max * sizeof(struct range));
      if (!info->ranges) {
         return FALSE;
      }
   }
   else if (info->count == info->max) {
      /* grow the ranges[] array */
      info->ranges = REALLOC(info->ranges,
                             info->max * sizeof(struct range),
                             2 * info->max * sizeof(struct range));
      if (!info->ranges) {
         return FALSE;
      }

      info->max *= 2;
   }

   /* save the range */
   info->ranges[info->count].start = start;
   info->ranges[info->count].count = count;
   info->count++;

   return TRUE;
}


/**
 * Implement primitive restart by breaking an indexed primitive into
 * pieces which do not contain restart indexes.  Each piece is then
 * drawn by calling pipe_context::draw_vbo().
 * \return PIPE_OK if no error, an error code otherwise.
 */
enum pipe_error
util_draw_vbo_without_prim_restart(struct pipe_context *context,
                                   const struct pipe_draw_info *info)
{
   const void *src_map;
   struct range_info ranges = {0};
   struct pipe_draw_info new_info;
   struct pipe_transfer *src_transfer = NULL;
   unsigned i, start, count;
230 231 232 233
   DrawElementsIndirectCommand indirect;
   unsigned info_start = info->start;
   unsigned info_count = info->count;
   unsigned info_instance_count = info->instance_count;
234

235
   assert(info->index_size);
236 237
   assert(info->primitive_restart);

238
   if (info->indirect && info->indirect->buffer) {
239 240 241 242 243 244
      indirect = read_indirect_elements(context, info->indirect);
      info_count = indirect.count;
      info_start = indirect.firstIndex;
      info_instance_count = indirect.primCount;
   }

245
   /* Get pointer to the index data */
246
   if (!info->has_user_indices) {
247
      /* map the index buffer (only the range we need to scan) */
248
      src_map = pipe_buffer_map_range(context, info->index.resource,
249 250
                                      info_start * info->index_size,
                                      info_count * info->index_size,
251
                                      PIPE_MAP_READ,
252 253 254 255 256 257
                                      &src_transfer);
      if (!src_map) {
         return PIPE_ERROR_OUT_OF_MEMORY;
      }
   }
   else {
258
      if (!info->index.user) {
259 260 261
         debug_printf("User-space index buffer is null!");
         return PIPE_ERROR_BAD_INPUT;
      }
262
      src_map = (const uint8_t *) info->index.user
263
         + info_start * info->index_size;
264 265 266
   }

#define SCAN_INDEXES(TYPE) \
267 268
   for (i = 0; i <= info_count; i++) { \
      if (i == info_count || \
269 270 271
          ((const TYPE *) src_map)[i] == info->restart_index) { \
         /* cut / restart */ \
         if (count > 0) { \
272
            if (!add_range(&ranges, info_start + start, count)) { \
273 274 275 276 277 278 279 280 281 282 283 284 285
               if (src_transfer) \
                  pipe_buffer_unmap(context, src_transfer); \
               return PIPE_ERROR_OUT_OF_MEMORY; \
            } \
         } \
         start = i + 1; \
         count = 0; \
      } \
      else { \
         count++; \
      } \
   }

286
   start = 0;
287
   count = 0;
288
   switch (info->index_size) {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
   case 1:
      SCAN_INDEXES(uint8_t);
      break;
   case 2:
      SCAN_INDEXES(uint16_t);
      break;
   case 4:
      SCAN_INDEXES(uint32_t);
      break;
   default:
      assert(!"Bad index size");
      return PIPE_ERROR_BAD_INPUT;
   }

   /* unmap index buffer */
   if (src_transfer)
      pipe_buffer_unmap(context, src_transfer);

   /* draw ranges between the restart indexes */
   new_info = *info;
309 310 311
   /* we've effectively remapped this to a direct draw */
   new_info.indirect = NULL;
   new_info.instance_count = info_instance_count;
312 313 314 315 316 317 318 319 320 321 322
   new_info.primitive_restart = FALSE;
   for (i = 0; i < ranges.count; i++) {
      new_info.start = ranges.ranges[i].start;
      new_info.count = ranges.ranges[i].count;
      context->draw_vbo(context, &new_info);
   }

   FREE(ranges.ranges);

   return PIPE_OK;
}