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 "svga_hw_reg.h"
33 #include "svga3d_shaderdefs.h"
34 
35 struct svga_context;
36 struct u_upload_mgr;
37 
38 /* Should include polygon?
39  */
40 static const unsigned svga_hw_prims =
41    ((1 << PIPE_PRIM_POINTS) |
42     (1 << PIPE_PRIM_LINES) |
43     (1 << PIPE_PRIM_LINE_STRIP) |
44     (1 << PIPE_PRIM_TRIANGLES) |
45     (1 << PIPE_PRIM_TRIANGLE_STRIP) |
46     (1 << PIPE_PRIM_TRIANGLE_FAN));
47 
48 
svga_translate_prim(unsigned mode,unsigned count,unsigned * out_count)49 static INLINE unsigned svga_translate_prim(unsigned mode,
50                                            unsigned count,
51                                            unsigned *out_count)
52 {
53    switch (mode) {
54    case PIPE_PRIM_POINTS:
55       *out_count = count;
56       return SVGA3D_PRIMITIVE_POINTLIST;
57 
58    case PIPE_PRIM_LINES:
59       *out_count = count / 2;
60       return SVGA3D_PRIMITIVE_LINELIST;
61 
62    case PIPE_PRIM_LINE_STRIP:
63       *out_count = count - 1;
64       return SVGA3D_PRIMITIVE_LINESTRIP;
65 
66    case PIPE_PRIM_TRIANGLES:
67       *out_count = count / 3;
68       return SVGA3D_PRIMITIVE_TRIANGLELIST;
69 
70    case PIPE_PRIM_TRIANGLE_STRIP:
71       *out_count = count - 2;
72       return SVGA3D_PRIMITIVE_TRIANGLESTRIP;
73 
74    case PIPE_PRIM_TRIANGLE_FAN:
75       *out_count = count - 2;
76       return SVGA3D_PRIMITIVE_TRIANGLEFAN;
77 
78    default:
79       assert(0);
80       *out_count = 0;
81       return 0;
82    }
83 }
84 
85 
86 struct index_cache {
87    u_generate_func generate;
88    unsigned gen_nr;
89 
90    /* If non-null, this buffer is filled by calling
91     *   generate(nr, map(buffer))
92     */
93    struct pipe_resource *buffer;
94 };
95 
96 
97 /** Max number of primitives per draw call */
98 #define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
99 
100 struct draw_cmd {
101    struct svga_winsys_context *swc;
102 
103    SVGA3dVertexDecl vdecl[SVGA3D_INPUTREG_MAX];
104    struct pipe_resource *vdecl_vb[SVGA3D_INPUTREG_MAX];
105    unsigned vdecl_count;
106 
107    SVGA3dPrimitiveRange prim[QSZ];
108    struct pipe_resource *prim_ib[QSZ];
109    unsigned prim_count;
110    unsigned min_index[QSZ];
111    unsigned max_index[QSZ];
112 };
113 
114 #define IDX_CACHE_MAX  8
115 
116 struct svga_hwtnl {
117    struct svga_context *svga;
118    struct u_upload_mgr *upload_ib;
119 
120    /* Additional negative index bias due to partial buffer uploads
121     * This is compensated for in the offset associated with all
122     * vertex buffers.
123     */
124 
125    int index_bias;
126 
127    /* Flatshade information:
128     */
129    unsigned api_pv;
130    unsigned hw_pv;
131    unsigned api_fillmode;
132 
133    /* Cache the results of running a particular generate func on each
134     * primitive type.
135     */
136    struct index_cache index_cache[PIPE_PRIM_MAX][IDX_CACHE_MAX];
137 
138    /* Try to build the maximal draw command packet before emitting:
139     */
140    struct draw_cmd cmd;
141 };
142 
143 
144 
145 /***********************************************************************
146  * Internal functions
147  */
148 enum pipe_error
149 svga_hwtnl_prim( struct svga_hwtnl *hwtnl,
150                  const SVGA3dPrimitiveRange *range,
151                  unsigned min_index,
152                  unsigned max_index,
153                  struct pipe_resource *ib );
154 
155 enum pipe_error
156 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
157                                        struct pipe_resource *indexBuffer,
158                                        unsigned index_size,
159                                        int index_bias,
160                                        unsigned min_index,
161                                        unsigned max_index,
162                                        unsigned prim,
163                                        unsigned start,
164                                        unsigned count );
165 
166 
167 #endif
168