accuFeatureGLSL.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "shaderGen/GLSL/accuFeatureGLSL.h"
  23. #include "shaderGen/shaderFeature.h"
  24. #include "shaderGen/shaderOp.h"
  25. #include "shaderGen/featureMgr.h"
  26. #include "materials/materialFeatureTypes.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "materials/processedMaterial.h"
  29. //****************************************************************************
  30. // Accu Texture
  31. //****************************************************************************
  32. void AccuTexFeatGLSL::processVert(Vector<ShaderComponent*> &componentList,
  33. const MaterialFeatureData &fd )
  34. {
  35. MultiLine *meta = new MultiLine;
  36. getOutTexCoord( "texCoord",
  37. "vec2",
  38. false,
  39. meta,
  40. componentList );
  41. getOutObjToTangentSpace( componentList, meta, fd );
  42. addOutAccuVec( componentList, meta );
  43. output = meta;
  44. }
  45. void AccuTexFeatGLSL::processPix(Vector<ShaderComponent*> &componentList,
  46. const MaterialFeatureData &fd )
  47. {
  48. MultiLine *meta = new MultiLine;
  49. output = meta;
  50. // OUT.col
  51. Var *color = (Var*) LangElement::find(getOutputTargetVarName(ShaderFeature::RenderTarget1));
  52. if (!color)
  53. {
  54. output = new GenOp(" //NULL COLOR!");
  55. return;
  56. }
  57. // accu map
  58. Var *accuMap = new Var;
  59. accuMap->setType( "sampler2D" );
  60. accuMap->setName( "accuMap" );
  61. accuMap->uniform = true;
  62. accuMap->sampler = true;
  63. accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  64. // accuColor var
  65. Var *accuColor = new Var;
  66. accuColor->setType( "vec4" );
  67. accuColor->setName( "accuColor" );
  68. LangElement *colorAccuDecl = new DecOp( accuColor );
  69. // plc (placement)
  70. Var *accuPlc = new Var;
  71. accuPlc->setType( "vec4" );
  72. accuPlc->setName( "plc" );
  73. LangElement *plcAccu = new DecOp( accuPlc );
  74. // accu constants
  75. Var *accuScale = (Var*)LangElement::find( "accuScale" );
  76. if ( !accuScale ) {
  77. accuScale = new Var;
  78. accuScale->setType( "float" );
  79. accuScale->setName( "accuScale" );
  80. accuScale->uniform = true;
  81. accuScale->sampler = false;
  82. accuScale->constSortPos = cspPotentialPrimitive;
  83. }
  84. Var *accuDirection = (Var*)LangElement::find( "accuDirection" );
  85. if ( !accuDirection ) {
  86. accuDirection = new Var;
  87. accuDirection->setType( "float" );
  88. accuDirection->setName( "accuDirection" );
  89. accuDirection->uniform = true;
  90. accuDirection->sampler = false;
  91. accuDirection->constSortPos = cspPotentialPrimitive;
  92. }
  93. Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
  94. if ( !accuStrength ) {
  95. accuStrength = new Var;
  96. accuStrength->setType( "float" );
  97. accuStrength->setName( "accuStrength" );
  98. accuStrength->uniform = true;
  99. accuStrength->sampler = false;
  100. accuStrength->constSortPos = cspPotentialPrimitive;
  101. }
  102. Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
  103. if ( !accuCoverage ) {
  104. accuCoverage = new Var;
  105. accuCoverage->setType( "float" );
  106. accuCoverage->setName( "accuCoverage" );
  107. accuCoverage->uniform = true;
  108. accuCoverage->sampler = false;
  109. accuCoverage->constSortPos = cspPotentialPrimitive;
  110. }
  111. Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
  112. if ( !accuSpecular ) {
  113. accuSpecular = new Var;
  114. accuSpecular->setType( "float" );
  115. accuSpecular->setName( "accuSpecular" );
  116. accuSpecular->uniform = true;
  117. accuSpecular->sampler = false;
  118. accuSpecular->constSortPos = cspPotentialPrimitive;
  119. }
  120. Var *inTex = getInTexCoord( "texCoord", "vec2", componentList );
  121. Var *accuVec = getInTexCoord( "accuVec", "vec3", componentList );
  122. Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
  123. if( bumpNorm == NULL ) {
  124. bumpNorm = (Var *)LangElement::find( "bumpNormal" );
  125. if (!bumpNorm)
  126. {
  127. output = new GenOp(" //NULL bumpNormal!");
  128. return;
  129. }
  130. }
  131. // get the accu pixel color
  132. meta->addStatement( new GenOp( " @ = tex2D(@, @ * @);\r\n", colorAccuDecl, accuMap, inTex, accuScale ) );
  133. // scale up normals
  134. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );
  135. // assign direction
  136. meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) );
  137. // saturate based on strength
  138. meta->addStatement( new GenOp( " @ = saturate( float4(dot( float3(@), @.xyz * pow(@, 5) ) ));\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) );
  139. // add coverage
  140. meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) );
  141. // clamp to a sensible value
  142. meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) );
  143. // light
  144. Var *lightColor = (Var*) LangElement::find( "d_lightcolor" );
  145. if(lightColor != NULL)
  146. meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) );
  147. // lerp with current pixel - use the accu alpha as well
  148. meta->addStatement( new GenOp( " @ = mix( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) );
  149. // the result should always be opaque
  150. meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );
  151. }
  152. void AccuTexFeatGLSL::setTexData(Material::StageData &stageDat,
  153. const MaterialFeatureData &fd,
  154. RenderPassData &passData,
  155. U32 &texIndex )
  156. {
  157. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  158. //if ( tex )
  159. //{
  160. passData.mSamplerNames[texIndex] = "accuMap";
  161. passData.mTexType[ texIndex++ ] = Material::AccuMap;
  162. //passData.mTexSlot[ texIndex++ ].texObject = tex;
  163. //}
  164. }
  165. void AccuTexFeatGLSL::getAccuVec(MultiLine *meta, LangElement *accuVec)
  166. {
  167. // Get the transform to world space.
  168. Var *objTrans = (Var*)LangElement::find( "objTrans" );
  169. if ( !objTrans )
  170. {
  171. objTrans = new Var;
  172. objTrans->setType( "float4x4" );
  173. objTrans->setName( "objTrans" );
  174. objTrans->uniform = true;
  175. objTrans->constSortPos = cspPrimitive;
  176. }
  177. // accu obj trans
  178. Var *aobjTrans = new Var;
  179. aobjTrans->setType( "float4x4" );
  180. aobjTrans->setName( "accuObjTrans" );
  181. LangElement *accuObjTransDecl = new DecOp( aobjTrans );
  182. Var *outObjToTangentSpace = (Var*)LangElement::find( "objToTangentSpace" );
  183. Var *tav = new Var;
  184. tav->setType( "float4" );
  185. tav->setName( "tAccuVec" );
  186. LangElement *tavDecl = new DecOp( tav );
  187. meta->addStatement( new GenOp( " @ = float4(0,0,1,0);\r\n", tavDecl ) );
  188. meta->addStatement( new GenOp( " @ = transpose(@);\r\n", accuObjTransDecl, objTrans ) );
  189. meta->addStatement( new GenOp( " @ = tMul(@, @);\r\n", tav, aobjTrans, tav ) );
  190. meta->addStatement( new GenOp( " @.xyz = tMul(@, @.xyz);\r\n", tav, outObjToTangentSpace, tav ) );
  191. meta->addStatement( new GenOp( " @.y *= -1;\r\n", tav ) );
  192. meta->addStatement( new GenOp( " @ = @.xyz;\r\n", accuVec, tav ) );
  193. }
  194. Var* AccuTexFeatGLSL::addOutAccuVec(Vector<ShaderComponent*> &componentList, MultiLine *meta)
  195. {
  196. Var *outAccuVec = (Var*)LangElement::find( "accuVec" );
  197. if ( !outAccuVec )
  198. {
  199. // Setup the connector.
  200. ShaderConnector *connectComp = dynamic_cast<ShaderConnector *>( componentList[C_CONNECTOR] );
  201. outAccuVec = connectComp->getElement( RT_TEXCOORD );
  202. outAccuVec->setName( "accuVec" );
  203. outAccuVec->setStructName( "OUT" );
  204. outAccuVec->setType( "float3" );
  205. getAccuVec( meta, outAccuVec );
  206. }
  207. return outAccuVec;
  208. }