Sign in to follow this  
jdub

[SlimDX]Why does this code fail?

Recommended Posts

jdub    459
This below code throws an invalid operation exception when I try to write to my Vertex Buffer with DataStream.WriteRange(). The code itself doesn't produce a grid like I wanted it to but, I am not worried about that right now. I am just wondering why it is throwing an exception here:
 public Grid(Device device, int width, int height, Vector2 tileSize)
        {
            if(device == null)
                throw new InvalidOperationException("Graphics Device cannot be null");

            amountOfTiles = width * height;

            this.device = device;
            this.vertices = new VertexBuffer ( device, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2), Usage.None, UntransformedVertex.FVFFormat, Pool.Default );

            DataStream dStream = vertices.Lock ( 0,
                                                 D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2),
                                                 LockFlags.NoSystemLock );
            
            //loop through all vertical lines
            for (float y = 0; y < height * tileSize.Y; y += tileSize.Y)
            {

                //loop through all horizontal tiles
                for (float x = 0; x < width * tileSize.X; x += tileSize.X)
                {

                    Color tileColor = Color.Blue;

                    UntransformedVertex[] tileVerts = new UntransformedVertex[4];
                    
                    //(2)---------(3)
                    //|***********/*|
                    //|*********/***|
                    //|*******/*****|
                    //|*****/*******|
                    //|***/*********|
                    //|*/***********|
                    //(0)*********(1)
                    tileVerts[0] = new UntransformedVertex ( new Vector3 ( x, y, 10 ), tileColor.ToArgb() );
                    tileVerts[1] = new UntransformedVertex ( new Vector3 ( x + tileSize.X, y, 10 ), tileColor.ToArgb() );
                    tileVerts[2] = new UntransformedVertex ( new Vector3( x, y + tileSize.Y, 10 ), tileColor.ToArgb() );
                    tileVerts[3] = new UntransformedVertex( new Vector3(x + tileSize.X, y + tileSize.Y, 10), tileColor.ToArgb() );

                    dStream.WriteRange ( tileVerts );
                }
            }

            vertices.Unlock ();
            dStream.Dispose ();
        }


when I switch it with this it doesn't throw an invalid operation exception:

        public Grid(Device device, int width, int height, Vector2 tileSize)
        {
            if(device == null)
                throw new InvalidOperationException("Graphics Device cannot be null");

            amountOfTiles = width * height;

            this.device = device;
            this.vertices = new VertexBuffer ( device, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2), Usage.None, UntransformedVertex.FVFFormat, Pool.Default );

            
            //loop through all vertical lines
            for (float y = 0; y < height * tileSize.Y; y += tileSize.Y)
            {

                //loop through all horizontal tiles
                for (float x = 0; x < width * tileSize.X; x += tileSize.X)
                {

                    Color tileColor = Color.Blue;

                    DataStream dStream = vertices.Lock ( 0,
                                                         D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2),
                                                         LockFlags.NoSystemLock );

                    UntransformedVertex[] tileVerts = new UntransformedVertex[4];
                    
                    //(2)---------(3)
                    //|***********/*|
                    //|*********/***|
                    //|*******/*****|
                    //|*****/*******|
                    //|***/*********|
                    //|*/***********|
                    //(0)*********(1)
                    tileVerts[0] = new UntransformedVertex ( new Vector3 ( x, y, 10 ), tileColor.ToArgb() );
                    tileVerts[1] = new UntransformedVertex ( new Vector3 ( x + tileSize.X, y, 10 ), tileColor.ToArgb() );
                    tileVerts[2] = new UntransformedVertex ( new Vector3( x, y + tileSize.Y, 10 ), tileColor.ToArgb() );
                    tileVerts[3] = new UntransformedVertex( new Vector3(x + tileSize.X, y + tileSize.Y, 10), tileColor.ToArgb() );
                    
                    
                    dStream.WriteRange ( tileVerts );

                    vertices.Unlock ();
                    dStream.Dispose ();
                }
            }
        }


Also, would it be better, speedwise, to put all of the vertices in a 2 dimensional array and then write it all to the vertex buffer in one big WriteRange() call? (btw this code was written with SlimDX) [Edited by - jdub on December 18, 2008 9:35:00 AM]

Share this post


Link to post
Share on other sites
jdub    459
My problem lies on this line:
dStream.WriteRange ( tileVerts );

it throws an InvalidOperation exception, but doesn't say anything about the problem.

Share this post


Link to post
Share on other sites
Promit    13246
You're stepping out of the bounds of the VB somehow. The second version doesn't crash because it repeatedly writes to the beginning of the vertex buffer. Check what your X and Y is at the point of crash compared to the size.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this