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 */
72 strb
->Base
._ActualFormat
= info
->base_format
;
73 strb
->Base
.RedBits
= info
->red_bits
;
74 strb
->Base
.GreenBits
= info
->green_bits
;
75 strb
->Base
.BlueBits
= info
->blue_bits
;
76 strb
->Base
.AlphaBits
= info
->alpha_bits
;
77 strb
->Base
.DepthBits
= info
->depth_bits
;
78 strb
->Base
.StencilBits
= info
->stencil_bits
;
79 strb
->Base
.DataType
= st_format_datatype(pipeFormat
);
81 assert(strb
->Base
.DataType
);
86 strb
->surface
= pipe
->surface_alloc(pipe
, pipeFormat
);
87 assert(strb
->surface
);
93 if (strb
->surface
->region
) {
94 /* loop here since mapping is refcounted */
95 struct pipe_region
*r
= strb
->surface
->region
;
97 pipe
->region_unmap(pipe
, r
);
98 pipe
->region_release(pipe
, &strb
->surface
->region
);
101 strb
->surface
->region
= pipe
->region_alloc(pipe
, cpp
, width
, height
, flags
);
102 if (!strb
->surface
->region
)
103 return GL_FALSE
; /* out of memory, try s/w buffer? */
105 ASSERT(strb
->surface
->region
->buffer
);
106 ASSERT(strb
->surface
->format
);
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 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
122 GET_CURRENT_CONTEXT(ctx
);
124 struct pipe_context
*pipe
= ctx
->st
->pipe
;
126 if (strb
&& strb
->surface
) {
127 if (strb
->surface
->region
) {
128 pipe
->region_release(pipe
, &strb
->surface
->region
);
134 _mesa_warning(NULL
, "st_renderbuffer_delete() called, but no current context");
141 * gl_renderbuffer::GetPointer()
144 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
147 /* By returning NULL we force all software rendering to go through
151 assert(0); /* Should never get called with softpipe */
158 * Called via ctx->Driver.NewFramebuffer()
160 static struct gl_framebuffer
*
161 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
163 /* XXX not sure we need to subclass gl_framebuffer for pipe */
164 return _mesa_new_framebuffer(ctx
, name
);
169 * Called via ctx->Driver.NewRenderbuffer()
171 static struct gl_renderbuffer
*
172 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
174 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
176 _mesa_init_renderbuffer(&strb
->Base
, name
);
177 strb
->Base
.Delete
= st_renderbuffer_delete
;
178 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
179 strb
->Base
.GetPointer
= null_get_pointer
;
187 struct gl_renderbuffer
*
188 st_new_renderbuffer_fb(struct pipe_region
*region
, GLuint width
, GLuint height
)
190 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
->Base
.Width
= width
;
199 strb
->Base
.Heigth
= height
;
201 strb
->region
= region
;
208 struct gl_renderbuffer
*
209 st_new_renderbuffer_fb(GLenum intFormat
)
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
= 0x42; /* XXX temp */
221 strb
->Base
.InternalFormat
= intFormat
;
227 strb
->Base
._BaseFormat
= GL_RGBA
;
229 case GL_DEPTH_COMPONENT16
:
230 case GL_DEPTH_COMPONENT32
:
231 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
233 case GL_DEPTH24_STENCIL8_EXT
:
234 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
236 case GL_STENCIL_INDEX8_EXT
:
237 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
241 "Unexpected intFormat in st_new_renderbuffer");
245 /* st-specific methods */
246 strb
->Base
.Delete
= st_renderbuffer_delete
;
247 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
248 strb
->Base
.GetPointer
= null_get_pointer
;
250 /* surface is allocate in alloc_renderbuffer_storage() */
251 strb
->surface
= NULL
;
260 * Called via ctx->Driver.BindFramebufferEXT().
263 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
264 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
270 * Called by ctx->Driver.FramebufferRenderbuffer
273 st_framebuffer_renderbuffer(GLcontext
*ctx
,
274 struct gl_framebuffer
*fb
,
276 struct gl_renderbuffer
*rb
)
278 /* XXX no need for derivation? */
279 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
284 * Called by ctx->Driver.RenderTexture
287 st_render_texture(GLcontext
*ctx
,
288 struct gl_framebuffer
*fb
,
289 struct gl_renderbuffer_attachment
*att
)
291 struct st_context
*st
= ctx
->st
;
292 struct st_renderbuffer
*strb
;
293 struct gl_renderbuffer
*rb
;
294 struct pipe_context
*pipe
= st
->pipe
;
295 struct pipe_mipmap_tree
*mt
;
297 assert(!att
->Renderbuffer
);
299 /* create new renderbuffer which wraps the texture image */
300 rb
= st_new_renderbuffer(ctx
, 0);
302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
306 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
307 assert(rb
->RefCount
== 1);
308 rb
->AllocStorage
= NULL
; /* should not get called */
309 strb
= st_renderbuffer(rb
);
311 /* get the mipmap tree for the texture */
312 mt
= st_get_texobj_mipmap_tree(att
->Texture
);
314 assert(mt
->level
[att
->TextureLevel
].width
);
316 rb
->Width
= mt
->level
[att
->TextureLevel
].width
;
317 rb
->Height
= mt
->level
[att
->TextureLevel
].height
;
319 /* the renderbuffer's surface is inside the mipmap_tree: */
320 strb
->surface
= pipe
->get_tex_surface(pipe
, mt
,
324 assert(strb
->surface
);
327 printf("RENDER TO TEXTURE obj=%p mt=%p surf=%p %d x %d\n",
328 att->Texture, mt, strb->surface, rb->Width, rb->Height);
331 /* Invalidate buffer state so that the pipe's framebuffer state
333 * That's where the new renderbuffer (which we just created) gets
334 * passed to the pipe as a (color/depth) render target.
336 st_invalidate_state(ctx
, _NEW_BUFFERS
);
341 * Called via ctx->Driver.FinishRenderTexture.
344 st_finish_render_texture(GLcontext
*ctx
,
345 struct gl_renderbuffer_attachment
*att
)
347 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
351 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, 0x0);
354 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
357 pipe_surface_reference(&strb
->surface
, NULL
);
359 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
361 /* restore previous framebuffer state */
362 st_invalidate_state(ctx
, _NEW_BUFFERS
);
367 void st_init_fbo_functions(struct dd_function_table
*functions
)
369 functions
->NewFramebuffer
= st_new_framebuffer
;
370 functions
->NewRenderbuffer
= st_new_renderbuffer
;
371 functions
->BindFramebuffer
= st_bind_framebuffer
;
372 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
373 functions
->RenderTexture
= st_render_texture
;
374 functions
->FinishRenderTexture
= st_finish_render_texture
;
375 /* no longer needed by core Mesa, drivers handle resizes...
376 functions->ResizeBuffers = st_resize_buffers;