2017-08-08 06:03:57 +00:00
|
|
|
#include "CFluidPlaneShader.hpp"
|
2018-06-07 04:43:26 +00:00
|
|
|
#include "World/CRipple.hpp"
|
|
|
|
#include "World/CRippleManager.hpp"
|
2017-08-08 06:03:57 +00:00
|
|
|
|
|
|
|
namespace urde
|
|
|
|
{
|
|
|
|
|
|
|
|
CFluidPlaneShader::Cache CFluidPlaneShader::_cache = {};
|
|
|
|
|
|
|
|
u16 CFluidPlaneShader::Cache::MakeCacheKey(const SFluidPlaneShaderInfo& info)
|
|
|
|
{
|
|
|
|
u16 ret = 0;
|
|
|
|
|
|
|
|
switch (info.m_type)
|
|
|
|
{
|
2018-06-07 04:43:26 +00:00
|
|
|
case EFluidType::NormalWater:
|
|
|
|
case EFluidType::PhazonFluid:
|
|
|
|
case EFluidType::Four:
|
2017-08-08 06:03:57 +00:00
|
|
|
if (info.m_hasLightmap)
|
|
|
|
{
|
|
|
|
ret |= 1 << 2;
|
|
|
|
if (info.m_doubleLightmapBlend)
|
|
|
|
ret |= 1 << 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.m_hasEnvMap && info.m_hasEnvBumpMap)
|
|
|
|
ret |= 1 << 4;
|
|
|
|
|
|
|
|
if (info.m_hasEnvMap)
|
|
|
|
ret |= 1 << 5;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
case EFluidType::PoisonWater:
|
2017-08-08 06:03:57 +00:00
|
|
|
ret |= 1;
|
|
|
|
|
|
|
|
if (info.m_hasLightmap)
|
|
|
|
{
|
|
|
|
ret |= 1 << 2;
|
|
|
|
if (info.m_doubleLightmapBlend)
|
|
|
|
ret |= 1 << 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.m_hasEnvBumpMap)
|
|
|
|
ret |= 1 << 4;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
case EFluidType::Lava:
|
2017-08-08 06:03:57 +00:00
|
|
|
ret |= 2;
|
|
|
|
|
|
|
|
if (info.m_hasBumpMap)
|
|
|
|
ret |= 1 << 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
case EFluidType::ThickLava:
|
2017-08-08 06:03:57 +00:00
|
|
|
ret |= 3;
|
|
|
|
|
|
|
|
if (info.m_hasBumpMap)
|
|
|
|
ret |= 1 << 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.m_hasPatternTex1)
|
|
|
|
ret |= 1 << 6;
|
|
|
|
if (info.m_hasPatternTex2)
|
|
|
|
ret |= 1 << 7;
|
|
|
|
if (info.m_hasColorTex)
|
|
|
|
ret |= 1 << 8;
|
|
|
|
|
|
|
|
if (info.m_additive)
|
|
|
|
ret |= 1 << 9;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-14 03:55:06 +00:00
|
|
|
u16 CFluidPlaneShader::Cache::MakeCacheKey(const SFluidPlaneDoorShaderInfo& info)
|
|
|
|
{
|
|
|
|
u16 ret = 0;
|
|
|
|
|
|
|
|
if (info.m_hasPatternTex1)
|
|
|
|
ret |= 1 << 0;
|
|
|
|
if (info.m_hasPatternTex2)
|
|
|
|
ret |= 1 << 1;
|
|
|
|
if (info.m_hasColorTex)
|
|
|
|
ret |= 1 << 2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-06-07 04:43:26 +00:00
|
|
|
CFluidPlaneShader::ShaderPair CFluidPlaneShader::Cache::GetOrBuildShader(const T& info)
|
2017-08-08 06:03:57 +00:00
|
|
|
{
|
|
|
|
u16 key = MakeCacheKey(info);
|
2017-08-14 03:55:06 +00:00
|
|
|
auto& slot = CacheSlot(info, key);
|
2018-06-07 04:43:26 +00:00
|
|
|
if (slot.m_regular)
|
2017-11-05 06:17:12 +00:00
|
|
|
return slot;
|
2017-08-08 06:03:57 +00:00
|
|
|
|
|
|
|
if (CGraphics::g_BooFactory == nullptr)
|
2018-06-07 04:43:26 +00:00
|
|
|
return {};
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-05-25 06:39:38 +00:00
|
|
|
CGraphics::CommitResources(
|
2017-08-08 06:03:57 +00:00
|
|
|
[&](boo::IGraphicsDataFactory::Context& ctx)
|
|
|
|
{
|
|
|
|
switch (ctx.platform())
|
|
|
|
{
|
2017-12-06 03:26:15 +00:00
|
|
|
#if BOO_HAS_GL
|
2017-08-08 06:03:57 +00:00
|
|
|
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
2017-11-05 06:17:12 +00:00
|
|
|
slot = BuildShader(static_cast<boo::GLDataFactory::Context&>(ctx), info);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
2017-12-06 03:26:15 +00:00
|
|
|
#endif
|
2017-08-08 06:03:57 +00:00
|
|
|
#if _WIN32
|
|
|
|
case boo::IGraphicsDataFactory::Platform::D3D11:
|
2018-05-25 06:39:38 +00:00
|
|
|
slot = BuildShader(static_cast<boo::D3DDataFactory::Context&>(ctx), info);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Metal:
|
2017-11-05 06:17:12 +00:00
|
|
|
slot = BuildShader(static_cast<boo::MetalDataFactory::Context&>(ctx), info);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_VULKAN
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
2017-11-05 06:17:12 +00:00
|
|
|
slot = BuildShader(static_cast<boo::VulkanDataFactory::Context&>(ctx), info);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-25 06:39:38 +00:00
|
|
|
} BooTrace);
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2017-11-05 06:17:12 +00:00
|
|
|
return slot;
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
template CFluidPlaneShader::ShaderPair
|
2017-08-14 03:55:06 +00:00
|
|
|
CFluidPlaneShader::Cache::GetOrBuildShader<SFluidPlaneShaderInfo>(const SFluidPlaneShaderInfo& info);
|
2018-06-07 04:43:26 +00:00
|
|
|
template CFluidPlaneShader::ShaderPair
|
2017-08-14 03:55:06 +00:00
|
|
|
CFluidPlaneShader::Cache::GetOrBuildShader<SFluidPlaneDoorShaderInfo>(const SFluidPlaneDoorShaderInfo& info);
|
|
|
|
|
|
|
|
void CFluidPlaneShader::Cache::Clear()
|
2017-08-08 06:03:57 +00:00
|
|
|
{
|
2017-08-14 03:55:06 +00:00
|
|
|
for (auto& p : m_cache)
|
2017-11-05 06:17:12 +00:00
|
|
|
p.reset();
|
2017-08-14 03:55:06 +00:00
|
|
|
for (auto& p : m_doorCache)
|
2017-11-05 06:17:12 +00:00
|
|
|
p.reset();
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
2017-08-08 06:03:57 +00:00
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
void CFluidPlaneShader::PrepareBinding(const ShaderPair& pipeline, u32 maxVertCount)
|
2017-08-14 03:55:06 +00:00
|
|
|
{
|
2018-05-25 06:39:38 +00:00
|
|
|
CGraphics::CommitResources(
|
2017-08-08 06:03:57 +00:00
|
|
|
[&](boo::IGraphicsDataFactory::Context& ctx)
|
|
|
|
{
|
2017-08-13 07:56:35 +00:00
|
|
|
m_vbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(Vertex), maxVertCount);
|
2018-06-07 04:43:26 +00:00
|
|
|
if (pipeline.m_tessellation)
|
|
|
|
m_pvbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(PatchVertex), maxVertCount);
|
2017-11-24 08:23:28 +00:00
|
|
|
m_uniBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(Uniform), 1);
|
2017-08-08 06:03:57 +00:00
|
|
|
|
|
|
|
switch (ctx.platform())
|
|
|
|
{
|
2017-12-06 03:26:15 +00:00
|
|
|
#if BOO_HAS_GL
|
2017-08-08 06:03:57 +00:00
|
|
|
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
2018-06-07 04:43:26 +00:00
|
|
|
m_dataBind = BuildBinding(static_cast<boo::GLDataFactory::Context&>(ctx), pipeline);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
2017-12-06 03:26:15 +00:00
|
|
|
#endif
|
2017-08-08 06:03:57 +00:00
|
|
|
#if _WIN32
|
|
|
|
case boo::IGraphicsDataFactory::Platform::D3D11:
|
2018-06-07 04:43:26 +00:00
|
|
|
m_dataBind = BuildBinding(static_cast<boo::D3DDataFactory::Context&>(ctx), pipeline);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Metal:
|
2018-06-07 04:43:26 +00:00
|
|
|
m_dataBind = BuildBinding(static_cast<boo::MetalDataFactory::Context&>(ctx), pipeline);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_VULKAN
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
2018-06-07 04:43:26 +00:00
|
|
|
m_dataBind = BuildBinding(static_cast<boo::VulkanDataFactory::Context&>(ctx), pipeline);
|
2017-08-08 06:03:57 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-25 06:39:38 +00:00
|
|
|
} BooTrace);
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:38:56 +00:00
|
|
|
void CFluidPlaneShader::Shutdown()
|
|
|
|
{
|
|
|
|
_cache.Clear();
|
|
|
|
switch (CGraphics::g_BooFactory->platform())
|
|
|
|
{
|
|
|
|
#if BOO_HAS_GL
|
|
|
|
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
|
|
|
CFluidPlaneShader::_Shutdown<boo::GLDataFactory>();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if _WIN32
|
|
|
|
case boo::IGraphicsDataFactory::Platform::D3D11:
|
2018-05-25 06:39:38 +00:00
|
|
|
CFluidPlaneShader::_Shutdown<boo::D3DDataFactory>();
|
2018-05-20 22:38:56 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_METAL
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Metal:
|
|
|
|
CFluidPlaneShader::_Shutdown<boo::MetalDataFactory>();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if BOO_HAS_VULKAN
|
|
|
|
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
|
|
|
CFluidPlaneShader::_Shutdown<boo::VulkanDataFactory>();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
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,
|
2017-08-14 03:55:06 +00:00
|
|
|
bool doubleLightmapBlend, bool additive, u32 maxVertCount)
|
|
|
|
: m_patternTex1(patternTex1),
|
|
|
|
m_patternTex2(patternTex2),
|
|
|
|
m_colorTex(colorTex),
|
|
|
|
m_bumpMap(bumpMap),
|
|
|
|
m_envMap(envMap),
|
|
|
|
m_envBumpMap(envBumpMap),
|
2018-06-07 04:43:26 +00:00
|
|
|
m_lightmap(lightmap),
|
|
|
|
m_rippleMap(rippleMap)
|
2017-08-14 03:55:06 +00:00
|
|
|
{
|
|
|
|
SFluidPlaneShaderInfo shaderInfo(type,
|
|
|
|
m_patternTex1.operator bool(),
|
|
|
|
m_patternTex2.operator bool(),
|
|
|
|
m_colorTex.operator bool(),
|
|
|
|
m_bumpMap.operator bool(),
|
|
|
|
m_envMap.operator bool(),
|
|
|
|
m_envBumpMap.operator bool(),
|
|
|
|
m_lightmap.operator bool(),
|
2018-06-07 04:43:26 +00:00
|
|
|
m_rippleMap.operator bool(),
|
2017-08-14 03:55:06 +00:00
|
|
|
doubleLightmapBlend, additive);
|
2018-06-07 04:43:26 +00:00
|
|
|
ShaderPair pipeline = _cache.GetOrBuildShader(shaderInfo);
|
|
|
|
PrepareBinding(pipeline, maxVertCount);
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:43:26 +00:00
|
|
|
CFluidPlaneShader::CFluidPlaneShader(const TLockedToken<CTexture>& patternTex1,
|
|
|
|
const TLockedToken<CTexture>& patternTex2,
|
|
|
|
const TLockedToken<CTexture>& colorTex,
|
2017-08-14 03:55:06 +00:00
|
|
|
u32 maxVertCount)
|
|
|
|
: m_patternTex1(patternTex1),
|
|
|
|
m_patternTex2(patternTex2),
|
|
|
|
m_colorTex(colorTex)
|
|
|
|
{
|
|
|
|
SFluidPlaneDoorShaderInfo shaderInfo(m_patternTex1.operator bool(),
|
|
|
|
m_patternTex2.operator bool(),
|
|
|
|
m_colorTex.operator bool());
|
2018-06-07 04:43:26 +00:00
|
|
|
ShaderPair pipeline = _cache.GetOrBuildShader(shaderInfo);
|
|
|
|
PrepareBinding(pipeline, maxVertCount);
|
2017-08-14 03:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFluidPlaneShader::prepareDraw(const RenderSetupInfo& info)
|
2017-08-08 06:03:57 +00:00
|
|
|
{
|
|
|
|
Uniform& uni = *reinterpret_cast<Uniform*>(m_uniBuf->map(sizeof(Uniform)));
|
|
|
|
uni.m_mv = CGraphics::g_GXModelView.toMatrix4f();
|
2017-08-14 03:55:06 +00:00
|
|
|
uni.m_mvNorm = info.normMtx;
|
2017-08-08 06:03:57 +00:00
|
|
|
uni.m_proj = CGraphics::GetPerspectiveProjectionMatrix(true);
|
|
|
|
for (int i=0 ; i<6 ; ++i)
|
2017-08-14 03:55:06 +00:00
|
|
|
uni.m_texMtxs[i] = info.texMtxs[i];
|
|
|
|
uni.m_lighting.ActivateLights(info.lights);
|
2017-08-08 06:03:57 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
2017-08-14 03:55:06 +00:00
|
|
|
uni.m_lighting.colorRegs[i] = info.kColors[i];
|
|
|
|
uni.m_lighting.mulColor = info.kColors[3];
|
|
|
|
uni.m_lighting.fog.m_rangeScale = info.indScale;
|
2017-08-08 06:03:57 +00:00
|
|
|
m_uniBuf->unmap();
|
2017-08-11 01:16:58 +00:00
|
|
|
}
|
2017-08-10 23:13:25 +00:00
|
|
|
|
2018-06-07 04:43:26 +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);
|
|
|
|
for (int i=0 ; i<6 ; ++i)
|
|
|
|
uni.m_texMtxs[i] = info.texMtxs[i];
|
|
|
|
int i = 0;
|
|
|
|
for (const CRipple& ripple : rippleManager.GetRipples())
|
|
|
|
{
|
|
|
|
assert(i < 20 && "Too many ripples");
|
|
|
|
Ripple& rOut = uni.m_ripple[i++];
|
|
|
|
if (ripple.GetTime() >= ripple.GetTimeFalloff())
|
|
|
|
{
|
|
|
|
rOut.center.zeroOut();
|
|
|
|
rOut.params.zeroOut();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
zeus::CVector3f localPos = ripple.GetCenter() - waterCenter;
|
|
|
|
rOut.center.x = localPos.x;
|
|
|
|
rOut.center.y = 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);
|
|
|
|
for (i=0 ; i<3 ; ++i)
|
|
|
|
uni.m_lighting.colorRegs[i] = info.kColors[i];
|
|
|
|
uni.m_lighting.mulColor = info.kColors[3];
|
|
|
|
uni.m_lighting.fog.m_rangeScale = info.indScale;
|
|
|
|
m_uniBuf->unmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFluidPlaneShader::loadVerts(const std::vector<Vertex>& verts, const std::vector<PatchVertex>& pVerts)
|
2017-08-11 01:16:58 +00:00
|
|
|
{
|
2017-08-10 23:13:25 +00:00
|
|
|
m_vbo->load(verts.data(), verts.size() * sizeof(Vertex));
|
2018-06-07 04:43:26 +00:00
|
|
|
if (m_pvbo)
|
|
|
|
m_pvbo->load(pVerts.data(), pVerts.size() * sizeof(PatchVertex));
|
2017-08-08 06:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|