r600: fix count prediction for IB case
[mesa.git] / src / mesa / drivers / dri / r600 / r700_render.c
index 0aef0b7ea1f7dc3e65b36143dedbab3a5709fcfe..b20b129d1709348d1af8d7e1be209de80ae45226 100644 (file)
@@ -253,14 +253,12 @@ 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 type, 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)
@@ -287,6 +285,7 @@ static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim
         total_emit =   3  /* VGT_PRIMITIVE_TYPE */
                     + 2  /* VGT_INDEX_TYPE */
                     + 2  /* NUM_INSTANCES */
+                     + 3  /* VGT_INDEX_OFFSET */
                      + 5 + 2; /* DRAW_INDEX */
     }
     else
@@ -294,7 +293,8 @@ static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim
         total_emit =   3 /* VGT_PRIMITIVE_TYPE */
                     + 2 /* VGT_INDEX_TYPE */
                     + 2 /* NUM_INSTANCES */
-                     + num_indices + 3; /* DRAW_INDEX_IMMD */
+                     + 3 /* VGT_INDEX_OFFSET */
+                     + 3; /* DRAW_INDEX_AUTO */
     }
 
     BEGIN_BATCH_NO_AUTOSTATE(total_emit);
@@ -332,13 +332,15 @@ static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim
     }
     else
     {
-        SETfield(vgt_draw_initiator, DI_SRC_SEL_IMMEDIATE, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
+        SETfield(vgt_draw_initiator, DI_SRC_SEL_AUTO_INDEX, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
     }
 
-       SETfield(vgt_draw_initiator, DI_MAJOR_MODE_0, MAJOR_MODE_shift, MAJOR_MODE_mask);
+    SETfield(vgt_draw_initiator, DI_MAJOR_MODE_0, MAJOR_MODE_shift, MAJOR_MODE_mask);
 
     if(GL_TRUE == bUseDrawIndex)
     {
+        R600_OUT_BATCH_REGSEQ(VGT_INDX_OFFSET, 1);
+        R600_OUT_BATCH(0);
         R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX, 3));
         R600_OUT_BATCH(context->ind_buf.bo_offset);
         R600_OUT_BATCH(0);
@@ -351,21 +353,11 @@ static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim
     }
     else
     {
-        R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD, (num_indices + 1)));
+        R600_OUT_BATCH_REGSEQ(VGT_INDX_OFFSET, 1);
+        R600_OUT_BATCH(start);
+        R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_AUTO,1));
         R600_OUT_BATCH(vgt_num_indices);
         R600_OUT_BATCH(vgt_draw_initiator);
-
-        for (i = start; i < (start + num_indices); i++)
-       {
-            if(vb->Elts)
-            {
-                R600_OUT_BATCH(vb->Elts[i]);
-            }
-            else
-           {
-                R600_OUT_BATCH(i);
-           }
-        }
     }
 
     END_BATCH();
@@ -375,173 +367,40 @@ static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim
 /* start 3d, idle, cb/db flush */
 #define PRE_EMIT_STATE_BUFSZ 10 + 5 + 14
 
-static GLuint r700PredictRenderSize(GLcontext* ctx, GLuint nr_prims)
+static GLuint r700PredictRenderSize(GLcontext* ctx,
+                                   const struct _mesa_prim *prim,
+                                   const struct _mesa_index_buffer *ib,
+                                   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;
+    if (ib)
+           dwords += nr_prims * 17;
     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;
+           for (i = 0; i < nr_prims; ++i)
+           {
+                dwords += 13;
+           }
     }
+
     state_size = radeonCountStateEmitSize(&context->radeon);
     flushed = rcommonEnsureCmdBufSpace(&context->radeon,
-            dwords + state_size, __FUNCTION__);
-
+                                      dwords + state_size,
+                                      __FUNCTION__);
     if (flushed)
-        dwords += radeonCountStateEmitSize(&context->radeon);
+           dwords += radeonCountStateEmitSize(&context->radeon);
     else
-        dwords += state_size;
+           dwords += state_size;
 
