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