gfxGLShader.cpp 38 KB

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