indices: add comments, assertions in u_indices.c file
[mesa.git] / src / gallium / auxiliary / indices / u_primconvert.c
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_index_buffer(ctx->primconvert, &ctx->indexbuf);
37 * util_primconvert_save_rasterizer_state(ctx->primconvert, ctx->rasterizer);
38 * util_primconvert_draw_vbo(ctx->primconvert, info);
39 * return;
40 * }
41 *
42 */
43
44 #include "pipe/p_state.h"
45 #include "util/u_draw.h"
46 #include "util/u_inlines.h"
47 #include "util/u_memory.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 struct pipe_index_buffer saved_ib;
56 uint32_t primtypes_mask;
57 unsigned api_pv;
58 // TODO we could cache/recycle the indexbuf created to translate prims..
59 };
60
61
62 struct primconvert_context *
63 util_primconvert_create(struct pipe_context *pipe, uint32_t primtypes_mask)
64 {
65 struct primconvert_context *pc = CALLOC_STRUCT(primconvert_context);
66 if (!pc)
67 return NULL;
68 pc->pipe = pipe;
69 pc->primtypes_mask = primtypes_mask;
70 return pc;
71 }
72
73 void
74 util_primconvert_destroy(struct primconvert_context *pc)
75 {
76 util_primconvert_save_index_buffer(pc, NULL);
77 free(pc);
78 }
79
80 void
81 util_primconvert_save_index_buffer(struct primconvert_context *pc,
82 const struct pipe_index_buffer *ib)
83 {
84 if (ib) {
85 pipe_resource_reference(&pc->saved_ib.buffer, ib->buffer);
86 pc->saved_ib.index_size = ib->index_size;
87 pc->saved_ib.offset = ib->offset;
88 pc->saved_ib.user_buffer = ib->user_buffer;
89 }
90 else {
91 pipe_resource_reference(&pc->saved_ib.buffer, NULL);
92 }
93 }
94
95 void
96 util_primconvert_save_rasterizer_state(struct primconvert_context *pc,
97 const struct pipe_rasterizer_state
98 *rast)
99 {
100 /* if we actually translated the provoking vertex for the buffer,
101 * we would actually need to save/restore rasterizer state. As
102 * it is, we just need to make note of the pv.
103 */
104 pc->api_pv = (rast->flatshade
105 && !rast->flatshade_first) ? PV_LAST : PV_FIRST;
106 }
107
108 void
109 util_primconvert_draw_vbo(struct primconvert_context *pc,
110 const struct pipe_draw_info *info)
111 {
112 struct pipe_index_buffer *ib = &pc->saved_ib;
113 struct pipe_index_buffer new_ib;
114 struct pipe_draw_info new_info;
115 struct pipe_transfer *src_transfer = NULL, *dst_transfer = NULL;
116 u_translate_func trans_func;
117 u_generate_func gen_func;
118 const void *src;
119 void *dst;
120
121 memset(&new_ib, 0, sizeof(new_ib));
122 util_draw_init_info(&new_info);
123 new_info.indexed = true;
124
125 if (info->indexed) {
126 u_index_translator(pc->primtypes_mask,
127 info->mode, pc->saved_ib.index_size, info->count,
128 pc->api_pv, pc->api_pv,
129 &new_info.mode, &new_ib.index_size, &new_info.count,
130 &trans_func);
131 src = ib->user_buffer;
132 if (!src) {
133 src = pipe_buffer_map(pc->pipe, ib->buffer,
134 PIPE_TRANSFER_READ, &src_transfer);
135 }
136 }
137 else {
138 u_index_generator(pc->primtypes_mask,
139 info->mode, info->start, info->count,
140 pc->api_pv, pc->api_pv,
141 &new_info.mode, &new_ib.index_size, &new_info.count,
142 &gen_func);
143 }
144
145
146 new_ib.buffer = pipe_buffer_create(pc->pipe->screen,
147 PIPE_BIND_INDEX_BUFFER,
148 PIPE_USAGE_IMMUTABLE,
149 new_ib.index_size * new_info.count);
150 dst =
151 pipe_buffer_map(pc->pipe, new_ib.buffer, PIPE_TRANSFER_WRITE,
152 &dst_transfer);
153
154 if (info->indexed) {
155 new_info.min_index = 0;
156 new_info.max_index = ~0;
157 trans_func(src, info->start, new_info.count, dst);
158 }
159 else {
160 new_info.min_index = info->start;
161 new_info.max_index = info->start + new_info.count;
162 gen_func(info->start, new_info.count, dst);
163 }
164
165 if (src_transfer)
166 pipe_buffer_unmap(pc->pipe, src_transfer);
167
168 if (dst_transfer)
169 pipe_buffer_unmap(pc->pipe, dst_transfer);
170
171 /* bind new index buffer: */
172 pc->pipe->set_index_buffer(pc->pipe, &new_ib);
173
174 /* to the translated draw: */
175 pc->pipe->draw_vbo(pc->pipe, &new_info);
176
177 /* and then restore saved ib: */
178 pc->pipe->set_index_buffer(pc->pipe, ib);
179
180 pipe_resource_reference(&new_ib.buffer, NULL);
181 }