As you know, Vertex Buffer can contain other properties besides Position, such as Texture Coordinate or Tangent. (at least in raterization)
DXR obtains this Vertex Buffer through BLAS, uploads it to the GPU, and performs Ray Tracing. but how does it distinguish which property is Vertex Position?
Or am I misunderstanding something?
My code is just like this. Vertex structure have Position, Texcoord, Tangent and biTangent.
std::vector<D3D12_RAYTRACING_GEOMETRY_DESC> geomDescs;
geomDescs.reserve(mMeshMap.size());
for (auto i = meshes.begin(); i != meshes.end(); ++i)
{
D3D12_RAYTRACING_GEOMETRY_DESC geomDesc = {};
geomDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geomDesc.Triangles.VertexBuffer.StartAddress = (*i)->GetVertexBufferAlloc()->GetResource()->GetGPUVirtualAddress();
geomDesc.Triangles.VertexBuffer.StrideInBytes = sizeof(Vertex);
geomDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
geomDesc.Triangles.VertexCount = (*i)->GetVertexCount();
if ((*i)->GetIndexCount() > 0)
{
geomDesc.Triangles.IndexBuffer = (*i)->GetIndexBufferAlloc()->GetResource()->GetGPUVirtualAddress();
geomDesc.Triangles.IndexFormat = DXGI_FORMAT_R32_UINT;
geomDesc.Triangles.IndexCount = (*i)->GetIndexCount();
}
geomDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;
geomDescs.push_back(geomDesc);
}
EDIT : OK, I've found that I need 'Bindless Resources', but I steel can't figure out exact way to do that.
first, how can I get right 'Index' for access Descriptor heap to get Collided triangle's attributes?
for that, I must need some Constant buffers for each BLAS, but How can I upload it and Identify in shader?
Second, do I need to upload every vertex attributes to GPU for Bindless Resources? like, Every Vertex Attributes for each BLAS Objects? in One Descriptor Heap?
I searched the DXR specs, and found the GeometryIndex
, InstanceID
, and
InstanceIndex
system values.
They're returning the current BLAS or TLAS index, and they are a user generated object, right?
What I'm supposed to do is simple. If I want to get the texture index for each BLAS Geometry, then upload the StructuredBuffer
indexed, the same as BLAS did.
I can get GeometryIndex
in Closest Hit (or Any Hit) shaders, so I can get material indices like this:
StructuredBuffer<TextureIndex> gTextureIndices;
TextureIndex currentIndex = gTextureIndices[GeometryIndex];
If you want the same BLAS but as a different material, then you can use InstanceIndex
instead of GeometryIndex
. (And StructuredBuffer
must be indexed, same as TLAS, for sure.)
UPDATE
Actually, there's an even simpler way to do this.
Make the resource for the constant buffer, initialize it as you want, and put it into the Shader Table's Hit Program.
It just works as general root constants (for each instance).
See NVIDIA DXR Sample's PerInstanceConstantBuffer
tutorial.