Per Instance Textures, and Vertex And Pixel Shaders RRS feed

  • Question

  • How do you implement per instance textures, vertex shaders, and pixel shaders?

    1. Two different model templates in Vertex Buffer, Square & Triangle
    2. Instance Buffer with [n] instances of type Square or Triangle.

    How do I add a Texture, VertexShader or PixelShader ID to the buffer data so that HLSL or the InputAssembly can determine which Shader to use at draw time?

    A. How do I "Set" multiple Pixel and Vertex Shaders into the DeviceContext, and how do I tell DirectX to "switch" the Vertex Shader that is loaded at render time?

    B. How many Shaders of each type, (Vertex, Pixel, Hull, etc), can I associate with model templates in the default Vertex  Buffer?

    C. Can I use some sort of Shader Selector in HLSL?

    Related C++ Code

    When I create an input layout, can I do this without specifying an actual Vertex Shader, or somehow specify more than one?

    NS::ThrowIfFailed( result = NS::DeviceManager::Device->CreateInputLayout(
    NS::ModelRenderer::InitialElementDescription, 2, vertexShaderFile->Data, vertexShaderFile->Length, & NS::ModelRenderer::StaticInputLayout) );

    When I set the VertexShader and PixelShader, how do I associate them with a particular model in my VertexBuffer? Is it possible to set more than one of each?

    DeviceManager::DeviceContext->VSSetShader(ModelRenderer::StaticVertexShader.Get(), nullptr, 0);
    DeviceManager::DeviceContext->PSSetShader(ModelRenderer::StaticPixelShader.Get(), nullptr

    • Edited by e kohen Monday, January 28, 2013 7:17 AM Clarified Question
    Monday, January 28, 2013 4:55 AM

All replies

  • You cannot use more than one shader of each type per Draw call. A Draw call executes each bound shader for the geometry it finds in the bound buffers. If you want different textures, you would add data about which texture you wanted into the per-instance data and then have the pixel shader choose the correct texture accordingly (either via a texture array or by using conditional branching).

    XNA/DirectX MVP | Website | Blog | @mikebmcl

    • Edited by MikeBMcL Saturday, February 2, 2013 8:32 AM
    • Proposed as answer by MikeBMcL Saturday, February 2, 2013 8:32 AM
    Saturday, February 2, 2013 8:32 AM