1
\$\begingroup\$

Could anybody provide working code using SharpDX with D3D11, which does instancing and uses SharpDX's content pipeline?

I have several problems there. I came to understand that one should use a SharpDX.Direct3D11.InputLayout to describe the vertex shader's input. There I already have a problem. I use SharpDX's content pipeline, so the shaders are compiled in compile time and not in runtime. Where do I get the shader's byte code then? It shouldn't be that hard, but it seems it's inaccessable from the effect object.

My other question is how can I set the created SharpDX.Direct3D11.InputLayout before the current DrawIndexedInstanced call?

\$\endgroup\$
  • \$\begingroup\$ Sorry, see answer below :) \$\endgroup\$ – ErnieDingo Jul 14 '17 at 22:55
0
\$\begingroup\$

the input layout is attached after compile, so you have defined the layout within the compiled shader code, you now need to attach the input layout. Remember, all your compile has done is create a binary file output, so you need to firstly load it (wherever it is) into a ShaderBytecode object then create in Sharpdx the relevant Shader from that bytecode.

For example:

D3D11.VertexShader m_vsShader = new SharpDX.Direct3D11.VertexShader(GlobalStatics.Device, m_vsShaderByteCode);

You then need to create your input layout (and as its instanced),

    m_bufferLayoutInstanced = new D3D11.InputLayout(GlobalStatics.Device, m_vsShaderByteCode.GetPart(SharpDX.D3DCompiler.ShaderBytecodePart.InputSignatureBlob), new[] {
             new D3D11.InputElement("POSITION", 0, DXGI.Format.R32G32B32A32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0),
                        new D3D11.InputElement("NORMAL",   0, DXGI.Format.R32G32B32A32_Float, 16, 0, D3D11.InputClassification.PerVertexData, 0),
                        new D3D11.InputElement("TEXCOORD", 0, DXGI.Format.R32G32B32A32_Float, 32, 0, D3D11.InputClassification.PerVertexData, 0),
                        new D3D11.InputElement("TEXCOORD", 1, DXGI.Format.R32G32B32A32_Float, 48, 0, D3D11.InputClassification.PerVertexData, 0),
         
                        new D3D11.InputElement("INSTANCE_TRANSFORM", 0, DXGI.Format.R32G32B32A32_Float, 0, 1, D3D11.InputClassification.PerInstanceData, 1),
                        new D3D11.InputElement("INSTANCE_TRANSFORM", 1, DXGI.Format.R32G32B32A32_Float, 16, 1, D3D11.InputClassification.PerInstanceData, 1),
                        new D3D11.InputElement("INSTANCE_TRANSFORM", 2, DXGI.Format.R32G32B32A32_Float, 32, 1, D3D11.InputClassification.PerInstanceData, 1),
                        new D3D11.InputElement("INSTANCE_TRANSFORM", 3, DXGI.Format.R32G32B32A32_Float, 48, 1 ,D3D11.InputClassification.PerInstanceData, 1),
                        new D3D11.InputElement("INSTANCE_FLOAT", 4, DXGI.Format.R32G32B32A32_Float, 64, 1 ,D3D11.InputClassification.PerInstanceData, 1)
                    });

\$\endgroup\$
-2
\$\begingroup\$

Already solved, there are some nice pitfalls, one should avoid:

  • For getting the shader's byte code, one must recompile the shader.
  • To set the input layout, one should cast the device to a SharpDX.Direct3D11.Device and use it's immediate context. Note that existing vertex buffer layout can be used to create the begining of the input layout.
  • One must also call the DrawIndexedInstanced method on the immediate context itself. Calling this method on a SharpDX.Toolkit.Graphics.GraphicsDevice will result in an incorrect parameter exception.
\$\endgroup\$
  • 1
    \$\begingroup\$ You claim to have solved the problem you presented (instancing), but this post doesn't actually answer that very well. It would be a much better answer if you actually provided an overview of the technique you used, since that's essentially what your original question posed. \$\endgroup\$ – Josh Apr 14 '14 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.