Merge remote branch 'origin/7.8'
[mesa.git] / src / gallium / drivers / cell / spu / spu_main.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #ifndef SPU_MAIN_H
29 #define SPU_MAIN_H
30
31
32 #include <spu_mfcio.h>
33
34 #include "cell/common.h"
35 #include "draw/draw_vertex.h"
36 #include "pipe/p_state.h"
37
38
39 #if DEBUG
40 /* These debug macros use the unusual construction ", ##__VA_ARGS__"
41 * which expands to the expected comma + args if variadic arguments
42 * are supplied, but swallows the comma if there are no variadic
43 * arguments (which avoids syntax errors that would otherwise occur).
44 */
45 #define D_PRINTF(flag, format,...) \
46 if (spu.init.debug_flags & (flag)) \
47 printf("SPU %u: " format, spu.init.id, ##__VA_ARGS__)
48 #else
49 #define D_PRINTF(...)
50 #endif
51
52
53 /**
54 * A tile is basically a TILE_SIZE x TILE_SIZE block of 4-byte pixels.
55 * The data may be addressed through several different types.
56 */
57 typedef union {
58 ushort us[TILE_SIZE][TILE_SIZE];
59 uint ui[TILE_SIZE][TILE_SIZE];
60 vector unsigned short us8[TILE_SIZE/2][TILE_SIZE/4];
61 vector unsigned int ui4[TILE_SIZE/2][TILE_SIZE/2];
62 } tile_t;
63
64
65 #define TILE_STATUS_CLEAR 1
66 #define TILE_STATUS_DEFINED 2 /**< defined in FB, but not in local store */
67 #define TILE_STATUS_CLEAN 3 /**< in local store, but not changed */
68 #define TILE_STATUS_DIRTY 4 /**< modified locally, but not put back yet */
69 #define TILE_STATUS_GETTING 5 /**< mfc_get() called but not yet arrived */
70
71
72 /** Function for sampling textures */
73 typedef void (*spu_sample_texture_2d_func)(vector float s,
74 vector float t,
75 uint unit, uint level, uint face,
76 vector float colors[4]);
77
78
79 /** Function for performing per-fragment ops */
80 typedef void (*spu_fragment_ops_func)(uint x, uint y,
81 tile_t *colorTile,
82 tile_t *depthStencilTile,
83 vector float fragZ,
84 vector float fragRed,
85 vector float fragGreen,
86 vector float fragBlue,
87 vector float fragAlpha,
88 vector unsigned int mask);
89
90 /** Function for running fragment program */
91 typedef vector unsigned int (*spu_fragment_program_func)(vector float *inputs,
92 vector float *outputs,
93 vector float *constants);
94
95
96 PIPE_ALIGN_TYPE(16,
97 struct spu_framebuffer
98 {
99 void *color_start; /**< addr of color surface in main memory */
100 void *depth_start; /**< addr of depth surface in main memory */
101 enum pipe_format color_format;
102 enum pipe_format depth_format;
103 uint width; /**< width in pixels */
104 uint height; /**< height in pixels */
105 uint width_tiles; /**< width in tiles */
106 uint height_tiles; /**< width in tiles */
107
108 uint color_clear_value;
109 uint depth_clear_value;
110
111 uint zsize; /**< 0, 2 or 4 bytes per Z */
112 float zscale; /**< 65535.0, 2^24-1 or 2^32-1 */
113 });
114
115
116 /** per-texture level info */
117 PIPE_ALIGN_TYPE(16,
118 struct spu_texture_level
119 {
120 void *start;
121 ushort width;
122 ushort height;
123 ushort depth;
124 ushort tiles_per_row;
125 uint bytes_per_image;
126 /** texcoord scale factors */
127 vector float scale_s;
128 vector float scale_t;
129 vector float scale_r;
130 /** texcoord masks (if REPEAT then size-1, else ~0) */
131 vector signed int mask_s;
132 vector signed int mask_t;
133 vector signed int mask_r;
134 /** texcoord clamp limits */
135 vector signed int max_s;
136 vector signed int max_t;
137 vector signed int max_r;
138 });
139
140
141 PIPE_ALIGN_TYPE(16,
142 struct spu_texture
143 {
144 struct spu_texture_level level[CELL_MAX_TEXTURE_LEVELS];
145 uint max_level;
146 uint target; /**< PIPE_TEXTURE_x */
147 });
148
149
150 /**
151 * All SPU global/context state will be in a singleton object of this type:
152 */
153 PIPE_ALIGN_TYPE(16,
154 struct spu_global
155 {
156 /** One-time init/constant info */
157 struct cell_init_info init;
158
159 /*
160 * Current state
161 */
162 struct spu_framebuffer fb;
163 struct pipe_depth_stencil_alpha_state depth_stencil_alpha;
164 struct pipe_blend_state blend;
165 struct pipe_blend_color blend_color;
166 struct pipe_sampler_state sampler[PIPE_MAX_SAMPLERS];
167 struct pipe_rasterizer_state rasterizer;
168 struct spu_texture texture[PIPE_MAX_SAMPLERS];
169 struct vertex_info vertex_info;
170
171 /** Current color and Z tiles */
172 PIPE_ALIGN_VAR(16) tile_t ctile;
173 PIPE_ALIGN_VAR(16) tile_t ztile;
174
175 /** Read depth/stencil tiles? */
176 boolean read_depth_stencil;
177
178 /** Current tiles' status */
179 ubyte cur_ctile_status;
180 ubyte cur_ztile_status;
181
182 /** Status of all tiles in framebuffer */
183 PIPE_ALIGN_VAR(16) ubyte ctile_status[CELL_MAX_HEIGHT/TILE_SIZE][CELL_MAX_WIDTH/TILE_SIZE];
184 PIPE_ALIGN_VAR(16) ubyte ztile_status[CELL_MAX_HEIGHT/TILE_SIZE][CELL_MAX_WIDTH/TILE_SIZE];
185
186 /** Current fragment ops machine code, at 8-byte boundary */
187 uint *fragment_ops_code;
188 uint fragment_ops_code_size;
189 /** Current fragment ops functions, 0 = frontfacing, 1 = backfacing */
190 spu_fragment_ops_func fragment_ops[2];
191
192 /** Current fragment program machine code, at 8-byte boundary */
193 PIPE_ALIGN_VAR(8) uint fragment_program_code[SPU_MAX_FRAGMENT_PROGRAM_INSTS];
194 /** Current fragment ops function */
195 spu_fragment_program_func fragment_program;
196
197 /** Current texture sampler function */
198 spu_sample_texture_2d_func sample_texture_2d[CELL_MAX_SAMPLERS];
199 spu_sample_texture_2d_func min_sample_texture_2d[CELL_MAX_SAMPLERS];
200 spu_sample_texture_2d_func mag_sample_texture_2d[CELL_MAX_SAMPLERS];
201
202 /** Fragment program constants */
203 vector float constants[4 * CELL_MAX_CONSTANTS];
204
205 });
206
207
208 extern struct spu_global spu;
209
210
211
212 /* DMA TAGS */
213
214 #define TAG_SURFACE_CLEAR 10
215 #define TAG_VERTEX_BUFFER 11
216 #define TAG_READ_TILE_COLOR 12
217 #define TAG_READ_TILE_Z 13
218 #define TAG_WRITE_TILE_COLOR 14
219 #define TAG_WRITE_TILE_Z 15
220 #define TAG_INDEX_BUFFER 16
221 #define TAG_BATCH_BUFFER 17
222 #define TAG_MISC 18
223 #define TAG_DCACHE0 20
224 #define TAG_DCACHE1 21
225 #define TAG_DCACHE2 22
226 #define TAG_DCACHE3 23
227 #define TAG_FENCE 24
228
229
230 static INLINE void
231 wait_on_mask(unsigned tagMask)
232 {
233 mfc_write_tag_mask( tagMask );
234 /* wait for completion of _any_ DMAs specified by tagMask */
235 mfc_read_tag_status_any();
236 }
237
238
239 static INLINE void
240 wait_on_mask_all(unsigned tagMask)
241 {
242 mfc_write_tag_mask( tagMask );
243 /* wait for completion of _any_ DMAs specified by tagMask */
244 mfc_read_tag_status_all();
245 }
246
247
248
249
250
251 static INLINE void
252 memset16(ushort *d, ushort value, uint count)
253 {
254 uint i;
255 for (i = 0; i < count; i++)
256 d[i] = value;
257 }
258
259
260 static INLINE void
261 memset32(uint *d, uint value, uint count)
262 {
263 uint i;
264 for (i = 0; i < count; i++)
265 d[i] = value;
266 }
267
268
269 #endif /* SPU_MAIN_H */