Merge branch '7.8'
[mesa.git] / src / gallium / drivers / svga / svga_draw_arrays.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "util/u_inlines.h"
29 #include "indices/u_indices.h"
30
31 #include "svga_hw_reg.h"
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_context.h"
35
36
37 #define DBG 0
38
39
40
41
42 static enum pipe_error generate_indices( struct svga_hwtnl *hwtnl,
43 unsigned nr,
44 unsigned index_size,
45 u_generate_func generate,
46 struct pipe_resource **out_buf )
47 {
48 struct pipe_context *pipe = &hwtnl->svga->pipe;
49 struct pipe_transfer *transfer;
50 unsigned size = index_size * nr;
51 struct pipe_resource *dst = NULL;
52 void *dst_map = NULL;
53
54 dst = pipe_buffer_create( pipe->screen,
55 PIPE_BIND_INDEX_BUFFER,
56 size );
57 if (dst == NULL)
58 goto fail;
59
60 dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE,
61 &transfer);
62 if (dst_map == NULL)
63 goto fail;
64
65 generate( nr,
66 dst_map );
67
68 pipe_buffer_unmap( pipe, dst, transfer );
69
70 *out_buf = dst;
71 return PIPE_OK;
72
73 fail:
74 if (dst_map)
75 pipe_buffer_unmap( pipe, dst, transfer );
76
77 if (dst)
78 pipe->screen->resource_destroy( pipe->screen, dst );
79
80 return PIPE_ERROR_OUT_OF_MEMORY;
81 }
82
83 static boolean compare( unsigned cached_nr,
84 unsigned nr,
85 unsigned type )
86 {
87 if (type == U_GENERATE_REUSABLE)
88 return cached_nr >= nr;
89 else
90 return cached_nr == nr;
91 }
92
93 static enum pipe_error retrieve_or_generate_indices( struct svga_hwtnl *hwtnl,
94 unsigned prim,
95 unsigned gen_type,
96 unsigned gen_nr,
97 unsigned gen_size,
98 u_generate_func generate,
99 struct pipe_resource **out_buf )
100 {
101 enum pipe_error ret = PIPE_OK;
102 int i;
103
104 for (i = 0; i < IDX_CACHE_MAX; i++) {
105 if (hwtnl->index_cache[prim][i].buffer != NULL &&
106 hwtnl->index_cache[prim][i].generate == generate)
107 {
108 if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type))
109 {
110 pipe_resource_reference( out_buf,
111 hwtnl->index_cache[prim][i].buffer );
112
113 if (DBG)
114 debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);
115
116 return PIPE_OK;
117 }
118 else if (gen_type == U_GENERATE_REUSABLE)
119 {
120 pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
121 NULL );
122
123 if (DBG)
124 debug_printf("%s discard %d/%d\n", __FUNCTION__,
125 i, hwtnl->index_cache[prim][i].gen_nr);
126
127 break;
128 }
129 }
130 }
131
132 if (i == IDX_CACHE_MAX)
133 {
134 unsigned smallest = 0;
135 unsigned smallest_size = ~0;
136
137 for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {
138 if (hwtnl->index_cache[prim][i].buffer == NULL)
139 {
140 smallest = i;
141 smallest_size = 0;
142 }
143 else if (hwtnl->index_cache[prim][i].gen_nr < smallest)
144 {
145 smallest = i;
146 smallest_size = hwtnl->index_cache[prim][i].gen_nr;
147 }
148 }
149
150 assert (smallest != IDX_CACHE_MAX);
151
152 pipe_resource_reference( &hwtnl->index_cache[prim][smallest].buffer,
153 NULL );
154
155 if (DBG)
156 debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,
157 smallest, smallest_size);
158
159 i = smallest;
160 }
161
162
163 ret = generate_indices( hwtnl,
164 gen_nr,
165 gen_size,
166 generate,
167 out_buf );
168 if (ret != PIPE_OK)
169 return ret;
170
171
172 hwtnl->index_cache[prim][i].generate = generate;
173 hwtnl->index_cache[prim][i].gen_nr = gen_nr;
174 pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
175 *out_buf );
176
177 if (DBG)
178 debug_printf("%s cache %d/%d\n", __FUNCTION__,
179 i, hwtnl->index_cache[prim][i].gen_nr);
180
181 return PIPE_OK;
182 }
183
184
185
186 static enum pipe_error
187 simple_draw_arrays( struct svga_hwtnl *hwtnl,
188 unsigned prim, unsigned start, unsigned count )
189 {
190 SVGA3dPrimitiveRange range;
191 unsigned hw_prim;
192 unsigned hw_count;
193
194 hw_prim = svga_translate_prim(prim, count, &hw_count);
195 if (hw_count == 0)
196 return PIPE_ERROR_BAD_INPUT;
197
198 range.primType = hw_prim;
199 range.primitiveCount = hw_count;
200 range.indexArray.surfaceId = SVGA3D_INVALID_ID;
201 range.indexArray.offset = 0;
202 range.indexArray.stride = 0;
203 range.indexWidth = 0;
204 range.indexBias = start;
205
206 /* Min/max index should be calculated prior to applying bias, so we
207 * end up with min_index = 0, max_index = count - 1 and everybody
208 * looking at those numbers knows to adjust them by
209 * range.indexBias.
210 */
211 return svga_hwtnl_prim( hwtnl, &range, 0, count - 1, NULL );
212 }
213
214
215
216
217
218
219
220
221
222
223 enum pipe_error
224 svga_hwtnl_draw_arrays( struct svga_hwtnl *hwtnl,
225 unsigned prim,
226 unsigned start,
227 unsigned count)
228 {
229 unsigned gen_prim, gen_size, gen_nr, gen_type;
230 u_generate_func gen_func;
231 enum pipe_error ret = PIPE_OK;
232
233 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
234 prim >= PIPE_PRIM_TRIANGLES)
235 {
236 gen_type = u_unfilled_generator( prim,
237 start,
238 count,
239 hwtnl->api_fillmode,
240 &gen_prim,
241 &gen_size,
242 &gen_nr,
243 &gen_func );
244 }
245 else {
246 gen_type = u_index_generator( svga_hw_prims,
247 prim,
248 start,
249 count,
250 hwtnl->api_pv,
251 hwtnl->hw_pv,
252 &gen_prim,
253 &gen_size,
254 &gen_nr,
255 &gen_func );
256 }
257
258 if (gen_type == U_GENERATE_LINEAR) {
259 return simple_draw_arrays( hwtnl, gen_prim, start, count );
260 }
261 else {
262 struct pipe_resource *gen_buf = NULL;
263
264 /* Need to draw as indexed primitive.
265 * Potentially need to run the gen func to build an index buffer.
266 */
267 ret = retrieve_or_generate_indices( hwtnl,
268 prim,
269 gen_type,
270 gen_nr,
271 gen_size,
272 gen_func,
273 &gen_buf );
274 if (ret)
275 goto done;
276
277 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
278 gen_buf,
279 gen_size,
280 0,
281 count - 1,
282 gen_prim,
283 0,
284 gen_nr,
285 start );
286 if (ret)
287 goto done;
288
289 done:
290 if (gen_buf)
291 pipe_resource_reference( &gen_buf, NULL );
292
293 return ret;
294 }
295 }
296