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