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

WIP : Add-Shadows #600

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
9 changes: 9 additions & 0 deletions rootex/core/renderer/rendering_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void RenderingDevice::createSwapChainBufferViews()
void RenderingDevice::createDepthStencil(DXGI_SWAP_CHAIN_DESC& sd, float width, float height)
{
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencil = nullptr;
Microsoft::WRL::ComPtr<ID3D11Texture2D> shadowTexture = nullptr;
D3D11_TEXTURE2D_DESC descDepth = { 0 };
descDepth.Width = width;
descDepth.Height = height;
Expand All @@ -78,13 +79,15 @@ void RenderingDevice::createDepthStencil(DXGI_SWAP_CHAIN_DESC& sd, float width,
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;

GFX_ERR_CHECK(m_Device->CreateTexture2D(&descDepth, nullptr, &depthStencil));
GFX_ERR_CHECK(m_Device->CreateTexture2D(&descDepth, nullptr, &shadowTexture));

D3D11_DEPTH_STENCIL_VIEW_DESC descDSView = {};
descDSView.Format = DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
descDSView.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSView.Texture2D.MipSlice = 0u;

GFX_ERR_CHECK(m_Device->CreateDepthStencilView(depthStencil.Get(), &descDSView, &m_MainDSV));
GFX_ERR_CHECK(m_Device->CreateDepthStencilView(shadowTexture.Get(), &descDSView, &m_ShadowTextureDSV));

D3D11_SHADER_RESOURCE_VIEW_DESC depthSRVDesc;
depthSRVDesc.Format = DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
Expand All @@ -93,6 +96,7 @@ void RenderingDevice::createDepthStencil(DXGI_SWAP_CHAIN_DESC& sd, float width,
depthSRVDesc.Texture2D.MipLevels = 1;

GFX_ERR_CHECK(m_Device->CreateShaderResourceView(depthStencil.Get(), &depthSRVDesc, &m_MainDSSRV));
GFX_ERR_CHECK(m_Device->CreateShaderResourceView(shadowTexture.Get(), &depthSRVDesc, &m_ShadowTextureDSSRV));

D3D11_SHADER_RESOURCE_VIEW_DESC stencilSRVDesc;
stencilSRVDesc.Format = DXGI_FORMAT_X32_TYPELESS_G8X24_UINT;
Expand Down Expand Up @@ -833,6 +837,11 @@ void RenderingDevice::setOffScreenRTVOnly()
m_Context->OMSetRenderTargets(1, m_OffScreenRTV.GetAddressOf(), nullptr);
}

void RenderingDevice::setShadowTextureDSV()
{
m_Context->OMSetRenderTargets(1, m_ShadowTextureDSV.GetAddressOf(), nullptr);
}

void RenderingDevice::setMainRT()
{
m_Context->OMSetRenderTargets(1, m_MainRTV.GetAddressOf(), nullptr);
Expand Down
4 changes: 4 additions & 0 deletions rootex/core/renderer/rendering_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ class RenderingDevice
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_MainRTV;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_MainRTSRV;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_MainDSV;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_ShadowTextureDSV;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_ShadowTextureDSSRV;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_MainDSSRV;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_MainStencilSRV;

Microsoft::WRL::ComPtr<ID3D11Texture2D> m_OffScreenTexture;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_OffScreenRTV;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_ShadowTextureDSV;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_OffScreenSRV;

Microsoft::WRL::ComPtr<ID3D11DepthStencilState> m_DSState;
Expand Down Expand Up @@ -154,6 +157,7 @@ class RenderingDevice

void setOffScreenRTVDSV();
void setOffScreenRTVOnly();
void setShadowTextureDSV();
void setMainRT();
void setRTV(Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv);
void setRTV(ID3D11RenderTargetView* rtv);
Expand Down
5 changes: 5 additions & 0 deletions rootex/core/renderer/shaders/custom_vertex_shader.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ cbuffer CBuf : register(PER_OBJECT_VS_HLSL)
matrix MInverseTranspose;
};

cbuffer CBuf : register(PER_FRAME_DL_VS_HLSL)
{
matrix DLTC;
};

cbuffer CBuf : register(PER_FRAME_VS_HLSL)
{
matrix V;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#define PER_FRAME_VS_CPP 2
#define PER_OBJECT_VS_CPP 3
#define BONES_VS_CPP 4
#define PER_FRAME_DL_VS_CPP 5

#define PER_OBJECT_VS_HLSL CONCAT(b, PER_OBJECT_VS_CPP)
#define PER_FRAME_DL_VS_HLSL CONCAT(b, PER_FRAME_DL_VS_CPP)
#define PER_FRAME_VS_HLSL CONCAT(b, PER_FRAME_VS_CPP)
#define PER_CAMERA_CHANGE_VS_HLSL CONCAT(b, PER_CAMERA_CHANGE_VS_CPP)
#define BONES_VS_HLSL CONCAT(b, BONES_VS_CPP)
Expand Down
19 changes: 19 additions & 0 deletions rootex/framework/systems/render_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "light_system.h"
#include "application.h"
#include "scene_loader.h"
#include "components/visual/light/directional_light_component.h"

#define LINE_MAX_VERTEX_COUNT 1000
#define LINE_VERTEX_COUNT 2
Expand Down Expand Up @@ -279,6 +280,17 @@ void RenderSystem::submitLine(const Vector3& from, const Vector3& to)
m_CurrentFrameLines.m_Indices.push_back(m_CurrentFrameLines.m_Indices.size());
}

void RenderSystem::setViewMatrixForShadowRender()
{
if (!ECSFactory::GetAllDirectionalLightComponent().empty())
{
DirectionalLightComponent& first = ECSFactory::GetAllDirectionalLightComponent().front();
const Matrix& directionalLight = first.getTransformComponent()->getAbsoluteTransform();
RenderingDevice::GetSingleton()->editBuffer(PerFrameVSCB { first.getTransformComponent()->getAbsoluteTransform().Transpose( }, m_PerFrameVSCB.Get());
RenderingDevice::GetSingleton()->setVSCB(PER_FRAME_DL_VS_CPP, 1, m_PerFrameVSCB.GetAddressOf());
};
}

void RenderSystem::submitBox(const Vector3& min, const Vector3& max)
{
Vector3 d = max - min;
Expand Down Expand Up @@ -395,6 +407,13 @@ void RenderSystem::setPerCameraVSCBs()

void RenderSystem::setPerFrameVSCBs(float fogStart, float fogEnd)
{
if (!ECSFactory::GetAllDirectionalLightComponent().empty())
{
DirectionalLightComponent& first = ECSFactory::GetAllDirectionalLightComponent().front();
RenderingDevice::GetSingleton()->editBuffer(PerFrameVSCB { first.getTransformComponent()->getAbsoluteTransform() }, m_PerFrameVSCB.Get());
RenderingDevice::GetSingleton()->setVSCB(PER_FRAME_DL_VS_CPP, 1, m_PerFrameVSCB.GetAddressOf());
}

const Matrix& view = getCamera()->getViewMatrix();
RenderingDevice::GetSingleton()->editBuffer(PerFrameVSCB { view.Transpose(), -fogStart, -fogEnd }, m_PerFrameVSCB.Get());

Expand Down
5 changes: 5 additions & 0 deletions rootex/framework/systems/render_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "components/visual/model/animated_model_component.h"
#include "components/visual/model/sprite_component.h"
#include "transform_system.h"
#include "components/space/transform_component.h"

#include "ASSAO/ASSAO.h"

Expand Down Expand Up @@ -61,6 +62,10 @@ class RenderSystem : public System
void submitCone(const Matrix& transform, const float& height, const float& radius);

void recoverLostDevice();
void getDirectionalLightComponent();

Matrix m_ViewMatrixForShadowRender;
void setViewMatrixForShadowRender();

void setCamera(CameraComponent* camera);
void restoreCamera();
Expand Down