cell: Use code-gen for alpha blend
[mesa.git] / src / gallium / drivers / cell / ppu / cell_pipe_state.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 /* Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Brian Paul
31 */
32
33 #include "pipe/p_util.h"
34 #include "pipe/p_inlines.h"
35 #include "draw/draw_context.h"
36 #include "cell_context.h"
37 #include "cell_state.h"
38 #include "cell_texture.h"
39 #include "cell_state_per_fragment.h"
40
41
42
43 static void *
44 cell_create_blend_state(struct pipe_context *pipe,
45 const struct pipe_blend_state *blend)
46 {
47 struct cell_blend_state *cb = MALLOC(sizeof(struct cell_blend_state));
48
49 (void) memcpy(cb, blend, sizeof(*blend));
50 cb->code.store = NULL;
51
52 return cb;
53 }
54
55
56 static void
57 cell_bind_blend_state(struct pipe_context *pipe, void *state)
58 {
59 struct cell_context *cell = cell_context(pipe);
60 struct cell_blend_state *blend = (struct cell_blend_state *) state;
61
62
63 draw_flush(cell->draw);
64
65 if ((blend != NULL) && (blend->code.store == NULL)) {
66 cell_generate_depth_stencil_test(blend);
67 }
68
69 cell->blend = blend;
70 cell->dirty |= CELL_NEW_BLEND;
71 }
72
73
74 static void
75 cell_delete_blend_state(struct pipe_context *pipe, void *blend)
76 {
77 struct cell_blend_state *cb = (struct cell_blend_state *) blend;
78
79 spe_release_func(& cb->code);
80 FREE(cb);
81 }
82
83
84 static void
85 cell_set_blend_color(struct pipe_context *pipe,
86 const struct pipe_blend_color *blend_color)
87 {
88 struct cell_context *cell = cell_context(pipe);
89
90 draw_flush(cell->draw);
91
92 cell->blend_color = *blend_color;
93
94 cell->dirty |= CELL_NEW_BLEND;
95 }
96
97
98
99
100 static void *
101 cell_create_depth_stencil_alpha_state(struct pipe_context *pipe,
102 const struct pipe_depth_stencil_alpha_state *depth_stencil)
103 {
104 struct cell_depth_stencil_alpha_state *cdsa =
105 MALLOC(sizeof(struct cell_depth_stencil_alpha_state));
106
107 (void) memcpy(cdsa, depth_stencil, sizeof(*depth_stencil));
108 cdsa->code.store = NULL;
109
110 return cdsa;
111 }
112
113
114 static void
115 cell_bind_depth_stencil_alpha_state(struct pipe_context *pipe,
116 void *depth_stencil)
117 {
118 struct cell_context *cell = cell_context(pipe);
119 struct cell_depth_stencil_alpha_state *cdsa =
120 (struct cell_depth_stencil_alpha_state *) depth_stencil;
121
122 draw_flush(cell->draw);
123
124 if ((cdsa != NULL) && (cdsa->code.store == NULL)) {
125 cell_generate_depth_stencil_test(cdsa);
126 }
127
128 cell->depth_stencil = cdsa;
129 cell->dirty |= CELL_NEW_DEPTH_STENCIL;
130 }
131
132
133 static void
134 cell_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *depth)
135 {
136 struct cell_depth_stencil_alpha_state *cdsa =
137 (struct cell_depth_stencil_alpha_state *) depth;
138
139 spe_release_func(& cdsa->code);
140 FREE(cdsa);
141 }
142
143
144 static void cell_set_clip_state( struct pipe_context *pipe,
145 const struct pipe_clip_state *clip )
146 {
147 struct cell_context *cell = cell_context(pipe);
148
149 /* pass the clip state to the draw module */
150 draw_set_clip_state(cell->draw, clip);
151 }
152
153
154
155 /* Called when driver state tracker notices changes to the viewport
156 * matrix:
157 */
158 static void
159 cell_set_viewport_state( struct pipe_context *pipe,
160 const struct pipe_viewport_state *viewport )
161 {
162 struct cell_context *cell = cell_context(pipe);
163
164 cell->viewport = *viewport; /* struct copy */
165 cell->dirty |= CELL_NEW_VIEWPORT;
166
167 /* pass the viewport info to the draw module */
168 draw_set_viewport_state(cell->draw, viewport);
169
170 /* Using tnl/ and vf/ modules is temporary while getting started.
171 * Full pipe will have vertex shader, vertex fetch of its own.
172 */
173 }
174
175
176 static void
177 cell_set_scissor_state( struct pipe_context *pipe,
178 const struct pipe_scissor_state *scissor )
179 {
180 struct cell_context *cell = cell_context(pipe);
181
182 memcpy( &cell->scissor, scissor, sizeof(*scissor) );
183 cell->dirty |= CELL_NEW_SCISSOR;
184 }
185
186
187 static void
188 cell_set_polygon_stipple( struct pipe_context *pipe,
189 const struct pipe_poly_stipple *stipple )
190 {
191 struct cell_context *cell = cell_context(pipe);
192
193 memcpy( &cell->poly_stipple, stipple, sizeof(*stipple) );
194 cell->dirty |= CELL_NEW_STIPPLE;
195 }
196
197
198
199 static void *
200 cell_create_rasterizer_state(struct pipe_context *pipe,
201 const struct pipe_rasterizer_state *setup)
202 {
203 struct pipe_rasterizer_state *state
204 = MALLOC(sizeof(struct pipe_rasterizer_state));
205 memcpy(state, setup, sizeof(struct pipe_rasterizer_state));
206 return state;
207 }
208
209
210 static void
211 cell_bind_rasterizer_state(struct pipe_context *pipe, void *setup)
212 {
213 struct cell_context *cell = cell_context(pipe);
214
215 /* pass-through to draw module */
216 draw_set_rasterizer_state(cell->draw, setup);
217
218 cell->rasterizer = (struct pipe_rasterizer_state *)setup;
219
220 cell->dirty |= CELL_NEW_RASTERIZER;
221 }
222
223
224 static void
225 cell_delete_rasterizer_state(struct pipe_context *pipe, void *rasterizer)
226 {
227 FREE(rasterizer);
228 }
229
230
231
232 static void *
233 cell_create_sampler_state(struct pipe_context *pipe,
234 const struct pipe_sampler_state *sampler)
235 {
236 return mem_dup(sampler, sizeof(*sampler));
237 }
238
239
240 static void
241 cell_bind_sampler_states(struct pipe_context *pipe,
242 unsigned num, void **samplers)
243 {
244 struct cell_context *cell = cell_context(pipe);
245
246 draw_flush(cell->draw);
247
248 assert(unit < PIPE_MAX_SAMPLERS);
249
250 memcpy(cell->sampler, samplers, num * sizeof(void *));
251 memset(&cell->sampler[num], 0, (PIPE_MAX_SAMPLERS - num) *
252 sizeof(void *));
253 cell->num_samplers = num;
254
255 cell->dirty |= CELL_NEW_SAMPLER;
256 }
257
258
259 static void
260 cell_delete_sampler_state(struct pipe_context *pipe,
261 void *sampler)
262 {
263 FREE( sampler );
264 }
265
266
267
268 static void
269 cell_set_sampler_textures(struct pipe_context *pipe,
270 unsigned num, struct pipe_texture **texture)
271 {
272 struct cell_context *cell = cell_context(pipe);
273 uint i;
274
275 /* Check for no-op */
276 if (num == cell->num_textures &&
277 !memcmp(cell->texture, texture, num * sizeof(struct pipe_texture *)))
278 return;
279
280 draw_flush(cell->draw);
281
282 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
283 struct pipe_texture *tex = i < num ? texture[i] : NULL;
284
285 pipe_texture_reference((struct pipe_texture **) &cell->texture[i], tex);
286 }
287
288 cell_update_texture_mapping(cell);
289
290 cell->dirty |= CELL_NEW_TEXTURE;
291 }
292
293
294
295 static void
296 cell_set_framebuffer_state(struct pipe_context *pipe,
297 const struct pipe_framebuffer_state *fb)
298 {
299 struct cell_context *cell = cell_context(pipe);
300
301 if (1 /*memcmp(&cell->framebuffer, fb, sizeof(*fb))*/) {
302 struct pipe_surface *csurf = fb->cbufs[0];
303 struct pipe_surface *zsurf = fb->zsbuf;
304 uint i;
305
306 /* unmap old surfaces */
307 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
308 if (cell->framebuffer.cbufs[i] && cell->cbuf_map[i]) {
309 pipe_surface_unmap(cell->framebuffer.cbufs[i]);
310 cell->cbuf_map[i] = NULL;
311 }
312 }
313
314 if (cell->framebuffer.zsbuf && cell->zsbuf_map) {
315 pipe_surface_unmap(cell->framebuffer.zsbuf);
316 cell->zsbuf_map = NULL;
317 }
318
319 /* update my state */
320 cell->framebuffer = *fb;
321
322 /* map new surfaces */
323 if (csurf)
324 cell->cbuf_map[0] = pipe_surface_map(csurf);
325
326 if (zsurf)
327 cell->zsbuf_map = pipe_surface_map(zsurf);
328
329 cell->dirty |= CELL_NEW_FRAMEBUFFER;
330 }
331 }
332
333
334
335 void
336 cell_init_state_functions(struct cell_context *cell)
337 {
338 cell->pipe.create_blend_state = cell_create_blend_state;
339 cell->pipe.bind_blend_state = cell_bind_blend_state;
340 cell->pipe.delete_blend_state = cell_delete_blend_state;
341
342 cell->pipe.create_sampler_state = cell_create_sampler_state;
343 cell->pipe.bind_sampler_states = cell_bind_sampler_states;
344 cell->pipe.delete_sampler_state = cell_delete_sampler_state;
345
346 cell->pipe.set_sampler_textures = cell_set_sampler_textures;
347
348 cell->pipe.create_depth_stencil_alpha_state = cell_create_depth_stencil_alpha_state;
349 cell->pipe.bind_depth_stencil_alpha_state = cell_bind_depth_stencil_alpha_state;
350 cell->pipe.delete_depth_stencil_alpha_state = cell_delete_depth_stencil_alpha_state;
351
352 cell->pipe.create_rasterizer_state = cell_create_rasterizer_state;
353 cell->pipe.bind_rasterizer_state = cell_bind_rasterizer_state;
354 cell->pipe.delete_rasterizer_state = cell_delete_rasterizer_state;
355
356 cell->pipe.set_blend_color = cell_set_blend_color;
357 cell->pipe.set_clip_state = cell_set_clip_state;
358
359 cell->pipe.set_framebuffer_state = cell_set_framebuffer_state;
360
361 cell->pipe.set_polygon_stipple = cell_set_polygon_stipple;
362 cell->pipe.set_scissor_state = cell_set_scissor_state;
363 cell->pipe.set_viewport_state = cell_set_viewport_state;
364 }
365