r600: fix r700PredictRenderSize for draw prims path
[mesa.git] / src / mesa / drivers / dri / r600 / r700_render.c
index b29a1aa918004166cfc3cf5bfc9c2a2bec7ca971..bdf0bfc0e43ebaedbb95b88a6d71ac54af359e76 100644 (file)
@@ -43,8 +43,8 @@
 #include "tnl/t_context.h"
 #include "tnl/t_vertex.h"
 #include "tnl/t_pipeline.h"
+#include "vbo/vbo_context.h"
 
-#include "radeon_mipmap_tree.h"
 #include "r600_context.h"
 #include "r600_cmdbuf.h"
 
 #include "r700_fragprog.h"
 #include "r700_state.h"
 
+#include "radeon_buffer_objects.h"
+#include "radeon_common_context.h"
+
+void r700WaitForIdle(context_t *context);
+void r700WaitForIdleClean(context_t *context);
+GLboolean r700SendTextureState(context_t *context);
+static unsigned int r700PrimitiveType(int prim);
+void r600UpdateTextureState(GLcontext * ctx);
+GLboolean r700SyncSurf(context_t *context,
+                      struct radeon_bo *pbo,
+                      uint32_t read_domain,
+                      uint32_t write_domain,
+                      uint32_t sync_type);
+
 void r700WaitForIdle(context_t *context)
 {
     BATCH_LOCALS(&context->radeon);
+    radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
     BEGIN_BATCH_NO_AUTOSTATE(3);
 
     R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG, 1));
@@ -70,6 +85,7 @@ void r700WaitForIdle(context_t *context)
 void r700WaitForIdleClean(context_t *context)
 {
     BATCH_LOCALS(&context->radeon);
+    radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
     BEGIN_BATCH_NO_AUTOSTATE(5);
 
     R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE, 0));
@@ -86,6 +102,7 @@ void r700WaitForIdleClean(context_t *context)
 void r700Start3D(context_t *context)
 {
     BATCH_LOCALS(&context->radeon);
+    radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
     if (context->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV770)
     {
         BEGIN_BATCH_NO_AUTOSTATE(2);
@@ -105,105 +122,41 @@ void r700Start3D(context_t *context)
     r700WaitForIdleClean(context);
 }
 
-static GLboolean r700SetupShaders(GLcontext * ctx)
+GLboolean r700SyncSurf(context_t *context,
+                      struct radeon_bo *pbo,
+                      uint32_t read_domain,
+                      uint32_t write_domain,
+                      uint32_t sync_type)
 {
-    context_t *context = R700_CONTEXT(ctx);
-
-    R700_CHIP_CONTEXT *r700 = (R700_CHIP_CONTEXT*)(&context->hw);
-
-    GLuint exportCount;
-
-    r700->ps.SQ_PGM_RESOURCES_PS.u32All = 0;
-    r700->vs.SQ_PGM_RESOURCES_VS.u32All = 0;
-
-    SETbit(r700->ps.SQ_PGM_RESOURCES_PS.u32All, PGM_RESOURCES__PRIME_CACHE_ON_DRAW_bit);
-    SETbit(r700->vs.SQ_PGM_RESOURCES_VS.u32All, PGM_RESOURCES__PRIME_CACHE_ON_DRAW_bit);
-
-    r700SetupVertexProgram(ctx);
-
-    r700SetupFragmentProgram(ctx);
-
-    exportCount = (r700->ps.SQ_PGM_EXPORTS_PS.u32All & EXPORT_MODE_mask) / (1 << EXPORT_MODE_shift);
-    r700->CB_SHADER_CONTROL.u32All = (1 << exportCount) - 1;
-
-    return GL_TRUE;
-}
-
-GLboolean r700SendTextureState(context_t *context)
-{
-    unsigned int i;
-    R700_CHIP_CONTEXT *r700 = (R700_CHIP_CONTEXT*)(&context->hw);
-    offset_modifiers offset_mod = {NO_SHIFT, 0, 0xFFFFFFFF};
-    struct radeon_bo *bo = NULL;
     BATCH_LOCALS(&context->radeon);
+    radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
+    uint32_t cp_coher_size;
 
-    for (i=0; i<R700_TEXTURE_NUMBERUNITS; i++) {
-           radeonTexObj *t = r700->textures[i];
-           if (t) {
-                   if (!t->image_override)
-                           bo = t->mt->bo;
-                   else
-                           bo = t->bo;
-                   if (bo) {
-                           BEGIN_BATCH_NO_AUTOSTATE(14);
-                           R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_RESOURCE, 7));
-                           R600_OUT_BATCH(i * 7);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_RESOURCE0);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_RESOURCE1);
-                           R600_OUT_BATCH_RELOC(r700->textures[i]->SQ_TEX_RESOURCE2,
-                                                bo,
-                                                0,
-                                                RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0, &offset_mod);
-                           R600_OUT_BATCH_RELOC(r700->textures[i]->SQ_TEX_RESOURCE3,
-                                                bo,
-                                                0,
-                                                RADEON_GEM_DOMAIN_GTT|RADEON_GEM_DOMAIN_VRAM, 0, 0, &offset_mod);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_RESOURCE4);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_RESOURCE5);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_RESOURCE6);
-
-                           R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_SAMPLER, 3));
-                           R600_OUT_BATCH(i * 3);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_SAMPLER0);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_SAMPLER1);
-                           R600_OUT_BATCH(r700->textures[i]->SQ_TEX_SAMPLER2);
-                           END_BATCH();
-                           COMMIT_BATCH();
-                   }
-           }
-    }
-    return GL_TRUE;
-}
-
-GLboolean r700SyncSurf(context_t *context)
-{
-    BATCH_LOCALS(&context->radeon);
-
-    /* TODO : too heavy? */
-    unsigned int CP_COHER_CNTL   = 0;
+    if (!pbo)
+           return GL_FALSE;
 
-    CP_COHER_CNTL |= (TC_ACTION_ENA_bit
-                     | VC_ACTION_ENA_bit
-                     | CB_ACTION_ENA_bit
-                     | DB_ACTION_ENA_bit
-                     | SH_ACTION_ENA_bit
-                     | SMX_ACTION_ENA_bit);
+    if (pbo->size == 0xffffffff)
+           cp_coher_size = 0xffffffff;
+    else
+           cp_coher_size = ((pbo->size + 255) >> 8);
 
-
-    BEGIN_BATCH_NO_AUTOSTATE(5);
+    BEGIN_BATCH_NO_AUTOSTATE(5 + 2);
     R600_OUT_BATCH(CP_PACKET3(R600_IT_SURFACE_SYNC, 3));
-    R600_OUT_BATCH(CP_COHER_CNTL);
-    R600_OUT_BATCH(0xFFFFFFFF);
-    R600_OUT_BATCH(0x00000000);
+    R600_OUT_BATCH(sync_type);
+    R600_OUT_BATCH(cp_coher_size);
+    R600_OUT_BATCH(0);
     R600_OUT_BATCH(10);
-
+    R600_OUT_BATCH_RELOC(0,
+                        pbo,
+                        0,
+                        read_domain, write_domain, 0);
     END_BATCH();
     COMMIT_BATCH();
 
     return GL_TRUE;
 }
 
