Skip to content
Draft
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
26 changes: 20 additions & 6 deletions Core/VoxelCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
using namespace Math;

// TODO: probably need params here that change the bounds
void VoxelCamera::UpdateMatrix()
void VoxelCamera::UpdateMatrix(AXIS axis)
{
constexpr float DUMMY = 2000;
// TODO: the orthographic frustum should be based on ??? something ??? (bounds of the scene? size of the cascade?)
constexpr float DUMMY = 500;

float right = DUMMY;
float left = -DUMMY;
float top = DUMMY;
float bottom = -DUMMY;
float _near = -DUMMY;
float _far = DUMMY;
float _near = -2000;
float _far = 2000;

float rml = right - left;
float rpl = right + left;
Expand All @@ -22,8 +23,21 @@ void VoxelCamera::UpdateMatrix()
float fmn = _far - _near;
float fpn = _far + _near;

// looking in a positive x direction for now
SetEyeAtUp(Vector3(3000, 0, 0), Vector3(0, 0, 0), Vector3(0, 1, 0));
// directions
switch (axis) {
case AXIS::X:
SetEyeAtUp(Vector3(2000, 0, 0), Vector3(0, 0, 0), Vector3(0, 1, 0));
break;
case AXIS::Y:
SetEyeAtUp(Vector3(0, DUMMY, 0), Vector3(0, 0, 0), Vector3(1, 0, 0));
break;
case AXIS::Z:
SetEyeAtUp(Vector3(0, 0, DUMMY), Vector3(0, 0, 0), Vector3(0, 1, 0));
break;
default:
ASSERT(false);
break;
}

// create an orthographic matrix
// https://wikimedia.org/api/rest_v1/media/math/render/svg/8ea4e438d7439b8fa504fb53fd7fafd678007243
Expand Down
8 changes: 7 additions & 1 deletion Core/VoxelCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ class VoxelCamera : public Math::BaseCamera
{
public:

enum AXIS {
X, // Implicitly assigned 0
Y, // Implicitly assigned 1
Z // Implicitly assigned 2
};

VoxelCamera() {}

void UpdateMatrix();
void UpdateMatrix(AXIS axis);
};
2 changes: 2 additions & 0 deletions Model/ConstantBuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ __declspec(align(256)) struct SDFGIGlobalConstants
{
float viewWidth;
float viewHeight;
int axis;
float pad;
};
1 change: 1 addition & 0 deletions Model/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace Renderer

DescriptorHandle m_CommonTextures;

// SDFGI
std::vector<GraphicsPSO> sm_VoxelPSOs;
DescriptorHandle m_SDFGIVoxelTextures;
Texture m_VoxelAlbedo;
Expand Down
2 changes: 2 additions & 0 deletions Model/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ namespace Renderer
m_CurrentDraw = 0;
}

MeshSorter() : MeshSorter(kDefault) {}

void SetCamera( const BaseCamera& camera ) { m_Camera = &camera; }
void SetViewport( const D3D12_VIEWPORT& viewport ) { m_Viewport = viewport; }
void SetScissor( const D3D12_RECT& scissor ) { m_Scissor = scissor; }
Expand Down
42 changes: 40 additions & 2 deletions Model/Shaders/DefaultPS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,18 @@ cbuffer GlobalConstants : register(b1)
}

#if SDFGI_VOXEL_PASS

enum VoxelAxis {
X = 0,
Y = 1,
Z = 2
};

cbuffer SDFGIConstants : register(b2)
{
float viewWidth;
float viewHeight;
VoxelAxis axis;
}

RWTexture3D<float4> SDFGIVoxelAlbedo : register(u0);
Expand Down Expand Up @@ -305,8 +313,38 @@ float4 main(VSOutput vsOutput) : SV_Target0
// TODO: Shade each light using Forward+ tiles

#if SDFGI_VOXEL_PASS
SDFGIVoxelAlbedo[uint3(20, 20, 20)] = float4(0., 1., 0., 1.);
//float3 pos = float3(vsOutput.position.xyz);
// SDFGIVoxelAlbedo[uint3(20, 20, 20)] = float4(0., 1., 0., 1.);

float screenResolution = 512.0;
float textureResolution = 128.0;
float2 uv = vsOutput.position.xy / screenResolution;

uint x = 0;
uint y = 0;
uint z = 0;
float3 color = float3(0., 0., 0.);

if (axis == X) {
x = uv.x * textureResolution;
y = uv.y * textureResolution;
z = vsOutput.position.z * textureResolution;
color = float3(1., 0., 0.);
}
else if (axis == Y) {
x = uv.x * textureResolution;
y = -vsOutput.position.z * textureResolution;
z = uv.y * textureResolution;
color = float3(0., 1., 0.);
}
else if (axis == Z) {
x = -vsOutput.position.z * textureResolution;
y = uv.x * textureResolution;
z = uv.y * textureResolution;
color = float3(0., 0., 1.);
}

SDFGIVoxelAlbedo[uint3(x, y, z)] = float4(color, 1.);

//colorAccum = float3(frac(pos.x), frac(pos.x), frac(pos.x));
#endif

