cell: sync up with sampler/texture state-setting changes
[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_states(struct pipe_context *pipe,
214 unsigned num, void **samplers)
215 {
216 struct cell_context *cell = cell_context(pipe);
217
218 draw_flush(cell->draw);
219
220 assert(unit < PIPE_MAX_SAMPLERS);
221
222 memcpy(cell->sampler, samplers, num * sizeof(void *));
223 memset(&cell->sampler[num], 0, (PIPE_MAX_SAMPLERS - num) *
224 sizeof(void *));
225 cell->num_samplers = num;
226
227 cell->dirty |= CELL_NEW_SAMPLER;
228 }
229
230
231 static void
232 cell_delete_sampler_state(struct pipe_context *pipe,
233 void *sampler)
234 {
235 FREE( sampler );
236 }
237
238
239
240 static void
241 cell_set_sampler_textures(struct pipe_context *pipe,
242 unsigned num, struct pipe_texture **texture)
243 {
244 struct cell_context *cell = cell_context(pipe);
245 uint i;
246
247 /* Check for no-op */
248 if (num == cell->num_textures &&
249 !memcmp(cell->texture, texture, num * sizeof(struct pipe_texture *)))
250 return;
251
252 draw_flush(cell->draw);
253
254 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
255 struct pipe_texture *tex = i < num ? texture[i] : NULL;
256
257 pipe_texture_reference((struct pipe_texture **) &cell->texture[i], tex);
258 }
259
260 cell_update_texture_mapping(cell);
261
262 cell->dirty |= CELL_NEW_TEXTURE;
263 }
264
265
266
267 static void
268 cell_set_framebuffer_state(struct pipe_context *pipe,
269 const struct pipe_framebuffer_state *fb)
270 {
271 struct cell_context *cell = cell_context(pipe);
272
273 if (1 /*memcmp(&cell->framebuffer, fb, sizeof(*fb))*/) {
274 struct pipe_surface *csurf = fb->cbufs[0];
275 struct pipe_surface *zsurf = fb->zsbuf;
276 uint i;
277
278 /* unmap old surfaces */
279 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
280 if (cell->framebuffer.cbufs[i] && cell->cbuf_map[i]) {
281 pipe_surface_unmap(cell->framebuffer.cbufs[i]);
282 cell->cbuf_map[i] = NULL;
283 }
284 }
285
286 if (cell->framebuffer.zsbuf && cell->zsbuf_map) {
287 pipe_surface_unmap(cell->framebuffer.zsbuf);
288 cell->zsbuf_map = NULL;
289 }
290
291 /* update my state */
292 cell->framebuffer = *fb;
293
294 /* map new surfaces */
295 if (csurf)
296 cell->cbuf_map[0] = pipe_surface_map(csurf);
297
298 if (zsurf)
299 cell->zsbuf_map = pipe_surface_map(zsurf);
300
301 cell->dirty |= CELL_NEW_FRAMEBUFFER;
302 }
303 }
304
305
306
307 void
308 cell_init_state_functions(struct cell_context *cell)
309 {
310 cell->pipe.create_blend_state = cell_create_blend_state;
311 cell->pipe.bind_blend_state = cell_bind_blend_state;
312 cell->pipe.delete_blend_state = cell_delete_blend_state;
313
314 cell->pipe.create_sampler_state = cell_create_sampler_state;
315 cell->pipe.bind_sampler_states = cell_bind_sampler_states;
316 cell->pipe.delete_sampler_state = cell_delete_sampler_state;
317
318 cell->pipe.set_sampler_textures = cell_set_sampler_textures;
319
320 cell->pipe.create_depth_stencil_alpha_state = cell_create_depth_stencil_alpha_state;
321 cell->pipe.bind_depth_stencil_alpha_state = cell_bind_depth_stencil_alpha_state;
322 cell->pipe.delete_depth_stencil_alpha_state = cell_delete_depth_stencil_alpha_state;
323
324 cell->pipe.create_rasterizer_state = cell_create_rasterizer_state;
325 cell->pipe.bind_rasterizer_state = cell_bind_rasterizer_state;
326 cell->pipe.delete_rasterizer_state = cell_delete_rasterizer_state;
327
328 cell->pipe.set_blend_color = cell_set_blend_color;
329 cell->pipe.set_clip_state = cell_set_clip_state;
330
331 cell->pipe.set_framebuffer_state = cell_set_framebuffer_state;
332
333 cell->pipe.set_polygon_stipple = cell_set_polygon_stipple;
334 cell->pipe.set_scissor_state = cell_set_scissor_state;
335 cell->pipe.set_viewport_state = cell_set_viewport_state;
336 }
337