bumpHLSL.cpp 18 KB

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