We are currently in the process of converting the website to the new design. Some pages, like this one, are still broken. We appreciate your patience.
Handmade Hero»Forums»Code
Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call

I was wondering how Casey ended up drawing multiple textures in one draw call? I remember he talked about that texture atlases are just one way of doing it, but never saw what Casey did.

My only understanding of how to do it is use a texture array and have an index for each instance. Is this a reasonable solution? What's the usual approach without using texture atlases? Are bindless textures relate to this?

Thanks 🙂

Mārtiņš Možeiko
2583 posts / 2 projects
Drawing multiple textures in one draw call
Edited by Mārtiņš Možeiko on

He's doing array texture for that.

Alternative to that is doing texture atlas - then you need to be careful with padding & mipmaps. If you don't need mipmaps, then things are simple - do 1 extra padding if you're doing linear filtering (or do simple math in shader to clamp to item borders).

Bindless textures also help if you're ok with non-core extension, and not supporting older GPU's / driver versions. It allows to pass handle to texture inside vertex attribute or vertex buffer or any other kind of data structure you have in UBO/SSBO or otherwise. This way you can use different texture per each element you draw in the same draw call.

Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call
Edited by Oliver Marsh on

Thanks Martins, is the texture array Casey used sampler2darray?

Mārtiņš Možeiko
2583 posts / 2 projects
Drawing multiple textures in one draw call
Edited by Mārtiņš Možeiko on
Replying to OliverMarsh (#25748)

Yes. You need to use sampler2DArray for array texture.

Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call
Edited by Oliver Marsh on

Sorry to bump this again, what's the equivalent to sampler2Darray in d3d11?

Mārtiņš Možeiko
2583 posts / 2 projects
Drawing multiple textures in one draw call
Edited by Mārtiņš Možeiko on

D3D11 has separate sampler state from texture. For sample state you do your regular sampler object - that does not depend on texture type.

Then you define Texture2DArray on which you will use any sampler you want and pass float3 (u,v,index) for Sample() method: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-sample

Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call
Replying to mmozeiko (#25805)

thanks martins :)

Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call

Hi, just a couple more questions :)

How did Casey manage the texture2Darrays? Did he alter the texture data when the game needed to draw a new texture that hadn't been loaded yet? Would this be a map/unmap call in d3d11?

I should watch the specific videos casey did this, does anyone know which these ones are?

Thankyou :)

Mārtiņš Možeiko
2583 posts / 2 projects
Drawing multiple textures in one draw call
Replying to OliverMarsh (#25820)

Last time I saw the that part code it updated slice of texture with glTexSubImage3D whenever needed.

On D3D you have different options - if you're updating often, then probably having staging texture (because you cannot write from cpu to default textures), then mapping/memcpy/unmap & copy to actual texture is probably best way. If that happens only occasionally, then UpdateSubresource is fine.

Oliver Marsh
195 posts / 1 project
Olster1.github.io
Drawing multiple textures in one draw call

Thanks again Martins