gfxGLShader.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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(GFXGLDevice* device) :
  327. mVertexShader(0),
  328. mPixelShader(0),
  329. mProgram(0),
  330. mDevice(device),
  331. mConstBufferSize(0),
  332. mConstBuffer(NULL)
  333. {
  334. }
  335. GFXGLShader::~GFXGLShader()
  336. {
  337. clearShaders();
  338. for(HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++)
  339. delete i->value;
  340. delete[] mConstBuffer;
  341. }
  342. void GFXGLShader::clearShaders()
  343. {
  344. glDeleteProgram(mProgram);
  345. glDeleteShader(mVertexShader);
  346. glDeleteShader(mPixelShader);
  347. mProgram = 0;
  348. mVertexShader = 0;
  349. mPixelShader = 0;
  350. }
  351. bool GFXGLShader::_init()
  352. {
  353. PROFILE_SCOPE(GFXGLShader_Init);
  354. // Don't initialize empty shaders.
  355. if ( mVertexFile.isEmpty() && mPixelFile.isEmpty() )
  356. return false;
  357. clearShaders();
  358. mProgram = glCreateProgram();
  359. // Set the macros and add the global ones.
  360. Vector<GFXShaderMacro> macros;
  361. macros.merge( mMacros );
  362. macros.merge( smGlobalMacros );
  363. macros.increment();
  364. macros.last().name = "TORQUE_SM";
  365. macros.last().value = 40;
  366. macros.increment();
  367. macros.last().name = "TORQUE_VERTEX_SHADER";
  368. macros.last().value = "";
  369. // Default to true so we're "successful" if a vertex/pixel shader wasn't specified.
  370. bool compiledVertexShader = true;
  371. bool compiledPixelShader = true;
  372. // Compile the vertex and pixel shaders if specified.
  373. if(!mVertexFile.isEmpty())
  374. compiledVertexShader = initShader(mVertexFile, true, macros);
  375. macros.last().name = "TORQUE_PIXEL_SHADER";
  376. if(!mPixelFile.isEmpty())
  377. compiledPixelShader = initShader(mPixelFile, false, macros);
  378. // If either shader was present and failed to compile, bail.
  379. if(!compiledVertexShader || !compiledPixelShader)
  380. return false;
  381. // Link it!
  382. glLinkProgram( mProgram );
  383. GLint activeAttribs = 0;
  384. glGetProgramiv(mProgram, GL_ACTIVE_ATTRIBUTES, &activeAttribs );
  385. GLint maxLength;
  386. glGetProgramiv(mProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
  387. FrameTemp<GLchar> tempData(maxLength+1);
  388. *tempData.address() = '\0';
  389. // Check atributes
  390. for (U32 i=0; i<activeAttribs; i++)
  391. {
  392. GLint size;
  393. GLenum type;
  394. glGetActiveAttrib(mProgram, i, maxLength + 1, NULL, &size, &type, tempData.address());
  395. StringTableEntry argName = StringTable->insert(tempData.address());
  396. CHECK_AARG(Torque::GL_VertexAttrib_Position, vPosition);
  397. CHECK_AARG(Torque::GL_VertexAttrib_Normal, vNormal);
  398. CHECK_AARG(Torque::GL_VertexAttrib_Color, vColor);
  399. CHECK_AARG(Torque::GL_VertexAttrib_Tangent, vTangent);
  400. CHECK_AARG(Torque::GL_VertexAttrib_TangentW, vTangentW);
  401. CHECK_AARG(Torque::GL_VertexAttrib_Binormal, vBinormal);
  402. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord0, vTexCoord0);
  403. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord1, vTexCoord1);
  404. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord2, vTexCoord2);
  405. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord3, vTexCoord3);
  406. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord4, vTexCoord4);
  407. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord5, vTexCoord5);
  408. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord6, vTexCoord6);
  409. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord7, vTexCoord7);
  410. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord8, vTexCoord8);
  411. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord9, vTexCoord9);
  412. }
  413. //always have OUT_col
  414. glBindFragDataLocation(mProgram, 0, "OUT_col");
  415. // Check OUT_colN
  416. for(U32 i=1;i<4;i++)
  417. {
  418. char buffer[10];
  419. dSprintf(buffer, sizeof(buffer), "OUT_col%u",i);
  420. GLint location = glGetFragDataLocation(mProgram, buffer);
  421. if(location>0)
  422. glBindFragDataLocation(mProgram, i, buffer);
  423. }
  424. // Link it again!
  425. glLinkProgram( mProgram );
  426. GLint linkStatus;
  427. glGetProgramiv( mProgram, GL_LINK_STATUS, &linkStatus );
  428. // Dump the info log to the console
  429. U32 logLength = 0;
  430. glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  431. if ( logLength )
  432. {
  433. FrameAllocatorMarker fam;
  434. char* log = (char*)fam.alloc( logLength );
  435. glGetProgramInfoLog( mProgram, logLength, NULL, log );
  436. if ( linkStatus == GL_FALSE )
  437. {
  438. if ( smLogErrors )
  439. {
  440. Con::errorf( "GFXGLShader::init - Error linking shader!" );
  441. Con::errorf( "Program %s / %s: %s",
  442. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  443. }
  444. }
  445. else if ( smLogWarnings )
  446. {
  447. Con::warnf( "Program %s / %s: %s",
  448. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  449. }
  450. }
  451. // If we failed to link, bail.
  452. if ( linkStatus == GL_FALSE )
  453. return false;
  454. initConstantDescs();
  455. initHandles();
  456. // Notify Buffers we might have changed in size.
  457. // If this was our first init then we won't have any activeBuffers
  458. // to worry about unnecessarily calling.
  459. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
  460. for ( ; biter != mActiveBuffers.end(); biter++ )
  461. ((GFXGLShaderConstBuffer*)(*biter))->onShaderReload( this );
  462. return true;
  463. }
  464. void GFXGLShader::initConstantDescs()
  465. {
  466. mConstants.clear();
  467. GLint numUniforms;
  468. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
  469. GLint maxNameLength;
  470. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
  471. if(!maxNameLength)
  472. return;
  473. maxNameLength++;
  474. FrameTemp<GLchar> uniformName(maxNameLength);
  475. for(U32 i = 0; i < numUniforms; i++)
  476. {
  477. GLint size;
  478. GLenum type;
  479. glGetActiveUniform(mProgram, i, maxNameLength, NULL, &size, &type, uniformName);
  480. GFXShaderConstDesc desc;
  481. desc.name = String((char*)uniformName);
  482. // Remove array brackets from the name
  483. desc.name = desc.name.substr(0, desc.name.find('['));
  484. // Insert $ to match D3D behavior of having a $ prepended to parameters to main.
  485. desc.name.insert(0, '$');
  486. desc.arraySize = size;
  487. switch(type)
  488. {
  489. case GL_FLOAT:
  490. desc.constType = GFXSCT_Float;
  491. break;
  492. case GL_FLOAT_VEC2:
  493. desc.constType = GFXSCT_Float2;
  494. break;
  495. case GL_FLOAT_VEC3:
  496. desc.constType = GFXSCT_Float3;
  497. break;
  498. case GL_FLOAT_VEC4:
  499. desc.constType = GFXSCT_Float4;
  500. break;
  501. case GL_INT:
  502. desc.constType = GFXSCT_Int;
  503. break;
  504. case GL_INT_VEC2:
  505. desc.constType = GFXSCT_Int2;
  506. break;
  507. case GL_INT_VEC3:
  508. desc.constType = GFXSCT_Int3;
  509. break;
  510. case GL_INT_VEC4:
  511. desc.constType = GFXSCT_Int4;
  512. break;
  513. case GL_FLOAT_MAT2:
  514. desc.constType = GFXSCT_Float2x2;
  515. break;
  516. case GL_FLOAT_MAT3:
  517. desc.constType = GFXSCT_Float3x3;
  518. break;
  519. case GL_FLOAT_MAT4:
  520. desc.constType = GFXSCT_Float4x4;
  521. break;
  522. case GL_FLOAT_MAT4x3: // jamesu - columns, rows
  523. desc.constType = GFXSCT_Float4x3;
  524. break;
  525. case GL_SAMPLER_1D:
  526. case GL_SAMPLER_2D:
  527. case GL_SAMPLER_3D:
  528. case GL_SAMPLER_1D_SHADOW:
  529. case GL_SAMPLER_2D_SHADOW:
  530. desc.constType = GFXSCT_Sampler;
  531. break;
  532. case GL_SAMPLER_CUBE:
  533. desc.constType = GFXSCT_SamplerCube;
  534. break;
  535. case GL_SAMPLER_CUBE_MAP_ARRAY_ARB:
  536. desc.constType = GFXSCT_SamplerCubeArray;
  537. break;
  538. case GL_SAMPLER_2D_ARRAY:
  539. desc.constType = GFXSCT_SamplerTextureArray;
  540. break;
  541. default:
  542. AssertFatal(false, "GFXGLShader::initConstantDescs - unrecognized uniform type");
  543. // If we don't recognize the constant don't add its description.
  544. continue;
  545. }
  546. mConstants.push_back(desc);
  547. }
  548. }
  549. void GFXGLShader::initHandles()
  550. {
  551. // Mark all existing handles as invalid.
  552. // Those that are found when parsing the descriptions will then be marked valid again.
  553. for ( HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter )
  554. (iter->value)->setValid( false );
  555. mValidHandles.clear();
  556. // Loop through all ConstantDescriptions,
  557. // if they aren't in the HandleMap add them, if they are reinitialize them.
  558. for ( U32 i = 0; i < mConstants.size(); i++ )
  559. {
  560. GFXShaderConstDesc &desc = mConstants[i];
  561. // Index element 1 of the name to skip the '$' we inserted earier.
  562. GLint loc = glGetUniformLocation(mProgram, &desc.name.c_str()[1]);
  563. AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str()));
  564. HandleMap::Iterator handle = mHandles.find(desc.name);
  565. S32 sampler = -1;
  566. if(desc.constType == GFXSCT_Sampler ||
  567. desc.constType == GFXSCT_SamplerCube ||
  568. desc.constType == GFXSCT_SamplerCubeArray ||
  569. desc.constType == GFXSCT_SamplerTextureArray)
  570. {
  571. S32 idx = mSamplerNamesOrdered.find_next(desc.name);
  572. AssertFatal(idx != -1, "");
  573. sampler = idx; //assignedSamplerNum++;
  574. }
  575. if ( handle != mHandles.end() )
  576. {
  577. handle->value->reinit( desc, loc, sampler );
  578. }
  579. else
  580. {
  581. mHandles[desc.name] = new GFXGLShaderConstHandle( this, desc, loc, sampler );
  582. }
  583. }
  584. // Loop through handles once more to set their offset and calculate our
  585. // constBuffer size.
  586. if ( mConstBuffer )
  587. delete[] mConstBuffer;
  588. mConstBufferSize = 0;
  589. for ( HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter )
  590. {
  591. GFXGLShaderConstHandle* handle = iter->value;
  592. if ( handle->isValid() )
  593. {
  594. mValidHandles.push_back(handle);
  595. handle->mOffset = mConstBufferSize;
  596. mConstBufferSize += handle->getSize();
  597. }
  598. }
  599. mConstBuffer = new U8[mConstBufferSize];
  600. dMemset(mConstBuffer, 0, mConstBufferSize);
  601. // Set our program so uniforms are assigned properly.
  602. mDevice->setShader(this, false);
  603. // Iterate through uniforms to set sampler numbers.
  604. for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
  605. {
  606. GFXGLShaderConstHandle* handle = iter->value;
  607. if(handle->isValid() &&
  608. (handle->getType() == GFXSCT_Sampler ||
  609. handle->getType() == GFXSCT_SamplerCube ||
  610. handle->getType() == GFXSCT_SamplerCubeArray ||
  611. handle->getType() == GFXSCT_SamplerTextureArray))
  612. {
  613. // Set sampler number on our program.
  614. glUniform1i(handle->mLocation, handle->mSamplerNum);
  615. // Set sampler in constant buffer so it does not get unset later.
  616. dMemcpy(mConstBuffer + handle->mOffset, &handle->mSamplerNum, handle->getSize());
  617. }
  618. }
  619. //instancing
  620. if (!mInstancingFormat)
  621. return;
  622. U32 offset = 0;
  623. for ( U32 i=0; i < mInstancingFormat->getElementCount(); i++ )
  624. {
  625. const GFXVertexElement &element = mInstancingFormat->getElement( i );
  626. String constName = String::ToString( "$%s", element.getSemantic().c_str() );
  627. HandleMap::Iterator handle = mHandles.find(constName);
  628. if ( handle != mHandles.end() )
  629. {
  630. AssertFatal(0, "");
  631. }
  632. else
  633. {
  634. GFXShaderConstDesc desc;
  635. desc.name = constName;
  636. desc.arraySize = 1;
  637. switch(element.getType())
  638. {
  639. case GFXDeclType_Float4:
  640. desc.constType = GFXSCT_Float4;
  641. break;
  642. default:
  643. desc.constType = GFXSCT_Float;
  644. break;
  645. }
  646. GFXGLShaderConstHandle *h = new GFXGLShaderConstHandle( this, desc, -1, -1 );
  647. h->mInstancingConstant = true;
  648. h->mOffset = offset;
  649. mHandles[constName] = h;
  650. offset += element.getSizeInBytes();
  651. ++i;
  652. // If this is a matrix we will have 2 or 3 more of these
  653. // semantics with the same name after it.
  654. for ( ; i < mInstancingFormat->getElementCount(); i++ )
  655. {
  656. const GFXVertexElement &nextElement = mInstancingFormat->getElement( i );
  657. if ( nextElement.getSemantic() != element.getSemantic() )
  658. {
  659. i--;
  660. break;
  661. }
  662. ++desc.arraySize;
  663. if(desc.arraySize == 4 && desc.constType == GFXSCT_Float4)
  664. {
  665. desc.arraySize = 1;
  666. desc.constType = GFXSCT_Float4x4;
  667. }
  668. offset += nextElement.getSizeInBytes();
  669. }
  670. }
  671. }
  672. }
  673. GFXShaderConstHandle* GFXGLShader::getShaderConstHandle(const String& name)
  674. {
  675. HandleMap::Iterator i = mHandles.find(name);
  676. if(i != mHandles.end())
  677. return i->value;
  678. else
  679. {
  680. GFXGLShaderConstHandle* handle = new GFXGLShaderConstHandle( this );
  681. mHandles[ name ] = handle;
  682. return handle;
  683. }
  684. }
  685. GFXShaderConstHandle* GFXGLShader::findShaderConstHandle(const String& name)
  686. {
  687. HandleMap::Iterator i = mHandles.find(name);
  688. if(i != mHandles.end())
  689. return i->value;
  690. else
  691. {
  692. return NULL;
  693. }
  694. }
  695. void GFXGLShader::setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer)
  696. {
  697. for(Vector<GFXGLShaderConstHandle*>::iterator i = mValidHandles.begin(); i != mValidHandles.end(); ++i)
  698. {
  699. GFXGLShaderConstHandle* handle = *i;
  700. AssertFatal(handle, "GFXGLShader::setConstantsFromBuffer - Null handle");
  701. if(handle->mInstancingConstant)
  702. continue;
  703. // Don't set if the value has not be changed.
  704. if(dMemcmp(mConstBuffer + handle->mOffset, buffer->mBuffer + handle->mOffset, handle->getSize()) == 0)
  705. continue;
  706. // Copy new value into our const buffer and set in GL.
  707. dMemcpy(mConstBuffer + handle->mOffset, buffer->mBuffer + handle->mOffset, handle->getSize());
  708. switch(handle->mDesc.constType)
  709. {
  710. case GFXSCT_Float:
  711. glUniform1fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  712. break;
  713. case GFXSCT_Float2:
  714. glUniform2fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  715. break;
  716. case GFXSCT_Float3:
  717. glUniform3fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  718. break;
  719. case GFXSCT_Float4:
  720. glUniform4fv(handle->mLocation, handle->mDesc.arraySize, (GLfloat*)(mConstBuffer + handle->mOffset));
  721. break;
  722. case GFXSCT_Int:
  723. case GFXSCT_Sampler:
  724. case GFXSCT_SamplerCube:
  725. case GFXSCT_SamplerCubeArray:
  726. case GFXSCT_SamplerTextureArray:
  727. glUniform1iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  728. break;
  729. case GFXSCT_Int2:
  730. glUniform2iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  731. break;
  732. case GFXSCT_Int3:
  733. glUniform3iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  734. break;
  735. case GFXSCT_Int4:
  736. glUniform4iv(handle->mLocation, handle->mDesc.arraySize, (GLint*)(mConstBuffer + handle->mOffset));
  737. break;
  738. case GFXSCT_Float2x2:
  739. glUniformMatrix2fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  740. break;
  741. case GFXSCT_Float3x3:
  742. glUniformMatrix3fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  743. break;
  744. case GFXSCT_Float4x3:
  745. // NOTE: To save a transpose here we could store the matrix transposed (i.e. column major) in the constant buffer.
  746. // See _mesa_uniform_matrix in the mesa source for the correct transpose algorithm for a 4x3 matrix.
  747. glUniformMatrix4x3fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  748. break;
  749. case GFXSCT_Float4x4:
  750. glUniformMatrix4fv(handle->mLocation, handle->mDesc.arraySize, true, (GLfloat*)(mConstBuffer + handle->mOffset));
  751. break;
  752. default:
  753. AssertFatal(0,"");
  754. break;
  755. }
  756. }
  757. }
  758. GFXShaderConstBufferRef GFXGLShader::allocConstBuffer()
  759. {
  760. GFXGLShaderConstBuffer* buffer = new GFXGLShaderConstBuffer(this, mConstBufferSize, mConstBuffer);
  761. buffer->registerResourceWithDevice(getOwningDevice());
  762. mActiveBuffers.push_back( buffer );
  763. return buffer;
  764. }
  765. void GFXGLShader::useProgram()
  766. {
  767. glUseProgram(mProgram);
  768. }
  769. void GFXGLShader::zombify()
  770. {
  771. clearShaders();
  772. dMemset(mConstBuffer, 0, mConstBufferSize);
  773. }
  774. char* GFXGLShader::_handleIncludes( const Torque::Path& path, FileStream *s )
  775. {
  776. // TODO: The #line pragma on GLSL takes something called a
  777. // "source-string-number" which it then never explains.
  778. //
  779. // Until i resolve this mystery i disabled this.
  780. //
  781. //String linePragma = String::ToString( "#line 1 \r\n");
  782. //U32 linePragmaLen = linePragma.length();
  783. U32 shaderLen = s->getStreamSize();
  784. char* buffer = (char*)dMalloc(shaderLen + 1);
  785. //dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
  786. s->read(shaderLen, buffer);
  787. buffer[shaderLen] = 0;
  788. char* p = dStrstr(buffer, "#include");
  789. while(p)
  790. {
  791. char* q = p;
  792. p += 8;
  793. if(dIsspace(*p))
  794. {
  795. U32 n = 0;
  796. while(dIsspace(*p)) ++p;
  797. AssertFatal(*p == '"', "Bad #include directive");
  798. ++p;
  799. static char includeFile[256];
  800. while(*p != '"')
  801. {
  802. AssertFatal(*p != 0, "Bad #include directive");
  803. includeFile[n++] = *p++;
  804. AssertFatal(n < sizeof(includeFile), "#include directive too long");
  805. }
  806. ++p;
  807. includeFile[n] = 0;
  808. // First try it as a local file.
  809. Torque::Path includePath = Torque::Path::Join(path.getPath(), '/', includeFile);
  810. includePath = Torque::Path::CompressPath(includePath);
  811. FileStream includeStream;
  812. if ( !includeStream.open( includePath, Torque::FS::File::Read ) )
  813. {
  814. // Try again assuming the path is absolute
  815. // and/or relative.
  816. includePath = String( includeFile );
  817. includePath = Torque::Path::CompressPath(includePath);
  818. if ( !includeStream.open( includePath, Torque::FS::File::Read ) )
  819. {
  820. AssertISV(false, avar("failed to open include '%s'.", includePath.getFullPath().c_str()));
  821. if ( smLogErrors )
  822. Con::errorf( "GFXGLShader::_handleIncludes - Failed to open include '%s'.",
  823. includePath.getFullPath().c_str() );
  824. // Fail... don't return the buffer.
  825. dFree(buffer);
  826. return NULL;
  827. }
  828. }
  829. char* includedText = _handleIncludes(includePath, &includeStream);
  830. // If a sub-include fails... cleanup and return.
  831. if ( !includedText )
  832. {
  833. dFree(buffer);
  834. return NULL;
  835. }
  836. // TODO: Disabled till this is fixed correctly.
  837. //
  838. // Count the number of lines in the file
  839. // before the include.
  840. /*
  841. U32 includeLine = 0;
  842. {
  843. char* nl = dStrstr( buffer, "\n" );
  844. while ( nl )
  845. {
  846. includeLine++;
  847. nl = dStrstr( nl, "\n" );
  848. if(nl) ++nl;
  849. }
  850. }
  851. */
  852. String manip(buffer);
  853. manip.erase(q-buffer, p-q);
  854. String sItx(includedText);
  855. // TODO: Disabled till this is fixed correctly.
  856. //
  857. // Add a new line pragma to restore the proper
  858. // file and line number after the include.
  859. //sItx += String::ToString( "\r\n#line %d \r\n", includeLine );
  860. dFree(includedText);
  861. manip.insert(q-buffer, sItx);
  862. char* manipBuf = dStrdup(manip.c_str());
  863. p = manipBuf + (q - buffer);
  864. dFree(buffer);
  865. buffer = manipBuf;
  866. }
  867. p = dStrstr(p, "#include");
  868. }
  869. return buffer;
  870. }
  871. bool GFXGLShader::_loadShaderFromStream( GLuint shader,
  872. const Torque::Path &path,
  873. FileStream *s,
  874. const Vector<GFXShaderMacro> &macros )
  875. {
  876. Vector<char*> buffers;
  877. Vector<U32> lengths;
  878. // The GLSL version declaration must go first!
  879. const char *versionDecl = "#version 330\n";
  880. buffers.push_back( dStrdup( versionDecl ) );
  881. lengths.push_back( dStrlen( versionDecl ) );
  882. //Required extensions. These are already checked when creating the GFX adapter, if we make it this far it's supported
  883. const char* cubeArrayExt = "#extension GL_ARB_texture_cube_map_array : enable\n";
  884. buffers.push_back(dStrdup(cubeArrayExt));
  885. lengths.push_back(dStrlen(cubeArrayExt));
  886. const char* gpuShader5Ext = "#extension GL_ARB_gpu_shader5 : enable\n";
  887. buffers.push_back(dStrdup(gpuShader5Ext));
  888. lengths.push_back(dStrlen(gpuShader5Ext));
  889. const char* newLine = "\r\n";
  890. buffers.push_back(dStrdup(newLine));
  891. lengths.push_back(dStrlen(newLine));
  892. // Now add all the macros.
  893. for( U32 i = 0; i < macros.size(); i++ )
  894. {
  895. if(macros[i].name.isEmpty()) // TODO OPENGL
  896. continue;
  897. String define = String::ToString( "#define %s %s\n", macros[i].name.c_str(), macros[i].value.c_str() );
  898. buffers.push_back( dStrdup( define.c_str() ) );
  899. lengths.push_back( define.length() );
  900. }
  901. // Now finally add the shader source.
  902. U32 shaderLen = s->getStreamSize();
  903. char *buffer = _handleIncludes(path, s);
  904. if ( !buffer )
  905. return false;
  906. buffers.push_back(buffer);
  907. lengths.push_back(shaderLen);
  908. glShaderSource(shader, buffers.size(), (const GLchar**)const_cast<const char**>(buffers.address()), NULL);
  909. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  910. FileStream stream;
  911. if ( !stream.open( path.getFullPath()+"_DEBUG", Torque::FS::File::Write ) )
  912. {
  913. AssertISV(false, avar("GFXGLShader::initShader - failed to write debug shader '%s'.", path.getFullPath().c_str()));
  914. }
  915. for(int i = 0; i < buffers.size(); ++i)
  916. stream.writeText(buffers[i]);
  917. #endif
  918. // Cleanup the shader source buffer.
  919. for ( U32 i=0; i < buffers.size(); i++ )
  920. dFree( buffers[i] );
  921. glCompileShader(shader);
  922. return true;
  923. }
  924. bool GFXGLShader::initShader( const Torque::Path &file,
  925. bool isVertex,
  926. const Vector<GFXShaderMacro> &macros )
  927. {
  928. PROFILE_SCOPE(GFXGLShader_CompileShader);
  929. GLuint activeShader = glCreateShader(isVertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  930. if(isVertex)
  931. mVertexShader = activeShader;
  932. else
  933. mPixelShader = activeShader;
  934. glAttachShader(mProgram, activeShader);
  935. // Ok it's not in the shader gen manager, so ask Torque for it
  936. FileStream stream;
  937. if ( !stream.open( file, Torque::FS::File::Read ) )
  938. {
  939. AssertISV(false, avar("GFXGLShader::initShader - failed to open shader '%s'.", file.getFullPath().c_str()));
  940. if ( smLogErrors )
  941. Con::errorf( "GFXGLShader::initShader - Failed to open shader file '%s'.",
  942. file.getFullPath().c_str() );
  943. return false;
  944. }
  945. if ( !_loadShaderFromStream( activeShader, file, &stream, macros ) )
  946. return false;
  947. GLint compile;
  948. glGetShaderiv(activeShader, GL_COMPILE_STATUS, &compile);
  949. // Dump the info log to the console
  950. U32 logLength = 0;
  951. glGetShaderiv(activeShader, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  952. GLint compileStatus = GL_TRUE;
  953. if ( logLength )
  954. {
  955. FrameAllocatorMarker fam;
  956. char* log = (char*)fam.alloc(logLength);
  957. glGetShaderInfoLog(activeShader, logLength, NULL, log);
  958. // Always print errors
  959. glGetShaderiv( activeShader, GL_COMPILE_STATUS, &compileStatus );
  960. if ( compileStatus == GL_FALSE )
  961. {
  962. if ( smLogErrors )
  963. {
  964. Con::errorf( "GFXGLShader::initShader - Error compiling shader!" );
  965. Con::errorf( "Program %s: %s", file.getFullPath().c_str(), log );
  966. }
  967. }
  968. else if ( smLogWarnings )
  969. Con::warnf( "Program %s: %s", file.getFullPath().c_str(), log );
  970. }
  971. return compileStatus != GL_FALSE;
  972. }
  973. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  974. const Vector<GFXShaderConstDesc>& GFXGLShader::getShaderConstDesc() const
  975. {
  976. PROFILE_SCOPE(GFXGLShader_GetShaderConstants);
  977. return mConstants;
  978. }
  979. /// Returns the alignment value for constType
  980. U32 GFXGLShader::getAlignmentValue(const GFXShaderConstType constType) const
  981. {
  982. // Alignment is the same thing as size for us.
  983. return shaderConstTypeSize(constType);
  984. }
  985. const String GFXGLShader::describeSelf() const
  986. {
  987. String ret;
  988. ret = String::ToString(" Program: %i", mProgram);
  989. ret += String::ToString(" Vertex Path: %s", mVertexFile.getFullPath().c_str());
  990. ret += String::ToString(" Pixel Path: %s", mPixelFile.getFullPath().c_str());
  991. return ret;
  992. }