Skip to content

Commit

Permalink
Fix Vulkan raytracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Oct 31, 2024
1 parent 14a78d4 commit dbd31ca
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 100 deletions.
96 changes: 47 additions & 49 deletions data/Shaders/lighting/deferredLighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -83,55 +83,53 @@ void CS_DeferredLighting(int2 dispatchThreadID : SV_DispatchThreadID)

color[i].rgb = applyLighting(gbufferSamples[i].albedo.rgb, lighting, viewConstants.getDisplayMode());

//
//#if _TOOLMODE
//switch(viewConstants.getDisplayMode())
//{
// case DisplayMode::None:
// break;
//
// case DisplayMode::Lighting_Diffuse:
// case DisplayMode::Lighting_Specular:
// case DisplayMode::Lighting_RayCount:
// break;
//
// default:
// color = float4(albedo.rgb, 1.0f);
// break;
//
// case DisplayMode::Deferred_GBuffer0_Albedo:
// color = float4(albedo.rgb, 1.0f);
// break;
//
// //case DisplayMode::Deferred_GBuffer0_A:
// // color = float4(albedo.aaa, 1.0f);
// // break;
//
// case DisplayMode::Deferred_GBuffer1_Normal:
// color = float4(normal.rgb*0.5f+0.5f, 1.0f);
// break;
//
// //case DisplayMode::Deferred_GBuffer1_A:
// // color = float4(normal.aaa, 1.0f);
// // break;
//
// case DisplayMode::Deferred_GBuffer2_Occlusion:
// color = float4(pbr.rrr, 1.0f);
// break;
//
// case DisplayMode::Deferred_GBuffer2_Roughness:
// color = float4(pbr.ggg, 1.0f);
// break;
//
// case DisplayMode::Deferred_GBuffer2_Metalness:
// color = float4(pbr.bbb, 1.0f);
// break;
//
// //case DisplayMode::Deferred_GBuffer1_A:
// // color = float4(normal.aaa, 1.0f);
// // break;
//}
//#endif
#if _TOOLMODE
switch(viewConstants.getDisplayMode())
{
case DisplayMode::None:
break;

case DisplayMode::Lighting_Diffuse:
case DisplayMode::Lighting_Specular:
case DisplayMode::Lighting_RayCount:
break;

default:
break;

case DisplayMode::Deferred_GBuffer0_Albedo:
color[i].rgb = gbufferSamples[i].albedo.rgb;
break;

//case DisplayMode::Deferred_GBuffer0_A:
// color[i].rgb = gbufferSamples[i].albedo.aaa;
// break;

case DisplayMode::Deferred_GBuffer1_Normal:
color[i].rgb = gbufferSamples[i].normal.rgb*0.5f+0.5f;
break;

//case DisplayMode::Deferred_GBuffer1_A:
// color[i].rgb = gbufferSamples[i].normal.aaa;
// break;

case DisplayMode::Deferred_GBuffer2_Occlusion:
color[i].rgb = gbufferSamples[i].pbr.rrr;
break;

case DisplayMode::Deferred_GBuffer2_Roughness:
color[i].rgb = gbufferSamples[i].pbr.ggg;
break;

case DisplayMode::Deferred_GBuffer2_Metalness:
color[i].rgb = gbufferSamples[i].pbr.bbb;
break;

//case DisplayMode::Deferred_GBuffer1_A:
// color[i].rgb = gbufferSamples[i].normal.aaa;
// break;
}
#endif
}

// Store
Expand Down
46 changes: 17 additions & 29 deletions data/Shaders/system/lighting.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -206,50 +206,39 @@ LightingResult computeDirectLighting(ViewConstants _viewConstants, float3 _eyePo
ray.TMax = 10;

RayQuery<RAY_FLAG_NONE> query;
query.TraceRayInline(tlas, 0, 0xff, ray);
query.TraceRayInline(tlas, RAY_FLAG_NONE, 0xff, ray);

#ifdef _TOOLMODE
rayCount++;
#endif

#if 1
query.Proceed();
#else
// TODO : get material and apply alphatest/alphaclip as in https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#tracerayinline-example-2
while (query.Proceed())
// https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#tracerayinline-example-2
while(query.Proceed())
{
switch(query.CandidateType())
{
//case CANDIDATE_PROCEDURAL_PRIMITIVE:
//break;

case CANDIDATE_NON_OPAQUE_TRIANGLE:
query.CommitNonOpaqueTriangleHit();
break;
}
}
#endif

switch(query.CommittedStatus())
{
default:
case COMMITTED_NOTHING:
break;

case COMMITTED_TRIANGLE_HIT:
shadow = 0;
break;
}

//if (query.Proceed())
//{
// //uint primitiveIndex = query.CommittedInstanceID();
// //float2 barycentrics = query.GetBarycentrics();
//
// return output;
//}
//else
//{
// switch(query.CommittedStatus())
// {
// case COMMITTED_TRIANGLE_HIT:
// shadow = 0;
// break;
// }
//}
shadow = 0;
break;

//case COMMITTED_PROCEDURAL_PRIMITIVE_HIT:
//break;
}

#else // _RAYTRACING

float4 shadowUV = mul(directional.getShadowMatrix(), float4(_worldPos, 1.0f));
Expand All @@ -268,7 +257,6 @@ LightingResult computeDirectLighting(ViewConstants _viewConstants, float3 _eyePo
shadow = 1.0f - ((1.0f-shadow) * si);
}

//output.diffuse = cosLi;
output.addLightContribution(Lo, cosLo, cosLi, Lr, F0, Li, Lradiance * shadow, _worldNormal, roughness, metalness);
}

