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