Merge branch '7.8'
[mesa.git] / src / gallium / drivers / cell / common.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 /**
29 * Types and tokens which are common to the SPU and PPU code.
30 */
31
32
33 #ifndef CELL_COMMON_H
34 #define CELL_COMMON_H
35
36 #include "pipe/p_compiler.h"
37 #include "pipe/p_format.h"
38 #include "pipe/p_state.h"
39 #include <stdio.h>
40
41 /** The standard assert macro doesn't seem to work reliably */
42 #define ASSERT(x) \
43 if (!(x)) { \
44 ubyte *p = NULL; \
45 fprintf(stderr, "%s:%d: %s(): assertion %s failed.\n", \
46 __FILE__, __LINE__, __FUNCTION__, #x); \
47 *p = 0; \
48 exit(1); \
49 }
50
51
52 #define JOIN(x, y) JOIN_AGAIN(x, y)
53 #define JOIN_AGAIN(x, y) x ## y
54
55 #define STATIC_ASSERT(e) \
56 {typedef char JOIN(assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1];}
57
58
59
60 /** for sanity checking */
61 #define ASSERT_ALIGN16(ptr) \
62 ASSERT((((unsigned long) (ptr)) & 0xf) == 0);
63
64
65 /** round up value to next multiple of 4 */
66 #define ROUNDUP4(k) (((k) + 0x3) & ~0x3)
67
68 /** round up value to next multiple of 8 */
69 #define ROUNDUP8(k) (((k) + 0x7) & ~0x7)
70
71 /** round up value to next multiple of 16 */
72 #define ROUNDUP16(k) (((k) + 0xf) & ~0xf)
73
74
75 #define CELL_MAX_SPUS 8
76
77 #define CELL_MAX_SAMPLERS 4
78 #define CELL_MAX_TEXTURE_LEVELS 12 /* 2k x 2k */
79 #define CELL_MAX_CONSTANTS 32 /**< number of float[4] constants */
80 #define CELL_MAX_WIDTH 1024 /**< max framebuffer width */
81 #define CELL_MAX_HEIGHT 1024 /**< max framebuffer width */
82
83 #define TILE_SIZE 32
84
85
86 /**
87 * The low byte of a mailbox word contains the command opcode.
88 * Remaining higher bytes are command specific.
89 */
90 #define CELL_CMD_OPCODE_MASK 0xff
91
92 #define CELL_CMD_EXIT 1
93 #define CELL_CMD_CLEAR_SURFACE 2
94 #define CELL_CMD_FINISH 3
95 #define CELL_CMD_RENDER 4
96 #define CELL_CMD_BATCH 5
97 #define CELL_CMD_RELEASE_VERTS 6
98 #define CELL_CMD_STATE_FRAMEBUFFER 10
99 #define CELL_CMD_STATE_FRAGMENT_OPS 11
100 #define CELL_CMD_STATE_SAMPLER 12
101 #define CELL_CMD_STATE_TEXTURE 13
102 #define CELL_CMD_STATE_VERTEX_INFO 14
103 #define CELL_CMD_STATE_VIEWPORT 15
104 #define CELL_CMD_STATE_UNIFORMS 16
105 #define CELL_CMD_STATE_VS_ARRAY_INFO 17
106 #define CELL_CMD_STATE_BIND_VS 18
107 #define CELL_CMD_STATE_FRAGMENT_PROGRAM 19
108 #define CELL_CMD_STATE_ATTRIB_FETCH 20
109 #define CELL_CMD_STATE_FS_CONSTANTS 21
110 #define CELL_CMD_STATE_RASTERIZER 22
111 #define CELL_CMD_VS_EXECUTE 23
112 #define CELL_CMD_FLUSH_BUFFER_RANGE 24
113 #define CELL_CMD_FENCE 25
114
115
116 /** Command/batch buffers */
117 #define CELL_NUM_BUFFERS 4
118 #define CELL_BUFFER_SIZE (4*1024) /**< 16KB would be the max */
119
120 #define CELL_BUFFER_STATUS_FREE 10
121 #define CELL_BUFFER_STATUS_USED 20
122
123 /** Debug flags */
124 #define CELL_DEBUG_CHECKER (1 << 0)
125 #define CELL_DEBUG_ASM (1 << 1)
126 #define CELL_DEBUG_SYNC (1 << 2)
127 #define CELL_DEBUG_FRAGMENT_OPS (1 << 3)
128 #define CELL_DEBUG_FRAGMENT_OP_FALLBACK (1 << 4)
129 #define CELL_DEBUG_CMD (1 << 5)
130 #define CELL_DEBUG_CACHE (1 << 6)
131
132 #define CELL_FENCE_IDLE 0
133 #define CELL_FENCE_EMITTED 1
134 #define CELL_FENCE_SIGNALLED 2
135
136 #define CELL_FACING_FRONT 0
137 #define CELL_FACING_BACK 1
138
139 struct cell_fence
140 {
141 /** There's a 16-byte status qword per SPU */
142 volatile uint status[CELL_MAX_SPUS][4];
143 };
144
145 #ifdef __SPU__
146 typedef vector unsigned int opcode_t;
147 #else
148 typedef unsigned int opcode_t[4];
149 #endif
150
151 /**
152 * Fence command sent to SPUs. In response, the SPUs will write
153 * CELL_FENCE_STATUS_SIGNALLED back to the fence status word in main memory.
154 */
155 struct cell_command_fence
156 {
157 opcode_t opcode; /**< CELL_CMD_FENCE */
158 struct cell_fence *fence;
159 uint32_t pad_[3];
160 };
161
162
163 /**
164 * Command to specify per-fragment operations state and generated code.
165 * Note that this is a variant-length structure, allocated with as
166 * much memory as needed to hold the generated code; the "code"
167 * field *must* be the last field in the structure. Also, the entire
168 * length of the structure (including the variant code field) must be
169 * a multiple of 8 bytes; we require that this structure itself be
170 * a multiple of 8 bytes, and that the generated code also be a multiple
171 * of 8 bytes.
172 *
173 * Also note that the dsa, blend, blend_color fields are really only needed
174 * for the fallback/C per-pixel code. They're not used when we generate
175 * dynamic SPU fragment code (which is the normal case), and will eventually
176 * be removed from this structure.
177 */
178 struct cell_command_fragment_ops
179 {
180 opcode_t opcode; /**< CELL_CMD_STATE_FRAGMENT_OPS */
181
182 /* Fields for the fallback case */
183 struct pipe_depth_stencil_alpha_state dsa;
184 struct pipe_blend_state blend;
185 struct pipe_blend_color blend_color;
186
187 /* Fields for the generated SPU code */
188 unsigned total_code_size;
189 unsigned front_code_index;
190 unsigned back_code_index;
191 /* this field has variant length, and must be the last field in
192 * the structure
193 */
194 unsigned code[0];
195 };
196
197
198 /** Max instructions for fragment programs */
199 #define SPU_MAX_FRAGMENT_PROGRAM_INSTS 512
200
201 /**
202 * Command to send a fragment program to SPUs.
203 */
204 struct cell_command_fragment_program
205 {
206 opcode_t opcode; /**< CELL_CMD_STATE_FRAGMENT_PROGRAM */
207 uint num_inst; /**< Number of instructions */
208 uint32_t pad[3];
209 unsigned code[SPU_MAX_FRAGMENT_PROGRAM_INSTS];
210 };
211
212
213 /**
214 * Tell SPUs about the framebuffer size, location
215 */
216 struct cell_command_framebuffer
217 {
218 opcode_t opcode; /**< CELL_CMD_STATE_FRAMEBUFFER */
219 int width, height;
220 void *color_start, *depth_start;
221 enum pipe_format color_format, depth_format;
222 uint32_t pad_[2];
223 };
224
225
226 /**
227 * Tell SPUs about rasterizer state.
228 */
229 struct cell_command_rasterizer
230 {
231 opcode_t opcode; /**< CELL_CMD_STATE_RASTERIZER */
232 struct pipe_rasterizer_state rasterizer;
233 uint32_t pad[1];
234 };
235
236
237 /**
238 * Clear framebuffer to the given value/color.
239 */
240 struct cell_command_clear_surface
241 {
242 opcode_t opcode; /**< CELL_CMD_CLEAR_SURFACE */
243 uint surface; /**< Temporary: 0=color, 1=Z */
244 uint value;
245 uint32_t pad[2];
246 };
247
248
249 /**
250 * Array info used by the vertex shader's vertex puller.
251 */
252 struct cell_array_info
253 {
254 uint64_t base; /**< Base address of the 0th element. */
255 uint attr; /**< Attribute that this state is for. */
256 uint pitch; /**< Byte pitch from one entry to the next. */
257 uint size;
258 uint function_offset;
259 };
260
261
262 struct cell_attribute_fetch_code
263 {
264 uint64_t base;
265 uint size;
266 };
267
268
269 struct cell_buffer_range
270 {
271 uint64_t base;
272 unsigned size;
273 };
274
275
276 struct cell_shader_info
277 {
278 uint64_t declarations;
279 uint64_t instructions;
280 uint64_t immediates;
281
282 unsigned num_outputs;
283 unsigned num_declarations;
284 unsigned num_instructions;
285 unsigned num_immediates;
286 };
287
288
289 #define SPU_VERTS_PER_BATCH 64
290 struct cell_command_vs
291 {
292 opcode_t opcode; /**< CELL_CMD_VS_EXECUTE */
293 uint64_t vOut[SPU_VERTS_PER_BATCH];
294 unsigned num_elts;
295 unsigned elts[SPU_VERTS_PER_BATCH];
296 float plane[12][4];
297 unsigned nr_planes;
298 unsigned nr_attrs;
299 };
300
301
302 struct cell_command_render
303 {
304 opcode_t opcode; /**< CELL_CMD_RENDER */
305 uint prim_type; /**< PIPE_PRIM_x */
306 uint num_verts;
307 uint vertex_size; /**< bytes per vertex */
308 uint num_indexes;
309 uint vertex_buf; /**< which cell->buffer[] contains the vertex data */
310 float xmin, ymin, xmax, ymax; /* XXX another dummy field */
311 uint min_index;
312 boolean inline_verts;
313 uint32_t pad_[1];
314 };
315
316
317 struct cell_command_release_verts
318 {
319 opcode_t opcode; /**< CELL_CMD_RELEASE_VERTS */
320 uint vertex_buf; /**< in [0, CELL_NUM_BUFFERS-1] */
321 uint32_t pad_[3];
322 };
323
324
325 struct cell_command_sampler
326 {
327 opcode_t opcode; /**< CELL_CMD_STATE_SAMPLER */
328 uint unit;
329 struct pipe_sampler_state state;
330 uint32_t pad_[3];
331 };
332
333
334 struct cell_command_texture
335 {
336 opcode_t opcode; /**< CELL_CMD_STATE_TEXTURE */
337 uint target; /**< PIPE_TEXTURE_x */
338 uint unit;
339 void *start[CELL_MAX_TEXTURE_LEVELS]; /**< Address in main memory */
340 ushort width[CELL_MAX_TEXTURE_LEVELS];
341 ushort height[CELL_MAX_TEXTURE_LEVELS];
342 ushort depth[CELL_MAX_TEXTURE_LEVELS];
343 };
344
345
346 #define MAX_SPU_FUNCTIONS 12
347 /**
348 * Used to tell the PPU about the address of particular functions in the
349 * SPU's address space.
350 */
351 struct cell_spu_function_info
352 {
353 uint num;
354 char names[MAX_SPU_FUNCTIONS][16];
355 uint addrs[MAX_SPU_FUNCTIONS];
356 char pad[12]; /**< Pad struct to multiple of 16 bytes (256 currently) */
357 };
358
359
360 /** This is the object passed to spe_create_thread() */
361 PIPE_ALIGN_TYPE(16,
362 struct cell_init_info
363 {
364 unsigned id;
365 unsigned num_spus;
366 unsigned debug_flags; /**< mask of CELL_DEBUG_x flags */
367 float inv_timebase; /**< 1.0/timebase, for perf measurement */
368
369 /** Buffers for command batches, vertex/index data */
370 ubyte *buffers[CELL_NUM_BUFFERS];
371 uint *buffer_status; /**< points at cell_context->buffer_status */
372
373 struct cell_spu_function_info *spu_functions;
374 });
375
376
377 #endif /* CELL_COMMON_H */