customFeatureHLSL.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if (mOwner->isMethod("processPixelHLSL"))
  59. Con::executef(mOwner, "processPixelHLSL");
  60. output = meta;
  61. }
  62. void CustomFeatureHLSL::setTexData(Material::StageData& stageDat,
  63. const MaterialFeatureData& fd,
  64. RenderPassData& passData,
  65. U32& texIndex)
  66. {
  67. //GFXTextureObject *tex = stageDat.getTex( MFT_AccuMap );
  68. //if ( tex )
  69. //{
  70. //passData.mSamplerNames[ texIndex ] = "AccuMap";
  71. //passData.mTexType[ texIndex++ ] = Material::AccuMap;
  72. //}
  73. if (mOwner->isMethod("setTextureData"))
  74. Con::executef(mOwner, "setTextureData");
  75. }
  76. void CustomFeatureHLSL::addUniform(String name, String type, String defaultValue, U32 arraySize)
  77. {
  78. //do the var/arg fetching here
  79. Var* newVar = (Var*)LangElement::find(name.c_str());
  80. if (!newVar)
  81. {
  82. VarHolder newVarHolder(name, type, "");
  83. newVarHolder.arraySize = arraySize;
  84. newVarHolder.sampler = false;
  85. newVarHolder.uniform = true;
  86. newVarHolder.constSortPos = cspPotentialPrimitive;
  87. mVars.push_back(newVarHolder);
  88. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  89. }
  90. }
  91. void CustomFeatureHLSL::addSampler(String name, String type, U32 arraySize)
  92. {
  93. //do the var/arg fetching here
  94. Var* newVar = (Var*)LangElement::find(name.c_str());
  95. if (!newVar)
  96. {
  97. //As far as I know, it's always SamplerState regardless of the texture's type
  98. VarHolder newVarHolder(name, "SamplerState", "");
  99. newVarHolder.arraySize = arraySize;
  100. newVarHolder.sampler = true;
  101. newVarHolder.uniform = true;
  102. newVarHolder.constNum = Var::getTexUnitNum(); // used as texture unit num here
  103. mVars.push_back(newVarHolder);
  104. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  105. }
  106. }
  107. void CustomFeatureHLSL::addTexture(String name, String type, String samplerState, U32 arraySize)
  108. {
  109. //do the var/arg fetching here
  110. Var* newVar = (Var*)LangElement::find(name.c_str());
  111. if (!newVar)
  112. {
  113. //go find our sampler state var
  114. U32 constNum = 0;
  115. Var* samplerStateVar = (Var*)LangElement::find(samplerState.c_str());
  116. if (!samplerStateVar)
  117. {
  118. //check our holder vars
  119. bool foundHolder = false;
  120. for (U32 v = 0; v < mVars.size(); v++)
  121. {
  122. if (mVars[v].varName == samplerState)
  123. {
  124. constNum = mVars[v].constNum;
  125. foundHolder = true;
  126. break;
  127. }
  128. }
  129. if (!foundHolder)
  130. {
  131. Con::errorf("CustomShaderFeature::addTexture: Unable to find texture's sampler state!");
  132. return;
  133. }
  134. }
  135. else
  136. {
  137. constNum = samplerStateVar->constNum;
  138. }
  139. VarHolder newVarHolder(name, type, "");
  140. newVarHolder.arraySize = arraySize;
  141. newVarHolder.texture = true;
  142. newVarHolder.uniform = true;
  143. newVarHolder.constNum = constNum; // used as texture unit num here
  144. mVars.push_back(newVarHolder);
  145. mOwner->mAddedShaderConstants.push_back(StringTable->insert(name.c_str()));
  146. }
  147. }
  148. void CustomFeatureHLSL::addVariable(String name, String type, String defaultValue)
  149. {
  150. //do the var/arg fetching here
  151. Var* newVar = (Var*)LangElement::find(name.c_str());
  152. if (!newVar)
  153. {
  154. if (!defaultValue.isEmpty())
  155. {
  156. char declareStatement[128];
  157. dSprintf(declareStatement, 128, " @ = %s;\n", defaultValue.c_str());
  158. newVar = new Var(name, type);
  159. LangElement* newVarDecl = new DecOp(newVar);
  160. meta->addStatement(new GenOp(declareStatement, newVarDecl));
  161. }
  162. else
  163. {
  164. VarHolder newVarHolder(name, type, defaultValue);
  165. mVars.push_back(newVarHolder);
  166. }
  167. }
  168. }
  169. void CustomFeatureHLSL::addConnector(String name, String type, String elementName)
  170. {
  171. // grab connector texcoord register
  172. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]);
  173. //Get element
  174. S32 element = -1;
  175. if (elementName == String("RT_POSITION"))
  176. element = RT_POSITION;
  177. else if (elementName == String("RT_NORMAL"))
  178. element = RT_NORMAL;
  179. else if (elementName == String("RT_BINORMAL"))
  180. element = RT_BINORMAL;
  181. else if (elementName == String("RT_TANGENT"))
  182. element = RT_TANGENT;
  183. else if (elementName == String("RT_TANGENTW"))
  184. element = RT_TANGENTW;
  185. else if (elementName == String("RT_COLOR"))
  186. element = RT_COLOR;
  187. else if (elementName == String("RT_TEXCOORD"))
  188. element = RT_TEXCOORD;
  189. else if (elementName == String("RT_VPOS"))
  190. element = RT_VPOS;
  191. else if (elementName == String("RT_SVPOSITION"))
  192. element = RT_SVPOSITION;
  193. else if (elementName == String("RT_BLENDINDICES"))
  194. element = RT_BLENDINDICES;
  195. else if (elementName == String("RT_BLENDWEIGHT"))
  196. element = RT_BLENDWEIGHT;
  197. if (element == -1)
  198. {
  199. Con::errorf("CustomShaderFeatureHLSL::addConnector - Invalid element type %s", elementName.c_str());
  200. return;
  201. }
  202. VarHolder newVarHolder(name, type, "");
  203. newVarHolder.elementId = element;
  204. if (mOutputState == VertexOutput)
  205. newVarHolder.structName = "OUT";
  206. else if (mOutputState == PixelOutput)
  207. newVarHolder.structName = "IN";
  208. mVars.push_back(newVarHolder);
  209. }
  210. void CustomFeatureHLSL::addVertTexCoord(String name)
  211. {
  212. VarHolder newVarHolder(name, "", "");
  213. newVarHolder.texCoord = true;
  214. mVars.push_back(newVarHolder);
  215. }
  216. void CustomFeatureHLSL::writeLine(String format, S32 argc, ConsoleValue *argv)
  217. {
  218. //do the var/arg fetching here
  219. Vector<Var*> varList;
  220. bool declarationStatement = false;
  221. for (U32 i = 0; i < argc; i++)
  222. {
  223. String varName = argv[i].getString();
  224. Var* newVar = (Var*)LangElement::find(varName.c_str());
  225. if (!newVar)
  226. {
  227. //ok, check our existing var holders, see if we just haven't utilized it yet
  228. for (U32 v = 0; v < mVars.size(); v++)
  229. {
  230. if (mVars[v].varName == varName)
  231. {
  232. if (!mVars[v].texCoord)
  233. {
  234. if (mVars[v].elementId != -1)
  235. {
  236. ShaderConnector* connectComp = dynamic_cast<ShaderConnector*>(mComponentList[C_CONNECTOR]);
  237. Var* newDeclVar = connectComp->getElement((RegisterType)mVars[v].elementId);
  238. newDeclVar->setName(mVars[v].varName);
  239. newDeclVar->setStructName(mVars[v].structName);
  240. newDeclVar->setType(mVars[v].type);
  241. newVar = newDeclVar;
  242. }
  243. else
  244. {
  245. Var* newDeclVar = new Var(mVars[v].varName, mVars[v].type);
  246. newDeclVar->arraySize = mVars[v].arraySize;
  247. newDeclVar->uniform = mVars[v].uniform;
  248. newDeclVar->sampler = mVars[v].sampler;
  249. newDeclVar->texture = mVars[v].texture;
  250. newDeclVar->constNum = mVars[v].constNum;
  251. newDeclVar->constSortPos = mVars[v].constSortPos;
  252. if (!newDeclVar->uniform)
  253. {
  254. LangElement* newVarDecl = new DecOp(newDeclVar);
  255. newVar = (Var*)newVarDecl;
  256. declarationStatement = true;
  257. }
  258. else
  259. {
  260. newVar = newDeclVar;
  261. }
  262. }
  263. }
  264. else
  265. {
  266. newVar = getVertTexCoord(mVars[v].varName);
  267. }
  268. mVars.erase(v);
  269. break;
  270. }
  271. }
  272. if (!newVar)
  273. {
  274. //couldn't find that variable, bail out
  275. Con::errorf("CustomShaderFeature::writeLine: unable to find variable %s, meaning it was not declared before being used!", argv[i].getString());
  276. return;
  277. }
  278. }
  279. varList.push_back(newVar);
  280. }
  281. //not happy about it, but do a trampoline here to pass along the args
  282. switch (varList.size())
  283. {
  284. case 0:
  285. meta->addStatement(new GenOp(format + "\n"));
  286. break;
  287. case 1:
  288. meta->addStatement(new GenOp(format + "\n", varList[0]));
  289. break;
  290. case 2:
  291. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1]));
  292. break;
  293. case 3:
  294. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2]));
  295. break;
  296. case 4:
  297. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3]));
  298. break;
  299. case 5:
  300. meta->addStatement(new GenOp(format + "\n", varList[0], varList[1], varList[2], varList[3], varList[4]));
  301. break;
  302. }
  303. }
  304. bool CustomFeatureHLSL::hasFeature(String name)
  305. {
  306. for (U32 i = 0; i < mFeatureData.materialFeatures.getCount(); i++)
  307. {
  308. String featureName = mFeatureData.materialFeatures.getAt(i).getName();
  309. if (name == featureName)
  310. return true;
  311. }
  312. return false;
  313. }