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