1 /**************************************************************************
3 * Copyright 2008 Red Hat Inc.
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 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/context.h"
36 #include "main/texformat.h"
37 #include "main/texrender.h"
39 #include "radeon_common.h"
40 #include "radeon_mipmap_tree.h"
42 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
43 #define DBG(...) do { \
44 if (RADEON_DEBUG & FILE_DEBUG_FLAG) \
45 _mesa_printf(__VA_ARGS__); \
48 static struct gl_framebuffer
*
49 radeon_new_framebuffer(GLcontext
*ctx
, GLuint name
)
51 return _mesa_new_framebuffer(ctx
, name
);
55 radeon_delete_renderbuffer(struct gl_renderbuffer
*rb
)
57 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(rb
);
62 radeon_bo_unref(rrb
->bo
);
68 radeon_get_pointer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
75 * Called via glRenderbufferStorageEXT() to set the format and allocate
76 * storage for a user-created renderbuffer.
79 radeon_alloc_renderbuffer_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
80 GLenum internalFormat
,
81 GLuint width
, GLuint height
)
83 struct radeon_context
*radeon
= RADEON_CONTEXT(ctx
);
84 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(rb
);
85 GLboolean software_buffer
= GL_FALSE
;
88 ASSERT(rb
->Name
!= 0);
89 switch (internalFormat
) {
93 rb
->_ActualFormat
= GL_RGB5
;
94 rb
->DataType
= GL_UNSIGNED_BYTE
;
105 rb
->_ActualFormat
= GL_RGB8
;
106 rb
->DataType
= GL_UNSIGNED_BYTE
;
121 rb
->_ActualFormat
= GL_RGBA8
;
122 rb
->DataType
= GL_UNSIGNED_BYTE
;
129 case GL_STENCIL_INDEX
:
130 case GL_STENCIL_INDEX1_EXT
:
131 case GL_STENCIL_INDEX4_EXT
:
132 case GL_STENCIL_INDEX8_EXT
:
133 case GL_STENCIL_INDEX16_EXT
:
134 /* alloc a depth+stencil buffer */
135 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
136 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
140 case GL_DEPTH_COMPONENT16
:
141 rb
->_ActualFormat
= GL_DEPTH_COMPONENT16
;
142 rb
->DataType
= GL_UNSIGNED_SHORT
;
146 case GL_DEPTH_COMPONENT
:
147 case GL_DEPTH_COMPONENT24
:
148 case GL_DEPTH_COMPONENT32
:
149 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
150 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
154 case GL_DEPTH_STENCIL_EXT
:
155 case GL_DEPTH24_STENCIL8_EXT
:
156 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
157 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
164 "Unexpected format in intel_alloc_renderbuffer_storage");
171 radeon_bo_unref(rrb
->bo
);
174 if (software_buffer
) {
175 return _mesa_soft_renderbuffer_storage(ctx
, rb
, internalFormat
,
179 uint32_t size
= width
* height
* cpp
;
180 uint32_t pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
182 fprintf(stderr
,"Allocating %d x %d radeon RBO (pitch %d)\n", width
,
185 rrb
->pitch
= pitch
* cpp
;
187 rrb
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
191 RADEON_GEM_DOMAIN_VRAM
,
202 * Called for each hardware renderbuffer when a _window_ is resized.
203 * Just update fields.
204 * Not used for user-created renderbuffers!
207 radeon_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
208 GLenum internalFormat
, GLuint width
, GLuint height
)
210 ASSERT(rb
->Name
== 0);
213 rb
->_ActualFormat
= internalFormat
;
220 radeon_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
221 GLuint width
, GLuint height
)
223 struct radeon_framebuffer
*radeon_fb
= (struct radeon_framebuffer
*)fb
;
226 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
228 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
234 /* Make sure all window system renderbuffers are up to date */
235 for (i
= 0; i
< 2; i
++) {
236 struct gl_renderbuffer
*rb
= &radeon_fb
->color_rb
[i
]->base
;
238 /* only resize if size is changing */
239 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
240 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
246 /** Dummy function for gl_renderbuffer::AllocStorage() */
248 radeon_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
249 GLenum internalFormat
, GLuint width
, GLuint height
)
251 _mesa_problem(ctx
, "radeon_op_alloc_storage should never be called.");
255 struct radeon_renderbuffer
*
256 radeon_create_renderbuffer(GLenum format
, __DRIdrawablePrivate
*driDrawPriv
)
258 struct radeon_renderbuffer
*rrb
;
260 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
264 _mesa_init_renderbuffer(&rrb
->base
, 0);
265 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
267 /* XXX format junk */
270 rrb
->base
._ActualFormat
= GL_RGB5
;
271 rrb
->base
._BaseFormat
= GL_RGBA
;
272 rrb
->base
.RedBits
= 5;
273 rrb
->base
.GreenBits
= 6;
274 rrb
->base
.BlueBits
= 5;
275 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
278 rrb
->base
._ActualFormat
= GL_RGBA8
;
279 rrb
->base
._BaseFormat
= GL_RGBA
;
280 rrb
->base
.RedBits
= 8;
281 rrb
->base
.GreenBits
= 8;
282 rrb
->base
.BlueBits
= 8;
283 rrb
->base
.AlphaBits
= 8;
284 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
286 case GL_STENCIL_INDEX8_EXT
:
287 rrb
->base
._ActualFormat
= GL_STENCIL_INDEX8_EXT
;
288 rrb
->base
._BaseFormat
= GL_STENCIL_INDEX
;
289 rrb
->base
.StencilBits
= 8;
290 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
292 case GL_DEPTH_COMPONENT16
:
293 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
294 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
295 rrb
->base
.DepthBits
= 16;
296 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
298 case GL_DEPTH_COMPONENT24
:
299 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
300 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
301 rrb
->base
.DepthBits
= 24;
302 rrb
->base
.DataType
= GL_UNSIGNED_INT
;
304 case GL_DEPTH24_STENCIL8_EXT
:
305 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
306 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
307 rrb
->base
.DepthBits
= 24;
308 rrb
->base
.StencilBits
= 8;
309 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
312 fprintf(stderr
, "%s: Unknown format 0x%04x\n", __FUNCTION__
, format
);
313 _mesa_delete_renderbuffer(&rrb
->base
);
317 rrb
->dPriv
= driDrawPriv
;
318 rrb
->base
.InternalFormat
= format
;
320 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
321 rrb
->base
.AllocStorage
= radeon_alloc_window_storage
;
322 rrb
->base
.GetPointer
= radeon_get_pointer
;
328 static struct gl_renderbuffer
*
329 radeon_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
331 struct radeon_renderbuffer
*rrb
;
333 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
337 _mesa_init_renderbuffer(&rrb
->base
, name
);
338 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
340 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
341 rrb
->base
.AllocStorage
= radeon_alloc_renderbuffer_storage
;
342 rrb
->base
.GetPointer
= radeon_get_pointer
;
348 radeon_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
349 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
351 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
352 radeon_draw_buffer(ctx
, fb
);
355 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
360 radeon_framebuffer_renderbuffer(GLcontext
* ctx
,
361 struct gl_framebuffer
*fb
,
362 GLenum attachment
, struct gl_renderbuffer
*rb
)
367 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
368 radeon_draw_buffer(ctx
, fb
);
373 radeon_update_wrapper(GLcontext
*ctx
, struct radeon_renderbuffer
*rrb
,
374 struct gl_texture_image
*texImage
)
376 if (texImage
->TexFormat
== &_mesa_texformat_argb8888
) {
378 rrb
->base
._ActualFormat
= GL_RGBA8
;
379 rrb
->base
._BaseFormat
= GL_RGBA
;
380 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
381 DBG("Render to RGBA8 texture OK\n");
383 else if (texImage
->TexFormat
== &_mesa_texformat_argb4444
) {
385 rrb
->base
._ActualFormat
= GL_RGBA8
;
386 rrb
->base
._BaseFormat
= GL_RGBA
;
387 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
388 DBG("Render to RGBA4 texture OK\n");
390 else if (texImage
->TexFormat
== &_mesa_texformat_rgb565
) {
392 rrb
->base
._ActualFormat
= GL_RGB5
;
393 rrb
->base
._BaseFormat
= GL_RGB
;
394 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
395 DBG("Render to RGB5 texture OK\n");
397 else if (texImage
->TexFormat
== &_mesa_texformat_z16
) {
399 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
400 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
401 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
402 DBG("Render to DEPTH16 texture OK\n");
404 else if (texImage
->TexFormat
== &_mesa_texformat_s8_z24
) {
406 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
407 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
408 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
409 DBG("Render to DEPTH_STENCIL texture OK\n");
412 DBG("Render to texture BAD FORMAT %d\n",
413 texImage
->TexFormat
->MesaFormat
);
417 rrb
->pitch
= texImage
->Width
* rrb
->cpp
;
418 rrb
->base
.InternalFormat
= rrb
->base
._ActualFormat
;
419 rrb
->base
.Width
= texImage
->Width
;
420 rrb
->base
.Height
= texImage
->Height
;
421 rrb
->base
.RedBits
= texImage
->TexFormat
->RedBits
;
422 rrb
->base
.GreenBits
= texImage
->TexFormat
->GreenBits
;
423 rrb
->base
.BlueBits
= texImage
->TexFormat
->BlueBits
;
424 rrb
->base
.AlphaBits
= texImage
->TexFormat
->AlphaBits
;
425 rrb
->base
.DepthBits
= texImage
->TexFormat
->DepthBits
;
427 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
428 rrb
->base
.AllocStorage
= radeon_nop_alloc_storage
;
434 static struct radeon_renderbuffer
*
435 radeon_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
437 const GLuint name
= ~0; /* not significant, but distinct for debugging */
438 struct radeon_renderbuffer
*rrb
;
440 /* make an radeon_renderbuffer to wrap the texture image */
441 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
443 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
447 _mesa_init_renderbuffer(&rrb
->base
, name
);
448 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
450 if (!radeon_update_wrapper(ctx
, rrb
, texImage
)) {
459 radeon_render_texture(GLcontext
* ctx
,
460 struct gl_framebuffer
*fb
,
461 struct gl_renderbuffer_attachment
*att
)
463 struct gl_texture_image
*newImage
464 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
465 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(att
->Renderbuffer
);
466 radeon_texture_image
*radeon_image
;
473 if (newImage
->Border
!= 0) {
474 /* Fallback on drawing to a texture with a border, which won't have a
477 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
478 _mesa_render_texture(ctx
, fb
, att
);
482 rrb
= radeon_wrap_texture(ctx
, newImage
);
484 /* bind the wrapper to the attachment point */
485 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &rrb
->base
);
488 /* fallback to software rendering */
489 _mesa_render_texture(ctx
, fb
, att
);
494 if (!radeon_update_wrapper(ctx
, rrb
, newImage
)) {
495 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
496 _mesa_render_texture(ctx
, fb
, att
);
500 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
502 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
505 /* point the renderbufer's region to the texture image region */
506 radeon_image
= (radeon_texture_image
*)newImage
;
507 if (rrb
->bo
!= radeon_image
->mt
->bo
) {
509 radeon_bo_unref(rrb
->bo
);
510 rrb
->bo
= radeon_image
->mt
->bo
;
511 radeon_bo_ref(rrb
->bo
);
514 /* compute offset of the particular 2D image within the texture region */
515 imageOffset
= radeon_miptree_image_offset(radeon_image
->mt
,
519 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
520 const GLuint
*offsets
= radeon_miptree_depth_offsets(radeon_image
->mt
,
522 imageOffset
+= offsets
[att
->Zoffset
];
525 /* store that offset in the region */
526 rrb
->draw_offset
= imageOffset
;
528 /* update drawing region, etc */
529 radeon_draw_buffer(ctx
, fb
);
533 radeon_finish_render_texture(GLcontext
* ctx
,
534 struct gl_renderbuffer_attachment
*att
)
539 radeon_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
544 radeon_blit_framebuffer(GLcontext
*ctx
,
545 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
546 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
547 GLbitfield mask
, GLenum filter
)
551 void radeon_fbo_init(struct radeon_context
*radeon
)
553 radeon
->glCtx
->Driver
.NewFramebuffer
= radeon_new_framebuffer
;
554 radeon
->glCtx
->Driver
.NewRenderbuffer
= radeon_new_renderbuffer
;
555 radeon
->glCtx
->Driver
.BindFramebuffer
= radeon_bind_framebuffer
;
556 radeon
->glCtx
->Driver
.FramebufferRenderbuffer
= radeon_framebuffer_renderbuffer
;
557 radeon
->glCtx
->Driver
.RenderTexture
= radeon_render_texture
;
558 radeon
->glCtx
->Driver
.FinishRenderTexture
= radeon_finish_render_texture
;
559 radeon
->glCtx
->Driver
.ResizeBuffers
= radeon_resize_buffers
;
560 radeon
->glCtx
->Driver
.ValidateFramebuffer
= radeon_validate_framebuffer
;
561 radeon
->glCtx
->Driver
.BlitFramebuffer
= radeon_blit_framebuffer
;
565 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer
*rb
,
566 struct radeon_bo
*bo
)
568 struct radeon_bo
*old
;
573 radeon_bo_unref(old
);