-unsigned int r700PrimitiveType(int prim)
+static unsigned int r700PrimitiveType(int prim)
 {
     switch (prim & PRIM_MODE_MASK)
     {
@@ -244,145 +197,307 @@ unsigned int r700PrimitiveType(int prim)
     }
 }
 
-static GLboolean r700RunRender(GLcontext * ctx,
-                                          struct tnl_pipeline_stage *stage)
+static int r700NumVerts(int num_verts, int prim)
 {
-    context_t *context = R700_CONTEXT(ctx);
-    R700_CHIP_CONTEXT *r700 = (R700_CHIP_CONTEXT*)(&context->hw);
-    int lastIndex = 0;
-#if 1
-    BATCH_LOCALS(&context->radeon);
-
-    unsigned int i, j;
-    TNLcontext *tnl = TNL_CONTEXT(ctx);
-    struct vertex_buffer *vb = &tnl->vb;
+       int verts_off = 0;
+
+       switch (prim & PRIM_MODE_MASK) {
+       case GL_POINTS:
+               verts_off = 0;
+               break;
+       case GL_LINES:
+               verts_off = num_verts % 2;
+               break;
+       case GL_LINE_STRIP:
+               if (num_verts < 2)
+                       verts_off = num_verts;
+               break;
+       case GL_LINE_LOOP:
+               if (num_verts < 2)
+                       verts_off = num_verts;
+               break;
+       case GL_TRIANGLES:
+               verts_off = num_verts % 3;
+               break;
+       case GL_TRIANGLE_STRIP:
+               if (num_verts < 3)
+                       verts_off = num_verts;
+               break;
+       case GL_TRIANGLE_FAN:
+               if (num_verts < 3)
+                       verts_off = num_verts;
+               break;
+       case GL_QUADS:
+               verts_off = num_verts % 4;
+               break;
+       case GL_QUAD_STRIP:
+               if (num_verts < 4)
+                       verts_off = num_verts;
+               else
+                       verts_off = num_verts % 2;
+               break;
+       case GL_POLYGON:
+               if (num_verts < 3)
+                       verts_off = num_verts;
+               break;
+       default:
+               assert(0);
+               return -1;
+               break;
+       }
+
+       return num_verts - verts_off;
+}
 
