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