bumpHLSL.cpp 17 KB

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