Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Red Hat Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
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"
38
39 #include "radeon_common.h"
40 #include "radeon_mipmap_tree.h"
41
42 #define FILE_DEBUG_FLAG RADEON_TEXTURE
43 #define DBG(...) do { \
44 if (RADEON_DEBUG & FILE_DEBUG_FLAG) \
45 _mesa_printf(__VA_ARGS__); \
46 } while(0)
47
48 static struct gl_framebuffer *
49 radeon_new_framebuffer(GLcontext *ctx, GLuint name)
50 {
51 return _mesa_new_framebuffer(ctx, name);
52 }
53
54 static void
55 radeon_delete_renderbuffer(struct gl_renderbuffer *rb)
56 {
57 struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
58
59 ASSERT(rrb);
60
61 if (rrb && rrb->bo) {
62 radeon_bo_unref(rrb->bo);
63 }
64 _mesa_free(rrb);
65 }
66
67 static void *
68 radeon_get_pointer(GLcontext *ctx, struct gl_renderbuffer *rb,
69 GLint x, GLint y)
70 {
71 return NULL;
72 }
73
74 /**
75 * Called via glRenderbufferStorageEXT() to set the format and allocate
76 * storage for a user-created renderbuffer.
77 */
78 static GLboolean
79 radeon_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
80 GLenum internalFormat,
81 GLuint width, GLuint height)
82 {
83 struct radeon_context *radeon = RADEON_CONTEXT(ctx);
84 struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
85 GLboolean software_buffer = GL_FALSE;
86 int cpp;
87
88 ASSERT(rb->Name != 0);
89 switch (internalFormat) {
90 case GL_R3_G3_B2:
91 case GL_RGB4:
92 case GL_RGB5:
93 rb->Format = _dri_texformat_rgb565;
94 rb->DataType = GL_UNSIGNED_BYTE;
95 cpp = 2;
96 break;
97 case GL_RGB:
98 case GL_RGB8:
99 case GL_RGB10:
100 case GL_RGB12:
101 case GL_RGB16:
102 rb->Format = _dri_texformat_argb8888;
103 rb->DataType = GL_UNSIGNED_BYTE;
104 cpp = 4;
105 break;
106 case GL_RGBA:
107 case GL_RGBA2:
108 case GL_RGBA4:
109 case GL_RGB5_A1:
110 case GL_RGBA8:
111 case GL_RGB10_A2:
112 case GL_RGBA12:
113 case GL_RGBA16:
114 rb->Format = _dri_texformat_argb8888;
115 rb->DataType = GL_UNSIGNED_BYTE;
116 cpp = 4;
117 break;
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;
126 cpp = 4;
127 break;
128 case GL_DEPTH_COMPONENT16:
129 rb->Format = MESA_FORMAT_Z16;
130 rb->DataType = GL_UNSIGNED_SHORT;
131 cpp = 2;
132 break;
133 case GL_DEPTH_COMPONENT:
134 case GL_DEPTH_COMPONENT24:
135 case GL_DEPTH_COMPONENT32:
136 rb->Format = MESA_FORMAT_X8_Z24;
137 rb->DataType = GL_UNSIGNED_INT;
138 cpp = 4;
139 break;
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;
144 cpp = 4;
145 break;
146 default:
147 _mesa_problem(ctx,
148 "Unexpected format in radeon_alloc_renderbuffer_storage");
149 return GL_FALSE;
150 }
151
152 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
153
154 if (ctx->Driver.Flush)
155 ctx->Driver.Flush(ctx); /* +r6/r7 */
156
157 if (rrb->bo)
158 radeon_bo_unref(rrb->bo);
159
160
161 if (software_buffer) {
162 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
163 width, height);
164 }
165 else {
166 uint32_t size;
167 uint32_t pitch = ((cpp * width + 63) & ~63) / cpp;
168
169 fprintf(stderr,"Allocating %d x %d radeon RBO (pitch %d)\n", width,
170 height, pitch);
171
172 size = pitch * height * cpp;
173 rrb->pitch = pitch * cpp;
174 rrb->cpp = cpp;
175 rrb->bo = radeon_bo_open(radeon->radeonScreen->bom,
176 0,
177 size,
178 0,
179 RADEON_GEM_DOMAIN_VRAM,
180 0);
181 rb->Width = width;
182 rb->Height = height;
183 return GL_TRUE;
184 }
185
186 }
187
188
189 /**
190 * Called for each hardware renderbuffer when a _window_ is resized.
191 * Just update fields.
192 * Not used for user-created renderbuffers!
193 */
194 static GLboolean
195 radeon_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
196 GLenum internalFormat, GLuint width, GLuint height)
197 {
198 ASSERT(rb->Name == 0);
199 rb->Width = width;
200 rb->Height = height;
201 rb->InternalFormat = internalFormat;
202
203 return GL_TRUE;
204 }
205
206
207 static void
208 radeon_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
209 GLuint width, GLuint height)
210 {
211 struct radeon_framebuffer *radeon_fb = (struct radeon_framebuffer*)fb;
212 int i;
213
214 _mesa_resize_framebuffer(ctx, fb, width, height);
215
216 fb->Initialized = GL_TRUE; /* XXX remove someday */
217
218 if (fb->Name != 0) {
219 return;
220 }
221
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;
225
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);
229 }
230 }
231 }
232
233
234 /** Dummy function for gl_renderbuffer::AllocStorage() */
235 static GLboolean
236 radeon_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
237 GLenum internalFormat, GLuint width, GLuint height)
238 {
239 _mesa_problem(ctx, "radeon_op_alloc_storage should never be called.");
240 return GL_FALSE;
241 }
242
243
244 /**
245 * Create a renderbuffer for a window's color, depth and/or stencil buffer.
246 * Not used for user-created renderbuffers.
247 */
248 struct radeon_renderbuffer *
249 radeon_create_renderbuffer(gl_format format, __DRIdrawablePrivate *driDrawPriv)
250 {
251 struct radeon_renderbuffer *rrb;
252
253 rrb = CALLOC_STRUCT(radeon_renderbuffer);
254 if (!rrb)
255 return NULL;
256
257 _mesa_init_renderbuffer(&rrb->base, 0);
258 rrb->base.ClassID = RADEON_RB_CLASS;
259
260 rrb->base.Format = format;
261
262 switch (format) {
263 case MESA_FORMAT_RGB565:
264 assert(_mesa_little_endian());
265 rrb->base.DataType = GL_UNSIGNED_BYTE;
266 rrb->base._BaseFormat = GL_RGB;
267 break;
268 case MESA_FORMAT_RGB565_REV:
269 assert(!_mesa_little_endian());
270 rrb->base.DataType = GL_UNSIGNED_BYTE;
271 rrb->base._BaseFormat = GL_RGB;
272 break;
273 case MESA_FORMAT_XRGB8888:
274 assert(_mesa_little_endian());
275 rrb->base.DataType = GL_UNSIGNED_BYTE;
276 rrb->base._BaseFormat = GL_RGB;
277 break;
278 case MESA_FORMAT_XRGB8888_REV:
279 assert(!_mesa_little_endian());
280 rrb->base.DataType = GL_UNSIGNED_BYTE;
281 rrb->base._BaseFormat = GL_RGB;
282 break;
283 case MESA_FORMAT_ARGB8888:
284 assert(_mesa_little_endian());
285 rrb->base.DataType = GL_UNSIGNED_BYTE;
286 rrb->base._BaseFormat = GL_RGBA;
287 break;
288 case MESA_FORMAT_ARGB8888_REV:
289 assert(!_mesa_little_endian());
290 rrb->base.DataType = GL_UNSIGNED_BYTE;
291 rrb->base._BaseFormat = GL_RGBA;
292 break;
293 case MESA_FORMAT_S8:
294 rrb->base.DataType = GL_UNSIGNED_BYTE;
295 rrb->base._BaseFormat = GL_STENCIL_INDEX;
296 break;
297 case MESA_FORMAT_Z16:
298 rrb->base.DataType = GL_UNSIGNED_SHORT;
299 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
300 break;
301 case MESA_FORMAT_X8_Z24:
302 rrb->base.DataType = GL_UNSIGNED_INT;
303 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
304 break;
305 case MESA_FORMAT_S8_Z24:
306 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
307 rrb->base._BaseFormat = GL_DEPTH_STENCIL;
308 break;
309 default:
310 fprintf(stderr, "%s: Unknown format %s\n",
311 __FUNCTION__, _mesa_get_format_name(format));
312 _mesa_delete_renderbuffer(&rrb->base);
313 return NULL;
314 }
315
316 rrb->dPriv = driDrawPriv;
317 rrb->base.InternalFormat = _mesa_get_format_base_format(format);
318
319 rrb->base.Delete = radeon_delete_renderbuffer;
320 rrb->base.AllocStorage = radeon_alloc_window_storage;
321 rrb->base.GetPointer = radeon_get_pointer;
322
323 rrb->bo = NULL;
324 return rrb;
325 }
326
327 static struct gl_renderbuffer *
328 radeon_new_renderbuffer(GLcontext * ctx, GLuint name)
329 {
330 struct radeon_renderbuffer *rrb;
331
332 rrb = CALLOC_STRUCT(radeon_renderbuffer);
333 if (!rrb)
334 return NULL;
335
336 _mesa_init_renderbuffer(&rrb->base, name);
337 rrb->base.ClassID = RADEON_RB_CLASS;
338
339 rrb->base.Delete = radeon_delete_renderbuffer;
340 rrb->base.AllocStorage = radeon_alloc_renderbuffer_storage;
341 rrb->base.GetPointer = radeon_get_pointer;
342
343 return &rrb->base;
344 }
345
346 static void
347 radeon_bind_framebuffer(GLcontext * ctx, GLenum target,
348 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
349 {
350 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
351 radeon_draw_buffer(ctx, fb);
352 }
353 else {
354 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
355 }
356 }
357
358 static void
359 radeon_framebuffer_renderbuffer(GLcontext * ctx,
360 struct gl_framebuffer *fb,
361 GLenum attachment, struct gl_renderbuffer *rb)
362 {
363
364 if (ctx->Driver.Flush)
365 ctx->Driver.Flush(ctx); /* +r6/r7 */
366
367 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
368 radeon_draw_buffer(ctx, fb);
369 }
370
371
372 /* TODO: According to EXT_fbo spec internal format of texture image
373 * once set during glTexImage call, should be preserved when
374 * attaching image to renderbuffer. When HW doesn't support
375 * rendering to format of attached image, set framebuffer
376 * completeness accordingly in radeon_validate_framebuffer (issue #79).
377 */
378 static GLboolean
379 radeon_update_wrapper(GLcontext *ctx, struct radeon_renderbuffer *rrb,
380 struct gl_texture_image *texImage)
381 {
382 int retry = 0;
383 gl_format texFormat;
384
385 restart:
386 if (texImage->TexFormat == _dri_texformat_argb8888) {
387 rrb->base.DataType = GL_UNSIGNED_BYTE;
388 DBG("Render to RGBA8 texture OK\n");
389 }
390 else if (texImage->TexFormat == _dri_texformat_rgb565) {
391 rrb->base.DataType = GL_UNSIGNED_BYTE;
392 DBG("Render to RGB5 texture OK\n");
393 }
394 else if (texImage->TexFormat == _dri_texformat_argb1555) {
395 rrb->base.DataType = GL_UNSIGNED_BYTE;
396 DBG("Render to ARGB1555 texture OK\n");
397 }
398 else if (texImage->TexFormat == _dri_texformat_argb4444) {
399 rrb->base.DataType = GL_UNSIGNED_BYTE;
400 DBG("Render to ARGB4444 texture OK\n");
401 }
402 else if (texImage->TexFormat == MESA_FORMAT_Z16) {
403 rrb->base.DataType = GL_UNSIGNED_SHORT;
404 DBG("Render to DEPTH16 texture OK\n");
405 }
406 else if (texImage->TexFormat == MESA_FORMAT_S8_Z24) {
407 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
408 DBG("Render to DEPTH_STENCIL texture OK\n");
409 }
410 else {
411 /* try redoing the FBO */
412 if (retry == 1) {
413 DBG("Render to texture BAD FORMAT %d\n",
414 texImage->TexFormat);
415 return GL_FALSE;
416 }
417 /* XXX why is the tex format being set here?
418 * I think this can be removed.
419 */
420 texImage->TexFormat = radeonChooseTextureFormat(ctx, texImage->InternalFormat, 0,
421 _mesa_get_format_datatype(texImage->TexFormat),
422 1);
423
424 retry++;
425 goto restart;
426 }
427
428 texFormat = texImage->TexFormat;
429
430 rrb->base.Format = texFormat;
431
432 rrb->cpp = _mesa_get_format_bytes(texFormat);
433 rrb->pitch = texImage->Width * rrb->cpp;
434 rrb->base.InternalFormat = texImage->InternalFormat;
435 rrb->base._BaseFormat = _mesa_base_fbo_format(ctx, rrb->base.InternalFormat);
436
437 rrb->base.Width = texImage->Width;
438 rrb->base.Height = texImage->Height;
439
440 rrb->base.Delete = radeon_delete_renderbuffer;
441 rrb->base.AllocStorage = radeon_nop_alloc_storage;
442
443 return GL_TRUE;
444 }
445
446
447 static struct radeon_renderbuffer *
448 radeon_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
449 {
450 const GLuint name = ~0; /* not significant, but distinct for debugging */
451 struct radeon_renderbuffer *rrb;
452
453 /* make an radeon_renderbuffer to wrap the texture image */
454 rrb = CALLOC_STRUCT(radeon_renderbuffer);
455 if (!rrb) {
456 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
457 return NULL;
458 }
459
460 _mesa_init_renderbuffer(&rrb->base, name);
461 rrb->base.ClassID = RADEON_RB_CLASS;
462
463 if (!radeon_update_wrapper(ctx, rrb, texImage)) {
464 _mesa_free(rrb);
465 return NULL;
466 }
467
468 return rrb;
469
470 }
471 static void
472 radeon_render_texture(GLcontext * ctx,
473 struct gl_framebuffer *fb,
474 struct gl_renderbuffer_attachment *att)
475 {
476 struct gl_texture_image *newImage
477 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
478 struct radeon_renderbuffer *rrb = radeon_renderbuffer(att->Renderbuffer);
479 radeon_texture_image *radeon_image;
480 GLuint imageOffset;
481
482 (void) fb;
483
484 ASSERT(newImage);
485
486 if (newImage->Border != 0) {
487 /* Fallback on drawing to a texture with a border, which won't have a
488 * miptree.
489 */
490 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
491 _mesa_render_texture(ctx, fb, att);
492 return;
493 }
494 else if (!rrb) {
495 rrb = radeon_wrap_texture(ctx, newImage);
496 if (rrb) {
497 /* bind the wrapper to the attachment point */
498 _mesa_reference_renderbuffer(&att->Renderbuffer, &rrb->base);
499 }
500 else {
501 /* fallback to software rendering */
502 _mesa_render_texture(ctx, fb, att);
503 return;
504 }
505 }
506
507 if (!radeon_update_wrapper(ctx, rrb, newImage)) {
508 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
509 _mesa_render_texture(ctx, fb, att);
510 return;
511 }
512
513 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
514 _glthread_GetID(),
515 att->Texture->Name, newImage->Width, newImage->Height,
516 rrb->base.RefCount);
517
518 /* point the renderbufer's region to the texture image region */
519 radeon_image = (radeon_texture_image *)newImage;
520 if (rrb->bo != radeon_image->mt->bo) {
521 if (rrb->bo)
522 radeon_bo_unref(rrb->bo);
523 rrb->bo = radeon_image->mt->bo;
524 radeon_bo_ref(rrb->bo);
525 }
526
527 /* compute offset of the particular 2D image within the texture region */
528 imageOffset = radeon_miptree_image_offset(radeon_image->mt,
529 att->CubeMapFace,
530 att->TextureLevel);
531
532 if (att->Texture->Target == GL_TEXTURE_3D) {
533 GLuint offsets[6];
534 radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
535 offsets);
536 imageOffset += offsets[att->Zoffset];
537 }
538
539 /* store that offset in the region, along with the correct pitch for
540 * the image we are rendering to */
541 rrb->draw_offset = imageOffset;
542 rrb->pitch = radeon_image->mt->levels[att->TextureLevel].rowstride;
543
544 /* update drawing region, etc */
545 radeon_draw_buffer(ctx, fb);
546 }
547
548 static void
549 radeon_finish_render_texture(GLcontext * ctx,
550 struct gl_renderbuffer_attachment *att)
551 {
552
553 }
554 static void
555 radeon_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
556 {
557 }
558
559 void radeon_fbo_init(struct radeon_context *radeon)
560 {
561 radeon->glCtx->Driver.NewFramebuffer = radeon_new_framebuffer;
562 radeon->glCtx->Driver.NewRenderbuffer = radeon_new_renderbuffer;
563 radeon->glCtx->Driver.BindFramebuffer = radeon_bind_framebuffer;
564 radeon->glCtx->Driver.FramebufferRenderbuffer = radeon_framebuffer_renderbuffer;
565 radeon->glCtx->Driver.RenderTexture = radeon_render_texture;
566 radeon->glCtx->Driver.FinishRenderTexture = radeon_finish_render_texture;
567 radeon->glCtx->Driver.ResizeBuffers = radeon_resize_buffers;
568 radeon->glCtx->Driver.ValidateFramebuffer = radeon_validate_framebuffer;
569 radeon->glCtx->Driver.BlitFramebuffer = _mesa_meta_BlitFramebuffer;
570 }
571
572
573 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer *rb,
574 struct radeon_bo *bo)
575 {
576 struct radeon_bo *old;
577 old = rb->bo;
578 rb->bo = bo;
579 radeon_bo_ref(bo);
580 if (old)
581 radeon_bo_unref(old);
582 }