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