-    struct r700_fragment_program *fp = (struct r700_fragment_program *)
-                                          (ctx->FragmentProgram._Current);
-    if (context->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV770)
+static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim)
+{
+       context_t *context = R700_CONTEXT(ctx);
+       BATCH_LOCALS(&context->radeon);
+       int type, i, total_emit;
+       int num_indices;
+       uint32_t vgt_draw_initiator = 0;
+       uint32_t vgt_index_type     = 0;
+       uint32_t vgt_primitive_type = 0;
+       uint32_t vgt_num_indices    = 0;
+       TNLcontext *tnl = TNL_CONTEXT(ctx);
+       struct vertex_buffer *vb = &tnl->vb;
+
+    GLboolean bUseDrawIndex;
+    if( (NULL != context->ind_buf.bo) && (GL_TRUE != context->ind_buf.bHostIb) )
     {
-        fp->r700AsmCode.bR6xx = 1;
+        bUseDrawIndex = GL_TRUE;
+    }
+    else
+    {
+        bUseDrawIndex = GL_FALSE;
     }
 
-    if (!r600ValidateBuffers(ctx))
-           return GL_TRUE;
-
-    r700Start3D(context); /* TODO : this is too much. */
-
-    r700SyncSurf(context); /* TODO : make it light. */
-
-    r700SendSQConfig(context);
+       type = r700PrimitiveType(prim);
+       num_indices = r700NumVerts(end - start, prim);
 
-    r700UpdateShaders(ctx);
+       radeon_print(RADEON_RENDER, RADEON_TRACE,
+               "%s type %x num_indices %d\n",
+               __func__, type, num_indices);
 
-    r700SetScissor(context);
-    r700SetRenderTarget(context, 0);
-    r700SetDepthTarget(context);
+       if (type < 0 || num_indices <= 0)
+               return;
 
-    if(r700SetupStreams(ctx))
+    if(GL_TRUE == bUseDrawIndex)
     {
-        return GL_TRUE;
+        total_emit =   3  /* VGT_PRIMITIVE_TYPE */
+                        + 2  /* VGT_INDEX_TYPE */
+                        + 2  /* NUM_INSTANCES */
+                     + 5+2; /* DRAW_INDEX */
+    }
+    else
+    {
+        total_emit =   3 /* VGT_PRIMITIVE_TYPE */
+                + 2 /* VGT_INDEX_TYPE */
+                + 2 /* NUM_INSTANCES */
+                 + num_indices + 3; /* DRAW_INDEX_IMMD */
     }
 
-    /* flush TX */
-    //r700SyncSurf(context); /*  */
+    BEGIN_BATCH_NO_AUTOSTATE(total_emit);
+       // prim
+    SETfield(vgt_primitive_type, type,
+       VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask);
+    R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG, 1));
+    R600_OUT_BATCH(mmVGT_PRIMITIVE_TYPE - ASIC_CONFIG_BASE_INDEX);
+    R600_OUT_BATCH(vgt_primitive_type);
 
-    r600UpdateTextureState(ctx);
-    r700SendTextureState(context);
+       // index type
+    SETfield(vgt_index_type, DI_INDEX_SIZE_32_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
 
-    if(GL_FALSE == fp->translated)
+    if(GL_TRUE == bUseDrawIndex)
     {
-        if( GL_FALSE == r700TranslateFragmentShader(fp, &(fp->mesa_program)) )
+        if(GL_TRUE != context->ind_buf.is_32bit)
         {
-            return GL_TRUE;
+            SETfield(vgt_index_type, DI_INDEX_SIZE_16_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
         }
     }
 
-    /* flush SQ */
-    //r700SyncSurf(context); /*  */
-    //r700SyncSurf(context); /*  */
+    R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE, 0));
+    R600_OUT_BATCH(vgt_index_type);
 
-    r700SetupShaders(ctx);
+       // num instances
+       R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES, 0));
+        R600_OUT_BATCH(1);
 
-    r700SendPSState(context);
-    r700SendVSState(context);
+       // draw packet
+    vgt_num_indices = num_indices;
 
-    /* flush vtx */
-    //r700SyncSurf(context); /*  */
+    if(GL_TRUE == bUseDrawIndex)
+    {
+        SETfield(vgt_draw_initiator, DI_SRC_SEL_DMA, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
+    }
+    else
+    {
+        SETfield(vgt_draw_initiator, DI_SRC_SEL_IMMEDIATE, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
+    }
 
-    r700SendContextStates(context);
-    r700SendViewportState(context, 0);
-    r700SendRenderTargetState(context, 0);
+       SETfield(vgt_draw_initiator, DI_MAJOR_MODE_0, MAJOR_MODE_shift, MAJOR_MODE_mask);
 
+    if(GL_TRUE == bUseDrawIndex)
+    {
+        R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX, 3));
+        R600_OUT_BATCH(context->ind_buf.bo_offset);
+        R600_OUT_BATCH(0);
+        R600_OUT_BATCH(vgt_num_indices);
+        R600_OUT_BATCH(vgt_draw_initiator);
+        R600_OUT_BATCH_RELOC(context->ind_buf.bo_offset,
+                             context->ind_buf.bo,
+                             context->ind_buf.bo_offset,
+                             RADEON_GEM_DOMAIN_GTT, 0, 0);
+    }
+    else
+    {
+        R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD, (num_indices + 1)));
+        R600_OUT_BATCH(vgt_num_indices);
+        R600_OUT_BATCH(vgt_draw_initiator);
+    }
 
-    /* richard test code */
-    for (i = 0; i < vb->PrimitiveCount; i++) 
+    if(NULL == context->ind_buf.bo)
     {
-        GLuint prim = _tnl_translate_prim(&vb->Primitive[i]);
-        GLuint start = vb->Primitive[i].start;
-        GLuint end = vb->Primitive[i].start + vb->Primitive[i].count;
-        GLuint numIndices = vb->Primitive[i].count;
-        GLuint numEntires;
+        for (i = start; i < (start + num_indices); i++) {
+            if(vb->Elts)
+            {
+                R600_OUT_BATCH(vb->Elts[i]);
+            }
+            else
+                R600_OUT_BATCH(i);
+        }
+    }
+    else
+    {        
+        if(GL_TRUE == context->ind_buf.bHostIb)
+        {
+            if(GL_TRUE != context->ind_buf.is_32bit)
+            {
+                GLushort * pIndex = (GLushort*)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
+                pIndex += start;
+                for (i = 0; i < num_indices; i++) 
+                {           
+                    R600_OUT_BATCH(*pIndex);
+                    pIndex++;
+                }
+            }
+            else
+            {                
+                GLuint * pIndex = (GLuint*)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
+                pIndex += start;                
+
+                for (i = 0; i < num_indices; i++) 
+                {                
+                    R600_OUT_BATCH(*pIndex);
+                    pIndex++;
+                }
+            }
+        }
+    }
 
