r300: add support for EXT_framebuffer_blit
[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 DEBUG_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 = width * height * cpp;
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 rrb->pitch = pitch * cpp;
188 rrb->cpp = cpp;
189 rrb->bo = radeon_bo_open(radeon->radeonScreen->bom,
190 0,
191 size,
192 0,
193 RADEON_GEM_DOMAIN_VRAM,
194 0);
195 rb->Width = width;
196 rb->Height = height;
197 return GL_TRUE;
198 }
199
200 }
201
202
203 /**
204 * Called for each hardware renderbuffer when a _window_ is resized.
205 * Just update fields.
206 * Not used for user-created renderbuffers!
207 */
208 static GLboolean
209 radeon_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
210 GLenum internalFormat, GLuint width, GLuint height)
211 {
212 ASSERT(rb->Name == 0);
213 rb->Width = width;
214 rb->Height = height;
215 rb->_ActualFormat = internalFormat;
216
217 return GL_TRUE;
218 }
219
220
221 static void
222 radeon_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
223 GLuint width, GLuint height)
224 {
225 struct radeon_framebuffer *radeon_fb = (struct radeon_framebuffer*)fb;
226 int i;
227
228 _mesa_resize_framebuffer(ctx, fb, width, height);
229
230 fb->Initialized = GL_TRUE; /* XXX remove someday */
231
232 if (fb->Name != 0) {
233 return;
234 }
235
236 /* Make sure all window system renderbuffers are up to date */
237 for (i = 0; i < 2; i++) {
238 struct gl_renderbuffer *rb = &radeon_fb->color_rb[i]->base;
239
240 /* only resize if size is changing */
241 if (rb && (rb->Width != width || rb->Height != height)) {
242 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
243 }
244 }
245 }
246
247
248 /** Dummy function for gl_renderbuffer::AllocStorage() */
249 static GLboolean
250 radeon_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
251 GLenum internalFormat, GLuint width, GLuint height)
252 {
253 _mesa_problem(ctx, "radeon_op_alloc_storage should never be called.");
254 return GL_FALSE;
255 }
256
257 struct radeon_renderbuffer *
258 radeon_create_renderbuffer(GLenum format, __DRIdrawablePrivate *driDrawPriv)
259 {
260 struct radeon_renderbuffer *rrb;
261
262 rrb = CALLOC_STRUCT(radeon_renderbuffer);
263 if (!rrb)
264 return NULL;
265
266 _mesa_init_renderbuffer(&rrb->base, 0);
267 rrb->base.ClassID = RADEON_RB_CLASS;
268
269 /* XXX format junk */
270 switch (format) {
271 case GL_RGB5:
272 rrb->base._ActualFormat = GL_RGB5;
273 rrb->base._BaseFormat = GL_RGBA;
274 rrb->base.RedBits = 5;
275 rrb->base.GreenBits = 6;
276 rrb->base.BlueBits = 5;
277 rrb->base.DataType = GL_UNSIGNED_BYTE;
278 break;
279 case GL_RGB8:
280 rrb->base._ActualFormat = GL_RGB8;
281 rrb->base._BaseFormat = GL_RGB;
282 rrb->base.RedBits = 8;
283 rrb->base.GreenBits = 8;
284 rrb->base.BlueBits = 8;
285 rrb->base.AlphaBits = 0;
286 rrb->base.DataType = GL_UNSIGNED_BYTE;
287 break;
288 case GL_RGBA8:
289 rrb->base._ActualFormat = GL_RGBA8;
290 rrb->base._BaseFormat = GL_RGBA;
291 rrb->base.RedBits = 8;
292 rrb->base.GreenBits = 8;
293 rrb->base.BlueBits = 8;
294 rrb->base.AlphaBits = 8;
295 rrb->base.DataType = GL_UNSIGNED_BYTE;
296 break;
297 case GL_STENCIL_INDEX8_EXT:
298 rrb->base._ActualFormat = GL_STENCIL_INDEX8_EXT;
299 rrb->base._BaseFormat = GL_STENCIL_INDEX;
300 rrb->base.StencilBits = 8;
301 rrb->base.DataType = GL_UNSIGNED_BYTE;
302 break;
303 case GL_DEPTH_COMPONENT16:
304 rrb->base._ActualFormat = GL_DEPTH_COMPONENT16;
305 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
306 rrb->base.DepthBits = 16;
307 rrb->base.DataType = GL_UNSIGNED_SHORT;
308 break;
309 case GL_DEPTH_COMPONENT24:
310 rrb->base._ActualFormat = GL_DEPTH_COMPONENT24;
311 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
312 rrb->base.DepthBits = 24;
313 rrb->base.DataType = GL_UNSIGNED_INT;
314 break;
315 case GL_DEPTH24_STENCIL8_EXT:
316 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
317 rrb->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
318 rrb->base.DepthBits = 24;
319 rrb->base.StencilBits = 8;
320 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
321 break;
322 default:
323 fprintf(stderr, "%s: Unknown format 0x%04x\n", __FUNCTION__, format);
324 _mesa_delete_renderbuffer(&rrb->base);
325 return NULL;
326 }
327
328 rrb->dPriv = driDrawPriv;
329 rrb->base.InternalFormat = format;
330
331 rrb->base.Delete = radeon_delete_renderbuffer;
332 rrb->base.AllocStorage = radeon_alloc_window_storage;
333 rrb->base.GetPointer = radeon_get_pointer;
334
335 rrb->bo = NULL;
336 return rrb;
337 }
338
339 static struct gl_renderbuffer *
340 radeon_new_renderbuffer(GLcontext * ctx, GLuint name)
341 {
342 struct radeon_renderbuffer *rrb;
343
344 rrb = CALLOC_STRUCT(radeon_renderbuffer);
345 if (!rrb)
346 return NULL;
347
348 _mesa_init_renderbuffer(&rrb->base, name);
349 rrb->base.ClassID = RADEON_RB_CLASS;
350
351 rrb->base.Delete = radeon_delete_renderbuffer;
352 rrb->base.AllocStorage = radeon_alloc_renderbuffer_storage;
353 rrb->base.GetPointer = radeon_get_pointer;
354
355 return &rrb->base;
356 }
357
358 static void
359 radeon_bind_framebuffer(GLcontext * ctx, GLenum target,
360 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
361 {
362 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
363 radeon_draw_buffer(ctx, fb);
364 }
365 else {
366 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
367 }
368 }
369
370 static void
371 radeon_framebuffer_renderbuffer(GLcontext * ctx,
372 struct gl_framebuffer *fb,
373 GLenum attachment, struct gl_renderbuffer *rb)
374 {
375
376 if (ctx->Driver.Flush)
377 ctx->Driver.Flush(ctx); /* +r6/r7 */
378
379 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
380 radeon_draw_buffer(ctx, fb);
381 }
382
383
384 static GLboolean
385 radeon_update_wrapper(GLcontext *ctx, struct radeon_renderbuffer *rrb,
386 struct gl_texture_image *texImage)
387 {
388 int retry = 0;
389 restart:
390 if (texImage->TexFormat == &_mesa_texformat_argb8888) {
391 rrb->cpp = 4;
392 rrb->base._ActualFormat = GL_RGBA8;
393 rrb->base._BaseFormat = GL_RGBA;
394 rrb->base.DataType = GL_UNSIGNED_BYTE;
395 DBG("Render to RGBA8 texture OK\n");
396 }
397 else if (texImage->TexFormat == &_mesa_texformat_rgb565) {
398 rrb->cpp = 2;
399 rrb->base._ActualFormat = GL_RGB5;
400 rrb->base._BaseFormat = GL_RGB;
401 rrb->base.DataType = GL_UNSIGNED_BYTE;
402 DBG("Render to RGB5 texture OK\n");
403 }
404 else if (texImage->TexFormat == &_mesa_texformat_argb1555) {
405 rrb->cpp = 2;
406 rrb->base._ActualFormat = GL_RGB5_A1;
407 rrb->base._BaseFormat = GL_RGBA;
408 rrb->base.DataType = GL_UNSIGNED_BYTE;
409 DBG("Render to ARGB1555 texture OK\n");
410 }
411 else if (texImage->TexFormat == &_mesa_texformat_argb4444) {
412 rrb->cpp = 2;
413 rrb->base._ActualFormat = GL_RGBA4;
414 rrb->base._BaseFormat = GL_RGBA;
415 rrb->base.DataType = GL_UNSIGNED_BYTE;
416 DBG("Render to ARGB1555 texture OK\n");
417 }
418 else if (texImage->TexFormat == &_mesa_texformat_z16) {
419 rrb->cpp = 2;
420 rrb->base._ActualFormat = GL_DEPTH_COMPONENT16;
421 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
422 rrb->base.DataType = GL_UNSIGNED_SHORT;
423 DBG("Render to DEPTH16 texture OK\n");
424 }
425 else if (texImage->TexFormat == &_mesa_texformat_s8_z24) {
426 rrb->cpp = 4;
427 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
428 rrb->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
429 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
430 DBG("Render to DEPTH_STENCIL texture OK\n");
431 }
432 else {
433 /* try redoing the FBO */
434 if (retry == 1) {
435 DBG("Render to texture BAD FORMAT %d\n",
436 texImage->TexFormat->MesaFormat);
437 return GL_FALSE;
438 }
439 texImage->TexFormat = radeonChooseTextureFormat(ctx, texImage->InternalFormat, 0,
440 texImage->TexFormat->DataType,
441 1);
442
443 retry++;
444 goto restart;
445 }
446
447 rrb->pitch = texImage->Width * rrb->cpp;
448 rrb->base.InternalFormat = rrb->base._ActualFormat;
449 rrb->base.Width = texImage->Width;
450 rrb->base.Height = texImage->Height;
451 rrb->base.RedBits = texImage->TexFormat->RedBits;
452 rrb->base.GreenBits = texImage->TexFormat->GreenBits;
453 rrb->base.BlueBits = texImage->TexFormat->BlueBits;
454 rrb->base.AlphaBits = texImage->TexFormat->AlphaBits;
455 rrb->base.DepthBits = texImage->TexFormat->DepthBits;
456 rrb->base.StencilBits = texImage->TexFormat->StencilBits;
457
458 rrb->base.Delete = radeon_delete_renderbuffer;
459 rrb->base.AllocStorage = radeon_nop_alloc_storage;
460
461 return GL_TRUE;
462 }
463
464
465 static struct radeon_renderbuffer *
466 radeon_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
467 {
468 const GLuint name = ~0; /* not significant, but distinct for debugging */
469 struct radeon_renderbuffer *rrb;
470
471 /* make an radeon_renderbuffer to wrap the texture image */
472 rrb = CALLOC_STRUCT(radeon_renderbuffer);
473 if (!rrb) {
474 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
475 return NULL;
476 }
477
478 _mesa_init_renderbuffer(&rrb->base, name);
479 rrb->base.ClassID = RADEON_RB_CLASS;
480
481 if (!radeon_update_wrapper(ctx, rrb, texImage)) {
482 _mesa_free(rrb);
483 return NULL;
484 }
485
486 return rrb;
487
488 }
489 static void
490 radeon_render_texture(GLcontext * ctx,
491 struct gl_framebuffer *fb,
492 struct gl_renderbuffer_attachment *att)
493 {
494 struct gl_texture_image *newImage
495 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
496 struct radeon_renderbuffer *rrb = radeon_renderbuffer(att->Renderbuffer);
497 radeon_texture_image *radeon_image;
498 GLuint imageOffset;
499
500 (void) fb;
501
502 ASSERT(newImage);
503
504 if (newImage->Border != 0) {
505 /* Fallback on drawing to a texture with a border, which won't have a
506 * miptree.
507 */
508 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
509 _mesa_render_texture(ctx, fb, att);
510 return;
511 }
512 else if (!rrb) {
513 rrb = radeon_wrap_texture(ctx, newImage);
514 if (rrb) {
515 /* bind the wrapper to the attachment point */
516 _mesa_reference_renderbuffer(&att->Renderbuffer, &rrb->base);
517 }
518 else {
519 /* fallback to software rendering */
520 _mesa_render_texture(ctx, fb, att);
521 return;
522 }
523 }
524
525 if (!radeon_update_wrapper(ctx, rrb, newImage)) {
526 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
527 _mesa_render_texture(ctx, fb, att);
528 return;
529 }
530
531 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
532 _glthread_GetID(),
533 att->Texture->Name, newImage->Width, newImage->Height,
534 rrb->base.RefCount);
535
536 /* point the renderbufer's region to the texture image region */
537 radeon_image = (radeon_texture_image *)newImage;
538 if (rrb->bo != radeon_image->mt->bo) {
539 if (rrb->bo)
540 radeon_bo_unref(rrb->bo);
541 rrb->bo = radeon_image->mt->bo;
542 radeon_bo_ref(rrb->bo);
543 }
544
545 /* compute offset of the particular 2D image within the texture region */
546 imageOffset = radeon_miptree_image_offset(radeon_image->mt,
547 att->CubeMapFace,
548 att->TextureLevel);
549
550 if (att->Texture->Target == GL_TEXTURE_3D) {
551 GLuint offsets[6];
552 radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
553 offsets);
554 imageOffset += offsets[att->Zoffset];
555 }
556
557 /* store that offset in the region */
558 rrb->draw_offset = imageOffset;
559
560 /* update drawing region, etc */
561 radeon_draw_buffer(ctx, fb);
562 }
563
564 static void
565 radeon_finish_render_texture(GLcontext * ctx,
566 struct gl_renderbuffer_attachment *att)
567 {
568
569 }
570 static void
571 radeon_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
572 {
573 }
574
575 void radeon_fbo_init(struct radeon_context *radeon)
576 {
577 radeon->glCtx->Driver.NewFramebuffer = radeon_new_framebuffer;
578 radeon->glCtx->Driver.NewRenderbuffer = radeon_new_renderbuffer;
579 radeon->glCtx->Driver.BindFramebuffer = radeon_bind_framebuffer;
580 radeon->glCtx->Driver.FramebufferRenderbuffer = radeon_framebuffer_renderbuffer;
581 radeon->glCtx->Driver.RenderTexture = radeon_render_texture;
582 radeon->glCtx->Driver.FinishRenderTexture = radeon_finish_render_texture;
583 radeon->glCtx->Driver.ResizeBuffers = radeon_resize_buffers;
584 radeon->glCtx->Driver.ValidateFramebuffer = radeon_validate_framebuffer;
585 radeon->glCtx->Driver.BlitFramebuffer = _mesa_meta_blit_framebuffer;
586 }
587
588
589 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer *rb,
590 struct radeon_bo *bo)
591 {
592 struct radeon_bo *old;
593 old = rb->bo;
594 rb->bo = bo;
595 radeon_bo_ref(bo);
596 if (old)
597 radeon_bo_unref(old);
598 }