CmGLRenderSystem.cpp 74 KB

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