Merge branch 'mesa_7_5_branch' into mesa_7_6_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/texformat.h"
37 #include "main/texrender.h"
38 #include "drivers/common/meta.h"
39
40 #include "radeon_common.h"
41 #include "radeon_mipmap_tree.h"
42
43 #define FILE_DEBUG_FLAG RADEON_TEXTURE
44 #define DBG(...) do { \
45 if (RADEON_DEBUG & FILE_DEBUG_FLAG) \
46 _mesa_printf(__VA_ARGS__); \
47 } while(0)
48
49 static struct gl_framebuffer *
50 radeon_new_framebuffer(GLcontext *ctx, GLuint name)
51 {
52 return _mesa_new_framebuffer(ctx, name);
53 }
54
55 static void
56 radeon_delete_renderbuffer(struct gl_renderbuffer *rb)
57 {
58 struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
59
60 ASSERT(rrb);
61
62 if (rrb && rrb->bo) {
63 radeon_bo_unref(rrb->bo);
64 }
65 _mesa_free(rrb);
66 }
67
68 static void *
69 radeon_get_pointer(GLcontext *ctx, struct gl_renderbuffer *rb,
70 GLint x, GLint y)
71 {
72 return NULL;
73 }
74
75 /**
76 * Called via glRenderbufferStorageEXT() to set the format and allocate
77 * storage for a user-created renderbuffer.
78 */
79 static GLboolean
80 radeon_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
81 GLenum internalFormat,
82 GLuint width, GLuint height)
83 {
84 struct radeon_context *radeon = RADEON_CONTEXT(ctx);
85 struct radeon_renderbuffer *rrb = radeon_renderbuffer(rb);
86 GLboolean software_buffer = GL_FALSE;
87 int cpp;
88
89 ASSERT(rb->Name != 0);
90 switch (internalFormat) {
91 case GL_R3_G3_B2:
92 case GL_RGB4:
93 case GL_RGB5:
94 rb->_ActualFormat = GL_RGB5;
95 rb->DataType = GL_UNSIGNED_BYTE;
96 rb->RedBits = 5;
97 rb->GreenBits = 6;
98 rb->BlueBits = 5;
99 cpp = 2;
100 break;
101 case GL_RGB:
102 case GL_RGB8:
103 case GL_RGB10:
104 case GL_RGB12:
105 case GL_RGB16:
106 rb->_ActualFormat = GL_RGB8;
107 rb->DataType = GL_UNSIGNED_BYTE;
108 rb->RedBits = 8;
109 rb->GreenBits = 8;
110 rb->BlueBits = 8;
111 rb->AlphaBits = 0;
112 cpp = 4;
113 break;
114 case GL_RGBA:
115 case GL_RGBA2:
116 case GL_RGBA4:
117 case GL_RGB5_A1:
118 case GL_RGBA8:
119 case GL_RGB10_A2:
120 case GL_RGBA12:
121 case GL_RGBA16:
122 rb->_ActualFormat = GL_RGBA8;
123 rb->DataType = GL_UNSIGNED_BYTE;
124 rb->RedBits = 8;
125 rb->GreenBits = 8;
126 rb->BlueBits = 8;
127 rb->AlphaBits = 8;
128 cpp = 4;
129 break;
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;
138 rb->StencilBits = 8;
139 cpp = 4;
140 break;
141 case GL_DEPTH_COMPONENT16:
142 rb->_ActualFormat = GL_DEPTH_COMPONENT16;
143 rb->DataType = GL_UNSIGNED_SHORT;
144 rb->DepthBits = 16;
145 cpp = 2;
146 break;
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;
152 rb->DepthBits = 24;
153 cpp = 4;
154 break;
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;
159 rb->DepthBits = 24;
160 rb->StencilBits = 8;
161 cpp = 4;
162 break;
163 default:
164 _mesa_problem(ctx,
165 "Unexpected format in intel_alloc_renderbuffer_storage");
166 return GL_FALSE;
167 }
168
169 if (ctx->Driver.Flush)
170 ctx->Driver.Flush(ctx); /* +r6/r7 */
171
172 if (rrb->bo)
173 radeon_bo_unref(rrb->bo);
174
175
176 if (software_buffer) {
177 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
178 width, height);
179 }
180 else {
181 uint32_t size;
182 uint32_t pitch = ((cpp * width + 63) & ~63) / cpp;
183
184 fprintf(stderr,"Allocating %d x %d radeon RBO (pitch %d)\n", width,
185 height, pitch);
186
187 size = pitch * height * cpp;
188 rrb->pitch = pitch * cpp;
189 rrb->cpp = cpp;
190 rrb->bo = radeon_bo_open(radeon->radeonScreen->bom,
191 0,
192 size,
193 0,
194 RADEON_GEM_DOMAIN_VRAM,
195 0);
196 rb->Width = width;
197 rb->Height = height;
198 return GL_TRUE;
199 }
200
201 }
202
203
204 /**
205 * Called for each hardware renderbuffer when a _window_ is resized.
206 * Just update fields.
207 * Not used for user-created renderbuffers!
208 */
209 static GLboolean
210 radeon_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
211 GLenum internalFormat, GLuint width, GLuint height)
212 {
213 ASSERT(rb->Name == 0);
214 rb->Width = width;
215 rb->Height = height;
216 rb->_ActualFormat = internalFormat;
217
218 return GL_TRUE;
219 }
220
221
222 static void
223 radeon_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
224 GLuint width, GLuint height)
225 {
226 struct radeon_framebuffer *radeon_fb = (struct radeon_framebuffer*)fb;
227 int i;
228
229 _mesa_resize_framebuffer(ctx, fb, width, height);
230
231 fb->Initialized = GL_TRUE; /* XXX remove someday */
232
233 if (fb->Name != 0) {
234 return;
235 }
236
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;
240
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);
244 }
245 }
246 }
247
248
249 /** Dummy function for gl_renderbuffer::AllocStorage() */
250 static GLboolean
251 radeon_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
252 GLenum internalFormat, GLuint width, GLuint height)
253 {
254 _mesa_problem(ctx, "radeon_op_alloc_storage should never be called.");
255 return GL_FALSE;
256 }
257
258 struct radeon_renderbuffer *
259 radeon_create_renderbuffer(GLenum format, __DRIdrawablePrivate *driDrawPriv)
260 {
261 struct radeon_renderbuffer *rrb;
262
263 rrb = CALLOC_STRUCT(radeon_renderbuffer);
264 if (!rrb)
265 return NULL;
266
267 _mesa_init_renderbuffer(&rrb->base, 0);
268 rrb->base.ClassID = RADEON_RB_CLASS;
269
270 /* XXX format junk */
271 switch (format) {
272 case GL_RGB5:
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;
279 break;
280 case GL_RGB8:
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;
288 break;
289 case GL_RGBA8:
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;
297 break;
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;
303 break;
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;
309 break;
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;
315 break;
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;
322 break;
323 default:
324 fprintf(stderr, "%s: Unknown format 0x%04x\n", __FUNCTION__, format);
325 _mesa_delete_renderbuffer(&rrb->base);
326 return NULL;
327 }
328
329 rrb->dPriv = driDrawPriv;
330 rrb->base.InternalFormat = format;
331
332 rrb->base.Delete = radeon_delete_renderbuffer;
333 rrb->base.AllocStorage = radeon_alloc_window_storage;
334 rrb->base.GetPointer = radeon_get_pointer;
335
336 rrb->bo = NULL;
337 return rrb;
338 }
339
340 static struct gl_renderbuffer *
341 radeon_new_renderbuffer(GLcontext * ctx, GLuint name)
342 {
343 struct radeon_renderbuffer *rrb;
344
345 rrb = CALLOC_STRUCT(radeon_renderbuffer);
346 if (!rrb)
347 return NULL;
348
349 _mesa_init_renderbuffer(&rrb->base, name);
350 rrb->base.ClassID = RADEON_RB_CLASS;
351
352 rrb->base.Delete = radeon_delete_renderbuffer;
353 rrb->base.AllocStorage = radeon_alloc_renderbuffer_storage;
354 rrb->base.GetPointer = radeon_get_pointer;
355
356 return &rrb->base;
357 }
358
359 static void
360 radeon_bind_framebuffer(GLcontext * ctx, GLenum target,
361 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
362 {
363 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
364 radeon_draw_buffer(ctx, fb);
365 }
366 else {
367 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
368 }
369 }
370
371 static void
372 radeon_framebuffer_renderbuffer(GLcontext * ctx,
373 struct gl_framebuffer *fb,
374 GLenum attachment, struct gl_renderbuffer *rb)
375 {
376
377 if (ctx->Driver.Flush)
378 ctx->Driver.Flush(ctx); /* +r6/r7 */
379
380 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
381 radeon_draw_buffer(ctx, fb);
382 }
383
384
385 static GLboolean
386 radeon_update_wrapper(GLcontext *ctx, struct radeon_renderbuffer *rrb,
387 struct gl_texture_image *texImage)
388 {
389 int retry = 0;
390 restart:
391 if (texImage->TexFormat == &_mesa_texformat_argb8888) {
392 rrb->cpp = 4;
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");
397 }
398 else if (texImage->TexFormat == &_mesa_texformat_rgb565) {
399 rrb->cpp = 2;
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");
404 }
405 else if (texImage->TexFormat == &_mesa_texformat_argb1555) {
406 rrb->cpp = 2;
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");
411 }
412 else if (texImage->TexFormat == &_mesa_texformat_argb4444) {
413 rrb->cpp = 2;
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");
418 }
419 else if (texImage->TexFormat == &_mesa_texformat_z16) {
420 rrb->cpp = 2;
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");
425 }
426 else if (texImage->TexFormat == &_mesa_texformat_s8_z24) {
427 rrb->cpp = 4;
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");
432 }
433 else {
434 /* try redoing the FBO */
435 if (retry == 1) {
436 DBG("Render to texture BAD FORMAT %d\n",
437 texImage->TexFormat->MesaFormat);
438 return GL_FALSE;
439 }
440 texImage->TexFormat = radeonChooseTextureFormat(ctx, texImage->InternalFormat, 0,
441 texImage->TexFormat->DataType,
442 1);
443
444 retry++;
445 goto restart;
446 }
447
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;
458
459 rrb->base.Delete = radeon_delete_renderbuffer;
460 rrb->base.AllocStorage = radeon_nop_alloc_storage;
461
462 return GL_TRUE;
463 }
464
465
466 static struct radeon_renderbuffer *
467 radeon_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
468 {
469 const GLuint name = ~0; /* not significant, but distinct for debugging */
470 struct radeon_renderbuffer *rrb;
471
472 /* make an radeon_renderbuffer to wrap the texture image */
473 rrb = CALLOC_STRUCT(radeon_renderbuffer);
474 if (!rrb) {
475 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
476 return NULL;
477 }
478
479 _mesa_init_renderbuffer(&rrb->base, name);
480 rrb->base.ClassID = RADEON_RB_CLASS;
481
482 if (!radeon_update_wrapper(ctx, rrb, texImage)) {
483 _mesa_free(rrb);
484 return NULL;
485 }
486
487 return rrb;
488
489 }
490 static void
491 radeon_render_texture(GLcontext * ctx,
492 struct gl_framebuffer *fb,
493 struct gl_renderbuffer_attachment *att)
494 {
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;
499 GLuint imageOffset;
500
501 (void) fb;
502
503 ASSERT(newImage);
504
505 if (newImage->Border != 0) {
506 /* Fallback on drawing to a texture with a border, which won't have a
507 * miptree.
508 */
509 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
510 _mesa_render_texture(ctx, fb, att);
511 return;
512 }
513 else if (!rrb) {
514 rrb = radeon_wrap_texture(ctx, newImage);
515 if (rrb) {
516 /* bind the wrapper to the attachment point */
517 _mesa_reference_renderbuffer(&att->Renderbuffer, &rrb->base);
518 }
519 else {
520 /* fallback to software rendering */
521 _mesa_render_texture(ctx, fb, att);
522 return;
523 }
524 }
525
526 if (!radeon_update_wrapper(ctx, rrb, newImage)) {
527 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
528 _mesa_render_texture(ctx, fb, att);
529 return;
530 }
531
532 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
533 _glthread_GetID(),
534 att->Texture->Name, newImage->Width, newImage->Height,
535 rrb->base.RefCount);
536
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) {
540 if (rrb->bo)
541 radeon_bo_unref(rrb->bo);
542 rrb->bo = radeon_image->mt->bo;
543 radeon_bo_ref(rrb->bo);
544 }
545
546 /* compute offset of the particular 2D image within the texture region */
547 imageOffset = radeon_miptree_image_offset(radeon_image->mt,
548 att->CubeMapFace,
549 att->TextureLevel);
550
551 if (att->Texture->Target == GL_TEXTURE_3D) {
552 GLuint offsets[6];
553 radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
554 offsets);
555 imageOffset += offsets[att->Zoffset];
556 }
557
558 /* store that offset in the region */
559 rrb->draw_offset = imageOffset;
560
561 /* update drawing region, etc */
562 radeon_draw_buffer(ctx, fb);
563 }
564
565 static void
566 radeon_finish_render_texture(GLcontext * ctx,
567 struct gl_renderbuffer_attachment *att)
568 {
569
570 }
571 static void
572 radeon_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
573 {
574 }
575
576 void radeon_fbo_init(struct radeon_context *radeon)
577 {
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_blit_framebuffer;
587 }
588
589
590 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer *rb,
591 struct radeon_bo *bo)
592 {
593 struct radeon_bo *old;
594 old = rb->bo;
595 rb->bo = bo;
596 radeon_bo_ref(bo);
597 if (old)
598 radeon_bo_unref(old);
599 }