Merge branch 'arb_half_float_vertex'
[mesa.git] / src / gallium / drivers / svga / svga_draw_elements.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 "pipe/p_inlines.h"
27 #include "util/u_upload_mgr.h"
28 #include "indices/u_indices.h"
29
30 #include "svga_cmd.h"
31 #include "svga_draw.h"
32 #include "svga_draw_private.h"
33 #include "svga_screen_buffer.h"
34 #include "svga_winsys.h"
35 #include "svga_context.h"
36
37 #include "svga_hw_reg.h"
38
39
40 static enum pipe_error
41 translate_indices( struct svga_hwtnl *hwtnl,
42 struct pipe_buffer *src,
43 unsigned offset,
44 unsigned nr,
45 unsigned index_size,
46 u_translate_func translate,
47 struct pipe_buffer **out_buf )
48 {
49 struct pipe_screen *screen = hwtnl->svga->pipe.screen;
50 unsigned size = index_size * nr;
51 const void *src_map = NULL;
52 struct pipe_buffer *dst = NULL;
53 void *dst_map = NULL;
54
55 dst = screen->buffer_create( screen, 32,
56 PIPE_BUFFER_USAGE_INDEX |
57 PIPE_BUFFER_USAGE_CPU_WRITE |
58 PIPE_BUFFER_USAGE_GPU_READ,
59 size );
60 if (dst == NULL)
61 goto fail;
62
63 src_map = pipe_buffer_map( screen, src, PIPE_BUFFER_USAGE_CPU_READ );
64 if (src_map == NULL)
65 goto fail;
66
67 dst_map = pipe_buffer_map( screen, dst, PIPE_BUFFER_USAGE_CPU_WRITE );
68 if (dst_map == NULL)
69 goto fail;
70
71 translate( (const char *)src_map + offset,
72 nr,
73 dst_map );
74
75 pipe_buffer_unmap( screen, src );
76 pipe_buffer_unmap( screen, dst );
77
78 *out_buf = dst;
79 return PIPE_OK;
80
81 fail:
82 if (src_map)
83 screen->buffer_unmap( screen, src );
84
85 if (dst_map)
86 screen->buffer_unmap( screen, dst );
87
88 if (dst)
89 screen->buffer_destroy( dst );
90
91 return PIPE_ERROR_OUT_OF_MEMORY;
92 }
93
94
95
96
97
98 enum pipe_error
99 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
100 struct pipe_buffer *index_buffer,
101 unsigned index_size,
102 unsigned min_index,
103 unsigned max_index,
104 unsigned prim,
105 unsigned start,
106 unsigned count,
107 unsigned bias )
108 {
109 struct pipe_buffer *upload_buffer = NULL;
110 SVGA3dPrimitiveRange range;
111 unsigned hw_prim;
112 unsigned hw_count;
113 unsigned index_offset = start * index_size;
114 int ret = PIPE_OK;
115
116 hw_prim = svga_translate_prim(prim, count, &hw_count);
117 if (hw_count == 0)
118 goto done;
119
120 if (index_buffer &&
121 svga_buffer_is_user_buffer(index_buffer))
122 {
123 assert( index_buffer->size >= index_offset + count * index_size );
124
125 ret = u_upload_buffer( hwtnl->upload_ib,
126 index_offset,
127 count * index_size,
128 index_buffer,
129 &index_offset,
130 &upload_buffer );
131 if (ret)
132 goto done;
133
134 /* Don't need to worry about refcounting index_buffer as this is
135 * just a stack variable without a counted reference of its own.
136 * The caller holds the reference.
137 */
138 index_buffer = upload_buffer;
139 }
140
141 range.primType = hw_prim;
142 range.primitiveCount = hw_count;
143 range.indexArray.offset = index_offset;
144 range.indexArray.stride = index_size;
145 range.indexWidth = index_size;
146 range.indexBias = bias;
147
148 ret = svga_hwtnl_prim( hwtnl, &range, min_index, max_index, index_buffer );
149 if (ret)
150 goto done;
151
152 done:
153 if (upload_buffer)
154 pipe_buffer_reference( &upload_buffer, NULL );
155
156 return ret;
157 }
158
159
160
161
162 enum pipe_error
163 svga_hwtnl_draw_range_elements( struct svga_hwtnl *hwtnl,
164 struct pipe_buffer *index_buffer,
165 unsigned index_size,
166 unsigned min_index,
167 unsigned max_index,
168 unsigned prim, unsigned start, unsigned count,
169 unsigned bias)
170 {
171 unsigned gen_prim, gen_size, gen_nr, gen_type;
172 u_translate_func gen_func;
173 enum pipe_error ret = PIPE_OK;
174
175 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
176 prim >= PIPE_PRIM_TRIANGLES)
177 {
178 gen_type = u_unfilled_translator( prim,
179 index_size,
180 count,
181 hwtnl->api_fillmode,
182 &gen_prim,
183 &gen_size,
184 &gen_nr,
185 &gen_func );
186 }
187 else
188 {
189 gen_type = u_index_translator( svga_hw_prims,
190 prim,
191 index_size,
192 count,
193 hwtnl->api_pv,
194 hwtnl->hw_pv,
195 &gen_prim,
196 &gen_size,
197 &gen_nr,
198 &gen_func );
199 }
200
201
202 if (gen_type == U_TRANSLATE_MEMCPY) {
203 /* No need for translation, just pass through to hardware:
204 */
205 return svga_hwtnl_simple_draw_range_elements( hwtnl, index_buffer,
206 index_size,
207 min_index,
208 max_index,
209 gen_prim, start, count, bias );
210 }
211 else {
212 struct pipe_buffer *gen_buf = NULL;
213
214 /* Need to allocate a new index buffer and run the translate
215 * func to populate it. Could potentially cache this translated
216 * index buffer with the original to avoid future
217 * re-translations. Not much point if we're just accelerating
218 * GL though, as index buffers are typically used only once
219 * there.
220 */
221 ret = translate_indices( hwtnl,
222 index_buffer,
223 start * index_size,
224 gen_nr,
225 gen_size,
226 gen_func,
227 &gen_buf );
228 if (ret)
229 goto done;
230
231 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
232 gen_buf,
233 gen_size,
234 min_index,
235 max_index,
236 gen_prim,
237 0,
238 gen_nr,
239 bias );
240 if (ret)
241 goto done;
242
243 done:
244 if (gen_buf)
245 pipe_buffer_reference( &gen_buf, NULL );
246
247 return ret;
248 }
249 }
250
251
252
253
254