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
37struct svga_context;
38struct 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 */
45static 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
57
58/**
59 * Translate a gallium PIPE_PRIM_x value to an SVGA3D_PRIMITIVE_x value.
60 * Also, compute the number of primitives that'll be drawn given a
61 * vertex count.
62 * Note that this function doesn't have to handle PIPE_PRIM_LINE_LOOP,
63 * PIPE_PRIM_QUADS, PIPE_PRIM_QUAD_STRIP or PIPE_PRIM_POLYGON.  We convert
64 * those to other types of primitives with index/translation code.
65 */
66static inline SVGA3dPrimitiveType
67svga_translate_prim(unsigned mode, unsigned vcount, unsigned *prim_count)
68{
69   switch (mode) {
70   case PIPE_PRIM_POINTS:
71      *prim_count = vcount;
72      return SVGA3D_PRIMITIVE_POINTLIST;
73
74   case PIPE_PRIM_LINES:
75      *prim_count = vcount / 2;
76      return SVGA3D_PRIMITIVE_LINELIST;
77
78   case PIPE_PRIM_LINE_STRIP:
79      *prim_count = vcount - 1;
80      return SVGA3D_PRIMITIVE_LINESTRIP;
81
82   case PIPE_PRIM_TRIANGLES:
83      *prim_count = vcount / 3;
84      return SVGA3D_PRIMITIVE_TRIANGLELIST;
85
86   case PIPE_PRIM_TRIANGLE_STRIP:
87      *prim_count = vcount - 2;
88      return SVGA3D_PRIMITIVE_TRIANGLESTRIP;
89
90   case PIPE_PRIM_TRIANGLE_FAN:
91      *prim_count = vcount - 2;
92      return SVGA3D_PRIMITIVE_TRIANGLEFAN;
93
94   case PIPE_PRIM_LINES_ADJACENCY:
95      *prim_count = vcount / 4;
96      return SVGA3D_PRIMITIVE_LINELIST_ADJ;
97
98   case PIPE_PRIM_LINE_STRIP_ADJACENCY:
99      *prim_count = vcount - 3;
100      return SVGA3D_PRIMITIVE_LINESTRIP_ADJ;
101
102   case PIPE_PRIM_TRIANGLES_ADJACENCY:
103      *prim_count = vcount / 6;
104      return SVGA3D_PRIMITIVE_TRIANGLELIST_ADJ;
105
106   case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
107      *prim_count = vcount / 2 - 2 ;
108      return SVGA3D_PRIMITIVE_TRIANGLESTRIP_ADJ;
109
110   default:
111      assert(0);
112      *prim_count = 0;
113      return 0;
114   }
115}
116
117
118struct index_cache {
119   u_generate_func generate;
120   unsigned gen_nr;
121
122   /* If non-null, this buffer is filled by calling generate(nr, map(buffer))
123    */
124   struct pipe_resource *buffer;
125};
126
127
128/** Max number of primitives per draw call */
129#define QSZ SVGA3D_MAX_DRAW_PRIMITIVE_RANGES
130
131struct draw_cmd {
132   struct svga_winsys_context *swc;
133
134   /* vertex layout info */
135   SVGA3dVertexDecl vdecl[SVGA3D_INPUTREG_MAX];
136   unsigned vdecl_count;
137   SVGA3dElementLayoutId vdecl_layout_id;
138   unsigned vdecl_buffer_index[SVGA3D_INPUTREG_MAX];
139
140   /* vertex buffer info */
141   struct pipe_vertex_buffer vbufs[SVGA3D_INPUTREG_MAX];
142   unsigned vbuf_count;
143
144   SVGA3dPrimitiveRange prim[QSZ];
145   struct pipe_resource *prim_ib[QSZ];
146   unsigned prim_count;   /**< number of primitives for this draw */
147   unsigned min_index[QSZ];
148   unsigned max_index[QSZ];
149};
150
151#define IDX_CACHE_MAX  8
152
153struct svga_hwtnl {
154   struct svga_context *svga;
155   struct u_upload_mgr *upload_ib;
156
157   /* Additional negative index bias due to partial buffer uploads
158    * This is compensated for in the offset associated with all
159    * vertex buffers.
160    */
161   int index_bias;
162
163   /* Provoking vertex information (for flat shading). */
164   unsigned api_pv;  /**< app-requested PV mode (PV_FIRST or PV_LAST) */
165   unsigned hw_pv;   /**< device-supported PV mode (PV_FIRST or PV_LAST) */
166
167   /* The triangle fillmode for the device (one of PIPE_POLYGON_MODE_{FILL,
168    * LINE,POINT}).  If the polygon front mode matches the back mode,
169    * api_fillmode will be that mode.  Otherwise, api_fillmode will be
170    * PIPE_POLYGON_MODE_FILL.
171    */
172   unsigned api_fillmode;
173
174   /* Cache the results of running a particular generate func on each
175    * primitive type.
176    */
177   struct index_cache index_cache[PIPE_PRIM_MAX][IDX_CACHE_MAX];
178
179   /* Try to build the maximal draw command packet before emitting:
180    */
181   struct draw_cmd cmd;
182};
183
184
185
186/**
187 * Do we need to use the gallium 'indices' helper to render unfilled
188 * triangles?
189 */
190static inline boolean
191svga_need_unfilled_fallback(const struct svga_hwtnl *hwtnl,
192                            enum pipe_prim_type prim)
193{
194   if (u_reduced_prim(prim) != PIPE_PRIM_TRIANGLES) {
195      /* if we're drawing points or lines, no fallback needed */
196      return FALSE;
197   }
198
199   if ((prim == PIPE_PRIM_QUADS ||
200        prim == PIPE_PRIM_QUAD_STRIP ||
201        prim == PIPE_PRIM_POLYGON) &&
202       hwtnl->api_fillmode == PIPE_POLYGON_MODE_LINE) {
203      /* We can't directly render quads or polygons.  They're
204       * converted to triangles.  If we let the device draw the triangle
205       * outlines we'll get an extra, stray lines in the interiors.
206       * So, to draw unfilled quads correctly, we need the fallback.
207       */
208      return true;
209   }
210   return false;
211}
212
213
214enum pipe_error
215svga_hwtnl_prim(struct svga_hwtnl *hwtnl,
216                const SVGA3dPrimitiveRange *range,
217                unsigned vcount,
218                unsigned min_index,
219                unsigned max_index,
220                struct pipe_resource *ib,
221                unsigned start_instance, unsigned instance_count);
222
223enum pipe_error
224svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
225                                      struct pipe_resource *indexBuffer,
226                                      unsigned index_size,
227                                      int index_bias,
228                                      unsigned min_index,
229                                      unsigned max_index,
230                                      enum pipe_prim_type prim,
231                                      unsigned start,
232                                      unsigned count,
233                                      unsigned start_instance,
234                                      unsigned instance_count);
235
236#endif
237