radeon: enable tiling fallbacks in 3D driver.
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_common_context.c
index f8a29fdba083d93b94db67b9e463dd9ab8ceb5cc..5e744f9deb724490a7dc621c8217c1f0bef896f5 100644 (file)
@@ -39,18 +39,68 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "drirenderbuffer.h"
 #include "main/context.h"
 #include "main/framebuffer.h"
+#include "main/renderbuffer.h"
 #include "main/state.h"
 #include "main/simple_list.h"
 #include "swrast/swrast.h"
 #include "swrast_setup/swrast_setup.h"
 #include "tnl/tnl.h"
 
+#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R600) /* +r6/r7 */
+#include "r600_context.h"
+#endif
+
 #define DRIVER_DATE "20090101"
 
 #ifndef RADEON_DEBUG
 int RADEON_DEBUG = (0);
 #endif
 
+
+static const char* get_chip_family_name(int chip_family)
+{
+       switch(chip_family) {
+       case CHIP_FAMILY_R100: return "R100";
+       case CHIP_FAMILY_RV100: return "RV100";
+       case CHIP_FAMILY_RS100: return "RS100";
+       case CHIP_FAMILY_RV200: return "RV200";
+       case CHIP_FAMILY_RS200: return "RS200";
+       case CHIP_FAMILY_R200: return "R200";
+       case CHIP_FAMILY_RV250: return "RV250";
+       case CHIP_FAMILY_RS300: return "RS300";
+       case CHIP_FAMILY_RV280: return "RV280";
+       case CHIP_FAMILY_R300: return "R300";
+       case CHIP_FAMILY_R350: return "R350";
+       case CHIP_FAMILY_RV350: return "RV350";
+       case CHIP_FAMILY_RV380: return "RV380";
+       case CHIP_FAMILY_R420: return "R420";
+       case CHIP_FAMILY_RV410: return "RV410";
+       case CHIP_FAMILY_RS400: return "RS400";
+       case CHIP_FAMILY_RS600: return "RS600";
+       case CHIP_FAMILY_RS690: return "RS690";
+       case CHIP_FAMILY_RS740: return "RS740";
+       case CHIP_FAMILY_RV515: return "RV515";
+       case CHIP_FAMILY_R520: return "R520";
+       case CHIP_FAMILY_RV530: return "RV530";
+       case CHIP_FAMILY_R580: return "R580";
+       case CHIP_FAMILY_RV560: return "RV560";
+       case CHIP_FAMILY_RV570: return "RV570";
+       case CHIP_FAMILY_R600: return "R600";
+       case CHIP_FAMILY_RV610: return "RV610";
+       case CHIP_FAMILY_RV630: return "RV630";
+       case CHIP_FAMILY_RV670: return "RV670";
+       case CHIP_FAMILY_RV620: return "RV620";
+       case CHIP_FAMILY_RV635: return "RV635";
+       case CHIP_FAMILY_RS780: return "RS780";
+       case CHIP_FAMILY_RV770: return "RV770";
+       case CHIP_FAMILY_RV730: return "RV730";
+       case CHIP_FAMILY_RV710: return "RV710";
+       case CHIP_FAMILY_RV740: return "RV740";
+       default: return "unknown";
+       }
+}
+
+
 /* Return various strings for glGetString().
  */
 static const GLubyte *radeonGetString(GLcontext * ctx, GLenum name)
@@ -72,18 +122,24 @@ static const GLubyte *radeonGetString(GLcontext * ctx, GLenum name)
                unsigned offset;
                GLuint agp_mode = (radeon->radeonScreen->card_type==RADEON_CARD_PCI) ? 0 :
                        radeon->radeonScreen->AGPMode;
-               const char* chipname;
+               const char* chipclass;
+               char hardwarename[32];
 
                if (IS_R600_CLASS(radeon->radeonScreen))
-                       chipname = "R600";
+                       chipclass = "R600";
                else if (IS_R300_CLASS(radeon->radeonScreen))
-                       chipname = "R300";
+                       chipclass = "R300";
                else if (IS_R200_CLASS(radeon->radeonScreen))
-                       chipname = "R200";
+                       chipclass = "R200";
                else
