Skip to content
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

Project 5: Yan Dong #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
93 changes: 87 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,92 @@
**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)
- Yan Dong
- [LinkedIn](https://www.linkedin.com/in/yan-dong-572b1113b/)
- [personal website](https://www.coffeier.com)
- [github](https://github.com/coffeiersama)
- Tested on: Windows 10, i7-8750 @ 2.22GHz (12CPUs) 16GB, GTX 1060 14202MB (OMEN 15-dc0xxx)

### (TODO: Your README)
[Result](#result) - [Rules](#feature) - [Performance Analysis](#performance-analysis) - [Bloppers](#bloopers)

Include screenshots, analysis, etc. (Remember, this is public, so don't put
anything here that you don't want to share with the world.)
## Result

![](images/result.png)

![](images/result.gif)

(gif seems compress some quality of picture)

## Feature

all basic implement: metaball, sphere, and box

Optimize Metaball Rendering

##### DXR Pipeline

![](https://developer.nvidia.com/sites/default/files/pictures/2018/dx12_rtx_tutorial/Images/DXROverview.svg)

There is detailed document for the DXR API, I think it can be separated into two parts: pass the data from CPU to GPU, do the ray tracing in GPU.

All detailed things can get in the reference website and the given slides.

##### Optimize Metaball Rendering

we want to loop over only active metaballs(intersect by the ray) to save the calculating time. The basic idea is when you do the intersection test, change the meta ball array, only store the active meta balls. They only calculate potential for these balls.

without optimizing, the fps is: (5 metaballs, ray depth 10)

![](images/withoutopt.gif)

with optimizing, the fps is :

![](images/withopt.gif)

![](images/fiveball.png)



## Performance-analysis

![](images/fps.png)

When we increase the ray depth from 3 to 10, at first the fps decrease obviously. But then, it do not decrease. I think this is caused by this very simple scene. Only having metaball and simple reflect materials, large ray depth is waste.



## Bloppers

##### no metaball

![](images/bloo.png)



This happened when I cannot see my metaball, there are two reasons.

The first is I calculate wrong ratio of the potential value.

The second is that after dividing 128 parts along tmin and tmax, I make the wrong calculation for the current position.(I used ray.origin + i*step * ray.direction, omit tmin iteself)

##### wrong color

![](images/wrongcolor.png)

the result is much lighter than it should be, check the code and equation, finally find that need to use abs((dot(normalize(I), normalize(N)))) to make sure 2 rays are doing the dot with the smaller angle(<90).

## Reference

https://docs.microsoft.com/en-us/windows/win32/direct3d12/raytcurrent

http://cwyman.org/code/dxrTutors/tutors/Tutor4/tutorial04.md.html

http://www.visualextract.com/posts/introduction-to-dxr/

http://intro-to-dxr.cwyman.org/presentations/IntroDXR_RaytracingShaders.pdf

https://github.com/Microsoft/DirectX-Graphics-Samples

## Thanks

Thanks Zheyuan Xie for helping me with lots of explanation of DXR API
Binary file added images/bloo.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/fiveball.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/result.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/result.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/withopt.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/withoutopt.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/wrongcolor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 26 additions & 16 deletions src/D3D12RaytracingProceduralGeometry/AnalyticPrimitives.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ 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,
in ProceduralPrimitiveAttributes attr, in float3 center = float3(0, 0, 0), in float radius = 1)
{
float t0, t1; // solutions for t if the ray intersects

Expand All @@ -123,9 +124,7 @@ bool RaySphereIntersectionTest(in Ray ray, out float thit, out float tmax, in Pr
thit = t1;
return true;
}
}
else
{
} else {
// use t0.
attr.normal = CalculateNormalForARaySphereHit(ray, t0, center);
if (is_a_valid_hit(ray, t0, attr.normal))
Expand Down Expand Up @@ -162,22 +161,33 @@ bool RaySolidSphereIntersectionTest(in Ray ray, out float thit, out float tmax,
}

// TODO-3.4.1: Change this code to support intersecting multiple spheres (~3 spheres).
// You can hardcode the local centers/radii of the spheres, just try to maintain them between 1 and -1 (and > 0 for the radii).
// You can hardcode the local centers/radii of the spheres, just try to maintain them between 1 and -1
//(and > 0 for the radii).
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;

thit = RayTCurrent();

float tmax;
if (RaySphereIntersectionTest(ray, thit, tmax, attr, center, radius))
{
return true;
//float3 center = float3(-0.2, 0, -0.2);
//float radius = 0.7f;
//
float3 centers[3] = { float3(-0.2f, 0.f, -0.2f) , float3(0.6f, 0.f, 0.f) , float3(0.1f, 0.f, 0.2f) };
float radiuses[3] = {0.3f, 0.5f, 0.2f};

thit = RayTCurrent();//current t
bool ishit = false;

for (int i = 0; i < 3; i++) {
float _tmax;
float _thit;
ProceduralPrimitiveAttributes _attr;
if (RaySphereIntersectionTest(ray, _thit, _tmax, _attr, centers[i], radiuses[i])) {
if (_thit < thit) {
thit = _thit;
attr = _attr;
ishit = true;
}
}
}

return false;
return ishit;
}

#endif // ANALYTICPRIMITIVES_H
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ PrebuildCheck.bat</Command>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="SDFPrimitives.hlsli" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Libraries\D3D12RaytracingFallback\src\FallbackLayer.vcxproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="SDFPrimitives.hlsli">
<Filter>Assets\Shaders\ProceduralPrimitives</Filter>
</None>
</ItemGroup>
<ItemGroup>
<FxCompile Include="Raytracing.hlsl">
Expand Down
90 changes: 75 additions & 15 deletions src/D3D12RaytracingProceduralGeometry/DXR-AccelerationStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ 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.Triangles.IndexFormat = DXGI_FORMAT_R16_UINT;
geometryDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;

geometryDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geometryDesc.Triangles.IndexBuffer = m_indexBuffer.resource->GetGPUVirtualAddress();
geometryDesc.Triangles.IndexCount = static_cast<UINT>(m_indexBuffer.resource->GetDesc().Width) / sizeof(Index);
geometryDesc.Triangles.VertexCount = static_cast<UINT>(m_vertexBuffer.resource->GetDesc().Width) / sizeof(Vertex);
geometryDesc.Triangles.VertexBuffer.StartAddress = m_vertexBuffer.resource->GetGPUVirtualAddress();
geometryDesc.Triangles.VertexBuffer.StrideInBytes = sizeof(Vertex);
geometryDesc.Flags = geometryFlags;
}

{
Expand All @@ -49,7 +58,11 @@ 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.

for (UINT i = 0; i < IntersectionShaderType::TotalPrimitiveCount; i++)
{
auto& geometryDesc = geometryDescs[BottomLevelASType::AABB][i];
geometryDesc.AABBs.AABBs.StartAddress = m_aabbBuffer.resource->GetGPUVirtualAddress() + i * sizeof(D3D12_RAYTRACING_AABB);
}
}
}

Expand All @@ -68,7 +81,11 @@ AccelerationStructureBuffers DXProceduralProject::BuildBottomLevelAS(const vecto
// 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.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
bottomLevelInputs.Flags = buildFlags;
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 +125,10 @@ 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.

{
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 +147,12 @@ 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{};
AccelerationStructureBuffers blas;
blas.accelerationStructure = bottomLevelAS;
blas.scratch = scratch;
blas.ResultDataMaxSizeInBytes = bottomLevelPrebuildInfo.ResultDataMaxSizeInBytes;

return blas;
}

// TODO-2.6: Build the instance descriptor for each bottom-level AS you built before.
Expand Down Expand Up @@ -179,7 +204,17 @@ 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;

// Set hit group offset to beyond the shader records for the triangle AABB.
instanceDesc.InstanceContributionToHitGroupIndex = BottomLevelASType::AABB * RayType::Count;
instanceDesc.AccelerationStructure = bottomLevelASaddresses[BottomLevelASType::AABB];

// Move all AABBS above the ground plane.
XMMATRIX mTranslation = XMMatrixTranslationFromVector(XMLoadFloat3(&XMFLOAT3(0, c_aabbWidth / 2, 0)));
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 +237,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.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
topLevelInputs.Flags = buildFlags;
topLevelInputs.NumDescs = NUM_BLAS;

D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO topLevelPrebuildInfo = {};
if (m_raytracingAPI == RaytracingAPI::FallbackLayer)
Expand All @@ -213,10 +251,10 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
{
m_dxrDevice->GetRaytracingAccelerationStructurePrebuildInfo(&topLevelInputs, &topLevelPrebuildInfo);
}
ThrowIfFalse(topLevelPrebuildInfo.ResultDataMaxSizeInBytes > 0);
//ThrowIfFalse(topLevelPrebuildInfo.ResultDataMaxSizeInBytes > 0);

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

AllocateUAVBuffer(device, topLevelPrebuildInfo.ScratchDataSizeInBytes, &scratch, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, L"ScratchResource");

// Allocate space for the top-level AS.
{
Expand All @@ -231,7 +269,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"TopLevelAccelerationStructure");
}

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

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

BuildBottomLevelASInstanceDescs<D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC>(bottomLevelASaddresses, &instanceDescsResource);
}
else // DirectX Raytracing
{
Expand All @@ -271,7 +309,7 @@ AccelerationStructureBuffers DXProceduralProject::BuildTopLevelAS(AccelerationSt
};

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

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

// Create a wrapped pointer to the acceleration structure.
Expand All @@ -283,7 +321,11 @@ 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.

{
topLevelBuildDesc.DestAccelerationStructureData = topLevelAS->GetGPUVirtualAddress();
topLevelInputs.InstanceDescs = instanceDescsResource->GetGPUVirtualAddress();
topLevelBuildDesc.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress();
}

// Build acceleration structure.
if (m_raytracingAPI == RaytracingAPI::FallbackLayer)
Expand All @@ -302,7 +344,13 @@ 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{};
AccelerationStructureBuffers topLevelASBuffers;
topLevelASBuffers.accelerationStructure = topLevelAS;
topLevelASBuffers.instanceDesc = instanceDescsResource;
topLevelASBuffers.scratch = scratch;
topLevelASBuffers.ResultDataMaxSizeInBytes = topLevelPrebuildInfo.ResultDataMaxSizeInBytes;

return topLevelASBuffers;
}

// TODO-2.6: This will wrap building the Acceleration Structure! This is what we will call when building our scene.
Expand All @@ -318,11 +366,20 @@ void DXProceduralProject::BuildAccelerationStructures()

// 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 i = 0; i < BottomLevelASType::Count; i++)
{
bottomLevelAS[i] = BuildBottomLevelAS(geometryDescs[i]);
}
}


// Batch all resource barriers for bottom-level AS builds.
Expand All @@ -335,8 +392,7 @@ void DXProceduralProject::BuildAccelerationStructures()
commandList->ResourceBarrier(BottomLevelASType::Count, resourceBarriers);

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

AccelerationStructureBuffers topLevelAS = BuildTopLevelAS(bottomLevelAS);

// Kick off acceleration structure construction.
m_deviceResources->ExecuteCommandList();
Expand All @@ -347,5 +403,9 @@ 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.

for (UINT i = 0; i < BottomLevelASType::Count; i++)
{
m_bottomLevelAS[i] = bottomLevelAS[i].accelerationStructure;
}
m_topLevelAS = topLevelAS.accelerationStructure;
}
Loading