CmGLRenderSystem.cpp 76 KB

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