2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-09 05:47:42 +00:00

Initial CFluidPlane implementation

This commit is contained in:
Jack Andersen
2017-08-07 20:03:57 -10:00
parent 5590e2f27a
commit 3650a58a1e
27 changed files with 1801 additions and 99 deletions

View File

@@ -19,7 +19,8 @@ if(WIN32)
Shaders/CScanLinesFilterHLSL.cpp
Shaders/CRandomStaticFilterHLSL.cpp
Shaders/CElementGenShadersHLSL.cpp
Shaders/CParticleSwooshShadersHLSL.cpp)
Shaders/CParticleSwooshShadersHLSL.cpp
Shaders/CFluidPlaneShaderHLSL.cpp)
elseif(BOO_HAS_METAL)
set(PLAT_SRCS
Shaders/CLineRendererShadersMetal.cpp
@@ -41,7 +42,8 @@ elseif(BOO_HAS_METAL)
Shaders/CScanLinesFilterMetal.cpp
Shaders/CRandomStaticFilterMetal.cpp
Shaders/CElementGenShadersMetal.cpp
Shaders/CParticleSwooshShadersMetal.cpp)
Shaders/CParticleSwooshShadersMetal.cpp
Shaders/CFluidPlaneShaderMetal.cpp)
endif()
set(GRAPHICS_SOURCES
@@ -85,6 +87,7 @@ set(GRAPHICS_SOURCES
Shaders/CRandomStaticFilter.hpp Shaders/CRandomStaticFilter.cpp Shaders/CRandomStaticFilterGLSL.cpp
Shaders/CElementGenShaders.hpp Shaders/CElementGenShaders.cpp Shaders/CElementGenShadersGLSL.cpp
Shaders/CParticleSwooshShaders.hpp Shaders/CParticleSwooshShaders.cpp Shaders/CParticleSwooshShadersGLSL.cpp
Shaders/CFluidPlaneShader.hpp Shaders/CFluidPlaneShader.cpp Shaders/CFluidPlaneShaderGLSL.cpp
${PLAT_SRCS})
runtime_add_list(Graphics GRAPHICS_SOURCES)

View File

@@ -390,50 +390,7 @@ void CBooModel::MakeTexturesFromMats(std::vector<TCachedToken<CTexture>>& toksOu
void CBooModel::ActivateLights(const std::vector<CLight>& lights)
{
m_lightingData.ambient = zeus::CColor::skBlack;
size_t curLight = 0;
for (const CLight& light : lights)
{
switch (light.x1c_type)
{
case ELightType::LocalAmbient:
m_lightingData.ambient += light.x18_color;
break;
case ELightType::Point:
case ELightType::Spot:
case ELightType::Custom:
case ELightType::Directional:
{
if (curLight >= URDE_MAX_LIGHTS)
continue;
CModelShaders::Light& lightOut = m_lightingData.lights[curLight++];
lightOut.pos = CGraphics::g_CameraMatrix * light.x0_pos;
lightOut.dir = CGraphics::g_CameraMatrix.basis * light.xc_dir;
lightOut.dir.normalize();
lightOut.color = light.x18_color;
lightOut.linAtt[0] = light.x24_distC;
lightOut.linAtt[1] = light.x28_distL;
lightOut.linAtt[2] = light.x2c_distQ;
lightOut.angAtt[0] = light.x30_angleC;
lightOut.angAtt[1] = light.x34_angleL;
lightOut.angAtt[2] = light.x38_angleQ;
if (light.x1c_type == ELightType::Directional)
lightOut.pos = (-lightOut.dir) * 1048576.f;
break;
}
default: break;
}
}
for (; curLight<URDE_MAX_LIGHTS ; ++curLight)
{
CModelShaders::Light& lightOut = m_lightingData.lights[curLight];
lightOut.color = zeus::CColor::skClear;
lightOut.linAtt[0] = 1.f;
lightOut.angAtt[0] = 1.f;
}
m_lightingData.ActivateLights(lights);
}
void CBooModel::DisableAllLights()

View File

@@ -0,0 +1,199 @@
#include "CFluidPlaneShader.hpp"
namespace urde
{
CFluidPlaneShader::Cache CFluidPlaneShader::_cache = {};
u16 CFluidPlaneShader::Cache::MakeCacheKey(const SFluidPlaneShaderInfo& info)
{
u16 ret = 0;
switch (info.m_type)
{
case CFluidPlane::EFluidType::NormalWater:
case CFluidPlane::EFluidType::Three:
case CFluidPlane::EFluidType::Four:
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;
case CFluidPlane::EFluidType::PoisonWater:
ret |= 1;
if (info.m_hasLightmap)
{
ret |= 1 << 2;
if (info.m_doubleLightmapBlend)
ret |= 1 << 3;
}
if (info.m_hasEnvBumpMap)
ret |= 1 << 4;
break;
case CFluidPlane::EFluidType::Lava:
ret |= 2;
if (info.m_hasBumpMap)
ret |= 1 << 2;
break;
case CFluidPlane::EFluidType::Five:
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;
}
boo::IShaderPipeline* CFluidPlaneShader::Cache::GetOrBuildShader(const SFluidPlaneShaderInfo& info)
{
u16 key = MakeCacheKey(info);
auto& slot = m_cache[key];
if (slot.second != nullptr)
return slot.second;
if (CGraphics::g_BooFactory == nullptr)
return nullptr;
slot.first = CGraphics::CommitResources(
[&](boo::IGraphicsDataFactory::Context& ctx)
{
switch (ctx.platform())
{
case boo::IGraphicsDataFactory::Platform::OpenGL:
slot.second = BuildShader(static_cast<boo::GLDataFactory::Context&>(ctx), info);
break;
#if _WIN32
case boo::IGraphicsDataFactory::Platform::D3D11:
case boo::IGraphicsDataFactory::Platform::D3D12:
slot.second = BuildShader(static_cast<boo::ID3DDataFactory::Context&>(ctx), info);
break;
#endif
#if BOO_HAS_METAL
case boo::IGraphicsDataFactory::Platform::Metal:
slot.second = BuildShader(static_cast<boo::MetalDataFactory::Context&>(ctx), info);
break;
#endif
#if BOO_HAS_VULKAN
case boo::IGraphicsDataFactory::Platform::Vulkan:
slot.second = BuildShader(static_cast<boo::VulkanDataFactory::Context&>(ctx), info);
break;
#endif
default: break;
}
return true;
});
return slot.second;
}
CFluidPlaneShader::CFluidPlaneShader(CFluidPlane::EFluidType type,
const std::experimental::optional<TLockedToken<CTexture>>& patternTex1,
const std::experimental::optional<TLockedToken<CTexture>>& patternTex2,
const std::experimental::optional<TLockedToken<CTexture>>& colorTex,
const std::experimental::optional<TLockedToken<CTexture>>& bumpMap,
const std::experimental::optional<TLockedToken<CTexture>>& envMap,
const std::experimental::optional<TLockedToken<CTexture>>& envBumpMap,
const std::experimental::optional<TLockedToken<CTexture>>& lightmap,
bool doubleLightmapBlend, bool additive)
: m_patternTex1(patternTex1),
m_patternTex2(patternTex2),
m_colorTex(colorTex),
m_bumpMap(bumpMap),
m_envMap(envMap),
m_envBumpMap(envBumpMap),
m_lightmap(lightmap)
{
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(),
doubleLightmapBlend, additive);
boo::IShaderPipeline* pipeline = _cache.GetOrBuildShader(shaderInfo);
m_gfxTok = CGraphics::CommitResources(
[&](boo::IGraphicsDataFactory::Context& ctx)
{
m_vbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(Vertex), 999); // TODO: Figure out how many
m_uniBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, 1024, 1);
switch (ctx.platform())
{
case boo::IGraphicsDataFactory::Platform::OpenGL:
m_dataBind = BuildBinding(static_cast<boo::GLDataFactory::Context&>(ctx), pipeline);
break;
#if _WIN32
case boo::IGraphicsDataFactory::Platform::D3D11:
case boo::IGraphicsDataFactory::Platform::D3D12:
m_dataBind = BuildBinding(static_cast<boo::ID3DDataFactory::Context&>(ctx), pipeline);
break;
#endif
#if BOO_HAS_METAL
case boo::IGraphicsDataFactory::Platform::Metal:
m_dataBind = BuildBinding(static_cast<boo::MetalDataFactory::Context&>(ctx), pipeline);
break;
#endif
#if BOO_HAS_VULKAN
case boo::IGraphicsDataFactory::Platform::Vulkan:
m_dataBind = BuildBinding(static_cast<boo::VulkanDataFactory::Context&>(ctx), pipeline);
break;
#endif
default: break;
}
return true;
});
}
void CFluidPlaneShader::draw(const zeus::CMatrix4f texMtxs[6], const zeus::CMatrix4f& normMtx, float indScale,
const std::vector<CLight>& lights, const zeus::CColor kColors[4])
{
Uniform& uni = *reinterpret_cast<Uniform*>(m_uniBuf->map(sizeof(Uniform)));
uni.m_mv = CGraphics::g_GXModelView.toMatrix4f();
uni.m_mvNorm = normMtx;
uni.m_proj = CGraphics::GetPerspectiveProjectionMatrix(true);
for (int i=0 ; i<6 ; ++i)
uni.m_texMtxs[i] = texMtxs[i];
uni.m_lighting.ActivateLights(lights);
for (int i=0 ; i<3 ; ++i)
uni.m_lighting.colorRegs[i] = kColors[i];
uni.m_lighting.mulColor = kColors[3];
uni.m_lighting.fog.m_rangeScale = indScale;
m_uniBuf->unmap();
CGraphics::SetShaderDataBinding(m_dataBind);
CGraphics::DrawArray(0, 0);
}
}

