customFeatureHLSL.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 "customFeatureHLSL.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 CustomFeatureHLSL::processVert(Vector<ShaderComponent*>& componentList,
  33. const MaterialFeatureData& fd)
  34. {
  35. /*MultiLine *meta = new MultiLine;
  36. getOutTexCoord( "texCoord",
  37. "float2",
  38. false,
  39. meta,
  40. componentList );
  41. getOutObjToTangentSpace( componentList, meta, fd );
  42. output = meta;*/
  43. meta = new MultiLine;
  44. mFeatureData = fd;
  45. mComponentList = componentList;
  46. mOutputState = VertexOutput;
  47. if (mOwner->isMethod("processVertHLSL"))
  48. Con::executef(mOwner, "processVertHLSL");
  49. output = meta;
  50. }
  51. void CustomFeatureHLSL::processPix(Vector<ShaderComponent*>& componentList,
  52. const MaterialFeatureData& fd)
  53. {
  54. meta = new MultiLine;
  55. mFeatureData = fd;
  56. mComponentList = componentList;
  57. mOutputState = PixelOutput;
  58. /*MultiLine *meta = new MultiLine;
  59. output = meta;
  60. // OUT.col
  61. Var *color = (Var*) LangElement::find( "col1" );
  62. if (!color)
  63. {
  64. output = new GenOp(" //NULL COLOR!");
  65. return;
  66. }
  67. // accu map
  68. Var *accuMap = new Var;
  69. accuMap->setType("SamplerState");
  70. accuMap->setName( "accuMap" );
  71. accuMap->uniform = true;
  72. accuMap->sampler = true;
  73. accuMap->constNum = Var::getTexUnitNum(); // used as texture unit num here
  74. // accuColor var
  75. Var *accuColor = new Var;
  76. accuColor->setType( "float4" );
  77. accuColor->setName( "accuColor" );
  78. LangElement *colorAccuDecl = new DecOp( accuColor );
  79. // plc (placement)
  80. Var *accuPlc = new Var;
  81. accuPlc->setType( "float4" );
  82. accuPlc->setName( "plc" );
  83. LangElement *plcAccu = new DecOp( accuPlc );
  84. // accu constants
  85. Var *accuScale = (Var*)LangElement::find( "accuScale" );
  86. if ( !accuScale )
  87. {
  88. accuScale = new Var;
  89. accuScale->setType( "float" );
  90. accuScale->setName( "accuScale" );
  91. accuScale->uniform = true;
  92. accuScale->sampler = false;
  93. accuScale->constSortPos = cspPotentialPrimitive;
  94. }
  95. Var *accuDirection = (Var*)LangElement::find( "accuDirection" );
  96. if ( !accuDirection )
  97. {
  98. accuDirection = new Var;
  99. accuDirection->setType( "float" );
  100. accuDirection->setName( "accuDirection" );
  101. accuDirection->uniform = true;
  102. accuDirection->sampler = false;
  103. accuDirection->constSortPos = cspPotentialPrimitive;
  104. }
  105. Var *accuStrength = (Var*)LangElement::find( "accuStrength" );
  106. if ( !accuStrength )
  107. {
  108. accuStrength = new Var;
  109. accuStrength->setType( "float" );
  110. accuStrength->setName( "accuStrength" );
  111. accuStrength->uniform = true;
  112. accuStrength->sampler = false;
  113. accuStrength->constSortPos = cspPotentialPrimitive;
  114. }
  115. Var *accuCoverage = (Var*)LangElement::find( "accuCoverage" );
  116. if ( !accuCoverage )
  117. {
  118. accuCoverage = new Var;
  119. accuCoverage->setType( "float" );
  120. accuCoverage->setName( "accuCoverage" );
  121. accuCoverage->uniform = true;
  122. accuCoverage->sampler = false;
  123. accuCoverage->constSortPos = cspPotentialPrimitive;
  124. }
  125. Var *accuSpecular = (Var*)LangElement::find( "accuSpecular" );
  126. if ( !accuSpecular )
  127. {
  128. accuSpecular = new Var;
  129. accuSpecular->setType( "float" );
  130. accuSpecular->setName( "accuSpecular" );
  131. accuSpecular->uniform = true;
  132. accuSpecular->sampler = false;
  133. accuSpecular->constSortPos = cspPotentialPrimitive;
  134. }
  135. Var *inTex = getInTexCoord( "texCoord", "float2", componentList );
  136. Var *accuVec = getInTexCoord( "accuVec", "float3", componentList );
  137. Var *bumpNorm = (Var *)LangElement::find( "bumpSample" );
  138. if( bumpNorm == NULL )
  139. {
  140. bumpNorm = (Var *)LangElement::find( "bumpNormal" );
  141. if (!bumpNorm)
  142. return;
  143. }
  144. // get the accu pixel color
  145. Var *accuMapTex = new Var;
  146. accuMapTex->setType("Texture2D");
  147. accuMapTex->setName("accuMapTex");
  148. accuMapTex->uniform = true;
  149. accuMapTex->texture = true;
  150. accuMapTex->constNum = accuMap->constNum;
  151. meta->addStatement(new GenOp(" @ = @.Sample(@, @ * @);\r\n", colorAccuDecl, accuMapTex, accuMap, inTex, accuScale));
  152. // scale up normals
  153. meta->addStatement( new GenOp( " @.xyz = @.xyz * 2.0 - 0.5;\r\n", bumpNorm, bumpNorm ) );
  154. // assign direction
  155. meta->addStatement( new GenOp( " @.z *= @*2.0;\r\n", accuVec, accuDirection ) );
  156. // saturate based on strength
  157. meta->addStatement( new GenOp( " @ = saturate( dot( @.xyz, @.xyz * pow(@, 5) ) );\r\n", plcAccu, bumpNorm, accuVec, accuStrength ) );
  158. // add coverage
  159. meta->addStatement( new GenOp( " @.a += (2 * pow(@/2, 5)) - 0.5;\r\n", accuPlc, accuCoverage ) );
  160. // clamp to a sensible value
  161. meta->addStatement( new GenOp( " @.a = clamp(@.a, 0, 1);\r\n", accuPlc, accuPlc ) );
  162. // light
  163. Var *lightColor = (Var*) LangElement::find( "d_lightcolor" );
  164. if(lightColor != NULL)
  165. meta->addStatement( new GenOp( " @ *= float4(@, 1.0);\r\n\r\n", accuColor, lightColor ) );
  166. // lerp with current pixel - use the accu alpha as well
  167. meta->addStatement( new GenOp( " @ = lerp( @, @, @.a * @.a);\r\n", color, color, accuColor, accuPlc, accuColor ) );
  168. // the result should always be opaque
  169. meta->addStatement( new GenOp( " @.a = 1.0;\r\n", color ) );*/
  170. if (mOwner->isMethod("processPixelHLSL"))
  171. Con::executef(mOwner, "processPixelHLSL");
  172. output = meta;
  173. }
  174. void CustomFeatureHLSL::setTexData(Material::StageData& stageDat,
  175. const MaterialFeatureData& fd,
  176. RenderPassData& passData,
  177. U32& texIndex)
  178. {
  179. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  180. //if ( tex )
  181. //{
  182. //passData.mSamplerNames[ texIndex ] = "AccuMap";
  183. //passData.mTexType[ texIndex++ ] = Material::AccuMap;
  184. //}
  185. if (mOwner->isMethod("setTextureData"))
  186. Con::executef(mOwner, "setTextureData");
  187. }
  188. void CustomFeatureHLSL::addUniform(String name, String type, String defaultValue, U32 arraySize)
  189. {
  190. //do the var/arg fetching here
  191. Var* newVar = (Var*)LangElement::find(name.c_str());
  192. if (!newVar)
  193. {
  194. VarHolder newVarHolder(name, type, "");
  195. newVarHolder.arraySize = arraySize;
  196. newVarHolder.sampler = false;
  197. newVarHolder.uniform = true;
  198. newVarHolder.constSortPos = cspPotentialPrimitive;
  199. mVars.push_back(newVarHolder);
  200. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  201. }
  202. }
  203. void CustomFeatureHLSL::addSampler(String name, String type, U32 arraySize)
  204. {
  205. //do the var/arg fetching here
  206. Var* newVar = (Var*)LangElement::find(name.c_str());
  207. if (!newVar)
  208. {
  209. //As far as I know, it's always SamplerState regardless of the texture's type
  210. VarHolder newVarHolder(name, "SamplerState", "");
  211. newVarHolder.arraySize = arraySize;
  212. newVarHolder.sampler = true;
  213. newVarHolder.uniform = true;
  214. newVarHolder.constNum = Var::getTexUnitNum(); // used as texture unit num here
  215. mVars.push_back(newVarHolder);
  216. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  217. }
  218. }
  219. void CustomFeatureHLSL::addTexture(String name, String type, String samplerState, U32 arraySize)
  220. {
  221. //do the var/arg fetching here
  222. Var* newVar = (Var*)LangElement::find(name.c_str());
  223. if (!newVar)
  224. {
  225. //go find our sampler state var
  226. U32 constNum = 0;
  227. Var* samplerStateVar = (Var*)LangElement::find(samplerState.c_str());
  228. if (!samplerStateVar)
  229. {
  230. //check our holder vars
  231. bool foundHolder = false;
  232. for (U32 v = 0; v < mVars.size(); v++)
  233. {
  234. if (mVars[v].varName == samplerState)
  235. {
  236. constNum = mVars[v].constNum;
  237. foundHolder = true;
  238. break;
  239. }
  240. }
  241. if (!foundHolder)
  242. {
  243. Con::errorf("CustomShaderFeature::addTexture: Unable to find texture's sampler state!");
  244. return;
  245. }
  246. }
  247. else
  248. {
  249. constNum = samplerStateVar->constNum;
  250. }
  251. VarHolder newVarHolder(name, type, "");
  252. newVarHolder.arraySize = arraySize;
  253. newVarHolder.texture = true;
  254. newVarHolder.uniform = true;
  255. newVarHolder.constNum = constNum; // used as texture unit num here
  256. mVars.push_back(newVarHolder);
  257. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  258. }
  259. }
  260. void CustomFeatureHLSL::addVariable(String name, String type, String defaultValue)
  261. {
  262. //do the var/arg fetching here
  263. Var* newVar = (Var*)LangElement::find(name.c_str());
  264. if (!newVar)
  265. {
  266. if (!defaultValue.isEmpty())
  267. {
  268. char declareStatement[128];
  269. dSprintf(declareStatement, 128, " @ = %s;\n", defaultValue.c_str());
  270. newVar = new Var(name, type);
  271. LangElement* newVarDecl = new DecOp(newVar);
  272. meta->addStatement(new GenOp(declareStatement, newVarDecl));
  273. }
  274. else
  275. {
  276. VarHolder newVarHolder(name, type, defaultValue);
  277. mVars.push_back(newVarHolder);
  278. }
  279. }
  280. }
  281. void CustomFeatureHLSL::addConnector(String name, String type, String elementName)
  282. {
  283. // grab connector texcoord register
  284. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]);
  285. //Get element
  286. S32 element = -1;
  287. if (elementName == String("RT_POSITION"))
  288. element = RT_POSITION;
  289. else if (elementName == String("RT_NORMAL"))
  290. element = RT_NORMAL;
  291. else if (elementName == String("RT_BINORMAL"))
  292. element = RT_BINORMAL;
  293. else if (elementName == String("RT_TANGENT"))
  294. element = RT_TANGENT;
  295. else if (elementName == String("RT_TANGENTW"))
  296. element = RT_TANGENTW;
  297. else if (elementName == String("RT_COLOR"))
  298. element = RT_COLOR;
  299. else if (elementName == String("RT_TEXCOORD"))
  300. element = RT_TEXCOORD;
  301. else if (elementName == String("RT_VPOS"))
  302. element = RT_VPOS;
  303. else if (elementName == String("RT_SVPOSITION"))
  304. element = RT_SVPOSITION;
  305. else if (elementName == String("RT_BLENDINDICES"))
  306. element = RT_BLENDINDICES;
  307. else if (elementName == String("RT_BLENDWEIGHT"))
  308. element = RT_BLENDWEIGHT;
  309. if (element == -1)
  310. {
  311. Con::errorf("CustomShaderFeatureHLSL::addConnector - Invalid element type %s", elementName.c_str());
  312. return;
  313. }
  314. VarHolder newVarHolder(name, type, "");
  315. newVarHolder.elementId = element;
  316. if (mOutputState == VertexOutput)
  317. newVarHolder.structName = "OUT";
  318. else if (mOutputState == PixelOutput)
  319. newVarHolder.structName = "IN";
  320. mVars.push_back(newVarHolder);
  321. }
  322. void CustomFeatureHLSL::addVertTexCoord(String name)
  323. {
  324. VarHolder newVarHolder(name, "", "");
  325. newVarHolder.texCoord = true;
  326. mVars.push_back(newVarHolder);
  327. }
  328. void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValue *argv)
  329. {
  330. //do the var/arg fetching here
  331. Vector<Var*> varList;
  332. bool declarationStatement = false;
  333. for (U32 i = 0; i < argc; i++)
  334. {
  335. String varName = argv[i].getString();
  336. Var* newVar = (Var*)LangElement::find(varName.c_str());
  337. if (!newVar)
  338. {
  339. //ok, check our existing var holders, see if we just haven't utilized it yet
  340. for (U32 v = 0; v < mVars.size(); v++)
  341. {
  342. if (mVars[v].varName == varName)
  343. {
  344. if (!mVars[v].texCoord)
  345. {
  346. if (mVars[v].elementId != -1)
  347. {
  348. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]);
  349. Var* newDeclVar = connectComp->getElement((RegisterType)mVars[v].elementId);
  350. newDeclVar->setName(mVars[v].varName);
  351. newDeclVar->setStructName(mVars[v].structName);
  352. newDeclVar->setType(mVars[v].type);
  353. newVar = newDeclVar;
  354. }
  355. else
  356. {
  357. Var* newDeclVar = new Var(mVars[v].varName, mVars[v].type);
  358. newDeclVar->arraySize = mVars[v].arraySize;
  359. newDeclVar->uniform = mVars[v].uniform;
  360. newDeclVar->sampler = mVars[v].sampler;
  361. newDeclVar->texture = mVars[v].texture;
  362. newDeclVar->constNum = mVars[v].constNum;
  363. newDeclVar->constSortPos = mVars[v].constSortPos;
  364. if (!newDeclVar->uniform)
  365. {
  366. LangElement* newVarDecl = new DecOp(newDeclVar);
  367. newVar = (Var*)newVarDecl;
  368. declarationStatement = true;
  369. }
  370. else
  371. {
  372. newVar = newDeclVar;
  373. }
  374. }
  375. }
  376. else
  377. {
  378. newVar = getVertTexCoord(mVars[v].varName);
  379. }
  380. mVars.erase(v);
  381. break;
  382. }
  383. }
  384. if (!newVar)
  385. {
  386. //couldn't find that variable, bail out
  387. Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getString());
  388. return;
  389. }
  390. }
  391. varList.push_back(newVar);
  392. }
  393. //not happy about it, but do a trampoline here to pass along the args
  394. switch (varList.size())
  395. {
  396. case 0:
  397. meta->addStatement(new GenOp(format + "\n"));
  398. break;
  399. case 1:
  400. meta->addStatement(new GenOp(format + "\n", varList[0]));
  401. break;
  402. case 2:
  403. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1]));
  404. break;
  405. case 3:
  406. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2]));
  407. break;
  408. case 4:
  409. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3]));
  410. break;
  411. case 5:
  412. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3], varList[4]));
  413. break;
  414. }
  415. }
  416. bool CustomFeatureHLSL::hasFeature(String name)
  417. {
  418. for (U32 i = 0; i < mFeatureData.materialFeatures.getCount(); i++)
  419. {
  420. String featureName = mFeatureData.materialFeatures.getAt(i).getName();
  421. if (name == featureName)
  422. return true;
  423. }
  424. return false;
  425. }