30 Ogre::RTShader::Program* vsProgram = programSet->getCpuProgram(Ogre::GPT_VERTEX_PROGRAM);
31 Ogre::RTShader::Function* vsMain = vsProgram->getEntryPointFunction();
32 Ogre::RTShader::Program* psProgram = programSet->getCpuProgram(Ogre::GPT_FRAGMENT_PROGRAM);
33 Ogre::RTShader::Function* psMain = psProgram->getEntryPointFunction();
35 vsProgram->addDependency(FFP_LIB_TRANSFORM);
37 psProgram->addDependency(FFP_LIB_TRANSFORM);
38 psProgram->addDependency(FFP_LIB_TEXTURING);
39 psProgram->addDependency(
"SGXLib_CookTorrance");
40 psProgram->addPreprocessorDefines(Ogre::StringUtil::format(
"LIGHT_COUNT=%d", mLightCount));
43 auto vsOutTexcoord = vsMain->getOutputParameter(Ogre::RTShader::Parameter::SPC_TEXTURE_COORDINATE0, Ogre::GCT_FLOAT2);
44 Ogre::RTShader::ParameterPtr vsInTexcoord;
47 vsInTexcoord = vsMain->resolveInputParameter(Ogre::RTShader::Parameter::SPC_TEXTURE_COORDINATE0, Ogre::GCT_FLOAT2);
48 vsOutTexcoord = vsMain->resolveOutputParameter(Ogre::RTShader::Parameter::SPC_TEXTURE_COORDINATE0, Ogre::GCT_FLOAT2);
50 auto psInTexcoord = psMain->resolveInputParameter(vsOutTexcoord);
53 auto vsInPosition = vsMain->getLocalParameter(Ogre::RTShader::Parameter::SPC_POSITION_OBJECT_SPACE);
55 vsInPosition = vsMain->resolveInputParameter(Ogre::RTShader::Parameter::SPC_POSITION_OBJECT_SPACE);
56 auto vsOutViewPos = vsMain->resolveOutputParameter(Ogre::RTShader::Parameter::SPC_POSITION_VIEW_SPACE);
57 auto viewPos = psMain->resolveInputParameter(vsOutViewPos);
58 auto worldViewMatrix = vsProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_WORLDVIEW_MATRIX);
61 auto viewNormal = psMain->getLocalParameter(Ogre::RTShader::Parameter::SPC_NORMAL_VIEW_SPACE);
62 Ogre::RTShader::ParameterPtr vsInNormal, vsOutNormal;
67 vsInNormal = vsMain->resolveInputParameter(Ogre::RTShader::Parameter::SPC_NORMAL_OBJECT_SPACE);
70 vsOutNormal = vsMain->resolveOutputParameter(Ogre::RTShader::Parameter::SPC_NORMAL_VIEW_SPACE);
73 viewNormal = psMain->resolveInputParameter(vsOutNormal);
77 auto outDiffuse = psMain->resolveOutputParameter(Ogre::RTShader::Parameter::SPC_COLOR_DIFFUSE);
78 auto outSpecular = psMain->resolveLocalParameter(Ogre::RTShader::Parameter::SPC_COLOR_SPECULAR);
81 auto vstage = vsMain->getStage(Ogre::RTShader::FFP_PS_COLOUR_BEGIN + 1);
82 auto fstage = psMain->getStage(Ogre::RTShader::FFP_PS_COLOUR_END + 50);
86 vstage.assign(vsInTexcoord, vsOutTexcoord);
87 vstage.callFunction(FFP_FUNC_TRANSFORM, worldViewMatrix, vsInPosition, vsOutViewPos);
92 auto worldViewITMatrix = vsProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_NORMAL_MATRIX);
93 vstage.callBuiltin(
"mul", worldViewITMatrix, vsInNormal, vsOutNormal);
96 auto sceneCol = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_DERIVED_SCENE_COLOUR);
97 auto litResult = psMain->resolveLocalParameter(Ogre::GCT_FLOAT4,
"litResult");
98 auto diffuse = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_SURFACE_DIFFUSE_COLOUR);
99 auto baseColor = psMain->resolveLocalParameter(Ogre::GCT_FLOAT4,
"baseColor");
100 auto ambientColor = psMain->resolveLocalParameter(Ogre::GCT_FLOAT4,
"ambientColor");
102 fstage.assign(Ogre::RTShader::In(sceneCol), Ogre::RTShader::Out(ambientColor));
105 auto mrparams = psMain->resolveLocalParameter(Ogre::GCT_FLOAT2,
"metalRoughness");
106 if (!mMetalRoughnessMapName.empty())
108 auto metalRoughnessSampler =
109 psProgram->resolveParameter(Ogre::GCT_SAMPLER2D,
"metalRoughnessSampler", mMRMapSamplerIndex);
110 auto mrSample = psMain->resolveLocalParameter(Ogre::GCT_FLOAT4,
"mrSample");
113 fstage.sampleTexture(metalRoughnessSampler, psInTexcoord, mrSample);
114 fstage.assign(Ogre::RTShader::In(mrSample).mask(Ogre::RTShader::Operand::OPM_YZ), mrparams);
118 fstage.mul(Ogre::RTShader::In(ambientColor).xyz(), Ogre::RTShader::In(mrSample).x(), Ogre::RTShader::Out(ambientColor).xyz());
122 auto specular = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_SURFACE_SPECULAR_COLOUR);
123 fstage.assign(Ogre::RTShader::In(specular).xy(), mrparams);
126 auto pixelParams = psMain->resolveLocalStructParameter(
"PixelParams",
"pixel");
128 fstage.mul(Ogre::RTShader::In(diffuse).xyz(), Ogre::RTShader::In(outDiffuse).xyz(), Ogre::RTShader::Out(baseColor).xyz());
129 fstage.assign(Ogre::Vector3(0), Ogre::RTShader::Out(outDiffuse).xyz());
131 fstage.mul(Ogre::RTShader::In(diffuse).
w(), Ogre::RTShader::In(outDiffuse).
w(), Ogre::RTShader::Out(outDiffuse).
w());
132 fstage.callFunction(
"PBR_MakeParams", { Ogre::RTShader::In(baseColor).xyz(), Ogre::RTShader::In(mrparams), Ogre::RTShader::InOut(pixelParams) });
134 fstage = psMain->getStage(Ogre::RTShader::FFP_PS_COLOUR_END + 60);
137 auto lightPos = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_LIGHT_POSITION_VIEW_SPACE_ARRAY, mLightCount);
138 auto lightDiffuse = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED_ARRAY, mLightCount);
139 auto pointParams = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_LIGHT_ATTENUATION_ARRAY, mLightCount);
140 auto spotParams = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_SPOTLIGHT_PARAMS_ARRAY, mLightCount);
141 auto lightDirView = psProgram->resolveParameter(Ogre::GpuProgramParameters::ACT_LIGHT_DIRECTION_VIEW_SPACE_ARRAY, mLightCount);
143 std::vector<Ogre::RTShader::Operand> params = { Ogre::RTShader::In(viewNormal), Ogre::RTShader::In(viewPos), Ogre::RTShader::In(ambientColor), Ogre::RTShader::In(lightPos),
144 Ogre::RTShader::In(lightDiffuse), Ogre::RTShader::In(pointParams), Ogre::RTShader::In(lightDirView), Ogre::RTShader::In(spotParams),
145 Ogre::RTShader::In(pixelParams), Ogre::RTShader::InOut(outDiffuse).xyz() };
147 if (mLtcLUT1SamplerIndex > -1)
149 auto ltcLUT1 = psProgram->resolveParameter(Ogre::GCT_SAMPLER2D,
"ltcLUT1Sampler", mLtcLUT1SamplerIndex);
150 auto ltcLUT2 = psProgram->resolveParameter(Ogre::GCT_SAMPLER2D,
"ltcLUT2Sampler", mLtcLUT1SamplerIndex + 1);
151 params.insert(params.begin(), { Ogre::RTShader::In(ltcLUT1), Ogre::RTShader::In(ltcLUT2) });
152 psProgram->addPreprocessorDefines(
"HAVE_AREA_LIGHTS");
155 if (
auto shadowFactor = psMain->getLocalParameter(
"lShadowFactor"))
157 params.insert(params.begin(), Ogre::RTShader::In(shadowFactor));
160 fstage.callFunction(
"PBR_Lights", params);