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 "pipe/p_compiler.h"
27 #include "util/u_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "util/u_upload_mgr.h"
33 #include "svga_context.h"
34 #include "svga_draw.h"
35 #include "svga_draw_private.h"
36 #include "svga_debug.h"
37 #include "svga_screen.h"
38 #include "svga_resource_buffer.h"
39 #include "svga_resource_texture.h"
40 #include "svga_surface.h"
41 #include "svga_winsys.h"
45 struct svga_hwtnl
*svga_hwtnl_create( struct svga_context
*svga
,
46 struct u_upload_mgr
*upload_ib
,
47 struct svga_winsys_context
*swc
)
49 struct svga_hwtnl
*hwtnl
= CALLOC_STRUCT(svga_hwtnl
);
54 hwtnl
->upload_ib
= upload_ib
;
64 void svga_hwtnl_destroy( struct svga_hwtnl
*hwtnl
)
68 for (i
= 0; i
< PIPE_PRIM_MAX
; i
++) {
69 for (j
= 0; j
< IDX_CACHE_MAX
; j
++) {
70 pipe_resource_reference( &hwtnl
->index_cache
[i
][j
].buffer
,
75 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++)
76 pipe_resource_reference(&hwtnl
->cmd
.vdecl_vb
[i
], NULL
);
78 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++)
79 pipe_resource_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
86 void svga_hwtnl_set_flatshade( struct svga_hwtnl
*hwtnl
,
88 boolean flatshade_first
)
90 hwtnl
->hw_pv
= PV_FIRST
;
91 hwtnl
->api_pv
= (flatshade
&& !flatshade_first
) ? PV_LAST
: PV_FIRST
;
94 void svga_hwtnl_set_unfilled( struct svga_hwtnl
*hwtnl
,
97 hwtnl
->api_fillmode
= mode
;
100 void svga_hwtnl_reset_vdecl( struct svga_hwtnl
*hwtnl
,
105 assert(hwtnl
->cmd
.prim_count
== 0);
107 for (i
= count
; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
108 pipe_resource_reference(&hwtnl
->cmd
.vdecl_vb
[i
],
112 hwtnl
->cmd
.vdecl_count
= count
;
116 void svga_hwtnl_vdecl( struct svga_hwtnl
*hwtnl
,
118 const SVGA3dVertexDecl
*decl
,
119 struct pipe_resource
*vb
)
121 assert(hwtnl
->cmd
.prim_count
== 0);
123 assert( i
< hwtnl
->cmd
.vdecl_count
);
125 hwtnl
->cmd
.vdecl
[i
] = *decl
;
127 pipe_resource_reference(&hwtnl
->cmd
.vdecl_vb
[i
], vb
);
133 svga_hwtnl_flush( struct svga_hwtnl
*hwtnl
)
135 struct svga_winsys_context
*swc
= hwtnl
->cmd
.swc
;
136 struct svga_context
*svga
= hwtnl
->svga
;
139 if (hwtnl
->cmd
.prim_count
) {
140 struct svga_winsys_surface
*vb_handle
[SVGA3D_INPUTREG_MAX
];
141 struct svga_winsys_surface
*ib_handle
[QSZ
];
142 struct svga_winsys_surface
*handle
;
143 SVGA3dVertexDecl
*vdecl
;
144 SVGA3dPrimitiveRange
*prim
;
147 /* Unmap upload manager vertex buffers */
148 u_upload_flush(svga
->upload_vb
);
150 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
151 handle
= svga_buffer_handle(svga
, hwtnl
->cmd
.vdecl_vb
[i
]);
153 return PIPE_ERROR_OUT_OF_MEMORY
;
155 vb_handle
[i
] = handle
;
158 /* Unmap upload manager index buffers */
159 u_upload_flush(svga
->upload_ib
);
161 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++) {
162 if (hwtnl
->cmd
.prim_ib
[i
]) {
163 handle
= svga_buffer_handle(svga
, hwtnl
->cmd
.prim_ib
[i
]);
165 return PIPE_ERROR_OUT_OF_MEMORY
;
170 ib_handle
[i
] = handle
;
173 SVGA_DBG(DEBUG_DMA
, "draw to sid %p, %d prims\n",
174 svga
->curr
.framebuffer
.cbufs
[0] ?
175 svga_surface(svga
->curr
.framebuffer
.cbufs
[0])->handle
: NULL
,
176 hwtnl
->cmd
.prim_count
);
178 ret
= SVGA3D_BeginDrawPrimitives(swc
,
180 hwtnl
->cmd
.vdecl_count
,
182 hwtnl
->cmd
.prim_count
);
189 hwtnl
->cmd
.vdecl_count
* sizeof hwtnl
->cmd
.vdecl
[0]);
191 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
192 /* Given rangeHint is considered to be relative to indexBias, and
193 * indexBias varies per primitive, we cannot accurately supply an
194 * rangeHint when emitting more than one primitive per draw command.
196 if (hwtnl
->cmd
.prim_count
== 1) {
197 vdecl
[i
].rangeHint
.first
= hwtnl
->cmd
.min_index
[0];
198 vdecl
[i
].rangeHint
.last
= hwtnl
->cmd
.max_index
[0] + 1;
201 vdecl
[i
].rangeHint
.first
= 0;
202 vdecl
[i
].rangeHint
.last
= 0;
205 swc
->surface_relocation(swc
,
206 &vdecl
[i
].array
.surfaceId
,
213 hwtnl
->cmd
.prim_count
* sizeof hwtnl
->cmd
.prim
[0]);
215 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++) {
216 swc
->surface_relocation(swc
,
217 &prim
[i
].indexArray
.surfaceId
,
220 pipe_resource_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
223 SVGA_FIFOCommitAll( swc
);
224 hwtnl
->cmd
.prim_count
= 0;
234 /***********************************************************************
235 * Internal functions:
238 enum pipe_error
svga_hwtnl_prim( struct svga_hwtnl
*hwtnl
,
239 const SVGA3dPrimitiveRange
*range
,
242 struct pipe_resource
*ib
)
249 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
250 struct pipe_resource
*vb
= hwtnl
->cmd
.vdecl_vb
[i
];
251 unsigned size
= vb
? vb
->width0
: 0;
252 unsigned offset
= hwtnl
->cmd
.vdecl
[i
].array
.offset
;
253 unsigned stride
= hwtnl
->cmd
.vdecl
[i
].array
.stride
;
254 unsigned index_bias
= range
->indexBias
;
259 assert(offset
< size
);
260 assert(index_bias
>= 0);
261 assert(min_index
<= max_index
);
262 assert(offset
+ index_bias
*stride
< size
);
263 if (min_index
!= ~0) {
264 assert(offset
+ (index_bias
+ min_index
) * stride
< size
);
267 switch (hwtnl
->cmd
.vdecl
[i
].identity
.type
) {
268 case SVGA3D_DECLTYPE_FLOAT1
:
271 case SVGA3D_DECLTYPE_FLOAT2
:
274 case SVGA3D_DECLTYPE_FLOAT3
:
277 case SVGA3D_DECLTYPE_FLOAT4
:
280 case SVGA3D_DECLTYPE_D3DCOLOR
:
283 case SVGA3D_DECLTYPE_UBYTE4
:
286 case SVGA3D_DECLTYPE_SHORT2
:
289 case SVGA3D_DECLTYPE_SHORT4
:
292 case SVGA3D_DECLTYPE_UBYTE4N
:
295 case SVGA3D_DECLTYPE_SHORT2N
:
298 case SVGA3D_DECLTYPE_SHORT4N
:
301 case SVGA3D_DECLTYPE_USHORT2N
:
304 case SVGA3D_DECLTYPE_USHORT4N
:
307 case SVGA3D_DECLTYPE_UDEC3
:
310 case SVGA3D_DECLTYPE_DEC3N
:
313 case SVGA3D_DECLTYPE_FLOAT16_2
:
316 case SVGA3D_DECLTYPE_FLOAT16_4
:
325 if (max_index
!= ~0) {
326 assert(offset
+ (index_bias
+ max_index
) * stride
+ width
<= size
);
330 assert(range
->indexWidth
== range
->indexArray
.stride
);
333 unsigned size
= ib
->width0
;
334 unsigned offset
= range
->indexArray
.offset
;
335 unsigned stride
= range
->indexArray
.stride
;
339 assert(offset
< size
);
342 switch (range
->primType
) {
343 case SVGA3D_PRIMITIVE_POINTLIST
:
344 count
= range
->primitiveCount
;
346 case SVGA3D_PRIMITIVE_LINELIST
:
347 count
= range
->primitiveCount
* 2;
349 case SVGA3D_PRIMITIVE_LINESTRIP
:
350 count
= range
->primitiveCount
+ 1;
352 case SVGA3D_PRIMITIVE_TRIANGLELIST
:
353 count
= range
->primitiveCount
* 3;
355 case SVGA3D_PRIMITIVE_TRIANGLESTRIP
:
356 count
= range
->primitiveCount
+ 2;
358 case SVGA3D_PRIMITIVE_TRIANGLEFAN
:
359 count
= range
->primitiveCount
+ 2;
367 assert(offset
+ count
*stride
<= size
);
372 if (hwtnl
->cmd
.prim_count
+1 >= QSZ
) {
373 ret
= svga_hwtnl_flush( hwtnl
);
378 /* min/max indices are relative to bias */
379 hwtnl
->cmd
.min_index
[hwtnl
->cmd
.prim_count
] = min_index
;
380 hwtnl
->cmd
.max_index
[hwtnl
->cmd
.prim_count
] = max_index
;
382 hwtnl
->cmd
.prim
[hwtnl
->cmd
.prim_count
] = *range
;
384 pipe_resource_reference(&hwtnl
->cmd
.prim_ib
[hwtnl
->cmd
.prim_count
], ib
);
385 hwtnl
->cmd
.prim_count
++;