-        unsigned int VGT_DRAW_INITIATOR = 0;
-        unsigned int VGT_INDEX_TYPE     = 0;
-        unsigned int VGT_PRIMITIVE_TYPE = 0;
-        unsigned int VGT_NUM_INDICES    = 0;
-        
-        numEntires = 2 /* VGT_INDEX_TYPE */
-                     + 3 /* VGT_PRIMITIVE_TYPE */
-                     + numIndices + 3; /* DRAW_INDEX_IMMD */                  
-                     
-        BEGIN_BATCH_NO_AUTOSTATE(numEntires);  
+    END_BATCH();
+    COMMIT_BATCH();
+}
 
-        VGT_INDEX_TYPE |= DI_INDEX_SIZE_32_BIT << INDEX_TYPE_shift;
+/* start 3d, idle, cb/db flush */
+#define PRE_EMIT_STATE_BUFSZ 10 + 5 + 14
 
-        R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE, 0));
-        R600_OUT_BATCH(VGT_INDEX_TYPE);
+static GLuint r700PredictRenderSize(GLcontext* ctx, GLuint nr_prims)
+{
+    context_t *context = R700_CONTEXT(ctx);
+    struct r700_vertex_program *vp = context->selected_vp;
+    GLboolean flushed;
+    GLuint dwords, i;
+    GLuint state_size;
+    /* pre calculate aos count so state prediction works */
+    context->radeon.tcl.aos_count = _mesa_bitcount(vp->mesa_program->Base.InputsRead);
+
+    dwords = PRE_EMIT_STATE_BUFSZ;
+    if (nr_prims)
+           dwords += nr_prims * 14;
+    else {
+           TNLcontext *tnl = TNL_CONTEXT(ctx);
+           struct vertex_buffer *vb = &tnl->vb;
+
+           for (i = 0; i < vb->PrimitiveCount; i++)
+                   dwords += vb->Primitive[i].count + 10;
+    }
+    state_size = radeonCountStateEmitSize(&context->radeon);
+    flushed = rcommonEnsureCmdBufSpace(&context->radeon,
+            dwords + state_size, __FUNCTION__);
+
+    if (flushed)
+        dwords += radeonCountStateEmitSize(&context->radeon);
+    else
+        dwords += state_size;
+
+    radeon_print(RADEON_RENDER, RADEON_VERBOSE,
+       "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
+    return dwords;
+}
 
-        VGT_NUM_INDICES = numIndices;
+static GLboolean r700RunRender(GLcontext * ctx,
+                              struct tnl_pipeline_stage *stage)
+{
+    context_t *context = R700_CONTEXT(ctx);
+    radeonContextPtr radeon = &context->radeon;
+    unsigned int i, id = 0;
+    TNLcontext *tnl = TNL_CONTEXT(ctx);
+    struct vertex_buffer *vb = &tnl->vb;
+    struct radeon_renderbuffer *rrb;
 
-        VGT_PRIMITIVE_TYPE |= r700PrimitiveType(prim) << VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift;
-        R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG, 1));
-        R600_OUT_BATCH(mmVGT_PRIMITIVE_TYPE - ASIC_CONFIG_BASE_INDEX);
-        R600_OUT_BATCH(VGT_PRIMITIVE_TYPE);
+    radeon_print(RADEON_RENDER, RADEON_NORMAL, "%s: cs begin at %d\n",
+                __func__, context->radeon.cmdbuf.cs->cdw);
 
-        VGT_DRAW_INITIATOR |= DI_SRC_SEL_IMMEDIATE << SOURCE_SELECT_shift;
-        VGT_DRAW_INITIATOR |= DI_MAJOR_MODE_0 << MAJOR_MODE_shift;
+    /* always emit CB base to prevent
+     * lock ups on some chips.
+     */
+    R600_STATECHANGE(context, cb_target);
+    /* mark vtx as dirty since it changes per-draw */
+    R600_STATECHANGE(context, vtx);
 
-        R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD, (numIndices + 1)));
-        R600_OUT_BATCH(VGT_NUM_INDICES);
-        R600_OUT_BATCH(VGT_DRAW_INITIATOR);
+    r700SetScissor(context);
+    r700SetupVertexProgram(ctx);
+    r700SetupFragmentProgram(ctx);
+    r600UpdateTextureState(ctx);
 
-        for (j = lastIndex; j < lastIndex + numIndices; j++)
-        {
-            R600_OUT_BATCH(j);
-        }
-        lastIndex += numIndices;
+    GLuint emit_end = r700PredictRenderSize(ctx, 0)
+        + context->radeon.cmdbuf.cs->cdw;
+    r700SetupStreams(ctx);
 
-        END_BATCH();
-        COMMIT_BATCH();
+    radeonEmitState(radeon);
+
+    radeon_debug_add_indent();
+    /* richard test code */
+    for (i = 0; i < vb->PrimitiveCount; i++) {
+        GLuint prim = _tnl_translate_prim(&vb->Primitive[i]);
+        GLuint start = vb->Primitive[i].start;
+        GLuint end = vb->Primitive[i].start + vb->Primitive[i].count;
+        r700RunRenderPrimitive(ctx, start, end, prim);
     }
