f35893537bf65681f52c6128ce8a67ad1b08d429
[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 "util/u_memory.h"
29 #include "cell_context.h"
30 #include "cell_gen_fragment.h"
31 #include "cell_state.h"
32 #include "cell_state_emit.h"
33 #include "cell_batch.h"
34 #include "cell_texture.h"
35 #include "draw/draw_context.h"
36 #include "draw/draw_private.h"
37
38
39 static void
40 emit_state_cmd(struct cell_context *cell, uint cmd,
41 const void *state, uint state_size)
42 {
43 uint64_t *dst = (uint64_t *)
44 cell_batch_alloc(cell, ROUNDUP8(sizeof(uint64_t) + state_size));
45 *dst = cmd;
46 memcpy(dst + 1, state, state_size);
47 }
48
49
50 /**
51 * For state marked as 'dirty', construct a state-update command block
52 * and insert it into the current batch buffer.
53 */
54 void
55 cell_emit_state(struct cell_context *cell)
56 {
57 if (cell->dirty & CELL_NEW_FRAMEBUFFER) {
58 struct pipe_surface *cbuf = cell->framebuffer.cbufs[0];
59 struct pipe_surface *zbuf = cell->framebuffer.zsbuf;
60 struct cell_command_framebuffer *fb
61 = cell_batch_alloc(cell, sizeof(*fb));
62 fb->opcode = CELL_CMD_STATE_FRAMEBUFFER;
63 fb->color_start = cell->cbuf_map[0];
64 fb->color_format = cbuf->format;
65 fb->depth_start = cell->zsbuf_map;
66 fb->depth_format = zbuf ? zbuf->format : PIPE_FORMAT_NONE;
67 fb->width = cell->framebuffer.width;
68 fb->height = cell->framebuffer.height;
69 #if 0
70 printf("EMIT color format %s\n", pf_name(fb->color_format));
71 printf("EMIT depth format %s\n", pf_name(fb->depth_format));
72 #endif
73 }
74
75 if (cell->dirty & (CELL_NEW_FS)) {
76 /* Send new fragment program to SPUs */
77 struct cell_command_fragment_program *fp
78 = cell_batch_alloc(cell, sizeof(*fp));
79 fp->opcode = CELL_CMD_STATE_FRAGMENT_PROGRAM;
80 fp->num_inst = cell->fs->code.num_inst;
81 memcpy(&fp->code, cell->fs->code.store,
82 SPU_MAX_FRAGMENT_PROGRAM_INSTS * SPE_INST_SIZE);
83 if (0) {
84 int i;
85 printf("PPU Emit CELL_CMD_STATE_FRAGMENT_PROGRAM:\n");
86 for (i = 0; i < fp->num_inst; i++) {
87 printf(" %3d: 0x%08x\n", i, fp->code[i]);
88 }
89 }
90 }
91
92 if (cell->dirty & (CELL_NEW_FRAMEBUFFER |
93 CELL_NEW_DEPTH_STENCIL |
94 CELL_NEW_BLEND)) {
95 /* XXX we don't want to always do codegen here. We should have
96 * a hash/lookup table to cache previous results...
97 */
98 struct cell_command_fragment_ops *fops
99 = cell_batch_alloc(cell, sizeof(*fops));
100 struct spe_function spe_code;
101
102 /* Prepare the buffer that will hold the generated code. */
103 spe_init_func(&spe_code, SPU_MAX_FRAGMENT_OPS_INSTS * SPE_INST_SIZE);
104
105 /* generate new code */
106 cell_gen_fragment_function(cell, &spe_code);
107
108 /* put the new code into the batch buffer */
109 fops->opcode = CELL_CMD_STATE_FRAGMENT_OPS;
110 memcpy(&fops->code, spe_code.store,
111 SPU_MAX_FRAGMENT_OPS_INSTS * SPE_INST_SIZE);
112 fops->dsa = *cell->depth_stencil;
113 fops->blend = *cell->blend;
114
115 /* free codegen buffer */
116 spe_release_func(&spe_code);
117 }
118
119 if (cell->dirty & CELL_NEW_SAMPLER) {
120 uint i;
121 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
122 if (cell->sampler[i]) {
123 struct cell_command_sampler *sampler
124 = cell_batch_alloc(cell, sizeof(*sampler));
125 sampler->opcode = CELL_CMD_STATE_SAMPLER;
126 sampler->unit = i;
127 sampler->state = *cell->sampler[i];
128 }
129 }
130 }
131
132 if (cell->dirty & CELL_NEW_TEXTURE) {
133 uint i;
134 for (i = 0;i < CELL_MAX_SAMPLERS; i++) {
135 struct cell_command_texture *texture
136 = cell_batch_alloc(cell, sizeof(*texture));
137 texture->opcode = CELL_CMD_STATE_TEXTURE;
138 texture->unit = i;
139 if (cell->texture[i]) {
140 texture->start = cell->texture[i]->tiled_data;
141 texture->width = cell->texture[i]->base.width[0];
142 texture->height = cell->texture[i]->base.height[0];
143 }
144 else {
145 texture->start = NULL;
146 texture->width = 1;
147 texture->height = 1;
148 }
149 }
150 }
151
152 if (cell->dirty & CELL_NEW_VERTEX_INFO) {
153 emit_state_cmd(cell, CELL_CMD_STATE_VERTEX_INFO,
154 &cell->vertex_info, sizeof(struct vertex_info));
155 }
156
157 #if 0
158 if (cell->dirty & CELL_NEW_VS) {
159 const struct draw_context *const draw = cell->draw;
160 struct cell_shader_info info;
161
162 info.num_outputs = draw_num_vs_outputs(draw);
163 info.declarations = (uintptr_t) draw->vs.machine.Declarations;
164 info.num_declarations = draw->vs.machine.NumDeclarations;
165 info.instructions = (uintptr_t) draw->vs.machine.Instructions;
166 info.num_instructions = draw->vs.machine.NumInstructions;
167 info.immediates = (uintptr_t) draw->vs.machine.Imms;
168 info.num_immediates = draw->vs.machine.ImmLimit / 4;
169
170 emit_state_cmd(cell, CELL_CMD_STATE_BIND_VS, &info, sizeof(info));
171 }
172 #endif
173 }