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