View File

@@ -0,0 +1,110 @@
#ifndef CFLUIDPLANESHADER_HPP
#define CFLUIDPLANESHADER_HPP
#include "RetroTypes.hpp"
#include "boo/graphicsdev/IGraphicsDataFactory.hpp"
#include "Runtime/World/CFluidPlane.hpp"
#include "CModelShaders.hpp"
#include "boo/graphicsdev/GL.hpp"
#include "boo/graphicsdev/D3D.hpp"
#include "boo/graphicsdev/Metal.hpp"
#include "boo/graphicsdev/Vulkan.hpp"
namespace urde
{
struct SFluidPlaneShaderInfo
{
CFluidPlane::EFluidType m_type;
bool m_hasPatternTex1;
bool m_hasPatternTex2;
bool m_hasColorTex;
bool m_hasBumpMap;
bool m_hasEnvMap;
bool m_hasEnvBumpMap;
bool m_hasLightmap;
bool m_doubleLightmapBlend;
bool m_additive;
SFluidPlaneShaderInfo(CFluidPlane::EFluidType type, bool hasPatternTex1, bool hasPatternTex2, bool hasColorTex,
bool hasBumpMap, bool hasEnvMap, bool hasEnvBumpMap, bool hasLightmap,
bool doubleLightmapBlend, bool additive)
: m_type(type), m_hasPatternTex1(hasPatternTex1), m_hasPatternTex2(hasPatternTex2), m_hasColorTex(hasColorTex),
m_hasBumpMap(hasBumpMap), m_hasEnvMap(hasEnvMap), m_hasEnvBumpMap(hasEnvBumpMap), m_hasLightmap(hasLightmap),
m_doubleLightmapBlend(doubleLightmapBlend), m_additive(additive)
{}
};
class CFluidPlaneShader
{
class Cache
{
std::pair<boo::GraphicsDataToken, boo::IShaderPipeline*> m_cache[1024] = {};
static u16 MakeCacheKey(const SFluidPlaneShaderInfo& info);
public:
boo::IShaderPipeline* GetOrBuildShader(const SFluidPlaneShaderInfo& info);
};
static Cache _cache;
struct Vertex
{
zeus::CVector3f m_pos;
zeus::CVector3f m_norm;
zeus::CVector3f m_binorm;
zeus::CVector3f m_tangent;
zeus::CColor m_color;
};
struct Uniform
{
zeus::CMatrix4f m_mv;
zeus::CMatrix4f m_mvNorm;
zeus::CMatrix4f m_proj;
zeus::CMatrix4f m_texMtxs[9]; // Pad out to 768 bytes
CModelShaders::LightingUniform m_lighting;
};
std::experimental::optional<TLockedToken<CTexture>> m_patternTex1;
std::experimental::optional<TLockedToken<CTexture>> m_patternTex2;
std::experimental::optional<TLockedToken<CTexture>> m_colorTex;
std::experimental::optional<TLockedToken<CTexture>> m_bumpMap;
std::experimental::optional<TLockedToken<CTexture>> m_envMap;
std::experimental::optional<TLockedToken<CTexture>> m_envBumpMap;
std::experimental::optional<TLockedToken<CTexture>> m_lightmap;
boo::GraphicsDataToken m_gfxTok;
boo::IGraphicsBufferD* m_vbo;
boo::IGraphicsBufferD* m_uniBuf;
boo::IShaderDataBinding* m_dataBind;
static boo::IShaderPipeline* BuildShader(boo::GLDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info);
boo::IShaderDataBinding* BuildBinding(boo::GLDataFactory::Context& ctx, boo::IShaderPipeline* pipeline);
#if _WIN32
static boo::IShaderPipeline* BuildShader(boo::ID3DDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info);
boo::IShaderDataBinding* BuildBinding(boo::ID3DDataFactory::Context& ctx, boo::IShaderPipeline* pipeline);
#endif
#if BOO_HAS_METAL
static boo::IShaderPipeline* BuildShader(boo::MetalDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info);
boo::IShaderDataBinding* BuildBinding(boo::MetalDataFactory::Context& ctx, boo::IShaderPipeline* pipeline);
#endif
#if BOO_HAS_VULKAN
static boo::IShaderPipeline* BuildShader(boo::VulkanDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info);
boo::IShaderDataBinding* BuildBinding(boo::VulkanDataFactory::Context& ctx, boo::IShaderPipeline* pipeline);
#endif
public:
CFluidPlaneShader(CFluidPlane::EFluidType type,
const std::experimental::optional<TLockedToken<CTexture>>& patternTex1,
const std::experimental::optional<TLockedToken<CTexture>>& patternTex2,
const std::experimental::optional<TLockedToken<CTexture>>& colorTex,
const std::experimental::optional<TLockedToken<CTexture>>& bumpMap,
const std::experimental::optional<TLockedToken<CTexture>>& envMap,
const std::experimental::optional<TLockedToken<CTexture>>& envBumpMap,
const std::experimental::optional<TLockedToken<CTexture>>& lightmap,
bool doubleLightmapBlend, bool additive);
void draw(const zeus::CMatrix4f texMtxs[6], const zeus::CMatrix4f& normMtx, float indScale,
const std::vector<CLight>& lights, const zeus::CColor kColors[4]);
};
}
#endif // CFLUIDPLANESHADER_HPP

