• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 /**
28  * This module converts provides a more convenient front-end to u_indices,
29  * etc, utils to convert primitive types supported not supported by the
30  * hardware.  It handles binding new index buffer state, and restoring
31  * previous state after.  To use, put something like this at the front of
32  * drivers pipe->draw_vbo():
33  *
34  *    // emulate unsupported primitives:
35  *    if (info->mode needs emulating) {
36  *       util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
37  *       util_primconvert_draw_vbo(ctx->primconvert, info);
38  *       return;
39  *    }
40  *
41  */
42 
43 #include "pipe/p_state.h"
44 #include "util/u_draw.h"
45 #include "util/u_inlines.h"
46 #include "util/u_memory.h"
47 #include "util/u_upload_mgr.h"
48 
49 #include "indices/u_indices.h"
50 #include "indices/u_primconvert.h"
51 
52 struct primconvert_context
53 {
54    struct pipe_context *pipe;
55    uint32_t primtypes_mask;
56    unsigned api_pv;
57 };
58 
59 
60 struct primconvert_context *
util_primconvert_create(struct pipe_context * pipe,uint32_t primtypes_mask)61 util_primconvert_create(struct pipe_context *pipe, uint32_t primtypes_mask)
62 {
63    struct primconvert_context *pc = CALLOC_STRUCT(primconvert_context);
64    if (!pc)
65       return NULL;
66    pc->pipe = pipe;
67    pc->primtypes_mask = primtypes_mask;
68    return pc;
69 }
70 
71 void
util_primconvert_destroy(struct primconvert_context * pc)72 util_primconvert_destroy(struct primconvert_context *pc)
73 {
74    FREE(pc);
75 }
76 
77 void
util_primconvert_save_rasterizer_state(struct primconvert_context * pc,const struct pipe_rasterizer_state * rast)78 util_primconvert_save_rasterizer_state(struct primconvert_context *pc,
79                                        const struct pipe_rasterizer_state
80                                        *rast)
81 {
82    /* if we actually translated the provoking vertex for the buffer,
83     * we would actually need to save/restore rasterizer state.  As
84     * it is, we just need to make note of the pv.
85     */
86    pc->api_pv = rast->flatshade_first ? PV_FIRST : PV_LAST;
87 }
88 
89 void
util_primconvert_draw_vbo(struct primconvert_context * pc,const struct pipe_draw_info * info)90 util_primconvert_draw_vbo(struct primconvert_context *pc,
91                           const struct pipe_draw_info *info)
92 {
93    struct pipe_draw_info new_info;
94    struct pipe_transfer *src_transfer = NULL;
95    u_translate_func trans_func;
96    u_generate_func gen_func;
97    const void *src = NULL;
98    void *dst;
99    unsigned ib_offset;
100 
101    util_draw_init_info(&new_info);
102    new_info.min_index = info->min_index;
103    new_info.max_index = info->max_index;
104    new_info.index_bias = info->index_bias;
105    new_info.start_instance = info->start_instance;
106    new_info.instance_count = info->instance_count;
107    new_info.primitive_restart = info->primitive_restart;
108    new_info.restart_index = info->restart_index;
109    if (info->index_size) {
110       enum pipe_prim_type mode = 0;
111       unsigned index_size;
112 
113       u_index_translator(pc->primtypes_mask,
114                          info->mode, info->index_size, info->count,
115                          pc->api_pv, pc->api_pv,
116                          info->primitive_restart ? PR_ENABLE : PR_DISABLE,
117                          &mode, &index_size, &new_info.count,
118                          &trans_func);
119       new_info.mode = mode;
120       new_info.index_size = index_size;
121       src = info->has_user_indices ? info->index.user : NULL;
122       if (!src) {
123          src = pipe_buffer_map(pc->pipe, info->index.resource,
124                                PIPE_TRANSFER_READ, &src_transfer);
125       }
126       src = (const uint8_t *)src;
127    }
128    else {
129       enum pipe_prim_type mode = 0;
130       unsigned index_size;
131 
132       u_index_generator(pc->primtypes_mask,
133                         info->mode, info->start, info->count,
134                         pc->api_pv, pc->api_pv,
135                         &mode, &index_size, &new_info.count,
136                         &gen_func);
137       new_info.mode = mode;
138       new_info.index_size = index_size;
139    }
140 
141    u_upload_alloc(pc->pipe->stream_uploader, 0, new_info.index_size * new_info.count, 4,
142                   &ib_offset, &new_info.index.resource, &dst);
143    new_info.start = ib_offset / new_info.index_size;
144 
145    if (info->index_size) {
146       trans_func(src, info->start, info->count, new_info.count, info->restart_index, dst);
147    }
148    else {
149       gen_func(info->start, new_info.count, dst);
150    }
151 
152    if (src_transfer)
153       pipe_buffer_unmap(pc->pipe, src_transfer);
154 
155    u_upload_unmap(pc->pipe->stream_uploader);
156 
157    /* to the translated draw: */
158    pc->pipe->draw_vbo(pc->pipe, &new_info);
159 
160    pipe_resource_reference(&new_info.index.resource, NULL);
161 }
162