Add surface storage allocation function to winsys interface.
[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 uint type = strb->screenSurface ? PIPE_SCREEN_SURFACE : PIPE_SURFACE;
91 const enum pipe_format pipeFormat
92 = st_choose_pipe_format(pipe, internalFormat, GL_NONE, GL_NONE, type);
93 GLbitfield flags = 0x0; /* XXX needed? */
94
95 if (!strb->surface) {
96 strb->surface = pipe->winsys->surface_alloc(pipe->winsys);
97 assert(strb->surface);
98 if (!strb->surface)
99 return GL_FALSE;
100 }
101
102 /* loop here since mapping is refcounted */
103 while (strb->surface->map)
104 pipe_surface_unmap(strb->surface);
105 if (strb->surface->buffer)
106 pipe->winsys->buffer_reference(pipe->winsys, &strb->surface->buffer,
107 NULL);
108
109 pipe->winsys->surface_alloc_storage(pipe->winsys,
110 strb->surface,
111 width,
112 height,
113 pipeFormat,
114 flags);
115 if (!strb->surface->buffer)
116 return GL_FALSE; /* out of memory, try s/w buffer? */
117
118 ASSERT(strb->surface->buffer);
119 ASSERT(strb->surface->format);
120 ASSERT(strb->surface->cpp);
121 ASSERT(strb->surface->width == width);
122 ASSERT(strb->surface->height == height);
123 ASSERT(strb->surface->pitch);
124
125 strb->Base.Width = width;
126 strb->Base.Height = height;
127
128 return GL_TRUE;
129 }
130
131
132 /**
133 * gl_renderbuffer::Delete()
134 */
135 static void
136 st_renderbuffer_delete(struct gl_renderbuffer *rb)
137 {
138 struct st_renderbuffer *strb = st_renderbuffer(rb);
139 ASSERT(strb);
140 if (strb->surface) {
141 struct pipe_winsys *ws = strb->surface->winsys;
142 ws->surface_release(ws, &strb->surface);
143 }
144 free(strb);
145 }
146
147
148 /**
149 * gl_renderbuffer::GetPointer()
150 */
151 static void *
152 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
153 GLint x, GLint y)
154 {
155 /* By returning NULL we force all software rendering to go through
156 * the span routines.
157 */
158 #if 0
159 assert(0); /* Should never get called with softpipe */
160 #endif
161 return NULL;
162 }
163
164
165 /**
166 * Called via ctx->Driver.NewFramebuffer()
167 */
168 static struct gl_framebuffer *
169 st_new_framebuffer(GLcontext *ctx, GLuint name)
170 {
171 /* XXX not sure we need to subclass gl_framebuffer for pipe */
172 return _mesa_new_framebuffer(ctx, name);
173 }
174
175
176 /**
177 * Called via ctx->Driver.NewRenderbuffer()
178 */
179 static struct gl_renderbuffer *
180 st_new_renderbuffer(GLcontext *ctx, GLuint name)
181 {
182 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
183 if (strb) {
184 _mesa_init_renderbuffer(&strb->Base, name);
185 strb->Base.Delete = st_renderbuffer_delete;
186 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
187 strb->Base.GetPointer = null_get_pointer;
188 return &strb->Base;
189 }
190 return NULL;
191 }
192
193
194 /**
195 * Allocate a renderbuffer for a an on-screen window (not a user-created
196 * renderbuffer). The window system code determines the internal format.
197 * \param screenSurface indicates if the renderbuffer is a front/back color
198 * buffer that'll be displayed/copied to the screen
199 */
200 struct gl_renderbuffer *
201 st_new_renderbuffer_fb(GLenum intFormat, GLboolean screenSurface)
202 {
203 struct st_renderbuffer *strb;
204
205 strb = CALLOC_STRUCT(st_renderbuffer);
206 if (!strb) {
207 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
208 return NULL;
209 }
210
211 _mesa_init_renderbuffer(&strb->Base, 0);
212 strb->Base.ClassID = 0x4242; /* just a unique value */
213 strb->Base.InternalFormat = intFormat;
214 strb->screenSurface = screenSurface;
215
216 switch (intFormat) {
217 case GL_RGB5:
218 case GL_RGBA8:
219 case GL_RGBA16:
220 strb->Base._BaseFormat = GL_RGBA;
221 break;
222 case GL_DEPTH_COMPONENT16:
223 case GL_DEPTH_COMPONENT32:
224 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
225 break;
226 case GL_DEPTH24_STENCIL8_EXT:
227 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
228 break;
229 case GL_STENCIL_INDEX8_EXT:
230 strb->Base._BaseFormat = GL_STENCIL_INDEX;
231 break;
232 default:
233 _mesa_problem(NULL,
234 "Unexpected intFormat in st_new_renderbuffer");
235 return NULL;
236 }
237
238 /* st-specific methods */
239 strb->Base.Delete = st_renderbuffer_delete;
240 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
241 strb->Base.GetPointer = null_get_pointer;
242
243 /* surface is allocated in st_renderbuffer_alloc_storage() */
244 strb->surface = NULL;
245
246 return &strb->Base;
247 }
248
249
250
251 /**
252 * Called via ctx->Driver.BindFramebufferEXT().
253 */
254 static void
255 st_bind_framebuffer(GLcontext *ctx, GLenum target,
256 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
257 {
258
259 }
260
261 /**
262 * Called by ctx->Driver.FramebufferRenderbuffer
263 */
264 static void
265 st_framebuffer_renderbuffer(GLcontext *ctx,
266 struct gl_framebuffer *fb,
267 GLenum attachment,
268 struct gl_renderbuffer *rb)
269 {
270 /* XXX no need for derivation? */
271 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
272 }
273
274
275 /**
276 * Called by ctx->Driver.RenderTexture
277 */
278 static void
279 st_render_texture(GLcontext *ctx,
280 struct gl_framebuffer *fb,
281 struct gl_renderbuffer_attachment *att)
282 {
283 struct st_context *st = ctx->st;
284 struct st_renderbuffer *strb;
285 struct gl_renderbuffer *rb;
286 struct pipe_context *pipe = st->pipe;
287 struct pipe_texture *pt;
288
289 assert(!att->Renderbuffer);
290
291 /* create new renderbuffer which wraps the texture image */
292 rb = st_new_renderbuffer(ctx, 0);
293 if (!rb) {
294 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
295 return;
296 }
297
298 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
299 assert(rb->RefCount == 1);
300 rb->AllocStorage = NULL; /* should not get called */
301 strb = st_renderbuffer(rb);
302
303 /* get the texture for the texture object */
304 pt = st_get_texobj_texture(att->Texture);
305 assert(pt);
306 assert(pt->width[att->TextureLevel]);
307
308 rb->Width = pt->width[att->TextureLevel];
309 rb->Height = pt->height[att->TextureLevel];
310
311 /* the renderbuffer's surface is inside the texture */
312 strb->surface = pipe->get_tex_surface(pipe, pt,
313 att->CubeMapFace,
314 att->TextureLevel,
315 att->Zoffset);
316 assert(strb->surface);
317
318 init_renderbuffer_bits(strb, pt->format);
319
320 /*
321 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
322 att->Texture, pt, strb->surface, rb->Width, rb->Height);
323 */
324
325 /* Invalidate buffer state so that the pipe's framebuffer state
326 * gets updated.
327 * That's where the new renderbuffer (which we just created) gets
328 * passed to the pipe as a (color/depth) render target.
329 */
330 st_invalidate_state(ctx, _NEW_BUFFERS);
331 }
332
333
334 /**
335 * Called via ctx->Driver.FinishRenderTexture.
336 */
337 static void
338 st_finish_render_texture(GLcontext *ctx,
339 struct gl_renderbuffer_attachment *att)
340 {
341 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
342
343 assert(strb);
344
345 ctx->st->pipe->flush(ctx->st->pipe, 0x0);
346
347 /*
348 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
349 */
350
351 pipe_surface_reference(&strb->surface, NULL);
352
353 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
354
355 /* restore previous framebuffer state */
356 st_invalidate_state(ctx, _NEW_BUFFERS);
357 }
358
359
360
361 void st_init_fbo_functions(struct dd_function_table *functions)
362 {
363 functions->NewFramebuffer = st_new_framebuffer;
364 functions->NewRenderbuffer = st_new_renderbuffer;
365 functions->BindFramebuffer = st_bind_framebuffer;
366 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
367 functions->RenderTexture = st_render_texture;
368 functions->FinishRenderTexture = st_finish_render_texture;
369 /* no longer needed by core Mesa, drivers handle resizes...
370 functions->ResizeBuffers = st_resize_buffers;
371 */
372 }