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