+    radeon_debug_remove_indent();
 
     /* Flush render op cached for last several quads. */
     r700WaitForIdleClean(context);
 
-    /* flush dst */
-    //r700SyncSurf(context); /*  */
+    rrb = radeon_get_colorbuffer(&context->radeon);
+    if (rrb && rrb->bo)
+           r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
+                        CB_ACTION_ENA_bit | (1 << (id + 6)));
 
-    radeonReleaseArrays(ctx, 0);
+    rrb = radeon_get_depthbuffer(&context->radeon);
+    if (rrb && rrb->bo)
+           r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
+                        DB_ACTION_ENA_bit | DB_DEST_BASE_ENA_bit);
 
-    //richard test
-    /* test stamp, write a number to mmSCRATCH4 */
-#if 0
-    BEGIN_BATCH_NO_AUTOSTATE(3);
-    R600_OUT_BATCH_REGVAL((0x2144 << 2), 0x56785678);
-    END_BATCH();
-    COMMIT_BATCH();
-#endif
+    radeonReleaseArrays(ctx, ~0);
+
+    radeon_print(RADEON_RENDER, RADEON_TRACE, "%s: cs end at %d\n",
+                __func__, context->radeon.cmdbuf.cs->cdw);
 
-#endif //0
-    rcommonFlushCmdBuf( &context->radeon, __FUNCTION__ );
+    if ( emit_end < context->radeon.cmdbuf.cs->cdw )
+       WARN_ONCE("Rendering was %d commands larger than predicted size."
+              " We might overflow  command buffer.\n", context->radeon.cmdbuf.cs->cdw - emit_end);
 
     return GL_FALSE;
 }
@@ -402,7 +517,10 @@ static GLboolean r700RunTCLRender(GLcontext * ctx,  /*----------------------*/
 
     /* TODO : sw fallback */
 
+    /* Need shader bo's setup before bo check */
+    r700UpdateShaders(ctx);
     /**
+
     * Ensure all enabled and complete textures are uploaded along with any buffers being used.
     */
     if(!r600ValidateBuffers(ctx))
@@ -410,10 +528,6 @@ static GLboolean r700RunTCLRender(GLcontext * ctx,  /*----------------------*/
         return GL_TRUE;
     }
 
-    context_t *context = R700_CONTEXT(ctx);
-
-    r700UpdateShaders(ctx);
-
     bRet = r700RunRender(ctx, stage);
 
     return bRet;
@@ -455,4 +569,585 @@ const struct tnl_pipeline_stage *r700_pipeline[] =
     0,
 };
 
