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