u_primconvert.c 6.04 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
 *
 * 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, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

/**
 * This module converts provides a more convenient front-end to u_indices,
 * etc, utils to convert primitive types supported not supported by the
 * hardware.  It handles binding new index buffer state, and restoring
 * previous state after.  To use, put something like this at the front of
 * drivers pipe->draw_vbo():
 *
 *    // emulate unsupported primitives:
 *    if (info->mode needs emulating) {
 *       util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
 *       util_primconvert_draw_vbo(ctx->primconvert, info);
 *       return;
 *    }
 *
 */

#include "pipe/p_state.h"
#include "util/u_draw.h"
#include "util/u_inlines.h"
#include "util/u_memory.h"
47
#include "util/u_prim_restart.h"
48
#include "util/u_upload_mgr.h"
49 50 51 52 53 54 55

#include "indices/u_indices.h"
#include "indices/u_primconvert.h"

struct primconvert_context
{
   struct pipe_context *pipe;
56
   struct primconvert_config cfg;
57 58 59 60 61
   unsigned api_pv;
};


struct primconvert_context *
62 63
util_primconvert_create_config(struct pipe_context *pipe,
                               struct primconvert_config *cfg)
64 65 66 67 68
{
   struct primconvert_context *pc = CALLOC_STRUCT(primconvert_context);
   if (!pc)
      return NULL;
   pc->pipe = pipe;
69
   pc->cfg = *cfg;
70 71 72
   return pc;
}

73 74 75 76 77 78 79
struct primconvert_context *
util_primconvert_create(struct pipe_context *pipe, uint32_t primtypes_mask)
{
   struct primconvert_config cfg = { .primtypes_mask = primtypes_mask };
   return util_primconvert_create_config(pipe, &cfg);
}

80 81 82
void
util_primconvert_destroy(struct primconvert_context *pc)
{
83
   FREE(pc);
84 85 86 87 88 89 90 91 92 93 94
}

void
util_primconvert_save_rasterizer_state(struct primconvert_context *pc,
                                       const struct pipe_rasterizer_state
                                       *rast)
{
   /* if we actually translated the provoking vertex for the buffer,
    * we would actually need to save/restore rasterizer state.  As
    * it is, we just need to make note of the pv.
    */
95
   pc->api_pv = rast->flatshade_first ? PV_FIRST : PV_LAST;
96 97 98 99 100 101 102
}

void
util_primconvert_draw_vbo(struct primconvert_context *pc,
                          const struct pipe_draw_info *info)
{
   struct pipe_draw_info new_info;
103
   struct pipe_transfer *src_transfer = NULL;
104 105
   u_translate_func trans_func;
   u_generate_func gen_func;
106
   const void *src = NULL;
107
   void *dst;
108
   unsigned ib_offset;
109 110

   util_draw_init_info(&new_info);
111 112
   new_info.min_index = info->min_index;
   new_info.max_index = info->max_index;
113
   new_info.index_bias = info->index_bias;
114 115
   new_info.start_instance = info->start_instance;
   new_info.instance_count = info->instance_count;
116 117
   new_info.primitive_restart = info->primitive_restart;
   new_info.restart_index = info->restart_index;
118
   if (info->index_size) {
119
      enum pipe_prim_type mode = 0;
120
      unsigned index_size;
121

122
      u_index_translator(pc->cfg.primtypes_mask,
123
                         info->mode, info->index_size, info->count,
124
                         pc->api_pv, pc->api_pv,
125
                         info->primitive_restart ? PR_ENABLE : PR_DISABLE,
126
                         &mode, &index_size, &new_info.count,
127
                         &trans_func);
128
      new_info.mode = mode;
129 130
      new_info.index_size = index_size;
      src = info->has_user_indices ? info->index.user : NULL;
131
      if (!src) {
132
         src = pipe_buffer_map(pc->pipe, info->index.resource,
133
                               PIPE_MAP_READ, &src_transfer);
134
      }
135
      src = (const uint8_t *)src;
136 137
   }
   else {
138
      enum pipe_prim_type mode = 0;
139
      unsigned index_size;
140

141
      u_index_generator(pc->cfg.primtypes_mask,
142 143
                        info->mode, info->start, info->count,
                        pc->api_pv, pc->api_pv,
144
                        &mode, &index_size, &new_info.count,
145
                        &gen_func);
146
      new_info.mode = mode;
147
      new_info.index_size = index_size;
148 149
   }

150 151 152
   u_upload_alloc(pc->pipe->stream_uploader, 0, new_info.index_size * new_info.count, 4,
                  &ib_offset, &new_info.index.resource, &dst);
   new_info.start = ib_offset / new_info.index_size;
153

154
   if (info->index_size) {
155
      trans_func(src, info->start, info->count, new_info.count, info->restart_index, dst);
156 157 158 159 160 161 162 163

      if (pc->cfg.fixed_prim_restart && info->primitive_restart) {
         new_info.restart_index = (1ull << (new_info.index_size * 8)) - 1;
         if (info->restart_index != new_info.restart_index)
            util_translate_prim_restart_data(new_info.index_size, dst, dst,
                                             new_info.count,
                                             info->restart_index);
      }
164 165 166 167 168 169 170 171
   }
   else {
      gen_func(info->start, new_info.count, dst);
   }

   if (src_transfer)
      pipe_buffer_unmap(pc->pipe, src_transfer);

172
   u_upload_unmap(pc->pipe->stream_uploader);
173 174

   /* to the translated draw: */
175
   pc->pipe->draw_vbo(pc->pipe, &new_info, NULL);
176

177
   pipe_resource_reference(&new_info.index.resource, NULL);
178
}