OSDN Git Service

Remove many dependencies on mesa headers.
[android-x86/external-mesa.git] / src / mesa / pipe / softpipe / sp_context.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 /* Author:
29  *    Keith Whitwell <keith@tungstengraphics.com>
30  */
31
32 #include "pipe/draw/draw_context.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_util.h"
35 #include "sp_clear.h"
36 #include "sp_context.h"
37 #include "sp_flush.h"
38 #include "sp_prim_setup.h"
39 #include "sp_region.h"
40 #include "sp_state.h"
41 #include "sp_surface.h"
42 #include "sp_tex_layout.h"
43 #include "sp_winsys.h"
44
45
46
47 /**
48  * Return list of supported surface/texture formats.
49  * If we find texture and drawable support differs, add a selector
50  * parameter or another function.
51  */
52 static const unsigned *
53 softpipe_supported_formats(struct pipe_context *pipe, unsigned *numFormats)
54 {
55 #if 0
56    static const unsigned supported[] = {
57       PIPE_FORMAT_U_R8_G8_B8_A8,
58       PIPE_FORMAT_U_A8_R8_G8_B8,
59       PIPE_FORMAT_U_R5_G6_B5,
60       PIPE_FORMAT_U_L8,
61       PIPE_FORMAT_U_A8,
62       PIPE_FORMAT_U_I8,
63       PIPE_FORMAT_U_L8_A8,
64       PIPE_FORMAT_S_R16_G16_B16_A16,
65       PIPE_FORMAT_YCBCR,
66       PIPE_FORMAT_YCBCR_REV,
67       PIPE_FORMAT_U_Z16,
68       PIPE_FORMAT_U_Z32,
69       PIPE_FORMAT_F_Z32,
70       PIPE_FORMAT_S8_Z24,
71       PIPE_FORMAT_U_S8
72    };
73
74    *numFormats = sizeof(supported)/sizeof(supported[0]);
75    return supported;
76 #else
77    struct softpipe_context *softpipe = softpipe_context( pipe );
78    return softpipe->winsys->supported_formats( softpipe->winsys, numFormats );
79 #endif
80 }
81
82
83 /** XXX remove these? */
84 #define MAX_TEXTURE_LEVELS 11
85 #define MAX_TEXTURE_RECT_SIZE 2048
86 #define MAX_3D_TEXTURE_LEVELS 8
87
88 static void
89 softpipe_max_texture_size(struct pipe_context *pipe, unsigned textureType,
90                           unsigned *maxWidth, unsigned *maxHeight,
91                           unsigned *maxDepth)
92 {
93    switch (textureType) {
94    case PIPE_TEXTURE_1D:
95       *maxWidth = 1 << (MAX_TEXTURE_LEVELS - 1);
96       break;
97    case PIPE_TEXTURE_2D:
98       *maxWidth =
99       *maxHeight = 1 << (MAX_TEXTURE_LEVELS - 1);
100       break;
101    case PIPE_TEXTURE_3D:
102       *maxWidth =
103       *maxHeight =
104       *maxDepth = 1 << (MAX_3D_TEXTURE_LEVELS - 1);
105       break;
106    case PIPE_TEXTURE_CUBE:
107       *maxWidth =
108       *maxHeight = MAX_TEXTURE_RECT_SIZE;
109       break;
110    default:
111       assert(0);
112    }
113 }
114
115
116 void
117 softpipe_map_surfaces(struct softpipe_context *sp)
118 {
119    struct pipe_context *pipe = &sp->pipe;
120    unsigned i;
121
122    for (i = 0; i < sp->framebuffer.num_cbufs; i++) {
123       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.cbufs[i]);
124       if (sps->surface.region)
125          pipe->region_map(pipe, sps->surface.region);
126    }
127
128    if (sp->framebuffer.zbuf) {
129       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
130       if (sps->surface.region)
131          pipe->region_map(pipe, sps->surface.region);
132    }
133
134    if (sp->framebuffer.sbuf) {
135       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.sbuf);
136       if (sps->surface.region)
137          pipe->region_map(pipe, sps->surface.region);
138    }
139
140    /* textures */
141    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
142       struct pipe_mipmap_tree *mt = sp->texture[i];
143       if (mt) {
144          pipe->region_map(pipe, mt->region);
145       }
146    }
147
148    /* XXX depth & stencil bufs */
149 }
150
151
152 void
153 softpipe_unmap_surfaces(struct softpipe_context *sp)
154 {
155    struct pipe_context *pipe = &sp->pipe;
156    unsigned i;
157
158    for (i = 0; i < sp->framebuffer.num_cbufs; i++) {
159       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.cbufs[i]);
160       if (sps->surface.region)
161          pipe->region_unmap(pipe, sps->surface.region);
162    }
163
164    if (sp->framebuffer.zbuf) {
165       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
166       if (sps->surface.region)
167          pipe->region_unmap(pipe, sps->surface.region);
168    }
169
170    if (sp->framebuffer.sbuf) {
171       struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.sbuf);
172       if (sps->surface.region)
173          pipe->region_unmap(pipe, sps->surface.region);
174    }
175
176    /* textures */
177    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
178       struct pipe_mipmap_tree *mt = sp->texture[i];
179       if (mt) {
180          pipe->region_unmap(pipe, mt->region);
181       }
182    }
183
184    /* XXX depth & stencil bufs */
185 }
186
187
188 static void softpipe_destroy( struct pipe_context *pipe )
189 {
190    struct softpipe_context *softpipe = softpipe_context( pipe );
191
192    draw_destroy( softpipe->draw );
193
194    free( softpipe );
195 }
196
197
198 static void softpipe_draw_vb( struct pipe_context *pipe,
199                              struct vertex_buffer *VB )
200 {
201    struct softpipe_context *softpipe = softpipe_context( pipe );
202
203    if (softpipe->dirty)
204       softpipe_update_derived( softpipe );
205
206    /* XXX move mapping/unmapping to higher/coarser level? */
207    softpipe_map_surfaces(softpipe);
208    draw_vb( softpipe->draw, VB );
209    softpipe_unmap_surfaces(softpipe);
210 }
211
212
213 static void
214 softpipe_draw_vertices(struct pipe_context *pipe,
215                        unsigned mode,
216                        unsigned numVertex, const float *verts,
217                        unsigned numAttribs, const unsigned attribs[])
218 {
219    struct softpipe_context *softpipe = softpipe_context( pipe );
220
221    if (softpipe->dirty)
222       softpipe_update_derived( softpipe );
223
224    /* XXX move mapping/unmapping to higher/coarser level? */
225    softpipe_map_surfaces(softpipe);
226    draw_vertices(softpipe->draw, mode, numVertex, verts, numAttribs, attribs);
227    softpipe_unmap_surfaces(softpipe);
228 }
229
230
231
232 static void softpipe_reset_occlusion_counter(struct pipe_context *pipe)
233 {
234    struct softpipe_context *softpipe = softpipe_context( pipe );
235    softpipe->occlusion_counter = 0;
236 }
237
238 /* XXX pipe param should be const */
239 static unsigned softpipe_get_occlusion_counter(struct pipe_context *pipe)
240 {
241    struct softpipe_context *softpipe = softpipe_context( pipe );
242    return softpipe->occlusion_counter;
243 }
244
245 static const char *softpipe_get_name( struct pipe_context *pipe )
246 {
247    return "softpipe";
248 }
249
250 static const char *softpipe_get_vendor( struct pipe_context *pipe )
251 {
252    return "Tungsten Graphics, Inc.";
253 }
254
255
256 struct pipe_context *softpipe_create( struct pipe_winsys *pipe_winsys,
257                                       struct softpipe_winsys *softpipe_winsys )
258 {
259    struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);
260
261    softpipe->pipe.winsys = pipe_winsys;
262    softpipe->pipe.destroy = softpipe_destroy;
263
264    /* queries */
265    softpipe->pipe.supported_formats = softpipe_supported_formats;
266    softpipe->pipe.max_texture_size = softpipe_max_texture_size;
267
268    /* state setters */
269    softpipe->pipe.set_alpha_test_state = softpipe_set_alpha_test_state;
270    softpipe->pipe.set_blend_color = softpipe_set_blend_color;
271    softpipe->pipe.set_blend_state = softpipe_set_blend_state;
272    softpipe->pipe.set_clip_state = softpipe_set_clip_state;
273    softpipe->pipe.set_clear_color_state = softpipe_set_clear_color_state;
274    softpipe->pipe.set_depth_state = softpipe_set_depth_test_state;
275    softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;
276    softpipe->pipe.set_fs_state = softpipe_set_fs_state;
277    softpipe->pipe.set_vs_state = softpipe_set_vs_state;
278    softpipe->pipe.set_polygon_stipple = softpipe_set_polygon_stipple;
279    softpipe->pipe.set_sampler_state = softpipe_set_sampler_state;
280    softpipe->pipe.set_scissor_state = softpipe_set_scissor_state;
281    softpipe->pipe.set_setup_state = softpipe_set_setup_state;
282    softpipe->pipe.set_stencil_state = softpipe_set_stencil_state;
283    softpipe->pipe.set_texture_state = softpipe_set_texture_state;
284    softpipe->pipe.set_viewport_state = softpipe_set_viewport_state;
285    softpipe->pipe.set_vertex_buffer = softpipe_set_vertex_buffer;
286    softpipe->pipe.set_vertex_element = softpipe_set_vertex_element;
287
288    softpipe->pipe.draw_vb = softpipe_draw_vb;
289    softpipe->pipe.draw_vertices = softpipe_draw_vertices;
290    softpipe->pipe.draw_arrays = softpipe_draw_arrays;
291
292    softpipe->pipe.clear = softpipe_clear;
293    softpipe->pipe.flush = softpipe_flush;
294    softpipe->pipe.reset_occlusion_counter = softpipe_reset_occlusion_counter;
295    softpipe->pipe.get_occlusion_counter = softpipe_get_occlusion_counter;
296    softpipe->pipe.get_name = softpipe_get_name;
297    softpipe->pipe.get_vendor = softpipe_get_vendor;
298
299    /* textures */
300    softpipe->pipe.mipmap_tree_layout = softpipe_mipmap_tree_layout;
301    softpipe->pipe.get_tex_surface = softpipe_get_tex_surface;
302
303    /* setup quad rendering stages */
304    softpipe->quad.polygon_stipple = sp_quad_polygon_stipple_stage(softpipe);
305    softpipe->quad.shade = sp_quad_shade_stage(softpipe);
306    softpipe->quad.alpha_test = sp_quad_alpha_test_stage(softpipe);
307    softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);
308    softpipe->quad.stencil_test = sp_quad_stencil_test_stage(softpipe);
309    softpipe->quad.occlusion = sp_quad_occlusion_stage(softpipe);
310    softpipe->quad.coverage = sp_quad_coverage_stage(softpipe);
311    softpipe->quad.bufloop = sp_quad_bufloop_stage(softpipe);
312    softpipe->quad.blend = sp_quad_blend_stage(softpipe);
313    softpipe->quad.colormask = sp_quad_colormask_stage(softpipe);
314    softpipe->quad.output = sp_quad_output_stage(softpipe);
315
316    softpipe->winsys = softpipe_winsys;
317
318    /*
319     * Create drawing context and plug our rendering stage into it.
320     */
321    softpipe->draw = draw_create();
322    assert(softpipe->draw);
323    draw_set_setup_stage(softpipe->draw, sp_draw_render_stage(softpipe));
324
325    draw_set_vertex_array_info(softpipe->draw,
326                               softpipe->vertex_buffer, 
327                               softpipe->vertex_element);
328
329    sp_init_region_functions(softpipe);
330    sp_init_surface_functions(softpipe);
331
332    /*
333     * XXX we could plug GL selection/feedback into the drawing pipeline
334     * by specifying a different setup/render stage.
335     */
336
337    return &softpipe->pipe;
338 }