Makeup checkin for radeon code change paired with r6/7 code.
[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
39 #include "radeon_common.h"
40 #include "radeon_mipmap_tree.h"
41
42 #define FILE_DEBUG_FLAG DEBUG_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->_ActualFormat = GL_RGB5;
94 rb->DataType = GL_UNSIGNED_BYTE;
95 rb->RedBits = 5;
96 rb->GreenBits = 6;
97 rb->BlueBits = 5;
98 cpp = 2;
99 break;
100 case GL_RGB:
101 case GL_RGB8:
102 case GL_RGB10:
103 case GL_RGB12:
104 case GL_RGB16:
105 rb->_ActualFormat = GL_RGB8;
106 rb->DataType = GL_UNSIGNED_BYTE;
107 rb->RedBits = 8;
108 rb->GreenBits = 8;
109 rb->BlueBits = 8;
110 rb->AlphaBits = 0;
111 cpp = 4;
112 break;
113 case GL_RGBA:
114 case GL_RGBA2:
115 case GL_RGBA4:
116 case GL_RGB5_A1:
117 case GL_RGBA8:
118 case GL_RGB10_A2:
119 case GL_RGBA12:
120 case GL_RGBA16:
121 rb->_ActualFormat = GL_RGBA8;
122 rb->DataType = GL_UNSIGNED_BYTE;
123 rb->RedBits = 8;
124 rb->GreenBits = 8;
125 rb->BlueBits = 8;
126 rb->AlphaBits = 8;
127 cpp = 4;
128 break;
129 case GL_STENCIL_INDEX:
130 case GL_STENCIL_INDEX1_EXT:
131 case GL_STENCIL_INDEX4_EXT:
132 case GL_STENCIL_INDEX8_EXT:
133 case GL_STENCIL_INDEX16_EXT:
134 /* alloc a depth+stencil buffer */
135 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
136 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
137 rb->StencilBits = 8;
138 cpp = 4;
139 break;
140 case GL_DEPTH_COMPONENT16:
141 rb->_ActualFormat = GL_DEPTH_COMPONENT16;
142 rb->DataType = GL_UNSIGNED_SHORT;
143 rb->DepthBits = 16;
144 cpp = 2;
145 break;
146 case GL_DEPTH_COMPONENT:
147 case GL_DEPTH_COMPONENT24:
148 case GL_DEPTH_COMPONENT32:
149 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
150 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
151 rb->DepthBits = 24;
152 cpp = 4;
153 break;
154 case GL_DEPTH_STENCIL_EXT:
155 case GL_DEPTH24_STENCIL8_EXT:
156 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
157 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
158 rb->DepthBits = 24;
159 rb->StencilBits = 8;
160 cpp = 4;
161 break;
162 default:
163 _mesa_problem(ctx,
164 "Unexpected format in intel_alloc_renderbuffer_storage");
165 return GL_FALSE;
166 }
167
168 ctx->Driver.Flush(ctx); /* +r6/r7 */
169
170 if (rrb->bo)
171 radeon_bo_unref(rrb->bo);
172
173
174 if (software_buffer) {
175 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
176 width, height);
177 }
178 else {
179 uint32_t size = width * height * cpp;
180 uint32_t pitch = ((cpp * width + 63) & ~63) / cpp;
181
182 fprintf(stderr,"Allocating %d x %d radeon RBO (pitch %d)\n", width,
183 height, pitch);
184
185 rrb->pitch = pitch * cpp;
186 rrb->cpp = cpp;
187 #ifdef RADEON_DEBUG_BO
188 rrb->bo = radeon_bo_open(radeon->radeonScreen->bom,
189 0,
190 size,
191 0,
192 RADEON_GEM_DOMAIN_VRAM,
193 0,
194 "Radeon RBO");
195 #else
196 rrb->bo = radeon_bo_open(radeon->radeonScreen->bom,
197 0,
198 size,
199 0,
200 RADEON_GEM_DOMAIN_VRAM,
201 0);
202 #endif /* RADEON_DEBUG_BO */
203 rb->Width = width;
204 rb->Height = height;
205 return GL_TRUE;
206 }
207
208 }
209
210
211 /**
212 * Called for each hardware renderbuffer when a _window_ is resized.
213 * Just update fields.
214 * Not used for user-created renderbuffers!
215 */
216 static GLboolean
217 radeon_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
218 GLenum internalFormat, GLuint width, GLuint height)
219 {
220 ASSERT(rb->Name == 0);
221 rb->Width = width;
222 rb->Height = height;
223 rb->_ActualFormat = internalFormat;
224
225 return GL_TRUE;
226 }
227
228
229 static void
230 radeon_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
231 GLuint width, GLuint height)
232 {
233 struct radeon_framebuffer *radeon_fb = (struct radeon_framebuffer*)fb;
234 int i;
235
236 _mesa_resize_framebuffer(ctx, fb, width, height);
237
238 fb->Initialized = GL_TRUE; /* XXX remove someday */
239
240 if (fb->Name != 0) {
241 return;
242 }
243
244 /* Make sure all window system renderbuffers are up to date */
245 for (i = 0; i < 2; i++) {
246 struct gl_renderbuffer *rb = &radeon_fb->color_rb[i]->base;
247
248 /* only resize if size is changing */
249 if (rb && (rb->Width != width || rb->Height != height)) {
250 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
251 }
252 }
253 }
254
255
256 /** Dummy function for gl_renderbuffer::AllocStorage() */
257 static GLboolean
258 radeon_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
259 GLenum internalFormat, GLuint width, GLuint height)
260 {
261 _mesa_problem(ctx, "radeon_op_alloc_storage should never be called.");
262 return GL_FALSE;
263 }
264
265 struct radeon_renderbuffer *
266 radeon_create_renderbuffer(GLenum format, __DRIdrawablePrivate *driDrawPriv)
267 {
268 struct radeon_renderbuffer *rrb;
269
270 rrb = CALLOC_STRUCT(radeon_renderbuffer);
271 if (!rrb)
272 return NULL;
273
274 _mesa_init_renderbuffer(&rrb->base, 0);
275 rrb->base.ClassID = RADEON_RB_CLASS;
276
277 /* XXX format junk */
278 switch (format) {
279 case GL_RGB5:
280 rrb->base._ActualFormat = GL_RGB5;
281 rrb->base._BaseFormat = GL_RGBA;
282 rrb->base.RedBits = 5;
283 rrb->base.GreenBits = 6;
284 rrb->base.BlueBits = 5;
285 rrb->base.DataType = GL_UNSIGNED_BYTE;
286 break;
287 case GL_RGB8:
288 rrb->base._ActualFormat = GL_RGB8;
289 rrb->base._BaseFormat = GL_RGB;
290 rrb->base.RedBits = 8;
291 rrb->base.GreenBits = 8;
292 rrb->base.BlueBits = 8;
293 rrb->base.AlphaBits = 8;
294 rrb->base.DataType = GL_UNSIGNED_BYTE;
295 break;
296 case GL_RGBA8:
297 rrb->base._ActualFormat = GL_RGBA8;
298 rrb->base._BaseFormat = GL_RGBA;
299 rrb->base.RedBits = 8;
300 rrb->base.GreenBits = 8;
301 rrb->base.BlueBits = 8;
302 rrb->base.AlphaBits = 8;
303 rrb->base.DataType = GL_UNSIGNED_BYTE;
304 break;
305 case GL_STENCIL_INDEX8_EXT:
306 rrb->base._ActualFormat = GL_STENCIL_INDEX8_EXT;
307 rrb->base._BaseFormat = GL_STENCIL_INDEX;
308 rrb->base.StencilBits = 8;
309 rrb->base.DataType = GL_UNSIGNED_BYTE;
310 break;
311 case GL_DEPTH_COMPONENT16:
312 rrb->base._ActualFormat = GL_DEPTH_COMPONENT16;
313 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
314 rrb->base.DepthBits = 16;
315 rrb->base.DataType = GL_UNSIGNED_SHORT;
316 break;
317 case GL_DEPTH_COMPONENT24:
318 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
319 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
320 rrb->base.DepthBits = 24;
321 rrb->base.DataType = GL_UNSIGNED_INT;
322 break;
323 case GL_DEPTH24_STENCIL8_EXT:
324 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
325 rrb->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
326 rrb->base.DepthBits = 24;
327 rrb->base.StencilBits = 8;
328 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
329 break;
330 default:
331 fprintf(stderr, "%s: Unknown format 0x%04x\n", __FUNCTION__, format);
332 _mesa_delete_renderbuffer(&rrb->base);
333 return NULL;
334 }
335
336 rrb->dPriv = driDrawPriv;
337 rrb->base.InternalFormat = format;
338
339 rrb->base.Delete = radeon_delete_renderbuffer;
340 rrb->base.AllocStorage = radeon_alloc_window_storage;
341 rrb->base.GetPointer = radeon_get_pointer;
342
343 rrb->bo = NULL;
344 return rrb;
345 }
346
347 static struct gl_renderbuffer *
348 radeon_new_renderbuffer(GLcontext * ctx, GLuint name)
349 {
350 struct radeon_renderbuffer *rrb;
351
352 rrb = CALLOC_STRUCT(radeon_renderbuffer);
353 if (!rrb)
354 return NULL;
355
356 _mesa_init_renderbuffer(&rrb->base, name);
357 rrb->base.ClassID = RADEON_RB_CLASS;
358
359 rrb->base.Delete = radeon_delete_renderbuffer;
360 rrb->base.AllocStorage = radeon_alloc_renderbuffer_storage;
361 rrb->base.GetPointer = radeon_get_pointer;
362
363 return &rrb->base;
364 }
365
366 static void
367 radeon_bind_framebuffer(GLcontext * ctx, GLenum target,
368 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
369 {
370 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
371 radeon_draw_buffer(ctx, fb);
372 }
373 else {
374 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
375 }
376 }
377
378 static void
379 radeon_framebuffer_renderbuffer(GLcontext * ctx,
380 struct gl_framebuffer *fb,
381 GLenum attachment, struct gl_renderbuffer *rb)
382 {
383
384 ctx->Driver.Flush(ctx); /* +r6/r7 */
385
386 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
387 radeon_draw_buffer(ctx, fb);
388 }
389
390
391 static GLboolean
392 radeon_update_wrapper(GLcontext *ctx, struct radeon_renderbuffer *rrb,
393 struct gl_texture_image *texImage)
394 {
395 int retry = 0;
396 restart:
397 if (texImage->TexFormat == &_mesa_texformat_argb8888) {
398 rrb->cpp = 4;
399 rrb->base._ActualFormat = GL_RGBA8;
400 rrb->base._BaseFormat = GL_RGBA;
401 rrb->base.DataType = GL_UNSIGNED_BYTE;
402 DBG("Render to RGBA8 texture OK\n");
403 }
404 else if (texImage->TexFormat == &_mesa_texformat_rgb565) {
405 rrb->cpp = 2;
406 rrb->base._ActualFormat = GL_RGB5;
407 rrb->base._BaseFormat = GL_RGB;
408 rrb->base.DataType = GL_UNSIGNED_SHORT;
409 DBG("Render to RGB5 texture OK\n");
410 }
411 else if (texImage->TexFormat == &_mesa_texformat_z16) {
412 rrb->cpp = 2;
413 rrb->base._ActualFormat = GL_DEPTH_COMPONENT16;
414 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
415 rrb->base.DataType = GL_UNSIGNED_SHORT;
416 DBG("Render to DEPTH16 texture OK\n");
417 }
418 else if (texImage->TexFormat == &_mesa_texformat_s8_z24) {
419 rrb->cpp = 4;
420 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
421 rrb->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
422 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
423 DBG("Render to DEPTH_STENCIL texture OK\n");
424 }
425 else {
426 /* try redoing the FBO */
427 if (retry == 1) {
428 DBG("Render to texture BAD FORMAT %d\n",
429 texImage->TexFormat->MesaFormat);
430 return GL_FALSE;
431 }
432 texImage->TexFormat = radeonChooseTextureFormat(ctx, texImage->InternalFormat, 0,
433 texImage->TexFormat->DataType,
434 1);
435
436 retry++;
437 goto restart;
438 }
439
440 rrb->pitch = texImage->Width * rrb->cpp;
441 rrb->base.InternalFormat = rrb->base._ActualFormat;
442 rrb->base.Width = texImage->Width;
443 rrb->base.Height = texImage->Height;
444 rrb->base.RedBits = texImage->TexFormat->RedBits;
445 rrb->base.GreenBits = texImage->TexFormat->GreenBits;
446 rrb->base.BlueBits = texImage->TexFormat->BlueBits;
447 rrb->base.AlphaBits = texImage->TexFormat->AlphaBits;
448 rrb->base.DepthBits = texImage->TexFormat->DepthBits;
449
450 rrb->base.Delete = radeon_delete_renderbuffer;
451 rrb->base.AllocStorage = radeon_nop_alloc_storage;
452
453 return GL_TRUE;
454 }
455
456
457 static struct radeon_renderbuffer *
458 radeon_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
459 {
460 const GLuint name = ~0; /* not significant, but distinct for debugging */
461 struct radeon_renderbuffer *rrb;
462
463 /* make an radeon_renderbuffer to wrap the texture image */
464 rrb = CALLOC_STRUCT(radeon_renderbuffer);
465 if (!rrb) {
466 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
467 return NULL;
468 }
469
470 _mesa_init_renderbuffer(&rrb->base, name);
471 rrb->base.ClassID = RADEON_RB_CLASS;
472
473 if (!radeon_update_wrapper(ctx, rrb, texImage)) {
474 _mesa_free(rrb);
475 return NULL;
476 }
477
478 return rrb;
479
480 }
481 static void
482 radeon_render_texture(GLcontext * ctx,
483 struct gl_framebuffer *fb,
484 struct gl_renderbuffer_attachment *att)
485 {
486 struct gl_texture_image *newImage
487 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
488 struct radeon_renderbuffer *rrb = radeon_renderbuffer(att->Renderbuffer);
489 radeon_texture_image *radeon_image;
490 GLuint imageOffset;
491
492 (void) fb;
493
494 ASSERT(newImage);
495
496 if (newImage->Border != 0) {
497 /* Fallback on drawing to a texture with a border, which won't have a
498 * miptree.
499 */
500 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
501 _mesa_render_texture(ctx, fb, att);
502 return;
503 }
504 else if (!rrb) {
505 rrb = radeon_wrap_texture(ctx, newImage);
506 if (rrb) {
507 /* bind the wrapper to the attachment point */
508 _mesa_reference_renderbuffer(&att->Renderbuffer, &rrb->base);
509 }
510 else {
511 /* fallback to software rendering */
512 _mesa_render_texture(ctx, fb, att);
513 return;
514 }
515 }
516
517 if (!radeon_update_wrapper(ctx, rrb, newImage)) {
518 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
519 _mesa_render_texture(ctx, fb, att);
520 return;
521 }
522
523 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
524 _glthread_GetID(),
525 att->Texture->Name, newImage->Width, newImage->Height,
526 rrb->base.RefCount);
527
528 /* point the renderbufer's region to the texture image region */
529 radeon_image = (radeon_texture_image *)newImage;
530 if (rrb->bo != radeon_image->mt->bo) {
531 if (rrb->bo)
532 radeon_bo_unref(rrb->bo);
533 rrb->bo = radeon_image->mt->bo;
534 radeon_bo_ref(rrb->bo);
535 }
536
537 /* compute offset of the particular 2D image within the texture region */
538 imageOffset = radeon_miptree_image_offset(radeon_image->mt,
539 att->CubeMapFace,
540 att->TextureLevel);
541
542 if (att->Texture->Target == GL_TEXTURE_3D) {
543 GLuint offsets[6];
544 radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
545 offsets);
546 imageOffset += offsets[att->Zoffset];
547 }
548
549 /* store that offset in the region */
550 rrb->draw_offset = imageOffset;
551
552 /* update drawing region, etc */
553 radeon_draw_buffer(ctx, fb);
554 }
555
556 static void
557 radeon_finish_render_texture(GLcontext * ctx,
558 struct gl_renderbuffer_attachment *att)
559 {
560
561 }
562 static void
563 radeon_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
564 {
565 }
566
567 static void
568 radeon_blit_framebuffer(GLcontext *ctx,
569 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
570 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
571 GLbitfield mask, GLenum filter)
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 = radeon_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 }