Don't pad renderbuffers to multiple of two pixels anymore.
[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 strb->Base._ActualFormat = info->base_format;
73 strb->Base.RedBits = info->red_bits;
74 strb->Base.GreenBits = info->green_bits;
75 strb->Base.BlueBits = info->blue_bits;
76 strb->Base.AlphaBits = info->alpha_bits;
77 strb->Base.DepthBits = info->depth_bits;
78 strb->Base.StencilBits = info->stencil_bits;
79 strb->Base.DataType = st_format_datatype(pipeFormat);
80
81 assert(strb->Base.DataType);
82
83 cpp = info->size;
84
85 if (!strb->surface) {
86 strb->surface = pipe->surface_alloc(pipe, pipeFormat);
87 assert(strb->surface);
88 if (!strb->surface)
89 return GL_FALSE;
90 }
91
92 /* free old region */
93 if (strb->surface->region) {
94 /* loop here since mapping is refcounted */
95 struct pipe_region *r = strb->surface->region;
96 while (r->map)
97 pipe->region_unmap(pipe, r);
98 pipe->region_release(pipe, &strb->surface->region);
99 }
100
101 strb->surface->region = pipe->region_alloc(pipe, cpp, width, height, flags);
102 if (!strb->surface->region)
103 return GL_FALSE; /* out of memory, try s/w buffer? */
104
105 ASSERT(strb->surface->region->buffer);
106 ASSERT(strb->surface->format);
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 struct st_renderbuffer *strb = st_renderbuffer(rb);
122 GET_CURRENT_CONTEXT(ctx);
123 if (ctx) {
124 struct pipe_context *pipe = ctx->st->pipe;
125 ASSERT(strb);
126 if (strb && strb->surface) {
127 if (strb->surface->region) {
128 pipe->region_release(pipe, &strb->surface->region);
129 }
130 free(strb->surface);
131 }
132 }
133 else {
134 _mesa_warning(NULL, "st_renderbuffer_delete() called, but no current context");
135 }
136 free(strb);
137 }
138
139
140 /**
141 * gl_renderbuffer::GetPointer()
142 */
143 static void *
144 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
145 GLint x, GLint y)
146 {
147 /* By returning NULL we force all software rendering to go through
148 * the span routines.
149 */
150 #if 0
151 assert(0); /* Should never get called with softpipe */
152 #endif
153 return NULL;
154 }
155
156
157 /**
158 * Called via ctx->Driver.NewFramebuffer()
159 */
160 static struct gl_framebuffer *
161 st_new_framebuffer(GLcontext *ctx, GLuint name)
162 {
163 /* XXX not sure we need to subclass gl_framebuffer for pipe */
164 return _mesa_new_framebuffer(ctx, name);
165 }
166
167
168 /**
169 * Called via ctx->Driver.NewRenderbuffer()
170 */
171 static struct gl_renderbuffer *
172 st_new_renderbuffer(GLcontext *ctx, GLuint name)
173 {
174 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
175 if (strb) {
176 _mesa_init_renderbuffer(&strb->Base, name);
177 strb->Base.Delete = st_renderbuffer_delete;
178 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
179 strb->Base.GetPointer = null_get_pointer;
180 return &strb->Base;
181 }
182 return NULL;
183 }
184
185
186 #if 000
187 struct gl_renderbuffer *
188 st_new_renderbuffer_fb(struct pipe_region *region, GLuint width, GLuint height)
189 {
190 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
191 if (!strb)
192 return;
193
194 _mesa_init_renderbuffer(&strb->Base, name);
195 strb->Base.Delete = st_renderbuffer_delete;
196 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
197 strb->Base.GetPointer = null_get_pointer;
198 strb->Base.Width = width;
199 strb->Base.Heigth = height;
200
201 strb->region = region;
202
203 return &strb->Base;
204 }
205
206 #else
207
208 struct gl_renderbuffer *
209 st_new_renderbuffer_fb(GLenum intFormat)
210 {
211 struct st_renderbuffer *strb;
212
213 strb = CALLOC_STRUCT(st_renderbuffer);
214 if (!strb) {
215 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
216 return NULL;
217 }
218
219 _mesa_init_renderbuffer(&strb->Base, 0);
220 strb->Base.ClassID = 0x42; /* XXX temp */
221 strb->Base.InternalFormat = intFormat;
222
223 switch (intFormat) {
224 case GL_RGB5:
225 case GL_RGBA8:
226 case GL_RGBA16:
227 strb->Base._BaseFormat = GL_RGBA;
228 break;
229 case GL_DEPTH_COMPONENT16:
230 case GL_DEPTH_COMPONENT32:
231 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
232 break;
233 case GL_DEPTH24_STENCIL8_EXT:
234 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
235 break;
236 case GL_STENCIL_INDEX8_EXT:
237 strb->Base._BaseFormat = GL_STENCIL_INDEX;
238 break;
239 default:
240 _mesa_problem(NULL,
241 "Unexpected intFormat in st_new_renderbuffer");
242 return NULL;
243 }
244
245 /* st-specific methods */
246 strb->Base.Delete = st_renderbuffer_delete;
247 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
248 strb->Base.GetPointer = null_get_pointer;
249
250 /* surface is allocate in alloc_renderbuffer_storage() */
251 strb->surface = NULL;
252
253 return &strb->Base;
254 }
255 #endif
256
257
258
259 /**
260 * Called via ctx->Driver.BindFramebufferEXT().
261 */
262 static void
263 st_bind_framebuffer(GLcontext *ctx, GLenum target,
264 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
265 {
266
267 }
268
269 /**
270 * Called by ctx->Driver.FramebufferRenderbuffer
271 */
272 static void
273 st_framebuffer_renderbuffer(GLcontext *ctx,
274 struct gl_framebuffer *fb,
275 GLenum attachment,
276 struct gl_renderbuffer *rb)
277 {
278 /* XXX no need for derivation? */
279 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
280 }
281
282
283 /**
284 * Called by ctx->Driver.RenderTexture
285 */
286 static void
287 st_render_texture(GLcontext *ctx,
288 struct gl_framebuffer *fb,
289 struct gl_renderbuffer_attachment *att)
290 {
291 struct st_context *st = ctx->st;
292 struct st_renderbuffer *strb;
293 struct gl_renderbuffer *rb;
294 struct pipe_context *pipe = st->pipe;
295 struct pipe_mipmap_tree *mt;
296
297 assert(!att->Renderbuffer);
298
299 /* create new renderbuffer which wraps the texture image */
300 rb = st_new_renderbuffer(ctx, 0);
301 if (!rb) {
302 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
303 return;
304 }
305
306 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
307 assert(rb->RefCount == 1);
308 rb->AllocStorage = NULL; /* should not get called */
309 strb = st_renderbuffer(rb);
310
311 /* get the mipmap tree for the texture */
312 mt = st_get_texobj_mipmap_tree(att->Texture);
313 assert(mt);
314 assert(mt->level[att->TextureLevel].width);
315
316 rb->Width = mt->level[att->TextureLevel].width;
317 rb->Height = mt->level[att->TextureLevel].height;
318
319 /* the renderbuffer's surface is inside the mipmap_tree: */
320 strb->surface = pipe->get_tex_surface(pipe, mt,
321 att->CubeMapFace,
322 att->TextureLevel,
323 att->Zoffset);
324 assert(strb->surface);
325
326 /*
327 printf("RENDER TO TEXTURE obj=%p mt=%p surf=%p %d x %d\n",
328 att->Texture, mt, strb->surface, rb->Width, rb->Height);
329 */
330
331 /* Invalidate buffer state so that the pipe's framebuffer state
332 * gets updated.
333 * That's where the new renderbuffer (which we just created) gets
334 * passed to the pipe as a (color/depth) render target.
335 */
336 st_invalidate_state(ctx, _NEW_BUFFERS);
337 }
338
339
340 /**
341 * Called via ctx->Driver.FinishRenderTexture.
342 */
343 static void
344 st_finish_render_texture(GLcontext *ctx,
345 struct gl_renderbuffer_attachment *att)
346 {
347 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
348
349 assert(strb);
350
351 ctx->st->pipe->flush(ctx->st->pipe, 0x0);
352
353 /*
354 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
355 */
356
357 pipe_surface_reference(&strb->surface, NULL);
358
359 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
360
361 /* restore previous framebuffer state */
362 st_invalidate_state(ctx, _NEW_BUFFERS);
363 }
364
365
366
367 void st_init_fbo_functions(struct dd_function_table *functions)
368 {
369 functions->NewFramebuffer = st_new_framebuffer;
370 functions->NewRenderbuffer = st_new_renderbuffer;
371 functions->BindFramebuffer = st_bind_framebuffer;
372 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
373 functions->RenderTexture = st_render_texture;
374 functions->FinishRenderTexture = st_finish_render_texture;
375 /* no longer needed by core Mesa, drivers handle resizes...
376 functions->ResizeBuffers = st_resize_buffers;
377 */
378 }