gBufferConditionerGLSL.cpp
Engine/source/lighting/advanced/glsl/gBufferConditionerGLSL.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/glsl/gBufferConditionerGLSL.h" 26 27#include "shaderGen/featureMgr.h" 28#include "gfx/gfxStringEnumTranslate.h" 29#include "materials/materialFeatureTypes.h" 30#include "materials/materialFeatureData.h" 31#include "shaderGen/GLSL/shaderFeatureGLSL.h" 32 33 34GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat, const NormalSpace nrmSpace ) : 35 Parent( bufferFormat ) 36{ 37 // Figure out how we should store the normal data. These are the defaults. 38 mCanWriteNegativeValues = false; 39 mNormalStorageType = CartesianXYZ; 40 41 // Note: We clear to a depth 1 (the w component) so 42 // that the unrendered parts of the scene end up 43 // farthest to the camera. 44 const NormalStorage &twoCmpNrmStorageType = ( nrmSpace == WorldSpace ? Spherical : LambertAzimuthal ); 45 switch(bufferFormat) 46 { 47 case GFXFormatR8G8B8A8: 48 mNormalStorageType = twoCmpNrmStorageType; 49 mBitsPerChannel = 8; 50 break; 51 52 case GFXFormatR16G16B16A16F: 53 // Floating point buffers don't need to encode negative values 54 mCanWriteNegativeValues = true; 55 mNormalStorageType = twoCmpNrmStorageType; 56 mBitsPerChannel = 16; 57 break; 58 59 // Store a 32bit depth with a sperical normal in the 60 // integer 16 format. This gives us perfect depth 61 // precision and high quality normals within a 64bit 62 // buffer format. 63 case GFXFormatR16G16B16A16: 64 mNormalStorageType = twoCmpNrmStorageType; 65 mBitsPerChannel = 16; 66 break; 67 68 case GFXFormatR32G32B32A32F: 69 mCanWriteNegativeValues = true; 70 mNormalStorageType = CartesianXYZ; 71 mBitsPerChannel = 32; 72 break; 73 74 default: 75 AssertFatal(false, "Unsupported G-Buffer format"); 76 } 77} 78 79GBufferConditionerGLSL::~GBufferConditionerGLSL() 80{ 81} 82 83void GBufferConditionerGLSL::processVert( Vector<ShaderComponent*> &componentList, 84 const MaterialFeatureData &fd ) 85{ 86 // If we have a normal map then that feature will 87 // take care of passing gbNormal to the pixel shader. 88 if ( fd.features[MFT_NormalMap] ) 89 return; 90 91 MultiLine *meta = new MultiLine; 92 output = meta; 93 94 // grab incoming vert normal 95 Var *inNormal = (Var*) LangElement::find( "normal" ); 96 if (!inNormal) 97 { 98 inNormal = new Var("normal", "vec3"); 99 meta->addStatement(new GenOp(" @ = vec3( 0.0, 0.0, 1.0 );\r\n", new DecOp(inNormal))); 100 Con::errorf("ShagerGen: Something went bad with ShaderGen. The normal should be already defined."); 101 } 102 AssertFatal( inNormal, "Something went bad with ShaderGen. The normal should be already defined." ); 103 104 // grab output for gbuffer normal 105 ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); 106 Var *outNormal = connectComp->getElement( RT_TEXCOORD ); 107 outNormal->setName( "gbNormal" ); 108 outNormal->setStructName( "OUT" ); 109 outNormal->setType( "float3" ); 110 111 if( !fd.features[MFT_ParticleNormal] ) 112 { 113 // Kick out the view-space normal 114 115 // TODO: Total hack because Conditioner is directly derived 116 // from ShaderFeature and not from ShaderFeatureGLSL. 117 NamedFeatureGLSL dummy( String::EmptyString ); 118 dummy.setInstancingFormat( mInstancingFormat ); 119 Var *worldViewOnly = dummy.getWorldView( componentList, fd.features[MFT_UseInstancing], meta ); 120 121 meta->addStatement( new GenOp(" @ = tMul(@, float4( normalize(@), 0.0 ) ).xyz;\r\n", 122 outNormal, worldViewOnly, inNormal ) ); 123 } 124 else 125 { 126 // Assume the particle normal generator has already put this in view space 127 // and normalized it 128 meta->addStatement( new GenOp( " @ = @;\r\n", outNormal, inNormal ) ); 129 } 130} 131 132void GBufferConditionerGLSL::processPix( Vector<ShaderComponent*> &componentList, 133 const MaterialFeatureData &fd ) 134{ 135 // sanity 136 AssertFatal( fd.features[MFT_EyeSpaceDepthOut], "No depth-out feature enabled! Bad news!" ); 137 138 MultiLine *meta = new MultiLine; 139 140 // grab connector normal 141 ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] ); 142 Var *gbNormal = (Var*) LangElement::find( "gbNormal" ); 143 if( !gbNormal ) 144 { 145 gbNormal = connectComp->getElement( RT_TEXCOORD ); 146 gbNormal->setName( "gbNormal" ); 147 gbNormal->setStructName( "IN" ); 148 gbNormal->setType( "float3" ); 149 gbNormal->uniform = false; 150 } 151 152 // find depth 153 ShaderFeature *depthFeat = FEATUREMGR->getByType( MFT_EyeSpaceDepthOut ); 154 AssertFatal( depthFeat != NULL, "No eye space depth feature found!" ); 155 156 Var *depth = (Var*) LangElement::find(depthFeat->getOutputVarName()); 157 AssertFatal( depth, "Something went bad with ShaderGen. The depth should be already generated by the EyeSpaceDepthOut feature." ); 158 159 160 Var *unconditionedOut = new Var; 161 unconditionedOut->setType("float4"); 162 unconditionedOut->setName("normal_depth"); 163 164 LangElement *outputDecl = new DecOp( unconditionedOut ); 165 166 // If we're doing deferred blending then we need 167 // to steal away the alpha channel before the 168 // conditioner stomps on it. 169 Var *alphaVal = NULL; 170 if ( fd.features[ MFT_IsTranslucentZWrite ] ) 171 { 172 alphaVal = new Var( "outAlpha", "float" ); 173 meta->addStatement( new GenOp( " @ = OUT_col1.a; // MFT_IsTranslucentZWrite\r\n", new DecOp( alphaVal ) ) ); 174 } 175 176 // NOTE: We renormalize the normal here as they 177 // will not stay normalized during interpolation. 178 meta->addStatement( new GenOp(" @ = @;", outputDecl, new GenOp( "float4(normalize(@), @)", gbNormal, depth ) ) ); 179 meta->addStatement( assignOutput( unconditionedOut ) ); 180 181 // If we have an alpha var then we're doing deferred lerp blending. 182 if ( alphaVal ) 183 { 184 Var *outColor = (Var*)LangElement::find( getOutputTargetVarName( DefaultTarget ) ); 185 meta->addStatement( new GenOp( " @.ba = float2( 0, @ ); // MFT_IsTranslucentZWrite\r\n", outColor, alphaVal ) ); 186 } 187 188 output = meta; 189} 190 191ShaderFeature::Resources GBufferConditionerGLSL::getResources( const MaterialFeatureData &fd ) 192{ 193 Resources res; 194 195 // Passing from VS->PS: 196 // - world space normal (gbNormal) 197 res.numTexReg = 1; 198 199 return res; 200} 201 202Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta ) 203{ 204 const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod ); 205 206 Var *retVal = NULL; 207 208 // The uncondition method inputs are changed 209 if( isCondition ) 210 retVal = Parent::printMethodHeader( methodType, methodName, stream, meta ); 211 else 212 { 213 Var *methodVar = new Var; 214 methodVar->setName(methodName); 215 methodVar->setType("float4"); 216 DecOp *methodDecl = new DecOp(methodVar); 217 218 Var *deferredSampler = new Var; 219 deferredSampler->setName("deferredSamplerVar"); 220 deferredSampler->setType("sampler2D"); 221 DecOp *deferredSamplerDecl = new DecOp(deferredSampler); 222 223 Var *screenUV = new Var; 224 screenUV->setName("screenUVVar"); 225 screenUV->setType("float2"); 226 DecOp *screenUVDecl = new DecOp(screenUV); 227 228 Var *bufferSample = new Var; 229 bufferSample->setName("bufferSample"); 230 bufferSample->setType("float4"); 231 DecOp *bufferSampleDecl = new DecOp(bufferSample); 232 233 meta->addStatement( new GenOp( "@(@, @)\r\n", methodDecl, deferredSamplerDecl, screenUVDecl ) ); 234 235 meta->addStatement( new GenOp( "{\r\n" ) ); 236 237 meta->addStatement( new GenOp( " // Sampler g-buffer\r\n" ) ); 238 239 // The gbuffer has no mipmaps, so use tex2dlod when 240 // possible so that the shader compiler can optimize. 241 meta->addStatement( new GenOp( "@ = tex2Dlod(@, float4(@,0,0));\r\n", bufferSampleDecl, deferredSampler, screenUV ) ); 242 243 // We don't use this way of passing var's around, so this should cause a crash 244 // if something uses this improperly 245 retVal = bufferSample; 246 } 247 248 return retVal; 249} 250 251GenOp* GBufferConditionerGLSL::_posnegEncode( GenOp *val ) 252{ 253 if(mNormalStorageType == LambertAzimuthal) 254 return mCanWriteNegativeValues ? val : new GenOp(avar("(%f * (@ + %f))", 1.0f/(M_SQRT2_F * 2.0f), M_SQRT2_F), val); 255 else 256 return mCanWriteNegativeValues ? val : new GenOp("(0.5 * (@ + 1.0))", val); 257} 258 259GenOp* GBufferConditionerGLSL::_posnegDecode( GenOp *val ) 260{ 261 if(mNormalStorageType == LambertAzimuthal) 262 return mCanWriteNegativeValues ? val : new GenOp(avar("(@ * %f - %f)", M_SQRT2_F * 2.0f, M_SQRT2_F), val); 263 else 264 return mCanWriteNegativeValues ? val : new GenOp("(@ * 2.0 - 1.0)", val); 265} 266 267Var* GBufferConditionerGLSL::_conditionOutput( Var *unconditionedOutput, MultiLine *meta ) 268{ 269 Var *retVar = new Var; 270 retVar->setType("float4"); 271 retVar->setName("_gbConditionedOutput"); 272 LangElement *outputDecl = new DecOp( retVar ); 273 274 switch(mNormalStorageType) 275 { 276 case CartesianXYZ: 277 meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.xyz, depth)\r\n" ) ); 278 meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl, 279 _posnegEncode(new GenOp("@.xyz", unconditionedOutput)), unconditionedOutput ) ); 280 break; 281 282 case CartesianXY: 283 meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) ); 284 meta->addStatement( new GenOp( " @ = float4(@, @.a);", outputDecl, 285 _posnegEncode(new GenOp("float3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) ); 286 break; 287 288 case Spherical: 289 meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) ); 290 meta->addStatement( new GenOp( " @ = float4(@, 0.0, @.a);\r\n", outputDecl, 291 _posnegEncode(new GenOp("float2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ), 292 unconditionedOutput ) ); 293 294 // HACK: This fixes the noise present when using a floating point 295 // gbuffer on Geforce cards and the "flat areas unlit" issues. 296 // 297 // We need work around atan2() above to fix this issue correctly 298 // without the extra overhead of this test. 299 // 300 meta->addStatement( new GenOp( " if ( abs( dot( @.xyz, float3( 0.0, 0.0, 1.0 ) ) ) > 0.999f ) @ = float4( 0, 1 * sign( @.z ), 0, @.a );\r\n", 301 unconditionedOutput, retVar, unconditionedOutput, unconditionedOutput ) ); 302 break; 303 304 case LambertAzimuthal: 305 //http://en.wikipedia.org/wiki/Lambert_azimuthal_equal-area_projection 306 // 307 // Note we're casting to half to use partial precision 308 // sqrt which is much faster on older Geforces while 309 // still being acceptable for normals. 310 // 311 meta->addStatement( new GenOp( " // g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) ); 312 meta->addStatement( new GenOp( " @ = float4(@, 0.0, @.a);\r\n", outputDecl, 313 _posnegEncode(new GenOp("sqrt(half(2.0/(1.0 - @.y))) * half2(@.xz)", unconditionedOutput, unconditionedOutput)), 314 unconditionedOutput ) ); 315 break; 316 } 317 318 // Encode depth into two channels 319 if(mNormalStorageType != CartesianXYZ) 320 { 321 const U64 maxValPerChannel = (U64)1 << mBitsPerChannel; 322 meta->addStatement( new GenOp( " \r\n // Encode depth into hi/lo\r\n" ) ); 323 meta->addStatement( new GenOp( avar( " float2 _tempDepth = frac(@.a * float2(1.0, %llu.0));\r\n", maxValPerChannel - 1 ), 324 unconditionedOutput ) ); 325 meta->addStatement( new GenOp( avar( " @.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/%llu.0, 0.0);\r\n\r\n", maxValPerChannel - 1 ), 326 retVar ) ); 327 } 328 329 AssertFatal( retVar != NULL, avar( "Cannot condition output to buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) ); 330 return retVar; 331} 332 333Var* GBufferConditionerGLSL::_unconditionInput( Var *conditionedInput, MultiLine *meta ) 334{ 335 Var *retVar = new Var; 336 retVar->setType("float4"); 337 retVar->setName("_gbUnconditionedInput"); 338 LangElement *outputDecl = new DecOp( retVar ); 339 340 switch(mNormalStorageType) 341 { 342 case CartesianXYZ: 343 meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.xyz, depth)\r\n" ) ); 344 meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl, 345 _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) ); 346 break; 347 348 case CartesianXY: 349 meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) ); 350 meta->addStatement( new GenOp( " @ = float4(@, @.a);\r\n", outputDecl, 351 _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) ); 352 meta->addStatement( new GenOp( " @.z *= sqrt(1.0 - dot(@.xy, @.xy));\r\n", retVar, retVar, retVar ) ); 353 break; 354 355 case Spherical: 356 meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) ); 357 meta->addStatement( new GenOp( " float2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) ); 358 meta->addStatement( new GenOp( " float2 sincosTheta;\r\n" ) ); 359 meta->addStatement( new GenOp( " sincos(spGPUAngles.x * 3.14159265358979323846f, sincosTheta.x, sincosTheta.y);\r\n" ) ); 360 meta->addStatement( new GenOp( " float2 sincosPhi = float2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) ); 361 meta->addStatement( new GenOp( " @ = float4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) ); 362 break; 363 364 case LambertAzimuthal: 365 // Note we're casting to half to use partial precision 366 // sqrt which is much faster on older Geforces while 367 // still being acceptable for normals. 368 // 369 meta->addStatement( new GenOp( " // g-buffer unconditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) ); 370 meta->addStatement( new GenOp( " float2 _inpXY = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) ); 371 meta->addStatement( new GenOp( " float _xySQ = dot(_inpXY, _inpXY);\r\n" ) ); 372 meta->addStatement( new GenOp( " @ = float4( sqrt(half(1.0 - (_xySQ / 4.0))) * _inpXY, -1.0 + (_xySQ / 2.0), @.a).xzyw;\r\n", outputDecl, conditionedInput ) ); 373 break; 374 } 375 376 // Recover depth from encoding 377 if(mNormalStorageType != CartesianXYZ) 378 { 379 const U64 maxValPerChannel = (U64)1 << mBitsPerChannel; 380 meta->addStatement( new GenOp( " \r\n // Decode depth\r\n" ) ); 381 meta->addStatement( new GenOp( avar( " @.w = dot( @.zw, float2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ), 382 retVar, conditionedInput ) ); 383 } 384 385 386 AssertFatal( retVar != NULL, avar( "Cannot uncondition input from buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) ); 387 return retVar; 388} 389 390