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