View File

@@ -0,0 +1,559 @@
#include "CFluidPlaneShader.hpp"
namespace urde
{
static const char* VS =
"#version 330\n"
BOO_GLSL_BINDING_HEAD
"layout(location=0) in vec4 posIn;\n"
"layout(location=1) in vec4 normalIn;\n"
"layout(location=2) in vec4 binormalIn;\n"
"layout(location=3) in vec4 tangentIn;\n"
"layout(location=4) in vec4 colorIn;\n"
"\n"
"UBINDING0 uniform FluidPlaneUniform\n"
"{\n"
" mat4 mv;\n"
" mat4 mvNorm;\n"
" mat4 proj;\n"
" mat4 texMtxs[6];\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" vec4 mvPos;\n"
" vec4 mvNorm;\n"
" vec4 mvBinorm;\n"
" vec4 mvTangent;\n"
" vec4 color;\n"
" vec2 uvs[7];\n"
"};\n"
"\n"
"SBINDING(0) out VertToFrag vtf;\n"
"void main()\n"
"{\n"
" vtf.mvPos = mv * vec4(posIn.xyz, 1.0);\n"
" gl_Position = proj * vtf.mvPos;\n"
" vtf.mvNorm = mvNorm * normalIn;\n"
" vtf.mvBinorm = mvNorm * binormalIn;\n"
" vtf.mvTangent = mvNorm * tangentIn;\n"
" vtf.color = colorIn;\n"
" vtf.uvs[0] = (texMtxs[0] * posIn).xy;\n"
" vtf.uvs[1] = (texMtxs[1] * posIn).xy;\n"
" vtf.uvs[2] = (texMtxs[2] * posIn).xy;\n"
"%s" // Additional TCGs here
"}\n";
static const char* FS =
"#version 330\n"
BOO_GLSL_BINDING_HEAD
"\n"
"struct Light\n"
"{\n"
" vec4 pos;\n"
" vec4 dir;\n"
" vec4 color;\n"
" vec4 linAtt;\n"
" vec4 angAtt;\n"
"};\n"
"struct Fog\n" // Reappropriated for indirect texture scaling
"{\n"
" vec4 color;\n"
" float indScale;\n"
" float start;\n"
"};\n"
"\n"
"UBINDING2 uniform LightingUniform\n"
"{\n"
" Light lights[" _XSTR(URDE_MAX_LIGHTS) "];\n"
" vec4 ambient;\n"
" vec4 kColor0;\n"
" vec4 kColor1;\n"
" vec4 kColor2;\n"
" vec4 kColor3;\n"
" Fog fog;\n"
"};\n"
"\n"
"vec4 LightingFunc(vec4 mvPosIn, vec4 mvNormIn)\n"
"{\n"
" vec4 ret = ambient;\n"
" \n"
" for (int i=0 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n"
" {\n"
" vec3 delta = mvPosIn.xyz - lights[i].pos.xyz;\n"
" float dist = length(delta);\n"
" float angDot = clamp(dot(normalize(delta), lights[i].dir.xyz), 0.0, 1.0);\n"
" float att = 1.0 / (lights[i].linAtt[2] * dist * dist +\n"
" lights[i].linAtt[1] * dist +\n"
" lights[i].linAtt[0]);\n"
" float angAtt = lights[i].angAtt[2] * angDot * angDot +\n"
" lights[i].angAtt[1] * angDot +\n"
" lights[i].angAtt[0];\n"
" ret += lights[i].color * clamp(angAtt, 0.0, 1.0) * att * clamp(dot(normalize(-delta), mvNormIn.xyz), 0.0, 1.0);\n"
" }\n"
" \n"
" return clamp(ret, vec4(0.0,0.0,0.0,0.0), vec4(1.0,1.0,1.0,1.0));\n"
"}\n"
"\n"
"struct VertToFrag\n"
"{\n"
" vec4 mvPos;\n"
" vec4 mvNorm;\n"
" vec4 mvBinorm;\n"
" vec4 mvTangent;\n"
" vec4 color;\n"
" vec2 uvs[7];\n"
"};\n"
"\n"
"SBINDING(0) in VertToFrag vtf;\n"
"layout(location=0) out vec4 colorOut;\n"
"%s" // Textures here
"void main()\n"
"{\n"
" vec4 lighting = LightingFunc(vtf.mvPos, normalize(vtf.mvNorm));\n"
"%s" // Combiner expression here
"}\n";
static void _BuildShader(std::string& finalVS, std::string& finalFS, int& nextTex, const char* texNames[7],
const SFluidPlaneShaderInfo& info)
{
std::string additionalTCGs;
std::string textures;
std::string combiner;
int nextTCG = 3;
int nextMtx = 4;
int bumpMapUv, envBumpMapUv, envMapUv, lightmapUv;
if (info.m_hasPatternTex1)
{
texNames[nextTex] = "patternTex1";
textures += hecl::Format("TBINDING%d uniform sampler2D patternTex1;\n", nextTex++);
}
if (info.m_hasPatternTex2)
{
texNames[nextTex] = "patternTex2";
textures += hecl::Format("TBINDING%d uniform sampler2D patternTex2;\n", nextTex++);
}
if (info.m_hasColorTex)
{
texNames[nextTex] = "colorTex";
textures += hecl::Format("TBINDING%d uniform sampler2D colorTex;\n", nextTex++);
}
if (info.m_hasBumpMap)
{
texNames[nextTex] = "bumpMap";
textures += hecl::Format("TBINDING%d uniform sampler2D bumpMap;\n", nextTex++);
}
if (info.m_hasEnvMap)
{
texNames[nextTex] = "envMap";
textures += hecl::Format("TBINDING%d uniform sampler2D envMap;\n", nextTex++);
}
if (info.m_hasEnvBumpMap)
{
texNames[nextTex] = "envBumpMap";
textures += hecl::Format("TBINDING%d uniform sampler2D envBumpMap;\n", nextTex++);
}
if (info.m_hasLightmap)
{
texNames[nextTex] = "lightMap";
textures += hecl::Format("TBINDING%d uniform sampler2D lightMap;\n", nextTex++);
}
if (info.m_hasBumpMap)
{
bumpMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (texMtxs[0] * posIn).xy;\n", nextTCG++);
}
if (info.m_hasEnvBumpMap)
{
envBumpMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (texMtxs[3] * posIn).xy;\n", nextTCG++);
}
if (info.m_hasEnvMap)
{
envMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (texMtxs[%d] * posIn).xy;\n", nextTCG++, nextMtx++);
}
if (info.m_hasLightmap)
{
lightmapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (texMtxs[%d] * posIn).xy;\n", nextTCG++, nextMtx++);
}
switch (info.m_type)
{
case CFluidPlane::EFluidType::NormalWater:
case CFluidPlane::EFluidType::Three:
case CFluidPlane::EFluidType::Four:
if (info.m_hasLightmap)
{
combiner += hecl::Format(" vec4 lightMapTexel = texture(lightMap, vtf.uvs[%d]);\n", lightmapUv);
// 0: Tex4TCG, Tex4, doubleLightmapBlend ? NULL : GX_COLOR1A1
// ZERO, TEX, KONST, doubleLightmapBlend ? ZERO : RAS
// Output reg 2
// KColor 2
if (info.m_doubleLightmapBlend)
{
// 1: Tex4TCG2, Tex4, GX_COLOR1A1
// C2, TEX, KONST, RAS
// Output reg 2
// KColor 3
// Tex * K2 + Lighting
combiner += " lighting += mix(lightMapTexel * kColor2, lightMapTexel, kColor3);\n";
}
else
{
// mix(Tex * K2, Tex, K3) + Lighting
combiner += " lighting += lightMapTexel * kColor2;\n";
}
}
// Next: Tex0TCG, Tex0, GX_COLOR1A1
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// Next: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// Next: Tex2TCG, Tex2, GX_COLOR1A1
// ZERO, TEX, hasTex4 ? C2 : RAS, PREV
// Output reg prev
// (Tex0 * kColor0 + Lighting) * Tex1 + VertColor + Tex2 * Lighting
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + lighting) *\n"
" texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = lighting * texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex && !info.m_hasEnvMap && info.m_hasEnvBumpMap)
{
// Make previous stage indirect, mtx0
combiner += hecl::Format(" vec2 indUvs = (texture(envBumpMap, vtf.uvs[%d]).ra - vec2(0.5, 0.5)) *\n"
" vec2(fog.indScale, -fog.indScale);", envBumpMapUv);
combiner += " colorOut += texture(colorTex, indUvs + vtf.uvs[2]) * lighting;\n";
}
else if (info.m_hasEnvMap)
{
// Next: envTCG, envTex, NULL
// PREV, TEX, KONST, ZERO
// Output reg prev
// KColor 1
// Make previous stage indirect, mtx0
if (info.m_hasColorTex)
combiner += " colorOut += texture(colorTex, vtf.uvs[2]) * lighting;\n";
combiner += hecl::Format(" vec2 indUvs = (texture(envBumpMap, vtf.uvs[%d]).ra - vec2(0.5, 0.5)) *\n"
" vec2(fog.indScale, -fog.indScale);", envBumpMapUv);
combiner += hecl::Format(" colorOut = mix(colorOut, texture(envMap, indUvs + vtf.uvs[%d]), kColor1);\n",
envMapUv);
}
else if (info.m_hasColorTex)
{
combiner += " colorOut += texture(colorTex, vtf.uvs[2]) * lighting;\n";
}
break;
case CFluidPlane::EFluidType::PoisonWater:
if (info.m_hasLightmap)
{
combiner += hecl::Format(" vec4 lightMapTexel = texture(lightMap, vtf.uvs[%d]);\n", lightmapUv);
// 0: Tex4TCG, Tex4, doubleLightmapBlend ? NULL : GX_COLOR1A1
// ZERO, TEX, KONST, doubleLightmapBlend ? ZERO : RAS
// Output reg 2
// KColor 2
if (info.m_doubleLightmapBlend)
{
// 1: Tex4TCG2, Tex4, GX_COLOR1A1
// C2, TEX, KONST, RAS
// Output reg 2
// KColor 3
// Tex * K2 + Lighting
combiner += " lighting += mix(lightMapTexel * kColor2, lightMapTexel, kColor3);\n";
}
else
{
// mix(Tex * K2, Tex, K3) + Lighting
combiner += " lighting += lightMapTexel * kColor2;\n";
}
}
// Next: Tex0TCG, Tex0, GX_COLOR1A1
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// Next: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// Next: Tex2TCG, Tex2, GX_COLOR1A1
// ZERO, TEX, hasTex4 ? C2 : RAS, PREV
// Output reg prev
// (Tex0 * kColor0 + Lighting) * Tex1 + VertColor + Tex2 * Lighting
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + lighting) *\n"
" texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = lighting * texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
{
if (info.m_hasEnvBumpMap)
{
// Make previous stage indirect, mtx0
combiner += hecl::Format(" vec2 indUvs = (texture(envBumpMap, vtf.uvs[%d]).ra - vec2(0.5, 0.5)) *\n"
" vec2(fog.indScale, -fog.indScale);", envBumpMapUv);
combiner += " colorOut += texture(colorTex, indUvs + vtf.uvs[2]) * lighting;\n";
}
else
{
combiner += " colorOut += texture(colorTex, vtf.uvs[2]) * lighting;\n";
}
}
break;
case CFluidPlane::EFluidType::Lava:
// 0: Tex0TCG, Tex0, GX_COLOR0A0
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// 1: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// 2: Tex2TCG, Tex2, NULL
// ZERO, TEX, ONE, PREV
// Output reg prev
// (Tex0 * kColor0 + VertColor) * Tex1 + VertColor + Tex2
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + vtf.color) *\n"
" texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = vtf.color * texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
combiner += " colorOut += texture(colorTex, vtf.uvs[2]);\n";
if (info.m_hasBumpMap)
{
// 3: bumpMapTCG, bumpMap, NULL
// ZERO, TEX, ONE, HALF
// Output reg 0, no clamp, no bias
// 4: bumpMapTCG2, bumpMap, NULL
// ZERO, TEX, ONE, C0
// Output reg 0, subtract, clamp, no bias
combiner += " vec3 lightVec = lights[3].pos.xyz - vtf.mvPos.xyz;\n"
" float lx = dot(vtf.mvTangent, lightVec);\n"
" float ly = dot(vtf.mvBinorm, lightVec);\n";
combiner += hecl::Format(" vec4 emboss1 = texture(bumpMap, vtf.uvs[%d]) + vec4(0.5);\n"
" vec4 emboss2 = texture(bumpMap, vtf.uvs[%d] + vec2(lx, ly));\n",
bumpMapUv, bumpMapUv);
// 5: NULL, NULL, NULL
// ZERO, PREV, C0, ZERO
// Output reg prev, scale 2, clamp
// colorOut * clamp(emboss1 + 0.5 - emboss2, 0.0, 1.0) * 2.0
combiner += "colorOut *= clamp((emboss1 + vec4(0.5) - emboss2) * vec4(2.0), vec4(0.0), vec4(1.0));\n";
}
break;
case CFluidPlane::EFluidType::Five:
// 0: Tex0TCG, Tex0, GX_COLOR0A0
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// 1: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// 2: Tex2TCG, Tex2, NULL
// ZERO, TEX, ONE, PREV
// Output reg prev
// (Tex0 * kColor0 + VertColor) * Tex1 + VertColor + Tex2
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + vtf.color) *\n"
" texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = vtf.color * texture(patternTex2, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
combiner += " colorOut += texture(colorTex, vtf.uvs[2]);\n";
if (info.m_hasBumpMap)
{
// 3: bumpMapTCG, bumpMap, NULL
// ZERO, TEX, PREV, ZERO
// Output reg prev, scale 2
combiner += hecl::Format(" vec4 emboss1 = texture(bumpMap, vtf.uvs[%d]) + vec4(0.5);\n", bumpMapUv);
combiner += "colorOut *= emboss1 * vec4(2.0);\n";
}
break;
}
combiner += " colorOut.a = kColor0.a;\n";
finalVS = hecl::Format(VS, additionalTCGs.c_str());
finalFS = hecl::Format(FS, textures.c_str(), combiner.c_str());
}
boo::IShaderPipeline*
CFluidPlaneShader::BuildShader(boo::GLDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info)
{
int nextTex = 0;
const char* texNames[7] = {};
std::string finalVS, finalFS;
_BuildShader(finalVS, finalFS, nextTex, texNames, info);
const char* uniNames[] = {"FluidPlaneUniform", "FluidPlaneUniform", "LightingUniform"};
return ctx.newShaderPipeline(finalVS.c_str(), finalFS.c_str(), size_t(nextTex), texNames, 3, uniNames,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::SrcAlpha,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::InvSrcAlpha,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false, true, false,
boo::CullMode::None);
}
#if BOO_HAS_VULKAN
static boo::IVertexFormat* s_vtxFmt = nullptr;
boo::IShaderPipeline*
CFluidPlaneShader::BuildShader(boo::VulkanDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info)
{
if (s_vtxFmt == nullptr)
{
boo::VertexElementDescriptor elements[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 0},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 1},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 2},
{nullptr, nullptr, boo::VertexSemantic::Color}
};
s_vtxFmt = ctx.newVertexFormat(5, elements);
}
int nextTex = 0;
const char* texNames[7] = {};
std::string finalVS, finalFS;
_BuildShader(finalVS, finalFS, nextTex, texNames, info);
const char* uniNames[] = {"FluidPlaneUniform", "FluidPlaneUniform", "LightingUniform"};
return ctx.newShaderPipeline(finalVS.c_str(), finalFS.c_str(), size_t(nextTex), texNames, 3, uniNames,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::SrcAlpha,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::InvSrcAlpha,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false, true, false,
boo::CullMode::None);
}
#endif
boo::IShaderDataBinding* CFluidPlaneShader::BuildBinding(boo::GLDataFactory::Context& ctx,
boo::IShaderPipeline* pipeline)
{
boo::VertexElementDescriptor elements[] =
{
{m_vbo, nullptr, boo::VertexSemantic::Position4},
{m_vbo, nullptr, boo::VertexSemantic::Normal4, 0},
{m_vbo, nullptr, boo::VertexSemantic::Normal4, 1},
{m_vbo, nullptr, boo::VertexSemantic::Normal4, 2},
{m_vbo, nullptr, boo::VertexSemantic::Color}
};
boo::IVertexFormat* vtxFmt = ctx.newVertexFormat(5, elements);
boo::IGraphicsBuffer* ubufs[] = { m_uniBuf, m_uniBuf, m_uniBuf };
boo::PipelineStage ubufStages[] = { boo::PipelineStage::Vertex, boo::PipelineStage::Vertex,
boo::PipelineStage::Fragment };
size_t ubufOffs[] = {0, 0, 768};
size_t ubufSizes[] = {768, 768, 256};
size_t texCount = 0;
boo::ITexture* texs[7] = {};
if (m_patternTex1)
texs[texCount++] = (*m_patternTex1)->GetBooTexture();
if (m_patternTex2)
texs[texCount++] = (*m_patternTex2)->GetBooTexture();
if (m_colorTex)
texs[texCount++] = (*m_colorTex)->GetBooTexture();
if (m_bumpMap)
texs[texCount++] = (*m_bumpMap)->GetBooTexture();
if (m_envMap)
texs[texCount++] = (*m_envMap)->GetBooTexture();
if (m_envBumpMap)
texs[texCount++] = (*m_envBumpMap)->GetBooTexture();
if (m_lightmap)
texs[texCount++] = (*m_lightmap)->GetBooTexture();
return ctx.newShaderDataBinding(pipeline, vtxFmt, m_vbo, nullptr, nullptr, 1, ubufs, ubufStages, ubufOffs,
ubufSizes, texCount, texs, nullptr, nullptr);
}
#if BOO_HAS_VULKAN
boo::IShaderDataBinding* CFluidPlaneShader::BuildBinding(boo::VulkanDataFactory::Context& ctx,
boo::IShaderPipeline* pipeline)
{
boo::IGraphicsBuffer* ubufs[] = { m_uniBuf, m_uniBuf, m_uniBuf };
boo::PipelineStage ubufStages[] = { boo::PipelineStage::Vertex, boo::PipelineStage::Vertex,
boo::PipelineStage::Fragment };
size_t ubufOffs[] = {0, 0, 768};
size_t ubufSizes[] = {768, 768, 256};
size_t texCount = 0;
boo::ITexture* texs[7] = {};
if (m_patternTex1)
texs[texCount++] = (*m_patternTex1)->GetBooTexture();
if (m_patternTex2)
texs[texCount++] = (*m_patternTex2)->GetBooTexture();
if (m_colorTex)
texs[texCount++] = (*m_colorTex)->GetBooTexture();
if (m_bumpMap)
texs[texCount++] = (*m_bumpMap)->GetBooTexture();
if (m_envMap)
texs[texCount++] = (*m_envMap)->GetBooTexture();
if (m_envBumpMap)
texs[texCount++] = (*m_envBumpMap)->GetBooTexture();
if (m_lightmap)
texs[texCount++] = (*m_lightmap)->GetBooTexture();
return ctx.newShaderDataBinding(pipeline, s_vtxFmt, m_vbo, nullptr, nullptr, 1, ubufs, ubufStages, ubufOffs,
ubufSizes, texCount, texs, nullptr, nullptr);
}
#endif
}

