metaforce/Runtime/Graphics/Shaders/CModelShadersGLSL.cpp

245 lines
7.0 KiB
C++
Raw Normal View History

2016-04-04 02:32:57 +00:00
#include "CModelShaders.hpp"
2016-04-04 05:02:09 +00:00
#include "hecl/Backend/GLSL.hpp"
2016-04-04 02:32:57 +00:00
namespace urde
{
2018-10-07 02:59:17 +00:00
using namespace std::literals;
2016-04-04 02:32:57 +00:00
2018-10-07 02:59:17 +00:00
extern const hecl::Backend::Function ExtensionLightingFuncsGLSL[];
extern const hecl::Backend::Function ExtensionPostFuncsGLSL[];
static std::string_view LightingGLSL =
2016-04-04 02:32:57 +00:00
"struct Light\n"
"{\n"
" vec4 pos;\n"
" vec4 dir;\n"
" vec4 color;\n"
" vec4 linAtt;\n"
" vec4 angAtt;\n"
"};\n"
2016-08-08 04:48:18 +00:00
"struct Fog\n"
"{\n"
" int mode;\n"
2016-08-08 04:48:18 +00:00
" vec4 color;\n"
" float rangeScale;\n"
" float start;\n"
"};\n"
2016-04-04 02:32:57 +00:00
"\n"
"UBINDING2 uniform LightingUniform\n"
"{\n"
" Light lights[" _XSTR(URDE_MAX_LIGHTS) "];\n"
" vec4 ambient;\n"
2016-07-21 05:21:45 +00:00
" vec4 colorReg0;\n"
" vec4 colorReg1;\n"
" vec4 colorReg2;\n"
2017-02-10 09:00:57 +00:00
" vec4 mulColor;\n"
2016-08-08 04:48:18 +00:00
" Fog fog;\n"
2016-04-04 02:32:57 +00:00
"};\n"
"\n"
"vec4 LightingFunc(vec3 mvPosIn, vec3 mvNormIn)\n"
2016-04-04 02:32: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"
2016-04-04 02:32:57 +00:00
" float dist = length(delta);\n"
2016-04-04 05:02:09 +00:00
" float angDot = clamp(dot(normalize(delta), lights[i].dir.xyz), 0.0, 1.0);\n"
2016-04-04 02:32: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"
" ret += lights[i].color * clamp(angAtt, 0.0, 1.0) * att * clamp(dot(normalize(-delta), mvNormIn), 0.0, 1.0);\n"
2016-04-04 02:32:57 +00:00
" }\n"
" \n"
2018-05-08 02:11:07 +00:00
" return ret;\n"
2018-10-07 02:59:17 +00:00
"}\n"sv;
2016-04-04 02:32:57 +00:00
2018-10-07 02:59:17 +00:00
static std::string_view LightingShadowGLSL =
"struct Light\n"
"{\n"
" vec4 pos;\n"
" vec4 dir;\n"
" vec4 color;\n"
" vec4 linAtt;\n"
" vec4 angAtt;\n"
"};\n"
"struct Fog\n"
"{\n"
" int mode;\n"
" vec4 color;\n"
" float rangeScale;\n"
" float start;\n"
"};\n"
"\n"
"UBINDING2 uniform LightingUniform\n"
"{\n"
" Light lights[" _XSTR(URDE_MAX_LIGHTS) "];\n"
" vec4 ambient;\n"
" vec4 colorReg0;\n"
" vec4 colorReg1;\n"
" vec4 colorReg2;\n"
" vec4 mulColor;\n"
" Fog fog;\n"
"};\n"
"\n"
"vec4 LightingShadowFunc(vec3 mvPosIn, vec3 mvNormIn)\n"
"{\n"
" vec2 shadowUV = vtf.extTcgs[0];\n"
" shadowUV.y = 1.0 - shadowUV.y;\n"
" \n"
" vec4 ret = ambient;\n"
" \n"
" vec3 delta = mvPosIn - lights[0].pos.xyz;\n"
" float dist = length(delta);\n"
2017-10-15 05:44:07 +00:00
" float angDot = clamp(dot(normalize(delta), lights[0].dir.xyz), 0.0, 1.0);\n"
" float att = 1.0 / (lights[0].linAtt[2] * dist * dist +\n"
" lights[0].linAtt[1] * dist +\n"
" lights[0].linAtt[0]);\n"
" float angAtt = lights[0].angAtt[2] * angDot * angDot +\n"
" lights[0].angAtt[1] * angDot +\n"
" lights[0].angAtt[0];\n"
" ret += lights[0].color * clamp(angAtt, 0.0, 1.0) * att * clamp(dot(normalize(-delta), mvNormIn), 0.0, 1.0) *\n"
" texture(extTex7, shadowUV).r;\n"
" \n"
" for (int i=1 ; i<" _XSTR(URDE_MAX_LIGHTS) " ; ++i)\n"
" {\n"
" vec3 delta = mvPosIn - 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), 0.0, 1.0);\n"
" }\n"
" \n"
2018-05-08 02:11:07 +00:00
" return ret;\n"
2018-10-07 02:59:17 +00:00
"}\n"sv;
2018-10-07 02:59:17 +00:00
static std::string_view MainPostGLSL =
2016-08-08 04:48:18 +00:00
"vec4 MainPostFunc(vec4 colorIn)\n"
"{\n"
2018-05-27 04:22:38 +00:00
" float fogZ, temp;\n"
" switch (fog.mode)\n"
" {\n"
2018-06-07 04:51:23 +00:00
" case 2:\n"
2018-05-27 04:22:38 +00:00
" fogZ = (-vtf.mvPos.z - fog.start) * fog.rangeScale;\n"
" break;\n"
2018-06-07 04:51:23 +00:00
" case 4:\n"
2018-05-27 04:22:38 +00:00
" fogZ = 1.0 - exp2(-8.0 * (-vtf.mvPos.z - fog.start) * fog.rangeScale);\n"
" break;\n"
2018-06-07 04:51:23 +00:00
" case 5:\n"
2018-05-27 04:22:38 +00:00
" temp = (-vtf.mvPos.z - fog.start) * fog.rangeScale;\n"
" fogZ = 1.0 - exp2(-8.0 * temp * temp);\n"
" break;\n"
2018-06-07 04:51:23 +00:00
" case 6:\n"
2018-05-27 04:22:38 +00:00
" fogZ = exp2(-8.0 * (fog.start + vtf.mvPos.z) * fog.rangeScale);\n"
" break;\n"
2018-06-07 04:51:23 +00:00
" case 7:\n"
2018-05-27 04:22:38 +00:00
" temp = (fog.start + vtf.mvPos.z) * fog.rangeScale;\n"
" fogZ = exp2(-8.0 * temp * temp);\n"
" break;\n"
" default:\n"
" fogZ = 0.0;\n"
" break;\n"
" }\n"
" return mix(colorIn, fog.color, clamp(fogZ, 0.0, 1.0));\n"
2016-08-08 04:48:18 +00:00
"}\n"
2018-10-07 02:59:17 +00:00
"\n"sv;
2016-08-08 04:48:18 +00:00
2018-10-07 02:59:17 +00:00
static std::string_view ThermalPostGLSL =
2016-07-31 02:06:47 +00:00
"UBINDING2 uniform ThermalUniform\n"
"{\n"
2017-02-10 09:09:06 +00:00
" vec4 tmulColor;\n"
2016-07-31 02:06:47 +00:00
" vec4 addColor;\n"
"};\n"
"vec4 ThermalPostFunc(vec4 colorIn)\n"
"{\n"
2018-02-05 06:56:09 +00:00
" return vec4(texture(extTex7, vtf.extTcgs[0]).rrr * tmulColor.rgb + addColor.rgb, tmulColor.a + addColor.a);\n"
2016-07-31 02:06:47 +00:00
"}\n"
2018-10-07 02:59:17 +00:00
"\n"sv;
2016-07-31 02:06:47 +00:00
2018-10-07 02:59:17 +00:00
static std::string_view SolidPostGLSL =
2017-03-05 07:57:12 +00:00
"UBINDING2 uniform SolidUniform\n"
"{\n"
" vec4 solidColor;\n"
"};\n"
"vec4 SolidPostFunc(vec4 colorIn)\n"
"{\n"
" return solidColor;\n"
"}\n"
2018-10-07 02:59:17 +00:00
"\n"sv;
2017-03-05 07:57:12 +00:00
2018-10-07 02:59:17 +00:00
static std::string_view MBShadowPostGLSL =
2017-03-05 07:57:12 +00:00
"UBINDING2 uniform MBShadowUniform\n"
"{\n"
" vec4 shadowUp;\n"
" float shadowId;\n"
"};\n"
"vec4 MBShadowPostFunc(vec4 colorIn)\n"
"{\n"
2017-03-05 23:03:23 +00:00
" float idTexel = texture(extTex0, vtf.extTcgs[0]).a;\n"
" float sphereTexel = texture(extTex1, vtf.extTcgs[1]).a;\n"
" float fadeTexel = texture(extTex2, vtf.extTcgs[2]).a;\n"
2017-03-05 07:57:12 +00:00
" float val = ((abs(idTexel - shadowId) < 0.001) ?\n"
" (dot(vtf.mvNorm.xyz, shadowUp.xyz) * shadowUp.w) : 0.0) *\n"
" sphereTexel * fadeTexel;\n"
" return vec4(0.0, 0.0, 0.0, val);\n"
"}\n"
2018-10-07 02:59:17 +00:00
"\n"sv;
2017-03-05 07:57:12 +00:00
2018-10-07 02:59:17 +00:00
const hecl::Backend::Function ExtensionLightingFuncsGLSL[] =
2016-04-04 02:32:57 +00:00
{
2018-10-07 02:59:17 +00:00
{},
{LightingGLSL, "LightingFunc"},
{},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{},
{},
{},
{},
{},
{},
{},
{LightingShadowGLSL, "LightingShadowFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"},
{LightingGLSL, "LightingFunc"}
};
const hecl::Backend::Function ExtensionPostFuncsGLSL[] =
{
{},
{MainPostGLSL, "MainPostFunc"},
{ThermalPostGLSL, "ThermalPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{SolidPostGLSL, "SolidPostFunc"},
{MBShadowPostGLSL, "MBShadowPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
{MainPostGLSL, "MainPostFunc"},
};
2016-04-04 02:32:57 +00:00
}