CmGLRenderSystem.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  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_internal();
  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_internal() );
  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(0));
  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. // Unbind frame buffer object
  439. if(mActiveRenderTarget)
  440. GLRTTManager::instancePtr()->unbind(mActiveRenderTarget);
  441. mActiveRenderTarget = target;
  442. // Switch context if different from current one
  443. GLContext *newContext = 0;
  444. target->getCustomAttribute_internal("GLCONTEXT", &newContext);
  445. if(newContext && mCurrentContext != newContext)
  446. {
  447. switchContext(newContext);
  448. }
  449. // Bind frame buffer object
  450. GLRTTManager::instancePtr()->bind(target);
  451. if (GLEW_EXT_framebuffer_sRGB)
  452. {
  453. // Enable / disable sRGB states
  454. if (target->isHardwareGammaEnabled())
  455. {
  456. glEnable(GL_FRAMEBUFFER_SRGB_EXT);
  457. // Note: could test GL_FRAMEBUFFER_SRGB_CAPABLE_EXT here before
  458. // enabling, but GL spec says incapable surfaces ignore the setting
  459. // anyway. We test the capability to enable isHardwareGammaEnabled.
  460. }
  461. else
  462. {
  463. glDisable(GL_FRAMEBUFFER_SRGB_EXT);
  464. }
  465. }
  466. }
  467. //-----------------------------------------------------------------------------
  468. void GLRenderSystem::beginFrame(void)
  469. {
  470. THROW_IF_NOT_RENDER_THREAD;
  471. // Activate the viewport clipping
  472. glEnable(GL_SCISSOR_TEST);
  473. }
  474. //-----------------------------------------------------------------------------
  475. void GLRenderSystem::endFrame(void)
  476. {
  477. THROW_IF_NOT_RENDER_THREAD;
  478. // Deactivate the viewport clipping.
  479. glDisable(GL_SCISSOR_TEST);
  480. // unbind GPU programs at end of frame
  481. // this is mostly to avoid holding bound programs that might get deleted
  482. // outside via the resource manager
  483. unbindGpuProgram(GPT_VERTEX_PROGRAM);
  484. unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  485. }
  486. //---------------------------------------------------------------------
  487. void GLRenderSystem::setVertexDeclaration(VertexDeclarationPtr decl)
  488. {
  489. THROW_IF_NOT_RENDER_THREAD;
  490. }
  491. //---------------------------------------------------------------------
  492. void GLRenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
  493. {
  494. THROW_IF_NOT_RENDER_THREAD;
  495. }
  496. //---------------------------------------------------------------------
  497. void GLRenderSystem::render(const RenderOperation& op)
  498. {
  499. THROW_IF_NOT_RENDER_THREAD;
  500. // Call super class
  501. RenderSystem::render(op);
  502. void* pBufferData = 0;
  503. bool multitexturing = (getCapabilities()->getNumTextureUnits() > 1);
  504. const VertexDeclaration::VertexElementList& decl =
  505. op.vertexData->vertexDeclaration->getElements();
  506. VertexDeclaration::VertexElementList::const_iterator elem, elemEnd;
  507. elemEnd = decl.end();
  508. vector<GLuint>::type attribsBound;
  509. for (elem = decl.begin(); elem != elemEnd; ++elem)
  510. {
  511. if (!op.vertexData->vertexBufferBinding->isBufferBound(elem->getSource()))
  512. continue; // skip unbound elements
  513. HardwareVertexBufferPtr vertexBuffer =
  514. op.vertexData->vertexBufferBinding->getBuffer(elem->getSource());
  515. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  516. {
  517. glBindBufferARB(GL_ARRAY_BUFFER_ARB,
  518. static_cast<const GLHardwareVertexBuffer*>(vertexBuffer.get())->getGLBufferId());
  519. pBufferData = VBO_BUFFER_OFFSET(elem->getOffset());
  520. }
  521. else
  522. {
  523. pBufferData = static_cast<const GLDefaultHardwareVertexBuffer*>(vertexBuffer.get())->getDataPtr(elem->getOffset());
  524. }
  525. if (op.vertexData->vertexStart)
  526. {
  527. pBufferData = static_cast<char*>(pBufferData) + op.vertexData->vertexStart * vertexBuffer->getVertexSize();
  528. }
  529. unsigned int i = 0;
  530. VertexElementSemantic sem = elem->getSemantic();
  531. bool isCustomAttrib = false;
  532. if (mCurrentVertexProgram)
  533. isCustomAttrib = mCurrentVertexProgram->isAttributeValid(sem, elem->getIndex());
  534. // Custom attribute support
  535. // tangents, binormals, blendweights etc always via this route
  536. // builtins may be done this way too
  537. if (isCustomAttrib)
  538. {
  539. GLint attrib = mCurrentVertexProgram->getAttributeIndex(sem, elem->getIndex());
  540. unsigned short typeCount = VertexElement::getTypeCount(elem->getType());
  541. GLboolean normalised = GL_FALSE;
  542. switch(elem->getType())
  543. {
  544. case VET_COLOUR:
  545. case VET_COLOUR_ABGR:
  546. case VET_COLOUR_ARGB:
  547. // Because GL takes these as a sequence of single unsigned bytes, count needs to be 4
  548. // VertexElement::getTypeCount treats them as 1 (RGBA)
  549. // Also need to normalise the fixed-point data
  550. typeCount = 4;
  551. normalised = GL_TRUE;
  552. break;
  553. default:
  554. break;
  555. };
  556. glVertexAttribPointerARB(
  557. attrib,
  558. typeCount,
  559. GLHardwareBufferManager::getGLType(elem->getType()),
  560. normalised,
  561. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  562. pBufferData);
  563. glEnableVertexAttribArrayARB(attrib);
  564. attribsBound.push_back(attrib);
  565. }
  566. else
  567. {
  568. // fixed-function & builtin attribute support
  569. switch(sem)
  570. {
  571. case VES_POSITION:
  572. glVertexPointer(VertexElement::getTypeCount(
  573. elem->getType()),
  574. GLHardwareBufferManager::getGLType(elem->getType()),
  575. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  576. pBufferData);
  577. glEnableClientState( GL_VERTEX_ARRAY );
  578. break;
  579. case VES_NORMAL:
  580. glNormalPointer(
  581. GLHardwareBufferManager::getGLType(elem->getType()),
  582. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  583. pBufferData);
  584. glEnableClientState( GL_NORMAL_ARRAY );
  585. break;
  586. case VES_DIFFUSE:
  587. glColorPointer(4,
  588. GLHardwareBufferManager::getGLType(elem->getType()),
  589. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  590. pBufferData);
  591. glEnableClientState( GL_COLOR_ARRAY );
  592. break;
  593. case VES_SPECULAR:
  594. if (GLEW_EXT_secondary_color)
  595. {
  596. glSecondaryColorPointerEXT(4,
  597. GLHardwareBufferManager::getGLType(elem->getType()),
  598. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  599. pBufferData);
  600. glEnableClientState( GL_SECONDARY_COLOR_ARRAY );
  601. }
  602. break;
  603. case VES_TEXTURE_COORDINATES:
  604. if (mCurrentVertexProgram)
  605. {
  606. // Programmable pipeline - direct UV assignment
  607. glClientActiveTextureARB(GL_TEXTURE0 + elem->getIndex());
  608. glTexCoordPointer(
  609. VertexElement::getTypeCount(elem->getType()),
  610. GLHardwareBufferManager::getGLType(elem->getType()),
  611. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  612. pBufferData);
  613. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  614. }
  615. else
  616. {
  617. // fixed function matching to units based on tex_coord_set
  618. for (i = 0; i < mDisabledTexUnitsFrom; i++)
  619. {
  620. // Only set this texture unit's texcoord pointer if it
  621. // is supposed to be using this element's index
  622. if (mTextureCoordIndex[i] == elem->getIndex() && i < mFixedFunctionTextureUnits)
  623. {
  624. if (multitexturing)
  625. glClientActiveTextureARB(GL_TEXTURE0 + i);
  626. glTexCoordPointer(
  627. VertexElement::getTypeCount(elem->getType()),
  628. GLHardwareBufferManager::getGLType(elem->getType()),
  629. static_cast<GLsizei>(vertexBuffer->getVertexSize()),
  630. pBufferData);
  631. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  632. }
  633. }
  634. }
  635. break;
  636. default:
  637. break;
  638. };
  639. } // isCustomAttrib
  640. }
  641. if (multitexturing)
  642. glClientActiveTextureARB(GL_TEXTURE0);
  643. // Find the correct type to render
  644. GLint primType;
  645. //Use adjacency if there is a geometry program and it requested adjacency info
  646. bool useAdjacency = (mGeometryProgramBound && mCurrentGeometryProgram->isAdjacencyInfoRequired());
  647. switch (op.operationType)
  648. {
  649. case RenderOperation::OT_POINT_LIST:
  650. primType = GL_POINTS;
  651. break;
  652. case RenderOperation::OT_LINE_LIST:
  653. primType = useAdjacency ? GL_LINES_ADJACENCY_EXT : GL_LINES;
  654. break;
  655. case RenderOperation::OT_LINE_STRIP:
  656. primType = useAdjacency ? GL_LINE_STRIP_ADJACENCY_EXT : GL_LINE_STRIP;
  657. break;
  658. default:
  659. case RenderOperation::OT_TRIANGLE_LIST:
  660. primType = useAdjacency ? GL_TRIANGLES_ADJACENCY_EXT : GL_TRIANGLES;
  661. break;
  662. case RenderOperation::OT_TRIANGLE_STRIP:
  663. primType = useAdjacency ? GL_TRIANGLE_STRIP_ADJACENCY_EXT : GL_TRIANGLE_STRIP;
  664. break;
  665. case RenderOperation::OT_TRIANGLE_FAN:
  666. primType = GL_TRIANGLE_FAN;
  667. break;
  668. }
  669. if (op.useIndexes)
  670. {
  671. if(mCurrentCapabilities->hasCapability(RSC_VBO))
  672. {
  673. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
  674. static_cast<GLHardwareIndexBuffer*>(
  675. op.indexData->indexBuffer.get())->getGLBufferId());
  676. pBufferData = VBO_BUFFER_OFFSET(
  677. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  678. }
  679. else
  680. {
  681. pBufferData = static_cast<GLDefaultHardwareIndexBuffer*>(
  682. op.indexData->indexBuffer.get())->getDataPtr(
  683. op.indexData->indexStart * op.indexData->indexBuffer->getIndexSize());
  684. }
  685. GLenum indexType = (op.indexData->indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  686. glDrawElements(primType, op.indexData->indexCount, indexType, pBufferData);
  687. }
  688. else
  689. {
  690. glDrawArrays(primType, 0, op.vertexData->vertexCount);
  691. }
  692. glDisableClientState( GL_VERTEX_ARRAY );
  693. // only valid up to GL_MAX_TEXTURE_UNITS, which is recorded in mFixedFunctionTextureUnits
  694. if (multitexturing)
  695. {
  696. for (int i = 0; i < mFixedFunctionTextureUnits; i++)
  697. {
  698. glClientActiveTextureARB(GL_TEXTURE0 + i);
  699. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  700. }
  701. glClientActiveTextureARB(GL_TEXTURE0);
  702. }
  703. else
  704. {
  705. glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  706. }
  707. glDisableClientState( GL_NORMAL_ARRAY );
  708. glDisableClientState( GL_COLOR_ARRAY );
  709. if (GLEW_EXT_secondary_color)
  710. {
  711. glDisableClientState( GL_SECONDARY_COLOR_ARRAY );
  712. }
  713. // unbind any custom attributes
  714. for (vector<GLuint>::type::iterator ai = attribsBound.begin(); ai != attribsBound.end(); ++ai)
  715. {
  716. glDisableVertexAttribArrayARB(*ai);
  717. }
  718. glColor4f(1,1,1,1);
  719. if (GLEW_EXT_secondary_color)
  720. {
  721. glSecondaryColor3fEXT(0.0f, 0.0f, 0.0f);
  722. }
  723. }
  724. //---------------------------------------------------------------------
  725. void GLRenderSystem::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  726. {
  727. THROW_IF_NOT_RENDER_THREAD;
  728. mScissorTop = top;
  729. mScissorBottom = bottom;
  730. mScissorLeft = left;
  731. mScissorRight = right;
  732. }
  733. //---------------------------------------------------------------------
  734. void GLRenderSystem::clearFrameBuffer(unsigned int buffers,
  735. const Color& colour, float depth, unsigned short stencil)
  736. {
  737. THROW_IF_NOT_RENDER_THREAD;
  738. bool colourMask = !mColourWrite[0] || !mColourWrite[1]
  739. || !mColourWrite[2] || !mColourWrite[3];
  740. GLbitfield flags = 0;
  741. if (buffers & FBT_COLOUR)
  742. {
  743. flags |= GL_COLOR_BUFFER_BIT;
  744. // Enable buffer for writing if it isn't
  745. if (colourMask)
  746. {
  747. glColorMask(true, true, true, true);
  748. }
  749. glClearColor(colour.r, colour.g, colour.b, colour.a);
  750. }
  751. if (buffers & FBT_DEPTH)
  752. {
  753. flags |= GL_DEPTH_BUFFER_BIT;
  754. // Enable buffer for writing if it isn't
  755. if (!mDepthWrite)
  756. {
  757. glDepthMask( GL_TRUE );
  758. }
  759. glClearDepth(depth);
  760. }
  761. if (buffers & FBT_STENCIL)
  762. {
  763. flags |= GL_STENCIL_BUFFER_BIT;
  764. // Enable buffer for writing if it isn't
  765. glStencilMask(0xFFFFFFFF);
  766. glClearStencil(stencil);
  767. }
  768. // Should be enable scissor test due the clear region is
  769. // relied on scissor box bounds.
  770. GLboolean scissorTestEnabled = glIsEnabled(GL_SCISSOR_TEST);
  771. if (!scissorTestEnabled)
  772. {
  773. glEnable(GL_SCISSOR_TEST);
  774. }
  775. // Sets the scissor box as same as viewport
  776. GLint viewport[4] = { 0, 0, 0, 0 };
  777. GLint scissor[4] = { 0, 0, 0, 0 };
  778. glGetIntegerv(GL_VIEWPORT, viewport);
  779. glGetIntegerv(GL_SCISSOR_BOX, scissor);
  780. bool scissorBoxDifference =
  781. viewport[0] != scissor[0] || viewport[1] != scissor[1] ||
  782. viewport[2] != scissor[2] || viewport[3] != scissor[3];
  783. if (scissorBoxDifference)
  784. {
  785. glScissor(viewport[0], viewport[1], viewport[2], viewport[3]);
  786. }
  787. // Clear buffers
  788. glClear(flags);
  789. // Restore scissor box
  790. if (scissorBoxDifference)
  791. {
  792. glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
  793. }
  794. // Restore scissor test
  795. if (!scissorTestEnabled)
  796. {
  797. glDisable(GL_SCISSOR_TEST);
  798. }
  799. // Reset buffer write state
  800. if (!mDepthWrite && (buffers & FBT_DEPTH))
  801. {
  802. glDepthMask( GL_FALSE );
  803. }
  804. if (colourMask && (buffers & FBT_COLOUR))
  805. {
  806. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  807. }
  808. if (buffers & FBT_STENCIL)
  809. {
  810. glStencilMask(mStencilWriteMask);
  811. }
  812. }
  813. /************************************************************************/
  814. /* PRIVATE */
  815. /************************************************************************/
  816. //-----------------------------------------------------------------------------
  817. void GLRenderSystem::setTextureAddressingMode(UINT16 stage, const UVWAddressingMode& uvw)
  818. {
  819. if (!activateGLTextureUnit(stage))
  820. return;
  821. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_S,
  822. getTextureAddressingMode(uvw.u));
  823. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_T,
  824. getTextureAddressingMode(uvw.v));
  825. glTexParameteri( mTextureTypes[stage], GL_TEXTURE_WRAP_R,
  826. getTextureAddressingMode(uvw.w));
  827. activateGLTextureUnit(0);
  828. }
  829. //-----------------------------------------------------------------------------
  830. void GLRenderSystem::setTextureBorderColor(UINT16 stage, const Color& colour)
  831. {
  832. GLfloat border[4] = { colour.r, colour.g, colour.b, colour.a };
  833. if (activateGLTextureUnit(stage))
  834. {
  835. glTexParameterfv( mTextureTypes[stage], GL_TEXTURE_BORDER_COLOR, border);
  836. activateGLTextureUnit(0);
  837. }
  838. }
  839. //-----------------------------------------------------------------------------
  840. void GLRenderSystem::setTextureMipmapBias(UINT16 stage, float bias)
  841. {
  842. if (mCurrentCapabilities->hasCapability(RSC_MIPMAP_LOD_BIAS))
  843. {
  844. if (activateGLTextureUnit(stage))
  845. {
  846. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
  847. activateGLTextureUnit(0);
  848. }
  849. }
  850. }
  851. void GLRenderSystem::setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op )
  852. {
  853. GLint sourceBlend = getBlendMode(sourceFactor);
  854. GLint destBlend = getBlendMode(destFactor);
  855. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO)
  856. {
  857. glDisable(GL_BLEND);
  858. }
  859. else
  860. {
  861. glEnable(GL_BLEND);
  862. glBlendFunc(sourceBlend, destBlend);
  863. }
  864. GLint func = GL_FUNC_ADD;
  865. switch(op)
  866. {
  867. case SBO_ADD:
  868. func = GL_FUNC_ADD;
  869. break;
  870. case SBO_SUBTRACT:
  871. func = GL_FUNC_SUBTRACT;
  872. break;
  873. case SBO_REVERSE_SUBTRACT:
  874. func = GL_FUNC_REVERSE_SUBTRACT;
  875. break;
  876. case SBO_MIN:
  877. func = GL_MIN;
  878. break;
  879. case SBO_MAX:
  880. func = GL_MAX;
  881. break;
  882. }
  883. if(GLEW_VERSION_1_4 || GLEW_ARB_imaging)
  884. {
  885. glBlendEquation(func);
  886. }
  887. else if(GLEW_EXT_blend_minmax && (func == GL_MIN || func == GL_MAX))
  888. {
  889. glBlendEquationEXT(func);
  890. }
  891. }
  892. //-----------------------------------------------------------------------------
  893. void GLRenderSystem::setSceneBlending(
  894. SceneBlendFactor sourceFactor, SceneBlendFactor destFactor,
  895. SceneBlendFactor sourceFactorAlpha, SceneBlendFactor destFactorAlpha,
  896. SceneBlendOperation op, SceneBlendOperation alphaOp )
  897. {
  898. GLint sourceBlend = getBlendMode(sourceFactor);
  899. GLint destBlend = getBlendMode(destFactor);
  900. GLint sourceBlendAlpha = getBlendMode(sourceFactorAlpha);
  901. GLint destBlendAlpha = getBlendMode(destFactorAlpha);
  902. if(sourceFactor == SBF_ONE && destFactor == SBF_ZERO &&
  903. sourceFactorAlpha == SBF_ONE && destFactorAlpha == SBF_ZERO)
  904. {
  905. glDisable(GL_BLEND);
  906. }
  907. else
  908. {
  909. glEnable(GL_BLEND);
  910. glBlendFuncSeparate(sourceBlend, destBlend, sourceBlendAlpha, destBlendAlpha);
  911. }
  912. GLint func = GL_FUNC_ADD, alphaFunc = GL_FUNC_ADD;
  913. switch(op)
  914. {
  915. case SBO_ADD:
  916. func = GL_FUNC_ADD;
  917. break;
  918. case SBO_SUBTRACT:
  919. func = GL_FUNC_SUBTRACT;
  920. break;
  921. case SBO_REVERSE_SUBTRACT:
  922. func = GL_FUNC_REVERSE_SUBTRACT;
  923. break;
  924. case SBO_MIN:
  925. func = GL_MIN;
  926. break;
  927. case SBO_MAX:
  928. func = GL_MAX;
  929. break;
  930. }
  931. switch(alphaOp)
  932. {
  933. case SBO_ADD:
  934. alphaFunc = GL_FUNC_ADD;
  935. break;
  936. case SBO_SUBTRACT:
  937. alphaFunc = GL_FUNC_SUBTRACT;
  938. break;
  939. case SBO_REVERSE_SUBTRACT:
  940. alphaFunc = GL_FUNC_REVERSE_SUBTRACT;
  941. break;
  942. case SBO_MIN:
  943. alphaFunc = GL_MIN;
  944. break;
  945. case SBO_MAX:
  946. alphaFunc = GL_MAX;
  947. break;
  948. }
  949. if(GLEW_VERSION_2_0) {
  950. glBlendEquationSeparate(func, alphaFunc);
  951. }
  952. else if(GLEW_EXT_blend_equation_separate) {
  953. glBlendEquationSeparateEXT(func, alphaFunc);
  954. }
  955. }
  956. //-----------------------------------------------------------------------------
  957. void GLRenderSystem::setAlphaTest(CompareFunction func, unsigned char value)
  958. {
  959. if(func == CMPF_ALWAYS_PASS)
  960. {
  961. glDisable(GL_ALPHA_TEST);
  962. }
  963. else
  964. {
  965. glEnable(GL_ALPHA_TEST);
  966. glAlphaFunc(convertCompareFunction(func), value / 255.0f);
  967. }
  968. }
  969. //-----------------------------------------------------------------------------
  970. void GLRenderSystem::setAlphaToCoverage(bool enable)
  971. {
  972. static bool lasta2c = false;
  973. if (enable != lasta2c && getCapabilities()->hasCapability(RSC_ALPHA_TO_COVERAGE))
  974. {
  975. if (enable)
  976. glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  977. else
  978. glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  979. lasta2c = enable;
  980. }
  981. }
  982. //---------------------------------------------------------------------
  983. void GLRenderSystem::setScissorTestEnable(bool enable)
  984. {
  985. // If request texture flipping, use "upper-left", otherwise use "lower-left"
  986. bool flipping = mActiveRenderTarget->requiresTextureFlipping();
  987. // GL measures from the bottom, not the top
  988. UINT32 targetHeight = mActiveRenderTarget->getHeight();
  989. // Calculate the "lower-left" corner of the viewport
  990. GLsizei x = 0, y = 0, w = 0, h = 0;
  991. if (enable)
  992. {
  993. glEnable(GL_SCISSOR_TEST);
  994. // GL uses width / height rather than right / bottom
  995. x = mScissorLeft;
  996. if (flipping)
  997. y = mScissorTop;
  998. else
  999. y = targetHeight - mScissorBottom;
  1000. w = mScissorRight - mScissorLeft;
  1001. h = mScissorBottom - mScissorTop;
  1002. glScissor(x, y, w, h);
  1003. }
  1004. else
  1005. {
  1006. glDisable(GL_SCISSOR_TEST);
  1007. // GL requires you to reset the scissor when disabling
  1008. w = mActiveViewport.getActualWidth();
  1009. h = mActiveViewport.getActualHeight();
  1010. x = mActiveViewport.getActualLeft();
  1011. if (flipping)
  1012. y = mActiveViewport.getActualTop();
  1013. else
  1014. y = targetHeight - mActiveViewport.getActualTop() - h;
  1015. glScissor(x, y, w, h);
  1016. }
  1017. }
  1018. //-----------------------------------------------------------------------------
  1019. void GLRenderSystem::setCullingMode(CullingMode mode)
  1020. {
  1021. mCullingMode = mode;
  1022. GLenum cullMode;
  1023. switch( mode )
  1024. {
  1025. case CULL_NONE:
  1026. glDisable( GL_CULL_FACE );
  1027. return;
  1028. default:
  1029. case CULL_CLOCKWISE:
  1030. if (mActiveRenderTarget &&
  1031. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  1032. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  1033. {
  1034. cullMode = GL_FRONT;
  1035. }
  1036. else
  1037. {
  1038. cullMode = GL_BACK;
  1039. }
  1040. break;
  1041. case CULL_ANTICLOCKWISE:
  1042. if (mActiveRenderTarget &&
  1043. ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  1044. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding)))
  1045. {
  1046. cullMode = GL_BACK;
  1047. }
  1048. else
  1049. {
  1050. cullMode = GL_FRONT;
  1051. }
  1052. break;
  1053. }
  1054. glEnable( GL_CULL_FACE );
  1055. glCullFace( cullMode );
  1056. }
  1057. //-----------------------------------------------------------------------------
  1058. void GLRenderSystem::setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
  1059. {
  1060. setDepthBufferCheckEnabled(depthTest);
  1061. setDepthBufferWriteEnabled(depthWrite);
  1062. setDepthBufferFunction(depthFunction);
  1063. }
  1064. //-----------------------------------------------------------------------------
  1065. void GLRenderSystem::setDepthBufferCheckEnabled(bool enabled)
  1066. {
  1067. if (enabled)
  1068. {
  1069. glClearDepth(1.0f);
  1070. glEnable(GL_DEPTH_TEST);
  1071. }
  1072. else
  1073. {
  1074. glDisable(GL_DEPTH_TEST);
  1075. }
  1076. }
  1077. //-----------------------------------------------------------------------------
  1078. void GLRenderSystem::setDepthBufferWriteEnabled(bool enabled)
  1079. {
  1080. GLboolean flag = enabled ? GL_TRUE : GL_FALSE;
  1081. glDepthMask( flag );
  1082. // Store for reference in _beginFrame
  1083. mDepthWrite = enabled;
  1084. }
  1085. //-----------------------------------------------------------------------------
  1086. void GLRenderSystem::setDepthBufferFunction(CompareFunction func)
  1087. {
  1088. glDepthFunc(convertCompareFunction(func));
  1089. }
  1090. //-----------------------------------------------------------------------------
  1091. void GLRenderSystem::setDepthBias(float constantBias, float slopeScaleBias)
  1092. {
  1093. if (constantBias != 0 || slopeScaleBias != 0)
  1094. {
  1095. glEnable(GL_POLYGON_OFFSET_FILL);
  1096. glEnable(GL_POLYGON_OFFSET_POINT);
  1097. glEnable(GL_POLYGON_OFFSET_LINE);
  1098. glPolygonOffset(-slopeScaleBias, -constantBias);
  1099. }
  1100. else
  1101. {
  1102. glDisable(GL_POLYGON_OFFSET_FILL);
  1103. glDisable(GL_POLYGON_OFFSET_POINT);
  1104. glDisable(GL_POLYGON_OFFSET_LINE);
  1105. }
  1106. }
  1107. //-----------------------------------------------------------------------------
  1108. void GLRenderSystem::setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha)
  1109. {
  1110. glColorMask(red, green, blue, alpha);
  1111. // record this
  1112. mColourWrite[0] = red;
  1113. mColourWrite[1] = blue;
  1114. mColourWrite[2] = green;
  1115. mColourWrite[3] = alpha;
  1116. }
  1117. //---------------------------------------------------------------------
  1118. void GLRenderSystem::setPolygonMode(PolygonMode level)
  1119. {
  1120. GLenum glmode;
  1121. switch(level)
  1122. {
  1123. case PM_POINTS:
  1124. glmode = GL_POINT;
  1125. break;
  1126. case PM_WIREFRAME:
  1127. glmode = GL_LINE;
  1128. break;
  1129. default:
  1130. case PM_SOLID:
  1131. glmode = GL_FILL;
  1132. break;
  1133. }
  1134. glPolygonMode(GL_FRONT_AND_BACK, glmode);
  1135. }
  1136. //---------------------------------------------------------------------
  1137. void GLRenderSystem::setStencilCheckEnabled(bool enabled)
  1138. {
  1139. if (enabled)
  1140. glEnable(GL_STENCIL_TEST);
  1141. else
  1142. glDisable(GL_STENCIL_TEST);
  1143. }
  1144. //---------------------------------------------------------------------
  1145. void GLRenderSystem::setStencilBufferOperations(StencilOperation stencilFailOp,
  1146. StencilOperation depthFailOp, StencilOperation passOp, bool front)
  1147. {
  1148. if (front)
  1149. {
  1150. glStencilOpSeparate(GL_FRONT,
  1151. convertStencilOp(stencilFailOp, mInvertVertexWinding),
  1152. convertStencilOp(depthFailOp, mInvertVertexWinding),
  1153. convertStencilOp(passOp, mInvertVertexWinding));
  1154. }
  1155. else
  1156. {
  1157. glStencilOpSeparate(GL_BACK,
  1158. convertStencilOp(stencilFailOp, !mInvertVertexWinding),
  1159. convertStencilOp(depthFailOp, !mInvertVertexWinding),
  1160. convertStencilOp(passOp, !mInvertVertexWinding));
  1161. }
  1162. }
  1163. //---------------------------------------------------------------------
  1164. void GLRenderSystem::setStencilBufferFunc(CompareFunction func, UINT32 mask, bool front)
  1165. {
  1166. mStencilReadMask = mask;
  1167. if(front)
  1168. {
  1169. mStencilCompareFront = func;
  1170. glStencilFuncSeparate(GL_FRONT, convertCompareFunction(mStencilCompareFront), mStencilRefValue, mStencilReadMask);
  1171. }
  1172. else
  1173. {
  1174. mStencilCompareBack = func;
  1175. glStencilFuncSeparate(GL_BACK, convertCompareFunction(mStencilCompareBack), mStencilRefValue, mStencilReadMask);
  1176. }
  1177. }
  1178. //---------------------------------------------------------------------
  1179. void GLRenderSystem::setStencilBufferWriteMask(UINT32 mask)
  1180. {
  1181. mStencilWriteMask = mask;
  1182. glStencilMask(mask);
  1183. }
  1184. //---------------------------------------------------------------------
  1185. void GLRenderSystem::setTextureFiltering(UINT16 unit,
  1186. FilterType ftype, FilterOptions fo)
  1187. {
  1188. if (!activateGLTextureUnit(unit))
  1189. return;
  1190. switch(ftype)
  1191. {
  1192. case FT_MIN:
  1193. mMinFilter = fo;
  1194. // Combine with existing mip filter
  1195. glTexParameteri(
  1196. mTextureTypes[unit],
  1197. GL_TEXTURE_MIN_FILTER,
  1198. getCombinedMinMipFilter());
  1199. break;
  1200. case FT_MAG:
  1201. switch (fo)
  1202. {
  1203. case FO_ANISOTROPIC: // GL treats linear and aniso the same
  1204. case FO_LINEAR:
  1205. glTexParameteri(
  1206. mTextureTypes[unit],
  1207. GL_TEXTURE_MAG_FILTER,
  1208. GL_LINEAR);
  1209. break;
  1210. case FO_POINT:
  1211. case FO_NONE:
  1212. glTexParameteri(
  1213. mTextureTypes[unit],
  1214. GL_TEXTURE_MAG_FILTER,
  1215. GL_NEAREST);
  1216. break;
  1217. }
  1218. break;
  1219. case FT_MIP:
  1220. mMipFilter = fo;
  1221. // Combine with existing min filter
  1222. glTexParameteri(
  1223. mTextureTypes[unit],
  1224. GL_TEXTURE_MIN_FILTER,
  1225. getCombinedMinMipFilter());
  1226. break;
  1227. }
  1228. activateGLTextureUnit(0);
  1229. }
  1230. //---------------------------------------------------------------------
  1231. void GLRenderSystem::setTextureAnisotropy(UINT16 unit, unsigned int maxAnisotropy)
  1232. {
  1233. if (!mCurrentCapabilities->hasCapability(RSC_ANISOTROPY))
  1234. return;
  1235. if (!activateGLTextureUnit(unit))
  1236. return;
  1237. GLfloat largest_supported_anisotropy = 0;
  1238. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest_supported_anisotropy);
  1239. if (maxAnisotropy > largest_supported_anisotropy)
  1240. maxAnisotropy = largest_supported_anisotropy ?
  1241. static_cast<UINT32>(largest_supported_anisotropy) : 1;
  1242. if (_getCurrentAnisotropy(unit) != maxAnisotropy)
  1243. glTexParameterf(mTextureTypes[unit], GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
  1244. activateGLTextureUnit(0);
  1245. }
  1246. //-----------------------------------------------------------------------------
  1247. void GLRenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
  1248. {
  1249. // A note on GL user clipping:
  1250. // When an ARB vertex program is enabled in GL, user clipping is completely
  1251. // disabled. There is no way around this, it's just turned off.
  1252. // When using GLSL, user clipping can work but you have to include a
  1253. // glClipVertex command in your vertex shader.
  1254. // Thus the planes set here may not actually be respected.
  1255. size_t i = 0;
  1256. size_t numClipPlanes;
  1257. GLdouble clipPlane[4];
  1258. // Save previous modelview
  1259. glMatrixMode(GL_MODELVIEW);
  1260. glPushMatrix();
  1261. // just load view matrix (identity world)
  1262. GLfloat mat[16];
  1263. makeGLMatrix(mat, mViewMatrix);
  1264. glLoadMatrixf(mat);
  1265. numClipPlanes = clipPlanes.size();
  1266. for (i = 0; i < numClipPlanes; ++i)
  1267. {
  1268. GLenum clipPlaneId = static_cast<GLenum>(GL_CLIP_PLANE0 + i);
  1269. const Plane& plane = clipPlanes[i];
  1270. if (i >= 6/*GL_MAX_CLIP_PLANES*/)
  1271. {
  1272. CM_EXCEPT(RenderingAPIException, "Unable to set clip plane");
  1273. }
  1274. clipPlane[0] = plane.normal.x;
  1275. clipPlane[1] = plane.normal.y;
  1276. clipPlane[2] = plane.normal.z;
  1277. clipPlane[3] = plane.d;
  1278. glClipPlane(clipPlaneId, clipPlane);
  1279. glEnable(clipPlaneId);
  1280. }
  1281. // disable remaining clip planes
  1282. for ( ; i < 6/*GL_MAX_CLIP_PLANES*/; ++i)
  1283. {
  1284. glDisable(static_cast<GLenum>(GL_CLIP_PLANE0 + i));
  1285. }
  1286. // restore matrices
  1287. glPopMatrix();
  1288. }
  1289. //---------------------------------------------------------------------
  1290. void GLRenderSystem::oneTimeContextInitialization()
  1291. {
  1292. if (GLEW_VERSION_1_2)
  1293. {
  1294. // Set nicer lighting model -- d3d9 has this by default
  1295. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
  1296. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  1297. }
  1298. if (GLEW_VERSION_1_4)
  1299. {
  1300. glEnable(GL_COLOR_SUM);
  1301. glDisable(GL_DITHER);
  1302. }
  1303. // Check for FSAA
  1304. // Enable the extension if it was enabled by the GLSupport
  1305. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  1306. {
  1307. int fsaa_active = false;
  1308. glGetIntegerv(GL_SAMPLE_BUFFERS_ARB,(GLint*)&fsaa_active);
  1309. if(fsaa_active)
  1310. {
  1311. glEnable(GL_MULTISAMPLE_ARB);
  1312. }
  1313. }
  1314. }
  1315. //---------------------------------------------------------------------
  1316. void GLRenderSystem::switchContext(GLContext *context)
  1317. {
  1318. // Unbind GPU programs and rebind to new context later, because
  1319. // scene manager treat render system as ONE 'context' ONLY, and it
  1320. // cached the GPU programs using state.
  1321. if (mCurrentVertexProgram)
  1322. mCurrentVertexProgram->unbindProgram();
  1323. if (mCurrentGeometryProgram)
  1324. mCurrentGeometryProgram->unbindProgram();
  1325. if (mCurrentFragmentProgram)
  1326. mCurrentFragmentProgram->unbindProgram();
  1327. // Disable lights
  1328. mCurrentLights = 0;
  1329. // Disable textures
  1330. disableTextureUnitsFrom(0);
  1331. // It's ready for switching
  1332. if (mCurrentContext)
  1333. mCurrentContext->endCurrent();
  1334. mCurrentContext = context;
  1335. mCurrentContext->setCurrent();
  1336. // Check if the context has already done one-time initialisation
  1337. if(!mCurrentContext->getInitialized())
  1338. {
  1339. oneTimeContextInitialization();
  1340. mCurrentContext->setInitialized();
  1341. }
  1342. // Rebind GPU programs to new context
  1343. if (mCurrentVertexProgram)
  1344. mCurrentVertexProgram->bindProgram();
  1345. if (mCurrentGeometryProgram)
  1346. mCurrentGeometryProgram->bindProgram();
  1347. if (mCurrentFragmentProgram)
  1348. mCurrentFragmentProgram->bindProgram();
  1349. // Must reset depth/colour write mask to according with user desired, otherwise,
  1350. // clearFrameBuffer would be wrong because the value we are recorded may be
  1351. // difference with the really state stored in GL context.
  1352. glDepthMask(mDepthWrite);
  1353. glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
  1354. glStencilMask(mStencilWriteMask);
  1355. }
  1356. //---------------------------------------------------------------------
  1357. void GLRenderSystem::registerContext(GLContext* context)
  1358. {
  1359. if (!mGLInitialised)
  1360. {
  1361. // set up glew and GLSupport
  1362. initialiseContext(context);
  1363. std::vector<CamelotEngine::String> tokens = StringUtil::split(mGLSupport->getGLVersion(), ".");
  1364. if (!tokens.empty())
  1365. {
  1366. mDriverVersion.major = parseInt(tokens[0]);
  1367. if (tokens.size() > 1)
  1368. mDriverVersion.minor = parseInt(tokens[1]);
  1369. if (tokens.size() > 2)
  1370. mDriverVersion.release = parseInt(tokens[2]);
  1371. }
  1372. mDriverVersion.build = 0;
  1373. // Initialise GL after the first window has been created
  1374. // TODO: fire this from emulation options, and don't duplicate float and Current capabilities
  1375. mCurrentCapabilities = createRenderSystemCapabilities();
  1376. initialiseFromRenderSystemCapabilities(mCurrentCapabilities);
  1377. // Initialise the main context
  1378. oneTimeContextInitialization();
  1379. if(mCurrentContext)
  1380. mCurrentContext->setInitialized();
  1381. }
  1382. }
  1383. //---------------------------------------------------------------------
  1384. void GLRenderSystem::unregisterContext(GLContext *context)
  1385. {
  1386. if(mCurrentContext == context) {
  1387. // Change the context to something else so that a valid context
  1388. // remains active. When this is the main context being unregistered,
  1389. // we set the main context to 0.
  1390. if(mCurrentContext != mMainContext) {
  1391. switchContext(mMainContext);
  1392. } else {
  1393. /// No contexts remain
  1394. mCurrentContext->endCurrent();
  1395. mCurrentContext = 0;
  1396. mMainContext = 0;
  1397. }
  1398. }
  1399. }
  1400. //---------------------------------------------------------------------
  1401. bool GLRenderSystem::activateGLTextureUnit(UINT16 unit)
  1402. {
  1403. if (mActiveTextureUnit != unit)
  1404. {
  1405. if (GLEW_VERSION_1_2 && unit < getCapabilities()->getNumTextureUnits())
  1406. {
  1407. glActiveTextureARB(GL_TEXTURE0 + unit);
  1408. mActiveTextureUnit = unit;
  1409. return true;
  1410. }
  1411. else if (!unit)
  1412. {
  1413. // always ok to use the first unit
  1414. return true;
  1415. }
  1416. else
  1417. {
  1418. return false;
  1419. }
  1420. }
  1421. else
  1422. {
  1423. return true;
  1424. }
  1425. }
  1426. //---------------------------------------------------------------------
  1427. void GLRenderSystem::initConfigOptions(void)
  1428. {
  1429. mGLSupport->addConfig();
  1430. }
  1431. //---------------------------------------------------------------------
  1432. GLfloat GLRenderSystem::_getCurrentAnisotropy(UINT16 unit)
  1433. {
  1434. GLfloat curAniso = 0;
  1435. glGetTexParameterfv(mTextureTypes[unit],
  1436. GL_TEXTURE_MAX_ANISOTROPY_EXT, &curAniso);
  1437. return curAniso ? curAniso : 1;
  1438. }
  1439. //---------------------------------------------------------------------
  1440. GLuint GLRenderSystem::getCombinedMinMipFilter(void) const
  1441. {
  1442. switch(mMinFilter)
  1443. {
  1444. case FO_ANISOTROPIC:
  1445. case FO_LINEAR:
  1446. switch(mMipFilter)
  1447. {
  1448. case FO_ANISOTROPIC:
  1449. case FO_LINEAR:
  1450. // linear min, linear mip
  1451. return GL_LINEAR_MIPMAP_LINEAR;
  1452. case FO_POINT:
  1453. // linear min, point mip
  1454. return GL_LINEAR_MIPMAP_NEAREST;
  1455. case FO_NONE:
  1456. // linear min, no mip
  1457. return GL_LINEAR;
  1458. }
  1459. break;
  1460. case FO_POINT:
  1461. case FO_NONE:
  1462. switch(mMipFilter)
  1463. {
  1464. case FO_ANISOTROPIC:
  1465. case FO_LINEAR:
  1466. // nearest min, linear mip
  1467. return GL_NEAREST_MIPMAP_LINEAR;
  1468. case FO_POINT:
  1469. // nearest min, point mip
  1470. return GL_NEAREST_MIPMAP_NEAREST;
  1471. case FO_NONE:
  1472. // nearest min, no mip
  1473. return GL_NEAREST;
  1474. }
  1475. break;
  1476. }
  1477. // should never get here
  1478. return 0;
  1479. }
  1480. //---------------------------------------------------------------------
  1481. GLint GLRenderSystem::convertStencilOp(StencilOperation op, bool invert) const
  1482. {
  1483. switch(op)
  1484. {
  1485. case SOP_KEEP:
  1486. return GL_KEEP;
  1487. case SOP_ZERO:
  1488. return GL_ZERO;
  1489. case SOP_REPLACE:
  1490. return GL_REPLACE;
  1491. case SOP_INCREMENT:
  1492. return invert ? GL_DECR : GL_INCR;
  1493. case SOP_DECREMENT:
  1494. return invert ? GL_INCR : GL_DECR;
  1495. case SOP_INCREMENT_WRAP:
  1496. return invert ? GL_DECR_WRAP_EXT : GL_INCR_WRAP_EXT;
  1497. case SOP_DECREMENT_WRAP:
  1498. return invert ? GL_INCR_WRAP_EXT : GL_DECR_WRAP_EXT;
  1499. case SOP_INVERT:
  1500. return GL_INVERT;
  1501. };
  1502. // to keep compiler happy
  1503. return SOP_KEEP;
  1504. }
  1505. //---------------------------------------------------------------------
  1506. GLint GLRenderSystem::convertCompareFunction(CompareFunction func) const
  1507. {
  1508. switch(func)
  1509. {
  1510. case CMPF_ALWAYS_FAIL:
  1511. return GL_NEVER;
  1512. case CMPF_ALWAYS_PASS:
  1513. return GL_ALWAYS;
  1514. case CMPF_LESS:
  1515. return GL_LESS;
  1516. case CMPF_LESS_EQUAL:
  1517. return GL_LEQUAL;
  1518. case CMPF_EQUAL:
  1519. return GL_EQUAL;
  1520. case CMPF_NOT_EQUAL:
  1521. return GL_NOTEQUAL;
  1522. case CMPF_GREATER_EQUAL:
  1523. return GL_GEQUAL;
  1524. case CMPF_GREATER:
  1525. return GL_GREATER;
  1526. };
  1527. // to keep compiler happy
  1528. return GL_ALWAYS;
  1529. }
  1530. //-----------------------------------------------------------------------------
  1531. String GLRenderSystem::getErrorDescription(long errCode) const
  1532. {
  1533. const GLubyte *errString = gluErrorString (errCode);
  1534. return (errString != 0) ? String((const char*) errString) : StringUtil::BLANK;
  1535. }
  1536. //-----------------------------------------------------------------------------
  1537. GLint GLRenderSystem::getBlendMode(SceneBlendFactor blendMode) const
  1538. {
  1539. switch(blendMode)
  1540. {
  1541. case SBF_ONE:
  1542. return GL_ONE;
  1543. case SBF_ZERO:
  1544. return GL_ZERO;
  1545. case SBF_DEST_COLOUR:
  1546. return GL_DST_COLOR;
  1547. case SBF_SOURCE_COLOUR:
  1548. return GL_SRC_COLOR;
  1549. case SBF_ONE_MINUS_DEST_COLOUR:
  1550. return GL_ONE_MINUS_DST_COLOR;
  1551. case SBF_ONE_MINUS_SOURCE_COLOUR:
  1552. return GL_ONE_MINUS_SRC_COLOR;
  1553. case SBF_DEST_ALPHA:
  1554. return GL_DST_ALPHA;
  1555. case SBF_SOURCE_ALPHA:
  1556. return GL_SRC_ALPHA;
  1557. case SBF_ONE_MINUS_DEST_ALPHA:
  1558. return GL_ONE_MINUS_DST_ALPHA;
  1559. case SBF_ONE_MINUS_SOURCE_ALPHA:
  1560. return GL_ONE_MINUS_SRC_ALPHA;
  1561. };
  1562. // to keep compiler happy
  1563. return GL_ONE;
  1564. }
  1565. //-----------------------------------------------------------------------------
  1566. GLint GLRenderSystem::getTextureAddressingMode(
  1567. TextureAddressingMode tam) const
  1568. {
  1569. switch(tam)
  1570. {
  1571. default:
  1572. case TAM_WRAP:
  1573. return GL_REPEAT;
  1574. case TAM_MIRROR:
  1575. return GL_MIRRORED_REPEAT;
  1576. case TAM_CLAMP:
  1577. return GL_CLAMP_TO_EDGE;
  1578. case TAM_BORDER:
  1579. return GL_CLAMP_TO_BORDER;
  1580. }
  1581. }
  1582. //-----------------------------------------------------------------------------
  1583. void GLRenderSystem::makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m)
  1584. {
  1585. size_t x = 0;
  1586. for (size_t i = 0; i < 4; i++)
  1587. {
  1588. for (size_t j = 0; j < 4; j++)
  1589. {
  1590. gl_matrix[x] = m[j][i];
  1591. x++;
  1592. }
  1593. }
  1594. }
  1595. //-----------------------------------------------------------------------
  1596. void GLRenderSystem::initialiseContext(GLContext* primary)
  1597. {
  1598. // Set main and current context
  1599. mMainContext = primary;
  1600. mCurrentContext = mMainContext;
  1601. // Set primary context as active
  1602. if(mCurrentContext)
  1603. mCurrentContext->setCurrent();
  1604. // Setup GLSupport
  1605. mGLSupport->initialiseExtensions();
  1606. // Get extension function pointers
  1607. #if CM_THREAD_SUPPORT != 1
  1608. glewContextInit(mGLSupport);
  1609. #endif
  1610. }
  1611. void GLRenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps)
  1612. {
  1613. if(caps->getRenderSystemName() != getName())
  1614. {
  1615. CM_EXCEPT(InvalidParametersException,
  1616. "Trying to initialize GLRenderSystem from RenderSystemCapabilities that do not support OpenGL");
  1617. }
  1618. // set texture the number of texture units
  1619. mFixedFunctionTextureUnits = caps->getNumTextureUnits();
  1620. //In GL there can be less fixed function texture units than general
  1621. //texture units. Get the minimum of the two.
  1622. if (caps->hasCapability(RSC_FRAGMENT_PROGRAM))
  1623. {
  1624. GLint maxTexCoords = 0;
  1625. glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &maxTexCoords);
  1626. if (mFixedFunctionTextureUnits > maxTexCoords)
  1627. {
  1628. mFixedFunctionTextureUnits = maxTexCoords;
  1629. }
  1630. }
  1631. if(caps->hasCapability(RSC_GL1_5_NOVBO))
  1632. {
  1633. // Assign ARB functions same to GL 1.5 version since
  1634. // interface identical
  1635. glBindBufferARB = glBindBuffer;
  1636. glBufferDataARB = glBufferData;
  1637. glBufferSubDataARB = glBufferSubData;
  1638. glDeleteBuffersARB = glDeleteBuffers;
  1639. glGenBuffersARB = glGenBuffers;
  1640. glGetBufferParameterivARB = glGetBufferParameteriv;
  1641. glGetBufferPointervARB = glGetBufferPointerv;
  1642. glGetBufferSubDataARB = glGetBufferSubData;
  1643. glIsBufferARB = glIsBuffer;
  1644. glMapBufferARB = glMapBuffer;
  1645. glUnmapBufferARB = glUnmapBuffer;
  1646. }
  1647. if(caps->hasCapability(RSC_VBO))
  1648. {
  1649. HardwareBufferManager::startUp(new GLHardwareBufferManager);
  1650. }
  1651. else
  1652. {
  1653. HardwareBufferManager::startUp(new GLDefaultHardwareBufferManager);
  1654. }
  1655. // GPU Program Manager setup
  1656. GpuProgramManager::startUp(new GLGpuProgramManager());
  1657. GLGpuProgramManager* gpuProgramManager = static_cast<GLGpuProgramManager*>(GpuProgramManager::instancePtr());
  1658. if(caps->hasCapability(RSC_VERTEX_PROGRAM))
  1659. {
  1660. if(caps->isShaderProfileSupported("arbvp1"))
  1661. {
  1662. gpuProgramManager->registerProgramFactory("arbvp1", createGLArbGpuProgram);
  1663. }
  1664. if(caps->isShaderProfileSupported("vp30"))
  1665. {
  1666. gpuProgramManager->registerProgramFactory("vp30", createGLArbGpuProgram);
  1667. }
  1668. if(caps->isShaderProfileSupported("vp40"))
  1669. {
  1670. gpuProgramManager->registerProgramFactory("vp40", createGLArbGpuProgram);
  1671. }
  1672. if(caps->isShaderProfileSupported("gp4vp"))
  1673. {
  1674. gpuProgramManager->registerProgramFactory("gp4vp", createGLArbGpuProgram);
  1675. }
  1676. if(caps->isShaderProfileSupported("gpu_vp"))
  1677. {
  1678. gpuProgramManager->registerProgramFactory("gpu_vp", createGLArbGpuProgram);
  1679. }
  1680. }
  1681. if(caps->hasCapability(RSC_GEOMETRY_PROGRAM))
  1682. {
  1683. //TODO : Should these be createGLArbGpuProgram or createGLGpuNVparseProgram?
  1684. if(caps->isShaderProfileSupported("nvgp4"))
  1685. {
  1686. gpuProgramManager->registerProgramFactory("nvgp4", createGLArbGpuProgram);
  1687. }
  1688. if(caps->isShaderProfileSupported("gp4gp"))
  1689. {
  1690. gpuProgramManager->registerProgramFactory("gp4gp", createGLArbGpuProgram);
  1691. }
  1692. if(caps->isShaderProfileSupported("gpu_gp"))
  1693. {
  1694. gpuProgramManager->registerProgramFactory("gpu_gp", createGLArbGpuProgram);
  1695. }
  1696. }
  1697. if(caps->hasCapability(RSC_FRAGMENT_PROGRAM))
  1698. {
  1699. if(caps->isShaderProfileSupported("ps_1_4"))
  1700. {
  1701. gpuProgramManager->registerProgramFactory("ps_1_4", createGL_ATI_FS_GpuProgram);
  1702. }
  1703. if(caps->isShaderProfileSupported("ps_1_3"))
  1704. {
  1705. gpuProgramManager->registerProgramFactory("ps_1_3", createGL_ATI_FS_GpuProgram);
  1706. }
  1707. if(caps->isShaderProfileSupported("ps_1_2"))
  1708. {
  1709. gpuProgramManager->registerProgramFactory("ps_1_2", createGL_ATI_FS_GpuProgram);
  1710. }
  1711. if(caps->isShaderProfileSupported("ps_1_1"))
  1712. {
  1713. gpuProgramManager->registerProgramFactory("ps_1_1", createGL_ATI_FS_GpuProgram);
  1714. }
  1715. if(caps->isShaderProfileSupported("arbfp1"))
  1716. {
  1717. gpuProgramManager->registerProgramFactory("arbfp1", createGLArbGpuProgram);
  1718. }
  1719. if(caps->isShaderProfileSupported("fp40"))
  1720. {
  1721. gpuProgramManager->registerProgramFactory("fp40", createGLArbGpuProgram);
  1722. }
  1723. if(caps->isShaderProfileSupported("fp30"))
  1724. {
  1725. gpuProgramManager->registerProgramFactory("fp30", createGLArbGpuProgram);
  1726. }
  1727. }
  1728. if(caps->isShaderProfileSupported("glsl"))
  1729. {
  1730. // NFZ - check for GLSL vertex and fragment shader support successful
  1731. mGLSLProgramFactory = new GLSLProgramFactory();
  1732. HighLevelGpuProgramManager::instance().addFactory(mGLSLProgramFactory);
  1733. }
  1734. if(caps->isShaderProfileSupported("cg"))
  1735. {
  1736. // NFZ - check for GLSL vertex and fragment shader support successful
  1737. mCgProgramFactory = new CgProgramFactory();
  1738. HighLevelGpuProgramManager::instance().addFactory(mCgProgramFactory);
  1739. }
  1740. if(caps->hasCapability(RSC_HWOCCLUSION))
  1741. {
  1742. if(caps->hasCapability(RSC_GL1_5_NOHWOCCLUSION))
  1743. {
  1744. // Assign ARB functions same to GL 1.5 version since
  1745. // interface identical
  1746. glBeginQueryARB = glBeginQuery;
  1747. glDeleteQueriesARB = glDeleteQueries;
  1748. glEndQueryARB = glEndQuery;
  1749. glGenQueriesARB = glGenQueries;
  1750. glGetQueryObjectivARB = glGetQueryObjectiv;
  1751. glGetQueryObjectuivARB = glGetQueryObjectuiv;
  1752. glGetQueryivARB = glGetQueryiv;
  1753. glIsQueryARB = glIsQuery;
  1754. }
  1755. }
  1756. // Check for framebuffer object extension
  1757. if(caps->hasCapability(RSC_FBO))
  1758. {
  1759. // Before GL version 2.0, we need to get one of the extensions
  1760. if(caps->hasCapability(RSC_FBO_ARB))
  1761. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersARB;
  1762. else if(caps->hasCapability(RSC_FBO_ATI))
  1763. GLEW_GET_FUN(__glewDrawBuffers) = glDrawBuffersATI;
  1764. if(caps->hasCapability(RSC_HWRENDER_TO_TEXTURE))
  1765. {
  1766. // Create FBO manager
  1767. GLRTTManager::startUp(new GLRTTManager(false));
  1768. }
  1769. }
  1770. else
  1771. {
  1772. CM_EXCEPT(RenderingAPIException, "GPU doesn't support frame buffer objects. OpenGL versions lower than 3.0 are not supported.");
  1773. }
  1774. /// Create the texture manager
  1775. TextureManager::startUp(new GLTextureManager(*mGLSupport));
  1776. mGLInitialised = true;
  1777. }
  1778. RenderSystemCapabilities* GLRenderSystem::createRenderSystemCapabilities() const
  1779. {
  1780. RenderSystemCapabilities* rsc = new RenderSystemCapabilities();
  1781. rsc->setCategoryRelevant(CAPS_CATEGORY_GL, true);
  1782. rsc->setDriverVersion(mDriverVersion);
  1783. const char* deviceName = (const char*)glGetString(GL_RENDERER);
  1784. const char* vendorName = (const char*)glGetString(GL_VENDOR);
  1785. rsc->setDeviceName(deviceName);
  1786. rsc->setRenderSystemName(getName());
  1787. // determine vendor
  1788. if (strstr(vendorName, "NVIDIA"))
  1789. rsc->setVendor(GPU_NVIDIA);
  1790. else if (strstr(vendorName, "ATI"))
  1791. rsc->setVendor(GPU_ATI);
  1792. else if (strstr(vendorName, "Intel"))
  1793. rsc->setVendor(GPU_INTEL);
  1794. else if (strstr(vendorName, "S3"))
  1795. rsc->setVendor(GPU_S3);
  1796. else if (strstr(vendorName, "Matrox"))
  1797. rsc->setVendor(GPU_MATROX);
  1798. else if (strstr(vendorName, "3DLabs"))
  1799. rsc->setVendor(GPU_3DLABS);
  1800. else if (strstr(vendorName, "SiS"))
  1801. rsc->setVendor(GPU_SIS);
  1802. else
  1803. rsc->setVendor(GPU_UNKNOWN);
  1804. // Supports fixed-function
  1805. rsc->setCapability(RSC_FIXED_FUNCTION);
  1806. // Check for hardware mipmapping support.
  1807. if(GLEW_VERSION_1_4 || GLEW_SGIS_generate_mipmap)
  1808. {
  1809. bool disableAutoMip = false;
  1810. #if CM_PLATFORM == CM_PLATFORM_APPLE || CM_PLATFORM == CM_PLATFORM_LINUX
  1811. // Apple & Linux ATI drivers have faults in hardware mipmap generation
  1812. if (rsc->getVendor() == GPU_ATI)
  1813. disableAutoMip = true;
  1814. #endif
  1815. // The Intel 915G frequently corrupts textures when using hardware mip generation
  1816. // I'm not currently sure how many generations of hardware this affects,
  1817. // so for now, be safe.
  1818. if (rsc->getVendor() == GPU_INTEL)
  1819. disableAutoMip = true;
  1820. // SiS chipsets also seem to have problems with this
  1821. if (rsc->getVendor() == GPU_SIS)
  1822. disableAutoMip = true;
  1823. if (!disableAutoMip)
  1824. rsc->setCapability(RSC_AUTOMIPMAP);
  1825. }
  1826. // Check for blending support
  1827. if(GLEW_VERSION_1_3 ||
  1828. GLEW_ARB_texture_env_combine ||
  1829. GLEW_EXT_texture_env_combine)
  1830. {
  1831. rsc->setCapability(RSC_BLENDING);
  1832. }
  1833. // Check for Multitexturing support and set number of texture units
  1834. if(GLEW_VERSION_1_3 ||
  1835. GLEW_ARB_multitexture)
  1836. {
  1837. GLint units;
  1838. glGetIntegerv( GL_MAX_TEXTURE_UNITS, &units );
  1839. if (GLEW_ARB_fragment_program)
  1840. {
  1841. // Also check GL_MAX_TEXTURE_IMAGE_UNITS_ARB since NV at least
  1842. // only increased this on the FX/6x00 series
  1843. GLint arbUnits;
  1844. glGetIntegerv( GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &arbUnits );
  1845. if (arbUnits > units)
  1846. units = arbUnits;
  1847. }
  1848. rsc->setNumTextureUnits(units);
  1849. }
  1850. else
  1851. {
  1852. // If no multitexture support then set one texture unit
  1853. rsc->setNumTextureUnits(1);
  1854. }
  1855. // Check for Anisotropy support
  1856. if(GLEW_EXT_texture_filter_anisotropic)
  1857. {
  1858. rsc->setCapability(RSC_ANISOTROPY);
  1859. }
  1860. // Check for DOT3 support
  1861. if(GLEW_VERSION_1_3 ||
  1862. GLEW_ARB_texture_env_dot3 ||
  1863. GLEW_EXT_texture_env_dot3)
  1864. {
  1865. rsc->setCapability(RSC_DOT3);
  1866. }
  1867. // Check for cube mapping
  1868. if(GLEW_VERSION_1_3 ||
  1869. GLEW_ARB_texture_cube_map ||
  1870. GLEW_EXT_texture_cube_map)
  1871. {
  1872. rsc->setCapability(RSC_CUBEMAPPING);
  1873. }
  1874. // Point sprites
  1875. if (GLEW_VERSION_2_0 || GLEW_ARB_point_sprite)
  1876. {
  1877. rsc->setCapability(RSC_POINT_SPRITES);
  1878. }
  1879. // Check for point parameters
  1880. if (GLEW_VERSION_1_4)
  1881. {
  1882. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
  1883. }
  1884. if (GLEW_ARB_point_parameters)
  1885. {
  1886. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_ARB);
  1887. }
  1888. if (GLEW_EXT_point_parameters)
  1889. {
  1890. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS_EXT);
  1891. }
  1892. // Check for hardware stencil support and set bit depth
  1893. GLint stencil;
  1894. glGetIntegerv(GL_STENCIL_BITS,&stencil);
  1895. if(stencil)
  1896. {
  1897. rsc->setCapability(RSC_HWSTENCIL);
  1898. rsc->setStencilBufferBitDepth(stencil);
  1899. }
  1900. if(GLEW_VERSION_1_5 || GLEW_ARB_vertex_buffer_object)
  1901. {
  1902. if (!GLEW_ARB_vertex_buffer_object)
  1903. {
  1904. rsc->setCapability(RSC_GL1_5_NOVBO);
  1905. }
  1906. rsc->setCapability(RSC_VBO);
  1907. }
  1908. if(GLEW_ARB_vertex_program)
  1909. {
  1910. rsc->setCapability(RSC_VERTEX_PROGRAM);
  1911. // Vertex Program Properties
  1912. rsc->setVertexProgramConstantBoolCount(0);
  1913. rsc->setVertexProgramConstantIntCount(0);
  1914. GLint floatConstantCount;
  1915. glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
  1916. rsc->setVertexProgramConstantFloatCount(floatConstantCount);
  1917. rsc->addShaderProfile("arbvp1");
  1918. rsc->addGpuProgramProfile(GPP_VS_1_1, "arbvp1"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1919. rsc->addGpuProgramProfile(GPP_VS_2_0, "arbvp1");
  1920. rsc->addGpuProgramProfile(GPP_VS_2_a, "arbvp1");
  1921. rsc->addGpuProgramProfile(GPP_VS_2_x, "arbvp1");
  1922. if (GLEW_NV_vertex_program2_option)
  1923. {
  1924. rsc->addShaderProfile("vp30");
  1925. rsc->addGpuProgramProfile(GPP_VS_3_0, "vp30"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1926. rsc->addGpuProgramProfile(GPP_VS_4_0, "vp30");
  1927. }
  1928. if (GLEW_NV_vertex_program3)
  1929. {
  1930. rsc->addShaderProfile("vp40");
  1931. rsc->addGpuProgramProfile(GPP_VS_3_0, "vp40"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1932. rsc->addGpuProgramProfile(GPP_VS_4_0, "vp40");
  1933. }
  1934. if (GLEW_NV_vertex_program4)
  1935. {
  1936. rsc->addShaderProfile("gp4vp");
  1937. rsc->addShaderProfile("gpu_vp");
  1938. }
  1939. }
  1940. if (GLEW_NV_register_combiners2 &&
  1941. GLEW_NV_texture_shader)
  1942. {
  1943. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  1944. }
  1945. // NFZ - check for ATI fragment shader support
  1946. if (GLEW_ATI_fragment_shader)
  1947. {
  1948. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  1949. // no boolean params allowed
  1950. rsc->setFragmentProgramConstantBoolCount(0);
  1951. // no integer params allowed
  1952. rsc->setFragmentProgramConstantIntCount(0);
  1953. // only 8 Vector4 constant floats supported
  1954. rsc->setFragmentProgramConstantFloatCount(8);
  1955. rsc->addShaderProfile("ps_1_4");
  1956. rsc->addShaderProfile("ps_1_3");
  1957. rsc->addShaderProfile("ps_1_2");
  1958. rsc->addShaderProfile("ps_1_1");
  1959. rsc->addGpuProgramProfile(GPP_PS_1_1, "ps_1_1"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1960. rsc->addGpuProgramProfile(GPP_PS_1_2, "ps_1_2");
  1961. rsc->addGpuProgramProfile(GPP_PS_1_3, "ps_1_3");
  1962. rsc->addGpuProgramProfile(GPP_PS_1_4, "ps_1_4");
  1963. }
  1964. if (GLEW_ARB_fragment_program)
  1965. {
  1966. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  1967. // Fragment Program Properties
  1968. rsc->setFragmentProgramConstantBoolCount(0);
  1969. rsc->setFragmentProgramConstantIntCount(0);
  1970. GLint floatConstantCount;
  1971. glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &floatConstantCount);
  1972. rsc->setFragmentProgramConstantFloatCount(floatConstantCount);
  1973. rsc->addShaderProfile("arbfp1");
  1974. rsc->addGpuProgramProfile(GPP_PS_1_1, "arbfp1"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1975. rsc->addGpuProgramProfile(GPP_PS_1_2, "arbfp1");
  1976. rsc->addGpuProgramProfile(GPP_PS_1_3, "arbfp1");
  1977. rsc->addGpuProgramProfile(GPP_PS_1_4, "arbfp1");
  1978. rsc->addGpuProgramProfile(GPP_PS_2_0, "arbfp1");
  1979. rsc->addGpuProgramProfile(GPP_PS_2_a, "arbfp1");
  1980. rsc->addGpuProgramProfile(GPP_PS_2_b, "arbfp1");
  1981. rsc->addGpuProgramProfile(GPP_PS_2_x, "arbfp1");
  1982. if (GLEW_NV_fragment_program_option)
  1983. {
  1984. rsc->addShaderProfile("fp30");
  1985. rsc->addGpuProgramProfile(GPP_PS_3_0, "fp30"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1986. rsc->addGpuProgramProfile(GPP_PS_3_x, "fp30");
  1987. rsc->addGpuProgramProfile(GPP_PS_4_0, "fp30");
  1988. }
  1989. if (GLEW_NV_fragment_program2)
  1990. {
  1991. rsc->addShaderProfile("fp40");
  1992. rsc->addGpuProgramProfile(GPP_PS_3_0, "fp40"); // TODO - I don't know if any of these GpuProgramProfile mappings are correct!
  1993. rsc->addGpuProgramProfile(GPP_PS_3_x, "fp40");
  1994. rsc->addGpuProgramProfile(GPP_PS_4_0, "fp40");
  1995. }
  1996. }
  1997. rsc->addShaderProfile("cg");
  1998. // NFZ - Check if GLSL is supported
  1999. if ( GLEW_VERSION_2_0 ||
  2000. (GLEW_ARB_shading_language_100 &&
  2001. GLEW_ARB_shader_objects &&
  2002. GLEW_ARB_fragment_shader &&
  2003. GLEW_ARB_vertex_shader) )
  2004. {
  2005. rsc->addShaderProfile("glsl");
  2006. }
  2007. // Check if geometry shaders are supported
  2008. if (GLEW_VERSION_2_0 &&
  2009. GLEW_EXT_geometry_shader4)
  2010. {
  2011. rsc->setCapability(RSC_GEOMETRY_PROGRAM);
  2012. rsc->addShaderProfile("nvgp4");
  2013. //Also add the CG profiles
  2014. rsc->addShaderProfile("gpu_gp");
  2015. rsc->addShaderProfile("gp4gp");
  2016. rsc->setGeometryProgramConstantBoolCount(0);
  2017. rsc->setGeometryProgramConstantIntCount(0);
  2018. GLint floatConstantCount = 0;
  2019. glGetIntegerv(GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_EXT, &floatConstantCount);
  2020. rsc->setGeometryProgramConstantFloatCount(floatConstantCount);
  2021. GLint maxOutputVertices;
  2022. glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&maxOutputVertices);
  2023. rsc->setGeometryProgramNumOutputVertices(maxOutputVertices);
  2024. }
  2025. //Check if render to vertex buffer (transform feedback in OpenGL)
  2026. if (GLEW_VERSION_2_0 &&
  2027. GLEW_NV_transform_feedback)
  2028. {
  2029. rsc->setCapability(RSC_HWRENDER_TO_VERTEX_BUFFER);
  2030. }
  2031. // Check for texture compression
  2032. if(GLEW_VERSION_1_3 || GLEW_ARB_texture_compression)
  2033. {
  2034. rsc->setCapability(RSC_TEXTURE_COMPRESSION);
  2035. // Check for dxt compression
  2036. if(GLEW_EXT_texture_compression_s3tc)
  2037. {
  2038. #if defined(__APPLE__) && defined(__PPC__)
  2039. // Apple on ATI & PPC has errors in DXT
  2040. if (mGLSupport->getGLVendor().find("ATI") == std::string::npos)
  2041. #endif
  2042. rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
  2043. }
  2044. // Check for vtc compression
  2045. if(GLEW_NV_texture_compression_vtc)
  2046. {
  2047. rsc->setCapability(RSC_TEXTURE_COMPRESSION_VTC);
  2048. }
  2049. }
  2050. // Scissor test is standard in GL 1.2 (is it emulated on some cards though?)
  2051. rsc->setCapability(RSC_SCISSOR_TEST);
  2052. // As are user clipping planes
  2053. rsc->setCapability(RSC_USER_CLIP_PLANES);
  2054. // 2-sided stencil?
  2055. if (GLEW_VERSION_2_0 || GLEW_EXT_stencil_two_side)
  2056. {
  2057. rsc->setCapability(RSC_TWO_SIDED_STENCIL);
  2058. }
  2059. // stencil wrapping?
  2060. if (GLEW_VERSION_1_4 || GLEW_EXT_stencil_wrap)
  2061. {
  2062. rsc->setCapability(RSC_STENCIL_WRAP);
  2063. }
  2064. // Check for hardware occlusion support
  2065. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  2066. {
  2067. // Some buggy driver claim that it is GL 1.5 compliant and
  2068. // not support ARB_occlusion_query
  2069. if (!GLEW_ARB_occlusion_query)
  2070. {
  2071. rsc->setCapability(RSC_GL1_5_NOHWOCCLUSION);
  2072. }
  2073. rsc->setCapability(RSC_HWOCCLUSION);
  2074. }
  2075. else if (GLEW_NV_occlusion_query)
  2076. {
  2077. // Support NV extension too for old hardware
  2078. rsc->setCapability(RSC_HWOCCLUSION);
  2079. }
  2080. // UBYTE4 always supported
  2081. rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
  2082. // Infinite far plane always supported
  2083. rsc->setCapability(RSC_INFINITE_FAR_PLANE);
  2084. // Check for non-power-of-2 texture support
  2085. if(GLEW_ARB_texture_non_power_of_two)
  2086. {
  2087. rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
  2088. }
  2089. // Check for Float textures
  2090. if(GLEW_ATI_texture_float || GLEW_ARB_texture_float)
  2091. {
  2092. rsc->setCapability(RSC_TEXTURE_FLOAT);
  2093. }
  2094. // 3D textures should be supported by GL 1.2, which is our minimum version
  2095. rsc->setCapability(RSC_TEXTURE_3D);
  2096. // Check for framebuffer object extension
  2097. if(GLEW_EXT_framebuffer_object)
  2098. {
  2099. // Probe number of draw buffers
  2100. // Only makes sense with FBO support, so probe here
  2101. if(GLEW_VERSION_2_0 ||
  2102. GLEW_ARB_draw_buffers ||
  2103. GLEW_ATI_draw_buffers)
  2104. {
  2105. GLint buffers;
  2106. glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &buffers);
  2107. rsc->setNumMultiRenderTargets(std::min<int>(buffers, (GLint)CM_MAX_MULTIPLE_RENDER_TARGETS));
  2108. rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  2109. if(!GLEW_VERSION_2_0)
  2110. {
  2111. // Before GL version 2.0, we need to get one of the extensions
  2112. if(GLEW_ARB_draw_buffers)
  2113. rsc->setCapability(RSC_FBO_ARB);
  2114. if(GLEW_ATI_draw_buffers)
  2115. rsc->setCapability(RSC_FBO_ATI);
  2116. }
  2117. // Set FBO flag for all 3 'subtypes'
  2118. rsc->setCapability(RSC_FBO);
  2119. }
  2120. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  2121. }
  2122. // Check GLSupport for PBuffer support
  2123. if(mGLSupport->supportsPBuffers())
  2124. {
  2125. // Use PBuffers
  2126. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  2127. rsc->setCapability(RSC_PBUFFER);
  2128. }
  2129. // Point size
  2130. if (GLEW_VERSION_1_4)
  2131. {
  2132. float ps;
  2133. glGetFloatv(GL_POINT_SIZE_MAX, &ps);
  2134. rsc->setMaxPointSize(ps);
  2135. }
  2136. else
  2137. {
  2138. GLint vSize[2];
  2139. glGetIntegerv(GL_POINT_SIZE_RANGE,vSize);
  2140. rsc->setMaxPointSize((float)vSize[1]);
  2141. }
  2142. // Vertex texture fetching
  2143. if (mGLSupport->checkExtension("GL_ARB_vertex_shader"))
  2144. {
  2145. GLint vUnits;
  2146. glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &vUnits);
  2147. rsc->setNumVertexTextureUnits(static_cast<UINT16>(vUnits));
  2148. if (vUnits > 0)
  2149. {
  2150. rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
  2151. }
  2152. // GL always shares vertex and fragment texture units (for now?)
  2153. rsc->setVertexTextureUnitsShared(true);
  2154. }
  2155. // Mipmap LOD biasing?
  2156. if (GLEW_VERSION_1_4 || GLEW_EXT_texture_lod_bias)
  2157. {
  2158. rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
  2159. }
  2160. // Alpha to coverage?
  2161. if (mGLSupport->checkExtension("GL_ARB_multisample"))
  2162. {
  2163. // Alpha to coverage always 'supported' when MSAA is available
  2164. // although card may ignore it if it doesn't specifically support A2C
  2165. rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
  2166. }
  2167. // Advanced blending operations
  2168. if(GLEW_VERSION_2_0)
  2169. {
  2170. rsc->setCapability(RSC_ADVANCED_BLEND_OPERATIONS);
  2171. }
  2172. return rsc;
  2173. }
  2174. /************************************************************************/
  2175. /* UTILITY */
  2176. /************************************************************************/
  2177. float GLRenderSystem::getMinimumDepthInputValue(void)
  2178. {
  2179. // Range [-1.0f, 1.0f]
  2180. return -1.0f;
  2181. }
  2182. //---------------------------------------------------------------------
  2183. float GLRenderSystem::getMaximumDepthInputValue(void)
  2184. {
  2185. // Range [-1.0f, 1.0f]
  2186. return 1.0f;
  2187. }
  2188. //---------------------------------------------------------------------
  2189. float GLRenderSystem::getHorizontalTexelOffset(void)
  2190. {
  2191. // No offset in GL
  2192. return 0.0f;
  2193. }
  2194. //---------------------------------------------------------------------
  2195. float GLRenderSystem::getVerticalTexelOffset(void)
  2196. {
  2197. // No offset in GL
  2198. return 0.0f;
  2199. }
  2200. VertexElementType GLRenderSystem::getColorVertexElementType(void) const
  2201. {
  2202. return VET_COLOUR_ABGR;
  2203. }
  2204. //---------------------------------------------------------------------
  2205. void GLRenderSystem::convertProjectionMatrix(const Matrix4& matrix,
  2206. Matrix4& dest, bool forGpuProgram)
  2207. {
  2208. // no any conversion request for OpenGL
  2209. dest = matrix;
  2210. }
  2211. }
  2212. #undef THROW_IF_NOT_RENDER_THREAD