st_cb_fbo.c revision 45bde75bd67e7e920f0113842d1fa58613ccc3ec
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_screen.h"
45#include "st_context.h"
46#include "st_cb_fbo.h"
47#include "st_cb_texture.h"
48#include "st_format.h"
49#include "st_public.h"
50#include "st_texture.h"
51
52
53
54/**
55 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
56 */
57static int
58init_renderbuffer_bits(struct st_renderbuffer *strb,
59                       enum pipe_format pipeFormat)
60{
61   struct pipe_format_info info;
62
63   if (!st_get_format_info( pipeFormat, &info )) {
64      assert( 0 );
65   }
66
67   strb->Base._ActualFormat = info.base_format;
68   strb->Base.RedBits = info.red_bits;
69   strb->Base.GreenBits = info.green_bits;
70   strb->Base.BlueBits = info.blue_bits;
71   strb->Base.AlphaBits = info.alpha_bits;
72   strb->Base.DepthBits = info.depth_bits;
73   strb->Base.StencilBits = info.stencil_bits;
74   strb->Base.DataType = st_format_datatype(pipeFormat);
75
76   return info.size;
77}
78
79/**
80 * gl_renderbuffer::AllocStorage()
81 * This is called to allocate the original drawing surface, and
82 * during window resize.
83 */
84static GLboolean
85st_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   struct pipe_texture template;
92   unsigned surface_usage;
93
94   /* Free the old surface and texture
95    */
96   pipe_surface_reference( &strb->surface, NULL );
97   pipe_texture_reference( &strb->texture, NULL );
98
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   pf_get_block(template.format, &template.block);
116   template.width[0] = width;
117   template.height[0] = height;
118   template.depth[0] = 1;
119   template.last_level = 0;
120   template.nr_samples = rb->NumSamples;
121
122   if (pf_is_depth_stencil(template.format)) {
123      template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
124   }
125   else {
126      template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
127                            PIPE_TEXTURE_USAGE_RENDER_TARGET);
128   }
129
130
131   /* Probably need dedicated flags for surface usage too:
132    */
133   surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
134                    PIPE_BUFFER_USAGE_GPU_WRITE);
135#if 0
136                    PIPE_BUFFER_USAGE_CPU_READ |
137                    PIPE_BUFFER_USAGE_CPU_WRITE);
138#endif
139
140   strb->texture = pipe->screen->texture_create( pipe->screen,
141                                                 &template );
142
143   /* Special path for accum buffers.
144    *
145    * Try a different surface format.  Since accum buffers are s/w
146    * only for now, the surface pixel format doesn't really matter,
147    * only that the buffer is large enough.
148    */
149   if (!strb->texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT)
150   {
151      /* Actually, just setting this usage value should be sufficient
152       * to tell the driver to go ahead and allocate the buffer, even
153       * if HW doesn't support the format.
154       */
155      template.tex_usage = 0;
156      surface_usage = (PIPE_BUFFER_USAGE_CPU_READ |
157                       PIPE_BUFFER_USAGE_CPU_WRITE);
158
159      strb->texture = pipe->screen->texture_create( pipe->screen,
160                                                    &template );
161
162   }
163
164   if (!strb->texture)
165      return FALSE;
166
167   strb->surface = pipe->screen->get_tex_surface( pipe->screen,
168                                                  strb->texture,
169                                                  0, 0, 0,
170                                                  surface_usage );
171
172   assert(strb->surface->texture);
173   assert(strb->surface->format);
174   assert(strb->surface->width == width);
175   assert(strb->surface->height == height);
176
177
178   return strb->surface != NULL;
179}
180
181
182/**
183 * gl_renderbuffer::Delete()
184 */
185static void
186st_renderbuffer_delete(struct gl_renderbuffer *rb)
187{
188   struct st_renderbuffer *strb = st_renderbuffer(rb);
189   ASSERT(strb);
190   pipe_surface_reference(&strb->surface, NULL);
191   pipe_texture_reference(&strb->texture, NULL);
192   _mesa_free(strb);
193}
194
195
196/**
197 * gl_renderbuffer::GetPointer()
198 */
199static void *
200null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
201                 GLint x, GLint y)
202{
203   /* By returning NULL we force all software rendering to go through
204    * the span routines.
205    */
206#if 0
207   assert(0);  /* Should never get called with softpipe */
208#endif
209   return NULL;
210}
211
212
213/**
214 * Called via ctx->Driver.NewFramebuffer()
215 */
216static struct gl_framebuffer *
217st_new_framebuffer(GLcontext *ctx, GLuint name)
218{
219   /* XXX not sure we need to subclass gl_framebuffer for pipe */
220   return _mesa_new_framebuffer(ctx, name);
221}
222
223
224/**
225 * Called via ctx->Driver.NewRenderbuffer()
226 */
227static struct gl_renderbuffer *
228st_new_renderbuffer(GLcontext *ctx, GLuint name)
229{
230   struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
231   if (strb) {
232      _mesa_init_renderbuffer(&strb->Base, name);
233      strb->Base.Delete = st_renderbuffer_delete;
234      strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
235      strb->Base.GetPointer = null_get_pointer;
236      strb->format = PIPE_FORMAT_NONE;
237      return &strb->Base;
238   }
239   return NULL;
240}
241
242
243/**
244 * Allocate a renderbuffer for a an on-screen window (not a user-created
245 * renderbuffer).  The window system code determines the format.
246 */
247struct gl_renderbuffer *
248st_new_renderbuffer_fb(enum pipe_format format, int samples)
249{
250   struct st_renderbuffer *strb;
251
252   strb = ST_CALLOC_STRUCT(st_renderbuffer);
253   if (!strb) {
254      _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
255      return NULL;
256   }
257
258   _mesa_init_renderbuffer(&strb->Base, 0);
259   strb->Base.ClassID = 0x4242; /* just a unique value */
260   strb->Base.NumSamples = samples;
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 */
320static void
321st_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 */
330static void
331st_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 */
344static void
345st_render_texture(GLcontext *ctx,
346                  struct gl_framebuffer *fb,
347                  struct gl_renderbuffer_attachment *att)
348{
349   struct st_renderbuffer *strb;
350   struct gl_renderbuffer *rb;
351   struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
352   struct st_texture_object *stObj;
353   const struct gl_texture_image *texImage =
354      att->Texture->Image[att->CubeMapFace][att->TextureLevel];
355
356   if (!pt)
357      return;
358
359   /* create new renderbuffer which wraps the texture image */
360   rb = st_new_renderbuffer(ctx, 0);
361   if (!rb) {
362      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
363      return;
364   }
365
366   _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
367   assert(rb->RefCount == 1);
368   rb->AllocStorage = NULL; /* should not get called */
369   strb = st_renderbuffer(rb);
370
371   /* get the texture for the texture object */
372   stObj = st_texture_object(att->Texture);
373
374   /* point renderbuffer at texobject */
375   strb->rtt = stObj;
376   strb->rtt_level = att->TextureLevel;
377   strb->rtt_face = att->CubeMapFace;
378   strb->rtt_slice = att->Zoffset;
379
380   rb->Width = texImage->Width2;
381   rb->Height = texImage->Height2;
382   /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
383
384   /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
385
386   pipe_texture_reference( &strb->texture, pt );
387
388   pipe_surface_reference(&strb->surface, NULL);
389
390   /* the new surface will be created during framebuffer validation */
391
392   init_renderbuffer_bits(strb, pt->format);
393
394   /*
395   printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p  %d x %d\n",
396          att->Texture, pt, strb->surface, rb->Width, rb->Height);
397   */
398
399   /* Invalidate buffer state so that the pipe's framebuffer state
400    * gets updated.
401    * That's where the new renderbuffer (which we just created) gets
402    * passed to the pipe as a (color/depth) render target.
403    */
404   st_invalidate_state(ctx, _NEW_BUFFERS);
405}
406
407
408/**
409 * Called via ctx->Driver.FinishRenderTexture.
410 */
411static void
412st_finish_render_texture(GLcontext *ctx,
413                         struct gl_renderbuffer_attachment *att)
414{
415   struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
416
417   if (!strb)
418      return;
419
420   st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
421
422   if (strb->surface)
423      pipe_surface_reference( &strb->surface, NULL );
424
425   strb->rtt = NULL;
426
427   /*
428   printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
429   */
430
431   _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
432
433   /* restore previous framebuffer state */
434   st_invalidate_state(ctx, _NEW_BUFFERS);
435}
436
437
438/**
439 * Check that the framebuffer configuration is valid in terms of what
440 * the driver can support.
441 *
442 * For Gallium we only supports combined Z+stencil, not separate buffers.
443 */
444static void
445st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
446{
447   const struct gl_renderbuffer *depthRb =
448      fb->Attachment[BUFFER_DEPTH].Renderbuffer;
449   const struct gl_renderbuffer *stencilRb =
450      fb->Attachment[BUFFER_STENCIL].Renderbuffer;
451
452   if (stencilRb && depthRb && stencilRb != depthRb) {
453      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
454   }
455}
456
457
458void st_init_fbo_functions(struct dd_function_table *functions)
459{
460   functions->NewFramebuffer = st_new_framebuffer;
461   functions->NewRenderbuffer = st_new_renderbuffer;
462   functions->BindFramebuffer = st_bind_framebuffer;
463   functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
464   functions->RenderTexture = st_render_texture;
465   functions->FinishRenderTexture = st_finish_render_texture;
466   functions->ValidateFramebuffer = st_validate_framebuffer;
467   /* no longer needed by core Mesa, drivers handle resizes...
468   functions->ResizeBuffers = st_resize_buffers;
469   */
470}
471