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()
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 pipeFormat
;
92 GLbitfield flags
= 0x0; /* XXX needed? */
95 strb
->surface
= pipe
->winsys
->surface_alloc(pipe
->winsys
);
96 assert(strb
->surface
);
97 assert(strb
->surface
->refcount
);
98 assert(strb
->surface
->winsys
);
103 if (strb
->surface
->buffer
)
104 pipe_buffer_reference(pipe
->winsys
, &strb
->surface
->buffer
,
107 /* Determine surface format here */
108 if (strb
->format
!= PIPE_FORMAT_NONE
) {
109 assert(strb
->format
!= 0);
110 /* we'll hit this for front/back color bufs */
111 pipeFormat
= strb
->format
;
114 pipeFormat
= st_choose_renderbuffer_format(pipe
, internalFormat
);
117 init_renderbuffer_bits(strb
, pipeFormat
);
119 pipe
->winsys
->surface_alloc_storage(pipe
->winsys
,
125 if (!strb
->surface
->buffer
)
126 return GL_FALSE
; /* out of memory, try s/w buffer? */
128 ASSERT(strb
->surface
->buffer
);
129 ASSERT(strb
->surface
->format
);
130 ASSERT(strb
->surface
->cpp
);
131 ASSERT(strb
->surface
->width
== width
);
132 ASSERT(strb
->surface
->height
== height
);
133 ASSERT(strb
->surface
->pitch
);
135 strb
->Base
.Width
= width
;
136 strb
->Base
.Height
= height
;
143 * gl_renderbuffer::Delete()
146 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
148 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
151 struct pipe_winsys
*ws
= strb
->surface
->winsys
;
152 ws
->surface_release(ws
, &strb
->surface
);
159 * gl_renderbuffer::GetPointer()
162 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
165 /* By returning NULL we force all software rendering to go through
169 assert(0); /* Should never get called with softpipe */
176 * Called via ctx->Driver.NewFramebuffer()
178 static struct gl_framebuffer
*
179 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
181 /* XXX not sure we need to subclass gl_framebuffer for pipe */
182 return _mesa_new_framebuffer(ctx
, name
);
187 * Called via ctx->Driver.NewRenderbuffer()
189 static struct gl_renderbuffer
*
190 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
192 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
194 _mesa_init_renderbuffer(&strb
->Base
, name
);
195 strb
->Base
.Delete
= st_renderbuffer_delete
;
196 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
197 strb
->Base
.GetPointer
= null_get_pointer
;
198 strb
->format
= PIPE_FORMAT_NONE
;
206 * Allocate a renderbuffer for a an on-screen window (not a user-created
207 * renderbuffer). The window system code determines the format.
209 struct gl_renderbuffer
*
210 st_new_renderbuffer_fb(enum pipe_format format
)
212 struct st_renderbuffer
*strb
;
214 strb
= CALLOC_STRUCT(st_renderbuffer
);
216 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
220 _mesa_init_renderbuffer(&strb
->Base
, 0);
221 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
222 strb
->format
= format
;
225 case PIPE_FORMAT_A8R8G8B8_UNORM
:
226 case PIPE_FORMAT_B8G8R8A8_UNORM
:
227 case PIPE_FORMAT_A1R5G5B5_UNORM
:
228 case PIPE_FORMAT_A4R4G4B4_UNORM
:
229 case PIPE_FORMAT_R5G6B5_UNORM
:
230 strb
->Base
.InternalFormat
= GL_RGBA
;
231 strb
->Base
._BaseFormat
= GL_RGBA
;
233 case PIPE_FORMAT_Z16_UNORM
:
234 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
235 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
237 case PIPE_FORMAT_Z32_UNORM
:
238 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
239 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
241 case PIPE_FORMAT_S8Z24_UNORM
:
242 case PIPE_FORMAT_Z24S8_UNORM
:
243 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
244 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
246 case PIPE_FORMAT_S8_UNORM
:
247 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
248 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
250 case PIPE_FORMAT_R16G16B16A16_SNORM
:
251 strb
->Base
.InternalFormat
= GL_RGBA16
;
252 strb
->Base
._BaseFormat
= GL_RGBA
;
256 "Unexpected format in st_new_renderbuffer_fb");
260 /* st-specific methods */
261 strb
->Base
.Delete
= st_renderbuffer_delete
;
262 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
263 strb
->Base
.GetPointer
= null_get_pointer
;
265 /* surface is allocated in st_renderbuffer_alloc_storage() */
266 strb
->surface
= NULL
;
275 * Called via ctx->Driver.BindFramebufferEXT().
278 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
279 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
285 * Called by ctx->Driver.FramebufferRenderbuffer
288 st_framebuffer_renderbuffer(GLcontext
*ctx
,
289 struct gl_framebuffer
*fb
,
291 struct gl_renderbuffer
*rb
)
293 /* XXX no need for derivation? */
294 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
299 * Called by ctx->Driver.RenderTexture
302 st_render_texture(GLcontext
*ctx
,
303 struct gl_framebuffer
*fb
,
304 struct gl_renderbuffer_attachment
*att
)
306 struct st_context
*st
= ctx
->st
;
307 struct st_renderbuffer
*strb
;
308 struct gl_renderbuffer
*rb
;
309 struct pipe_context
*pipe
= st
->pipe
;
310 struct pipe_screen
*screen
= pipe
->screen
;
311 struct pipe_texture
*pt
;
313 assert(!att
->Renderbuffer
);
315 /* create new renderbuffer which wraps the texture image */
316 rb
= st_new_renderbuffer(ctx
, 0);
318 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
322 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
323 assert(rb
->RefCount
== 1);
324 rb
->AllocStorage
= NULL
; /* should not get called */
325 strb
= st_renderbuffer(rb
);
327 /* get the texture for the texture object */
328 pt
= st_get_texobj_texture(att
->Texture
);
330 assert(pt
->width
[att
->TextureLevel
]);
332 rb
->Width
= pt
->width
[att
->TextureLevel
];
333 rb
->Height
= pt
->height
[att
->TextureLevel
];
335 /* the renderbuffer's surface is inside the texture */
336 strb
->surface
= screen
->get_tex_surface(screen
, pt
,
340 assert(strb
->surface
);
342 init_renderbuffer_bits(strb
, pt
->format
);
345 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
346 att->Texture, pt, strb->surface, rb->Width, rb->Height);
349 /* Invalidate buffer state so that the pipe's framebuffer state
351 * That's where the new renderbuffer (which we just created) gets
352 * passed to the pipe as a (color/depth) render target.
354 st_invalidate_state(ctx
, _NEW_BUFFERS
);
359 * Called via ctx->Driver.FinishRenderTexture.
362 st_finish_render_texture(GLcontext
*ctx
,
363 struct gl_renderbuffer_attachment
*att
)
365 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
369 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, PIPE_FLUSH_RENDER_CACHE
);
372 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
375 pipe_surface_reference(&strb
->surface
, NULL
);
377 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
379 /* restore previous framebuffer state */
380 st_invalidate_state(ctx
, _NEW_BUFFERS
);
385 void st_init_fbo_functions(struct dd_function_table
*functions
)
387 functions
->NewFramebuffer
= st_new_framebuffer
;
388 functions
->NewRenderbuffer
= st_new_renderbuffer
;
389 functions
->BindFramebuffer
= st_bind_framebuffer
;
390 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
391 functions
->RenderTexture
= st_render_texture
;
392 functions
->FinishRenderTexture
= st_finish_render_texture
;
393 /* no longer needed by core Mesa, drivers handle resizes...
394 functions->ResizeBuffers = st_resize_buffers;