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 "pipe/p_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
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"
40 struct svga_hwtnl
*svga_hwtnl_create( struct svga_context
*svga
,
41 struct u_upload_mgr
*upload_ib
,
42 struct svga_winsys_context
*swc
)
44 struct svga_hwtnl
*hwtnl
= CALLOC_STRUCT(svga_hwtnl
);
49 hwtnl
->upload_ib
= upload_ib
;
59 void svga_hwtnl_destroy( struct svga_hwtnl
*hwtnl
)
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
,
70 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++)
71 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
], NULL
);
73 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++)
74 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
81 void svga_hwtnl_set_flatshade( struct svga_hwtnl
*hwtnl
,
83 boolean flatshade_first
)
85 hwtnl
->hw_pv
= PV_FIRST
;
86 hwtnl
->api_pv
= (flatshade
&& !flatshade_first
) ? PV_LAST
: PV_FIRST
;
89 void svga_hwtnl_set_unfilled( struct svga_hwtnl
*hwtnl
,
92 hwtnl
->api_fillmode
= mode
;
95 void svga_hwtnl_reset_vdecl( struct svga_hwtnl
*hwtnl
,
100 assert(hwtnl
->cmd
.prim_count
== 0);
102 for (i
= count
; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
103 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
],
107 hwtnl
->cmd
.vdecl_count
= count
;
111 void svga_hwtnl_vdecl( struct svga_hwtnl
*hwtnl
,
113 const SVGA3dVertexDecl
*decl
,
114 struct pipe_buffer
*vb
)
116 assert(hwtnl
->cmd
.prim_count
== 0);
118 assert( i
< hwtnl
->cmd
.vdecl_count
);
120 hwtnl
->cmd
.vdecl
[i
] = *decl
;
122 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
],
129 svga_hwtnl_flush( struct svga_hwtnl
*hwtnl
)
131 struct svga_winsys_context
*swc
= hwtnl
->cmd
.swc
;
132 struct svga_context
*svga
= hwtnl
->svga
;
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
;
143 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
144 handle
= svga_buffer_handle(svga
, hwtnl
->cmd
.vdecl_vb
[i
]);
146 return PIPE_ERROR_OUT_OF_MEMORY
;
148 vb_handle
[i
] = handle
;
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
]);
155 return PIPE_ERROR_OUT_OF_MEMORY
;
160 ib_handle
[i
] = handle
;
163 ret
= SVGA3D_BeginDrawPrimitives(swc
,
165 hwtnl
->cmd
.vdecl_count
,
167 hwtnl
->cmd
.prim_count
);
174 hwtnl
->cmd
.vdecl_count
* sizeof hwtnl
->cmd
.vdecl
[0]);
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.
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;
186 vdecl
[i
].rangeHint
.first
= 0;
187 vdecl
[i
].rangeHint
.last
= 0;
190 swc
->surface_relocation(swc
,
191 &vdecl
[i
].array
.surfaceId
,
193 PIPE_BUFFER_USAGE_GPU_READ
);
198 hwtnl
->cmd
.prim_count
* sizeof hwtnl
->cmd
.prim
[0]);
200 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++) {
201 swc
->surface_relocation(swc
,
202 &prim
[i
].indexArray
.surfaceId
,
204 PIPE_BUFFER_USAGE_GPU_READ
);
205 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
208 SVGA_FIFOCommitAll( swc
);
209 hwtnl
->cmd
.prim_count
= 0;
219 /***********************************************************************
220 * Internal functions:
223 enum pipe_error
svga_hwtnl_prim( struct svga_hwtnl
*hwtnl
,
224 const SVGA3dPrimitiveRange
*range
,
227 struct pipe_buffer
*ib
)
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
;
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
);
250 switch (hwtnl
->cmd
.vdecl
[i
].identity
.type
) {
251 case SVGA3D_DECLTYPE_FLOAT1
:
254 case SVGA3D_DECLTYPE_FLOAT2
:
257 case SVGA3D_DECLTYPE_FLOAT3
:
260 case SVGA3D_DECLTYPE_FLOAT4
:
263 case SVGA3D_DECLTYPE_D3DCOLOR
:
266 case SVGA3D_DECLTYPE_UBYTE4
:
269 case SVGA3D_DECLTYPE_SHORT2
:
272 case SVGA3D_DECLTYPE_SHORT4
:
275 case SVGA3D_DECLTYPE_UBYTE4N
:
278 case SVGA3D_DECLTYPE_SHORT2N
:
281 case SVGA3D_DECLTYPE_SHORT4N
:
284 case SVGA3D_DECLTYPE_USHORT2N
:
287 case SVGA3D_DECLTYPE_USHORT4N
:
290 case SVGA3D_DECLTYPE_UDEC3
:
293 case SVGA3D_DECLTYPE_DEC3N
:
296 case SVGA3D_DECLTYPE_FLOAT16_2
:
299 case SVGA3D_DECLTYPE_FLOAT16_4
:
308 assert(!stride
|| width
<= stride
);
309 assert(offset
+ (index_bias
+ max_index
)*stride
+ width
<= size
);
312 assert(range
->indexWidth
== range
->indexArray
.stride
);
315 unsigned size
= ib
->size
;
316 unsigned offset
= range
->indexArray
.offset
;
317 unsigned stride
= range
->indexArray
.stride
;
321 assert(offset
< size
);
324 switch (range
->primType
) {
325 case SVGA3D_PRIMITIVE_POINTLIST
:
326 count
= range
->primitiveCount
;
328 case SVGA3D_PRIMITIVE_LINELIST
:
329 count
= range
->primitiveCount
* 2;
331 case SVGA3D_PRIMITIVE_LINESTRIP
:
332 count
= range
->primitiveCount
+ 1;
334 case SVGA3D_PRIMITIVE_TRIANGLELIST
:
335 count
= range
->primitiveCount
* 3;
337 case SVGA3D_PRIMITIVE_TRIANGLESTRIP
:
338 count
= range
->primitiveCount
+ 2;
340 case SVGA3D_PRIMITIVE_TRIANGLEFAN
:
341 count
= range
->primitiveCount
+ 2;
349 assert(offset
+ count
*stride
<= size
);
354 if (hwtnl
->cmd
.prim_count
+1 >= QSZ
) {
355 ret
= svga_hwtnl_flush( hwtnl
);
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
;
364 hwtnl
->cmd
.prim
[hwtnl
->cmd
.prim_count
] = *range
;
366 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[hwtnl
->cmd
.prim_count
], ib
);
367 hwtnl
->cmd
.prim_count
++;