From ee8a1c1bc661b9faea34ccec7bdcbf38193bad0c Mon Sep 17 00:00:00 2001 From: Saty <5752427+SatyPardus@users.noreply.github.com> Date: Sun, 27 Sep 2026 00:16:27 +0200 Subject: [PATCH] ShaderEffect: port ShaderEffect, ShadowCache, manager and parser TUs ShaderEffect.cpp (0x8728C0-0x873F50), ShadowCache.cpp (0x873F60-0x876370), ShaderEffectManager.cpp (0x876370-0x876E30) and ShaderEffectParser.cpp (0x876E30-0x8773F0). Shared additions: GxRenderTargetSet, CGxDevice::DeviceGetRenderTarget, C44Matrix::Identity, CWFrustum (m_sceneLink removed: 0xF4 stride), M2LIGHTTYPE and the CM2Lighting::SetupGxLights declaration. Co-Authored-By: Claude Opus 5.5 (1M context) --- Engine/Source/Gx/CGxDevice/CGxDevice.cpp | 6 + Engine/Source/Gx/CGxDevice/CGxDevice.h | 1 + Engine/Source/Gx/Gx.cpp | 7 + Engine/Source/Gx/Gx.h | 1 + Engine/Source/Model2/M2Light.h | 5 +- Engine/Source/Model2/Types.h | 5 + Engine/Source/ShaderEffect/ShaderEffect.cpp | 587 ++++++++- Engine/Source/ShaderEffect/ShaderEffect.h | 77 +- .../ShaderEffect/ShaderEffectManager.cpp | 70 +- .../Source/ShaderEffect/ShaderEffectManager.h | 17 + .../ShaderEffect/ShaderEffectParser.cpp | 237 +++- .../Source/ShaderEffect/ShaderEffectParser.h | 35 + Engine/Source/ShaderEffect/ShadowCache.cpp | 1070 ++++++++++++++++- Engine/Source/ShaderEffect/ShadowCache.h | 110 ++ Engine/Source/ShaderEffect/Types.h | 27 + Engine/Source/Tempest/C44Matrix.cpp | 21 + Engine/Source/Tempest/C44Matrix.h | 1 + _Triage/CWFrustum.h | 5 +- 18 files changed, 2265 insertions(+), 17 deletions(-) create mode 100644 Engine/Source/ShaderEffect/Types.h diff --git a/Engine/Source/Gx/CGxDevice/CGxDevice.cpp b/Engine/Source/Gx/CGxDevice/CGxDevice.cpp index 042bf33..9d40d3c 100644 --- a/Engine/Source/Gx/CGxDevice/CGxDevice.cpp +++ b/Engine/Source/Gx/CGxDevice/CGxDevice.cpp @@ -1897,3 +1897,9 @@ void CGxDevice::ITexWHDStartEnd(CGxTex* texId, uint32_t& width, uint32_t& height mipCount = baseMip + 1; } } + +// VERIFY: 4458969e manual +// OFFSET: 0x00682D50 +void CGxDevice::DeviceGetRenderTarget(EGxBuffer buffer, CGxTex*& texture) { + texture = this->m_textureTarget[buffer].m_texture; +} diff --git a/Engine/Source/Gx/CGxDevice/CGxDevice.h b/Engine/Source/Gx/CGxDevice/CGxDevice.h index 9a7a128..ddbc72f 100644 --- a/Engine/Source/Gx/CGxDevice/CGxDevice.h +++ b/Engine/Source/Gx/CGxDevice/CGxDevice.h @@ -304,6 +304,7 @@ class CGxDevice { const CRect& DeviceCurWindow(); const CRect& DeviceDefWindow(); void DeviceGamma(CGxGammaRamp& ramp); + void DeviceGetRenderTarget(EGxBuffer buffer, CGxTex*& texture); void DeviceScreenShot(); void DeviceSetCurWindow(const CRect& rect); void DeviceSetDefWindow(const CRect& rect); diff --git a/Engine/Source/Gx/Gx.cpp b/Engine/Source/Gx/Gx.cpp index 9517ff7..e8f9529 100644 --- a/Engine/Source/Gx/Gx.cpp +++ b/Engine/Source/Gx/Gx.cpp @@ -750,3 +750,10 @@ void GxFormatColor(CImVector& color) { color = formatted; } } + +// VERIFY: 25db0bac manual +// OFFSET: 0x0057E4F0 +void GxRenderTargetSet(EGxBuffer buffer, CGxTex* texture, uint32_t plane) { + GxRsSet(GxRs_ScissorTest, 0); + g_theGxDevicePtr->DeviceSetRenderTarget(buffer, texture, plane); +} diff --git a/Engine/Source/Gx/Gx.h b/Engine/Source/Gx/Gx.h index 8bb084f..c6195e5 100644 --- a/Engine/Source/Gx/Gx.h +++ b/Engine/Source/Gx/Gx.h @@ -22,6 +22,7 @@ class CRect; char* GxBufLock(CGxBuf* buf); void GxBufUnlock(CGxBuf* buf, uint32_t size); void GxBufDestroy(CGxBuf* buf); +void GxRenderTargetSet(EGxBuffer buffer, CGxTex* texture, uint32_t plane); void GxBufData(CGxBuf* buf, const void* data, uint32_t size, uint32_t offset); CGxBuf* GxBufStream(EGxPoolTarget target, uint32_t itemSize, uint32_t itemCount); void GxPrimIndexPtr(uint32_t indexCount, const uint16_t* indices); diff --git a/Engine/Source/Model2/M2Light.h b/Engine/Source/Model2/M2Light.h index 6b553b2..e76d1ff 100644 --- a/Engine/Source/Model2/M2Light.h +++ b/Engine/Source/Model2/M2Light.h @@ -4,6 +4,7 @@ #include #include #include +#include "Engine/Source/Model2/Types.h" #include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/C4Plane.h" #include "Engine/Source/Tempest/CAaSphere.h" @@ -15,7 +16,7 @@ class CM2Light { public: /* 0000 */ CM2Scene* m_scene; /* 0004 */ uint32_t m_stamp; - /* 0008 */ uint32_t m_type; + /* 0008 */ M2LIGHTTYPE m_type; /* 000C */ C3Vector m_pos; /* 0018 */ C3Vector m_viewPos; /* 0024 */ C3Vector m_dir; @@ -53,6 +54,8 @@ class CM2Lighting { /* 00B4 */ float m_fogDensity; /* 00B8 */ C3Vector m_fogColor; /* 00C4 */ C4Plane m_liquidPlane; + + void SetupGxLights(const C3Vector* origin); }; #endif diff --git a/Engine/Source/Model2/Types.h b/Engine/Source/Model2/Types.h index d921162..6ae12bb 100644 --- a/Engine/Source/Model2/Types.h +++ b/Engine/Source/Model2/Types.h @@ -5,6 +5,11 @@ #include #include +enum M2LIGHTTYPE { + M2LIGHT_DIRECT = 0, + M2LIGHT_POINT = 1, +}; + class CM2ModelCall; class CM2ModelCall { diff --git a/Engine/Source/ShaderEffect/ShaderEffect.cpp b/Engine/Source/ShaderEffect/ShaderEffect.cpp index 6bb4e6c..eecacea 100644 --- a/Engine/Source/ShaderEffect/ShaderEffect.cpp +++ b/Engine/Source/ShaderEffect/ShaderEffect.cpp @@ -1 +1,586 @@ -#include "ShaderEffect.h" +#include +#include +#include +#include +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/CGxDevice/CGxShader.h" +#include "Engine/Source/Gx/CGxMatrixStack.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/ShaderEffect/ShadowCache.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/CImVector.h" + +// STATIC: 0x00D43008 +int32_t CShaderEffect::s_useAlphaRef; + +// STATIC: 0x00D4300C +float CShaderEffect::s_fogMul; + +// STATIC: 0x00D43010 +uint32_t CShaderEffect::s_shadowValue; + +// STATIC: 0x00D43014 +uint32_t CShaderEffect::s_usePcfFiltering; + +// STATIC: 0x00D43018 +uint32_t CShaderEffect::s_localLightCount; + +// STATIC: 0x00D4301C +int32_t CShaderEffect::s_lightEnabled; + +// STATIC: 0x00D43020 +int32_t CShaderEffect::s_enableShaders; + +// STATIC: 0x00D43024 +CShaderEffect* CShaderEffect::s_curEffect; + +// STATIC: 0x00D43028 +C3Vector CShaderEffect::s_sunDiffuse; + +// STATIC: 0x00D43034 +C3Vector CShaderEffect::s_sunAmbient; + +// STATIC: 0x00D43040 +C3Vector CShaderEffect::s_sunDir; + +// STATIC: 0x00D4304C +C3Vector CShaderEffect::s_sunSpec; + +// STATIC: 0x00D43058 +C4Vector CShaderEffect::s_fogColorAlphaRef; + +// STATIC: 0x00D43068 +C4Vector CShaderEffect::s_fogParams; + +// STATIC: 0x00D43098 +LocalLights CShaderEffect::s_localLights; + +// VERIFY: 9049c785 manual +// OFFSET: 0x008728C0 +void CShaderEffect::InitFixedFuncPass(const EGxTexOp* colorOps, const EGxTexOp* alphaOps, uint32_t count) { + this->m_fixedFuncPassCount = count; + + for (uint32_t i = 0; i < count; i++) { + this->m_colorOps[i] = colorOps[i]; + this->m_alphaOps[i] = alphaOps[i]; + } +} + +// VERIFY: be9bb4f7 manual +// OFFSET: 0x00872900 +void CShaderEffect::ComputeLocalLights(LocalLights* localLights, uint32_t lightCount, CM2Light** lights, const C3Vector* origin) { + uint32_t i = 0; + + if (lightCount) { + C44Matrix view; + + if (origin) { + GxXformView(view); + } + + for (; i < lightCount; i++) { + CM2Light* light = lights[i]; + + if (light->m_type == M2LIGHT_POINT) { + localLights->m_color[i].x = light->m_dirColor.x; + localLights->m_color[i].y = light->m_dirColor.y; + localLights->m_color[i].z = light->m_dirColor.z; + localLights->m_color[i].w = 1.0f; + + C3Vector position; + + if (origin) { + position = (light->m_pos - *origin) * view; + } else { + position = light->m_viewPos; + } + + localLights->m_position[i].x = position.x; + localLights->m_position[i].y = position.y; + localLights->m_position[i].z = position.z; + localLights->m_position[i].w = 1.0f; + + localLights->m_constantAttenuation[i] = light->m_constantAttenuation; + localLights->m_linearAttenuation[i] = light->m_linearAttenuation; + localLights->m_quadraticAttenuation[i] = light->m_quadraticAttenuation; + } else { + localLights->m_color[i].x = 0.0f; + localLights->m_color[i].y = 0.0f; + localLights->m_color[i].z = 0.0f; + localLights->m_color[i].w = 0.0f; + } + } + } + + for (; i < 4; i++) { + localLights->m_color[i].x = 0.0f; + localLights->m_color[i].y = 0.0f; + localLights->m_color[i].z = 0.0f; + localLights->m_color[i].w = 0.0f; + } +} + +// VERIFY: 3d4e9331 auto +// OFFSET: 0x00872AD0 +void CShaderEffect::SetUsePcfFiltering(uint32_t usePcfFiltering) { + if (usePcfFiltering) { + CShaderEffect::s_usePcfFiltering = 1; + + if (CShaderEffect::s_enableShaders) { + return; + } + } + + CShaderEffect::s_usePcfFiltering = 0; +} + +// VERIFY: b3663650 manual +// OFFSET: 0x00872B00 +void CShaderEffect::UpdateWorldViewMatrix() { + if (!CShaderEffect::s_enableShaders) { + return; + } + + C44Matrix view; + C44Matrix world; + GxXformView(view); + + CGxMatrixStack& worldStack = g_theGxDevicePtr->m_xforms[GxXform_World]; + world = worldStack.m_mtx[worldStack.m_level]; + + C44Matrix worldView = world * view; + worldView = worldView.Transpose(); + + GxShaderConstantsSet(GxSh_Vertex, 31, reinterpret_cast(&worldView), 4); +} + +// VERIFY: 3063e016 auto +// OFFSET: 0x00872C10 +void CShaderEffect::UpdateProjMatrix() { + if (!CShaderEffect::s_enableShaders) { + return; + } + + C44Matrix projection; + projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + projection.c0 = projection.c0 * -1.0f; + projection.c1 = projection.c1 * -1.0f; + projection.c2 = projection.c2 * -1.0f; + projection.c3 = -1.0f * projection.c3; + } + + projection = projection.Transpose(); + + GxShaderConstantsSet(GxSh_Vertex, 2, reinterpret_cast(&projection), 4); +} + +// VERIFY: 326b299e auto +// OFFSET: 0x00872CE0 +void CShaderEffect::InitShaderSystem(int32_t enableShaders, int32_t usePcfFiltering) { + CShaderEffect::s_enableShaders = enableShaders; + CShaderEffect::SetUsePcfFiltering(usePcfFiltering); + CShaderEffect::s_fogMul = 1.0f; + CShaderEffect::s_useAlphaRef = g_theGxDevicePtr->Caps().unk_0130; +} + +// VERIFY: fd7f8aba manual +// OFFSET: 0x00872D30 +void CShaderEffect::InitEffect(const char* vertexShader, const char* pixelShader) { + memset(this->m_vertexShaders, 0, sizeof(this->m_vertexShaders)); + memset(this->m_pixelShaders, 0, sizeof(this->m_pixelShaders)); + this->m_fixedFuncPassCount = 0; + + if (!CShaderEffect::s_enableShaders || !vertexShader || !pixelShader) { + return; + } + + g_theGxDevicePtr->ShaderCreate(this->m_vertexShaders, GxSh_Vertex, "Shaders\\Vertex", vertexShader, sizeof(this->m_vertexShaders) / sizeof(this->m_vertexShaders[0])); + g_theGxDevicePtr->ShaderCreate(this->m_pixelShaders, GxSh_Pixel, "Shaders\\Pixel", pixelShader, sizeof(this->m_pixelShaders) / sizeof(this->m_pixelShaders[0])); +} + +// VERIFY: 9b6a2325 manual +// OFFSET: 0x00872DE0 +uint32_t CShaderEffect::SelectShadowShader() { + if (!(CShaderEffect::s_fogColorAlphaRef.w > 0.0f) || (g_theGxDevicePtr->Caps().unk_0130 && !CShaderEffect::s_shadowValue)) { + return CShaderEffect::s_shadowValue + 4 * CShaderEffect::s_usePcfFiltering; + } + + return CShaderEffect::s_shadowValue + 4 * (CShaderEffect::s_usePcfFiltering + 2); +} + +// VERIFY: ae00ba32 manual +// OFFSET: 0x00872E40 +void CShaderEffect::SetGlobalLighting(CM2Light* light) { + CShaderEffect::s_sunDir = light->m_dir; + CShaderEffect::s_sunSpec = light->m_specColor; + + C44Matrix view; + GxXformView(view); + + C3Vector sunDir; + sunDir.x = view.c0 * CShaderEffect::s_sunDir.z + view.b0 * CShaderEffect::s_sunDir.y + view.a0 * CShaderEffect::s_sunDir.x; + sunDir.y = CShaderEffect::s_sunDir.z * view.c1 + CShaderEffect::s_sunDir.y * view.b1 + CShaderEffect::s_sunDir.x * view.a1; + sunDir.z = CShaderEffect::s_sunDir.z * view.c2 + CShaderEffect::s_sunDir.y * view.b2 + CShaderEffect::s_sunDir.x * view.a2; + CShaderEffect::s_sunDir = sunDir; + + if (CShaderEffect::s_enableShaders) { + C4Vector sunSpec; + sunSpec.x = CShaderEffect::s_sunSpec.x; + sunSpec.y = CShaderEffect::s_sunSpec.y; + sunSpec.z = CShaderEffect::s_sunSpec.z; + sunSpec.w = 14.0f; + + GxShaderConstantsSet(GxSh_Vertex, 12, &CShaderEffect::s_sunDir.x, 1); + GxShaderConstantsSet(GxSh_Vertex, 13, &sunSpec.x, 1); + } +} + +// VERIFY: fd36fa66 manual +// OFFSET: 0x00872F90 +void CShaderEffect::SetCurrent() { + CShaderEffect::s_curEffect = this; + + if (CShaderEffect::s_enableShaders || !this->m_fixedFuncPassCount) { + return; + } + + for (uint32_t i = 0; i < this->m_fixedFuncPassCount; i++) { + GxRsSet(static_cast(GxRs_ColorOp0 + i), this->m_colorOps[i]); + GxRsSet(static_cast(GxRs_AlphaOp0 + i), this->m_alphaOps[i]); + } +} + +// VERIFY: 0344992e manual +// OFFSET: 0x00873060 +void CShaderEffect::SetShaders(uint32_t vertexPermute, uint32_t pixelPermute) { + int32_t useAlphaRef = 1; + + if (CShaderEffect::s_enableShaders) { + g_theGxDevicePtr->RsSet(GxRs_VertexShader, CShaderEffect::s_curEffect->m_vertexShaders[vertexPermute]); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CShaderEffect::s_curEffect->m_pixelShaders[pixelPermute]); + useAlphaRef = (pixelPermute & 8) == 0; + } + + if (useAlphaRef == CShaderEffect::s_useAlphaRef) { + return; + } + + CShaderEffect::s_useAlphaRef = useAlphaRef; + + if (useAlphaRef) { + float alphaRef = CShaderEffect::s_fogColorAlphaRef.w * 255.0f; + GxRsSet(GxRs_AlphaRef, static_cast(alphaRef)); + } else { + GxShaderConstantsSet(GxSh_Pixel, 2, &CShaderEffect::s_fogColorAlphaRef.x, 1); + GxRsSet(GxRs_AlphaRef, 0); + } +} + +// VERIFY: f5c47bbc auto +// OFFSET: 0x00873160 +void CShaderEffect::SetDefaultShaders(uint32_t lightCount) { + if (!CShaderEffect::s_enableShaders) { + return; + } + + uint32_t shadowValue = CShaderEffect::s_shadowValue > 2 ? 2 : CShaderEffect::s_shadowValue; + uint32_t lights = lightCount > 2 ? 2 : lightCount; + + uint32_t vertexPermute = CShaderEffect::s_lightEnabled + 2 * (CShaderEffect::s_localLightCount + 5 * (lights + 3 * shadowValue)); + + CShaderEffect::SetShaders(vertexPermute, CShaderEffect::SelectShadowShader()); +} + +// VERIFY: 71c6c864 manual +// OFFSET: 0x008731C0 +void CShaderEffect::SetLightingEnabled(int32_t enabled) { + CShaderEffect::s_lightEnabled = enabled; + + if (!CShaderEffect::s_enableShaders) { + GxRsSet(GxRs_Lighting, enabled); + } +} + +// VERIFY: 26a9b9a3 manual +// OFFSET: 0x00873210 +void CShaderEffect::SetFogParams(float start, float end, float density, const CImVector& color) { + if (CShaderEffect::s_enableShaders) { + CShaderEffect::s_fogColorAlphaRef.x = color.r * (1.0f / 255.0f); + CShaderEffect::s_fogColorAlphaRef.y = color.g * (1.0f / 255.0f); + CShaderEffect::s_fogColorAlphaRef.z = (1.0f / 255.0f) * color.b; + + double scale = 1.0 / (end - start); + CShaderEffect::s_fogParams.x = -(CShaderEffect::s_fogMul * scale); + CShaderEffect::s_fogParams.y = end * scale; + CShaderEffect::s_fogParams.z = density; + CShaderEffect::s_fogParams.w = 0.0f; + + if (!g_theGxDevicePtr->Caps().unk_0134) { + GxShaderConstantsSet(GxSh_Pixel, 2, &CShaderEffect::s_fogColorAlphaRef.x, 1); + return; + } + } else { + GxRsSet(GxRs_FogStart, start); + GxRsSet(GxRs_FogEnd, end); + } + + GxRsSet(GxRs_FogColor, *reinterpret_cast(&color)); +} + +// VERIFY: af8a3952 manual +// OFFSET: 0x00873390 +void CShaderEffect::SetFogEnabled(int32_t enabled) { + if (enabled && g_theGxDevicePtr->MasterEnable(GxMasterEnable_Fog)) { + if (CShaderEffect::s_enableShaders) { + GxShaderConstantsSet(GxSh_Vertex, 30, &CShaderEffect::s_fogParams.x, 1); + + if (!g_theGxDevicePtr->Caps().unk_0138) { + return; + } + } + + GxRsSet(GxRs_Fog, 1); + return; + } + + if (!CShaderEffect::s_enableShaders || g_theGxDevicePtr->Caps().unk_0138) { + GxRsSet(GxRs_Fog, 0); + } else { + float fogParams[4] = { 0.0f, 1.0f, 1.0f, 0.0f }; + GxShaderConstantsSet(GxSh_Vertex, 30, fogParams, 1); + } +} + +// VERIFY: c6b67cc5 manual +// OFFSET: 0x00873480 +void CShaderEffect::SetTexMtx_Identity(uint32_t stage) { + if (CShaderEffect::s_enableShaders) { + float matrix[8] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; + GxShaderConstantsSet(GxSh_Vertex, 2 * stage + 6, matrix, 2); + return; + } + + GxRsSet(static_cast(GxRs_TextureShader0 + stage), 0); + g_theGxDevicePtr->m_xforms[stage].Identity(); + GxRsSet(static_cast(GxRs_TexGen0 + stage), 0); +} + +// VERIFY: ce1f9620 manual +// OFFSET: 0x00873550 +void CShaderEffect::SetTexMtx_SphereMap(uint32_t stage) { + if (CShaderEffect::s_enableShaders) { + float matrix[8] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; + GxShaderConstantsSet(GxSh_Vertex, 2 * stage + 6, matrix, 2); + return; + } + + GxRsSet(static_cast(GxRs_TextureShader0 + stage), 0); + g_theGxDevicePtr->m_xforms[stage].Identity(); + GxRsSet(static_cast(GxRs_TexGen0 + stage), 6); +} + +// VERIFY: d55e347c manual +// OFFSET: 0x00873620 +void CShaderEffect::SetTexMtx(const C44Matrix& matrix, uint32_t stage) { + if (CShaderEffect::s_enableShaders) { + float constants[8] = { matrix.a0, matrix.b0, matrix.c0, matrix.d0, matrix.a1, matrix.b1, matrix.c1, matrix.d1 }; + GxShaderConstantsSet(GxSh_Vertex, 2 * stage + 6, constants, 2); + return; + } + + GxRsSet(static_cast(GxRs_TextureShader0 + stage), 1); + + CGxMatrixStack& stack = g_theGxDevicePtr->m_xforms[stage]; + stack.m_dirty = 1; + stack.m_flags[stack.m_level] &= ~CGxMatrixStack::F_Identity; + stack.m_mtx[stack.m_level] = matrix; + + GxRsSet(static_cast(GxRs_TexGen0 + stage), 0); +} + +// VERIFY: 571cb58b manual +// OFFSET: 0x00873730 +void CShaderEffect::SetTexMtx_TexGen(EGxTexGen texGen, const C44Matrix& matrix, uint32_t stage) { + if (CShaderEffect::s_enableShaders) { + C44Matrix view; + GxXformView(view); + + float swap = view.a1; + view.a1 = view.b0; + view.b0 = swap; + swap = view.a2; + view.a2 = view.c0; + view.c0 = swap; + swap = view.b2; + view.b2 = view.c1; + view.c1 = swap; + view.d0 = view.d0 * -1.0f; + view.d1 = view.d1 * -1.0f; + view.d2 = -1.0f * view.d2; + + C44Matrix texMatrix = view * matrix; + float constants[8] = { texMatrix.a0, texMatrix.b0, texMatrix.c0, texMatrix.d0, texMatrix.a1, texMatrix.b1, texMatrix.c1, texMatrix.d1 }; + GxShaderConstantsSet(GxSh_Vertex, 2 * stage + 6, constants, 2); + return; + } + + GxRsSet(static_cast(GxRs_TextureShader0 + stage), 1); + + CGxMatrixStack& stack = g_theGxDevicePtr->m_xforms[stage]; + stack.m_dirty = 1; + stack.m_flags[stack.m_level] &= ~CGxMatrixStack::F_Identity; + stack.m_mtx[stack.m_level] = matrix; + + GxRsSet(static_cast(GxRs_TexGen0 + stage), 2); +} + +// VERIFY: f365063d manual +// OFFSET: 0x00873900 +void CShaderEffect::SetDiffuse(const C4Vector& color) { + if (CShaderEffect::s_enableShaders) { + GxShaderConstantsSet(GxSh_Vertex, 28, &color.x, 1); + return; + } + + float r = color.x < 0.0f ? 0.0f : (color.x >= 1.0f ? 1.0f : color.x); + float g = color.y < 0.0f ? 0.0f : (color.y >= 1.0f ? 1.0f : color.y); + float b = color.z < 0.0f ? 0.0f : (color.z >= 1.0f ? 1.0f : color.z); + float a = color.w < 0.0f ? 0.0f : (color.w >= 1.0f ? 1.0f : color.w); + + CImVector diffuse; + diffuse.r = static_cast(r * 255.0f); + diffuse.g = static_cast(g * 255.0f); + diffuse.b = static_cast(b * 255.0f); + diffuse.a = static_cast(a * 255.0f); + + GxRsSet(GxRs_MatDiffuse, *reinterpret_cast(&diffuse)); +} + +// VERIFY: d0cdacd9 manual +// OFFSET: 0x00873A50 +void CShaderEffect::SetEmissive(const C4Vector& color) { + if (CShaderEffect::s_enableShaders) { + GxShaderConstantsSet(GxSh_Vertex, 29, &color.x, 1); + return; + } + + float r = color.x < 0.0f ? 0.0f : (color.x >= 1.0f ? 1.0f : color.x); + float g = color.y < 0.0f ? 0.0f : (color.y >= 1.0f ? 1.0f : color.y); + float b = color.z < 0.0f ? 0.0f : (color.z >= 1.0f ? 1.0f : color.z); + float a = color.w < 0.0f ? 0.0f : (color.w >= 1.0f ? 1.0f : color.w); + + CImVector emissive; + emissive.r = static_cast(r * 255.0f); + emissive.g = static_cast(g * 255.0f); + emissive.b = static_cast(b * 255.0f); + emissive.a = static_cast(a * 255.0f); + + GxRsSet(GxRs_MatEmissive, *reinterpret_cast(&emissive)); +} + +// VERIFY: 7e25358b manual +// OFFSET: 0x00873BA0 +void CShaderEffect::SetAlphaRef(float alphaRef) { + CShaderEffect::s_fogColorAlphaRef.w = alphaRef; + + if (!CShaderEffect::s_useAlphaRef) { + GxShaderConstantsSet(GxSh_Pixel, 2, &CShaderEffect::s_fogColorAlphaRef.x, 1); + return; + } + + float value = alphaRef * 255.0f; + GxRsSet(GxRs_AlphaRef, static_cast(value)); +} + +// VERIFY: a90cae3f manual +// OFFSET: 0x00873C20 +CShaderEffect::~CShaderEffect() { + if (!CShaderEffect::s_enableShaders) { + return; + } + + for (uint32_t i = 0; i < sizeof(this->m_vertexShaders) / sizeof(this->m_vertexShaders[0]); i++) { + if (this->m_vertexShaders[i]) { + g_theGxDevicePtr->ShaderDestroy(this->m_vertexShaders[i]); + } + + this->m_vertexShaders[i] = nullptr; + } + + for (uint32_t i = 0; i < sizeof(this->m_pixelShaders) / sizeof(this->m_pixelShaders[0]); i++) { + if (this->m_pixelShaders[i]) { + g_theGxDevicePtr->ShaderDestroy(this->m_pixelShaders[i]); + } + + this->m_pixelShaders[i] = nullptr; + } +} + +// VERIFY: 17775013 manual +// OFFSET: 0x00873CA0 +void CShaderEffect::SetLocalLighting(CM2Lighting* lighting, int32_t enabled, const C3Vector* origin) { + CShaderEffect::s_lightEnabled = enabled; + + if (!CShaderEffect::s_enableShaders) { + GxRsSet(GxRs_Lighting, enabled); + } + + CShaderEffect::s_localLightCount = lighting ? lighting->m_lightCount : 0; + + if (!CShaderEffect::s_enableShaders) { + if (enabled) { + lighting->SetupGxLights(origin); + } + + return; + } + + if (!enabled) { + return; + } + + CShaderEffect::s_sunDir = lighting->m_sunDir; + + if (CShaderEffect::s_sunDir.x != 0.0f || CShaderEffect::s_sunDir.y != 0.0f || CShaderEffect::s_sunDir.z != 0.0f) { + float scale = 1.0 / sqrt(CShaderEffect::s_sunDir.z * CShaderEffect::s_sunDir.z + CShaderEffect::s_sunDir.y * CShaderEffect::s_sunDir.y + CShaderEffect::s_sunDir.x * CShaderEffect::s_sunDir.x); + CShaderEffect::s_sunDir.x = CShaderEffect::s_sunDir.x * scale; + CShaderEffect::s_sunDir.y = CShaderEffect::s_sunDir.y * scale; + CShaderEffect::s_sunDir.z = scale * CShaderEffect::s_sunDir.z; + } + + CShaderEffect::s_sunAmbient = lighting->m_sunAmbient; + + float r = lighting->m_sunDiffuse.x; + float g = lighting->m_sunDiffuse.y; + float b = lighting->m_sunDiffuse.z; + CShaderEffect::s_sunDiffuse.x = r >= 1.0f ? 1.0f : r; + CShaderEffect::s_sunDiffuse.y = g >= 1.0f ? 1.0f : g; + CShaderEffect::s_sunDiffuse.z = b < 1.0f ? b : 1.0f; + + GxShaderConstantsSet(GxSh_Vertex, 10, &CShaderEffect::s_sunDiffuse.x, 1); + GxShaderConstantsSet(GxSh_Vertex, 11, &CShaderEffect::s_sunAmbient.x, 1); + GxShaderConstantsSet(GxSh_Vertex, 12, &CShaderEffect::s_sunDir.x, 1); + + if (CShaderEffect::s_localLightCount) { + CShaderEffect::ComputeLocalLights(&CShaderEffect::s_localLights, CShaderEffect::s_localLightCount, lighting->m_lights, origin); + GxShaderConstantsSet(GxSh_Vertex, 17, reinterpret_cast(&CShaderEffect::s_localLights), sizeof(LocalLights) / sizeof(C4Vector)); + } + + CShadowCache::SetShadowMapGenericInterior(lighting->m_flags & 8); +} + +// VERIFY: fd00dfa0 manual +// OFFSET: 0x00873EE0 +void CShaderEffect::SetAlphaRefDefault() { + float alphaRef = CGxDevice::s_alphaRef[g_theGxDevicePtr->m_appRenderStates[GxRs_BlendingMode].m_value.m_data.i[0]] * (1.0f / 255.0f); + CShaderEffect::SetAlphaRef(alphaRef); +} + +// VERIFY: 47270730 auto +// OFFSET: 0x00873F30 +CShaderEffect::CShaderEffect() { +} diff --git a/Engine/Source/ShaderEffect/ShaderEffect.h b/Engine/Source/ShaderEffect/ShaderEffect.h index 87e9f87..9d33f2e 100644 --- a/Engine/Source/ShaderEffect/ShaderEffect.h +++ b/Engine/Source/ShaderEffect/ShaderEffect.h @@ -3,22 +3,77 @@ #include #include +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/ShaderEffect/Types.h" #include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/C4Vector.h" +class C44Matrix; class CGxShader; -class CShaderEffect; -struct HASHKEY_STRI; +class CImVector; +class CM2Light; +class CM2Lighting; -class CShaderEffect { +struct LocalLights { + /* 0000 */ C4Vector m_color[4]; + /* 0040 */ C4Vector m_position[4]; + /* 0080 */ float m_constantAttenuation[4]; + /* 0090 */ float m_linearAttenuation[4]; + /* 00A0 */ float m_quadraticAttenuation[4]; +}; + +class CShaderEffect : public TSHashObject { public: - /* 0000 */ TSHashObject m_hashObj; - /* 0018 */ uint32_t m_fixedFuncOpCount; - /* 001C */ uint32_t m_colorOps; - /* 0020 */ uint32_t unk_0020; - /* 0024 */ uint32_t m_alphaOps; - /* 0028 */ uint32_t m_vertexShaders; - /* 002C */ CGxShader* m_vertexShaders2 [90]; - /* 0194 */ CGxShader* m_pixelShaders [16]; + static CShaderEffect* s_curEffect; + static int32_t s_enableShaders; + static C4Vector s_fogColorAlphaRef; + static float s_fogMul; + static C4Vector s_fogParams; + static int32_t s_lightEnabled; + static uint32_t s_localLightCount; + static LocalLights s_localLights; + static uint32_t s_shadowValue; + static C3Vector s_sunAmbient; + static C3Vector s_sunDiffuse; + static C3Vector s_sunDir; + static C3Vector s_sunSpec; + static int32_t s_useAlphaRef; + static uint32_t s_usePcfFiltering; + + static void ComputeLocalLights(LocalLights* localLights, uint32_t lightCount, CM2Light** lights, const C3Vector* origin); + static void InitShaderSystem(int32_t enableShaders, int32_t usePcfFiltering); + static uint32_t SelectShadowShader(); + static void SetAlphaRef(float alphaRef); + static void SetAlphaRefDefault(); + static void SetDefaultShaders(uint32_t lightCount); + static void SetDiffuse(const C4Vector& color); + static void SetEmissive(const C4Vector& color); + static void SetFogEnabled(int32_t enabled); + static void SetFogParams(float start, float end, float density, const CImVector& color); + static void SetGlobalLighting(CM2Light* light); + static void SetLightingEnabled(int32_t enabled); + static void SetLocalLighting(CM2Lighting* lighting, int32_t enabled, const C3Vector* origin); + static void SetShaders(uint32_t vertexPermute, uint32_t pixelPermute); + static void SetTexMtx(const C44Matrix& matrix, uint32_t stage); + static void SetTexMtx_Identity(uint32_t stage); + static void SetTexMtx_SphereMap(uint32_t stage); + static void SetTexMtx_TexGen(EGxTexGen texGen, const C44Matrix& matrix, uint32_t stage); + static void SetUsePcfFiltering(uint32_t usePcfFiltering); + static void UpdateProjMatrix(); + static void UpdateWorldViewMatrix(); + + /* 0018 */ uint32_t m_fixedFuncPassCount; + /* 001C */ EGxTexOp m_colorOps[2]; + /* 0024 */ EGxTexOp m_alphaOps[2]; + /* 002C */ CGxShader* m_vertexShaders[90]; + /* 0194 */ CGxShader* m_pixelShaders[16]; + + CShaderEffect(); + ~CShaderEffect(); + void InitEffect(const char* vertexShader, const char* pixelShader); + void InitFixedFuncPass(const EGxTexOp* colorOps, const EGxTexOp* alphaOps, uint32_t count); + void SetCurrent(); }; #endif diff --git a/Engine/Source/ShaderEffect/ShaderEffectManager.cpp b/Engine/Source/ShaderEffect/ShaderEffectManager.cpp index 89287ca..756af2a 100644 --- a/Engine/Source/ShaderEffect/ShaderEffectManager.cpp +++ b/Engine/Source/ShaderEffect/ShaderEffectManager.cpp @@ -1 +1,69 @@ -#include "ShaderEffectManager.h" +#include +#include +#include +#include "Engine/Source/ShaderEffect/ShaderEffectManager.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00D43408 +TSHashTable CShaderEffectManager::m_ShaderList; + +// VERIFY: 554f80ac manual +// OFFSET: 0x008764D0 +void CShaderEffectManager::DestroyAllEffects() { + CShaderEffect* effect = CShaderEffectManager::m_ShaderList.m_fulllist.Head(); + + while (effect) { + effect = CShaderEffectManager::m_ShaderList.DeleteNode(effect); + } +} + +// VERIFY: e6522edb auto +// OFFSET: 0x00876530 +CShaderEffect* CShaderEffectManager::GetEffect(const char* name) { + return CShaderEffectManager::m_ShaderList.Ptr(name); +} + +// VERIFY: 82eb3c99 manual +// OFFSET: 0x00876BE0 +CShaderEffect* CShaderEffectManager::CreateEffect(const char* name) { + CShaderEffectManager::m_ShaderList.Ptr(name); + + return CShaderEffectManager::m_ShaderList.New(name, 0, 0); +} + +// VERIFY: 156a1818 manual +// OFFSET: 0x00876CA0 +void CShaderEffectManager::ParseEffectCallback(const CShaderEffectParser::ParsedEffect& effect, void* param) { + CShaderEffect* shaderEffect = CShaderEffectManager::m_ShaderList.New(effect.m_name, 0, 0); + + shaderEffect->InitEffect(effect.m_shaderPasses[0].m_vertexShader, effect.m_shaderPasses[0].m_pixelShader); + + for (uint32_t i = 0; i < effect.m_fixedFuncPassCount; i++) { + const CShaderEffectParser::ParsedEffect::FixedFuncPass& pass = effect.m_fixedFuncPasses[i]; + shaderEffect->InitFixedFuncPass(pass.m_colorOps, pass.m_alphaOps, pass.m_pass); + } +} + +// VERIFY: c6a3a3f5 manual +// OFFSET: 0x00876D90 +void CShaderEffectManager::AddEffectFile(const char* fileName) { + char path[256]; + sprintf(path, "Shaders\\Effects\\%s", fileName); + + SFileImpl* file; + SFileOpenFile(path, &file); + + if (!file) { + return; + } + + uint32_t size = SFileGetFileSize(file, nullptr); + char* data = static_cast(SMemAlloc(size + 1, __FILE__, __LINE__, 0)); + SFileReadFile(file, data, size, nullptr, nullptr, 0); + SFileCloseFile(file); + + CShaderEffectParser::ParseEffectFile(data, size, CShaderEffectManager::ParseEffectCallback, nullptr); + + delete[] data; +} diff --git a/Engine/Source/ShaderEffect/ShaderEffectManager.h b/Engine/Source/ShaderEffect/ShaderEffectManager.h index 24116ec..3b33b8d 100644 --- a/Engine/Source/ShaderEffect/ShaderEffectManager.h +++ b/Engine/Source/ShaderEffect/ShaderEffectManager.h @@ -1,4 +1,21 @@ #ifndef ENGINE_SOURCE_SHADEREFFECT_SHADEREFFECTMANAGER_H #define ENGINE_SOURCE_SHADEREFFECT_SHADEREFFECTMANAGER_H +#include +#include +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/ShaderEffect/ShaderEffectParser.h" +#include "Engine/Source/Storm/H/stpl.h" + +class CShaderEffectManager { + public: + static TSHashTable m_ShaderList; + + static void AddEffectFile(const char* fileName); + static CShaderEffect* CreateEffect(const char* name); + static void DestroyAllEffects(); + static CShaderEffect* GetEffect(const char* name); + static void ParseEffectCallback(const CShaderEffectParser::ParsedEffect& effect, void* param); +}; + #endif diff --git a/Engine/Source/ShaderEffect/ShaderEffectParser.cpp b/Engine/Source/ShaderEffect/ShaderEffectParser.cpp index c094e50..ffcaff5 100644 --- a/Engine/Source/ShaderEffect/ShaderEffectParser.cpp +++ b/Engine/Source/ShaderEffect/ShaderEffectParser.cpp @@ -1 +1,236 @@ -#include "ShaderEffectParser.h" +#include +#include +#include +#include +#include "Engine/Source/ShaderEffect/ShaderEffectParser.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00A4D678 +const EGxTexOp CShaderEffectParser::s_gxTexOpValues[GxTexOps_Last] = { + GxTexOp_Mod, + GxTexOp_Mod2x, + GxTexOp_Add, + GxTexOp_PassThru, + GxTexOp_Decal, + GxTexOp_Fade, +}; + +// STATIC: 0x00B1D5C8 +const char* CShaderEffectParser::s_gxTexOpNames[GxTexOps_Last] = { + "Mod", + "Mod2x", + "Add", + "PassThru", + "Decal", + "Fade", +}; + +// VERIFY: 64b6e307 auto +// OFFSET: 0x00876F10 +static int32_t FindScope(const char* data, uint32_t start, uint32_t end, uint32_t* open, uint32_t* close) { + int32_t depth = 0; + + for (uint32_t i = start; i < end; i++) { + if (data[i] == '{') { + if (!depth) { + *open = i; + } + + depth++; + } else if (data[i] == '}' && !--depth) { + *close = i; + return 1; + } + } + + return 0; +} + +// VERIFY: 6bc79b07 auto +// OFFSET: 0x00876F70 +static EGxTexOp LookupTexOp(const char* name) { + for (uint32_t i = 0; i < GxTexOps_Last; i++) { + if (!SStrCmpI(name, CShaderEffectParser::s_gxTexOpNames[i], STORM_MAX_STR)) { + return CShaderEffectParser::s_gxTexOpValues[i]; + } + } + + return GxTexOp_Mod; +} + +// VERIFY: 361a6237 manual +// OFFSET: 0x00876E30 +int32_t CShaderEffectParser::ExtractFuncKeyword(const char* text, const char* keyword, char (*args)[64], uint32_t& argCount, uint32_t& pos) { + size_t length = strlen(keyword); + + if (strncmp(text, keyword, length) || text[length] != '(') { + return 0; + } + + if (text[length + 1] == ')') { + argCount = 0; + pos += length + 2; + return 1; + } + + argCount = 1; + memset(args[0], 0, sizeof(args[0])); + + uint32_t i = length + 1; + uint32_t j = 0; + char c = text[i]; + + while (c != ')') { + if (c == ',') { + argCount++; + memset(args[argCount - 1], 0, sizeof(args[0])); + j = 0; + i++; + } else { + args[argCount - 1][j++] = c; + } + + c = text[i + 1]; + i++; + } + + pos += i + 1; + + return 1; +} + +// VERIFY: 43a0acdb manual +// OFFSET: 0x00876FB0 +void CShaderEffectParser::ParseFixedFunc(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect) { + uint32_t open; + uint32_t close; + + if (!FindScope(data, pos, size, &open, &close)) { + return; + } + + pos = open + 1; + + while (pos < close) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "Pass", args, argCount, pos)) { + ParsedEffect::FixedFuncPass* pass = &effect->m_fixedFuncPasses[effect->m_fixedFuncPassCount]; + + char* end; + pass->m_pass = strtol(args[0], &end, 10); + + uint32_t passOpen; + uint32_t passClose; + FindScope(data, pos, size, &passOpen, &passClose); + + pass->m_colorOps[0] = GxTexOp_Mod; + pass->m_colorOps[1] = GxTexOp_Mod; + pass->m_colorOps[2] = GxTexOp_Mod; + pass->m_alphaOps[0] = GxTexOp_Mod; + pass->m_alphaOps[1] = GxTexOp_Mod; + pass->m_alphaOps[2] = GxTexOp_Mod; + + while (pos < passClose) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "ColorOp0", args, argCount, pos)) { + pass->m_colorOps[0] = LookupTexOp(args[0]); + } else if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "ColorOp1", args, argCount, pos)) { + pass->m_colorOps[1] = LookupTexOp(args[0]); + } else if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "AlphaOp0", args, argCount, pos)) { + pass->m_alphaOps[0] = LookupTexOp(args[0]); + } else if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "AlphaOp1", args, argCount, pos)) { + pass->m_alphaOps[1] = LookupTexOp(args[0]); + } + + pos++; + } + + effect->m_fixedFuncPassCount++; + pos = passClose + 1; + } + + pos++; + } + + pos = close + 1; +} + +// VERIFY: 6fc19477 manual +// OFFSET: 0x00877150 +void CShaderEffectParser::ParseShader(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect) { + uint32_t open; + uint32_t close; + + if (!FindScope(data, pos, size, &open, &close)) { + return; + } + + pos = open + 1; + + while (pos < close) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "Pass", args, argCount, pos)) { + ParsedEffect::ShaderPass* pass = &effect->m_shaderPasses[effect->m_shaderPassCount]; + + uint32_t passOpen; + uint32_t passClose; + FindScope(data, pos, size, &passOpen, &passClose); + + while (pos < passClose) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "VertexShader", args, argCount, pos)) { + strcpy(pass->m_vertexShader, args[0]); + } else if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "PixelShader", args, argCount, pos)) { + strcpy(pass->m_pixelShader, args[0]); + } + + pos++; + } + + effect->m_shaderPassCount++; + pos = passClose + 1; + } + + pos++; + } + + pos = close + 1; +} + +// VERIFY: 353721c3 auto +// OFFSET: 0x00877290 +void CShaderEffectParser::ParseEffect(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect) { + SStrCopy(effect->m_name, args[0], sizeof(effect->m_name)); + effect->m_fixedFuncPassCount = 0; + effect->m_shaderPassCount = 0; + + uint32_t open; + uint32_t close; + + if (!FindScope(data, pos, size, &open, &close)) { + return; + } + + pos = open + 1; + + while (pos < close) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "FixedFunc", args, argCount, pos)) { + CShaderEffectParser::ParseFixedFunc(data, size, pos, args, argCount, effect); + } else if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "Shader", args, argCount, pos)) { + CShaderEffectParser::ParseShader(data, size, pos, args, argCount, effect); + } + + pos++; + } +} + +// VERIFY: e3f85c73 auto +// OFFSET: 0x00877360 +void CShaderEffectParser::ParseEffectFile(char* data, uint32_t size, void (*callback)(const ParsedEffect&, void*), void* param) { + char args[16][64]; + ParsedEffect effect; + uint32_t argCount; + + for (uint32_t pos = 0; pos < size; pos++) { + if (CShaderEffectParser::ExtractFuncKeyword(data + pos, "Effect", args, argCount, pos)) { + CShaderEffectParser::ParseEffect(data, size, pos, args, argCount, &effect); + callback(effect, param); + } + } +} diff --git a/Engine/Source/ShaderEffect/ShaderEffectParser.h b/Engine/Source/ShaderEffect/ShaderEffectParser.h index 0d03614..b2e086e 100644 --- a/Engine/Source/ShaderEffect/ShaderEffectParser.h +++ b/Engine/Source/ShaderEffect/ShaderEffectParser.h @@ -1,4 +1,39 @@ #ifndef ENGINE_SOURCE_SHADEREFFECT_SHADEREFFECTPARSER_H #define ENGINE_SOURCE_SHADEREFFECT_SHADEREFFECTPARSER_H +#include +#include +#include "Engine/Source/ShaderEffect/Types.h" + +class CShaderEffectParser { + public: + struct ParsedEffect { + struct FixedFuncPass { + /* 0000 */ uint32_t m_pass; + /* 0004 */ EGxTexOp m_colorOps[3]; + /* 0010 */ EGxTexOp m_alphaOps[3]; + }; + + struct ShaderPass { + /* 0000 */ char m_vertexShader[64]; + /* 0040 */ char m_pixelShader[64]; + }; + + /* 0000 */ char m_name[64]; + /* 0040 */ uint32_t m_fixedFuncPassCount; + /* 0044 */ FixedFuncPass m_fixedFuncPasses[1]; + /* 0060 */ uint32_t m_shaderPassCount; + /* 0064 */ ShaderPass m_shaderPasses[1]; + }; + + static const char* s_gxTexOpNames[GxTexOps_Last]; + static const EGxTexOp s_gxTexOpValues[GxTexOps_Last]; + + static int32_t ExtractFuncKeyword(const char* text, const char* keyword, char (*args)[64], uint32_t& argCount, uint32_t& pos); + static void ParseEffect(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect); + static void ParseEffectFile(char* data, uint32_t size, void (*callback)(const ParsedEffect&, void*), void* param); + static void ParseFixedFunc(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect); + static void ParseShader(char* data, uint32_t size, uint32_t& pos, char (*args)[64], uint32_t argCount, ParsedEffect* effect); +}; + #endif diff --git a/Engine/Source/ShaderEffect/ShadowCache.cpp b/Engine/Source/ShaderEffect/ShadowCache.cpp index 2cb7f75..21aaa9a 100644 --- a/Engine/Source/ShaderEffect/ShadowCache.cpp +++ b/Engine/Source/ShaderEffect/ShadowCache.cpp @@ -1 +1,1069 @@ -#include "ShadowCache.h" +#include +#include +#include +#include "Engine/Source/ShaderEffect/ShadowCache.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/CGxTexFlags.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Tempest/CFrustum.h" +#include "Engine/Source/Tempest/CImVector.h" + +// STATIC: 0x00B1D518 +float CShadowCache::s_debugAspect = 1.0f; + +// STATIC: 0x00B1D51C +int32_t CShadowCache::s_recreateTextures = 1; + +// STATIC: 0x00B1D520 +const float CShadowCache::s_bandRadius[3] = { 40.0f, 160.0f, 640.0f }; + +// STATIC: 0x00B1D52C +const float CShadowCache::s_bandUpdateDistance[3] = { 4.0f, 16.0f, 1024.0f }; + +// STATIC: 0x00B1D538 +const char* CShadowCache::s_shadowModeDescs[NUM_SHADOW_MODES + 1] = { + "[LOWEST]Precomputed terrain and no dynamic shadows.", + "[LOW]Precomputed terrain and dynamic PC/NPC shadows (low-res).", + "[MEDIUM]Precomputed terrain and dynamic PC/NPC shadows (high-res).", + "[MED-HIGH]Full environmental and PC/NPC shadows, low-res, lg-dist.", + "[HIGH]Full environmental and PC/NPC shadows, hi-res, lg-dist.", + "[VERY HIGH]Cascaded shadow maps.", + "[INVALID]Unsupported quality level.", +}; + +// STATIC: 0x00B1D554 +const uint32_t CShadowCache::s_shadowValues[NUM_SHADOW_MODES + 1] = { 0, 1, 1, 2, 2, 3, 3 }; + +// STATIC: 0x00D43148 +CTexture* CShadowCache::s_intDynShadow; + +// STATIC: 0x00D4314C +int32_t CShadowCache::s_needsRebuild; + +// STATIC: 0x00D43150 +uint32_t CShadowCache::s_shadowSize; + +// STATIC: 0x00D43154 +EShadowMode CShadowCache::s_shadowMode; + +// STATIC: 0x00D43158 +ShadowMatrixCallback CShadowCache::s_matrixCallback; + +// STATIC: 0x00D4315C +ShadowFrustumCallback CShadowCache::s_frustumCallback; + +// STATIC: 0x00D43160 +ShadowQueryCallback CShadowCache::s_queryCallback; + +// STATIC: 0x00D43164 +ShadowRenderCallback CShadowCache::s_renderCallback; + +// STATIC: 0x00D43168 +int32_t CShadowCache::s_debugLevel; + +// STATIC: 0x00D4316C +uint8_t CShadowCache::s_frameCounter; + +// STATIC: 0x00D43170 +float CShadowCache::s_extDynShadowCullRect[4]; + +// STATIC: 0x00D43180 +C3Vector CShadowCache::s_sunDir; + +// STATIC: 0x00D4318C +C4Vector CShadowCache::s_sunDirViewSpace; + +// STATIC: 0x00D4319C +C4Vector CShadowCache::s_cutoffPlane; + +// STATIC: 0x00D431AC +C4Vector CShadowCache::s_cutoffPlaneDefault; + +// STATIC: 0x00D431BC +float CShadowCache::s_textureSize[2]; + +// STATIC: 0x00D431C8 +float CShadowCache::s_shadowScale; + +// STATIC: 0x00D431D0 +C4Vector CShadowCache::s_pcfOffsets[8]; + +// STATIC: 0x00D43250 +CShadowBand CShadowCache::s_extDynShadow; + +// STATIC: 0x00D43290 +CShadowBand CShadowCache::s_extShadowBands[3]; + +// STATIC: 0x00D43348 +C4Vector CShadowCache::s_shadowMatrices[12]; + +static void ShadowCacheTextureCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { +} + +// VERIFY: f7bdb006 auto +// OFFSET: 0x00873F60 +const char* CShadowCache::GetShadowModeDesc(uint32_t mode) { + if (mode >= NUM_SHADOW_MODES + 1) { + return CShadowCache::s_shadowModeDescs[NUM_SHADOW_MODES]; + } + + return CShadowCache::s_shadowModeDescs[mode]; +} + +// VERIFY: 2a55048c auto +// OFFSET: 0x00873F80 +EShadowMode CShadowCache::GetShadowMode() { + return CShadowCache::s_recreateTextures == 0 ? CShadowCache::s_shadowMode : SHADOW_LOWEST; +} + +// VERIFY: bbdd945f auto +// OFFSET: 0x00873FA0 +void CShadowCache::SetMatrixCallback(ShadowMatrixCallback callback) { + CShadowCache::s_matrixCallback = callback; +} + +// VERIFY: 2800d06b auto +// OFFSET: 0x00873FB0 +void CShadowCache::SetFrustumCallback(ShadowFrustumCallback callback) { + CShadowCache::s_frustumCallback = callback; +} + +// VERIFY: 583f7889 auto +// OFFSET: 0x00873FC0 +void CShadowCache::SetQueryCallback(ShadowQueryCallback callback) { + CShadowCache::s_queryCallback = callback; +} + +// VERIFY: f4783dcb auto +// OFFSET: 0x00873FD0 +void CShadowCache::SetRenderCallback(ShadowRenderCallback callback) { + CShadowCache::s_renderCallback = callback; +} + +// VERIFY: 4d6705e3 auto +// OFFSET: 0x00873FE0 +void __fastcall CShadowCache::NotifyTexturesRecreate() { + CShadowCache::s_recreateTextures = 1; +} + +// VERIFY: 2be7bf88 auto +// OFFSET: 0x00873FF0 +uint32_t CShadowCache::GetShadowValue() { + return CShadowCache::s_shadowValues[CShadowCache::GetShadowMode()]; +} + +// VERIFY: 01640fc8 auto +// OFFSET: 0x00874010 +void CShadowCache::SetDebugParams(float aspect, int32_t level) { + CShadowCache::s_debugAspect = aspect; + CShadowCache::s_debugLevel = level; +} + +// VERIFY: 6a7262c7 auto +// OFFSET: 0x00874030 +void CShadowCache::SetShadowScale(float scale) { + CShadowCache::s_shadowScale = scale; +} + +// VERIFY: 7406ae09 manual +// OFFSET: 0x00874040 +CShadowBand::CShadowBand() { + this->m_textures[0] = nullptr; + this->m_textures[1] = nullptr; + this->m_radius = 0.0f; + this->m_updateDistance = 0.0f; + this->m_lastPos = C3Vector(0.0f, 0.0f, 0.0f); + this->m_pos = C3Vector(0.0f, 0.0f, 0.0f); + this->m_dir = C3Vector(1.0f, 0.0f, 0.0f); + this->m_updateCount = 0; + this->m_current = 0; +} + +// VERIFY: e21b5039 auto +// OFFSET: 0x008740D0 +int32_t CShadowCache::ValidateShadowMode(EShadowMode mode) { + int32_t vertexTarget = g_theGxDevicePtr->Caps().m_shaderTargets[GxSh_Vertex]; + int32_t pixelTarget = g_theGxDevicePtr->Caps().m_shaderTargets[GxSh_Pixel]; + int32_t depthFormat = g_theGxDevicePtr->Caps().m_rttFormat[GxTex_D24X8] || g_theGxDevicePtr->Caps().m_rttFormat[GxTex_R32F]; + + switch (mode) { + case SHADOW_LOWEST: + return 1; + + case SHADOW_LOW: + case SHADOW_MEDIUM: + switch (g_theGxDevicePtr->DeviceApi()) { + case GxApi_D3d9: + case GxApi_D3d9Ex: + if (vertexTarget != 1 && vertexTarget != 2 && vertexTarget != 3) { + return 0; + } + + if (pixelTarget != 3 && pixelTarget != 4) { + return 0; + } + + return depthFormat ? 1 : 0; + + case GxApi_D3d10: + case GxApi_D3d11: + return depthFormat; + + case GxApi_GLL: + if ((vertexTarget == 6 || vertexTarget == 10) && (pixelTarget == 12 || pixelTarget == 11)) { + return depthFormat; + } + + return 0; + + default: + return 0; + } + + case SHADOW_MEDIUM_HIGH: + case SHADOW_HIGH: + case SHADOW_VERY_HIGH: + switch (g_theGxDevicePtr->DeviceApi()) { + case GxApi_D3d9: + case GxApi_D3d9Ex: + if (vertexTarget != 3 || pixelTarget != 4) { + return 0; + } + + return depthFormat ? 1 : 0; + + case GxApi_D3d10: + case GxApi_D3d11: + return depthFormat; + + default: + return 0; + } + + default: + return 0; + } +} + +// VERIFY: c1d6590c auto +// OFFSET: 0x00874210 +int32_t CShadowCache::SetShadowMode(EShadowMode mode) { + if (!CShadowCache::ValidateShadowMode(mode)) { + return 0; + } + + CShadowCache::s_shadowMode = mode; + CShadowCache::s_recreateTextures = 1; + + return 1; +} + +// VERIFY: b392dedb manual +// OFFSET: 0x00874240 +void CShadowCache::Destroy() { + if (CShadowCache::s_extDynShadow.m_textures[0]) { + HandleClose(reinterpret_cast(CShadowCache::s_extDynShadow.m_textures[0])); + CShadowCache::s_extDynShadow.m_textures[0] = nullptr; + } + + if (CShadowCache::s_extDynShadow.m_textures[1]) { + HandleClose(reinterpret_cast(CShadowCache::s_extDynShadow.m_textures[1])); + CShadowCache::s_extDynShadow.m_textures[1] = nullptr; + } + + if (CShadowCache::s_intDynShadow) { + HandleClose(reinterpret_cast(CShadowCache::s_intDynShadow)); + CShadowCache::s_intDynShadow = nullptr; + } + + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + + if (band.m_textures[0]) { + HandleClose(reinterpret_cast(band.m_textures[0])); + band.m_textures[0] = nullptr; + } + + if (band.m_textures[1]) { + HandleClose(reinterpret_cast(band.m_textures[1])); + band.m_textures[1] = nullptr; + } + } + + g_theGxDevicePtr->RemoveTextureRecreationCallback(CShadowCache::NotifyTexturesRecreate); +} + +// VERIFY: ac294af0 manual +// OFFSET: 0x008742E0 +void CShadowCache::InitPcfOffsets() { + float size = CShadowCache::s_shadowSize; + CShadowCache::s_textureSize[0] = size; + CShadowCache::s_textureSize[1] = size; + + float texel = 1.0 / size; + + CShadowCache::s_pcfOffsets[0].x = 0.8f * texel; + CShadowCache::s_pcfOffsets[0].y = -1.0f * texel; + CShadowCache::s_pcfOffsets[0].z = 0.0f; + CShadowCache::s_pcfOffsets[0].w = 0.0f; + CShadowCache::s_pcfOffsets[1].x = texel * -0.2f; + CShadowCache::s_pcfOffsets[1].y = texel * -0.8f; + CShadowCache::s_pcfOffsets[1].z = 0.0f; + CShadowCache::s_pcfOffsets[1].w = 0.0f; + CShadowCache::s_pcfOffsets[2].x = texel * 0.2f; + CShadowCache::s_pcfOffsets[2].y = texel * -0.6f; + CShadowCache::s_pcfOffsets[2].z = 0.0f; + CShadowCache::s_pcfOffsets[2].w = 0.0f; + CShadowCache::s_pcfOffsets[3].x = texel; + CShadowCache::s_pcfOffsets[3].y = -0.4f * texel; + CShadowCache::s_pcfOffsets[3].z = 0.0f; + CShadowCache::s_pcfOffsets[3].w = 0.0f; + CShadowCache::s_pcfOffsets[4].x = texel * -0.6f; + CShadowCache::s_pcfOffsets[4].y = texel * -0.2f; + CShadowCache::s_pcfOffsets[4].z = 0.0f; + CShadowCache::s_pcfOffsets[4].w = 0.0f; + CShadowCache::s_pcfOffsets[5].x = texel * 0.6f; + CShadowCache::s_pcfOffsets[5].y = texel * 0.2f; + CShadowCache::s_pcfOffsets[5].z = 0.0f; + CShadowCache::s_pcfOffsets[5].w = 0.0f; + CShadowCache::s_pcfOffsets[6].x = -1.0f * texel; + CShadowCache::s_pcfOffsets[6].y = -0.4f * texel; + CShadowCache::s_pcfOffsets[6].z = 0.0f; + CShadowCache::s_pcfOffsets[6].w = 0.0f; + CShadowCache::s_pcfOffsets[7].x = -0.4f * texel; + CShadowCache::s_pcfOffsets[7].y = texel * -0.6f; + CShadowCache::s_pcfOffsets[7].z = 0.0f; + CShadowCache::s_pcfOffsets[7].w = 0.0f; +} + +// VERIFY: 0ffef1a7 auto +// OFFSET: 0x008744E0 +void CShadowCache::SetShadowMapGenericGlobal() { + if (CShadowCache::s_recreateTextures) { + return; + } + + EShadowMode mode = CShadowCache::s_shadowMode; + + if (mode <= SHADOW_LOWEST) { + return; + } + + GxShaderConstantsSet(GxSh_Vertex, 224, &CShadowCache::s_shadowMatrices[0].x, 12); + GxShaderConstantsSet(GxSh_Pixel, 4, &CShadowCache::s_sunDirViewSpace.x, 1); + GxShaderConstantsSet(GxSh_Pixel, 5, &CShadowCache::s_pcfOffsets[0].x, 8); + + if (mode > SHADOW_MEDIUM) { + CShadowBand* bands = CShadowCache::s_extShadowBands; + g_theGxDevicePtr->RsSet(GxRs_Texture5, TextureGetGxTex(bands[0].m_textures[bands[0].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture6, TextureGetGxTex(bands[1].m_textures[bands[1].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture7, TextureGetGxTex(bands[2].m_textures[bands[2].m_current], 1, nullptr)); + } +} + +// VERIFY: 23abd6bb auto +// OFFSET: 0x008745D0 +void CShadowCache::SetShadowMapGenericInterior(int32_t interior) { + if (CShadowCache::s_recreateTextures) { + return; + } + + EShadowMode mode = CShadowCache::s_shadowMode; + + if (mode <= SHADOW_LOWEST) { + return; + } + + const C4Vector* cutoffPlane = &CShadowCache::s_cutoffPlane; + CGxTex* texture = TextureGetGxTex(CShadowCache::s_intDynShadow, 1, nullptr); + + if (!interior) { + texture = TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[CShaderEffect::s_usePcfFiltering], 1, nullptr); + + if (mode > SHADOW_MEDIUM) { + cutoffPlane = &CShadowCache::s_cutoffPlaneDefault; + } + } + + GxShaderConstantsSet(GxSh_Pixel, 3, &cutoffPlane->x, 1); + g_theGxDevicePtr->RsSet(GxRs_Texture4, texture); +} + +// VERIFY: f70f0371 auto +// OFFSET: 0x00874660 +void CShadowCache::SetShadowMapTerrain() { + if (CShadowCache::s_recreateTextures) { + return; + } + + EShadowMode mode = CShadowCache::s_shadowMode; + + if (mode <= SHADOW_LOWEST) { + return; + } + + GxShaderConstantsSet(GxSh_Vertex, 37, &CShadowCache::s_shadowMatrices[0].x, 12); + GxShaderConstantsSet(GxSh_Pixel, 3, &CShadowCache::s_pcfOffsets[0].x, 8); + g_theGxDevicePtr->RsSet(GxRs_Texture5, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[CShaderEffect::s_usePcfFiltering], 1, nullptr)); + + if (mode > SHADOW_MEDIUM) { + CShadowBand* bands = CShadowCache::s_extShadowBands; + g_theGxDevicePtr->RsSet(GxRs_Texture6, TextureGetGxTex(bands[0].m_textures[bands[0].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture7, TextureGetGxTex(bands[1].m_textures[bands[1].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture8, TextureGetGxTex(bands[2].m_textures[bands[2].m_current], 1, nullptr)); + } +} + +// VERIFY: 0b31ef9c auto +// OFFSET: 0x00874760 +void CShadowCache::SetShadowMapDetailDoodad() { + if (CShadowCache::s_recreateTextures) { + return; + } + + EShadowMode mode = CShadowCache::s_shadowMode; + + if (mode <= SHADOW_LOWEST) { + return; + } + + GxShaderConstantsSet(GxSh_Vertex, 23, &CShadowCache::s_shadowMatrices[0].x, 12); + GxShaderConstantsSet(GxSh_Pixel, 3, &CShadowCache::s_cutoffPlaneDefault.x, 1); + GxShaderConstantsSet(GxSh_Pixel, 4, &CShadowCache::s_sunDirViewSpace.x, 1); + GxShaderConstantsSet(GxSh_Pixel, 5, &CShadowCache::s_pcfOffsets[0].x, 8); + g_theGxDevicePtr->RsSet(GxRs_Texture4, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[CShaderEffect::s_usePcfFiltering], 1, nullptr)); + + if (mode > SHADOW_MEDIUM) { + CShadowBand* bands = CShadowCache::s_extShadowBands; + g_theGxDevicePtr->RsSet(GxRs_Texture5, TextureGetGxTex(bands[0].m_textures[bands[0].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture6, TextureGetGxTex(bands[1].m_textures[bands[1].m_current], 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture7, TextureGetGxTex(bands[2].m_textures[bands[2].m_current], 1, nullptr)); + } +} + +// VERIFY: 2584124d manual +// OFFSET: 0x00874890 +void CShadowCache::GatherBands(CShadowCacheQueryData& data, const C3Vector& pos, CWFrustum& frustum, int32_t useCullFrustum) { + uint32_t frame = 0; + + if (CShadowCache::GetShadowMode() < SHADOW_VERY_HIGH) { + C44Matrix identity; + + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + + data.m_textureIndex[i] = band.m_current == 0; + data.m_active[i] = 0; + + if (!band.m_updateCount) { + float dx = band.m_lastPos.x - pos.x; + float dy = band.m_lastPos.y - pos.y; + float dz = band.m_lastPos.z - pos.z; + + if (dx * dx + dy * dy + dz * dz <= band.m_updateDistance) { + continue; + } + } + + data.m_radius[i] = band.m_radius; + + if (!band.m_updateCount) { + band.m_pos = pos; + } + + data.m_pos[i] = band.m_pos; + + float step; + uint32_t row; + + if (i >= 2) { + step = data.m_radius[i] * 0.4f; + data.m_bounds[i][0] = (band.m_updateCount % 5) * step - data.m_radius[i]; + data.m_bounds[i][1] = data.m_bounds[i][0] + step; + row = band.m_updateCount / 5; + } else { + step = data.m_radius[i] * 0.6666667f; + data.m_bounds[i][0] = (band.m_updateCount % 3) * step - data.m_radius[i]; + data.m_bounds[i][1] = data.m_bounds[i][0] + step; + row = band.m_updateCount / 3; + } + + data.m_bounds[i][2] = row * step - data.m_radius[i]; + data.m_bounds[i][3] = step + data.m_bounds[i][2]; + + data.m_bounds[i][1] = data.m_radius[i] * 2.0f * (ceil(CShadowCache::s_shadowSize / (data.m_radius[i] + data.m_radius[i]) * data.m_bounds[i][1]) / CShadowCache::s_shadowSize); + data.m_bounds[i][3] = ceil(CShadowCache::s_shadowSize / (2.0f * data.m_radius[i]) * data.m_bounds[i][3]) / CShadowCache::s_shadowSize * (data.m_radius[i] + data.m_radius[i]); + + float scale = 0.5f / data.m_radius[i]; + data.m_texBounds[i][0] = data.m_bounds[i][0] * scale + 0.5f; + data.m_texBounds[i][1] = data.m_bounds[i][1] * scale + 0.5f; + data.m_texBounds[i][2] = 0.5f - data.m_bounds[i][3] * scale; + data.m_texBounds[i][3] = 0.5f - scale * data.m_bounds[i][2]; + + CShadowCache::s_frustumCallback(data.m_frustums[i], data.m_matrices[i], band.m_dir, data, i); + data.m_frustums[i].Extents(data.m_extents[i]); + + if (i && data.m_active[i - 1]) { + data.m_cullFrustums[i] = frustum; + data.m_useCullFrustum[i] = 1; + } else { + data.m_useCullFrustum[i] = 0; + } + + data.m_queryFlags[i] = 8; + + float bounds[4]; + bounds[0] = data.m_bounds[i][0]; + bounds[1] = data.m_bounds[i][1]; + bounds[2] = data.m_bounds[i][2]; + bounds[3] = data.m_bounds[i][3]; + + data.m_bounds[i][0] = -data.m_radius[i]; + data.m_bounds[i][1] = data.m_radius[i]; + data.m_bounds[i][2] = -data.m_radius[i]; + data.m_bounds[i][3] = data.m_radius[i]; + + CShadowCache::s_frustumCallback(frustum, identity, band.m_dir, data, i); + frustum.InvertPlanes(); + + data.m_bounds[i][0] = bounds[0]; + data.m_bounds[i][1] = bounds[1]; + data.m_bounds[i][2] = bounds[2]; + data.m_bounds[i][3] = bounds[3]; + + band.m_updateCount++; + + if ((i < 2 && band.m_updateCount >= 9) || (i == 2 && band.m_updateCount >= 25)) { + band.m_lastPos = band.m_pos; + band.m_updateCount = 0; + band.m_current = band.m_current == 0; + } + + data.m_active[i] = 1; + data.m_lastBand = i; + } + } else { + frame = CShadowCache::s_frameCounter; + + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + + data.m_textureIndex[i] = 0; + data.m_active[i] = 0; + data.m_radius[i] = band.m_radius; + band.m_pos = pos; + + float cell; + + if (i == 0) { + cell = 2.0f; + } else if (i == 1) { + cell = 4.0f; + } else { + cell = 16.0f; + } + + float invCell = 1.0f / cell; + band.m_pos.x = floor(invCell * band.m_pos.x) * cell; + band.m_pos.y = floor(band.m_pos.y * invCell) * cell; + band.m_pos.z = floor(band.m_pos.z * invCell) * cell; + + data.m_pos[i] = band.m_pos; + + data.m_bounds[i][0] = -data.m_radius[i]; + data.m_bounds[i][1] = data.m_radius[i]; + data.m_bounds[i][2] = -data.m_radius[i]; + data.m_bounds[i][3] = data.m_radius[i]; + + data.m_texBounds[i][0] = 0.0f; + data.m_texBounds[i][1] = 1.0f; + data.m_texBounds[i][2] = 0.0f; + data.m_texBounds[i][3] = 1.0f; + + CShadowCache::s_frustumCallback(data.m_frustums[i], data.m_matrices[i], band.m_dir, data, i); + data.m_frustums[i].Extents(data.m_extents[i]); + + data.m_useCullFrustum[i] = useCullFrustum; + + if (useCullFrustum) { + data.m_cullFrustums[i] = frustum; + } + + data.m_queryFlags[i] = 13; + frustum = data.m_frustums[i]; + frustum.InvertPlanes(); + + band.m_lastPos = band.m_pos; + data.m_active[i] = 1; + data.m_lastBand = i; + useCullFrustum = 1; + } + } + + CShadowCache::s_queryCallback(data, frame); +} + +// VERIFY: bfe3e321 manual +// OFFSET: 0x00874FB0 +void CShadowCache::RenderBands(CShadowCacheQueryData& data) { + EShadowMode mode = CShadowCache::GetShadowMode(); + + for (int32_t i = 0; i <= data.m_lastBand; i++) { + if (!data.m_active[i]) { + continue; + } + + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + CGxTex* colorTarget = nullptr; + CGxTex* target; + + if (CShaderEffect::s_usePcfFiltering) { + if (mode < SHADOW_VERY_HIGH) { + colorTarget = TextureGetGxTex(band.m_textures[data.m_textureIndex[i]], 1, nullptr); + } else { + colorTarget = TextureGetGxTex(band.m_textures[0], 1, nullptr); + } + + target = TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr); + } else if (mode >= SHADOW_VERY_HIGH) { + target = TextureGetGxTex(band.m_textures[0], 1, nullptr); + } else { + target = TextureGetGxTex(band.m_textures[data.m_textureIndex[i]], 1, nullptr); + } + + CShadowCache::s_renderCallback(data, i, target, colorTarget, band.m_dir); + } +} + +// VERIFY: 5c2fb76e manual +// OFFSET: 0x008750B0 +void CShadowCache::PostUpdate() { + if (!CShadowCache::s_matrixCallback || !CShadowCache::s_frustumCallback || !CShadowCache::s_queryCallback || !CShadowCache::s_renderCallback) { + return; + } + + C44Matrix scale; + C44Matrix translate; + + scale.Scale(C3Vector(1.0f, -1.0f, CShadowCache::s_shadowScale)); + + C3Vector offset; + offset.x = 0.5f / CShadowCache::s_textureSize[0]; + offset.y = offset.x; + offset.z = 0.0f; + translate.Translate(offset); + + C44Matrix matrix; + CShadowCache::s_matrixCallback(CShadowCache::s_extDynShadow.m_lastPos, CShadowCache::s_extDynShadow.m_radius, matrix, CShadowCache::s_extDynShadow.m_dir, -1); + matrix = matrix * scale * translate; + + CShadowCache::s_shadowMatrices[0].x = matrix.a0; + CShadowCache::s_shadowMatrices[0].y = matrix.b0; + CShadowCache::s_shadowMatrices[0].z = matrix.c0; + CShadowCache::s_shadowMatrices[0].w = matrix.d0; + CShadowCache::s_shadowMatrices[1].x = matrix.a1; + CShadowCache::s_shadowMatrices[1].y = matrix.b1; + CShadowCache::s_shadowMatrices[1].z = matrix.c1; + CShadowCache::s_shadowMatrices[1].w = matrix.d1; + CShadowCache::s_shadowMatrices[2].x = matrix.a2; + CShadowCache::s_shadowMatrices[2].y = matrix.b2; + CShadowCache::s_shadowMatrices[2].z = matrix.c2; + CShadowCache::s_shadowMatrices[2].w = matrix.d2; + + for (int32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + + CShadowCache::s_matrixCallback(band.m_lastPos, band.m_radius, matrix, band.m_dir, i); + matrix = matrix * scale * translate; + + CShadowCache::s_shadowMatrices[3 + 3 * i].x = matrix.a0; + CShadowCache::s_shadowMatrices[3 + 3 * i].y = matrix.b0; + CShadowCache::s_shadowMatrices[3 + 3 * i].z = matrix.c0; + CShadowCache::s_shadowMatrices[3 + 3 * i].w = matrix.d0; + CShadowCache::s_shadowMatrices[4 + 3 * i].x = matrix.a1; + CShadowCache::s_shadowMatrices[4 + 3 * i].y = matrix.b1; + CShadowCache::s_shadowMatrices[4 + 3 * i].z = matrix.c1; + CShadowCache::s_shadowMatrices[4 + 3 * i].w = matrix.d1; + CShadowCache::s_shadowMatrices[5 + 3 * i].x = matrix.a2; + CShadowCache::s_shadowMatrices[5 + 3 * i].y = matrix.b2; + CShadowCache::s_shadowMatrices[5 + 3 * i].z = matrix.c2; + CShadowCache::s_shadowMatrices[5 + 3 * i].w = matrix.d2; + } +} + +// VERIFY: 6227ff43 manual +// OFFSET: 0x008753F0 +CShadowCacheQueryData::CShadowCacheQueryData() { + for (uint32_t i = 0; i < 3; i++) { + this->m_cullRects[i][0] = 0.0f; + this->m_cullRects[i][1] = 0.0f; + this->m_cullRects[i][2] = 0.0f; + this->m_cullRects[i][3] = 0.0f; + } + + for (uint32_t i = 0; i < 3; i++) { + this->m_queryFlags[i] = 0; + this->m_active[i] = 0; + this->m_useCullFrustum[i] = 0; + this->m_textureIndex[i] = 0; + this->m_radius[i] = 0.0f; + this->m_bounds[i][0] = 0.0f; + this->m_bounds[i][1] = 0.0f; + this->m_bounds[i][2] = 0.0f; + this->m_bounds[i][3] = 0.0f; + this->m_texBounds[i][0] = 0.0f; + this->m_texBounds[i][1] = 0.0f; + this->m_texBounds[i][2] = 0.0f; + this->m_texBounds[i][3] = 0.0f; + } + + this->m_useFrustum = 0; + this->m_lastBand = -1; +} + +// VERIFY: e0449636 manual +// OFFSET: 0x00875760 +void CShadowCache::ClearBandTextures() { + if (CShadowCache::s_shadowMode <= SHADOW_MEDIUM) { + CShadowCache::s_needsRebuild = 0; + return; + } + + CGxTex* colorTarget = nullptr; + CGxTex* depthTarget = nullptr; + g_theGxDevicePtr->DeviceGetRenderTarget(GxBuffers_Color, colorTarget); + + float minX; + float maxX; + float minY; + float maxY; + float minZ; + float maxZ; + GxXformViewport(minX, maxX, minY, maxY, minZ, maxZ); + + CImVector clearColor; + clearColor.b = 0xFF; + clearColor.g = 0xFF; + clearColor.r = 0xFF; + clearColor.a = 0xFF; + + if (CShaderEffect::s_usePcfFiltering) { + GxRsSet(GxRs_ColorWrite, 0); + g_theGxDevicePtr->DeviceGetRenderTarget(GxBuffers_Depth, depthTarget); + } + + GxXformSetViewport(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f); + + if (CShaderEffect::s_usePcfFiltering) { + if (CShadowCache::s_extDynShadow.m_textures[0]) { + GxRenderTargetSet(GxBuffers_Color, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr), 0); + } + + if (CShadowCache::s_extDynShadow.m_textures[1]) { + GxRenderTargetSet(GxBuffers_Depth, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[1], 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + + if (CShadowCache::s_intDynShadow) { + GxRenderTargetSet(GxBuffers_Depth, TextureGetGxTex(CShadowCache::s_intDynShadow, 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + } else { + GxRenderTargetSet(GxBuffers_Depth, nullptr, 0); + + if (CShadowCache::s_extDynShadow.m_textures[0]) { + GxRenderTargetSet(GxBuffers_Color, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + + if (CShadowCache::s_intDynShadow) { + GxRenderTargetSet(GxBuffers_Color, TextureGetGxTex(CShadowCache::s_intDynShadow, 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + } + + EGxBuffer buffer; + + if (CShaderEffect::s_usePcfFiltering) { + buffer = GxBuffers_Depth; + + if (CShadowCache::s_extDynShadow.m_textures[0]) { + GxRenderTargetSet(GxBuffers_Color, TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr), 0); + } + } else { + buffer = GxBuffers_Color; + GxRenderTargetSet(GxBuffers_Depth, nullptr, 0); + } + + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + + if (band.m_textures[0]) { + GxRenderTargetSet(buffer, TextureGetGxTex(band.m_textures[0], 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + + if (band.m_textures[1]) { + GxRenderTargetSet(buffer, TextureGetGxTex(band.m_textures[1], 1, nullptr), 0); + GxSceneClear(3, clearColor); + } + } + + GxXformSetViewport(minX, maxX, minY, maxY, minZ, maxZ); + GxRenderTargetSet(GxBuffers_Color, colorTarget, 0); + + if (CShaderEffect::s_usePcfFiltering) { + GxRsSet(GxRs_ColorWrite, 15); + GxRenderTargetSet(GxBuffers_Depth, depthTarget, 0); + } + + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + band.m_lastPos = C3Vector(0.0f, 0.0f, 0.0f); + band.m_pos = C3Vector(0.0f, 0.0f, 0.0f); + band.m_updateCount = 0; + band.m_current = 0; + } + + CShadowCache::s_needsRebuild = 0; +} + +// VERIFY: 53ac3144 manual +// OFFSET: 0x00875C10 +void CShadowCache::PreUpdate(const C3Vector& sunDir) { + CShadowCache::s_sunDir = sunDir; + + C44Matrix view; + GxXformView(view); + + C3Vector& dir = CShadowCache::s_sunDir; + float x = view.c0 * dir.z + view.b0 * dir.y + view.a0 * dir.x; + float y = view.c1 * dir.z + view.b1 * dir.y + dir.x * view.a1; + float z = dir.y * view.b2 + dir.z * view.c2 + dir.x * view.a2; + float scale = 1.0 / sqrt(y * y + z * z + x * x); + + CShadowCache::s_sunDirViewSpace.x = x * scale; + CShadowCache::s_sunDirViewSpace.y = y * scale; + CShadowCache::s_sunDirViewSpace.z = scale * z; + CShadowCache::s_sunDirViewSpace.w = 0.0f; +} + +// VERIFY: 98b606e7 manual +// OFFSET: 0x00875D30 +void CShadowCache::Init() { + CShadowCache::s_shadowSize = 1024; + + if (CShadowCache::s_shadowMode >= SHADOW_HIGH || CShadowCache::s_shadowMode == SHADOW_MEDIUM) { + CShadowCache::s_shadowSize = 2048; + } + + CShadowCache::InitPcfOffsets(); + + CShadowCache::s_extDynShadow.m_textures[0] = nullptr; + CShadowCache::s_extDynShadow.m_textures[1] = nullptr; + CShadowCache::s_intDynShadow = nullptr; + + for (uint32_t i = 0; i < 3; i++) { + CShadowCache::s_extShadowBands[i].m_textures[0] = nullptr; + CShadowCache::s_extShadowBands[i].m_textures[1] = nullptr; + } + + CGxTexFlags flags(GxTex_Linear, 0, 0, 0, 0, 0, 1, 0, 0, 0); + EGxTexFormat format; + + if (CShaderEffect::s_usePcfFiltering) { + format = GxTex_D24X8; + flags.m_filter = GxTex_Linear; + } else { + format = GxTex_R32F; + flags.m_filter = GxTex_Nearest; + } + + flags.m_wrapU = 0; + flags.m_wrapV = 0; + flags.m_renderTarget = 1; + + uint32_t size = CShadowCache::s_shadowSize; + + if (CShadowCache::s_shadowMode) { + if (CShaderEffect::s_usePcfFiltering) { + CShadowCache::s_extDynShadow.m_textures[0] = TextureCreate(GxTex_2d, size, size, 0, GxTex_Argb8888, GxTex_Argb8888, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + CShadowCache::s_extDynShadow.m_textures[1] = TextureCreate(GxTex_2d, size, size, 0, format, format, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + } else { + CShadowCache::s_extDynShadow.m_textures[0] = TextureCreate(GxTex_2d, size, size, 0, format, format, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + CShadowCache::s_extDynShadow.m_textures[1] = nullptr; + } + + CShadowCache::s_intDynShadow = TextureCreate(GxTex_2d, size, size, 0, format, format, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + CShadowCache::s_extDynShadow.m_radius = 20.0f; + + if (CShadowCache::s_shadowMode > SHADOW_MEDIUM) { + for (uint32_t i = 0; i < 3; i++) { + CShadowBand& band = CShadowCache::s_extShadowBands[i]; + band.m_textures[0] = TextureCreate(GxTex_2d, size, size, 0, format, format, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + + if (CShadowCache::s_shadowMode < SHADOW_VERY_HIGH) { + band.m_textures[1] = TextureCreate(GxTex_2d, size, size, 0, format, format, flags, nullptr, ShadowCacheTextureCallback, "ShadowCache", 0); + } + + band.m_radius = CShadowCache::s_bandRadius[i]; + band.m_updateDistance = CShadowCache::s_bandUpdateDistance[i]; + } + } + } + + CShadowCache::ClearBandTextures(); + g_theGxDevicePtr->AddTextureRecreationCallback(CShadowCache::NotifyTexturesRecreate); + CShadowCache::s_needsRebuild = 0; + + CShadowCache::s_cutoffPlaneDefault.x = 0.0f; + CShadowCache::s_cutoffPlaneDefault.y = 0.0f; + CShadowCache::s_cutoffPlaneDefault.z = 0.0f; + CShadowCache::s_cutoffPlaneDefault.w = 0.0f; + CShadowCache::s_cutoffPlane.x = 0.0f; + CShadowCache::s_cutoffPlane.y = 0.0f; + CShadowCache::s_cutoffPlane.z = 0.0f; + CShadowCache::s_cutoffPlane.w = 0.0f; +} + +// VERIFY: 6f6e0a5e auto +// OFFSET: 0x00875F80 +int32_t CShadowCache::Update(const C3Vector& pos, int32_t useCullFrustum) { + int32_t recreate = CShadowCache::s_recreateTextures; + uint32_t frame = 0; + + if (recreate) { + CShadowCache::Destroy(); + CShadowCache::Init(); + recreate = 0; + CShadowCache::s_recreateTextures = 0; + } + + if (!CShadowCache::s_matrixCallback || !CShadowCache::s_frustumCallback || !CShadowCache::s_queryCallback || !CShadowCache::s_renderCallback) { + return 0; + } + + if (recreate) { + return 1; + } + + EShadowMode mode = CShadowCache::s_shadowMode; + + if (mode < SHADOW_LOW) { + return 1; + } + + CShadowCacheQueryData data; + CShadowCache::s_frameCounter++; + + float radius = CShadowCache::s_extDynShadow.m_radius; + data.m_radius[0] = radius; + + float size = CShadowCache::s_shadowSize; + CShadowCache::s_extDynShadow.m_lastPos = pos; + data.m_pos[0] = pos; + + float texel = 1.0 / size; + CShadowCache::s_extDynShadow.m_lastPos.x = floor(size * CShadowCache::s_extDynShadow.m_lastPos.x + 0.5) * texel; + CShadowCache::s_extDynShadow.m_lastPos.y = floor(CShadowCache::s_extDynShadow.m_lastPos.y * size + 0.5) * texel; + + CGxTex* depthTarget = nullptr; + + if (CShaderEffect::s_usePcfFiltering) { + GxRsSet(GxRs_ColorWrite, 0); + g_theGxDevicePtr->DeviceGetRenderTarget(GxBuffers_Depth, depthTarget); + } + + data.m_bounds[0][0] = -radius; + data.m_bounds[0][2] = -radius; + data.m_bounds[0][1] = radius; + data.m_bounds[0][3] = radius; + data.m_texBounds[0][0] = 0.0f; + data.m_texBounds[0][1] = 1.0f; + data.m_texBounds[0][3] = 1.0f; + data.m_texBounds[0][2] = 0.0f; + + CShadowCache::s_frustumCallback(data.m_frustums[0], data.m_matrices[0], CShadowCache::s_extDynShadow.m_dir, data, 0); + data.m_frustums[0].Extents(data.m_extents[0]); + + CShadowCache::s_extDynShadow.m_dir = C3Vector(1.0f, 0.0f, 0.0f); + + CShadowCache::s_extDynShadowCullRect[0] = data.m_cullRects[0][0]; + CShadowCache::s_extDynShadowCullRect[1] = data.m_cullRects[0][1]; + CShadowCache::s_extDynShadowCullRect[2] = data.m_cullRects[0][2]; + CShadowCache::s_extDynShadowCullRect[3] = data.m_cullRects[0][3]; + + data.m_lastBand = 0; + + if (mode >= SHADOW_VERY_HIGH) { + frame = CShadowCache::s_frameCounter; + } + + data.m_queryFlags[0] = 3; + data.m_useFrustum = 0; + CShadowCache::s_queryCallback(data, frame); + + CGxTex* target = TextureGetGxTex(CShadowCache::s_intDynShadow, 1, nullptr); + CGxTex* colorTarget = nullptr; + + if (CShaderEffect::s_usePcfFiltering) { + target = TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr); + colorTarget = TextureGetGxTex(CShadowCache::s_intDynShadow, 1, nullptr); + } + + CShadowCache::s_renderCallback(data, 0, target, colorTarget, CShadowCache::s_extDynShadow.m_dir); + + if (useCullFrustum) { + data.m_queryFlags[0] = 1; + + if (mode > SHADOW_HIGH) { + data.m_queryFlags[0] = 13; + } else if (mode > SHADOW_MEDIUM) { + data.m_queryFlags[0] = 5; + } + + CShadowCache::s_queryCallback(data, frame); + + target = TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[0], 1, nullptr); + colorTarget = nullptr; + + if (CShaderEffect::s_usePcfFiltering) { + colorTarget = TextureGetGxTex(CShadowCache::s_extDynShadow.m_textures[1], 1, nullptr); + } + + CShadowCache::s_renderCallback(data, 0, target, colorTarget, CShadowCache::s_extDynShadow.m_dir); + } + + if (mode > SHADOW_MEDIUM) { + if (CShadowCache::s_needsRebuild) { + CShadowCache::ClearBandTextures(); + } + + if (useCullFrustum || mode < SHADOW_VERY_HIGH) { + int32_t useBandCull = 0; + CWFrustum frustum; + + if (mode < SHADOW_VERY_HIGH) { + data.m_useFrustum = 0; + } else { + useBandCull = 1; + frustum = data.m_frustums[0]; + frustum.InvertPlanes(); + data.m_useFrustum = 1; + } + + CShadowCache::GatherBands(data, pos, frustum, useBandCull); + CShadowCache::RenderBands(data); + } + } + + if (CShaderEffect::s_usePcfFiltering) { + GxRsSet(GxRs_ColorWrite, 15); + GxRenderTargetSet(GxBuffers_Depth, depthTarget, 0); + } + + return 1; +} diff --git a/Engine/Source/ShaderEffect/ShadowCache.h b/Engine/Source/ShaderEffect/ShadowCache.h index 21a50d9..f26ae89 100644 --- a/Engine/Source/ShaderEffect/ShadowCache.h +++ b/Engine/Source/ShaderEffect/ShadowCache.h @@ -1,4 +1,114 @@ #ifndef ENGINE_SOURCE_SHADEREFFECT_SHADOWCACHE_H #define ENGINE_SOURCE_SHADEREFFECT_SHADOWCACHE_H +#include +#include +#include "Engine/Source/ShaderEffect/Types.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "_Triage/CWFrustum.h" + +class CFrustum; +class CGxTex; +class CTexture; +struct CShadowCacheQueryData; + +typedef void (*ShadowMatrixCallback)(C3Vector& center, float radius, C44Matrix& matrix, C3Vector& direction, int32_t band); +typedef void (*ShadowFrustumCallback)(CFrustum& frustum, C44Matrix& matrix, const C3Vector& direction, CShadowCacheQueryData& data, int32_t band); +typedef int32_t (*ShadowQueryCallback)(CShadowCacheQueryData& data, uint32_t frame); +typedef int32_t (*ShadowRenderCallback)(CShadowCacheQueryData& data, int32_t band, CGxTex* target, CGxTex* colorTarget, C3Vector& direction); + +struct CShadowBand { + /* 0000 */ CTexture* m_textures[2]; + /* 0008 */ float m_radius; + /* 000C */ float m_updateDistance; + /* 0010 */ C3Vector m_lastPos; + /* 001C */ C3Vector m_pos; + /* 0028 */ C3Vector m_dir; + /* 0034 */ uint32_t m_updateCount; + /* 0038 */ uint32_t m_current; + + CShadowBand(); +}; + +struct CShadowCacheQueryData { + /* 0000 */ int32_t m_queryFlags[3]; + /* 000C */ int32_t m_active[3]; + /* 0018 */ int32_t m_useCullFrustum[3]; + /* 0024 */ CAaBox m_extents[3]; + /* 006C */ CWFrustum m_frustums[3]; + /* 0348 */ CWFrustum m_cullFrustums[3]; + /* 0624 */ CWFrustum unk_0624[3]; + /* 0900 */ float m_cullRects[3][4]; + /* 0930 */ C3Vector m_pos[3]; + /* 0954 */ uint8_t m_textureIndex[3]; + /* 0958 */ float m_radius[3]; + /* 0964 */ float m_bounds[3][4]; + /* 0994 */ float m_texBounds[3][4]; + /* 09C4 */ C44Matrix m_matrices[3]; + /* 0A84 */ int32_t m_lastBand; + /* 0A88 */ int32_t m_useFrustum; + + CShadowCacheQueryData(); +}; + +class CShadowCache { + public: + static const float s_bandRadius[3]; + static const float s_bandUpdateDistance[3]; + static C4Vector s_cutoffPlane; + static C4Vector s_cutoffPlaneDefault; + static float s_debugAspect; + static int32_t s_debugLevel; + static CShadowBand s_extDynShadow; + static float s_extDynShadowCullRect[4]; + static CShadowBand s_extShadowBands[3]; + static uint8_t s_frameCounter; + static ShadowFrustumCallback s_frustumCallback; + static CTexture* s_intDynShadow; + static ShadowMatrixCallback s_matrixCallback; + static int32_t s_needsRebuild; + static C4Vector s_pcfOffsets[8]; + static ShadowQueryCallback s_queryCallback; + static int32_t s_recreateTextures; + static ShadowRenderCallback s_renderCallback; + static EShadowMode s_shadowMode; + static C4Vector s_shadowMatrices[12]; + static float s_shadowScale; + static uint32_t s_shadowSize; + static const char* s_shadowModeDescs[NUM_SHADOW_MODES + 1]; + static const uint32_t s_shadowValues[NUM_SHADOW_MODES + 1]; + static C3Vector s_sunDir; + static C4Vector s_sunDirViewSpace; + static float s_textureSize[2]; + + static void ClearBandTextures(); + static void Destroy(); + static void GatherBands(CShadowCacheQueryData& data, const C3Vector& pos, CWFrustum& frustum, int32_t useCullFrustum); + static EShadowMode GetShadowMode(); + static const char* GetShadowModeDesc(uint32_t mode); + static uint32_t GetShadowValue(); + static void Init(); + static void InitPcfOffsets(); + static void __fastcall NotifyTexturesRecreate(); + static void PostUpdate(); + static void PreUpdate(const C3Vector& sunDir); + static void RenderBands(CShadowCacheQueryData& data); + static void SetDebugParams(float aspect, int32_t level); + static void SetFrustumCallback(ShadowFrustumCallback callback); + static void SetMatrixCallback(ShadowMatrixCallback callback); + static void SetQueryCallback(ShadowQueryCallback callback); + static void SetRenderCallback(ShadowRenderCallback callback); + static void SetShadowMapDetailDoodad(); + static void SetShadowMapGenericGlobal(); + static void SetShadowMapGenericInterior(int32_t interior); + static void SetShadowMapTerrain(); + static int32_t SetShadowMode(EShadowMode mode); + static void SetShadowScale(float scale); + static int32_t Update(const C3Vector& pos, int32_t useCullFrustum); + static int32_t ValidateShadowMode(EShadowMode mode); +}; + #endif diff --git a/Engine/Source/ShaderEffect/Types.h b/Engine/Source/ShaderEffect/Types.h new file mode 100644 index 0000000..6229245 --- /dev/null +++ b/Engine/Source/ShaderEffect/Types.h @@ -0,0 +1,27 @@ +#ifndef ENGINE_SOURCE_SHADEREFFECT_TYPES_H +#define ENGINE_SOURCE_SHADEREFFECT_TYPES_H + +#include +#include + +enum EGxTexOp { + GxTexOp_Mod = 0, + GxTexOp_Mod2x = 1, + GxTexOp_Add = 2, + GxTexOp_PassThru = 3, + GxTexOp_Decal = 4, + GxTexOp_Fade = 5, + GxTexOps_Last = 6, +}; + +enum EShadowMode { + SHADOW_LOWEST = 0, + SHADOW_LOW = 1, + SHADOW_MEDIUM = 2, + SHADOW_MEDIUM_HIGH = 3, + SHADOW_HIGH = 4, + SHADOW_VERY_HIGH = 5, + NUM_SHADOW_MODES = 6, +}; + +#endif diff --git a/Engine/Source/Tempest/C44Matrix.cpp b/Engine/Source/Tempest/C44Matrix.cpp index bd41b85..52fe255 100644 --- a/Engine/Source/Tempest/C44Matrix.cpp +++ b/Engine/Source/Tempest/C44Matrix.cpp @@ -3,6 +3,27 @@ #include "Engine/Source/Tempest/C44Matrix.h" #include "Engine/Source/Tempest/C4Quaternion.h" +// VERIFY: bcc62f69 auto +// OFFSET: 0x0057C2A0 +void C44Matrix::Identity() { + this->d3 = 1.0f; + this->c2 = 1.0f; + this->b1 = 1.0f; + this->a0 = 1.0f; + this->d2 = 0.0f; + this->d1 = 0.0f; + this->d0 = 0.0f; + this->c3 = 0.0f; + this->c1 = 0.0f; + this->c0 = 0.0f; + this->b3 = 0.0f; + this->b2 = 0.0f; + this->b0 = 0.0f; + this->a3 = 0.0f; + this->a2 = 0.0f; + this->a1 = 0.0f; +} + // VERIFY: b9812d24 manual // OFFSET: 0x00407F40 C44Matrix::C44Matrix() { diff --git a/Engine/Source/Tempest/C44Matrix.h b/Engine/Source/Tempest/C44Matrix.h index 4349eec..95a5a0c 100644 --- a/Engine/Source/Tempest/C44Matrix.h +++ b/Engine/Source/Tempest/C44Matrix.h @@ -41,6 +41,7 @@ class C44Matrix { C44Matrix AffineInverse() const; C44Matrix AffineInverse(float scale) const; float Determinant() const; + void Identity(); C44Matrix Inverse() const; C44Matrix Inverse(float determinant) const; void Rotate(const C4Quaternion& q); diff --git a/_Triage/CWFrustum.h b/_Triage/CWFrustum.h index a5987ad..71db6e9 100644 --- a/_Triage/CWFrustum.h +++ b/_Triage/CWFrustum.h @@ -14,7 +14,10 @@ class CWFrustum : public CFrustum { /* 00E8 */ float m_aspect; /* 00EC */ float m_minz; /* 00F0 */ float m_maxz; - /* 00F4 */ TSLink m_sceneLink; + + CWFrustum(); + CWFrustum& operator=(const CWFrustum& source); + void InvertPlanes(); }; #endif