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 "st_context.h"
45 #include "st_cb_fbo.h"
46 #include "st_cb_texture.h"
47 #include "st_format.h"
48 #include "st_public.h"
53 * gl_renderbuffer::AllocStorage()
56 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
57 GLenum internalFormat
,
58 GLuint width
, GLuint height
)
60 struct pipe_context
*pipe
= ctx
->st
->pipe
;
61 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
62 const GLuint pipeFormat
63 = st_choose_pipe_format(pipe
, internalFormat
, GL_NONE
, GL_NONE
);
64 const struct pipe_format_info
*info
= st_get_format_info(pipeFormat
);
66 GLbitfield flags
= PIPE_SURFACE_FLAG_RENDER
; /* want to render to surface */
73 case PIPE_FORMAT_S8_Z24
:
74 strb
->Base
.DataType
= GL_UNSIGNED_INT
;
77 strb
->Base
.DataType
= GL_UNSIGNED_BYTE
; /* XXX fix */
80 strb
->Base
._ActualFormat
= info
->base_format
;
81 strb
->Base
.RedBits
= info
->red_bits
;
82 strb
->Base
.GreenBits
= info
->green_bits
;
83 strb
->Base
.BlueBits
= info
->blue_bits
;
84 strb
->Base
.AlphaBits
= info
->alpha_bits
;
85 strb
->Base
.DepthBits
= info
->depth_bits
;
86 strb
->Base
.StencilBits
= info
->stencil_bits
;
91 strb
->surface
= pipe
->surface_alloc(pipe
, pipeFormat
);
92 assert(strb
->surface
);
98 if (strb
->surface
->region
) {
99 pipe
->region_release(pipe
, &strb
->surface
->region
);
102 strb
->surface
->region
= pipe
->region_alloc(pipe
, cpp
, width
, height
, flags
);
103 if (!strb
->surface
->region
)
104 return GL_FALSE
; /* out of memory, try s/w buffer? */
106 ASSERT(strb
->surface
->region
->buffer
);
108 strb
->Base
.Width
= strb
->surface
->width
= width
;
109 strb
->Base
.Height
= strb
->surface
->height
= height
;
116 * gl_renderbuffer::Delete()
119 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
121 GET_CURRENT_CONTEXT(ctx
);
122 struct pipe_context
*pipe
= ctx
->st
->pipe
;
123 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
125 if (strb
&& strb
->surface
) {
126 if (strb
->surface
->region
) {
127 pipe
->region_release(pipe
, &strb
->surface
->region
);
136 * gl_renderbuffer::GetPointer()
139 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
142 /* By returning NULL we force all software rendering to go through
146 assert(0); /* Should never get called with softpipe */
153 * Called via ctx->Driver.NewFramebuffer()
155 static struct gl_framebuffer
*
156 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
158 /* XXX not sure we need to subclass gl_framebuffer for pipe */
159 return _mesa_new_framebuffer(ctx
, name
);
164 * Called via ctx->Driver.NewRenderbuffer()
166 static struct gl_renderbuffer
*
167 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
169 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
171 _mesa_init_renderbuffer(&strb
->Base
, name
);
172 strb
->Base
.Delete
= st_renderbuffer_delete
;
173 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
174 strb
->Base
.GetPointer
= null_get_pointer
;
182 struct gl_renderbuffer
*
183 st_new_renderbuffer_fb(struct pipe_region
*region
, GLuint width
, GLuint height
)
185 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
189 _mesa_init_renderbuffer(&strb
->Base
, name
);
190 strb
->Base
.Delete
= st_renderbuffer_delete
;
191 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
192 strb
->Base
.GetPointer
= null_get_pointer
;
193 strb
->Base
.Width
= width
;
194 strb
->Base
.Heigth
= height
;
196 strb
->region
= region
;
203 struct gl_renderbuffer
*
204 st_new_renderbuffer_fb(GLenum intFormat
)
206 struct st_renderbuffer
*strb
;
208 strb
= CALLOC_STRUCT(st_renderbuffer
);
210 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
214 _mesa_init_renderbuffer(&strb
->Base
, 0);
215 strb
->Base
.ClassID
= 0x42; /* XXX temp */
216 strb
->Base
.InternalFormat
= intFormat
;
221 strb
->Base
._BaseFormat
= GL_RGBA
;
223 case GL_DEPTH_COMPONENT16
:
224 case GL_DEPTH_COMPONENT32
:
225 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
227 case GL_DEPTH24_STENCIL8_EXT
:
228 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
232 "Unexpected intFormat in st_new_renderbuffer");
236 /* st-specific methods */
237 strb
->Base
.Delete
= st_renderbuffer_delete
;
238 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
239 strb
->Base
.GetPointer
= null_get_pointer
;
241 /* surface is allocate in alloc_renderbuffer_storage() */
242 strb
->surface
= NULL
;
251 * Called via ctx->Driver.BindFramebufferEXT().
254 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
255 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
261 * Called by ctx->Driver.FramebufferRenderbuffer
264 st_framebuffer_renderbuffer(GLcontext
*ctx
,
265 struct gl_framebuffer
*fb
,
267 struct gl_renderbuffer
*rb
)
269 /* XXX no need for derivation? */
270 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
275 * Called by ctx->Driver.RenderTexture
278 st_render_texture(GLcontext
*ctx
,
279 struct gl_framebuffer
*fb
,
280 struct gl_renderbuffer_attachment
*att
)
282 struct st_context
*st
= ctx
->st
;
283 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
284 struct pipe_context
*pipe
= st
->pipe
;
285 struct pipe_framebuffer_state framebuffer
;
286 struct pipe_mipmap_tree
*mt
;
288 mt
= st_get_texobj_mipmap_tree(att
->Texture
);
290 /* the renderbuffer's surface is inside the mipmap_tree: */
291 strb
->surface
= pipe
->get_tex_surface(pipe
, mt
,
296 /* update pipe's framebuffer state */
297 memset(&framebuffer
, 0, sizeof(framebuffer
));
298 framebuffer
.num_cbufs
= 1;
299 framebuffer
.cbufs
[0] = strb
->surface
;
300 if (memcmp(&framebuffer
, &st
->state
.framebuffer
, sizeof(framebuffer
)) != 0) {
301 st
->state
.framebuffer
= framebuffer
;
302 st
->pipe
->set_framebuffer_state( st
->pipe
, &framebuffer
);
308 * Called via ctx->Driver.FinishRenderTexture.
311 st_finish_render_texture(GLcontext
*ctx
,
312 struct gl_renderbuffer_attachment
*att
)
314 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
316 pipe_surface_unreference(&strb
->surface
);
318 /* restore previous framebuffer state */
319 st_invalidate_state(ctx
, _NEW_BUFFERS
);
324 void st_init_fbo_functions(struct dd_function_table
*functions
)
326 functions
->NewFramebuffer
= st_new_framebuffer
;
327 functions
->NewRenderbuffer
= st_new_renderbuffer
;
328 functions
->BindFramebuffer
= st_bind_framebuffer
;
329 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
330 functions
->RenderTexture
= st_render_texture
;
331 functions
->FinishRenderTexture
= st_finish_render_texture
;
332 /* no longer needed by core Mesa, drivers handle resizes...
333 functions->ResizeBuffers = st_resize_buffers;