added softpipe_mipmap_tree_layout
[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 "main/imports.h"
33 #include "main/macros.h"
34 #include "pipe/draw/draw_context.h"
35 #include "pipe/p_defines.h"
36 #include "sp_buffer.h"
37 #include "sp_clear.h"
38 #include "sp_context.h"
39 #include "sp_prim_setup.h"
40 #include "sp_region.h"
41 #include "sp_state.h"
42 #include "sp_surface.h"
43 #include "sp_tex_layout.h"
44 #include "sp_winsys.h"
45
46
47
48 /**
49 * Return list of supported surface/texture formats.
50 * If we find texture and drawable support differs, add a selector
51 * parameter or another function.
52 */
53 static const GLuint *
54 softpipe_supported_formats(struct pipe_context *pipe, GLuint *numFormats)
55 {
56 static const GLuint 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 }
77
78
79
80 static void map_surfaces(struct softpipe_context *sp)
81 {
82 struct pipe_context *pipe = &sp->pipe;
83 GLuint i;
84
85 for (i = 0; i < sp->framebuffer.num_cbufs; i++) {
86 struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.cbufs[i]); pipe->region_map(pipe, sps->surface.region);
87 }
88
89 if (sp->framebuffer.zbuf) {
90 struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
91 pipe->region_map(pipe, sps->surface.region);
92 }
93
94 /* XXX depth & stencil bufs */
95 }
96
97
98 static void unmap_surfaces(struct softpipe_context *sp)
99 {
100 struct pipe_context *pipe = &sp->pipe;
101 GLuint i;
102
103 for (i = 0; i < sp->framebuffer.num_cbufs; i++) {
104 struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.cbufs[i]);
105 pipe->region_unmap(pipe, sps->surface.region);
106 }
107
108 if (sp->framebuffer.zbuf) {
109 struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
110 pipe->region_unmap(pipe, sps->surface.region);
111 }
112 /* XXX depth & stencil bufs */
113 }
114
115
116 static void softpipe_destroy( struct pipe_context *pipe )
117 {
118 struct softpipe_context *softpipe = softpipe_context( pipe );
119
120 draw_destroy( softpipe->draw );
121
122 free( softpipe );
123 }
124
125
126 static void softpipe_draw_vb( struct pipe_context *pipe,
127 struct vertex_buffer *VB )
128 {
129 struct softpipe_context *softpipe = softpipe_context( pipe );
130
131 if (softpipe->dirty)
132 softpipe_update_derived( softpipe );
133
134 /* XXX move mapping/unmapping to higher/coarser level? */
135 map_surfaces(softpipe);
136 draw_vb( softpipe->draw, VB );
137 unmap_surfaces(softpipe);
138 }
139
140
141 static void
142 softpipe_draw_vertices(struct pipe_context *pipe,
143 GLuint mode,
144 GLuint numVertex, const GLfloat *verts,
145 GLuint numAttribs, const GLuint attribs[])
146 {
147 struct softpipe_context *softpipe = softpipe_context( pipe );
148
149 if (softpipe->dirty)
150 softpipe_update_derived( softpipe );
151
152 /* XXX move mapping/unmapping to higher/coarser level? */
153 map_surfaces(softpipe);
154 draw_vertices(softpipe->draw, mode, numVertex, verts, numAttribs, attribs);
155 unmap_surfaces(softpipe);
156 }
157
158
159
160 static void softpipe_reset_occlusion_counter(struct pipe_context *pipe)
161 {
162 struct softpipe_context *softpipe = softpipe_context( pipe );
163 softpipe->occlusion_counter = 0;
164 }
165
166 /* XXX pipe param should be const */
167 static GLuint softpipe_get_occlusion_counter(struct pipe_context *pipe)
168 {
169 struct softpipe_context *softpipe = softpipe_context( pipe );
170 return softpipe->occlusion_counter;
171 }
172
173
174 struct pipe_context *softpipe_create( struct softpipe_winsys *sws )
175 {
176 struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);
177
178 softpipe->pipe.destroy = softpipe_destroy;
179
180 softpipe->pipe.supported_formats = softpipe_supported_formats;
181
182 softpipe->pipe.set_alpha_test_state = softpipe_set_alpha_test_state;
183 softpipe->pipe.set_blend_color = softpipe_set_blend_color;
184 softpipe->pipe.set_blend_state = softpipe_set_blend_state;
185 softpipe->pipe.set_clip_state = softpipe_set_clip_state;
186 softpipe->pipe.set_clear_color_state = softpipe_set_clear_color_state;
187 softpipe->pipe.set_depth_state = softpipe_set_depth_test_state;
188 softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;
189 softpipe->pipe.set_fs_state = softpipe_set_fs_state;
190 softpipe->pipe.set_polygon_stipple = softpipe_set_polygon_stipple;
191 softpipe->pipe.set_sampler_state = softpipe_set_sampler_state;
192 softpipe->pipe.set_scissor_state = softpipe_set_scissor_state;
193 softpipe->pipe.set_setup_state = softpipe_set_setup_state;
194 softpipe->pipe.set_stencil_state = softpipe_set_stencil_state;
195 softpipe->pipe.set_texture_state = softpipe_set_texture_state;
196 softpipe->pipe.set_viewport_state = softpipe_set_viewport_state;
197 softpipe->pipe.draw_vb = softpipe_draw_vb;
198 softpipe->pipe.draw_vertices = softpipe_draw_vertices;
199 softpipe->pipe.clear = softpipe_clear;
200 softpipe->pipe.reset_occlusion_counter = softpipe_reset_occlusion_counter;
201 softpipe->pipe.get_occlusion_counter = softpipe_get_occlusion_counter;
202
203 softpipe->pipe.mipmap_tree_layout = softpipe_mipmap_tree_layout;
204
205 softpipe->quad.polygon_stipple = sp_quad_polygon_stipple_stage(softpipe);
206 softpipe->quad.shade = sp_quad_shade_stage(softpipe);
207 softpipe->quad.alpha_test = sp_quad_alpha_test_stage(softpipe);
208 softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);
209 softpipe->quad.stencil_test = sp_quad_stencil_test_stage(softpipe);
210 softpipe->quad.occlusion = sp_quad_occlusion_stage(softpipe);
211 softpipe->quad.coverage = sp_quad_coverage_stage(softpipe);
212 softpipe->quad.bufloop = sp_quad_bufloop_stage(softpipe);
213 softpipe->quad.blend = sp_quad_blend_stage(softpipe);
214 softpipe->quad.colormask = sp_quad_colormask_stage(softpipe);
215 softpipe->quad.output = sp_quad_output_stage(softpipe);
216
217 softpipe->winsys = sws;
218
219 /*
220 * Create drawing context and plug our rendering stage into it.
221 */
222 softpipe->draw = draw_create();
223 assert(softpipe->draw);
224 draw_set_setup_stage(softpipe->draw, sp_draw_render_stage(softpipe));
225
226 sp_init_buffer_functions(softpipe);
227 sp_init_region_functions(softpipe);
228 sp_init_surface_functions(softpipe);
229
230 /*
231 * XXX we could plug GL selection/feedback into the drawing pipeline
232 * by specifying a different setup/render stage.
233 */
234
235 return &softpipe->pipe;
236 }