2019-09-29 00:30:53 +00:00
|
|
|
#include "Runtime/Graphics/Shaders/CFluidPlaneShader.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/World/CRipple.hpp"
|
|
|
|
#include "Runtime/World/CRippleManager.hpp"
|
|
|
|
|
|
|
|
#include <hecl/Pipeline.hpp>
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2017-08-08 06:03:57 +00:00
|
|
|
|
|
|
|
CFluidPlaneShader::Cache CFluidPlaneShader::_cache = {};
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
u16 CFluidPlaneShader::Cache::MakeCacheKey(const SFluidPlaneShaderInfo& info) {
|
|
|
|
u16 ret = 0;
|
|
|
|
|
|
|
|
switch (info.m_type) {
|
|
|
|
case EFluidType::NormalWater:
|
|
|
|
case EFluidType::PhazonFluid:
|
|
|
|
case EFluidType::Four:
|
|
|
|
if (info.m_hasLightmap) {
|
|
|
|
ret |= 1 << 2;
|
|
|
|
if (info.m_doubleLightmapBlend)
|
|
|
|
ret |= 1 << 3;
|
|
|
|
}
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (!info.m_hasEnvMap && info.m_hasEnvBumpMap)
|
|
|
|
ret |= 1 << 4;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasEnvMap)
|
|
|
|
ret |= 1 << 5;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
case EFluidType::PoisonWater:
|
|
|
|
ret |= 1;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasLightmap) {
|
|
|
|
ret |= 1 << 2;
|
|
|
|
if (info.m_doubleLightmapBlend)
|
|
|
|
ret |= 1 << 3;
|
|
|
|
}
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasEnvBumpMap)
|
|
|
|
ret |= 1 << 4;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
case EFluidType::Lava:
|
|
|
|
ret |= 2;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasBumpMap)
|
|
|
|
ret |= 1 << 2;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
case EFluidType::ThickLava:
|
|
|
|
ret |= 3;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasBumpMap)
|
|
|
|
ret |= 1 << 2;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasPatternTex1)
|
|
|
|
ret |= 1 << 6;
|
|
|
|
if (info.m_hasPatternTex2)
|
|
|
|
ret |= 1 << 7;
|
|
|
|
if (info.m_hasColorTex)
|
|
|
|
ret |= 1 << 8;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_additive)
|
|
|
|
ret |= 1 << 9;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return ret;
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
u16 CFluidPlaneShader::Cache::MakeCacheKey(const SFluidPlaneDoorShaderInfo& info) {
|
|
|
|
u16 ret = 0;
|
2017-08-14 03:55:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (info.m_hasPatternTex1)
|
|
|
|
ret |= 1 << 0;
|
|
|
|
if (info.m_hasPatternTex2)
|
|
|
|
ret |= 1 << 1;
|
|
|
|
if (info.m_hasColorTex)
|
|
|
|
ret |= 1 << 2;
|
2017-08-14 03:55:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return ret;
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
template <>
|
|
|
|
CFluidPlaneShader::ShaderPair
|
|
|
|
CFluidPlaneShader::Cache::GetOrBuildShader<SFluidPlaneShaderInfo>(const SFluidPlaneShaderInfo& info) {
|
|
|
|
u16 key = MakeCacheKey(info);
|
|
|
|
auto& slot = CacheSlot(info, key);
|
|
|
|
if (slot.m_regular)
|
2017-11-05 06:17:12 +00:00
|
|
|
return slot;
|
2018-10-07 02:59:17 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
slot.m_regular = hecl::conv->convert(Shader_CFluidPlaneShader{info, false});
|
|
|
|
if (info.m_tessellation)
|
|
|
|
slot.m_tessellation = hecl::conv->convert(Shader_CFluidPlaneShader{info, true});
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return slot;
|
2018-10-07 02:59:17 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
template <>
|
|
|
|
CFluidPlaneShader::ShaderPair
|
|
|
|
CFluidPlaneShader::Cache::GetOrBuildShader<SFluidPlaneDoorShaderInfo>(const SFluidPlaneDoorShaderInfo& info) {
|
|
|
|
u16 key = MakeCacheKey(info);
|
|
|
|
auto& slot = CacheSlot(info, key);
|
|
|
|
if (slot.m_regular)
|
|
|
|
return slot;
|
2017-08-14 03:55:06 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
slot.m_regular = hecl::conv->convert(Shader_CFluidPlaneDoorShader{info});
|
|
|
|
|
|
|
|
return slot;
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CFluidPlaneShader::Cache::Clear() {
|
|
|
|
for (auto& p : m_cache)
|
|
|
|
p.reset();
|
|
|
|
for (auto& p : m_doorCache)
|
|
|
|
p.reset();
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 06:02:56 +00:00
|
|
|
void CFluidPlaneShader::PrepareBinding(u32 maxVertCount) {
|
2018-12-08 05:30:43 +00:00
|
|
|
CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) {
|
|
|
|
m_vbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(Vertex), maxVertCount);
|
2019-09-29 02:22:12 +00:00
|
|
|
if (m_pipelines.m_tessellation) {
|
2018-12-08 05:30:43 +00:00
|
|
|
m_pvbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(PatchVertex), maxVertCount);
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
m_uniBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(Uniform), 1);
|
|
|
|
|
2019-09-29 02:22:12 +00:00
|
|
|
const std::array<boo::ObjToken<boo::IGraphicsBuffer>, 3> ubufs{{
|
|
|
|
m_uniBuf.get(),
|
|
|
|
m_uniBuf.get(),
|
|
|
|
m_uniBuf.get(),
|
|
|
|
}};
|
|
|
|
constexpr std::array<boo::PipelineStage, 3> ubufStages{
|
|
|
|
boo::PipelineStage::Vertex,
|
|
|
|
boo::PipelineStage::Vertex,
|
|
|
|
boo::PipelineStage::Fragment,
|
|
|
|
};
|
|
|
|
constexpr std::array<size_t, 3> ubufOffs{
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1280,
|
|
|
|
};
|
|
|
|
constexpr std::array<size_t, 3> ubufSizes{
|
|
|
|
1280,
|
|
|
|
1280,
|
|
|
|
sizeof(CModelShaders::LightingUniform),
|
|
|
|
};
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
size_t texCount = 0;
|
2019-09-29 02:22:12 +00:00
|
|
|
std::array<boo::ObjToken<boo::ITexture>, 8> texs;
|
|
|
|
if (m_patternTex1) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_patternTex1->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_patternTex2) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_patternTex2->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_colorTex) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_colorTex->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_bumpMap) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_bumpMap->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_envMap) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_envMap->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_envBumpMap) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_envBumpMap->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
if (m_lightmap) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_lightmap->GetBooTexture();
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
|
|
|
auto regular = ctx.newShaderDataBinding(m_pipelines.m_regular, m_vbo.get(), nullptr, nullptr, ubufs.size(),
|
|
|
|
ubufs.data(), ubufStages.data(), ubufOffs.data(), ubufSizes.data(),
|
|
|
|
texCount, texs.data(), nullptr, nullptr);
|
2018-12-08 05:30:43 +00:00
|
|
|
boo::ObjToken<boo::IShaderDataBinding> tessellation;
|
2019-06-21 06:02:56 +00:00
|
|
|
if (m_pipelines.m_tessellation) {
|
2018-12-08 05:30:43 +00:00
|
|
|
texs[texCount++] = m_rippleMap.get();
|
2019-09-29 02:22:12 +00:00
|
|
|
tessellation = ctx.newShaderDataBinding(m_pipelines.m_tessellation, m_pvbo.get(), nullptr, nullptr, ubufs.size(),
|
|
|
|
ubufs.data(), ubufStages.data(), ubufOffs.data(), ubufSizes.data(),
|
|
|
|
texCount, texs.data(), nullptr, nullptr);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
m_dataBind = {regular, tessellation};
|
|
|
|
return true;
|
|
|
|
} BooTrace);
|
2018-05-20 22:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CFluidPlaneShader::Shutdown() { _cache.Clear(); }
|
|
|
|
|
|
|
|
CFluidPlaneShader::CFluidPlaneShader(EFluidType type, const TLockedToken<CTexture>& patternTex1,
|
|
|
|
const TLockedToken<CTexture>& patternTex2, const TLockedToken<CTexture>& colorTex,
|
|
|
|
const TLockedToken<CTexture>& bumpMap, const TLockedToken<CTexture>& envMap,
|
|
|
|
const TLockedToken<CTexture>& envBumpMap, const TLockedToken<CTexture>& lightmap,
|
|
|
|
const boo::ObjToken<boo::ITextureS>& rippleMap, bool doubleLightmapBlend,
|
|
|
|
bool additive, u32 maxVertCount)
|
|
|
|
: m_patternTex1(patternTex1)
|
|
|
|
, m_patternTex2(patternTex2)
|
|
|
|
, m_colorTex(colorTex)
|
|
|
|
, m_bumpMap(bumpMap)
|
|
|
|
, m_envMap(envMap)
|
|
|
|
, m_envBumpMap(envBumpMap)
|
|
|
|
, m_lightmap(lightmap)
|
|
|
|
, m_rippleMap(rippleMap) {
|
2020-05-13 00:45:30 +00:00
|
|
|
SFluidPlaneShaderInfo shaderInfo(type, m_patternTex1.HasReference(), m_patternTex2.HasReference(),
|
|
|
|
m_colorTex.HasReference(), m_bumpMap.HasReference(), m_envMap.HasReference(),
|
|
|
|
m_envBumpMap.HasReference(), m_lightmap.HasReference(), m_rippleMap.operator bool(),
|
|
|
|
doubleLightmapBlend, additive);
|
2019-06-21 06:02:56 +00:00
|
|
|
m_pipelines = _cache.GetOrBuildShader(shaderInfo);
|
|
|
|
PrepareBinding(maxVertCount);
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
CFluidPlaneShader::CFluidPlaneShader(const TLockedToken<CTexture>& patternTex1,
|
2018-12-08 05:30:43 +00:00
|
|
|
const TLockedToken<CTexture>& patternTex2, const TLockedToken<CTexture>& colorTex,
|
2017-08-14 03:55:06 +00:00
|
|
|
u32 maxVertCount)
|
2018-12-08 05:30:43 +00:00
|
|
|
: m_patternTex1(patternTex1), m_patternTex2(patternTex2), m_colorTex(colorTex) {
|
2020-05-13 00:45:30 +00:00
|
|
|
SFluidPlaneDoorShaderInfo shaderInfo(m_patternTex1.HasReference(), m_patternTex2.HasReference(),
|
|
|
|
m_colorTex.HasReference());
|
2019-06-21 06:02:56 +00:00
|
|
|
m_pipelines = _cache.GetOrBuildShader(shaderInfo);
|
|
|
|
PrepareBinding(maxVertCount);
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CFluidPlaneShader::prepareDraw(const RenderSetupInfo& info) {
|
|
|
|
Uniform& uni = *reinterpret_cast<Uniform*>(m_uniBuf->map(sizeof(Uniform)));
|
|
|
|
uni.m_mv = CGraphics::g_GXModelView.toMatrix4f();
|
|
|
|
uni.m_mvNorm = info.normMtx;
|
|
|
|
uni.m_proj = CGraphics::GetPerspectiveProjectionMatrix(true);
|
2019-09-29 02:22:12 +00:00
|
|
|
uni.m_texMtxs = info.texMtxs;
|
2018-12-08 05:30:43 +00:00
|
|
|
uni.m_lighting.ActivateLights(info.lights);
|
2019-09-29 02:22:12 +00:00
|
|
|
for (size_t i = 0; i < uni.m_lighting.colorRegs.size(); ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
uni.m_lighting.colorRegs[i] = info.kColors[i];
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
uni.m_lighting.mulColor = info.kColors[3];
|
2019-03-03 23:04:18 +00:00
|
|
|
uni.m_lighting.fog = CGraphics::g_Fog;
|
2019-03-03 06:19:42 +00:00
|
|
|
uni.m_pad2.x() = info.indScale;
|
2018-12-08 05:30:43 +00:00
|
|
|
m_uniBuf->unmap();
|
2017-08-11 01:16:58 +00:00
|
|
|
}
|
2017-08-10 23:13:25 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CFluidPlaneShader::prepareDraw(const RenderSetupInfo& info, const zeus::CVector3f& waterCenter,
|
|
|
|
const CRippleManager& rippleManager, const zeus::CColor& colorMul,
|
|
|
|
float rippleNormResolution) {
|
|
|
|
Uniform& uni = *reinterpret_cast<Uniform*>(m_uniBuf->map(sizeof(Uniform)));
|
|
|
|
uni.m_mv = CGraphics::g_GXModelView.toMatrix4f();
|
|
|
|
uni.m_mvNorm = info.normMtx;
|
|
|
|
uni.m_proj = CGraphics::GetPerspectiveProjectionMatrix(true);
|
2019-09-29 02:22:12 +00:00
|
|
|
uni.m_texMtxs = info.texMtxs;
|
|
|
|
|
|
|
|
size_t i = 0;
|
2018-12-08 05:30:43 +00:00
|
|
|
for (const CRipple& ripple : rippleManager.GetRipples()) {
|
2019-09-29 02:22:12 +00:00
|
|
|
assert(i < uni.m_ripple.size() && "Too many ripples");
|
2018-12-08 05:30:43 +00:00
|
|
|
Ripple& rOut = uni.m_ripple[i++];
|
|
|
|
if (ripple.GetTime() >= ripple.GetTimeFalloff()) {
|
|
|
|
rOut.center.zeroOut();
|
|
|
|
rOut.params.zeroOut();
|
|
|
|
continue;
|
2018-06-07 04:43:26 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CVector3f localPos = ripple.GetCenter() - waterCenter;
|
|
|
|
rOut.center.x() = float(localPos.x());
|
|
|
|
rOut.center.y() = float(localPos.y());
|
|
|
|
rOut.center.z() = ripple.GetTime() * ripple.GetOOTimeFalloff();
|
|
|
|
rOut.center.w() = ripple.GetOODistanceFalloff();
|
|
|
|
rOut.params.x() = ripple.GetAmplitude();
|
|
|
|
rOut.params.y() = ripple.GetPhase();
|
|
|
|
rOut.params.z() =
|
|
|
|
(1.f - ripple.GetTime() * ripple.GetOOTimeFalloff() * ripple.GetOOTimeFalloff()) * ripple.GetFrequency();
|
|
|
|
}
|
|
|
|
uni.m_colorMul = colorMul;
|
|
|
|
uni.m_pad[0].x() = rippleNormResolution;
|
|
|
|
uni.m_lighting.ActivateLights(info.lights);
|
2019-09-29 02:22:12 +00:00
|
|
|
for (i = 0; i < uni.m_lighting.colorRegs.size(); ++i) {
|
2018-12-08 05:30:43 +00:00
|
|
|
uni.m_lighting.colorRegs[i] = info.kColors[i];
|
2019-09-29 02:22:12 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
uni.m_lighting.mulColor = info.kColors[3];
|
|
|
|
uni.m_lighting.fog = CGraphics::g_Fog;
|
|
|
|
uni.m_pad2.x() = info.indScale;
|
|
|
|
m_uniBuf->unmap();
|
2018-06-07 04:43:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CFluidPlaneShader::loadVerts(const std::vector<Vertex>& verts, const std::vector<PatchVertex>& pVerts) {
|
|
|
|
m_vbo->load(verts.data(), verts.size() * sizeof(Vertex));
|
|
|
|
if (m_pvbo)
|
|
|
|
m_pvbo->load(pVerts.data(), pVerts.size() * sizeof(PatchVertex));
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|