Array of textures
sayan1an opened this issue · 4 comments
Hi,
I'm trying to do texture mapping using ray-query. Shader side code would look something like this -
myArrayOfTextures[giveMeMaterialIdx(committedInstanceIndex)].Load(uv)
As such, I need to create an array-of-textures. Texture2DArrays aren't helpful as all slices must have the same resolution. Any suggestions or alternatives?
I found something similar here:
https://github.com/gboisse/gfx/blob/7c326e1c10a8a83b81e46c81b30263bd0f1e7ff3/examples/common/gpu_scene.cpp
I'll give it a try.
This seems to work (derived from the above example):
Shader:
Buffer<uint> gInstanceIdxToMatIdx;
Texture2D gAlbedoTextures[] : register(space99);
SamplerState gTextureSampler;
// Sample
gAlbedoTextures[gInstanceIdxToMatIdx[instanceIndex]].SampleLevel(gTextureSampler, uv, 0).xyz;
Host:
std::vector<GfxTexture> albedoTextures;
createAndPushAllTextures();
texSampler = gfxCreateSamplerState(..., D3D12_FILTER_MIN_MAG_MIP_POINT,...);
gfxProgramSetParameter(ctx, program, "gTextureSampler", texSampler);
gfxProgramSetParameter(ctx, program, "gAlbedoTextures", albedoTextures.data(), (uint32_t)albedoTextures.size());
That’s indeed the way to use bindless texturing in gfx.
Note that in some cases, you might need to add NonUniformResourceIndex()
, e.g.:
gAlbedoTextures[NonUniformResourceIndex(gInstanceIdxToMatIdx[instanceIndex])].Sample(...);
See here for more information: https://asawicki.info/news_1608_direct3d_12_-_watch_out_for_non-uniform_resource_index
Thank you for pointing that out.