CmGLRenderSystem.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in*
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.s
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLRenderSystem.h"
  25. #include "CmRenderSystem.h"
  26. #include "CmCamera.h"
  27. #include "CmGLTextureManager.h"
  28. #include "CmGLHardwareVertexBuffer.h"
  29. #include "CmGLHardwareIndexBuffer.h"
  30. #include "CmGLDefaultHardwareBufferManager.h"
  31. #include "CmGLUtil.h"
  32. #include "CmGLSLGpuProgram.h"
  33. #include "CmGLSLProgram.h"
  34. #include "CmGLGpuProgramManager.h"
  35. #include "CmException.h"
  36. #include "CmGLSLExtSupport.h"
  37. #include "CmGLHardwareOcclusionQuery.h"
  38. #include "CmGLContext.h"
  39. #include "CmAsyncOp.h"
  40. #include "CmBlendState.h"
  41. #include "CmRasterizerState.h"
  42. #include "CmDepthStencilState.h"
  43. #include "CmGLRenderTexture.h"
  44. #include "CmGLRenderWindowManager.h"
  45. #include "CmGpuParams.h"
  46. #include "CmDebug.h"
  47. #if CM_DEBUG_MODE
  48. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  49. #else
  50. #define THROW_IF_NOT_RENDER_THREAD
  51. #endif
  52. // Convenience macro from ARB_vertex_buffer_object spec
  53. #define VBO_BUFFER_OFFSET(i) ((char *)NULL + (i))
  54. #if CM_THREAD_SUPPORT != 1
  55. GLenum glewContextInit (CamelotEngine::GLSupport *glSupport);
  56. #endif
  57. namespace CamelotEngine
  58. {
  59. /************************************************************************/
  60. /* PUBLIC INTERFACE */
  61. /************************************************************************/
  62. GLRenderSystem::GLRenderSystem()
  63. : mDepthWrite(true),
  64. mGLSLProgramFactory(0),
  65. mCgProgramFactory(0),
  66. mActiveTextureUnit(0),
  67. mScissorTop(0), mScissorBottom(720), mScissorLeft(0), mScissorRight(1280),
  68. mStencilReadMask(0xFFFFFFFF),
  69. mStencilWriteMask(0xFFFFFFFF),
  70. mStencilCompareFront(CMPF_ALWAYS_PASS),
  71. mStencilCompareBack(CMPF_ALWAYS_PASS),
  72. mStencilRefValue(0),
  73. mFragmentTexOffset(0),
  74. mVertexTexOffset(0),
  75. mGeometryTexOffset(0),
  76. mTextureTypes(nullptr)
  77. {
  78. // Get our GLSupport
  79. mGLSupport = CamelotEngine::getGLSupport();
  80. mViewMatrix = Matrix4::IDENTITY;
  81. mColourWrite[0] = mColourWrite[1] = mColourWrite[2] = mColourWrite[3] = true;
  82. mActiveRenderTarget = 0;
  83. mCurrentContext = 0;
  84. mMainContext = 0;
  85. mGLInitialised = false;
  86. mMinFilter = FO_LINEAR;
  87. mMipFilter = FO_POINT;
  88. mCurrentVertexProgram = nullptr;
  89. mCurrentGeometryProgram = nullptr;
  90. mCurrentFragmentProgram = nullptr;
  91. mCurrentHullProgram = nullptr;
  92. mCurrentDomainProgram = nullptr;
  93. }
  94. GLRenderSystem::~GLRenderSystem()
  95. {
  96. destroy_internal();
  97. // Destroy render windows
  98. for (auto i = mRenderTargets.begin(); i != mRenderTargets.end(); ++i)
  99. {
  100. delete *i;
  101. }
  102. mRenderTargets.clear();
  103. if(mGLSupport)
  104. delete mGLSupport;
  105. }
  106. const String& GLRenderSystem::getName(void) const
  107. {
  108. static String strName("GLRenderSystem");
  109. return strName;
  110. }
  111. void GLRenderSystem::initialize_internal()
  112. {
  113. THROW_IF_NOT_RENDER_THREAD;
  114. mGLSupport->start();
  115. RenderWindowManager::startUp(new GLRenderWindowManager(this));
  116. RenderSystem::initialize_internal();
  117. }
  118. void GLRenderSystem::destroy_internal(void)
  119. {
  120. RenderSystem::destroy_internal();
  121. // Deleting the GLSL program factory
  122. if (mGLSLProgramFactory)
  123. {
  124. // Remove from manager safely
  125. HighLevelGpuProgramManager::instance().removeFactory(mGLSLProgramFactory);
  126. delete mGLSLProgramFactory;
  127. mGLSLProgramFactory = 0;
  128. }
  129. // Deleting Cg GLSL program factory
  130. if (mCgProgramFactory)
  131. {
  132. // Remove from manager safely
  133. HighLevelGpuProgramManager::instance().removeFactory(mCgProgramFactory);
  134. delete mCgProgramFactory;
  135. mCgProgramFactory = 0;
  136. }
  137. // Deleting the GPU program manager and hardware buffer manager. Has to be done before the mGLSupport->stop().
  138. GpuProgramManager::shutDown();
  139. HardwareBufferManager::shutDown();
  140. GLRTTManager::shutDown();
  141. // Delete extra threads contexts
  142. for (GLContextList::iterator i = mBackgroundContextList.begin();
  143. i != mBackgroundContextList.end(); ++i)
  144. {
  145. GLContext* pCurContext = *i;
  146. pCurContext->releaseContext();
  147. delete pCurContext;
  148. }
  149. mBackgroundContextList.clear();
  150. mGLSupport->stop();
  151. mStopRendering = true;
  152. TextureManager::shutDown();
  153. RenderWindowManager::shutDown();
  154. // There will be a new initial window and so forth, thus any call to test
  155. // some params will access an invalid pointer, so it is best to reset
  156. // the whole state.
  157. mGLInitialised = 0;
  158. }
  159. //---------------------------------------------------------------------
  160. void GLRenderSystem::bindGpuProgram(GpuProgramHandle prg)
  161. {
  162. THROW_IF_NOT_RENDER_THREAD;
  163. GpuProgram* bindingPrg = prg->getBindingDelegate_internal();
  164. GLSLGpuProgram* glprg = static_cast<GLSLGpuProgram*>(bindingPrg);
  165. // Unbind previous gpu program first.
  166. //
  167. // Note:
  168. // 1. Even if both previous and current are the same object, we can't
  169. // bypass re-bind completely since the object itself maybe modified.
  170. // But we can bypass unbind based on the assumption that object
  171. // internally GL program type shouldn't be changed after it has
  172. // been created. The behavior of bind to a GL program type twice
  173. // should be same as unbind and rebind that GL program type, even
  174. // for difference objects.
  175. // 2. We also assumed that the program's type (vertex or fragment) should
  176. // not be changed during it's in using. If not, the following switch
  177. // statement will confuse GL state completely, and we can't fix it
  178. // here. To fix this case, we must coding the program implementation
  179. // itself, if type is changing (during load/unload, etc), and it's inuse,
  180. // unbind and notify render system to correct for its state.
  181. //
  182. switch (glprg->getType())
  183. {
  184. case GPT_VERTEX_PROGRAM:
  185. if (mCurrentVertexProgram != glprg)
  186. {
  187. unbindGpuProgram(glprg->getType());
  188. mCurrentVertexProgram = glprg;
  189. }
  190. break;
  191. case GPT_FRAGMENT_PROGRAM:
  192. if (mCurrentFragmentProgram != glprg)
  193. {
  194. unbindGpuProgram(glprg->getType());
  195. mCurrentFragmentProgram = glprg;
  196. }
  197. break;
  198. case GPT_GEOMETRY_PROGRAM:
  199. if (mCurrentGeometryProgram != glprg)
  200. {
  201. unbindGpuProgram(glprg->getType());
  202. mCurrentGeometryProgram = glprg;
  203. }
  204. break;
  205. case GPT_DOMAIN_PROGRAM:
  206. if (mCurrentDomainProgram != glprg)
  207. {
  208. unbindGpuProgram(glprg->getType());
  209. mCurrentDomainProgram = glprg;
  210. }
  211. break;
  212. case GPT_HULL_PROGRAM:
  213. if (mCurrentHullProgram != glprg)
  214. {
  215. unbindGpuProgram(glprg->getType());
  216. mCurrentHullProgram = glprg;
  217. }
  218. break;
  219. }
  220. RenderSystem::bindGpuProgram(prg);
  221. }
  222. //---------------------------------------------------------------------
  223. void GLRenderSystem::unbindGpuProgram(GpuProgramType gptype)
  224. {
  225. THROW_IF_NOT_RENDER_THREAD;
  226. setActiveProgram(gptype, nullptr);
  227. RenderSystem::unbindGpuProgram(gptype);
  228. }
  229. //-----------------------------------------------------------------------------
  230. void GLRenderSystem::bindGpuParams(GpuProgramType gptype, GpuParamsPtr params)
  231. {
  232. THROW_IF_NOT_RENDER_THREAD;
  233. params->updateIfDirty();
  234. const GpuParamDesc& paramDesc = params->getParamDesc();
  235. GLSLGpuProgram* activeProgram = getActiveProgram(gptype);
  236. GLuint glProgram = activeProgram->getGLSLProgram()->getGLHandle();
  237. for(auto iter = paramDesc.textures.begin(); iter != paramDesc.textures.end(); ++iter)
  238. {
  239. TextureHandle texture = params->getTexture(iter->second.slot);
  240. if(!texture.isLoaded())
  241. setTexture(gptype, iter->second.slot, false, nullptr);
  242. else
  243. setTexture(gptype, iter->second.slot, true, texture.getInternalPtr());
  244. }
  245. UINT32 texUnit = 0;
  246. for(auto iter = paramDesc.samplers.begin(); iter != paramDesc.samplers.end(); ++iter)
  247. {
  248. SamplerStatePtr samplerState = params->getSamplerState(iter->second.slot);
  249. if(samplerState == nullptr)
  250. setSamplerState(gptype, iter->second.slot, SamplerState::getDefault());
  251. else
  252. setSamplerState(gptype, iter->second.slot, *samplerState);
  253. glProgramUniform1i(glProgram, iter->second.slot, getGLTextureUnit(gptype, texUnit));
  254. texUnit++;
  255. }
  256. for(auto iter = paramDesc.params.begin(); iter != paramDesc.params.end(); ++iter)
  257. {
  258. const GpuParamMemberDesc& paramDesc = iter->second;
  259. GpuParamBlockPtr paramBlock = params->getParamBlock(paramDesc.paramBlockSlot);
  260. if(paramDesc.paramBlockSlot == 0) // 0 means uniforms are not in a block
  261. {
  262. const UINT8* ptrData = paramBlock->getDataPtr(paramDesc.cpuMemOffset * sizeof(UINT32));
  263. switch(paramDesc.type)
  264. {
  265. case GCT_FLOAT1:
  266. glProgramUniform1fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLfloat*)ptrData);
  267. break;
  268. case GCT_FLOAT2:
  269. glProgramUniform2fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLfloat*)ptrData);
  270. break;
  271. case GCT_FLOAT3:
  272. glProgramUniform3fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLfloat*)ptrData);
  273. break;
  274. case GCT_FLOAT4:
  275. glProgramUniform4fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLfloat*)ptrData);
  276. break;
  277. case GCT_MATRIX_2X2:
  278. glProgramUniformMatrix2fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  279. GL_TRUE, (GLfloat*)ptrData);
  280. break;
  281. case GCT_MATRIX_2X3:
  282. glProgramUniformMatrix2x3fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  283. GL_TRUE, (GLfloat*)ptrData);
  284. break;
  285. case GCT_MATRIX_2X4:
  286. glProgramUniformMatrix2x4fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  287. GL_TRUE, (GLfloat*)ptrData);
  288. break;
  289. case GCT_MATRIX_3X2:
  290. glProgramUniformMatrix3x2fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  291. GL_TRUE, (GLfloat*)ptrData);
  292. break;
  293. case GCT_MATRIX_3X3:
  294. glProgramUniformMatrix3fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  295. GL_TRUE, (GLfloat*)ptrData);
  296. break;
  297. case GCT_MATRIX_3X4:
  298. glProgramUniformMatrix3x4fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  299. GL_TRUE, (GLfloat*)ptrData);
  300. break;
  301. case GCT_MATRIX_4X2:
  302. glProgramUniformMatrix4x2fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  303. GL_TRUE, (GLfloat*)ptrData);
  304. break;
  305. case GCT_MATRIX_4X3:
  306. glProgramUniformMatrix4x3fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  307. GL_TRUE, (GLfloat*)ptrData);
  308. break;
  309. case GCT_MATRIX_4X4:
  310. glProgramUniformMatrix4fv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize,
  311. GL_TRUE, (GLfloat*)ptrData);
  312. break;
  313. case GCT_INT1:
  314. glProgramUniform1iv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLint*)ptrData);
  315. break;
  316. case GCT_INT2:
  317. glProgramUniform2iv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLint*)ptrData);
  318. break;
  319. case GCT_INT3:
  320. glProgramUniform3iv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLint*)ptrData);
  321. break;
  322. case GCT_INT4:
  323. glProgramUniform4iv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLint*)ptrData);
  324. break;
  325. case GMT_BOOL:
  326. glProgramUniform1uiv(glProgram, paramDesc.gpuMemOffset, paramDesc.arraySize, (GLuint*)ptrData);
  327. break;
  328. case GCT_UNKNOWN:
  329. break;
  330. }
  331. }
  332. else
  333. {
  334. CM_EXCEPT(NotImplementedException, "Support for uniform blocks not implemented yet");
  335. }
  336. }
  337. }
  338. //-----------------------------------------------------------------------------
  339. void GLRenderSystem::setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &texPtr)
  340. {
  341. THROW_IF_NOT_RENDER_THREAD;
  342. unit = getGLTextureUnit(gptype, unit);
  343. GLTexturePtr tex = std::static_pointer_cast<GLTexture>(texPtr);
  344. GLenum lastTextureType = mTextureTypes[unit];
  345. if (!activateGLTextureUnit(unit))
  346. return;
  347. if (enabled && tex)
  348. {
  349. mTextureTypes[unit] = tex->getGLTextureTarget();
  350. glBindTexture(mTextureTypes[unit], tex->getGLID());
  351. }
  352. else
  353. {
  354. // TODO - This doesn't actually disable all textures set on this image unit, only the 2D ones
  355. // - If a non-2D sampler is used, the texture will still be displayed
  356. // bind zero texture
  357. glBindTexture(GL_TEXTURE_2D, 0);
  358. }
  359. activateGLTextureUnit(0);
  360. }
  361. //-----------------------------------------------------------------------
  362. void GLRenderSystem::setSamplerState(GpuProgramType gptype, UINT16 unit, const SamplerState& state)
  363. {
  364. THROW_IF_NOT_RENDER_THREAD;
  365. unit = getGLTextureUnit(gptype, unit);
  366. // Set texture layer filtering
  367. setTextureFiltering(unit, FT_MIN, state.getTextureFiltering(FT_MIN));
  368. setTextureFiltering(unit, FT_MAG, state.getTextureFiltering(FT_MAG));
  369. setTextureFiltering(unit, FT_MIP, state.getTextureFiltering(FT_MIP));
  370. // Set texture anisotropy
  371. setTextureAnisotropy(unit, state.getTextureAnisotropy());
  372. // Set mipmap biasing
  373. setTextureMipmapBias(unit, state.getTextureMipmapBias());
  374. // Texture addressing mode
  375. const UVWAddressingMode& uvw = state.getTextureAddressingMode();
  376. setTextureAddressingMode(unit, uvw);
  377. // Set border color
  378. setTextureBorderColor(unit, state.getBorderColor());
  379. }
  380. //-----------------------------------------------------------------------------
  381. void GLRenderSystem::setBlendState(const BlendState& blendState)
  382. {
  383. THROW_IF_NOT_RENDER_THREAD;
  384. // Alpha to coverage
  385. setAlphaToCoverage(blendState.getAlphaToCoverageEnabled());
  386. // Blend states
  387. // DirectX 9 doesn't allow us to specify blend state per render target, so we just use the first one.
  388. if(blendState.getBlendEnabled(0))
  389. {
  390. setSceneBlending(blendState.getSrcBlend(0), blendState.getDstBlend(0), blendState.getAlphaSrcBlend(0), blendState.getAlphaDstBlend(0)
  391. , blendState.getBlendOperation(0), blendState.getAlphaBlendOperation(0));
  392. }
  393. else
  394. {
  395. setSceneBlending(SBF_ONE, SBF_ZERO, SBO_ADD);
  396. }
  397. // Color write mask
  398. UINT8 writeMask = blendState.getRenderTargetWriteMask(0);
  399. setColorBufferWriteEnabled((writeMask & 0x1) != 0, (writeMask & 0x2) != 0, (writeMask & 0x4) != 0, (writeMask & 0x8) != 0);
  400. }
  401. //----------------------------------------------------------------------
  402. void GLRenderSystem::setRasterizerState(const RasterizerState& rasterizerState)
  403. {
  404. THROW_IF_NOT_RENDER_THREAD;
  405. setDepthBias((float)rasterizerState.getDepthBias(), rasterizerState.getSlopeScaledDepthBias());
  406. setCullingMode(rasterizerState.getCullMode());
  407. setPolygonMode(rasterizerState.getPolygonMode());
  408. setScissorTestEnable(rasterizerState.getScissorEnable());
  409. }
  410. //---------------------------------------------------------------------
  411. void GLRenderSystem::setDepthStencilState(const DepthStencilState& depthStencilState)
  412. {
  413. THROW_IF_NOT_RENDER_THREAD;
  414. // Set stencil buffer options
  415. setStencilCheckEnabled(depthStencilState.getStencilEnable());
  416. setStencilBufferOperations(depthStencilState.getStencilFrontFailOp(), depthStencilState.getStencilFrontZFailOp(), depthStencilState.getStencilFrontPassOp(), true);
  417. setStencilBufferFunc(depthStencilState.getStencilFrontCompFunc(), depthStencilState.getStencilReadMask(), true);
  418. setStencilBufferOperations(depthStencilState.getStencilBackFailOp(), depthStencilState.getStencilBackZFailOp(), depthStencilState.getStencilBackPassOp(), false);
  419. setStencilBufferFunc(depthStencilState.getStencilBackCompFunc(), depthStencilState.getStencilReadMask(), false);
  420. setStencilBufferWriteMask(depthStencilState.getStencilWriteMask());
  421. // Set depth buffer options
  422. setDepthBufferCheckEnabled(depthStencilState.getDepthReadEnable());
  423. setDepthBufferWriteEnabled(depthStencilState.getDepthWriteEnable());
  424. setDepthBufferFunction(depthStencilState.getDepthComparisonFunc());
  425. }
  426. //---------------------------------------------------------------------
  427. void GLRenderSystem::setStencilRefValue(UINT32 refValue)
  428. {
  429. THROW_IF_NOT_RENDER_THREAD;
  430. mStencilRefValue = refValue;
  431. glStencilFuncSeparate(GL_FRONT, convertCompareFunction(mStencilCompareFront), mStencilRefValue, mStencilReadMask);
  432. glStencilFuncSeparate(GL_BACK, convertCompareFunction(mStencilCompareBack), mStencilRefValue, mStencilReadMask);
  433. }
  434. //-----------------------------------------------------------------------------
  435. void GLRenderSystem::setViewport(const Viewport& vp)
  436. {
  437. THROW_IF_NOT_RENDER_THREAD;
  438. RenderTargetPtr target;
  439. target = vp.getTarget();
  440. setRenderTarget(target.get());
  441. mActiveViewport = vp;
  442. GLsizei x, y, w, h;
  443. // Calculate the "lower-left" corner of the viewport
  444. w = vp.getActualWidth();
  445. h = vp.getActualHeight();
  446. x = vp.getActualLeft();
  447. y = vp.getActualTop();
  448. if (!target->requiresTextureFlipping())
  449. {
  450. // Convert "upper-left" corner to "lower-left"
  451. y = target->getHeight() - h - y;
  452. }
  453. glViewport(x, y, w, h);
  454. // Configure the viewport clipping
  455. glScissor(x, y, w, h);
  456. }
  457. //---------------------------------------------------------------------
  458. void GLRenderSystem::setRenderTarget(RenderTarget *target)
  459. {
  460. THROW_IF_NOT_RENDER_THREAD;
  461. mActiveRenderTarget = target;
  462. // Switch context if different from current one
  463. GLContext *newContext = 0;
  464. target->getCustomAttribute("GLCONTEXT", &newContext);
  465. if(newContext && mCurrentContext != newContext)
  466. {
  467. switchContext(newContext);
  468. }
  469. GLFrameBufferObject *fbo = 0;
  470. target->getCustomAttribute("FBO", &fbo);
  471. if(fbo)
  472. fbo->bind();
  473. else
  474. // Old style context (window/pbuffer) or copying render texture
  475. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  476. if (GLEW_EXT_framebuffer_sRGB)
  477. {
  478. // Enable / disable sRGB states
  479. if (target->isHardwareGammaEnabled())
  480. {
  481. glEnable(GL_FRAMEBUFFER_SRGB_EXT);
  482. // Note: could test GL_FRAMEBUFFER_SRGB_CAPABLE_EXT here before
  483. // enabling, but GL spec says incapable surfaces ignore the setting
  484. // anyway. We test the capability to enable isHardwareGammaEnabled.
  485. }
  486. else
  487. {
  488. glDisable(GL_FRAMEBUFFER_SRGB_EXT);
  489. }
  490. }
  491. }
  492. //-----------------------------------------------------------------------------
  493. void GLRenderSystem::beginFrame(void)
  494. {
  495. THROW_IF_NOT_RENDER_THREAD;
  496. // Activate the viewport clipping
  497. glEnable(GL_SCISSOR_TEST);
  498. }
  499. //-----------------------------------------------------------------------------
  500. void GLRenderSystem::endFrame(void)
  501. {
  502. THROW_IF_NOT_RENDER_THREAD;
  503. // Deactivate the viewport clipping.
  504. glDisable(GL_SCISSOR_TEST);
  505. // unbind GPU programs at end of frame
  506. // this is mostly to avoid holding bound programs that might get deleted
  507. // outside via the resource manager
  508. unbindGpuProgram(GPT_VERTEX_PROGRAM);
  509. unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  510. }
  511. //---------------------------------------------------------------------
  512. void GLRenderSystem::setVertexDeclaration(VertexDeclarationPtr decl)
  513. {
  514. THROW_IF_NOT_RENDER_THREAD;
  515. }
  516. //---------------------------------------------------------------------
  517. void GLRenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
  518. {
  519. THROW_IF_NOT_RENDER_THREAD;
  520. }
  521. //---------------------------------------------------------------------
  522. void GLRenderSystem::render(const RenderOperation& op)
  523. {
  524. THROW_IF_NOT_RENDER_THREAD;
  525. // TODO - Generate pipeline and bind program
  526. // Call super class
  527. RenderSystem::render(op);
  528. void* pBufferData = 0;
  529. const VertexDeclaration::VertexElementList& decl = op.vertexData->vertexDeclaration->getElements();
  530. VertexDeclaration::VertexElementList::const_iterator elem, elemEnd;
  531. elemEnd = decl.end();
  532. vector<GLuint>::type attribsBound;
  533. for (elem = decl.begin(); elem != elemEnd; ++elem)
  534. {
  535. if (!op.vertexData->vertexBufferBinding->isBufferBound(elem->getSource()))
  536. continue; // skip unbound elements
  537. HardwareVertexBufferPtr vertexBuffer =
  538. op.vertexData->vertexBufferBinding->getBuffer(elem->getSource());
  539. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  540. {
  541. glBindBufferARB(GL_ARRAY_BUFFER_ARB,
  542. static_cast<const GLHardwareVertexBuffer*>(vertexBuffer.get())->getGLBufferId());
  543. pBufferData = VBO_BUFFER_OFFSET(elem->getOffset());
  544. }
  545. else
  546. {
  547. pBufferData = static_cast<const GLDefaultHardwareVertexBuffer*>(vertexBuffer.get())->getDataPtr(elem->getOffset());
  548. }
  549. if (op.vertexData->vertexStart)
  550. {
  551. pBufferData = static_cast<char*>(pBufferData) + op.vertexData->vertexStart * vertexBuffer->getVertexSize();
  552. }
  553. unsigned int i = 0;
  554. VertexElementSemantic sem = elem->getSemantic();
  555. // TODO - Vertex elem not set
  556. //bool isCustomAttrib = false;
  557. //if (mCurrentVertexProgram)
  558. // isCustomAttrib = mCurrentVertexProgram->isAttributeValid(sem, elem->getIndex());
  559. //// Custom attribute support
  560. //// tangents, binormals, blendweights etc always via this route
  561. //// builtins may be done this way too
  562. //if (isCustomAttrib)
  563. //{
  564. // GLint attrib = mCurrentVertexProgram->getAttributeIndex(sem, elem->getIndex());
  565. //unsigned short typeCount = VertexElement::getTypeCount(elem->getType());
  566. //GLboolean normalised = GL_FALSE;
  567. //switch(elem->getType())
  568. //{
  569. //case VET_COLOR:
  570. //case VET_COLOR_ABGR:
  571. //case VET_COLOR_ARGB:
  572. // // Because GL takes these as a sequence of single unsigned bytes, count needs to be 4
  573. // // VertexElement::getTypeCount treats them as 1 (RGBA)
  574. // // Also need to normalise the fixed-point data
  575. // typeCount = 4;
  576. // normalised = GL_TRUE;
  577. // break;
  578. //default:
  579. // break;
  580. //};
  581. // glVertexAttribPointerARB(
  582. // attrib,
  583. // typeCount,
  584. // GLHardwareBufferManager::getGLType(elem->getType()),
  585. // normalised,
  586. // static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  587. // pBufferData);
  588. // glEnableVertexAttribArrayARB(attrib);
  589. // attribsBound.push_back(attrib);
  590. //}
  591. }
  592. // Find the correct type to render
  593. GLint primType;
  594. //Use adjacency if there is a geometry program and it requested adjacency info
  595. bool useAdjacency = (mGeometryProgramBound && mCurrentGeometryProgram->isAdjacencyInfoRequired());
  596. switch (op.operationType)
  597. {
  598. case RenderOperation::OT_POINT_LIST:
  599. primType = GL_POINTS;
  600. break;
  601. case RenderOperation::OT_LINE_LIST:
  602. primType = useAdjacency ? GL_LINES_ADJACENCY_EXT : GL_LINES;
  603. break;
  604. case RenderOperation::OT_LINE_STRIP:
  605. primType = useAdjacency ? GL_LINE_STRIP_ADJACENCY_EXT : GL_LINE_STRIP;
  606. break;
  607. default:
  608. case RenderOperation::OT_TRIANGLE_LIST:
  609. primType = useAdjacency ? GL_TRIANGLES_ADJACENCY_EXT : GL_TRIANGLES;
  610. break;
  611. case RenderOperation::OT_TRIANGLE_STRIP:
  612. primType = useAdjacency ? GL_TRIANGLE_STRIP_ADJACENCY_EXT : GL_TRIANGLE_STRIP;
  613. break;
  614. case RenderOperation::OT_TRIANGLE_FAN:
  615. primType = GL_TRIANGLE_FAN;
  616. break;
  617. }
  618. if (op.useIndexes)
  619. {
  620. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  621. {
  622. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
  623. static_cast<GLHardwareIndexBuffer*>(
  624. op.indexData->indexBuffer.get())->getGLBufferId());
  625. pBufferData = VBO_BUFFER_OFFSET(
  626. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  627. }
  628. else
  629. {
  630. pBufferData = static_cast<GLDefaultHardwareIndexBuffer*>(
  631. op.indexData->indexBuffer.get())->getDataPtr(
  632. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  633. }
  634. GLenum indexType = (op.indexData->indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  635. glDrawElements(primType, op.indexData->indexCount, indexType, pBufferData);
  636. }
  637. else
  638. {
  639. glDrawArrays(primType, 0, op.vertexData->vertexCount);
  640. }
  641. // unbind any custom attributes
  642. for (vector<GLuint>::type::iterator ai = attribsBound.begin(); ai != attribsBound.end(); ++ai)
  643. {
  644. glDisableVertexAttribArrayARB(*ai);
  645. }
  646. glColor4f(1,1,1,1);
  647. }
  648. //---------------------------------------------------------------------
  649. void GLRenderSystem::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  650. {
  651. THROW_IF_NOT_RENDER_THREAD;
  652. mScissorTop = top;
  653. mScissorBottom = bottom;
  654. mScissorLeft = left;
  655. mScissorRight = right;
  656. }
  657. //---------------------------------------------------------------------
  658. void GLRenderSystem::clearFrameBuffer(unsigned int buffers,
  659. const Color& colour, float depth, unsigned short stencil)
  660. {
  661. THROW_IF_NOT_RENDER_THREAD;
  662. bool colourMask = !mColourWrite[0] || !mColourWrite[1]
  663. || !mColourWrite[2] || !mColourWrite[3];
  664. GLbitfield flags = 0;
  665. if (buffers & FBT_COLOUR)
  666. {
  667. flags |= GL_COLOR_BUFFER_BIT;
  668. // Enable buffer for writing if it isn't
  669. if (colourMask)
  670. {
  671. glColorMask(true, true, true, true);
  672. }
  673. glClearColor(colour.r, colour.g, colour.b, colour.a);
  674. }
  675. if (buffers & FBT_DEPTH)
  676. {
  677. flags |= GL_DEPTH_BUFFER_BIT;
  678. // Enable buffer for writing if it isn't
  679. if (!mDepthWrite)
  680. {
  681. glDepthMask( GL_TRUE );
  682. }
  683. glClearDepth(depth);
  684. }
  685. if (buffers & FBT_STENCIL)
  686. {
  687. flags |= GL_STENCIL_BUFFER_BIT;
  688. // Enable buffer for writing if it isn't
  689. glStencilMask(0xFFFFFFFF);
  690. glClearStencil(stencil);
  691. }
  692. // Should be enable scissor test due the clear region is
  693. // relied on scissor box bounds.
  694. GLboolean scissorTestEnabled = glIsEnabled(GL_SCISSOR_TEST);
  695. if (!scissorTestEnabled)
  696. {
  697. glEnable(GL_SCISSOR_TEST);
  698. }
  699. // Sets the scissor box as same as viewport
  700. GLint viewport[4] = { 0, 0, 0, 0 };
  701. GLint scissor[4] = { 0, 0, 0, 0 };
  702. glGetIntegerv(GL_VIEWPORT, viewport);
  703. glGetIntegerv(GL_SCISSOR_BOX, scissor);
  704. bool scissorBoxDifference =
  705. viewport[0] != scissor[0] || viewport[1] != scissor[1] ||
  706. viewport[2] != scissor[2] || viewport[3] != scissor[3];
  707. if (scissorBoxDifference)
  708. {
  709. glScissor(viewport[0], viewport[1], viewport[2], viewport[3]);
  710. }
  711. // Clear buffers
  712. glClear(flags);
  713. // Restore scissor box
  714. if (scissorBoxDifference)
  715. {
  716. glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
  717. }
  718. // Restore scissor test
  719. if (!scissorTestEnabled)
  720. {
  721. glDisable(GL_SCISSOR_TEST);
  722. }
  723. // Reset buffer write state
  724. if (!mDepthWrite && (buffers & FBT_DEPTH))
  725. {
  726. glDepthMask( GL_FALSE );
  727. }
  728. if (colourMask && (buffers & FBT_COLOUR))
  729. {
  730. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  731. }
  732. if (buffers & FBT_STENCIL)
  733. {
  734. glStencilMask(mStencilWriteMask);
  735. }
  736. }
  737. /************************************************************************/
  738. /* PRIVATE */
  739. /************************************************************************/
  740. //-----------------------------------------------------------------------------
  741. void GLRenderSystem::setTextureAddressingMode(UINT16 stage, const UVWAddressingMode& uvw)
  742. {
  743. if (!activateGLTextureUnit(stage))
  744. return;
  745. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_S,
  746. getTextureAddressingMode(uvw.u));
  747. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_T,
  748. getTextureAddressingMode(uvw.v));
  749. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_R,
  750. getTextureAddressingMode(uvw.w));
  751. activateGLTextureUnit(0);
  752. }
  753. //-----------------------------------------------------------------------------
  754. void GLRenderSystem::setTextureBorderColor(UINT16 stage, const Color& colour)
  755. {
  756. GLfloat border[4] = { colour.r, colour.g, colour.b, colour.a };
  757. if (activateGLTextureUnit(stage))
  758. {
  759. glTexParameterfv( mTextureTypes[stage], GL_TEXTURE_BORDER_COLOR, border);
  760. activateGLTextureUnit(0);
  761. }
  762. }
  763. //-----------------------------------------------------------------------------
  764. void GLRenderSystem::setTextureMipmapBias(UINT16 stage, float bias)
  765. {
  766. if (mCurrentCapabilities->hasCapability(RSC_MIPMAP_LOD_BIAS))
  767. {
  768. if (activateGLTextureUnit(stage))
  769. {
  770. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
  771. activateGLTextureUnit(0);
  772. }
  773. }
  774. }
  775. void GLRenderSystem::setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op )
  776. {
  777. GLint sourceBlend = getBlendMode(sourceFactor);
  778. GLint destBlend = getBlendMode(destFactor);
  779. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO)
  780. {
  781. glDisable(GL_BLEND);
  782. }
  783. else
  784. {
  785. glEnable(GL_BLEND);
  786. glBlendFunc(sourceBlend, destBlend);
  787. }
  788. GLint func = GL_FUNC_ADD;
  789. switch(op)
  790. {
  791. case SBO_ADD:
  792. func = GL_FUNC_ADD;
  793. break;
  794. case SBO_SUBTRACT:
  795. func = GL_FUNC_SUBTRACT;
  796. break;
  797. case SBO_REVERSE_SUBTRACT:
  798. func = GL_FUNC_REVERSE_SUBTRACT;
  799. break;
  800. case SBO_MIN:
  801. func = GL_MIN;
  802. break;
  803. case SBO_MAX:
  804. func = GL_MAX;
  805. break;
  806. }
  807. if(GLEW_VERSION_1_4 || GLEW_ARB_imaging)
  808. {
  809. glBlendEquation(func);
  810. }
  811. else if(GLEW_EXT_blend_minmax && (func == GL_MIN || func == GL_MAX))
  812. {
  813. glBlendEquationEXT(func);
  814. }
  815. }
  816. //-----------------------------------------------------------------------------
  817. void GLRenderSystem::setSceneBlending(
  818. SceneBlendFactor sourceFactor, SceneBlendFactor destFactor,
  819. SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha,
  820. SceneBlendOperation op, SceneBlendOperation alphaOp )
  821. {
  822. GLint sourceBlend = getBlendMode(sourceFactor);
  823. GLint destBlend = getBlendMode(destFactor);
  824. GLint sourceBlendAlpha = getBlendMode(sourceFactorAlpha);
  825. GLint destBlendAlpha = getBlendMode(destFactorAlpha);
  826. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO &&
  827. sourceFactorAlpha == SBF_ONE && destFactorAlpha == SBF_ZERO)
  828. {
  829. glDisable(GL_BLEND);
  830. }
  831. else
  832. {
  833. glEnable(GL_BLEND);
  834. glBlendFuncSeparate(sourceBlend, destBlend, sourceBlendAlpha, destBlendAlpha);
  835. }
  836. GLint func = GL_FUNC_ADD, alphaFunc = GL_FUNC_ADD;
  837. switch(op)
  838. {
  839. case SBO_ADD:
  840. func = GL_FUNC_ADD;
  841. break;
  842. case SBO_SUBTRACT:
  843. func = GL_FUNC_SUBTRACT;
  844. break;
  845. case SBO_REVERSE_SUBTRACT:
  846. func = GL_FUNC_REVERSE_SUBTRACT;
  847. break;
  848. case SBO_MIN:
  849. func = GL_MIN;
  850. break;
  851. case SBO_MAX:
  852. func = GL_MAX;
  853. break;
  854. }
  855. switch(alphaOp)
  856. {
  857. case SBO_ADD:
  858. alphaFunc = GL_FUNC_ADD;
  859. break;
  860. case SBO_SUBTRACT:
  861. alphaFunc = GL_FUNC_SUBTRACT;
  862. break;
  863. case SBO_REVERSE_SUBTRACT:
  864. alphaFunc = GL_FUNC_REVERSE_SUBTRACT;
  865. break;
  866. case SBO_MIN:
  867. alphaFunc = GL_MIN;
  868. break;
  869. case SBO_MAX:
  870. alphaFunc = GL_MAX;
  871. break;
  872. }
  873. if(GLEW_VERSION_2_0) {
  874. glBlendEquationSeparate(func, alphaFunc);
  875. }
  876. else if(GLEW_EXT_blend_equation_separate) {
  877. glBlendEquationSeparateEXT(func, alphaFunc);
  878. }
  879. }
  880. //-----------------------------------------------------------------------------
  881. void GLRenderSystem::setAlphaTest(CompareFunction func, unsigned char value)
  882. {
  883. if(func == CMPF_ALWAYS_PASS)
  884. {
  885. glDisable(GL_ALPHA_TEST);
  886. }
  887. else
  888. {
  889. glEnable(GL_ALPHA_TEST);
  890. glAlphaFunc(convertCompareFunction(func), value / 255.0f);
  891. }
  892. }
  893. //-----------------------------------------------------------------------------
  894. void GLRenderSystem::setAlphaToCoverage(bool enable)
  895. {
  896. static bool lasta2c = false;
  897. if (enable != lasta2c && getCapabilities()->hasCapability(RSC_ALPHA_TO_COVERAGE))
  898. {
  899. if (enable)
  900. glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  901. else
  902. glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  903. lasta2c = enable;
  904. }
  905. }
  906. //---------------------------------------------------------------------
  907. void GLRenderSystem::setScissorTestEnable(bool enable)
  908. {
  909. // If request texture flipping, use "upper-left", otherwise use "lower-left"
  910. bool flipping = mActiveRenderTarget->requiresTextureFlipping();
  911. // GL measures from the bottom, not the top
  912. UINT32 targetHeight = mActiveRenderTarget->getHeight();
  913. // Calculate the "lower-left" corner of the viewport
  914. GLsizei x = 0, y = 0, w = 0, h = 0;
  915. if (enable)
  916. {
  917. glEnable(GL_SCISSOR_TEST);
  918. // GL uses width / height rather than right / bottom
  919. x = mScissorLeft;
  920. if (flipping)
  921. y = mScissorTop;
  922. else
  923. y = targetHeight - mScissorBottom;
  924. w = mScissorRight - mScissorLeft;
  925. h = mScissorBottom - mScissorTop;
  926. glScissor(x, y, w, h);
  927. }
  928. else
  929. {
  930. glDisable(GL_SCISSOR_TEST);
  931. // GL requires you to reset the scissor when disabling
  932. w = mActiveViewport.getActualWidth();
  933. h = mActiveViewport.getActualHeight();
  934. x = mActiveViewport.getActualLeft();
  935. if (flipping)
  936. y = mActiveViewport.getActualTop();
  937. else
  938. y = targetHeight - mActiveViewport.getActualTop() - h;
  939. glScissor(x, y, w, h);
  940. }
  941. }
  942. //-----------------------------------------------------------------------------
  943. void GLRenderSystem::setCullingMode(CullingMode mode)
  944. {
  945. mCullingMode = mode;
  946. GLenum cullMode;
  947. switch( mode )
  948. {
  949. case CULL_NONE:
  950. glDisable( GL_CULL_FACE );
  951. return;
  952. default:
  953. case CULL_CLOCKWISE:
  954. if (mActiveRenderTarget &&
  955. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  956. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  957. {
  958. cullMode = GL_FRONT;
  959. }
  960. else
  961. {
  962. cullMode = GL_BACK;
  963. }
  964. break;
  965. case CULL_ANTICLOCKWISE:
  966. if (mActiveRenderTarget &&
  967. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  968. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  969. {
  970. cullMode = GL_BACK;
  971. }
  972. else
  973. {
  974. cullMode = GL_FRONT;
  975. }
  976. break;
  977. }
  978. glEnable( GL_CULL_FACE );
  979. glCullFace( cullMode );
  980. }
  981. //-----------------------------------------------------------------------------
  982. void GLRenderSystem::setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
  983. {
  984. setDepthBufferCheckEnabled(depthTest);
  985. setDepthBufferWriteEnabled(depthWrite);
  986. setDepthBufferFunction(depthFunction);
  987. }
  988. //-----------------------------------------------------------------------------
  989. void GLRenderSystem::setDepthBufferCheckEnabled(bool enabled)
  990. {
  991. if (enabled)
  992. {
  993. glClearDepth(1.0f);
  994. glEnable(GL_DEPTH_TEST);
  995. }
  996. else
  997. {
  998. glDisable(GL_DEPTH_TEST);
  999. }
  1000. }
  1001. //-----------------------------------------------------------------------------
  1002. void GLRenderSystem::setDepthBufferWriteEnabled(bool enabled)
  1003. {
  1004. GLboolean flag = enabled ? GL_TRUE : GL_FALSE;
  1005. glDepthMask( flag );
  1006. // Store for reference in _beginFrame
  1007. mDepthWrite = enabled;
  1008. }
  1009. //-----------------------------------------------------------------------------
  1010. void GLRenderSystem::setDepthBufferFunction(CompareFunction func)
  1011. {
  1012. glDepthFunc(convertCompareFunction(func));
  1013. }
  1014. //-----------------------------------------------------------------------------
  1015. void GLRenderSystem::setDepthBias(float constantBias, float slopeScaleBias)
  1016. {
  1017. if (constantBias != 0 || slopeScaleBias != 0)
  1018. {
  1019. glEnable(GL_POLYGON_OFFSET_FILL);
  1020. glEnable(GL_POLYGON_OFFSET_POINT);
  1021. glEnable(GL_POLYGON_OFFSET_LINE);
  1022. glPolygonOffset(-slopeScaleBias, -constantBias);
  1023. }
  1024. else
  1025. {
  1026. glDisable(GL_POLYGON_OFFSET_FILL);
  1027. glDisable(GL_POLYGON_OFFSET_POINT);
  1028. glDisable(GL_POLYGON_OFFSET_LINE);
  1029. }
  1030. }
  1031. //-----------------------------------------------------------------------------
  1032. void GLRenderSystem::setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha)
  1033. {
  1034. glColorMask(red, green, blue, alpha);
  1035. // record this
  1036. mColourWrite[0] = red;
  1037. mColourWrite[1] = blue;
  1038. mColourWrite[2] = green;
  1039. mColourWrite[3] = alpha;
  1040. }
  1041. //---------------------------------------------------------------------
  1042. void GLRenderSystem::setPolygonMode(PolygonMode level)
  1043. {
  1044. GLenum glmode;
  1045. switch(level)
  1046. {
  1047. case PM_POINTS:
  1048. glmode = GL_POINT;
  1049. break;
  1050. case PM_WIREFRAME:
  1051. glmode = GL_LINE;
  1052. break;
  1053. default:
  1054. case PM_SOLID:
  1055. glmode = GL_FILL;
  1056. break;
  1057. }
  1058. glPolygonMode(GL_FRONT_AND_BACK, glmode);
  1059. }
  1060. //---------------------------------------------------------------------
  1061. void GLRenderSystem::setStencilCheckEnabled(bool enabled)
  1062. {
  1063. if (enabled)
  1064. glEnable(GL_STENCIL_TEST);
  1065. else
  1066. glDisable(GL_STENCIL_TEST);
  1067. }
  1068. //---------------------------------------------------------------------
  1069. void GLRenderSystem::setStencilBufferOperations(StencilOperation stencilFailOp,
  1070. StencilOperation depthFailOp, StencilOperation passOp, bool front)
  1071. {
  1072. if (front)
  1073. {
  1074. glStencilOpSeparate(GL_FRONT,
  1075. convertStencilOp(stencilFailOp, mInvertVertexWinding),
  1076. convertStencilOp(depthFailOp, mInvertVertexWinding),
  1077. convertStencilOp(passOp, mInvertVertexWinding));
  1078. }
  1079. else
  1080. {
  1081. glStencilOpSeparate(GL_BACK,
  1082. convertStencilOp(stencilFailOp, !mInvertVertexWinding),
  1083. convertStencilOp(depthFailOp, !mInvertVertexWinding),
  1084. convertStencilOp(passOp, !mInvertVertexWinding));
  1085. }
  1086. }
  1087. //---------------------------------------------------------------------
  1088. void GLRenderSystem::setStencilBufferFunc(CompareFunction func, UINT32 mask, bool front)
  1089. {
  1090. mStencilReadMask = mask;
  1091. if(front)
  1092. {
  1093. mStencilCompareFront = func;
  1094. glStencilFuncSeparate(GL_FRONT, convertCompareFunction(mStencilCompareFront), mStencilRefValue, mStencilReadMask);
  1095. }
  1096. else
  1097. {
  1098. mStencilCompareBack = func;
  1099. glStencilFuncSeparate(GL_BACK, convertCompareFunction(mStencilCompareBack), mStencilRefValue, mStencilReadMask);
  1100. }
  1101. }
  1102. //---------------------------------------------------------------------
  1103. void GLRenderSystem::setStencilBufferWriteMask(UINT32 mask)
  1104. {
  1105. mStencilWriteMask = mask;
  1106. glStencilMask(mask);
  1107. }
  1108. //---------------------------------------------------------------------
  1109. void GLRenderSystem::setTextureFiltering(UINT16 unit,
  1110. FilterType ftype, FilterOptions fo)
  1111. {
  1112. if (!activateGLTextureUnit(unit))
  1113. return;
  1114. switch(ftype)
  1115. {
  1116. case FT_MIN:
  1117. mMinFilter = fo;
  1118. // Combine with existing mip filter
  1119. glTexParameteri(
  1120. mTextureTypes[unit],
  1121. GL_TEXTURE_MIN_FILTER,
  1122. getCombinedMinMipFilter());
  1123. break;
  1124. case FT_MAG:
  1125. switch (fo)
  1126. {
  1127. case FO_ANISOTROPIC: // GL treats linear and aniso the same
  1128. case FO_LINEAR:
  1129. glTexParameteri(
  1130. mTextureTypes[unit],
  1131. GL_TEXTURE_MAG_FILTER,
  1132. GL_LINEAR);
  1133. break;
  1134. case FO_POINT:
  1135. case FO_NONE:
  1136. glTexParameteri(
  1137. mTextureTypes[unit],
  1138. GL_TEXTURE_MAG_FILTER,
  1139. GL_NEAREST);
  1140. break;
  1141. }
  1142. break;
  1143. case FT_MIP:
  1144. mMipFilter = fo;
  1145. // Combine with existing min filter
  1146. glTexParameteri(
  1147. mTextureTypes[unit],
  1148. GL_TEXTURE_MIN_FILTER,
  1149. getCombinedMinMipFilter());
  1150. break;
  1151. }
  1152. activateGLTextureUnit(0);
  1153. }
  1154. //---------------------------------------------------------------------
  1155. void GLRenderSystem::setTextureAnisotropy(UINT16 unit, unsigned int maxAnisotropy)
  1156. {
  1157. if (!mCurrentCapabilities->hasCapability(RSC_ANISOTROPY))
  1158. return;
  1159. if (!activateGLTextureUnit(unit))
  1160. return;
  1161. GLfloat largest_supported_anisotropy = 0;
  1162. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest_supported_anisotropy);
  1163. if (maxAnisotropy > largest_supported_anisotropy)
  1164. maxAnisotropy = largest_supported_anisotropy ?
  1165. static_cast<UINT32>(largest_supported_anisotropy) : 1;
  1166. if (_getCurrentAnisotropy(unit) != maxAnisotropy)
  1167. glTexParameterf(mTextureTypes[unit], GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
  1168. activateGLTextureUnit(0);
  1169. }
  1170. //-----------------------------------------------------------------------------
  1171. void GLRenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
  1172. {
  1173. // A note on GL user clipping:
  1174. // When an ARB vertex program is enabled in GL, user clipping is completely
  1175. // disabled. There is no way around this, it's just turned off.
  1176. // When using GLSL, user clipping can work but you have to include a
  1177. // glClipVertex command in your vertex shader.
  1178. // Thus the planes set here may not actually be respected.
  1179. size_t i = 0;
  1180. size_t numClipPlanes;
  1181. GLdouble clipPlane[4];
  1182. // Save previous modelview
  1183. glMatrixMode(GL_MODELVIEW);
  1184. glPushMatrix();
  1185. // just load view matrix (identity world)
  1186. GLfloat mat[16];
  1187. makeGLMatrix(mat, mViewMatrix);
  1188. glLoadMatrixf(mat);
  1189. numClipPlanes = clipPlanes.size();
  1190. for (i = 0; i < numClipPlanes; ++i)
  1191. {
  1192. GLenum clipPlaneId = static_cast<GLenum>(GL_CLIP_PLANE0 + i);
  1193. const Plane& plane = clipPlanes[i];
  1194. if (i >= 6/*GL_MAX_CLIP_PLANES*/)
  1195. {
  1196. CM_EXCEPT(RenderingAPIException, "Unable to set clip plane");
  1197. }
  1198. clipPlane[0] = plane.normal.x;
  1199. clipPlane[1] = plane.normal.y;
  1200. clipPlane[2] = plane.normal.z;
  1201. clipPlane[3] = plane.d;
  1202. glClipPlane(clipPlaneId, clipPlane);
  1203. glEnable(clipPlaneId);
  1204. }
  1205. // disable remaining clip planes
  1206. for ( ; i < 6/*GL_MAX_CLIP_PLANES*/; ++i)
  1207. {
  1208. glDisable(static_cast<GLenum>(GL_CLIP_PLANE0 + i));
  1209. }
  1210. // restore matrices
  1211. glPopMatrix();
  1212. }
  1213. //---------------------------------------------------------------------
  1214. void GLRenderSystem::oneTimeContextInitialization()
  1215. {
  1216. if (GLEW_VERSION_1_2)
  1217. {
  1218. // Set nicer lighting model -- d3d9 has this by default
  1219. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
  1220. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  1221. }
  1222. if (GLEW_VERSION_1_4)
  1223. {
  1224. glEnable(GL_COLOR_SUM);
  1225. glDisable(GL_DITHER);
  1226. }
  1227. // Check for FSAA
  1228. // Enable the extension if it was enabled by the GLSupport
  1229. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  1230. {
  1231. int fsaa_active = false;
  1232. glGetIntegerv(GL_SAMPLE_BUFFERS_ARB,(GLint*)&fsaa_active);
  1233. if(fsaa_active)
  1234. {
  1235. glEnable(GL_MULTISAMPLE_ARB);
  1236. }
  1237. }
  1238. }
  1239. //---------------------------------------------------------------------
  1240. void GLRenderSystem::switchContext(GLContext *context)
  1241. {
  1242. // Unbind GPU programs and rebind to new context later, because
  1243. // scene manager treat render system as ONE 'context' ONLY, and it
  1244. // cached the GPU programs using state.
  1245. unbindGpuProgram(GPT_VERTEX_PROGRAM);
  1246. unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  1247. unbindGpuProgram(GPT_GEOMETRY_PROGRAM);
  1248. unbindGpuProgram(GPT_HULL_PROGRAM);
  1249. unbindGpuProgram(GPT_DOMAIN_PROGRAM);
  1250. // It's ready for switching
  1251. if (mCurrentContext)
  1252. mCurrentContext->endCurrent();
  1253. mCurrentContext = context;
  1254. mCurrentContext->setCurrent();
  1255. // Check if the context has already done one-time initialisation
  1256. if(!mCurrentContext->getInitialized())
  1257. {
  1258. oneTimeContextInitialization();
  1259. mCurrentContext->setInitialized();
  1260. }
  1261. // Must reset depth/colour write mask to according with user desired, otherwise,
  1262. // clearFrameBuffer would be wrong because the value we are recorded may be
  1263. // difference with the really state stored in GL context.
  1264. glDepthMask(mDepthWrite);
  1265. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  1266. glStencilMask(mStencilWriteMask);
  1267. }
  1268. //---------------------------------------------------------------------
  1269. void GLRenderSystem::registerContext(GLContext* context)
  1270. {
  1271. if (!mGLInitialised)
  1272. {
  1273. // set up glew and GLSupport
  1274. initialiseContext(context);
  1275. std::vector<CamelotEngine::String> tokens = StringUtil::split(mGLSupport->getGLVersion(), ".");
  1276. if (!tokens.empty())
  1277. {
  1278. mDriverVersion.major = parseInt(tokens[0]);
  1279. if (tokens.size() > 1)
  1280. mDriverVersion.minor = parseInt(tokens[1]);
  1281. if (tokens.size() > 2)
  1282. mDriverVersion.release = parseInt(tokens[2]);
  1283. }
  1284. mDriverVersion.build = 0;
  1285. // Initialise GL after the first window has been created
  1286. // TODO: fire this from emulation options, and don't duplicate float and Current capabilities
  1287. mCurrentCapabilities = createRenderSystemCapabilities();
  1288. initialiseFromRenderSystemCapabilities(mCurrentCapabilities);
  1289. // Initialise the main context
  1290. oneTimeContextInitialization();
  1291. if(mCurrentContext)
  1292. mCurrentContext->setInitialized();
  1293. }
  1294. }
  1295. //---------------------------------------------------------------------
  1296. void GLRenderSystem::unregisterContext(GLContext *context)
  1297. {
  1298. if(mCurrentContext == context) {
  1299. // Change the context to something else so that a valid context
  1300. // remains active. When this is the main context being unregistered,
  1301. // we set the main context to 0.
  1302. if(mCurrentContext != mMainContext) {
  1303. switchContext(mMainContext);
  1304. } else {
  1305. /// No contexts remain
  1306. mCurrentContext->endCurrent();
  1307. mCurrentContext = 0;
  1308. mMainContext = 0;
  1309. }
  1310. }
  1311. }
  1312. //---------------------------------------------------------------------
  1313. bool GLRenderSystem::activateGLTextureUnit(UINT16 unit)
  1314. {
  1315. if (mActiveTextureUnit != unit)
  1316. {
  1317. if (unit < getCapabilities()->getNumCombinedTextureUnits())
  1318. {
  1319. glActiveTexture(GL_TEXTURE0 + unit);
  1320. mActiveTextureUnit = unit;
  1321. return true;
  1322. }
  1323. else if (!unit)
  1324. {
  1325. // always ok to use the first unit
  1326. return true;
  1327. }
  1328. else
  1329. {
  1330. LOGWRN("Provided texture unit index is higher than OpenGL supports. Provided: " + toString(unit) +
  1331. ". Supported range: 0 .. " + toString(getCapabilities()->getNumCombinedTextureUnits() - 1));
  1332. return false;
  1333. }
  1334. }
  1335. else
  1336. {
  1337. return true;
  1338. }
  1339. }
  1340. //---------------------------------------------------------------------
  1341. GLfloat GLRenderSystem::_getCurrentAnisotropy(UINT16 unit)
  1342. {
  1343. GLfloat curAniso = 0;
  1344. glGetTexParameterfv(mTextureTypes[unit],
  1345. GL_TEXTURE_MAX_ANISOTROPY_EXT, &curAniso);
  1346. return curAniso ? curAniso : 1;
  1347. }
  1348. //---------------------------------------------------------------------
  1349. GLuint GLRenderSystem::getCombinedMinMipFilter(void) const
  1350. {
  1351. switch(mMinFilter)
  1352. {
  1353. case FO_ANISOTROPIC:
  1354. case FO_LINEAR:
  1355. switch(mMipFilter)
  1356. {
  1357. case FO_ANISOTROPIC:
  1358. case FO_LINEAR:
  1359. // linear min, linear mip
  1360. return GL_LINEAR_MIPMAP_LINEAR;
  1361. case FO_POINT:
  1362. // linear min, point mip
  1363. return GL_LINEAR_MIPMAP_NEAREST;
  1364. case FO_NONE:
  1365. // linear min, no mip
  1366. return GL_LINEAR;
  1367. }
  1368. break;
  1369. case FO_POINT:
  1370. case FO_NONE:
  1371. switch(mMipFilter)
  1372. {
  1373. case FO_ANISOTROPIC:
  1374. case FO_LINEAR:
  1375. // nearest min, linear mip
  1376. return GL_NEAREST_MIPMAP_LINEAR;
  1377. case FO_POINT:
  1378. // nearest min, point mip
  1379. return GL_NEAREST_MIPMAP_NEAREST;
  1380. case FO_NONE:
  1381. // nearest min, no mip
  1382. return GL_NEAREST;
  1383. }
  1384. break;
  1385. }
  1386. // should never get here
  1387. return 0;
  1388. }
  1389. //---------------------------------------------------------------------
  1390. GLint GLRenderSystem::convertStencilOp(StencilOperation op, bool invert) const
  1391. {
  1392. switch(op)
  1393. {
  1394. case SOP_KEEP:
  1395. return GL_KEEP;
  1396. case SOP_ZERO:
  1397. return GL_ZERO;
  1398. case SOP_REPLACE:
  1399. return GL_REPLACE;
  1400. case SOP_INCREMENT:
  1401. return invert ? GL_DECR : GL_INCR;
  1402. case SOP_DECREMENT:
  1403. return invert ? GL_INCR : GL_DECR;
  1404. case SOP_INCREMENT_WRAP:
  1405. return invert ? GL_DECR_WRAP_EXT : GL_INCR_WRAP_EXT;
  1406. case SOP_DECREMENT_WRAP:
  1407. return invert ? GL_INCR_WRAP_EXT : GL_DECR_WRAP_EXT;
  1408. case SOP_INVERT:
  1409. return GL_INVERT;
  1410. };
  1411. // to keep compiler happy
  1412. return SOP_KEEP;
  1413. }
  1414. //---------------------------------------------------------------------
  1415. GLint GLRenderSystem::convertCompareFunction(CompareFunction func) const
  1416. {
  1417. switch(func)
  1418. {
  1419. case CMPF_ALWAYS_FAIL:
  1420. return GL_NEVER;
  1421. case CMPF_ALWAYS_PASS:
  1422. return GL_ALWAYS;
  1423. case CMPF_LESS:
  1424. return GL_LESS;
  1425. case CMPF_LESS_EQUAL:
  1426. return GL_LEQUAL;
  1427. case CMPF_EQUAL:
  1428. return GL_EQUAL;
  1429. case CMPF_NOT_EQUAL:
  1430. return GL_NOTEQUAL;
  1431. case CMPF_GREATER_EQUAL:
  1432. return GL_GEQUAL;
  1433. case CMPF_GREATER:
  1434. return GL_GREATER;
  1435. };
  1436. // to keep compiler happy
  1437. return GL_ALWAYS;
  1438. }
  1439. //-----------------------------------------------------------------------------
  1440. String GLRenderSystem::getErrorDescription(long errCode) const
  1441. {
  1442. const GLubyte *errString = gluErrorString (errCode);
  1443. return (errString != 0) ? String((const char*) errString) : StringUtil::BLANK;
  1444. }
  1445. //-----------------------------------------------------------------------------
  1446. GLint GLRenderSystem::getBlendMode(SceneBlendFactor blendMode) const
  1447. {
  1448. switch(blendMode)
  1449. {
  1450. case SBF_ONE:
  1451. return GL_ONE;
  1452. case SBF_ZERO:
  1453. return GL_ZERO;
  1454. case SBF_DEST_COLOUR:
  1455. return GL_DST_COLOR;
  1456. case SBF_SOURCE_COLOUR:
  1457. return GL_SRC_COLOR;
  1458. case SBF_ONE_MINUS_DEST_COLOUR:
  1459. return GL_ONE_MINUS_DST_COLOR;
  1460. case SBF_ONE_MINUS_SOURCE_COLOUR:
  1461. return GL_ONE_MINUS_SRC_COLOR;
  1462. case SBF_DEST_ALPHA:
  1463. return GL_DST_ALPHA;
  1464. case SBF_SOURCE_ALPHA:
  1465. return GL_SRC_ALPHA;
  1466. case SBF_ONE_MINUS_DEST_ALPHA:
  1467. return GL_ONE_MINUS_DST_ALPHA;
  1468. case SBF_ONE_MINUS_SOURCE_ALPHA:
  1469. return GL_ONE_MINUS_SRC_ALPHA;
  1470. };
  1471. // to keep compiler happy
  1472. return GL_ONE;
  1473. }
  1474. //-----------------------------------------------------------------------------
  1475. GLint GLRenderSystem::getTextureAddressingMode(
  1476. TextureAddressingMode tam) const
  1477. {
  1478. switch(tam)
  1479. {
  1480. default:
  1481. case TAM_WRAP:
  1482. return GL_REPEAT;
  1483. case TAM_MIRROR:
  1484. return GL_MIRRORED_REPEAT;
  1485. case TAM_CLAMP:
  1486. return GL_CLAMP_TO_EDGE;
  1487. case TAM_BORDER:
  1488. return GL_CLAMP_TO_BORDER;
  1489. }
  1490. }
  1491. //-----------------------------------------------------------------------------
  1492. void GLRenderSystem::makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m)
  1493. {
  1494. size_t x = 0;
  1495. for (size_t i = 0; i < 4; i++)
  1496. {
  1497. for (size_t j = 0; j < 4; j++)
  1498. {
  1499. gl_matrix[x] = m[j][i];
  1500. x++;
  1501. }
  1502. }
  1503. }
  1504. //-----------------------------------------------------------------------
  1505. void GLRenderSystem::initialiseContext(GLContext* primary)
  1506. {
  1507. // Set main and current context
  1508. mMainContext = primary;
  1509. mCurrentContext = mMainContext;
  1510. // Set primary context as active
  1511. if(mCurrentContext)
  1512. mCurrentContext->setCurrent();
  1513. // Setup GLSupport
  1514. mGLSupport->initialiseExtensions();
  1515. // Get extension function pointers
  1516. #if CM_THREAD_SUPPORT != 1
  1517. glewContextInit(mGLSupport);
  1518. #endif
  1519. }
  1520. void GLRenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps)
  1521. {
  1522. if(caps->getRenderSystemName() != getName())
  1523. {
  1524. CM_EXCEPT(InvalidParametersException,
  1525. "Trying to initialize GLRenderSystem from RenderSystemCapabilities that do not support OpenGL");
  1526. }
  1527. if(caps->hasCapability(RSC_GL1_5_NOVBO))
  1528. {
  1529. // Assign ARB functions same to GL 1.5 version since
  1530. // interface identical
  1531. glBindBufferARB = glBindBuffer;
  1532. glBufferDataARB = glBufferData;
  1533. glBufferSubDataARB = glBufferSubData;
  1534. glDeleteBuffersARB = glDeleteBuffers;
  1535. glGenBuffersARB = glGenBuffers;
  1536. glGetBufferParameterivARB = glGetBufferParameteriv;
  1537. glGetBufferPointervARB = glGetBufferPointerv;
  1538. glGetBufferSubDataARB = glGetBufferSubData;
  1539. glIsBufferARB = glIsBuffer;
  1540. glMapBufferARB = glMapBuffer;
  1541. glUnmapBufferARB = glUnmapBuffer;
  1542. }
  1543. if(caps->hasCapability(RSC_VBO))
  1544. {
  1545. HardwareBufferManager::startUp(new GLHardwareBufferManager);
  1546. }
  1547. else
  1548. {
  1549. HardwareBufferManager::startUp(new GLDefaultHardwareBufferManager);
  1550. }
  1551. // GPU Program Manager setup
  1552. GpuProgramManager::startUp(new GLGpuProgramManager());
  1553. if(caps->isShaderProfileSupported("glsl"))
  1554. {
  1555. // NFZ - check for GLSL vertex and fragment shader support successful
  1556. mGLSLProgramFactory = new GLSLProgramFactory();
  1557. HighLevelGpuProgramManager::instance().addFactory(mGLSLProgramFactory);
  1558. }
  1559. if(caps->isShaderProfileSupported("cg"))
  1560. {
  1561. // NFZ - check for GLSL vertex and fragment shader support successful
  1562. mCgProgramFactory = new CgProgramFactory();
  1563. HighLevelGpuProgramManager::instance().addFactory(mCgProgramFactory);
  1564. }
  1565. if(caps->hasCapability(RSC_HWOCCLUSION))
  1566. {
  1567. if(caps->hasCapability(RSC_GL1_5_NOHWOCCLUSION))
  1568. {
  1569. // Assign ARB functions same to GL 1.5 version since
  1570. // interface identical
  1571. glBeginQueryARB = glBeginQuery;
  1572. glDeleteQueriesARB = glDeleteQueries;
  1573. glEndQueryARB = glEndQuery;
  1574. glGenQueriesARB = glGenQueries;
  1575. glGetQueryObjectivARB = glGetQueryObjectiv;
  1576. glGetQueryObjectuivARB = glGetQueryObjectuiv;
  1577. glGetQueryivARB = glGetQueryiv;
  1578. glIsQueryARB = glIsQuery;
  1579. }
  1580. }
  1581. // Check for framebuffer object extension
  1582. if(caps->hasCapability(RSC_FBO))
  1583. {
  1584. // Before GL version 2.0, we need to get one of the extensions
  1585. if(caps->hasCapability(RSC_FBO_ARB))
  1586. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersARB;
  1587. else if(caps->hasCapability(RSC_FBO_ATI))
  1588. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersATI;
  1589. if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
  1590. {
  1591. // Create FBO manager
  1592. GLRTTManager::startUp(new GLRTTManager());
  1593. }
  1594. }
  1595. else
  1596. {
  1597. CM_EXCEPT(RenderingAPIException, "GPU doesn't support frame buffer objects. OpenGL versions lower than 3.0 are not supported.");
  1598. }
  1599. mFragmentTexOffset = 0;
  1600. mVertexTexOffset = caps->getNumTextureUnits(GPT_FRAGMENT_PROGRAM);
  1601. mGeometryTexOffset = mVertexTexOffset + caps->getNumTextureUnits(GPT_VERTEX_PROGRAM);
  1602. UINT16 numCombinedTexUnits = caps->getNumCombinedTextureUnits();
  1603. if((mFragmentTexOffset + mVertexTexOffset + mGeometryTexOffset) > numCombinedTexUnits)
  1604. CM_EXCEPT(InternalErrorException, "Number of combined texture units less than the number of individual units!?");
  1605. mTextureTypes = new GLenum[numCombinedTexUnits];
  1606. for(UINT16 i = 0; i < numCombinedTexUnits; i++)
  1607. mTextureTypes[i] = 0;
  1608. /// Create the texture manager
  1609. TextureManager::startUp(new GLTextureManager(*mGLSupport));
  1610. mGLInitialised = true;
  1611. }
  1612. RenderSystemCapabilities* GLRenderSystem::createRenderSystemCapabilities() const
  1613. {
  1614. RenderSystemCapabilities* rsc = new RenderSystemCapabilities();
  1615. rsc->setCategoryRelevant(CAPS_CATEGORY_GL, true);
  1616. rsc->setDriverVersion(mDriverVersion);
  1617. const char* deviceName = (const char*)glGetString(GL_RENDERER);
  1618. const char* vendorName = (const char*)glGetString(GL_VENDOR);
  1619. rsc->setDeviceName(deviceName);
  1620. rsc->setRenderSystemName(getName());
  1621. // determine vendor
  1622. if (strstr(vendorName, "NVIDIA"))
  1623. rsc->setVendor(GPU_NVIDIA);
  1624. else if (strstr(vendorName, "ATI"))
  1625. rsc->setVendor(GPU_ATI);
  1626. else if (strstr(vendorName, "Intel"))
  1627. rsc->setVendor(GPU_INTEL);
  1628. else if (strstr(vendorName, "S3"))
  1629. rsc->setVendor(GPU_S3);
  1630. else if (strstr(vendorName, "Matrox"))
  1631. rsc->setVendor(GPU_MATROX);
  1632. else if (strstr(vendorName, "3DLabs"))
  1633. rsc->setVendor(GPU_3DLABS);
  1634. else if (strstr(vendorName, "SiS"))
  1635. rsc->setVendor(GPU_SIS);
  1636. else
  1637. rsc->setVendor(GPU_UNKNOWN);
  1638. // Supports fixed-function
  1639. rsc->setCapability(RSC_FIXED_FUNCTION);
  1640. // Check for hardware mipmapping support.
  1641. if(GLEW_VERSION_1_4 || GLEW_SGIS_generate_mipmap)
  1642. {
  1643. bool disableAutoMip = false;
  1644. #if CM_PLATFORM == CM_PLATFORM_APPLE || CM_PLATFORM == CM_PLATFORM_LINUX
  1645. // Apple & Linux ATI drivers have faults in hardware mipmap generation
  1646. if (rsc->getVendor() == GPU_ATI)
  1647. disableAutoMip = true;
  1648. #endif
  1649. // The Intel 915G frequently corrupts textures when using hardware mip generation
  1650. // I'm not currently sure how many generations of hardware this affects,
  1651. // so for now, be safe.
  1652. if (rsc->getVendor() == GPU_INTEL)
  1653. disableAutoMip = true;
  1654. // SiS chipsets also seem to have problems with this
  1655. if (rsc->getVendor() == GPU_SIS)
  1656. disableAutoMip = true;
  1657. if (!disableAutoMip)
  1658. rsc->setCapability(RSC_AUTOMIPMAP);
  1659. }
  1660. // Check for blending support
  1661. if(GLEW_VERSION_1_3 ||
  1662. GLEW_ARB_texture_env_combine ||
  1663. GLEW_EXT_texture_env_combine)
  1664. {
  1665. rsc->setCapability(RSC_BLENDING);
  1666. }
  1667. // Check for Anisotropy support
  1668. if(GLEW_EXT_texture_filter_anisotropic)
  1669. {
  1670. rsc->setCapability(RSC_ANISOTROPY);
  1671. }
  1672. // Check for DOT3 support
  1673. if(GLEW_VERSION_1_3 ||
  1674. GLEW_ARB_texture_env_dot3 ||
  1675. GLEW_EXT_texture_env_dot3)
  1676. {
  1677. rsc->setCapability(RSC_DOT3);
  1678. }
  1679. // Check for cube mapping
  1680. if(GLEW_VERSION_1_3 ||
  1681. GLEW_ARB_texture_cube_map ||
  1682. GLEW_EXT_texture_cube_map)
  1683. {
  1684. rsc->setCapability(RSC_CUBEMAPPING);
  1685. }
  1686. // Point sprites
  1687. if (GLEW_VERSION_2_0 || GLEW_ARB_point_sprite)
  1688. {
  1689. rsc->setCapability(RSC_POINT_SPRITES);
  1690. }
  1691. // Check for point parameters
  1692. if (GLEW_VERSION_1_4)
  1693. {
  1694. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
  1695. }
  1696. if (GLEW_ARB_point_parameters)
  1697. {
  1698. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB);
  1699. }
  1700. if (GLEW_EXT_point_parameters)
  1701. {
  1702. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT);
  1703. }
  1704. // Check for hardware stencil support and set bit depth
  1705. GLint stencil;
  1706. glGetIntegerv(GL_STENCIL_BITS,&stencil);
  1707. if(stencil)
  1708. {
  1709. rsc->setCapability(RSC_HWSTENCIL);
  1710. rsc->setStencilBufferBitDepth(stencil);
  1711. }
  1712. if(GLEW_VERSION_1_5 || GLEW_ARB_vertex_buffer_object)
  1713. {
  1714. if (!GLEW_ARB_vertex_buffer_object)
  1715. {
  1716. rsc->setCapability(RSC_GL1_5_NOVBO);
  1717. }
  1718. rsc->setCapability(RSC_VBO);
  1719. }
  1720. rsc->setCapability(RSC_VERTEX_PROGRAM);
  1721. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  1722. rsc->addShaderProfile("cg");
  1723. // NFZ - Check if GLSL is supported
  1724. if ( GLEW_VERSION_2_0 ||
  1725. (GLEW_ARB_shading_language_100 &&
  1726. GLEW_ARB_shader_objects &&
  1727. GLEW_ARB_fragment_shader &&
  1728. GLEW_ARB_vertex_shader) )
  1729. {
  1730. rsc->addShaderProfile("glsl");
  1731. }
  1732. // Check if geometry shaders are supported
  1733. if (GLEW_VERSION_2_0 &&
  1734. GLEW_EXT_geometry_shader4)
  1735. {
  1736. rsc->setCapability(RSC_GEOMETRY_PROGRAM);
  1737. rsc->addShaderProfile("nvgp4");
  1738. //Also add the CG profiles
  1739. rsc->addShaderProfile("gpu_gp");
  1740. rsc->addShaderProfile("gp4gp");
  1741. rsc->setGeometryProgramConstantBoolCount(0);
  1742. rsc->setGeometryProgramConstantIntCount(0);
  1743. GLint floatConstantCount = 0;
  1744. glGetIntegerv(GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_EXT, &floatConstantCount);
  1745. rsc->setGeometryProgramConstantFloatCount(floatConstantCount);
  1746. GLint maxOutputVertices;
  1747. glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&maxOutputVertices);
  1748. rsc->setGeometryProgramNumOutputVertices(maxOutputVertices);
  1749. }
  1750. //Check if render to vertex buffer (transform feedback in OpenGL)
  1751. if (GLEW_VERSION_2_0 &&
  1752. GLEW_NV_transform_feedback)
  1753. {
  1754. rsc->setCapability(RSC_HWRENDER_TO_VERTEX_BUFFER);
  1755. }
  1756. // Check for texture compression
  1757. if(GLEW_VERSION_1_3 || GLEW_ARB_texture_compression)
  1758. {
  1759. rsc->setCapability(RSC_TEXTURE_COMPRESSION);
  1760. // Check for dxt compression
  1761. if(GLEW_EXT_texture_compression_s3tc)
  1762. {
  1763. #if defined(__APPLE__) && defined(__PPC__)
  1764. // Apple on ATI & PPC has errors in DXT
  1765. if (mGLSupport->getGLVendor().find("ATI") == std::string::npos)
  1766. #endif
  1767. rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
  1768. }
  1769. // Check for vtc compression
  1770. if(GLEW_NV_texture_compression_vtc)
  1771. {
  1772. rsc->setCapability(RSC_TEXTURE_COMPRESSION_VTC);
  1773. }
  1774. }
  1775. // Scissor test is standard in GL 1.2 (is it emulated on some cards though?)
  1776. rsc->setCapability(RSC_SCISSOR_TEST);
  1777. // As are user clipping planes
  1778. rsc->setCapability(RSC_USER_CLIP_PLANES);
  1779. // 2-sided stencil?
  1780. if (GLEW_VERSION_2_0 || GLEW_EXT_stencil_two_side)
  1781. {
  1782. rsc->setCapability(RSC_TWO_SIDED_STENCIL);
  1783. }
  1784. // stencil wrapping?
  1785. if (GLEW_VERSION_1_4 || GLEW_EXT_stencil_wrap)
  1786. {
  1787. rsc->setCapability(RSC_STENCIL_WRAP);
  1788. }
  1789. // Check for hardware occlusion support
  1790. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  1791. {
  1792. // Some buggy driver claim that it is GL 1.5 compliant and
  1793. // not support ARB_occlusion_query
  1794. if (!GLEW_ARB_occlusion_query)
  1795. {
  1796. rsc->setCapability(RSC_GL1_5_NOHWOCCLUSION);
  1797. }
  1798. rsc->setCapability(RSC_HWOCCLUSION);
  1799. }
  1800. else if (GLEW_NV_occlusion_query)
  1801. {
  1802. // Support NV extension too for old hardware
  1803. rsc->setCapability(RSC_HWOCCLUSION);
  1804. }
  1805. // UBYTE4 always supported
  1806. rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
  1807. // Infinite far plane always supported
  1808. rsc->setCapability(RSC_INFINITE_FAR_PLANE);
  1809. // Check for non-power-of-2 texture support
  1810. if(GLEW_ARB_texture_non_power_of_two)
  1811. {
  1812. rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
  1813. }
  1814. // Check for Float textures
  1815. if(GLEW_ATI_texture_float || GLEW_ARB_texture_float)
  1816. {
  1817. rsc->setCapability(RSC_TEXTURE_FLOAT);
  1818. }
  1819. // 3D textures should always be supported
  1820. rsc->setCapability(RSC_TEXTURE_3D);
  1821. // Check for framebuffer object extension
  1822. if(GLEW_EXT_framebuffer_object)
  1823. {
  1824. // Probe number of draw buffers
  1825. // Only makes sense with FBO support, so probe here
  1826. if(GLEW_VERSION_2_0 ||
  1827. GLEW_ARB_draw_buffers ||
  1828. GLEW_ATI_draw_buffers)
  1829. {
  1830. GLint buffers;
  1831. glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &buffers);
  1832. rsc->setNumMultiRenderTargets(std::min<int>(buffers, (GLint)CM_MAX_MULTIPLE_RENDER_TARGETS));
  1833. rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  1834. if(!GLEW_VERSION_2_0)
  1835. {
  1836. // Before GL version 2.0, we need to get one of the extensions
  1837. if(GLEW_ARB_draw_buffers)
  1838. rsc->setCapability(RSC_FBO_ARB);
  1839. if(GLEW_ATI_draw_buffers)
  1840. rsc->setCapability(RSC_FBO_ATI);
  1841. }
  1842. // Set FBO flag for all 3 'subtypes'
  1843. rsc->setCapability(RSC_FBO);
  1844. }
  1845. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  1846. }
  1847. // Check GLSupport for PBuffer support
  1848. if(mGLSupport->supportsPBuffers())
  1849. {
  1850. // Use PBuffers
  1851. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  1852. rsc->setCapability(RSC_PBUFFER);
  1853. }
  1854. // Point size
  1855. float ps;
  1856. glGetFloatv(GL_POINT_SIZE_MAX, &ps);
  1857. rsc->setMaxPointSize(ps);
  1858. // Max number of fragment shader textures
  1859. GLint units;
  1860. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &units);
  1861. rsc->setNumTextureUnits(GPT_FRAGMENT_PROGRAM, static_cast<UINT16>(units));
  1862. // Max number of vertex shader textures
  1863. GLint vUnits;
  1864. glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &vUnits);
  1865. rsc->setNumTextureUnits(GPT_VERTEX_PROGRAM, static_cast<UINT16>(vUnits));
  1866. if (vUnits > 0)
  1867. {
  1868. rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
  1869. }
  1870. if (mGLSupport->checkExtension("ARB_geometry_shader4"))
  1871. {
  1872. GLint geomUnits;
  1873. glGetIntegerv(GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS, &geomUnits);
  1874. rsc->setNumTextureUnits(GPT_GEOMETRY_PROGRAM, static_cast<UINT16>(geomUnits));
  1875. }
  1876. if (mGLSupport->checkExtension("ARB_tessellation_shader"))
  1877. {
  1878. rsc->setCapability(RSC_TESSELLATION_PROGRAM);
  1879. }
  1880. if (mGLSupport->checkExtension("ARB_compute_shader")) // Enable once I include GL 4.3
  1881. {
  1882. //rsc->setCapability(RSC_COMPUTE_PROGRAM);
  1883. //GLint computeUnits;
  1884. //glGetIntegerv(GL_MAX_COMPUTE_TEXTURE_IMAGE_UNITS, &computeUnits);
  1885. //rsc->setNumTextureUnits(GPT_COMPUTE_PROGRAM, static_cast<UINT16>(computeUnits));
  1886. }
  1887. GLint combinedTexUnits;
  1888. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &combinedTexUnits);
  1889. rsc->setNumCombinedTextureUnits(static_cast<UINT16>(combinedTexUnits));
  1890. // Mipmap LOD biasing
  1891. rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
  1892. // Alpha to coverage?
  1893. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  1894. {
  1895. // Alpha to coverage always 'supported' when MSAA is available
  1896. // although card may ignore it if it doesn't specifically support A2C
  1897. rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
  1898. }
  1899. // Advanced blending operations
  1900. if(GLEW_VERSION_2_0)
  1901. {
  1902. rsc->setCapability(RSC_ADVANCED_BLEND_OPERATIONS);
  1903. }
  1904. return rsc;
  1905. }
  1906. //---------------------------------------------------------------------
  1907. UINT32 GLRenderSystem::getGLTextureUnit(GpuProgramType gptype, UINT32 unit)
  1908. {
  1909. if(gptype != GPT_VERTEX_PROGRAM && gptype != GPT_FRAGMENT_PROGRAM && gptype != GPT_GEOMETRY_PROGRAM)
  1910. {
  1911. CM_EXCEPT(InvalidParametersException, "OpenGL cannot assign textures to this gpu program type: " + toString(gptype));
  1912. }
  1913. UINT32 numSupportedUnits = mCurrentCapabilities->getNumTextureUnits(gptype);
  1914. if(unit < 0 || unit >= numSupportedUnits)
  1915. {
  1916. CM_EXCEPT(InvalidParametersException, "Invalid texture unit index for the provided stage. Unit index: " + toString(unit) + ". Stage: " +
  1917. toString(gptype) + ". Supported range is 0 .. " + toString(numSupportedUnits - 1));
  1918. }
  1919. switch(gptype)
  1920. {
  1921. case GPT_FRAGMENT_PROGRAM:
  1922. return mFragmentTexOffset + unit;
  1923. case GPT_VERTEX_PROGRAM:
  1924. return mVertexTexOffset + unit;
  1925. case GPT_GEOMETRY_PROGRAM:
  1926. return mGeometryTexOffset + unit;
  1927. default:
  1928. CM_EXCEPT(InternalErrorException, "Invalid program type: " + toString(gptype));
  1929. }
  1930. }
  1931. void GLRenderSystem::setActiveProgram(GpuProgramType gptype, GLSLGpuProgram* program)
  1932. {
  1933. switch (gptype)
  1934. {
  1935. case GPT_VERTEX_PROGRAM:
  1936. mCurrentVertexProgram = program;
  1937. break;
  1938. case GPT_FRAGMENT_PROGRAM:
  1939. mCurrentFragmentProgram = program;
  1940. break;
  1941. case GPT_GEOMETRY_PROGRAM:
  1942. mCurrentGeometryProgram = program;
  1943. break;
  1944. case GPT_DOMAIN_PROGRAM:
  1945. mCurrentDomainProgram = program;
  1946. break;
  1947. case GPT_HULL_PROGRAM:
  1948. mCurrentHullProgram = program;
  1949. break;
  1950. }
  1951. }
  1952. GLSLGpuProgram* GLRenderSystem::getActiveProgram(GpuProgramType gptype) const
  1953. {
  1954. switch (gptype)
  1955. {
  1956. case GPT_VERTEX_PROGRAM:
  1957. return mCurrentVertexProgram;
  1958. break;
  1959. case GPT_FRAGMENT_PROGRAM:
  1960. return mCurrentFragmentProgram;
  1961. break;
  1962. case GPT_GEOMETRY_PROGRAM:
  1963. return mCurrentGeometryProgram;
  1964. break;
  1965. case GPT_DOMAIN_PROGRAM:
  1966. return mCurrentDomainProgram;
  1967. break;
  1968. case GPT_HULL_PROGRAM:
  1969. return mCurrentHullProgram;
  1970. break;
  1971. default:
  1972. CM_EXCEPT(InvalidParametersException, "Insupported gpu program type: " + toString(gptype));
  1973. }
  1974. }
  1975. /************************************************************************/
  1976. /* UTILITY */
  1977. /************************************************************************/
  1978. float GLRenderSystem::getMinimumDepthInputValue(void)
  1979. {
  1980. // Range [-1.0f, 1.0f]
  1981. return -1.0f;
  1982. }
  1983. //---------------------------------------------------------------------
  1984. float GLRenderSystem::getMaximumDepthInputValue(void)
  1985. {
  1986. // Range [-1.0f, 1.0f]
  1987. return 1.0f;
  1988. }
  1989. //---------------------------------------------------------------------
  1990. float GLRenderSystem::getHorizontalTexelOffset(void)
  1991. {
  1992. // No offset in GL
  1993. return 0.0f;
  1994. }
  1995. //---------------------------------------------------------------------
  1996. float GLRenderSystem::getVerticalTexelOffset(void)
  1997. {
  1998. // No offset in GL
  1999. return 0.0f;
  2000. }
  2001. VertexElementType GLRenderSystem::getColorVertexElementType(void) const
  2002. {
  2003. return VET_COLOR_ABGR;
  2004. }
  2005. //---------------------------------------------------------------------
  2006. void GLRenderSystem::convertProjectionMatrix(const Matrix4& matrix,
  2007. Matrix4& dest, bool forGpuProgram)
  2008. {
  2009. // no any conversion request for OpenGL
  2010. dest = matrix;
  2011. }
  2012. }
  2013. #undef THROW_IF_NOT_RENDER_THREAD