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