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_resource **out_buf )
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;
54 dst = pipe_buffer_create( pipe->screen,
55 PIPE_BIND_INDEX_BUFFER,
61 dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE,
69 pipe_buffer_unmap( pipe, transfer );
76 pipe_buffer_unmap( pipe, transfer );
79 pipe->screen->resource_destroy( pipe->screen, dst );
81 return PIPE_ERROR_OUT_OF_MEMORY;
84 static boolean compare( unsigned cached_nr,
88 if (type == U_GENERATE_REUSABLE)
89 return cached_nr >= nr;
91 return cached_nr == nr;
94 static enum pipe_error retrieve_or_generate_indices( struct svga_hwtnl *hwtnl,
99 u_generate_func generate,
100 struct pipe_resource **out_buf )
102 enum pipe_error ret = PIPE_OK;
105 for (i = 0; i < IDX_CACHE_MAX; i++) {
106 if (hwtnl->index_cache[prim][i].buffer != NULL &&
107 hwtnl->index_cache[prim][i].generate == generate)
109 if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type))
111 pipe_resource_reference( out_buf,
112 hwtnl->index_cache[prim][i].buffer );
115 debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);
119 else if (gen_type == U_GENERATE_REUSABLE)
121 pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
125 debug_printf("%s discard %d/%d\n", __FUNCTION__,
126 i, hwtnl->index_cache[prim][i].gen_nr);
133 if (i == IDX_CACHE_MAX)
135 unsigned smallest = 0;
136 unsigned smallest_size = ~0;
138 for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {
139 if (hwtnl->index_cache[prim][i].buffer == NULL)
144 else if (hwtnl->index_cache[prim][i].gen_nr < smallest)
147 smallest_size = hwtnl->index_cache[prim][i].gen_nr;
151 assert (smallest != IDX_CACHE_MAX);
153 pipe_resource_reference( &hwtnl->index_cache[prim][smallest].buffer,
157 debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,
158 smallest, smallest_size);
164 ret = generate_indices( hwtnl,
173 hwtnl->index_cache[prim][i].generate = generate;
174 hwtnl->index_cache[prim][i].gen_nr = gen_nr;
175 pipe_resource_reference( &hwtnl->index_cache[prim][i].buffer,
179 debug_printf("%s cache %d/%d\n", __FUNCTION__,
180 i, hwtnl->index_cache[prim][i].gen_nr);
187 static enum pipe_error
188 simple_draw_arrays( struct svga_hwtnl *hwtnl,
189 unsigned prim, unsigned start, unsigned count )
191 SVGA3dPrimitiveRange range;
195 hw_prim = svga_translate_prim(prim, count, &hw_count);
197 return PIPE_ERROR_BAD_INPUT;
199 range.primType = hw_prim;
200 range.primitiveCount = hw_count;
201 range.indexArray.surfaceId = SVGA3D_INVALID_ID;
202 range.indexArray.offset = 0;
203 range.indexArray.stride = 0;
204 range.indexWidth = 0;
205 range.indexBias = start;
207 /* Min/max index should be calculated prior to applying bias, so we
208 * end up with min_index = 0, max_index = count - 1 and everybody
209 * looking at those numbers knows to adjust them by
212 return svga_hwtnl_prim( hwtnl, &range, 0, count - 1, NULL );
225 svga_hwtnl_draw_arrays( struct svga_hwtnl *hwtnl,
230 unsigned gen_prim, gen_size, gen_nr, gen_type;
231 u_generate_func gen_func;
232 enum pipe_error ret = PIPE_OK;
234 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
235 prim >= PIPE_PRIM_TRIANGLES)
237 gen_type = u_unfilled_generator( prim,
247 gen_type = u_index_generator( svga_hw_prims,
259 if (gen_type == U_GENERATE_LINEAR) {
260 return simple_draw_arrays( hwtnl, gen_prim, start, count );
263 struct pipe_resource *gen_buf = NULL;
265 /* Need to draw as indexed primitive.
266 * Potentially need to run the gen func to build an index buffer.
268 ret = retrieve_or_generate_indices( hwtnl,
278 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
293 pipe_resource_reference( &gen_buf, NULL );