1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
28 #include "util/u_inlines.h"
29 #include "indices/u_indices.h"
31 #include "svga_hw_reg.h"
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_context.h"
40 static enum pipe_error
41 generate_indices(struct svga_hwtnl
*hwtnl
,
44 u_generate_func generate
, struct pipe_resource
**out_buf
)
46 struct pipe_context
*pipe
= &hwtnl
->svga
->pipe
;
47 struct pipe_transfer
*transfer
;
48 unsigned size
= index_size
* nr
;
49 struct pipe_resource
*dst
= NULL
;
52 dst
= pipe_buffer_create(pipe
->screen
,
53 PIPE_BIND_INDEX_BUFFER
, PIPE_USAGE_DEFAULT
, size
);
57 dst_map
= pipe_buffer_map(pipe
, dst
, PIPE_TRANSFER_WRITE
, &transfer
);
61 generate(0, nr
, dst_map
);
63 pipe_buffer_unmap(pipe
, transfer
);
70 pipe_buffer_unmap(pipe
, transfer
);
73 pipe
->screen
->resource_destroy(pipe
->screen
, dst
);
75 return PIPE_ERROR_OUT_OF_MEMORY
;
80 compare(unsigned cached_nr
, unsigned nr
, unsigned type
)
82 if (type
== U_GENERATE_REUSABLE
)
83 return cached_nr
>= nr
;
85 return cached_nr
== nr
;
89 static enum pipe_error
90 retrieve_or_generate_indices(struct svga_hwtnl
*hwtnl
,
95 u_generate_func generate
,
96 struct pipe_resource
**out_buf
)
98 enum pipe_error ret
= PIPE_OK
;
101 for (i
= 0; i
< IDX_CACHE_MAX
; i
++) {
102 if (hwtnl
->index_cache
[prim
][i
].buffer
!= NULL
&&
103 hwtnl
->index_cache
[prim
][i
].generate
== generate
) {
104 if (compare(hwtnl
->index_cache
[prim
][i
].gen_nr
, gen_nr
, gen_type
)) {
105 pipe_resource_reference(out_buf
,
106 hwtnl
->index_cache
[prim
][i
].buffer
);
109 debug_printf("%s retrieve %d/%d\n", __FUNCTION__
, i
, gen_nr
);
113 else if (gen_type
== U_GENERATE_REUSABLE
) {
114 pipe_resource_reference(&hwtnl
->index_cache
[prim
][i
].buffer
,
118 debug_printf("%s discard %d/%d\n", __FUNCTION__
,
119 i
, hwtnl
->index_cache
[prim
][i
].gen_nr
);
126 if (i
== IDX_CACHE_MAX
) {
127 unsigned smallest
= 0;
128 unsigned smallest_size
= ~0;
130 for (i
= 0; i
< IDX_CACHE_MAX
&& smallest_size
; i
++) {
131 if (hwtnl
->index_cache
[prim
][i
].buffer
== NULL
) {
135 else if (hwtnl
->index_cache
[prim
][i
].gen_nr
< smallest
) {
137 smallest_size
= hwtnl
->index_cache
[prim
][i
].gen_nr
;
141 assert(smallest
!= IDX_CACHE_MAX
);
143 pipe_resource_reference(&hwtnl
->index_cache
[prim
][smallest
].buffer
,
147 debug_printf("%s discard smallest %d/%d\n", __FUNCTION__
,
148 smallest
, smallest_size
);
153 ret
= generate_indices(hwtnl
, gen_nr
, gen_size
, generate
, out_buf
);
157 hwtnl
->index_cache
[prim
][i
].generate
= generate
;
158 hwtnl
->index_cache
[prim
][i
].gen_nr
= gen_nr
;
159 pipe_resource_reference(&hwtnl
->index_cache
[prim
][i
].buffer
, *out_buf
);
162 debug_printf("%s cache %d/%d\n", __FUNCTION__
,
163 i
, hwtnl
->index_cache
[prim
][i
].gen_nr
);
169 static enum pipe_error
170 simple_draw_arrays(struct svga_hwtnl
*hwtnl
,
171 unsigned prim
, unsigned start
, unsigned count
)
173 SVGA3dPrimitiveRange range
;
177 hw_prim
= svga_translate_prim(prim
, count
, &hw_count
);
179 return PIPE_ERROR_BAD_INPUT
;
181 range
.primType
= hw_prim
;
182 range
.primitiveCount
= hw_count
;
183 range
.indexArray
.surfaceId
= SVGA3D_INVALID_ID
;
184 range
.indexArray
.offset
= 0;
185 range
.indexArray
.stride
= 0;
186 range
.indexWidth
= 0;
187 range
.indexBias
= start
;
189 /* Min/max index should be calculated prior to applying bias, so we
190 * end up with min_index = 0, max_index = count - 1 and everybody
191 * looking at those numbers knows to adjust them by
194 return svga_hwtnl_prim(hwtnl
, &range
, 0, count
- 1, NULL
);
199 svga_hwtnl_draw_arrays(struct svga_hwtnl
*hwtnl
,
200 unsigned prim
, unsigned start
, unsigned count
)
202 unsigned gen_prim
, gen_size
, gen_nr
, gen_type
;
203 u_generate_func gen_func
;
204 enum pipe_error ret
= PIPE_OK
;
206 if (hwtnl
->api_fillmode
!= PIPE_POLYGON_MODE_FILL
&&
207 prim
>= PIPE_PRIM_TRIANGLES
) {
208 /* Convert unfilled polygons into points, lines, triangles */
209 gen_type
= u_unfilled_generator(prim
,
214 &gen_size
, &gen_nr
, &gen_func
);
217 /* Convert PIPE_PRIM_LINE_LOOP to PIPE_PRIM_LINESTRIP,
218 * convert PIPE_PRIM_POLYGON to PIPE_PRIM_TRIANGLE_FAN,
219 * etc, if needed (as determined by svga_hw_prims mask).
221 gen_type
= u_index_generator(svga_hw_prims
,
227 &gen_prim
, &gen_size
, &gen_nr
, &gen_func
);
230 if (gen_type
== U_GENERATE_LINEAR
) {
231 return simple_draw_arrays(hwtnl
, gen_prim
, start
, count
);
234 struct pipe_resource
*gen_buf
= NULL
;
236 /* Need to draw as indexed primitive.
237 * Potentially need to run the gen func to build an index buffer.
239 ret
= retrieve_or_generate_indices(hwtnl
,
243 gen_size
, gen_func
, &gen_buf
);
247 ret
= svga_hwtnl_simple_draw_range_elements(hwtnl
,
253 gen_prim
, 0, gen_nr
);
260 pipe_resource_reference(&gen_buf
, NULL
);