-                       chipname = "R100";
+                       chipclass = "R100";
+
+               sprintf(hardwarename, "%s (%s %04X)",
+                       chipclass,
+                       get_chip_family_name(radeon->radeonScreen->chip_family),
+                       radeon->radeonScreen->device_id);
 
-               offset = driGetRendererString(buffer, chipname, DRIVER_DATE,
+               offset = driGetRendererString(buffer, hardwarename, DRIVER_DATE,
                                              agp_mode);
 
                if (IS_R600_CLASS(radeon->radeonScreen)) {
@@ -149,11 +205,10 @@ GLboolean radeonInitContext(radeonContextPtr radeon,
        ctx = radeon->glCtx;
        driContextPriv->driverPrivate = radeon;
 
+       meta_init_metaops(ctx, &radeon->meta);
        /* DRI fields */
        radeon->dri.context = driContextPriv;
        radeon->dri.screen = sPriv;
-       radeon->dri.drawable = NULL;
-       radeon->dri.readable = NULL;
        radeon->dri.hwContext = driContextPriv->hHWContext;
        radeon->dri.hwLock = &sPriv->pSAREA->lock;
        radeon->dri.fd = sPriv->fd;
@@ -166,8 +221,11 @@ GLboolean radeonInitContext(radeonContextPtr radeon,
        fthrottle_mode = driQueryOptioni(&radeon->optionCache, "fthrottle_mode");
        radeon->iw.irq_seq = -1;
        radeon->irqsEmitted = 0;
-       radeon->do_irqs = (fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
-                         radeon->radeonScreen->irq);
+       if (IS_R600_CLASS(radeon->radeonScreen))
+               radeon->do_irqs = 0;
+       else
+               radeon->do_irqs = (fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
+                                  radeon->radeonScreen->irq);
 
        radeon->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
 
@@ -176,13 +234,34 @@ GLboolean radeonInitContext(radeonContextPtr radeon,
                        "IRQ's not enabled, falling back to %s: %d %d\n",
                        radeon->do_usleeps ? "usleeps" : "busy waits",
                        fthrottle_mode, radeon->radeonScreen->irq);
-       
+
         radeon->texture_depth = driQueryOptioni (&radeon->optionCache,
                                                "texture_depth");
         if (radeon->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
                 radeon->texture_depth = ( glVisual->rgbBits > 16 ) ?
                DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
 
+       if (IS_R600_CLASS(radeon->radeonScreen)) {
+               radeon->texture_row_align = 256;
+               radeon->texture_rect_row_align = 256;
+               radeon->texture_compressed_row_align = 256;
+       } else if (IS_R200_CLASS(radeon->radeonScreen) ||
+                  IS_R100_CLASS(radeon->radeonScreen)) {
+               radeon->texture_row_align = 32;
+               radeon->texture_rect_row_align = 64;
+               radeon->texture_compressed_row_align = 32;
+       } else { /* R300 - not sure this is all correct */
+               int chip_family = radeon->radeonScreen->chip_family;
+               if (chip_family == CHIP_FAMILY_RS600 ||
+                   chip_family == CHIP_FAMILY_RS690 ||
+                   chip_family == CHIP_FAMILY_RS740)
+                       radeon->texture_row_align = 64;
+               else
+                       radeon->texture_row_align = 32;
+               radeon->texture_rect_row_align = 64;
+               radeon->texture_compressed_row_align = 64;
+       }
+
        return GL_TRUE;
 }
 
@@ -216,34 +295,44 @@ void radeonDestroyContext(__DRIcontextPrivate *driContextPriv )
        radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
        radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
 
+    /* +r6/r7 */
+    __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
+       radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
+    /* --------- */
+
        if (radeon == current) {
                radeon_firevertices(radeon);
                _mesa_make_current(NULL, NULL, NULL);
        }
-       
+
        assert(radeon);
-       if (radeon) {
+       if (radeon) 
+    {
+
+#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R600) /* +r6/r7 */
+           if (IS_R600_CLASS(screen))
+        {
+               r600DestroyContext(driContextPriv);
+        }
+#endif
 
                if (radeon->dma.current) {
                        rcommonFlushCmdBuf( radeon, __FUNCTION__ );
                }
 
                radeonReleaseArrays(radeon->glCtx, ~0);
-
+               meta_destroy_metaops(&radeon->meta);
                if (radeon->vtbl.free_context)
                        radeon->vtbl.free_context(radeon->glCtx);
                _swsetup_DestroyContext( radeon->glCtx );
                _tnl_DestroyContext( radeon->glCtx );
                _vbo_DestroyContext( radeon->glCtx );
                _swrast_DestroyContext( radeon->glCtx );
-       
-               radeonDestroyBuffer(radeon->dri.drawable);
-               radeonDestroyBuffer(radeon->dri.readable);
 
                /* free atom list */
                /* free the Mesa context */
                _mesa_destroy_context(radeon->glCtx);
-               
+
                /* _mesa_destroy_context() might result in calls to functions that
                 * depend on the DriverCtx, so don't set it to NULL before.
                 *
@@ -251,9 +340,12 @@ void radeonDestroyContext(__DRIcontextPrivate *driContextPriv )
                 */
                /* free the option cache */
                driDestroyOptionCache(&radeon->optionCache);
-               
+
                rcommonDestroyCmdBuf(radeon);
 
+#if RADEON_COMMON && defined(RADEON_COMMON_FOR_R600) /* +r6/r7 */
+           if (!IS_R600_CLASS(screen))
+#endif
                radeon_destroy_atom_list(radeon);
 
                if (radeon->state.scissor.pClipRects) {
@@ -294,48 +386,88 @@ radeon_make_kernel_renderbuffer_current(radeonContextPtr radeon,
 
        if ((rb = (void *)draw->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->frontOffset,
+                                               0,
+                                               0,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Front Buf");
+#else
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->frontOffset,
                                                0,
                                                0,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->frontPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->backOffset,
+                                               0,
+                                               0,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Back Buf");
+#else
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->backOffset,
                                                0,
                                                0,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->backPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_DEPTH].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->depthOffset,
+                                               0,
+                                               0,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Z Buf");
+#else
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->depthOffset,
                                                0,
                                                0,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->depthPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_STENCIL].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->depthOffset,
+                                               0,
+                                               0,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Stencil Buf");
+#else
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->depthOffset,
                                                0,
                                                0,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->depthPitch * rb->cpp;
@@ -349,15 +481,25 @@ radeon_make_renderbuffer_current(radeonContextPtr radeon,
        int size = 4096*4096*4;
        /* if radeon->fake */
        struct radeon_renderbuffer *rb;
-       
+
        if (radeon->radeonScreen->kernel_mm) {
                radeon_make_kernel_renderbuffer_current(radeon, draw);
                return;
        }
-                       
+
 
        if ((rb = (void *)draw->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->frontOffset +
+                                               radeon->radeonScreen->fbLocation,
+                                               size,
+                                               4096,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Front Buf");
+#else
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->frontOffset +
                                                radeon->radeonScreen->fbLocation,
@@ -365,12 +507,23 @@ radeon_make_renderbuffer_current(radeonContextPtr radeon,
                                                4096,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->frontPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->backOffset +
+                                               radeon->radeonScreen->fbLocation,
+                                               size,
+                                               4096,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Back Buf");
+#else
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->backOffset +
                                                radeon->radeonScreen->fbLocation,
@@ -378,44 +531,84 @@ radeon_make_renderbuffer_current(radeonContextPtr radeon,
                                                4096,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->backPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_DEPTH].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->depthOffset +
+                                               radeon->radeonScreen->fbLocation,
+                                               size,
+                                               4096,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Z Buf");
+#else
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->depthOffset +
                                                radeon->radeonScreen->fbLocation,
                                                size,
                                                4096,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->depthPitch * rb->cpp;
        }
        if ((rb = (void *)draw->base.Attachment[BUFFER_STENCIL].Renderbuffer)) {
                if (!rb->bo) {
+#ifdef RADEON_DEBUG_BO
                        rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               radeon->radeonScreen->depthOffset +
+                                               radeon->radeonScreen->fbLocation,
+                                               size,
+                                               4096,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               0,
+                        "Stencil Buf");
+#else
+            rb->bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                radeon->radeonScreen->depthOffset +
                                                radeon->radeonScreen->fbLocation,
                                                size,
                                                4096,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                0);
+#endif /* RADEON_DEBUG_BO */
                }
                rb->cpp = radeon->radeonScreen->cpp;
                rb->pitch = radeon->radeonScreen->depthPitch * rb->cpp;
        }
 }
 
+static unsigned
+radeon_bits_per_pixel(const struct radeon_renderbuffer *rb)
+{
+   switch (rb->base._ActualFormat) {
+   case GL_RGB5:
+   case GL_DEPTH_COMPONENT16:
+      return 16;
+   case GL_RGB8:
+   case GL_RGBA8:
+   case GL_DEPTH_COMPONENT24:
+   case GL_DEPTH24_STENCIL8_EXT:
+   case GL_STENCIL_INDEX8_EXT:
+      return 32;
+   default:
+      return 0;
+   }
+}
 
 void
 radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
 {
        unsigned int attachments[10];
-       __DRIbuffer *buffers;
+       __DRIbuffer *buffers = NULL;
        __DRIscreen *screen;
        struct radeon_renderbuffer *rb;
        int i, count;
@@ -426,26 +619,70 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
 
        if (RADEON_DEBUG & DEBUG_DRI)
            fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
-       
+
        draw = drawable->driverPrivate;
        screen = context->driScreenPriv;
        radeon = (radeonContextPtr) context->driverPrivate;
-       i = 0;
-       if (draw->color_rb[0])
-               attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
-       if (draw->color_rb[1])
-               attachments[i++] = __DRI_BUFFER_BACK_LEFT;
-       if (radeon_get_renderbuffer(&draw->base, BUFFER_DEPTH))
-               attachments[i++] = __DRI_BUFFER_DEPTH;
-       if (radeon_get_renderbuffer(&draw->base, BUFFER_STENCIL))
-               attachments[i++] = __DRI_BUFFER_STENCIL;
-       
-       buffers = (*screen->dri2.loader->getBuffers)(drawable,
-                                                    &drawable->w,
-                                                    &drawable->h,
-                                                    attachments, i,
-                                                    &count,
-                                                    drawable->loaderPrivate);
+
+       if (screen->dri2.loader
+          && (screen->dri2.loader->base.version > 2)
+          && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
+               struct radeon_renderbuffer *depth_rb;
+               struct radeon_renderbuffer *stencil_rb;
+
+               i = 0;
+               if ((radeon->is_front_buffer_rendering ||
+                    radeon->is_front_buffer_reading ||
+                    !draw->color_rb[1])
+                   && draw->color_rb[0]) {
+                       attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
+                       attachments[i++] = radeon_bits_per_pixel(draw->color_rb[0]);
+               }
+
+               if (draw->color_rb[1]) {
+                       attachments[i++] = __DRI_BUFFER_BACK_LEFT;
+                       attachments[i++] = radeon_bits_per_pixel(draw->color_rb[1]);
+               }
+
+               depth_rb = radeon_get_renderbuffer(&draw->base, BUFFER_DEPTH);
+               stencil_rb = radeon_get_renderbuffer(&draw->base, BUFFER_STENCIL);
+
+               if ((depth_rb != NULL) && (stencil_rb != NULL)) {
+                       attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
+                       attachments[i++] = radeon_bits_per_pixel(depth_rb);
+               } else if (depth_rb != NULL) {
+                       attachments[i++] = __DRI_BUFFER_DEPTH;
+                       attachments[i++] = radeon_bits_per_pixel(depth_rb);
+               } else if (stencil_rb != NULL) {
+                       attachments[i++] = __DRI_BUFFER_STENCIL;
+                       attachments[i++] = radeon_bits_per_pixel(stencil_rb);
+               }
+
+               buffers = (*screen->dri2.loader->getBuffersWithFormat)(drawable,
+                                                               &drawable->w,
+                                                               &drawable->h,
+                                                               attachments, i / 2,
+                                                               &count,
+                                                               drawable->loaderPrivate);
+       } else if (screen->dri2.loader) {
+               i = 0;
+               if (draw->color_rb[0])
+                       attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
+               if (draw->color_rb[1])
+                       attachments[i++] = __DRI_BUFFER_BACK_LEFT;
+               if (radeon_get_renderbuffer(&draw->base, BUFFER_DEPTH))
+                       attachments[i++] = __DRI_BUFFER_DEPTH;
+               if (radeon_get_renderbuffer(&draw->base, BUFFER_STENCIL))
+                       attachments[i++] = __DRI_BUFFER_STENCIL;
+
+               buffers = (*screen->dri2.loader->getBuffers)(drawable,
+                                                                &drawable->w,
+                                                                &drawable->h,
+                                                                attachments, i,
+                                                                &count,
+                                                                drawable->loaderPrivate);
+       }
+
        if (buffers == NULL)
                return;
 
@@ -470,6 +707,10 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
                        rb = draw->color_rb[0];
                        regname = "dri2 front buffer";
                        break;
+               case __DRI_BUFFER_FAKE_FRONT_LEFT:
+                       rb = draw->color_rb[0];
+                       regname = "dri2 fake front buffer";
+                       break;
                case __DRI_BUFFER_BACK_LEFT:
                        rb = draw->color_rb[1];
                        regname = "dri2 back buffer";
@@ -478,6 +719,10 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
                        rb = radeon_get_renderbuffer(&draw->base, BUFFER_DEPTH);
                        regname = "dri2 depth buffer";
                        break;
+               case __DRI_BUFFER_DEPTH_STENCIL:
+                       rb = radeon_get_renderbuffer(&draw->base, BUFFER_DEPTH);
+                       regname = "dri2 depth / stencil buffer";
+                       break;
                case __DRI_BUFFER_STENCIL:
                        rb = radeon_get_renderbuffer(&draw->base, BUFFER_STENCIL);
                        regname = "dri2 stencil buffer";
@@ -507,8 +752,8 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
 
                rb->cpp = buffers[i].cpp;
                rb->pitch = buffers[i].pitch;
-               rb->width = drawable->w;
-               rb->height = drawable->h;
+               rb->base.Width = drawable->w;
+               rb->base.Height = drawable->h;
                rb->has_surface = 0;
 
                if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_bo) {
@@ -517,18 +762,37 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
                        bo = depth_bo;
                        radeon_bo_ref(bo);
                } else {
+                       uint32_t tiling_flags = 0, pitch = 0;
+                       int ret;
+#ifdef RADEON_DEBUG_BO
+                       bo = radeon_bo_open(radeon->radeonScreen->bom,
+                                               buffers[i].name,
+                                               0,
+                                               0,
+                                               RADEON_GEM_DOMAIN_VRAM,
+                                               buffers[i].flags,
+                        regname);
+#else
                        bo = radeon_bo_open(radeon->radeonScreen->bom,
                                                buffers[i].name,
                                                0,
                                                0,
                                                RADEON_GEM_DOMAIN_VRAM,
                                                buffers[i].flags);
+#endif /* RADEON_DEBUG_BO */
                        if (bo == NULL) {
 
                                fprintf(stderr, "failed to attach %s %d\n",
                                        regname, buffers[i].name);
-                               
+
                        }
+
+                       ret = radeon_bo_get_tiling(bo, &tiling_flags, &pitch);
+                       if (tiling_flags & RADEON_TILING_MACRO)
+                               bo->flags |= RADEON_BO_FLAGS_MACRO_TILE;
+                       if (tiling_flags & RADEON_TILING_MICRO)
+                               bo->flags |= RADEON_BO_FLAGS_MICRO_TILE;
+                       
                }
 
                if (buffers[i].attachment == __DRI_BUFFER_DEPTH) {
@@ -539,7 +803,24 @@ radeon_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
 
                radeon_renderbuffer_set_bo(rb, bo);
                radeon_bo_unref(bo);
-                   
+
+               if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
+                       rb = radeon_get_renderbuffer(&draw->base, BUFFER_STENCIL);
+                       if (rb != NULL) {
+                               struct radeon_bo *stencil_bo = NULL;
+
+                               if (rb->bo) {
+                                       uint32_t name = radeon_gem_name_bo(rb->bo);
+                                       if (name == buffers[i].name)
+                                               continue;
+                               }
+
+                               stencil_bo = bo;
+                               radeon_bo_ref(stencil_bo);
+                               radeon_renderbuffer_set_bo(rb, stencil_bo);
+                               radeon_bo_unref(stencil_bo);
+                       }
+               }
        }
 
        driUpdateFramebufferSize(radeon->glCtx, drawable);
@@ -567,25 +848,21 @@ GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
        drfb = driDrawPriv->driverPrivate;
        readfb = driReadPriv->driverPrivate;
 
-       if (driContextPriv->driScreenPriv->dri2.enabled) {    
+       if (driContextPriv->driScreenPriv->dri2.enabled) {
                radeon_update_renderbuffers(driContextPriv, driDrawPriv);
                if (driDrawPriv != driReadPriv)
                        radeon_update_renderbuffers(driContextPriv, driReadPriv);
-               radeon->state.color.rrb =
-                       radeon_get_renderbuffer(&drfb->base, BUFFER_BACK_LEFT);
-               radeon->state.depth.rrb =
-                       radeon_get_renderbuffer(&drfb->base, BUFFER_DEPTH);
+               _mesa_reference_renderbuffer(&radeon->state.color.rb,
+                       &(radeon_get_renderbuffer(&drfb->base, BUFFER_BACK_LEFT)->base));
+               _mesa_reference_renderbuffer(&radeon->state.depth.rb,
+                       &(radeon_get_renderbuffer(&drfb->base, BUFFER_DEPTH)->base));
        } else {
                radeon_make_renderbuffer_current(radeon, drfb);
        }
 
-
        if (RADEON_DEBUG & DEBUG_DRI)
             fprintf(stderr, "%s ctx %p dfb %p rfb %p\n", __FUNCTION__, radeon->glCtx, drfb, readfb);
 
-       if (radeon->dri.readable != driReadPriv)
-               radeon->dri.readable = driReadPriv;
-
        driUpdateFramebufferSize(radeon->glCtx, driDrawPriv);
        if (driReadPriv != driDrawPriv)
                driUpdateFramebufferSize(radeon->glCtx, driReadPriv);
@@ -595,35 +872,32 @@ GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
        _mesa_update_state(radeon->glCtx);
 
        if (radeon->glCtx->DrawBuffer == &drfb->base) {
-
-               if (radeon->dri.drawable != driDrawPriv) {
-                       if (driDrawPriv->swap_interval == (unsigned)-1) {
-                               int i;
-                               driDrawPriv->vblFlags =
-                                       (radeon->radeonScreen->irq != 0)
-                                       ? driGetDefaultVBlankFlags(&radeon->
-                                                                  optionCache)
-                                       : VBLANK_FLAG_NO_IRQ;
-                                   
-                               driDrawableInitVBlank(driDrawPriv);
-                               drfb->vbl_waited = driDrawPriv->vblSeq;
-
-                               for (i = 0; i < 2; i++) {
-                                       if (drfb->color_rb[i])
-                                               drfb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
-                               }
-                                   
+               if (driDrawPriv->swap_interval == (unsigned)-1) {
+                       int i;
+                       driDrawPriv->vblFlags =
+                               (radeon->radeonScreen->irq != 0)
+                               ? driGetDefaultVBlankFlags(&radeon->
+                                                          optionCache)
+                               : VBLANK_FLAG_NO_IRQ;
+
+                       driDrawableInitVBlank(driDrawPriv);
+                       drfb->vbl_waited = driDrawPriv->vblSeq;
+
+                       for (i = 0; i < 2; i++) {
+                               if (drfb->color_rb[i])
+                                       drfb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
                        }
-                       radeon->dri.drawable = driDrawPriv;
-                       
-                       radeon_window_moved(radeon);
+
                }
+
+               radeon_window_moved(radeon);
                radeon_draw_buffer(radeon->glCtx, &drfb->base);
        }
 
 
        if (RADEON_DEBUG & DEBUG_DRI)
                fprintf(stderr, "End %s\n", __FUNCTION__);
+
        return GL_TRUE;
 }