mesa/r300classic: Fix dri2Invalidate/radeon_prepare_render for page flipping.
[mesa.git] / src / mesa / drivers / dri / r300 / r300_draw.c
index 2ac898bd59de6308aa24e4666f9163414c779ba2..0c4d8537c61a53627edd1fb73def61c37fae5849 100644 (file)
@@ -29,7 +29,6 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/state.h"
-#include "main/api_validate.h"
 #include "main/enums.h"
 #include "main/simple_list.h"
 
 #include "r300_cmdbuf.h"
 
 #include "radeon_buffer_objects.h"
+#include "radeon_common_context.h"
 
 #include "tnl/tnl.h"
 #include "tnl/t_vp_build.h"
 #include "vbo/vbo_context.h"
-#include "swrast/swrast.h"
-#include "swrast_setup/swrast_setup.h"
 
 
 static int getTypeSize(GLenum type)
@@ -55,6 +53,8 @@ static int getTypeSize(GLenum type)
        switch (type) {
                case GL_DOUBLE:
                        return sizeof(GLdouble);
+               case GL_HALF_FLOAT:
+                       return sizeof(GLhalfARB);
                case GL_FLOAT:
                        return sizeof(GLfloat);
                case GL_INT:
@@ -75,7 +75,7 @@ static int getTypeSize(GLenum type)
        }
 }
 
-static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
+static void r300FixupIndexBuffer(struct gl_context *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        GLvoid *src_ptr;
@@ -90,12 +90,16 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
        }
        src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
 
+       radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
+                       "%s: Fixing index buffer format. type %d\n",
+                       __func__, mesa_ind_buf->type);
+
        if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) {
                GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
                GLubyte *in = (GLubyte *)src_ptr;
 
                radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
-
+               radeon_bo_map(r300->ind_buf.bo, 1);
                assert(r300->ind_buf.bo->ptr != NULL);
                out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
 
@@ -106,7 +110,7 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
                if (i < mesa_ind_buf->count) {
                        *out++ = in[i];
                }
-
+               radeon_bo_unmap(r300->ind_buf.bo);
 #if MESA_BIG_ENDIAN
        } else { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
                GLushort *in = (GLushort *)src_ptr;
@@ -115,6 +119,7 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
                radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo,
                                     &r300->ind_buf.bo_offset, size, 4);
 
+               radeon_bo_map(r300->ind_buf.bo, 1);
                assert(r300->ind_buf.bo->ptr != NULL);
                out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
 
@@ -125,6 +130,7 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
                if (i < mesa_ind_buf->count) {
                        *out++ = in[i];
                }
+               radeon_bo_unmap(r300->ind_buf.bo);
 #endif
        }
 
@@ -137,7 +143,7 @@ static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
 }
 
 
-static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
+static void r300SetupIndexBuffer(struct gl_context *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
 
@@ -145,6 +151,7 @@ static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
                r300->ind_buf.bo = NULL;
                return;
        }
+       radeon_print(RADEON_RENDER, RADEON_TRACE, "%s\n", __func__);
 
 #if MESA_BIG_ENDIAN
        if (mesa_ind_buf->type == GL_UNSIGNED_INT) {
@@ -167,10 +174,12 @@ static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
 
                radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
 
+               radeon_bo_map(r300->ind_buf.bo, 1);
                assert(r300->ind_buf.bo->ptr != NULL);
                dst_ptr = ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
-               _mesa_memcpy(dst_ptr, src_ptr, size);
+               memcpy(dst_ptr, src_ptr, size);
 
+               radeon_bo_unmap(r300->ind_buf.bo);
                r300->ind_buf.is_32bit = (mesa_ind_buf->type == GL_UNSIGNED_INT);
                r300->ind_buf.count = mesa_ind_buf->count;
 
@@ -210,7 +219,7 @@ static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer
  * Convert attribute data type to float
  * If the attribute uses named buffer object replace the bo with newly allocated bo
  */
-static void r300ConvertAttrib(GLcontext *ctx, int count, const struct gl_client_array *input, struct vertex_attribute *attr)
+static void r300ConvertAttrib(struct gl_context *ctx, int count, const struct gl_client_array *input, struct vertex_attribute *attr)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        const GLvoid *src_ptr;
@@ -236,12 +245,14 @@ static void r300ConvertAttrib(GLcontext *ctx, int count, const struct gl_client_
        }
 
        radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, sizeof(GLfloat) * input->Size * count, 32);
+       radeon_bo_map(attr->bo, 1);
        dst_ptr = (GLfloat *)ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
 
-       if (RADEON_DEBUG & DEBUG_FALLBACKS) {
-               fprintf(stderr, "%s: Converting vertex attributes, attribute data format %x,", __FUNCTION__, input->Type);
-               fprintf(stderr, "stride %d, components %d\n", stride, input->Size);
-       }
+       radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
+                       "%s: Converting vertex attributes, attribute data format %x,"
+                       "stride %d, components %d\n"
+                       , __FUNCTION__, input->Type
+                       , stride, input->Size);
 
        assert(src_ptr != NULL);
 
