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 #ifndef SVGA_DRAW_H_
27 #define SVGA_DRAW_H_
28 
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"
36 
37 struct svga_context;
38 struct u_upload_mgr;
39 
40 /**
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.
44  */
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));
57 
58 
59 /**
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
62  * vertex count.
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.
66  */
67 static inline SVGA3dPrimitiveType
svga_translate_prim(unsigned mode,unsigned vcount,unsigned * prim_count,ubyte vertices_per_patch)68 svga_translate_prim(unsigned mode, unsigned vcount, unsigned *prim_count,
69                     ubyte vertices_per_patch)
70 {
71    switch (mode) {
72    case PIPE_PRIM_POINTS:
73       *prim_count = vcount;
74       return SVGA3D_PRIMITIVE_POINTLIST;
75 
76    case PIPE_PRIM_LINES:
77       *prim_count = vcount / 2;
78       return SVGA3D_PRIMITIVE_LINELIST;
79 
80    case PIPE_PRIM_LINE_STRIP:
81       *prim_count = vcount - 1;
82       return SVGA3D_PRIMITIVE_LINESTRIP;
83 
84    case PIPE_PRIM_TRIANGLES:
85       *prim_count = vcount / 3;
86       return SVGA3D_PRIMITIVE_TRIANGLELIST;
87 
88    case PIPE_PRIM_TRIANGLE_STRIP:
89       *prim_count = vcount - 2;
90       return SVGA3D_PRIMITIVE_TRIANGLESTRIP;
91 
92    case PIPE_PRIM_TRIANGLE_FAN:
93       *prim_count = vcount - 2;
94       return SVGA3D_PRIMITIVE_TRIANGLEFAN;
95 
96    case PIPE_PRIM_LINES_ADJACENCY:
97       *prim_count = vcount / 4;
98       return SVGA3D_PRIMITIVE_LINELIST_ADJ;
99 
100    case PIPE_PRIM_LINE_STRIP_ADJACENCY:
101       *prim_count = vcount - 3;
102       return SVGA3D_PRIMITIVE_LINESTRIP_ADJ;
103 
104    case PIPE_PRIM_TRIANGLES_ADJACENCY:
105       *prim_count = vcount / 6;
106       return SVGA3D_PRIMITIVE_TRIANGLELIST_ADJ;
107 
108    case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
109       *prim_count = vcount / 2 - 2 ;
110       return SVGA3D_PRIMITIVE_TRIANGLESTRIP_ADJ;
111 
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;
118 
119    default:
120       assert(0);
121       *prim_count = 0;
122       return 0;
123    }
124 }
125 
126 
127 struct index_cache {
128    u_generate_func generate;
129    unsigned gen_nr;
130 
131    /* If non-null, this buffer is filled by calling generate(nr, map(buffer))
132     */
133    struct pipe_resource *buffer;
134 };
135 
136 
137 /** Max number of primitives per draw call */
138 #define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
139 
140 struct draw_cmd {
141    struct svga_winsys_context *swc;
142 
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];
148 
149    /* vertex buffer info */
150    struct pipe_vertex_buffer vbufs[SVGA3D_INPUTREG_MAX];
151    unsigned vbuf_count;
152 
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];
158 };
159 
160 #define IDX_CACHE_MAX  8
161 
162 struct svga_hwtnl {
163    struct svga_context *svga;
164    struct u_upload_mgr *upload_ib;
165 
166    /* Additional negative index bias due to partial buffer uploads
167     * This is compensated for in the offset associated with all
168     * vertex buffers.
169     */
170    int index_bias;
171 
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) */
175 
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.
180     */
181    unsigned api_fillmode;
182 
183    /* Cache the results of running a particular generate func on each
184     * primitive type.
185     */
186    struct index_cache index_cache[PIPE_PRIM_MAX][IDX_CACHE_MAX];
187 
188    /* Try to build the maximal draw command packet before emitting:
189     */
190    struct draw_cmd cmd;
191 };
192 
193 
194 
195 /**
196  * Do we need to use the gallium 'indices' helper to render unfilled
197  * triangles?
198  */
199 static inline boolean
svga_need_unfilled_fallback(const struct svga_hwtnl * hwtnl,enum pipe_prim_type prim)200 svga_need_unfilled_fallback(const struct svga_hwtnl *hwtnl,
201                             enum pipe_prim_type prim)
202 {
203    if (u_reduced_prim(prim) != PIPE_PRIM_TRIANGLES) {
204       /* if we're drawing points or lines, no fallback needed */
205       return FALSE;
206    }
207 
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.
216        */
217       return true;
218    }
219    return false;
220 }
221 
222 
223 enum pipe_error
224 svga_hwtnl_prim(struct svga_hwtnl *hwtnl,
225                 const SVGA3dPrimitiveRange *range,
226                 unsigned vcount,
227                 unsigned min_index,
228                 unsigned max_index,
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);
233 
234 enum pipe_error
235 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
236                                       struct pipe_resource *indexBuffer,
237                                       unsigned index_size,
238                                       int index_bias,
239                                       unsigned min_index,
240                                       unsigned max_index,
241                                       enum pipe_prim_type prim,
242                                       unsigned start,
243                                       unsigned count,
244                                       unsigned start_instance,
245                                       unsigned instance_count,
246                                       ubyte vertices_per_patch);
247 
248 #endif
249