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