deferredShadingFeaturesHLSL.cpp
Engine/source/lighting/advanced/hlsl/deferredShadingFeaturesHLSL.cpp
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/hlsl/deferredShadingFeaturesHLSL.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 DeferredOrmMapHLSL::getOutputTargets(const MaterialFeatureData& fd) const 40{ 41 return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget; 42} 43 44void DeferredOrmMapHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) 45{ 46 // Get the texture coord. 47 Var *texCoord = getInTexCoord( "texCoord", "float2", 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("fragout"); 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", "float4"); 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( "SamplerState" ); 76 ormMap->setName( "ORMConfigMap" ); 77 ormMap->uniform = true; 78 ormMap->sampler = true; 79 ormMap->constNum = Var::getTexUnitNum(); 80 81 Var* ormMapTex = new Var; 82 ormMapTex->setName("ORMConfigMapTex"); 83 ormMapTex->setType("Texture2D"); 84 ormMapTex->uniform = true; 85 ormMapTex->texture = true; 86 ormMapTex->constNum = ormMap->constNum; 87 LangElement *texOp = new GenOp("@.Sample(@, @)", ormMapTex, ormMap, texCoord); 88 89 Var *metalness = (Var*)LangElement::find("metalness"); 90 if (!metalness) metalness = new Var("metalness", "float"); 91 Var *roughness = (Var*)LangElement::find("roughness"); 92 if (!roughness) roughness = new Var("roughness", "float"); 93 Var* ao = (Var*)LangElement::find("ao"); 94 if (!ao) ao = new Var("ao", "float"); 95 96 97 meta->addStatement(new GenOp(" @.gba = @.rgb;\r\n", ormConfig, texOp)); 98 99 meta->addStatement(new GenOp(" @ = @.g;\r\n", new DecOp(ao), ormConfig)); 100 meta->addStatement(new GenOp(" @ = @.b;\r\n", new DecOp(roughness), ormConfig)); 101 if (fd.features[MFT_InvertRoughness]) 102 { 103 meta->addStatement(new GenOp(" @.b = 1.0-@.b;\r\n", ormConfig, ormConfig)); 104 meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", roughness, roughness)); 105 } 106 meta->addStatement(new GenOp(" @ = @.a;\r\n", new DecOp(metalness), ormConfig)); 107 108 output = meta; 109} 110 111ShaderFeature::Resources DeferredOrmMapHLSL::getResources( const MaterialFeatureData &fd ) 112{ 113 Resources res; 114 res.numTex = 1; 115 res.numTexReg = 1; 116 117 return res; 118} 119 120void DeferredOrmMapHLSL::setTexData( Material::StageData &stageDat, 121 const MaterialFeatureData &fd, 122 RenderPassData &passData, 123 U32 &texIndex ) 124{ 125 GFXTextureObject *tex = stageDat.getTex(MFT_OrmMap); 126 if ( tex ) 127 { 128 passData.mTexType[ texIndex ] = Material::Standard; 129 passData.mSamplerNames[ texIndex ] = "ORMConfigMap"; 130 passData.mTexSlot[ texIndex++ ].texObject = tex; 131 } 132} 133 134void DeferredOrmMapHLSL::processVert( Vector<ShaderComponent*> &componentList, 135 const MaterialFeatureData &fd ) 136{ 137 MultiLine *meta = new MultiLine; 138 getOutTexCoord( "texCoord", 139 "float2", 140 fd.features[MFT_TexAnim], 141 meta, 142 componentList ); 143 output = meta; 144} 145 146U32 MatInfoFlagsHLSL::getOutputTargets(const MaterialFeatureData& fd) const 147{ 148 return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget; 149} 150 151// Material Info Flags -> Red ( Flags ) of Material Info Buffer. 152void MatInfoFlagsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) 153{ 154 // search for material var 155 Var* ormConfig; 156 if (fd.features[MFT_isDeferred]) 157 { 158 ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2)); 159 if (!ormConfig) 160 { 161 // create material var 162 ormConfig = new Var; 163 ormConfig->setType("fragout"); 164 ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2)); 165 ormConfig->setStructName("OUT"); 166 } 167 } 168 else 169 { 170 ormConfig = (Var*)LangElement::find("ORMConfig"); 171 if (!ormConfig) ormConfig = new Var("ORMConfig", "float4"); 172 } 173 174 Var *matInfoFlags = new Var; 175 matInfoFlags->setType( "float" ); 176 matInfoFlags->setName( "matInfoFlags" ); 177 matInfoFlags->uniform = true; 178 matInfoFlags->constSortPos = cspPotentialPrimitive; 179 180 output = new GenOp( " @.r = @;\r\n", ormConfig, matInfoFlags ); 181} 182 183U32 ORMConfigVarsHLSL::getOutputTargets(const MaterialFeatureData& fd) const 184{ 185 return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget2 : ShaderFeature::DefaultTarget; 186} 187 188void ORMConfigVarsHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd ) 189{ 190 MultiLine* meta = new MultiLine; 191 Var* ormConfig; 192 if (fd.features[MFT_isDeferred]) 193 { 194 ormConfig = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget2)); 195 if (!ormConfig) 196 { 197 // create material var 198 ormConfig = new Var; 199 ormConfig->setType("fragout"); 200 ormConfig->setName(getOutputTargetVarName(ShaderFeature::RenderTarget2)); 201 ormConfig->setStructName("OUT"); 202 } 203 } 204 else 205 { 206 ormConfig = (Var*)LangElement::find("ORMConfig"); 207 if (!ormConfig) ormConfig = new Var("ORMConfig", "float4"); 208 meta->addStatement(new GenOp(" @;\r\n", new DecOp(ormConfig))); 209 } 210 Var *metalness = new Var("metalness", "float"); 211 metalness->uniform = true; 212 metalness->constSortPos = cspPotentialPrimitive; 213 214 Var *roughness = new Var("roughness", "float"); 215 roughness->uniform = true; 216 roughness->constSortPos = cspPotentialPrimitive; 217 218 //matinfo.g slot reserved for AO later 219 meta->addStatement(new GenOp(" @.g = 1.0;\r\n", ormConfig)); 220 meta->addStatement(new GenOp(" @.b = @;\r\n", ormConfig, roughness)); 221 if (fd.features[MFT_InvertRoughness]) 222 meta->addStatement(new GenOp(" @ = 1.0-@;\r\n", roughness, roughness)); 223 meta->addStatement(new GenOp(" @.a = @;\r\n", ormConfig, metalness)); 224 output = meta; 225} 226 227U32 GlowMapHLSL::getOutputTargets(const MaterialFeatureData& fd) const 228{ 229 return fd.features[MFT_isDeferred] ? ShaderFeature::RenderTarget3 : ShaderFeature::DefaultTarget; 230} 231 232//deferred emissive 233void GlowMapHLSL::processPix(Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd) 234{ 235 Var* texCoord = getInTexCoord("texCoord", "float2", componentList); 236 237 // create texture var 238 Var* glowMap = new Var; 239 glowMap->setType("SamplerState"); 240 glowMap->setName("glowMap"); 241 glowMap->uniform = true; 242 glowMap->sampler = true; 243 glowMap->constNum = Var::getTexUnitNum(); 244 245 Var* glowMapTex = new Var; 246 glowMapTex->setName("glowMapTex"); 247 glowMapTex->setType("Texture2D"); 248 glowMapTex->uniform = true; 249 glowMapTex->texture = true; 250 glowMapTex->constNum = glowMap->constNum; 251 LangElement* texOp = new GenOp("@.Sample(@, @)", glowMapTex, glowMap, texCoord); 252 253 Var* glowMul = new Var("glowMul", "float"); 254 glowMul->uniform = true; 255 glowMul->constSortPos = cspPotentialPrimitive; 256 257 Var *targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget)); 258 if (fd.features[MFT_isDeferred]) 259 { 260 targ = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget3)); 261 if (!targ) 262 { 263 // create scene color target var 264 targ = new Var; 265 targ->setType("fragout"); 266 targ->setName(getOutputTargetVarName(ShaderFeature::RenderTarget3)); 267 targ->setStructName("OUT"); 268 output = new GenOp("@ = float4(@.rgb*@,0);", targ, texOp, glowMul); 269 } 270 else 271 { 272 output = new GenOp("@ += float4(@.rgb*@,0);", targ, texOp, glowMul); 273 } 274 } 275 else 276 { 277 output = new GenOp("@ += float4(@.rgb*@,@.a);", targ, texOp, glowMul, targ); 278 } 279 280} 281 282ShaderFeature::Resources GlowMapHLSL::getResources(const MaterialFeatureData& fd) 283{ 284 Resources res; 285 res.numTex = 1; 286 res.numTexReg = 1; 287 288 return res; 289} 290 291void GlowMapHLSL::setTexData(Material::StageData& stageDat, 292 const MaterialFeatureData& fd, 293 RenderPassData& passData, 294 U32& texIndex) 295{ 296 GFXTextureObject* tex = stageDat.getTex(MFT_GlowMap); 297 if (tex) 298 { 299 passData.mTexType[texIndex] = Material::Standard; 300 passData.mSamplerNames[texIndex] = "glowMap"; 301 passData.mTexSlot[texIndex++].texObject = tex; 302 } 303} 304