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/renderbuffer.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_screen.h"
46 #include "st_context.h"
47 #include "st_cb_fbo.h"
48 #include "st_cb_flush.h"
49 #include "st_format.h"
50 #include "st_texture.h"
51 #include "st_manager.h"
53 #include "util/u_format.h"
54 #include "util/u_inlines.h"
58 * gl_renderbuffer::AllocStorage()
59 * This is called to allocate the original drawing surface, and
60 * during window resize.
63 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
64 GLenum internalFormat
,
65 GLuint width
, GLuint height
)
67 struct st_context
*st
= st_context(ctx
);
68 struct pipe_screen
*screen
= st
->pipe
->screen
;
69 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
70 enum pipe_format format
;
72 if (strb
->format
!= PIPE_FORMAT_NONE
)
73 format
= strb
->format
;
75 format
= st_choose_renderbuffer_format(screen
, internalFormat
);
77 /* init renderbuffer fields */
78 strb
->Base
.Width
= width
;
79 strb
->Base
.Height
= height
;
80 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
81 strb
->Base
.DataType
= st_format_datatype(format
);
83 strb
->defined
= GL_FALSE
; /* undefined contents now */
90 assert(strb
->format
!= PIPE_FORMAT_NONE
);
92 strb
->stride
= util_format_get_stride(strb
->format
, width
);
93 size
= util_format_get_2d_size(strb
->format
, strb
->stride
, height
);
95 strb
->data
= malloc(size
);
97 return strb
->data
!= NULL
;
100 struct pipe_resource
template;
102 /* Free the old surface and texture
104 pipe_surface_reference( &strb
->surface
, NULL
);
105 pipe_resource_reference( &strb
->texture
, NULL
);
106 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
108 /* Setup new texture template.
110 memset(&template, 0, sizeof(template));
111 template.target
= PIPE_TEXTURE_2D
;
112 template.format
= format
;
113 template.width0
= width
;
114 template.height0
= height
;
116 template.last_level
= 0;
117 template.nr_samples
= rb
->NumSamples
;
118 if (util_format_is_depth_or_stencil(format
)) {
119 template.bind
= PIPE_BIND_DEPTH_STENCIL
;
122 template.bind
= (PIPE_BIND_DISPLAY_TARGET
|
123 PIPE_BIND_RENDER_TARGET
);
126 strb
->texture
= screen
->resource_create(screen
, &template);
131 strb
->surface
= screen
->get_tex_surface(screen
,
136 assert(strb
->surface
->texture
);
137 assert(strb
->surface
->format
);
138 assert(strb
->surface
->width
== width
);
139 assert(strb
->surface
->height
== height
);
142 return strb
->surface
!= NULL
;
148 * gl_renderbuffer::Delete()
151 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
153 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
155 pipe_surface_reference(&strb
->surface
, NULL
);
156 pipe_resource_reference(&strb
->texture
, NULL
);
157 pipe_sampler_view_reference(&strb
->sampler_view
, NULL
);
164 * gl_renderbuffer::GetPointer()
167 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
170 /* By returning NULL we force all software rendering to go through
174 assert(0); /* Should never get called with softpipe */
181 * Called via ctx->Driver.NewFramebuffer()
183 static struct gl_framebuffer
*
184 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
186 /* XXX not sure we need to subclass gl_framebuffer for pipe */
187 return _mesa_new_framebuffer(ctx
, name
);
192 * Called via ctx->Driver.NewRenderbuffer()
194 static struct gl_renderbuffer
*
195 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
197 struct st_renderbuffer
*strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
199 _mesa_init_renderbuffer(&strb
->Base
, name
);
200 strb
->Base
.Delete
= st_renderbuffer_delete
;
201 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
202 strb
->Base
.GetPointer
= null_get_pointer
;
203 strb
->format
= PIPE_FORMAT_NONE
;
211 * Allocate a renderbuffer for a an on-screen window (not a user-created
212 * renderbuffer). The window system code determines the format.
214 struct gl_renderbuffer
*
215 st_new_renderbuffer_fb(enum pipe_format format
, int samples
, boolean sw
)
217 struct st_renderbuffer
*strb
;
219 strb
= ST_CALLOC_STRUCT(st_renderbuffer
);
221 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
225 _mesa_init_renderbuffer(&strb
->Base
, 0);
226 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
227 strb
->Base
.NumSamples
= samples
;
228 strb
->Base
.Format
= st_pipe_format_to_mesa_format(format
);
229 strb
->Base
.DataType
= st_format_datatype(format
);
230 strb
->format
= format
;
234 case PIPE_FORMAT_R8G8B8A8_UNORM
:
235 case PIPE_FORMAT_B8G8R8A8_UNORM
:
236 case PIPE_FORMAT_A8R8G8B8_UNORM
:
237 case PIPE_FORMAT_R8G8B8X8_UNORM
:
238 case PIPE_FORMAT_B8G8R8X8_UNORM
:
239 case PIPE_FORMAT_X8R8G8B8_UNORM
:
240 case PIPE_FORMAT_B5G5R5A1_UNORM
:
241 case PIPE_FORMAT_B4G4R4A4_UNORM
:
242 case PIPE_FORMAT_B5G6R5_UNORM
:
243 strb
->Base
.InternalFormat
= GL_RGBA
;
245 case PIPE_FORMAT_Z16_UNORM
:
246 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
248 case PIPE_FORMAT_Z32_UNORM
:
249 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
251 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
252 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
253 case PIPE_FORMAT_Z24X8_UNORM
:
254 case PIPE_FORMAT_X8Z24_UNORM
:
255 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
257 case PIPE_FORMAT_S8_USCALED
:
258 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
260 case PIPE_FORMAT_R16G16B16A16_SNORM
:
261 strb
->Base
.InternalFormat
= GL_RGBA16
;
265 "Unexpected format in st_new_renderbuffer_fb");
270 /* st-specific methods */
271 strb
->Base
.Delete
= st_renderbuffer_delete
;
272 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
273 strb
->Base
.GetPointer
= null_get_pointer
;
275 /* surface is allocated in st_renderbuffer_alloc_storage() */
276 strb
->surface
= NULL
;
285 * Called via ctx->Driver.BindFramebufferEXT().
288 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
289 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
295 * Called by ctx->Driver.FramebufferRenderbuffer
298 st_framebuffer_renderbuffer(GLcontext
*ctx
,
299 struct gl_framebuffer
*fb
,
301 struct gl_renderbuffer
*rb
)
303 /* XXX no need for derivation? */
304 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
309 * Called by ctx->Driver.RenderTexture
312 st_render_texture(GLcontext
*ctx
,
313 struct gl_framebuffer
*fb
,
314 struct gl_renderbuffer_attachment
*att
)
316 struct st_context
*st
= st_context(ctx
);
317 struct pipe_context
*pipe
= st
->pipe
;
318 struct pipe_screen
*screen
= pipe
->screen
;
319 struct st_renderbuffer
*strb
;
320 struct gl_renderbuffer
*rb
;
321 struct pipe_resource
*pt
= st_get_texobj_resource(att
->Texture
);
322 struct st_texture_object
*stObj
;
323 const struct gl_texture_image
*texImage
;
325 /* When would this fail? Perhaps assert? */
329 /* get pointer to texture image we're rendeing to */
330 texImage
= att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
332 /* create new renderbuffer which wraps the texture image */
333 rb
= st_new_renderbuffer(ctx
, 0);
335 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
339 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
340 assert(rb
->RefCount
== 1);
341 rb
->AllocStorage
= NULL
; /* should not get called */
342 strb
= st_renderbuffer(rb
);
344 assert(strb
->Base
.RefCount
> 0);
346 /* get the texture for the texture object */
347 stObj
= st_texture_object(att
->Texture
);
349 /* point renderbuffer at texobject */
351 strb
->rtt_level
= att
->TextureLevel
;
352 strb
->rtt_face
= att
->CubeMapFace
;
353 strb
->rtt_slice
= att
->Zoffset
;
355 rb
->Width
= texImage
->Width2
;
356 rb
->Height
= texImage
->Height2
;
357 rb
->_BaseFormat
= texImage
->_BaseFormat
;
358 /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
360 /*printf("***** pipe texture %d x %d\n", pt->width0, pt->height0);*/
362 pipe_resource_reference( &strb
->texture
, pt
);
364 pipe_surface_reference(&strb
->surface
, NULL
);
366 pipe_sampler_view_reference(&strb
->sampler_view
,
367 st_get_texture_sampler_view(stObj
, pipe
));
369 assert(strb
->rtt_level
<= strb
->texture
->last_level
);
371 /* new surface for rendering into the texture */
372 strb
->surface
= screen
->get_tex_surface(screen
,
377 PIPE_BIND_RENDER_TARGET
);
379 strb
->format
= pt
->format
;
381 strb
->Base
.Format
= st_pipe_format_to_mesa_format(pt
->format
);
382 strb
->Base
.DataType
= st_format_datatype(pt
->format
);
385 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
386 att->Texture, pt, strb->surface, rb->Width, rb->Height);
389 /* Invalidate buffer state so that the pipe's framebuffer state
391 * That's where the new renderbuffer (which we just created) gets
392 * passed to the pipe as a (color/depth) render target.
394 st_invalidate_state(ctx
, _NEW_BUFFERS
);
399 * Called via ctx->Driver.FinishRenderTexture.
402 st_finish_render_texture(GLcontext
*ctx
,
403 struct gl_renderbuffer_attachment
*att
)
405 struct st_context
*st
= st_context(ctx
);
406 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
411 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
416 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
419 /* restore previous framebuffer state */
420 st_invalidate_state(ctx
, _NEW_BUFFERS
);
425 * Validate a renderbuffer attachment for a particular set of bindings.
428 st_validate_attachment(struct pipe_screen
*screen
,
429 const struct gl_renderbuffer_attachment
*att
,
432 const struct st_texture_object
*stObj
= st_texture_object(att
->Texture
);
434 /* Only validate texture attachments for now, since
435 * st_renderbuffer_alloc_storage makes sure that
436 * the format is supported.
438 if (att
->Type
!= GL_TEXTURE
)
444 return screen
->is_format_supported(screen
, stObj
->pt
->format
,
445 PIPE_TEXTURE_2D
, bindings
, 0);
450 * Check that the framebuffer configuration is valid in terms of what
451 * the driver can support.
453 * For Gallium we only supports combined Z+stencil, not separate buffers.
456 st_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
458 struct st_context
*st
= st_context(ctx
);
459 struct pipe_screen
*screen
= st
->pipe
->screen
;
460 const struct gl_renderbuffer
*depthRb
=
461 fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
462 const struct gl_renderbuffer
*stencilRb
=
463 fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
466 if (stencilRb
&& depthRb
&& stencilRb
!= depthRb
) {
467 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
471 if (!st_validate_attachment(screen
,
472 &fb
->Attachment
[BUFFER_DEPTH
],
473 PIPE_BIND_DEPTH_STENCIL
)) {
474 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
477 if (!st_validate_attachment(screen
,
478 &fb
->Attachment
[BUFFER_STENCIL
],
479 PIPE_BIND_DEPTH_STENCIL
)) {
480 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
483 for (i
= 0; i
< ctx
->Const
.MaxColorAttachments
; i
++) {
484 if (!st_validate_attachment(screen
,
485 &fb
->Attachment
[BUFFER_COLOR0
+ i
],
486 PIPE_BIND_RENDER_TARGET
)) {
487 fb
->_Status
= GL_FRAMEBUFFER_UNSUPPORTED_EXT
;
495 * Called via glDrawBuffer.
498 st_DrawBuffers(GLcontext
*ctx
, GLsizei count
, const GLenum
*buffers
)
500 struct st_context
*st
= st_context(ctx
);
501 GLframebuffer
*fb
= ctx
->DrawBuffer
;
507 /* add the renderbuffers on demand */
508 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
509 gl_buffer_index idx
= fb
->_ColorDrawBufferIndexes
[i
];
510 st_manager_add_color_renderbuffer(st
, fb
, idx
);
516 * Called via glReadBuffer.
519 st_ReadBuffer(GLcontext
*ctx
, GLenum buffer
)
521 struct st_context
*st
= st_context(ctx
);
522 GLframebuffer
*fb
= ctx
->ReadBuffer
;
526 /* add the renderbuffer on demand */
527 st_manager_add_color_renderbuffer(st
, fb
, fb
->_ColorReadBufferIndex
);
531 void st_init_fbo_functions(struct dd_function_table
*functions
)
533 functions
->NewFramebuffer
= st_new_framebuffer
;
534 functions
->NewRenderbuffer
= st_new_renderbuffer
;
535 functions
->BindFramebuffer
= st_bind_framebuffer
;
536 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
537 functions
->RenderTexture
= st_render_texture
;
538 functions
->FinishRenderTexture
= st_finish_render_texture
;
539 functions
->ValidateFramebuffer
= st_validate_framebuffer
;
540 /* no longer needed by core Mesa, drivers handle resizes...
541 functions->ResizeBuffers = st_resize_buffers;
544 functions
->DrawBuffers
= st_DrawBuffers
;
545 functions
->ReadBuffer
= st_ReadBuffer
;
548 struct pipe_sampler_view
*
549 st_get_renderbuffer_sampler_view(struct st_renderbuffer
*rb
,
550 struct pipe_context
*pipe
)
552 if (!rb
->sampler_view
) {
553 rb
->sampler_view
= st_create_texture_sampler_view(pipe
, rb
->texture
);
556 return rb
->sampler_view
;