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
;
81 debug_printf("**** Create New Fragment Ops\n");
83 /* Prepare the buffer that will hold the generated code. */
84 spe_init_func(&spe_code
, SPU_MAX_FRAGMENT_OPS_INSTS
* SPE_INST_SIZE
);
86 /* generate new code */
87 cell_gen_fragment_function(cell
, &spe_code
);
89 /* alloc new fragment ops command */
90 ops
= CALLOC_STRUCT(cell_command_fragment_ops
);
92 /* populate the new cell_command_fragment_ops object */
93 ops
->opcode
= CELL_CMD_STATE_FRAGMENT_OPS
;
94 memcpy(ops
->code
, spe_code
.store
, spe_code_size(&spe_code
));
95 ops
->dsa
= *cell
->depth_stencil
;
96 ops
->blend
= *cell
->blend
;
98 /* insert cell_command_fragment_ops object into keymap/cache */
99 util_keymap_insert(cell
->fragment_ops_cache
, &key
, ops
, NULL
);
101 /* release rtasm buffer */
102 spe_release_func(&spe_code
);
106 debug_printf("**** Re-use Fragment Ops\n");
115 emit_state_cmd(struct cell_context
*cell
, uint cmd
,
116 const void *state
, uint state_size
)
118 uint64_t *dst
= (uint64_t *)
119 cell_batch_alloc(cell
, ROUNDUP8(sizeof(uint64_t) + state_size
));
121 memcpy(dst
+ 1, state
, state_size
);
126 * For state marked as 'dirty', construct a state-update command block
127 * and insert it into the current batch buffer.
130 cell_emit_state(struct cell_context
*cell
)
132 if (cell
->dirty
& CELL_NEW_FRAMEBUFFER
) {
133 struct pipe_surface
*cbuf
= cell
->framebuffer
.cbufs
[0];
134 struct pipe_surface
*zbuf
= cell
->framebuffer
.zsbuf
;
135 struct cell_command_framebuffer
*fb
136 = cell_batch_alloc(cell
, sizeof(*fb
));
137 fb
->opcode
= CELL_CMD_STATE_FRAMEBUFFER
;
138 fb
->color_start
= cell
->cbuf_map
[0];
139 fb
->color_format
= cbuf
->format
;
140 fb
->depth_start
= cell
->zsbuf_map
;
141 fb
->depth_format
= zbuf
? zbuf
->format
: PIPE_FORMAT_NONE
;
142 fb
->width
= cell
->framebuffer
.width
;
143 fb
->height
= cell
->framebuffer
.height
;
145 printf("EMIT color format %s\n", pf_name(fb
->color_format
));
146 printf("EMIT depth format %s\n", pf_name(fb
->depth_format
));
150 if (cell
->dirty
& (CELL_NEW_RASTERIZER
)) {
151 struct cell_command_rasterizer
*rast
=
152 cell_batch_alloc(cell
, sizeof(*rast
));
153 rast
->opcode
= CELL_CMD_STATE_RASTERIZER
;
154 rast
->rasterizer
= *cell
->rasterizer
;
157 if (cell
->dirty
& (CELL_NEW_FS
)) {
158 /* Send new fragment program to SPUs */
159 struct cell_command_fragment_program
*fp
160 = cell_batch_alloc(cell
, sizeof(*fp
));
161 fp
->opcode
= CELL_CMD_STATE_FRAGMENT_PROGRAM
;
162 fp
->num_inst
= cell
->fs
->code
.num_inst
;
163 memcpy(&fp
->code
, cell
->fs
->code
.store
,
164 SPU_MAX_FRAGMENT_PROGRAM_INSTS
* SPE_INST_SIZE
);
167 printf("PPU Emit CELL_CMD_STATE_FRAGMENT_PROGRAM:\n");
168 for (i
= 0; i
< fp
->num_inst
; i
++) {
169 printf(" %3d: 0x%08x\n", i
, fp
->code
[i
]);
174 if (cell
->dirty
& (CELL_NEW_FS_CONSTANTS
)) {
175 const uint shader
= PIPE_SHADER_FRAGMENT
;
176 const uint num_const
= cell
->constants
[shader
].size
/ sizeof(float);
178 float *buf
= cell_batch_alloc(cell
, 16 + num_const
* sizeof(float));
179 uint64_t *ibuf
= (uint64_t *) buf
;
180 const float *constants
= pipe_buffer_map(cell
->pipe
.screen
,
181 cell
->constants
[shader
].buffer
,
182 PIPE_BUFFER_USAGE_CPU_READ
);
183 ibuf
[0] = CELL_CMD_STATE_FS_CONSTANTS
;
186 for (i
= 0; i
< num_const
; i
++) {
187 buf
[j
++] = constants
[i
];
189 pipe_buffer_unmap(cell
->pipe
.screen
, cell
->constants
[shader
].buffer
);
192 if (cell
->dirty
& (CELL_NEW_FRAMEBUFFER
|
193 CELL_NEW_DEPTH_STENCIL
|
195 struct cell_command_fragment_ops
*fops
, *fops_cmd
;
196 fops_cmd
= cell_batch_alloc(cell
, sizeof(*fops_cmd
));
197 fops
= lookup_fragment_ops(cell
);
198 memcpy(fops_cmd
, fops
, sizeof(*fops
));
201 if (cell
->dirty
& CELL_NEW_SAMPLER
) {
203 for (i
= 0; i
< CELL_MAX_SAMPLERS
; i
++) {
204 if (cell
->dirty_samplers
& (1 << i
)) {
205 if (cell
->sampler
[i
]) {
206 struct cell_command_sampler
*sampler
207 = cell_batch_alloc(cell
, sizeof(*sampler
));
208 sampler
->opcode
= CELL_CMD_STATE_SAMPLER
;
210 sampler
->state
= *cell
->sampler
[i
];
214 cell
->dirty_samplers
= 0x0;
217 if (cell
->dirty
& CELL_NEW_TEXTURE
) {
219 for (i
= 0;i
< CELL_MAX_SAMPLERS
; i
++) {
220 if (cell
->dirty_textures
& (1 << i
)) {
221 struct cell_command_texture
*texture
222 = cell_batch_alloc(cell
, sizeof(*texture
));
223 texture
->opcode
= CELL_CMD_STATE_TEXTURE
;
225 if (cell
->texture
[i
]) {
227 for (level
= 0; level
< CELL_MAX_TEXTURE_LEVELS
; level
++) {
228 texture
->start
[level
] = cell
->texture
[i
]->tiled_mapped
[level
];
229 texture
->width
[level
] = cell
->texture
[i
]->base
.width
[level
];
230 texture
->height
[level
] = cell
->texture
[i
]->base
.height
[level
];
231 texture
->depth
[level
] = cell
->texture
[i
]->base
.depth
[level
];
233 texture
->target
= cell
->texture
[i
]->base
.target
;
237 for (level
= 0; level
< CELL_MAX_TEXTURE_LEVELS
; level
++) {
238 texture
->start
[level
] = NULL
;
239 texture
->width
[level
] = 0;
240 texture
->height
[level
] = 0;
241 texture
->depth
[level
] = 0;
247 cell
->dirty_textures
= 0x0;
250 if (cell
->dirty
& CELL_NEW_VERTEX_INFO
) {
251 emit_state_cmd(cell
, CELL_CMD_STATE_VERTEX_INFO
,
252 &cell
->vertex_info
, sizeof(struct vertex_info
));
256 if (cell
->dirty
& CELL_NEW_VS
) {
257 const struct draw_context
*const draw
= cell
->draw
;
258 struct cell_shader_info info
;
260 info
.num_outputs
= draw_num_vs_outputs(draw
);
261 info
.declarations
= (uintptr_t) draw
->vs
.machine
.Declarations
;
262 info
.num_declarations
= draw
->vs
.machine
.NumDeclarations
;
263 info
.instructions
= (uintptr_t) draw
->vs
.machine
.Instructions
;
264 info
.num_instructions
= draw
->vs
.machine
.NumInstructions
;
265 info
.immediates
= (uintptr_t) draw
->vs
.machine
.Imms
;
266 info
.num_immediates
= draw
->vs
.machine
.ImmLimit
/ 4;
268 emit_state_cmd(cell
, CELL_CMD_STATE_BIND_VS
, &info
, sizeof(info
));