View File

@@ -0,0 +1,4 @@
//
// Created by Jack Andersen on 8/6/17.
//

View File

@@ -0,0 +1,480 @@
#include "CFluidPlaneShader.hpp"
namespace urde
{
static boo::IVertexFormat* s_vtxFmt = nullptr;
static const char* VS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertData\n"
"{\n"
" float4 posIn [[ attribute(0) ]];\n"
" float4 normalIn [[ attribute(1) ]];\n"
" float4 binormalIn [[ attribute(2) ]];\n"
" float4 tangentIn [[ attribute(3) ]];\n"
" float4 colorIn [[ attribute(4) ]];\n"
"};\n"
"\n"
"UBINDING0 uniform FluidPlaneUniform\n"
"{\n"
" float4x4 mv;\n"
" float4x4 mvNorm;\n"
" float4x4 proj;\n"
" float4x4 texMtxs[6];\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uvs[7];\n"
"};\n"
"\n"
"vertex VertToFrag vmain(VertData v [[ stage_in ]],\n"
" constant FluidPlaneUniform& fu [[ buffer(2) ]])\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.mvPos = fu.mv * float4(v.posIn.xyz, 1.0);\n"
" gl_Position = fu.proj * vtf.mvPos;\n"
" vtf.mvNorm = fu.mvNorm * v.normalIn;\n"
" vtf.mvBinorm = fu.mvNorm * v.binormalIn;\n"
" vtf.mvTangent = fu.mvNorm * v.tangentIn;\n"
" vtf.color = v.colorIn;\n"
" vtf.uvs[0] = (fu.texMtxs[0] * v.posIn).xy;\n"
" vtf.uvs[1] = (fu.texMtxs[1] * v.posIn).xy;\n"
" vtf.uvs[2] = (fu.texMtxs[2] * v.posIn).xy;\n"
"%s" // Additional TCGs here
" return vtf;\n"
"}\n";
static const char* FS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"constexpr sampler samp(address::repeat, filter::linear);\n"
"\n"
"struct Light\n"
"{\n"
" float4 pos;\n"
" float4 dir;\n"
" float4 color;\n"
" float4 linAtt;\n"
" float4 angAtt;\n"
"};\n"
"struct Fog\n" // Reappropriated for indirect texture scaling
"{\n"
" float4 color;\n"
" float indScale;\n"
" float start;\n"
"};\n"
"\n"
"struct LightingUniform\n"
"{\n"
" Light lights[" _XSTR(URDE_MAX_LIGHTS) "];\n"
" float4 ambient;\n"
" float4 kColor0;\n"
" float4 kColor1;\n"
" float4 kColor2;\n"
" float4 kColor3;\n"
" Fog fog;\n"
"};\n"
"\n"
"static float4 LightingFunc(float4 mvPosIn, float4 mvNormIn)\n"
"{\n"
" float4 ret = ambient;\n"
" \n"
" for (int i=0 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n"
" {\n"
" float3 delta = mvPosIn.xyz - lights[i].pos.xyz;\n"
" float dist = length(delta);\n"
" float angDot = clamp(dot(normalize(delta), lights[i].dir.xyz), 0.0, 1.0);\n"
" float att = 1.0 / (lights[i].linAtt[2] * dist * dist +\n"
" lights[i].linAtt[1] * dist +\n"
" lights[i].linAtt[0]);\n"
" float angAtt = lights[i].angAtt[2] * angDot * angDot +\n"
" lights[i].angAtt[1] * angDot +\n"
" lights[i].angAtt[0];\n"
" ret += lights[i].color * clamp(angAtt, 0.0, 1.0) * att * clamp(dot(normalize(-delta), mvNormIn.xyz), 0.0, 1.0);\n"
" }\n"
" \n"
" return clamp(ret, float4(0.0,0.0,0.0,0.0), float4(1.0,1.0,1.0,1.0));\n"
"}\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uvs[7];\n"
"};\n"
"\n"
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n"
" constant LightingUniform& lu [[ buffer(4) ]]%s)\n" // Textures here
"{\n"
" float4 lighting = LightingFunc(vtf.mvPos, normalize(vtf.mvNorm));\n"
" float4 colorOut;\n"
"%s" // Combiner expression here
" return colorOut;\n"
"}\n";
boo::IShaderPipeline*
CFluidPlaneShader::BuildShader(boo::MetalDataFactory::Context& ctx, const SFluidPlaneShaderInfo& info)
{
if (s_vtxFmt == nullptr)
{
boo::VertexElementDescriptor elements[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 0},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 1},
{nullptr, nullptr, boo::VertexSemantic::Normal4, 2},
{nullptr, nullptr, boo::VertexSemantic::Color}
};
s_vtxFmt = ctx.newVertexFormat(5, elements);
}
std::string additionalTCGs;
std::string textures;
std::string combiner;
int nextTex = 0;
int nextTCG = 3;
int nextMtx = 4;
int bumpMapUv, envBumpMapUv, envMapUv, lightmapUv;
if (info.m_hasPatternTex1)
textures += hecl::Format(",\ntexture2d<float> patternTex1 [[ texture(%d) ]]", nextTex++);
if (info.m_hasPatternTex2)
textures += hecl::Format(",\ntexture2d<float> patternTex2 [[ texture(%d) ]]", nextTex++);
if (info.m_hasColorTex)
textures += hecl::Format(",\ntexture2d<float> colorTex [[ texture(%d) ]]", nextTex++);
if (info.m_hasBumpMap)
textures += hecl::Format(",\ntexture2d<float> bumpMap [[ texture(%d) ]]", nextTex++);
if (info.m_hasEnvMap)
textures += hecl::Format(",\ntexture2d<float> envMap [[ texture(%d) ]]", nextTex++);
if (info.m_hasEnvBumpMap)
textures += hecl::Format(",\ntexture2d<float> envBumpMap [[ texture(%d) ]]", nextTex++);
if (info.m_hasLightmap)
textures += hecl::Format(",\ntexture2d<float> lightMap [[ texture(%d) ]]", nextTex++);
if (info.m_hasBumpMap)
{
bumpMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (fu.texMtxs[0] * v.posIn).xy;\n", nextTCG++);
}
if (info.m_hasEnvBumpMap)
{
envBumpMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (fu.texMtxs[3] * v.posIn).xy;\n", nextTCG++);
}
if (info.m_hasEnvMap)
{
envMapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (fu.texMtxs[%d] * v.posIn).xy;\n", nextTCG++, nextMtx++);
}
if (info.m_hasLightmap)
{
lightmapUv = nextTCG;
additionalTCGs += hecl::Format(" vtf.uvs[%d] = (fu.texMtxs[%d] * v.posIn).xy;\n", nextTCG++, nextMtx++);
}
switch (info.m_type)
{
case CFluidPlane::EFluidType::NormalWater:
case CFluidPlane::EFluidType::Three:
case CFluidPlane::EFluidType::Four:
if (info.m_hasLightmap)
{
combiner += hecl::Format(" float4 lightMapTexel = lightMap.sample(samp, vtf.uvs[%d]);\n", lightmapUv);
// 0: Tex4TCG, Tex4, doubleLightmapBlend ? NULL : GX_COLOR1A1
// ZERO, TEX, KONST, doubleLightmapBlend ? ZERO : RAS
// Output reg 2
// KColor 2
if (info.m_doubleLightmapBlend)
{
// 1: Tex4TCG2, Tex4, GX_COLOR1A1
// C2, TEX, KONST, RAS
// Output reg 2
// KColor 3
// Tex * K2 + Lighting
combiner += " lighting += mix(lightMapTexel * lu.kColor2, lightMapTexel, lu.kColor3);\n";
}
else
{
// mix(Tex * K2, Tex, K3) + Lighting
combiner += " lighting += lightMapTexel * lu.kColor2;\n";
}
}
// Next: Tex0TCG, Tex0, GX_COLOR1A1
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// Next: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// Next: Tex2TCG, Tex2, GX_COLOR1A1
// ZERO, TEX, hasTex4 ? C2 : RAS, PREV
// Output reg prev
// (Tex0 * kColor0 + Lighting) * Tex1 + VertColor + Tex2 * Lighting
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (patternTex1.sample(samp, vtf.uvs[0]) * lu.kColor0 + lighting) *\n"
" patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = lighting * patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex && !info.m_hasEnvMap && info.m_hasEnvBumpMap)
{
// Make previous stage indirect, mtx0
combiner += hecl::Format(" float2 indUvs = (envBumpMap.sample(samp, vtf.uvs[%d]).ra - float2(0.5, 0.5)) *\n"
" float2(lu.fog.indScale, -lu.fog.indScale);", envBumpMapUv);
combiner += " colorOut += colorTex.sample(samp, indUvs + vtf.uvs[2]) * lighting;\n";
}
else if (info.m_hasEnvMap)
{
// Next: envTCG, envTex, NULL
// PREV, TEX, KONST, ZERO
// Output reg prev
// KColor 1
// Make previous stage indirect, mtx0
if (info.m_hasColorTex)
combiner += " colorOut += colorTex.sample(samp, vtf.uvs[2]) * lighting;\n";
combiner += hecl::Format(" float2 indUvs = (envBumpMap.sample(samp, vtf.uvs[%d]).ra - float2(0.5, 0.5)) *\n"
" float2(lu.fog.indScale, -lu.fog.indScale);", envBumpMapUv);
combiner += hecl::Format(" colorOut = mix(colorOut, envMap.sample(samp, indUvs + vtf.uvs[%d]), lu.kColor1);\n",
envMapUv);
}
else if (info.m_hasColorTex)
{
combiner += " colorOut += colorTex.sample(samp, vtf.uvs[2]) * lighting;\n";
}
break;
case CFluidPlane::EFluidType::PoisonWater:
if (info.m_hasLightmap)
{
combiner += hecl::Format(" float4 lightMapTexel = lightMap.sample(samp, vtf.uvs[%d]);\n", lightmapUv);
// 0: Tex4TCG, Tex4, doubleLightmapBlend ? NULL : GX_COLOR1A1
// ZERO, TEX, KONST, doubleLightmapBlend ? ZERO : RAS
// Output reg 2
// KColor 2
if (info.m_doubleLightmapBlend)
{
// 1: Tex4TCG2, Tex4, GX_COLOR1A1
// C2, TEX, KONST, RAS
// Output reg 2
// KColor 3
// Tex * K2 + Lighting
combiner += " lighting += mix(lightMapTexel * lu.kColor2, lightMapTexel, lu.kColor3);\n";
}
else
{
// mix(Tex * K2, Tex, K3) + Lighting
combiner += " lighting += lightMapTexel * lu.kColor2;\n";
}
}
// Next: Tex0TCG, Tex0, GX_COLOR1A1
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// Next: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// Next: Tex2TCG, Tex2, GX_COLOR1A1
// ZERO, TEX, hasTex4 ? C2 : RAS, PREV
// Output reg prev
// (Tex0 * kColor0 + Lighting) * Tex1 + VertColor + Tex2 * Lighting
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (patternTex1.sample(samp, vtf.uvs[0]) * lu.kColor0 + lighting) *\n"
" patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = lighting * patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
{
if (info.m_hasEnvBumpMap)
{
// Make previous stage indirect, mtx0
combiner += hecl::Format(" float2 indUvs = (envBumpMap.sample(samp, vtf.uvs[%d]).ra - float2(0.5, 0.5)) *\n"
" float2(lu.fog.indScale, -lu.fog.indScale);", envBumpMapUv);
combiner += " colorOut += colorTex.sample(samp, indUvs + vtf.uvs[2]) * lighting;\n";
}
else
{
combiner += " colorOut += colorTex.sample(samp, vtf.uvs[2]) * lighting;\n";
}
}
break;
case CFluidPlane::EFluidType::Lava:
// 0: Tex0TCG, Tex0, GX_COLOR0A0
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// 1: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// 2: Tex2TCG, Tex2, NULL
// ZERO, TEX, ONE, PREV
// Output reg prev
// (Tex0 * kColor0 + VertColor) * Tex1 + VertColor + Tex2
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (patternTex1.sample(samp, vtf.uvs[0]) * lu.kColor0 + vtf.color) *\n"
" patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = vtf.color * patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
combiner += " colorOut += colorTex.sample(samp, vtf.uvs[2]);\n";
if (info.m_hasBumpMap)
{
// 3: bumpMapTCG, bumpMap, NULL
// ZERO, TEX, ONE, HALF
// Output reg 0, no clamp, no bias
// 4: bumpMapTCG2, bumpMap, NULL
// ZERO, TEX, ONE, C0
// Output reg 0, subtract, clamp, no bias
combiner += " float3 lightVec = lights[3].pos.xyz - vtf.mvPos.xyz;\n"
" float lx = dot(vtf.mvTangent.xyz, lightVec);\n"
" float ly = dot(vtf.mvBinorm.xyz, lightVec);\n";
combiner += hecl::Format(" float4 emboss1 = bumpMap.sample(samp, vtf.uvs[%d]) + float4(0.5);\n"
" float4 emboss2 = bumpMap.sample(samp, vtf.uvs[%d] + float2(lx, ly));\n",
bumpMapUv, bumpMapUv);
// 5: NULL, NULL, NULL
// ZERO, PREV, C0, ZERO
// Output reg prev, scale 2, clamp
// colorOut * clamp(emboss1 + 0.5 - emboss2, 0.0, 1.0) * 2.0
combiner += "colorOut *= clamp((emboss1 + float4(0.5) - emboss2) * float4(2.0), float4(0.0), float4(1.0));\n";
}
break;
case CFluidPlane::EFluidType::Five:
// 0: Tex0TCG, Tex0, GX_COLOR0A0
// ZERO, TEX, KONST, RAS
// Output reg prev
// KColor 0
// 1: Tex1TCG, Tex1, GX_COLOR0A0
// ZERO, TEX, PREV, RAS
// Output reg prev
// 2: Tex2TCG, Tex2, NULL
// ZERO, TEX, ONE, PREV
// Output reg prev
// (Tex0 * kColor0 + VertColor) * Tex1 + VertColor + Tex2
if (info.m_hasPatternTex2)
{
if (info.m_hasPatternTex1)
combiner += " colorOut = (patternTex1.sample(samp, vtf.uvs[0]) * lu.kColor0 + vtf.color) *\n"
" patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
else
combiner += " colorOut = vtf.color * patternTex2.sample(samp, vtf.uvs[1]) + vtf.color;\n";
}
else
{
combiner += " colorOut = vtf.color;\n";
}
if (info.m_hasColorTex)
combiner += " colorOut += colorTex.sample(samp, vtf.uvs[2]);\n";
if (info.m_hasBumpMap)
{
// 3: bumpMapTCG, bumpMap, NULL
// ZERO, TEX, PREV, ZERO
// Output reg prev, scale 2
combiner += hecl::Format(" float4 emboss1 = bumpMap.sample(samp, vtf.uvs[%d]) + float4(0.5);\n", bumpMapUv);
combiner += "colorOut *= emboss1 * float4(2.0);\n";
}
break;
}
combiner += " colorOut.a = kColor0.a;\n";
std::string finalVS = hecl::Format(VS, additionalTCGs.c_str());
std::string finalFS = hecl::Format(FS, textures.c_str(), combiner.c_str());
return ctx.newShaderPipeline(finalVS.c_str(), finalFS.c_str(), s_vtxFmt, CGraphics::g_ViewportSamples,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::SrcAlpha,
info.m_additive ? boo::BlendFactor::One : boo::BlendFactor::InvSrcAlpha,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false, true, false,
boo::CullMode::None);
}
boo::IShaderDataBinding* CFluidPlaneShader::BuildBinding(boo::MetalDataFactory::Context& ctx,
boo::IShaderPipeline* pipeline)
{
boo::IGraphicsBuffer* ubufs[] = { m_uniBuf, m_uniBuf, m_uniBuf };
boo::PipelineStage ubufStages[] = { boo::PipelineStage::Vertex, boo::PipelineStage::Vertex,
boo::PipelineStage::Fragment };
size_t ubufOffs[] = {0, 0, 768};
size_t ubufSizes[] = {768, 768, 256};
size_t texCount = 0;
boo::ITexture* texs[7] = {};
if (m_patternTex1)
texs[texCount++] = (*m_patternTex1)->GetBooTexture();
if (m_patternTex2)
texs[texCount++] = (*m_patternTex2)->GetBooTexture();
if (m_colorTex)
texs[texCount++] = (*m_colorTex)->GetBooTexture();
if (m_bumpMap)
texs[texCount++] = (*m_bumpMap)->GetBooTexture();
if (m_envMap)
texs[texCount++] = (*m_envMap)->GetBooTexture();
if (m_envBumpMap)
texs[texCount++] = (*m_envBumpMap)->GetBooTexture();
if (m_lightmap)
texs[texCount++] = (*m_lightmap)->GetBooTexture();
return ctx.newShaderDataBinding(pipeline, s_vtxFmt, m_vbo, nullptr, nullptr, 1, ubufs, ubufStages, ubufOffs,
ubufSizes, texCount, texs, nullptr, nullptr);
}
}

