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_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"
55 * Compute the renderbuffer's Red/Green/EtcBit fields from the pipe format.
58 init_renderbuffer_bits(struct st_renderbuffer
*strb
,
59 enum pipe_format pipeFormat
)
61 struct pipe_format_info info
;
63 if (!st_get_format_info( pipeFormat
, &info
)) {
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
);
80 * gl_renderbuffer::AllocStorage()
81 * This is called to allocate the original drawing surface, and
82 * during window resize.
85 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
86 GLenum internalFormat
,
87 GLuint width
, GLuint height
)
89 struct pipe_context
*pipe
= ctx
->st
->pipe
;
90 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
91 enum pipe_format format
;
93 if (strb
->format
!= PIPE_FORMAT_NONE
)
94 format
= strb
->format
;
96 format
= st_choose_renderbuffer_format(pipe
, internalFormat
);
98 /* init renderbuffer fields */
99 strb
->Base
.Width
= width
;
100 strb
->Base
.Height
= height
;
101 init_renderbuffer_bits(strb
, format
);
104 struct pipe_format_block block
;
107 _mesa_free(strb
->data
);
109 assert(strb
->format
!= PIPE_FORMAT_NONE
);
110 pf_get_block(strb
->format
, &block
);
112 strb
->stride
= pf_get_stride(&block
, width
);
113 size
= pf_get_2d_size(&block
, strb
->stride
, height
);
115 strb
->data
= _mesa_malloc(size
);
117 return strb
->data
!= NULL
;
120 struct pipe_texture
template;
121 unsigned surface_usage
;
123 /* Free the old surface and texture
125 pipe_surface_reference( &strb
->surface
, NULL
);
126 pipe_texture_reference( &strb
->texture
, NULL
);
128 /* Setup new texture template.
130 memset(&template, 0, sizeof(template));
131 template.target
= PIPE_TEXTURE_2D
;
132 template.format
= format
;
133 pf_get_block(format
, &template.block
);
134 template.width
[0] = width
;
135 template.height
[0] = height
;
136 template.depth
[0] = 1;
137 template.last_level
= 0;
138 template.nr_samples
= rb
->NumSamples
;
139 if (pf_is_depth_stencil(format
)) {
140 template.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
143 template.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
144 PIPE_TEXTURE_USAGE_RENDER_TARGET
);
147 /* Probably need dedicated flags for surface usage too:
149 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
150 PIPE_BUFFER_USAGE_GPU_WRITE
);
152 PIPE_BUFFER_USAGE_CPU_READ
|
153 PIPE_BUFFER_USAGE_CPU_WRITE
);
156 strb
->texture
= pipe
->screen
->texture_create( pipe
->screen
,
162 strb
->surface
= pipe
->screen
->get_tex_surface( pipe
->screen
,
167 assert(strb
->surface
->texture
);
168 assert(strb
->surface
->format
);
169 assert(strb
->surface
->width
== width
);
170 assert(strb
->surface
->height
== height
);
173 return strb
->surface
!= NULL
;
179 * gl_renderbuffer::Delete()
182 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
184 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
186 pipe_surface_reference(&strb
->surface
, NULL
);
187 pipe_texture_reference(&strb
->texture
, NULL
);
188 _mesa_free(strb
->data
);
194 * gl_renderbuffer::GetPointer()
197 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
200 /* By returning NULL we force all software rendering to go through
204 assert(0); /* Should never get called with softpipe */
211 * Called via ctx->Driver.NewFramebuffer()
213 static struct gl_framebuffer
*
214 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
216 /* XXX not sure we need to subclass gl_framebuffer for pipe */
217 return _mesa_new_framebuffer(ctx
, name
);
222 * Called via ctx->Driver.NewRenderbuffer()
224 static struct gl_renderbuffer
*
225 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
227 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
229 _mesa_init_renderbuffer(&strb
->Base
, name
);
230 strb
->Base
.Delete
= st_renderbuffer_delete
;
231 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
232 strb
->Base
.GetPointer
= null_get_pointer
;
233 strb
->format
= PIPE_FORMAT_NONE
;
241 * Allocate a renderbuffer for a an on-screen window (not a user-created
242 * renderbuffer). The window system code determines the format.
244 struct gl_renderbuffer
*
245 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
247 struct st_renderbuffer
*strb
;
249 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
251 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
255 _mesa_init_renderbuffer(&strb
->Base
, 0);
256 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
257 strb
->Base
.NumSamples
= samples
;
258 strb
->format
= format
;
259 init_renderbuffer_bits(strb
, format
);
263 case PIPE_FORMAT_A8R8G8B8_UNORM
:
264 case PIPE_FORMAT_B8G8R8A8_UNORM
:
265 case PIPE_FORMAT_X8R8G8B8_UNORM
:
266 case PIPE_FORMAT_B8G8R8X8_UNORM
:
267 case PIPE_FORMAT_A1R5G5B5_UNORM
:
268 case PIPE_FORMAT_A4R4G4B4_UNORM
:
269 case PIPE_FORMAT_R5G6B5_UNORM
:
270 strb
->Base
.InternalFormat
= GL_RGBA
;
271 strb
->Base
._BaseFormat
= GL_RGBA
;
273 case PIPE_FORMAT_Z16_UNORM
:
274 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
275 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
277 case PIPE_FORMAT_Z32_UNORM
:
278 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
279 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
281 case PIPE_FORMAT_S8Z24_UNORM
:
282 case PIPE_FORMAT_Z24S8_UNORM
:
283 case PIPE_FORMAT_X8Z24_UNORM
:
284 case PIPE_FORMAT_Z24X8_UNORM
:
285 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
286 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
288 case PIPE_FORMAT_S8_UNORM
:
289 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
290 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
292 case PIPE_FORMAT_R16G16B16A16_SNORM
:
293 strb
->Base
.InternalFormat
= GL_RGBA16
;
294 strb
->Base
._BaseFormat
= GL_RGBA
;
298 "Unexpected format in st_new_renderbuffer_fb");
302 /* st-specific methods */
303 strb
->Base
.Delete
= st_renderbuffer_delete
;
304 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
305 strb
->Base
.GetPointer
= null_get_pointer
;
307 /* surface is allocated in st_renderbuffer_alloc_storage() */
308 strb
->surface
= NULL
;
317 * Called via ctx->Driver.BindFramebufferEXT().
320 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
321 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
327 * Called by ctx->Driver.FramebufferRenderbuffer
330 st_framebuffer_renderbuffer(GLcontext
*ctx
,
331 struct gl_framebuffer
*fb
,
333 struct gl_renderbuffer
*rb
)
335 /* XXX no need for derivation? */
336 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
341 * Called by ctx->Driver.RenderTexture
344 st_render_texture(GLcontext
*ctx
,
345 struct gl_framebuffer
*fb
,
346 struct gl_renderbuffer_attachment
*att
)
348 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
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
];
359 /* create new renderbuffer which wraps the texture image */
360 rb
= st_new_renderbuffer(ctx
, 0);
362 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
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
);
371 assert(strb
->Base
.RefCount
> 0);
373 /* get the texture for the texture object */
374 stObj
= st_texture_object(att
->Texture
);
376 /* point renderbuffer at texobject */
378 strb
->rtt_level
= att
->TextureLevel
;
379 strb
->rtt_face
= att
->CubeMapFace
;
380 strb
->rtt_slice
= att
->Zoffset
;
382 rb
->Width
= texImage
->Width2
;
383 rb
->Height
= texImage
->Height2
;
384 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
386 /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
388 pipe_texture_reference( &strb
->texture
, pt
);
390 pipe_surface_reference(&strb
->surface
, NULL
);
392 /* new surface for rendering into the texture */
393 strb
->surface
= screen
->get_tex_surface(screen
,
398 PIPE_BUFFER_USAGE_GPU_READ
|
399 PIPE_BUFFER_USAGE_GPU_WRITE
);
401 init_renderbuffer_bits(strb
, pt
->format
);
404 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
405 att->Texture, pt, strb->surface, rb->Width, rb->Height);
408 /* Invalidate buffer state so that the pipe's framebuffer state
410 * That's where the new renderbuffer (which we just created) gets
411 * passed to the pipe as a (color/depth) render target.
413 st_invalidate_state(ctx
, _NEW_BUFFERS
);
418 * Called via ctx->Driver.FinishRenderTexture.
421 st_finish_render_texture(GLcontext
*ctx
,
422 struct gl_renderbuffer_attachment
*att
)
424 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
429 st_flush( ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
432 pipe_surface_reference( &strb
->surface
, NULL
);
437 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
440 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
442 /* restore previous framebuffer state */
443 st_invalidate_state(ctx
, _NEW_BUFFERS
);
448 * Check that the framebuffer configuration is valid in terms of what
449 * the driver can support.
451 * For Gallium we only supports combined Z+stencil, not separate buffers.
454 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
456 const struct gl_renderbuffer
*depthRb
=
457 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
458 const struct gl_renderbuffer
*stencilRb
=
459 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
461 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
462 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
467 void st_init_fbo_functions(struct dd_function_table
*functions
)
469 functions
->NewFramebuffer
= st_new_framebuffer
;
470 functions
->NewRenderbuffer
= st_new_renderbuffer
;
471 functions
->BindFramebuffer
= st_bind_framebuffer
;
472 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
473 functions
->RenderTexture
= st_render_texture
;
474 functions
->FinishRenderTexture
= st_finish_render_texture
;
475 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
476 /* no longer needed by core Mesa, drivers handle resizes...
477 functions->ResizeBuffers = st_resize_buffers;