Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / cell / ppu / cell_state_emit.c
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 #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"
38
39
40 /**
41 * Find/create a cell_command_fragment_ops object corresponding to the
42 * current blend/stencil/z/colormask/etc. state.
43 */
44 static struct cell_command_fragment_ops *
45 lookup_fragment_ops(struct cell_context *cell)
46 {
47 struct cell_fragment_ops_key key;
48 struct cell_command_fragment_ops *ops;
49
50 /*
51 * Build key
52 */
53 memset(&key, 0, sizeof(key));
54 key.blend = *cell->blend;
55 key.blend_color = cell->blend_color;
56 key.dsa = *cell->depth_stencil;
57
58 if (cell->framebuffer.cbufs[0])
59 key.color_format = cell->framebuffer.cbufs[0]->format;
60 else
61 key.color_format = PIPE_FORMAT_NONE;
62
63 if (cell->framebuffer.zsbuf)
64 key.zs_format = cell->framebuffer.zsbuf->format;
65 else
66 key.zs_format = PIPE_FORMAT_NONE;
67
68 /*
69 * Look up key in cache.
70 */
71 ops = (struct cell_command_fragment_ops *)
72 util_keymap_lookup(cell->fragment_ops_cache, &key);
73
74 /*
75 * If not found, create/save new fragment ops command.
76 */
77 if (!ops) {
78 struct spe_function spe_code_front, spe_code_back;
79
80 if (0)
81 debug_printf("**** Create New Fragment Ops\n");
82
83 /* Prepare the buffer that will hold the generated code. */
84 spe_init_func(&spe_code_front, SPU_MAX_FRAGMENT_OPS_INSTS * SPE_INST_SIZE);
85 spe_init_func(&spe_code_back, SPU_MAX_FRAGMENT_OPS_INSTS * SPE_INST_SIZE);
86
87 /* generate new code. Always generate new code for both front-facing
88 * and back-facing fragments, even if it's the same code in both
89 * cases.
90 */
91 cell_gen_fragment_function(cell, CELL_FACING_FRONT, &spe_code_front);
92 cell_gen_fragment_function(cell, CELL_FACING_BACK, &spe_code_back);
93
94 /* alloc new fragment ops command */
95 ops = CALLOC_STRUCT(cell_command_fragment_ops);
96
97 /* populate the new cell_command_fragment_ops object */
98 ops->opcode = CELL_CMD_STATE_FRAGMENT_OPS;
99 memcpy(ops->code_front, spe_code_front.store, spe_code_size(&spe_code_front));
100 memcpy(ops->code_back, spe_code_back.store, spe_code_size(&spe_code_back));
101 ops->dsa = *cell->depth_stencil;
102 ops->blend = *cell->blend;
103
104 /* insert cell_command_fragment_ops object into keymap/cache */
105 util_keymap_insert(cell->fragment_ops_cache, &key, ops, NULL);
106
107 /* release rtasm buffer */
108 spe_release_func(&spe_code_front);
109 spe_release_func(&spe_code_back);
110 }
111 else {
112 if (0)
113 debug_printf("**** Re-use Fragment Ops\n");
114 }
115
116 return ops;
117 }
118
119
120
121 static void
122 emit_state_cmd(struct cell_context *cell, uint cmd,
123 const void *state, uint state_size)
124 {
125 uint64_t *dst = (uint64_t *)
126 cell_batch_alloc(cell, ROUNDUP8(sizeof(uint64_t) + state_size));
127 *dst = cmd;
128 memcpy(dst + 1, state, state_size);
129 }
130
131
132 /**
133 * For state marked as 'dirty', construct a state-update command block
134 * and insert it into the current batch buffer.
135 */
136 void
137 cell_emit_state(struct cell_context *cell)
138 {
139 if (cell->dirty & CELL_NEW_FRAMEBUFFER) {
140 struct pipe_surface *cbuf = cell->framebuffer.cbufs[0];
141 struct pipe_surface *zbuf = cell->framebuffer.zsbuf;
142 struct cell_command_framebuffer *fb
143 = cell_batch_alloc(cell, sizeof(*fb));
144 fb->opcode = CELL_CMD_STATE_FRAMEBUFFER;
145 fb->color_start = cell->cbuf_map[0];
146 fb->color_format = cbuf->format;
147 fb->depth_start = cell->zsbuf_map;
148 fb->depth_format = zbuf ? zbuf->format : PIPE_FORMAT_NONE;
149 fb->width = cell->framebuffer.width;
150 fb->height = cell->framebuffer.height;
151 #if 0
152 printf("EMIT color format %s\n", pf_name(fb->color_format));
153 printf("EMIT depth format %s\n", pf_name(fb->depth_format));
154 #endif
155 }
156
157 if (cell->dirty & (CELL_NEW_RASTERIZER)) {
158 struct cell_command_rasterizer *rast =
159 cell_batch_alloc(cell, sizeof(*rast));
160 rast->opcode = CELL_CMD_STATE_RASTERIZER;
161 rast->rasterizer = *cell->rasterizer;
162 }
163
164 if (cell->dirty & (CELL_NEW_FS)) {
165 /* Send new fragment program to SPUs */
166 struct cell_command_fragment_program *fp
167 = cell_batch_alloc(cell, sizeof(*fp));
168 fp->opcode = CELL_CMD_STATE_FRAGMENT_PROGRAM;
169 fp->num_inst = cell->fs->code.num_inst;
170 memcpy(&fp->code, cell->fs->code.store,
171 SPU_MAX_FRAGMENT_PROGRAM_INSTS * SPE_INST_SIZE);
172 if (0) {
173 int i;
174 printf("PPU Emit CELL_CMD_STATE_FRAGMENT_PROGRAM:\n");
175 for (i = 0; i < fp->num_inst; i++) {
176 printf(" %3d: 0x%08x\n", i, fp->code[i]);
177 }
178 }
179 }
180
181 if (cell->dirty & (CELL_NEW_FS_CONSTANTS)) {
182 const uint shader = PIPE_SHADER_FRAGMENT;
183 const uint num_const = cell->constants[shader].size / sizeof(float);
184 uint i, j;
185 float *buf = cell_batch_alloc(cell, 16 + num_const * sizeof(float));
186 uint64_t *ibuf = (uint64_t *) buf;
187 const float *constants = pipe_buffer_map(cell->pipe.screen,
188 cell->constants[shader].buffer,
189 PIPE_BUFFER_USAGE_CPU_READ);
190 ibuf[0] = CELL_CMD_STATE_FS_CONSTANTS;
191 ibuf[1] = num_const;
192 j = 4;
193 for (i = 0; i < num_const; i++) {
194 buf[j++] = constants[i];
195 }
196 pipe_buffer_unmap(cell->pipe.screen, cell->constants[shader].buffer);
197 }
198
199 if (cell->dirty & (CELL_NEW_FRAMEBUFFER |
200 CELL_NEW_DEPTH_STENCIL |
201 CELL_NEW_BLEND)) {
202 struct cell_command_fragment_ops *fops, *fops_cmd;
203 fops_cmd = cell_batch_alloc(cell, sizeof(*fops_cmd));
204 fops = lookup_fragment_ops(cell);
205 memcpy(fops_cmd, fops, sizeof(*fops));
206 }
207
208 if (cell->dirty & CELL_NEW_SAMPLER) {
209 uint i;
210 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
211 if (cell->dirty_samplers & (1 << i)) {
212 if (cell->sampler[i]) {
213 struct cell_command_sampler *sampler
214 = cell_batch_alloc(cell, sizeof(*sampler));
215 sampler->opcode = CELL_CMD_STATE_SAMPLER;
216 sampler->unit = i;
217 sampler->state = *cell->sampler[i];
218 }
219 }
220 }
221 cell->dirty_samplers = 0x0;
222 }
223
224 if (cell->dirty & CELL_NEW_TEXTURE) {
225 uint i;
226 for (i = 0;i < CELL_MAX_SAMPLERS; i++) {
227 if (cell->dirty_textures & (1 << i)) {
228 struct cell_command_texture *texture
229 = cell_batch_alloc(cell, sizeof(*texture));
230 texture->opcode = CELL_CMD_STATE_TEXTURE;
231 texture->unit = i;
232 if (cell->texture[i]) {
233 uint level;
234 for (level = 0; level < CELL_MAX_TEXTURE_LEVELS; level++) {
235 texture->start[level] = cell->texture[i]->tiled_mapped[level];
236 texture->width[level] = cell->texture[i]->base.width[level];
237 texture->height[level] = cell->texture[i]->base.height[level];
238 texture->depth[level] = cell->texture[i]->base.depth[level];
239 }
240 texture->target = cell->texture[i]->base.target;
241 }
242 else {
243 uint level;
244 for (level = 0; level < CELL_MAX_TEXTURE_LEVELS; level++) {
245 texture->start[level] = NULL;
246 texture->width[level] = 0;
247 texture->height[level] = 0;
248 texture->depth[level] = 0;
249 }
250 texture->target = 0;
251 }
252 }
253 }
254 cell->dirty_textures = 0x0;
255 }
256
257 if (cell->dirty & CELL_NEW_VERTEX_INFO) {
258 emit_state_cmd(cell, CELL_CMD_STATE_VERTEX_INFO,
259 &cell->vertex_info, sizeof(struct vertex_info));
260 }
261
262 #if 0
263 if (cell->dirty & CELL_NEW_VS) {
264 const struct draw_context *const draw = cell->draw;
265 struct cell_shader_info info;
266
267 info.num_outputs = draw_num_vs_outputs(draw);
268 info.declarations = (uintptr_t) draw->vs.machine.Declarations;
269 info.num_declarations = draw->vs.machine.NumDeclarations;
270 info.instructions = (uintptr_t) draw->vs.machine.Instructions;
271 info.num_instructions = draw->vs.machine.NumInstructions;
272 info.immediates = (uintptr_t) draw->vs.machine.Imms;
273 info.num_immediates = draw->vs.machine.ImmLimit / 4;
274
275 emit_state_cmd(cell, CELL_CMD_STATE_BIND_VS, &info, sizeof(info));
276 }
277 #endif
278 }