intel: softwareBuffer in intel_alloc_renderbuffer_storage was always false, remove
[mesa.git] / src / mesa / drivers / dri / intel / intel_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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 "intel_context.h"
40 #include "intel_batchbuffer.h"
41 #include "intel_buffers.h"
42 #include "intel_fbo.h"
43 #include "intel_mipmap_tree.h"
44 #include "intel_regions.h"
45
46
47 #define FILE_DEBUG_FLAG DEBUG_FBO
48
49
50 /**
51 * Create a new framebuffer object.
52 */
53 static struct gl_framebuffer *
54 intel_new_framebuffer(GLcontext * ctx, GLuint name)
55 {
56 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
57 * class
58 */
59 return _mesa_new_framebuffer(ctx, name);
60 }
61
62
63 /** Called by gl_renderbuffer::Delete() */
64 static void
65 intel_delete_renderbuffer(struct gl_renderbuffer *rb)
66 {
67 GET_CURRENT_CONTEXT(ctx);
68 struct intel_context *intel = intel_context(ctx);
69 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
70
71 ASSERT(irb);
72
73 if (irb->span_cache != NULL)
74 _mesa_free(irb->span_cache);
75
76 if (intel && irb->region) {
77 intel_region_release(&irb->region);
78 }
79
80 _mesa_free(irb);
81 }
82
83
84 /**
85 * Return a pointer to a specific pixel in a renderbuffer.
86 */
87 static void *
88 intel_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
89 GLint x, GLint y)
90 {
91 /* By returning NULL we force all software rendering to go through
92 * the span routines.
93 */
94 return NULL;
95 }
96
97
98 /**
99 * Called via glRenderbufferStorageEXT() to set the format and allocate
100 * storage for a user-created renderbuffer.
101 */
102 static GLboolean
103 intel_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
104 GLenum internalFormat,
105 GLuint width, GLuint height)
106 {
107 struct intel_context *intel = intel_context(ctx);
108 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
109 int cpp;
110 GLuint pitch;
111
112 ASSERT(rb->Name != 0);
113
114 switch (internalFormat) {
115 case GL_R3_G3_B2:
116 case GL_RGB4:
117 case GL_RGB5:
118 rb->Format = MESA_FORMAT_RGB565;
119 rb->DataType = GL_UNSIGNED_BYTE;
120 cpp = 2;
121 break;
122 case GL_RGB:
123 case GL_RGB8:
124 case GL_RGB10:
125 case GL_RGB12:
126 case GL_RGB16:
127 rb->Format = MESA_FORMAT_ARGB8888; /* XXX: Need xrgb8888 */
128 rb->DataType = GL_UNSIGNED_BYTE;
129 cpp = 4;
130 break;
131 case GL_RGBA:
132 case GL_RGBA2:
133 case GL_RGBA4:
134 case GL_RGB5_A1:
135 case GL_RGBA8:
136 case GL_RGB10_A2:
137 case GL_RGBA12:
138 case GL_RGBA16:
139 rb->Format = MESA_FORMAT_ARGB8888;
140 rb->DataType = GL_UNSIGNED_BYTE;
141 cpp = 4;
142 break;
143 case GL_STENCIL_INDEX:
144 case GL_STENCIL_INDEX1_EXT:
145 case GL_STENCIL_INDEX4_EXT:
146 case GL_STENCIL_INDEX8_EXT:
147 case GL_STENCIL_INDEX16_EXT:
148 /* alloc a depth+stencil buffer */
149 rb->Format = MESA_FORMAT_S8_Z24;
150 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
151 cpp = 4;
152 break;
153 case GL_DEPTH_COMPONENT16:
154 rb->Format = MESA_FORMAT_Z16;
155 rb->DataType = GL_UNSIGNED_SHORT;
156 cpp = 2;
157 break;
158 case GL_DEPTH_COMPONENT:
159 case GL_DEPTH_COMPONENT24:
160 case GL_DEPTH_COMPONENT32:
161 rb->Format = MESA_FORMAT_S8_Z24;
162 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
163 cpp = 4;
164 break;
165 case GL_DEPTH_STENCIL_EXT:
166 case GL_DEPTH24_STENCIL8_EXT:
167 rb->Format = MESA_FORMAT_S8_Z24;
168 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
169 cpp = 4;
170 break;
171 default:
172 _mesa_problem(ctx,
173 "Unexpected format in intel_alloc_renderbuffer_storage");
174 return GL_FALSE;
175 }
176
177 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
178
179 intelFlush(ctx);
180
181 /* free old region */
182 if (irb->region) {
183 intel_region_release(&irb->region);
184 }
185
186 /* allocate new memory region/renderbuffer */
187
188 /* Choose a pitch to match hardware requirements:
189 */
190 pitch = ((cpp * width + 63) & ~63) / cpp;
191
192 /* alloc hardware renderbuffer */
193 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width, height, pitch);
194
195 irb->region = intel_region_alloc(intel, I915_TILING_NONE, cpp,
196 width, height, pitch, GL_TRUE);
197 if (!irb->region)
198 return GL_FALSE; /* out of memory? */
199
200 ASSERT(irb->region->buffer);
201
202 rb->Width = width;
203 rb->Height = height;
204
205 return GL_TRUE;
206 }
207
208
209 /**
210 * Called for each hardware renderbuffer when a _window_ is resized.
211 * Just update fields.
212 * Not used for user-created renderbuffers!
213 */
214 static GLboolean
215 intel_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
216 GLenum internalFormat, GLuint width, GLuint height)
217 {
218 ASSERT(rb->Name == 0);
219 rb->Width = width;
220 rb->Height = height;
221 rb->InternalFormat = internalFormat;
222
223 return GL_TRUE;
224 }
225
226
227 static void
228 intel_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
229 GLuint width, GLuint height)
230 {
231 struct intel_framebuffer *intel_fb = (struct intel_framebuffer*)fb;
232 int i;
233
234 _mesa_resize_framebuffer(ctx, fb, width, height);
235
236 fb->Initialized = GL_TRUE; /* XXX remove someday */
237
238 if (fb->Name != 0) {
239 return;
240 }
241
242 /* Make sure all window system renderbuffers are up to date */
243 for (i = 0; i < 2; i++) {
244 struct gl_renderbuffer *rb = &intel_fb->color_rb[i]->Base;
245
246 /* only resize if size is changing */
247 if (rb && (rb->Width != width || rb->Height != height)) {
248 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
249 }
250 }
251 }
252
253
254 /** Dummy function for gl_renderbuffer::AllocStorage() */
255 static GLboolean
256 intel_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
257 GLenum internalFormat, GLuint width, GLuint height)
258 {
259 _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
260 return GL_FALSE;
261 }
262
263
264 void
265 intel_renderbuffer_set_region(struct intel_renderbuffer *rb,
266 struct intel_region *region)
267 {
268 struct intel_region *old;
269
270 old = rb->region;
271 rb->region = NULL;
272 intel_region_reference(&rb->region, region);
273 intel_region_release(&old);
274 }
275
276
277 /**
278 * Create a new intel_renderbuffer which corresponds to an on-screen window,
279 * not a user-created renderbuffer.
280 */
281 struct intel_renderbuffer *
282 intel_create_renderbuffer(gl_format format)
283 {
284 GET_CURRENT_CONTEXT(ctx);
285
286 struct intel_renderbuffer *irb;
287 const GLuint name = 0;
288
289 irb = CALLOC_STRUCT(intel_renderbuffer);
290 if (!irb) {
291 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
292 return NULL;
293 }
294
295 _mesa_init_renderbuffer(&irb->Base, name);
296 irb->Base.ClassID = INTEL_RB_CLASS;
297
298 switch (format) {
299 case MESA_FORMAT_RGB565:
300 irb->Base._BaseFormat = GL_RGB;
301 irb->Base.DataType = GL_UNSIGNED_BYTE;
302 break;
303 case MESA_FORMAT_XRGB8888:
304 /* XXX this is a hack since XRGB surfaces don't seem to work
305 * properly yet. Reading the alpha channel returns 0 instead of 1.
306 */
307 format = MESA_FORMAT_ARGB8888;
308 irb->Base._BaseFormat = GL_RGB;
309 irb->Base.DataType = GL_UNSIGNED_BYTE;
310 break;
311 case MESA_FORMAT_ARGB8888:
312 irb->Base._BaseFormat = GL_RGBA;
313 irb->Base.DataType = GL_UNSIGNED_BYTE;
314 break;
315 case MESA_FORMAT_Z16:
316 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
317 irb->Base.DataType = GL_UNSIGNED_SHORT;
318 break;
319 case MESA_FORMAT_X8_Z24:
320 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
321 irb->Base.DataType = GL_UNSIGNED_INT;
322 break;
323 case MESA_FORMAT_S8_Z24:
324 irb->Base._BaseFormat = GL_DEPTH_STENCIL;
325 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
326 break;
327 default:
328 _mesa_problem(NULL,
329 "Unexpected intFormat in intel_create_renderbuffer");
330 _mesa_free(irb);
331 return NULL;
332 }
333
334 irb->Base.Format = format;
335 irb->Base.InternalFormat = irb->Base._BaseFormat;
336
337 /* intel-specific methods */
338 irb->Base.Delete = intel_delete_renderbuffer;
339 irb->Base.AllocStorage = intel_alloc_window_storage;
340 irb->Base.GetPointer = intel_get_pointer;
341
342 return irb;
343 }
344
345
346 /**
347 * Create a new renderbuffer object.
348 * Typically called via glBindRenderbufferEXT().
349 */
350 static struct gl_renderbuffer *
351 intel_new_renderbuffer(GLcontext * ctx, GLuint name)
352 {
353 /*struct intel_context *intel = intel_context(ctx); */
354 struct intel_renderbuffer *irb;
355
356 irb = CALLOC_STRUCT(intel_renderbuffer);
357 if (!irb) {
358 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
359 return NULL;
360 }
361
362 _mesa_init_renderbuffer(&irb->Base, name);
363 irb->Base.ClassID = INTEL_RB_CLASS;
364
365 /* intel-specific methods */
366 irb->Base.Delete = intel_delete_renderbuffer;
367 irb->Base.AllocStorage = intel_alloc_renderbuffer_storage;
368 irb->Base.GetPointer = intel_get_pointer;
369 /* span routines set in alloc_storage function */
370
371 return &irb->Base;
372 }
373
374
375 /**
376 * Called via glBindFramebufferEXT().
377 */
378 static void
379 intel_bind_framebuffer(GLcontext * ctx, GLenum target,
380 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
381 {
382 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
383 intel_draw_buffer(ctx, fb);
384 }
385 else {
386 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
387 }
388 }
389
390
391 /**
392 * Called via glFramebufferRenderbufferEXT().
393 */
394 static void
395 intel_framebuffer_renderbuffer(GLcontext * ctx,
396 struct gl_framebuffer *fb,
397 GLenum attachment, struct gl_renderbuffer *rb)
398 {
399 DBG("Intel FramebufferRenderbuffer %u %u\n", fb->Name, rb ? rb->Name : 0);
400
401 intelFlush(ctx);
402
403 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
404 intel_draw_buffer(ctx, fb);
405 }
406
407
408 static GLboolean
409 intel_update_wrapper(GLcontext *ctx, struct intel_renderbuffer *irb,
410 struct gl_texture_image *texImage)
411 {
412 gl_format texFormat;
413
414 if (texImage->TexFormat == MESA_FORMAT_ARGB8888) {
415 irb->Base.DataType = GL_UNSIGNED_BYTE;
416 DBG("Render to RGBA8 texture OK\n");
417 }
418 else if (texImage->TexFormat == MESA_FORMAT_XRGB8888) {
419 irb->Base.DataType = GL_UNSIGNED_BYTE;
420 DBG("Render to XGBA8 texture OK\n");
421 }
422 else if (texImage->TexFormat == MESA_FORMAT_RGB565) {
423 irb->Base.DataType = GL_UNSIGNED_BYTE;
424 DBG("Render to RGB5 texture OK\n");
425 }
426 else if (texImage->TexFormat == MESA_FORMAT_ARGB1555) {
427 irb->Base.DataType = GL_UNSIGNED_BYTE;
428 DBG("Render to ARGB1555 texture OK\n");
429 }
430 else if (texImage->TexFormat == MESA_FORMAT_ARGB4444) {
431 irb->Base.DataType = GL_UNSIGNED_BYTE;
432 DBG("Render to ARGB4444 texture OK\n");
433 }
434 else if (texImage->TexFormat == MESA_FORMAT_Z16) {
435 irb->Base.DataType = GL_UNSIGNED_SHORT;
436 DBG("Render to DEPTH16 texture OK\n");
437 }
438 else if (texImage->TexFormat == MESA_FORMAT_S8_Z24) {
439 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
440 DBG("Render to DEPTH_STENCIL texture OK\n");
441 }
442 else {
443 DBG("Render to texture BAD FORMAT %d\n", texImage->TexFormat);
444 return GL_FALSE;
445 }
446
447 irb->Base.Format = texImage->TexFormat;
448
449 texFormat = texImage->TexFormat;
450
451 irb->Base.InternalFormat = texImage->InternalFormat;
452 irb->Base._BaseFormat = _mesa_base_fbo_format(ctx, irb->Base.InternalFormat);
453 irb->Base.Width = texImage->Width;
454 irb->Base.Height = texImage->Height;
455
456 irb->Base.Delete = intel_delete_renderbuffer;
457 irb->Base.AllocStorage = intel_nop_alloc_storage;
458
459 return GL_TRUE;
460 }
461
462
463 /**
464 * When glFramebufferTexture[123]D is called this function sets up the
465 * gl_renderbuffer wrapper around the texture image.
466 * This will have the region info needed for hardware rendering.
467 */
468 static struct intel_renderbuffer *
469 intel_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
470 {
471 const GLuint name = ~0; /* not significant, but distinct for debugging */
472 struct intel_renderbuffer *irb;
473
474 /* make an intel_renderbuffer to wrap the texture image */
475 irb = CALLOC_STRUCT(intel_renderbuffer);
476 if (!irb) {
477 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
478 return NULL;
479 }
480
481 _mesa_init_renderbuffer(&irb->Base, name);
482 irb->Base.ClassID = INTEL_RB_CLASS;
483
484 if (!intel_update_wrapper(ctx, irb, texImage)) {
485 _mesa_free(irb);
486 return NULL;
487 }
488
489 return irb;
490 }
491
492
493 /**
494 * Called by glFramebufferTexture[123]DEXT() (and other places) to
495 * prepare for rendering into texture memory. This might be called
496 * many times to choose different texture levels, cube faces, etc
497 * before intel_finish_render_texture() is ever called.
498 */
499 static void
500 intel_render_texture(GLcontext * ctx,
501 struct gl_framebuffer *fb,
502 struct gl_renderbuffer_attachment *att)
503 {
504 struct gl_texture_image *newImage
505 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
506 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
507 struct intel_texture_image *intel_image;
508 GLuint dst_x, dst_y;
509
510 (void) fb;
511
512 ASSERT(newImage);
513
514 intel_image = intel_texture_image(newImage);
515 if (!intel_image->mt) {
516 /* Fallback on drawing to a texture that doesn't have a miptree
517 * (has a border, width/height 0, etc.)
518 */
519 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
520 _mesa_render_texture(ctx, fb, att);
521 return;
522 }
523 else if (!irb) {
524 irb = intel_wrap_texture(ctx, newImage);
525 if (irb) {
526 /* bind the wrapper to the attachment point */
527 _mesa_reference_renderbuffer(&att->Renderbuffer, &irb->Base);
528 }
529 else {
530 /* fallback to software rendering */
531 _mesa_render_texture(ctx, fb, att);
532 return;
533 }
534 }
535
536 if (!intel_update_wrapper(ctx, irb, newImage)) {
537 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
538 _mesa_render_texture(ctx, fb, att);
539 return;
540 }
541
542 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
543 _glthread_GetID(),
544 att->Texture->Name, newImage->Width, newImage->Height,
545 irb->Base.RefCount);
546
547 /* point the renderbufer's region to the texture image region */
548 if (irb->region != intel_image->mt->region) {
549 if (irb->region)
550 intel_region_release(&irb->region);
551 intel_region_reference(&irb->region, intel_image->mt->region);
552 }
553
554 /* compute offset of the particular 2D image within the texture region */
555 intel_miptree_get_image_offset(intel_image->mt,
556 att->TextureLevel,
557 att->CubeMapFace,
558 att->Zoffset,
559 &dst_x, &dst_y);
560
561 intel_image->mt->region->draw_offset = (dst_y * intel_image->mt->pitch +
562 dst_x) * intel_image->mt->cpp;
563 intel_image->mt->region->draw_x = dst_x;
564 intel_image->mt->region->draw_y = dst_y;
565 intel_image->used_as_render_target = GL_TRUE;
566
567 /* update drawing region, etc */
568 intel_draw_buffer(ctx, fb);
569 }
570
571
572 /**
573 * Called by Mesa when rendering to a texture is done.
574 */
575 static void
576 intel_finish_render_texture(GLcontext * ctx,
577 struct gl_renderbuffer_attachment *att)
578 {
579 struct intel_context *intel = intel_context(ctx);
580 struct gl_texture_object *tex_obj = att->Texture;
581 struct gl_texture_image *image =
582 tex_obj->Image[att->CubeMapFace][att->TextureLevel];
583 struct intel_texture_image *intel_image = intel_texture_image(image);
584
585 /* Flag that this image may now be validated into the object's miptree. */
586 intel_image->used_as_render_target = GL_FALSE;
587
588 /* Since we've (probably) rendered to the texture and will (likely) use
589 * it in the texture domain later on in this batchbuffer, flush the
590 * batch. Once again, we wish for a domain tracker in libdrm to cover
591 * usage inside of a batchbuffer like GEM does in the kernel.
592 */
593 intel_batchbuffer_emit_mi_flush(intel->batch);
594 }
595
596 /**
597 * Do additional "completeness" testing of a framebuffer object.
598 */
599 static void
600 intel_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
601 {
602 const struct intel_renderbuffer *depthRb =
603 intel_get_renderbuffer(fb, BUFFER_DEPTH);
604 const struct intel_renderbuffer *stencilRb =
605 intel_get_renderbuffer(fb, BUFFER_STENCIL);
606 int i;
607
608 if (stencilRb && stencilRb != depthRb) {
609 /* we only support combined depth/stencil buffers, not separate
610 * stencil buffers.
611 */
612 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
613 }
614
615 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
616 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
617 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
618
619 if (rb == NULL)
620 continue;
621
622 if (irb == NULL) {
623 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
624 continue;
625 }
626
627 switch (irb->Base.Format) {
628 case MESA_FORMAT_ARGB8888:
629 case MESA_FORMAT_XRGB8888:
630 case MESA_FORMAT_RGB565:
631 case MESA_FORMAT_ARGB1555:
632 case MESA_FORMAT_ARGB4444:
633 break;
634 default:
635 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
636 }
637 }
638 }
639
640
641 /**
642 * Do one-time context initializations related to GL_EXT_framebuffer_object.
643 * Hook in device driver functions.
644 */
645 void
646 intel_fbo_init(struct intel_context *intel)
647 {
648 intel->ctx.Driver.NewFramebuffer = intel_new_framebuffer;
649 intel->ctx.Driver.NewRenderbuffer = intel_new_renderbuffer;
650 intel->ctx.Driver.BindFramebuffer = intel_bind_framebuffer;
651 intel->ctx.Driver.FramebufferRenderbuffer = intel_framebuffer_renderbuffer;
652 intel->ctx.Driver.RenderTexture = intel_render_texture;
653 intel->ctx.Driver.FinishRenderTexture = intel_finish_render_texture;
654 intel->ctx.Driver.ResizeBuffers = intel_resize_buffers;
655 intel->ctx.Driver.ValidateFramebuffer = intel_validate_framebuffer;
656 intel->ctx.Driver.BlitFramebuffer = _mesa_meta_BlitFramebuffer;
657 }