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_winsys.h"
45 #include "st_context.h"
46 #include "st_cb_fbo.h"
47 #include "st_cb_texture.h"
48 #include "st_format.h"
49 #include "st_public.h"
54 * gl_renderbuffer::AllocStorage()
57 st_renderbuffer_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
58 GLenum internalFormat
,
59 GLuint width
, GLuint height
)
61 struct pipe_context
*pipe
= ctx
->st
->pipe
;
62 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
63 const GLuint pipeFormat
64 = st_choose_pipe_format(pipe
, internalFormat
, GL_NONE
, GL_NONE
);
65 const struct pipe_format_info
*info
= st_get_format_info(pipeFormat
);
67 GLbitfield flags
= PIPE_SURFACE_FLAG_RENDER
; /* want to render to surface */
73 strb
->Base
._ActualFormat
= info
->base_format
;
74 strb
->Base
.RedBits
= info
->red_bits
;
75 strb
->Base
.GreenBits
= info
->green_bits
;
76 strb
->Base
.BlueBits
= info
->blue_bits
;
77 strb
->Base
.AlphaBits
= info
->alpha_bits
;
78 strb
->Base
.DepthBits
= info
->depth_bits
;
79 strb
->Base
.StencilBits
= info
->stencil_bits
;
80 strb
->Base
.DataType
= st_format_datatype(pipeFormat
);
82 assert(strb
->Base
.DataType
);
87 strb
->surface
= pipe
->winsys
->surface_alloc(pipe
->winsys
, pipeFormat
);
88 assert(strb
->surface
);
94 if (strb
->surface
->region
) {
95 /* loop here since mapping is refcounted */
96 struct pipe_region
*r
= strb
->surface
->region
;
98 pipe
->region_unmap(pipe
, r
);
99 pipe
->winsys
->region_release(pipe
->winsys
, &strb
->surface
->region
);
102 strb
->surface
->region
= pipe
->winsys
->region_alloc(pipe
->winsys
, cpp
,
103 width
, height
, flags
);
104 if (!strb
->surface
->region
)
105 return GL_FALSE
; /* out of memory, try s/w buffer? */
107 ASSERT(strb
->surface
->region
->buffer
);
108 ASSERT(strb
->surface
->format
);
110 strb
->Base
.Width
= strb
->surface
->width
= width
;
111 strb
->Base
.Height
= strb
->surface
->height
= height
;
118 * gl_renderbuffer::Delete()
121 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
123 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
124 GET_CURRENT_CONTEXT(ctx
);
126 struct pipe_context
*pipe
= ctx
->st
->pipe
;
128 if (strb
&& strb
->surface
) {
129 if (strb
->surface
->region
) {
130 pipe
->winsys
->region_release(pipe
->winsys
, &strb
->surface
->region
);
136 _mesa_warning(NULL
, "st_renderbuffer_delete() called, but no current context");
143 * gl_renderbuffer::GetPointer()
146 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
149 /* By returning NULL we force all software rendering to go through
153 assert(0); /* Should never get called with softpipe */
160 * Called via ctx->Driver.NewFramebuffer()
162 static struct gl_framebuffer
*
163 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
165 /* XXX not sure we need to subclass gl_framebuffer for pipe */
166 return _mesa_new_framebuffer(ctx
, name
);
171 * Called via ctx->Driver.NewRenderbuffer()
173 static struct gl_renderbuffer
*
174 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
176 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
178 _mesa_init_renderbuffer(&strb
->Base
, name
);
179 strb
->Base
.Delete
= st_renderbuffer_delete
;
180 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
181 strb
->Base
.GetPointer
= null_get_pointer
;
189 struct gl_renderbuffer
*
190 st_new_renderbuffer_fb(struct pipe_region
*region
, GLuint width
, GLuint height
)
192 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
196 _mesa_init_renderbuffer(&strb
->Base
, name
);
197 strb
->Base
.Delete
= st_renderbuffer_delete
;
198 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
199 strb
->Base
.GetPointer
= null_get_pointer
;
200 strb
->Base
.Width
= width
;
201 strb
->Base
.Heigth
= height
;
203 strb
->region
= region
;
210 struct gl_renderbuffer
*
211 st_new_renderbuffer_fb(GLenum intFormat
)
213 struct st_renderbuffer
*strb
;
215 strb
= CALLOC_STRUCT(st_renderbuffer
);
217 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
221 _mesa_init_renderbuffer(&strb
->Base
, 0);
222 strb
->Base
.ClassID
= 0x42; /* XXX temp */
223 strb
->Base
.InternalFormat
= intFormat
;
229 strb
->Base
._BaseFormat
= GL_RGBA
;
231 case GL_DEPTH_COMPONENT16
:
232 case GL_DEPTH_COMPONENT32
:
233 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
235 case GL_DEPTH24_STENCIL8_EXT
:
236 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
238 case GL_STENCIL_INDEX8_EXT
:
239 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
243 "Unexpected intFormat in st_new_renderbuffer");
247 /* st-specific methods */
248 strb
->Base
.Delete
= st_renderbuffer_delete
;
249 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
250 strb
->Base
.GetPointer
= null_get_pointer
;
252 /* surface is allocate in alloc_renderbuffer_storage() */
253 strb
->surface
= NULL
;
262 * Called via ctx->Driver.BindFramebufferEXT().
265 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
266 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
272 * Called by ctx->Driver.FramebufferRenderbuffer
275 st_framebuffer_renderbuffer(GLcontext
*ctx
,
276 struct gl_framebuffer
*fb
,
278 struct gl_renderbuffer
*rb
)
280 /* XXX no need for derivation? */
281 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
286 * Called by ctx->Driver.RenderTexture
289 st_render_texture(GLcontext
*ctx
,
290 struct gl_framebuffer
*fb
,
291 struct gl_renderbuffer_attachment
*att
)
293 struct st_context
*st
= ctx
->st
;
294 struct st_renderbuffer
*strb
;
295 struct gl_renderbuffer
*rb
;
296 struct pipe_context
*pipe
= st
->pipe
;
297 struct pipe_mipmap_tree
*mt
;
299 assert(!att
->Renderbuffer
);
301 /* create new renderbuffer which wraps the texture image */
302 rb
= st_new_renderbuffer(ctx
, 0);
304 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
308 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
309 assert(rb
->RefCount
== 1);
310 rb
->AllocStorage
= NULL
; /* should not get called */
311 strb
= st_renderbuffer(rb
);
313 /* get the mipmap tree for the texture */
314 mt
= st_get_texobj_mipmap_tree(att
->Texture
);
316 assert(mt
->level
[att
->TextureLevel
].width
);
318 rb
->Width
= mt
->level
[att
->TextureLevel
].width
;
319 rb
->Height
= mt
->level
[att
->TextureLevel
].height
;
321 /* the renderbuffer's surface is inside the mipmap_tree: */
322 strb
->surface
= pipe
->get_tex_surface(pipe
, mt
,
326 assert(strb
->surface
);
329 printf("RENDER TO TEXTURE obj=%p mt=%p surf=%p %d x %d\n",
330 att->Texture, mt, strb->surface, rb->Width, rb->Height);
333 /* Invalidate buffer state so that the pipe's framebuffer state
335 * That's where the new renderbuffer (which we just created) gets
336 * passed to the pipe as a (color/depth) render target.
338 st_invalidate_state(ctx
, _NEW_BUFFERS
);
343 * Called via ctx->Driver.FinishRenderTexture.
346 st_finish_render_texture(GLcontext
*ctx
,
347 struct gl_renderbuffer_attachment
*att
)
349 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
353 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, 0x0);
356 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
359 pipe_surface_reference(&strb
->surface
, NULL
);
361 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
363 /* restore previous framebuffer state */
364 st_invalidate_state(ctx
, _NEW_BUFFERS
);
369 void st_init_fbo_functions(struct dd_function_table
*functions
)
371 functions
->NewFramebuffer
= st_new_framebuffer
;
372 functions
->NewRenderbuffer
= st_new_renderbuffer
;
373 functions
->BindFramebuffer
= st_bind_framebuffer
;
374 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
375 functions
->RenderTexture
= st_render_texture
;
376 functions
->FinishRenderTexture
= st_finish_render_texture
;
377 /* no longer needed by core Mesa, drivers handle resizes...
378 functions->ResizeBuffers = st_resize_buffers;