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 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "util/u_upload_mgr.h"
28 #include "indices/u_indices.h"
31 #include "svga_draw.h"
32 #include "svga_draw_private.h"
33 #include "svga_resource_buffer.h"
34 #include "svga_winsys.h"
35 #include "svga_context.h"
37 #include "svga_hw_reg.h"
40 static enum pipe_error
41 translate_indices( struct svga_hwtnl *hwtnl,
42 struct pipe_resource *src,
46 u_translate_func translate,
47 struct pipe_resource **out_buf )
49 struct pipe_context *pipe = &hwtnl->svga->pipe;
50 struct pipe_transfer *src_transfer = NULL;
51 struct pipe_transfer *dst_transfer = NULL;
52 unsigned size = index_size * nr;
53 const void *src_map = NULL;
54 struct pipe_resource *dst = NULL;
57 dst = pipe_buffer_create( pipe->screen,
58 PIPE_BIND_INDEX_BUFFER,
64 src_map = pipe_buffer_map( pipe, src, PIPE_TRANSFER_READ, &src_transfer );
68 dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer );
72 translate( (const char *)src_map + offset,
76 pipe_buffer_unmap( pipe, src_transfer );
77 pipe_buffer_unmap( pipe, dst_transfer );
84 pipe_buffer_unmap( pipe, src_transfer );
87 pipe_buffer_unmap( pipe, dst_transfer );
90 pipe->screen->resource_destroy( pipe->screen, dst );
92 return PIPE_ERROR_OUT_OF_MEMORY;
100 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
101 struct pipe_resource *index_buffer,
110 struct pipe_resource *upload_buffer = NULL;
111 SVGA3dPrimitiveRange range;
114 unsigned index_offset = start * index_size;
117 hw_prim = svga_translate_prim(prim, count, &hw_count);
122 svga_buffer_is_user_buffer(index_buffer))
125 assert( index_buffer->width0 >= index_offset + count * index_size );
127 ret = u_upload_buffer( hwtnl->upload_ib,
138 /* Don't need to worry about refcounting index_buffer as this is
139 * just a stack variable without a counted reference of its own.
140 * The caller holds the reference.
142 index_buffer = upload_buffer;
145 range.primType = hw_prim;
146 range.primitiveCount = hw_count;
147 range.indexArray.offset = index_offset;
148 range.indexArray.stride = index_size;
149 range.indexWidth = index_size;
150 range.indexBias = index_bias;
152 ret = svga_hwtnl_prim( hwtnl, &range, min_index, max_index, index_buffer );
158 pipe_resource_reference( &upload_buffer, NULL );
167 svga_hwtnl_draw_range_elements( struct svga_hwtnl *hwtnl,
168 struct pipe_resource *index_buffer,
173 unsigned prim, unsigned start, unsigned count)
175 unsigned gen_prim, gen_size, gen_nr, gen_type;
176 u_translate_func gen_func;
177 enum pipe_error ret = PIPE_OK;
179 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
180 prim >= PIPE_PRIM_TRIANGLES)
182 gen_type = u_unfilled_translator( prim,
193 gen_type = u_index_translator( svga_hw_prims,
206 if (gen_type == U_TRANSLATE_MEMCPY) {
207 /* No need for translation, just pass through to hardware:
209 return svga_hwtnl_simple_draw_range_elements( hwtnl, index_buffer,
214 gen_prim, start, count );
217 struct pipe_resource *gen_buf = NULL;
219 /* Need to allocate a new index buffer and run the translate
220 * func to populate it. Could potentially cache this translated
221 * index buffer with the original to avoid future
222 * re-translations. Not much point if we're just accelerating
223 * GL though, as index buffers are typically used only once
226 ret = translate_indices( hwtnl,
236 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
250 pipe_resource_reference( &gen_buf, NULL );