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 /* Softpipe operates on quads, so pad dimensions to multiples of 2 */
104 height
+= height
& 1;
106 strb
->surface
->region
= pipe
->region_alloc(pipe
, cpp
, width
, height
, flags
);
107 if (!strb
->surface
->region
)
108 return GL_FALSE
; /* out of memory, try s/w buffer? */
110 ASSERT(strb
->surface
->region
->buffer
);
111 ASSERT(strb
->surface
->format
);
113 strb
->Base
.Width
= strb
->surface
->width
= width
;
114 strb
->Base
.Height
= strb
->surface
->height
= height
;
121 * gl_renderbuffer::Delete()
124 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
126 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
127 GET_CURRENT_CONTEXT(ctx
);
129 struct pipe_context
*pipe
= ctx
->st
->pipe
;
131 if (strb
&& strb
->surface
) {
132 if (strb
->surface
->region
) {
133 pipe
->region_release(pipe
, &strb
->surface
->region
);
139 _mesa_warning(NULL
, "st_renderbuffer_delete() called, but no current context");
146 * gl_renderbuffer::GetPointer()
149 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
152 /* By returning NULL we force all software rendering to go through
156 assert(0); /* Should never get called with softpipe */
163 * Called via ctx->Driver.NewFramebuffer()
165 static struct gl_framebuffer
*
166 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
168 /* XXX not sure we need to subclass gl_framebuffer for pipe */
169 return _mesa_new_framebuffer(ctx
, name
);
174 * Called via ctx->Driver.NewRenderbuffer()
176 static struct gl_renderbuffer
*
177 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
179 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
181 _mesa_init_renderbuffer(&strb
->Base
, name
);
182 strb
->Base
.Delete
= st_renderbuffer_delete
;
183 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
184 strb
->Base
.GetPointer
= null_get_pointer
;
192 struct gl_renderbuffer
*
193 st_new_renderbuffer_fb(struct pipe_region
*region
, GLuint width
, GLuint height
)
195 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
199 _mesa_init_renderbuffer(&strb
->Base
, name
);
200 strb
->Base
.Delete
= st_renderbuffer_delete
;
201 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
202 strb
->Base
.GetPointer
= null_get_pointer
;
203 strb
->Base
.Width
= width
;
204 strb
->Base
.Heigth
= height
;
206 strb
->region
= region
;
213 struct gl_renderbuffer
*
214 st_new_renderbuffer_fb(GLenum intFormat
)
216 struct st_renderbuffer
*strb
;
218 strb
= CALLOC_STRUCT(st_renderbuffer
);
220 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
224 _mesa_init_renderbuffer(&strb
->Base
, 0);
225 strb
->Base
.ClassID
= 0x42; /* XXX temp */
226 strb
->Base
.InternalFormat
= intFormat
;
231 strb
->Base
._BaseFormat
= GL_RGBA
;
233 case GL_DEPTH_COMPONENT16
:
234 case GL_DEPTH_COMPONENT32
:
235 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
237 case GL_DEPTH24_STENCIL8_EXT
:
238 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
240 case GL_STENCIL_INDEX8_EXT
:
241 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
245 "Unexpected intFormat in st_new_renderbuffer");
249 /* st-specific methods */
250 strb
->Base
.Delete
= st_renderbuffer_delete
;
251 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
252 strb
->Base
.GetPointer
= null_get_pointer
;
254 /* surface is allocate in alloc_renderbuffer_storage() */
255 strb
->surface
= NULL
;
264 * Called via ctx->Driver.BindFramebufferEXT().
267 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
268 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
274 * Called by ctx->Driver.FramebufferRenderbuffer
277 st_framebuffer_renderbuffer(GLcontext
*ctx
,
278 struct gl_framebuffer
*fb
,
280 struct gl_renderbuffer
*rb
)
282 /* XXX no need for derivation? */
283 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
288 * Called by ctx->Driver.RenderTexture
291 st_render_texture(GLcontext
*ctx
,
292 struct gl_framebuffer
*fb
,
293 struct gl_renderbuffer_attachment
*att
)
295 struct st_context
*st
= ctx
->st
;
296 struct st_renderbuffer
*strb
;
297 struct gl_renderbuffer
*rb
;
298 struct pipe_context
*pipe
= st
->pipe
;
299 struct pipe_mipmap_tree
*mt
;
301 assert(!att
->Renderbuffer
);
303 /* create new renderbuffer which wraps the texture image */
304 rb
= st_new_renderbuffer(ctx
, 0);
306 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
310 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
311 assert(rb
->RefCount
== 1);
312 rb
->AllocStorage
= NULL
; /* should not get called */
313 strb
= st_renderbuffer(rb
);
315 /* get the mipmap tree for the texture */
316 mt
= st_get_texobj_mipmap_tree(att
->Texture
);
318 assert(mt
->level
[0].width
);
320 rb
->Width
= mt
->level
[0].width
;
321 rb
->Height
= mt
->level
[0].height
;
323 /* the renderbuffer's surface is inside the mipmap_tree: */
324 strb
->surface
= pipe
->get_tex_surface(pipe
, mt
,
328 assert(strb
->surface
);
331 printf("RENDER TO TEXTURE obj=%p mt=%p surf=%p %d x %d\n",
332 att->Texture, mt, strb->surface, rb->Width, rb->Height);
335 /* Invalidate buffer state so that the pipe's framebuffer state
337 * That's where the new renderbuffer (which we just created) gets
338 * passed to the pipe as a (color/depth) render target.
340 st_invalidate_state(ctx
, _NEW_BUFFERS
);
345 * Called via ctx->Driver.FinishRenderTexture.
348 st_finish_render_texture(GLcontext
*ctx
,
349 struct gl_renderbuffer_attachment
*att
)
351 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
355 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, 0x0);
358 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
361 pipe_surface_reference(&strb
->surface
, NULL
);
363 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
365 /* restore previous framebuffer state */
366 st_invalidate_state(ctx
, _NEW_BUFFERS
);
371 void st_init_fbo_functions(struct dd_function_table
*functions
)
373 functions
->NewFramebuffer
= st_new_framebuffer
;
374 functions
->NewRenderbuffer
= st_new_renderbuffer
;
375 functions
->BindFramebuffer
= st_bind_framebuffer
;
376 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
377 functions
->RenderTexture
= st_render_texture
;
378 functions
->FinishRenderTexture
= st_finish_render_texture
;
379 /* no longer needed by core Mesa, drivers handle resizes...
380 functions->ResizeBuffers = st_resize_buffers;