r300: Remove calls to rcommonEnsureCmdBufSpace.
[mesa.git] / src / mesa / drivers / dri / r300 / r300_draw.c
index ab2287a5e253e821fa7a5053e3f2ab0fa161f162..b3d19a90c3099dabe4d52969b889ea279d6028c8 100644 (file)
@@ -31,6 +31,7 @@
 #include "main/state.h"
 #include "main/api_validate.h"
 #include "main/enums.h"
+#include "main/simple_list.h"
 
 #include "r300_reg.h"
 #include "r300_context.h"
@@ -38,6 +39,7 @@
 #include "r300_render.h"
 #include "r300_state.h"
 #include "r300_tex.h"
+#include "r300_cmdbuf.h"
 
 #include "radeon_buffer_objects.h"
 
@@ -107,12 +109,13 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
 
 #if MESA_BIG_ENDIAN
        } else { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
+               GLuint size;
                GLushort *in = (GLushort *)src_ptr;
                size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
 
                radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offet, size, 4);
 
-               assert(r300->ind_buf.bo->ptr != NULL)
+               assert(r300->ind_buf.bo->ptr != NULL);
                out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
 
                for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
@@ -326,7 +329,6 @@ static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const st
 
                type = GL_FLOAT;
 
-               r300ConvertAttrib(ctx, count, input, &r300_attr);
                if (input->StrideB == 0) {
                        r300_attr.stride = 0;
                } else {
@@ -337,41 +339,14 @@ static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const st
        } else {
                type = input->Type;
                r300_attr.dwords = (getTypeSize(type) * input->Size + 3)/ 4;
-               if (input->BufferObj->Name) {
-                       if (stride % 4 != 0) {
-                               assert(((int) input->Ptr) % input->StrideB == 0);
-                               r300AlignDataToDword(ctx, input, count, &r300_attr);
-                               r300_attr.is_named_bo = GL_FALSE;
-                       } else {
-                               r300_attr.stride = input->StrideB;
-                               r300_attr.bo_offset = (GLuint) input->Ptr;
-                               r300_attr.bo = get_radeon_buffer_object(input->BufferObj)->bo;
-                               r300_attr.is_named_bo = GL_TRUE;
-                       }
-               } else {
-                       int size;
-                       uint32_t *dst;
+               if (!input->BufferObj->Name) {
 
                        if (input->StrideB == 0) {
-                               size = getTypeSize(input->Type) * input->Size;
-                               count = 1;
                                r300_attr.stride = 0;
                        } else {
-                               size = getTypeSize(input->Type) * input->Size * count;
                                r300_attr.stride = (getTypeSize(type) * input->Size + 3) & ~3;
                        }
 
-                       radeonAllocDmaRegion(&r300->radeon, &r300_attr.bo, &r300_attr.bo_offset, size, 32);
-                       assert(r300_attr.bo->ptr != NULL);
-                       dst = (uint32_t *)ADD_POINTERS(r300_attr.bo->ptr, r300_attr.bo_offset);
-                       switch (r300_attr.dwords) {
-                               case 1: radeonEmitVec4(dst, input->Ptr, input->StrideB, count); break;
-                               case 2: radeonEmitVec8(dst, input->Ptr, input->StrideB, count); break;
-                               case 3: radeonEmitVec12(dst, input->Ptr, input->StrideB, count); break;
-                               case 4: radeonEmitVec16(dst, input->Ptr, input->StrideB, count); break;
-                               default: assert(0); break;
-                       }
-
                        r300_attr.is_named_bo = GL_FALSE;
                }
        }
@@ -466,7 +441,6 @@ static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *ar
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        struct r300_vertex_buffer *vbuf = &r300->vbuf;
-
        {
                int i, tmp;
 
@@ -490,36 +464,83 @@ static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *ar
        r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
        if (r300->fallback)
                return;
+}
 
-       {
-               int i;
+static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *input[], int count)
+{
+       r300ContextPtr r300 = R300_CONTEXT(ctx);
+       struct r300_vertex_buffer *vbuf = &r300->vbuf;
+       GLuint stride;
+       int ret;
+       int i, index;
+
+       for (index = 0; index < vbuf->num_attribs; index++) {
+               struct radeon_aos *aos = &r300->radeon.tcl.aos[index];
+               i = vbuf->attribs[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) {
+
+                       r300ConvertAttrib(ctx, count, input[i], &vbuf->attribs[index]);
+               } else {
+                       if (input[i]->BufferObj->Name) {
+                               if (stride % 4 != 0) {
+                                       assert(((intptr_t) input[i]->Ptr) % input[i]->StrideB == 0);
+                                       r300AlignDataToDword(ctx, input[i], count, &vbuf->attribs[index]);
+                                       vbuf->attribs[index].is_named_bo = GL_FALSE;
+                               } else {
+                                       vbuf->attribs[index].stride = input[i]->StrideB;
+                                       vbuf->attribs[index].bo_offset = (intptr_t) input[i]->Ptr;
+                                       vbuf->attribs[index].bo = get_radeon_buffer_object(input[i]->BufferObj)->bo;
+                                       vbuf->attribs[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(&r300->radeon, &vbuf->attribs[index].bo, &vbuf->attribs[index].bo_offset, size, 32);
+                               assert(vbuf->attribs[index].bo->ptr != NULL);
+                               dst = (uint32_t *)ADD_POINTERS(vbuf->attribs[index].bo->ptr, vbuf->attribs[index].bo_offset);
+                               switch (vbuf->attribs[index].dwords) {
+                                       case 1: radeonEmitVec4(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
+                                       case 2: radeonEmitVec8(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
+                                       case 3: radeonEmitVec12(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
+                                       case 4: radeonEmitVec16(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
+                                       default: assert(0); break;
+                               }
 
-               for (i = 0; i < vbuf->num_attribs; i++) {
-                       struct radeon_aos *aos = &r300->radeon.tcl.aos[i];
-
-                       aos->count = vbuf->attribs[i].stride == 0 ? 1 : count;
-                       aos->stride = vbuf->attribs[i].stride / sizeof(float);
-                       aos->offset = vbuf->attribs[i].bo_offset;
-                       aos->components = vbuf->attribs[i].dwords;
-                       aos->bo = vbuf->attribs[i].bo;
-
-                       radeon_cs_space_check_with_bo(r300->radeon.cmdbuf.cs,
-                                                                                 r300->vbuf.attribs[i].bo,
-                                                                                 RADEON_GEM_DOMAIN_GTT, 0);
-                       if (vbuf->attribs[i].is_named_bo) {
-                               radeon_cs_space_add_persistent_bo(r300->radeon.cmdbuf.cs,
-                                                                                                 r300->vbuf.attribs[i].bo,
-                                                                                                 RADEON_GEM_DOMAIN_GTT, 0);
                        }
                }
-               r300->radeon.tcl.aos_count = vbuf->num_attribs;
 
-               if (r300->ind_buf.bo) {
-                       radeon_cs_space_check_with_bo(r300->radeon.cmdbuf.cs,
-                                                                                 r300->ind_buf.bo,
-                                                                                 RADEON_GEM_DOMAIN_GTT, 0);
+               aos->count = vbuf->attribs[index].stride == 0 ? 1 : count;
+               aos->stride = vbuf->attribs[index].stride / sizeof(float);
+               aos->components = vbuf->attribs[index].dwords;
+               aos->bo = vbuf->attribs[index].bo;
+               aos->offset = vbuf->attribs[index].bo_offset;
+
+               if (vbuf->attribs[index].is_named_bo) {
+                       radeon_cs_space_add_persistent_bo(r300->radeon.cmdbuf.cs, r300->vbuf.attribs[index].bo, RADEON_GEM_DOMAIN_GTT, 0);
                }
        }
+
+       r300->radeon.tcl.aos_count = vbuf->num_attribs;
+       ret = radeon_cs_space_check_with_bo(r300->radeon.cmdbuf.cs, first_elem(&r300->radeon.dma.reserved)->bo, RADEON_GEM_DOMAIN_GTT, 0);
+       r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, ret);
+
 }
 
 static void r300FreeData(GLcontext *ctx)
@@ -547,6 +568,34 @@ static void r300FreeData(GLcontext *ctx)
        }
 }
 
+static GLuint r300PredictTryDrawPrimsSize(GLcontext *ctx, GLuint nr_prims)
+{
+       struct r300_context *r300 = R300_CONTEXT(ctx);
+       struct r300_vertex_buffer *vbuf = &r300->vbuf;
+       int flushed;
+       GLuint dwords;
+       GLuint state_size;
+
+       dwords = 2*CACHE_FLUSH_BUFSZ;
+       dwords += PRE_EMIT_STATE_BUFSZ;
+       dwords += (AOS_BUFSZ(vbuf->num_attribs)
+               + SCISSORS_BUFSZ
+               + FIREAOS_BUFSZ )*nr_prims;
+
+       state_size = radeonCountStateEmitSize(&r300->radeon);
+       flushed = rcommonEnsureCmdBufSpace(&r300->radeon,
+                       dwords + state_size,
+                       __FUNCTION__);
+       if (flushed)
+               dwords += radeonCountStateEmitSize(&r300->radeon);
+       else
+               dwords += state_size;
+
+       if (RADEON_DEBUG & DEBUG_PRIMS)
+               fprintf(stderr, "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
+       return dwords;
+}
+
 static GLboolean r300TryDrawPrims(GLcontext *ctx,
                                         const struct gl_client_array *arrays[],
                                         const struct _mesa_prim *prim,
@@ -558,6 +607,10 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
        struct r300_context *r300 = R300_CONTEXT(ctx);
        GLuint i;
 
+       if (RADEON_DEBUG & DEBUG_PRIMS)
+               fprintf(stderr, "%s: %u (%d-%d) cs begin at %d\n", 
+                               __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
+
        if (ctx->NewState)
                _mesa_update_state( ctx );
 
@@ -568,14 +621,6 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
 
        r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, !r300ValidateBuffers(ctx));
 
-       r300SetupIndexBuffer(ctx, ib);
-
-       /* ensure we have the cmd buf space in advance to cover
-        * the state + DMA AOS pointers */
-       rcommonEnsureCmdBufSpace(&r300->radeon,
-                           r300->radeon.hw.max_state_size + (50*sizeof(int)),
-                           __FUNCTION__);
-
        r300SetVertexFormat(ctx, arrays, max_index + 1);
 
        if (r300->fallback)
@@ -585,6 +630,17 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
 
        r300UpdateShaderStates(r300);
 
+       /* ensure we have the cmd buf space in advance to cover
+        * the state + DMA AOS pointers */
+       r300PredictTryDrawPrimsSize(ctx, nr_prims);
+
+       r300SetupIndexBuffer(ctx, ib);
+
+       r300AllocDmaRegions(ctx, arrays, max_index + 1);
+
+       if (r300->fallback)
+               return GL_FALSE;
+
        r300EmitCacheFlush(r300);
        radeonEmitState(&r300->radeon);
 
@@ -596,6 +652,10 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
 
        r300FreeData(ctx);
 
+       if (RADEON_DEBUG & DEBUG_PRIMS)
+               fprintf(stderr, "%s: %u (%d-%d) cs begin at %d\n", 
+                       __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
+
        return GL_TRUE;
 }