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
)
393 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) {
395 rrb
->base
._ActualFormat
= GL_RGBA8
;
396 rrb
->base
._BaseFormat
= GL_RGBA
;
397 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
398 DBG("Render to RGBA8 texture OK\n");
400 else if (texImage
->TexFormat
== MESA_FORMAT_RGB565
) {
402 rrb
->base
._ActualFormat
= GL_RGB5
;
403 rrb
->base
._BaseFormat
= GL_RGB
;
404 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
405 DBG("Render to RGB5 texture OK\n");
407 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB1555
) {
409 rrb
->base
._ActualFormat
= GL_RGB5_A1
;
410 rrb
->base
._BaseFormat
= GL_RGBA
;
411 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
412 DBG("Render to ARGB1555 texture OK\n");
414 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB4444
) {
416 rrb
->base
._ActualFormat
= GL_RGBA4
;
417 rrb
->base
._BaseFormat
= GL_RGBA
;
418 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
419 DBG("Render to ARGB1555 texture OK\n");
421 else if (texImage
->TexFormat
== MESA_FORMAT_Z16
) {
423 rrb
->base
._ActualFormat
= GL_DEPTH_COMPONENT16
;
424 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
425 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
426 DBG("Render to DEPTH16 texture OK\n");
428 else if (texImage
->TexFormat
== MESA_FORMAT_S8_Z24
) {
430 rrb
->base
._ActualFormat
= GL_DEPTH24_STENCIL8_EXT
;
431 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL_EXT
;
432 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
433 DBG("Render to DEPTH_STENCIL texture OK\n");
436 /* try redoing the FBO */
438 DBG("Render to texture BAD FORMAT %d\n",
439 texImage
->TexFormat
);
442 texImage
->TexFormat
= radeonChooseTextureFormat(ctx
, texImage
->InternalFormat
, 0,
443 _mesa_get_format_datatype(texImage
->TexFormat
),
450 texFormat
= texImage
->TexFormat
;
452 rrb
->pitch
= texImage
->Width
* rrb
->cpp
;
453 rrb
->base
.InternalFormat
= rrb
->base
._ActualFormat
;
454 rrb
->base
.Width
= texImage
->Width
;
455 rrb
->base
.Height
= texImage
->Height
;
456 rrb
->base
.RedBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_RED_SIZE
);
457 rrb
->base
.GreenBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_GREEN_SIZE
);
458 rrb
->base
.BlueBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_BLUE_SIZE
);
459 rrb
->base
.AlphaBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_ALPHA_SIZE
);
460 rrb
->base
.DepthBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_DEPTH_SIZE_ARB
);
461 rrb
->base
.StencilBits
= _mesa_get_format_bits(texFormat
, GL_TEXTURE_STENCIL_SIZE_EXT
);
463 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
464 rrb
->base
.AllocStorage
= radeon_nop_alloc_storage
;
470 static struct radeon_renderbuffer
*
471 radeon_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
473 const GLuint name
= ~0; /* not significant, but distinct for debugging */
474 struct radeon_renderbuffer
*rrb
;
476 /* make an radeon_renderbuffer to wrap the texture image */
477 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
479 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
483 _mesa_init_renderbuffer(&rrb
->base
, name
);
484 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
486 if (!radeon_update_wrapper(ctx
, rrb
, texImage
)) {
495 radeon_render_texture(GLcontext
* ctx
,
496 struct gl_framebuffer
*fb
,
497 struct gl_renderbuffer_attachment
*att
)
499 struct gl_texture_image
*newImage
500 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
501 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(att
->Renderbuffer
);
502 radeon_texture_image
*radeon_image
;
509 if (newImage
->Border
!= 0) {
510 /* Fallback on drawing to a texture with a border, which won't have a
513 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
514 _mesa_render_texture(ctx
, fb
, att
);
518 rrb
= radeon_wrap_texture(ctx
, newImage
);
520 /* bind the wrapper to the attachment point */
521 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &rrb
->base
);
524 /* fallback to software rendering */
525 _mesa_render_texture(ctx
, fb
, att
);
530 if (!radeon_update_wrapper(ctx
, rrb
, newImage
)) {
531 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
532 _mesa_render_texture(ctx
, fb
, att
);
536 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
538 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
541 /* point the renderbufer's region to the texture image region */
542 radeon_image
= (radeon_texture_image
*)newImage
;
543 if (rrb
->bo
!= radeon_image
->mt
->bo
) {
545 radeon_bo_unref(rrb
->bo
);
546 rrb
->bo
= radeon_image
->mt
->bo
;
547 radeon_bo_ref(rrb
->bo
);
550 /* compute offset of the particular 2D image within the texture region */
551 imageOffset
= radeon_miptree_image_offset(radeon_image
->mt
,
555 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
557 radeon_miptree_depth_offsets(radeon_image
->mt
, att
->TextureLevel
,
559 imageOffset
+= offsets
[att
->Zoffset
];
562 /* store that offset in the region */
563 rrb
->draw_offset
= imageOffset
;
565 /* update drawing region, etc */
566 radeon_draw_buffer(ctx
, fb
);
570 radeon_finish_render_texture(GLcontext
* ctx
,
571 struct gl_renderbuffer_attachment
*att
)
576 radeon_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
580 void radeon_fbo_init(struct radeon_context
*radeon
)
582 radeon
->glCtx
->Driver
.NewFramebuffer
= radeon_new_framebuffer
;
583 radeon
->glCtx
->Driver
.NewRenderbuffer
= radeon_new_renderbuffer
;
584 radeon
->glCtx
->Driver
.BindFramebuffer
= radeon_bind_framebuffer
;
585 radeon
->glCtx
->Driver
.FramebufferRenderbuffer
= radeon_framebuffer_renderbuffer
;
586 radeon
->glCtx
->Driver
.RenderTexture
= radeon_render_texture
;
587 radeon
->glCtx
->Driver
.FinishRenderTexture
= radeon_finish_render_texture
;
588 radeon
->glCtx
->Driver
.ResizeBuffers
= radeon_resize_buffers
;
589 radeon
->glCtx
->Driver
.ValidateFramebuffer
= radeon_validate_framebuffer
;
590 radeon
->glCtx
->Driver
.BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
594 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer
*rb
,
595 struct radeon_bo
*bo
)
597 struct radeon_bo
*old
;
602 radeon_bo_unref(old
);