CmGLRenderSystem.cpp 74 KB

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