Merge commit 'origin/gallium-master-merge'
[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_pipe_state.h"
39 #include "cell_state.h"
40 #include "cell_texture.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 return mem_dup(blend, sizeof(*blend));
49 }
50
51
52 static void
53 cell_bind_blend_state(struct pipe_context *pipe, void *blend)
54 {
55 struct cell_context *cell = cell_context(pipe);
56
57 draw_flush(cell->draw);
58
59 cell->blend = (struct pipe_blend_state *) blend;
60 cell->dirty |= CELL_NEW_BLEND;
61 }
62
63
64 static void
65 cell_delete_blend_state(struct pipe_context *pipe, void *blend)
66 {
67 FREE(blend);
68 }
69
70
71 static void
72 cell_set_blend_color(struct pipe_context *pipe,
73 const struct pipe_blend_color *blend_color)
74 {
75 struct cell_context *cell = cell_context(pipe);
76
77 draw_flush(cell->draw);
78
79 cell->blend_color = *blend_color;
80
81 cell->dirty |= CELL_NEW_BLEND;
82 }
83
84
85
86
87 static void *
88 cell_create_depth_stencil_alpha_state(struct pipe_context *pipe,
89 const struct pipe_depth_stencil_alpha_state *dsa)
90 {
91 return mem_dup(dsa, sizeof(*dsa));
92 }
93
94
95 static void
96 cell_bind_depth_stencil_alpha_state(struct pipe_context *pipe,
97 void *dsa)
98 {
99 struct cell_context *cell = cell_context(pipe);
100
101 draw_flush(cell->draw);
102
103 cell->depth_stencil = (struct pipe_depth_stencil_alpha_state *) dsa;
104 cell->dirty |= CELL_NEW_DEPTH_STENCIL;
105 }
106
107
108 static void
109 cell_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *dsa)
110 {
111 FREE(dsa);
112 }
113
114
115 static void
116 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 *rasterizer)
174 {
175 return mem_dup(rasterizer, sizeof(*rasterizer));
176 }
177
178
179 static void
180 cell_bind_rasterizer_state(struct pipe_context *pipe, void *rast)
181 {
182 struct pipe_rasterizer_state *rasterizer =
183 (struct pipe_rasterizer_state *) rast;
184 struct cell_context *cell = cell_context(pipe);
185
186 /* pass-through to draw module */
187 draw_set_rasterizer_state(cell->draw, rasterizer);
188
189 cell->rasterizer = rasterizer;
190
191 cell->dirty |= CELL_NEW_RASTERIZER;
192 }
193
194
195 static void
196 cell_delete_rasterizer_state(struct pipe_context *pipe, void *rasterizer)
197 {
198 FREE(rasterizer);
199 }
200
201
202
203 static void *
204 cell_create_sampler_state(struct pipe_context *pipe,
205 const struct pipe_sampler_state *sampler)
206 {
207 return mem_dup(sampler, sizeof(*sampler));
208 }
209
210
211 static void
212 cell_bind_sampler_states(struct pipe_context *pipe,
213 unsigned num, void **samplers)
214 {
215 struct cell_context *cell = cell_context(pipe);
216 uint i, changed = 0x0;
217
218 assert(num <= CELL_MAX_SAMPLERS);
219
220 draw_flush(cell->draw);
221
222 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
223 struct pipe_sampler_state *new_samp = i < num ? samplers[i] : NULL;
224 if (cell->sampler[i] != new_samp) {
225 cell->sampler[i] = new_samp;
226 changed |= (1 << i);
227 }
228 }
229
230 if (changed) {
231 cell->dirty |= CELL_NEW_SAMPLER;
232 cell->dirty_samplers |= changed;
233 }
234 }
235
236
237 static void
238 cell_delete_sampler_state(struct pipe_context *pipe,
239 void *sampler)
240 {
241 FREE( sampler );
242 }
243
244
245
246 static void
247 cell_set_sampler_textures(struct pipe_context *pipe,
248 unsigned num, struct pipe_texture **texture)
249 {
250 struct cell_context *cell = cell_context(pipe);
251 uint i, changed = 0x0;
252
253 assert(num <= CELL_MAX_SAMPLERS);
254
255 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
256 struct pipe_texture *new_tex = i < num ? texture[i] : NULL;
257 if ((struct pipe_texture *) cell->texture[i] != new_tex) {
258 pipe_texture_reference((struct pipe_texture **) &cell->texture[i],
259 new_tex);
260 changed |= (1 << i);
261 }
262 }
263
264 cell->num_textures = num;
265
266 if (changed) {
267 cell->dirty |= CELL_NEW_TEXTURE;
268 cell->dirty_textures |= changed;
269 }
270 }
271
272
273
274 static void
275 cell_set_framebuffer_state(struct pipe_context *pipe,
276 const struct pipe_framebuffer_state *fb)
277 {
278 struct cell_context *cell = cell_context(pipe);
279
280 if (1 /*memcmp(&cell->framebuffer, fb, sizeof(*fb))*/) {
281 struct pipe_surface *csurf = fb->cbufs[0];
282 struct pipe_surface *zsurf = fb->zsbuf;
283 uint i;
284 uint flags = (PIPE_BUFFER_USAGE_GPU_WRITE |
285 PIPE_BUFFER_USAGE_GPU_READ);
286
287 /* unmap old surfaces */
288 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
289 if (cell->framebuffer.cbufs[i] && cell->cbuf_map[i]) {
290 pipe_surface_unmap(cell->framebuffer.cbufs[i]);
291 cell->cbuf_map[i] = NULL;
292 }
293 }
294
295 if (cell->framebuffer.zsbuf && cell->zsbuf_map) {
296 pipe_surface_unmap(cell->framebuffer.zsbuf);
297 cell->zsbuf_map = NULL;
298 }
299
300 /* Finish any pending rendering to the current surface before
301 * installing a new surface!
302 */
303 cell_flush_int(cell, CELL_FLUSH_WAIT);
304
305 /* update my state
306 * (this is also where old surfaces will finally get freed)
307 */
308 cell->framebuffer.width = fb->width;
309 cell->framebuffer.height = fb->height;
310 cell->framebuffer.nr_cbufs = fb->nr_cbufs;
311 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
312 pipe_surface_reference(&cell->framebuffer.cbufs[i], fb->cbufs[i]);
313 }
314 pipe_surface_reference(&cell->framebuffer.zsbuf, fb->zsbuf);
315
316 /* map new surfaces */
317 if (csurf)
318 cell->cbuf_map[0] = pipe_surface_map(csurf, flags);
319
320 if (zsurf)
321 cell->zsbuf_map = pipe_surface_map(zsurf, flags);
322
323 cell->dirty |= CELL_NEW_FRAMEBUFFER;
324 }
325 }
326
327
328
329 void
330 cell_init_state_functions(struct cell_context *cell)
331 {
332 cell->pipe.create_blend_state = cell_create_blend_state;
333 cell->pipe.bind_blend_state = cell_bind_blend_state;
334 cell->pipe.delete_blend_state = cell_delete_blend_state;
335
336 cell->pipe.create_sampler_state = cell_create_sampler_state;
337 cell->pipe.bind_sampler_states = cell_bind_sampler_states;
338 cell->pipe.delete_sampler_state = cell_delete_sampler_state;
339
340 cell->pipe.set_sampler_textures = cell_set_sampler_textures;
341
342 cell->pipe.create_depth_stencil_alpha_state = cell_create_depth_stencil_alpha_state;
343 cell->pipe.bind_depth_stencil_alpha_state = cell_bind_depth_stencil_alpha_state;
344 cell->pipe.delete_depth_stencil_alpha_state = cell_delete_depth_stencil_alpha_state;
345
346 cell->pipe.create_rasterizer_state = cell_create_rasterizer_state;
347 cell->pipe.bind_rasterizer_state = cell_bind_rasterizer_state;
348 cell->pipe.delete_rasterizer_state = cell_delete_rasterizer_state;
349
350 cell->pipe.set_blend_color = cell_set_blend_color;
351 cell->pipe.set_clip_state = cell_set_clip_state;
352
353 cell->pipe.set_framebuffer_state = cell_set_framebuffer_state;
354
355 cell->pipe.set_polygon_stipple = cell_set_polygon_stipple;
356 cell->pipe.set_scissor_state = cell_set_scissor_state;
357 cell->pipe.set_viewport_state = cell_set_viewport_state;
358 }