+#define CONVERT( TYPE, MACRO ) do {            \
+       GLuint i, j, sz;                                \
+       sz = input->Size;                               \
+       if (input->Normalized) {                        \
+               for (i = 0; i < count; i++) {           \
+                       const TYPE *in = (TYPE *)src_ptr;               \
+                       for (j = 0; j < sz; j++) {              \
+                               *dst_ptr++ = MACRO(*in);                \
+                               in++;                           \
+                       }                                       \
+                       src_ptr += stride;                      \
+               }                                               \
+       } else {                                        \
+               for (i = 0; i < count; i++) {           \
+                       const TYPE *in = (TYPE *)src_ptr;               \
+                       for (j = 0; j < sz; j++) {              \
+                               *dst_ptr++ = (GLfloat)(*in);            \
+                               in++;                           \
+                       }                                       \
+                       src_ptr += stride;                      \
+               }                                               \
+       }                                               \
+} while (0)
+
+/**
+ * Convert attribute data type to float
+ * If the attribute uses named buffer object replace the bo with newly allocated bo
+ */
+static void r700ConvertAttrib(GLcontext *ctx, int count, 
+                              const struct gl_client_array *input, 
+                              struct StreamDesc *attr)
+{
+    context_t *context = R700_CONTEXT(ctx);
+    const GLvoid *src_ptr;
+    GLboolean mapped_named_bo = GL_FALSE;
+    GLfloat *dst_ptr;
+    GLuint stride;
+
+    stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
+
+    /* Convert value for first element only */
+    if (input->StrideB == 0)
+    {
+        count = 1;
+    }
+
+    if (input->BufferObj->Name) 
+    {
+        if (!input->BufferObj->Pointer) 
+        {
+            ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
+            mapped_named_bo = GL_TRUE;
+        }
+
+        src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
+    } 
+    else 
+    {
+        src_ptr = input->Ptr;
+    }
+
+    radeonAllocDmaRegion(&context->radeon, &attr->bo, &attr->bo_offset, 
+                         sizeof(GLfloat) * input->Size * count, 32);
+    dst_ptr = (GLfloat *)ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
+
+    assert(src_ptr != NULL);
+
+    switch (input->Type) 
+    {
+        case GL_DOUBLE:
+            CONVERT(GLdouble, (GLfloat));
+            break;
+        case GL_UNSIGNED_INT:
+            CONVERT(GLuint, UINT_TO_FLOAT);
+            break;
+        case GL_INT:
+            CONVERT(GLint, INT_TO_FLOAT);
+            break;
+        case GL_UNSIGNED_SHORT:
+            CONVERT(GLushort, USHORT_TO_FLOAT);
+            break;
+        case GL_SHORT:
+            CONVERT(GLshort, SHORT_TO_FLOAT);
+            break;
+        case GL_UNSIGNED_BYTE:
+            assert(input->Format != GL_BGRA);
+            CONVERT(GLubyte, UBYTE_TO_FLOAT);
+            break;
+        case GL_BYTE:
+            CONVERT(GLbyte, BYTE_TO_FLOAT);
+            break;
+        default:
+            assert(0);
+            break;
+    }
+
+    if (mapped_named_bo) 
+    {
+        ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
+    }
+}
+
+static void r700AlignDataToDword(GLcontext *ctx, 
+                                 const struct gl_client_array *input, 
+                                 int count, 
+                                 struct StreamDesc *attr)
+{
+    context_t *context = R700_CONTEXT(ctx);
+    const int dst_stride = (input->StrideB + 3) & ~3;
+    const int size = getTypeSize(input->Type) * input->Size * count;
+    GLboolean mapped_named_bo = GL_FALSE;
+
+    radeonAllocDmaRegion(&context->radeon, &attr->bo, &attr->bo_offset, size, 32);
+
+    if (!input->BufferObj->Pointer) 
+    {
+        ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
+        mapped_named_bo = GL_TRUE;
+    }
+
+    {
+        GLvoid *src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
+        GLvoid *dst_ptr = ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
+        int i;
+
+        for (i = 0; i < count; ++i) 
+        {
+            _mesa_memcpy(dst_ptr, src_ptr, input->StrideB);
+            src_ptr += input->StrideB;
+            dst_ptr += dst_stride;
+        }
+    }
+
+    if (mapped_named_bo) 
+    {
+        ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
+    }
+
+    attr->stride = dst_stride;
+}
+
+static void r700SetupStreams2(GLcontext *ctx, const struct gl_client_array *input[], int count)
+{
+       context_t *context = R700_CONTEXT(ctx);
+    GLuint stride;
+    int ret;
+    int i, index;
+
+    R600_STATECHANGE(context, vtx);
+
+    for(index = 0; index < context->nNumActiveAos; index++) 
+    {
+        struct radeon_aos *aos = &context->radeon.tcl.aos[index];
+        i = context->stream_desc[index].element;
+
+        stride = (input[i]->StrideB == 0) ? getTypeSize(input[i]->Type) * input[i]->Size : input[i]->StrideB;
+
+        if (input[i]->Type == GL_DOUBLE || input[i]->Type == GL_UNSIGNED_INT || input[i]->Type == GL_INT ||
+#if MESA_BIG_ENDIAN
+            getTypeSize(input[i]->Type) != 4 || 
+#endif
+            stride < 4) 
+        {
+            r700ConvertAttrib(ctx, count, input[i], &context->stream_desc[index]);
+        } 
+        else 
+        {
+            if (input[i]->BufferObj->Name) 
+            {
+                if (stride % 4 != 0) 
+                {
+                    assert(((intptr_t) input[i]->Ptr) % input[i]->StrideB == 0);
+                    r700AlignDataToDword(ctx, input[i], count, &context->stream_desc[index]);
+                    context->stream_desc[index].is_named_bo = GL_FALSE;
+                } 
+                else 
+                {
+                    context->stream_desc[index].stride = input[i]->StrideB;
+                    context->stream_desc[index].bo_offset = (intptr_t) input[i]->Ptr;
+                    context->stream_desc[index].bo = get_radeon_buffer_object(input[i]->BufferObj)->bo;
+                    context->stream_desc[index].is_named_bo = GL_TRUE;
+                }
+            } 
+            else 
+            {
+                int size;
+                int local_count = count;
+                uint32_t *dst;
+
+                if (input[i]->StrideB == 0) 
+                {
+                    size = getTypeSize(input[i]->Type) * input[i]->Size;
+                    local_count = 1;
+                } 
+                else 
+                {
+                    size = getTypeSize(input[i]->Type) * input[i]->Size * local_count;
+                }
+
+                radeonAllocDmaRegion(&context->radeon, &context->stream_desc[index].bo, 
+                                     &context->stream_desc[index].bo_offset, size, 32);
+                assert(context->stream_desc[index].bo->ptr != NULL);
+                dst = (uint32_t *)ADD_POINTERS(context->stream_desc[index].bo->ptr, 
+                                               context->stream_desc[index].bo_offset);
+
+                switch (context->stream_desc[index].dwords) 
+                {
+                case 1:                     
+                    radeonEmitVec4(dst, input[i]->Ptr, input[i]->StrideB, local_count);                         
+                                       context->stream_desc[index].stride = 4; 
+                    break;
+                case 2: 
+                    radeonEmitVec8(dst, input[i]->Ptr, input[i]->StrideB, local_count); 
+                                       context->stream_desc[index].stride = 8; 
+                    break;
+                case 3: 
+                    radeonEmitVec12(dst, input[i]->Ptr, input[i]->StrideB, local_count); 
+                                       context->stream_desc[index].stride = 12; 
+                    break;
+                case 4: 
+                    radeonEmitVec16(dst, input[i]->Ptr, input[i]->StrideB, local_count); 
+                                       context->stream_desc[index].stride = 16; 
+                    break;
+                default: 
+                    assert(0); 
+                    break;
+                }
+            }
+        }
+
+        aos->count = context->stream_desc[index].stride == 0 ? 1 : count;
+        aos->stride = context->stream_desc[index].stride / sizeof(float);
+        aos->components = context->stream_desc[index].dwords;
+        aos->bo = context->stream_desc[index].bo;
+        aos->offset = context->stream_desc[index].bo_offset;
+
+        if(context->stream_desc[index].is_named_bo) 
+        {
+            radeon_cs_space_add_persistent_bo(context->radeon.cmdbuf.cs, 
+                                              context->stream_desc[index].bo, 
+                                              RADEON_GEM_DOMAIN_GTT, 0);
+        }
+    }
+
+    context->radeon.tcl.aos_count = context->nNumActiveAos;
+    ret = radeon_cs_space_check_with_bo(context->radeon.cmdbuf.cs, 
+                                        first_elem(&context->radeon.dma.reserved)->bo, 
+                                        RADEON_GEM_DOMAIN_GTT, 0);    
+}
+
+static void r700FreeData(GLcontext *ctx)
+{
+    /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
+     * to prevent double unref in radeonReleaseArrays
+     * called during context destroy
+     */
+    context_t *context = R700_CONTEXT(ctx);
+    
+    int i;
+
+    for (i = 0; i < context->nNumActiveAos; i++) 
+    {
+        if (!context->stream_desc[i].is_named_bo) 
+        {
+               radeon_bo_unref(context->stream_desc[i].bo);
+        }
+        context->radeon.tcl.aos[i].bo = NULL;
+    }
+    
+    if (context->ind_buf.bo != NULL) 
+    {
+        if(context->ind_buf.bHostIb != GL_TRUE)
+        {
+            radeon_bo_unref(context->ind_buf.bo);
+        }
+        else
+        {
+            FREE(context->ind_buf.bo->ptr);
+            FREE(context->ind_buf.bo);
+            context->ind_buf.bo = NULL;
+        }
+    }
+}
+
+static void r700FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
+{
+    context_t *context = R700_CONTEXT(ctx);
+    GLvoid *src_ptr;
+    GLuint *out;
+    int i;
+    GLboolean mapped_named_bo = GL_FALSE;
+
+    if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) 
+    {
+        ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
+        mapped_named_bo = GL_TRUE;
+        assert(mesa_ind_buf->obj->Pointer != NULL);
+    }
+    src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
+
+    if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) 
+    {
+        GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
+        GLubyte *in = (GLubyte *)src_ptr;
+
+        if(context->ind_buf.bHostIb != GL_TRUE)
+        {
+            radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo, 
+                                 &context->ind_buf.bo_offset, size, 4);
+
+            assert(context->ind_buf.bo->ptr != NULL);
+            out = (GLuint *)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
+        }
+        else
+        {
+            context->ind_buf.bo        = MALLOC_STRUCT(radeon_bo);
+            context->ind_buf.bo->ptr   = ALIGN_MALLOC(size, 4);
+            context->ind_buf.bo_offset = 0;
+            out                        = (GLuint *)context->ind_buf.bo->ptr;
+        }
+
+        for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) 
+        {
+            *out++ = in[i] | in[i + 1] << 16;
+        }
+
+        if (i < mesa_ind_buf->count) 
+        {
+            *out++ = in[i];
+        }
+
+#if MESA_BIG_ENDIAN
+    } 
+    else 
+    { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
+        GLushort *in = (GLushort *)src_ptr;
+        GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
+
+        if(context->ind_buf.bHostIb != GL_TRUE)
+        {
+            radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo,
+                                 &context->ind_buf.bo_offset, size, 4);
+
+            assert(context->ind_buf.bo->ptr != NULL);
+            out = (GLuint *)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
+        }
+        else
+        {
+            context->ind_buf.bo        = MALLOC_STRUCT(radeon_bo);
+            context->ind_buf.bo->ptr   = ALIGN_MALLOC(size, 4);
+            context->ind_buf.bo_offset = 0;
+            out                        = (GLuint *)context->ind_buf.bo->ptr;
+        }
+
+        for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) 
+        {
+            *out++ = in[i] | in[i + 1] << 16;
+        }
+
+        if (i < mesa_ind_buf->count) 
+        {
+            *out++ = in[i];
+        }
+#endif
+    }
+
+    context->ind_buf.is_32bit = GL_FALSE;
+    context->ind_buf.count = mesa_ind_buf->count;
+
+    if (mapped_named_bo) 
+    {
+        ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
+    }
+}
+
+static void r700SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
+{
+    context_t *context = R700_CONTEXT(ctx);
+
+    if (!mesa_ind_buf) {
+        context->ind_buf.bo = NULL;
+        return;
+    }
+
+    context->ind_buf.bHostIb = GL_FALSE;
+
+#if MESA_BIG_ENDIAN
+    if (mesa_ind_buf->type == GL_UNSIGNED_INT) 
+    {
+#else
+    if (mesa_ind_buf->type != GL_UNSIGNED_BYTE) 
+    {
+#endif
+        const GLvoid *src_ptr;
+        GLvoid *dst_ptr;
+        GLboolean mapped_named_bo = GL_FALSE;
+
+        if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) 
+        {
+               ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
+               assert(mesa_ind_buf->obj->Pointer != NULL);
+               mapped_named_bo = GL_TRUE;
+        }
+
+        src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
+
+        const GLuint size = mesa_ind_buf->count * getTypeSize(mesa_ind_buf->type);
+
+        if(context->ind_buf.bHostIb != GL_TRUE)
+        {
+            radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo, 
+                                 &context->ind_buf.bo_offset, size, 4);
+            assert(context->ind_buf.bo->ptr != NULL);
+            dst_ptr = ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
+        }
+        else
+        {
+            context->ind_buf.bo        = MALLOC_STRUCT(radeon_bo);
+            context->ind_buf.bo->ptr   = ALIGN_MALLOC(size, 4);
+            context->ind_buf.bo_offset = 0;
+            dst_ptr                    = context->ind_buf.bo->ptr;
+        }
+
+        _mesa_memcpy(dst_ptr, src_ptr, size);
+
+        context->ind_buf.is_32bit = (mesa_ind_buf->type == GL_UNSIGNED_INT);
+        context->ind_buf.count = mesa_ind_buf->count;
+
+        if (mapped_named_bo) 
+        {
+               ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
+        }
+    } 
+    else 
+    {
+           r700FixupIndexBuffer(ctx, mesa_ind_buf);
+    }
+}
+
+static GLboolean r700TryDrawPrims(GLcontext *ctx,
+                                        const struct gl_client_array *arrays[],
+                                        const struct _mesa_prim *prim,
+                                        GLuint nr_prims,
+                                        const struct _mesa_index_buffer *ib,
+                                        GLuint min_index,
+                                        GLuint max_index )
+{
+    context_t *context = R700_CONTEXT(ctx);
+    radeonContextPtr radeon = &context->radeon;
+    GLuint i, id = 0;
+    GLboolean bValidedbuffer;
+    struct radeon_renderbuffer *rrb;
+
+    if (ctx->NewState)
+    {
+        _mesa_update_state( ctx );
+    }
+
+    bValidedbuffer = r600ValidateBuffers(ctx);
+
+    /* always emit CB base to prevent
+     * lock ups on some chips.
+     */
+    R600_STATECHANGE(context, cb_target);
+    /* mark vtx as dirty since it changes per-draw */
+    R600_STATECHANGE(context, vtx);
+
+    _tnl_UpdateFixedFunctionProgram(ctx);
+    r700SetVertexFormat(ctx, arrays, max_index + 1);
+       r700SetupStreams2(ctx, arrays, max_index + 1);
+    r700UpdateShaders2(ctx);
+
+    r700SetScissor(context);
+
+    r700SetupVertexProgram(ctx);
+
+    r700SetupFragmentProgram(ctx);
+
+    r600UpdateTextureState(ctx);
+
+    GLuint emit_end = r700PredictRenderSize(ctx, nr_prims)
+                    + context->radeon.cmdbuf.cs->cdw;
+
+    r700SetupIndexBuffer(ctx, ib);
+
+    radeonEmitState(radeon);
+
+    for (i = 0; i < nr_prims; ++i) 
+    {
+           r700RunRenderPrimitive(ctx, 
+                               prim[i].start, 
+                               prim[i].start + prim[i].count, 
+                               prim[i].mode);
+    }
+    /* Flush render op cached for last several quads. */
+    r700WaitForIdleClean(context);
+
+    rrb = radeon_get_colorbuffer(&context->radeon);
+    if (rrb && rrb->bo)
+           r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
+                        CB_ACTION_ENA_bit | (1 << (id + 6)));
+
+    rrb = radeon_get_depthbuffer(&context->radeon);
+    if (rrb && rrb->bo)
+           r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
+                        DB_ACTION_ENA_bit | DB_DEST_BASE_ENA_bit);
+
+    r700FreeData(ctx);
+
+    if (emit_end < context->radeon.cmdbuf.cs->cdw)
+    {
+        WARN_ONCE("Rendering was %d commands larger than predicted size."
+            " We might overflow  command buffer.\n", context->radeon.cmdbuf.cs->cdw - emit_end);
+    }
+
+    return GL_TRUE;
+}
+
+static void r700DrawPrimsRe(GLcontext *ctx,
+                        const struct gl_client_array *arrays[],
+                        const struct _mesa_prim *prim,
+                        GLuint nr_prims,
+                        const struct _mesa_index_buffer *ib,
+                        GLboolean index_bounds_valid,
+                        GLuint min_index,
+                        GLuint max_index)
+{
+    GLboolean retval = GL_FALSE;
+
+    /* This check should get folded into just the places that
+        * min/max index are really needed.
+        */
+       if (!index_bounds_valid) {
+               vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
+       }
+
+       if (min_index) {
+               vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r700DrawPrimsRe );
+               return;
+       }
+
+       /* Make an attempt at drawing */
+       retval = r700TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
+
+       /* If failed run tnl pipeline - it should take care of fallbacks */
+       if (!retval)
+               _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
+}
+
+static void r700DrawPrims(GLcontext *ctx,
+                        const struct gl_client_array *arrays[],
+                        const struct _mesa_prim *prim,
+                        GLuint nr_prims,
+                        const struct _mesa_index_buffer *ib,
+                        GLboolean index_bounds_valid,
+                        GLuint min_index,
+                        GLuint max_index)
+{
+    context_t *context = R700_CONTEXT(ctx);
+
+    /* For non indexed drawing, using tnl pipe. */
+    if(!ib)
+    {
+        context->ind_buf.bo = NULL;
+
+        _tnl_vbo_draw_prims(ctx, arrays, prim, nr_prims, ib,
+                            index_bounds_valid, min_index, max_index);
+        return;
+    }
+
+       r700DrawPrimsRe(ctx, arrays, prim, nr_prims, ib, index_bounds_valid, min_index, max_index);
+}
+
+void r700InitDraw(GLcontext *ctx)
+{
+       struct vbo_context *vbo = vbo_context(ctx);
+
+       vbo->draw_prims = r700DrawPrims;
+}
+