Torque3D Documentation / _generateds / deferredShadingFeaturesGLSL.cpp

deferredShadingFeaturesGLSL.cpp

Engine/source/lighting/advanced/glsl/deferredShadingFeaturesGLSL.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "lighting/advanced/glsl/deferredShadingFeaturesGLSL.h"
 26
 27#include "lighting/advanced/advancedLightBinManager.h"
 28#include "shaderGen/langElement.h"
 29#include "shaderGen/shaderOp.h"
 30#include "shaderGen/conditionerFeature.h"
 31#include "renderInstance/renderDeferredMgr.h"
 32#include "materials/processedMaterial.h"
 33#include "materials/materialFeatureTypes.h"
 34
 35
 36//****************************************************************************
 37// Deferred Shading Features
 38//****************************************************************************
 39U32 DeferredOrmMapGLSL::getOutputTargets(const MaterialFeatureData& fd) const
 40{
 41   return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
 42}
 43
 44void DeferredOrmMapGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
 45{
 46   // Get the texture coord.
 47   Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
 48   
 49   MultiLine* meta = new MultiLine;
 50   Var* ormConfig;
 51   if (fd.features[MFT_isDeferred])
 52   {
 53      ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
 54      if (!ormConfig)
 55      {
 56         // create material var
 57         ormConfig = new Var;
 58         ormConfig->setType("vec4");
 59         ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
 60         ormConfig->setStructName("OUT");
 61      }
 62   }
 63   else
 64   {
 65      ormConfig = (Var*)LangElement::find("ORMConfig");
 66      if (!ormConfig)
 67      {
 68         ormConfig = new Var("ORMConfig", "vec4");
 69         meta->addStatement(new GenOp("   @;\r\n", new DecOp(ormConfig)));
 70      }
 71   }
 72
 73   // create texture var
 74   Var *ormMap = new Var;
 75   ormMap->setType( "sampler2D" );
 76   ormMap->setName( "ormMap" );
 77   ormMap->uniform = true;
 78   ormMap->sampler = true;
 79   ormMap->constNum = Var::getTexUnitNum();
 80   LangElement *texOp = new GenOp( "tex2D(@, @)", ormMap, texCoord );
 81
 82   Var *metalness = (Var*)LangElement::find("metalness");
 83   if (!metalness) metalness = new Var("metalness", "float");
 84   Var *roughness = (Var*)LangElement::find("roughness");
 85   if (!roughness) roughness = new Var("roughness", "float");
 86   Var* ao = (Var*)LangElement::find("ao");
 87   if (!ao) ao = new Var("ao", "float");
 88
 89
 90   meta->addStatement(new GenOp("   @.gba = @.rgb;\r\n", ormConfig, texOp));
 91
 92   meta->addStatement(new GenOp("   @ = @.g;\r\n", new DecOp(ao), ormConfig));
 93   meta->addStatement(new GenOp("   @ = @.b;\r\n", new DecOp(roughness), ormConfig));
 94   if (fd.features[MFT_InvertRoughness])
 95   {
 96      meta->addStatement(new GenOp("   @.b = 1.0-@.b;\r\n", ormConfig, ormConfig));
 97      meta->addStatement(new GenOp("   @ = 1.0-@;\r\n", roughness, roughness));
 98   }
 99   meta->addStatement(new GenOp("   @ = @.a;\r\n", new DecOp(metalness), ormConfig));
100
101   output = meta;
102}
103
104ShaderFeature::Resources DeferredOrmMapGLSL::getResources( const MaterialFeatureData &fd )
105{
106   Resources res; 
107   res.numTex = 1;
108   res.numTexReg = 1;
109
110   return res;
111}
112
113void DeferredOrmMapGLSL::setTexData(   Material::StageData &stageDat,
114                                       const MaterialFeatureData &fd,
115                                       RenderPassData &passData,
116                                       U32 &texIndex )
117{
118   GFXTextureObject *tex = stageDat.getTex(MFT_OrmMap);
119   if ( tex )
120   {
121      passData.mTexType[ texIndex ] = Material::Standard;
122      passData.mSamplerNames[ texIndex ] = "ormMap";
123      passData.mTexSlot[ texIndex++ ].texObject = tex;
124   }
125}
126
127void DeferredOrmMapGLSL::processVert( Vector<ShaderComponent*> &componentList,
128                                       const MaterialFeatureData &fd )
129{
130   MultiLine *meta = new MultiLine;
131   getOutTexCoord(   "texCoord", 
132                     "vec2", 
133                     fd.features[MFT_TexAnim], 
134                     meta, 
135                     componentList );
136   output = meta;
137}
138
139U32 MatInfoFlagsGLSL::getOutputTargets(const MaterialFeatureData& fd) const
140{
141   return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
142}
143
144// Material Info Flags -> Red ( Flags ) of Material Info Buffer.
145void MatInfoFlagsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
146{
147   MultiLine *meta = new MultiLine;
148
149   Var* ormConfig;
150   if (fd.features[MFT_isDeferred])
151   {
152      ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
153      if (!ormConfig)
154      {
155         // create material var
156         ormConfig = new Var;
157         ormConfig->setType("vec4");
158         ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
159         ormConfig->setStructName("OUT");
160      }
161   }
162   else
163   {
164      ormConfig = (Var*)LangElement::find("ORMConfig");
165      if (!ormConfig)
166      {
167         ormConfig = new Var("ORMConfig", "vec4");
168         meta->addStatement(new GenOp("   @;\r\n", new DecOp(ormConfig)));
169      }
170   }
171
172   Var *matInfoFlags = new Var;
173   matInfoFlags->setType( "float" );
174   matInfoFlags->setName( "matInfoFlags" );
175   matInfoFlags->uniform = true;
176   matInfoFlags->constSortPos = cspPotentialPrimitive;
177
178   meta->addStatement(output = new GenOp("   @.r = @;\r\n", ormConfig, matInfoFlags));
179   output = meta;
180}
181
182U32 ORMConfigVarsGLSL::getOutputTargets(const MaterialFeatureData& fd) const
183{
184   return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget;
185}
186
187// Spec Strength -> Blue Channel of Material Info Buffer.
188// Spec Power -> Alpha Channel ( of Material Info Buffer.
189void ORMConfigVarsGLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
190{
191   MultiLine* meta = new MultiLine;
192
193   Var* ormConfig;
194   if (fd.features[MFT_isDeferred])
195   {
196      ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2));
197      if (!ormConfig)
198      {
199         // create material var
200         ormConfig = new Var;
201         ormConfig->setType("vec4");
202         ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2));
203         ormConfig->setStructName("OUT");
204      }
205   }
206   else
207   {
208      ormConfig = (Var*)LangElement::find("ORMConfig");
209      if (!ormConfig)
210      {
211         ormConfig = new Var("ORMConfig", "vec4");
212         meta->addStatement(new GenOp("   @;\r\n", new DecOp(ormConfig)));
213      }
214   }
215   Var* metalness = new Var("metalness", "float");
216   metalness->uniform = true;
217   metalness->constSortPos = cspPotentialPrimitive;
218
219   Var* roughness = new Var("roughness", "float");
220   roughness->uniform = true;
221   roughness->constSortPos = cspPotentialPrimitive;
222
223   //matinfo.g slot reserved for AO later
224   meta->addStatement(new GenOp("   @.g = 1.0;\r\n", ormConfig));
225   meta->addStatement(new GenOp("   @.b = @;\r\n", ormConfig, roughness));
226   if (fd.features[MFT_InvertRoughness])
227      meta->addStatement(new GenOp("   @ = 1.0-@;\r\n", roughness, roughness));
228   meta->addStatement(new GenOp("   @.a = @;\r\n", ormConfig, metalness));
229   output = meta;
230}
231
232U32 GlowMapGLSL::getOutputTargets(const MaterialFeatureData& fd) const
233{
234   return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget;
235}
236
237//deferred emissive
238void GlowMapGLSL::processPix(Vector<ShaderComponent*>& componentList, const MaterialFeatureData& fd)
239{
240   Var* texCoord = getInTexCoord("texCoord", "vec2", componentList);
241
242   Var* glowMap = new Var;
243   glowMap->setType("sampler2D");
244   glowMap->setName("glowMap");
245   glowMap->uniform = true;
246   glowMap->sampler = true;
247   glowMap->constNum = Var::getTexUnitNum();
248   LangElement* texOp = new GenOp("tex2D(@, @)", glowMap, texCoord);
249   
250   Var* glowMul = new Var("glowMul", "float");
251   glowMul->uniform = true;
252   glowMul->constSortPos = cspPotentialPrimitive;
253
254   Var* targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
255   if (fd.features[MFT_isDeferred])
256   {
257      targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3));
258      if (!targ)
259      {
260         // create scene color target var
261         targ = new Var;
262         targ->setType("vec4");
263         targ->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3));
264         targ->setStructName("OUT");
265         output = new GenOp("@ = vec4(@.rgb*@,0);", targ, texOp, glowMul);
266      }
267      else
268      {
269         output = new GenOp("@ += vec4(@.rgb*@,0);", targ, texOp, glowMul);
270      }
271   }
272   else
273   {
274      output = new GenOp("@ += vec4(@.rgb*@,@.a);", targ, texOp, glowMul, targ);
275   }
276
277}
278
279ShaderFeature::Resources GlowMapGLSL::getResources(const MaterialFeatureData& fd)
280{
281   Resources res;
282   res.numTex = 1;
283   res.numTexReg = 1;
284
285   return res;
286}
287
288void GlowMapGLSL::setTexData(Material::StageData& stageDat,
289   const MaterialFeatureData& fd,
290   RenderPassData& passData,
291   U32& texIndex)
292{
293   GFXTextureObject* tex = stageDat.getTex(MFT_GlowMap);
294   if (tex)
295   {
296      passData.mTexType[texIndex] = Material::Standard;
297      passData.mSamplerNames[texIndex] = "glowMap";
298      passData.mTexSlot[texIndex++].texObject = tex;
299   }
300}
301