gfxGLShader.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 "gfx/gl/gfxGLShader.h"
  24. #include "gfx/gl/gfxGLVertexAttribLocation.h"
  25. #include "gfx/gl/gfxGLDevice.h"
  26. #include "core/frameAllocator.h"
  27. #include "core/stream/fileStream.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "math/mPoint2.h"
  30. #include "gfx/gfxStructs.h"
  31. #include "console/console.h"
  32. class GFXGLShaderConstHandle : public GFXShaderConstHandle
  33. {
  34. friend class GFXGLShader;
  35. public:
  36. GFXGLShaderConstHandle( GFXGLShader *shader );
  37. GFXGLShaderConstHandle( GFXGLShader *shader, const GFXShaderConstDesc &desc, GLuint loc, S32 samplerNum );
  38. virtual ~GFXGLShaderConstHandle();
  39. void reinit( const GFXShaderConstDesc &desc, GLuint loc, S32 samplerNum );
  40. const String& getName() const { return mDesc.name; }
  41. GFXShaderConstType getType() const { return mDesc.constType; }
  42. U32 getArraySize() const { return mDesc.arraySize; }
  43. U32 getSize() const;
  44. void setValid( bool valid ) { mValid = valid; }
  45. /// @warning This will always return the value assigned when the shader was
  46. /// initialized. If the value is later changed this method won't reflect that.
  47. S32 getSamplerRegister() const { return mSamplerNum; }
  48. GFXShaderConstDesc mDesc;
  49. GFXGLShader* mShader;
  50. GLuint mLocation;
  51. U32 mOffset;
  52. U32 mSize;
  53. S32 mSamplerNum;
  54. bool mInstancingConstant;
  55. };
  56. GFXGLShaderConstHandle::GFXGLShaderConstHandle( GFXGLShader *shader )
  57. : mShader( shader ), mLocation(0), mOffset(0), mSize(0), mSamplerNum(-1), mInstancingConstant(false)
  58. {
  59. mValid = false;
  60. }
  61. static U32 shaderConstTypeSize(GFXShaderConstType type)
  62. {
  63. switch(type)
  64. {
  65. case GFXSCT_Float:
  66. case GFXSCT_Int:
  67. case GFXSCT_Sampler:
  68. case GFXSCT_SamplerCube:
  69. return 4;
  70. case GFXSCT_Float2:
  71. case GFXSCT_Int2:
  72. return 8;
  73. case GFXSCT_Float3:
  74. case GFXSCT_Int3:
  75. return 12;
  76. case GFXSCT_Float4:
  77. case GFXSCT_Int4:
  78. return 16;
  79. case GFXSCT_Float2x2:
  80. return 16;
  81. case GFXSCT_Float3x3:
  82. return 36;
  83. case GFXSCT_Float4x4:
  84. return 64;
  85. default:
  86. AssertFatal(false,"shaderConstTypeSize - Unrecognized constant type");
  87. return 0;
  88. }
  89. }
  90. GFXGLShaderConstHandle::GFXGLShaderConstHandle( GFXGLShader *shader, const GFXShaderConstDesc &desc, GLuint loc, S32 samplerNum )
  91. : mShader(shader), mInstancingConstant(false)
  92. {
  93. reinit(desc, loc, samplerNum);
  94. }
  95. void GFXGLShaderConstHandle::reinit( const GFXShaderConstDesc& desc, GLuint loc, S32 samplerNum )
  96. {
  97. mDesc = desc;
  98. mLocation = loc;
  99. mSamplerNum = samplerNum;
  100. mOffset = 0;
  101. mInstancingConstant = false;
  102. U32 elemSize = shaderConstTypeSize(mDesc.constType);
  103. AssertFatal(elemSize, "GFXGLShaderConst::GFXGLShaderConst - elemSize is 0");
  104. mSize = mDesc.arraySize * elemSize;
  105. mValid = true;
  106. }
  107. U32 GFXGLShaderConstHandle::getSize() const
  108. {
  109. return mSize;
  110. }
  111. GFXGLShaderConstHandle::~GFXGLShaderConstHandle()
  112. {
  113. }
  114. GFXGLShaderConstBuffer::GFXGLShaderConstBuffer(GFXGLShader* shader, U32 bufSize, U8* existingConstants)
  115. {
  116. mShader = shader;
  117. mBuffer = new U8[bufSize];
  118. mWasLost = true;
  119. // Copy the existing constant buffer to preserve sampler numbers
  120. /// @warning This preserves a lot more than sampler numbers, obviously. If there
  121. /// is any code that assumes a new constant buffer will have everything set to
  122. /// 0, it will break.
  123. dMemcpy(mBuffer, existingConstants, bufSize);
  124. }
  125. GFXGLShaderConstBuffer::~GFXGLShaderConstBuffer()
  126. {
  127. delete[] mBuffer;
  128. if ( mShader )
  129. mShader->_unlinkBuffer( this );
  130. }
  131. template<typename ConstType>
  132. void GFXGLShaderConstBuffer::internalSet(GFXShaderConstHandle* handle, const ConstType& param)
  133. {
  134. AssertFatal(handle, "GFXGLShaderConstBuffer::internalSet - Handle is NULL!" );
  135. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::internalSet - Handle is not valid!" );
  136. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  137. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  138. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  139. U8 *buf = mBuffer + _glHandle->mOffset;
  140. if(_glHandle->mInstancingConstant)
  141. buf = mInstPtr + _glHandle->mOffset;
  142. dMemcpy(buf, &param, sizeof(ConstType));
  143. }
  144. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const F32 fv)
  145. {
  146. internalSet(handle, fv);
  147. }
  148. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2F& fv)
  149. {
  150. internalSet(handle, fv);
  151. }
  152. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3F& fv)
  153. {
  154. internalSet(handle, fv);
  155. }
  156. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4F& fv)
  157. {
  158. internalSet(handle, fv);
  159. }
  160. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const PlaneF& fv)
  161. {
  162. internalSet(handle, fv);
  163. }
  164. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const ColorF& fv)
  165. {
  166. internalSet(handle, fv);
  167. }
  168. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const S32 fv)
  169. {
  170. internalSet(handle, fv);
  171. }
  172. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2I& fv)
  173. {
  174. internalSet(handle, fv);
  175. }
  176. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3I& fv)
  177. {
  178. internalSet(handle, fv);
  179. }
  180. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4I& fv)
  181. {
  182. internalSet(handle, fv);
  183. }
  184. template<typename ConstType>
  185. void GFXGLShaderConstBuffer::internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv)
  186. {
  187. AssertFatal(handle, "GFXGLShaderConstBuffer::internalSet - Handle is NULL!" );
  188. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::internalSet - Handle is not valid!" );
  189. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  190. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  191. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  192. AssertFatal(!_glHandle->mInstancingConstant, "GFXGLShaderConstBuffer::set - Instancing not supported for array");
  193. const U8* fvBuffer = static_cast<const U8*>(fv.getBuffer());
  194. for(U32 i = 0; i < fv.size(); ++i)
  195. {
  196. dMemcpy(mBuffer + _glHandle->mOffset + i * sizeof(ConstType), fvBuffer, sizeof(ConstType));
  197. fvBuffer += fv.getElementSize();
  198. }
  199. }
  200. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv)
  201. {
  202. internalSet(handle, fv);
  203. }
  204. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv)
  205. {
  206. internalSet(handle, fv);
  207. }
  208. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv)
  209. {
  210. internalSet(handle, fv);
  211. }
  212. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv)
  213. {
  214. internalSet(handle, fv);
  215. }
  216. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv)
  217. {
  218. internalSet(handle, fv);
  219. }
  220. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv)
  221. {
  222. internalSet(handle, fv);
  223. }
  224. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv)
  225. {
  226. internalSet(handle, fv);
  227. }
  228. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv)
  229. {
  230. internalSet(handle, fv);
  231. }
  232. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType)
  233. {
  234. AssertFatal(handle, "GFXGLShaderConstBuffer::set - Handle is NULL!" );
  235. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::set - Handle is not valid!" );
  236. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  237. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  238. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  239. AssertFatal(!_glHandle->mInstancingConstant || matType == GFXSCT_Float4x4, "GFXGLShaderConstBuffer::set - Only support GFXSCT_Float4x4 for instancing");
  240. switch(matType)
  241. {
  242. case GFXSCT_Float2x2:
  243. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[0] = mat[0];
  244. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[1] = mat[1];
  245. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[2] = mat[4];
  246. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[3] = mat[5];
  247. break;
  248. case GFXSCT_Float3x3:
  249. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[0] = mat[0];
  250. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[1] = mat[1];
  251. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[2] = mat[2];
  252. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[3] = mat[4];
  253. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[4] = mat[5];
  254. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[5] = mat[6];
  255. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[6] = mat[8];
  256. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[7] = mat[9];
  257. reinterpret_cast<F32*>(mBuffer + _glHandle->mOffset)[8] = mat[10];
  258. break;
  259. case GFXSCT_Float4x4:
  260. {
  261. if(_glHandle->mInstancingConstant)
  262. {
  263. MatrixF transposed;
  264. mat.transposeTo(transposed);
  265. dMemcpy( mInstPtr + _glHandle->mOffset, (const F32*)transposed, sizeof(MatrixF) );
  266. return;
  267. }
  268. dMemcpy(mBuffer + _glHandle->mOffset, (const F32*)mat, sizeof(MatrixF));
  269. break;
  270. }
  271. default:
  272. AssertFatal(false, "GFXGLShaderConstBuffer::set - Invalid matrix type");
  273. break;
  274. }
  275. }
  276. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
  277. {
  278. AssertFatal(handle, "GFXGLShaderConstBuffer::set - Handle is NULL!" );
  279. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::set - Handle is not valid!" );
  280. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  281. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  282. AssertFatal(!_glHandle->mInstancingConstant, "GFXGLShaderConstBuffer::set - Instancing not supported for matrix arrays");
  283. switch (matrixType) {
  284. case GFXSCT_Float4x4:
  285. dMemcpy(mBuffer + _glHandle->mOffset, (F32*)mat, _glHandle->getSize());
  286. break;
  287. default:
  288. AssertFatal(false, "GFXGLShaderConstBuffer::set - setting array of non 4x4 matrices!");
  289. break;
  290. }
  291. }
  292. void GFXGLShaderConstBuffer::activate()
  293. {
  294. PROFILE_SCOPE(GFXGLShaderConstBuffer_activate);
  295. mShader->setConstantsFromBuffer(this);
  296. mWasLost = false;
  297. }
  298. const String GFXGLShaderConstBuffer::describeSelf() const
  299. {
  300. return String();
  301. }
  302. void GFXGLShaderConstBuffer::onShaderReload( GFXGLShader *shader )
  303. {
  304. AssertFatal( shader == mShader, "GFXGLShaderConstBuffer::onShaderReload, mismatched shaders!" );
  305. delete[] mBuffer;
  306. mBuffer = new U8[mShader->mConstBufferSize];
  307. dMemset(mBuffer, 0, mShader->mConstBufferSize);
  308. mWasLost = true;
  309. }
  310. GFXGLShader::GFXGLShader() :
  311. mVertexShader(0),
  312. mPixelShader(0),
  313. mProgram(0),
  314. mConstBufferSize(0),
  315. mConstBuffer(NULL)
  316. {
  317. }
  318. GFXGLShader::~GFXGLShader()
  319. {
  320. clearShaders();
  321. for(HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++)
  322. delete i->value;
  323. delete[] mConstBuffer;
  324. }
  325. void GFXGLShader::clearShaders()
  326. {
  327. glDeleteProgram(mProgram);
  328. glDeleteShader(mVertexShader);
  329. glDeleteShader(mPixelShader);
  330. mProgram = 0;
  331. mVertexShader = 0;
  332. mPixelShader = 0;
  333. }
  334. bool GFXGLShader::_init()
  335. {
  336. PROFILE_SCOPE(GFXGLShader_Init);
  337. // Don't initialize empty shaders.
  338. if ( mVertexFile.isEmpty() && mPixelFile.isEmpty() )
  339. return false;
  340. clearShaders();
  341. mProgram = glCreateProgram();
  342. // Set the macros and add the global ones.
  343. Vector<GFXShaderMacro> macros;
  344. macros.merge( mMacros );
  345. macros.merge( smGlobalMacros );
  346. // Add the shader version to the macros.
  347. const U32 mjVer = (U32)mFloor( mPixVersion );
  348. const U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f );
  349. macros.increment();
  350. macros.last().name = "TORQUE_SM";
  351. macros.last().value = String::ToString( mjVer * 10 + mnVer );
  352. macros.increment();
  353. macros.last().name = "TORQUE_VERTEX_SHADER";
  354. macros.last().value = "";
  355. // Default to true so we're "successful" if a vertex/pixel shader wasn't specified.
  356. bool compiledVertexShader = true;
  357. bool compiledPixelShader = true;
  358. // Compile the vertex and pixel shaders if specified.
  359. if(!mVertexFile.isEmpty())
  360. compiledVertexShader = initShader(mVertexFile, true, macros);
  361. macros.last().name = "TORQUE_PIXEL_SHADER";
  362. if(!mPixelFile.isEmpty())
  363. compiledPixelShader = initShader(mPixelFile, false, macros);
  364. // If either shader was present and failed to compile, bail.
  365. if(!compiledVertexShader || !compiledPixelShader)
  366. return false;
  367. //bind vertex attributes
  368. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Position, "vPosition");
  369. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Normal, "vNormal");
  370. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Color, "vColor");
  371. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Tangent, "vTangent");
  372. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TangentW, "vTangentW");
  373. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_Binormal, "vBinormal");
  374. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord0, "vTexCoord0");
  375. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord1, "vTexCoord1");
  376. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord2, "vTexCoord2");
  377. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord3, "vTexCoord3");
  378. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord4, "vTexCoord4");
  379. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord5, "vTexCoord5");
  380. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord6, "vTexCoord6");
  381. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord7, "vTexCoord7");
  382. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord8, "vTexCoord8");
  383. glBindAttribLocation(mProgram, Torque::GL_VertexAttrib_TexCoord9, "vTexCoord9");
  384. //bind fragment out color
  385. glBindFragDataLocation(mProgram, 0, "OUT_col");
  386. glBindFragDataLocation(mProgram, 1, "OUT_col1");
  387. glBindFragDataLocation(mProgram, 2, "OUT_col2");
  388. glBindFragDataLocation(mProgram, 3, "OUT_col3");
  389. // Link it!
  390. glLinkProgram( mProgram );
  391. GLint linkStatus;
  392. glGetProgramiv( mProgram, GL_LINK_STATUS, &linkStatus );
  393. // Dump the info log to the console
  394. U32 logLength = 0;
  395. glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  396. if ( logLength )
  397. {
  398. FrameAllocatorMarker fam;
  399. char* log = (char*)fam.alloc( logLength );
  400. glGetProgramInfoLog( mProgram, logLength, NULL, log );
  401. if ( linkStatus == GL_FALSE )
  402. {
  403. if ( smLogErrors )
  404. {
  405. Con::errorf( "GFXGLShader::init - Error linking shader!" );
  406. Con::errorf( "Program %s / %s: %s",
  407. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  408. }
  409. }
  410. else if ( smLogWarnings )
  411. {
  412. Con::warnf( "Program %s / %s: %s",
  413. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  414. }
  415. }
  416. // If we failed to link, bail.
  417. if ( linkStatus == GL_FALSE )
  418. return false;
  419. initConstantDescs();
  420. initHandles();
  421. // Notify Buffers we might have changed in size.
  422. // If this was our first init then we won't have any activeBuffers
  423. // to worry about unnecessarily calling.
  424. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
  425. for ( ; biter != mActiveBuffers.end(); biter++ )
  426. ((GFXGLShaderConstBuffer*)(*biter))->onShaderReload( this );
  427. return true;
  428. }
  429. void GFXGLShader::initConstantDescs()
  430. {
  431. mConstants.clear();
  432. GLint numUniforms;
  433. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
  434. GLint maxNameLength;
  435. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
  436. if(!maxNameLength)
  437. return;
  438. FrameTemp<GLchar> uniformName(maxNameLength);
  439. for(U32 i = 0; i < numUniforms; i++)
  440. {
  441. GLint size;
  442. GLenum type;
  443. glGetActiveUniform(mProgram, i, maxNameLength, NULL, &size, &type, uniformName);
  444. GFXShaderConstDesc desc;
  445. desc.name = String((char*)uniformName);
  446. // Remove array brackets from the name
  447. desc.name = desc.name.substr(0, desc.name.find('['));
  448. // Insert $ to match D3D behavior of having a $ prepended to parameters to main.
  449. desc.name.insert(0, '$');
  450. desc.arraySize = size;
  451. switch(type)
  452. {
  453. case GL_FLOAT:
  454. desc.constType = GFXSCT_Float;
  455. break;
  456. case GL_FLOAT_VEC2:
  457. desc.constType = GFXSCT_Float2;
  458. break;
  459. case GL_FLOAT_VEC3:
  460. desc.constType = GFXSCT_Float3;
  461. break;
  462. case GL_FLOAT_VEC4:
  463. desc.constType = GFXSCT_Float4;
  464. break;
  465. case GL_INT:
  466. desc.constType = GFXSCT_Int;
  467. break;
  468. case GL_INT_VEC2:
  469. desc.constType = GFXSCT_Int2;
  470. break;
  471. case GL_INT_VEC3:
  472. desc.constType = GFXSCT_Int3;
  473. break;
  474. case GL_INT_VEC4:
  475. desc.constType = GFXSCT_Int4;
  476. break;
  477. case GL_FLOAT_MAT2:
  478. desc.constType = GFXSCT_Float2x2;
  479. break;
  480. case GL_FLOAT_MAT3:
  481. desc.constType = GFXSCT_Float3x3;
  482. break;
  483. case GL_FLOAT_MAT4:
  484. desc.constType = GFXSCT_Float4x4;
  485. break;
  486. case GL_SAMPLER_1D:
  487. case GL_SAMPLER_2D:
  488. case GL_SAMPLER_3D:
  489. case GL_SAMPLER_1D_SHADOW:
  490. case GL_SAMPLER_2D_SHADOW:
  491. desc.constType = GFXSCT_Sampler;
  492. break;
  493. case GL_SAMPLER_CUBE:
  494. desc.constType = GFXSCT_SamplerCube;
  495. break;
  496. default:
  497. AssertFatal(false, "GFXGLShader::initConstantDescs - unrecognized uniform type");
  498. // If we don't recognize the constant don't add its description.
  499. continue;
  500. }
  501. mConstants.push_back(desc);
  502. }
  503. }
  504. void GFXGLShader::initHandles()
  505. {
  506. // Mark all existing handles as invalid.
  507. // Those that are found when parsing the descriptions will then be marked valid again.
  508. for ( HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter )
  509. (iter->value)->setValid( false );
  510. mValidHandles.clear();
  511. // Loop through all ConstantDescriptions,
  512. // if they aren't in the HandleMap add them, if they are reinitialize them.
  513. for ( U32 i = 0; i < mConstants.size(); i++ )
  514. {
  515. GFXShaderConstDesc &desc = mConstants[i];
  516. // Index element 1 of the name to skip the '$' we inserted earier.
  517. GLint loc = glGetUniformLocation(mProgram, &desc.name.c_str()[1]);
  518. AssertFatal(loc != -1, "");
  519. HandleMap::Iterator handle = mHandles.find(desc.name);
  520. S32 sampler = -1;
  521. if(desc.constType == GFXSCT_Sampler || desc.constType == GFXSCT_SamplerCube)
  522. {
  523. S32 idx = mSamplerNamesOrdered.find_next(desc.name);
  524. AssertFatal(idx != -1, "");
  525. sampler = idx; //assignedSamplerNum++;
  526. }
  527. if ( handle != mHandles.end() )
  528. {
  529. handle->value->reinit( desc, loc, sampler );
  530. }
  531. else
  532. {
  533. mHandles[desc.name] = new GFXGLShaderConstHandle( this, desc, loc, sampler );
  534. }
  535. }
  536. // Loop through handles once more to set their offset and calculate our
  537. // constBuffer size.
  538. if ( mConstBuffer )
  539. delete[] mConstBuffer;
  540. mConstBufferSize = 0;
  541. for ( HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter )
  542. {
  543. GFXGLShaderConstHandle* handle = iter->value;
  544. if ( handle->isValid() )
  545. {
  546. mValidHandles.push_back(handle);
  547. handle->mOffset = mConstBufferSize;
  548. mConstBufferSize += handle->getSize();
  549. }
  550. }
  551. mConstBuffer = new U8[mConstBufferSize];
  552. dMemset(mConstBuffer, 0, mConstBufferSize);
  553. // Set our program so uniforms are assigned properly.
  554. glUseProgram(mProgram);
  555. // Iterate through uniforms to set sampler numbers.
  556. for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
  557. {
  558. GFXGLShaderConstHandle* handle = iter->value;
  559. if(handle->isValid() && (handle->getType() == GFXSCT_Sampler || handle->getType() == GFXSCT_SamplerCube))
  560. {
  561. // Set sampler number on our program.
  562. glUniform1i(handle->mLocation, handle->mSamplerNum);
  563. // Set sampler in constant buffer so it does not get unset later.
  564. dMemcpy(mConstBuffer + handle->mOffset, &handle->mSamplerNum, handle->getSize());
  565. }
  566. }
  567. glUseProgram(0);
  568. //instancing
  569. if (!mInstancingFormat)
  570. return;
  571. U32 offset = 0;
  572. for ( U32 i=0; i < mInstancingFormat->getElementCount(); i++ )
  573. {
  574. const GFXVertexElement &element = mInstancingFormat->getElement( i );
  575. String constName = String::ToString( "$%s", element.getSemantic().c_str() );
  576. HandleMap::Iterator handle = mHandles.find(constName);
  577. if ( handle != mHandles.end() )
  578. {
  579. AssertFatal(0, "");
  580. }
  581. else
  582. {
  583. GFXShaderConstDesc desc;
  584. desc.name = constName;
  585. desc.arraySize = 1;
  586. switch(element.getType())
  587. {
  588. case GFXDeclType_Float4:
  589. desc.constType = GFXSCT_Float4;
  590. break;
  591. default:
  592. desc.constType = GFXSCT_Float;
  593. break;
  594. }
  595. GFXGLShaderConstHandle *h = new GFXGLShaderConstHandle( this, desc, -1, -1 );
  596. h->mInstancingConstant = true;
  597. h->mOffset = offset;
  598. mHandles[constName] = h;
  599. offset += element.getSizeInBytes();
  600. ++i;
  601. // If this is a matrix we will have 2 or 3 more of these
  602. // semantics with the same name after it.
  603. for ( ; i < mInstancingFormat->getElementCount(); i++ )
  604. {
  605. const GFXVertexElement &nextElement = mInstancingFormat->getElement( i );
  606. if ( nextElement.getSemantic() != element.getSemantic() )
  607. {
  608. i--;
  609. break;
  610. }
  611. ++desc.arraySize;
  612. if(desc.arraySize == 4 && desc.constType == GFXSCT_Float4)
  613. {
  614. desc.arraySize = 1;
  615. desc.constType = GFXSCT_Float4x4;
  616. }
  617. offset += nextElement.getSizeInBytes();
  618. }
  619. }
  620. }
  621. }
  622. GFXShaderConstHandle* GFXGLShader::getShaderConstHandle(const String& name)
  623. {
  624. HandleMap::Iterator i = mHandles.find(name);
  625. if(i != mHandles.end())
  626. return i->value;
  627. else
  628. {
  629. GFXGLShaderConstHandle* handle = new GFXGLShaderConstHandle( this );
  630. mHandles[ name ] = handle;
  631. return handle;
  632. }
  633. }
  634. GFXShaderConstHandle* GFXGLShader::findShaderConstHandle(const String& name)
  635. {
  636. HandleMap::Iterator i = mHandles.find(name);
  637. if(i != mHandles.end())
  638. return i->value;
  639. else
  640. {
  641. return NULL;
  642. }
  643. }
  644. void GFXGLShader::setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer)
  645. {
  646. for(Vector<GFXGLShaderConstHandle*>::iterator i = mValidHandles.begin(); i != mValidHandles.end(); ++i)
  647. {
  648. GFXGLShaderConstHandle* handle = *i;
  649. AssertFatal(handle, "GFXGLShader::setConstantsFromBuffer - Null handle");
  650. if(handle->mInstancingConstant)
  651. continue;
  652. // Don't set if the value has not be changed.
  653. if(dMemcmp(mConstBuffer + handle->mOffset, buffer->mBuffer + handle->mOffset, handle->getSize()) == 0)
  654. continue;
  655. // Copy new value into our const buffer and set in GL.
  656. dMemcpy(mConstBuffer + handle->mOffset, buffer->mBuffer + handle->mOffset, handle->getSize());
  657. switch(handle->mDesc.constType)
  658. {
  659. case GFXSCT_Float:
  660. glUniform1fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  661. break;
  662. case GFXSCT_Float2:
  663. glUniform2fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  664. break;
  665. case GFXSCT_Float3:
  666. glUniform3fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  667. break;
  668. case GFXSCT_Float4:
  669. glUniform4fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  670. break;
  671. case GFXSCT_Int:
  672. case GFXSCT_Sampler:
  673. case GFXSCT_SamplerCube:
  674. glUniform1iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  675. break;
  676. case GFXSCT_Int2:
  677. glUniform2iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  678. break;
  679. case GFXSCT_Int3:
  680. glUniform3iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  681. break;
  682. case GFXSCT_Int4:
  683. glUniform4iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  684. break;
  685. case GFXSCT_Float2x2:
  686. glUniformMatrix2fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  687. break;
  688. case GFXSCT_Float3x3:
  689. glUniformMatrix3fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  690. break;
  691. case GFXSCT_Float4x4:
  692. glUniformMatrix4fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  693. break;
  694. default:
  695. AssertFatal(0,"");
  696. break;
  697. }
  698. }
  699. }
  700. GFXShaderConstBufferRef GFXGLShader::allocConstBuffer()
  701. {
  702. GFXGLShaderConstBuffer* buffer = new GFXGLShaderConstBuffer(this, mConstBufferSize, mConstBuffer);
  703. buffer->registerResourceWithDevice(getOwningDevice());
  704. mActiveBuffers.push_back( buffer );
  705. return buffer;
  706. }
  707. void GFXGLShader::useProgram()
  708. {
  709. glUseProgram(mProgram);
  710. }
  711. void GFXGLShader::zombify()
  712. {
  713. clearShaders();
  714. dMemset(mConstBuffer, 0, mConstBufferSize);
  715. }
  716. char* GFXGLShader::_handleIncludes( const Torque::Path& path, FileStream *s )
  717. {
  718. // TODO: The #line pragma on GLSL takes something called a
  719. // "source-string-number" which it then never explains.
  720. //
  721. // Until i resolve this mystery i disabled this.
  722. //
  723. //String linePragma = String::ToString( "#line 1 \r\n");
  724. //U32 linePragmaLen = linePragma.length();
  725. U32 shaderLen = s->getStreamSize();
  726. char* buffer = (char*)dMalloc(shaderLen + 1);
  727. //dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
  728. s->read(shaderLen, buffer);
  729. buffer[shaderLen] = 0;
  730. char* p = dStrstr(buffer, "#include");
  731. while(p)
  732. {
  733. char* q = p;
  734. p += 8;
  735. if(dIsspace(*p))
  736. {
  737. U32 n = 0;
  738. while(dIsspace(*p)) ++p;
  739. AssertFatal(*p == '"', "Bad #include directive");
  740. ++p;
  741. static char includeFile[256];
  742. while(*p != '"')
  743. {
  744. AssertFatal(*p != 0, "Bad #include directive");
  745. includeFile[n++] = *p++;
  746. AssertFatal(n < sizeof(includeFile), "#include directive too long");
  747. }
  748. ++p;
  749. includeFile[n] = 0;
  750. // First try it as a local file.
  751. Torque::Path includePath = Torque::Path::Join(path.getPath(), '/', includeFile);
  752. includePath = Torque::Path::CompressPath(includePath);
  753. FileStream includeStream;
  754. if ( !includeStream.open( includePath, Torque::FS::File::Read ) )
  755. {
  756. // Try again assuming the path is absolute
  757. // and/or relative.
  758. includePath = String( includeFile );
  759. includePath = Torque::Path::CompressPath(includePath);
  760. if ( !includeStream.open( includePath, Torque::FS::File::Read ) )
  761. {
  762. AssertISV(false, avar("failed to open include '%s'.", includePath.getFullPath().c_str()));
  763. if ( smLogErrors )
  764. Con::errorf( "GFXGLShader::_handleIncludes - Failed to open include '%s'.",
  765. includePath.getFullPath().c_str() );
  766. // Fail... don't return the buffer.
  767. dFree(buffer);
  768. return NULL;
  769. }
  770. }
  771. char* includedText = _handleIncludes(includePath, &includeStream);
  772. // If a sub-include fails... cleanup and return.
  773. if ( !includedText )
  774. {
  775. dFree(buffer);
  776. return NULL;
  777. }
  778. // TODO: Disabled till this is fixed correctly.
  779. //
  780. // Count the number of lines in the file
  781. // before the include.
  782. /*
  783. U32 includeLine = 0;
  784. {
  785. char* nl = dStrstr( buffer, "\n" );
  786. while ( nl )
  787. {
  788. includeLine++;
  789. nl = dStrstr( nl, "\n" );
  790. if(nl) ++nl;
  791. }
  792. }
  793. */
  794. String manip(buffer);
  795. manip.erase(q-buffer, p-q);
  796. String sItx(includedText);
  797. // TODO: Disabled till this is fixed correctly.
  798. //
  799. // Add a new line pragma to restore the proper
  800. // file and line number after the include.
  801. //sItx += String::ToString( "\r\n#line %d \r\n", includeLine );
  802. dFree(includedText);
  803. manip.insert(q-buffer, sItx);
  804. char* manipBuf = dStrdup(manip.c_str());
  805. p = manipBuf + (q - buffer);
  806. dFree(buffer);
  807. buffer = manipBuf;
  808. }
  809. p = dStrstr(p, "#include");
  810. }
  811. return buffer;
  812. }
  813. bool GFXGLShader::_loadShaderFromStream( GLuint shader,
  814. const Torque::Path &path,
  815. FileStream *s,
  816. const Vector<GFXShaderMacro> &macros )
  817. {
  818. Vector<char*> buffers;
  819. Vector<U32> lengths;
  820. // The GLSL version declaration must go first!
  821. const char *versionDecl = "#version 150\r\n";
  822. buffers.push_back( dStrdup( versionDecl ) );
  823. lengths.push_back( dStrlen( versionDecl ) );
  824. if(GFXGL->mCapabilities.shaderModel5)
  825. {
  826. const char *extension = "#extension GL_ARB_gpu_shader5 : enable\r\n";
  827. buffers.push_back( dStrdup( extension ) );
  828. lengths.push_back( dStrlen( extension ) );
  829. }
  830. const char *newLine = "\r\n";
  831. buffers.push_back( dStrdup( newLine ) );
  832. lengths.push_back( dStrlen( newLine ) );
  833. // Now add all the macros.
  834. for( U32 i = 0; i < macros.size(); i++ )
  835. {
  836. if(macros[i].name.isEmpty()) // TODO OPENGL
  837. continue;
  838. String define = String::ToString( "#define %s %s\n", macros[i].name.c_str(), macros[i].value.c_str() );
  839. buffers.push_back( dStrdup( define.c_str() ) );
  840. lengths.push_back( define.length() );
  841. }
  842. // Now finally add the shader source.
  843. U32 shaderLen = s->getStreamSize();
  844. char *buffer = _handleIncludes(path, s);
  845. if ( !buffer )
  846. return false;
  847. buffers.push_back(buffer);
  848. lengths.push_back(shaderLen);
  849. glShaderSource(shader, buffers.size(), (const GLchar**)const_cast<const char**>(buffers.address()), NULL);
  850. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  851. FileStream stream;
  852. if ( !stream.open( path.getFullPath()+"_DEBUG", Torque::FS::File::Write ) )
  853. {
  854. AssertISV(false, avar("GFXGLShader::initShader - failed to write debug shader '%s'.", path.getFullPath().c_str()));
  855. }
  856. for(int i = 0; i < buffers.size(); ++i)
  857. stream.writeText(buffers[i]);
  858. #endif
  859. // Cleanup the shader source buffer.
  860. for ( U32 i=0; i < buffers.size(); i++ )
  861. dFree( buffers[i] );
  862. glCompileShader(shader);
  863. return true;
  864. }
  865. bool GFXGLShader::initShader( const Torque::Path &file,
  866. bool isVertex,
  867. const Vector<GFXShaderMacro> &macros )
  868. {
  869. PROFILE_SCOPE(GFXGLShader_CompileShader);
  870. GLuint activeShader = glCreateShader(isVertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  871. if(isVertex)
  872. mVertexShader = activeShader;
  873. else
  874. mPixelShader = activeShader;
  875. glAttachShader(mProgram, activeShader);
  876. // Ok it's not in the shader gen manager, so ask Torque for it
  877. FileStream stream;
  878. if ( !stream.open( file, Torque::FS::File::Read ) )
  879. {
  880. AssertISV(false, avar("GFXGLShader::initShader - failed to open shader '%s'.", file.getFullPath().c_str()));
  881. if ( smLogErrors )
  882. Con::errorf( "GFXGLShader::initShader - Failed to open shader file '%s'.",
  883. file.getFullPath().c_str() );
  884. return false;
  885. }
  886. if ( !_loadShaderFromStream( activeShader, file, &stream, macros ) )
  887. return false;
  888. GLint compile;
  889. glGetShaderiv(activeShader, GL_COMPILE_STATUS, &compile);
  890. // Dump the info log to the console
  891. U32 logLength = 0;
  892. glGetShaderiv(activeShader, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  893. GLint compileStatus = GL_TRUE;
  894. if ( logLength )
  895. {
  896. FrameAllocatorMarker fam;
  897. char* log = (char*)fam.alloc(logLength);
  898. glGetShaderInfoLog(activeShader, logLength, NULL, log);
  899. // Always print errors
  900. glGetShaderiv( activeShader, GL_COMPILE_STATUS, &compileStatus );
  901. if ( compileStatus == GL_FALSE )
  902. {
  903. if ( smLogErrors )
  904. {
  905. Con::errorf( "GFXGLShader::initShader - Error compiling shader!" );
  906. Con::errorf( "Program %s: %s", file.getFullPath().c_str(), log );
  907. }
  908. }
  909. else if ( smLogWarnings )
  910. Con::warnf( "Program %s: %s", file.getFullPath().c_str(), log );
  911. }
  912. return compileStatus != GL_FALSE;
  913. }
  914. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  915. const Vector<GFXShaderConstDesc>& GFXGLShader::getShaderConstDesc() const
  916. {
  917. PROFILE_SCOPE(GFXGLShader_GetShaderConstants);
  918. return mConstants;
  919. }
  920. /// Returns the alignment value for constType
  921. U32 GFXGLShader::getAlignmentValue(const GFXShaderConstType constType) const
  922. {
  923. // Alignment is the same thing as size for us.
  924. return shaderConstTypeSize(constType);
  925. }
  926. const String GFXGLShader::describeSelf() const
  927. {
  928. String ret;
  929. ret = String::ToString(" Program: %i", mProgram);
  930. ret += String::ToString(" Vertex Path: %s", mVertexFile.getFullPath().c_str());
  931. ret += String::ToString(" Pixel Path: %s", mPixelFile.getFullPath().c_str());
  932. return ret;
  933. }