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_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"
43 struct svga_hwtnl
*svga_hwtnl_create( struct svga_context
*svga
,
44 struct u_upload_mgr
*upload_ib
,
45 struct svga_winsys_context
*swc
)
47 struct svga_hwtnl
*hwtnl
= CALLOC_STRUCT(svga_hwtnl
);
52 hwtnl
->upload_ib
= upload_ib
;
62 void svga_hwtnl_destroy( struct svga_hwtnl
*hwtnl
)
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
,
73 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++)
74 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
], NULL
);
76 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++)
77 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
84 void svga_hwtnl_set_flatshade( struct svga_hwtnl
*hwtnl
,
86 boolean flatshade_first
)
88 hwtnl
->hw_pv
= PV_FIRST
;
89 hwtnl
->api_pv
= (flatshade
&& !flatshade_first
) ? PV_LAST
: PV_FIRST
;
92 void svga_hwtnl_set_unfilled( struct svga_hwtnl
*hwtnl
,
95 hwtnl
->api_fillmode
= mode
;
98 void svga_hwtnl_reset_vdecl( struct svga_hwtnl
*hwtnl
,
103 assert(hwtnl
->cmd
.prim_count
== 0);
105 for (i
= count
; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
106 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
],
110 hwtnl
->cmd
.vdecl_count
= count
;
114 void svga_hwtnl_vdecl( struct svga_hwtnl
*hwtnl
,
116 const SVGA3dVertexDecl
*decl
,
117 struct pipe_buffer
*vb
)
119 assert(hwtnl
->cmd
.prim_count
== 0);
121 assert( i
< hwtnl
->cmd
.vdecl_count
);
123 hwtnl
->cmd
.vdecl
[i
] = *decl
;
125 pipe_buffer_reference(&hwtnl
->cmd
.vdecl_vb
[i
],
132 svga_hwtnl_flush( struct svga_hwtnl
*hwtnl
)
134 struct svga_winsys_context
*swc
= hwtnl
->cmd
.swc
;
135 struct svga_context
*svga
= hwtnl
->svga
;
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
;
146 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
147 handle
= svga_buffer_handle(svga
, hwtnl
->cmd
.vdecl_vb
[i
]);
149 return PIPE_ERROR_OUT_OF_MEMORY
;
151 vb_handle
[i
] = handle
;
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
]);
158 return PIPE_ERROR_OUT_OF_MEMORY
;
163 ib_handle
[i
] = handle
;
166 SVGA_DBG(DEBUG_DMA
, "draw to sid %p, %d prims\n",
167 svga
->curr
.framebuffer
.cbufs
[0] ?
168 svga_surface(svga
->curr
.framebuffer
.cbufs
[0])->handle
: NULL
,
169 hwtnl
->cmd
.prim_count
);
171 ret
= SVGA3D_BeginDrawPrimitives(swc
,
173 hwtnl
->cmd
.vdecl_count
,
175 hwtnl
->cmd
.prim_count
);
182 hwtnl
->cmd
.vdecl_count
* sizeof hwtnl
->cmd
.vdecl
[0]);
184 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
185 /* Given rangeHint is considered to be relative to indexBias, and
186 * indexBias varies per primitive, we cannot accurately supply an
187 * rangeHint when emitting more than one primitive per draw command.
189 if (hwtnl
->cmd
.prim_count
== 1) {
190 vdecl
[i
].rangeHint
.first
= hwtnl
->cmd
.min_index
[0];
191 vdecl
[i
].rangeHint
.last
= hwtnl
->cmd
.max_index
[0] + 1;
194 vdecl
[i
].rangeHint
.first
= 0;
195 vdecl
[i
].rangeHint
.last
= 0;
198 swc
->surface_relocation(swc
,
199 &vdecl
[i
].array
.surfaceId
,
201 PIPE_BUFFER_USAGE_GPU_READ
);
206 hwtnl
->cmd
.prim_count
* sizeof hwtnl
->cmd
.prim
[0]);
208 for (i
= 0; i
< hwtnl
->cmd
.prim_count
; i
++) {
209 swc
->surface_relocation(swc
,
210 &prim
[i
].indexArray
.surfaceId
,
212 PIPE_BUFFER_USAGE_GPU_READ
);
213 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[i
], NULL
);
216 SVGA_FIFOCommitAll( swc
);
217 hwtnl
->cmd
.prim_count
= 0;
227 /***********************************************************************
228 * Internal functions:
231 enum pipe_error
svga_hwtnl_prim( struct svga_hwtnl
*hwtnl
,
232 const SVGA3dPrimitiveRange
*range
,
235 struct pipe_buffer
*ib
)
242 for (i
= 0; i
< hwtnl
->cmd
.vdecl_count
; i
++) {
243 struct pipe_buffer
*vb
= hwtnl
->cmd
.vdecl_vb
[i
];
244 unsigned size
= vb
? vb
->size
: 0;
245 unsigned offset
= hwtnl
->cmd
.vdecl
[i
].array
.offset
;
246 unsigned stride
= hwtnl
->cmd
.vdecl
[i
].array
.stride
;
247 unsigned index_bias
= range
->indexBias
;
252 assert(offset
< size
);
253 assert(index_bias
>= 0);
254 assert(min_index
<= max_index
);
255 assert(offset
+ index_bias
*stride
< size
);
256 assert(offset
+ (index_bias
+ min_index
)*stride
< size
);
258 switch (hwtnl
->cmd
.vdecl
[i
].identity
.type
) {
259 case SVGA3D_DECLTYPE_FLOAT1
:
262 case SVGA3D_DECLTYPE_FLOAT2
:
265 case SVGA3D_DECLTYPE_FLOAT3
:
268 case SVGA3D_DECLTYPE_FLOAT4
:
271 case SVGA3D_DECLTYPE_D3DCOLOR
:
274 case SVGA3D_DECLTYPE_UBYTE4
:
277 case SVGA3D_DECLTYPE_SHORT2
:
280 case SVGA3D_DECLTYPE_SHORT4
:
283 case SVGA3D_DECLTYPE_UBYTE4N
:
286 case SVGA3D_DECLTYPE_SHORT2N
:
289 case SVGA3D_DECLTYPE_SHORT4N
:
292 case SVGA3D_DECLTYPE_USHORT2N
:
295 case SVGA3D_DECLTYPE_USHORT4N
:
298 case SVGA3D_DECLTYPE_UDEC3
:
301 case SVGA3D_DECLTYPE_DEC3N
:
304 case SVGA3D_DECLTYPE_FLOAT16_2
:
307 case SVGA3D_DECLTYPE_FLOAT16_4
:
316 assert(!stride
|| width
<= stride
);
317 assert(offset
+ (index_bias
+ max_index
)*stride
+ width
<= size
);
320 assert(range
->indexWidth
== range
->indexArray
.stride
);
323 unsigned size
= ib
->size
;
324 unsigned offset
= range
->indexArray
.offset
;
325 unsigned stride
= range
->indexArray
.stride
;
329 assert(offset
< size
);
332 switch (range
->primType
) {
333 case SVGA3D_PRIMITIVE_POINTLIST
:
334 count
= range
->primitiveCount
;
336 case SVGA3D_PRIMITIVE_LINELIST
:
337 count
= range
->primitiveCount
* 2;
339 case SVGA3D_PRIMITIVE_LINESTRIP
:
340 count
= range
->primitiveCount
+ 1;
342 case SVGA3D_PRIMITIVE_TRIANGLELIST
:
343 count
= range
->primitiveCount
* 3;
345 case SVGA3D_PRIMITIVE_TRIANGLESTRIP
:
346 count
= range
->primitiveCount
+ 2;
348 case SVGA3D_PRIMITIVE_TRIANGLEFAN
:
349 count
= range
->primitiveCount
+ 2;
357 assert(offset
+ count
*stride
<= size
);
362 if (hwtnl
->cmd
.prim_count
+1 >= QSZ
) {
363 ret
= svga_hwtnl_flush( hwtnl
);
368 /* min/max indices are relative to bias */
369 hwtnl
->cmd
.min_index
[hwtnl
->cmd
.prim_count
] = min_index
;
370 hwtnl
->cmd
.max_index
[hwtnl
->cmd
.prim_count
] = max_index
;
372 hwtnl
->cmd
.prim
[hwtnl
->cmd
.prim_count
] = *range
;
374 pipe_buffer_reference(&hwtnl
->cmd
.prim_ib
[hwtnl
->cmd
.prim_count
], ib
);
375 hwtnl
->cmd
.prim_count
++;