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