st_cb_fbo.c revision 45e76d2665b38ba3787548310efc59e969124c01
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.Format = info.mesa_format;
68   strb->Base.DataType = st_format_datatype(pipeFormat);
69
70   return info.size;
71}
72
73/**
74 * gl_renderbuffer::AllocStorage()
75 * This is called to allocate the original drawing surface, and
76 * during window resize.
77 */
78static GLboolean
79st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
80                              GLenum internalFormat,
81                              GLuint width, GLuint height)
82{
83   struct pipe_context *pipe = ctx->st->pipe;
84   struct st_renderbuffer *strb = st_renderbuffer(rb);
85   enum pipe_format format;
86
87   if (strb->format != PIPE_FORMAT_NONE)
88      format = strb->format;
89   else
90      format = st_choose_renderbuffer_format(pipe, internalFormat);
91
92   /* init renderbuffer fields */
93   strb->Base.Width  = width;
94   strb->Base.Height = height;
95   init_renderbuffer_bits(strb, format);
96
97   strb->defined = GL_FALSE;  /* undefined contents now */
98
99   if(strb->software) {
100      struct pipe_format_block block;
101      size_t size;
102
103      _mesa_free(strb->data);
104
105      assert(strb->format != PIPE_FORMAT_NONE);
106      pf_get_block(strb->format, &block);
107
108      strb->stride = pf_get_stride(&block, width);
109      size = pf_get_2d_size(&block, strb->stride, height);
110
111      strb->data = _mesa_malloc(size);
112
113      return strb->data != NULL;
114   }
115   else {
116      struct pipe_texture template;
117      unsigned surface_usage;
118
119      /* Free the old surface and texture
120       */
121      pipe_surface_reference( &strb->surface, NULL );
122      pipe_texture_reference( &strb->texture, NULL );
123
124      /* Setup new texture template.
125       */
126      memset(&template, 0, sizeof(template));
127      template.target = PIPE_TEXTURE_2D;
128      template.format = format;
129      pf_get_block(format, &template.block);
130      template.width[0] = width;
131      template.height[0] = height;
132      template.depth[0] = 1;
133      template.last_level = 0;
134      template.nr_samples = rb->NumSamples;
135      if (pf_is_depth_stencil(format)) {
136         template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
137      }
138      else {
139         template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
140                               PIPE_TEXTURE_USAGE_RENDER_TARGET);
141      }
142
143      /* Probably need dedicated flags for surface usage too:
144       */
145      surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
146                       PIPE_BUFFER_USAGE_GPU_WRITE);
147#if 0
148                       PIPE_BUFFER_USAGE_CPU_READ |
149                       PIPE_BUFFER_USAGE_CPU_WRITE);
150#endif
151
152      strb->texture = pipe->screen->texture_create( pipe->screen,
153                                                    &template );
154
155      if (!strb->texture)
156         return FALSE;
157
158      strb->surface = pipe->screen->get_tex_surface( pipe->screen,
159                                                     strb->texture,
160                                                     0, 0, 0,
161                                                     surface_usage );
162
163      assert(strb->surface->texture);
164      assert(strb->surface->format);
165      assert(strb->surface->width == width);
166      assert(strb->surface->height == height);
167
168
169      return strb->surface != NULL;
170   }
171}
172
173
174/**
175 * gl_renderbuffer::Delete()
176 */
177static void
178st_renderbuffer_delete(struct gl_renderbuffer *rb)
179{
180   struct st_renderbuffer *strb = st_renderbuffer(rb);
181   ASSERT(strb);
182   pipe_surface_reference(&strb->surface, NULL);
183   pipe_texture_reference(&strb->texture, NULL);
184   _mesa_free(strb->data);
185   _mesa_free(strb);
186}
187
188
189/**
190 * gl_renderbuffer::GetPointer()
191 */
192static void *
193null_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
194                 GLint x, GLint y)
195{
196   /* By returning NULL we force all software rendering to go through
197    * the span routines.
198    */
199#if 0
200   assert(0);  /* Should never get called with softpipe */
201#endif
202   return NULL;
203}
204
205
206/**
207 * Called via ctx->Driver.NewFramebuffer()
208 */
209static struct gl_framebuffer *
210st_new_framebuffer(GLcontext *ctx, GLuint name)
211{
212   /* XXX not sure we need to subclass gl_framebuffer for pipe */
213   return _mesa_new_framebuffer(ctx, name);
214}
215
216
217/**
218 * Called via ctx->Driver.NewRenderbuffer()
219 */
220static struct gl_renderbuffer *
221st_new_renderbuffer(GLcontext *ctx, GLuint name)
222{
223   struct st_renderbuffer *strb = ST_CALLOC_STRUCT(st_renderbuffer);
224   if (strb) {
225      _mesa_init_renderbuffer(&strb->Base, name);
226      strb->Base.Delete = st_renderbuffer_delete;
227      strb->Base.AllocStorage = st_renderbuffer_alloc_storage;
228      strb->Base.GetPointer = null_get_pointer;
229      strb->format = PIPE_FORMAT_NONE;
230      return &strb->Base;
231   }
232   return NULL;
233}
234
235
236/**
237 * Allocate a renderbuffer for a an on-screen window (not a user-created
238 * renderbuffer).  The window system code determines the format.
239 */
240struct gl_renderbuffer *
241st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
242{
243   struct st_renderbuffer *strb;
244
245   strb = ST_CALLOC_STRUCT(st_renderbuffer);
246   if (!strb) {
247      _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
248      return NULL;
249   }
250
251   _mesa_init_renderbuffer(&strb->Base, 0);
252   strb->Base.ClassID = 0x4242; /* just a unique value */
253   strb->Base.NumSamples = samples;
254   strb->format = format;
255   init_renderbuffer_bits(strb, format);
256   strb->software = sw;
257
258   switch (format) {
259   case PIPE_FORMAT_A8R8G8B8_UNORM:
260   case PIPE_FORMAT_B8G8R8A8_UNORM:
261   case PIPE_FORMAT_X8R8G8B8_UNORM:
262   case PIPE_FORMAT_B8G8R8X8_UNORM:
263   case PIPE_FORMAT_A1R5G5B5_UNORM:
264   case PIPE_FORMAT_A4R4G4B4_UNORM:
265   case PIPE_FORMAT_R5G6B5_UNORM:
266      strb->Base.InternalFormat = GL_RGBA;
267      break;
268   case PIPE_FORMAT_Z16_UNORM:
269      strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
270      break;
271   case PIPE_FORMAT_Z32_UNORM:
272      strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
273      break;
274   case PIPE_FORMAT_S8Z24_UNORM:
275   case PIPE_FORMAT_Z24S8_UNORM:
276   case PIPE_FORMAT_X8Z24_UNORM:
277   case PIPE_FORMAT_Z24X8_UNORM:
278      strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
279      break;
280   case PIPE_FORMAT_S8_UNORM:
281      strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
282      break;
283   case PIPE_FORMAT_R16G16B16A16_SNORM:
284      strb->Base.InternalFormat = GL_RGBA16;
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 */
309static void
310st_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 */
319static void
320st_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 */
333static void
334st_render_texture(GLcontext *ctx,
335                  struct gl_framebuffer *fb,
336                  struct gl_renderbuffer_attachment *att)
337{
338   struct pipe_screen *screen = ctx->st->pipe->screen;
339   struct st_renderbuffer *strb;
340   struct gl_renderbuffer *rb;
341   struct pipe_texture *pt = st_get_texobj_texture(att->Texture);
342   struct st_texture_object *stObj;
343   const struct gl_texture_image *texImage =
344      att->Texture->Image[att->CubeMapFace][att->TextureLevel];
345
346   if (!pt)
347      return;
348
349   /* create new renderbuffer which wraps the texture image */
350   rb = st_new_renderbuffer(ctx, 0);
351   if (!rb) {
352      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
353      return;
354   }
355
356   _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
357   assert(rb->RefCount == 1);
358   rb->AllocStorage = NULL; /* should not get called */
359   strb = st_renderbuffer(rb);
360
361   assert(strb->Base.RefCount > 0);
362
363   /* get the texture for the texture object */
364   stObj = st_texture_object(att->Texture);
365
366   /* point renderbuffer at texobject */
367   strb->rtt = stObj;
368   strb->rtt_level = att->TextureLevel;
369   strb->rtt_face = att->CubeMapFace;
370   strb->rtt_slice = att->Zoffset;
371
372   rb->Width = texImage->Width2;
373   rb->Height = texImage->Height2;
374   /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
375
376   /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
377
378   pipe_texture_reference( &strb->texture, pt );
379
380   pipe_surface_reference(&strb->surface, NULL);
381
382   /* new surface for rendering into the texture */
383   strb->surface = screen->get_tex_surface(screen,
384                                           strb->texture,
385                                           strb->rtt_face,
386                                           strb->rtt_level,
387                                           strb->rtt_slice,
388                                           PIPE_BUFFER_USAGE_GPU_READ |
389                                           PIPE_BUFFER_USAGE_GPU_WRITE);
390
391   init_renderbuffer_bits(strb, pt->format);
392
393   /*
394   printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p  %d x %d\n",
395          att->Texture, pt, strb->surface, rb->Width, rb->Height);
396   */
397
398   /* Invalidate buffer state so that the pipe's framebuffer state
399    * gets updated.
400    * That's where the new renderbuffer (which we just created) gets
401    * passed to the pipe as a (color/depth) render target.
402    */
403   st_invalidate_state(ctx, _NEW_BUFFERS);
404}
405
406
407/**
408 * Called via ctx->Driver.FinishRenderTexture.
409 */
410static void
411st_finish_render_texture(GLcontext *ctx,
412                         struct gl_renderbuffer_attachment *att)
413{
414   struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer);
415
416   if (!strb)
417      return;
418
419   st_flush( ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL );
420
421   if (strb->surface)
422      pipe_surface_reference( &strb->surface, NULL );
423
424   strb->rtt = NULL;
425
426   /*
427   printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
428   */
429
430   _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
431
432   /* restore previous framebuffer state */
433   st_invalidate_state(ctx, _NEW_BUFFERS);
434}
435
436
437/**
438 * Validate a renderbuffer attachment for a particular usage.
439 */
440
441static GLboolean
442st_validate_attachment(struct pipe_screen *screen,
443		       const struct gl_renderbuffer_attachment *att,
444		       GLuint usage)
445{
446   const struct st_texture_object *stObj =
447      st_texture_object(att->Texture);
448
449   /**
450    * Only validate texture attachments for now, since
451    * st_renderbuffer_alloc_storage makes sure that
452    * the format is supported.
453    */
454
455   if (att->Type != GL_TEXTURE)
456      return GL_TRUE;
457
458   if (!stObj)
459      return GL_FALSE;
460
461   return screen->is_format_supported(screen, stObj->pt->format,
462				      PIPE_TEXTURE_2D,
463				      usage, 0);
464}
465
466/**
467 * Check that the framebuffer configuration is valid in terms of what
468 * the driver can support.
469 *
470 * For Gallium we only supports combined Z+stencil, not separate buffers.
471 */
472static void
473st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
474{
475   struct pipe_screen *screen = ctx->st->pipe->screen;
476   const struct gl_renderbuffer *depthRb =
477      fb->Attachment[BUFFER_DEPTH].Renderbuffer;
478   const struct gl_renderbuffer *stencilRb =
479      fb->Attachment[BUFFER_STENCIL].Renderbuffer;
480   GLuint i;
481
482   if (stencilRb && depthRb && stencilRb != depthRb) {
483      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
484      return;
485   }
486
487   if (!st_validate_attachment(screen,
488			       &fb->Attachment[BUFFER_DEPTH],
489			       PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
490      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
491      return;
492   }
493   if (!st_validate_attachment(screen,
494			       &fb->Attachment[BUFFER_STENCIL],
495			       PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
496      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
497      return;
498   }
499   for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
500      if (!st_validate_attachment(screen,
501				  &fb->Attachment[BUFFER_COLOR0 + i],
502				  PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
503	 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
504	 return;
505      }
506   }
507}
508
509
510/**
511 * Copy back color buffer to front color buffer.
512 */
513static void
514copy_back_to_front(struct st_context *st,
515                   struct gl_framebuffer *fb,
516                   gl_buffer_index frontIndex,
517                   gl_buffer_index backIndex)
518
519{
520   struct st_framebuffer *stfb = (struct st_framebuffer *) fb;
521   struct pipe_surface *surf_front, *surf_back;
522
523   (void) st_get_framebuffer_surface(stfb, frontIndex, &surf_front);
524   (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
525
526   if (surf_front && surf_back) {
527      st->pipe->surface_copy(st->pipe,
528                             surf_front, 0, 0,  /* dest */
529                             surf_back, 0, 0,   /* src */
530                             fb->Width, fb->Height);
531   }
532}
533
534
535/**
536 * Check if we're drawing into, or read from, a front color buffer.  If the
537 * front buffer is missing, create it now.
538 *
539 * The back color buffer must exist since we'll use its format/samples info
540 * for creating the front buffer.
541 *
542 * \param frontIndex  either BUFFER_FRONT_LEFT or BUFFER_FRONT_RIGHT
543 * \param backIndex  either BUFFER_BACK_LEFT or BUFFER_BACK_RIGHT
544 */
545static void
546check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
547                          gl_buffer_index frontIndex,
548                          gl_buffer_index backIndex)
549{
550   if (fb->Attachment[frontIndex].Renderbuffer == NULL) {
551      GLboolean create = GL_FALSE;
552
553      /* check if drawing to or reading from front buffer */
554      if (fb->_ColorReadBufferIndex == frontIndex) {
555         create = GL_TRUE;
556      }
557      else {
558         GLuint b;
559         for (b = 0; b < fb->_NumColorDrawBuffers; b++) {
560            if (fb->_ColorDrawBufferIndexes[b] == frontIndex) {
561               create = GL_TRUE;
562               break;
563            }
564         }
565      }
566
567      if (create) {
568         struct st_renderbuffer *back;
569         struct gl_renderbuffer *front;
570         enum pipe_format colorFormat;
571         uint samples;
572
573         if (0)
574            _mesa_debug(ctx, "Allocate new front buffer\n");
575
576         /* get back renderbuffer info */
577         back = st_renderbuffer(fb->Attachment[backIndex].Renderbuffer);
578         colorFormat = back->format;
579         samples = back->Base.NumSamples;
580
581         /* create front renderbuffer */
582         front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
583         _mesa_add_renderbuffer(fb, frontIndex, front);
584
585         /* alloc texture/surface for new front buffer */
586         front->AllocStorage(ctx, front, front->InternalFormat,
587                             fb->Width, fb->Height);
588
589         /* initialize the front color buffer contents by copying
590          * the back buffer.
591          */
592         copy_back_to_front(ctx->st, fb, frontIndex, backIndex);
593      }
594   }
595}
596
597
598/**
599 * If front left/right color buffers are missing, create them now.
600 */
601static void
602check_create_front_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
603{
604   /* check if we need to create the front left buffer now */
605   check_create_front_buffer(ctx, fb, BUFFER_FRONT_LEFT, BUFFER_BACK_LEFT);
606
607   if (fb->Visual.stereoMode) {
608      check_create_front_buffer(ctx, fb, BUFFER_FRONT_RIGHT, BUFFER_BACK_RIGHT);
609   }
610
611   st_invalidate_state(ctx, _NEW_BUFFERS);
612}
613
614
615/**
616 * Called via glDrawBuffer.
617 */
618static void
619st_DrawBuffers(GLcontext *ctx, GLsizei count, const GLenum *buffers)
620{
621   (void) count;
622   (void) buffers;
623   check_create_front_buffers(ctx, ctx->DrawBuffer);
624}
625
626
627/**
628 * Called via glReadBuffer.
629 */
630static void
631st_ReadBuffer(GLcontext *ctx, GLenum buffer)
632{
633   (void) buffer;
634   check_create_front_buffers(ctx, ctx->ReadBuffer);
635}
636
637
638void st_init_fbo_functions(struct dd_function_table *functions)
639{
640   functions->NewFramebuffer = st_new_framebuffer;
641   functions->NewRenderbuffer = st_new_renderbuffer;
642   functions->BindFramebuffer = st_bind_framebuffer;
643   functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer;
644   functions->RenderTexture = st_render_texture;
645   functions->FinishRenderTexture = st_finish_render_texture;
646   functions->ValidateFramebuffer = st_validate_framebuffer;
647   /* no longer needed by core Mesa, drivers handle resizes...
648   functions->ResizeBuffers = st_resize_buffers;
649   */
650
651   functions->DrawBuffers = st_DrawBuffers;
652   functions->ReadBuffer = st_ReadBuffer;
653}
654