
[ad_1]
I believe TLAS is currupted or initialized fallacious, however cannot discover any errors.
BuildRaytracingAccelerationStructure operate would not throw any error in debug layer, it simply throw Device Remove after first body.
When I take away TraceRay from the shader, some other issues are works properly however with out accessing RaytracingAccelerationStructure.
here is how I initialize my Acceleration constructions.
(and github handle for full code : https://github.com/kcjsend2/Chulsu/tree/essential/Chulsu)
first, load single triangle mesh.
void AssetManager::LoadTestTriangleInstance(ID3D12Device5* system, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker& tracker)
{
Vertex v1, v2, v3;
v1.place = { 0, 1, 0 };
v2.place = { 0.866f, -0.5f, 0 };
v3.place = { -0.866f, -0.5f, 0 };
const Vertex vertices[] = { v1, v2, v3 };
shared_ptr<Instance> occasion = make_shared<Instance>();
SubMesh subMesh;
subMesh.InitializeBuffers(system, cmdList, alloc, tracker, *this, sizeof(Vertex), NULL, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, vertices, 3, NULL, 0);
vector<SubMesh> subMeshes;
subMeshes.push_back(subMesh);
auto mesh = make_shared<Mesh>(subMeshes);
mMeshMap["Triangle"] = mesh;
instance->SetMesh(mesh);
instance->Update();
mInstances.push_back(occasion);
}
And Build BLAS with triangle.
void AssetManager::BuildBLAS(ID3D12Device5* system, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker tracker)
{
std::vector<D3D12_RAYTRACING_GEOMETRY_DESC> geomDescs;
geomDescs.reserve(mMeshMap.dimension());
for (auto i = mMeshMap.start(); i != mMeshMap.finish(); ++i)
{
auto mesh = i->second;
auto subMeshes = mesh->GetSubMeshes();
for (auto j = subMeshes.start(); j != subMeshes.finish(); ++j)
{
D3D12_RAYTRACING_GEOMETRY_DESC geomDesc = {};
geomDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geomDesc.Triangles.VertexBuffer.StartAddress = (*j).GetVertexBufferAlloc()->GetUseful resource()->GetGPUVirtualAddress();
geomDesc.Triangles.VertexBuffer.StrideInBytes = sizeof(Vertex);
geomDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
geomDesc.Triangles.VertexRely = (*j).GetVertexRely();
if ((*j).GetIndexCount() > 0)
{
geomDesc.Triangles.IndexBuffer = (*j).GetIndexBufferAlloc()->GetUseful resource()->GetGPUVirtualAddress();
geomDesc.Triangles.IndexFormat = DXGI_FORMAT_R32_UINT;
geomDesc.Triangles.IndexCount = (*j).GetIndexCount();
}
geomDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;
geomDescs.push_back(geomDesc);
}
// Get the scale necessities for the scratch and AS buffers
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
inputs.NumDescs = geomDescs.dimension();
inputs.pGeometryDescs = geomDescs.knowledge();
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO data = {};
device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &data);
// Create the buffers. They have to help UAV, and since we're going to instantly use them, we create them with an unordered-access state
AccelerationStructureBuffers buffers;
buffers.mScratch = CreateUseful resource(system, cmdList, alloc, tracker, NULL, data.ScratchDataSizeInBytes, 1,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
buffers.mResult = CreateUseful resource(system, cmdList, alloc, tracker, NULL, data.ResultDataMaxSizeInBytes, 1,
D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
// Create the bottom-level AS
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC asDesc = {};
asDesc.Inputs = inputs;
asDesc.DestAccelerationStructureKnowledge = buffers.mResult->GetUseful resource()->GetGPUVirtualAddress();
asDesc.ScratchAccelerationStructureKnowledge = buffers.mScratch->GetUseful resource()->GetGPUVirtualAddress();
cmdList->BuildRaytracingAccelerationStructure(&asDesc, 0, nullptr);
// We have to insert a UAV barrier earlier than utilizing the acceleration constructions in a raytracing operation
D3D12_RESOURCE_BARRIER uavBarrier = {};
uavBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
uavBarrier.UAV.pResource = buffers.mResult->GetUseful resource();
cmdList->ResourceBarrier(1, &uavBarrier);
mesh->SetBLAS(buffers);
}
}
now for TLAS…
void AssetManager::BuildTLAS(ID3D12Device5* system, ID3D12GraphicsCommandList4* cmdList, ComPtr<D3D12MA::Allocator> alloc, ResourceStateTracker tracker, UINT& tlasSize)
{
// First, get the scale of the TLAS buffers and create them
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
inputs.NumDescs = mInstances.dimension();
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO data;
device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &data);
// Create the buffers
AccelerationStructureBuffers buffers;
buffers.mScratch = CreateUseful resource(system, cmdList, alloc, tracker, NULL, data.ScratchDataSizeInBytes, 1,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
buffers.mResult = CreateUseful resource(system, cmdList, alloc, tracker, NULL, data.ResultDataMaxSizeInBytes, 1,
D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
tlasSize = data.ResultDataMaxSizeInBytes;
// The occasion desc needs to be inside a buffer, create and map the buffer
buffers.mInstanceDesc = CreateUseful resource(system, cmdList, alloc, tracker, NULL, sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * mInstances.dimension(), 1,
D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_DIMENSION_BUFFER, DXGI_FORMAT_UNKNOWN, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE, D3D12_HEAP_TYPE_UPLOAD);
D3D12_RAYTRACING_INSTANCE_DESC* instanceDescs;
buffers.mInstanceDesc->GetUseful resource()->Map(0, nullptr, (void**)&instanceDescs);
ZeroMemory(instanceDescs, sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * mInstances.dimension());
// The transformation matrices for the situations
XMFLOAT4X4 rework;
rework = Matrix4x4::Identity4x4(); // Identity
for (uint32_t i = 0; i < mInstances.dimension(); i++)
{
instanceDescs[i].InstanceID = i; // This worth shall be uncovered to the shader through InstanceID()
instanceDescs[i].InstanceContributionToHitGroupIndex = i; // This is the offset contained in the shader-table. Since we now have distinctive constant-buffer for every occasion, we'd like a unique offset
instanceDescs[i].Flags = D3D12_RAYTRACING_INSTANCE_FLAG_NONE;
XMFLOAT4X4 m = Matrix4x4::Transpose(rework);
memcpy(instanceDescs[i].Transform, &m, sizeof(instanceDescs[i].Transform));
instanceDescs[i].AccelerationStructure = mInstances[i]->GetMesh()->GetBLAS().mResult->GetUseful resource()->GetGPUVirtualAddress();
instanceDescs[i].InstanceMasks = 0xFF;
}
// Unmap
buffers.mInstanceDesc->GetUseful resource()->Unmap(0, nullptr);
// Create the TLAS
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC asDesc = {};
asDesc.Inputs = inputs;
asDesc.Inputs.InstanceDescs = buffers.mInstanceDesc->GetUseful resource()->GetGPUVirtualAddress();
asDesc.DestAccelerationStructureKnowledge = buffers.mResult->GetUseful resource()->GetGPUVirtualAddress();
asDesc.ScratchAccelerationStructureKnowledge = buffers.mScratch->GetUseful resource()->GetGPUVirtualAddress();
cmdList->BuildRaytracingAccelerationStructure(&asDesc, 0, nullptr);
// We have to insert a UAV barrier earlier than utilizing the acceleration constructions in a raytracing operation
D3D12_RESOURCE_BARRIER uavBarrier = {};
uavBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
uavBarrier.UAV.pResource = buffers.mResult->GetUseful resource();
cmdList->ResourceBarrier(1, &uavBarrier);
mTLAS = buffers;
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.RaytracingAccelerationStructure.Location = mTLAS.mResult->GetUseful resource()->GetGPUVirtualAddress();
//Currently simply do that...
device->CreateShaderResourceView(nullptr, &srvDesc, GetIndexedCPUHandle(mHeapCurrentIndex));
mHeapCurrentIndex++;
}
I attempt to debug with PIX, nevertheless it simply says AS is properly uploaded on Descriptor Heap, and there is nothing I can get about what’s fallacious with AS’s inside.
[ad_2]