metaforce/Shaders/shader_CFluidPlaneShaderGLS...

783 lines
28 KiB
C++
Raw Normal View History

2018-10-07 02:59:17 +00:00
#include "shader_CFluidPlaneShader.hpp"
2019-07-20 04:27:21 +00:00
#include <fmt/ostream.h>
#include <sstream>
2017-08-08 06:03:57 +00:00
2019-03-03 06:19:42 +00:00
#define FOG_STRUCT_GLSL \
"struct Fog\n" \
"{\n" \
" vec4 color;\n" \
" float A;\n" \
" float B;\n" \
" float C;\n" \
2019-03-03 23:04:18 +00:00
" int mode;\n" \
2019-03-03 06:19:42 +00:00
" float indScale;\n" \
"};\n"
#define FOG_ALGORITHM_GLSL \
"vec4 MainPostFunc(vec4 colorIn)\n" \
"{\n" \
" float fogZ;\n" \
" float fogF = clamp((fog.A / (fog.B - gl_FragCoord.z)) - fog.C, 0.0, 1.0);\n" \
" switch (fog.mode)\n" \
" {\n" \
" case 2:\n" \
" fogZ = fogF;\n" \
" break;\n" \
" case 4:\n" \
" fogZ = 1.0 - exp2(-8.0 * fogF);\n" \
" break;\n" \
" case 5:\n" \
" fogZ = 1.0 - exp2(-8.0 * fogF * fogF);\n" \
" break;\n" \
" case 6:\n" \
" fogZ = exp2(-8.0 * (1.0 - fogF));\n" \
" break;\n" \
" case 7:\n" \
" fogF = 1.0 - fogF;\n" \
" fogZ = exp2(-8.0 * fogF * fogF);\n" \
" break;\n" \
" default:\n" \
" fogZ = 0.0;\n" \
" break;\n" \
" }\n" \
2019-07-20 04:27:21 +00:00
"#if IS_ADDITIVE\n" \
2019-03-03 06:19:42 +00:00
" return vec4(mix(colorIn, vec4(0.0), clamp(fogZ, 0.0, 1.0)).rgb, colorIn.a);\n" \
"#else\n" \
" return vec4(mix(colorIn, fog.color, clamp(fogZ, 0.0, 1.0)).rgb, colorIn.a);\n" \
"#endif\n" \
"}\n"
2017-08-08 06:03:57 +00:00
static const char* VS =
2018-12-08 05:30:43 +00:00
"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"
" vec4 pos = vec4(posIn.xyz, 1.0);\n"
" vtf.mvPos = mv * pos;\n"
" gl_Position = proj * vtf.mvPos;\n"
" vtf.mvNorm = mvNorm * normalIn;\n"
" vtf.mvBinorm = mvNorm * binormalIn;\n"
" vtf.mvTangent = mvNorm * tangentIn;\n"
" vtf.color = vec4(colorIn.xyz, 1.0);\n"
" vtf.uvs[0] = (texMtxs[0] * pos).xy;\n"
" vtf.uvs[1] = (texMtxs[1] * pos).xy;\n"
" vtf.uvs[2] = (texMtxs[2] * pos).xy;\n"
2019-07-20 04:27:21 +00:00
" ADDITIONAL_TCGS\n" // Additional TCGs here
2018-12-08 05:30:43 +00:00
"}\n";
2017-08-08 06:03:57 +00:00
static const char* TessVS =
2018-12-08 05:30:43 +00:00
"layout(location=0) in vec4 posIn;\n"
"layout(location=1) in vec4 outerLevelsIn;\n"
"layout(location=2) in vec2 innerLevelsIn;\n"
"\n"
"struct VertToControl\n"
"{\n"
" vec4 minMaxPos;\n"
" vec4 outerLevels;\n"
" vec2 innerLevels;\n"
"};\n"
"\n"
"SBINDING(0) out VertToControl vtc;\n"
"\n"
"void main()\n"
"{\n"
" vtc.minMaxPos = posIn;\n"
" vtc.outerLevels = outerLevelsIn;\n"
" vtc.innerLevels = innerLevelsIn;\n"
"}\n";
static const char* TessCS =
2018-12-08 05:30:43 +00:00
"#extension GL_ARB_tessellation_shader: enable\n"
"layout(vertices = 1) out;\n"
"\n"
"struct VertToControl\n"
"{\n"
" vec4 minMaxPos;\n"
" vec4 outerLevels;\n"
" vec2 innerLevels;\n"
"};\n"
"\n"
"SBINDING(0) in VertToControl vtc[];\n"
"SBINDING(0) patch out vec4 minMaxPos;\n"
"\n"
"void main()\n"
"{\n"
" minMaxPos = vtc[gl_InvocationID].minMaxPos;\n"
" for (int i=0 ; i<4 ; ++i)\n"
" gl_TessLevelOuter[i] = vtc[gl_InvocationID].outerLevels[i];\n"
" for (int i=0 ; i<2 ; ++i)\n"
" gl_TessLevelInner[i] = vtc[gl_InvocationID].innerLevels[i];\n"
"}";
static const char* TessES =
2018-12-08 05:30:43 +00:00
"#extension GL_ARB_tessellation_shader: enable\n"
"layout(quads, equal_spacing) in;\n"
"\n"
"struct Ripple\n"
"{\n"
" vec4 center; // time, distFalloff\n"
" vec4 params; // amplitude, lookupPhase, lookupTime\n"
"};\n"
"\n"
"UBINDING0 uniform FluidPlaneUniform\n"
"{\n"
" mat4 mv;\n"
" mat4 mvNorm;\n"
" mat4 proj;\n"
" mat4 texMtxs[6];\n"
" Ripple ripples[20];\n"
" vec4 colorMul;\n"
" float rippleNormResolution;\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) patch in vec4 minMaxPos;\n"
"SBINDING(0) out VertToFrag vtf;\n"
"\n"
2019-07-21 08:42:52 +00:00
"RIPPLE_TEXTURE_BINDING uniform sampler2D RippleMap;\n"
2018-12-08 05:30:43 +00:00
"\n"
"const float PI_X2 = 6.283185307179586;\n"
"\n"
"void ApplyRipple(in Ripple ripple, in vec2 pos, inout float height)\n"
"{\n"
" float dist = length(ripple.center.xy - pos);\n"
" float rippleV = textureLod(RippleMap, vec2(dist * ripple.center.w, ripple.center.z), 0.0).r;\n"
" height += rippleV * ripple.params.x * sin((dist * ripple.params.y + ripple.params.z) * PI_X2);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
" vec2 posIn = vec2(mix(minMaxPos.x, minMaxPos.z, gl_TessCoord.x),\n"
" mix(minMaxPos.y, minMaxPos.w, gl_TessCoord.y));\n"
" float height = 0.0;\n"
" float upHeight = 0.0;\n"
" float downHeight = 0.0;\n"
" float rightHeight = 0.0;\n"
" float leftHeight = 0.0;\n"
" for (int i=0 ; i<20 ; ++i)\n"
" {\n"
" ApplyRipple(ripples[i], posIn, height);\n"
" ApplyRipple(ripples[i], posIn + vec2(0.0, rippleNormResolution), upHeight);\n"
" ApplyRipple(ripples[i], posIn - vec2(0.0, rippleNormResolution), downHeight);\n"
" ApplyRipple(ripples[i], posIn + vec2(rippleNormResolution, 0.0), rightHeight);\n"
" ApplyRipple(ripples[i], posIn - vec2(rippleNormResolution, 0.0), leftHeight);\n"
" }\n"
" vec4 normalIn = vec4(normalize(vec3((leftHeight - rightHeight),\n"
" (downHeight - upHeight),\n"
" rippleNormResolution)), 1.0);\n"
" vec4 binormalIn = vec4(normalIn.x, normalIn.z, -normalIn.y, 1.0);\n"
" vec4 tangentIn = vec4(normalIn.z, normalIn.y, -normalIn.x, 1.0);\n"
" vec4 pos = vec4(posIn, height, 1.0);\n"
" vtf.mvPos = mv * pos;\n"
" gl_Position = proj * vtf.mvPos;\n"
" vtf.mvNorm = mvNorm * normalIn;\n"
" vtf.mvBinorm = mvNorm * binormalIn;\n"
" vtf.mvTangent = mvNorm * tangentIn;\n"
" vtf.color = max(height, 0.0) * colorMul;\n"
" vtf.color.a = 1.0;\n"
" vtf.uvs[0] = (texMtxs[0] * pos).xy;\n"
" vtf.uvs[1] = (texMtxs[1] * pos).xy;\n"
" vtf.uvs[2] = (texMtxs[2] * pos).xy;\n"
2019-07-20 04:27:21 +00:00
" ADDITIONAL_TCGS\n" // Additional TCGs here
2018-12-08 05:30:43 +00:00
"}\n";
2017-08-08 06:03:57 +00:00
static const char* FS =
"struct Light\n"
"{\n"
" vec4 pos;\n"
" vec4 dir;\n"
" vec4 color;\n"
" vec4 linAtt;\n"
" vec4 angAtt;\n"
"};\n"
2019-03-03 06:19:42 +00:00
FOG_STRUCT_GLSL
2017-08-08 06:03:57 +00:00
"\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"
2019-02-08 07:56:54 +00:00
" vec4 addColor;\n"
2017-08-08 06:03:57 +00:00
" Fog fog;\n"
"};\n"
"\n"
"vec4 LightingFunc(vec3 mvPosIn, vec3 mvNormIn)\n"
2017-08-08 06:03:57 +00:00
"{\n"
" vec4 ret = ambient;\n"
" \n"
" for (int i=0 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n"
" {\n"
" vec3 delta = mvPosIn - lights[i].pos.xyz;\n"
2017-08-08 06:03:57 +00:00
" float dist = length(delta);\n"
2019-01-29 20:14:34 +00:00
" vec3 deltaNorm = delta / dist;\n"
" float angDot = max(dot(deltaNorm, lights[i].dir.xyz), 0.0);\n"
2017-08-08 06:03:57 +00:00
" 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"
2019-01-29 20:14:34 +00:00
" ret += lights[i].color * angAtt * att * max(dot(-deltaNorm, mvNormIn), 0.0);\n"
2017-08-08 06:03:57 +00:00
" }\n"
" \n"
2019-01-29 20:14:34 +00:00
" return clamp(ret, 0.0, 1.0);\n"
2017-08-08 06:03:57 +00:00
"}\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"
2019-03-03 06:19:42 +00:00
FOG_ALGORITHM_GLSL
2018-10-21 04:14:00 +00:00
"\n"
2017-08-08 06:03:57 +00:00
"layout(location=0) out vec4 colorOut;\n"
2019-07-20 04:27:21 +00:00
"TEXTURE_DECLS\n" // Textures here
2017-08-08 06:03:57 +00:00
"void main()\n"
"{\n"
" vec4 lighting = LightingFunc(vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz));\n"
2019-07-20 04:27:21 +00:00
" COMBINER_EXPRS\n" // Combiner expression here
2018-10-21 04:14:00 +00:00
" colorOut = MainPostFunc(colorOut);\n"
2017-08-08 06:03:57 +00:00
"}\n";
static const char* FSDoor =
"\n"
"struct Light\n"
"{\n"
" vec4 pos;\n"
" vec4 dir;\n"
" vec4 color;\n"
" vec4 linAtt;\n"
" vec4 angAtt;\n"
"};\n"
2019-03-03 06:19:42 +00:00
FOG_STRUCT_GLSL
"\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"
2019-02-08 07:56:54 +00:00
" vec4 addColor;\n"
" Fog fog;\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"
2019-03-03 06:19:42 +00:00
FOG_ALGORITHM_GLSL
"\n"
"SBINDING(0) in VertToFrag vtf;\n"
"layout(location=0) out vec4 colorOut;\n"
2019-07-20 04:27:21 +00:00
"TEXTURE_DECLS\n" // Textures here
"void main()\n"
"{\n"
2019-07-20 04:27:21 +00:00
" COMBINER_EXPRS\n" // Combiner expression here
2019-03-03 06:19:42 +00:00
" colorOut = MainPostFunc(colorOut);\n"
"}\n";
2018-12-08 05:30:43 +00:00
static std::string _BuildFS(const SFluidPlaneShaderInfo& info) {
2019-07-20 04:27:21 +00:00
std::stringstream out;
2018-12-08 05:30:43 +00:00
int nextTex = 0;
int nextTCG = 3;
int bumpMapUv, envBumpMapUv, envMapUv, lightmapUv;
2019-07-20 04:27:21 +00:00
out << "#define TEXTURE_DECLS ";
if (info.m_hasPatternTex1)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D patternTex1;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasPatternTex2)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D patternTex2;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasColorTex)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D colorTex;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D bumpMap;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D envMap;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D envBumpMap;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasLightmap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D lightMap;"), nextTex++);
2019-07-20 04:27:21 +00:00
out << '\n';
2018-12-08 05:30:43 +00:00
2019-07-20 04:27:21 +00:00
if (info.m_hasBumpMap)
2018-12-08 05:30:43 +00:00
bumpMapUv = nextTCG++;
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvBumpMap)
2018-12-08 05:30:43 +00:00
envBumpMapUv = nextTCG++;
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvMap)
2018-12-08 05:30:43 +00:00
envMapUv = nextTCG++;
2019-07-20 04:27:21 +00:00
if (info.m_hasLightmap)
2018-12-08 05:30:43 +00:00
lightmapUv = nextTCG;
2019-07-20 04:27:21 +00:00
out << "#define COMBINER_EXPRS ";
2018-12-08 05:30:43 +00:00
switch (info.m_type) {
case EFluidType::NormalWater:
case EFluidType::PhazonFluid:
case EFluidType::Four:
if (info.m_hasLightmap) {
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vec4 lightMapTexel = texture(lightMap, vtf.uvs[{}]);"), lightmapUv);
2018-12-08 05:30:43 +00:00
// 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
2019-07-20 04:27:21 +00:00
out << "lighting += mix(lightMapTexel * kColor2, lightMapTexel, kColor3);";
2018-12-08 05:30:43 +00:00
} else {
// mix(Tex * K2, Tex, K3) + Lighting
2019-07-20 04:27:21 +00:00
out << "lighting += lightMapTexel * kColor2;";
2018-12-08 05:30:43 +00:00
}
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
// 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)
2019-07-20 04:27:21 +00:00
out <<
"colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + lighting) * "
"texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = lighting * texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex && !info.m_hasEnvMap && info.m_hasEnvBumpMap) {
// Make previous stage indirect, mtx0
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"vec2 indUvs = (texture(envBumpMap, vtf.uvs[{}]).ra - vec2(0.5, 0.5)) * "
"vec2(fog.indScale, -fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, indUvs + vtf.uvs[2]) * lighting;";
2018-12-08 05:30:43 +00:00
} 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)
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]) * lighting;";
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"vec2 indUvs = (texture(envBumpMap, vtf.uvs[{}]).ra - vec2(0.5, 0.5)) * "
"vec2(fog.indScale, -fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("colorOut = mix(colorOut, texture(envMap, indUvs + vtf.uvs[{}]), kColor1);"), envMapUv);
2018-12-08 05:30:43 +00:00
} else if (info.m_hasColorTex) {
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]) * lighting;";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
break;
case EFluidType::PoisonWater:
if (info.m_hasLightmap) {
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vec4 lightMapTexel = texture(lightMap, vtf.uvs[{}]);"), lightmapUv);
2018-12-08 05:30:43 +00:00
// 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
2019-07-20 04:27:21 +00:00
out << "lighting += mix(lightMapTexel * kColor2, lightMapTexel, kColor3);";
2018-12-08 05:30:43 +00:00
} else {
// mix(Tex * K2, Tex, K3) + Lighting
2019-07-20 04:27:21 +00:00
out << "lighting += lightMapTexel * kColor2;";
2018-12-08 05:30:43 +00:00
}
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
// 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)
2019-07-20 04:27:21 +00:00
out <<
"colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + lighting) * "
"texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = lighting * texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex) {
if (info.m_hasEnvBumpMap) {
// Make previous stage indirect, mtx0
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"vec2 indUvs = (texture(envBumpMap, vtf.uvs[{}]).ra - vec2(0.5, 0.5)) * "
"vec2(fog.indScale, -fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, indUvs + vtf.uvs[2]) * lighting;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]) * lighting;";
2018-12-08 05:30:43 +00:00
}
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
break;
case 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)
2019-07-20 04:27:21 +00:00
out <<
"colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + vtf.color) * "
"texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color * texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]);";
2018-12-08 05:30:43 +00:00
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
2019-07-20 04:27:21 +00:00
out <<
"vec3 lightVec = lights[3].pos.xyz - vtf.mvPos.xyz;"
"float lx = dot(vtf.mvTangent.xyz, lightVec);"
"float ly = dot(vtf.mvBinorm.xyz, lightVec);";
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"vec4 emboss1 = texture(bumpMap, vtf.uvs[{}]) + vec4(0.5);"
"vec4 emboss2 = texture(bumpMap, vtf.uvs[{}] + vec2(lx, ly));"),
2018-12-08 05:30:43 +00:00
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
2019-07-20 04:27:21 +00:00
out << "colorOut *= clamp((emboss1 + vec4(0.5) - emboss2) * vec4(2.0), vec4(0.0), vec4(1.0));";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
break;
case EFluidType::ThickLava:
// 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)
2019-07-20 04:27:21 +00:00
out <<
"colorOut = (texture(patternTex1, vtf.uvs[0]) * kColor0 + vtf.color) * "
"texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color * texture(patternTex2, vtf.uvs[1]) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]);";
2018-12-08 05:30:43 +00:00
if (info.m_hasBumpMap) {
// 3: bumpMapTCG, bumpMap, NULL
// ZERO, TEX, PREV, ZERO
// Output reg prev, scale 2
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vec4 emboss1 = texture(bumpMap, vtf.uvs[{}]) + vec4(0.5);"), bumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut *= emboss1 * vec4(2.0);";
2018-12-08 05:30:43 +00:00
}
2017-08-08 06:03:57 +00:00
2018-12-08 05:30:43 +00:00
break;
}
2019-07-20 04:27:21 +00:00
out << "colorOut.a = kColor0.a;\n";
2019-07-20 04:27:21 +00:00
out << "#define IS_ADDITIVE " << int(info.m_additive) << '\n';
out << FS;
return out.str();
2018-12-08 05:30:43 +00:00
}
2019-07-20 04:27:21 +00:00
static void _BuildAdditionalTCGs(std::stringstream& out, const SFluidPlaneShaderInfo& info) {
2018-12-08 05:30:43 +00:00
int nextTCG = 3;
int nextMtx = 4;
2019-07-21 08:42:52 +00:00
out << "#define ADDITIONAL_TCGS ";
2019-07-20 04:27:21 +00:00
if (info.m_hasBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vtf.uvs[{}] = (texMtxs[0] * pos).xy;"), nextTCG++);
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vtf.uvs[{}] = (texMtxs[3] * vec4(normalIn.xyz, 1.0)).xy;"), nextTCG++);
2019-07-20 04:27:21 +00:00
if (info.m_hasEnvMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vtf.uvs[{}] = (texMtxs[{}] * pos).xy;"), nextTCG++, nextMtx++);
2019-07-20 04:27:21 +00:00
if (info.m_hasLightmap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vtf.uvs[{}] = (texMtxs[{}] * pos).xy;"), nextTCG, nextMtx);
2019-07-20 04:27:21 +00:00
out << '\n';
}
2017-11-24 08:23:28 +00:00
2018-12-08 05:30:43 +00:00
static std::string _BuildVS(const SFluidPlaneShaderInfo& info, bool tessellation) {
if (tessellation)
return TessVS;
2018-10-07 02:59:17 +00:00
2019-07-20 04:27:21 +00:00
std::stringstream out;
_BuildAdditionalTCGs(out, info);
out << VS;
return out.str();
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildVS(in, tessellation);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildVS(in, tessellation);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::NX, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildVS(in, tessellation);
2018-10-07 02:59:17 +00:00
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildFS(in);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildFS(in);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::NX, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildFS(in);
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Control>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return TessCS;
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Control>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return TessCS;
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::NX, hecl::PipelineStage::Control>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return TessCS;
2018-10-07 02:59:17 +00:00
}
2018-12-08 05:30:43 +00:00
static std::string BuildES(const SFluidPlaneShaderInfo& info) {
int nextTex = 0;
if (info.m_hasPatternTex1)
nextTex++;
if (info.m_hasPatternTex2)
nextTex++;
if (info.m_hasColorTex)
nextTex++;
if (info.m_hasBumpMap)
nextTex++;
if (info.m_hasEnvMap)
nextTex++;
if (info.m_hasEnvBumpMap)
nextTex++;
if (info.m_hasLightmap)
nextTex++;
2019-07-20 04:27:21 +00:00
std::stringstream out;
_BuildAdditionalTCGs(out, info);
2019-07-21 08:42:52 +00:00
out << "#define RIPPLE_TEXTURE_BINDING TBINDING" << nextTex << '\n';
2019-07-20 04:27:21 +00:00
out << TessES;
return out.str();
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Evaluation>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return BuildES(in);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Evaluation>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return BuildES(in);
2018-10-07 02:59:17 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::NX, hecl::PipelineStage::Evaluation>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return BuildES(in);
2017-08-08 06:03:57 +00:00
}
2018-12-08 05:30:43 +00:00
static std::string _BuildVS(const SFluidPlaneDoorShaderInfo& info) {
2019-07-20 04:27:21 +00:00
std::stringstream out;
out << "#define ADDITIONAL_TCGS\n";
out << VS;
return out.str();
2018-10-07 02:59:17 +00:00
}
2018-12-08 05:30:43 +00:00
static std::string _BuildFS(const SFluidPlaneDoorShaderInfo& info) {
int nextTex = 0;
2019-07-20 04:27:21 +00:00
std::stringstream out;
2018-12-08 05:30:43 +00:00
2019-07-21 08:42:52 +00:00
out << "#define TEXTURE_DECLS ";
2019-07-20 04:27:21 +00:00
if (info.m_hasPatternTex1)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D patternTex1;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasPatternTex2)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D patternTex2;"), nextTex++);
2019-07-20 04:27:21 +00:00
if (info.m_hasColorTex)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("TBINDING{} uniform sampler2D colorTex;"), nextTex++);
2019-07-20 04:27:21 +00:00
out << '\n';
2018-12-08 05:30:43 +00:00
// Tex0 * kColor0 * Tex1 + Tex2
2019-07-20 04:27:21 +00:00
out << "#define COMBINER_EXPRS ";
2018-12-08 05:30:43 +00:00
if (info.m_hasPatternTex1 && info.m_hasPatternTex2) {
2019-07-20 04:27:21 +00:00
out <<
"colorOut = texture(patternTex1, vtf.uvs[0]) * kColor0 * "
"texture(patternTex2, vtf.uvs[1]);";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vec4(0.0);";
2018-12-08 05:30:43 +00:00
}
if (info.m_hasColorTex) {
2019-07-20 04:27:21 +00:00
out << "colorOut += texture(colorTex, vtf.uvs[2]);";
2018-12-08 05:30:43 +00:00
}
2019-07-20 04:27:21 +00:00
out << "colorOut.a = kColor0.a;\n";
2018-12-08 05:30:43 +00:00
2019-07-20 04:27:21 +00:00
out << "#define IS_ADDITIVE 0\n";
out << FSDoor;
return out.str();
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildVS(in);
}
2018-05-20 22:38:56 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildVS(in);
2017-08-08 06:03:57 +00:00
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::NX, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildVS(in);
}
2018-05-20 22:38:56 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::OpenGL, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildFS(in);
2018-05-20 22:38:56 +00:00
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::Vulkan, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildFS(in);
2017-08-08 06:03:57 +00:00
}
2018-10-07 02:59:17 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::NX, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildFS(in);
2017-08-08 06:43:27 +00:00
}