gallium: Use MALLOC().
[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 "pipe/p_inlines.h"
45 #include "pipe/p_winsys.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_cb_texture.h"
49 #include "st_format.h"
50 #include "st_public.h"
51
52
53
54 /**
55 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
56 */
57 static int
58 init_renderbuffer_bits(struct st_renderbuffer *strb,
59 enum pipe_format pipeFormat)
60 {
61 struct pipe_format_info info;
62
63 if (!st_get_format_info( pipeFormat, &info )) {
64 assert( 0 );
65 }
66
67 strb->Base._ActualFormat = info.base_format;
68 strb->Base.RedBits = info.red_bits;
69 strb->Base.GreenBits = info.green_bits;
70 strb->Base.BlueBits = info.blue_bits;
71 strb->Base.AlphaBits = info.alpha_bits;
72 strb->Base.DepthBits = info.depth_bits;
73 strb->Base.StencilBits = info.stencil_bits;
74 strb->Base.DataType = st_format_datatype(pipeFormat);
75
76 return info.size;
77 }
78
79
80 /**
81 * gl_renderbuffer::AllocStorage()
82 */
83 static GLboolean
84 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
85 GLenum internalFormat,
86 GLuint width, GLuint height)
87 {
88 struct pipe_context *pipe = ctx->st->pipe;
89 struct st_renderbuffer *strb = st_renderbuffer(rb);
90 enum pipe_format pipeFormat;
91 GLbitfield flags = 0x0; /* XXX needed? */
92
93 if (!strb->surface) {
94 strb->surface = pipe->winsys->surface_alloc(pipe->winsys);
95 assert(strb->surface);
96 assert(strb->surface->refcount);
97 assert(strb->surface->winsys);
98 if (!strb->surface)
99 return GL_FALSE;
100 }
101
102 if (strb->surface->buffer)
103 pipe_buffer_reference(pipe->winsys, &strb->surface->buffer,
104 NULL);
105
106 /* Determine surface format here */
107 if (strb->format != PIPE_FORMAT_NONE) {
108 assert(strb->format != 0);
109 /* we'll hit this for front/back color bufs */
110 pipeFormat = strb->format;
111 }
112 else {
113 pipeFormat = st_choose_renderbuffer_format(pipe, internalFormat);
114 }
115
116 init_renderbuffer_bits(strb, pipeFormat);
117
118 pipe->winsys->surface_alloc_storage(pipe->winsys,
119 strb->surface,
120 width,
121 height,
122 pipeFormat,
123 flags);
124 if (!strb->surface->buffer)
125 return GL_FALSE; /* out of memory, try s/w buffer? */
126
127 ASSERT(strb->surface->buffer);
128 ASSERT(strb->surface->format);
129 ASSERT(strb->surface->cpp);
130 ASSERT(strb->surface->width == width);
131 ASSERT(strb->surface->height == height);
132 ASSERT(strb->surface->pitch);
133
134 strb->Base.Width = width;
135 strb->Base.Height = height;
136
137 return GL_TRUE;
138 }
139
140
141 /**
142 * gl_renderbuffer::Delete()
143 */
144 static void
145 st_renderbuffer_delete(struct gl_renderbuffer *rb)
146 {
147 struct st_renderbuffer *strb = st_renderbuffer(rb);
148 ASSERT(strb);
149 if (strb->surface) {
150 struct pipe_winsys *ws = strb->surface->winsys;
151 ws->surface_release(ws, &strb->surface);
152 }
153 free(strb);
154 }
155
156
157 /**
158 * gl_renderbuffer::GetPointer()
159 */
160 static void *
161 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
162 GLint x, GLint y)
163 {
164 /* By returning NULL we force all software rendering to go through
165 * the span routines.
166 */
167 #if 0
168 assert(0); /* Should never get called with softpipe */
169 #endif
170 return NULL;
171 }
172
173
174 /**
175 * Called via ctx->Driver.NewFramebuffer()
176 */
177 static struct gl_framebuffer *
178 st_new_framebuffer(GLcontext *ctx, GLuint name)
179 {
180 /* XXX not sure we need to subclass gl_framebuffer for pipe */
181 return _mesa_new_framebuffer(ctx, name);
182 }
183
184
185 /**
186 * Called via ctx->Driver.NewRenderbuffer()
187 */
188 static struct gl_renderbuffer *
189 st_new_renderbuffer(GLcontext *ctx, GLuint name)
190 {
191 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
192 if (strb) {
193 _mesa_init_renderbuffer(&strb->Base, name);
194 strb->Base.Delete = st_renderbuffer_delete;
195 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
196 strb->Base.GetPointer = null_get_pointer;
197 strb->format = PIPE_FORMAT_NONE;
198 return &strb->Base;
199 }
200 return NULL;
201 }
202
203
204 /**
205 * Allocate a renderbuffer for a an on-screen window (not a user-created
206 * renderbuffer). The window system code determines the format.
207 */
208 struct gl_renderbuffer *
209 st_new_renderbuffer_fb(enum pipe_format format)
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 = 0x4242; /* just a unique value */
221 strb->format = format;
222
223 switch (format) {
224 case PIPE_FORMAT_A8R8G8B8_UNORM:
225 case PIPE_FORMAT_B8G8R8A8_UNORM:
226 case PIPE_FORMAT_A1R5G5B5_UNORM:
227 case PIPE_FORMAT_A4R4G4B4_UNORM:
228 case PIPE_FORMAT_R5G6B5_UNORM:
229 strb->Base.InternalFormat = GL_RGBA;
230 strb->Base._BaseFormat = GL_RGBA;
231 break;
232 case PIPE_FORMAT_Z16_UNORM:
233 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
234 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
235 break;
236 case PIPE_FORMAT_Z32_UNORM:
237 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
238 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
239 break;
240 case PIPE_FORMAT_S8Z24_UNORM:
241 case PIPE_FORMAT_Z24S8_UNORM:
242 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
243 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
244 break;
245 case PIPE_FORMAT_S8_UNORM:
246 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
247 strb->Base._BaseFormat = GL_STENCIL_INDEX;
248 break;
249 case PIPE_FORMAT_R16G16B16A16_SNORM:
250 strb->Base.InternalFormat = GL_RGBA16;
251 strb->Base._BaseFormat = GL_RGBA;
252 break;
253 default:
254 _mesa_problem(NULL,
255 "Unexpected format in st_new_renderbuffer_fb");
256 return NULL;
257 }
258
259 /* st-specific methods */
260 strb->Base.Delete = st_renderbuffer_delete;
261 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
262 strb->Base.GetPointer = null_get_pointer;
263
264 /* surface is allocated in st_renderbuffer_alloc_storage() */
265 strb->surface = NULL;
266
267 return &strb->Base;
268 }
269
270
271
272
273 /**
274 * Called via ctx->Driver.BindFramebufferEXT().
275 */
276 static void
277 st_bind_framebuffer(GLcontext *ctx, GLenum target,
278 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
279 {
280
281 }
282
283 /**
284 * Called by ctx->Driver.FramebufferRenderbuffer
285 */
286 static void
287 st_framebuffer_renderbuffer(GLcontext *ctx,
288 struct gl_framebuffer *fb,
289 GLenum attachment,
290 struct gl_renderbuffer *rb)
291 {
292 /* XXX no need for derivation? */
293 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
294 }
295
296
297 /**
298 * Called by ctx->Driver.RenderTexture
299 */
300 static void
301 st_render_texture(GLcontext *ctx,
302 struct gl_framebuffer *fb,
303 struct gl_renderbuffer_attachment *att)
304 {
305 struct st_context *st = ctx->st;
306 struct st_renderbuffer *strb;
307 struct gl_renderbuffer *rb;
308 struct pipe_context *pipe = st->pipe;
309 struct pipe_texture *pt;
310
311 assert(!att->Renderbuffer);
312
313 /* create new renderbuffer which wraps the texture image */
314 rb = st_new_renderbuffer(ctx, 0);
315 if (!rb) {
316 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
317 return;
318 }
319
320 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
321 assert(rb->RefCount == 1);
322 rb->AllocStorage = NULL; /* should not get called */
323 strb = st_renderbuffer(rb);
324
325 /* get the texture for the texture object */
326 pt = st_get_texobj_texture(att->Texture);
327 assert(pt);
328 assert(pt->width[att->TextureLevel]);
329
330 rb->Width = pt->width[att->TextureLevel];
331 rb->Height = pt->height[att->TextureLevel];
332
333 /* the renderbuffer's surface is inside the texture */
334 strb->surface = pipe->get_tex_surface(pipe, pt,
335 att->CubeMapFace,
336 att->TextureLevel,
337 att->Zoffset);
338 assert(strb->surface);
339
340 init_renderbuffer_bits(strb, pt->format);
341
342 /*
343 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
344 att->Texture, pt, strb->surface, rb->Width, rb->Height);
345 */
346
347 /* Invalidate buffer state so that the pipe's framebuffer state
348 * gets updated.
349 * That's where the new renderbuffer (which we just created) gets
350 * passed to the pipe as a (color/depth) render target.
351 */
352 st_invalidate_state(ctx, _NEW_BUFFERS);
353 }
354
355
356 /**
357 * Called via ctx->Driver.FinishRenderTexture.
358 */
359 static void
360 st_finish_render_texture(GLcontext *ctx,
361 struct gl_renderbuffer_attachment *att)
362 {
363 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
364
365 assert(strb);
366
367 ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE);
368
369 /*
370 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
371 */
372
373 pipe_surface_reference(&strb->surface, NULL);
374
375 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
376
377 /* restore previous framebuffer state */
378 st_invalidate_state(ctx, _NEW_BUFFERS);
379 }
380
381
382
383 void st_init_fbo_functions(struct dd_function_table *functions)
384 {
385 functions->NewFramebuffer = st_new_framebuffer;
386 functions->NewRenderbuffer = st_new_renderbuffer;
387 functions->BindFramebuffer = st_bind_framebuffer;
388 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
389 functions->RenderTexture = st_render_texture;
390 functions->FinishRenderTexture = st_finish_render_texture;
391 /* no longer needed by core Mesa, drivers handle resizes...
392 functions->ResizeBuffers = st_resize_buffers;
393 */
394 }