Squashed commit of the following:
[profile/ivi/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 "util/u_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_stencil_ref(struct pipe_context *pipe,
117                      const struct pipe_stencil_ref *stencil_ref)
118 {
119    struct cell_context *cell = cell_context(pipe);
120
121    draw_flush(cell->draw);
122
123    cell->stencil_ref = *stencil_ref;
124
125    cell->dirty |= CELL_NEW_DEPTH_STENCIL;
126 }
127
128 static void
129 cell_set_clip_state(struct pipe_context *pipe,
130                     const struct pipe_clip_state *clip)
131 {
132    struct cell_context *cell = cell_context(pipe);
133
134    /* pass the clip state to the draw module */
135    draw_set_clip_state(cell->draw, clip);
136 }
137
138
139
140 /* Called when driver state tracker notices changes to the viewport
141  * matrix:
142  */
143 static void
144 cell_set_viewport_state( struct pipe_context *pipe,
145                          const struct pipe_viewport_state *viewport )
146 {
147    struct cell_context *cell = cell_context(pipe);
148
149    cell->viewport = *viewport; /* struct copy */
150    cell->dirty |= CELL_NEW_VIEWPORT;
151
152    /* pass the viewport info to the draw module */
153    draw_set_viewport_state(cell->draw, viewport);
154
155    /* Using tnl/ and vf/ modules is temporary while getting started.
156     * Full pipe will have vertex shader, vertex fetch of its own.
157     */
158 }
159
160
161 static void
162 cell_set_scissor_state( struct pipe_context *pipe,
163                         const struct pipe_scissor_state *scissor )
164 {
165    struct cell_context *cell = cell_context(pipe);
166
167    memcpy( &cell->scissor, scissor, sizeof(*scissor) );
168    cell->dirty |= CELL_NEW_SCISSOR;
169 }
170
171
172 static void
173 cell_set_polygon_stipple( struct pipe_context *pipe,
174                           const struct pipe_poly_stipple *stipple )
175 {
176    struct cell_context *cell = cell_context(pipe);
177
178    memcpy( &cell->poly_stipple, stipple, sizeof(*stipple) );
179    cell->dirty |= CELL_NEW_STIPPLE;
180 }
181
182
183
184 static void *
185 cell_create_rasterizer_state(struct pipe_context *pipe,
186                              const struct pipe_rasterizer_state *rasterizer)
187 {
188    return mem_dup(rasterizer, sizeof(*rasterizer));
189 }
190
191
192 static void
193 cell_bind_rasterizer_state(struct pipe_context *pipe, void *rast)
194 {
195    struct pipe_rasterizer_state *rasterizer =
196       (struct pipe_rasterizer_state *) rast;
197    struct cell_context *cell = cell_context(pipe);
198
199    /* pass-through to draw module */
200    draw_set_rasterizer_state(cell->draw, rasterizer);
201
202    cell->rasterizer = rasterizer;
203
204    cell->dirty |= CELL_NEW_RASTERIZER;
205 }
206
207
208 static void
209 cell_delete_rasterizer_state(struct pipe_context *pipe, void *rasterizer)
210 {
211    FREE(rasterizer);
212 }
213
214
215
216 static void *
217 cell_create_sampler_state(struct pipe_context *pipe,
218                           const struct pipe_sampler_state *sampler)
219 {
220    return mem_dup(sampler, sizeof(*sampler));
221 }
222
223
224 static void
225 cell_bind_sampler_states(struct pipe_context *pipe,
226                          unsigned num, void **samplers)
227 {
228    struct cell_context *cell = cell_context(pipe);
229    uint i, changed = 0x0;
230
231    assert(num <= CELL_MAX_SAMPLERS);
232
233    draw_flush(cell->draw);
234
235    for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
236       struct pipe_sampler_state *new_samp = i < num ? samplers[i] : NULL;
237       if (cell->sampler[i] != new_samp) {
238          cell->sampler[i] = new_samp;
239          changed |= (1 << i);
240       }
241    }
242
243    if (changed) {
244       cell->dirty |= CELL_NEW_SAMPLER;
245       cell->dirty_samplers |= changed;
246    }
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_fragment_sampler_views(struct pipe_context *pipe,
261                                 unsigned num,
262                                 struct pipe_sampler_view **views)
263 {
264    struct cell_context *cell = cell_context(pipe);
265    uint i, changed = 0x0;
266
267    assert(num <= CELL_MAX_SAMPLERS);
268
269    for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
270       struct pipe_sampler_view *new_view = i < num ? views[i] : NULL;
271       struct pipe_sampler_view *old_view = cell->fragment_sampler_views[i];
272
273       if (old_view != new_view) {
274          struct pipe_resource *new_tex = new_view ? new_view->texture : NULL;
275
276          pipe_sampler_view_reference(&cell->fragment_sampler_views[i],
277                                      views[i]);
278          pipe_resource_reference((struct pipe_resource **) &cell->texture[i],
279                                 (struct pipe_resource *) new_tex);
280
281          changed |= (1 << i);
282       }
283    }
284
285    cell->num_textures = num;
286
287    if (changed) {
288       cell->dirty |= CELL_NEW_TEXTURE;
289       cell->dirty_textures |= changed;
290    }
291 }
292
293
294 static struct pipe_sampler_view *
295 cell_create_sampler_view(struct pipe_context *pipe,
296                          struct pipe_resource *texture,
297                          const struct pipe_sampler_view *templ)
298 {
299    struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
300
301    if (view) {
302       *view = *templ;
303       view->reference.count = 1;
304       view->texture = NULL;
305       pipe_resource_reference(&view->texture, texture);
306       view->context = pipe;
307    }
308
309    return view;
310 }
311
312
313 static void
314 cell_sampler_view_destroy(struct pipe_context *pipe,
315                           struct pipe_sampler_view *view)
316 {
317    pipe_resource_reference(&view->texture, NULL);
318    FREE(view);
319 }
320
321
322 /**
323  * Map color and z/stencil framebuffer surfaces.
324  */
325 static void
326 cell_map_surfaces(struct cell_context *cell)
327 {
328 #if 0
329    struct pipe_screen *screen = cell->pipe.screen;
330 #endif
331    uint i;
332
333    for (i = 0; i < 1; i++) {
334       struct pipe_surface *ps = cell->framebuffer.cbufs[i];
335       if (ps) {
336          struct cell_resource *ct = cell_resource(ps->texture);
337 #if 0
338          cell->cbuf_map[i] = screen->buffer_map(screen,
339                                                 ct->buffer,
340                                                 (PIPE_BUFFER_USAGE_GPU_READ |
341                                                  PIPE_BUFFER_USAGE_GPU_WRITE));
342 #else
343          cell->cbuf_map[i] = ct->data;
344 #endif
345       }
346    }
347
348    {
349       struct pipe_surface *ps = cell->framebuffer.zsbuf;
350       if (ps) {
351          struct cell_resource *ct = cell_resource(ps->texture);
352 #if 0
353          cell->zsbuf_map = screen->buffer_map(screen,
354                                               ct->buffer,
355                                               (PIPE_BUFFER_USAGE_GPU_READ |
356                                                PIPE_BUFFER_USAGE_GPU_WRITE));
357 #else
358          cell->zsbuf_map = ct->data;
359 #endif
360       }
361    }
362 }
363
364
365 /**
366  * Unmap color and z/stencil framebuffer surfaces.
367  */
368 static void
369 cell_unmap_surfaces(struct cell_context *cell)
370 {
371    /*struct pipe_screen *screen = cell->pipe.screen;*/
372    uint i;
373
374    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
375       struct pipe_surface *ps = cell->framebuffer.cbufs[i];
376       if (ps && cell->cbuf_map[i]) {
377          /*struct cell_resource *ct = cell_resource(ps->texture);*/
378          assert(ps->texture);
379          /*assert(ct->buffer);*/
380
381          /*screen->buffer_unmap(screen, ct->buffer);*/
382          cell->cbuf_map[i] = NULL;
383       }
384    }
385
386    {
387       struct pipe_surface *ps = cell->framebuffer.zsbuf;
388       if (ps && cell->zsbuf_map) {
389          /*struct cell_resource *ct = cell_resource(ps->texture);*/
390          /*screen->buffer_unmap(screen, ct->buffer);*/
391          cell->zsbuf_map = NULL;
392       }
393    }
394 }
395
396
397 static void
398 cell_set_framebuffer_state(struct pipe_context *pipe,
399                            const struct pipe_framebuffer_state *fb)
400 {
401    struct cell_context *cell = cell_context(pipe);
402
403    if (1 /*memcmp(&cell->framebuffer, fb, sizeof(*fb))*/) {
404       uint i;
405
406       /* unmap old surfaces */
407       cell_unmap_surfaces(cell);
408
409       /* Finish any pending rendering to the current surface before
410        * installing a new surface!
411        */
412       cell_flush_int(cell, CELL_FLUSH_WAIT);
413
414       /* update my state
415        * (this is also where old surfaces will finally get freed)
416        */
417       cell->framebuffer.width = fb->width;
418       cell->framebuffer.height = fb->height;
419       cell->framebuffer.nr_cbufs = fb->nr_cbufs;
420       for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
421          pipe_surface_reference(&cell->framebuffer.cbufs[i], fb->cbufs[i]);
422       }
423       pipe_surface_reference(&cell->framebuffer.zsbuf, fb->zsbuf);
424
425       /* map new surfaces */
426       cell_map_surfaces(cell);
427
428       cell->dirty |= CELL_NEW_FRAMEBUFFER;
429    }
430 }
431
432
433
434 void
435 cell_init_state_functions(struct cell_context *cell)
436 {
437    cell->pipe.create_blend_state = cell_create_blend_state;
438    cell->pipe.bind_blend_state   = cell_bind_blend_state;
439    cell->pipe.delete_blend_state = cell_delete_blend_state;
440
441    cell->pipe.create_sampler_state = cell_create_sampler_state;
442    cell->pipe.bind_fragment_sampler_states = cell_bind_sampler_states;
443    cell->pipe.delete_sampler_state = cell_delete_sampler_state;
444
445    cell->pipe.set_fragment_sampler_views = cell_set_fragment_sampler_views;
446    cell->pipe.create_sampler_view = cell_create_sampler_view;
447    cell->pipe.sampler_view_destroy = cell_sampler_view_destroy;
448
449    cell->pipe.create_depth_stencil_alpha_state = cell_create_depth_stencil_alpha_state;
450    cell->pipe.bind_depth_stencil_alpha_state   = cell_bind_depth_stencil_alpha_state;
451    cell->pipe.delete_depth_stencil_alpha_state = cell_delete_depth_stencil_alpha_state;
452
453    cell->pipe.create_rasterizer_state = cell_create_rasterizer_state;
454    cell->pipe.bind_rasterizer_state   = cell_bind_rasterizer_state;
455    cell->pipe.delete_rasterizer_state = cell_delete_rasterizer_state;
456
457    cell->pipe.set_blend_color = cell_set_blend_color;
458    cell->pipe.set_stencil_ref = cell_set_stencil_ref;
459    cell->pipe.set_clip_state = cell_set_clip_state;
460
461    cell->pipe.set_framebuffer_state = cell_set_framebuffer_state;
462
463    cell->pipe.set_polygon_stipple = cell_set_polygon_stipple;
464    cell->pipe.set_scissor_state = cell_set_scissor_state;
465    cell->pipe.set_viewport_state = cell_set_viewport_state;
466 }