bumpGLSL.cpp 17 KB

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