CmGLRenderSystem.cpp 75 KB

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