Skip to content

Final Submission #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d0a1348
working on root signatures
micallef25 Oct 17, 2019
74797eb
help
micallef25 Oct 17, 2019
aa172fd
Add files via upload
micallef25 Oct 19, 2019
e3b09b2
Update README.md
micallef25 Oct 19, 2019
274fc7f
Update README.md
micallef25 Oct 19, 2019
c561603
Update README.md
micallef25 Oct 20, 2019
6d862f8
Update README.md
micallef25 Oct 20, 2019
5fb77c0
Update README.md
micallef25 Oct 20, 2019
c41d495
Update README.md
micallef25 Oct 20, 2019
3171ab4
Update README.md
micallef25 Oct 20, 2019
57f4f6f
Update README.md
micallef25 Oct 20, 2019
e16ee7a
3.1,3.2,3.3 may be working
micallef25 Oct 21, 2019
e75757b
Merge branch 'master' of https://github.com/micallef25/Project5-Direc…
micallef25 Oct 21, 2019
43f94cd
Update README.md
micallef25 Oct 27, 2019
4e809ab
idk shit works I guess
micallef25 Oct 27, 2019
7e6c2a2
Merge branch 'master' of https://github.com/micallef25/Project5-Direc…
micallef25 Oct 27, 2019
7ce759e
Update README.md
micallef25 Oct 27, 2019
affa4ed
Update README.md
micallef25 Oct 27, 2019
c5ca751
Update README.md
micallef25 Oct 27, 2019
653c02f
Update README.md
micallef25 Oct 27, 2019
94d36c4
Update README.md
micallef25 Oct 27, 2019
618e42d
Update README.md
micallef25 Oct 27, 2019
bb4b256
Update README.md
micallef25 Oct 27, 2019
3ad1298
Update README.md
micallef25 Oct 27, 2019
61517c7
Update README.md
micallef25 Oct 27, 2019
0e84216
add perforamnce analysis
micallef25 Oct 27, 2019
3fa93be
Merge branch 'master' of https://github.com/micallef25/Project5-Direc…
micallef25 Oct 27, 2019
3bc08dc
Update README.md
micallef25 Oct 27, 2019
c4ce480
Update README.md
micallef25 Oct 27, 2019
1c7681b
Update README.md
micallef25 Oct 27, 2019
cd73632
Update README.md
micallef25 Oct 27, 2019
1c0f2fa
Update README.md
micallef25 Oct 27, 2019
fa82889
Update README.md
micallef25 Oct 27, 2019
1527677
Update README.md
micallef25 Oct 27, 2019
853e31e
Update README.md
micallef25 Oct 27, 2019
03b73cb
Update README.md
micallef25 Oct 27, 2019
29e3083
Update README.md
micallef25 Oct 27, 2019
26ae2c6
Update README.md
micallef25 Oct 27, 2019
3a9e561
Update README.md
micallef25 Oct 27, 2019
b93da00
Update README.md
micallef25 Oct 27, 2019
0135851
Update README.md
micallef25 Oct 27, 2019
cf3212f
Update README.md
micallef25 Oct 27, 2019
615fe59
Update README.md
micallef25 Oct 27, 2019
400a1a6
fixed metaball bug
micallef25 Oct 27, 2019
33a0ee9
Merge branch 'master' of https://github.com/micallef25/Project5-Direc…
micallef25 Oct 27, 2019
d0bdece
Update README.md
micallef25 Oct 27, 2019
18e09f2
Update README.md
micallef25 Oct 27, 2019
a0bbab1
adding gifa
micallef25 Oct 27, 2019
ebdfce9
adding gifa
micallef25 Oct 27, 2019
a0c8659
Update README.md
micallef25 Oct 27, 2019
b6b120c
adding gifa
micallef25 Oct 27, 2019
45079f7
Update README.md
micallef25 Oct 27, 2019
da37142
Update README.md
micallef25 Oct 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 68 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
**University of Pennsylvania, CIS 565: GPU Programming and Architecture,
Project 5 - DirectX Procedural Raytracing**

* (TODO) YOUR NAME HERE
* (TODO) [LinkedIn](), [personal website](), [twitter](), etc.
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Eric Micallef
* https://www.linkedin.com/in/eric-micallef-99291714b/

* Tested on: Windows 10, i5, Nvidia GTX1660 (Personal)

