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_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"
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
);
82 * gl_renderbuffer::AllocStorage()
83 * This is called to allocate the original drawing surface, and
84 * during window resize.
87 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
88 GLenum internalFormat
,
89 GLuint width
, GLuint height
)
91 struct pipe_context
*pipe
= ctx
->st
->pipe
;
92 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
93 enum pipe_format pipeFormat
;
94 GLbitfield flags
= 0x0; /* XXX needed? */
98 /* first time surface creation */
99 strb
->surface
= pipe
->winsys
->surface_alloc(pipe
->winsys
);
100 assert(strb
->surface
);
101 assert(strb
->surface
->refcount
);
102 assert(strb
->surface
->winsys
);
106 else if (strb
->surface
->buffer
) {
107 /* release/discard the old surface buffer */
108 pipe_reference_buffer(pipe
, &strb
->surface
->buffer
, NULL
);
111 /* Determine surface format here */
112 if (strb
->format
!= PIPE_FORMAT_NONE
) {
113 assert(strb
->format
!= 0);
114 /* we'll hit this for front/back color bufs */
115 pipeFormat
= strb
->format
;
118 pipeFormat
= st_choose_renderbuffer_format(pipe
, internalFormat
);
121 init_renderbuffer_bits(strb
, pipeFormat
);
123 ret
= pipe
->winsys
->surface_alloc_storage(pipe
->winsys
,
129 if (ret
|| !strb
->surface
->buffer
) {
130 if (pipeFormat
== DEFAULT_ACCUM_PIPE_FORMAT
) {
131 /* Accum buffer. Try a different surface format. Since accum
132 * buffers are s/w only for now, the surface pixel format doesn't
133 * really matter, only that the buffer is large enough.
136 enum pipe_format accum_format
;
138 /* allocate a buffer of (typically) double height to get 64bpp */
139 accum_format
= st_choose_renderbuffer_format(pipe
, GL_RGBA
);
140 sz
= pf_get_size(accum_format
);
141 mult
= pf_get_size(DEFAULT_ACCUM_PIPE_FORMAT
) / sz
;
143 ret
= pipe
->winsys
->surface_alloc_storage(pipe
->winsys
,
145 width
, height
* mult
,
146 accum_format
, flags
);
148 return GL_FALSE
; /* we've _really_ failed */
152 return GL_FALSE
; /* out of memory, try s/w buffer? */
156 ASSERT(strb
->surface
->buffer
);
157 ASSERT(strb
->surface
->format
);
158 ASSERT(strb
->surface
->cpp
);
159 ASSERT(strb
->surface
->width
== width
);
160 /*ASSERT(strb->surface->height == height);*/
161 ASSERT(strb
->surface
->pitch
);
163 strb
->Base
.Width
= width
;
164 strb
->Base
.Height
= height
;
171 * gl_renderbuffer::Delete()
174 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
176 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
179 struct pipe_winsys
*ws
= strb
->surface
->winsys
;
180 ws
->surface_release(ws
, &strb
->surface
);
187 * gl_renderbuffer::GetPointer()
190 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
193 /* By returning NULL we force all software rendering to go through
197 assert(0); /* Should never get called with softpipe */
204 * Called via ctx->Driver.NewFramebuffer()
206 static struct gl_framebuffer
*
207 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
209 /* XXX not sure we need to subclass gl_framebuffer for pipe */
210 return _mesa_new_framebuffer(ctx
, name
);
215 * Called via ctx->Driver.NewRenderbuffer()
217 static struct gl_renderbuffer
*
218 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
220 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
222 _mesa_init_renderbuffer(&strb
->Base
, name
);
223 strb
->Base
.Delete
= st_renderbuffer_delete
;
224 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
225 strb
->Base
.GetPointer
= null_get_pointer
;
226 strb
->format
= PIPE_FORMAT_NONE
;
234 * Allocate a renderbuffer for a an on-screen window (not a user-created
235 * renderbuffer). The window system code determines the format.
237 struct gl_renderbuffer
*
238 st_new_renderbuffer_fb(enum pipe_format format
)
240 struct st_renderbuffer
*strb
;
242 strb
= CALLOC_STRUCT(st_renderbuffer
);
244 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
248 _mesa_init_renderbuffer(&strb
->Base
, 0);
249 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
250 strb
->format
= format
;
253 case PIPE_FORMAT_A8R8G8B8_UNORM
:
254 case PIPE_FORMAT_B8G8R8A8_UNORM
:
255 case PIPE_FORMAT_X8R8G8B8_UNORM
:
256 case PIPE_FORMAT_B8G8R8X8_UNORM
:
257 case PIPE_FORMAT_A1R5G5B5_UNORM
:
258 case PIPE_FORMAT_A4R4G4B4_UNORM
:
259 case PIPE_FORMAT_R5G6B5_UNORM
:
260 strb
->Base
.InternalFormat
= GL_RGBA
;
261 strb
->Base
._BaseFormat
= GL_RGBA
;
263 case PIPE_FORMAT_Z16_UNORM
:
264 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
265 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
267 case PIPE_FORMAT_Z32_UNORM
:
268 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
269 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
271 case PIPE_FORMAT_S8Z24_UNORM
:
272 case PIPE_FORMAT_Z24S8_UNORM
:
273 case PIPE_FORMAT_X8Z24_UNORM
:
274 case PIPE_FORMAT_Z24X8_UNORM
:
275 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
276 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
278 case PIPE_FORMAT_S8_UNORM
:
279 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
280 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
282 case DEFAULT_ACCUM_PIPE_FORMAT
: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
283 strb
->Base
.InternalFormat
= GL_RGBA16
;
284 strb
->Base
._BaseFormat
= GL_RGBA
;
288 "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(GLcontext
*ctx
, GLenum target
,
311 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
317 * Called by ctx->Driver.FramebufferRenderbuffer
320 st_framebuffer_renderbuffer(GLcontext
*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(GLcontext
*ctx
,
335 struct gl_framebuffer
*fb
,
336 struct gl_renderbuffer_attachment
*att
)
338 struct st_context
*st
= ctx
->st
;
339 struct st_renderbuffer
*strb
;
340 struct gl_renderbuffer
*rb
;
341 struct pipe_context
*pipe
= st
->pipe
;
342 struct pipe_screen
*screen
= pipe
->screen
;
343 struct pipe_texture
*pt
;
345 assert(!att
->Renderbuffer
);
347 /* create new renderbuffer which wraps the texture image */
348 rb
= st_new_renderbuffer(ctx
, 0);
350 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
354 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
355 assert(rb
->RefCount
== 1);
356 rb
->AllocStorage
= NULL
; /* should not get called */
357 strb
= st_renderbuffer(rb
);
359 /* get the texture for the texture object */
360 pt
= st_get_texobj_texture(att
->Texture
);
362 assert(pt
->width
[att
->TextureLevel
]);
364 rb
->Width
= pt
->width
[att
->TextureLevel
];
365 rb
->Height
= pt
->height
[att
->TextureLevel
];
367 /* the renderbuffer's surface is inside the texture */
368 strb
->surface
= screen
->get_tex_surface(screen
, pt
,
372 assert(strb
->surface
);
373 assert(screen
->is_format_supported(screen
, strb
->surface
->format
, PIPE_TEXTURE
));
374 assert(screen
->is_format_supported(screen
, strb
->surface
->format
, PIPE_SURFACE
));
376 init_renderbuffer_bits(strb
, pt
->format
);
379 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
380 att->Texture, pt, strb->surface, rb->Width, rb->Height);
383 /* Invalidate buffer state so that the pipe's framebuffer state
385 * That's where the new renderbuffer (which we just created) gets
386 * passed to the pipe as a (color/depth) render target.
388 st_invalidate_state(ctx
, _NEW_BUFFERS
);
393 * Called via ctx->Driver.FinishRenderTexture.
396 st_finish_render_texture(GLcontext
*ctx
,
397 struct gl_renderbuffer_attachment
*att
)
399 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
403 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
405 ctx
->st
->pipe
->texture_update(ctx
->st
->pipe
,
406 st_get_texobj_texture(att
->Texture
),
407 att
->CubeMapFace
, 1 << att
->TextureLevel
);
410 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
413 pipe_surface_reference(&strb
->surface
, NULL
);
415 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
417 /* restore previous framebuffer state */
418 st_invalidate_state(ctx
, _NEW_BUFFERS
);
423 void st_init_fbo_functions(struct dd_function_table
*functions
)
425 functions
->NewFramebuffer
= st_new_framebuffer
;
426 functions
->NewRenderbuffer
= st_new_renderbuffer
;
427 functions
->BindFramebuffer
= st_bind_framebuffer
;
428 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
429 functions
->RenderTexture
= st_render_texture
;
430 functions
->FinishRenderTexture
= st_finish_render_texture
;
431 /* no longer needed by core Mesa, drivers handle resizes...
432 functions->ResizeBuffers = st_resize_buffers;