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/texrender.h"
37 #include "drivers/common/meta.h"
39 #include "radeon_common.h"
40 #include "radeon_mipmap_tree.h"
42 #define FILE_DEBUG_FLAG RADEON_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
->Format
= MESA_FORMAT_RGB565
;
94 rb
->DataType
= GL_UNSIGNED_BYTE
;
102 rb
->Format
= MESA_FORMAT_ARGB8888
;
103 rb
->DataType
= GL_UNSIGNED_BYTE
;
114 rb
->Format
= MESA_FORMAT_ARGB8888
;
115 rb
->DataType
= GL_UNSIGNED_BYTE
;
118 case GL_STENCIL_INDEX
:
119 case GL_STENCIL_INDEX1_EXT
:
120 case GL_STENCIL_INDEX4_EXT
:
121 case GL_STENCIL_INDEX8_EXT
:
122 case GL_STENCIL_INDEX16_EXT
:
123 /* alloc a depth+stencil buffer */
124 rb
->Format
= MESA_FORMAT_S8_Z24
;
125 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
128 case GL_DEPTH_COMPONENT16
:
129 rb
->Format
= MESA_FORMAT_Z16
;
130 rb
->DataType
= GL_UNSIGNED_SHORT
;
133 case GL_DEPTH_COMPONENT
:
134 case GL_DEPTH_COMPONENT24
:
135 case GL_DEPTH_COMPONENT32
:
136 rb
->Format
= MESA_FORMAT_Z32
;
137 rb
->DataType
= GL_UNSIGNED_INT
;
140 case GL_DEPTH_STENCIL_EXT
:
141 case GL_DEPTH24_STENCIL8_EXT
:
142 rb
->Format
= MESA_FORMAT_S8_Z24
;
143 rb
->DataType
= GL_UNSIGNED_INT_24_8_EXT
;
148 "Unexpected format in intel_alloc_renderbuffer_storage");
152 rb
->_BaseFormat
= _mesa_base_fbo_format(ctx
, internalFormat
);
154 if (ctx
->Driver
.Flush
)
155 ctx
->Driver
.Flush(ctx
); /* +r6/r7 */
158 radeon_bo_unref(rrb
->bo
);
161 if (software_buffer
) {
162 return _mesa_soft_renderbuffer_storage(ctx
, rb
, internalFormat
,
167 uint32_t pitch
= ((cpp
* width
+ 63) & ~63) / cpp
;
169 fprintf(stderr
,"Allocating %d x %d radeon RBO (pitch %d)\n", width
,
172 size
= pitch
* height
* cpp
;
173 rrb
->pitch
= pitch
* cpp
;
175 rrb
->bo
= radeon_bo_open(radeon
->radeonScreen
->bom
,
179 RADEON_GEM_DOMAIN_VRAM
,
190 * Called for each hardware renderbuffer when a _window_ is resized.
191 * Just update fields.
192 * Not used for user-created renderbuffers!
195 radeon_alloc_window_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
196 GLenum internalFormat
, GLuint width
, GLuint height
)
198 ASSERT(rb
->Name
== 0);
201 rb
->InternalFormat
= internalFormat
;
208 radeon_resize_buffers(GLcontext
*ctx
, struct gl_framebuffer
*fb
,
209 GLuint width
, GLuint height
)
211 struct radeon_framebuffer
*radeon_fb
= (struct radeon_framebuffer
*)fb
;
214 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
216 fb
->Initialized
= GL_TRUE
; /* XXX remove someday */
222 /* Make sure all window system renderbuffers are up to date */
223 for (i
= 0; i
< 2; i
++) {
224 struct gl_renderbuffer
*rb
= &radeon_fb
->color_rb
[i
]->base
;
226 /* only resize if size is changing */
227 if (rb
&& (rb
->Width
!= width
|| rb
->Height
!= height
)) {
228 rb
->AllocStorage(ctx
, rb
, rb
->InternalFormat
, width
, height
);
234 /** Dummy function for gl_renderbuffer::AllocStorage() */
236 radeon_nop_alloc_storage(GLcontext
* ctx
, struct gl_renderbuffer
*rb
,
237 GLenum internalFormat
, GLuint width
, GLuint height
)
239 _mesa_problem(ctx
, "radeon_op_alloc_storage should never be called.");
245 * Create a renderbuffer for a window's color, depth and/or stencil buffer.
246 * Not used for user-created renderbuffers.
248 struct radeon_renderbuffer
*
249 radeon_create_renderbuffer(gl_format format
, __DRIdrawablePrivate
*driDrawPriv
)
251 struct radeon_renderbuffer
*rrb
;
253 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
257 _mesa_init_renderbuffer(&rrb
->base
, 0);
258 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
260 rrb
->base
.Format
= format
;
263 case MESA_FORMAT_RGB565
:
264 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
265 rrb
->base
._BaseFormat
= GL_RGB
;
267 case MESA_FORMAT_XRGB8888
:
268 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
269 rrb
->base
._BaseFormat
= GL_RGB
;
271 case MESA_FORMAT_ARGB8888
:
272 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
273 rrb
->base
._BaseFormat
= GL_RGBA
;
276 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
277 rrb
->base
._BaseFormat
= GL_STENCIL_INDEX
;
279 case MESA_FORMAT_Z16
:
280 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
281 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
283 case MESA_FORMAT_X8_Z24
:
284 rrb
->base
.DataType
= GL_UNSIGNED_INT
;
285 rrb
->base
._BaseFormat
= GL_DEPTH_COMPONENT
;
287 case MESA_FORMAT_S8_Z24
:
288 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
289 rrb
->base
._BaseFormat
= GL_DEPTH_STENCIL
;
292 fprintf(stderr
, "%s: Unknown format %s\n",
293 __FUNCTION__
, _mesa_get_format_name(format
));
294 _mesa_delete_renderbuffer(&rrb
->base
);
298 rrb
->dPriv
= driDrawPriv
;
299 rrb
->base
.InternalFormat
= _mesa_get_format_base_format(format
);
301 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
302 rrb
->base
.AllocStorage
= radeon_alloc_window_storage
;
303 rrb
->base
.GetPointer
= radeon_get_pointer
;
309 static struct gl_renderbuffer
*
310 radeon_new_renderbuffer(GLcontext
* ctx
, GLuint name
)
312 struct radeon_renderbuffer
*rrb
;
314 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
318 _mesa_init_renderbuffer(&rrb
->base
, name
);
319 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
321 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
322 rrb
->base
.AllocStorage
= radeon_alloc_renderbuffer_storage
;
323 rrb
->base
.GetPointer
= radeon_get_pointer
;
329 radeon_bind_framebuffer(GLcontext
* ctx
, GLenum target
,
330 struct gl_framebuffer
*fb
, struct gl_framebuffer
*fbread
)
332 if (target
== GL_FRAMEBUFFER_EXT
|| target
== GL_DRAW_FRAMEBUFFER_EXT
) {
333 radeon_draw_buffer(ctx
, fb
);
336 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
341 radeon_framebuffer_renderbuffer(GLcontext
* ctx
,
342 struct gl_framebuffer
*fb
,
343 GLenum attachment
, struct gl_renderbuffer
*rb
)
346 if (ctx
->Driver
.Flush
)
347 ctx
->Driver
.Flush(ctx
); /* +r6/r7 */
349 _mesa_framebuffer_renderbuffer(ctx
, fb
, attachment
, rb
);
350 radeon_draw_buffer(ctx
, fb
);
355 radeon_update_wrapper(GLcontext
*ctx
, struct radeon_renderbuffer
*rrb
,
356 struct gl_texture_image
*texImage
)
362 if (texImage
->TexFormat
== MESA_FORMAT_ARGB8888
) {
363 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
364 DBG("Render to RGBA8 texture OK\n");
366 else if (texImage
->TexFormat
== MESA_FORMAT_RGB565
) {
367 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
368 DBG("Render to RGB5 texture OK\n");
370 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB1555
) {
371 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
372 DBG("Render to ARGB1555 texture OK\n");
374 else if (texImage
->TexFormat
== MESA_FORMAT_ARGB4444
) {
375 rrb
->base
.DataType
= GL_UNSIGNED_BYTE
;
376 DBG("Render to ARGB1555 texture OK\n");
378 else if (texImage
->TexFormat
== MESA_FORMAT_Z16
) {
379 rrb
->base
.DataType
= GL_UNSIGNED_SHORT
;
380 DBG("Render to DEPTH16 texture OK\n");
382 else if (texImage
->TexFormat
== MESA_FORMAT_S8_Z24
) {
383 rrb
->base
.DataType
= GL_UNSIGNED_INT_24_8_EXT
;
384 DBG("Render to DEPTH_STENCIL texture OK\n");
387 /* try redoing the FBO */
389 DBG("Render to texture BAD FORMAT %d\n",
390 texImage
->TexFormat
);
393 /* XXX why is the tex format being set here?
394 * I think this can be removed.
396 texImage
->TexFormat
= radeonChooseTextureFormat(ctx
, texImage
->InternalFormat
, 0,
397 _mesa_get_format_datatype(texImage
->TexFormat
),
404 texFormat
= texImage
->TexFormat
;
406 rrb
->base
.Format
= texFormat
;
408 rrb
->cpp
= _mesa_get_format_bytes(texFormat
);
409 rrb
->pitch
= texImage
->Width
* rrb
->cpp
;
410 rrb
->base
.InternalFormat
= texImage
->InternalFormat
;
411 rrb
->base
._BaseFormat
= _mesa_base_fbo_format(ctx
, rrb
->base
.InternalFormat
);
413 rrb
->base
.Width
= texImage
->Width
;
414 rrb
->base
.Height
= texImage
->Height
;
416 rrb
->base
.Delete
= radeon_delete_renderbuffer
;
417 rrb
->base
.AllocStorage
= radeon_nop_alloc_storage
;
423 static struct radeon_renderbuffer
*
424 radeon_wrap_texture(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
426 const GLuint name
= ~0; /* not significant, but distinct for debugging */
427 struct radeon_renderbuffer
*rrb
;
429 /* make an radeon_renderbuffer to wrap the texture image */
430 rrb
= CALLOC_STRUCT(radeon_renderbuffer
);
432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glFramebufferTexture");
436 _mesa_init_renderbuffer(&rrb
->base
, name
);
437 rrb
->base
.ClassID
= RADEON_RB_CLASS
;
439 if (!radeon_update_wrapper(ctx
, rrb
, texImage
)) {
448 radeon_render_texture(GLcontext
* ctx
,
449 struct gl_framebuffer
*fb
,
450 struct gl_renderbuffer_attachment
*att
)
452 struct gl_texture_image
*newImage
453 = att
->Texture
->Image
[att
->CubeMapFace
][att
->TextureLevel
];
454 struct radeon_renderbuffer
*rrb
= radeon_renderbuffer(att
->Renderbuffer
);
455 radeon_texture_image
*radeon_image
;
462 if (newImage
->Border
!= 0) {
463 /* Fallback on drawing to a texture with a border, which won't have a
466 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
467 _mesa_render_texture(ctx
, fb
, att
);
471 rrb
= radeon_wrap_texture(ctx
, newImage
);
473 /* bind the wrapper to the attachment point */
474 _mesa_reference_renderbuffer(&att
->Renderbuffer
, &rrb
->base
);
477 /* fallback to software rendering */
478 _mesa_render_texture(ctx
, fb
, att
);
483 if (!radeon_update_wrapper(ctx
, rrb
, newImage
)) {
484 _mesa_reference_renderbuffer(&att
->Renderbuffer
, NULL
);
485 _mesa_render_texture(ctx
, fb
, att
);
489 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
491 att
->Texture
->Name
, newImage
->Width
, newImage
->Height
,
494 /* point the renderbufer's region to the texture image region */
495 radeon_image
= (radeon_texture_image
*)newImage
;
496 if (rrb
->bo
!= radeon_image
->mt
->bo
) {
498 radeon_bo_unref(rrb
->bo
);
499 rrb
->bo
= radeon_image
->mt
->bo
;
500 radeon_bo_ref(rrb
->bo
);
503 /* compute offset of the particular 2D image within the texture region */
504 imageOffset
= radeon_miptree_image_offset(radeon_image
->mt
,
508 if (att
->Texture
->Target
== GL_TEXTURE_3D
) {
510 radeon_miptree_depth_offsets(radeon_image
->mt
, att
->TextureLevel
,
512 imageOffset
+= offsets
[att
->Zoffset
];
515 /* store that offset in the region, along with the correct pitch for
516 * the image we are rendering to */
517 rrb
->draw_offset
= imageOffset
;
518 rrb
->pitch
= radeon_image
->mt
->levels
[att
->TextureLevel
].rowstride
;
520 /* update drawing region, etc */
521 radeon_draw_buffer(ctx
, fb
);
525 radeon_finish_render_texture(GLcontext
* ctx
,
526 struct gl_renderbuffer_attachment
*att
)
531 radeon_validate_framebuffer(GLcontext
*ctx
, struct gl_framebuffer
*fb
)
535 void radeon_fbo_init(struct radeon_context
*radeon
)
537 radeon
->glCtx
->Driver
.NewFramebuffer
= radeon_new_framebuffer
;
538 radeon
->glCtx
->Driver
.NewRenderbuffer
= radeon_new_renderbuffer
;
539 radeon
->glCtx
->Driver
.BindFramebuffer
= radeon_bind_framebuffer
;
540 radeon
->glCtx
->Driver
.FramebufferRenderbuffer
= radeon_framebuffer_renderbuffer
;
541 radeon
->glCtx
->Driver
.RenderTexture
= radeon_render_texture
;
542 radeon
->glCtx
->Driver
.FinishRenderTexture
= radeon_finish_render_texture
;
543 radeon
->glCtx
->Driver
.ResizeBuffers
= radeon_resize_buffers
;
544 radeon
->glCtx
->Driver
.ValidateFramebuffer
= radeon_validate_framebuffer
;
545 radeon
->glCtx
->Driver
.BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
549 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer
*rb
,
550 struct radeon_bo
*bo
)
552 struct radeon_bo
*old
;
557 radeon_bo_unref(old
);