st_cb_fbo.c revision a73ae3d5eb8419feab5aea26573aa41b72f941eb
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 */ 58static int 59init_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 */ 86static GLboolean 87st_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 struct pipe_texture template, *texture; 94 unsigned surface_usage; 95 96 /* Free the old surface (and texture if we hold the last 97 * reference): 98 */ 99 pipe_surface_reference( &strb->surface, NULL ); 100 101 memset(&template, 0, sizeof(template)); 102 103 if (strb->format != PIPE_FORMAT_NONE) { 104 template.format = strb->format; 105 } 106 else { 107 template.format = st_choose_renderbuffer_format(pipe, internalFormat); 108 } 109 110 strb->Base.Width = width; 111 strb->Base.Height = height; 112 init_renderbuffer_bits(strb, template.format); 113 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 template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET | 121 PIPE_TEXTURE_USAGE_RENDER_TARGET); 122 123 /* Probably need dedicated flags for surface usage too: 124 */ 125 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ | 126 PIPE_BUFFER_USAGE_GPU_WRITE | 127 PIPE_BUFFER_USAGE_CPU_READ | 128 PIPE_BUFFER_USAGE_CPU_WRITE); 129 130 texture = pipe->screen->texture_create( pipe->screen, 131 &template ); 132 133 /* Special path for accum buffers. 134 * 135 * Try a different surface format. Since accum buffers are s/w 136 * only for now, the surface pixel format doesn't really matter, 137 * only that the buffer is large enough. 138 */ 139 if (!texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT) 140 { 141 /* Actually, just setting this usage value should be sufficient 142 * to tell the driver to go ahead and allocate the buffer, even 143 * if HW doesn't support the format. 144 */ 145 template.tex_usage = 0; 146 surface_usage = (PIPE_BUFFER_USAGE_CPU_READ | 147 PIPE_BUFFER_USAGE_CPU_WRITE); 148 149 texture = pipe->screen->texture_create( pipe->screen, 150 &template ); 151 152 } 153 154 if (!texture) 155 return FALSE; 156 157 strb->surface = pipe->screen->get_tex_surface( pipe->screen, 158 texture, 159 0, 0, 0, 160 surface_usage ); 161 162 pipe_texture_reference( &texture, NULL ); 163 164 assert(strb->surface->buffer); 165 assert(strb->surface->format); 166 assert(strb->surface->cpp); 167 assert(strb->surface->width == width); 168 assert(strb->surface->height == height); 169 assert(strb->surface->pitch); 170 171 172 return strb->surface != NULL; 173} 174 175 176/** 177 * gl_renderbuffer::Delete() 178 */ 179static void 180st_renderbuffer_delete(struct gl_renderbuffer *rb) 181{ 182 struct st_renderbuffer *strb = st_renderbuffer(rb); 183 ASSERT(strb); 184 pipe_surface_reference(&strb->surface, NULL); 185 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 = 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) 242{ 243 struct st_renderbuffer *strb; 244 245 strb = 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->format = format; 254 255 switch (format) { 256 case PIPE_FORMAT_A8R8G8B8_UNORM: 257 case PIPE_FORMAT_B8G8R8A8_UNORM: 258 case PIPE_FORMAT_X8R8G8B8_UNORM: 259 case PIPE_FORMAT_B8G8R8X8_UNORM: 260 case PIPE_FORMAT_A1R5G5B5_UNORM: 261 case PIPE_FORMAT_A4R4G4B4_UNORM: 262 case PIPE_FORMAT_R5G6B5_UNORM: 263 strb->Base.InternalFormat = GL_RGBA; 264 strb->Base._BaseFormat = GL_RGBA; 265 break; 266 case PIPE_FORMAT_Z16_UNORM: 267 strb->Base.InternalFormat = GL_DEPTH_COMPONENT16; 268 strb->Base._BaseFormat = GL_DEPTH_COMPONENT; 269 break; 270 case PIPE_FORMAT_Z32_UNORM: 271 strb->Base.InternalFormat = GL_DEPTH_COMPONENT32; 272 strb->Base._BaseFormat = GL_DEPTH_COMPONENT; 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 strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT; 280 break; 281 case PIPE_FORMAT_S8_UNORM: 282 strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT; 283 strb->Base._BaseFormat = GL_STENCIL_INDEX; 284 break; 285 case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/ 286 strb->Base.InternalFormat = GL_RGBA16; 287 strb->Base._BaseFormat = GL_RGBA; 288 break; 289 default: 290 _mesa_problem(NULL, 291 "Unexpected format in st_new_renderbuffer_fb"); 292 return NULL; 293 } 294 295 /* st-specific methods */ 296 strb->Base.Delete = st_renderbuffer_delete; 297 strb->Base.AllocStorage = st_renderbuffer_alloc_storage; 298 strb->Base.GetPointer = null_get_pointer; 299 300 /* surface is allocated in st_renderbuffer_alloc_storage() */ 301 strb->surface = NULL; 302 303 return &strb->Base; 304} 305 306 307 308 309/** 310 * Called via ctx->Driver.BindFramebufferEXT(). 311 */ 312static void 313st_bind_framebuffer(GLcontext *ctx, GLenum target, 314 struct gl_framebuffer *fb, struct gl_framebuffer *fbread) 315{ 316 317} 318 319/** 320 * Called by ctx->Driver.FramebufferRenderbuffer 321 */ 322static void 323st_framebuffer_renderbuffer(GLcontext *ctx, 324 struct gl_framebuffer *fb, 325 GLenum attachment, 326 struct gl_renderbuffer *rb) 327{ 328 /* XXX no need for derivation? */ 329 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb); 330} 331 332 333/** 334 * Called by ctx->Driver.RenderTexture 335 */ 336static void 337st_render_texture(GLcontext *ctx, 338 struct gl_framebuffer *fb, 339 struct gl_renderbuffer_attachment *att) 340{ 341 struct st_context *st = ctx->st; 342 struct st_renderbuffer *strb; 343 struct gl_renderbuffer *rb; 344 struct pipe_context *pipe = st->pipe; 345 struct pipe_screen *screen = pipe->screen; 346 struct pipe_texture *pt; 347 348 assert(!att->Renderbuffer); 349 350 /* create new renderbuffer which wraps the texture image */ 351 rb = st_new_renderbuffer(ctx, 0); 352 if (!rb) { 353 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()"); 354 return; 355 } 356 357 _mesa_reference_renderbuffer(&att->Renderbuffer, rb); 358 assert(rb->RefCount == 1); 359 rb->AllocStorage = NULL; /* should not get called */ 360 strb = st_renderbuffer(rb); 361 362 /* get the texture for the texture object */ 363 pt = st_get_texobj_texture(att->Texture); 364 assert(pt); 365 assert(pt->width[att->TextureLevel]); 366 367 rb->Width = pt->width[att->TextureLevel]; 368 rb->Height = pt->height[att->TextureLevel]; 369 370 /* the renderbuffer's surface is inside the texture */ 371 strb->surface = screen->get_tex_surface(screen, pt, 372 att->CubeMapFace, 373 att->TextureLevel, 374 att->Zoffset, 375 PIPE_BUFFER_USAGE_CPU_READ | 376 PIPE_BUFFER_USAGE_CPU_WRITE | 377 PIPE_BUFFER_USAGE_GPU_READ | 378 PIPE_BUFFER_USAGE_GPU_WRITE); 379 assert(strb->surface); 380 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_TEXTURE)); 381 assert(screen->is_format_supported(screen, strb->surface->format, PIPE_SURFACE)); 382 383 init_renderbuffer_bits(strb, pt->format); 384 385 /* 386 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n", 387 att->Texture, pt, strb->surface, rb->Width, rb->Height); 388 */ 389 390 /* Invalidate buffer state so that the pipe's framebuffer state 391 * gets updated. 392 * That's where the new renderbuffer (which we just created) gets 393 * passed to the pipe as a (color/depth) render target. 394 */ 395 st_invalidate_state(ctx, _NEW_BUFFERS); 396} 397 398 399/** 400 * Called via ctx->Driver.FinishRenderTexture. 401 */ 402static void 403st_finish_render_texture(GLcontext *ctx, 404 struct gl_renderbuffer_attachment *att) 405{ 406 struct pipe_screen *screen = ctx->st->pipe->screen; 407 struct st_renderbuffer *strb = st_renderbuffer(att->Renderbuffer); 408 409 assert(strb); 410 411 ctx->st->pipe->flush(ctx->st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 412 413 screen->tex_surface_release( screen, &strb->surface ); 414 415 /* 416 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface); 417 */ 418 419 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); 420 421 /* restore previous framebuffer state */ 422 st_invalidate_state(ctx, _NEW_BUFFERS); 423} 424 425 426 427void st_init_fbo_functions(struct dd_function_table *functions) 428{ 429 functions->NewFramebuffer = st_new_framebuffer; 430 functions->NewRenderbuffer = st_new_renderbuffer; 431 functions->BindFramebuffer = st_bind_framebuffer; 432 functions->FramebufferRenderbuffer = st_framebuffer_renderbuffer; 433 functions->RenderTexture = st_render_texture; 434 functions->FinishRenderTexture = st_finish_render_texture; 435 /* no longer needed by core Mesa, drivers handle resizes... 436 functions->ResizeBuffers = st_resize_buffers; 437 */ 438} 439