metaforce/Shaders/shader_CFluidPlaneShaderMet...

760 lines
28 KiB
C++
Raw Normal View History

2018-10-16 03:17:02 +00:00
#include "shader_CFluidPlaneShader.hpp"
2019-07-20 04:27:21 +00:00
#include <fmt/ostream.h>
#include <sstream>
2018-10-16 03:17:02 +00:00
2019-03-03 06:19:42 +00:00
#define FOG_STRUCT_METAL \
"struct Fog\n" \
"{\n" \
" float4 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_METAL \
"static float4 MainPostFunc(thread VertToFrag& vtf, constant LightingUniform& lu, float4 colorIn)\n" \
"{\n" \
" float fogZ;\n" \
2019-07-20 04:27:21 +00:00
" float fogF = saturate((lu.fog.A / (lu.fog.B - (1.0 - vtf.pos.z))) - lu.fog.C);\n" \
2019-03-03 06:19:42 +00:00
" switch (lu.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 float4(mix(colorIn, float4(0.0), saturate(fogZ)).rgb, colorIn.a);\n" \
"#else\n" \
2019-07-20 04:27:21 +00:00
" return float4(mix(colorIn, lu.fog.color, saturate(fogZ)).rgb, colorIn.a);\n" \
2019-03-03 06:19:42 +00:00
"#endif\n" \
"}\n"
2018-10-16 03:17:02 +00:00
static const char* VS =
2018-12-08 05:30:43 +00:00
"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"
"struct FluidPlaneUniform\n"
"{\n"
" float4x4 mv;\n"
" float4x4 mvNorm;\n"
" float4x4 proj;\n"
" float4x4 texMtxs[6];\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float2 uv6;\n"
"};\n"
"\n"
"vertex VertToFrag vmain(VertData v [[ stage_in ]],\n"
" constant FluidPlaneUniform& fu [[ buffer(2) ]])\n"
"{\n"
" VertToFrag vtf;\n"
" float4 pos = float4(v.posIn.xyz, 1.0);\n"
" float4 normalIn = v.normalIn;\n"
" vtf.mvPos = fu.mv * pos;\n"
" vtf.pos = 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.uv0 = (fu.texMtxs[0] * pos).xy;\n"
" vtf.uv1 = (fu.texMtxs[1] * pos).xy;\n"
" vtf.uv2 = (fu.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
" return vtf;\n"
"}\n";
2018-10-16 03:17:02 +00:00
static const char* TessCS =
2018-12-08 05:30:43 +00:00
"struct VertData\n"
"{\n"
" float4 minMaxPos [[ attribute(0) ]];\n"
" float4 outerLevelsIn [[ attribute(1) ]];\n"
" float2 innerLevelsIn [[ attribute(2) ]];\n"
"};\n"
"\n"
"struct KernelPatchInfo {\n"
" uint numPatches; // total number of patches to process.\n"
" // we need this because this value may\n"
" // not be a multiple of threadgroup size.\n"
" ushort numPatchesInThreadGroup; // number of patches processed by a\n"
" // thread-group\n"
" ushort numControlPointsPerPatch;\n"
"};\n"
"\n"
"kernel void\n"
"cmain(VertData v [[ stage_in ]],\n"
" constant KernelPatchInfo& patchInfo [[ buffer(2) ]],\n"
" device MTLQuadTessellationFactorsHalf* tessellationFactorBuffer [[ buffer(3) ]],\n"
" ushort lID [[ thread_position_in_threadgroup ]],\n"
" ushort groupID [[ threadgroup_position_in_grid ]])\n"
"{\n"
" uint patchGroupID = groupID * patchInfo.numPatchesInThreadGroup;\n"
"\n"
" // execute the per-patch hull function\n"
" if (lID < patchInfo.numPatchesInThreadGroup)\n"
" {\n"
" uint patchID = patchGroupID + lID;\n"
" device MTLQuadTessellationFactorsHalf& patchOut = tessellationFactorBuffer[patchID];\n"
" for (int i=0 ; i<4 ; ++i)\n"
" patchOut.edgeTessellationFactor[i] = v.outerLevelsIn[i];\n"
" for (int i=0 ; i<2 ; ++i)\n"
" patchOut.insideTessellationFactor[i] = v.innerLevelsIn[i];\n"
" }\n"
"}\n";
2018-10-16 03:17:02 +00:00
static const char* TessES =
2018-12-08 05:30:43 +00:00
"struct Ripple\n"
"{\n"
" float4 center; // time, distFalloff\n"
" float4 params; // amplitude, lookupPhase, lookupTime\n"
"};\n"
"\n"
"struct FluidPlaneUniform\n"
"{\n"
" float4x4 mv;\n"
" float4x4 mvNorm;\n"
" float4x4 proj;\n"
" float4x4 texMtxs[6];\n"
" Ripple ripples[20];\n"
" float4 colorMul;\n"
" float rippleNormResolution;\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float2 uv6;\n"
"};\n"
"\n"
"struct VertData\n"
"{\n"
" float4 minMaxPos [[ attribute(0) ]];\n"
" float4 outerLevelsIn [[ attribute(1) ]];\n"
" float2 innerLevelsIn [[ attribute(2) ]];\n"
"};\n"
"\n"
"#define PI_X2 6.283185307179586\n"
"\n"
"static void ApplyRipple(constant Ripple& ripple, float2 pos, thread float& height,\n"
" sampler samp, texture2d<float> RippleMap)\n"
"{\n"
" float dist = length(ripple.center.xy - pos);\n"
" float rippleV = RippleMap.sample(samp, float2(dist * ripple.center.w, ripple.center.z), level(0.0)).r;\n"
" height += rippleV * ripple.params.x * sin((dist * ripple.params.y + ripple.params.z) * PI_X2);\n"
"}\n"
"\n"
"[[ patch(quad, 1) ]]\n"
"vertex VertToFrag emain(VertData v [[ stage_in ]], float2 TessCoord [[ position_in_patch ]],\n"
" constant FluidPlaneUniform& fu [[ buffer(2) ]],\n"
" sampler samp [[ sampler(2) ]],\n"
2019-07-20 04:27:21 +00:00
" texture2d<float> RippleMap [[ texture(RIPPLE_TEXTURE_IDX) ]])\n"
2018-12-08 05:30:43 +00:00
"{\n"
" float2 posIn = float2(mix(v.minMaxPos.x, v.minMaxPos.z, TessCoord.x),\n"
" mix(v.minMaxPos.y, v.minMaxPos.w, 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(fu.ripples[i], posIn, height, samp, RippleMap);\n"
" ApplyRipple(fu.ripples[i], posIn + float2(0.0, fu.rippleNormResolution), upHeight, samp, RippleMap);\n"
" ApplyRipple(fu.ripples[i], posIn - float2(0.0, fu.rippleNormResolution), downHeight, samp, RippleMap);\n"
" ApplyRipple(fu.ripples[i], posIn + float2(fu.rippleNormResolution, 0.0), rightHeight, samp, RippleMap);\n"
" ApplyRipple(fu.ripples[i], posIn - float2(fu.rippleNormResolution, 0.0), leftHeight, samp, RippleMap);\n"
" }\n"
" float4 normalIn = float4(normalize(float3((leftHeight - rightHeight),\n"
" (downHeight - upHeight),\n"
" fu.rippleNormResolution)), 1.0);\n"
" float4 binormalIn = float4(normalIn.x, normalIn.z, -normalIn.y, 1.0);\n"
" float4 tangentIn = float4(normalIn.z, normalIn.y, -normalIn.x, 1.0);\n"
" float4 pos = float4(posIn, height, 1.0);\n"
" VertToFrag vtf;\n"
" vtf.mvPos = (fu.mv * pos);\n"
" vtf.pos = (fu.proj * vtf.mvPos);\n"
" vtf.mvNorm = (fu.mvNorm * normalIn);\n"
" vtf.mvBinorm = (fu.mvNorm * binormalIn);\n"
" vtf.mvTangent = (fu.mvNorm * tangentIn);\n"
" vtf.color = max(height, 0.0) * fu.colorMul;\n"
" vtf.color.a = 1.0;\n"
" vtf.uv0 = (fu.texMtxs[0] * pos).xy;\n"
" vtf.uv1 = (fu.texMtxs[1] * pos).xy;\n"
" vtf.uv2 = (fu.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
" return vtf;\n"
"}\n";
2018-10-16 03:17:02 +00:00
static const char* FS =
"struct Light\n"
"{\n"
" float4 pos;\n"
" float4 dir;\n"
" float4 color;\n"
" float4 linAtt;\n"
" float4 angAtt;\n"
"};\n"
2019-03-03 06:19:42 +00:00
FOG_STRUCT_METAL
2018-10-16 03:17:02 +00:00
"\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"
2019-02-08 07:56:54 +00:00
" float4 addColor;\n"
2018-10-16 03:17:02 +00:00
" Fog fog;\n"
"};\n"
"\n"
"static float4 LightingFunc(constant LightingUniform& lu, float3 mvPosIn, float3 mvNormIn)\n"
"{\n"
" float4 ret = lu.ambient;\n"
" \n"
" for (int i=0 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n"
" {\n"
" float3 delta = mvPosIn - lu.lights[i].pos.xyz;\n"
" float dist = length(delta);\n"
" float angDot = clamp(dot(normalize(delta), lu.lights[i].dir.xyz), 0.0, 1.0);\n"
" float att = 1.0 / (lu.lights[i].linAtt[2] * dist * dist +\n"
" lu.lights[i].linAtt[1] * dist +\n"
" lu.lights[i].linAtt[0]);\n"
" float angAtt = lu.lights[i].angAtt[2] * angDot * angDot +\n"
" lu.lights[i].angAtt[1] * angDot +\n"
" lu.lights[i].angAtt[0];\n"
2019-01-23 07:52:19 +00:00
" ret += lu.lights[i].color * angAtt * att * saturate(dot(normalize(-delta), mvNormIn));\n"
2018-10-16 03:17:02 +00:00
" }\n"
" \n"
2019-01-29 20:14:34 +00:00
" return saturate(ret);\n"
2018-10-16 03:17:02 +00:00
"}\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float2 uv6;\n"
"};\n"
"\n"
2019-03-03 06:19:42 +00:00
FOG_ALGORITHM_METAL
2018-10-21 04:14:00 +00:00
"\n"
2018-10-16 03:17:02 +00:00
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n"
" sampler samp [[ sampler(0) ]],\n"
2019-07-20 04:27:21 +00:00
" constant LightingUniform& lu [[ buffer(4) ]]"
" TEXTURE_PARAMS)\n" // Textures here
2018-10-16 03:17:02 +00:00
"{\n"
" float4 lighting = LightingFunc(lu, vtf.mvPos.xyz, normalize(vtf.mvNorm.xyz));\n"
" float4 colorOut;\n"
2019-07-20 04:27:21 +00:00
" COMBINER_EXPRS\n" // Combiner expression here
2018-10-21 04:14:00 +00:00
" return MainPostFunc(vtf, lu, colorOut);\n"
2018-10-16 03:17:02 +00:00
"}\n";
static const char* FSDoor =
"struct Light\n"
"{\n"
" float4 pos;\n"
" float4 dir;\n"
" float4 color;\n"
" float4 linAtt;\n"
" float4 angAtt;\n"
"};\n"
2019-03-03 06:19:42 +00:00
FOG_STRUCT_METAL
2018-10-16 03:17:02 +00:00
"\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"
2019-02-08 07:56:54 +00:00
" float4 addColor;\n"
2018-10-16 03:17:02 +00:00
" Fog fog;\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float4 mvPos;\n"
" float4 mvNorm;\n"
" float4 mvBinorm;\n"
" float4 mvTangent;\n"
" float4 color;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float2 uv6;\n"
"};\n"
2019-03-03 06:19:42 +00:00
FOG_ALGORITHM_METAL
2018-10-16 03:17:02 +00:00
"\n"
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n"
" sampler samp [[ sampler(0) ]],\n"
2019-07-20 04:27:21 +00:00
" constant LightingUniform& lu [[ buffer(4) ]]"
" TEXTURE_PARAMS)\n" // Textures here
2018-10-16 03:17:02 +00:00
"{\n"
" float4 colorOut;\n"
2019-07-20 04:27:21 +00:00
" COMBINER_EXPRS\n" // Combiner expression here
2019-03-03 06:19:42 +00:00
" return MainPostFunc(vtf, lu, colorOut);\n"
2018-10-16 03:17:02 +00:00
"}\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_PARAMS ";
2018-12-08 05:30:43 +00:00
if (info.m_hasPatternTex1)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> patternTex1 [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasPatternTex2)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> patternTex2 [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> colorTex [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> bumpMap [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasEnvMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> envMap [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasEnvBumpMap)
2020-05-09 23:15:31 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> envBumpMap [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasLightmap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> lightMap [[ texture({}) ]]"), nextTex++);
2019-07-20 04:27:21 +00:00
out << '\n';
2018-12-08 05:30:43 +00:00
if (info.m_hasBumpMap) {
bumpMapUv = nextTCG;
}
if (info.m_hasEnvBumpMap) {
envBumpMapUv = nextTCG;
}
if (info.m_hasEnvMap) {
envMapUv = nextTCG;
}
if (info.m_hasLightmap) {
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("float4 lightMapTexel = lightMap.sample(samp, vtf.uv{});"), 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 * lu.kColor2, lightMapTexel, lu.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 * lu.kColor2;";
2018-12-08 05:30:43 +00:00
}
2018-10-16 03:17:02 +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 = (patternTex1.sample(samp, vtf.uv0) * lu.kColor0 + lighting) * "
"patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = lighting * patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2018-10-16 03:17:02 +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
"float2 indUvs = (envBumpMap.sample(samp, vtf.uv{}).ra - float2(0.5, 0.5)) * "
"float2(lu.fog.indScale, -lu.fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, indUvs + vtf.uv2) * 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 += colorTex.sample(samp, vtf.uv2) * lighting;";
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"float2 indUvs = (envBumpMap.sample(samp, vtf.uv{}).ra - float2(0.5, 0.5)) * "
"float2(lu.fog.indScale, -lu.fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(
2019-07-20 04:27:21 +00:00
"colorOut = mix(colorOut, envMap.sample(samp, indUvs + vtf.uv{}), lu.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 += colorTex.sample(samp, vtf.uv2) * lighting;";
2018-10-16 03:17:02 +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("float4 lightMapTexel = lightMap.sample(samp, vtf.uv{});"), 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 * lu.kColor2, lightMapTexel, lu.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 * lu.kColor2;";
2018-12-08 05:30:43 +00:00
}
}
2018-10-16 03:17:02 +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 = (patternTex1.sample(samp, vtf.uv0) * lu.kColor0 + lighting) * "
"patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = lighting * patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2018-12-08 05:30:43 +00:00
}
2018-10-16 03:17:02 +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
"float2 indUvs = (envBumpMap.sample(samp, vtf.uv{}).ra - float2(0.5, 0.5)) * "
"float2(lu.fog.indScale, -lu.fog.indScale);"),
2018-12-08 05:30:43 +00:00
envBumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, indUvs + vtf.uv2) * lighting;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, vtf.uv2) * lighting;";
2018-12-08 05:30:43 +00:00
}
}
2018-10-16 03:17:02 +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 = (patternTex1.sample(samp, vtf.uv0) * lu.kColor0 + vtf.color) * "
"patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color * patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2018-10-16 03:17:02 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, vtf.uv2);";
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 <<
"float3 lightVec = lu.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
"float4 emboss1 = bumpMap.sample(samp, vtf.uv{}) + float4(0.5);"
"float4 emboss2 = bumpMap.sample(samp, vtf.uv{} + float2(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 + float4(0.5) - emboss2) * float4(2.0), float4(0.0), float4(1.0));";
2018-10-16 03:17:02 +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 = (patternTex1.sample(samp, vtf.uv0) * lu.kColor0 + vtf.color) * "
"patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
else
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color * patternTex2.sample(samp, vtf.uv1) + vtf.color;";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = vtf.color;";
2018-10-16 03:17:02 +00:00
}
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, vtf.uv2);";
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("float4 emboss1 = bumpMap.sample(samp, vtf.uv{}) + float4(0.5);"), bumpMapUv);
2019-07-20 04:27:21 +00:00
out << "colorOut *= emboss1 * float4(2.0);";
2018-10-16 03:17:02 +00:00
}
2018-12-08 05:30:43 +00:00
break;
}
2019-07-20 04:27:21 +00:00
out << "colorOut.a = lu.kColor0.a;\n";
2018-12-08 05:30:43 +00:00
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-20 04:27:21 +00:00
out << "#define ADDITIONAL_TCGS ";
if (info.m_hasBumpMap)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING("vtf.uv{} = (fu.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.uv{} = (fu.texMtxs[3] * float4(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.uv{} = (fu.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.uv{} = (fu.texMtxs[{}] * pos).xy;"), nextTCG++, nextMtx++);
2019-07-20 04:27:21 +00:00
out << '\n';
2018-10-16 03:17:02 +00:00
}
2018-12-08 05:30:43 +00:00
static std::string _BuildVS(const SFluidPlaneShaderInfo& info, bool tessellation) {
2019-07-20 04:27:21 +00:00
std::stringstream out;
_BuildAdditionalTCGs(out, info);
out << VS;
return out.str();
2018-10-16 03:17:02 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Metal, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildVS(in, tessellation);
2018-10-16 03:17:02 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Metal, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return _BuildFS(in);
2018-10-16 03:17:02 +00:00
}
2018-10-19 02:44:41 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Metal, hecl::PipelineStage::Control>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return TessCS;
2018-10-19 02:44:41 +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);
out << "#define RIPPLE_TEXTURE_IDX " << nextTex << '\n';
out << TessES;
return out.str();
2018-10-16 03:17:02 +00:00
}
2018-10-19 02:44:41 +00:00
2018-10-16 03:17:02 +00:00
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneShader<hecl::PlatformType::Metal, hecl::PipelineStage::Evaluation>::BuildShader(
const SFluidPlaneShaderInfo& in, bool tessellation) {
return BuildES(in);
2018-10-16 03:17:02 +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-16 03:17:02 +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-20 04:27:21 +00:00
out << "#define TEXTURE_PARAMS ";
2018-12-08 05:30:43 +00:00
if (info.m_hasPatternTex1)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> patternTex1 [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasPatternTex2)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> patternTex2 [[ texture({}) ]]"), nextTex++);
2018-12-08 05:30:43 +00:00
if (info.m_hasColorTex)
2020-04-11 22:51:39 +00:00
fmt::print(out, FMT_STRING(",texture2d<float> colorTex [[ texture({}) ]]"), 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 = patternTex1.sample(samp, vtf.uv0) * lu.kColor0 * "
"patternTex2.sample(samp, vtf.uv1);";
2018-12-08 05:30:43 +00:00
} else {
2019-07-20 04:27:21 +00:00
out << "colorOut = float4(0.0);";
2018-12-08 05:30:43 +00:00
}
if (info.m_hasColorTex) {
2019-07-20 04:27:21 +00:00
out << "colorOut += colorTex.sample(samp, vtf.uv2);";
2018-12-08 05:30:43 +00:00
}
2019-07-20 04:27:21 +00:00
out << "colorOut.a = lu.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-16 03:17:02 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::Metal, hecl::PipelineStage::Vertex>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildVS(in);
2018-10-16 03:17:02 +00:00
}
template <>
2018-12-08 05:30:43 +00:00
std::string StageObject_CFluidPlaneDoorShader<hecl::PlatformType::Metal, hecl::PipelineStage::Fragment>::BuildShader(
const SFluidPlaneDoorShaderInfo& in) {
return _BuildFS(in);
2018-10-16 03:17:02 +00:00
}