@@ -273,12 +284,13 @@ static void r300ConvertAttrib(GLcontext *ctx, int count, const struct gl_client_
                        break;
        }
 
+       radeon_bo_unmap(attr->bo);
        if (mapped_named_bo) {
                ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
        }
 }
 
-static void r300AlignDataToDword(GLcontext *ctx, const struct gl_client_array *input, int count, struct vertex_attribute *attr)
+static void r300AlignDataToDword(struct gl_context *ctx, const struct gl_client_array *input, int count, struct vertex_attribute *attr)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        const int dst_stride = (input->StrideB + 3) & ~3;
@@ -287,18 +299,22 @@ static void r300AlignDataToDword(GLcontext *ctx, const struct gl_client_array *i
 
        radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, size, 32);
 
+       radeon_bo_map(attr->bo, 1);
+
        if (!input->BufferObj->Pointer) {
                ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
                mapped_named_bo = GL_TRUE;
        }
 
+       radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT, "%s. Vertex alignment doesn't match hw requirements.\n", __func__);
+
        {
                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);
+                       memcpy(dst_ptr, src_ptr, input->StrideB);
                        src_ptr += input->StrideB;
                        dst_ptr += dst_stride;
                }
@@ -308,17 +324,19 @@ static void r300AlignDataToDword(GLcontext *ctx, const struct gl_client_array *i
                ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
        }
 
+       radeon_bo_unmap(attr->bo);
        attr->stride = dst_stride;
 }
 
-static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const struct gl_client_array *input)
+static void r300TranslateAttrib(struct gl_context *ctx, GLuint attr, int count, const struct gl_client_array *input)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        struct r300_vertex_buffer *vbuf = &r300->vbuf;
-       struct vertex_attribute r300_attr;
+       struct vertex_attribute r300_attr = { 0 };
        GLenum type;
        GLuint stride;
 
+       radeon_print(RADEON_RENDER, RADEON_TRACE, "%s\n", __func__);
        stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
 
        if (input->Type == GL_DOUBLE || input->Type == GL_UNSIGNED_INT || input->Type == GL_INT ||
@@ -366,6 +384,18 @@ static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const st
                        r300_attr._signed = 0;
                        r300_attr.normalize = 0;
                        break;
+               case GL_HALF_FLOAT:
+                       switch (input->Size) {
+                               case 1:
+                               case 2:
+                                       r300_attr.data_type = R300_DATA_TYPE_FLT16_2;
+                                       break;
+                               case 3:
+                               case 4:
+                                       r300_attr.data_type = R300_DATA_TYPE_FLT16_4;
+                                       break;
+                       }
+                       break;
                case GL_SHORT:
                        r300_attr._signed = 1;
                        r300_attr.normalize = input->Normalized;
@@ -437,10 +467,11 @@ static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const st
        ++vbuf->num_attribs;
 }
 
-static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *arrays[], int count)
+static void r300SetVertexFormat(struct gl_context *ctx, const struct gl_client_array *arrays[], int count)
 {
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        struct r300_vertex_buffer *vbuf = &r300->vbuf;
+       radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s\n", __func__);
        {
                int i, tmp;
 
@@ -466,13 +497,16 @@ static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *ar
                return;
 }
 
-static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *input[], int count)
+static void r300AllocDmaRegions(struct gl_context *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;
+       radeon_print(RADEON_RENDER, RADEON_VERBOSE,
+                       "%s: count %d num_attribs %d\n",
+                       __func__, count, vbuf->num_attribs);
 
        for (index = 0; index < vbuf->num_attribs; index++) {
                struct radeon_aos *aos = &r300->radeon.tcl.aos[index];
@@ -489,8 +523,7 @@ static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *in
                        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);
+                               if (stride % 4 != 0 || (intptr_t)input[i]->Ptr % 4 != 0) {
                                        r300AlignDataToDword(ctx, input[i], count, &vbuf->attribs[index]);
                                        vbuf->attribs[index].is_named_bo = GL_FALSE;
                                } else {
@@ -513,6 +546,7 @@ static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *in
                                }
 
                                radeonAllocDmaRegion(&r300->radeon, &vbuf->attribs[index].bo, &vbuf->attribs[index].bo_offset, size, 32);
+                               radeon_bo_map(vbuf->attribs[index].bo, 1);
                                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) {
@@ -522,6 +556,7 @@ static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *in
                                        case 4: radeonEmitVec16(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
                                        default: assert(0); break;
                                }
+                               radeon_bo_unmap(vbuf->attribs[index].bo);
 
                        }
                }
@@ -543,12 +578,13 @@ static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *in
 
 }
 
