1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2009 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg/**
29848b8605Smrg * The rast code is concerned with rasterization of command bins.
30848b8605Smrg * Each screen tile has a bin associated with it.  To render the
31848b8605Smrg * scene we iterate over the tile bins and execute the commands
32848b8605Smrg * in each bin.
33848b8605Smrg * We'll do that with multiple threads...
34848b8605Smrg */
35848b8605Smrg
36848b8605Smrg
37848b8605Smrg#ifndef LP_RAST_H
38848b8605Smrg#define LP_RAST_H
39848b8605Smrg
40848b8605Smrg#include "pipe/p_compiler.h"
41848b8605Smrg#include "util/u_pack_color.h"
42848b8605Smrg#include "lp_jit.h"
43848b8605Smrg
44848b8605Smrg
45848b8605Smrgstruct lp_rasterizer;
46848b8605Smrgstruct lp_scene;
47848b8605Smrgstruct lp_fence;
48848b8605Smrgstruct cmd_bin;
49848b8605Smrg
50848b8605Smrg#define FIXED_TYPE_WIDTH 64
51848b8605Smrg/** For sub-pixel positioning */
52848b8605Smrg#define FIXED_ORDER 8
53848b8605Smrg#define FIXED_ONE (1<<FIXED_ORDER)
54848b8605Smrg#define FIXED_SHIFT (FIXED_TYPE_WIDTH - 1)
55848b8605Smrg/** Maximum length of an edge in a primitive in pixels.
56848b8605Smrg *  If the framebuffer is large we have to think about fixed-point
57848b8605Smrg *  integer overflow. Coordinates need ((FIXED_TYPE_WIDTH/2) - 1) bits
58848b8605Smrg *  to be able to fit product of two such coordinates inside
59848b8605Smrg *  FIXED_TYPE_WIDTH, any larger and we could overflow a
60848b8605Smrg *  FIXED_TYPE_WIDTH_-bit int.
61848b8605Smrg */
62848b8605Smrg#define MAX_FIXED_LENGTH (1 << (((FIXED_TYPE_WIDTH/2) - 1) - FIXED_ORDER))
63848b8605Smrg
64848b8605Smrg#define MAX_FIXED_LENGTH32 (1 << (((32/2) - 1) - FIXED_ORDER))
65848b8605Smrg
66848b8605Smrg/* Rasterizer output size going to jit fs, width/height */
67848b8605Smrg#define LP_RASTER_BLOCK_SIZE 4
68848b8605Smrg
69848b8605Smrg#define LP_MAX_ACTIVE_BINNED_QUERIES 64
70848b8605Smrg
71848b8605Smrg#define IMUL64(a, b) (((int64_t)(a)) * ((int64_t)(b)))
72848b8605Smrg
73848b8605Smrgstruct lp_rasterizer_task;
74848b8605Smrg
75848b8605Smrg
76848b8605Smrg/**
77848b8605Smrg * Rasterization state.
78848b8605Smrg * Objects of this type are put into the shared data bin and pointed
79848b8605Smrg * to by commands in the per-tile bins.
80848b8605Smrg */
81848b8605Smrgstruct lp_rast_state {
82848b8605Smrg   /* State for the shader.  This also contains state which feeds into
83848b8605Smrg    * the fragment shader, such as blend color and alpha ref value.
84848b8605Smrg    */
85848b8605Smrg   struct lp_jit_context jit_context;
86848b8605Smrg
87848b8605Smrg   /* The shader itself.  Probably we also need to pass a pointer to
88848b8605Smrg    * the tile color/z/stencil data somehow
89848b8605Smrg     */
90848b8605Smrg   struct lp_fragment_shader_variant *variant;
91848b8605Smrg};
92848b8605Smrg
93848b8605Smrg
94848b8605Smrg/**
95848b8605Smrg * Coefficients necessary to run the shader at a given location.
96848b8605Smrg * First coefficient is position.
97848b8605Smrg * These pointers point into the bin data buffer.
98848b8605Smrg */
99848b8605Smrgstruct lp_rast_shader_inputs {
100848b8605Smrg   unsigned frontfacing:1;      /** True for front-facing */
101848b8605Smrg   unsigned disable:1;          /** Partially binned, disable this command */
102848b8605Smrg   unsigned opaque:1;           /** Is opaque */
103848b8605Smrg   unsigned pad0:29;            /* wasted space */
104848b8605Smrg   unsigned stride;             /* how much to advance data between a0, dadx, dady */
105848b8605Smrg   unsigned layer;              /* the layer to render to (from gs, already clamped) */
106848b8605Smrg   unsigned viewport_index;     /* the active viewport index (from gs, already clamped) */
107848b8605Smrg   /* followed by a0, dadx, dady and planes[] */
108848b8605Smrg};
109848b8605Smrg
110848b8605Smrgstruct lp_rast_plane {
111848b8605Smrg   /* edge function values at minx,miny ?? */
112848b8605Smrg   int64_t c;
113848b8605Smrg
114848b8605Smrg   int32_t dcdx;
115848b8605Smrg   int32_t dcdy;
116848b8605Smrg
117848b8605Smrg   /* one-pixel sized trivial reject offsets for each plane */
118b8e80941Smrg   uint32_t eo;
119b8e80941Smrg   /*
120b8e80941Smrg    * We rely on this struct being 64bit aligned (ideally it would be 128bit
121b8e80941Smrg    * but that's quite the waste) and therefore on 32bit we need padding
122b8e80941Smrg    * since otherwise (even with the 64bit number in there) it wouldn't be.
123b8e80941Smrg    */
124b8e80941Smrg   uint32_t pad;
125848b8605Smrg};
126848b8605Smrg
127848b8605Smrg/**
128848b8605Smrg * Rasterization information for a triangle known to be in this bin,
129848b8605Smrg * plus inputs to run the shader:
130848b8605Smrg * These fields are tile- and bin-independent.
131848b8605Smrg * Objects of this type are put into the lp_setup_context::data buffer.
132848b8605Smrg */
133848b8605Smrgstruct lp_rast_triangle {
134848b8605Smrg#ifdef DEBUG
135848b8605Smrg   float v[3][2];
136848b8605Smrg   float pad0;
137848b8605Smrg   float pad1;
138848b8605Smrg#endif
139848b8605Smrg
140848b8605Smrg   /* inputs for the shader */
141848b8605Smrg   struct lp_rast_shader_inputs inputs;
142848b8605Smrg   /* planes are also allocated here */
143848b8605Smrg};
144848b8605Smrg
145848b8605Smrg
146848b8605Smrgstruct lp_rast_clear_rb {
147848b8605Smrg   union util_color color_val;
148848b8605Smrg   unsigned cbuf;
149848b8605Smrg};
150848b8605Smrg
151848b8605Smrg
152848b8605Smrg#define GET_A0(inputs) ((float (*)[4])((inputs)+1))
153848b8605Smrg#define GET_DADX(inputs) ((float (*)[4])((char *)((inputs) + 1) + (inputs)->stride))
154848b8605Smrg#define GET_DADY(inputs) ((float (*)[4])((char *)((inputs) + 1) + 2 * (inputs)->stride))
155848b8605Smrg#define GET_PLANES(tri) ((struct lp_rast_plane *)((char *)(&(tri)->inputs + 1) + 3 * (tri)->inputs.stride))
156848b8605Smrg
157848b8605Smrg
158848b8605Smrg
159848b8605Smrgstruct lp_rasterizer *
160848b8605Smrglp_rast_create( unsigned num_threads );
161848b8605Smrg
162848b8605Smrgvoid
163848b8605Smrglp_rast_destroy( struct lp_rasterizer * );
164848b8605Smrg
165848b8605Smrgvoid
166848b8605Smrglp_rast_queue_scene( struct lp_rasterizer *rast,
167848b8605Smrg                     struct lp_scene *scene );
168848b8605Smrg
169848b8605Smrgvoid
170848b8605Smrglp_rast_finish( struct lp_rasterizer *rast );
171848b8605Smrg
172848b8605Smrg
173848b8605Smrgunion lp_rast_cmd_arg {
174848b8605Smrg   const struct lp_rast_shader_inputs *shade_tile;
175848b8605Smrg   struct {
176848b8605Smrg      const struct lp_rast_triangle *tri;
177848b8605Smrg      unsigned plane_mask;
178848b8605Smrg   } triangle;
179848b8605Smrg   const struct lp_rast_state *set_state;
180848b8605Smrg   const struct lp_rast_clear_rb *clear_rb;
181848b8605Smrg   struct {
182848b8605Smrg      uint64_t value;
183848b8605Smrg      uint64_t mask;
184848b8605Smrg   } clear_zstencil;
185848b8605Smrg   const struct lp_rast_state *state;
186848b8605Smrg   struct lp_fence *fence;
187848b8605Smrg   struct llvmpipe_query *query_obj;
188848b8605Smrg};
189848b8605Smrg
190848b8605Smrg
191848b8605Smrg/* Cast wrappers.  Hopefully these compile to noops!
192848b8605Smrg */
193b8e80941Smrgstatic inline union lp_rast_cmd_arg
194848b8605Smrglp_rast_arg_inputs( const struct lp_rast_shader_inputs *shade_tile )
195848b8605Smrg{
196848b8605Smrg   union lp_rast_cmd_arg arg;
197848b8605Smrg   arg.shade_tile = shade_tile;
198848b8605Smrg   return arg;
199848b8605Smrg}
200848b8605Smrg
201b8e80941Smrgstatic inline union lp_rast_cmd_arg
202848b8605Smrglp_rast_arg_triangle( const struct lp_rast_triangle *triangle,
203848b8605Smrg                      unsigned plane_mask)
204848b8605Smrg{
205848b8605Smrg   union lp_rast_cmd_arg arg;
206848b8605Smrg   arg.triangle.tri = triangle;
207848b8605Smrg   arg.triangle.plane_mask = plane_mask;
208848b8605Smrg   return arg;
209848b8605Smrg}
210848b8605Smrg
211848b8605Smrg/**
212848b8605Smrg * Build argument for a contained triangle.
213848b8605Smrg *
214848b8605Smrg * All planes are enabled, so instead of the plane mask we pass the upper
215848b8605Smrg * left coordinates of the a block that fully encloses the triangle.
216848b8605Smrg */
217b8e80941Smrgstatic inline union lp_rast_cmd_arg
218848b8605Smrglp_rast_arg_triangle_contained( const struct lp_rast_triangle *triangle,
219848b8605Smrg                                unsigned x, unsigned y)
220848b8605Smrg{
221848b8605Smrg   union lp_rast_cmd_arg arg;
222848b8605Smrg   arg.triangle.tri = triangle;
223848b8605Smrg   arg.triangle.plane_mask = x | (y << 8);
224848b8605Smrg   return arg;
225848b8605Smrg}
226848b8605Smrg
227b8e80941Smrgstatic inline union lp_rast_cmd_arg
228848b8605Smrglp_rast_arg_state( const struct lp_rast_state *state )
229848b8605Smrg{
230848b8605Smrg   union lp_rast_cmd_arg arg;
231848b8605Smrg   arg.set_state = state;
232848b8605Smrg   return arg;
233848b8605Smrg}
234848b8605Smrg
235b8e80941Smrgstatic inline union lp_rast_cmd_arg
236848b8605Smrglp_rast_arg_fence( struct lp_fence *fence )
237848b8605Smrg{
238848b8605Smrg   union lp_rast_cmd_arg arg;
239848b8605Smrg   arg.fence = fence;
240848b8605Smrg   return arg;
241848b8605Smrg}
242848b8605Smrg
243848b8605Smrg
244b8e80941Smrgstatic inline union lp_rast_cmd_arg
245848b8605Smrglp_rast_arg_clearzs( uint64_t value, uint64_t mask )
246848b8605Smrg{
247848b8605Smrg   union lp_rast_cmd_arg arg;
248848b8605Smrg   arg.clear_zstencil.value = value;
249848b8605Smrg   arg.clear_zstencil.mask = mask;
250848b8605Smrg   return arg;
251848b8605Smrg}
252848b8605Smrg
253848b8605Smrg
254b8e80941Smrgstatic inline union lp_rast_cmd_arg
255848b8605Smrglp_rast_arg_query( struct llvmpipe_query *pq )
256848b8605Smrg{
257848b8605Smrg   union lp_rast_cmd_arg arg;
258848b8605Smrg   arg.query_obj = pq;
259848b8605Smrg   return arg;
260848b8605Smrg}
261848b8605Smrg
262b8e80941Smrgstatic inline union lp_rast_cmd_arg
263848b8605Smrglp_rast_arg_null( void )
264848b8605Smrg{
265848b8605Smrg   union lp_rast_cmd_arg arg;
266848b8605Smrg   arg.set_state = NULL;
267848b8605Smrg   return arg;
268848b8605Smrg}
269848b8605Smrg
270848b8605Smrg
271848b8605Smrg/**
272848b8605Smrg * Binnable Commands.
273848b8605Smrg * These get put into bins by the setup code and are called when
274848b8605Smrg * the bins are executed.
275848b8605Smrg */
276848b8605Smrg#define LP_RAST_OP_CLEAR_COLOR       0x0
277848b8605Smrg#define LP_RAST_OP_CLEAR_ZSTENCIL    0x1
278848b8605Smrg#define LP_RAST_OP_TRIANGLE_1        0x2
279848b8605Smrg#define LP_RAST_OP_TRIANGLE_2        0x3
280848b8605Smrg#define LP_RAST_OP_TRIANGLE_3        0x4
281848b8605Smrg#define LP_RAST_OP_TRIANGLE_4        0x5
282848b8605Smrg#define LP_RAST_OP_TRIANGLE_5        0x6
283848b8605Smrg#define LP_RAST_OP_TRIANGLE_6        0x7
284848b8605Smrg#define LP_RAST_OP_TRIANGLE_7        0x8
285848b8605Smrg#define LP_RAST_OP_TRIANGLE_8        0x9
286848b8605Smrg#define LP_RAST_OP_TRIANGLE_3_4      0xa
287848b8605Smrg#define LP_RAST_OP_TRIANGLE_3_16     0xb
288848b8605Smrg#define LP_RAST_OP_TRIANGLE_4_16     0xc
289848b8605Smrg#define LP_RAST_OP_SHADE_TILE        0xd
290848b8605Smrg#define LP_RAST_OP_SHADE_TILE_OPAQUE 0xe
291848b8605Smrg#define LP_RAST_OP_BEGIN_QUERY       0xf
292848b8605Smrg#define LP_RAST_OP_END_QUERY         0x10
293848b8605Smrg#define LP_RAST_OP_SET_STATE         0x11
294848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_1     0x12
295848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_2     0x13
296848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_3     0x14
297848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_4     0x15
298848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_5     0x16
299848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_6     0x17
300848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_7     0x18
301848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_8     0x19
302848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_3_4   0x1a
303848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_3_16  0x1b
304848b8605Smrg#define LP_RAST_OP_TRIANGLE_32_4_16  0x1c
305848b8605Smrg
306848b8605Smrg#define LP_RAST_OP_MAX               0x1d
307848b8605Smrg#define LP_RAST_OP_MASK              0xff
308848b8605Smrg
309848b8605Smrgvoid
310848b8605Smrglp_debug_bins( struct lp_scene *scene );
311848b8605Smrgvoid
312848b8605Smrglp_debug_draw_bins_by_cmd_length( struct lp_scene *scene );
313848b8605Smrgvoid
314848b8605Smrglp_debug_draw_bins_by_coverage( struct lp_scene *scene );
315848b8605Smrg
316848b8605Smrg
317848b8605Smrg#endif
318