Expand Down
75 changes: 60 additions & 15 deletions ModelViewer/ModelViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ void ModelViewer::Startup( void )
#ifdef LEGACY_RENDERER
Sponza::Startup(m_Camera);
#else
m_ModelInst = Renderer::LoadModel(L"Sponza/PBR/sponza2.gltf", forceRebuild);
// m_ModelInst = Renderer::LoadModel(L"Sponza/PBR/sponza2.gltf", forceRebuild);
m_ModelInst = Renderer::LoadModel(L"Models/BoxAndPlane/BoxAndPlane.gltf", forceRebuild);
m_ModelInst.Resize(100.0f * m_ModelInst.GetRadius());
OrientedBox obb = m_ModelInst.GetBoundingBox();
float modelRadius = Length(obb.GetDimensions()) * 0.5f;
Expand Down Expand Up @@ -350,7 +351,6 @@ void ModelViewer::RenderScene( void )
Math::BaseCamera voxelCam = m_Camera;
#else
VoxelCamera voxelCam;
voxelCam.UpdateMatrix();
#endif

globals.ViewProjMatrix = voxelCam.GetViewProjMatrix();
Expand Down Expand Up @@ -382,43 +382,88 @@ void ModelViewer::RenderScene( void )
SDFGIglobals.viewHeight = height;
}

// gfxContext.TransitionResource(voxelTextures.VoxelAlbedo, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
std::array<MeshSorter, 3> sorters;

MeshSorter sorter(MeshSorter::kDefault);
sorter.SetCamera(voxelCam);
sorter.SetViewport(voxelViewport);
sorter.SetScissor(voxelScissor);
sorter.SetDepthStencilTarget(g_SceneDepthBuffer);
sorter.AddRenderTarget(g_SceneColorBuffer);
for (MeshSorter& sorter : sorters) {
sorter.SetCamera(voxelCam);
sorter.SetViewport(voxelViewport);
sorter.SetScissor(voxelScissor);
sorter.SetDepthStencilTarget(g_SceneDepthBuffer);
sorter.AddRenderTarget(g_SceneColorBuffer);
}


// Begin rendering depth
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_WRITE, true);
gfxContext.ClearDepth(g_SceneDepthBuffer);

m_ModelInst.Render(sorter);
sorter.Sort();
m_ModelInst.Render(sorters[0]);
sorters[0].Sort();

{
/*{
ScopedTimer _prof(L"Depth Pre-Pass", gfxContext);
sorter.RenderMeshes(MeshSorter::kZPass, gfxContext, globals);
}

SSAO::Render(gfxContext, m_Camera);
SSAO::Render(gfxContext, m_Camera);*/

gfxContext.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, true);
gfxContext.ClearColor(g_SceneColorBuffer);

// TODO: We should be rendering with the shadow pass...

{
ScopedTimer _prof(L"Render Voxel", gfxContext);
ScopedTimer _prof(L"Render Voxel X", gfxContext);

gfxContext.TransitionResource(g_SSAOFullScreen, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_READ);
gfxContext.SetRenderTarget(g_SceneColorBuffer.GetRTV());
gfxContext.SetViewportAndScissor(viewport, scissor);

sorter.RenderVoxels(MeshSorter::kOpaque, gfxContext, globals, SDFGIglobals);
// do 3 passes (render from x, y and z axis)

voxelCam.UpdateMatrix(VoxelCamera::X);
SDFGIglobals.axis = 0;
sorters[0].RenderVoxels(MeshSorter::kOpaque, gfxContext, globals, SDFGIglobals);
}


// Code below crashes!

m_ModelInst.Render(sorters[1]);
sorters[1].Sort();

{
ScopedTimer _prof(L"Render Voxel Y", gfxContext);

gfxContext.TransitionResource(g_SSAOFullScreen, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_READ);
gfxContext.SetRenderTarget(g_SceneColorBuffer.GetRTV());
gfxContext.SetViewportAndScissor(viewport, scissor);

// do 3 passes (render from x, y and z axis)

voxelCam.UpdateMatrix(VoxelCamera::Y);
SDFGIglobals.axis = 1;
sorters[1].RenderVoxels(MeshSorter::kOpaque, gfxContext, globals, SDFGIglobals);
}

m_ModelInst.Render(sorters[1]);
sorters[1].Sort();

{
ScopedTimer _prof(L"Render Voxel Z", gfxContext);

gfxContext.TransitionResource(g_SSAOFullScreen, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_READ);
gfxContext.SetRenderTarget(g_SceneColorBuffer.GetRTV());
gfxContext.SetViewportAndScissor(viewport, scissor);

// do 3 passes (render from x, y and z axis)

voxelCam.UpdateMatrix(VoxelCamera::Z);
SDFGIglobals.axis = 2;
sorters[2].RenderVoxels(MeshSorter::kOpaque, gfxContext, globals, SDFGIglobals);
}
}
#else
Expand Down
Binary file added ModelViewer/Models/BoxAndPlane/BoxAndPlane.bin
Binary file not shown.
Loading