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 uint type
= strb
->screenSurface
? PIPE_SCREEN_SURFACE
: PIPE_SURFACE
;
91 const enum pipe_format pipeFormat
92 = st_choose_pipe_format(pipe
, internalFormat
, GL_NONE
, GL_NONE
, type
);
93 GLbitfield flags
= 0x0; /* XXX needed? */
96 strb
->surface
= pipe
->winsys
->surface_alloc(pipe
->winsys
);
97 assert(strb
->surface
);
102 /* loop here since mapping is refcounted */
103 while (strb
->surface
->map
)
104 pipe_surface_unmap(strb
->surface
);
105 if (strb
->surface
->buffer
)
106 pipe
->winsys
->buffer_reference(pipe
->winsys
, &strb
->surface
->buffer
,
109 pipe
->winsys
->surface_alloc_storage(pipe
->winsys
,
115 if (!strb
->surface
->buffer
)
116 return GL_FALSE
; /* out of memory, try s/w buffer? */
118 ASSERT(strb
->surface
->buffer
);
119 ASSERT(strb
->surface
->format
);
120 ASSERT(strb
->surface
->cpp
);
121 ASSERT(strb
->surface
->width
== width
);
122 ASSERT(strb
->surface
->height
== height
);
123 ASSERT(strb
->surface
->pitch
);
125 strb
->Base
.Width
= width
;
126 strb
->Base
.Height
= height
;
133 * gl_renderbuffer::Delete()
136 st_renderbuffer_delete(struct gl_renderbuffer
*rb
)
138 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
141 struct pipe_winsys
*ws
= strb
->surface
->winsys
;
142 ws
->surface_release(ws
, &strb
->surface
);
149 * gl_renderbuffer::GetPointer()
152 null_get_pointer(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
155 /* By returning NULL we force all software rendering to go through
159 assert(0); /* Should never get called with softpipe */
166 * Called via ctx->Driver.NewFramebuffer()
168 static struct gl_framebuffer
*
169 st_new_framebuffer(GLcontext
*ctx
, GLuint name
)
171 /* XXX not sure we need to subclass gl_framebuffer for pipe */
172 return _mesa_new_framebuffer(ctx
, name
);
177 * Called via ctx->Driver.NewRenderbuffer()
179 static struct gl_renderbuffer
*
180 st_new_renderbuffer(GLcontext
*ctx
, GLuint name
)
182 struct st_renderbuffer
*strb
= CALLOC_STRUCT(st_renderbuffer
);
184 _mesa_init_renderbuffer(&strb
->Base
, name
);
185 strb
->Base
.Delete
= st_renderbuffer_delete
;
186 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
187 strb
->Base
.GetPointer
= null_get_pointer
;
195 * Allocate a renderbuffer for a an on-screen window (not a user-created
196 * renderbuffer). The window system code determines the internal format.
197 * \param screenSurface indicates if the renderbuffer is a front/back color
198 * buffer that'll be displayed/copied to the screen
200 struct gl_renderbuffer
*
201 st_new_renderbuffer_fb(GLenum intFormat
, GLboolean screenSurface
)
203 struct st_renderbuffer
*strb
;
205 strb
= CALLOC_STRUCT(st_renderbuffer
);
207 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "creating renderbuffer");
211 _mesa_init_renderbuffer(&strb
->Base
, 0);
212 strb
->Base
.ClassID
= 0x4242; /* just a unique value */
213 strb
->Base
.InternalFormat
= intFormat
;
214 strb
->screenSurface
= screenSurface
;
220 strb
->Base
._BaseFormat
= GL_RGBA
;
222 case GL_DEPTH_COMPONENT16
:
223 case GL_DEPTH_COMPONENT32
:
224 strb
->Base
._BaseFormat
= GL_DEPTH_COMPONENT
;
226 case GL_DEPTH24_STENCIL8_EXT
:
227 strb
->Base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
229 case GL_STENCIL_INDEX8_EXT
:
230 strb
->Base
._BaseFormat
= GL_STENCIL_INDEX
;
234 "Unexpected intFormat in st_new_renderbuffer");
238 /* st-specific methods */
239 strb
->Base
.Delete
= st_renderbuffer_delete
;
240 strb
->Base
.AllocStorage
= st_renderbuffer_alloc_storage
;
241 strb
->Base
.GetPointer
= null_get_pointer
;
243 /* surface is allocated in st_renderbuffer_alloc_storage() */
244 strb
->surface
= NULL
;
252 * Called via ctx->Driver.BindFramebufferEXT().
255 st_bind_framebuffer(GLcontext
*ctx
, GLenum target
,
256 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
262 * Called by ctx->Driver.FramebufferRenderbuffer
265 st_framebuffer_renderbuffer(GLcontext
*ctx
,
266 struct gl_framebuffer
*fb
,
268 struct gl_renderbuffer
*rb
)
270 /* XXX no need for derivation? */
271 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
276 * Called by ctx->Driver.RenderTexture
279 st_render_texture(GLcontext
*ctx
,
280 struct gl_framebuffer
*fb
,
281 struct gl_renderbuffer_attachment
*att
)
283 struct st_context
*st
= ctx
->st
;
284 struct st_renderbuffer
*strb
;
285 struct gl_renderbuffer
*rb
;
286 struct pipe_context
*pipe
= st
->pipe
;
287 struct pipe_texture
*pt
;
289 assert(!att
->Renderbuffer
);
291 /* create new renderbuffer which wraps the texture image */
292 rb
= st_new_renderbuffer(ctx
, 0);
294 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture()");
298 _mesa_reference_renderbuffer(&att
->Renderbuffer
, rb
);
299 assert(rb
->RefCount
== 1);
300 rb
->AllocStorage
= NULL
; /* should not get called */
301 strb
= st_renderbuffer(rb
);
303 /* get the texture for the texture object */
304 pt
= st_get_texobj_texture(att
->Texture
);
306 assert(pt
->width
[att
->TextureLevel
]);
308 rb
->Width
= pt
->width
[att
->TextureLevel
];
309 rb
->Height
= pt
->height
[att
->TextureLevel
];
311 /* the renderbuffer's surface is inside the texture */
312 strb
->surface
= pipe
->get_tex_surface(pipe
, pt
,
316 assert(strb
->surface
);
318 init_renderbuffer_bits(strb
, pt
->format
);
321 printf("RENDER TO TEXTURE obj=%p pt=%p surf=%p %d x %d\n",
322 att->Texture, pt, strb->surface, rb->Width, rb->Height);
325 /* Invalidate buffer state so that the pipe's framebuffer state
327 * That's where the new renderbuffer (which we just created) gets
328 * passed to the pipe as a (color/depth) render target.
330 st_invalidate_state(ctx
, _NEW_BUFFERS
);
335 * Called via ctx->Driver.FinishRenderTexture.
338 st_finish_render_texture(GLcontext
*ctx
,
339 struct gl_renderbuffer_attachment
*att
)
341 struct st_renderbuffer
*strb
= st_renderbuffer(att
->Renderbuffer
);
345 ctx
->st
->pipe
->flush(ctx
->st
->pipe
, 0x0);
348 printf("FINISH RENDER TO TEXTURE surf=%p\n", strb->surface);
351 pipe_surface_reference(&strb
->surface
, NULL
);
353 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
355 /* restore previous framebuffer state */
356 st_invalidate_state(ctx
, _NEW_BUFFERS
);
361 void st_init_fbo_functions(struct dd_function_table
*functions
)
363 functions
->NewFramebuffer
= st_new_framebuffer
;
364 functions
->NewRenderbuffer
= st_new_renderbuffer
;
365 functions
->BindFramebuffer
= st_bind_framebuffer
;
366 functions
->FramebufferRenderbuffer
= st_framebuffer_renderbuffer
;
367 functions
->RenderTexture
= st_render_texture
;
368 functions
->FinishRenderTexture
= st_finish_render_texture
;
369 /* no longer needed by core Mesa, drivers handle resizes...
370 functions->ResizeBuffers = st_resize_buffers;