gfxGLShader.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. GFXGLShaderConstHandle::GFXGLShaderConstHandle(GFXGLShader* shader)
  34. : mShader(shader),
  35. mUBOUniform(false),
  36. mInstancingConstant(false)
  37. {
  38. dMemset(&mDesc, 0, sizeof(mDesc));
  39. mValid = false;
  40. }
  41. GFXGLShaderConstHandle::GFXGLShaderConstHandle(GFXGLShader* shader,
  42. const GFXShaderConstDesc& desc)
  43. : mShader(shader),
  44. mDesc(desc),
  45. mUBOUniform(false),
  46. mInstancingConstant(false)
  47. {
  48. if (desc.constType == GFXSCT_ConstBuffer)
  49. mValid = false;
  50. else
  51. mValid = true;
  52. }
  53. void GFXGLShaderConstHandle::reinit(const GFXShaderConstDesc& desc)
  54. {
  55. mDesc = desc;
  56. mValid = true;
  57. }
  58. GFXGLShaderConstHandle::~GFXGLShaderConstHandle()
  59. {
  60. }
  61. const GFXShaderConstDesc GFXGLShaderConstHandle::getDesc()
  62. {
  63. return mDesc;
  64. }
  65. static U32 shaderConstTypeSize(GFXShaderConstType type)
  66. {
  67. switch (type)
  68. {
  69. case GFXSCT_Float:
  70. case GFXSCT_Int:
  71. case GFXSCT_UInt:
  72. case GFXSCT_Bool:
  73. case GFXSCT_Sampler:
  74. case GFXSCT_SamplerCube:
  75. case GFXSCT_SamplerCubeArray:
  76. case GFXSCT_SamplerTextureArray:
  77. return 4;
  78. case GFXSCT_Float2:
  79. case GFXSCT_Int2:
  80. case GFXSCT_UInt2:
  81. case GFXSCT_Bool2:
  82. return 8;
  83. case GFXSCT_Float3:
  84. case GFXSCT_Int3:
  85. case GFXSCT_UInt3:
  86. case GFXSCT_Bool3:
  87. return 12;
  88. case GFXSCT_Float4:
  89. case GFXSCT_Int4:
  90. case GFXSCT_UInt4:
  91. case GFXSCT_Bool4:
  92. return 16;
  93. case GFXSCT_Float2x2:
  94. return 16;
  95. case GFXSCT_Float3x3:
  96. return 36;
  97. case GFXSCT_Float4x3:
  98. return 48;
  99. case GFXSCT_Float4x4:
  100. return 64;
  101. default:
  102. AssertFatal(false, "shaderConstTypeSize - Unrecognized constant type");
  103. return 0;
  104. }
  105. }
  106. GFXGLShaderConstBuffer::GFXGLShaderConstBuffer(GFXGLShader* shader)
  107. {
  108. mShader = shader;
  109. mWasLost = true;
  110. }
  111. GFXGLShaderConstBuffer::~GFXGLShaderConstBuffer()
  112. {
  113. if (mShader)
  114. mShader->_unlinkBuffer(this);
  115. }
  116. template<typename ConstType>
  117. void GFXGLShaderConstBuffer::internalSet(GFXShaderConstHandle* handle, const ConstType& param)
  118. {
  119. AssertFatal(handle, "GFXGLShaderConstBuffer::internalSet - Handle is NULL!");
  120. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::internalSet - Handle is not valid!");
  121. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  122. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  123. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  124. U8* basePointer;
  125. if (!_glHandle->mUBOUniform)
  126. {
  127. basePointer = mBufferMap[-1].data;
  128. }
  129. else
  130. {
  131. basePointer = mBufferMap[_glHandle->mDesc.bindPoint].data;
  132. }
  133. U8* buf = basePointer + _glHandle->mDesc.offset;
  134. if (_glHandle->mInstancingConstant)
  135. buf = mInstPtr + _glHandle->mDesc.offset;
  136. dMemcpy(buf, &param, sizeof(ConstType));
  137. }
  138. GFXShader* GFXGLShaderConstBuffer::getShader()
  139. {
  140. return mShader;
  141. }
  142. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const F32 fv)
  143. {
  144. internalSet(handle, fv);
  145. }
  146. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2F& fv)
  147. {
  148. internalSet(handle, fv);
  149. }
  150. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3F& fv)
  151. {
  152. internalSet(handle, fv);
  153. }
  154. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4F& fv)
  155. {
  156. internalSet(handle, fv);
  157. }
  158. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const PlaneF& fv)
  159. {
  160. internalSet(handle, fv);
  161. }
  162. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const LinearColorF& fv)
  163. {
  164. internalSet(handle, fv);
  165. }
  166. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const S32 fv)
  167. {
  168. internalSet(handle, fv);
  169. }
  170. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2I& fv)
  171. {
  172. internalSet(handle, fv);
  173. }
  174. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3I& fv)
  175. {
  176. internalSet(handle, fv);
  177. }
  178. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4I& fv)
  179. {
  180. internalSet(handle, fv);
  181. }
  182. template<typename ConstType>
  183. void GFXGLShaderConstBuffer::internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv)
  184. {
  185. AssertFatal(handle, "GFXGLShaderConstBuffer::internalSet - Handle is NULL!");
  186. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::internalSet - Handle is not valid!");
  187. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  188. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  189. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  190. AssertFatal(!_glHandle->mInstancingConstant, "GFXGLShaderConstBuffer::set - Instancing not supported for array");
  191. U8* basePointer;
  192. if (!_glHandle->mUBOUniform)
  193. {
  194. basePointer = mBufferMap[-1].data;
  195. }
  196. else
  197. {
  198. basePointer = mBufferMap[_glHandle->mDesc.bindPoint].data;
  199. }
  200. const U8* fvBuffer = static_cast<const U8*>(fv.getBuffer());
  201. for (U32 i = 0; i < fv.size(); ++i)
  202. {
  203. dMemcpy(basePointer + _glHandle->mDesc.offset + i * sizeof(ConstType), fvBuffer, sizeof(ConstType));
  204. fvBuffer += fv.getElementSize();
  205. }
  206. }
  207. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv)
  208. {
  209. internalSet(handle, fv);
  210. }
  211. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv)
  212. {
  213. internalSet(handle, fv);
  214. }
  215. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv)
  216. {
  217. internalSet(handle, fv);
  218. }
  219. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv)
  220. {
  221. internalSet(handle, fv);
  222. }
  223. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv)
  224. {
  225. internalSet(handle, fv);
  226. }
  227. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv)
  228. {
  229. internalSet(handle, fv);
  230. }
  231. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv)
  232. {
  233. internalSet(handle, fv);
  234. }
  235. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv)
  236. {
  237. internalSet(handle, fv);
  238. }
  239. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matType)
  240. {
  241. AssertFatal(handle, "GFXGLShaderConstBuffer::set - Handle is NULL!");
  242. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::set - Handle is not valid!");
  243. AssertFatal(dynamic_cast<GFXGLShaderConstHandle*>(handle), "GFXGLShaderConstBuffer::set - Incorrect const buffer type");
  244. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  245. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  246. AssertFatal(!_glHandle->mInstancingConstant || matType == GFXSCT_Float4x4, "GFXGLShaderConstBuffer::set - Only support GFXSCT_Float4x4 for instancing");
  247. U8* basePointer;
  248. if (!_glHandle->mUBOUniform)
  249. {
  250. basePointer = mBufferMap[-1].data;
  251. }
  252. else
  253. {
  254. basePointer = mBufferMap[_glHandle->mDesc.bindPoint].data;
  255. }
  256. switch (matType)
  257. {
  258. case GFXSCT_Float2x2:
  259. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[0] = mat[0];
  260. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[1] = mat[1];
  261. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[2] = mat[4];
  262. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[3] = mat[5];
  263. break;
  264. case GFXSCT_Float3x3:
  265. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[0] = mat[0];
  266. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[1] = mat[1];
  267. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[2] = mat[2];
  268. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[3] = mat[4];
  269. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[4] = mat[5];
  270. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[5] = mat[6];
  271. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[6] = mat[8];
  272. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[7] = mat[9];
  273. reinterpret_cast<F32*>(basePointer + _glHandle->mDesc.offset)[8] = mat[10];
  274. break;
  275. case GFXSCT_Float4x3:
  276. dMemcpy(basePointer + _glHandle->mDesc.offset, (const F32*)mat, (sizeof(F32) * 12));// matrix with end row chopped off
  277. break;
  278. case GFXSCT_Float4x4:
  279. {
  280. if (_glHandle->mInstancingConstant)
  281. {
  282. MatrixF transposed;
  283. mat.transposeTo(transposed);
  284. dMemcpy(mInstPtr + _glHandle->mDesc.offset, (const F32*)transposed, sizeof(MatrixF));
  285. return;
  286. }
  287. dMemcpy(basePointer + _glHandle->mDesc.offset, (const F32*)mat, sizeof(MatrixF));
  288. break;
  289. }
  290. default:
  291. AssertFatal(false, "GFXGLShaderConstBuffer::set - Invalid matrix type");
  292. break;
  293. }
  294. }
  295. void GFXGLShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
  296. {
  297. AssertFatal(handle, "GFXGLShaderConstBuffer::set - Handle is NULL!");
  298. AssertFatal(handle->isValid(), "GFXGLShaderConstBuffer::set - Handle is not valid!");
  299. GFXGLShaderConstHandle* _glHandle = static_cast<GFXGLShaderConstHandle*>(handle);
  300. AssertFatal(mShader == _glHandle->mShader, "GFXGLShaderConstBuffer::set - Should only set handles which are owned by our shader");
  301. AssertFatal(!_glHandle->mInstancingConstant, "GFXGLShaderConstBuffer::set - Instancing not supported for matrix arrays");
  302. U8* basePointer;
  303. if (!_glHandle->mUBOUniform)
  304. {
  305. basePointer = mBufferMap[-1].data;
  306. }
  307. else
  308. {
  309. basePointer = mBufferMap[_glHandle->mDesc.bindPoint].data;
  310. }
  311. switch (matrixType) {
  312. case GFXSCT_Float4x3:
  313. // Copy each item with the last row chopped off
  314. for (int i = 0; i < arraySize; i++)
  315. {
  316. dMemcpy(basePointer + _glHandle->mDesc.offset + (i * (sizeof(F32) * 12)), (F32*)(mat + i), sizeof(F32) * 12);
  317. }
  318. break;
  319. case GFXSCT_Float4x4:
  320. dMemcpy(basePointer + _glHandle->mDesc.offset, (F32*)mat, _glHandle->getSize());
  321. break;
  322. default:
  323. AssertFatal(false, "GFXGLShaderConstBuffer::set - setting array of non 4x4 matrices!");
  324. break;
  325. }
  326. }
  327. void GFXGLShaderConstBuffer::activate(GFXGLShaderConstBuffer* prevShaderBuffer)
  328. {
  329. PROFILE_SCOPE(GFXGLShaderConstBuffer_activate);
  330. for (BufferMap::Iterator i = mBufferMap.begin(); i != mBufferMap.end(); ++i)
  331. {
  332. const S32 thisBufferDesc = i->key;
  333. // set the global buffer differently
  334. if (thisBufferDesc == -1)
  335. {
  336. mShader->setConstantsFromBuffer(mBufferMap[-1].data);
  337. continue;
  338. }
  339. ConstantBuffer thisBuff = i->value;
  340. if (prevShaderBuffer && prevShaderBuffer != this)
  341. {
  342. const ConstantBuffer prevBuffer = prevShaderBuffer->mBufferMap[i->key];
  343. if (prevBuffer.data && !prevBuffer.isDirty)
  344. {
  345. if (prevBuffer.size != thisBuff.size)
  346. {
  347. thisBuff.isDirty = true;
  348. }
  349. else
  350. {
  351. if (dMemcmp(prevBuffer.data, thisBuff.data, thisBuff.size) != 0)
  352. {
  353. thisBuff.isDirty = true;
  354. }
  355. else
  356. {
  357. thisBuff.isDirty = false;
  358. }
  359. }
  360. }
  361. else
  362. {
  363. thisBuff.isDirty = true;
  364. }
  365. }
  366. else
  367. {
  368. thisBuff.isDirty = true;
  369. }
  370. if (thisBuff.data && thisBuff.isDirty)
  371. {
  372. glBindBuffer(GL_UNIFORM_BUFFER, thisBuff.bufHandle);
  373. glBufferData(GL_UNIFORM_BUFFER, thisBuff.size, thisBuff.data, GL_DYNAMIC_DRAW);
  374. glBindBufferBase(GL_UNIFORM_BUFFER, thisBufferDesc, thisBuff.bufHandle);
  375. }
  376. }
  377. mWasLost = false;
  378. }
  379. void GFXGLShaderConstBuffer::addBuffer(const GFXShaderConstDesc desc)
  380. {
  381. // if this is the global buffer set it to the highest.
  382. if (desc.bindPoint == -1)
  383. {
  384. // we dont create a bufferhandle for this one.
  385. U8* buf = new U8[desc.size];
  386. dMemset(buf, 0, desc.size);
  387. mBufferMap[-1].data = buf;
  388. mBufferMap[-1].size = desc.size;
  389. mBufferMap[-1].isDirty = true;
  390. }
  391. else
  392. {
  393. U8* buf = new U8[desc.size];
  394. dMemset(buf, 0, desc.size);
  395. mBufferMap[desc.bindPoint].data = buf;
  396. mBufferMap[desc.bindPoint].size = desc.size;
  397. mBufferMap[desc.bindPoint].isDirty = true;
  398. mBufferMap[desc.bindPoint].bufHandle = GFXGL->getDeviceBuffer(desc);
  399. }
  400. }
  401. const String GFXGLShaderConstBuffer::describeSelf() const
  402. {
  403. return String();
  404. }
  405. void GFXGLShaderConstBuffer::onShaderReload(GFXGLShader* shader)
  406. {
  407. AssertFatal(shader == mShader, "GFXGLShaderConstBuffer::onShaderReload, mismatched shaders!");
  408. for (auto& pair : mBufferMap) {
  409. delete[] pair.value.data;
  410. }
  411. mBufferMap.clear(); // Clear the map
  412. for (GFXGLShader::BufferMap::Iterator i = shader->mBuffers.begin(); i != shader->mBuffers.end(); ++i)
  413. {
  414. // add our buffer descriptions to the full const buffer.
  415. this->addBuffer(i->value);
  416. }
  417. mWasLost = true;
  418. }
  419. GFXGLShader::GFXGLShader(GFXGLDevice* device) :
  420. mVertexShader(0),
  421. mPixelShader(0),
  422. mGeometryShader(0),
  423. mProgram(0),
  424. mDevice(device),
  425. mGlobalConstBuffer(NULL)
  426. {
  427. }
  428. GFXGLShader::~GFXGLShader()
  429. {
  430. clearShaders();
  431. for (auto& pair : mHandles) {
  432. if (pair.value != nullptr) {
  433. delete pair.value;
  434. pair.value = nullptr;
  435. }
  436. }
  437. mHandles.clear();
  438. if (mGlobalConstBuffer)
  439. delete[] mGlobalConstBuffer;
  440. }
  441. void GFXGLShader::clearShaders()
  442. {
  443. glDeleteProgram(mProgram);
  444. glDeleteShader(mVertexShader);
  445. glDeleteShader(mPixelShader);
  446. glDeleteShader(mGeometryShader);
  447. mProgram = 0;
  448. mVertexShader = 0;
  449. mPixelShader = 0;
  450. mGeometryShader = 0;
  451. }
  452. bool GFXGLShader::_init()
  453. {
  454. PROFILE_SCOPE(GFXGLShader_Init);
  455. // Don't initialize empty shaders.
  456. if (mVertexFile.isEmpty() && mPixelFile.isEmpty())
  457. return false;
  458. clearShaders();
  459. mProgram = glCreateProgram();
  460. // Set the macros and add the global ones.
  461. Vector<GFXShaderMacro> macros;
  462. macros.merge(mMacros);
  463. macros.merge(smGlobalMacros);
  464. macros.increment();
  465. macros.last().name = "TORQUE_SM";
  466. macros.last().value = 40;
  467. macros.increment();
  468. macros.last().name = "TORQUE_VERTEX_SHADER";
  469. macros.last().value = "";
  470. // Default to true so we're "successful" if a vertex/pixel shader wasn't specified.
  471. bool compiledVertexShader = true;
  472. bool compiledPixelShader = true;
  473. bool compiledGeometryShader = true;
  474. // Compile the vertex and pixel shaders if specified.
  475. if (!mVertexFile.isEmpty())
  476. {
  477. compiledVertexShader = initShader(mVertexFile, GFXShaderStage::VERTEX_SHADER, macros);
  478. if (!compiledVertexShader)
  479. return false;
  480. }
  481. if (!mPixelFile.isEmpty())
  482. {
  483. macros.last().name = "TORQUE_PIXEL_SHADER";
  484. compiledPixelShader = initShader(mPixelFile, GFXShaderStage::PIXEL_SHADER, macros);
  485. if (!compiledPixelShader)
  486. return false;
  487. }
  488. if (!mGeometryFile.isEmpty())
  489. {
  490. macros.last().name = "TORQUE_GEOMETRY_SHADER";
  491. compiledGeometryShader = initShader(mGeometryFile, GFXShaderStage::GEOMETRY_SHADER, macros);
  492. if (!compiledGeometryShader)
  493. return false;
  494. }
  495. // Link it!
  496. glLinkProgram(mProgram);
  497. GLint activeAttribs = 0;
  498. glGetProgramiv(mProgram, GL_ACTIVE_ATTRIBUTES, &activeAttribs);
  499. GLint maxLength;
  500. glGetProgramiv(mProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
  501. FrameTemp<GLchar> tempData(maxLength + 1);
  502. *tempData.address() = '\0';
  503. // Check atributes
  504. for (U32 i = 0; i < activeAttribs; i++)
  505. {
  506. GLint size;
  507. GLenum type;
  508. glGetActiveAttrib(mProgram, i, maxLength + 1, NULL, &size, &type, tempData.address());
  509. StringTableEntry argName = StringTable->insert(tempData.address());
  510. CHECK_AARG(Torque::GL_VertexAttrib_Position, vPosition);
  511. CHECK_AARG(Torque::GL_VertexAttrib_Normal, vNormal);
  512. CHECK_AARG(Torque::GL_VertexAttrib_Color, vColor);
  513. CHECK_AARG(Torque::GL_VertexAttrib_Tangent, vTangent);
  514. CHECK_AARG(Torque::GL_VertexAttrib_TangentW, vTangentW);
  515. CHECK_AARG(Torque::GL_VertexAttrib_Binormal, vBinormal);
  516. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord0, vTexCoord0);
  517. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord1, vTexCoord1);
  518. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord2, vTexCoord2);
  519. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord3, vTexCoord3);
  520. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord4, vTexCoord4);
  521. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord5, vTexCoord5);
  522. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord6, vTexCoord6);
  523. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord7, vTexCoord7);
  524. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord8, vTexCoord8);
  525. CHECK_AARG(Torque::GL_VertexAttrib_TexCoord9, vTexCoord9);
  526. }
  527. //always have OUT_col
  528. glBindFragDataLocation(mProgram, 0, "OUT_col");
  529. // Check OUT_colN
  530. for (U32 i = 1; i < 4; i++)
  531. {
  532. char buffer[10];
  533. dSprintf(buffer, sizeof(buffer), "OUT_col%u", i);
  534. GLint location = glGetFragDataLocation(mProgram, buffer);
  535. if (location > 0)
  536. glBindFragDataLocation(mProgram, i, buffer);
  537. }
  538. // Link it again!
  539. glLinkProgram(mProgram);
  540. GLint linkStatus;
  541. glGetProgramiv(mProgram, GL_LINK_STATUS, &linkStatus);
  542. // Dump the info log to the console
  543. U32 logLength = 0;
  544. glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  545. if (logLength)
  546. {
  547. FrameAllocatorMarker fam;
  548. char* log = (char*)fam.alloc(logLength);
  549. glGetProgramInfoLog(mProgram, logLength, NULL, log);
  550. if (linkStatus == GL_FALSE)
  551. {
  552. if (smLogErrors)
  553. {
  554. Con::errorf("GFXGLShader::init - Error linking shader!");
  555. Con::errorf("Program %s / %s: %s",
  556. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  557. }
  558. }
  559. else if (smLogWarnings)
  560. {
  561. Con::warnf("Program %s / %s: %s",
  562. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), log);
  563. }
  564. }
  565. // If we failed to link, bail.
  566. if (linkStatus == GL_FALSE)
  567. return false;
  568. initConstantDescs();
  569. initHandles();
  570. // Notify Buffers we might have changed in size.
  571. // If this was our first init then we won't have any activeBuffers
  572. // to worry about unnecessarily calling.
  573. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
  574. for (; biter != mActiveBuffers.end(); biter++)
  575. ((GFXGLShaderConstBuffer*)(*biter))->onShaderReload(this);
  576. return true;
  577. }
  578. void GFXGLShader::initConstantDescs()
  579. {
  580. // clear our vectors.
  581. mShaderConsts.clear();
  582. GLint maxNameLength;
  583. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxNameLength);
  584. if (!maxNameLength)
  585. return;
  586. maxNameLength++;
  587. FrameTemp<GLchar> uniformName(maxNameLength);
  588. // parse ubos first and add them to our table, same as in dx
  589. // this is required so that in the other uniform loop we dont add
  590. // a uniform that exists in a ubo again.
  591. GLint numUBOS;
  592. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUBOS);
  593. for (U32 i = 0; i < numUBOS; i++) {
  594. GFXShaderConstDesc desc;
  595. GLint uboNameLen;
  596. glGetActiveUniformBlockiv(mProgram, i, GL_UNIFORM_BLOCK_NAME_LENGTH, &uboNameLen);
  597. if (!uboNameLen)
  598. return;
  599. uboNameLen++;
  600. // get the name of the ubo for getting required data.
  601. FrameTemp<GLchar> uboName(uboNameLen);
  602. glGetActiveUniformBlockName(mProgram, i, uboNameLen, NULL, uboName);
  603. GLint uboBinding;
  604. glGetActiveUniformBlockiv(mProgram, i, GL_UNIFORM_BLOCK_BINDING, &uboBinding);
  605. GLint uboSize;
  606. glGetActiveUniformBlockiv(mProgram, i, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize);
  607. GLint numUboUniforms;
  608. glGetActiveUniformBlockiv(mProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &numUboUniforms);
  609. GLint* indices = new GLint[numUboUniforms];
  610. glGetActiveUniformBlockiv(mProgram, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, indices);
  611. // fill out ubo desc.
  612. desc.name = String((char*)uboName);
  613. desc.bindPoint = uboBinding == 0 ? glGetUniformBlockIndex(mProgram, uboName) : uboBinding;
  614. glUniformBlockBinding(mProgram, glGetUniformBlockIndex(mProgram, uboName), desc.bindPoint);
  615. desc.size = uboSize;
  616. desc.constType = GFXSCT_ConstBuffer;
  617. desc.samplerReg = -1;
  618. mBuffers[desc.name] = desc;
  619. // loop uniforms in the ubo.
  620. for (U32 j = 0; j < numUboUniforms; j++)
  621. {
  622. GFXShaderConstDesc varDesc;
  623. GLint uniformIndex = indices[j];
  624. GLint size;
  625. GLenum type;
  626. GLint offset;
  627. glGetActiveUniformsiv(mProgram, 1, (const GLuint*)&uniformIndex, GL_UNIFORM_OFFSET, &offset);
  628. glGetActiveUniform(mProgram, uniformIndex, maxNameLength, NULL, &size, &type, uniformName);
  629. varDesc.name = String((char*)uniformName);
  630. // remove array brackets.
  631. varDesc.name = varDesc.name.substr(0, varDesc.name.find('['));
  632. // Insert $ to match D3D behavior of having a $ prepended to parameters to main.
  633. varDesc.name.insert(0, '$');
  634. varDesc.bindPoint = desc.bindPoint; // just set to the buffer bindpoint for uniforms in a ubo.
  635. varDesc.offset = offset;
  636. varDesc.arraySize = size;
  637. varDesc.constType = convertConstType(type);
  638. varDesc.size = shaderConstTypeSize(varDesc.constType) * size;
  639. varDesc.samplerReg = -1;
  640. #ifdef OPENGL_DEBUG_SPEW
  641. Con::printf("Variable Name %s:, offset: %d, size: %d, constantDesc.Elements: %d", varDesc.name.c_str(), varDesc.offset, varDesc.size, varDesc.arraySize);
  642. #endif
  643. mShaderConsts.push_back(varDesc);
  644. }
  645. }
  646. GLint numUniforms;
  647. glGetProgramiv(mProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
  648. for (U32 i = 0; i < numUniforms; i++)
  649. {
  650. // skip if this uniform is inside a ubo.
  651. GLint blk;
  652. glGetActiveUniformsiv(mProgram, 1, (GLuint*)&i, GL_UNIFORM_BLOCK_INDEX, &blk);
  653. if (blk != -1)
  654. {
  655. continue;
  656. }
  657. GLint size;
  658. GLenum type;
  659. glGetActiveUniform(mProgram, i, maxNameLength, NULL, &size, &type, uniformName);
  660. GFXShaderConstDesc desc;
  661. desc.name = String((char*)uniformName);
  662. // Remove array brackets from the name
  663. desc.name = desc.name.substr(0, desc.name.find('['));
  664. // Insert $ to match D3D behavior of having a $ prepended to parameters to main.
  665. desc.name.insert(0, '$');
  666. desc.bindPoint = -1;
  667. desc.arraySize = size;
  668. desc.constType = convertConstType(type);
  669. desc.size = shaderConstTypeSize(desc.constType) * size;
  670. desc.samplerReg = -1;
  671. mShaderConsts.push_back(desc);
  672. }
  673. }
  674. GFXShaderConstType GFXGLShader::convertConstType(GLenum constType)
  675. {
  676. switch (constType)
  677. {
  678. case GL_FLOAT:
  679. return GFXSCT_Float;
  680. break;
  681. case GL_FLOAT_VEC2:
  682. return GFXSCT_Float2;
  683. break;
  684. case GL_FLOAT_VEC3:
  685. return GFXSCT_Float3;
  686. break;
  687. case GL_FLOAT_VEC4:
  688. return GFXSCT_Float4;
  689. break;
  690. case GL_INT:
  691. return GFXSCT_Int;
  692. break;
  693. case GL_INT_VEC2:
  694. return GFXSCT_Int2;
  695. break;
  696. case GL_INT_VEC3:
  697. return GFXSCT_Int3;
  698. break;
  699. case GL_INT_VEC4:
  700. return GFXSCT_Int4;
  701. break;
  702. case GL_UNSIGNED_INT:
  703. return GFXSCT_UInt;
  704. break;
  705. case GL_UNSIGNED_INT_VEC2:
  706. return GFXSCT_UInt2;
  707. break;
  708. case GL_UNSIGNED_INT_VEC3:
  709. return GFXSCT_UInt3;
  710. break;
  711. case GL_UNSIGNED_INT_VEC4:
  712. return GFXSCT_UInt4;
  713. break;
  714. case GL_BOOL:
  715. return GFXSCT_Bool;
  716. break;
  717. case GL_BOOL_VEC2:
  718. return GFXSCT_Bool2;
  719. break;
  720. case GL_BOOL_VEC3:
  721. return GFXSCT_Bool3;
  722. break;
  723. case GL_BOOL_VEC4:
  724. return GFXSCT_Bool4;
  725. break;
  726. case GL_FLOAT_MAT2:
  727. return GFXSCT_Float2x2;
  728. break;
  729. case GL_FLOAT_MAT3:
  730. return GFXSCT_Float3x3;
  731. break;
  732. case GL_FLOAT_MAT4:
  733. return GFXSCT_Float4x4;
  734. break;
  735. case GL_FLOAT_MAT4x3: // jamesu - columns, rows
  736. return GFXSCT_Float4x3;
  737. break;
  738. case GL_SAMPLER_1D:
  739. case GL_SAMPLER_2D:
  740. case GL_SAMPLER_3D:
  741. case GL_SAMPLER_1D_SHADOW:
  742. case GL_SAMPLER_2D_SHADOW:
  743. return GFXSCT_Sampler;
  744. break;
  745. case GL_SAMPLER_CUBE:
  746. return GFXSCT_SamplerCube;
  747. break;
  748. case GL_SAMPLER_CUBE_MAP_ARRAY_ARB:
  749. return GFXSCT_SamplerCubeArray;
  750. break;
  751. case GL_SAMPLER_2D_ARRAY:
  752. return GFXSCT_SamplerTextureArray;
  753. break;
  754. default:
  755. AssertFatal(false, "Unknown shader constant class enum, maybe you could add it?");
  756. // If we don't recognize the constant don't add its description.
  757. break;
  758. }
  759. return GFXSCT_Uknown;
  760. }
  761. void GFXGLShader::initHandles()
  762. {
  763. // Mark all existing handles as invalid.
  764. // Those that are found when parsing the descriptions will then be marked valid again.
  765. for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
  766. (iter->value)->setValid(false);
  767. // Loop through constants that exist in ubos.
  768. for (U32 i = 0; i < mShaderConsts.size(); i++)
  769. {
  770. GFXShaderConstDesc& desc = mShaderConsts[i];
  771. // Index element 1 of the name to skip the '$' we inserted earier.
  772. GLint loc = glGetUniformLocation(mProgram, &desc.name.c_str()[1]);
  773. // The location for uniforms inside a UBO come back as -1.
  774. // AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str()));
  775. HandleMap::Iterator handle = mHandles.find(desc.name);
  776. S32 sampler = -1;
  777. if (desc.constType == GFXSCT_Sampler ||
  778. desc.constType == GFXSCT_SamplerCube ||
  779. desc.constType == GFXSCT_SamplerCubeArray ||
  780. desc.constType == GFXSCT_SamplerTextureArray)
  781. {
  782. S32 idx = mSamplerNamesOrdered.find_next(desc.name);
  783. AssertFatal(idx != -1, "");
  784. sampler = idx; //assignedSamplerNum++;
  785. desc.samplerReg = idx;
  786. }
  787. if (handle != mHandles.end())
  788. {
  789. if (desc.bindPoint == -1)
  790. {
  791. AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str()));
  792. desc.bindPoint = loc;
  793. mHandles[desc.name]->mUBOUniform = false;
  794. }
  795. else
  796. {
  797. mHandles[desc.name]->mUBOUniform = true;
  798. }
  799. handle->value->reinit(desc);
  800. }
  801. else
  802. {
  803. if (desc.bindPoint == -1)
  804. {
  805. AssertFatal(loc != -1, avar("uniform %s in shader file Vert: (%s) Frag: (%s)", &desc.name.c_str()[1], mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str()));
  806. desc.bindPoint = loc;
  807. mHandles[desc.name] = new GFXGLShaderConstHandle(this, desc);
  808. mHandles[desc.name]->mUBOUniform = false;
  809. }
  810. else
  811. {
  812. mHandles[desc.name] = new GFXGLShaderConstHandle(this, desc);
  813. mHandles[desc.name]->mUBOUniform = true;
  814. }
  815. }
  816. }
  817. // we have a global const buffer, set it up and add it.
  818. U32 constBufferSize = 0;
  819. if (mGlobalConstBuffer)
  820. delete[] mGlobalConstBuffer;
  821. for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
  822. {
  823. GFXGLShaderConstHandle* handle = iter->value;
  824. if (handle->isValid() && !handle->mUBOUniform)
  825. {
  826. handle->mDesc.offset = constBufferSize;
  827. constBufferSize += handle->getSize();
  828. }
  829. }
  830. if (constBufferSize > 0)
  831. {
  832. GFXShaderConstDesc desc;
  833. // fill out ubo desc.
  834. desc.name = "Global";
  835. desc.bindPoint = -1;
  836. desc.size = constBufferSize;
  837. desc.constType = GFXSCT_ConstBuffer;
  838. desc.samplerReg = -1;
  839. mBuffers[desc.name] = desc;
  840. mGlobalConstBuffer = new U8[constBufferSize];
  841. dMemset(mGlobalConstBuffer, 0, constBufferSize);
  842. }
  843. // Set our program so uniforms are assigned properly.
  844. mDevice->setShader(this, false);
  845. // Iterate through uniforms to set sampler numbers.
  846. for (HandleMap::Iterator iter = mHandles.begin(); iter != mHandles.end(); ++iter)
  847. {
  848. GFXGLShaderConstHandle* handle = iter->value;
  849. if (handle->isValid() &&
  850. (handle->getType() == GFXSCT_Sampler ||
  851. handle->getType() == GFXSCT_SamplerCube ||
  852. handle->getType() == GFXSCT_SamplerCubeArray ||
  853. handle->getType() == GFXSCT_SamplerTextureArray))
  854. {
  855. // Set sampler number on our program.
  856. glUniform1i(handle->mDesc.bindPoint, handle->mDesc.samplerReg);
  857. // Set sampler in constant buffer so it does not get unset later.
  858. dMemcpy(mGlobalConstBuffer + handle->mDesc.offset, &handle->mDesc.samplerReg, handle->getSize());
  859. }
  860. }
  861. //instancing
  862. if (!mInstancingFormat)
  863. return;
  864. U32 offset = 0;
  865. for (U32 i = 0; i < mInstancingFormat->getElementCount(); i++)
  866. {
  867. const GFXVertexElement& element = mInstancingFormat->getElement(i);
  868. String constName = String::ToString("$%s", element.getSemantic().c_str());
  869. HandleMap::Iterator handle = mHandles.find(constName);
  870. if (handle != mHandles.end())
  871. {
  872. AssertFatal(0, "");
  873. }
  874. else
  875. {
  876. GFXShaderConstDesc desc;
  877. desc.name = constName;
  878. desc.arraySize = 1;
  879. switch (element.getType())
  880. {
  881. case GFXDeclType_Float4:
  882. desc.constType = GFXSCT_Float4;
  883. break;
  884. default:
  885. desc.constType = GFXSCT_Float;
  886. break;
  887. }
  888. GFXGLShaderConstHandle* h = new GFXGLShaderConstHandle(this, desc);
  889. h->mInstancingConstant = true;
  890. h->mDesc.offset = offset;
  891. h->mUBOUniform = false;
  892. mHandles[constName] = h;
  893. offset += element.getSizeInBytes();
  894. ++i;
  895. // If this is a matrix we will have 2 or 3 more of these
  896. // semantics with the same name after it.
  897. for (; i < mInstancingFormat->getElementCount(); i++)
  898. {
  899. const GFXVertexElement& nextElement = mInstancingFormat->getElement(i);
  900. if (nextElement.getSemantic() != element.getSemantic())
  901. {
  902. i--;
  903. break;
  904. }
  905. ++desc.arraySize;
  906. if (desc.arraySize == 4 && desc.constType == GFXSCT_Float4)
  907. {
  908. desc.arraySize = 1;
  909. desc.constType = GFXSCT_Float4x4;
  910. }
  911. offset += nextElement.getSizeInBytes();
  912. }
  913. }
  914. }
  915. }
  916. GFXShaderConstHandle* GFXGLShader::getShaderConstHandle(const String& name)
  917. {
  918. HandleMap::Iterator i = mHandles.find(name);
  919. if (i != mHandles.end())
  920. return i->value;
  921. else
  922. {
  923. GFXGLShaderConstHandle* handle = new GFXGLShaderConstHandle(this);
  924. handle->setValid(false);
  925. mHandles[name] = handle;
  926. return handle;
  927. }
  928. }
  929. GFXShaderConstHandle* GFXGLShader::findShaderConstHandle(const String& name)
  930. {
  931. HandleMap::Iterator i = mHandles.find(name);
  932. if (i != mHandles.end())
  933. return i->value;
  934. else
  935. {
  936. return NULL;
  937. }
  938. }
  939. void GFXGLShader::setConstantsFromBuffer(U8* buffer)
  940. {
  941. for (HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); ++i)
  942. {
  943. GFXGLShaderConstHandle* handle = i->value;
  944. AssertFatal(handle, "GFXGLShader::setConstantsFromBuffer - Null handle");
  945. // skip ubo uniforms.
  946. if (handle->mUBOUniform || !handle->isValid())
  947. continue;
  948. if (handle->mInstancingConstant)
  949. continue;
  950. // Don't set if the value has not be changed.
  951. if (dMemcmp(mGlobalConstBuffer + handle->mDesc.offset, buffer + handle->mDesc.offset, handle->getSize()) == 0)
  952. continue;
  953. // Copy new value into our const buffer and set in GL.
  954. dMemcpy(mGlobalConstBuffer + handle->mDesc.offset, buffer + handle->mDesc.offset, handle->getSize());
  955. switch (handle->mDesc.constType)
  956. {
  957. case GFXSCT_Float:
  958. glUniform1fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  959. break;
  960. case GFXSCT_Float2:
  961. glUniform2fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  962. break;
  963. case GFXSCT_Float3:
  964. glUniform3fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  965. break;
  966. case GFXSCT_Float4:
  967. glUniform4fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  968. break;
  969. case GFXSCT_Sampler:
  970. case GFXSCT_SamplerCube:
  971. case GFXSCT_SamplerCubeArray:
  972. case GFXSCT_SamplerTextureArray:
  973. // Set sampler number on our program.
  974. glUniform1i(handle->mDesc.bindPoint, handle->mDesc.samplerReg);
  975. break;
  976. case GFXSCT_Bool:
  977. case GFXSCT_Int:
  978. glUniform1iv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLint*)(mGlobalConstBuffer + handle->mDesc.offset));
  979. break;
  980. case GFXSCT_Bool2:
  981. case GFXSCT_Int2:
  982. glUniform2iv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLint*)(mGlobalConstBuffer + handle->mDesc.offset));
  983. break;
  984. case GFXSCT_Bool3:
  985. case GFXSCT_Int3:
  986. glUniform3iv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLint*)(mGlobalConstBuffer + handle->mDesc.offset));
  987. break;
  988. case GFXSCT_Bool4:
  989. case GFXSCT_Int4:
  990. glUniform4iv(handle->mDesc.bindPoint, handle->mDesc.arraySize, (GLint*)(mGlobalConstBuffer + handle->mDesc.offset));
  991. break;
  992. case GFXSCT_Float2x2:
  993. glUniformMatrix2fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, true, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  994. break;
  995. case GFXSCT_Float3x3:
  996. glUniformMatrix3fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, true, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  997. break;
  998. case GFXSCT_Float4x3:
  999. // NOTE: To save a transpose here we could store the matrix transposed (i.e. column major) in the constant buffer.
  1000. // See _mesa_uniform_matrix in the mesa source for the correct transpose algorithm for a 4x3 matrix.
  1001. glUniformMatrix4x3fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, true, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  1002. break;
  1003. case GFXSCT_Float4x4:
  1004. glUniformMatrix4fv(handle->mDesc.bindPoint, handle->mDesc.arraySize, true, (GLfloat*)(mGlobalConstBuffer + handle->mDesc.offset));
  1005. break;
  1006. default:
  1007. AssertFatal(0, "");
  1008. break;
  1009. }
  1010. }
  1011. }
  1012. GFXShaderConstBufferRef GFXGLShader::allocConstBuffer()
  1013. {
  1014. GFXGLShaderConstBuffer* buffer = new GFXGLShaderConstBuffer(this);
  1015. for (BufferMap::Iterator i = mBuffers.begin(); i != mBuffers.end(); ++i)
  1016. {
  1017. // add our buffer descriptions to the full const buffer.
  1018. buffer->addBuffer(i->value);
  1019. }
  1020. buffer->registerResourceWithDevice(getOwningDevice());
  1021. mActiveBuffers.push_back(buffer);
  1022. return buffer;
  1023. }
  1024. void GFXGLShader::useProgram()
  1025. {
  1026. glUseProgram(mProgram);
  1027. }
  1028. void GFXGLShader::zombify()
  1029. {
  1030. clearShaders();
  1031. }
  1032. char* GFXGLShader::_handleIncludes(const Torque::Path& path, FileStream* s)
  1033. {
  1034. // TODO: The #line pragma on GLSL takes something called a
  1035. // "source-string-number" which it then never explains.
  1036. //
  1037. // Until i resolve this mystery i disabled this.
  1038. //
  1039. //String linePragma = String::ToString( "#line 1 \r\n");
  1040. //U32 linePragmaLen = linePragma.length();
  1041. U32 shaderLen = s->getStreamSize();
  1042. char* buffer = (char*)dMalloc(shaderLen + 1);
  1043. //dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
  1044. s->read(shaderLen, buffer);
  1045. buffer[shaderLen] = 0;
  1046. char* p = dStrstr(buffer, "#include");
  1047. while (p)
  1048. {
  1049. char* q = p;
  1050. p += 8;
  1051. if (dIsspace(*p))
  1052. {
  1053. U32 n = 0;
  1054. while (dIsspace(*p)) ++p;
  1055. AssertFatal(*p == '"', "Bad #include directive");
  1056. ++p;
  1057. static char includeFile[256];
  1058. while (*p != '"')
  1059. {
  1060. AssertFatal(*p != 0, "Bad #include directive");
  1061. includeFile[n++] = *p++;
  1062. AssertFatal(n < sizeof(includeFile), "#include directive too long");
  1063. }
  1064. ++p;
  1065. includeFile[n] = 0;
  1066. // First try it as a local file.
  1067. Torque::Path includePath = Torque::Path::Join(path.getPath(), '/', includeFile);
  1068. includePath = Torque::Path::CompressPath(includePath);
  1069. FileStream includeStream;
  1070. if (!includeStream.open(includePath, Torque::FS::File::Read))
  1071. {
  1072. // Try again assuming the path is absolute
  1073. // and/or relative.
  1074. includePath = String(includeFile);
  1075. includePath = Torque::Path::CompressPath(includePath);
  1076. if (!includeStream.open(includePath, Torque::FS::File::Read))
  1077. {
  1078. AssertISV(false, avar("failed to open include '%s'.", includePath.getFullPath().c_str()));
  1079. if (smLogErrors)
  1080. Con::errorf("GFXGLShader::_handleIncludes - Failed to open include '%s'.",
  1081. includePath.getFullPath().c_str());
  1082. // Fail... don't return the buffer.
  1083. dFree(buffer);
  1084. return NULL;
  1085. }
  1086. }
  1087. char* includedText = _handleIncludes(includePath, &includeStream);
  1088. // If a sub-include fails... cleanup and return.
  1089. if (!includedText)
  1090. {
  1091. dFree(buffer);
  1092. return NULL;
  1093. }
  1094. // TODO: Disabled till this is fixed correctly.
  1095. //
  1096. // Count the number of lines in the file
  1097. // before the include.
  1098. /*
  1099. U32 includeLine = 0;
  1100. {
  1101. char* nl = dStrstr( buffer, "\n" );
  1102. while ( nl )
  1103. {
  1104. includeLine++;
  1105. nl = dStrstr( nl, "\n" );
  1106. if(nl) ++nl;
  1107. }
  1108. }
  1109. */
  1110. String manip(buffer);
  1111. manip.erase(q - buffer, p - q);
  1112. String sItx(includedText);
  1113. // TODO: Disabled till this is fixed correctly.
  1114. //
  1115. // Add a new line pragma to restore the proper
  1116. // file and line number after the include.
  1117. //sItx += String::ToString( "\r\n#line %d \r\n", includeLine );
  1118. dFree(includedText);
  1119. manip.insert(q - buffer, sItx);
  1120. char* manipBuf = dStrdup(manip.c_str());
  1121. p = manipBuf + (q - buffer);
  1122. dFree(buffer);
  1123. buffer = manipBuf;
  1124. }
  1125. p = dStrstr(p, "#include");
  1126. }
  1127. return buffer;
  1128. }
  1129. bool GFXGLShader::_loadShaderFromStream(GLuint shader,
  1130. const Torque::Path& path,
  1131. FileStream* s,
  1132. const Vector<GFXShaderMacro>& macros)
  1133. {
  1134. Vector<char*> buffers;
  1135. Vector<U32> lengths;
  1136. // The GLSL version declaration must go first!
  1137. const char* versionDecl = "#version 330\n";
  1138. buffers.push_back(dStrdup(versionDecl));
  1139. lengths.push_back(dStrlen(versionDecl));
  1140. //Required extensions. These are already checked when creating the GFX adapter, if we make it this far it's supported
  1141. const char* cubeArrayExt = "#extension GL_ARB_texture_cube_map_array : enable\n";
  1142. buffers.push_back(dStrdup(cubeArrayExt));
  1143. lengths.push_back(dStrlen(cubeArrayExt));
  1144. const char* gpuShader5Ext = "#extension GL_ARB_gpu_shader5 : enable\n";
  1145. buffers.push_back(dStrdup(gpuShader5Ext));
  1146. lengths.push_back(dStrlen(gpuShader5Ext));
  1147. const char* newLine = "\r\n";
  1148. buffers.push_back(dStrdup(newLine));
  1149. lengths.push_back(dStrlen(newLine));
  1150. // Now add all the macros.
  1151. for (U32 i = 0; i < macros.size(); i++)
  1152. {
  1153. if (macros[i].name.isEmpty()) // TODO OPENGL
  1154. continue;
  1155. String define = String::ToString("#define %s %s\n", macros[i].name.c_str(), macros[i].value.c_str());
  1156. buffers.push_back(dStrdup(define.c_str()));
  1157. lengths.push_back(define.length());
  1158. }
  1159. // Now finally add the shader source.
  1160. U32 shaderLen = s->getStreamSize();
  1161. char* buffer = _handleIncludes(path, s);
  1162. if (!buffer)
  1163. return false;
  1164. buffers.push_back(buffer);
  1165. lengths.push_back(shaderLen);
  1166. glShaderSource(shader, buffers.size(), (const GLchar**)const_cast<const char**>(buffers.address()), NULL);
  1167. #if defined(TORQUE_DEBUG) && defined(TORQUE_DEBUG_GFX)
  1168. FileStream stream;
  1169. if (!stream.open(path.getFullPath() + "_DEBUG", Torque::FS::File::Write))
  1170. {
  1171. AssertISV(false, avar("GFXGLShader::initShader - failed to write debug shader '%s'.", path.getFullPath().c_str()));
  1172. }
  1173. for (int i = 0; i < buffers.size(); ++i)
  1174. stream.writeText(buffers[i]);
  1175. #endif
  1176. // Cleanup the shader source buffer.
  1177. for (U32 i = 0; i < buffers.size(); i++)
  1178. dFree(buffers[i]);
  1179. glCompileShader(shader);
  1180. return true;
  1181. }
  1182. bool GFXGLShader::initShader(const Torque::Path& file,
  1183. GFXShaderStage stage,
  1184. const Vector<GFXShaderMacro>& macros)
  1185. {
  1186. PROFILE_SCOPE(GFXGLShader_CompileShader);
  1187. GLuint activeShader = 0;
  1188. switch (stage)
  1189. {
  1190. case VERTEX_SHADER:
  1191. activeShader = glCreateShader(GL_VERTEX_SHADER);
  1192. mVertexShader = activeShader;
  1193. break;
  1194. case PIXEL_SHADER:
  1195. activeShader = glCreateShader(GL_FRAGMENT_SHADER);
  1196. mPixelShader = activeShader;
  1197. break;
  1198. case GEOMETRY_SHADER:
  1199. activeShader = glCreateShader(GL_GEOMETRY_SHADER);
  1200. mGeometryShader = activeShader;
  1201. break;
  1202. case DOMAIN_SHADER:
  1203. break;
  1204. case HULL_SHADER:
  1205. break;
  1206. case COMPUTE_SHADER:
  1207. break;
  1208. default:
  1209. break;
  1210. }
  1211. glAttachShader(mProgram, activeShader);
  1212. // Ok it's not in the shader gen manager, so ask Torque for it
  1213. FileStream stream;
  1214. if (!stream.open(file, Torque::FS::File::Read))
  1215. {
  1216. AssertISV(false, avar("GFXGLShader::initShader - failed to open shader '%s'.", file.getFullPath().c_str()));
  1217. if (smLogErrors)
  1218. Con::errorf("GFXGLShader::initShader - Failed to open shader file '%s'.",
  1219. file.getFullPath().c_str());
  1220. return false;
  1221. }
  1222. if (!_loadShaderFromStream(activeShader, file, &stream, macros))
  1223. {
  1224. if (smLogErrors)
  1225. Con::errorf("GFXGLShader::initShader - unable to load shader from stream: '%s'.", file.getFullPath().c_str());
  1226. return false;
  1227. }
  1228. GLint compile;
  1229. glGetShaderiv(activeShader, GL_COMPILE_STATUS, &compile);
  1230. // Dump the info log to the console
  1231. U32 logLength = 0;
  1232. glGetShaderiv(activeShader, GL_INFO_LOG_LENGTH, (GLint*)&logLength);
  1233. if (logLength)
  1234. {
  1235. FrameAllocatorMarker fam;
  1236. char* log = (char*)fam.alloc(logLength);
  1237. glGetShaderInfoLog(activeShader, logLength, NULL, log);
  1238. if (compile == GL_FALSE)
  1239. {
  1240. if (smLogErrors)
  1241. {
  1242. Con::errorf("GFXGLShader::initShader - Error compiling shader!");
  1243. Con::errorf("Program %s: %s", file.getFullPath().c_str(), log);
  1244. }
  1245. }
  1246. else if (smLogWarnings)
  1247. Con::warnf("Program %s: %s", file.getFullPath().c_str(), log);
  1248. }
  1249. return compile != GL_FALSE;
  1250. }
  1251. /// Returns our list of shader constants, the material can get this and just set the constants it knows about
  1252. const Vector<GFXShaderConstDesc>& GFXGLShader::getShaderConstDesc() const
  1253. {
  1254. PROFILE_SCOPE(GFXGLShader_GetShaderConstants);
  1255. return mShaderConsts;
  1256. }
  1257. /// Returns the alignment value for constType
  1258. U32 GFXGLShader::getAlignmentValue(const GFXShaderConstType constType) const
  1259. {
  1260. // Alignment is the same thing as size for us.
  1261. return shaderConstTypeSize(constType);
  1262. }
  1263. const String GFXGLShader::describeSelf() const
  1264. {
  1265. String ret;
  1266. ret = String::ToString(" Program: %i", mProgram);
  1267. ret += String::ToString(" Vertex Path: %s", mVertexFile.getFullPath().c_str());
  1268. ret += String::ToString(" Pixel Path: %s", mPixelFile.getFullPath().c_str());
  1269. return ret;
  1270. }