gfxGLShader.cpp 37 KB

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