-static void r300FreeData(GLcontext *ctx)
+static void r300FreeData(struct gl_context *ctx)
 {
        /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
         * to prevent double unref in radeonReleaseArrays
         * called during context destroy
         */
+       radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s\n", __func__);
        r300ContextPtr r300 = R300_CONTEXT(ctx);
        {
                int i;
@@ -568,18 +604,28 @@ static void r300FreeData(GLcontext *ctx)
        }
 }
 
-static GLuint r300PredictTryDrawPrimsSize(GLcontext *ctx, GLuint nr_prims)
+static GLuint r300PredictTryDrawPrimsSize(struct gl_context *ctx,
+               GLuint nr_prims, const struct _mesa_prim *prim)
 {
        struct r300_context *r300 = R300_CONTEXT(ctx);
        struct r300_vertex_buffer *vbuf = &r300->vbuf;
        GLboolean flushed;
        GLuint dwords;
        GLuint state_size;
+       int i;
+       GLuint extra_prims = 0;
+
+       /* Check for primitive splitting. */
+       for (i = 0; i < nr_prims; ++i) {
+               const GLuint num_verts =  r300NumVerts(r300, prim[i].count, prim[i].mode);
+               extra_prims += num_verts/(65535 - 32);
+       }
+       nr_prims += extra_prims;
 
        dwords = 2*CACHE_FLUSH_BUFSZ;
        dwords += PRE_EMIT_STATE_BUFSZ;
        dwords += (AOS_BUFSZ(vbuf->num_attribs)
-               + SCISSORS_BUFSZ
+               + SCISSORS_BUFSZ*2
                + FIREAOS_BUFSZ )*nr_prims;
 
        state_size = radeonCountStateEmitSize(&r300->radeon);
@@ -591,12 +637,11 @@ static GLuint r300PredictTryDrawPrimsSize(GLcontext *ctx, GLuint nr_prims)
        else
                dwords += state_size;
 
-       if (RADEON_DEBUG & DEBUG_PRIMS)
-               fprintf(stderr, "%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 r300TryDrawPrims(GLcontext *ctx,
+static GLboolean r300TryDrawPrims(struct gl_context *ctx,
                                         const struct gl_client_array *arrays[],
                                         const struct _mesa_prim *prim,
                                         GLuint nr_prims,
@@ -607,8 +652,7 @@ 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", 
+       radeon_print(RADEON_RENDER, RADEON_NORMAL, "%s: %u (%d-%d) cs begin at %d\n",
                                __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
 
        if (ctx->NewState)
@@ -632,7 +676,8 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
 
        /* ensure we have the cmd buf space in advance to cover
         * the state + DMA AOS pointers */
-       r300PredictTryDrawPrimsSize(ctx, nr_prims);
+       GLuint emit_end = r300PredictTryDrawPrimsSize(ctx, nr_prims, prim)
+               + r300->radeon.cmdbuf.cs->cdw;
 
        r300SetupIndexBuffer(ctx, ib);
 
@@ -652,14 +697,17 @@ static GLboolean r300TryDrawPrims(GLcontext *ctx,
 
        r300FreeData(ctx);
 
-       if (RADEON_DEBUG & DEBUG_PRIMS)
-               fprintf(stderr, "%s: %u (%d-%d) cs ending at %d\n",
+       radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s: %u (%d-%d) cs ending at %d\n",
                        __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
 
+       if (emit_end < r300->radeon.cmdbuf.cs->cdw)
+               WARN_ONCE("Rendering was %d commands larger than predicted size."
+                               " We might overflow  command buffer.\n", r300->radeon.cmdbuf.cs->cdw - emit_end);
+
        return GL_TRUE;
 }
 
-static void r300DrawPrims(GLcontext *ctx,
+static void r300DrawPrims(struct gl_context *ctx,
                         const struct gl_client_array *arrays[],
                         const struct _mesa_prim *prim,
                         GLuint nr_prims,
@@ -669,6 +717,10 @@ static void r300DrawPrims(GLcontext *ctx,
                         GLuint max_index)
 {
        GLboolean retval;
+       struct r300_context *r300 = R300_CONTEXT(ctx);
+       radeonContextPtr radeon = &r300->radeon;
+
+       radeon_prepare_render(radeon);
 
        /* This check should get folded into just the places that
         * min/max index are really needed.
@@ -678,6 +730,9 @@ static void r300DrawPrims(GLcontext *ctx,
        }
 
        if (min_index) {
+               radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
+                               "%s: Rebasing primitives. %p nr_prims %d min_index %u max_index %u\n",
+                               __func__, prim, nr_prims, min_index, max_index);
                vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims );
                return;
        }
@@ -690,7 +745,7 @@ static void r300DrawPrims(GLcontext *ctx,
                _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
 }
 
-void r300InitDraw(GLcontext *ctx)
+void r300InitDraw(struct gl_context *ctx)
 {
        struct vbo_context *vbo = vbo_context(ctx);