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"
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
);
81 * gl_renderbuffer::AllocStorage()
84 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
85 GLenum internalFormat
,
86 GLuint width
, GLuint height
)
88 struct pipe_context
*pipe
= ctx
->st
->pipe
;
89 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
90 enum pipe_format pipeFormat
;
91 GLbitfield flags
= 0x0; /* XXX needed? */
94 strb
->surface
= pipe
->winsys
->surface_alloc(pipe
->winsys
);
95 assert(strb
->surface
);
96 assert(strb
->surface
->refcount
);
97 assert(strb
->surface
->winsys
);
102 if (strb
->surface
->buffer
)
103 pipe_buffer_reference(pipe
->winsys
, &strb
->surface
->buffer
,
106 /* Determine surface format here */
107 if (strb
->format
!= PIPE_FORMAT_NONE
) {
108 assert(strb
->format
!= 0);
109 /* we'll hit this for front/back color bufs */
110 pipeFormat
= strb
->format
;
113 pipeFormat
= st_choose_renderbuffer_format(pipe
, internalFormat
);
116 init_renderbuffer_bits(strb
, pipeFormat
);
118 pipe
->winsys
->surface_alloc_storage(pipe
->winsys
,
124 if (!strb
->surface
->buffer
)
125 return GL_FALSE
; /* out of memory, try s/w buffer? */
127 ASSERT(strb
->surface
->buffer
);
128 ASSERT(strb
->surface
->format
);
129 ASSERT(strb
->surface
->cpp
);
130 ASSERT(strb
->surface
->width
== width
);
131 ASSERT(strb
->surface
->height
== height
);
132 ASSERT(strb
->surface
->pitch
);
134 strb
->Base
.Width
= width
;
135 strb
->Base
.Height
= height
;
142 * gl_renderbuffer::Delete()
145 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
147 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
150 struct pipe_winsys
*ws
= strb
->surface
->winsys
;
151 ws
->surface_release(ws
, &strb
->surface
);
158 * gl_renderbuffer::GetPointer()
161 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
164 /* By returning NULL we force all software rendering to go through
168 assert(0); /* Should never get called with softpipe */
175 * Called via ctx->Driver.NewFramebuffer()
177 static struct gl_framebuffer
*
178 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
180 /* XXX not sure we need to subclass gl_framebuffer for pipe */
181 return _mesa_new_framebuffer(ctx
, name
);
186 * Called via ctx->Driver.NewRenderbuffer()
188 static struct gl_renderbuffer
*
189 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
191 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
193 _mesa_init_renderbuffer(&strb
->Base
, name
);
194 strb
->Base
.Delete
= st_renderbuffer_delete
;
195 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
196 strb
->Base
.GetPointer
= null_get_pointer
;
197 strb
->format
= PIPE_FORMAT_NONE
;
205 * Allocate a renderbuffer for a an on-screen window (not a user-created
206 * renderbuffer). The window system code determines the format.
208 struct gl_renderbuffer
*
209 st_new_renderbuffer_fb(enum pipe_format format
)
211 struct st_renderbuffer
*strb
;
213 strb
= CALLOC_STRUCT(st_renderbuffer
);
215 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
219 _mesa_init_renderbuffer(&strb
->Base
, 0);
220 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
221 strb
->format
= format
;
224 case PIPE_FORMAT_A8R8G8B8_UNORM
:
225 case PIPE_FORMAT_B8G8R8A8_UNORM
:
226 case PIPE_FORMAT_A1R5G5B5_UNORM
:
227 case PIPE_FORMAT_A4R4G4B4_UNORM
:
228 case PIPE_FORMAT_R5G6B5_UNORM
:
229 strb
->Base
.InternalFormat
= GL_RGBA
;
230 strb
->Base
._BaseFormat
= GL_RGBA
;
232 case PIPE_FORMAT_Z16_UNORM
:
233 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT16
;
234 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
236 case PIPE_FORMAT_Z32_UNORM
:
237 strb
->Base
.InternalFormat
= GL_DEPTH_COMPONENT32
;
238 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
240 case PIPE_FORMAT_S8Z24_UNORM
:
241 case PIPE_FORMAT_Z24S8_UNORM
:
242 strb
->Base
.InternalFormat
= GL_DEPTH24_STENCIL8_EXT
;
243 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
245 case PIPE_FORMAT_S8_UNORM
:
246 strb
->Base
.InternalFormat
= GL_STENCIL_INDEX8_EXT
;
247 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
249 case PIPE_FORMAT_R16G16B16A16_SNORM
:
250 strb
->Base
.InternalFormat
= GL_RGBA16
;
251 strb
->Base
._BaseFormat
= GL_RGBA
;
255 "Unexpected format in st_new_renderbuffer_fb");
259 /* st-specific methods */
260 strb
->Base
.Delete
= st_renderbuffer_delete
;
261 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
262 strb
->Base
.GetPointer
= null_get_pointer
;
264 /* surface is allocated in st_renderbuffer_alloc_storage() */
265 strb
->surface
= NULL
;
274 * Called via ctx->Driver.BindFramebufferEXT().
277 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
278 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
284 * Called by ctx->Driver.FramebufferRenderbuffer
287 st_framebuffer_renderbuffer(GLcontext
*ctx
,
288 struct gl_framebuffer
*fb
,
290 struct gl_renderbuffer
*rb
)
292 /* XXX no need for derivation? */
293 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
298 * Called by ctx->Driver.RenderTexture
301 st_render_texture(GLcontext
*ctx
,
302 struct gl_framebuffer
*fb
,
303 struct gl_renderbuffer_attachment
*att
)
305 struct st_context
*st
= ctx
->st
;
306 struct st_renderbuffer
*strb
;
307 struct gl_renderbuffer
*rb
;
308 struct pipe_context
*pipe
= st
->pipe
;
309 struct pipe_texture
*pt
;
311 assert(!att
->Renderbuffer
);
313 /* create new renderbuffer which wraps the texture image */
314 rb
= st_new_renderbuffer(ctx
, 0);
316 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
320 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
321 assert(rb
->RefCount
== 1);
322 rb
->AllocStorage
= NULL
; /* should not get called */
323 strb
= st_renderbuffer(rb
);
325 /* get the texture for the texture object */
326 pt
= st_get_texobj_texture(att
->Texture
);
328 assert(pt
->width
[att
->TextureLevel
]);
330 rb
->Width
= pt
->width
[att
->TextureLevel
];
331 rb
->Height
= pt
->height
[att
->TextureLevel
];
333 /* the renderbuffer's surface is inside the texture */
334 strb
->surface
= pipe
->get_tex_surface(pipe
, pt
,
338 assert(strb
->surface
);
340 init_renderbuffer_bits(strb
, pt
->format
);
343 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
344 att->Texture, pt, strb->surface, rb->Width, rb->Height);
347 /* Invalidate buffer state so that the pipe's framebuffer state
349 * That's where the new renderbuffer (which we just created) gets
350 * passed to the pipe as a (color/depth) render target.
352 st_invalidate_state(ctx
, _NEW_BUFFERS
);
357 * Called via ctx->Driver.FinishRenderTexture.
360 st_finish_render_texture(GLcontext
*ctx
,
361 struct gl_renderbuffer_attachment
*att
)
363 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
367 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, PIPE_FLUSH_RENDER_CACHE
);
370 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
373 pipe_surface_reference(&strb
->surface
, NULL
);
375 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
377 /* restore previous framebuffer state */
378 st_invalidate_state(ctx
, _NEW_BUFFERS
);
383 void st_init_fbo_functions(struct dd_function_table
*functions
)
385 functions
->NewFramebuffer
= st_new_framebuffer
;
386 functions
->NewRenderbuffer
= st_new_renderbuffer
;
387 functions
->BindFramebuffer
= st_bind_framebuffer
;
388 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
389 functions
->RenderTexture
= st_render_texture
;
390 functions
->FinishRenderTexture
= st_finish_render_texture
;
391 /* no longer needed by core Mesa, drivers handle resizes...
392 functions->ResizeBuffers = st_resize_buffers;