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 **********************************************************/
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_defines.h"
31 #include "indices/u_indices.h"
32 #include "util/u_prim.h"
33 #include "svga_context.h"
34 #include "svga_hw_reg.h"
35 #include "svga3d_shaderdefs.h"
41 * Mask indicating which types of gallium primitives are actually
42 * handled by the svga device. Other types will be converted to
43 * these types by the index/translation code.
45 static const unsigned svga_hw_prims
=
46 ((1 << PIPE_PRIM_POINTS
) |
47 (1 << PIPE_PRIM_LINES
) |
48 (1 << PIPE_PRIM_LINE_STRIP
) |
49 (1 << PIPE_PRIM_TRIANGLES
) |
50 (1 << PIPE_PRIM_TRIANGLE_STRIP
) |
51 (1 << PIPE_PRIM_TRIANGLE_FAN
) |
52 (1 << PIPE_PRIM_LINES_ADJACENCY
) |
53 (1 << PIPE_PRIM_LINE_STRIP_ADJACENCY
) |
54 (1 << PIPE_PRIM_TRIANGLES_ADJACENCY
) |
55 (1 << PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
) |
56 (1 << PIPE_PRIM_PATCHES
));
60 * Translate a gallium PIPE_PRIM_x value to an SVGA3D_PRIMITIVE_x value.
61 * Also, compute the number of primitives that'll be drawn given a
63 * Note that this function doesn't have to handle PIPE_PRIM_LINE_LOOP,
64 * PIPE_PRIM_QUADS, PIPE_PRIM_QUAD_STRIP or PIPE_PRIM_POLYGON. We convert
65 * those to other types of primitives with index/translation code.
67 static inline SVGA3dPrimitiveType
68 svga_translate_prim(unsigned mode
, unsigned vcount
, unsigned *prim_count
,
69 ubyte vertices_per_patch
)
72 case PIPE_PRIM_POINTS
:
74 return SVGA3D_PRIMITIVE_POINTLIST
;
77 *prim_count
= vcount
/ 2;
78 return SVGA3D_PRIMITIVE_LINELIST
;
80 case PIPE_PRIM_LINE_STRIP
:
81 *prim_count
= vcount
- 1;
82 return SVGA3D_PRIMITIVE_LINESTRIP
;
84 case PIPE_PRIM_TRIANGLES
:
85 *prim_count
= vcount
/ 3;
86 return SVGA3D_PRIMITIVE_TRIANGLELIST
;
88 case PIPE_PRIM_TRIANGLE_STRIP
:
89 *prim_count
= vcount
- 2;
90 return SVGA3D_PRIMITIVE_TRIANGLESTRIP
;
92 case PIPE_PRIM_TRIANGLE_FAN
:
93 *prim_count
= vcount
- 2;
94 return SVGA3D_PRIMITIVE_TRIANGLEFAN
;
96 case PIPE_PRIM_LINES_ADJACENCY
:
97 *prim_count
= vcount
/ 4;
98 return SVGA3D_PRIMITIVE_LINELIST_ADJ
;
100 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
101 *prim_count
= vcount
- 3;
102 return SVGA3D_PRIMITIVE_LINESTRIP_ADJ
;
104 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
105 *prim_count
= vcount
/ 6;
106 return SVGA3D_PRIMITIVE_TRIANGLELIST_ADJ
;
108 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
109 *prim_count
= vcount
/ 2 - 2 ;
110 return SVGA3D_PRIMITIVE_TRIANGLESTRIP_ADJ
;
112 case PIPE_PRIM_PATCHES
:
113 *prim_count
= vcount
/ vertices_per_patch
;
114 assert(vertices_per_patch
>= 1);
115 assert(vertices_per_patch
<= 32);
116 return (SVGA3D_PRIMITIVE_1_CONTROL_POINT_PATCH
- 1)
117 + vertices_per_patch
;
128 u_generate_func generate
;
131 /* If non-null, this buffer is filled by calling generate(nr, map(buffer))
133 struct pipe_resource
*buffer
;
137 /** Max number of primitives per draw call */
138 #define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
141 struct svga_winsys_context
*swc
;
143 /* vertex layout info */
144 SVGA3dVertexDecl vdecl
[SVGA3D_INPUTREG_MAX
];
145 unsigned vdecl_count
;
146 SVGA3dElementLayoutId vdecl_layout_id
;
147 unsigned vdecl_buffer_index
[SVGA3D_INPUTREG_MAX
];
149 /* vertex buffer info */
150 struct pipe_vertex_buffer vbufs
[SVGA3D_INPUTREG_MAX
];
153 SVGA3dPrimitiveRange prim
[QSZ
];
154 struct pipe_resource
*prim_ib
[QSZ
];
155 unsigned prim_count
; /**< number of primitives for this draw */
156 unsigned min_index
[QSZ
];
157 unsigned max_index
[QSZ
];
160 #define IDX_CACHE_MAX 8
163 struct svga_context
*svga
;
164 struct u_upload_mgr
*upload_ib
;
166 /* Additional negative index bias due to partial buffer uploads
167 * This is compensated for in the offset associated with all
172 /* Provoking vertex information (for flat shading). */
173 unsigned api_pv
; /**< app-requested PV mode (PV_FIRST or PV_LAST) */
174 unsigned hw_pv
; /**< device-supported PV mode (PV_FIRST or PV_LAST) */
176 /* The triangle fillmode for the device (one of PIPE_POLYGON_MODE_{FILL,
177 * LINE,POINT}). If the polygon front mode matches the back mode,
178 * api_fillmode will be that mode. Otherwise, api_fillmode will be
179 * PIPE_POLYGON_MODE_FILL.
181 unsigned api_fillmode
;
183 /* Cache the results of running a particular generate func on each
186 struct index_cache index_cache
[PIPE_PRIM_MAX
][IDX_CACHE_MAX
];
188 /* Try to build the maximal draw command packet before emitting:
196 * Do we need to use the gallium 'indices' helper to render unfilled
199 static inline boolean
200 svga_need_unfilled_fallback(const struct svga_hwtnl
*hwtnl
,
201 enum pipe_prim_type prim
)
203 if (u_reduced_prim(prim
) != PIPE_PRIM_TRIANGLES
) {
204 /* if we're drawing points or lines, no fallback needed */
208 if ((prim
== PIPE_PRIM_QUADS
||
209 prim
== PIPE_PRIM_QUAD_STRIP
||
210 prim
== PIPE_PRIM_POLYGON
) &&
211 hwtnl
->api_fillmode
== PIPE_POLYGON_MODE_LINE
) {
212 /* We can't directly render quads or polygons. They're
213 * converted to triangles. If we let the device draw the triangle
214 * outlines we'll get an extra, stray lines in the interiors.
215 * So, to draw unfilled quads correctly, we need the fallback.
224 svga_hwtnl_prim(struct svga_hwtnl
*hwtnl
,
225 const SVGA3dPrimitiveRange
*range
,
229 struct pipe_resource
*ib
,
230 unsigned start_instance
, unsigned instance_count
,
231 const struct pipe_draw_indirect_info
*indirect
,
232 const struct pipe_stream_output_target
*so_vertex_count
);
235 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl
*hwtnl
,
236 struct pipe_resource
*indexBuffer
,
241 enum pipe_prim_type prim
,
244 unsigned start_instance
,
245 unsigned instance_count
,
246 ubyte vertices_per_patch
);