10e39e240dbf2b2f1e86954cda84ddff5017b083
2 * Copyright 2014 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "u_inlines.h"
29 #include "util/u_memory.h"
30 #include "u_prim_restart.h"
34 * Translate an index buffer for primitive restart.
35 * Create a new index buffer which is a copy of the original index buffer
36 * except that instances of 'restart_index' are converted to 0xffff or
38 * Also, index buffers using 1-byte indexes are converted to 2-byte indexes.
41 util_translate_prim_restart_ib(struct pipe_context
*context
,
42 const struct pipe_draw_info
*info
,
43 struct pipe_resource
**dst_buffer
)
45 struct pipe_screen
*screen
= context
->screen
;
46 struct pipe_transfer
*src_transfer
= NULL
, *dst_transfer
= NULL
;
47 void *src_map
= NULL
, *dst_map
= NULL
;
48 const unsigned src_index_size
= info
->index_size
;
49 unsigned dst_index_size
;
51 /* 1-byte indexes are converted to 2-byte indexes, 4-byte stays 4-byte */
52 dst_index_size
= MAX2(2, info
->index_size
);
53 assert(dst_index_size
== 2 || dst_index_size
== 4);
55 /* no user buffers for now */
56 assert(!info
->has_user_indices
);
58 /* Create new index buffer */
59 *dst_buffer
= pipe_buffer_create(screen
, PIPE_BIND_INDEX_BUFFER
,
61 info
->count
* dst_index_size
);
65 /* Map new / dest index buffer */
66 dst_map
= pipe_buffer_map(context
, *dst_buffer
,
67 PIPE_TRANSFER_WRITE
, &dst_transfer
);
71 /* Map original / src index buffer */
72 src_map
= pipe_buffer_map_range(context
, info
->index
.resource
,
73 info
->start
* src_index_size
,
74 info
->count
* src_index_size
,
80 if (src_index_size
== 1 && dst_index_size
== 2) {
81 uint8_t *src
= (uint8_t *) src_map
;
82 uint16_t *dst
= (uint16_t *) dst_map
;
84 for (i
= 0; i
< info
->count
; i
++) {
85 dst
[i
] = (src
[i
] == info
->restart_index
) ? 0xffff : src
[i
];
88 else if (src_index_size
== 2 && dst_index_size
== 2) {
89 uint16_t *src
= (uint16_t *) src_map
;
90 uint16_t *dst
= (uint16_t *) dst_map
;
92 for (i
= 0; i
< info
->count
; i
++) {
93 dst
[i
] = (src
[i
] == info
->restart_index
) ? 0xffff : src
[i
];
97 uint32_t *src
= (uint32_t *) src_map
;
98 uint32_t *dst
= (uint32_t *) dst_map
;
100 assert(src_index_size
== 4);
101 assert(dst_index_size
== 4);
102 for (i
= 0; i
< info
->count
; i
++) {
103 dst
[i
] = (src
[i
] == info
->restart_index
) ? 0xffffffff : src
[i
];
107 pipe_buffer_unmap(context
, src_transfer
);
108 pipe_buffer_unmap(context
, dst_transfer
);
114 pipe_buffer_unmap(context
, src_transfer
);
116 pipe_buffer_unmap(context
, dst_transfer
);
118 pipe_resource_reference(dst_buffer
, NULL
);
119 return PIPE_ERROR_OUT_OF_MEMORY
;
123 /** Helper structs for util_draw_vbo_without_prim_restart() */
126 unsigned start
, count
;
130 struct range
*ranges
;
136 * Helper function for util_draw_vbo_without_prim_restart()
137 * \return true for success, false if out of memory
140 add_range(struct range_info
*info
, unsigned start
, unsigned count
)
142 if (info
->max
== 0) {
144 info
->ranges
= MALLOC(info
->max
* sizeof(struct range
));
149 else if (info
->count
== info
->max
) {
150 /* grow the ranges[] array */
151 info
->ranges
= REALLOC(info
->ranges
,
152 info
->max
* sizeof(struct range
),
153 2 * info
->max
* sizeof(struct range
));
162 info
->ranges
[info
->count
].start
= start
;
163 info
->ranges
[info
->count
].count
= count
;
171 * Implement primitive restart by breaking an indexed primitive into
172 * pieces which do not contain restart indexes. Each piece is then
173 * drawn by calling pipe_context::draw_vbo().
174 * \return PIPE_OK if no error, an error code otherwise.
177 util_draw_vbo_without_prim_restart(struct pipe_context
*context
,
178 const struct pipe_draw_info
*info
)
181 struct range_info ranges
= {0};
182 struct pipe_draw_info new_info
;
183 struct pipe_transfer
*src_transfer
= NULL
;
184 unsigned i
, start
, count
;
186 assert(info
->index_size
);
187 assert(info
->primitive_restart
);
189 /* Get pointer to the index data */
190 if (!info
->has_user_indices
) {
191 /* map the index buffer (only the range we need to scan) */
192 src_map
= pipe_buffer_map_range(context
, info
->index
.resource
,
193 info
->start
* info
->index_size
,
194 info
->count
* info
->index_size
,
198 return PIPE_ERROR_OUT_OF_MEMORY
;
202 if (!info
->index
.user
) {
203 debug_printf("User-space index buffer is null!");
204 return PIPE_ERROR_BAD_INPUT
;
206 src_map
= (const uint8_t *) info
->index
.user
207 + info
->start
* info
->index_size
;
210 #define SCAN_INDEXES(TYPE) \
211 for (i = 0; i <= info->count; i++) { \
212 if (i == info->count || \
213 ((const TYPE *) src_map)[i] == info->restart_index) { \
214 /* cut / restart */ \
216 if (!add_range(&ranges, info->start + start, count)) { \
218 pipe_buffer_unmap(context, src_transfer); \
219 return PIPE_ERROR_OUT_OF_MEMORY; \
232 switch (info
->index_size
) {
234 SCAN_INDEXES(uint8_t);
237 SCAN_INDEXES(uint16_t);
240 SCAN_INDEXES(uint32_t);
243 assert(!"Bad index size");
244 return PIPE_ERROR_BAD_INPUT
;
247 /* unmap index buffer */
249 pipe_buffer_unmap(context
, src_transfer
);
251 /* draw ranges between the restart indexes */
253 new_info
.primitive_restart
= FALSE
;
254 for (i
= 0; i
< ranges
.count
; i
++) {
255 new_info
.start
= ranges
.ranges
[i
].start
;
256 new_info
.count
= ranges
.ranges
[i
].count
;
257 context
->draw_vbo(context
, &new_info
);