[add] Cascade 일단 렌더링 되는거 확인
This commit is contained in:
@@ -46,7 +46,7 @@ void CPipeLine::Transform_Frustum_ToLocalSpace(_fmatrix WorldMatrixInverse)
|
||||
Make_Planes(vLocalPoints, m_vLocalPlanes);
|
||||
}
|
||||
|
||||
_bool CPipeLine::isIn_WorldFrustum(_fvector vWorldPos, _float fRadius)
|
||||
_bool CPipeLine::IsIn_WorldFrustum(_fvector vWorldPos, _float fRadius)
|
||||
{
|
||||
for (size_t i = 0; i < 6; i++)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ _bool CPipeLine::isIn_WorldFrustum(_fvector vWorldPos, _float fRadius)
|
||||
return true;
|
||||
}
|
||||
|
||||
_bool CPipeLine::isIn_LocalFrustum(_fvector vLocalPos, _float fRadius)
|
||||
_bool CPipeLine::IsIn_LocalFrustum(_fvector vLocalPos, _float fRadius)
|
||||
{
|
||||
for (size_t i = 0; i < 6; i++)
|
||||
{
|
||||
@@ -68,9 +68,66 @@ _bool CPipeLine::isIn_LocalFrustum(_fvector vLocalPos, _float fRadius)
|
||||
return true;
|
||||
}
|
||||
|
||||
pair<_bool, _uint> CPipeLine::IsIn_ShadowViewFrustum(_fvector vWorldCenter, _float fRadius)
|
||||
{
|
||||
pair<_bool, _uint> pairResult = { false, UINT_MAX };
|
||||
// 월드 센터를 셰도우뷰 센터로 바꿈
|
||||
_vector vShadowViewCenter = XMVectorSetW(XMVector3Rotate(vWorldCenter, XMLoadFloat4(&m_vShadowInvDirectionalRPYQuat)), 1.f);
|
||||
|
||||
_float fSafeRadius = fRadius; // 인스턴싱된 객체 고려해서 좀 더 넓게 탐색
|
||||
|
||||
for (_uint iCascadeIndex = 0; iCascadeIndex < 3; ++iCascadeIndex) {
|
||||
const _float4* pTargetPlanes = nullptr;
|
||||
if (iCascadeIndex == 0) {
|
||||
pTargetPlanes = m_vNearShadowViewBoxPlane;
|
||||
fSafeRadius = fRadius;
|
||||
}
|
||||
if (iCascadeIndex == 1) {
|
||||
pTargetPlanes = m_vMiddleShadowViewBoxPlane;
|
||||
fSafeRadius = fRadius;
|
||||
}
|
||||
if (iCascadeIndex == 2) {
|
||||
pTargetPlanes = m_vFarShadowViewBoxPlane;
|
||||
fSafeRadius = fRadius;
|
||||
}
|
||||
|
||||
_bool bIsInside = true;
|
||||
|
||||
for (_uint planeIndex = 0; planeIndex < 6; ++planeIndex) {
|
||||
_vector vPlane = XMLoadFloat4(&pTargetPlanes[planeIndex]);
|
||||
_float fDistance = XMVectorGetX(XMPlaneDotCoord(vPlane, vShadowViewCenter));
|
||||
if (fSafeRadius < fDistance){
|
||||
bIsInside = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (true == bIsInside) {
|
||||
pairResult.first = true;
|
||||
pairResult.second = iCascadeIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pairResult;
|
||||
}
|
||||
|
||||
HRESULT CPipeLine::Bind_CascadeSplitRatio(class CShader* pShader, const _char* pConstantName, _bool bNear)
|
||||
{
|
||||
if (true == bNear) {
|
||||
if (FAILED(pShader->Bind_RawValue(pConstantName, &m_fShadowNearBoxRatio, sizeof(_float)))) {
|
||||
return E_FAIL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (FAILED(pShader->Bind_RawValue(pConstantName, &m_fShadowFarBoxRatio, sizeof(_float)))) {
|
||||
return E_FAIL;
|
||||
}
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CPipeLine::Bind_GlobalSRV(CShader* pShader, const _tchar* wszKeyGlobalSRV, const _char* pConstantName)
|
||||
{
|
||||
ID3D11ShaderResourceView* pSRV = Find_GlobalShader(wszKeyGlobalSRV);
|
||||
ID3D11ShaderResourceView* pSRV = Find_GlobalShaderResourceView(wszKeyGlobalSRV);
|
||||
if (FAILED(pShader->Bind_SRV(pConstantName, pSRV))) {
|
||||
return E_FAIL;
|
||||
}
|
||||
@@ -79,7 +136,7 @@ HRESULT CPipeLine::Bind_GlobalSRV(CShader* pShader, const _tchar* wszKeyGlobalSR
|
||||
|
||||
HRESULT CPipeLine::Load_GlobalSRV(const _tchar* wszKeyGlobalSRV, filesystem::path pathSRVFolder)
|
||||
{
|
||||
ID3D11ShaderResourceView* pSRV = Find_GlobalShader(wszKeyGlobalSRV);
|
||||
ID3D11ShaderResourceView* pSRV = Find_GlobalShaderResourceView(wszKeyGlobalSRV);
|
||||
if (nullptr != pSRV) {
|
||||
return E_FAIL;
|
||||
}
|
||||
@@ -91,39 +148,94 @@ HRESULT CPipeLine::Load_GlobalSRV(const _tchar* wszKeyGlobalSRV, filesystem::pat
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CPipeLine::Ready_Shadow_Light(const _float4& vShadowDirRPYQuat)
|
||||
{
|
||||
_vector vShadowQuat = XMQuaternionNormalize(XMLoadFloat4(&vShadowDirRPYQuat));
|
||||
XMStoreFloat4(&m_vShadowDirectionalRPYQuat, vShadowQuat);
|
||||
XMStoreFloat4(&m_vShadowInvDirectionalRPYQuat, XMQuaternionInverse(vShadowQuat));
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CPipeLine::Bind_Shadow_Resource(CShader* pShader, const _char* pConstantName, D3DTS eType, _uint iShadowBoxIndex) const
|
||||
{
|
||||
return pShader->Bind_Matrix(pConstantName, &m_ShadowTransformStateMatrices[iShadowBoxIndex][ENUM_CLASS(eType)]);
|
||||
}
|
||||
|
||||
const _float4x4* CPipeLine::Get_ShadowMatricesPtr(_uint iShadowBoxIndex)
|
||||
{
|
||||
return m_ShadowTransformStateMatrices[iShadowBoxIndex];
|
||||
}
|
||||
|
||||
_float CPipeLine::Get_ShadowBoxFar(_uint iShadowBoxIndex)
|
||||
{
|
||||
_float4* targetPlanes = nullptr;
|
||||
if (iShadowBoxIndex == 0) targetPlanes = m_vNearShadowViewBoxPlane;
|
||||
if (iShadowBoxIndex == 1) targetPlanes = m_vMiddleShadowViewBoxPlane;
|
||||
if (iShadowBoxIndex == 2) targetPlanes = m_vFarShadowViewBoxPlane;
|
||||
|
||||
if (nullptr == targetPlanes) {
|
||||
assert(false);
|
||||
return -1.f;
|
||||
}
|
||||
return -targetPlanes[5].w; // Far
|
||||
}
|
||||
|
||||
void CPipeLine::Update()
|
||||
{
|
||||
for (_uint i = ENUM_CLASS(D3DTS::VIEW_INV); i < ENUM_CLASS(D3DTS::END); ++i)
|
||||
{
|
||||
XMStoreFloat4x4(&m_TransformStateMatrices[i], XMMatrixInverse(nullptr, Get_Transform_Matrix(static_cast<D3DTS>(i-2))));
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
Describe_Entity();
|
||||
#endif // DEBUG
|
||||
Update_ShadowDepthNdcZ();
|
||||
|
||||
XMStoreFloat4x4(&m_TransformStateMatrices[ENUM_CLASS(D3DTS::VIEW_INV)], XMMatrixInverse(nullptr, Get_Transform_Matrix(D3DTS::VIEW)));
|
||||
XMStoreFloat4x4(&m_TransformStateMatrices[ENUM_CLASS(D3DTS::PROJ_INV)], XMMatrixInverse(nullptr, Get_Transform_Matrix(D3DTS::PROJ)));
|
||||
|
||||
|
||||
memcpy(&m_vCamPosition, &m_TransformStateMatrices[ENUM_CLASS(D3DTS::VIEW_INV)].m[3], sizeof(_float4));
|
||||
_matrix ProjInvMatrix = XMLoadFloat4x4(&m_TransformStateMatrices[ENUM_CLASS(D3DTS::PROJ_INV)]);
|
||||
_matrix ViewInvMatrix = XMLoadFloat4x4(&m_TransformStateMatrices[ENUM_CLASS(D3DTS::VIEW_INV)]);
|
||||
for (size_t i = 0; i < 8; i++)
|
||||
_matrix WorldFromClipMatrix = XMMatrixMultiply(ProjInvMatrix, ViewInvMatrix);
|
||||
|
||||
_vector vShadowInvQuat = XMLoadFloat4(&m_vShadowInvDirectionalRPYQuat);
|
||||
for (size_t i = 0; i < 8; ++i)
|
||||
{
|
||||
XMStoreFloat4(&m_vWorldPoints[i], XMVector3TransformCoord(XMLoadFloat4(&m_vOriginalPoints[i]), ProjInvMatrix));
|
||||
XMStoreFloat4(&m_vWorldPoints[i], XMVector3TransformCoord(XMLoadFloat4(&m_vWorldPoints[i]), ViewInvMatrix));
|
||||
XMStoreFloat4(&m_vWorldPoints[i], XMVector3TransformCoord(XMLoadFloat4(&m_vOriginalRenderFrustumPoints[i]), WorldFromClipMatrix));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 16; ++i)
|
||||
{ // World Point To 라이트 정렬 공간
|
||||
_vector vPoint = XMVector3TransformCoord(XMLoadFloat4(&m_vOriginalShadowFrustumPoints[i]), WorldFromClipMatrix);
|
||||
vPoint = XMVectorSetW(XMVector3Rotate(vPoint, vShadowInvQuat), 1.f);
|
||||
XMStoreFloat4(&m_vShadowViewPoints[i], vPoint);
|
||||
}
|
||||
|
||||
Make_Planes(m_vWorldPoints, m_vWorldPlanes);
|
||||
Make_LightBoxes();
|
||||
}
|
||||
|
||||
HRESULT CPipeLine::Initialize()
|
||||
{
|
||||
m_vOriginalPoints[0] = _float4(-1.f, 1.f, 0.f, 1.f); // LT
|
||||
m_vOriginalPoints[1] = _float4(1.f, 1.f, 0.f, 1.f); // RT
|
||||
m_vOriginalPoints[2] = _float4(1.f, -1.f, 0.f, 1.f); // RB
|
||||
m_vOriginalPoints[3] = _float4(-1.f, -1.f, 0.f, 1.f); // LB
|
||||
m_vOriginalShadowFrustumPoints[0] = m_vOriginalRenderFrustumPoints[0] = _float4(-1.f, 1.f, 0.f, 1.f); // LT
|
||||
m_vOriginalShadowFrustumPoints[1] = m_vOriginalRenderFrustumPoints[1] = _float4(1.f, 1.f, 0.f, 1.f); // RT
|
||||
m_vOriginalShadowFrustumPoints[2] = m_vOriginalRenderFrustumPoints[2] = _float4(1.f, -1.f, 0.f, 1.f); // RB
|
||||
m_vOriginalShadowFrustumPoints[3] = m_vOriginalRenderFrustumPoints[3] = _float4(-1.f, -1.f, 0.f, 1.f); // LB
|
||||
|
||||
m_vOriginalPoints[4] = _float4(-1.f, 1.f, 1.f, 1.f); // LT
|
||||
m_vOriginalPoints[5] = _float4(1.f, 1.f, 1.f, 1.f); // RT
|
||||
m_vOriginalPoints[6] = _float4(1.f, -1.f, 1.f, 1.f); // RB
|
||||
m_vOriginalPoints[7] = _float4(-1.f, -1.f, 1.f, 1.f); // LB
|
||||
m_vOriginalShadowFrustumPoints[4] = _float4(-1.f, 1.f, m_fShadowNearBoxRatio, 1.f); // LT
|
||||
m_vOriginalShadowFrustumPoints[5] = _float4(1.f, 1.f, m_fShadowNearBoxRatio, 1.f); // RT
|
||||
m_vOriginalShadowFrustumPoints[6] = _float4(1.f, -1.f, m_fShadowNearBoxRatio, 1.f); // RB
|
||||
m_vOriginalShadowFrustumPoints[7] = _float4(-1.f, -1.f, m_fShadowNearBoxRatio, 1.f); // LB
|
||||
|
||||
for (_uint i = ENUM_CLASS(D3DTS::VIEW); i < ENUM_CLASS(D3DTS::END); ++i)
|
||||
{
|
||||
m_vOriginalShadowFrustumPoints[8] = _float4(-1.f, 1.f, m_fShadowFarBoxRatio, 1.f); // LT
|
||||
m_vOriginalShadowFrustumPoints[9] = _float4(1.f, 1.f, m_fShadowFarBoxRatio, 1.f); // RT
|
||||
m_vOriginalShadowFrustumPoints[10]= _float4(1.f, -1.f, m_fShadowFarBoxRatio, 1.f); // RB
|
||||
m_vOriginalShadowFrustumPoints[11]= _float4(-1.f, -1.f, m_fShadowFarBoxRatio, 1.f); // LB
|
||||
|
||||
m_vOriginalShadowFrustumPoints[12] = m_vOriginalRenderFrustumPoints[4] = _float4(-1.f, 1.f, 1.f, 1.f); // LT
|
||||
m_vOriginalShadowFrustumPoints[13] = m_vOriginalRenderFrustumPoints[5] = _float4(1.f, 1.f, 1.f, 1.f); // RT
|
||||
m_vOriginalShadowFrustumPoints[14] = m_vOriginalRenderFrustumPoints[6] = _float4(1.f, -1.f, 1.f, 1.f); // RB
|
||||
m_vOriginalShadowFrustumPoints[15] = m_vOriginalRenderFrustumPoints[7] = _float4(-1.f, -1.f, 1.f, 1.f); // LB
|
||||
|
||||
for (_uint i = ENUM_CLASS(D3DTS::VIEW); i < ENUM_CLASS(D3DTS::END); ++i) {
|
||||
XMStoreFloat4x4(&m_TransformStateMatrices[i], XMMatrixIdentity());
|
||||
}
|
||||
|
||||
@@ -140,7 +252,116 @@ void CPipeLine::Make_Planes(const _float4* pPoints, _float4* pPlanes)
|
||||
XMStoreFloat4(&pPlanes[5], XMPlaneFromPoints(XMLoadFloat4(&pPoints[0]), XMLoadFloat4(&pPoints[1]), XMLoadFloat4(&pPoints[2])));
|
||||
}
|
||||
|
||||
ID3D11ShaderResourceView* CPipeLine::Find_GlobalShader(const _tchar* wszKeyGlobalSRV)
|
||||
void CPipeLine::Make_LightBoxes()
|
||||
{
|
||||
// AABB Lighting space
|
||||
_vector vShadowInvQuat = XMLoadFloat4(&m_vShadowInvDirectionalRPYQuat);
|
||||
_matrix ViewMatrix = XMMatrixRotationQuaternion(vShadowInvQuat);
|
||||
_matrix ViewInvMatrix = XMMatrixInverse(nullptr, ViewMatrix);
|
||||
|
||||
for (_uint iIndex = 0; iIndex < 3; ++iIndex)
|
||||
{
|
||||
_float4 vCascadePoints8[8] = {};
|
||||
Get_CascadePoints8(m_vShadowViewPoints, iIndex, vCascadePoints8);
|
||||
|
||||
_float fMinX = vCascadePoints8[0].x;
|
||||
_float fMinY = vCascadePoints8[0].y;
|
||||
_float fMinZ = vCascadePoints8[0].z;
|
||||
|
||||
_float fMaxX = vCascadePoints8[0].x;
|
||||
_float fMaxY = vCascadePoints8[0].y;
|
||||
_float fMaxZ = vCascadePoints8[0].z;
|
||||
|
||||
for (_uint iPointIndex = 1; iPointIndex < 8; ++iPointIndex)
|
||||
{
|
||||
fMinX = min(fMinX, vCascadePoints8[iPointIndex].x);
|
||||
fMinY = min(fMinY, vCascadePoints8[iPointIndex].y);
|
||||
fMinZ = min(fMinZ, vCascadePoints8[iPointIndex].z);
|
||||
|
||||
fMaxX = max(fMaxX, vCascadePoints8[iPointIndex].x);
|
||||
fMaxY = max(fMaxY, vCascadePoints8[iPointIndex].y);
|
||||
fMaxZ = max(fMaxZ, vCascadePoints8[iPointIndex].z);
|
||||
}
|
||||
|
||||
_float4* targetPlanes = nullptr;
|
||||
if (iIndex == 0) targetPlanes = m_vNearShadowViewBoxPlane;
|
||||
if (iIndex == 1) targetPlanes = m_vMiddleShadowViewBoxPlane;
|
||||
if (iIndex == 2) targetPlanes = m_vFarShadowViewBoxPlane;
|
||||
|
||||
targetPlanes[0] = _float4(-1.f, 0.f, 0.f, fMinX); // Left
|
||||
targetPlanes[1] = _float4(1.f, 0.f, 0.f, -fMaxX); // Right
|
||||
targetPlanes[2] = _float4(0.f, -1.f, 0.f, fMinY); // Bottom
|
||||
targetPlanes[3] = _float4(0.f, 1.f, 0.f, -fMaxY); // Top
|
||||
targetPlanes[4] = _float4(0.f, 0.f, -1.f, fMinZ); // Near
|
||||
targetPlanes[5] = _float4(0.f, 0.f, 1.f, -fMaxZ); // Far
|
||||
|
||||
|
||||
XMStoreFloat4x4(&m_ShadowTransformStateMatrices[iIndex][ENUM_CLASS(D3DTS::VIEW)], ViewMatrix);
|
||||
XMStoreFloat4x4(&m_ShadowTransformStateMatrices[iIndex][ENUM_CLASS(D3DTS::VIEW_INV)], ViewInvMatrix);
|
||||
|
||||
_matrix ProjMatrix = XMMatrixOrthographicOffCenterLH(fMinX, fMaxX, fMinY, fMaxY, fMinZ, fMaxZ);
|
||||
XMStoreFloat4x4(&m_ShadowTransformStateMatrices[iIndex][ENUM_CLASS(D3DTS::PROJ)], ProjMatrix);
|
||||
XMStoreFloat4x4(&m_ShadowTransformStateMatrices[iIndex][ENUM_CLASS(D3DTS::PROJ_INV)], XMMatrixInverse(nullptr, ProjMatrix));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void CPipeLine::Get_CascadePoints8(const _float4* pShadowViewPoints16, _uint iIndexCascade, _float4* pOutPoints8)
|
||||
{
|
||||
_uint iIndexNear = 0;
|
||||
_uint iIndexFar = 0;
|
||||
|
||||
if (iIndexCascade == 0) { iIndexNear = 0; iIndexFar = 4; } // near ~ shadownear
|
||||
if (iIndexCascade == 1) { iIndexNear = 4; iIndexFar = 8; } // shadownear ~ shadowfar
|
||||
if (iIndexCascade == 2) { iIndexNear = 8; iIndexFar = 12; } // shadowfar ~ far
|
||||
|
||||
for (_uint iIndex = 0; iIndex < 4; ++iIndex) {
|
||||
pOutPoints8[iIndex] = pShadowViewPoints16[iIndexNear + iIndex];
|
||||
pOutPoints8[4 + iIndex] = pShadowViewPoints16[iIndexFar + iIndex];
|
||||
}
|
||||
}
|
||||
|
||||
void CPipeLine::Update_ShadowDepthNdcZ()
|
||||
{
|
||||
_matrix ProjectionMatrix = Get_Transform_Matrix(D3DTS::PROJ);
|
||||
|
||||
_float fProjMatrix22 = ProjectionMatrix.r[2].m128_f32[2];
|
||||
_float fProjMatrix32 = ProjectionMatrix.r[3].m128_f32[2];
|
||||
|
||||
_float fNearPlane = -fProjMatrix32 / fProjMatrix22;
|
||||
_float fFarPlane = (fProjMatrix22 * fNearPlane) / (fProjMatrix22 - 1.0f);
|
||||
|
||||
_float fShadowViewDepth0 = fNearPlane + (fFarPlane - fNearPlane) * m_fShadowNearBoxRatio;
|
||||
_float fShadowViewDepth1 = fNearPlane + (fFarPlane - fNearPlane) * m_fShadowFarBoxRatio;
|
||||
|
||||
_float fNDCZ0 = { };
|
||||
{
|
||||
_vector vViewPort = XMVectorSet(0.f, 0.f, fShadowViewDepth0, 1.f);
|
||||
_vector vProj = XMVector4Transform(vViewPort, ProjectionMatrix);
|
||||
_float fProjZ = XMVectorGetZ(vProj);
|
||||
_float fProjW = XMVectorGetW(vProj);
|
||||
fNDCZ0 = (fProjZ / fProjW);
|
||||
}
|
||||
_float fNDCZ1 = { };
|
||||
{
|
||||
_vector vViewPort = XMVectorSet(0.f, 0.f, fShadowViewDepth1, 1.f);
|
||||
_vector vProj = XMVector4Transform(vViewPort, ProjectionMatrix);
|
||||
_float fProjZ = XMVectorGetZ(vProj);
|
||||
_float fProjW = XMVectorGetW(vProj);
|
||||
fNDCZ1 = (fProjZ / fProjW);
|
||||
}
|
||||
|
||||
fNDCZ0 = clamp(fNDCZ0, 0.0f, 1.0f);
|
||||
fNDCZ1 = clamp(fNDCZ1, 0.0f, 1.0f);
|
||||
|
||||
for (size_t iIndex = 0; iIndex < 4; ++iIndex)
|
||||
{
|
||||
m_vOriginalShadowFrustumPoints[4 + iIndex].z = fNDCZ0;
|
||||
m_vOriginalShadowFrustumPoints[8 + iIndex].z = fNDCZ1;
|
||||
}
|
||||
}
|
||||
|
||||
ID3D11ShaderResourceView* CPipeLine::Find_GlobalShaderResourceView(const _tchar* wszKeyGlobalSRV)
|
||||
{
|
||||
auto iter = m_mapGlobalSRV.find(wszKeyGlobalSRV);
|
||||
if (iter == m_mapGlobalSRV.end()) {
|
||||
@@ -170,3 +391,27 @@ void CPipeLine::Free()
|
||||
m_mapGlobalSRV.clear();
|
||||
SAFE_RELEASE(m_pGameInstance);
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
|
||||
void CPipeLine::Describe_Entity()
|
||||
{
|
||||
GUI::Begin("SYSTEM");
|
||||
if (GUI::CollapsingHeader("PipeLine")) {
|
||||
_bool bModified = { false };
|
||||
_float fNear = m_fShadowNearBoxRatio;
|
||||
_float fFar = m_fShadowFarBoxRatio;
|
||||
if (GUI::DragFloat("m_fShadowNearBoxRatio", &fNear, 0.01f, 0.01f, 0.999f, "%.2f")) {
|
||||
bModified = true;
|
||||
}
|
||||
if (GUI::DragFloat("m_fShadowFarBoxRatio", &fFar, 0.01f, 0.01f, 0.999f, "%.2f")) {
|
||||
bModified = true;
|
||||
}
|
||||
if (true == bModified) {
|
||||
m_fShadowNearBoxRatio = fNear;
|
||||
m_fShadowFarBoxRatio = fFar;
|
||||
}
|
||||
}
|
||||
GUI::End();
|
||||
}
|
||||
|
||||
#endif // _DEBUG
|
||||
|
||||
Reference in New Issue
Block a user