shaderCompGLSL.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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( "gl_Vertex" );
  38. return newVar;
  39. }
  40. case RT_NORMAL:
  41. {
  42. Var *newVar = new Var;
  43. mElementList.push_back( newVar );
  44. newVar->setConnectName( "gl_Normal" );
  45. return newVar;
  46. }
  47. case RT_COLOR:
  48. {
  49. Var *newVar = new Var;
  50. mElementList.push_back( newVar );
  51. newVar->setConnectName( "gl_Color" );
  52. return newVar;
  53. }
  54. case RT_TEXCOORD:
  55. case RT_BINORMAL:
  56. case RT_TANGENT:
  57. {
  58. Var *newVar = new Var;
  59. mElementList.push_back( newVar );
  60. char out[32];
  61. dSprintf( (char*)out, sizeof(out), "gl_MultiTexCoord%d", mCurTexElem );
  62. newVar->setConnectName( out );
  63. newVar->constNum = mCurTexElem;
  64. newVar->arraySize = numElements;
  65. if ( numRegisters != -1 )
  66. mCurTexElem += numRegisters;
  67. else
  68. mCurTexElem += numElements;
  69. return newVar;
  70. }
  71. default:
  72. break;
  73. }
  74. return NULL;
  75. }
  76. void AppVertConnectorGLSL::sortVars()
  77. {
  78. // Not required in GLSL
  79. }
  80. void AppVertConnectorGLSL::setName( char *newName )
  81. {
  82. dStrcpy( (char*)mName, newName );
  83. }
  84. void AppVertConnectorGLSL::reset()
  85. {
  86. for( U32 i=0; i<mElementList.size(); i++ )
  87. {
  88. mElementList[i] = NULL;
  89. }
  90. mElementList.setSize( 0 );
  91. mCurTexElem = 0;
  92. }
  93. void AppVertConnectorGLSL::print( Stream &stream )
  94. {
  95. // print out elements
  96. for( U32 i=0; i<mElementList.size(); i++ )
  97. {
  98. Var *var = mElementList[i];
  99. U8 output[256];
  100. const char* swizzle;
  101. if(!dStrcmp((const char*)var->type, "float"))
  102. swizzle = "x";
  103. else if(!dStrcmp((const char*)var->type, "vec2"))
  104. swizzle = "xy";
  105. else if(!dStrcmp((const char*)var->type, "vec3"))
  106. swizzle = "xyz";
  107. else
  108. swizzle = "xyzw";
  109. // This is ugly. We use #defines to match user defined names with
  110. // built in vars. There is no cleaner way to do this.
  111. dSprintf( (char*)output, sizeof(output), "#define %s %s.%s\r\n", var->name, var->connectName, swizzle );
  112. stream.write( dStrlen((char*)output), output );
  113. }
  114. }
  115. Var * VertPixelConnectorGLSL::getElement( RegisterType type,
  116. U32 numElements,
  117. U32 numRegisters )
  118. {
  119. switch( type )
  120. {
  121. case RT_POSITION:
  122. case RT_NORMAL:
  123. case RT_COLOR:
  124. {
  125. Var *newVar = new Var;
  126. mElementList.push_back( newVar );
  127. return newVar;
  128. }
  129. case RT_TEXCOORD:
  130. case RT_BINORMAL:
  131. case RT_TANGENT:
  132. {
  133. Var *newVar = new Var;
  134. newVar->arraySize = numElements;
  135. if ( numRegisters != -1 )
  136. mCurTexElem += numRegisters;
  137. else
  138. mCurTexElem += numElements;
  139. mElementList.push_back( newVar );
  140. return newVar;
  141. }
  142. default:
  143. break;
  144. }
  145. return NULL;
  146. }
  147. void VertPixelConnectorGLSL::sortVars()
  148. {
  149. // Not needed in GLSL
  150. }
  151. void VertPixelConnectorGLSL::setName( char *newName )
  152. {
  153. dStrcpy( (char*)mName, newName );
  154. }
  155. void VertPixelConnectorGLSL::reset()
  156. {
  157. for( U32 i=0; i<mElementList.size(); i++ )
  158. {
  159. mElementList[i] = NULL;
  160. }
  161. mElementList.setSize( 0 );
  162. mCurTexElem = 0;
  163. }
  164. void VertPixelConnectorGLSL::print( Stream &stream )
  165. {
  166. // print out elements
  167. for( U32 i=0; i<mElementList.size(); i++ )
  168. {
  169. U8 output[256];
  170. Var *var = mElementList[i];
  171. if(!dStrcmp((const char*)var->name, "gl_Position"))
  172. continue;
  173. if(var->arraySize <= 1)
  174. dSprintf((char*)output, sizeof(output), "varying %s %s;\r\n", var->type, var->name);
  175. else
  176. dSprintf((char*)output, sizeof(output), "varying %s %s[%d];\r\n", var->type, var->name, var->arraySize);
  177. stream.write( dStrlen((char*)output), output );
  178. }
  179. }
  180. void VertexParamsDefGLSL::print( Stream &stream )
  181. {
  182. // find all the uniform variables and print them out
  183. for( U32 i=0; i<LangElement::elementList.size(); i++)
  184. {
  185. Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
  186. if( var )
  187. {
  188. if( var->uniform )
  189. {
  190. U8 output[256];
  191. if(var->arraySize <= 1)
  192. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
  193. else
  194. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
  195. stream.write( dStrlen((char*)output), output );
  196. }
  197. }
  198. }
  199. const char *closer = "\r\n\r\nvoid main()\r\n{\r\n";
  200. stream.write( dStrlen(closer), closer );
  201. }
  202. void PixelParamsDefGLSL::print( Stream &stream )
  203. {
  204. // find all the uniform variables and print them out
  205. for( U32 i=0; i<LangElement::elementList.size(); i++)
  206. {
  207. Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
  208. if( var )
  209. {
  210. if( var->uniform )
  211. {
  212. U8 output[256];
  213. if(var->arraySize <= 1)
  214. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
  215. else
  216. dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
  217. stream.write( dStrlen((char*)output), output );
  218. }
  219. }
  220. }
  221. const char *closer = "\r\nvoid main()\r\n{\r\n";
  222. stream.write( dStrlen(closer), closer );
  223. }