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/macros.h"
41 #include "main/mfeatures.h"
42 #include "main/renderbuffer.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_screen.h"
47 #include "st_context.h"
48 #include "st_cb_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_format.h"
51 #include "st_texture.h"
52 #include "st_manager.h"
54 #include "util/u_format.h"
55 #include "util/u_inlines.h"
56 #include "util/u_surface.h"
60 * gl_renderbuffer::AllocStorage()
61 * This is called to allocate the original drawing surface, and
62 * during window resize.
65 st_renderbuffer_alloc_storage(struct gl_context
* ctx
,
66 struct gl_renderbuffer
*rb
,
67 GLenum internalFormat
,
68 GLuint width
, GLuint height
)
70 struct st_context
*st
= st_context(ctx
);
71 struct pipe_context
*pipe
= st
->pipe
;
72 struct pipe_screen
*screen
= st
->pipe
->screen
;
73 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
74 enum pipe_format format
;
75 struct pipe_surface surf_tmpl
;
77 if (strb
->format
!= PIPE_FORMAT_NONE
)
78 format
= strb
->format
;
80 format
= st_choose_renderbuffer_format(screen
, internalFormat
,
83 /* init renderbuffer fields */
84 strb
->Base
.Width
= width
;
85 strb
->Base
.Height
= height
;
86 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
87 strb
->Base
._BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
88 strb
->Base
.DataType
= st_format_datatype(format
);
90 strb
->defined
= GL_FALSE
; /* undefined contents now */
97 assert(strb
->format
!= PIPE_FORMAT_NONE
);
99 strb
->stride
= util_format_get_stride(strb
->format
, width
);
100 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
102 strb
->data
= malloc(size
);
104 return strb
->data
!= NULL
;
107 struct pipe_resource
template;
109 /* Free the old surface and texture
111 pipe_surface_reference( &strb
->surface
, NULL
);
112 pipe_resource_reference( &strb
->texture
, NULL
);
113 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
115 /* Setup new texture template.
117 memset(&template, 0, sizeof(template));
118 template.target
= st
->internal_target
;
119 template.format
= format
;
120 template.width0
= width
;
121 template.height0
= height
;
123 template.array_size
= 1;
124 template.last_level
= 0;
125 template.nr_samples
= rb
->NumSamples
;
126 if (util_format_is_depth_or_stencil(format
)) {
127 template.bind
= PIPE_BIND_DEPTH_STENCIL
;
130 template.bind
= (PIPE_BIND_DISPLAY_TARGET
|
131 PIPE_BIND_RENDER_TARGET
);
134 strb
->texture
= screen
->resource_create(screen
, &template);
139 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
140 u_surface_default_template(&surf_tmpl
, strb
->texture
, template.bind
);
141 strb
->surface
= pipe
->create_surface(pipe
,
145 assert(strb
->surface
->texture
);
146 assert(strb
->surface
->format
);
147 assert(strb
->surface
->width
== width
);
148 assert(strb
->surface
->height
== height
);
151 return strb
->surface
!= NULL
;
157 * gl_renderbuffer::Delete()
160 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
162 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
164 pipe_surface_reference(&strb
->surface
, NULL
);
165 pipe_resource_reference(&strb
->texture
, NULL
);
166 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
173 * gl_renderbuffer::GetPointer()
176 null_get_pointer(struct gl_context
* ctx
, struct gl_renderbuffer
*rb
,
179 /* By returning NULL we force all software rendering to go through
183 assert(0); /* Should never get called with softpipe */
190 * Called via ctx->Driver.NewFramebuffer()
192 static struct gl_framebuffer
*
193 st_new_framebuffer(struct gl_context
*ctx
, GLuint name
)
195 /* XXX not sure we need to subclass gl_framebuffer for pipe */
196 return _mesa_new_framebuffer(ctx
, name
);
201 * Called via ctx->Driver.NewRenderbuffer()
203 static struct gl_renderbuffer
*
204 st_new_renderbuffer(struct gl_context
*ctx
, GLuint name
)
206 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
208 _mesa_init_renderbuffer(&strb
->Base
, name
);
209 strb
->Base
.Delete
= st_renderbuffer_delete
;
210 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
211 strb
->Base
.GetPointer
= null_get_pointer
;
212 strb
->format
= PIPE_FORMAT_NONE
;
220 * Allocate a renderbuffer for a an on-screen window (not a user-created
221 * renderbuffer). The window system code determines the format.
223 struct gl_renderbuffer
*
224 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
226 struct st_renderbuffer
*strb
;
228 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
230 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
234 _mesa_init_renderbuffer(&strb
->Base
, 0);
235 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
236 strb
->Base
.NumSamples
= samples
;
237 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
238 strb
->Base
._BaseFormat
= _mesa_get_format_base_format(strb
->Base
.Format
);
239 strb
->Base
.DataType
= st_format_datatype(format
);
240 strb
->format
= format
;
244 case PIPE_FORMAT_R8G8B8A8_UNORM
:
245 case PIPE_FORMAT_B8G8R8A8_UNORM
:
246 case PIPE_FORMAT_A8R8G8B8_UNORM
:
247 case PIPE_FORMAT_R8G8B8X8_UNORM
:
248 case PIPE_FORMAT_B8G8R8X8_UNORM
:
249 case PIPE_FORMAT_X8R8G8B8_UNORM
:
250 case PIPE_FORMAT_B5G5R5A1_UNORM
:
251 case PIPE_FORMAT_B4G4R4A4_UNORM
:
252 case PIPE_FORMAT_B5G6R5_UNORM
:
253 strb
->Base
.InternalFormat
= GL_RGBA
;
255 case PIPE_FORMAT_Z16_UNORM
:
256 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
258 case PIPE_FORMAT_Z32_UNORM
:
259 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
261 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
262 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
263 case PIPE_FORMAT_Z24X8_UNORM
:
264 case PIPE_FORMAT_X8Z24_UNORM
:
265 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
267 case PIPE_FORMAT_S8_USCALED
:
268 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
270 case PIPE_FORMAT_R16G16B16A16_SNORM
:
271 strb
->Base
.InternalFormat
= GL_RGBA16
;
273 case PIPE_FORMAT_R8_UNORM
:
274 strb
->Base
.InternalFormat
= GL_R8
;
276 case PIPE_FORMAT_R8G8_UNORM
:
277 strb
->Base
.InternalFormat
= GL_RG8
;
279 case PIPE_FORMAT_R16_UNORM
:
280 strb
->Base
.InternalFormat
= GL_R16
;
282 case PIPE_FORMAT_R16G16_UNORM
:
283 strb
->Base
.InternalFormat
= GL_RG16
;
287 "Unexpected format in st_new_renderbuffer_fb");
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
;
297 /* surface is allocated in st_renderbuffer_alloc_storage() */
298 strb
->surface
= NULL
;
307 * Called via ctx->Driver.BindFramebufferEXT().
310 st_bind_framebuffer(struct gl_context
*ctx
, GLenum target
,
311 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
317 * Called by ctx->Driver.FramebufferRenderbuffer
320 st_framebuffer_renderbuffer(struct gl_context
*ctx
,
321 struct gl_framebuffer
*fb
,
323 struct gl_renderbuffer
*rb
)
325 /* XXX no need for derivation? */
326 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
331 * Called by ctx->Driver.RenderTexture
334 st_render_texture(struct gl_context
*ctx
,
335 struct gl_framebuffer
*fb
,
336 struct gl_renderbuffer_attachment
*att
)
338 struct st_context
*st
= st_context(ctx
);
339 struct pipe_context
*pipe
= st
->pipe
;
340 struct st_renderbuffer
*strb
;
341 struct gl_renderbuffer
*rb
;
342 struct pipe_resource
*pt
= st_get_texobj_resource(att
->Texture
);
343 struct st_texture_object
*stObj
;
344 const struct gl_texture_image
*texImage
;
345 struct pipe_surface surf_tmpl
;
347 /* When would this fail? Perhaps assert? */
351 /* get pointer to texture image we're rendeing to */
352 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
354 /* create new renderbuffer which wraps the texture image */
355 rb
= st_new_renderbuffer(ctx
, 0);
357 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
361 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
362 assert(rb
->RefCount
== 1);
363 rb
->AllocStorage
= NULL
; /* should not get called */
364 strb
= st_renderbuffer(rb
);
366 assert(strb
->Base
.RefCount
> 0);
368 /* get the texture for the texture object */
369 stObj
= st_texture_object(att
->Texture
);
371 /* point renderbuffer at texobject */
373 strb
->rtt_level
= att
->TextureLevel
;
374 strb
->rtt_face
= att
->CubeMapFace
;
375 strb
->rtt_slice
= att
->Zoffset
;
377 rb
->Width
= texImage
->Width2
;
378 rb
->Height
= texImage
->Height2
;
379 rb
->_BaseFormat
= texImage
->_BaseFormat
;
380 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
382 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
384 pipe_resource_reference( &strb
->texture
, pt
);
386 pipe_surface_reference(&strb
->surface
, NULL
);
388 pipe_sampler_view_reference(&strb
->sampler_view
,
389 st_get_texture_sampler_view(stObj
, pipe
));
391 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
393 /* new surface for rendering into the texture */
394 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
395 surf_tmpl
.format
= ctx
->Color
.sRGBEnabled
? strb
->texture
->format
: util_format_linear(strb
->texture
->format
);
396 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
397 surf_tmpl
.u
.tex
.level
= strb
->rtt_level
;
398 surf_tmpl
.u
.tex
.first_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
399 surf_tmpl
.u
.tex
.last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
400 strb
->surface
= pipe
->create_surface(pipe
,
404 strb
->format
= pt
->format
;
406 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
407 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
410 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
411 att->Texture, pt, strb->surface, rb->Width, rb->Height);
414 /* Invalidate buffer state so that the pipe's framebuffer state
416 * That's where the new renderbuffer (which we just created) gets
417 * passed to the pipe as a (color/depth) render target.
419 st_invalidate_state(ctx
, _NEW_BUFFERS
);
424 * Called via ctx->Driver.FinishRenderTexture.
427 st_finish_render_texture(struct gl_context
*ctx
,
428 struct gl_renderbuffer_attachment
*att
)
430 struct st_context
*st
= st_context(ctx
);
431 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
436 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
441 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
444 /* restore previous framebuffer state */
445 st_invalidate_state(ctx
, _NEW_BUFFERS
);
450 * Validate a renderbuffer attachment for a particular set of bindings.
453 st_validate_attachment(struct gl_context
*ctx
,
454 struct pipe_screen
*screen
,
455 const struct gl_renderbuffer_attachment
*att
,
458 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
459 enum pipe_format format
;
462 /* Only validate texture attachments for now, since
463 * st_renderbuffer_alloc_storage makes sure that
464 * the format is supported.
466 if (att
->Type
!= GL_TEXTURE
)
472 format
= stObj
->pt
->format
;
474 stObj
->base
.Image
[att
->CubeMapFace
][att
->TextureLevel
]->TexFormat
;
476 /* If the encoding is sRGB and sRGB rendering cannot be enabled,
477 * check for linear format support instead.
478 * Later when we create a surface, we change the format to a linear one. */
479 if (!ctx
->Const
.sRGBCapable
&&
480 _mesa_get_format_color_encoding(texFormat
) == GL_SRGB
) {
481 const gl_format linearFormat
= _mesa_get_srgb_format_linear(texFormat
);
482 format
= st_mesa_format_to_pipe_format(linearFormat
);
485 return screen
->is_format_supported(screen
, format
,
487 stObj
->pt
->nr_samples
, bindings
, 0);
492 * Check if two renderbuffer attachments name a combined depth/stencil
496 st_is_depth_stencil_combined(const struct gl_renderbuffer_attachment
*depth
,
497 const struct gl_renderbuffer_attachment
*stencil
)
499 assert(depth
&& stencil
);
501 if (depth
->Type
== stencil
->Type
) {
502 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
503 depth
->Renderbuffer
== stencil
->Renderbuffer
)
506 if (depth
->Type
== GL_TEXTURE
&&
507 depth
->Texture
== stencil
->Texture
)
516 * Check that the framebuffer configuration is valid in terms of what
517 * the driver can support.
519 * For Gallium we only supports combined Z+stencil, not separate buffers.
522 st_validate_framebuffer(struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
524 struct st_context
*st
= st_context(ctx
);
525 struct pipe_screen
*screen
= st
->pipe
->screen
;
526 const struct gl_renderbuffer_attachment
*depth
=
527 &fb
->Attachment
[BUFFER_DEPTH
];
528 const struct gl_renderbuffer_attachment
*stencil
=
529 &fb
->Attachment
[BUFFER_STENCIL
];
532 if (depth
->Type
&& stencil
->Type
&& depth
->Type
!= stencil
->Type
) {
533 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
536 if (depth
->Type
== GL_RENDERBUFFER_EXT
&&
537 stencil
->Type
== GL_RENDERBUFFER_EXT
&&
538 depth
->Renderbuffer
!= stencil
->Renderbuffer
) {
539 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
542 if (depth
->Type
== GL_TEXTURE
&&
543 stencil
->Type
== GL_TEXTURE
&&
544 depth
->Texture
!= stencil
->Texture
) {
545 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
549 if (!st_validate_attachment(ctx
,
552 PIPE_BIND_DEPTH_STENCIL
)) {
553 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
556 if (!st_validate_attachment(ctx
,
559 PIPE_BIND_DEPTH_STENCIL
)) {
560 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
563 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
564 if (!st_validate_attachment(ctx
,
566 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
567 PIPE_BIND_RENDER_TARGET
)) {
568 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
576 * Called via glDrawBuffer.
579 st_DrawBuffers(struct gl_context
*ctx
, GLsizei count
, const GLenum
*buffers
)
581 struct st_context
*st
= st_context(ctx
);
582 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
588 /* add the renderbuffers on demand */
589 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
590 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
591 st_manager_add_color_renderbuffer(st
, fb
, idx
);
597 * Called via glReadBuffer.
600 st_ReadBuffer(struct gl_context
*ctx
, GLenum buffer
)
602 struct st_context
*st
= st_context(ctx
);
603 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
607 /* add the renderbuffer on demand */
608 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
612 void st_init_fbo_functions(struct dd_function_table
*functions
)
614 #if FEATURE_EXT_framebuffer_object
615 functions
->NewFramebuffer
= st_new_framebuffer
;
616 functions
->NewRenderbuffer
= st_new_renderbuffer
;
617 functions
->BindFramebuffer
= st_bind_framebuffer
;
618 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
619 functions
->RenderTexture
= st_render_texture
;
620 functions
->FinishRenderTexture
= st_finish_render_texture
;
621 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
623 /* no longer needed by core Mesa, drivers handle resizes...
624 functions->ResizeBuffers = st_resize_buffers;
627 functions
->DrawBuffers
= st_DrawBuffers
;
628 functions
->ReadBuffer
= st_ReadBuffer
;
632 struct pipe_sampler_view
*
633 st_get_renderbuffer_sampler_view(struct st_renderbuffer
*rb
,
634 struct pipe_context
*pipe
)
636 if (!rb
->sampler_view
) {
637 rb
->sampler_view
= st_create_texture_sampler_view(pipe
, rb
->texture
);
640 return rb
->sampler_view
;