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