gallium: added some assertions to st_render_texture() to check surface format
[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 * This is called to allocate the original drawing surface, and
84 * during window resize.
85 */
86 static GLboolean
87 st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
88 GLenum internalFormat,
89 GLuint width, GLuint height)
90 {
91 struct pipe_context *pipe = ctx->st->pipe;
92 struct st_renderbuffer *strb = st_renderbuffer(rb);
93 enum pipe_format pipeFormat;
94 GLbitfield flags = 0x0; /* XXX needed? */
95 int ret;
96
97 if (!strb->surface) {
98 /* first time surface creation */
99 strb->surface = pipe->winsys->surface_alloc(pipe->winsys);
100 assert(strb->surface);
101 assert(strb->surface->refcount);
102 assert(strb->surface->winsys);
103 if (!strb->surface)
104 return GL_FALSE;
105 }
106 else if (strb->surface->buffer) {
107 /* release/discard the old surface buffer */
108 pipe_buffer_reference(pipe->winsys, &strb->surface->buffer, NULL);
109 }
110
111 /* Determine surface format here */
112 if (strb->format != PIPE_FORMAT_NONE) {
113 assert(strb->format != 0);
114 /* we'll hit this for front/back color bufs */
115 pipeFormat = strb->format;
116 }
117 else {
118 pipeFormat = st_choose_renderbuffer_format(pipe, internalFormat);
119 }
120
121 init_renderbuffer_bits(strb, pipeFormat);
122
123 ret = pipe->winsys->surface_alloc_storage(pipe->winsys,
124 strb->surface,
125 width,
126 height,
127 pipeFormat,
128 flags);
129 if (ret || !strb->surface->buffer) {
130 if (pipeFormat == DEFAULT_ACCUM_PIPE_FORMAT) {
131 /* Accum buffer. Try a different surface format. Since accum
132 * buffers are s/w only for now, the surface pixel format doesn't
133 * really matter, only that the buffer is large enough.
134 */
135 int sz, mult;
136 enum pipe_format accum_format;
137
138 /* allocate a buffer of (typically) double height to get 64bpp */
139 accum_format = st_choose_renderbuffer_format(pipe, GL_RGBA);
140 sz = pf_get_size(accum_format);
141 mult = pf_get_size(DEFAULT_ACCUM_PIPE_FORMAT) / sz;
142
143 ret = pipe->winsys->surface_alloc_storage(pipe->winsys,
144 strb->surface,
145 width, height * mult,
146 accum_format, flags);
147 if (ret)
148 return GL_FALSE; /* we've _really_ failed */
149
150 }
151 else {
152 return GL_FALSE; /* out of memory, try s/w buffer? */
153 }
154 }
155
156 ASSERT(strb->surface->buffer);
157 ASSERT(strb->surface->format);
158 ASSERT(strb->surface->cpp);
159 ASSERT(strb->surface->width == width);
160 /*ASSERT(strb->surface->height == height);*/
161 ASSERT(strb->surface->pitch);
162
163 strb->Base.Width = width;
164 strb->Base.Height = height;
165
166 return GL_TRUE;
167 }
168
169
170 /**
171 * gl_renderbuffer::Delete()
172 */
173 static void
174 st_renderbuffer_delete(struct gl_renderbuffer *rb)
175 {
176 struct st_renderbuffer *strb = st_renderbuffer(rb);
177 ASSERT(strb);
178 if (strb->surface) {
179 struct pipe_winsys *ws = strb->surface->winsys;
180 ws->surface_release(ws, &strb->surface);
181 }
182 free(strb);
183 }
184
185
186 /**
187 * gl_renderbuffer::GetPointer()
188 */
189 static void *
190 null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
191 GLint x, GLint y)
192 {
193 /* By returning NULL we force all software rendering to go through
194 * the span routines.
195 */
196 #if 0
197 assert(0); /* Should never get called with softpipe */
198 #endif
199 return NULL;
200 }
201
202
203 /**
204 * Called via ctx->Driver.NewFramebuffer()
205 */
206 static struct gl_framebuffer *
207 st_new_framebuffer(GLcontext *ctx, GLuint name)
208 {
209 /* XXX not sure we need to subclass gl_framebuffer for pipe */
210 return _mesa_new_framebuffer(ctx, name);
211 }
212
213
214 /**
215 * Called via ctx->Driver.NewRenderbuffer()
216 */
217 static struct gl_renderbuffer *
218 st_new_renderbuffer(GLcontext *ctx, GLuint name)
219 {
220 struct st_renderbuffer *strb = CALLOC_STRUCT(st_renderbuffer);
221 if (strb) {
222 _mesa_init_renderbuffer(&strb->Base, name);
223 strb->Base.Delete = st_renderbuffer_delete;
224 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
225 strb->Base.GetPointer = null_get_pointer;
226 strb->format = PIPE_FORMAT_NONE;
227 return &strb->Base;
228 }
229 return NULL;
230 }
231
232
233 /**
234 * Allocate a renderbuffer for a an on-screen window (not a user-created
235 * renderbuffer). The window system code determines the format.
236 */
237 struct gl_renderbuffer *
238 st_new_renderbuffer_fb(enum pipe_format format)
239 {
240 struct st_renderbuffer *strb;
241
242 strb = CALLOC_STRUCT(st_renderbuffer);
243 if (!strb) {
244 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
245 return NULL;
246 }
247
248 _mesa_init_renderbuffer(&strb->Base, 0);
249 strb->Base.ClassID = 0x4242; /* just a unique value */
250 strb->format = format;
251
252 switch (format) {
253 case PIPE_FORMAT_A8R8G8B8_UNORM:
254 case PIPE_FORMAT_B8G8R8A8_UNORM:
255 case PIPE_FORMAT_X8R8G8B8_UNORM:
256 case PIPE_FORMAT_B8G8R8X8_UNORM:
257 case PIPE_FORMAT_A1R5G5B5_UNORM:
258 case PIPE_FORMAT_A4R4G4B4_UNORM:
259 case PIPE_FORMAT_R5G6B5_UNORM:
260 strb->Base.InternalFormat = GL_RGBA;
261 strb->Base._BaseFormat = GL_RGBA;
262 break;
263 case PIPE_FORMAT_Z16_UNORM:
264 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
265 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
266 break;
267 case PIPE_FORMAT_Z32_UNORM:
268 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
269 strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
270 break;
271 case PIPE_FORMAT_S8Z24_UNORM:
272 case PIPE_FORMAT_Z24S8_UNORM:
273 case PIPE_FORMAT_X8Z24_UNORM:
274 case PIPE_FORMAT_Z24X8_UNORM:
275 strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
276 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
277 break;
278 case PIPE_FORMAT_S8_UNORM:
279 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
280 strb->Base._BaseFormat = GL_STENCIL_INDEX;
281 break;
282 case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
283 strb->Base.InternalFormat = GL_RGBA16;
284 strb->Base._BaseFormat = GL_RGBA;
285 break;
286 default:
287 _mesa_problem(NULL,
288 "Unexpected format in st_new_renderbuffer_fb");
289 return NULL;
290 }
291
292 /* st-specific methods */
293 strb->Base.Delete = st_renderbuffer_delete;
294 strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
295 strb->Base.GetPointer = null_get_pointer;
296
297 /* surface is allocated in st_renderbuffer_alloc_storage() */
298 strb->surface = NULL;
299
300 return &strb->Base;
301 }
302
303
304
305
306 /**
307 * Called via ctx->Driver.BindFramebufferEXT().
308 */
309 static void
310 st_bind_framebuffer(GLcontext *ctx, GLenum target,
311 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
312 {
313
314 }
315
316 /**
317 * Called by ctx->Driver.FramebufferRenderbuffer
318 */
319 static void
320 st_framebuffer_renderbuffer(GLcontext *ctx,
321 struct gl_framebuffer *fb,
322 GLenum attachment,
323 struct gl_renderbuffer *rb)
324 {
325 /* XXX no need for derivation? */
326 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
327 }
328
329
330 /**
331 * Called by ctx->Driver.RenderTexture
332 */
333 static void
334 st_render_texture(GLcontext *ctx,
335 struct gl_framebuffer *fb,
336 struct gl_renderbuffer_attachment *att)
337 {
338 struct st_context *st = ctx->st;
339 struct st_renderbuffer *strb;
340 struct gl_renderbuffer *rb;
341 struct pipe_context *pipe = st->pipe;
342 struct pipe_screen *screen = pipe->screen;
343 struct pipe_texture *pt;
344
345 assert(!att->Renderbuffer);
346
347 /* create new renderbuffer which wraps the texture image */
348 rb = st_new_renderbuffer(ctx, 0);
349 if (!rb) {
350 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
351 return;
352 }
353
354 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
355 assert(rb->RefCount == 1);
356 rb->AllocStorage = NULL; /* should not get called */
357 strb = st_renderbuffer(rb);
358
359 /* get the texture for the texture object */
360 pt = st_get_texobj_texture(att->Texture);
361 assert(pt);
362 assert(pt->width[att->TextureLevel]);
363
364 rb->Width = pt->width[att->TextureLevel];
365 rb->Height = pt->height[att->TextureLevel];
366
367 /* the renderbuffer's surface is inside the texture */
368 strb->surface = screen->get_tex_surface(screen, pt,
369 att->CubeMapFace,
370 att->TextureLevel,
371 att->Zoffset);
372 assert(strb->surface);
373 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_TEXTURE));
374 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_SURFACE));
375
376 init_renderbuffer_bits(strb, pt->format);
377
378 /*
379 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
380 att->Texture, pt, strb->surface, rb->Width, rb->Height);
381 */
382
383 /* Invalidate buffer state so that the pipe's framebuffer state
384 * gets updated.
385 * That's where the new renderbuffer (which we just created) gets
386 * passed to the pipe as a (color/depth) render target.
387 */
388 st_invalidate_state(ctx, _NEW_BUFFERS);
389 }
390
391
392 /**
393 * Called via ctx->Driver.FinishRenderTexture.
394 */
395 static void
396 st_finish_render_texture(GLcontext *ctx,
397 struct gl_renderbuffer_attachment *att)
398 {
399 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
400
401 assert(strb);
402
403 ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
404
405 ctx->st->pipe->texture_update(ctx->st->pipe,
406 st_get_texobj_texture(att->Texture),
407 att->CubeMapFace, 1 << att->TextureLevel);
408
409 /*
410 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
411 */
412
413 pipe_surface_reference(&strb->surface, NULL);
414
415 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
416
417 /* restore previous framebuffer state */
418 st_invalidate_state(ctx, _NEW_BUFFERS);
419 }
420
421
422
423 void st_init_fbo_functions(struct dd_function_table *functions)
424 {
425 functions->NewFramebuffer = st_new_framebuffer;
426 functions->NewRenderbuffer = st_new_renderbuffer;
427 functions->BindFramebuffer = st_bind_framebuffer;
428 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
429 functions->RenderTexture = st_render_texture;
430 functions->FinishRenderTexture = st_finish_render_texture;
431 /* no longer needed by core Mesa, drivers handle resizes...
432 functions->ResizeBuffers = st_resize_buffers;
433 */
434 }