CmGLRenderSystem.cpp 76 KB

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