What is a good way to batch render multiple quads with different textures in Direct3D 11?

291 Views Asked by At

I want to create a batch renderer that can switch between multiple textures of different sizes per instance. I am attempting to create a 2D renderer and just want to batch render textured quads with different textures. I don't want to combine these textures into one texture atlas. I was watching this video which shows the implementation of this exact thing in OpenGL, but I cannot seem to implement the equivalent functionality in DirectX 11. In this OpenGL implementation, multiple textures are bound to different texture slots, and an index is passed per instance that switches between the textures.

in vec2 v_TexCoord;
in float v_TexIndex;

uniform sampler2D u_Textures[2];

void main()
{
    int index = int(v_TexIndex);
    o_Color = texture(u_Textures[index], v_TexCoord);
}

My first approach was to attempt the same thing. I loaded my textures into different ID3D11ShaderResourceViews and uploaded them using PSSetShaderResources like this

ID3D11ShaderResourceViews *resourceViews[2] = {texResource1, texResource2};
deviceContext->PSSetShaderResources(0, 2, resourceViews);

and tried to use this code in the pixel shader

Texture2D textures[3];
SamplerState sampler;

float4 main(VertexInput input) : SV_Target
{
    int index = input.TexIndex;
    return textures[index].Sample(sampler, index);
}

but I got the error "X3512: sampler array index must be a literal expression". When, instead of feeding the pixel shader the index and I instead hard code a number like 0 or 1, the respective texture appears on screen. It appears DirectX doesn't support this feature that OpenGL does. I've seen solutions like using Texture2DArrays, but as far as I'm aware, the textures in those arrays have to be the same size, unlike OpenGL. I've also seen solutions using switch statements, but that would get large if I wanted to bind eight textures at the same time, for example. I also don't want it to raise performance issues. I am looking for any help that could point me in the correct direction towards implementing this myself.

0

There are 0 best solutions below