bumpGLSL.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "shaderGen/GLSL/bumpGLSL.h"
  24. #include "shaderGen/shaderOp.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "materials/matInstance.h"
  27. #include "materials/processedMaterial.h"
  28. #include "materials/materialFeatureTypes.h"
  29. #include "shaderGen/shaderGenVars.h"
  30. #include "shaderGen/shaderGen.h"
  31. void BumpFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  32. const MaterialFeatureData &fd )
  33. {
  34. MultiLine *meta = new MultiLine;
  35. output = meta;
  36. const bool useTexAnim = fd.features[MFT_TexAnim];
  37. // Output the texture coord.
  38. getOutTexCoord( "texCoord",
  39. "vec2",
  40. useTexAnim,
  41. meta,
  42. componentList );
  43. const bool useFoliageTexCoord = fd.features[MFT_Foliage];
  44. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  45. addOutDetailTexCoord( componentList,
  46. meta,
  47. useTexAnim, useFoliageTexCoord);
  48. // Also output the worldToTanget transform which
  49. // we use to create the world space normal.
  50. getOutWorldToTangent( componentList, meta, fd );
  51. }
  52. void BumpFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  53. const MaterialFeatureData &fd )
  54. {
  55. MultiLine *meta = new MultiLine;
  56. output = meta;
  57. // Get the texture coord.
  58. Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
  59. // Sample the bumpmap.
  60. Var *bumpMap = getNormalMapTex();
  61. LangElement *texOp = NULL;
  62. //Handle atlased textures
  63. // http://www.infinity-universe.com/Infinity/index.php?option=com_content&task=view&id=65&Itemid=47
  64. if(fd.features[MFT_NormalMapAtlas])
  65. {
  66. // This is a big block of code, so put a comment in the shader code
  67. meta->addStatement( new GenOp( " // Atlased texture coordinate calculation (see BumpFeat*LSL for details)\r\n") );
  68. Var *atlasedTex = new Var;
  69. atlasedTex->setName("atlasedBumpCoord");
  70. atlasedTex->setType( "vec2" );
  71. LangElement *atDecl = new DecOp(atlasedTex);
  72. // Parameters of the texture atlas
  73. Var *atParams = new Var;
  74. atParams->setType( "float4" );
  75. atParams->setName("bumpAtlasParams");
  76. atParams->uniform = true;
  77. atParams->constSortPos = cspPotentialPrimitive;
  78. // Parameters of the texture (tile) this object is using in the atlas
  79. Var *tileParams = new Var;
  80. tileParams->setType( "float4" );
  81. tileParams->setName("bumpAtlasTileParams");
  82. tileParams->uniform = true;
  83. tileParams->constSortPos = cspPotentialPrimitive;
  84. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  85. if(is_sm3)
  86. {
  87. // Figure out the mip level
  88. meta->addStatement( new GenOp( " float2 _dx_bump = ddx(@ * @.z);\r\n", texCoord, atParams ) );
  89. meta->addStatement( new GenOp( " float2 _dy_bump = ddy(@ * @.z);\r\n", texCoord, atParams ) );
  90. meta->addStatement( new GenOp( " float mipLod_bump = 0.5 * log2(max(dot(_dx_bump, _dx_bump), dot(_dy_bump, _dy_bump)));\r\n"));
  91. meta->addStatement( new GenOp( " mipLod_bump = clamp(mipLod_bump, 0.0, @.w);\r\n", atParams));
  92. // And the size of the mip level
  93. meta->addStatement(new GenOp(" float mipPixSz_bump = pow(2.0, @.w - mipLod_bump);\r\n", atParams));
  94. meta->addStatement( new GenOp( " float2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams ) );
  95. }
  96. else
  97. {
  98. meta->addStatement(new GenOp(" float2 mipSz = float2(1.0, 1.0);\r\n"));
  99. }
  100. // Tiling mode
  101. if( true ) // Wrap
  102. meta->addStatement( new GenOp( " @ = frac(@);\r\n", atDecl, texCoord ) );
  103. else // Clamp
  104. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, texCoord));
  105. // Finally scale/offset, and correct for filtering
  106. meta->addStatement( new GenOp( " @ = @ * ((mipSz_bump * @.xy - 1.0) / mipSz_bump) + 0.5 / mipSz_bump + @.xy * @.xy;\r\n",
  107. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  108. // Add a newline
  109. meta->addStatement(new GenOp( "\r\n"));
  110. if(is_sm3)
  111. {
  112. texOp = new GenOp( "tex2Dlod(@, float4(@, 0.0, mipLod_bump))", bumpMap, texCoord );
  113. }
  114. else
  115. {
  116. texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
  117. }
  118. }
  119. else
  120. {
  121. texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
  122. }
  123. Var *bumpNorm = new Var( "bumpNormal", "float4" );
  124. meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) );
  125. // If we have a detail normal map we add the xy coords of
  126. // it to the base normal map. This gives us the effect we
  127. // want with few instructions and minial artifacts.
  128. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  129. {
  130. bumpMap = new Var;
  131. bumpMap->setType( "sampler2D" );
  132. bumpMap->setName( "detailBumpMap" );
  133. bumpMap->uniform = true;
  134. bumpMap->sampler = true;
  135. bumpMap->constNum = Var::getTexUnitNum();
  136. texCoord = getInTexCoord( "detCoord", "vec2", componentList );
  137. texOp = new GenOp( "tex2D(@, @)", bumpMap, texCoord );
  138. Var *detailBump = new Var;
  139. detailBump->setName( "detailBump" );
  140. detailBump->setType( "float4" );
  141. meta->addStatement( expandNormalMap( texOp, new DecOp( detailBump ), detailBump, fd ) );
  142. Var *detailBumpScale = new Var;
  143. detailBumpScale->setType( "float" );
  144. detailBumpScale->setName( "detailBumpStrength" );
  145. detailBumpScale->uniform = true;
  146. detailBumpScale->constSortPos = cspPass;
  147. meta->addStatement( new GenOp( " @.xy += @.xy * @;\r\n", bumpNorm, detailBump, detailBumpScale ) );
  148. }
  149. // We transform it into world space by reversing the
  150. // multiplication by the worldToTanget transform.
  151. Var *wsNormal = new Var( "wsNormal", "vec3" );
  152. Var *worldToTanget = getInWorldToTangent( componentList );
  153. meta->addStatement( new GenOp( " @ = normalize( tMul( @.xyz, @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
  154. }
  155. ShaderFeature::Resources BumpFeatGLSL::getResources( const MaterialFeatureData &fd )
  156. {
  157. Resources res;
  158. // If we have no parallax then we bring on the normal tex.
  159. if ( !fd.features[MFT_Parallax] )
  160. res.numTex = 1;
  161. // Only the parallax or diffuse map will add texture
  162. // coords other than us.
  163. if ( !fd.features[MFT_Parallax] &&
  164. !fd.features[MFT_DiffuseMap] &&
  165. !fd.features[MFT_OverlayMap] &&
  166. !fd.features[MFT_DetailMap] )
  167. res.numTexReg++;
  168. // We pass the world to tanget space transform.
  169. res.numTexReg += 3;
  170. // Do we have detail normal mapping?
  171. if ( fd.features[MFT_DetailNormalMap] )
  172. {
  173. res.numTex++;
  174. if ( !fd.features[MFT_DetailMap] )
  175. res.numTexReg++;
  176. }
  177. return res;
  178. }
  179. void BumpFeatGLSL::setTexData( Material::StageData &stageDat,
  180. const MaterialFeatureData &fd,
  181. RenderPassData &passData,
  182. U32 &texIndex )
  183. {
  184. // If we had a parallax feature then it takes
  185. // care of hooking up the normal map texture.
  186. if ( fd.features[MFT_Parallax] )
  187. return;
  188. if ( fd.features[MFT_NormalMap] )
  189. {
  190. passData.mTexType[ texIndex ] = Material::Bump;
  191. passData.mSamplerNames[ texIndex ] = "bumpMap";
  192. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
  193. }
  194. if ( fd.features[ MFT_DetailNormalMap ] )
  195. {
  196. passData.mTexType[ texIndex ] = Material::DetailBump;
  197. passData.mSamplerNames[ texIndex ] = "detailBumpMap";
  198. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
  199. }
  200. }
  201. ParallaxFeatGLSL::ParallaxFeatGLSL()
  202. : mIncludeDep(ShaderGen::smCommonShaderPath + String("/gl/torque.glsl" ))
  203. {
  204. addDependency( &mIncludeDep );
  205. }
  206. Var* ParallaxFeatGLSL::_getUniformVar( const char *name, const char *type, ConstantSortPosition csp )
  207. {
  208. Var *theVar = (Var*)LangElement::find( name );
  209. if ( !theVar )
  210. {
  211. theVar = new Var;
  212. theVar->setType( type );
  213. theVar->setName( name );
  214. theVar->uniform = true;
  215. theVar->constSortPos = csp;
  216. }
  217. return theVar;
  218. }
  219. void ParallaxFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  220. const MaterialFeatureData &fd )
  221. {
  222. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  223. "ParallaxFeatGLSL::processVert - We don't support SM 1.x!" );
  224. MultiLine *meta = new MultiLine;
  225. // Add the texture coords.
  226. getOutTexCoord( "texCoord",
  227. "vec2",
  228. fd.features[MFT_TexAnim],
  229. meta,
  230. componentList );
  231. // Grab the input position.
  232. Var *inPos = (Var*)LangElement::find( "inPosition" );
  233. if ( !inPos )
  234. inPos = (Var*)LangElement::find( "position" );
  235. // Get the object space eye position and the
  236. // object to tangent space transform.
  237. Var *eyePos = _getUniformVar( "eyePos", "vec3", cspPrimitive );
  238. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  239. // Now send the negative view vector in tangent space to the pixel shader.
  240. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  241. Var *outNegViewTS = connectComp->getElement( RT_TEXCOORD );
  242. outNegViewTS->setName( "outNegViewTS" );
  243. outNegViewTS->setStructName( "OUT" );
  244. outNegViewTS->setType( "vec3" );
  245. meta->addStatement( new GenOp( " @ = tMul( @, float3( @.xyz - @ ) );\r\n",
  246. outNegViewTS, objToTangentSpace, inPos, eyePos ) );
  247. // If we have texture anim matrix the tangent
  248. // space view vector may need to be rotated.
  249. Var *texMat = (Var*)LangElement::find( "texMat" );
  250. if ( texMat )
  251. {
  252. meta->addStatement( new GenOp( " @ = tMul(@, float4(@,0)).xyz;\r\n",
  253. outNegViewTS, texMat, outNegViewTS ) );
  254. }
  255. output = meta;
  256. }
  257. void ParallaxFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  258. const MaterialFeatureData &fd )
  259. {
  260. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  261. "ParallaxFeatGLSL::processPix - We don't support SM 1.x!" );
  262. MultiLine *meta = new MultiLine;
  263. // Order matters... get this first!
  264. Var *texCoord = getInTexCoord( "texCoord", "vec2", componentList );
  265. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  266. // We need the negative tangent space view vector
  267. // as in parallax mapping we step towards the camera.
  268. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  269. if ( !negViewTS )
  270. {
  271. Var *inNegViewTS = (Var*)LangElement::find( "outNegViewTS" );
  272. if ( !inNegViewTS )
  273. {
  274. inNegViewTS = connectComp->getElement( RT_TEXCOORD );
  275. inNegViewTS->setName( "outNegViewTS" );
  276. inNegViewTS->setStructName( "IN" );
  277. inNegViewTS->setType( "vec3" );
  278. }
  279. negViewTS = new Var( "negViewTS", "vec3" );
  280. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", new DecOp( negViewTS ), inNegViewTS ) );
  281. }
  282. // Get the rest of our inputs.
  283. Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float", cspPotentialPrimitive );
  284. Var *normalMap = getNormalMapTex();
  285. // Call the library function to do the rest.
  286. if (fd.features.hasFeature(MFT_IsBC3nm, getProcessIndex()))
  287. {
  288. meta->addStatement(new GenOp(" @.xy += parallaxOffsetDxtnm( @, @.xy, @, @ );\r\n",
  289. texCoord, normalMap, texCoord, negViewTS, parallaxInfo));
  290. }
  291. else
  292. {
  293. meta->addStatement(new GenOp(" @.xy += parallaxOffset( @, @.xy, @, @ );\r\n",
  294. texCoord, normalMap, texCoord, negViewTS, parallaxInfo));
  295. }
  296. // TODO: Fix second UV maybe?
  297. output = meta;
  298. }
  299. ShaderFeature::Resources ParallaxFeatGLSL::getResources( const MaterialFeatureData &fd )
  300. {
  301. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  302. "ParallaxFeatGLSL::getResources - We don't support SM 1.x!" );
  303. Resources res;
  304. // We add the outViewTS to the outputstructure.
  305. res.numTexReg = 1;
  306. // If this isn't a deferred then we will be
  307. // creating the normal map here.
  308. if ( !fd.features.hasFeature( MFT_DeferredConditioner ) )
  309. res.numTex = 1;
  310. return res;
  311. }
  312. void ParallaxFeatGLSL::setTexData( Material::StageData &stageDat,
  313. const MaterialFeatureData &fd,
  314. RenderPassData &passData,
  315. U32 &texIndex )
  316. {
  317. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  318. "ParallaxFeatGLSL::setTexData - We don't support SM 1.x!" );
  319. GFXTextureObject *tex = stageDat.getTex( MFT_NormalMap );
  320. if ( tex )
  321. {
  322. passData.mSamplerNames[ texIndex ] = "bumpMap";
  323. passData.mTexType[ texIndex ] = Material::Bump;
  324. passData.mTexSlot[ texIndex++ ].texObject = tex;
  325. }
  326. }
  327. void NormalsOutFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  328. const MaterialFeatureData &fd )
  329. {
  330. // If we have normal maps then we can count
  331. // on it to generate the world space normal.
  332. if ( fd.features[MFT_NormalMap] )
  333. return;
  334. MultiLine *meta = new MultiLine;
  335. output = meta;
  336. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  337. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  338. outNormal->setName( "wsNormal" );
  339. outNormal->setStructName( "OUT" );
  340. outNormal->setType( "vec3" );
  341. // Find the incoming vertex normal.
  342. Var *inNormal = (Var*)LangElement::find( "normal" );
  343. if ( inNormal )
  344. {
  345. // Transform the normal to world space.
  346. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  347. meta->addStatement( new GenOp( " @ = tMul( @, normalize( vec4(@, 0.0) ) ).xyz;\r\n", outNormal, objTrans, inNormal ) );
  348. }
  349. else
  350. {
  351. // If we don't have a vertex normal... just pass the
  352. // camera facing normal to the pixel shader.
  353. meta->addStatement( new GenOp( " @ = float3( 0.0, 0.0, 1.0 );\r\n", outNormal ) );
  354. }
  355. }
  356. void NormalsOutFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  357. const MaterialFeatureData &fd )
  358. {
  359. MultiLine *meta = new MultiLine;
  360. output = meta;
  361. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  362. if ( !wsNormal )
  363. {
  364. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  365. wsNormal = connectComp->getElement( RT_TEXCOORD );
  366. wsNormal->setName( "wsNormal" );
  367. wsNormal->setStructName( "IN" );
  368. wsNormal->setType( "vec3" );
  369. // If we loaded the normal its our resposibility
  370. // to normalize it... the interpolators won't.
  371. //
  372. // Note we cast to half here to get partial precision
  373. // optimized code which is an acceptable loss of
  374. // precision for normals and performs much better
  375. // on older Geforce cards.
  376. //
  377. meta->addStatement( new GenOp( " @ = normalize( half3( @ ) );\r\n", wsNormal, wsNormal ) );
  378. }
  379. LangElement *normalOut;
  380. Var *outColor = (Var*)LangElement::find(getOutputTargetVarName(ShaderFeature::DefaultTarget));
  381. if ( outColor && !fd.features[MFT_AlphaTest] )
  382. normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, @.a )", wsNormal, outColor );
  383. else
  384. normalOut = new GenOp( "float4( ( -@ + 1 ) * 0.5, 1 )", wsNormal );
  385. meta->addStatement( new GenOp( " @;\r\n",
  386. assignColor( normalOut, Material::None ) ) );
  387. }