pipe->region_alloc() now takes width instead of pitch, plus a flags param
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.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
29 /**
30 * Framebuffer/renderbuffer functions.
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/fbobject.h"
39 #include "main/framebuffer.h"
40 #include "main/renderbuffer.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "st_context.h"
45 #include "st_cb_fbo.h"
46 #include "st_cb_texture.h"
47 #include "st_format.h"
48 #include "st_public.h"
49
50
51
52 /**
53 * gl_renderbuffer::AllocStorage()
54 */
55 static GLboolean
56 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
57 GLenum internalFormat,
58 GLuint width, GLuint height)
59 {
60 struct pipe_context *pipe = ctx->st->pipe;
61 struct st_renderbuffer *strb = st_renderbuffer(rb);
62 const GLuint pipeFormat
63 = st_choose_pipe_format(pipe, internalFormat, GL_NONE, GL_NONE);
64 const struct pipe_format_info *info = st_get_format_info(pipeFormat);
65 GLuint cpp;
66 GLbitfield flags = PIPE_SURFACE_FLAG_RENDER; /* want to render to surface */
67
68 assert(info);
69 if (!info)
70 return GL_FALSE;
71
72 switch (pipeFormat) {
73 case PIPE_FORMAT_S8_Z24:
74 strb->Base.DataType = GL_UNSIGNED_INT;
75 break;
76 default:
77 strb->Base.DataType = GL_UNSIGNED_BYTE; /* XXX fix */
78 }
79
80 strb->Base._ActualFormat = info->base_format;
81 strb->Base.RedBits = info->red_bits;
82 strb->Base.GreenBits = info->green_bits;
83 strb->Base.BlueBits = info->blue_bits;
84 strb->Base.AlphaBits = info->alpha_bits;
85 strb->Base.DepthBits = info->depth_bits;
86 strb->Base.StencilBits = info->stencil_bits;
87
88 cpp = info->size;
89
90 if (!strb->surface) {
91 strb->surface = pipe->surface_alloc(pipe, pipeFormat);
92 assert(strb->surface);
93 if (!strb->surface)
94 return GL_FALSE;
95 }
96
97 /* free old region */
98 if (strb->surface->region) {
99 pipe->region_release(pipe, &strb->surface->region);
100 }
101
102 strb->surface->region = pipe->region_alloc(pipe, cpp, width, height, flags);
103 if (!strb->surface->region)
104 return GL_FALSE; /* out of memory, try s/w buffer? */
105
106 ASSERT(strb->surface->region->buffer);
107
108 strb->Base.Width = strb->surface->width = width;
109 strb->Base.Height = strb->surface->height = height;
110
111 return GL_TRUE;
112 }
113
114
115 /**
116 * gl_renderbuffer::Delete()
117 */
118 static void
119 st_renderbuffer_delete(struct gl_renderbuffer *rb)
120 {
121 GET_CURRENT_CONTEXT(ctx);
122 struct pipe_context *pipe = ctx->st->pipe;
123 struct st_renderbuffer *strb = st_renderbuffer(rb);
124 ASSERT(strb);
125 if (strb && strb->surface) {
126 if (strb->surface->region) {
127 pipe->region_release(pipe, &strb->surface->region);
128 }
129 free(strb->surface);
130 }
131 free(strb);
132 }
133
134
135 /**
136 * gl_renderbuffer::GetPointer()
137 */
138 static void *
139 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
140 GLint x, GLint y)
141 {
142 /* By returning NULL we force all software rendering to go through
143 * the span routines.
144 */
145 #if 0
146 assert(0); /* Should never get called with softpipe */
147 #endif
148 return NULL;
149 }
150
151
152 /**
153 * Called via ctx->Driver.NewFramebuffer()
154 */
155 static struct gl_framebuffer *
156 st_new_framebuffer(GLcontext *ctx, GLuint name)
157 {
158 /* XXX not sure we need to subclass gl_framebuffer for pipe */
159 return _mesa_new_framebuffer(ctx, name);
160 }
161
162
163 /**
164 * Called via ctx->Driver.NewRenderbuffer()
165 */
166 static struct gl_renderbuffer *
167 st_new_renderbuffer(GLcontext *ctx, GLuint name)
168 {
169 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
170 if (strb) {
171 _mesa_init_renderbuffer(&strb->Base, name);
172 strb->Base.Delete = st_renderbuffer_delete;
173 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
174 strb->Base.GetPointer = null_get_pointer;
175 return &strb->Base;
176 }
177 return NULL;
178 }
179
180
181 #if 000
182 struct gl_renderbuffer *
183 st_new_renderbuffer_fb(struct pipe_region *region, GLuint width, GLuint height)
184 {
185 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
186 if (!strb)
187 return;
188
189 _mesa_init_renderbuffer(&strb->Base, name);
190 strb->Base.Delete = st_renderbuffer_delete;
191 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
192 strb->Base.GetPointer = null_get_pointer;
193 strb->Base.Width = width;
194 strb->Base.Heigth = height;
195
196 strb->region = region;
197
198 return &strb->Base;
199 }
200
201 #else
202
203 struct gl_renderbuffer *
204 st_new_renderbuffer_fb(GLenum intFormat)
205 {
206 struct st_renderbuffer *strb;
207
208 strb = CALLOC_STRUCT(st_renderbuffer);
209 if (!strb) {
210 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
211 return NULL;
212 }
213
214 _mesa_init_renderbuffer(&strb->Base, 0);
215 strb->Base.ClassID = 0x42; /* XXX temp */
216 strb->Base.InternalFormat = intFormat;
217
218 switch (intFormat) {
219 case GL_RGB5:
220 case GL_RGBA8:
221 strb->Base._BaseFormat = GL_RGBA;
222 break;
223 case GL_DEPTH_COMPONENT16:
224 case GL_DEPTH_COMPONENT32:
225 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
226 break;
227 case GL_DEPTH24_STENCIL8_EXT:
228 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
229 break;
230 default:
231 _mesa_problem(NULL,
232 "Unexpected intFormat in st_new_renderbuffer");
233 return NULL;
234 }
235
236 /* st-specific methods */
237 strb->Base.Delete = st_renderbuffer_delete;
238 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
239 strb->Base.GetPointer = null_get_pointer;
240
241 /* surface is allocate in alloc_renderbuffer_storage() */
242 strb->surface = NULL;
243
244 return &strb->Base;
245 }
246 #endif
247
248
249
250 /**
251 * Called via ctx->Driver.BindFramebufferEXT().
252 */
253 static void
254 st_bind_framebuffer(GLcontext *ctx, GLenum target,
255 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
256 {
257
258 }
259
260 /**
261 * Called by ctx->Driver.FramebufferRenderbuffer
262 */
263 static void
264 st_framebuffer_renderbuffer(GLcontext *ctx,
265 struct gl_framebuffer *fb,
266 GLenum attachment,
267 struct gl_renderbuffer *rb)
268 {
269 /* XXX no need for derivation? */
270 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
271 }
272
273
274 /**
275 * Called by ctx->Driver.RenderTexture
276 */
277 static void
278 st_render_texture(GLcontext *ctx,
279 struct gl_framebuffer *fb,
280 struct gl_renderbuffer_attachment *att)
281 {
282 struct st_context *st = ctx->st;
283 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
284 struct pipe_context *pipe = st->pipe;
285 struct pipe_framebuffer_state framebuffer;
286 struct pipe_mipmap_tree *mt;
287
288 mt = st_get_texobj_mipmap_tree(att->Texture);
289
290 /* the renderbuffer's surface is inside the mipmap_tree: */
291 strb->surface = pipe->get_tex_surface(pipe, mt,
292 att->CubeMapFace,
293 att->TextureLevel,
294 att->Zoffset);
295
296 /* update pipe's framebuffer state */
297 memset(&framebuffer, 0, sizeof(framebuffer));
298 framebuffer.num_cbufs = 1;
299 framebuffer.cbufs[0] = strb->surface;
300 if (memcmp(&framebuffer, &st->state.framebuffer, sizeof(framebuffer)) != 0) {
301 st->state.framebuffer = framebuffer;
302 st->pipe->set_framebuffer_state( st->pipe, &framebuffer );
303 }
304 }
305
306
307 /**
308 * Called via ctx->Driver.FinishRenderTexture.
309 */
310 static void
311 st_finish_render_texture(GLcontext *ctx,
312 struct gl_renderbuffer_attachment *att)
313 {
314 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
315
316 pipe_surface_unreference(&strb->surface);
317
318 /* restore previous framebuffer state */
319 st_invalidate_state(ctx, _NEW_BUFFERS);
320 }
321
322
323
324 void st_init_fbo_functions(struct dd_function_table *functions)
325 {
326 functions->NewFramebuffer = st_new_framebuffer;
327 functions->NewRenderbuffer = st_new_renderbuffer;
328 functions->BindFramebuffer = st_bind_framebuffer;
329 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
330 functions->RenderTexture = st_render_texture;
331 functions->FinishRenderTexture = st_finish_render_texture;
332 /* no longer needed by core Mesa, drivers handle resizes...
333 functions->ResizeBuffers = st_resize_buffers;
334 */
335 }