Merge branch 'radeon-texrewrite-clean' into mesa_7_7_branch
[mesa.git] / src / gallium / drivers / svga / svga_draw.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_compiler.h"
27 #include "pipe/p_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_screen.h"
35 #include "svga_screen_buffer.h"
36 #include "svga_winsys.h"
37 #include "svga_cmd.h"
38
39
40 struct svga_hwtnl *svga_hwtnl_create( struct svga_context *svga,
41 struct u_upload_mgr *upload_ib,
42 struct svga_winsys_context *swc )
43 {
44 struct svga_hwtnl *hwtnl = CALLOC_STRUCT(svga_hwtnl);
45 if (hwtnl == NULL)
46 goto fail;
47
48 hwtnl->svga = svga;
49 hwtnl->upload_ib = upload_ib;
50
51 hwtnl->cmd.swc = swc;
52
53 return hwtnl;
54
55 fail:
56 return NULL;
57 }
58
59 void svga_hwtnl_destroy( struct svga_hwtnl *hwtnl )
60 {
61 int i, j;
62
63 for (i = 0; i < PIPE_PRIM_MAX; i++) {
64 for (j = 0; j < IDX_CACHE_MAX; j++) {
65 pipe_buffer_reference( &hwtnl->index_cache[i][j].buffer,
66 NULL );
67 }
68 }
69
70 for (i = 0; i < hwtnl->cmd.vdecl_count; i++)
71 pipe_buffer_reference(&hwtnl->cmd.vdecl_vb[i], NULL);
72
73 for (i = 0; i < hwtnl->cmd.prim_count; i++)
74 pipe_buffer_reference(&hwtnl->cmd.prim_ib[i], NULL);
75
76
77 FREE(hwtnl);
78 }
79
80
81 void svga_hwtnl_set_flatshade( struct svga_hwtnl *hwtnl,
82 boolean flatshade,
83 boolean flatshade_first )
84 {
85 hwtnl->hw_pv = PV_FIRST;
86 hwtnl->api_pv = (flatshade && !flatshade_first) ? PV_LAST : PV_FIRST;
87 }
88
89 void svga_hwtnl_set_unfilled( struct svga_hwtnl *hwtnl,
90 unsigned mode )
91 {
92 hwtnl->api_fillmode = mode;
93 }
94
95 void svga_hwtnl_reset_vdecl( struct svga_hwtnl *hwtnl,
96 unsigned count )
97 {
98 unsigned i;
99
100 assert(hwtnl->cmd.prim_count == 0);
101
102 for (i = count; i < hwtnl->cmd.vdecl_count; i++) {
103 pipe_buffer_reference(&hwtnl->cmd.vdecl_vb[i],
104 NULL);
105 }
106
107 hwtnl->cmd.vdecl_count = count;
108 }
109
110
111 void svga_hwtnl_vdecl( struct svga_hwtnl *hwtnl,
112 unsigned i,
113 const SVGA3dVertexDecl *decl,
114 struct pipe_buffer *vb)
115 {
116 assert(hwtnl->cmd.prim_count == 0);
117
118 assert( i < hwtnl->cmd.vdecl_count );
119
120 hwtnl->cmd.vdecl[i] = *decl;
121
122 pipe_buffer_reference(&hwtnl->cmd.vdecl_vb[i],
123 vb);
124 }
125
126
127
128 enum pipe_error
129 svga_hwtnl_flush( struct svga_hwtnl *hwtnl )
130 {
131 struct svga_winsys_context *swc = hwtnl->cmd.swc;
132 struct svga_context *svga = hwtnl->svga;
133 enum pipe_error ret;
134
135 if (hwtnl->cmd.prim_count) {
136 struct svga_winsys_surface *vb_handle[SVGA3D_INPUTREG_MAX];
137 struct svga_winsys_surface *ib_handle[QSZ];
138 struct svga_winsys_surface *handle;
139 SVGA3dVertexDecl *vdecl;
140 SVGA3dPrimitiveRange *prim;
141 unsigned i;
142
143 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
144 handle = svga_buffer_handle(svga, hwtnl->cmd.vdecl_vb[i]);
145 if (handle == NULL)
146 return PIPE_ERROR_OUT_OF_MEMORY;
147
148 vb_handle[i] = handle;
149 }
150
151 for (i = 0; i < hwtnl->cmd.prim_count; i++) {
152 if (hwtnl->cmd.prim_ib[i]) {
153 handle = svga_buffer_handle(svga, hwtnl->cmd.prim_ib[i]);
154 if (handle == NULL)
155 return PIPE_ERROR_OUT_OF_MEMORY;
156 }
157 else
158 handle = NULL;
159
160 ib_handle[i] = handle;
161 }
162
163 ret = SVGA3D_BeginDrawPrimitives(swc,
164 &vdecl,
165 hwtnl->cmd.vdecl_count,
166 &prim,
167 hwtnl->cmd.prim_count);
168 if (ret != PIPE_OK)
169 return ret;
170
171
172 memcpy( vdecl,
173 hwtnl->cmd.vdecl,
174 hwtnl->cmd.vdecl_count * sizeof hwtnl->cmd.vdecl[0]);
175
176 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
177 /* Given rangeHint is considered to be relative to indexBias, and
178 * indexBias varies per primitive, we cannot accurately supply an
179 * rangeHint when emitting more than one primitive per draw command.
180 */
181 if (hwtnl->cmd.prim_count == 1) {
182 vdecl[i].rangeHint.first = hwtnl->cmd.min_index[0];
183 vdecl[i].rangeHint.last = hwtnl->cmd.max_index[0] + 1;
184 }
185 else {
186 vdecl[i].rangeHint.first = 0;
187 vdecl[i].rangeHint.last = 0;
188 }
189
190 swc->surface_relocation(swc,
191 &vdecl[i].array.surfaceId,
192 vb_handle[i],
193 PIPE_BUFFER_USAGE_GPU_READ);
194 }
195
196 memcpy( prim,
197 hwtnl->cmd.prim,
198 hwtnl->cmd.prim_count * sizeof hwtnl->cmd.prim[0]);
199
200 for (i = 0; i < hwtnl->cmd.prim_count; i++) {
201 swc->surface_relocation(swc,
202 &prim[i].indexArray.surfaceId,
203 ib_handle[i],
204 PIPE_BUFFER_USAGE_GPU_READ);
205 pipe_buffer_reference(&hwtnl->cmd.prim_ib[i], NULL);
206 }
207
208 SVGA_FIFOCommitAll( swc );
209 hwtnl->cmd.prim_count = 0;
210 }
211
212 return PIPE_OK;
213 }
214
215
216
217
218
219 /***********************************************************************
220 * Internal functions:
221 */
222
223 enum pipe_error svga_hwtnl_prim( struct svga_hwtnl *hwtnl,
224 const SVGA3dPrimitiveRange *range,
225 unsigned min_index,
226 unsigned max_index,
227 struct pipe_buffer *ib )
228 {
229 int ret = PIPE_OK;
230
231 #ifdef DEBUG
232 {
233 unsigned i;
234 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
235 struct pipe_buffer *vb = hwtnl->cmd.vdecl_vb[i];
236 unsigned size = vb ? vb->size : 0;
237 unsigned offset = hwtnl->cmd.vdecl[i].array.offset;
238 unsigned stride = hwtnl->cmd.vdecl[i].array.stride;
239 unsigned index_bias = range->indexBias;
240 unsigned width;
241
242 assert(vb);
243 assert(size);
244 assert(offset < size);
245 assert(index_bias >= 0);
246 assert(min_index <= max_index);
247 assert(offset + index_bias*stride < size);
248 assert(offset + (index_bias + min_index)*stride < size);
249
250 switch (hwtnl->cmd.vdecl[i].identity.type) {
251 case SVGA3D_DECLTYPE_FLOAT1:
252 width = 4;
253 break;
254 case SVGA3D_DECLTYPE_FLOAT2:
255 width = 4*2;
256 break;
257 case SVGA3D_DECLTYPE_FLOAT3:
258 width = 4*3;
259 break;
260 case SVGA3D_DECLTYPE_FLOAT4:
261 width = 4*4;
262 break;
263 case SVGA3D_DECLTYPE_D3DCOLOR:
264 width = 4;
265 break;
266 case SVGA3D_DECLTYPE_UBYTE4:
267 width = 1*4;
268 break;
269 case SVGA3D_DECLTYPE_SHORT2:
270 width = 2*2;
271 break;
272 case SVGA3D_DECLTYPE_SHORT4:
273 width = 2*4;
274 break;
275 case SVGA3D_DECLTYPE_UBYTE4N:
276 width = 1*4;
277 break;
278 case SVGA3D_DECLTYPE_SHORT2N:
279 width = 2*2;
280 break;
281 case SVGA3D_DECLTYPE_SHORT4N:
282 width = 2*4;
283 break;
284 case SVGA3D_DECLTYPE_USHORT2N:
285 width = 2*2;
286 break;
287 case SVGA3D_DECLTYPE_USHORT4N:
288 width = 2*4;
289 break;
290 case SVGA3D_DECLTYPE_UDEC3:
291 width = 4;
292 break;
293 case SVGA3D_DECLTYPE_DEC3N:
294 width = 4;
295 break;
296 case SVGA3D_DECLTYPE_FLOAT16_2:
297 width = 2*2;
298 break;
299 case SVGA3D_DECLTYPE_FLOAT16_4:
300 width = 2*4;
301 break;
302 default:
303 assert(0);
304 width = 0;
305 break;
306 }
307
308 assert(!stride || width <= stride);
309 assert(offset + (index_bias + max_index)*stride + width <= size);
310 }
311
312 assert(range->indexWidth == range->indexArray.stride);
313
314 if(ib) {
315 unsigned size = ib->size;
316 unsigned offset = range->indexArray.offset;
317 unsigned stride = range->indexArray.stride;
318 unsigned count;
319
320 assert(size);
321 assert(offset < size);
322 assert(stride);
323
324 switch (range->primType) {
325 case SVGA3D_PRIMITIVE_POINTLIST:
326 count = range->primitiveCount;
327 break;
328 case SVGA3D_PRIMITIVE_LINELIST:
329 count = range->primitiveCount * 2;
330 break;
331 case SVGA3D_PRIMITIVE_LINESTRIP:
332 count = range->primitiveCount + 1;
333 break;
334 case SVGA3D_PRIMITIVE_TRIANGLELIST:
335 count = range->primitiveCount * 3;
336 break;
337 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
338 count = range->primitiveCount + 2;
339 break;
340 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
341 count = range->primitiveCount + 2;
342 break;
343 default:
344 assert(0);
345 count = 0;
346 break;
347 }
348
349 assert(offset + count*stride <= size);
350 }
351 }
352 #endif
353
354 if (hwtnl->cmd.prim_count+1 >= QSZ) {
355 ret = svga_hwtnl_flush( hwtnl );
356 if (ret != PIPE_OK)
357 return ret;
358 }
359
360 /* min/max indices are relative to bias */
361 hwtnl->cmd.min_index[hwtnl->cmd.prim_count] = min_index;
362 hwtnl->cmd.max_index[hwtnl->cmd.prim_count] = max_index;
363
364 hwtnl->cmd.prim[hwtnl->cmd.prim_count] = *range;
365
366 pipe_buffer_reference(&hwtnl->cmd.prim_ib[hwtnl->cmd.prim_count], ib);
367 hwtnl->cmd.prim_count++;
368
369 return ret;
370 }