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