customFeatureGLSL.cpp 11 KB

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