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