-    radeon_print(RADEON_RENDER, RADEON_VERBOSE,
-       "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
+    radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
     return dwords;
-}
-
-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;
-
-    radeon_print(RADEON_RENDER, RADEON_NORMAL, "%s: cs begin at %d\n",
-                __func__, context->radeon.cmdbuf.cs->cdw);
-
-    /* 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);
-
-    r700SetScissor(context);
-    r700SetupVertexProgram(ctx);
-    r700SetupFragmentProgram(ctx);
-    r600UpdateTextureState(ctx);
-
-    GLuint emit_end = r700PredictRenderSize(ctx, 0)
-        + context->radeon.cmdbuf.cs->cdw;
-    r700SetupStreams(ctx);
-
-    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);
-
-    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);
-
-    radeonReleaseArrays(ctx, ~0);
-
-    radeon_print(RADEON_RENDER, RADEON_TRACE, "%s: cs end at %d\n",
-                __func__, context->radeon.cmdbuf.cs->cdw);
-
-    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;
-}
-
-static GLboolean r700RunNonTCLRender(GLcontext * ctx,
-                                    struct tnl_pipeline_stage *stage) /* -------------------- */
-{
-       GLboolean bRet = GL_TRUE;
-       
-       return bRet;
-}
-
-static GLboolean r700RunTCLRender(GLcontext * ctx,  /*----------------------*/
-                                 struct tnl_pipeline_stage *stage)
-{
-       GLboolean bRet = GL_FALSE;
-
-    /* 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))
-    {
-        return GL_TRUE;
-    }
-
-    bRet = r700RunRender(ctx, stage);
 
-    return bRet;
-       //GL_FALSE will stop to do other pipe stage in _tnl_run_pipeline
-    //The render here DOES finish the whole pipe, so GL_FALSE should be returned for success.
 }
 
-const struct tnl_pipeline_stage _r700_render_stage = {
-       "r700 Hardware Rasterization",
-       NULL,
-       NULL,
-       NULL,
-       NULL,
-       r700RunNonTCLRender
-};
-
-const struct tnl_pipeline_stage _r700_tcl_stage = {
-       "r700 Hardware Transform, Clipping and Lighting",
-       NULL,
-       NULL,
-       NULL,
-       NULL,
-       r700RunTCLRender
-};
-
-const struct tnl_pipeline_stage *r700_pipeline[] = 
-{
-    &_r700_tcl_stage,
-    &_tnl_vertex_transform_stage,
-       &_tnl_normal_transform_stage,
-       &_tnl_lighting_stage,
-       &_tnl_fog_coordinate_stage,
-       &_tnl_texgen_stage,
-       &_tnl_texture_transform_stage,
-       &_tnl_vertex_program_stage,
-
-    &_r700_render_stage,
-    &_tnl_render_stage,
-    0,
-};
-
 #define CONVERT( TYPE, MACRO ) do {            \
        GLuint i, j, sz;                                \
        sz = input->Size;                               \
@@ -683,7 +542,7 @@ static void r700AlignDataToDword(GLcontext *ctx,
     attr->stride = dst_stride;
 }
 
-static void r700SetupStreams2(GLcontext *ctx, const struct gl_client_array *input[], int count)
+static void r700SetupStreams(GLcontext *ctx, const struct gl_client_array *input[], int count)
 {
        context_t *context = R700_CONTEXT(ctx);
     GLuint stride;
@@ -751,19 +610,15 @@ static void r700SetupStreams2(GLcontext *ctx, const struct gl_client_array *inpu
                 {
                 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); 
@@ -786,7 +641,6 @@ static void r700SetupStreams2(GLcontext *ctx, const struct gl_client_array *inpu
         }
     }
 
-    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);    
@@ -941,12 +795,12 @@ static void r700SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
 }
 
 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 )
+                                 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;
@@ -954,15 +808,12 @@ static GLboolean r700TryDrawPrims(GLcontext *ctx,
     struct radeon_renderbuffer *rrb;
 
     if (ctx->NewState)
-    {
         _mesa_update_state( ctx );
-    }
 
     _tnl_UpdateFixedFunctionProgram(ctx);
     r700SetVertexFormat(ctx, arrays, max_index + 1);
-    r700SetupIndexBuffer(ctx, ib);
     /* shaders need to be updated before buffers are validated */
-    r700UpdateShaders2(ctx);
+    r700UpdateShaders(ctx);
     if (!r600ValidateBuffers(ctx))
            return GL_FALSE;
 
@@ -978,10 +829,11 @@ static GLboolean r700TryDrawPrims(GLcontext *ctx,
     r700SetupFragmentProgram(ctx);
     r600UpdateTextureState(ctx);
 
-    GLuint emit_end = r700PredictRenderSize(ctx, nr_prims)
+    GLuint emit_end = r700PredictRenderSize(ctx, prim, ib, nr_prims)
                     + context->radeon.cmdbuf.cs->cdw;
 
-    r700SetupStreams2(ctx, arrays, max_index + 1);
+    r700SetupIndexBuffer(ctx, ib);
+    r700SetupStreams(ctx, arrays, max_index + 1);
 
     radeonEmitState(radeon);
 
@@ -1019,18 +871,18 @@ static GLboolean r700TryDrawPrims(GLcontext *ctx,
     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)
+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)
 {
-    GLboolean retval = GL_FALSE;
+       GLboolean retval = GL_FALSE;
 
-    /* This check should get folded into just the places that
+       /* This check should get folded into just the places that
         * min/max index are really needed.
         */
        if (!index_bounds_valid) {
@@ -1038,7 +890,7 @@ static void r700DrawPrimsRe(GLcontext *ctx,
        }
 
        if (min_index) {
-               vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r700DrawPrimsRe );
+               vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r700DrawPrims );
                return;
        }
 
@@ -1050,30 +902,6 @@ static void r700DrawPrimsRe(GLcontext *ctx,
                _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);