gfxGLShader.cpp 38 KB

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