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"
42 static enum pipe_error generate_indices( struct svga_hwtnl *hwtnl,
45 u_generate_func generate,
46 struct pipe_buffer **out_buf )
48 struct pipe_screen *screen = hwtnl->svga->pipe.screen;
49 unsigned size = index_size * nr;
50 struct pipe_buffer *dst = NULL;
53 dst = screen->buffer_create( screen, 32,
54 PIPE_BUFFER_USAGE_INDEX |
55 PIPE_BUFFER_USAGE_CPU_WRITE |
56 PIPE_BUFFER_USAGE_GPU_READ,
61 dst_map = pipe_buffer_map( screen, dst, PIPE_BUFFER_USAGE_CPU_WRITE );
68 pipe_buffer_unmap( screen, dst );
75 screen->buffer_unmap( screen, dst );
78 screen->buffer_destroy( dst );
80 return PIPE_ERROR_OUT_OF_MEMORY;
83 static boolean compare( unsigned cached_nr,
87 if (type == U_GENERATE_REUSABLE)
88 return cached_nr >= nr;
90 return cached_nr == nr;
93 static enum pipe_error retrieve_or_generate_indices( struct svga_hwtnl *hwtnl,
98 u_generate_func generate,
99 struct pipe_buffer **out_buf )
101 enum pipe_error ret = PIPE_OK;
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)
108 if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type))
110 pipe_buffer_reference( out_buf,
111 hwtnl->index_cache[prim][i].buffer );
114 debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);
118 else if (gen_type == U_GENERATE_REUSABLE)
120 pipe_buffer_reference( &hwtnl->index_cache[prim][i].buffer,
124 debug_printf("%s discard %d/%d\n", __FUNCTION__,
125 i, hwtnl->index_cache[prim][i].gen_nr);
132 if (i == IDX_CACHE_MAX)
134 unsigned smallest = 0;
135 unsigned smallest_size = ~0;
137 for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {
138 if (hwtnl->index_cache[prim][i].buffer == NULL)
143 else if (hwtnl->index_cache[prim][i].gen_nr < smallest)
146 smallest_size = hwtnl->index_cache[prim][i].gen_nr;
150 assert (smallest != IDX_CACHE_MAX);
152 pipe_buffer_reference( &hwtnl->index_cache[prim][smallest].buffer,
156 debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,
157 smallest, smallest_size);
163 ret = generate_indices( hwtnl,
172 hwtnl->index_cache[prim][i].generate = generate;
173 hwtnl->index_cache[prim][i].gen_nr = gen_nr;
174 pipe_buffer_reference( &hwtnl->index_cache[prim][i].buffer,
178 debug_printf("%s cache %d/%d\n", __FUNCTION__,
179 i, hwtnl->index_cache[prim][i].gen_nr);
186 static enum pipe_error
187 simple_draw_arrays( struct svga_hwtnl *hwtnl,
188 unsigned prim, unsigned start, unsigned count )
190 SVGA3dPrimitiveRange range;
194 hw_prim = svga_translate_prim(prim, count, &hw_count);
196 return PIPE_ERROR_BAD_INPUT;
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;
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
211 return svga_hwtnl_prim( hwtnl, &range, 0, count - 1, NULL );
224 svga_hwtnl_draw_arrays( struct svga_hwtnl *hwtnl,
229 unsigned gen_prim, gen_size, gen_nr, gen_type;
230 u_generate_func gen_func;
231 enum pipe_error ret = PIPE_OK;
233 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
234 prim >= PIPE_PRIM_TRIANGLES)
236 gen_type = u_unfilled_generator( prim,
246 gen_type = u_index_generator( svga_hw_prims,
258 if (gen_type == U_GENERATE_LINEAR) {
259 return simple_draw_arrays( hwtnl, gen_prim, start, count );
262 struct pipe_buffer *gen_buf = NULL;
264 /* Need to draw as indexed primitive.
265 * Potentially need to run the gen func to build an index buffer.
267 ret = retrieve_or_generate_indices( hwtnl,
277 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
291 pipe_buffer_reference( &gen_buf, NULL );