3

I'm writing some code that is drawing 2D sprites using D3D11 (SharpDX for WinRT apps). All of this is working, however it is not fast. Here's a bit of the code I'm using:

        // Setup local variables
        var d3dDevice = App.deviceManager.DeviceDirect3D;
        var d3dContext = App.deviceManager.ContextDirect3D;

        var vertices = SharpDX.Direct3D11.Buffer.Create(App.deviceManager.DeviceDirect3D, BindFlags.VertexBuffer, new[]
        {
            // Position                                  Colour                      UV
            x0 - OFFSET, y0 - OFFSET, 0.0f, 1.0f ,       1.0f, 1.0f, 1.0f, 1.0f,     u, v,
            x1 - OFFSET, y1 - OFFSET, 0.0f, 1.0f ,       1.0f, 1.0f, 1.0f, 1.0f,     u2, v,
            x3 - OFFSET, y3 - OFFSET, 0.0f, 1.0f ,       1.0f, 1.0f, 1.0f, 1.0f,     u, v2,
            x2 - OFFSET, y2 - OFFSET, 0.0f, 1.0f ,       1.0f, 1.0f, 1.0f, 1.0f,     u2, v2,
        });

        textureView = new ShaderResourceView( App.deviceManager.DeviceDirect3D, texture );

        // Setup the pipeline
        d3dContext.InputAssembler.SetVertexBuffers( 0, vertexBufferBinding );
        d3dContext.InputAssembler.InputLayout = layout;
        d3dContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
        d3dContext.VertexShader.SetConstantBuffer( 0, constantBuffer );
        d3dContext.VertexShader.Set( vertexShader );
        d3dContext.PixelShader.Set( pixelShader );

        if( textureView != null )
            d3dContext.PixelShader.SetShaderResource( 0, textureView );

        // Draw the quad
        d3dContext.Draw( 4, 0 );

This code is called for every sprite rendered on screen. I can not guarantee the life of each sprite. They could be in the same position next frame or a different position. There could be more, there could be less. According to my profiler, SharpDX.Direct3D11.Buffer.Create is taking 50% of the entire frame when rendering a lot of sprites. I assume there is a better way to do this but I'm struggling to get my head around what to do. Can anyone offer any suggestions for this?

Real World
  • 1,593
  • 1
  • 21
  • 46
  • Do you need additional information on this topic? I would be glad to extend my post, if you feel this question is still unanswered. – Lucius Jan 21 '13 at 14:37
  • Please feel free. I'm currently on another project so I'm unable to reproduce my problem to provide any additional information but your answer below, while very informative, seemed mostly unrelated to my original question. – Real World Jan 21 '13 at 19:34

0 Answers0