Torque3D Documentation / _generateds / gBufferConditionerHLSL.cpp

gBufferConditionerHLSL.cpp

Engine/source/lighting/advanced/hlsl/gBufferConditionerHLSL.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/hlsl/gBufferConditionerHLSL.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/hlsl/shaderFeatureHLSL.h"
 32#include "gfx/gfxDevice.h"
 33
 34GBufferConditionerHLSL::GBufferConditionerHLSL( 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
 79GBufferConditionerHLSL::~GBufferConditionerHLSL()
 80{
 81}
 82
 83void GBufferConditionerHLSL::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", "float3");
 99      meta->addStatement(new GenOp("   @ = float3( 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 ShaderFeatureHLSL.
117      NamedFeatureHLSL dummy( String::EmptyString );
118      dummy.setInstancingFormat( mInstancingFormat );
119      Var *worldViewOnly = dummy.getWorldView( componentList, fd.features[MFT_UseInstancing], meta );
120
121      meta->addStatement(  new GenOp("   @ = mul(@, 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 GBufferConditionerHLSL::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
171   Var* targ = (Var*)LangElement::find(getOutputTargetVarName(DefaultTarget));
172   if (fd.features[MFT_isDeferred])
173      targ = (Var*)LangElement::find(getOutputTargetVarName(RenderTarget1));
174
175   if ( fd.features[ MFT_IsTranslucentZWrite ] )
176   {
177      alphaVal = new Var( "outAlpha", "float" );
178      meta->addStatement( new GenOp( "   @ = @.a; // MFT_IsTranslucentZWrite\r\n", new DecOp( alphaVal ), targ) );
179   }
180
181
182   // NOTE: We renormalize the normal here as they
183   // will not stay normalized during interpolation.
184   meta->addStatement( new GenOp("   @ = @;", outputDecl, new GenOp( "float4(normalize(@), @)", gbNormal, depth ) ) );
185   meta->addStatement( assignOutput( unconditionedOut ) );
186
187   // If we have an alpha var then we're doing deferred lerp blending.
188   if ( alphaVal )
189   {
190      meta->addStatement( new GenOp( "   @.ba = float2( 0, @ ); // MFT_IsTranslucentZWrite\r\n", targ, alphaVal ) );
191   }
192
193   output = meta;
194}
195
196ShaderFeature::Resources GBufferConditionerHLSL::getResources( const MaterialFeatureData &fd )
197{
198   Resources res;
199
200   // Passing from VS->PS:
201   // - world space normal (gbNormal)
202   res.numTexReg = 1;
203
204   return res; 
205}
206
207Var* GBufferConditionerHLSL::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
208{
209   const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod );
210
211   Var *retVal = NULL;
212
213   // The uncondition method inputs are changed
214   if( isCondition )
215      retVal = Parent::printMethodHeader( methodType, methodName, stream, meta );
216   else
217   {
218      Var *methodVar = new Var;
219      methodVar->setName(methodName);
220      methodVar->setType("inline float4");
221      DecOp *methodDecl = new DecOp(methodVar);
222
223      Var *deferredSampler = new Var;
224      deferredSampler->setName("deferredSamplerVar");
225      deferredSampler->setType("SamplerState");
226      DecOp *deferredSamplerDecl = new DecOp(deferredSampler);
227
228      Var *screenUV = new Var;
229      screenUV->setName("screenUVVar");
230      screenUV->setType("float2");
231      DecOp *screenUVDecl = new DecOp(screenUV);
232
233      DecOp *deferredTexDecl = NULL;
234
235      Var *deferredTex = new Var;
236      deferredTex->setName("deferredTexVar");
237      deferredTex->setType("Texture2D");
238      deferredTex->texture = true;
239      deferredTex->constNum = deferredSampler->constNum;
240      deferredTexDecl = new DecOp(deferredTex);
241
242      Var *bufferSample = new Var;
243      bufferSample->setName("bufferSample");
244      bufferSample->setType("float4");
245      DecOp *bufferSampleDecl = new DecOp(bufferSample); 
246
247      meta->addStatement(new GenOp("@(@, @, @)\r\n", methodDecl, deferredSamplerDecl, deferredTexDecl, screenUVDecl));
248
249      meta->addStatement( new GenOp( "{\r\n" ) );
250
251      meta->addStatement( new GenOp( "   // Sampler g-buffer\r\n" ) );
252
253      // The gbuffer has no mipmaps, so use tex2dlod when 
254      // possible so that the shader compiler can optimize.
255      meta->addStatement(new GenOp("      @ = @.SampleLevel(@, @,0);\r\n", bufferSampleDecl, deferredTex, deferredSampler, screenUV));
256
257      // We don't use this way of passing var's around, so this should cause a crash
258      // if something uses this improperly
259      retVal = bufferSample;
260   }
261
262   return retVal;
263}
264
265GenOp* GBufferConditionerHLSL::_posnegEncode( GenOp *val )
266{
267   if(mNormalStorageType == LambertAzimuthal)
268      return mCanWriteNegativeValues ? val : new GenOp(avar("(%f * (@ + %f))", 1.0f/(M_SQRT2_F * 2.0f), M_SQRT2_F), val);
269   else
270      return mCanWriteNegativeValues ? val : new GenOp("(0.5 * (@ + 1.0))", val);
271}
272
273GenOp* GBufferConditionerHLSL::_posnegDecode( GenOp *val )
274{
275   if(mNormalStorageType == LambertAzimuthal)
276      return mCanWriteNegativeValues ? val : new GenOp(avar("(@ * %f - %f)", M_SQRT2_F * 2.0f, M_SQRT2_F), val);
277   else
278      return mCanWriteNegativeValues ? val : new GenOp("(@ * 2.0 - 1.0)", val);
279}
280
281Var* GBufferConditionerHLSL::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
282{
283   Var *retVar = new Var;
284   retVar->setType("float4");
285   retVar->setName("_gbConditionedOutput");
286   LangElement *outputDecl = new DecOp( retVar );
287
288   switch(mNormalStorageType)
289   {
290      case CartesianXYZ:
291         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.xyz, depth)\r\n" ) );
292         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
293            _posnegEncode(new GenOp("@.xyz", unconditionedOutput)), unconditionedOutput ) );
294         break;
295
296      case CartesianXY:
297         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
298         meta->addStatement( new GenOp( "   @ = float4(@, @.a);", outputDecl, 
299            _posnegEncode(new GenOp("float3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) );
300         break;
301
302      case Spherical:
303         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
304         meta->addStatement( new GenOp( "   @ = float4(@, 0.0, @.a);\r\n", outputDecl, 
305            _posnegEncode(new GenOp("float2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ), 
306            unconditionedOutput ) );
307
308         // HACK: This fixes the noise present when using a floating point
309         // gbuffer on Geforce cards and the "flat areas unlit" issues.
310         //
311         // We need work around atan2() above to fix this issue correctly
312         // without the extra overhead of this test.
313         //
314         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", 
315            unconditionedOutput, retVar, unconditionedOutput, unconditionedOutput ) );
316         break;
317
318      case LambertAzimuthal:
319         //http://en.wikipedia.org/wiki/Lambert_azimuthal_equal-area_projection
320         //
321         // Note we're casting to half to use partial precision
322         // sqrt which is much faster on older Geforces while
323         // still being acceptable for normals.
324         //
325         meta->addStatement( new GenOp( "   // g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
326         meta->addStatement( new GenOp( "   @ = float4(@, 0.0, @.a);\r\n", outputDecl, 
327            _posnegEncode(new GenOp("sqrt(half(2.0/(1.0 - @.y))) * half2(@.xz)", unconditionedOutput, unconditionedOutput)), 
328            unconditionedOutput ) );
329         break;
330   }
331
332   // Encode depth into two channels
333   if(mNormalStorageType != CartesianXYZ)
334   {
335      const U64 maxValPerChannel = (U64)1 << mBitsPerChannel;
336      meta->addStatement( new GenOp( "   \r\n   // Encode depth into hi/lo\r\n" ) );
337      meta->addStatement( new GenOp( avar( "   float2 _tempDepth = frac(@.a * float2(1.0, %llu.0));\r\n", maxValPerChannel - 1 ), 
338         unconditionedOutput ) );
339      meta->addStatement( new GenOp( avar( "   @.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/%llu.0, 0.0);\r\n\r\n", maxValPerChannel - 1 ), 
340         retVar ) );
341   }
342
343   AssertFatal( retVar != NULL, avar( "Cannot condition output to buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
344   return retVar; 
345}
346
347Var* GBufferConditionerHLSL::_unconditionInput( Var *conditionedInput, MultiLine *meta )
348{
349   Var *retVar = new Var;
350   retVar->setType("float4");
351   retVar->setName("_gbUnconditionedInput");
352   LangElement *outputDecl = new DecOp( retVar );
353
354   switch(mNormalStorageType)
355   {
356      case CartesianXYZ:
357         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.xyz, depth)\r\n" ) );
358         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
359            _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
360         break;
361
362      case CartesianXY:
363         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
364         meta->addStatement( new GenOp( "   @ = float4(@, @.a);\r\n", outputDecl, 
365            _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
366         meta->addStatement( new GenOp( "   @.z *= sqrt(1.0 - dot(@.xy, @.xy));\r\n", retVar, retVar, retVar ) );
367         break;
368
369      case Spherical:
370         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
371         meta->addStatement( new GenOp( "   float2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
372         meta->addStatement( new GenOp( "   float2 sincosTheta;\r\n" ) );
373         meta->addStatement( new GenOp( "   sincos(spGPUAngles.x * 3.14159265358979323846f, sincosTheta.x, sincosTheta.y);\r\n" ) );
374         meta->addStatement( new GenOp( "   float2 sincosPhi = float2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) );
375         meta->addStatement( new GenOp( "   @ = float4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) );
376         break;
377
378      case LambertAzimuthal:
379         // Note we're casting to half to use partial precision
380         // sqrt which is much faster on older Geforces while
381         // still being acceptable for normals.
382         //      
383         meta->addStatement( new GenOp( "   // g-buffer unconditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)\r\n" ) );
384         meta->addStatement( new GenOp( "   float2 _inpXY = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
385         meta->addStatement( new GenOp( "   float _xySQ = dot(_inpXY, _inpXY);\r\n" ) );
386         meta->addStatement( new GenOp( "   @ = float4( sqrt(half(1.0 - (_xySQ / 4.0))) * _inpXY, -1.0 + (_xySQ / 2.0), @.a).xzyw;\r\n", outputDecl, conditionedInput ) );
387         break;
388   }
389
390   // Recover depth from encoding
391   if(mNormalStorageType != CartesianXYZ)
392   {
393      const U64 maxValPerChannel = (U64)1 << mBitsPerChannel;
394      meta->addStatement( new GenOp( "   \r\n   // Decode depth\r\n" ) );
395      meta->addStatement( new GenOp( avar( "   @.w = dot( @.zw, float2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ), 
396         retVar, conditionedInput ) );
397   }
398
399
400   AssertFatal( retVar != NULL, avar( "Cannot uncondition input from buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
401   return retVar; 
402}
403
404