Expand Down
Binary file modified src/application/application.aps
Binary file not shown.
Binary file modified src/application/application.rc
Binary file not shown.
22 changes: 16 additions & 6 deletions src/application/application.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,24 @@
<ClCompile Include="WinMain.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="application.rc" />
<ResourceCompile Include="application.rc">
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Final_Vulkan|ARM64EC'">VG_FINAL;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Final_DX12|ARM64EC'">VG_FINAL;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Final_Vulkan|x64'">VG_FINAL;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Final_DX12|x64'">VG_FINAL;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug_DX12|ARM64EC'">VG_DEBUG;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug_Vulkan|ARM64EC'">VG_DEBUG;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug_DX12|x64'">VG_DEBUG;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug_Vulkan|x64'">VG_DEBUG;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release_DX12|ARM64EC'">VG_RELEASE;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release_Vulkan|ARM64EC'">VG_RELEASE;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release_DX12|x64'">VG_RELEASE;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release_Vulkan|x64'">VG_RELEASE;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Image Include="icon1.ico" />
<Image Include="icon2.ico" />
<Image Include="icon3.ico" />
<Image Include="icon4.ico" />
<Image Include="icon5.ico" />
<Image Include="editor.ico" />
<Image Include="game.ico" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
Expand Down
File renamed without changes.
Binary file added src/application/game.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion src/engine/Resource/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace vg::engine
else
{
if (!_newPath.empty())
VG_ERROR("[Resource] Could not find file \"%s\"", _newPath.c_str());
VG_ERROR("[Resource] Could not find %s file \"%s\" referenced by GameObject \"%s\"", _resource->GetClassName(), _newPath.c_str(), _resource->GetParentGameObject()->GetName().c_str());
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/gfx/Device/vulkan/Device_vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ namespace vg::gfx::vulkan
ignore = true;
}

if (ignore)
{
VG_SAFE_FREE(message);
}
else
if (!ignore)
{
string msg = (string)message;
msg = msg.substr(0, msg.find("\n"));

switch (severity)
{
case Severity::Error:
Expand All @@ -295,11 +294,15 @@ namespace vg::gfx::vulkan
break;
}

VG_SAFE_FREE(message);
if (deviceParams.breakOnErrors)
VG_ASSERT(Severity::Error != severity, msg.c_str());

if ((Severity::Error == severity && deviceParams.breakOnErrors) || (Severity::Warning == severity && deviceParams.breakOnWarnings))
DebugBreak();
if (deviceParams.breakOnWarnings)
VG_ASSERT(Severity::Warning != severity, msg.c_str());
}

VG_SAFE_FREE(message);

return false;
}

Expand Down
23 changes: 18 additions & 5 deletions src/gfx/Raytracing/vulkan/BLAS_vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace vg::gfx::vulkan
//--------------------------------------------------------------------------------------
void BLAS::init(bool _update)
{
gfx::Device * device = gfx::Device::get();
gfx::Device * device = gfx::Device::get();
const gfx::DeviceCaps & caps = device->getDeviceCaps();

// BLAS build Info
Expand Down Expand Up @@ -150,11 +150,24 @@ namespace vg::gfx::vulkan
desc.buffer = m_resultBuffer->getResource().getVulkanBuffer();
desc.size = sizeInfo.accelerationStructureSize;
desc.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;

if (_update)
{
m_VKRTAccelStructInputs.srcAccelerationStructure = src;
m_VKRTAccelStructInputs.dstAccelerationStructure = m_VKBLAS;

VG_ASSERT(m_VKRTAccelStructInputs.srcAccelerationStructure == m_VKRTAccelStructInputs.dstAccelerationStructure);
}
else
{
VG_VERIFY_VULKAN(device->createAccelerationStructure(&desc, nullptr, &m_VKBLAS));

m_VKRTAccelStructInputs.srcAccelerationStructure = VK_NULL_HANDLE;
m_VKRTAccelStructInputs.dstAccelerationStructure = m_VKBLAS;

VG_ASSERT(m_VKRTAccelStructInputs.srcAccelerationStructure != m_VKRTAccelStructInputs.dstAccelerationStructure);
}

VG_VERIFY_VULKAN(device->createAccelerationStructure(&desc, nullptr, &m_VKBLAS));

m_VKRTAccelStructInputs.srcAccelerationStructure = src;
m_VKRTAccelStructInputs.dstAccelerationStructure = m_VKBLAS;
m_VKRTAccelStructInputs.scratchData.deviceAddress = m_scratchBuffer->getResource().getVulkanDeviceAddress();
}

Expand Down
2 changes: 1 addition & 1 deletion src/gfx/Resource/vulkan/Buffer_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ namespace vg::gfx::vulkan
~Buffer();

private:
VkBufferView m_vkBufferView;
VkBufferView m_vkBufferView = VK_NULL_HANDLE;
};
}
2 changes: 2 additions & 0 deletions src/renderer/RayTracing/RayTracingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ namespace vg::renderer
blas->addIndexedGeometry(ib, ibOffset + batch.offset, batch.count, skinVB, skinVBOffset, modelVB->getBufDesc().getElementCount(), vertexStride, (gfx::SurfaceType::Opaque == surfaceType) ? true : false);
}

//VG_INFO("[Renderer] Update BLAS 0x%016X for instance \"%s\" with key 0x%016X", blas, skin->GetName().c_str(), key);

blas->update(_cmdList);
}

Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#define VG_FRAMEWORK_VERSION_MAJOR 0
#define VG_FRAMEWORK_VERSION_MINOR 38
#define VG_FRAMEWORK_VERSION_PATCH 1
#define VG_FRAMEWORK_VERSION_PATCH 2

0 comments on commit dbd31ca

Please sign in to comment.