bumpGLSL.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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( "out_texCoord", "vec2", true, componentList );
  58. // Sample the bumpmap.
  59. Var *bumpMap = getNormalMapTex();
  60. LangElement *texOp = NULL;
  61. //Handle atlased textures
  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("vec4");
  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("vec4");
  79. tileParams->setName("bumpAtlasTileParams");
  80. tileParams->uniform = true;
  81. tileParams->constSortPos = cspPotentialPrimitive;
  82. const bool is_sm3 = (GFX->getPixelShaderVersion() > 2.0f);
  83. // getPixelShaderVersion() on Mac currently returns 2.0,
  84. // or 3.0 if Advanced Lighting is enabled
  85. if(is_sm3)
  86. {
  87. // Figure out the mip level
  88. meta->addStatement(new GenOp(" vec2 _dx_bump = dFdx(@ * @.z);\r\n", texCoord, atParams));
  89. meta->addStatement(new GenOp(" vec2 _dy_bump = dFdy(@ * @.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(" vec2 mipSz_bump = mipPixSz_bump / @.xy;\r\n", atParams));
  95. }
  96. else
  97. {
  98. meta->addStatement(new GenOp(" vec2 mipSz = float2(1.0, 1.0);\r\n"));
  99. }
  100. // Tiling mode
  101. // TODO: Select wrap or clamp somehow
  102. if( true ) // Wrap
  103. meta->addStatement(new GenOp(" @ = fract(@);\r\n", atDecl, texCoord));
  104. else // Clamp
  105. meta->addStatement(new GenOp(" @ = saturate(@);\r\n", atDecl, texCoord));
  106. // Finally scale/offset, and correct for filtering
  107. meta->addStatement(new GenOp(" @ = @ * ((mipSz_bump * @.xy - 1.0) / mipSz_bump) + 0.5 / mipSz_bump + @.xy * @.xy;\r\n",
  108. atlasedTex, atlasedTex, atParams, atParams, tileParams));
  109. // Add a newline
  110. meta->addStatement(new GenOp( "\r\n"));
  111. if(is_sm3)
  112. {
  113. texOp = new GenOp( "texture2DLod(@, vec4(@, 0.0, mipLod_bump)", bumpMap, texCoord );
  114. }
  115. else
  116. {
  117. texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
  118. }
  119. }
  120. else
  121. {
  122. texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
  123. }
  124. Var *bumpNorm = new Var( "bumpNormal", "vec4" );
  125. meta->addStatement( expandNormalMap( texOp, new DecOp( bumpNorm ), bumpNorm, fd ) );
  126. // If we have a detail normal map we add the xy coords of
  127. // it to the base normal map. This gives us the effect we
  128. // want with few instructions and minial artifacts.
  129. if ( fd.features.hasFeature( MFT_DetailNormalMap ) )
  130. {
  131. bumpMap = new Var;
  132. bumpMap->setType( "sampler2D" );
  133. bumpMap->setName( "detailBumpMap" );
  134. bumpMap->uniform = true;
  135. bumpMap->sampler = true;
  136. bumpMap->constNum = Var::getTexUnitNum();
  137. texCoord = getInTexCoord( "detCoord", "vec2", true, componentList );
  138. texOp = new GenOp( "texture2D(@, @)", bumpMap, texCoord );
  139. Var *detailBump = new Var;
  140. detailBump->setName( "detailBump" );
  141. detailBump->setType( "vec4" );
  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", "vec3" );
  153. Var *worldToTanget = getInWorldToTangent( componentList );
  154. meta->addStatement( new GenOp( " @ = normalize( vec3( @.xyz * @ ) );\r\n", new DecOp( wsNormal ), bumpNorm, worldToTanget ) );
  155. }
  156. ShaderFeature::Resources BumpFeatGLSL::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 BumpFeatGLSL::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.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_NormalMap );
  193. }
  194. if ( fd.features[ MFT_DetailNormalMap ] )
  195. {
  196. passData.mTexType[ texIndex ] = Material::DetailBump;
  197. passData.mTexSlot[ texIndex++ ].texObject = stageDat.getTex( MFT_DetailNormalMap );
  198. }
  199. }
  200. //
  201. Var* ParallaxFeatGLSL::_getUniformVar( const char *name, const char *type )
  202. {
  203. Var *theVar = (Var*)LangElement::find( name );
  204. if ( !theVar )
  205. {
  206. theVar = new Var;
  207. theVar->setType( type );
  208. theVar->setName( name );
  209. theVar->uniform = true;
  210. theVar->constSortPos = cspPass;
  211. }
  212. return theVar;
  213. }
  214. void ParallaxFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  215. const MaterialFeatureData &fd )
  216. {
  217. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  218. "ParallaxFeatGLSL::processVert - We don't support SM 1.x!" );
  219. MultiLine *meta = new MultiLine;
  220. // Add the texture coords.
  221. getOutTexCoord( "texCoord",
  222. "vec2",
  223. true,
  224. fd.features[MFT_TexAnim],
  225. meta,
  226. componentList );
  227. // Grab the input position.
  228. Var *inPos = (Var*)LangElement::find( "inPosition" );
  229. if ( !inPos )
  230. inPos = (Var*)LangElement::find( "position" );
  231. // Get the object space eye position and the world
  232. // to tangent transform.
  233. Var *eyePos = _getUniformVar( "eyePos", "vec3" );
  234. Var *objToTangentSpace = getOutObjToTangentSpace( componentList, meta, fd );
  235. // send transform to pixel shader
  236. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  237. Var *outViewTS = connectComp->getElement( RT_TEXCOORD, 1 );
  238. outViewTS->setName( "outViewTS" );
  239. outViewTS->setType( "vec3" );
  240. meta->addStatement( new GenOp( " @ = ( @ - @.xyz ) * transpose( @ );\r\n",
  241. outViewTS, inPos, eyePos, objToTangentSpace ) );
  242. output = meta;
  243. }
  244. void ParallaxFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  245. const MaterialFeatureData &fd )
  246. {
  247. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  248. "ParallaxFeatGLSL::processPix - We don't support SM 1.x!" );
  249. MultiLine *meta = new MultiLine;
  250. // Order matters... get this first!
  251. Var *texCoord = getInTexCoord( "texCoord", "vec2", true, componentList );
  252. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  253. // We need the negative tangent space view vector
  254. // as in parallax mapping we step towards the camera.
  255. Var *negViewTS = (Var*)LangElement::find( "negViewTS" );
  256. if ( !negViewTS )
  257. {
  258. Var *inViewTS = (Var*)LangElement::find( "outViewTS" );
  259. if ( !inViewTS )
  260. {
  261. inViewTS = connectComp->getElement( RT_TEXCOORD, 1 );
  262. inViewTS->setName( "outViewTS" );
  263. inViewTS->setType( "vec3" );
  264. }
  265. negViewTS = new Var( "negViewTS", "vec3" );
  266. meta->addStatement( new GenOp( " @ = -normalize( @ );\r\n", new DecOp( negViewTS ), inViewTS ) );
  267. }
  268. // Get the rest of our inputs.
  269. Var *parallaxInfo = _getUniformVar( "parallaxInfo", "float" );
  270. Var *normalMap = getNormalMapTex();
  271. // Do 3 parallax samples to get acceptable
  272. // quality without too much overhead.
  273. Var *pdepth = findOrCreateLocal( "pdepth", "float", meta );
  274. Var *poffset = findOrCreateLocal( "poffset", "vec2", meta );
  275. meta->addStatement( new GenOp( " @ = texture2D( @, @.xy ).a;\r\n", pdepth, normalMap, texCoord ) );
  276. meta->addStatement( new GenOp( " @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
  277. meta->addStatement( new GenOp( " @ = ( @ + texture2D( @, @.xy + @ ).a ) * 0.5;\r\n", pdepth, pdepth, normalMap, texCoord, poffset ) );
  278. meta->addStatement( new GenOp( " @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
  279. meta->addStatement( new GenOp( " @ = ( @ + texture2D( @, @.xy + @ ).a ) * 0.5;\r\n", pdepth, pdepth, normalMap, texCoord, poffset ) );
  280. meta->addStatement( new GenOp( " @ = @.xy * ( @ * @ );\r\n", poffset, negViewTS, pdepth, parallaxInfo ) );
  281. meta->addStatement( new GenOp( " @.xy += @;\r\n", texCoord, poffset ) );
  282. // TODO: Fix second UV.
  283. output = meta;
  284. }
  285. ShaderFeature::Resources ParallaxFeatGLSL::getResources( const MaterialFeatureData &fd )
  286. {
  287. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  288. "ParallaxFeatGLSL::getResources - We don't support SM 1.x!" );
  289. Resources res;
  290. // We add the outViewTS to the outputstructure.
  291. res.numTexReg = 1;
  292. // If this isn't a prepass then we will be
  293. // creating the normal map here.
  294. if ( !fd.features.hasFeature( MFT_PrePassConditioner ) )
  295. res.numTex = 1;
  296. return res;
  297. }
  298. void ParallaxFeatGLSL::setTexData( Material::StageData &stageDat,
  299. const MaterialFeatureData &fd,
  300. RenderPassData &passData,
  301. U32 &texIndex )
  302. {
  303. AssertFatal( GFX->getPixelShaderVersion() >= 2.0,
  304. "ParallaxFeatGLSL::setTexData - We don't support SM 1.x!" );
  305. GFXTextureObject *tex = stageDat.getTex( MFT_NormalMap );
  306. if ( tex )
  307. {
  308. passData.mTexType[ texIndex ] = Material::Bump;
  309. passData.mTexSlot[ texIndex++ ].texObject = tex;
  310. }
  311. }
  312. //
  313. void NormalsOutFeatGLSL::processVert( Vector<ShaderComponent*> &componentList,
  314. const MaterialFeatureData &fd )
  315. {
  316. // If we have normal maps then we can count
  317. // on it to generate the world space normal.
  318. if ( fd.features[MFT_NormalMap] )
  319. return;
  320. MultiLine *meta = new MultiLine;
  321. output = meta;
  322. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  323. Var *outNormal = connectComp->getElement( RT_TEXCOORD );
  324. outNormal->setName( "wsNormal" );
  325. outNormal->setType( "vec3" );
  326. outNormal->mapsToSampler = false;
  327. // Find the incoming vertex normal.
  328. Var *inNormal = (Var*)LangElement::find( "normal" );
  329. if ( inNormal )
  330. {
  331. // Transform the normal to world space.
  332. Var *objTrans = getObjTrans( componentList, fd.features[MFT_UseInstancing], meta );
  333. meta->addStatement( new GenOp( " @ = @ * normalize( @ );\r\n", outNormal, objTrans, inNormal ) );
  334. }
  335. else
  336. {
  337. // If we don't have a vertex normal... just pass the
  338. // camera facing normal to the pixel shader.
  339. meta->addStatement( new GenOp( " @ = vec3( 0.0, 0.0, 1.0 );\r\n", outNormal ) );
  340. }
  341. }
  342. void NormalsOutFeatGLSL::processPix( Vector<ShaderComponent*> &componentList,
  343. const MaterialFeatureData &fd )
  344. {
  345. MultiLine *meta = new MultiLine;
  346. output = meta;
  347. Var *wsNormal = (Var*)LangElement::find( "wsNormal" );
  348. if ( !wsNormal )
  349. {
  350. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  351. wsNormal = connectComp->getElement( RT_TEXCOORD );
  352. wsNormal->setName( "wsNormal" );
  353. wsNormal->setType( "vec3" );
  354. // If we loaded the normal its our resposibility
  355. // to normalize it... the interpolators won't.
  356. //
  357. meta->addStatement( new GenOp( " @ = normalize( @ );\r\n", wsNormal, wsNormal ) );
  358. }
  359. LangElement *normalOut;
  360. Var *outColor = (Var*)LangElement::find( "col" );
  361. if ( outColor )
  362. normalOut = new GenOp( "vec4( ( -@ + 1 ) * 0.5, @.a )", wsNormal, outColor );
  363. else
  364. normalOut = new GenOp( "vec4( ( -@ + 1 ) * 0.5, 1 )", wsNormal );
  365. meta->addStatement( new GenOp( " @;\r\n",
  366. assignColor( normalOut, Material::None ) ) );
  367. }