drivers: don't include texformat.h
[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->_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_DEPTH_COMPONENT24;
150 rb->DataType = GL_UNSIGNED_INT;
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 if (ctx->Driver.Flush)
169 ctx->Driver.Flush(ctx); /* +r6/r7 */
170
171 if (rrb->bo)
172 radeon_bo_unref(rrb->bo);
173
174
175 if (software_buffer) {
176 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
177 width, height);
178 }
179 else {
180 uint32_t size;
181 uint32_t pitch = ((cpp * width + 63) & ~63) / cpp;
182
183 fprintf(stderr,"Allocating %d x %d radeon RBO (pitch %d)\n", width,
184 height, pitch);
185
186 size = pitch * height * cpp;
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 gl_format texFormat;
390
391 restart:
392 if (texImage->TexFormat == MESA_FORMAT_ARGB8888) {
393 rrb->cpp = 4;
394 rrb->base._ActualFormat = GL_RGBA8;
395 rrb->base._BaseFormat = GL_RGBA;
396 rrb->base.DataType = GL_UNSIGNED_BYTE;
397 DBG("Render to RGBA8 texture OK\n");
398 }
399 else if (texImage->TexFormat == MESA_FORMAT_RGB565) {
400 rrb->cpp = 2;
401 rrb->base._ActualFormat = GL_RGB5;
402 rrb->base._BaseFormat = GL_RGB;
403 rrb->base.DataType = GL_UNSIGNED_BYTE;
404 DBG("Render to RGB5 texture OK\n");
405 }
406 else if (texImage->TexFormat == MESA_FORMAT_ARGB1555) {
407 rrb->cpp = 2;
408 rrb->base._ActualFormat = GL_RGB5_A1;
409 rrb->base._BaseFormat = GL_RGBA;
410 rrb->base.DataType = GL_UNSIGNED_BYTE;
411 DBG("Render to ARGB1555 texture OK\n");
412 }
413 else if (texImage->TexFormat == MESA_FORMAT_ARGB4444) {
414 rrb->cpp = 2;
415 rrb->base._ActualFormat = GL_RGBA4;
416 rrb->base._BaseFormat = GL_RGBA;
417 rrb->base.DataType = GL_UNSIGNED_BYTE;
418 DBG("Render to ARGB1555 texture OK\n");
419 }
420 else if (texImage->TexFormat == MESA_FORMAT_Z16) {
421 rrb->cpp = 2;
422 rrb->base._ActualFormat = GL_DEPTH_COMPONENT16;
423 rrb->base._BaseFormat = GL_DEPTH_COMPONENT;
424 rrb->base.DataType = GL_UNSIGNED_SHORT;
425 DBG("Render to DEPTH16 texture OK\n");
426 }
427 else if (texImage->TexFormat == MESA_FORMAT_S8_Z24) {
428 rrb->cpp = 4;
429 rrb->base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
430 rrb->base._BaseFormat = GL_DEPTH_STENCIL_EXT;
431 rrb->base.DataType = GL_UNSIGNED_INT_24_8_EXT;
432 DBG("Render to DEPTH_STENCIL texture OK\n");
433 }
434 else {
435 /* try redoing the FBO */
436 if (retry == 1) {
437 DBG("Render to texture BAD FORMAT %d\n",
438 texImage->TexFormat);
439 return GL_FALSE;
440 }
441 texImage->TexFormat = radeonChooseTextureFormat(ctx, texImage->InternalFormat, 0,
442 _mesa_get_format_datatype(texImage->TexFormat),
443 1);
444
445 retry++;
446 goto restart;
447 }
448
449 texFormat = texImage->TexFormat;
450
451 rrb->pitch = texImage->Width * rrb->cpp;
452 rrb->base.InternalFormat = rrb->base._ActualFormat;
453 rrb->base.Width = texImage->Width;
454 rrb->base.Height = texImage->Height;
455 rrb->base.RedBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_RED_SIZE);
456 rrb->base.GreenBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_GREEN_SIZE);
457 rrb->base.BlueBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_BLUE_SIZE);
458 rrb->base.AlphaBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_ALPHA_SIZE);
459 rrb->base.DepthBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_DEPTH_SIZE_ARB);
460 rrb->base.StencilBits = _mesa_get_format_bits(texFormat, GL_TEXTURE_STENCIL_SIZE_EXT);
461
462 rrb->base.Delete = radeon_delete_renderbuffer;
463 rrb->base.AllocStorage = radeon_nop_alloc_storage;
464
465 return GL_TRUE;
466 }
467
468
469 static struct radeon_renderbuffer *
470 radeon_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
471 {
472 const GLuint name = ~0; /* not significant, but distinct for debugging */
473 struct radeon_renderbuffer *rrb;
474
475 /* make an radeon_renderbuffer to wrap the texture image */
476 rrb = CALLOC_STRUCT(radeon_renderbuffer);
477 if (!rrb) {
478 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
479 return NULL;
480 }
481
482 _mesa_init_renderbuffer(&rrb->base, name);
483 rrb->base.ClassID = RADEON_RB_CLASS;
484
485 if (!radeon_update_wrapper(ctx, rrb, texImage)) {
486 _mesa_free(rrb);
487 return NULL;
488 }
489
490 return rrb;
491
492 }
493 static void
494 radeon_render_texture(GLcontext * ctx,
495 struct gl_framebuffer *fb,
496 struct gl_renderbuffer_attachment *att)
497 {
498 struct gl_texture_image *newImage
499 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
500 struct radeon_renderbuffer *rrb = radeon_renderbuffer(att->Renderbuffer);
501 radeon_texture_image *radeon_image;
502 GLuint imageOffset;
503
504 (void) fb;
505
506 ASSERT(newImage);
507
508 if (newImage->Border != 0) {
509 /* Fallback on drawing to a texture with a border, which won't have a
510 * miptree.
511 */
512 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
513 _mesa_render_texture(ctx, fb, att);
514 return;
515 }
516 else if (!rrb) {
517 rrb = radeon_wrap_texture(ctx, newImage);
518 if (rrb) {
519 /* bind the wrapper to the attachment point */
520 _mesa_reference_renderbuffer(&att->Renderbuffer, &rrb->base);
521 }
522 else {
523 /* fallback to software rendering */
524 _mesa_render_texture(ctx, fb, att);
525 return;
526 }
527 }
528
529 if (!radeon_update_wrapper(ctx, rrb, newImage)) {
530 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
531 _mesa_render_texture(ctx, fb, att);
532 return;
533 }
534
535 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
536 _glthread_GetID(),
537 att->Texture->Name, newImage->Width, newImage->Height,
538 rrb->base.RefCount);
539
540 /* point the renderbufer's region to the texture image region */
541 radeon_image = (radeon_texture_image *)newImage;
542 if (rrb->bo != radeon_image->mt->bo) {
543 if (rrb->bo)
544 radeon_bo_unref(rrb->bo);
545 rrb->bo = radeon_image->mt->bo;
546 radeon_bo_ref(rrb->bo);
547 }
548
549 /* compute offset of the particular 2D image within the texture region */
550 imageOffset = radeon_miptree_image_offset(radeon_image->mt,
551 att->CubeMapFace,
552 att->TextureLevel);
553
554 if (att->Texture->Target == GL_TEXTURE_3D) {
555 GLuint offsets[6];
556 radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
557 offsets);
558 imageOffset += offsets[att->Zoffset];
559 }
560
561 /* store that offset in the region */
562 rrb->draw_offset = imageOffset;
563
564 /* update drawing region, etc */
565 radeon_draw_buffer(ctx, fb);
566 }
567
568 static void
569 radeon_finish_render_texture(GLcontext * ctx,
570 struct gl_renderbuffer_attachment *att)
571 {
572
573 }
574 static void
575 radeon_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
576 {
577 }
578
579 void radeon_fbo_init(struct radeon_context *radeon)
580 {
581 radeon->glCtx->Driver.NewFramebuffer = radeon_new_framebuffer;
582 radeon->glCtx->Driver.NewRenderbuffer = radeon_new_renderbuffer;
583 radeon->glCtx->Driver.BindFramebuffer = radeon_bind_framebuffer;
584 radeon->glCtx->Driver.FramebufferRenderbuffer = radeon_framebuffer_renderbuffer;
585 radeon->glCtx->Driver.RenderTexture = radeon_render_texture;
586 radeon->glCtx->Driver.FinishRenderTexture = radeon_finish_render_texture;
587 radeon->glCtx->Driver.ResizeBuffers = radeon_resize_buffers;
588 radeon->glCtx->Driver.ValidateFramebuffer = radeon_validate_framebuffer;
589 radeon->glCtx->Driver.BlitFramebuffer = _mesa_meta_BlitFramebuffer;
590 }
591
592
593 void radeon_renderbuffer_set_bo(struct radeon_renderbuffer *rb,
594 struct radeon_bo *bo)
595 {
596 struct radeon_bo *old;
597 old = rb->bo;
598 rb->bo = bo;
599 radeon_bo_ref(bo);
600 if (old)
601 radeon_bo_unref(old);
602 }