gfxGLShader.cpp 38 KB

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