bumpGLSL.cpp 16 KB

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