CmGLRenderSystem.cpp 73 KB

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