1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Framebuffer/renderbuffer functions.
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"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "pipe/p_inlines.h"
45 #include "pipe/p_screen.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"
56 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
59 init_renderbuffer_bits(struct st_renderbuffer
*strb
,
60 enum pipe_format pipeFormat
)
62 struct pipe_format_info info
;
64 if (!st_get_format_info( pipeFormat
, &info
)) {
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
);
81 * gl_renderbuffer::AllocStorage()
82 * This is called to allocate the original drawing surface, and
83 * during window resize.
86 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
87 GLenum internalFormat
,
88 GLuint width
, GLuint height
)
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
;
95 /* Free the old surface and texture
97 pipe_surface_reference( &strb
->surface
, NULL
);
98 pipe_texture_reference( &strb
->texture
, NULL
);
101 memset(&template, 0, sizeof(template));
103 if (strb
->format
!= PIPE_FORMAT_NONE
) {
104 template.format
= strb
->format
;
107 template.format
= st_choose_renderbuffer_format(pipe
, internalFormat
);
110 strb
->Base
.Width
= width
;
111 strb
->Base
.Height
= height
;
112 init_renderbuffer_bits(strb
, template.format
);
114 template.target
= PIPE_TEXTURE_2D
;
115 template.compressed
= 0;
116 pf_get_block(template.format
, &template.block
);
117 template.width
[0] = width
;
118 template.height
[0] = height
;
119 template.depth
[0] = 1;
120 template.last_level
= 0;
121 template.nr_samples
= rb
->NumSamples
;
123 if (pf_is_depth_stencil(template.format
)) {
124 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
127 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
128 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
132 /* Probably need dedicated flags for surface usage too:
134 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
135 PIPE_BUFFER_USAGE_GPU_WRITE
);
137 PIPE_BUFFER_USAGE_CPU_READ
|
138 PIPE_BUFFER_USAGE_CPU_WRITE
);
141 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
144 /* Special path for accum buffers.
146 * Try a different surface format. Since accum buffers are s/w
147 * only for now, the surface pixel format doesn't really matter,
148 * only that the buffer is large enough.
150 if (!strb
->texture
&& template.format
== DEFAULT_ACCUM_PIPE_FORMAT
)
152 /* Actually, just setting this usage value should be sufficient
153 * to tell the driver to go ahead and allocate the buffer, even
154 * if HW doesn't support the format.
156 template.tex_usage
= 0;
157 surface_usage
= (PIPE_BUFFER_USAGE_CPU_READ
|
158 PIPE_BUFFER_USAGE_CPU_WRITE
);
160 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
168 strb
->surface
= pipe
->screen
->get_tex_surface( pipe
->screen
,
173 assert(strb
->surface
->texture
);
174 assert(strb
->surface
->format
);
175 assert(strb
->surface
->width
== width
);
176 assert(strb
->surface
->height
== height
);
179 return strb
->surface
!= NULL
;
184 * gl_renderbuffer::Delete()
187 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
189 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
191 pipe_surface_reference(&strb
->surface
, NULL
);
192 pipe_texture_reference(&strb
->texture
, NULL
);
198 * gl_renderbuffer::GetPointer()
201 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
204 /* By returning NULL we force all software rendering to go through
208 assert(0); /* Should never get called with softpipe */
215 * Called via ctx->Driver.NewFramebuffer()
217 static struct gl_framebuffer
*
218 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
220 /* XXX not sure we need to subclass gl_framebuffer for pipe */
221 return _mesa_new_framebuffer(ctx
, name
);
226 * Called via ctx->Driver.NewRenderbuffer()
228 static struct gl_renderbuffer
*
229 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
231 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
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
;
245 * Allocate a renderbuffer for a an on-screen window (not a user-created
246 * renderbuffer). The window system code determines the format.
248 struct gl_renderbuffer
*
249 st_new_renderbuffer_fb(enum pipe_format format
, int samples
)
251 struct st_renderbuffer
*strb
;
253 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
255 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
259 _mesa_init_renderbuffer(&strb
->Base
, 0);
260 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
261 strb
->Base
.NumSamples
= samples
;
262 strb
->format
= format
;
265 case PIPE_FORMAT_A8R8G8B8_UNORM
:
266 case PIPE_FORMAT_B8G8R8A8_UNORM
:
267 case PIPE_FORMAT_X8R8G8B8_UNORM
:
268 case PIPE_FORMAT_B8G8R8X8_UNORM
:
269 case PIPE_FORMAT_A1R5G5B5_UNORM
:
270 case PIPE_FORMAT_A4R4G4B4_UNORM
:
271 case PIPE_FORMAT_R5G6B5_UNORM
:
272 strb
->Base
.InternalFormat
= GL_RGBA
;
273 strb
->Base
._BaseFormat
= GL_RGBA
;
275 case PIPE_FORMAT_Z16_UNORM
:
276 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
277 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
279 case PIPE_FORMAT_Z32_UNORM
:
280 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
281 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
283 case PIPE_FORMAT_S8Z24_UNORM
:
284 case PIPE_FORMAT_Z24S8_UNORM
:
285 case PIPE_FORMAT_X8Z24_UNORM
:
286 case PIPE_FORMAT_Z24X8_UNORM
:
287 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
288 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
290 case PIPE_FORMAT_S8_UNORM
:
291 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
292 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
294 case DEFAULT_ACCUM_PIPE_FORMAT
: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
295 strb
->Base
.InternalFormat
= GL_RGBA16
;
296 strb
->Base
._BaseFormat
= GL_RGBA
;
300 "Unexpected format in st_new_renderbuffer_fb");
304 /* st-specific methods */
305 strb
->Base
.Delete
= st_renderbuffer_delete
;
306 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
307 strb
->Base
.GetPointer
= null_get_pointer
;
309 /* surface is allocated in st_renderbuffer_alloc_storage() */
310 strb
->surface
= NULL
;
319 * Called via ctx->Driver.BindFramebufferEXT().
322 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
323 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
329 * Called by ctx->Driver.FramebufferRenderbuffer
332 st_framebuffer_renderbuffer(GLcontext
*ctx
,
333 struct gl_framebuffer
*fb
,
335 struct gl_renderbuffer
*rb
)
337 /* XXX no need for derivation? */
338 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
343 * Called by ctx->Driver.RenderTexture
346 st_render_texture(GLcontext
*ctx
,
347 struct gl_framebuffer
*fb
,
348 struct gl_renderbuffer_attachment
*att
)
350 struct st_renderbuffer
*strb
;
351 struct gl_renderbuffer
*rb
;
352 struct pipe_texture
*pt
= st_get_texobj_texture(att
->Texture
);
353 struct st_texture_object
*stObj
;
354 const struct gl_texture_image
*texImage
=
355 att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
360 /* create new renderbuffer which wraps the texture image */
361 rb
= st_new_renderbuffer(ctx
, 0);
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
367 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
368 assert(rb
->RefCount
== 1);
369 rb
->AllocStorage
= NULL
; /* should not get called */
370 strb
= st_renderbuffer(rb
);
372 /* get the texture for the texture object */
373 stObj
= st_texture_object(att
->Texture
);
375 /* point renderbuffer at texobject */
377 strb
->rtt_level
= att
->TextureLevel
;
378 strb
->rtt_face
= att
->CubeMapFace
;
379 strb
->rtt_slice
= att
->Zoffset
;
381 rb
->Width
= texImage
->Width2
;
382 rb
->Height
= texImage
->Height2
;
383 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
385 /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
387 pipe_texture_reference( &strb
->texture
, pt
);
389 pipe_surface_reference(&strb
->surface
, NULL
);
391 /* the new surface will be created during framebuffer validation */
393 init_renderbuffer_bits(strb
, pt
->format
);
396 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
397 att->Texture, pt, strb->surface, rb->Width, rb->Height);
400 /* Invalidate buffer state so that the pipe's framebuffer state
402 * That's where the new renderbuffer (which we just created) gets
403 * passed to the pipe as a (color/depth) render target.
405 st_invalidate_state(ctx
, _NEW_BUFFERS
);
410 * Called via ctx->Driver.FinishRenderTexture.
413 st_finish_render_texture(GLcontext
*ctx
,
414 struct gl_renderbuffer_attachment
*att
)
416 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
421 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
424 pipe_surface_reference( &strb
->surface
, NULL
);
429 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
432 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
434 /* restore previous framebuffer state */
435 st_invalidate_state(ctx
, _NEW_BUFFERS
);
440 * Check that the framebuffer configuration is valid in terms of what
441 * the driver can support.
443 * For Gallium we only supports combined Z+stencil, not separate buffers.
446 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
448 const struct gl_renderbuffer
*depthRb
=
449 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
450 const struct gl_renderbuffer
*stencilRb
=
451 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
453 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
454 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
459 void st_init_fbo_functions(struct dd_function_table
*functions
)
461 functions
->NewFramebuffer
= st_new_framebuffer
;
462 functions
->NewRenderbuffer
= st_new_renderbuffer
;
463 functions
->BindFramebuffer
= st_bind_framebuffer
;
464 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
465 functions
->RenderTexture
= st_render_texture
;
466 functions
->FinishRenderTexture
= st_finish_render_texture
;
467 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
468 /* no longer needed by core Mesa, drivers handle resizes...
469 functions->ResizeBuffers = st_resize_buffers;