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"
38 #include "drivers/common/meta.h"
40 #include "radeon_common.h"
41 #include "radeon_mipmap_tree.h"
43 #define FILE_DEBUG_FLAG RADEON_TEXTURE
44 #define DBG(...) do { \
45 if (RADEON_DEBUG & FILE_DEBUG_FLAG) \
46 _mesa_printf(__VA_ARGS__); \
49 static struct gl_framebuffer
*
50 radeon_new_framebuffer(GLcontext
*ctx
, GLuint name
)
52 return _mesa_new_framebuffer(ctx
, name
);
56 radeon_delete_renderbuffer(struct gl_renderbuffer
*rb
)
58 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(rb
);
63 radeon_bo_unref(rrb
->bo
);
69 radeon_get_pointer(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
76 * Called via glRenderbufferStorageEXT() to set the format and allocate
77 * storage for a user-created renderbuffer.
80 radeon_alloc_renderbuffer_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
81 GLenum internalFormat
,
82 GLuint width
, GLuint height
)
84 struct radeon_context
*radeon
= RADEON_CONTEXT(ctx
);
85 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(rb
);
86 GLboolean software_buffer
= GL_FALSE
;
89 ASSERT(rb
->Name
!= 0);
90 switch (internalFormat
) {
94 rb
->_ActualFormat
= GL_RGB5
;
95 rb
->DataType
= GL_UNSIGNED_BYTE
;
106 rb
->_ActualFormat
= GL_RGB8
;
107 rb
->DataType
= GL_UNSIGNED_BYTE
;
122 rb
->_ActualFormat
= GL_RGBA8
;
123 rb
->DataType
= GL_UNSIGNED_BYTE
;
130 case GL_STENCIL_INDEX
:
131 case GL_STENCIL_INDEX1_EXT
:
132 case GL_STENCIL_INDEX4_EXT
:
133 case GL_STENCIL_INDEX8_EXT
:
134 case GL_STENCIL_INDEX16_EXT
:
135 /* alloc a depth+stencil buffer */
136 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
137 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
141 case GL_DEPTH_COMPONENT16
:
142 rb
->_ActualFormat
= GL_DEPTH_COMPONENT16
;
143 rb
->DataType
= GL_UNSIGNED_SHORT
;
147 case GL_DEPTH_COMPONENT
:
148 case GL_DEPTH_COMPONENT24
:
149 case GL_DEPTH_COMPONENT32
:
150 rb
->_ActualFormat
= GL_DEPTH_COMPONENT24
;
151 rb
->DataType
= GL_UNSIGNED_INT
;
155 case GL_DEPTH_STENCIL_EXT
:
156 case GL_DEPTH24_STENCIL8_EXT
:
157 rb
->_ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
158 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
165 "Unexpected format in intel_alloc_renderbuffer_storage");
169 if (ctx
->Driver
.Flush
)
170 ctx
->Driver
.Flush(ctx
); /* +r6/r7 */
173 radeon_bo_unref(rrb
->bo
);
176 if (software_buffer
) {
177 return _mesa_soft_renderbuffer_storage(ctx
, rb
, internalFormat
,
182 uint32_t pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
184 fprintf(stderr
,"Allocating %d x %d radeon RBO (pitch %d)\n", width
,
187 size
= pitch
* height
* cpp
;
188 rrb
->pitch
= pitch
* cpp
;
190 rrb
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
194 RADEON_GEM_DOMAIN_VRAM
,
205 * Called for each hardware renderbuffer when a _window_ is resized.
206 * Just update fields.
207 * Not used for user-created renderbuffers!
210 radeon_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
211 GLenum internalFormat
, GLuint width
, GLuint height
)
213 ASSERT(rb
->Name
== 0);
216 rb
->_ActualFormat
= internalFormat
;
223 radeon_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
224 GLuint width
, GLuint height
)
226 struct radeon_framebuffer
*radeon_fb
= (struct radeon_framebuffer
*)fb
;
229 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
231 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
237 /* Make sure all window system renderbuffers are up to date */
238 for (i
= 0; i
< 2; i
++) {
239 struct gl_renderbuffer
*rb
= &radeon_fb
->color_rb
[i
]->base
;
241 /* only resize if size is changing */
242 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
243 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
249 /** Dummy function for gl_renderbuffer::AllocStorage() */
251 radeon_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
252 GLenum internalFormat
, GLuint width
, GLuint height
)
254 _mesa_problem(ctx
, "radeon_op_alloc_storage should never be called.");
258 struct radeon_renderbuffer
*
259 radeon_create_renderbuffer(GLenum format
, __DRIdrawablePrivate
*driDrawPriv
)
261 struct radeon_renderbuffer
*rrb
;
263 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
267 _mesa_init_renderbuffer(&rrb
->base
, 0);
268 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
270 /* XXX format junk */
273 rrb
->base
._ActualFormat
= GL_RGB5
;
274 rrb
->base
._BaseFormat
= GL_RGBA
;
275 rrb
->base
.RedBits
= 5;
276 rrb
->base
.GreenBits
= 6;
277 rrb
->base
.BlueBits
= 5;
278 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
281 rrb
->base
._ActualFormat
= GL_RGB8
;
282 rrb
->base
._BaseFormat
= GL_RGB
;
283 rrb
->base
.RedBits
= 8;
284 rrb
->base
.GreenBits
= 8;
285 rrb
->base
.BlueBits
= 8;
286 rrb
->base
.AlphaBits
= 0;
287 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
290 rrb
->base
._ActualFormat
= GL_RGBA8
;
291 rrb
->base
._BaseFormat
= GL_RGBA
;
292 rrb
->base
.RedBits
= 8;
293 rrb
->base
.GreenBits
= 8;
294 rrb
->base
.BlueBits
= 8;
295 rrb
->base
.AlphaBits
= 8;
296 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
298 case GL_STENCIL_INDEX8_EXT
:
299 rrb
->base
._ActualFormat
= GL_STENCIL_INDEX8_EXT
;
300 rrb
->base
._BaseFormat
= GL_STENCIL_INDEX
;
301 rrb
->base
.StencilBits
= 8;
302 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
304 case GL_DEPTH_COMPONENT16
:
305 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
306 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
307 rrb
->base
.DepthBits
= 16;
308 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
310 case GL_DEPTH_COMPONENT24
:
311 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT24
;
312 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
313 rrb
->base
.DepthBits
= 24;
314 rrb
->base
.DataType
= GL_UNSIGNED_INT
;
316 case GL_DEPTH24_STENCIL8_EXT
:
317 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
318 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
319 rrb
->base
.DepthBits
= 24;
320 rrb
->base
.StencilBits
= 8;
321 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
324 fprintf(stderr
, "%s: Unknown format 0x%04x\n", __FUNCTION__
, format
);
325 _mesa_delete_renderbuffer(&rrb
->base
);
329 rrb
->dPriv
= driDrawPriv
;
330 rrb
->base
.InternalFormat
= format
;
332 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
333 rrb
->base
.AllocStorage
= radeon_alloc_window_storage
;
334 rrb
->base
.GetPointer
= radeon_get_pointer
;
340 static struct gl_renderbuffer
*
341 radeon_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
343 struct radeon_renderbuffer
*rrb
;
345 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
349 _mesa_init_renderbuffer(&rrb
->base
, name
);
350 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
352 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
353 rrb
->base
.AllocStorage
= radeon_alloc_renderbuffer_storage
;
354 rrb
->base
.GetPointer
= radeon_get_pointer
;
360 radeon_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
361 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
363 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
364 radeon_draw_buffer(ctx
, fb
);
367 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
372 radeon_framebuffer_renderbuffer(GLcontext
* ctx
,
373 struct gl_framebuffer
*fb
,
374 GLenum attachment
, struct gl_renderbuffer
*rb
)
377 if (ctx
->Driver
.Flush
)
378 ctx
->Driver
.Flush(ctx
); /* +r6/r7 */
380 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
381 radeon_draw_buffer(ctx
, fb
);
386 radeon_update_wrapper(GLcontext
*ctx
, struct radeon_renderbuffer
*rrb
,
387 struct gl_texture_image
*texImage
)
391 if (texImage
->TexFormat
== &_mesa_texformat_argb8888
) {
393 rrb
->base
._ActualFormat
= GL_RGBA8
;
394 rrb
->base
._BaseFormat
= GL_RGBA
;
395 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
396 DBG("Render to RGBA8 texture OK\n");
398 else if (texImage
->TexFormat
== &_mesa_texformat_rgb565
) {
400 rrb
->base
._ActualFormat
= GL_RGB5
;
401 rrb
->base
._BaseFormat
= GL_RGB
;
402 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
403 DBG("Render to RGB5 texture OK\n");
405 else if (texImage
->TexFormat
== &_mesa_texformat_argb1555
) {
407 rrb
->base
._ActualFormat
= GL_RGB5_A1
;
408 rrb
->base
._BaseFormat
= GL_RGBA
;
409 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
410 DBG("Render to ARGB1555 texture OK\n");
412 else if (texImage
->TexFormat
== &_mesa_texformat_argb4444
) {
414 rrb
->base
._ActualFormat
= GL_RGBA4
;
415 rrb
->base
._BaseFormat
= GL_RGBA
;
416 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
417 DBG("Render to ARGB1555 texture OK\n");
419 else if (texImage
->TexFormat
== &_mesa_texformat_z16
) {
421 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
422 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
423 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
424 DBG("Render to DEPTH16 texture OK\n");
426 else if (texImage
->TexFormat
== &_mesa_texformat_s8_z24
) {
428 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
429 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
430 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
431 DBG("Render to DEPTH_STENCIL texture OK\n");
434 /* try redoing the FBO */
436 DBG("Render to texture BAD FORMAT %d\n",
437 texImage
->TexFormat
->MesaFormat
);
440 texImage
->TexFormat
= radeonChooseTextureFormat(ctx
, texImage
->InternalFormat
, 0,
441 texImage
->TexFormat
->DataType
,
448 rrb
->pitch
= texImage
->Width
* rrb
->cpp
;
449 rrb
->base
.InternalFormat
= rrb
->base
._ActualFormat
;
450 rrb
->base
.Width
= texImage
->Width
;
451 rrb
->base
.Height
= texImage
->Height
;
452 rrb
->base
.RedBits
= texImage
->TexFormat
->RedBits
;
453 rrb
->base
.GreenBits
= texImage
->TexFormat
->GreenBits
;
454 rrb
->base
.BlueBits
= texImage
->TexFormat
->BlueBits
;
455 rrb
->base
.AlphaBits
= texImage
->TexFormat
->AlphaBits
;
456 rrb
->base
.DepthBits
= texImage
->TexFormat
->DepthBits
;
457 rrb
->base
.StencilBits
= texImage
->TexFormat
->StencilBits
;
459 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
460 rrb
->base
.AllocStorage
= radeon_nop_alloc_storage
;
466 static struct radeon_renderbuffer
*
467 radeon_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
469 const GLuint name
= ~0; /* not significant, but distinct for debugging */
470 struct radeon_renderbuffer
*rrb
;
472 /* make an radeon_renderbuffer to wrap the texture image */
473 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
475 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
479 _mesa_init_renderbuffer(&rrb
->base
, name
);
480 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
482 if (!radeon_update_wrapper(ctx
, rrb
, texImage
)) {
491 radeon_render_texture(GLcontext
* ctx
,
492 struct gl_framebuffer
*fb
,
493 struct gl_renderbuffer_attachment
*att
)
495 struct gl_texture_image
*newImage
496 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
497 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(att
->Renderbuffer
);
498 radeon_texture_image
*radeon_image
;
505 if (newImage
->Border
!= 0) {
506 /* Fallback on drawing to a texture with a border, which won't have a
509 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
510 _mesa_render_texture(ctx
, fb
, att
);
514 rrb
= radeon_wrap_texture(ctx
, newImage
);
516 /* bind the wrapper to the attachment point */
517 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &rrb
->base
);
520 /* fallback to software rendering */
521 _mesa_render_texture(ctx
, fb
, att
);
526 if (!radeon_update_wrapper(ctx
, rrb
, newImage
)) {
527 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
528 _mesa_render_texture(ctx
, fb
, att
);
532 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
534 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
537 /* point the renderbufer's region to the texture image region */
538 radeon_image
= (radeon_texture_image
*)newImage
;
539 if (rrb
->bo
!= radeon_image
->mt
->bo
) {
541 radeon_bo_unref(rrb
->bo
);
542 rrb
->bo
= radeon_image
->mt
->bo
;
543 radeon_bo_ref(rrb
->bo
);
546 /* compute offset of the particular 2D image within the texture region */
547 imageOffset
= radeon_miptree_image_offset(radeon_image
->mt
,
551 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
553 radeon_miptree_depth_offsets(radeon_image
->mt
, att
->TextureLevel
,
555 imageOffset
+= offsets
[att
->Zoffset
];
558 /* store that offset in the region */
559 rrb
->draw_offset
= imageOffset
;
561 /* update drawing region, etc */
562 radeon_draw_buffer(ctx
, fb
);
566 radeon_finish_render_texture(GLcontext
* ctx
,
567 struct gl_renderbuffer_attachment
*att
)
572 radeon_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
576 void radeon_fbo_init(struct radeon_context
*radeon
)
578 radeon
->glCtx
->Driver
.NewFramebuffer
= radeon_new_framebuffer
;
579 radeon
->glCtx
->Driver
.NewRenderbuffer
= radeon_new_renderbuffer
;
580 radeon
->glCtx
->Driver
.BindFramebuffer
= radeon_bind_framebuffer
;
581 radeon
->glCtx
->Driver
.FramebufferRenderbuffer
= radeon_framebuffer_renderbuffer
;
582 radeon
->glCtx
->Driver
.RenderTexture
= radeon_render_texture
;
583 radeon
->glCtx
->Driver
.FinishRenderTexture
= radeon_finish_render_texture
;
584 radeon
->glCtx
->Driver
.ResizeBuffers
= radeon_resize_buffers
;
585 radeon
->glCtx
->Driver
.ValidateFramebuffer
= radeon_validate_framebuffer
;
586 radeon
->glCtx
->Driver
.BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
590 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer
*rb
,
591 struct radeon_bo
*bo
)
593 struct radeon_bo
*old
;
598 radeon_bo_unref(old
);