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