gBufferConditionerGLSL.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "lighting/advanced/glsl/gBufferConditionerGLSL.h"
  24. #include "shaderGen/featureMgr.h"
  25. #include "gfx/gfxStringEnumTranslate.h"
  26. #include "materials/materialFeatureTypes.h"
  27. #include "materials/materialFeatureData.h"
  28. GBufferConditionerGLSL::GBufferConditionerGLSL( const GFXFormat bufferFormat ) :
  29. Parent( bufferFormat )
  30. {
  31. // Figure out how we should store the normal data. These are the defaults.
  32. mCanWriteNegativeValues = false;
  33. mNormalStorageType = CartesianXYZ;
  34. // Note: We clear to a depth 1 (the w component) so
  35. // that the unrendered parts of the scene end up
  36. // farthest to the camera.
  37. switch(bufferFormat)
  38. {
  39. case GFXFormatR8G8B8A8:
  40. // TODO: Some kind of logic here. Spherical is better, but is more
  41. // expensive.
  42. mNormalStorageType = Spherical;
  43. mBitsPerChannel = 8;
  44. break;
  45. case GFXFormatR16G16B16A16F:
  46. // Floating point buffers don't need to encode negative values
  47. mCanWriteNegativeValues = true;
  48. mNormalStorageType = Spherical;
  49. mBitsPerChannel = 16;
  50. break;
  51. // Store a 32bit depth with a sperical normal in the
  52. // integer 16 format. This gives us perfect depth
  53. // precision and high quality normals within a 64bit
  54. // buffer format.
  55. case GFXFormatR16G16B16A16:
  56. mNormalStorageType = Spherical;
  57. mBitsPerChannel = 16;
  58. break;
  59. case GFXFormatR32G32B32A32F:
  60. mCanWriteNegativeValues = true;
  61. mNormalStorageType = CartesianXYZ;
  62. mBitsPerChannel = 32;
  63. break;
  64. default:
  65. AssertFatal(false, "Unsupported G-Buffer format");
  66. }
  67. }
  68. GBufferConditionerGLSL::~GBufferConditionerGLSL()
  69. {
  70. }
  71. void GBufferConditionerGLSL::processVert( Vector<ShaderComponent*> &componentList,
  72. const MaterialFeatureData &fd )
  73. {
  74. output = NULL;
  75. if( !fd.features[MFT_NormalMap] )
  76. {
  77. // grab incoming vert normal
  78. Var *inNormal = (Var*) LangElement::find( "normal" );
  79. AssertFatal( inNormal, "Something went bad with ShaderGen. The normal should be already defined." );
  80. // grab output for gbuffer normal
  81. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  82. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  83. outNormal->setName( "gbNormal" );
  84. outNormal->setType( "vec3" );
  85. // create objToWorld variable
  86. Var *objToWorld = (Var*) LangElement::find( "objTrans" );
  87. if( !objToWorld )
  88. {
  89. objToWorld = new Var;
  90. objToWorld->setType( "mat4" );
  91. objToWorld->setName( "objTrans" );
  92. objToWorld->uniform = true;
  93. objToWorld->constSortPos = cspPrimitive;
  94. }
  95. // Kick out the world-space normal
  96. LangElement *statement = new GenOp( " @ = vec3(@ * vec4(normalize(@), 0.0));\r\n", outNormal, objToWorld, inNormal );
  97. output = statement;
  98. }
  99. }
  100. void GBufferConditionerGLSL::processPix( Vector<ShaderComponent*> &componentList,
  101. const MaterialFeatureData &fd )
  102. {
  103. // sanity
  104. AssertFatal( fd.features[MFT_EyeSpaceDepthOut], "No depth-out feature enabled! Bad news!" );
  105. MultiLine *meta = new MultiLine;
  106. // grab connector normal
  107. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  108. Var *gbNormal = (Var*) LangElement::find( "gbNormal" );
  109. if( !gbNormal )
  110. {
  111. gbNormal = connectComp->getElement( RT_TEXCOORD );
  112. gbNormal->setName( "gbNormal" );
  113. gbNormal->setType( "vec3" );
  114. gbNormal->mapsToSampler = false;
  115. gbNormal->uniform = false;
  116. }
  117. // find depth
  118. ShaderFeature *depthFeat = FEATUREMGR->getByType( MFT_EyeSpaceDepthOut );
  119. AssertFatal( depthFeat != NULL, "No eye space depth feature found!" );
  120. Var *depth = (Var*) LangElement::find(depthFeat->getOutputVarName());
  121. AssertFatal( depth, "Something went bad with ShaderGen. The depth should be already generated by the EyeSpaceDepthOut feature." );
  122. Var *unconditionedOut = new Var;
  123. unconditionedOut->setType("vec4");
  124. unconditionedOut->setName("normal_depth");
  125. LangElement *outputDecl = new DecOp( unconditionedOut );
  126. // NOTE: We renormalize the normal here as they
  127. // will not stay normalized during interpolation.
  128. meta->addStatement( new GenOp(" @ = @;", outputDecl, new GenOp( "vec4(normalize(@), @)", gbNormal, depth ) ) );
  129. meta->addStatement( assignOutput( unconditionedOut ) );
  130. output = meta;
  131. }
  132. ShaderFeature::Resources GBufferConditionerGLSL::getResources( const MaterialFeatureData &fd )
  133. {
  134. Resources res;
  135. // Passing from VS->PS:
  136. // - world space normal (gbNormal)
  137. res.numTexReg = 1;
  138. return res;
  139. }
  140. Var* GBufferConditionerGLSL::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
  141. {
  142. const bool isCondition = ( methodType == ConditionerFeature::ConditionMethod );
  143. Var *retVal = NULL;
  144. // The uncondition method inputs are changed
  145. if( isCondition )
  146. retVal = Parent::printMethodHeader( methodType, methodName, stream, meta );
  147. else
  148. {
  149. Var *methodVar = new Var;
  150. methodVar->setName(methodName);
  151. methodVar->setType("vec4");
  152. DecOp *methodDecl = new DecOp(methodVar);
  153. Var *prepassSampler = new Var;
  154. prepassSampler->setName("prepassSamplerVar");
  155. prepassSampler->setType("sampler2D");
  156. DecOp *prepassSamplerDecl = new DecOp(prepassSampler);
  157. Var *screenUV = new Var;
  158. screenUV->setName("screenUVVar");
  159. screenUV->setType("vec2");
  160. DecOp *screenUVDecl = new DecOp(screenUV);
  161. Var *bufferSample = new Var;
  162. bufferSample->setName("bufferSample");
  163. bufferSample->setType("vec4");
  164. DecOp *bufferSampleDecl = new DecOp(bufferSample);
  165. meta->addStatement( new GenOp( "@(@, @)\r\n", methodDecl, prepassSamplerDecl, screenUVDecl ) );
  166. meta->addStatement( new GenOp( "{\r\n" ) );
  167. meta->addStatement( new GenOp( " // Sampler g-buffer\r\n" ) );
  168. // The gbuffer has no mipmaps, so use tex2dlod when
  169. // so that the shader compiler can optimize.
  170. meta->addStatement( new GenOp( " @ = texture2DLod(@, @, 0.0);\r\n", bufferSampleDecl, prepassSampler, screenUV ) );
  171. // We don't use this way of passing var's around, so this should cause a crash
  172. // if something uses this improperly
  173. retVal = bufferSample;
  174. }
  175. return retVal;
  176. }
  177. GenOp* GBufferConditionerGLSL::_posnegEncode( GenOp *val )
  178. {
  179. return mCanWriteNegativeValues ? val : new GenOp("0.5 * (@ + 1.0)", val);
  180. }
  181. GenOp* GBufferConditionerGLSL::_posnegDecode( GenOp *val )
  182. {
  183. return mCanWriteNegativeValues ? val : new GenOp("@ * 2.0 - 1.0", val);
  184. }
  185. Var* GBufferConditionerGLSL::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
  186. {
  187. Var *retVar = new Var;
  188. retVar->setType("vec4");
  189. retVar->setName("_gbConditionedOutput");
  190. LangElement *outputDecl = new DecOp( retVar );
  191. switch(mNormalStorageType)
  192. {
  193. case CartesianXYZ:
  194. meta->addStatement( new GenOp( " // g-buffer conditioner: vec4(normal.xyz, depth)\r\n" ) );
  195. meta->addStatement( new GenOp( " @ = vec4(@, @.a);\r\n", outputDecl,
  196. _posnegEncode(new GenOp("@.xyz", unconditionedOutput)), unconditionedOutput ) );
  197. break;
  198. case CartesianXY:
  199. meta->addStatement( new GenOp( " // g-buffer conditioner: vec4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
  200. meta->addStatement( new GenOp( " @ = vec4(@, @.a);", outputDecl,
  201. _posnegEncode(new GenOp("vec3(@.xy, sign(@.z))", unconditionedOutput, unconditionedOutput)), unconditionedOutput ) );
  202. break;
  203. case Spherical:
  204. meta->addStatement( new GenOp( " // g-buffer conditioner: vec4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
  205. meta->addStatement( new GenOp( " @ = vec4(@, 0.0, @.a);\r\n", outputDecl,
  206. _posnegEncode(new GenOp("vec2(atan2(@.y, @.x) / 3.14159265358979323846f, @.z)", unconditionedOutput, unconditionedOutput, unconditionedOutput ) ),
  207. unconditionedOutput ) );
  208. break;
  209. }
  210. // Encode depth into two channels
  211. if(mNormalStorageType != CartesianXYZ)
  212. {
  213. const U64 maxValPerChannel = 1 << mBitsPerChannel;
  214. const U64 extraVal = (maxValPerChannel * maxValPerChannel - 1) - (maxValPerChannel - 1) * 2;
  215. meta->addStatement( new GenOp( " \r\n // Encode depth into hi/lo\r\n" ) );
  216. meta->addStatement( new GenOp( avar( " vec3 _tempDepth = fract(@.a * vec3(1.0, %llu.0, %llu.0));\r\n", maxValPerChannel - 1, extraVal ),
  217. unconditionedOutput ) );
  218. meta->addStatement( new GenOp( avar( " @.zw = _tempDepth.xy - _tempDepth.yz * vec2(1.0/%llu.0, 1.0/%llu.0);\r\n\r\n", maxValPerChannel - 1, maxValPerChannel - 1 ),
  219. retVar ) );
  220. }
  221. AssertFatal( retVar != NULL, avar( "Cannot condition output to buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
  222. return retVar;
  223. }
  224. Var* GBufferConditionerGLSL::_unconditionInput( Var *conditionedInput, MultiLine *meta )
  225. {
  226. Var *retVar = new Var;
  227. retVar->setType("vec4");
  228. retVar->setName("_gbUnconditionedInput");
  229. LangElement *outputDecl = new DecOp( retVar );
  230. switch(mNormalStorageType)
  231. {
  232. case CartesianXYZ:
  233. meta->addStatement( new GenOp( " // g-buffer unconditioner: vec4(normal.xyz, depth)\r\n" ) );
  234. meta->addStatement( new GenOp( " @ = vec4(@, @.a);\r\n", outputDecl,
  235. _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
  236. break;
  237. case CartesianXY:
  238. meta->addStatement( new GenOp( " // g-buffer unconditioner: vec4(normal.xy, depth Hi + z-sign, depth Lo)\r\n" ) );
  239. meta->addStatement( new GenOp( " @ = vec4(@, @.a);\r\n", outputDecl,
  240. _posnegDecode(new GenOp("@.xyz", conditionedInput)), conditionedInput ) );
  241. meta->addStatement( new GenOp( " @.z *= sqrt(1.0 - dot(@.xy, @.xy));\r\n", retVar, retVar, retVar ) );
  242. break;
  243. case Spherical:
  244. meta->addStatement( new GenOp( " // g-buffer unconditioner: vec4(normal.theta, normal.phi, depth Hi, depth Lo)\r\n" ) );
  245. meta->addStatement( new GenOp( " vec2 spGPUAngles = @;\r\n", _posnegDecode(new GenOp("@.xy", conditionedInput)) ) );
  246. meta->addStatement( new GenOp( " vec2 sincosTheta;\r\n" ) );
  247. meta->addStatement( new GenOp( " sincosTheta.x = sin(spGPUAngles.x * 3.14159265358979323846);\r\n" ) );
  248. meta->addStatement( new GenOp( " sincosTheta.y = cos(spGPUAngles.x * 3.14159265358979323846);\r\n" ) );
  249. meta->addStatement( new GenOp( " vec2 sincosPhi = vec2(sqrt(1.0 - spGPUAngles.y * spGPUAngles.y), spGPUAngles.y);\r\n" ) );
  250. meta->addStatement( new GenOp( " @ = vec4(sincosTheta.y * sincosPhi.x, sincosTheta.x * sincosPhi.x, sincosPhi.y, @.a);\r\n", outputDecl, conditionedInput ) );
  251. break;
  252. }
  253. // Recover depth from encoding
  254. if(mNormalStorageType != CartesianXYZ)
  255. {
  256. const U64 maxValPerChannel = 1 << mBitsPerChannel;
  257. meta->addStatement( new GenOp( " \r\n // Decode depth\r\n" ) );
  258. meta->addStatement( new GenOp( avar( " @.w = dot( @.zw, vec2(1.0, 1.0/%llu.0));\r\n", maxValPerChannel - 1 ),
  259. retVar, conditionedInput ) );
  260. }
  261. AssertFatal( retVar != NULL, avar( "Cannot uncondition input from buffer format: %s", GFXStringTextureFormat[getBufferFormat()] ) );
  262. return retVar;
  263. }