### (TODO: Your README)
- [Overview](#Overview)
- [Performance Analysis](#Performance-Analysis)
- [Debilitating Bugs/Bloopers](#Debilitating-Bugs-AKA-Bloopers-In-CIS565-World)
- [References](#References)

# Overview

Special thanks to Ziad. He is probably sick of seeing me in his OH and on Piazza. Without him I would have thrown my desktop out the window.

In this project we used DirectX12 to implement a basic path tracer. In this homework we were asked to implement the CPU side of DirectX12 as well as the GPU logic of raytracing in DirectX12. Below you will see some animations of the ray tracer in motion.

![](images/sceenshot.PNG)

![](images/done.gif)

![](images/lighting.gif)

![](images/pivot.gif)

# Performance Analysis

Below are some data points after manipulating the recursion depth. As you can see, the further we follow the ray slower our application becomes. I also did not see any noticeable rendering difference with these increasing recursion depths.

![](images/FPS.png)

# Debilitating Bugs AKA Bloopers In CIS565 World

![](images/looks_close_enough.PNG)

After watching my program crash 10000000000000000000 times because my cpu side was wrong, finally I got something up... but then I had to sadly go back and fix alot of stuff ...

![](images/fuzzy.PNG)

turns out there was alot wrong here ... alot ... But I do like the darker purple and darker yellow colors!

![](images/wtf.PNG)

my lerping for fading was off.. way offf.... Lerping is not to be confused with the ever popular LARPing.

![](images/weird_balls.gif)

If you look you can see that the two purple balls move in a very odd manner. was kind of neat.

problem was my logic for keeping track of closet hit was a bit off. I was keeping track of tmax, a sensical person would have looked at the function return and saw it was evaluating thit... I became sensical after about 2 hours of debugging.

![](images/floatvsfloat3.gif)

This is what happens when you accidentally return a float instead of float3 ):

# References

Ziad ... without him I would have thrown my computer out the window

http://viclw17.github.io/2018/11/29/raymarching-algorithm/

https://www.freecodecamp.org/news/understanding-linear-interpolation-in-ui-animations-74701eb9957c/

https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection

https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-box-intersection

https://www.scratchapixel.com/lessons/3d-basic-rendering/phong-shader-BRDF

https://www.scratchapixel.com/lessons/advanced-rendering/rendering-distance-fields/blobbies

Include screenshots, analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
Binary file added images/AABB.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/FPS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/TLAS_and_BLAS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/aa.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/done.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/fin.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/floatvsfloat3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/fuzzy.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lerp.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lighting.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/looks_close_enough.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/metaball.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pivot.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pix.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/regular.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/sceenshot.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/weird_balls.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/wtf.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 29 additions & 8 deletions src/D3D12RaytracingProceduralGeometry/AnalyticPrimitives.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ float3 CalculateNormalForARaySphereHit(in Ray ray, in float thit, float3 center)
}

// Test if a ray with RayFlags and segment <RayTMin(), RayTCurrent()> intersects a hollow sphere.
bool RaySphereIntersectionTest(in Ray ray, out float thit, out float tmax, in ProceduralPrimitiveAttributes attr, in float3 center = float3(0, 0, 0), in float radius = 1)
bool RaySphereIntersectionTest(in Ray ray, out float thit, out float tmax, out ProceduralPrimitiveAttributes attr, in float3 center = float3(0, 0, 0), in float radius = 1)
{
float t0, t1; // solutions for t if the ray intersects

Expand Down Expand Up @@ -166,18 +166,39 @@ bool RaySolidSphereIntersectionTest(in Ray ray, out float thit, out float tmax,
bool RayMultipleSpheresIntersectionTest(in Ray ray, out float thit, out ProceduralPrimitiveAttributes attr)
{
// Define the spheres in local space (within the aabb)
float3 center = float3(-0.2, 0, -0.2);
float radius = 0.7f;
float3 center[3];
float radius[3];
bool hit = false;

center[0] = float3(-0.2, 0, -0.2);
radius[0] = 0.5f;
center[1] = float3(-.4, -.5, -0.9);
radius[1] = .3f;
center[2] = float3(0.7, 0.8, 0.2);
radius[2] = .2f;

thit = RayTCurrent();

float tmax;
if (RaySphereIntersectionTest(ray, thit, tmax, attr, center, radius))
thit = RayTCurrent();
float temp_max =0;
float tmax = 0;
float temp_hit = thit;
ProceduralPrimitiveAttributes temp_attr;
for (int i = 0; i < 3; i++)
{
return true;
if (RaySphereIntersectionTest(ray, temp_hit, temp_max, temp_attr, center[i], radius[i]))
{
//not sure why thit and not max but oh well tmax causes for a mind bending bug
if (temp_hit < thit)
{
tmax = temp_max;
attr = temp_attr;
thit = temp_hit;
hit = true;
}
}
}

return false;
return hit;
}

#endif // ANALYTICPRIMITIVES_H
116 changes: 101 additions & 15 deletions src/D3D12RaytracingProceduralGeometry/DXR-AccelerationStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,28 @@ void DXProceduralProject::BuildGeometryDescsForBottomLevelAS(array<vector<D3D12_
// The number of elements of a D3D12 resource can be accessed from GetDesc().Width (e.g m_indexBuffer.resource->GetDesc().Width)
auto& geometryDesc = geometryDescs[BottomLevelASType::Triangle][0];
geometryDesc = {};

//
geometryDesc.Flags = geometryFlags;
geometryDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;

//
geometryDesc.Triangles.IndexBuffer = m_indexBuffer.resource->GetGPUVirtualAddress();

// why is width count? because it says so above i guess?
geometryDesc.Triangles.IndexCount = m_indexBuffer.resource->GetDesc().Width / sizeof(Index);
geometryDesc.Triangles.IndexFormat = DXGI_FORMAT_R16_UINT;

// think stride is ok based on below
//https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_raytracing_geometry_triangles_desc
// doc says zero again?
UINT64 stride = sizeof(Vertex);
geometryDesc.Triangles.VertexBuffer.StartAddress = m_vertexBuffer.resource->GetGPUVirtualAddress();
geometryDesc.Triangles.VertexBuffer.StrideInBytes = stride;

//how do I get count?
geometryDesc.Triangles.VertexCount = m_vertexBuffer.resource->GetDesc().Width/ sizeof(Vertex);
geometryDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
}

{
Expand All @@ -49,6 +70,15 @@ void DXProceduralProject::BuildGeometryDescsForBottomLevelAS(array<vector<D3D12_
// Remember to use m_aabbBuffer to get the AABB geometry data you previously filled in.
// Note: Having separate geometries allows of separate shader record binding per geometry.
// In this project, this lets us specify custom hit groups per AABB geometry.
//D3D12_RAYTRACING_GEOMETRY_AABBS_DESC aabb_desc = {}; // just needs start and stride?
// loop through the primitive and analytic types getting the start and stride
for (UINT count = 0; count < IntersectionShaderType::TotalPrimitiveCount; count++)
{
auto& geometryDesc = geometryDescs[BottomLevelASType::AABB][count];
// https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_raytracing_geometry_aabbs_desc
UINT stride = sizeof(D3D12_RAYTRACING_AABB)*count;
geometryDesc.AABBs.AABBs.StartAddress = m_aabbBuffer.resource->GetGPUVirtualAddress() + stride;
}

}
}
Expand All @@ -61,14 +91,17 @@ AccelerationStructureBuffers DXProceduralProject::BuildBottomLevelAS(const vecto
ComPtr<ID3D12Resource> scratch; // temporary AS data
ComPtr<ID3D12Resource> bottomLevelAS; // actual bottom-level AS resource

// This is what we need to fill in.
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC bottomLevelBuildDesc = {};

// The bottom-level inputs are the geometries you build in BuildGeometryDescsForBottomLevelAS()
// Again, these tell the AS where the actual geometry data is and how it is laid out.
// TODO-2.6: fill the bottom-level inputs. Consider using D3D12_ELEMENTS_LAYOUT_ARRAY as the DescsLayout.
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS &bottomLevelInputs = bottomLevelBuildDesc.Inputs;

bottomLevelInputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
bottomLevelInputs.Flags = buildFlags;
bottomLevelInputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
bottomLevelInputs.NumDescs = static_cast<UINT>( geometryDescs.size() );
bottomLevelInputs.pGeometryDescs = geometryDescs.data();

// Query the driver for resource requirements to build an acceleration structure. We've done this for you.
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO bottomLevelPrebuildInfo = {};
Expand Down Expand Up @@ -108,7 +141,9 @@ AccelerationStructureBuffers DXProceduralProject::BuildBottomLevelAS(const vecto
// TODO-2.6: Now that you have the scratch and actual bottom-level AS desc, pass their GPU addresses to the bottomLevelBuildDesc.
// Consider reading about D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC.
// This should be as easy as passing the GPU addresses to the struct using GetGPUVirtualAddress() calls.

// there are two virtual address fields in the structure so we set them here
bottomLevelBuildDesc.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress();
bottomLevelBuildDesc.DestAccelerationStructureData = bottomLevelAS->GetGPUVirtualAddress();

// Fill up the command list with a command that tells the GPU how to build the bottom-level AS.
if (m_raytracingAPI == RaytracingAPI::FallbackLayer)
Expand All @@ -127,7 +162,17 @@ AccelerationStructureBuffers DXProceduralProject::BuildBottomLevelAS(const vecto
// the AccelerationStructureBuffers struct so the top-level AS can use it!
// Don't forget that this is the return value.
// Consider looking into the AccelerationStructureBuffers struct in DXR-Structs.h
return AccelerationStructureBuffers{};
//ComPtr<ID3D12Resource> scratch; // temp data needed by the GPU to build the AS
//ComPtr<ID3D12Resource> accelerationStructure; // actual AS data
//ComPtr<ID3D12Resource> instanceDesc; // used only for top-level AS
//UINT64 ResultDataMaxSizeInBytes; // this can be acquired from the prebuild info
AccelerationStructureBuffers return_buff = {};
return_buff.scratch = scratch;
return_buff.accelerationStructure = bottomLevelAS;
return_buff.instanceDesc = nullptr; // used only for top level
return_buff.ResultDataMaxSizeInBytes = bottomLevelPrebuildInfo.ResultDataMaxSizeInBytes;

return return_buff;
}

// TODO-2.6: Build the instance descriptor for each bottom-level AS you built before.
Expand Down Expand Up @@ -179,7 +224,19 @@ void DXProceduralProject::BuildBottomLevelASInstanceDescs(BLASPtrType *bottomLev
// Where do you think procedural shader records would start then? Hint: right after.
// * Make each instance hover above the ground by ~ half its width
{
auto& instanceDesc = instanceDescs[BottomLevelASType::AABB];
instanceDesc = {};
instanceDesc.InstanceMask = 1; // making the mask same as triangle for now
// how the fuck is this intuitive? I try numbers until stuff appeared on my screen .. great use of my time and money
instanceDesc.InstanceContributionToHitGroupIndex = BottomLevelASType::AABB * RayType::Count;// maybe this?
instanceDesc.AccelerationStructure = bottomLevelASaddresses[BottomLevelASType::AABB];

// TODO do I need anything else here?
// also not intuitive
XMMATRIX mTranslation = XMMatrixTranslationFromVector(XMLoadFloat3(&XMFLOAT3(0, c_aabbWidth / 2, 0)));

// Store the transform in the instanceDesc.
XMStoreFloat3x4(reinterpret_cast<XMFLOAT3X4*>(instanceDesc.Transform), mTranslation);
}

// Upload all these instances to the GPU, and make sure the resouce is set to instanceDescsResource.
Expand All @@ -202,7 +259,10 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
// TODO-2.6: fill in the topLevelInputs, read about D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS.
// Consider using D3D12_ELEMENTS_LAYOUT_ARRAY as a DescsLayout since we are using an array of bottom-level AS.
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS &topLevelInputs = topLevelBuildDesc.Inputs;

topLevelInputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
topLevelInputs.Flags = buildFlags;
topLevelInputs.NumDescs = BottomLevelASType::Count;// trianlge and AABB or are there more or some value ERIC
topLevelInputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;

D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO topLevelPrebuildInfo = {};
if (m_raytracingAPI == RaytracingAPI::FallbackLayer)
Expand All @@ -216,7 +276,8 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
ThrowIfFalse(topLevelPrebuildInfo.ResultDataMaxSizeInBytes > 0);

// TODO-2.6: Allocate a UAV buffer for the scracth/temporary top-level AS data.

// give unordered access the explanation is on line 121
AllocateUAVBuffer(device, topLevelPrebuildInfo.ScratchDataSizeInBytes, &scratch, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, L"ScratchTODO2.6");

// Allocate space for the top-level AS.
{
Expand All @@ -231,7 +292,7 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
}

// TODO-2.6: Allocate a UAV buffer for the actual top-level AS.

AllocateUAVBuffer(device, topLevelPrebuildInfo.ResultDataMaxSizeInBytes, &topLevelAS, initialResourceState, L"TopLevelAS2.6");
}

// Note on Emulated GPU pointers (AKA Wrapped pointers) requirement in Fallback Layer:
Expand Down Expand Up @@ -259,7 +320,8 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
};

// TODO-2.6: Call the fallback-templated version of BuildBottomLevelASInstanceDescs() you completed above.

// what do I pass in for the template. WRAPPED_GPU_POINTER seems wrong
BuildBottomLevelASInstanceDescs< D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC, WRAPPED_GPU_POINTER>(bottomLevelASaddresses, &instanceDescsResource);
}
else // DirectX Raytracing
{
Expand All @@ -271,7 +333,7 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
};

// TODO-2.6: Call the DXR-templated version of BuildBottomLevelASInstanceDescs() you completed above.

BuildBottomLevelASInstanceDescs< D3D12_RAYTRACING_INSTANCE_DESC, D3D12_GPU_VIRTUAL_ADDRESS>(bottomLevelASaddresses, &instanceDescsResource);
}

// Create a wrapped pointer to the acceleration structure.
Expand All @@ -283,7 +345,10 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt

// TODO-2.6: fill in the topLevelBuildDesc. Read about D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC.
// This should be as easy as passing the GPU addresses to the struct using GetGPUVirtualAddress() calls.

// like above except we add the instance since we are top level
topLevelInputs.InstanceDescs = instanceDescsResource->GetGPUVirtualAddress();
topLevelBuildDesc.DestAccelerationStructureData = topLevelAS->GetGPUVirtualAddress();
topLevelBuildDesc.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress();

// Build acceleration structure.
if (m_raytracingAPI == RaytracingAPI::FallbackLayer)
Expand All @@ -302,7 +367,17 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
// Very similar to how you did this in BuildBottomLevelAS() except now you have to worry about topLevelASBuffers.instanceDesc.
// Consider looking into the AccelerationStructureBuffers struct in DXR-Structs.h.
// Make sure to return the topLevelASBuffers before you exit the function.
return AccelerationStructureBuffers{};
//ComPtr<ID3D12Resource> scratch; // temp data needed by the GPU to build the AS
//ComPtr<ID3D12Resource> accelerationStructure; // actual AS data
//ComPtr<ID3D12Resource> instanceDesc; // used only for top-level AS
//UINT64 ResultDataMaxSizeInBytes; // this can be acquired from the prebuild info
AccelerationStructureBuffers return_buff = {};
return_buff.scratch = scratch;
return_buff.accelerationStructure = topLevelAS;
return_buff.instanceDesc = instanceDescsResource;
return_buff.ResultDataMaxSizeInBytes = topLevelPrebuildInfo.ResultDataMaxSizeInBytes;

return return_buff;
}

// TODO-2.6: This will wrap building the Acceleration Structure! This is what we will call when building our scene.
Expand All @@ -312,18 +387,23 @@ void DXProceduralProject::BuildAccelerationStructures()
auto commandList = m_deviceResources->GetCommandList();
auto commandQueue = m_deviceResources->GetCommandQueue();
auto commandAllocator = m_deviceResources->GetCommandAllocator();
auto flag = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE; // the other flags seem too fancy for me

// Reset the command list for the acceleration structure construction.
commandList->Reset(commandAllocator, nullptr);

// TODO-2.6: Build the geometry descriptors. Hint: you filled in a function that does this.
array<vector<D3D12_RAYTRACING_GEOMETRY_DESC>, BottomLevelASType::Count> geometryDescs;


BuildGeometryDescsForBottomLevelAS(geometryDescs);

// TODO-2.6: For each bottom-level object (triangle, procedural), build a bottom-level AS.
// Hint: you filled in a function that does this.
AccelerationStructureBuffers bottomLevelAS[BottomLevelASType::Count];

for (UINT count = 0; count < BottomLevelASType::Count; count++)
{
bottomLevelAS[count] = BuildBottomLevelAS(geometryDescs[count], flag);
}

// Batch all resource barriers for bottom-level AS builds.
// This will Notifies the driver that it needs to synchronize multiple accesses to resources.
Expand All @@ -336,7 +416,7 @@ void DXProceduralProject::BuildAccelerationStructures()

// TODO-2.6: Build top-level AS. Hint, you already made a function that does this.
AccelerationStructureBuffers topLevelAS;

topLevelAS = BuildTopLevelAS(bottomLevelAS, flag);

// Kick off acceleration structure construction.
m_deviceResources->ExecuteCommandList();
Expand All @@ -347,5 +427,11 @@ void DXProceduralProject::BuildAccelerationStructures()
// TODO-2.6: Store the AS buffers. The rest of the buffers will be released once we exit the function.
// Do this for both the bottom-level and the top-level AS. Consider re-reading the DXProceduralProject class
// to find what member variables should be set.

// set all bottoms
for (UINT count = 0; count < BottomLevelASType::Count; count++)
{
m_bottomLevelAS[count] = bottomLevelAS[count].accelerationStructure;
}
// set the top level we only have one for this prj
m_topLevelAS = topLevelAS.accelerationStructure;
}
Loading