c0567728e920b696fa4af3bf0e60b50d6586a577
[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 "util/u_prim.h"
30 #include "indices/u_indices.h"
31
32 #include "svga_hw_reg.h"
33 #include "svga_draw.h"
34 #include "svga_draw_private.h"
35 #include "svga_context.h"
36 #include "svga_shader.h"
37
38
39 #define DBG 0
40
41
42 static enum pipe_error
43 generate_indices(struct svga_hwtnl *hwtnl,
44 unsigned nr,
45 unsigned index_size,
46 u_generate_func generate, 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, PIPE_BIND_INDEX_BUFFER,
55 PIPE_USAGE_IMMUTABLE, size);
56 if (!dst)
57 goto fail;
58
59 dst_map = pipe_buffer_map(pipe, dst, PIPE_TRANSFER_WRITE, &transfer);
60 if (!dst_map)
61 goto fail;
62
63 generate(0, nr, dst_map);
64
65 pipe_buffer_unmap(pipe, transfer);
66
67 *out_buf = dst;
68 return PIPE_OK;
69
70 fail:
71 if (dst_map)
72 pipe_buffer_unmap(pipe, transfer);
73
74 if (dst)
75 pipe->screen->resource_destroy(pipe->screen, dst);
76
77 return PIPE_ERROR_OUT_OF_MEMORY;
78 }
79
80
81 static boolean
82 compare(unsigned cached_nr, unsigned nr, unsigned type)
83 {
84 if (type == U_GENERATE_REUSABLE)
85 return cached_nr >= nr;
86 else
87 return cached_nr == nr;
88 }
89
90
91 static enum pipe_error
92 retrieve_or_generate_indices(struct svga_hwtnl *hwtnl,
93 unsigned prim,
94 unsigned gen_type,
95 unsigned gen_nr,
96 unsigned gen_size,
97 u_generate_func generate,
98 struct pipe_resource **out_buf)
99 {
100 enum pipe_error ret = PIPE_OK;
101 int i;
102
103 for (i = 0; i < IDX_CACHE_MAX; i++) {
104 if (hwtnl->index_cache[prim][i].buffer != NULL &&
105 hwtnl->index_cache[prim][i].generate == generate) {
106 if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type)) {
107 pipe_resource_reference(out_buf,
108 hwtnl->index_cache[prim][i].buffer);
109
110 if (DBG)
111 debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);
112
113 return PIPE_OK;
114 }
115 else if (gen_type == U_GENERATE_REUSABLE) {
116 pipe_resource_reference(&hwtnl->index_cache[prim][i].buffer,
117 NULL);
118
119 if (DBG)
120 debug_printf("%s discard %d/%d\n", __FUNCTION__,
121 i, hwtnl->index_cache[prim][i].gen_nr);
122
123 break;
124 }
125 }
126 }
127
128 if (i == IDX_CACHE_MAX) {
129 unsigned smallest = 0;
130 unsigned smallest_size = ~0;
131
132 for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {
133 if (hwtnl->index_cache[prim][i].buffer == NULL) {
134 smallest = i;
135 smallest_size = 0;
136 }
137 else if (hwtnl->index_cache[prim][i].gen_nr < smallest) {
138 smallest = i;
139 smallest_size = hwtnl->index_cache[prim][i].gen_nr;
140 }
141 }
142
143 assert(smallest != IDX_CACHE_MAX);
144
145 pipe_resource_reference(&hwtnl->index_cache[prim][smallest].buffer,
146 NULL);
147
148 if (DBG)
149 debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,
150 smallest, smallest_size);
151
152 i = smallest;
153 }
154
155 ret = generate_indices(hwtnl, gen_nr, gen_size, generate, out_buf);
156 if (ret != PIPE_OK)
157 return ret;
158
159 hwtnl->index_cache[prim][i].generate = generate;
160 hwtnl->index_cache[prim][i].gen_nr = gen_nr;
161 pipe_resource_reference(&hwtnl->index_cache[prim][i].buffer, *out_buf);
162
163 if (DBG)
164 debug_printf("%s cache %d/%d\n", __FUNCTION__,
165 i, hwtnl->index_cache[prim][i].gen_nr);
166
167 return PIPE_OK;
168 }
169
170
171 static enum pipe_error
172 simple_draw_arrays(struct svga_hwtnl *hwtnl,
173 unsigned prim, unsigned start, unsigned count,
174 unsigned start_instance, unsigned instance_count)
175 {
176 SVGA3dPrimitiveRange range;
177 unsigned hw_prim;
178 unsigned hw_count;
179
180 hw_prim = svga_translate_prim(prim, count, &hw_count);
181 if (hw_count == 0)
182 return PIPE_ERROR_BAD_INPUT;
183
184 range.primType = hw_prim;
185 range.primitiveCount = hw_count;
186 range.indexArray.surfaceId = SVGA3D_INVALID_ID;
187 range.indexArray.offset = 0;
188 range.indexArray.stride = 0;
189 range.indexWidth = 0;
190 range.indexBias = start;
191
192 /* Min/max index should be calculated prior to applying bias, so we
193 * end up with min_index = 0, max_index = count - 1 and everybody
194 * looking at those numbers knows to adjust them by
195 * range.indexBias.
196 */
197 return svga_hwtnl_prim(hwtnl, &range, count,
198 0, count - 1, NULL,
199 start_instance, instance_count);
200 }
201
202
203 enum pipe_error
204 svga_hwtnl_draw_arrays(struct svga_hwtnl *hwtnl,
205 unsigned prim, unsigned start, unsigned count,
206 unsigned start_instance, unsigned instance_count)
207 {
208 unsigned gen_prim, gen_size, gen_nr;
209 enum indices_mode gen_type;
210 u_generate_func gen_func;
211 enum pipe_error ret = PIPE_OK;
212 unsigned api_pv = hwtnl->api_pv;
213 struct svga_context *svga = hwtnl->svga;
214
215 if (svga->curr.rast->templ.flatshade &&
216 svga->state.hw_draw.fs->constant_color_output) {
217 /* The fragment color is a constant, not per-vertex so the whole
218 * primitive will be the same color (except for possible blending).
219 * We can ignore the current provoking vertex state and use whatever
220 * the hardware wants.
221 */
222 api_pv = hwtnl->hw_pv;
223
224 if (hwtnl->api_fillmode == PIPE_POLYGON_MODE_FILL) {
225 /* Do some simple primitive conversions to avoid index buffer
226 * generation below. Note that polygons and quads are not directly
227 * supported by the svga device. Also note, we can only do this
228 * for flat/constant-colored rendering because of provoking vertex.
229 */
230 if (prim == PIPE_PRIM_POLYGON) {
231 prim = PIPE_PRIM_TRIANGLE_FAN;
232 }
233 else if (prim == PIPE_PRIM_QUADS && count == 4) {
234 prim = PIPE_PRIM_TRIANGLE_FAN;
235 }
236 }
237 }
238
239 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
240 u_reduced_prim(prim) == PIPE_PRIM_TRIANGLES) {
241 /* Convert unfilled polygons into points, lines, triangles */
242 gen_type = u_unfilled_generator(prim,
243 start,
244 count,
245 hwtnl->api_fillmode,
246 &gen_prim,
247 &gen_size, &gen_nr, &gen_func);
248 }
249 else {
250 /* Convert PIPE_PRIM_LINE_LOOP to PIPE_PRIM_LINESTRIP,
251 * convert PIPE_PRIM_POLYGON to PIPE_PRIM_TRIANGLE_FAN,
252 * etc, if needed (as determined by svga_hw_prims mask).
253 */
254 gen_type = u_index_generator(svga_hw_prims,
255 prim,
256 start,
257 count,
258 api_pv,
259 hwtnl->hw_pv,
260 &gen_prim, &gen_size, &gen_nr, &gen_func);
261 }
262
263 if (gen_type == U_GENERATE_LINEAR) {
264 return simple_draw_arrays(hwtnl, gen_prim, start, count,
265 start_instance, instance_count);
266 }
267 else {
268 struct pipe_resource *gen_buf = NULL;
269
270 /* Need to draw as indexed primitive.
271 * Potentially need to run the gen func to build an index buffer.
272 */
273 ret = retrieve_or_generate_indices(hwtnl,
274 prim,
275 gen_type,
276 gen_nr,
277 gen_size, gen_func, &gen_buf);
278 if (ret != PIPE_OK)
279 goto done;
280
281 pipe_debug_message(&svga->debug.callback, PERF_INFO,
282 "generating temporary index buffer for drawing %s",
283 u_prim_name(prim));
284
285 ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
286 gen_buf,
287 gen_size,
288 start,
289 0,
290 count - 1,
291 gen_prim, 0, gen_nr,
292 start_instance,
293 instance_count);
294 if (ret != PIPE_OK)
295 goto done;
296
297 done:
298 if (gen_buf)
299 pipe_resource_reference(&gen_buf, NULL);
300
301 return ret;
302 }
303 }