1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "pipe/p_inlines.h"
29 #include "util/u_memory.h"
30 #include "cell_context.h"
31 #include "cell_gen_fragment.h"
32 #include "cell_state.h"
33 #include "cell_state_emit.h"
34 #include "cell_batch.h"
35 #include "cell_texture.h"
36 #include "draw/draw_context.h"
37 #include "draw/draw_private.h"
41 * Find/create a cell_command_fragment_ops object corresponding to the
42 * current blend/stencil/z/colormask/etc. state.
44 static struct cell_command_fragment_ops
*
45 lookup_fragment_ops(struct cell_context
*cell
)
47 struct cell_fragment_ops_key key
;
48 struct cell_command_fragment_ops
*ops
;
53 memset(&key
, 0, sizeof(key
));
54 key
.blend
= *cell
->blend
;
55 key
.blend_color
= cell
->blend_color
;
56 key
.dsa
= *cell
->depth_stencil
;
58 if (cell
->framebuffer
.cbufs
[0])
59 key
.color_format
= cell
->framebuffer
.cbufs
[0]->format
;
61 key
.color_format
= PIPE_FORMAT_NONE
;
63 if (cell
->framebuffer
.zsbuf
)
64 key
.zs_format
= cell
->framebuffer
.zsbuf
->format
;
66 key
.zs_format
= PIPE_FORMAT_NONE
;
69 * Look up key in cache.
71 ops
= (struct cell_command_fragment_ops
*)
72 util_keymap_lookup(cell
->fragment_ops_cache
, &key
);
75 * If not found, create/save new fragment ops command.
78 struct spe_function spe_code_front
, spe_code_back
;
79 unsigned int facing_dependent
, total_code_size
;
82 debug_printf("**** Create New Fragment Ops\n");
84 /* Prepare the buffer that will hold the generated code. The
85 * "0" passed in for the size means that the SPE code will
88 spe_init_func(&spe_code_front
, 0);
89 spe_init_func(&spe_code_back
, 0);
91 /* Generate new code. Always generate new code for both front-facing
92 * and back-facing fragments, even if it's the same code in both
95 cell_gen_fragment_function(cell
, CELL_FACING_FRONT
, &spe_code_front
);
96 cell_gen_fragment_function(cell
, CELL_FACING_BACK
, &spe_code_back
);
98 /* Make sure the code is a multiple of 8 bytes long; this is
99 * required to ensure that the dual pipe instruction alignment
100 * is correct. It's also important for the SPU unpacking,
101 * which assumes 8-byte boundaries.
103 unsigned int front_code_size
= spe_code_size(&spe_code_front
);
104 while (front_code_size
% 8 != 0) {
105 spe_lnop(&spe_code_front
);
106 front_code_size
= spe_code_size(&spe_code_front
);
108 unsigned int back_code_size
= spe_code_size(&spe_code_back
);
109 while (back_code_size
% 8 != 0) {
110 spe_lnop(&spe_code_back
);
111 back_code_size
= spe_code_size(&spe_code_back
);
114 /* Determine whether the code we generated is facing-dependent, by
115 * determining whether the generated code is different for the front-
116 * and back-facing fragments.
118 if (front_code_size
== back_code_size
&& memcmp(spe_code_front
.store
, spe_code_back
.store
, front_code_size
) == 0) {
119 /* Code is identical; only need one copy. */
120 facing_dependent
= 0;
121 total_code_size
= front_code_size
;
124 /* Code is different for front-facing and back-facing fragments.
125 * Need to send both copies.
127 facing_dependent
= 1;
128 total_code_size
= front_code_size
+ back_code_size
;
131 /* alloc new fragment ops command. Note that this structure
132 * has variant length based on the total code size required.
134 ops
= CALLOC_VARIANT_LENGTH_STRUCT(cell_command_fragment_ops
, total_code_size
);
135 /* populate the new cell_command_fragment_ops object */
136 ops
->opcode
= CELL_CMD_STATE_FRAGMENT_OPS
;
137 ops
->total_code_size
= total_code_size
;
138 ops
->front_code_index
= 0;
139 memcpy(ops
->code
, spe_code_front
.store
, front_code_size
);
140 if (facing_dependent
) {
141 /* We have separate front- and back-facing code. Append the
142 * back-facing code to the buffer. Be careful because the code
143 * size is in bytes, but the buffer is of unsigned elements.
145 ops
->back_code_index
= front_code_size
/ sizeof(spe_code_front
.store
[0]);
146 memcpy(ops
->code
+ ops
->back_code_index
, spe_code_back
.store
, back_code_size
);
149 /* Use the same code for front- and back-facing fragments */
150 ops
->back_code_index
= ops
->front_code_index
;
153 /* Set the fields for the fallback case. Note that these fields
154 * (and the whole fallback case) will eventually go away.
156 ops
->dsa
= *cell
->depth_stencil
;
157 ops
->blend
= *cell
->blend
;
158 ops
->blend_color
= cell
->blend_color
;
160 /* insert cell_command_fragment_ops object into keymap/cache */
161 util_keymap_insert(cell
->fragment_ops_cache
, &key
, ops
, NULL
);
163 /* release rtasm buffer */
164 spe_release_func(&spe_code_front
);
165 spe_release_func(&spe_code_back
);
169 debug_printf("**** Re-use Fragment Ops\n");
178 emit_state_cmd(struct cell_context
*cell
, uint cmd
,
179 const void *state
, uint state_size
)
181 uint64_t *dst
= (uint64_t *)
182 cell_batch_alloc(cell
, ROUNDUP8(sizeof(uint64_t) + state_size
));
184 memcpy(dst
+ 1, state
, state_size
);
189 * For state marked as 'dirty', construct a state-update command block
190 * and insert it into the current batch buffer.
193 cell_emit_state(struct cell_context
*cell
)
195 if (cell
->dirty
& CELL_NEW_FRAMEBUFFER
) {
196 struct pipe_surface
*cbuf
= cell
->framebuffer
.cbufs
[0];
197 struct pipe_surface
*zbuf
= cell
->framebuffer
.zsbuf
;
198 struct cell_command_framebuffer
*fb
199 = cell_batch_alloc(cell
, sizeof(*fb
));
200 fb
->opcode
= CELL_CMD_STATE_FRAMEBUFFER
;
201 fb
->color_start
= cell
->cbuf_map
[0];
202 fb
->color_format
= cbuf
->format
;
203 fb
->depth_start
= cell
->zsbuf_map
;
204 fb
->depth_format
= zbuf
? zbuf
->format
: PIPE_FORMAT_NONE
;
205 fb
->width
= cell
->framebuffer
.width
;
206 fb
->height
= cell
->framebuffer
.height
;
208 printf("EMIT color format %s\n", pf_name(fb
->color_format
));
209 printf("EMIT depth format %s\n", pf_name(fb
->depth_format
));
213 if (cell
->dirty
& (CELL_NEW_RASTERIZER
)) {
214 struct cell_command_rasterizer
*rast
=
215 cell_batch_alloc(cell
, sizeof(*rast
));
216 rast
->opcode
= CELL_CMD_STATE_RASTERIZER
;
217 rast
->rasterizer
= *cell
->rasterizer
;
220 if (cell
->dirty
& (CELL_NEW_FS
)) {
221 /* Send new fragment program to SPUs */
222 struct cell_command_fragment_program
*fp
223 = cell_batch_alloc(cell
, sizeof(*fp
));
224 fp
->opcode
= CELL_CMD_STATE_FRAGMENT_PROGRAM
;
225 fp
->num_inst
= cell
->fs
->code
.num_inst
;
226 memcpy(&fp
->code
, cell
->fs
->code
.store
,
227 SPU_MAX_FRAGMENT_PROGRAM_INSTS
* SPE_INST_SIZE
);
230 printf("PPU Emit CELL_CMD_STATE_FRAGMENT_PROGRAM:\n");
231 for (i
= 0; i
< fp
->num_inst
; i
++) {
232 printf(" %3d: 0x%08x\n", i
, fp
->code
[i
]);
237 if (cell
->dirty
& (CELL_NEW_FS_CONSTANTS
)) {
238 const uint shader
= PIPE_SHADER_FRAGMENT
;
239 const uint num_const
= cell
->constants
[shader
].size
/ sizeof(float);
241 float *buf
= cell_batch_alloc(cell
, 16 + num_const
* sizeof(float));
242 uint64_t *ibuf
= (uint64_t *) buf
;
243 const float *constants
= pipe_buffer_map(cell
->pipe
.screen
,
244 cell
->constants
[shader
].buffer
,
245 PIPE_BUFFER_USAGE_CPU_READ
);
246 ibuf
[0] = CELL_CMD_STATE_FS_CONSTANTS
;
249 for (i
= 0; i
< num_const
; i
++) {
250 buf
[j
++] = constants
[i
];
252 pipe_buffer_unmap(cell
->pipe
.screen
, cell
->constants
[shader
].buffer
);
255 if (cell
->dirty
& (CELL_NEW_FRAMEBUFFER
|
256 CELL_NEW_DEPTH_STENCIL
|
258 struct cell_command_fragment_ops
*fops
, *fops_cmd
;
259 /* Note that cell_command_fragment_ops is a variant-sized record */
260 fops
= lookup_fragment_ops(cell
);
261 fops_cmd
= cell_batch_alloc(cell
, sizeof(*fops_cmd
) + fops
->total_code_size
);
262 memcpy(fops_cmd
, fops
, sizeof(*fops
) + fops
->total_code_size
);
265 if (cell
->dirty
& CELL_NEW_SAMPLER
) {
267 for (i
= 0; i
< CELL_MAX_SAMPLERS
; i
++) {
268 if (cell
->dirty_samplers
& (1 << i
)) {
269 if (cell
->sampler
[i
]) {
270 struct cell_command_sampler
*sampler
271 = cell_batch_alloc(cell
, sizeof(*sampler
));
272 sampler
->opcode
= CELL_CMD_STATE_SAMPLER
;
274 sampler
->state
= *cell
->sampler
[i
];
278 cell
->dirty_samplers
= 0x0;
281 if (cell
->dirty
& CELL_NEW_TEXTURE
) {
283 for (i
= 0;i
< CELL_MAX_SAMPLERS
; i
++) {
284 if (cell
->dirty_textures
& (1 << i
)) {
285 struct cell_command_texture
*texture
286 = cell_batch_alloc(cell
, sizeof(*texture
));
287 texture
->opcode
= CELL_CMD_STATE_TEXTURE
;
289 if (cell
->texture
[i
]) {
291 for (level
= 0; level
< CELL_MAX_TEXTURE_LEVELS
; level
++) {
292 texture
->start
[level
] = cell
->texture
[i
]->tiled_mapped
[level
];
293 texture
->width
[level
] = cell
->texture
[i
]->base
.width
[level
];
294 texture
->height
[level
] = cell
->texture
[i
]->base
.height
[level
];
295 texture
->depth
[level
] = cell
->texture
[i
]->base
.depth
[level
];
297 texture
->target
= cell
->texture
[i
]->base
.target
;
301 for (level
= 0; level
< CELL_MAX_TEXTURE_LEVELS
; level
++) {
302 texture
->start
[level
] = NULL
;
303 texture
->width
[level
] = 0;
304 texture
->height
[level
] = 0;
305 texture
->depth
[level
] = 0;
311 cell
->dirty_textures
= 0x0;
314 if (cell
->dirty
& CELL_NEW_VERTEX_INFO
) {
315 emit_state_cmd(cell
, CELL_CMD_STATE_VERTEX_INFO
,
316 &cell
->vertex_info
, sizeof(struct vertex_info
));
320 if (cell
->dirty
& CELL_NEW_VS
) {
321 const struct draw_context
*const draw
= cell
->draw
;
322 struct cell_shader_info info
;
324 info
.num_outputs
= draw_num_vs_outputs(draw
);
325 info
.declarations
= (uintptr_t) draw
->vs
.machine
.Declarations
;
326 info
.num_declarations
= draw
->vs
.machine
.NumDeclarations
;
327 info
.instructions
= (uintptr_t) draw
->vs
.machine
.Instructions
;
328 info
.num_instructions
= draw
->vs
.machine
.NumInstructions
;
329 info
.immediates
= (uintptr_t) draw
->vs
.machine
.Imms
;
330 info
.num_immediates
= draw
->vs
.machine
.ImmLimit
/ 4;
332 emit_state_cmd(cell
, CELL_CMD_STATE_BIND_VS
, &info
, sizeof(info
));