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