View File

@@ -1,10 +1,58 @@
#include "CModelShaders.hpp"
#include "Graphics/CLight.hpp"
namespace urde
{
std::experimental::optional<CModelShaders> CModelShaders::g_ModelShaders;
void CModelShaders::LightingUniform::ActivateLights(const std::vector<CLight>& lts)
{
ambient = zeus::CColor::skBlack;
size_t curLight = 0;
for (const CLight& light : lts)
{
switch (light.GetType())
{
case ELightType::LocalAmbient:
ambient += light.GetColor();
break;
case ELightType::Point:
case ELightType::Spot:
case ELightType::Custom:
case ELightType::Directional:
{
if (curLight >= URDE_MAX_LIGHTS)
continue;
CModelShaders::Light& lightOut = lights[curLight++];
lightOut.pos = CGraphics::g_CameraMatrix * light.GetPosition();
lightOut.dir = CGraphics::g_CameraMatrix.basis * light.GetDirection();
lightOut.dir.normalize();
lightOut.color = light.GetColor();
lightOut.linAtt[0] = light.GetAttenuationConstant();
lightOut.linAtt[1] = light.GetAttenuationLinear();
lightOut.linAtt[2] = light.GetAttenuationQuadratic();
lightOut.angAtt[0] = light.GetAngleAttenuationConstant();
lightOut.angAtt[1] = light.GetAngleAttenuationLinear();
lightOut.angAtt[2] = light.GetAngleAttenuationQuadratic();
if (light.GetType() == ELightType::Directional)
lightOut.pos = (-lightOut.dir) * 1048576.f;
break;
}
}
}
for (; curLight<URDE_MAX_LIGHTS ; ++curLight)
{
CModelShaders::Light& lightOut = lights[curLight];
lightOut.color = zeus::CColor::skClear;
lightOut.linAtt[0] = 1.f;
lightOut.angAtt[0] = 1.f;
}
}
hecl::Runtime::ShaderCacheExtensions
CModelShaders::GetShaderExtensions(boo::IGraphicsDataFactory::Platform plat)
{

View File

@@ -7,7 +7,7 @@
#include "zeus/CColor.hpp"
#include "Graphics/CGraphics.hpp"
#define URDE_MAX_LIGHTS 16
#define URDE_MAX_LIGHTS 8
namespace urde
{
@@ -55,6 +55,8 @@ public:
zeus::CColor colorRegs[3];
zeus::CColor mulColor;
CGraphics::CFogState fog;
void ActivateLights(const std::vector<CLight>& lts);
};
struct ThermalUniform