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"
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 struct pipe_index_buffer
*src_buffer
,
43 struct pipe_resource
**dst_buffer
,
45 unsigned restart_index
)
47 struct pipe_screen
*screen
= context
->screen
;
48 struct pipe_transfer
*src_transfer
= NULL
, *dst_transfer
= NULL
;
49 void *src_map
= NULL
, *dst_map
= NULL
;
50 const unsigned src_index_size
= src_buffer
->index_size
;
51 unsigned dst_index_size
;
53 /* 1-byte indexes are converted to 2-byte indexes, 4-byte stays 4-byte */
54 dst_index_size
= MAX2(2, src_buffer
->index_size
);
55 assert(dst_index_size
== 2 || dst_index_size
== 4);
57 /* no user buffers for now */
58 assert(src_buffer
->user_buffer
== NULL
);
60 /* Create new index buffer */
61 *dst_buffer
= pipe_buffer_create(screen
, PIPE_BIND_INDEX_BUFFER
,
63 num_indexes
* dst_index_size
);
67 /* Map new / dest index buffer */
68 dst_map
= pipe_buffer_map(context
, *dst_buffer
,
69 PIPE_TRANSFER_WRITE
, &dst_transfer
);
73 /* Map original / src index buffer */
74 src_map
= pipe_buffer_map_range(context
, src_buffer
->buffer
,
76 num_indexes
* src_index_size
,
82 if (src_index_size
== 1 && dst_index_size
== 2) {
83 uint8_t *src
= (uint8_t *) src_map
;
84 uint16_t *dst
= (uint16_t *) dst_map
;
86 for (i
= 0; i
< num_indexes
; i
++) {
87 dst
[i
] = (src
[i
] == restart_index
) ? 0xffff : src
[i
];
90 else if (src_index_size
== 2 && dst_index_size
== 2) {
91 uint16_t *src
= (uint16_t *) src_map
;
92 uint16_t *dst
= (uint16_t *) dst_map
;
94 for (i
= 0; i
< num_indexes
; i
++) {
95 dst
[i
] = (src
[i
] == restart_index
) ? 0xffff : src
[i
];
99 uint32_t *src
= (uint32_t *) src_map
;
100 uint32_t *dst
= (uint32_t *) dst_map
;
102 assert(src_index_size
== 4);
103 assert(dst_index_size
== 4);
104 for (i
= 0; i
< num_indexes
; i
++) {
105 dst
[i
] = (src
[i
] == restart_index
) ? 0xffffffff : src
[i
];
109 pipe_buffer_unmap(context
, src_transfer
);
110 pipe_buffer_unmap(context
, dst_transfer
);
116 pipe_buffer_unmap(context
, src_transfer
);
118 pipe_buffer_unmap(context
, dst_transfer
);
120 screen
->resource_destroy(screen
, *dst_buffer
);
121 return PIPE_ERROR_OUT_OF_MEMORY
;
125 /** Helper structs for util_draw_vbo_without_prim_restart() */
128 unsigned start
, count
;
132 struct range
*ranges
;
138 * Helper function for util_draw_vbo_without_prim_restart()
139 * \return true for success, false if out of memory
142 add_range(struct range_info
*info
, unsigned start
, unsigned count
)
144 if (info
->max
== 0) {
146 info
->ranges
= MALLOC(info
->max
* sizeof(struct range
));
151 else if (info
->count
== info
->max
) {
152 /* grow the ranges[] array */
153 info
->ranges
= REALLOC(info
->ranges
,
154 info
->max
* sizeof(struct range
),
155 2 * info
->max
* sizeof(struct range
));
164 info
->ranges
[info
->count
].start
= start
;
165 info
->ranges
[info
->count
].count
= count
;
173 * Implement primitive restart by breaking an indexed primitive into
174 * pieces which do not contain restart indexes. Each piece is then
175 * drawn by calling pipe_context::draw_vbo().
176 * \return PIPE_OK if no error, an error code otherwise.
179 util_draw_vbo_without_prim_restart(struct pipe_context
*context
,
180 const struct pipe_index_buffer
*ib
,
181 const struct pipe_draw_info
*info
)
184 struct range_info ranges
= {0};
185 struct pipe_draw_info new_info
;
186 struct pipe_transfer
*src_transfer
= NULL
;
187 unsigned i
, start
, count
;
189 assert(info
->indexed
);
190 assert(info
->primitive_restart
);
192 /* Get pointer to the index data */
194 /* map the index buffer (only the range we need to scan) */
195 src_map
= pipe_buffer_map_range(context
, ib
->buffer
,
196 ib
->offset
+ info
->start
* ib
->index_size
,
197 info
->count
* ib
->index_size
,
201 return PIPE_ERROR_OUT_OF_MEMORY
;
205 if (!ib
->user_buffer
) {
206 debug_printf("User-space index buffer is null!");
207 return PIPE_ERROR_BAD_INPUT
;
209 src_map
= (const uint8_t *) ib
->user_buffer
211 + info
->start
* ib
->index_size
;
214 #define SCAN_INDEXES(TYPE) \
215 for (i = 0; i <= info->count; i++) { \
216 if (i == info->count || \
217 ((const TYPE *) src_map)[i] == info->restart_index) { \
218 /* cut / restart */ \
220 if (!add_range(&ranges, info->start + start, count)) { \
222 pipe_buffer_unmap(context, src_transfer); \
223 return PIPE_ERROR_OUT_OF_MEMORY; \
236 switch (ib
->index_size
) {
238 SCAN_INDEXES(uint8_t);
241 SCAN_INDEXES(uint16_t);
244 SCAN_INDEXES(uint32_t);
247 assert(!"Bad index size");
248 return PIPE_ERROR_BAD_INPUT
;
251 /* unmap index buffer */
253 pipe_buffer_unmap(context
, src_transfer
);
255 /* draw ranges between the restart indexes */
257 new_info
.primitive_restart
= FALSE
;
258 for (i
= 0; i
< ranges
.count
; i
++) {
259 new_info
.start
= ranges
.ranges
[i
].start
;
260 new_info
.count
= ranges
.ranges
[i
].count
;
261 context
->draw_vbo(context
, &new_info
);