shaderCompGLSL.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/shaderCompGLSL.h"
  24. #include "shaderGen/shaderComp.h"
  25. #include "shaderGen/langElement.h"
  26. #include "gfx/gfxDevice.h"
  27. Var * AppVertConnectorGLSL::getElement( RegisterType type,
  28. U32 numElements,
  29. U32 numRegisters )
  30. {
  31. switch( type )
  32. {
  33. case RT_POSITION:
  34. {
  35. Var *newVar = new Var;
  36. mElementList.push_back( newVar );
  37. newVar->setConnectName( "vPosition" );
  38. return newVar;
  39. }
  40. case RT_NORMAL:
  41. {
  42. Var *newVar = new Var;
  43. mElementList.push_back( newVar );
  44. newVar->setConnectName( "vNormal" );
  45. return newVar;
  46. }
  47. case RT_BINORMAL:
  48. {
  49. Var *newVar = new Var;
  50. mElementList.push_back( newVar );
  51. newVar->setConnectName( "vBinormal" );
  52. return newVar;
  53. }
  54. case RT_COLOR:
  55. {
  56. Var *newVar = new Var;
  57. mElementList.push_back( newVar );
  58. newVar->setConnectName( "vColor" );
  59. return newVar;
  60. }
  61. case RT_TANGENT:
  62. {
  63. Var *newVar = new Var;
  64. mElementList.push_back( newVar );
  65. newVar->setConnectName( "vTangent" );
  66. return newVar;
  67. }
  68. case RT_TANGENTW:
  69. {
  70. Var *newVar = new Var;
  71. mElementList.push_back( newVar );
  72. newVar->setConnectName( "vTangentW" );
  73. return newVar;
  74. }
  75. case RT_TEXCOORD:
  76. {
  77. Var *newVar = new Var;
  78. mElementList.push_back( newVar );
  79. char out[32];
  80. dSprintf( (char*)out, sizeof(out), "vTexCoord%d", mCurTexElem );
  81. newVar->setConnectName( out );
  82. newVar->constNum = mCurTexElem;
  83. newVar->arraySize = numElements;
  84. if ( numRegisters != -1 )
  85. mCurTexElem += numRegisters;
  86. else
  87. mCurTexElem += numElements;
  88. return newVar;
  89. }
  90. default:
  91. break;
  92. }
  93. return NULL;
  94. }
  95. void AppVertConnectorGLSL::sortVars()
  96. {
  97. // Not required in GLSL
  98. }
  99. void AppVertConnectorGLSL::setName( char *newName )
  100. {
  101. dStrcpy( (char*)mName, newName );
  102. }
  103. void AppVertConnectorGLSL::reset()
  104. {
  105. for( U32 i=0; i<mElementList.size(); i++ )
  106. {
  107. mElementList[i] = NULL;
  108. }
  109. mElementList.setSize( 0 );
  110. mCurTexElem = 0;
  111. }
  112. void AppVertConnectorGLSL::print( Stream &stream, bool isVertexShader )
  113. {
  114. if(!isVertexShader)
  115. return;
  116. U8 output[256];
  117. // print struct
  118. dSprintf( (char*)output, sizeof(output), "struct VertexData\r\n" );
  119. stream.write( dStrlen((char*)output), output );
  120. dSprintf( (char*)output, sizeof(output), "{\r\n" );
  121. stream.write( dStrlen((char*)output), output );
  122. for( U32 i=0; i<mElementList.size(); i++ )
  123. {
  124. Var *var = mElementList[i];
  125. if( var->arraySize == 1)
  126. {
  127. dSprintf( (char*)output, sizeof(output), " %s %s;\r\n", var->type, (char*)var->name );
  128. stream.write( dStrlen((char*)output), output );
  129. }
  130. else
  131. {
  132. dSprintf( (char*)output, sizeof(output), " %s %s[%d];\r\n", var->type, (char*)var->name, var->arraySize );
  133. stream.write( dStrlen((char*)output), output );
  134. }
  135. }
  136. dSprintf( (char*)output, sizeof(output), "} IN;\r\n\r\n" );
  137. stream.write( dStrlen((char*)output), output );
  138. // print in elements
  139. for( U32 i=0; i<mElementList.size(); i++ )
  140. {
  141. Var *var = mElementList[i];
  142. for(int j = 0; j < var->arraySize; ++j)
  143. {
  144. const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
  145. dSprintf( (char*)output, sizeof(output), "in %s %s;\r\n", var->type, name );
  146. stream.write( dStrlen((char*)output), output );
  147. }
  148. dSprintf( (char*)output, sizeof(output), "#define IN_%s IN.%s\r\n", var->name, var->name ); // TODO REMOVE
  149. stream.write( dStrlen((char*)output), output );
  150. }
  151. const char* newLine ="\r\n";
  152. stream.write( dStrlen((char*)newLine), newLine );
  153. }
  154. Var * VertPixelConnectorGLSL::getElement( RegisterType type,
  155. U32 numElements,
  156. U32 numRegisters )
  157. {
  158. switch( type )
  159. {
  160. case RT_POSITION:
  161. {
  162. Var *newVar = new Var;
  163. mElementList.push_back( newVar );
  164. newVar->setConnectName( "POSITION" );
  165. return newVar;
  166. }
  167. case RT_NORMAL:
  168. {
  169. Var *newVar = new Var;
  170. mElementList.push_back( newVar );
  171. newVar->setConnectName( "NORMAL" );
  172. return newVar;
  173. }
  174. case RT_COLOR:
  175. {
  176. Var *newVar = new Var;
  177. mElementList.push_back( newVar );
  178. newVar->setConnectName( "COLOR" );
  179. return newVar;
  180. }
  181. /*case RT_BINORMAL:
  182. {
  183. Var *newVar = new Var;
  184. mElementList.push_back( newVar );
  185. newVar->setConnectName( "BINORMAL" );
  186. return newVar;
  187. }
  188. case RT_TANGENT:
  189. {
  190. Var *newVar = new Var;
  191. mElementList.push_back( newVar );
  192. newVar->setConnectName( "TANGENT" );
  193. return newVar;
  194. } */
  195. case RT_TEXCOORD:
  196. case RT_BINORMAL:
  197. case RT_TANGENT:
  198. {
  199. Var *newVar = new Var;
  200. newVar->arraySize = numElements;
  201. char out[32];
  202. dSprintf( (char*)out, sizeof(out), "TEXCOORD%d", mCurTexElem );
  203. newVar->setConnectName( out );
  204. if ( numRegisters != -1 )
  205. mCurTexElem += numRegisters;
  206. else
  207. mCurTexElem += numElements;
  208. mElementList.push_back( newVar );
  209. return newVar;
  210. }
  211. default:
  212. break;
  213. }
  214. return NULL;
  215. }
  216. void VertPixelConnectorGLSL::sortVars()
  217. {
  218. // Not needed in GLSL
  219. }
  220. void VertPixelConnectorGLSL::setName( char *newName )
  221. {
  222. dStrcpy( (char*)mName, newName );
  223. }
  224. void VertPixelConnectorGLSL::reset()
  225. {
  226. for( U32 i=0; i<mElementList.size(); i++ )
  227. {
  228. mElementList[i] = NULL;
  229. }
  230. mElementList.setSize( 0 );
  231. mCurTexElem = 0;
  232. }
  233. void VertPixelConnectorGLSL::print( Stream &stream, bool isVerterShader )
  234. {
  235. // print out elements
  236. for( U32 i=0; i<mElementList.size(); i++ )
  237. {
  238. U8 output[256];
  239. Var *var = mElementList[i];
  240. if(!dStrcmp((const char*)var->name, "gl_Position"))
  241. continue;
  242. if(var->arraySize <= 1)
  243. dSprintf((char*)output, sizeof(output), "%s %s _%s_;\r\n", (isVerterShader ? "out" : "in"), var->type, var->connectName);
  244. else
  245. dSprintf((char*)output, sizeof(output), "%s %s _%s_[%d];\r\n", (isVerterShader ? "out" : "in"),var->type, var->connectName, var->arraySize);
  246. stream.write( dStrlen((char*)output), output );
  247. }
  248. printStructDefines(stream, !isVerterShader);
  249. }
  250. void VertPixelConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
  251. {
  252. if(isVerterShader)
  253. return;
  254. const char *newLine = "\r\n";
  255. const char *header = " //-------------------------\r\n";
  256. stream.write( dStrlen((char*)newLine), newLine );
  257. stream.write( dStrlen((char*)header), header );
  258. // print out elements
  259. for( U32 i=0; i<mElementList.size(); i++ )
  260. {
  261. U8 output[256];
  262. Var *var = mElementList[i];
  263. if(!dStrcmp((const char*)var->name, "gl_Position"))
  264. continue;
  265. dSprintf((char*)output, sizeof(output), " %s IN_%s = _%s_;\r\n", var->type, var->name, var->connectName);
  266. stream.write( dStrlen((char*)output), output );
  267. }
  268. stream.write( dStrlen((char*)header), header );
  269. stream.write( dStrlen((char*)newLine), newLine );
  270. }
  271. void AppVertConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
  272. {
  273. if(!isVerterShader)
  274. return;
  275. const char *newLine = "\r\n";
  276. const char *header = " //-------------------------\r\n";
  277. stream.write( dStrlen((char*)newLine), newLine );
  278. stream.write( dStrlen((char*)header), header );
  279. // print out elements
  280. for( U32 i=0; i<mElementList.size(); i++ )
  281. {
  282. Var *var = mElementList[i];
  283. U8 output[256];
  284. if(var->arraySize <= 1)
  285. {
  286. dSprintf((char*)output, sizeof(output), " IN.%s = %s;\r\n", var->name, var->connectName);
  287. stream.write( dStrlen((char*)output), output );
  288. }
  289. else
  290. {
  291. for(int j = 0; j < var->arraySize; ++j)
  292. {
  293. const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
  294. dSprintf((char*)output, sizeof(output), " IN.%s[%d] = %s;\r\n", var->name, j, name );
  295. stream.write( dStrlen((char*)output), output );
  296. }
  297. }
  298. }
  299. stream.write( dStrlen((char*)header), header );
  300. stream.write( dStrlen((char*)newLine), newLine );
  301. }
  302. Vector<String> initDeprecadedDefines()
  303. {
  304. Vector<String> vec;
  305. vec.push_back( "isBack");
  306. return vec;
  307. }
  308. void VertPixelConnectorGLSL::printStructDefines( Stream &stream, bool in )
  309. {
  310. const char* connectionDir = in ? "IN" : "OUT";
  311. static Vector<String> deprecatedDefines = initDeprecadedDefines();
  312. const char *newLine = "\r\n";
  313. const char *header = "// Struct defines\r\n";
  314. stream.write( dStrlen((char*)newLine), newLine );
  315. stream.write( dStrlen((char*)header), header );
  316. // print out elements
  317. for( U32 i=0; i<mElementList.size(); i++ )
  318. {
  319. U8 output[256];
  320. Var *var = mElementList[i];
  321. if(!dStrcmp((const char*)var->name, "gl_Position"))
  322. continue;
  323. if(!in)
  324. {
  325. dSprintf((char*)output, sizeof(output), "#define %s_%s _%s_\r\n", connectionDir, var->name, var->connectName);
  326. stream.write( dStrlen((char*)output), output );
  327. continue;
  328. }
  329. }
  330. stream.write( dStrlen((char*)newLine), newLine );
  331. }
  332. void VertexParamsDefGLSL::print( Stream &stream, bool isVerterShader )
  333. {
  334. // find all the uniform variables and print them out
  335. for( U32 i=0; i<LangElement::elementList.size(); i++)
  336. {
  337. Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
  338. if( var )
  339. {
  340. if( var->uniform )
  341. {
  342. U8 output[256];
  343. if(var->arraySize <= 1)
  344. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
  345. else
  346. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
  347. stream.write( dStrlen((char*)output), output );
  348. }
  349. }
  350. }
  351. const char *closer = "\r\n\r\nvoid main()\r\n{\r\n";
  352. stream.write( dStrlen(closer), closer );
  353. }
  354. void PixelParamsDefGLSL::print( Stream &stream, bool isVerterShader )
  355. {
  356. // find all the uniform variables and print them out
  357. for( U32 i=0; i<LangElement::elementList.size(); i++)
  358. {
  359. Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
  360. if( var )
  361. {
  362. if( var->uniform )
  363. {
  364. U8 output[256];
  365. if(var->arraySize <= 1)
  366. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
  367. else
  368. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
  369. stream.write( dStrlen((char*)output), output );
  370. }
  371. }
  372. }
  373. const char *closer = "\r\nvoid main()\r\n{\r\n";
  374. stream.write( dStrlen(closer), closer );
  375. for( U32 i=0; i<LangElement::elementList.size(); i++)
  376. {
  377. Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
  378. if( var )
  379. {
  380. if( var->uniform && !var->sampler)
  381. {
  382. U8 output[256];
  383. if(var->arraySize <= 1)
  384. dSprintf((char*)output, sizeof(output), " %s %s = %s;\r\n", var->type, var->name, var->name);
  385. else
  386. dSprintf((char*)output, sizeof(output), " %s %s[%d] = %s;\r\n", var->type, var->name, var->arraySize, var->name);
  387. stream.write( dStrlen((char*)output), output );
  388. }
  389. }
  390. }
  391. }