1/**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * The rast code is concerned with rasterization of command bins.
30 * Each screen tile has a bin associated with it.  To render the
31 * scene we iterate over the tile bins and execute the commands
32 * in each bin.
33 * We'll do that with multiple threads...
34 */
35
36
37#ifndef LP_RAST_H
38#define LP_RAST_H
39
40#include "pipe/p_compiler.h"
41#include "util/u_pack_color.h"
42#include "lp_jit.h"
43
44
45struct lp_rasterizer;
46struct lp_scene;
47struct lp_fence;
48struct cmd_bin;
49
50#define FIXED_TYPE_WIDTH 64
51/** For sub-pixel positioning */
52#define FIXED_ORDER 8
53#define FIXED_ONE (1<<FIXED_ORDER)
54#define FIXED_SHIFT (FIXED_TYPE_WIDTH - 1)
55/** Maximum length of an edge in a primitive in pixels.
56 *  If the framebuffer is large we have to think about fixed-point
57 *  integer overflow. Coordinates need ((FIXED_TYPE_WIDTH/2) - 1) bits
58 *  to be able to fit product of two such coordinates inside
59 *  FIXED_TYPE_WIDTH, any larger and we could overflow a
60 *  FIXED_TYPE_WIDTH_-bit int.
61 */
62#define MAX_FIXED_LENGTH (1 << (((FIXED_TYPE_WIDTH/2) - 1) - FIXED_ORDER))
63
64#define MAX_FIXED_LENGTH32 (1 << (((32/2) - 1) - FIXED_ORDER))
65
66/* Rasterizer output size going to jit fs, width/height */
67#define LP_RASTER_BLOCK_SIZE 4
68
69#define LP_MAX_ACTIVE_BINNED_QUERIES 64
70
71#define IMUL64(a, b) (((int64_t)(a)) * ((int64_t)(b)))
72
73struct lp_rasterizer_task;
74
75
76/**
77 * Rasterization state.
78 * Objects of this type are put into the shared data bin and pointed
79 * to by commands in the per-tile bins.
80 */
81struct lp_rast_state {
82   /* State for the shader.  This also contains state which feeds into
83    * the fragment shader, such as blend color and alpha ref value.
84    */
85   struct lp_jit_context jit_context;
86
87   /* The shader itself.  Probably we also need to pass a pointer to
88    * the tile color/z/stencil data somehow
89     */
90   struct lp_fragment_shader_variant *variant;
91};
92
93
94/**
95 * Coefficients necessary to run the shader at a given location.
96 * First coefficient is position.
97 * These pointers point into the bin data buffer.
98 */
99struct lp_rast_shader_inputs {
100   unsigned frontfacing:1;      /** True for front-facing */
101   unsigned disable:1;          /** Partially binned, disable this command */
102   unsigned opaque:1;           /** Is opaque */
103   unsigned pad0:29;            /* wasted space */
104   unsigned stride;             /* how much to advance data between a0, dadx, dady */
105   unsigned layer;              /* the layer to render to (from gs, already clamped) */
106   unsigned viewport_index;     /* the active viewport index (from gs, already clamped) */
107   /* followed by a0, dadx, dady and planes[] */
108};
109
110struct lp_rast_plane {
111   /* edge function values at minx,miny ?? */
112   int64_t c;
113
114   int32_t dcdx;
115   int32_t dcdy;
116
117   /* one-pixel sized trivial reject offsets for each plane */
118   uint32_t eo;
119   /*
120    * We rely on this struct being 64bit aligned (ideally it would be 128bit
121    * but that's quite the waste) and therefore on 32bit we need padding
122    * since otherwise (even with the 64bit number in there) it wouldn't be.
123    */
124   uint32_t pad;
125};
126
127/**
128 * Rasterization information for a triangle known to be in this bin,
129 * plus inputs to run the shader:
130 * These fields are tile- and bin-independent.
131 * Objects of this type are put into the lp_setup_context::data buffer.
132 */
133struct lp_rast_triangle {
134#ifdef DEBUG
135   float v[3][2];
136   float pad0;
137   float pad1;
138#endif
139
140   /* inputs for the shader */
141   struct lp_rast_shader_inputs inputs;
142   /* planes are also allocated here */
143};
144
145
146struct lp_rast_clear_rb {
147   union util_color color_val;
148   unsigned cbuf;
149};
150
151
152#define GET_A0(inputs) ((float (*)[4])((inputs)+1))
153#define GET_DADX(inputs) ((float (*)[4])((char *)((inputs) + 1) + (inputs)->stride))
154#define GET_DADY(inputs) ((float (*)[4])((char *)((inputs) + 1) + 2 * (inputs)->stride))
155#define GET_PLANES(tri) ((struct lp_rast_plane *)((char *)(&(tri)->inputs + 1) + 3 * (tri)->inputs.stride))
156
157
158
159struct lp_rasterizer *
160lp_rast_create( unsigned num_threads );
161
162void
163lp_rast_destroy( struct lp_rasterizer * );
164
165void
166lp_rast_queue_scene( struct lp_rasterizer *rast,
167                     struct lp_scene *scene );
168
169void
170lp_rast_finish( struct lp_rasterizer *rast );
171
172
173union lp_rast_cmd_arg {
174   const struct lp_rast_shader_inputs *shade_tile;
175   struct {
176      const struct lp_rast_triangle *tri;
177      unsigned plane_mask;
178   } triangle;
179   const struct lp_rast_state *set_state;
180   const struct lp_rast_clear_rb *clear_rb;
181   struct {
182      uint64_t value;
183      uint64_t mask;
184   } clear_zstencil;
185   const struct lp_rast_state *state;
186   struct lp_fence *fence;
187   struct llvmpipe_query *query_obj;
188};
189
190
191/* Cast wrappers.  Hopefully these compile to noops!
192 */
193static inline union lp_rast_cmd_arg
194lp_rast_arg_inputs( const struct lp_rast_shader_inputs *shade_tile )
195{
196   union lp_rast_cmd_arg arg;
197   arg.shade_tile = shade_tile;
198   return arg;
199}
200
201static inline union lp_rast_cmd_arg
202lp_rast_arg_triangle( const struct lp_rast_triangle *triangle,
203                      unsigned plane_mask)
204{
205   union lp_rast_cmd_arg arg;
206   arg.triangle.tri = triangle;
207   arg.triangle.plane_mask = plane_mask;
208   return arg;
209}
210
211/**
212 * Build argument for a contained triangle.
213 *
214 * All planes are enabled, so instead of the plane mask we pass the upper
215 * left coordinates of the a block that fully encloses the triangle.
216 */
217static inline union lp_rast_cmd_arg
218lp_rast_arg_triangle_contained( const struct lp_rast_triangle *triangle,
219                                unsigned x, unsigned y)
220{
221   union lp_rast_cmd_arg arg;
222   arg.triangle.tri = triangle;
223   arg.triangle.plane_mask = x | (y << 8);
224   return arg;
225}
226
227static inline union lp_rast_cmd_arg
228lp_rast_arg_state( const struct lp_rast_state *state )
229{
230   union lp_rast_cmd_arg arg;
231   arg.set_state = state;
232   return arg;
233}
234
235static inline union lp_rast_cmd_arg
236lp_rast_arg_fence( struct lp_fence *fence )
237{
238   union lp_rast_cmd_arg arg;
239   arg.fence = fence;
240   return arg;
241}
242
243
244static inline union lp_rast_cmd_arg
245lp_rast_arg_clearzs( uint64_t value, uint64_t mask )
246{
247   union lp_rast_cmd_arg arg;
248   arg.clear_zstencil.value = value;
249   arg.clear_zstencil.mask = mask;
250   return arg;
251}
252
253
254static inline union lp_rast_cmd_arg
255lp_rast_arg_query( struct llvmpipe_query *pq )
256{
257   union lp_rast_cmd_arg arg;
258   arg.query_obj = pq;
259   return arg;
260}
261
262static inline union lp_rast_cmd_arg
263lp_rast_arg_null( void )
264{
265   union lp_rast_cmd_arg arg;
266   arg.set_state = NULL;
267   return arg;
268}
269
270
271/**
272 * Binnable Commands.
273 * These get put into bins by the setup code and are called when
274 * the bins are executed.
275 */
276#define LP_RAST_OP_CLEAR_COLOR       0x0
277#define LP_RAST_OP_CLEAR_ZSTENCIL    0x1
278#define LP_RAST_OP_TRIANGLE_1        0x2
279#define LP_RAST_OP_TRIANGLE_2        0x3
280#define LP_RAST_OP_TRIANGLE_3        0x4
281#define LP_RAST_OP_TRIANGLE_4        0x5
282#define LP_RAST_OP_TRIANGLE_5        0x6
283#define LP_RAST_OP_TRIANGLE_6        0x7
284#define LP_RAST_OP_TRIANGLE_7        0x8
285#define LP_RAST_OP_TRIANGLE_8        0x9
286#define LP_RAST_OP_TRIANGLE_3_4      0xa
287#define LP_RAST_OP_TRIANGLE_3_16     0xb
288#define LP_RAST_OP_TRIANGLE_4_16     0xc
289#define LP_RAST_OP_SHADE_TILE        0xd
290#define LP_RAST_OP_SHADE_TILE_OPAQUE 0xe
291#define LP_RAST_OP_BEGIN_QUERY       0xf
292#define LP_RAST_OP_END_QUERY         0x10
293#define LP_RAST_OP_SET_STATE         0x11
294#define LP_RAST_OP_TRIANGLE_32_1     0x12
295#define LP_RAST_OP_TRIANGLE_32_2     0x13
296#define LP_RAST_OP_TRIANGLE_32_3     0x14
297#define LP_RAST_OP_TRIANGLE_32_4     0x15
298#define LP_RAST_OP_TRIANGLE_32_5     0x16
299#define LP_RAST_OP_TRIANGLE_32_6     0x17
300#define LP_RAST_OP_TRIANGLE_32_7     0x18
301#define LP_RAST_OP_TRIANGLE_32_8     0x19
302#define LP_RAST_OP_TRIANGLE_32_3_4   0x1a
303#define LP_RAST_OP_TRIANGLE_32_3_16  0x1b
304#define LP_RAST_OP_TRIANGLE_32_4_16  0x1c
305
306#define LP_RAST_OP_MAX               0x1d
307#define LP_RAST_OP_MASK              0xff
308
309void
310lp_debug_bins( struct lp_scene *scene );
311void
312lp_debug_draw_bins_by_cmd_length( struct lp_scene *scene );
313void
314lp_debug_draw_bins_by_coverage( struct lp_scene *scene );
315
316
317#endif
318