OgreD3D9RenderSystem.cpp 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988
  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.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreD3D9RenderSystem.h"
  25. #include "OgreD3D9Prerequisites.h"
  26. #include "OgreD3D9DriverList.h"
  27. #include "OgreD3D9Driver.h"
  28. #include "OgreD3D9VideoModeList.h"
  29. #include "OgreD3D9VideoMode.h"
  30. #include "OgreD3D9RenderWindow.h"
  31. #include "OgreD3D9TextureManager.h"
  32. #include "OgreD3D9Texture.h"
  33. #include "OgreMath.h"
  34. #include "OgreD3D9HardwareBufferManager.h"
  35. #include "OgreD3D9HardwareIndexBuffer.h"
  36. #include "OgreD3D9HardwareVertexBuffer.h"
  37. #include "OgreD3D9VertexDeclaration.h"
  38. #include "OgreD3D9GpuProgram.h"
  39. #include "OgreD3D9GpuProgramManager.h"
  40. #include "OgreD3D9HLSLProgramFactory.h"
  41. #include "OgreD3D9HardwareOcclusionQuery.h"
  42. #include "OgreFrustum.h"
  43. #include "OgreD3D9MultiRenderTarget.h"
  44. #include "OgreD3D9DeviceManager.h"
  45. #include "OgreD3D9ResourceManager.h"
  46. #define FLOAT2DWORD(f) *((DWORD*)&f)
  47. namespace Ogre
  48. {
  49. D3D9RenderSystem* D3D9RenderSystem::msD3D9RenderSystem = NULL;
  50. //---------------------------------------------------------------------
  51. D3D9RenderSystem::D3D9RenderSystem( HINSTANCE hInstance )
  52. {
  53. // update singleton access pointer.
  54. msD3D9RenderSystem = this;
  55. // set the instance being passed
  56. mhInstance = hInstance;
  57. // set pointers to NULL
  58. mpD3D = NULL;
  59. mDriverList = NULL;
  60. mActiveD3DDriver = NULL;
  61. mTextureManager = NULL;
  62. mHardwareBufferManager = NULL;
  63. mGpuProgramManager = NULL;
  64. mUseNVPerfHUD = false;
  65. mHLSLProgramFactory = NULL;
  66. mDeviceManager = NULL;
  67. // Create the resource manager.
  68. mResourceManager = new D3D9ResourceManager();
  69. // init lights
  70. for(int i = 0; i < MAX_LIGHTS; i++ )
  71. mLights[i] = 0;
  72. // Create our Direct3D object
  73. if( NULL == (mpD3D = Direct3DCreate9(D3D_SDK_VERSION)) )
  74. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Failed to create Direct3D9 object", "D3D9RenderSystem::D3D9RenderSystem" );
  75. // set config options defaults
  76. initConfigOptions();
  77. // fsaa options
  78. mFSAAHint = "";
  79. mFSAASamples = 0;
  80. // set stages desc. to defaults
  81. for (size_t n = 0; n < OGRE_MAX_TEXTURE_LAYERS; n++)
  82. {
  83. mTexStageDesc[n].autoTexCoordType = TEXCALC_NONE;
  84. mTexStageDesc[n].coordIndex = 0;
  85. mTexStageDesc[n].texType = D3D9Mappings::D3D_TEX_TYPE_NORMAL;
  86. mTexStageDesc[n].pTex = 0;
  87. mTexStageDesc[n].pVertexTex = 0;
  88. }
  89. mLastVertexSourceCount = 0;
  90. mCurrentLights.clear();
  91. // Enumerate events
  92. mEventNames.push_back("DeviceLost");
  93. mEventNames.push_back("DeviceRestored");
  94. }
  95. //---------------------------------------------------------------------
  96. D3D9RenderSystem::~D3D9RenderSystem()
  97. {
  98. shutdown();
  99. // Deleting the HLSL program factory
  100. if (mHLSLProgramFactory)
  101. {
  102. // TODO PORT - If i'll be supporting factories in this way, enable this
  103. //// Remove from manager safely
  104. //if (HighLevelGpuProgramManager::getSingletonPtr())
  105. // HighLevelGpuProgramManager::getSingleton().removeFactory(mHLSLProgramFactory);
  106. delete mHLSLProgramFactory;
  107. mHLSLProgramFactory = 0;
  108. }
  109. SAFE_RELEASE( mpD3D );
  110. SAFE_DELETE ( mResourceManager );
  111. msD3D9RenderSystem = NULL;
  112. }
  113. //---------------------------------------------------------------------
  114. const String& D3D9RenderSystem::getName() const
  115. {
  116. static String strName( "Direct3D9 Rendering Subsystem");
  117. return strName;
  118. }
  119. //---------------------------------------------------------------------
  120. D3D9DriverList* D3D9RenderSystem::getDirect3DDrivers()
  121. {
  122. if( !mDriverList )
  123. mDriverList = new D3D9DriverList();
  124. return mDriverList;
  125. }
  126. //---------------------------------------------------------------------
  127. bool D3D9RenderSystem::_checkMultiSampleQuality(D3DMULTISAMPLE_TYPE type, DWORD *outQuality, D3DFORMAT format, UINT adapterNum, D3DDEVTYPE deviceType, BOOL fullScreen)
  128. {
  129. HRESULT hr;
  130. hr = mpD3D->CheckDeviceMultiSampleType(
  131. adapterNum,
  132. deviceType,
  133. format,
  134. fullScreen,
  135. type,
  136. outQuality);
  137. if (SUCCEEDED(hr))
  138. return true;
  139. else
  140. return false;
  141. }
  142. //---------------------------------------------------------------------
  143. void D3D9RenderSystem::initConfigOptions()
  144. {
  145. D3D9DriverList* driverList;
  146. D3D9Driver* driver;
  147. ConfigOption optDevice;
  148. ConfigOption optVideoMode;
  149. ConfigOption optFullScreen;
  150. ConfigOption optVSync;
  151. ConfigOption optVSyncInterval;
  152. ConfigOption optAA;
  153. ConfigOption optFPUMode;
  154. ConfigOption optNVPerfHUD;
  155. ConfigOption optSRGB;
  156. ConfigOption optResourceCeationPolicy;
  157. driverList = this->getDirect3DDrivers();
  158. optDevice.name = "Rendering Device";
  159. optDevice.currentValue.clear();
  160. optDevice.possibleValues.clear();
  161. optDevice.immutable = false;
  162. optVideoMode.name = "Video Mode";
  163. optVideoMode.currentValue = "800 x 600 @ 32-bit colour";
  164. optVideoMode.immutable = false;
  165. optFullScreen.name = "Full Screen";
  166. optFullScreen.possibleValues.push_back( "Yes" );
  167. optFullScreen.possibleValues.push_back( "No" );
  168. optFullScreen.currentValue = "Yes";
  169. optFullScreen.immutable = false;
  170. optResourceCeationPolicy.name = "Resource Creation Policy";
  171. optResourceCeationPolicy.possibleValues.push_back( "Create on all devices" );
  172. optResourceCeationPolicy.possibleValues.push_back( "Create on active device" );
  173. if (mResourceManager->getCreationPolicy() == RCP_CREATE_ON_ACTIVE_DEVICE)
  174. optResourceCeationPolicy.currentValue = "Create on active device";
  175. else if (mResourceManager->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  176. optResourceCeationPolicy.currentValue = "Create on all devices";
  177. else
  178. optResourceCeationPolicy.currentValue = "N/A";
  179. optResourceCeationPolicy.immutable = false;
  180. for( unsigned j=0; j < driverList->count(); j++ )
  181. {
  182. driver = driverList->item(j);
  183. optDevice.possibleValues.push_back( driver->DriverDescription() );
  184. // Make first one default
  185. if( j==0 )
  186. optDevice.currentValue = driver->DriverDescription();
  187. }
  188. optVSync.name = "VSync";
  189. optVSync.immutable = false;
  190. optVSync.possibleValues.push_back( "Yes" );
  191. optVSync.possibleValues.push_back( "No" );
  192. optVSync.currentValue = "No";
  193. optVSyncInterval.name = "VSync Interval";
  194. optVSyncInterval.immutable = false;
  195. optVSyncInterval.possibleValues.push_back( "1" );
  196. optVSyncInterval.possibleValues.push_back( "2" );
  197. optVSyncInterval.possibleValues.push_back( "3" );
  198. optVSyncInterval.possibleValues.push_back( "4" );
  199. optVSyncInterval.currentValue = "1";
  200. optAA.name = "FSAA";
  201. optAA.immutable = false;
  202. optAA.possibleValues.push_back( "None" );
  203. optAA.currentValue = "None";
  204. optFPUMode.name = "Floating-point mode";
  205. #if OGRE_DOUBLE_PRECISION
  206. optFPUMode.currentValue = "Consistent";
  207. #else
  208. optFPUMode.currentValue = "Fastest";
  209. #endif
  210. optFPUMode.possibleValues.clear();
  211. optFPUMode.possibleValues.push_back("Fastest");
  212. optFPUMode.possibleValues.push_back("Consistent");
  213. optFPUMode.immutable = false;
  214. optNVPerfHUD.currentValue = "No";
  215. optNVPerfHUD.immutable = false;
  216. optNVPerfHUD.name = "Allow NVPerfHUD";
  217. optNVPerfHUD.possibleValues.push_back( "Yes" );
  218. optNVPerfHUD.possibleValues.push_back( "No" );
  219. // SRGB on auto window
  220. optSRGB.name = "sRGB Gamma Conversion";
  221. optSRGB.possibleValues.push_back("Yes");
  222. optSRGB.possibleValues.push_back("No");
  223. optSRGB.currentValue = "No";
  224. optSRGB.immutable = false;
  225. mOptions[optDevice.name] = optDevice;
  226. mOptions[optVideoMode.name] = optVideoMode;
  227. mOptions[optFullScreen.name] = optFullScreen;
  228. mOptions[optVSync.name] = optVSync;
  229. mOptions[optVSyncInterval.name] = optVSyncInterval;
  230. mOptions[optAA.name] = optAA;
  231. mOptions[optFPUMode.name] = optFPUMode;
  232. mOptions[optNVPerfHUD.name] = optNVPerfHUD;
  233. mOptions[optSRGB.name] = optSRGB;
  234. mOptions[optResourceCeationPolicy.name] = optResourceCeationPolicy;
  235. refreshD3DSettings();
  236. }
  237. //---------------------------------------------------------------------
  238. void D3D9RenderSystem::refreshD3DSettings()
  239. {
  240. ConfigOption* optVideoMode;
  241. D3D9Driver* driver = 0;
  242. D3D9VideoMode* videoMode;
  243. ConfigOptionMap::iterator opt = mOptions.find( "Rendering Device" );
  244. if( opt != mOptions.end() )
  245. {
  246. for( unsigned j=0; j < getDirect3DDrivers()->count(); j++ )
  247. {
  248. D3D9Driver* curDriver = getDirect3DDrivers()->item(j);
  249. if( curDriver->DriverDescription() == opt->second.currentValue )
  250. {
  251. driver = curDriver;
  252. break;
  253. }
  254. }
  255. if (driver)
  256. {
  257. opt = mOptions.find( "Video Mode" );
  258. optVideoMode = &opt->second;
  259. optVideoMode->possibleValues.clear();
  260. // get vide modes for this device
  261. for( unsigned k=0; k < driver->getVideoModeList()->count(); k++ )
  262. {
  263. videoMode = driver->getVideoModeList()->item( k );
  264. optVideoMode->possibleValues.push_back( videoMode->getDescription() );
  265. }
  266. // Reset video mode to default if previous doesn't avail in new possible values
  267. StringVector::const_iterator itValue =
  268. std::find(optVideoMode->possibleValues.begin(),
  269. optVideoMode->possibleValues.end(),
  270. optVideoMode->currentValue);
  271. if (itValue == optVideoMode->possibleValues.end())
  272. {
  273. optVideoMode->currentValue = "800 x 600 @ 32-bit colour";
  274. }
  275. // Also refresh FSAA options
  276. refreshFSAAOptions();
  277. }
  278. }
  279. }
  280. //---------------------------------------------------------------------
  281. void D3D9RenderSystem::setConfigOption( const String &name, const String &value )
  282. {
  283. bool viewModeChanged = false;
  284. // Find option
  285. ConfigOptionMap::iterator it = mOptions.find( name );
  286. // Update
  287. if( it != mOptions.end() )
  288. it->second.currentValue = value;
  289. else
  290. {
  291. StringUtil::StrStreamType str;
  292. str << "Option named '" << name << "' does not exist.";
  293. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, str.str(), "D3D9RenderSystem::setConfigOption" );
  294. }
  295. // Refresh other options if D3DDriver changed
  296. if( name == "Rendering Device" )
  297. refreshD3DSettings();
  298. if( name == "Full Screen" )
  299. {
  300. // Video mode is applicable
  301. it = mOptions.find( "Video Mode" );
  302. if (it->second.currentValue.empty())
  303. {
  304. it->second.currentValue = "800 x 600 @ 32-bit colour";
  305. viewModeChanged = true;
  306. }
  307. }
  308. if( name == "FSAA" )
  309. {
  310. StringVector values = StringUtil::split(value, " ", 1);
  311. mFSAASamples = StringConverter::parseUnsignedInt(values[0]);
  312. if (values.size() > 1)
  313. mFSAAHint = values[1];
  314. }
  315. if( name == "VSync" )
  316. {
  317. if (value == "Yes")
  318. mVSync = true;
  319. else
  320. mVSync = false;
  321. }
  322. if( name == "VSync Interval" )
  323. {
  324. mVSyncInterval = StringConverter::parseUnsignedInt(value);
  325. }
  326. if( name == "Allow NVPerfHUD" )
  327. {
  328. if (value == "Yes")
  329. mUseNVPerfHUD = true;
  330. else
  331. mUseNVPerfHUD = false;
  332. }
  333. if (viewModeChanged || name == "Video Mode")
  334. {
  335. refreshFSAAOptions();
  336. }
  337. if (name == "Resource Creation Policy")
  338. {
  339. if (value == "Create on active device")
  340. mResourceManager->setCreationPolicy(RCP_CREATE_ON_ACTIVE_DEVICE);
  341. else if (value == "Create on all devices")
  342. mResourceManager->setCreationPolicy(RCP_CREATE_ON_ALL_DEVICES);
  343. }
  344. }
  345. //---------------------------------------------------------------------
  346. void D3D9RenderSystem::refreshFSAAOptions()
  347. {
  348. ConfigOptionMap::iterator it = mOptions.find( "FSAA" );
  349. ConfigOption* optFSAA = &it->second;
  350. optFSAA->possibleValues.clear();
  351. optFSAA->possibleValues.push_back("0");
  352. it = mOptions.find("Rendering Device");
  353. D3D9Driver *driver = getDirect3DDrivers()->item(it->second.currentValue);
  354. if (driver)
  355. {
  356. it = mOptions.find("Video Mode");
  357. D3D9VideoMode *videoMode = driver->getVideoModeList()->item(it->second.currentValue);
  358. if (videoMode)
  359. {
  360. DWORD numLevels = 0;
  361. bool bOK;
  362. for (unsigned int n = 2; n < 25; n++)
  363. {
  364. bOK = this->_checkMultiSampleQuality(
  365. (D3DMULTISAMPLE_TYPE)n,
  366. &numLevels,
  367. videoMode->getFormat(),
  368. driver->getAdapterNumber(),
  369. D3DDEVTYPE_HAL,
  370. TRUE);
  371. if (bOK)
  372. {
  373. optFSAA->possibleValues.push_back(StringConverter::toString(n));
  374. if (n >= 8)
  375. optFSAA->possibleValues.push_back(StringConverter::toString(n) + " [Quality]");
  376. }
  377. }
  378. }
  379. }
  380. // Reset FSAA to none if previous doesn't avail in new possible values
  381. StringVector::const_iterator itValue =
  382. std::find(optFSAA->possibleValues.begin(),
  383. optFSAA->possibleValues.end(),
  384. optFSAA->currentValue);
  385. if (itValue == optFSAA->possibleValues.end())
  386. {
  387. optFSAA->currentValue = "0";
  388. }
  389. }
  390. //---------------------------------------------------------------------
  391. String D3D9RenderSystem::validateConfigOptions()
  392. {
  393. ConfigOptionMap::iterator it;
  394. // check if video mode is selected
  395. it = mOptions.find( "Video Mode" );
  396. if (it->second.currentValue.empty())
  397. return "A video mode must be selected.";
  398. it = mOptions.find( "Rendering Device" );
  399. bool foundDriver = false;
  400. D3D9DriverList* driverList = getDirect3DDrivers();
  401. for( ushort j=0; j < driverList->count(); j++ )
  402. {
  403. if( driverList->item(j)->DriverDescription() == it->second.currentValue )
  404. {
  405. foundDriver = true;
  406. break;
  407. }
  408. }
  409. if (!foundDriver)
  410. {
  411. // Just pick the first driver
  412. setConfigOption("Rendering Device", driverList->item(0)->DriverDescription());
  413. return "Your DirectX driver name has changed since the last time you ran OGRE; "
  414. "the 'Rendering Device' has been changed.";
  415. }
  416. it = mOptions.find( "VSync" );
  417. if( it->second.currentValue == "Yes" )
  418. mVSync = true;
  419. else
  420. mVSync = false;
  421. return StringUtil::BLANK;
  422. }
  423. //---------------------------------------------------------------------
  424. RenderWindow* D3D9RenderSystem::_initialise( bool autoCreateWindow, const String& windowTitle )
  425. {
  426. RenderWindow* autoWindow = NULL;
  427. // Init using current settings
  428. mActiveD3DDriver = NULL;
  429. ConfigOptionMap::iterator opt = mOptions.find( "Rendering Device" );
  430. for( uint j=0; j < getDirect3DDrivers()->count(); j++ )
  431. {
  432. if( getDirect3DDrivers()->item(j)->DriverDescription() == opt->second.currentValue )
  433. {
  434. mActiveD3DDriver = getDirect3DDrivers()->item(j);
  435. break;
  436. }
  437. }
  438. if( !mActiveD3DDriver )
  439. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Problems finding requested Direct3D driver!", "D3D9RenderSystem::initialise" );
  440. // get driver version
  441. mDriverVersion.major = HIWORD(mActiveD3DDriver->getAdapterIdentifier().DriverVersion.HighPart);
  442. mDriverVersion.minor = LOWORD(mActiveD3DDriver->getAdapterIdentifier().DriverVersion.HighPart);
  443. mDriverVersion.release = HIWORD(mActiveD3DDriver->getAdapterIdentifier().DriverVersion.LowPart);
  444. mDriverVersion.build = LOWORD(mActiveD3DDriver->getAdapterIdentifier().DriverVersion.LowPart);
  445. // Create the device manager.
  446. mDeviceManager = new D3D9DeviceManager();
  447. // Create the texture manager for use by others
  448. // TODO PORT - Texture manager is null! This is bound to cause problems but I need to get this compiling
  449. //mTextureManager = new D3D9TextureManager();
  450. // Also create hardware buffer manager
  451. mHardwareBufferManager = new D3D9HardwareBufferManager();
  452. // Create the GPU program manager
  453. mGpuProgramManager = new D3D9GpuProgramManager();
  454. // Create & register HLSL factory
  455. mHLSLProgramFactory = new D3D9HLSLProgramFactory();
  456. if( autoCreateWindow )
  457. {
  458. bool fullScreen;
  459. opt = mOptions.find( "Full Screen" );
  460. if( opt == mOptions.end() )
  461. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Can't find full screen option!", "D3D9RenderSystem::initialise" );
  462. fullScreen = opt->second.currentValue == "Yes";
  463. D3D9VideoMode* videoMode = NULL;
  464. unsigned int width, height;
  465. String temp;
  466. opt = mOptions.find( "Video Mode" );
  467. if( opt == mOptions.end() )
  468. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Can't find Video Mode option!", "D3D9RenderSystem::initialise" );
  469. // The string we are manipulating looks like this :width x height @ colourDepth
  470. // Pull out the colour depth by getting what comes after the @ and a space
  471. String colourDepth = opt->second.currentValue.substr(opt->second.currentValue.rfind('@')+1);
  472. // Now we know that the width starts a 0, so if we can find the end we can parse that out
  473. String::size_type widthEnd = opt->second.currentValue.find(' ');
  474. // we know that the height starts 3 characters after the width and goes until the next space
  475. String::size_type heightEnd = opt->second.currentValue.find(' ', widthEnd+3);
  476. // Now we can parse out the values
  477. width = StringConverter::parseInt(opt->second.currentValue.substr(0, widthEnd));
  478. height = StringConverter::parseInt(opt->second.currentValue.substr(widthEnd+3, heightEnd));
  479. for( unsigned j=0; j < mActiveD3DDriver->getVideoModeList()->count(); j++ )
  480. {
  481. temp = mActiveD3DDriver->getVideoModeList()->item(j)->getDescription();
  482. // In full screen we only want to allow supported resolutions, so temp and opt->second.currentValue need to
  483. // match exactly, but in windowed mode we can allow for arbitrary window sized, so we only need
  484. // to match the colour values
  485. if(fullScreen && (temp == opt->second.currentValue) ||
  486. !fullScreen && (temp.substr(temp.rfind('@')+1) == colourDepth))
  487. {
  488. videoMode = mActiveD3DDriver->getVideoModeList()->item(j);
  489. break;
  490. }
  491. }
  492. if( !videoMode )
  493. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Can't find requested video mode.", "D3D9RenderSystem::initialise" );
  494. // sRGB window option
  495. bool hwGamma = false;
  496. opt = mOptions.find( "sRGB Gamma Conversion" );
  497. if( opt == mOptions.end() )
  498. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Can't find sRGB option!", "D3D9RenderSystem::initialise" );
  499. hwGamma = opt->second.currentValue == "Yes";
  500. NameValuePairList miscParams;
  501. miscParams["colourDepth"] = StringConverter::toString(videoMode->getColourDepth());
  502. miscParams["FSAA"] = StringConverter::toString(mFSAASamples);
  503. miscParams["FSAAHint"] = mFSAAHint;
  504. miscParams["vsync"] = StringConverter::toString(mVSync);
  505. miscParams["vsyncInterval"] = StringConverter::toString(mVSyncInterval);
  506. miscParams["useNVPerfHUD"] = StringConverter::toString(mUseNVPerfHUD);
  507. miscParams["gamma"] = StringConverter::toString(hwGamma);
  508. miscParams["monitorIndex"] = StringConverter::toString(static_cast<int>(mActiveD3DDriver->getAdapterNumber()));
  509. autoWindow = _createRenderWindow( windowTitle, width, height,
  510. fullScreen, &miscParams );
  511. // If we have 16bit depth buffer enable w-buffering.
  512. assert( autoWindow );
  513. if ( autoWindow->getColourDepth() == 16 )
  514. {
  515. mWBuffer = true;
  516. }
  517. else
  518. {
  519. mWBuffer = false;
  520. }
  521. }
  522. // call superclass method
  523. RenderSystem::_initialise( autoCreateWindow );
  524. return autoWindow;
  525. }
  526. //---------------------------------------------------------------------
  527. void D3D9RenderSystem::reinitialise()
  528. {
  529. this->shutdown();
  530. this->_initialise( true );
  531. }
  532. //---------------------------------------------------------------------
  533. void D3D9RenderSystem::shutdown()
  534. {
  535. RenderSystem::shutdown();
  536. SAFE_DELETE( mDeviceManager );
  537. SAFE_DELETE( mDriverList );
  538. mActiveD3DDriver = NULL;
  539. SAFE_DELETE( mTextureManager );
  540. SAFE_DELETE( mHardwareBufferManager );
  541. SAFE_DELETE( mGpuProgramManager );
  542. }
  543. //---------------------------------------------------------------------
  544. RenderWindow* D3D9RenderSystem::_createRenderWindow(const String &name,
  545. unsigned int width, unsigned int height, bool fullScreen,
  546. const NameValuePairList *miscParams)
  547. {
  548. // Log a message
  549. StringStream ss;
  550. ss << "D3D9RenderSystem::_createRenderWindow \"" << name << "\", " <<
  551. width << "x" << height << " ";
  552. if(fullScreen)
  553. ss << "fullscreen ";
  554. else
  555. ss << "windowed ";
  556. if(miscParams)
  557. {
  558. ss << " miscParams: ";
  559. NameValuePairList::const_iterator it;
  560. for(it=miscParams->begin(); it!=miscParams->end(); ++it)
  561. {
  562. ss << it->first << "=" << it->second << " ";
  563. }
  564. }
  565. String msg;
  566. // Make sure we don't already have a render target of the
  567. // same name as the one supplied
  568. if( mRenderTargets.find( name ) != mRenderTargets.end() )
  569. {
  570. msg = "A render target of the same name '" + name + "' already "
  571. "exists. You cannot create a new window with this name.";
  572. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, msg, "D3D9RenderSystem::_createRenderWindow" );
  573. }
  574. D3D9RenderWindow* renderWindow = new D3D9RenderWindow(mhInstance);
  575. renderWindow->create(name, width, height, fullScreen, miscParams);
  576. mResourceManager->lockDeviceAccess();
  577. try
  578. {
  579. mDeviceManager->linkRenderWindow(renderWindow);
  580. }
  581. catch (const Ogre::RenderingAPIException&)
  582. {
  583. // after catching the exception, clean up
  584. mResourceManager->unlockDeviceAccess();
  585. renderWindow->destroy();
  586. // re-throw
  587. throw;
  588. }
  589. mResourceManager->unlockDeviceAccess();
  590. mRenderWindows.push_back(renderWindow);
  591. updateRenderSystemCapabilities(renderWindow);
  592. attachRenderTarget( *renderWindow );
  593. return renderWindow;
  594. }
  595. //---------------------------------------------------------------------
  596. bool D3D9RenderSystem::_createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
  597. RenderWindowList& createdWindows)
  598. {
  599. // Call base render system method.
  600. if (false == RenderSystem::_createRenderWindows(renderWindowDescriptions, createdWindows))
  601. return false;
  602. // Simply call _createRenderWindow in a loop.
  603. for (size_t i = 0; i < renderWindowDescriptions.size(); ++i)
  604. {
  605. const RenderWindowDescription& curRenderWindowDescription = renderWindowDescriptions[i];
  606. RenderWindow* curWindow = NULL;
  607. curWindow = _createRenderWindow(curRenderWindowDescription.name,
  608. curRenderWindowDescription.width,
  609. curRenderWindowDescription.height,
  610. curRenderWindowDescription.useFullScreen,
  611. &curRenderWindowDescription.miscParams);
  612. createdWindows.push_back(curWindow);
  613. }
  614. return true;
  615. }
  616. //---------------------------------------------------------------------
  617. RenderSystemCapabilities* D3D9RenderSystem::updateRenderSystemCapabilities(D3D9RenderWindow* renderWindow)
  618. {
  619. RenderSystemCapabilities* rsc = mRealCapabilities;
  620. if (rsc == NULL)
  621. rsc = new RenderSystemCapabilities();
  622. rsc->setCategoryRelevant(CAPS_CATEGORY_D3D9, true);
  623. rsc->setDriverVersion(mDriverVersion);
  624. rsc->setDeviceName(mActiveD3DDriver->DriverDescription());
  625. rsc->setRenderSystemName(getName());
  626. // Supports fixed-function
  627. rsc->setCapability(RSC_FIXED_FUNCTION);
  628. // Init caps to maximum.
  629. rsc->setNumTextureUnits(1024);
  630. rsc->setCapability(RSC_ANISOTROPY);
  631. rsc->setCapability(RSC_AUTOMIPMAP);
  632. rsc->setCapability(RSC_DOT3);
  633. rsc->setCapability(RSC_CUBEMAPPING);
  634. rsc->setCapability(RSC_SCISSOR_TEST);
  635. rsc->setCapability(RSC_TWO_SIDED_STENCIL);
  636. rsc->setCapability(RSC_STENCIL_WRAP);
  637. rsc->setCapability(RSC_HWOCCLUSION);
  638. rsc->setCapability(RSC_USER_CLIP_PLANES);
  639. rsc->setCapability(RSC_VERTEX_FORMAT_UBYTE4);
  640. rsc->setCapability(RSC_TEXTURE_3D);
  641. rsc->setCapability(RSC_NON_POWER_OF_2_TEXTURES);
  642. rsc->setNonPOW2TexturesLimited(false);
  643. rsc->setNumMultiRenderTargets(OGRE_MAX_MULTIPLE_RENDER_TARGETS);
  644. rsc->setCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  645. rsc->setCapability(RSC_POINT_SPRITES);
  646. rsc->setCapability(RSC_POINT_EXTENDED_PARAMETERS);
  647. rsc->setMaxPointSize(10.0);
  648. rsc->setCapability(RSC_MIPMAP_LOD_BIAS);
  649. rsc->setCapability(RSC_PERSTAGECONSTANT);
  650. rsc->setCapability(RSC_HWSTENCIL);
  651. rsc->setStencilBufferBitDepth(8);
  652. rsc->setCapability(RSC_ADVANCED_BLEND_OPERATIONS);
  653. for (uint i=0; i < mDeviceManager->getDeviceCount(); ++i)
  654. {
  655. D3D9Device* device = mDeviceManager->getDevice(i);
  656. IDirect3DDevice9* d3d9Device = device->getD3D9Device();
  657. IDirect3DSurface9* pSurf;
  658. // Check for hardware stencil support
  659. d3d9Device->GetDepthStencilSurface(&pSurf);
  660. if (pSurf != NULL)
  661. {
  662. D3DSURFACE_DESC surfDesc;
  663. pSurf->GetDesc(&surfDesc);
  664. pSurf->Release();
  665. if (surfDesc.Format != D3DFMT_D15S1 &&
  666. surfDesc.Format != D3DFMT_D24S8 &&
  667. surfDesc.Format != D3DFMT_D24X4S4 &&
  668. surfDesc.Format != D3DFMT_D24FS8)
  669. rsc->unsetCapability(RSC_HWSTENCIL);
  670. }
  671. // Check for hardware occlusion support
  672. HRESULT hr = d3d9Device->CreateQuery(D3DQUERYTYPE_OCCLUSION, NULL);
  673. if (FAILED(hr))
  674. rsc->unsetCapability(RSC_HWOCCLUSION);
  675. }
  676. // Update RS caps using the minimum value found in adapter list.
  677. for (unsigned int i=0; i < mDriverList->count(); ++i)
  678. {
  679. D3D9Driver* pCurDriver = mDriverList->item(i);
  680. const D3DCAPS9& rkCurCaps = pCurDriver->getD3D9DeviceCaps();
  681. if (rkCurCaps.MaxSimultaneousTextures < rsc->getNumTextureUnits())
  682. {
  683. rsc->setNumTextureUnits(static_cast<ushort>(rkCurCaps.MaxSimultaneousTextures));
  684. }
  685. // Check for Anisotropy.
  686. if (rkCurCaps.MaxAnisotropy <= 1)
  687. rsc->unsetCapability(RSC_ANISOTROPY);
  688. // Check automatic mipmap generation.
  689. if ((rkCurCaps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP) == 0)
  690. rsc->unsetCapability(RSC_AUTOMIPMAP);
  691. // Check Dot product 3.
  692. if ((rkCurCaps.TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3) == 0)
  693. rsc->unsetCapability(RSC_DOT3);
  694. // Scissor test
  695. if ((rkCurCaps.RasterCaps & D3DPRASTERCAPS_SCISSORTEST) == 0)
  696. rsc->unsetCapability(RSC_SCISSOR_TEST);
  697. // Two-sided stencil
  698. if ((rkCurCaps.StencilCaps & D3DSTENCILCAPS_TWOSIDED) == 0)
  699. rsc->unsetCapability(RSC_TWO_SIDED_STENCIL);
  700. // stencil wrap
  701. if ((rkCurCaps.StencilCaps & D3DSTENCILCAPS_INCR) == 0 ||
  702. (rkCurCaps.StencilCaps & D3DSTENCILCAPS_DECR) == 0)
  703. rsc->unsetCapability(RSC_STENCIL_WRAP);
  704. // User clip planes
  705. if (rkCurCaps.MaxUserClipPlanes == 0)
  706. rsc->unsetCapability(RSC_USER_CLIP_PLANES);
  707. // UBYTE4 type?
  708. if ((rkCurCaps.DeclTypes & D3DDTCAPS_UBYTE4) == 0)
  709. rsc->unsetCapability(RSC_VERTEX_FORMAT_UBYTE4);
  710. // Check cube map support.
  711. if ((rkCurCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP) == 0)
  712. rsc->unsetCapability(RSC_CUBEMAPPING);
  713. // 3D textures?
  714. if ((rkCurCaps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP) == 0)
  715. rsc->unsetCapability(RSC_TEXTURE_3D);
  716. if (rkCurCaps.TextureCaps & D3DPTEXTURECAPS_POW2)
  717. {
  718. // Conditional support for non POW2
  719. if (rkCurCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
  720. rsc->setNonPOW2TexturesLimited(true);
  721. // Only power of 2 supported.
  722. else
  723. rsc->unsetCapability(RSC_NON_POWER_OF_2_TEXTURES);
  724. }
  725. // Number of render targets
  726. if (rkCurCaps.NumSimultaneousRTs < rsc->getNumMultiRenderTargets())
  727. {
  728. rsc->setNumMultiRenderTargets(std::min((ushort)rkCurCaps.NumSimultaneousRTs, (ushort)OGRE_MAX_MULTIPLE_RENDER_TARGETS));
  729. }
  730. if((rkCurCaps.PrimitiveMiscCaps & D3DPMISCCAPS_MRTINDEPENDENTBITDEPTHS) == 0)
  731. {
  732. rsc->unsetCapability(RSC_MRT_DIFFERENT_BIT_DEPTHS);
  733. }
  734. // Point sprites
  735. if (rkCurCaps.MaxPointSize <= 1.0f)
  736. {
  737. rsc->unsetCapability(RSC_POINT_SPRITES);
  738. // sprites and extended parameters go together in D3D
  739. rsc->unsetCapability(RSC_POINT_EXTENDED_PARAMETERS);
  740. }
  741. // Take the minimum point size.
  742. if (rkCurCaps.MaxPointSize < rsc->getMaxPointSize())
  743. rsc->setMaxPointSize(rkCurCaps.MaxPointSize);
  744. // Mipmap LOD biasing?
  745. if ((rkCurCaps.RasterCaps & D3DPRASTERCAPS_MIPMAPLODBIAS) == 0)
  746. rsc->unsetCapability(RSC_MIPMAP_LOD_BIAS);
  747. // Do we support per-stage src_manual constants?
  748. // HACK - ATI drivers seem to be buggy and don't support per-stage constants properly?
  749. // TODO: move this to RSC
  750. if((rkCurCaps.PrimitiveMiscCaps & D3DPMISCCAPS_PERSTAGECONSTANT) == 0)
  751. rsc->unsetCapability(RSC_PERSTAGECONSTANT);
  752. // Advanced blend operations? min max subtract rev
  753. if((rkCurCaps.PrimitiveMiscCaps & D3DPMISCCAPS_BLENDOP) == 0)
  754. rsc->unsetCapability(RSC_ADVANCED_BLEND_OPERATIONS);
  755. }
  756. // Blending between stages supported
  757. rsc->setCapability(RSC_BLENDING);
  758. // We always support compression, D3DX will decompress if device does not support
  759. rsc->setCapability(RSC_TEXTURE_COMPRESSION);
  760. rsc->setCapability(RSC_TEXTURE_COMPRESSION_DXT);
  761. // We always support VBOs
  762. rsc->setCapability(RSC_VBO);
  763. convertVertexShaderCaps(rsc);
  764. convertPixelShaderCaps(rsc);
  765. // Adapter details
  766. const D3DADAPTER_IDENTIFIER9& adapterID = mActiveD3DDriver->getAdapterIdentifier();
  767. // determine vendor
  768. // Full list of vendors here: http://www.pcidatabase.com/vendors.php?sort=id
  769. switch(adapterID.VendorId)
  770. {
  771. case 0x10DE:
  772. rsc->setVendor(GPU_NVIDIA);
  773. break;
  774. case 0x1002:
  775. rsc->setVendor(GPU_ATI);
  776. break;
  777. case 0x163C:
  778. case 0x8086:
  779. rsc->setVendor(GPU_INTEL);
  780. break;
  781. case 0x5333:
  782. rsc->setVendor(GPU_S3);
  783. break;
  784. case 0x3D3D:
  785. rsc->setVendor(GPU_3DLABS);
  786. break;
  787. case 0x102B:
  788. rsc->setVendor(GPU_MATROX);
  789. break;
  790. case 0x1039:
  791. rsc->setVendor(GPU_SIS);
  792. break;
  793. default:
  794. rsc->setVendor(GPU_UNKNOWN);
  795. break;
  796. };
  797. // Infinite projection?
  798. // We have no capability for this, so we have to base this on our
  799. // experience and reports from users
  800. // Non-vertex program capable hardware does not appear to support it
  801. if (rsc->hasCapability(RSC_VERTEX_PROGRAM))
  802. {
  803. // GeForce4 Ti (and presumably GeForce3) does not
  804. // render infinite projection properly, even though it does in GL
  805. // So exclude all cards prior to the FX range from doing infinite
  806. if (rsc->getVendor() != GPU_NVIDIA || // not nVidia
  807. !((adapterID.DeviceId >= 0x200 && adapterID.DeviceId <= 0x20F) || //gf3
  808. (adapterID.DeviceId >= 0x250 && adapterID.DeviceId <= 0x25F) || //gf4ti
  809. (adapterID.DeviceId >= 0x280 && adapterID.DeviceId <= 0x28F) || //gf4ti
  810. (adapterID.DeviceId >= 0x170 && adapterID.DeviceId <= 0x18F) || //gf4 go
  811. (adapterID.DeviceId >= 0x280 && adapterID.DeviceId <= 0x28F))) //gf4ti go
  812. {
  813. rsc->setCapability(RSC_INFINITE_FAR_PLANE);
  814. }
  815. }
  816. // We always support rendertextures bigger than the frame buffer
  817. rsc->setCapability(RSC_HWRENDER_TO_TEXTURE);
  818. // Determine if any floating point texture format is supported
  819. D3DFORMAT floatFormats[6] = {D3DFMT_R16F, D3DFMT_G16R16F,
  820. D3DFMT_A16B16G16R16F, D3DFMT_R32F, D3DFMT_G32R32F,
  821. D3DFMT_A32B32G32R32F};
  822. IDirect3DSurface9* bbSurf;
  823. renderWindow->getCustomAttribute("DDBACKBUFFER", &bbSurf);
  824. D3DSURFACE_DESC bbSurfDesc;
  825. bbSurf->GetDesc(&bbSurfDesc);
  826. for (int i = 0; i < 6; ++i)
  827. {
  828. if (SUCCEEDED(mpD3D->CheckDeviceFormat(mActiveD3DDriver->getAdapterNumber(),
  829. D3DDEVTYPE_HAL, bbSurfDesc.Format,
  830. 0, D3DRTYPE_TEXTURE, floatFormats[i])))
  831. {
  832. rsc->setCapability(RSC_TEXTURE_FLOAT);
  833. break;
  834. }
  835. }
  836. // TODO: make convertVertex/Fragment fill in rsc
  837. // TODO: update the below line to use rsc
  838. // Vertex textures
  839. if (rsc->isShaderProfileSupported("vs_3_0"))
  840. {
  841. // Run through all the texture formats looking for any which support
  842. // vertex texture fetching. Must have at least one!
  843. // All ATI Radeon up to X1n00 say they support vs_3_0,
  844. // but they support no texture formats for vertex texture fetch (cheaters!)
  845. if (checkVertexTextureFormats(renderWindow))
  846. {
  847. rsc->setCapability(RSC_VERTEX_TEXTURE_FETCH);
  848. // always 4 vertex texture units in vs_3_0, and never shared
  849. rsc->setNumVertexTextureUnits(4);
  850. rsc->setVertexTextureUnitsShared(false);
  851. }
  852. }
  853. // Check alpha to coverage support
  854. // this varies per vendor! But at least SM3 is required
  855. if (rsc->isShaderProfileSupported("ps_3_0"))
  856. {
  857. // NVIDIA needs a separate check
  858. if (rsc->getVendor() == GPU_NVIDIA)
  859. {
  860. if (mpD3D->CheckDeviceFormat(
  861. D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0,D3DRTYPE_SURFACE,
  862. (D3DFORMAT)MAKEFOURCC('A', 'T', 'O', 'C')) == S_OK)
  863. {
  864. rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
  865. }
  866. }
  867. else if (rsc->getVendor() == GPU_ATI)
  868. {
  869. // There is no check on ATI, we have to assume SM3 == support
  870. rsc->setCapability(RSC_ALPHA_TO_COVERAGE);
  871. }
  872. // no other cards have Dx9 hacks for alpha to coverage, as far as I know
  873. }
  874. if (mRealCapabilities == NULL)
  875. {
  876. mRealCapabilities = rsc;
  877. mRealCapabilities->addShaderProfile("hlsl");
  878. // if we are using custom capabilities, then
  879. // mCurrentCapabilities has already been loaded
  880. if(!mUseCustomCapabilities)
  881. mCurrentCapabilities = mRealCapabilities;
  882. initialiseFromRenderSystemCapabilities(mCurrentCapabilities, renderWindow);
  883. }
  884. return rsc;
  885. }
  886. //---------------------------------------------------------------------
  887. void D3D9RenderSystem::convertVertexShaderCaps(RenderSystemCapabilities* rsc) const
  888. {
  889. ushort major = 0xFF;
  890. ushort minor = 0xFF;
  891. D3DCAPS9 minVSCaps;
  892. // Find the device with the lowest vertex shader caps.
  893. for (unsigned int i=0; i < mDriverList->count(); ++i)
  894. {
  895. D3D9Driver* pCurDriver = mDriverList->item(i);
  896. const D3DCAPS9& rkCurCaps = pCurDriver->getD3D9DeviceCaps();
  897. ushort currMajor = static_cast<ushort>((rkCurCaps.VertexShaderVersion & 0x0000FF00) >> 8);
  898. ushort currMinor = static_cast<ushort>(rkCurCaps.VertexShaderVersion & 0x000000FF);
  899. if (currMajor < major)
  900. {
  901. major = currMajor;
  902. minor = currMinor;
  903. minVSCaps = rkCurCaps;
  904. }
  905. else if (currMajor == major && currMinor < minor)
  906. {
  907. minor = currMinor;
  908. minVSCaps = rkCurCaps;
  909. }
  910. }
  911. // In case we didn't found any vertex shader support
  912. // try the IDirect3DDevice9 caps instead of the IDirect3D9
  913. // software vertex processing is reported there
  914. if (major == 0 && minor == 0)
  915. {
  916. IDirect3DDevice9* lpD3DDevice9 = getActiveD3D9Device();
  917. D3DCAPS9 d3dDeviceCaps9;
  918. lpD3DDevice9->GetDeviceCaps(&d3dDeviceCaps9);
  919. major = static_cast<ushort>((d3dDeviceCaps9.VertexShaderVersion & 0x0000FF00) >> 8);
  920. minor = static_cast<ushort>(d3dDeviceCaps9.VertexShaderVersion & 0x000000FF);
  921. }
  922. bool vs2x = false;
  923. bool vs2a = false;
  924. // Special case detection for vs_2_x/a support
  925. if (major >= 2)
  926. {
  927. if ((minVSCaps.VS20Caps.Caps & D3DVS20CAPS_PREDICATION) &&
  928. (minVSCaps.VS20Caps.DynamicFlowControlDepth > 0) &&
  929. (minVSCaps.VS20Caps.NumTemps >= 12))
  930. {
  931. vs2x = true;
  932. }
  933. if ((minVSCaps.VS20Caps.Caps & D3DVS20CAPS_PREDICATION) &&
  934. (minVSCaps.VS20Caps.DynamicFlowControlDepth > 0) &&
  935. (minVSCaps.VS20Caps.NumTemps >= 13))
  936. {
  937. vs2a = true;
  938. }
  939. }
  940. // Populate max param count
  941. switch (major)
  942. {
  943. case 1:
  944. // No boolean params allowed
  945. rsc->setVertexProgramConstantBoolCount(0);
  946. // No integer params allowed
  947. rsc->setVertexProgramConstantIntCount(0);
  948. // float params, always 4D
  949. rsc->setVertexProgramConstantFloatCount(static_cast<ushort>(minVSCaps.MaxVertexShaderConst));
  950. break;
  951. case 2:
  952. // 16 boolean params allowed
  953. rsc->setVertexProgramConstantBoolCount(16);
  954. // 16 integer params allowed, 4D
  955. rsc->setVertexProgramConstantIntCount(16);
  956. // float params, always 4D
  957. rsc->setVertexProgramConstantFloatCount(static_cast<ushort>(minVSCaps.MaxVertexShaderConst));
  958. break;
  959. case 3:
  960. // 16 boolean params allowed
  961. rsc->setVertexProgramConstantBoolCount(16);
  962. // 16 integer params allowed, 4D
  963. rsc->setVertexProgramConstantIntCount(16);
  964. // float params, always 4D
  965. rsc->setVertexProgramConstantFloatCount(static_cast<ushort>(minVSCaps.MaxVertexShaderConst));
  966. break;
  967. }
  968. // populate syntax codes in program manager (no breaks in this one so it falls through)
  969. switch(major)
  970. {
  971. case 3:
  972. rsc->addShaderProfile("vs_3_0");
  973. case 2:
  974. if (vs2x)
  975. rsc->addShaderProfile("vs_2_x");
  976. if (vs2a)
  977. rsc->addShaderProfile("vs_2_a");
  978. rsc->addShaderProfile("vs_2_0");
  979. case 1:
  980. rsc->addShaderProfile("vs_1_1");
  981. rsc->setCapability(RSC_VERTEX_PROGRAM);
  982. }
  983. }
  984. //---------------------------------------------------------------------
  985. void D3D9RenderSystem::convertPixelShaderCaps(RenderSystemCapabilities* rsc) const
  986. {
  987. ushort major = 0xFF;
  988. ushort minor = 0xFF;
  989. D3DCAPS9 minPSCaps;
  990. // Find the device with the lowest pixel shader caps.
  991. for (unsigned int i=0; i < mDriverList->count(); ++i)
  992. {
  993. D3D9Driver* pCurDriver = mDriverList->item(i);
  994. const D3DCAPS9& currCaps = pCurDriver->getD3D9DeviceCaps();
  995. ushort currMajor = static_cast<ushort>((currCaps.PixelShaderVersion & 0x0000FF00) >> 8);
  996. ushort currMinor = static_cast<ushort>(currCaps.PixelShaderVersion & 0x000000FF);
  997. if (currMajor < major)
  998. {
  999. major = currMajor;
  1000. minor = currMinor;
  1001. minPSCaps = currCaps;
  1002. }
  1003. else if (currMajor == major && currMinor < minor)
  1004. {
  1005. minor = currMinor;
  1006. minPSCaps = currCaps;
  1007. }
  1008. }
  1009. bool ps2a = false;
  1010. bool ps2b = false;
  1011. bool ps2x = false;
  1012. // Special case detection for ps_2_x/a/b support
  1013. if (major >= 2)
  1014. {
  1015. if ((minPSCaps.PS20Caps.Caps & D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT) &&
  1016. (minPSCaps.PS20Caps.NumTemps >= 32))
  1017. {
  1018. ps2b = true;
  1019. }
  1020. if ((minPSCaps.PS20Caps.Caps & D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT) &&
  1021. (minPSCaps.PS20Caps.Caps & D3DPS20CAPS_NODEPENDENTREADLIMIT) &&
  1022. (minPSCaps.PS20Caps.Caps & D3DPS20CAPS_ARBITRARYSWIZZLE) &&
  1023. (minPSCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) &&
  1024. (minPSCaps.PS20Caps.Caps & D3DPS20CAPS_PREDICATION) &&
  1025. (minPSCaps.PS20Caps.NumTemps >= 22))
  1026. {
  1027. ps2a = true;
  1028. }
  1029. // Does this enough?
  1030. if (ps2a || ps2b)
  1031. {
  1032. ps2x = true;
  1033. }
  1034. }
  1035. switch (major)
  1036. {
  1037. case 1:
  1038. // no boolean params allowed
  1039. rsc->setFragmentProgramConstantBoolCount(0);
  1040. // no integer params allowed
  1041. rsc->setFragmentProgramConstantIntCount(0);
  1042. // float params, always 4D
  1043. // NB in ps_1_x these are actually stored as fixed point values,
  1044. // but they are entered as floats
  1045. rsc->setFragmentProgramConstantFloatCount(8);
  1046. break;
  1047. case 2:
  1048. // 16 boolean params allowed
  1049. rsc->setFragmentProgramConstantBoolCount(16);
  1050. // 16 integer params allowed, 4D
  1051. rsc->setFragmentProgramConstantIntCount(16);
  1052. // float params, always 4D
  1053. rsc->setFragmentProgramConstantFloatCount(32);
  1054. break;
  1055. case 3:
  1056. // 16 boolean params allowed
  1057. rsc->setFragmentProgramConstantBoolCount(16);
  1058. // 16 integer params allowed, 4D
  1059. rsc->setFragmentProgramConstantIntCount(16);
  1060. // float params, always 4D
  1061. rsc->setFragmentProgramConstantFloatCount(224);
  1062. break;
  1063. }
  1064. // populate syntax codes in program manager (no breaks in this one so it falls through)
  1065. switch(major)
  1066. {
  1067. case 3:
  1068. if (minor > 0)
  1069. rsc->addShaderProfile("ps_3_x");
  1070. rsc->addShaderProfile("ps_3_0");
  1071. case 2:
  1072. if (ps2x)
  1073. rsc->addShaderProfile("ps_2_x");
  1074. if (ps2a)
  1075. rsc->addShaderProfile("ps_2_a");
  1076. if (ps2b)
  1077. rsc->addShaderProfile("ps_2_b");
  1078. rsc->addShaderProfile("ps_2_0");
  1079. case 1:
  1080. if (major > 1 || minor >= 4)
  1081. rsc->addShaderProfile("ps_1_4");
  1082. if (major > 1 || minor >= 3)
  1083. rsc->addShaderProfile("ps_1_3");
  1084. if (major > 1 || minor >= 2)
  1085. rsc->addShaderProfile("ps_1_2");
  1086. rsc->addShaderProfile("ps_1_1");
  1087. rsc->setCapability(RSC_FRAGMENT_PROGRAM);
  1088. }
  1089. }
  1090. //-----------------------------------------------------------------------
  1091. bool D3D9RenderSystem::checkVertexTextureFormats(D3D9RenderWindow* renderWindow) const
  1092. {
  1093. bool anySupported = false;
  1094. IDirect3DSurface9* bbSurf;
  1095. renderWindow->getCustomAttribute("DDBACKBUFFER", &bbSurf);
  1096. D3DSURFACE_DESC bbSurfDesc;
  1097. bbSurf->GetDesc(&bbSurfDesc);
  1098. for (uint ipf = static_cast<uint>(PF_L8); ipf < static_cast<uint>(PF_COUNT); ++ipf)
  1099. {
  1100. PixelFormat pf = (PixelFormat)ipf;
  1101. D3DFORMAT fmt =
  1102. D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(pf));
  1103. if (SUCCEEDED(mpD3D->CheckDeviceFormat(
  1104. mActiveD3DDriver->getAdapterNumber(), D3DDEVTYPE_HAL, bbSurfDesc.Format,
  1105. D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, fmt)))
  1106. {
  1107. // cool, at least one supported
  1108. anySupported = true;
  1109. }
  1110. }
  1111. return anySupported;
  1112. }
  1113. //-----------------------------------------------------------------------
  1114. void D3D9RenderSystem::initialiseFromRenderSystemCapabilities(RenderSystemCapabilities* caps, RenderTarget* primary)
  1115. {
  1116. if (caps->getRenderSystemName() != getName())
  1117. {
  1118. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  1119. "Trying to initialize D3D9RenderSystem from RenderSystemCapabilities that do not support Direct3D9",
  1120. "D3D9RenderSystem::initialiseFromRenderSystemCapabilities");
  1121. }
  1122. // TODO PORT - If i'll be supporting factories enable this
  1123. //if (caps->isShaderProfileSupported("hlsl"))
  1124. // HighLevelGpuProgramManager::getSingleton().addFactory(mHLSLProgramFactory);
  1125. }
  1126. //-----------------------------------------------------------------------
  1127. bool D3D9RenderSystem::_checkTextureFilteringSupported(TextureType ttype, PixelFormat format, int usage)
  1128. {
  1129. // Gets D3D format
  1130. D3DFORMAT d3dPF = D3D9Mappings::_getPF(format);
  1131. if (d3dPF == D3DFMT_UNKNOWN)
  1132. return false;
  1133. for (uint i = 0; i < mDeviceManager->getDeviceCount(); ++i)
  1134. {
  1135. D3D9Device* currDevice = mDeviceManager->getDevice(i);
  1136. D3D9RenderWindow* currDevicePrimaryWindow = currDevice->getPrimaryWindow();
  1137. IDirect3DSurface9* pSurface = currDevicePrimaryWindow->getRenderSurface();
  1138. D3DSURFACE_DESC srfDesc;
  1139. // Get surface desc
  1140. if (FAILED(pSurface->GetDesc(&srfDesc)))
  1141. return false;
  1142. // Calculate usage
  1143. DWORD d3dusage = D3DUSAGE_QUERY_FILTER;
  1144. if (usage & TU_RENDERTARGET)
  1145. d3dusage |= D3DUSAGE_RENDERTARGET;
  1146. if (usage & TU_DYNAMIC)
  1147. d3dusage |= D3DUSAGE_DYNAMIC;
  1148. // Detect resource type
  1149. D3DRESOURCETYPE rtype;
  1150. switch(ttype)
  1151. {
  1152. case TEX_TYPE_1D:
  1153. case TEX_TYPE_2D:
  1154. rtype = D3DRTYPE_TEXTURE;
  1155. break;
  1156. case TEX_TYPE_3D:
  1157. rtype = D3DRTYPE_VOLUMETEXTURE;
  1158. break;
  1159. case TEX_TYPE_CUBE_MAP:
  1160. rtype = D3DRTYPE_CUBETEXTURE;
  1161. break;
  1162. default:
  1163. return false;
  1164. }
  1165. HRESULT hr = mpD3D->CheckDeviceFormat(
  1166. currDevice->getAdapterNumber(),
  1167. currDevice->getDeviceType(),
  1168. srfDesc.Format,
  1169. d3dusage,
  1170. rtype,
  1171. d3dPF);
  1172. if (FAILED(hr))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. //-----------------------------------------------------------------------
  1178. MultiRenderTarget * D3D9RenderSystem::createMultiRenderTarget(const String & name)
  1179. {
  1180. MultiRenderTarget *retval;
  1181. retval = new D3D9MultiRenderTarget(name);
  1182. attachRenderTarget(*retval);
  1183. return retval;
  1184. }
  1185. //---------------------------------------------------------------------
  1186. void D3D9RenderSystem::destroyRenderTarget(const String& name)
  1187. {
  1188. D3D9RenderWindow* renderWindow = NULL;
  1189. // Check render windows
  1190. D3D9RenderWindowList::iterator sw;
  1191. for (sw = mRenderWindows.begin(); sw != mRenderWindows.end(); ++sw)
  1192. {
  1193. if ((*sw)->getName() == name)
  1194. {
  1195. renderWindow = (*sw);
  1196. mRenderWindows.erase(sw);
  1197. break;
  1198. }
  1199. }
  1200. // Do the real removal
  1201. RenderSystem::destroyRenderTarget(name);
  1202. }
  1203. //---------------------------------------------------------------------
  1204. String D3D9RenderSystem::getErrorDescription( long errorNumber ) const
  1205. {
  1206. const String errMsg = DXGetErrorDescription( errorNumber );
  1207. return errMsg;
  1208. }
  1209. //---------------------------------------------------------------------
  1210. VertexElementType D3D9RenderSystem::getColourVertexElementType() const
  1211. {
  1212. return VET_COLOUR_ARGB;
  1213. }
  1214. //---------------------------------------------------------------------
  1215. void D3D9RenderSystem::_convertProjectionMatrix(const Matrix4& matrix,
  1216. Matrix4& dest, bool forGpuProgram)
  1217. {
  1218. dest = matrix;
  1219. // Convert depth range from [-1,+1] to [0,1]
  1220. dest[2][0] = (dest[2][0] + dest[3][0]) / 2;
  1221. dest[2][1] = (dest[2][1] + dest[3][1]) / 2;
  1222. dest[2][2] = (dest[2][2] + dest[3][2]) / 2;
  1223. dest[2][3] = (dest[2][3] + dest[3][3]) / 2;
  1224. if (!forGpuProgram)
  1225. {
  1226. // Convert right-handed to left-handed
  1227. dest[0][2] = -dest[0][2];
  1228. dest[1][2] = -dest[1][2];
  1229. dest[2][2] = -dest[2][2];
  1230. dest[3][2] = -dest[3][2];
  1231. }
  1232. }
  1233. //---------------------------------------------------------------------
  1234. void D3D9RenderSystem::_makeProjectionMatrix(const Radian& fovy, Real aspect, Real nearPlane,
  1235. Real farPlane, Matrix4& dest, bool forGpuProgram)
  1236. {
  1237. Radian theta ( fovy * 0.5 );
  1238. Real h = 1 / Math::Tan(theta);
  1239. Real w = h / aspect;
  1240. Real q, qn;
  1241. if (farPlane == 0)
  1242. {
  1243. q = 1 - Frustum::INFINITE_FAR_PLANE_ADJUST;
  1244. qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 1);
  1245. }
  1246. else
  1247. {
  1248. q = farPlane / ( farPlane - nearPlane );
  1249. qn = -q * nearPlane;
  1250. }
  1251. dest = Matrix4::ZERO;
  1252. dest[0][0] = w;
  1253. dest[1][1] = h;
  1254. if (forGpuProgram)
  1255. {
  1256. dest[2][2] = -q;
  1257. dest[3][2] = -1.0f;
  1258. }
  1259. else
  1260. {
  1261. dest[2][2] = q;
  1262. dest[3][2] = 1.0f;
  1263. }
  1264. dest[2][3] = qn;
  1265. }
  1266. //---------------------------------------------------------------------
  1267. void D3D9RenderSystem::_makeOrthoMatrix(const Radian& fovy, Real aspect, Real nearPlane, Real farPlane,
  1268. Matrix4& dest, bool forGpuProgram )
  1269. {
  1270. Radian thetaY (fovy / 2.0f);
  1271. Real tanThetaY = Math::Tan(thetaY);
  1272. //Real thetaX = thetaY * aspect;
  1273. Real tanThetaX = tanThetaY * aspect; //Math::Tan(thetaX);
  1274. Real half_w = tanThetaX * nearPlane;
  1275. Real half_h = tanThetaY * nearPlane;
  1276. Real iw = static_cast<Real>(1.0 / half_w);
  1277. Real ih = static_cast<Real>(1.0 / half_h);
  1278. Real q;
  1279. if (farPlane == 0)
  1280. {
  1281. q = 0;
  1282. }
  1283. else
  1284. {
  1285. q = static_cast<Real>(1.0 / (farPlane - nearPlane));
  1286. }
  1287. dest = Matrix4::ZERO;
  1288. dest[0][0] = iw;
  1289. dest[1][1] = ih;
  1290. dest[2][2] = q;
  1291. dest[2][3] = -nearPlane / (farPlane - nearPlane);
  1292. dest[3][3] = 1;
  1293. if (forGpuProgram)
  1294. {
  1295. dest[2][2] = -dest[2][2];
  1296. }
  1297. }
  1298. //---------------------------------------------------------------------
  1299. void D3D9RenderSystem::setAmbientLight( float r, float g, float b )
  1300. {
  1301. HRESULT hr = __SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( r, g, b, 1.0f ) );
  1302. if( FAILED( hr ) )
  1303. OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
  1304. "Failed to set render stat D3DRS_AMBIENT", "D3D9RenderSystem::setAmbientLight" );
  1305. }
  1306. //---------------------------------------------------------------------
  1307. void D3D9RenderSystem::_useLights(const LightList& lights, unsigned short limit)
  1308. {
  1309. IDirect3DDevice9* activeDevice = getActiveD3D9Device();
  1310. LightList::const_iterator i, iend;
  1311. iend = lights.end();
  1312. unsigned short num = 0;
  1313. for (i = lights.begin(); i != iend && num < limit; ++i, ++num)
  1314. {
  1315. setD3D9Light(num, *i);
  1316. }
  1317. // Disable extra lights
  1318. for (; num < mCurrentLights[activeDevice]; ++num)
  1319. {
  1320. setD3D9Light(num, NULL);
  1321. }
  1322. mCurrentLights[activeDevice] = std::min(limit, static_cast<unsigned short>(lights.size()));
  1323. }
  1324. //---------------------------------------------------------------------
  1325. void D3D9RenderSystem::setShadingType( ShadeOptions so )
  1326. {
  1327. HRESULT hr = __SetRenderState( D3DRS_SHADEMODE, D3D9Mappings::get(so) );
  1328. if( FAILED( hr ) )
  1329. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1330. "Failed to set render stat D3DRS_SHADEMODE", "D3D9RenderSystem::setShadingType" );
  1331. }
  1332. //---------------------------------------------------------------------
  1333. void D3D9RenderSystem::setLightingEnabled( bool enabled )
  1334. {
  1335. HRESULT hr;
  1336. if( FAILED( hr = __SetRenderState( D3DRS_LIGHTING, enabled ) ) )
  1337. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1338. "Failed to set render state D3DRS_LIGHTING", "D3D9RenderSystem::setLightingEnabled" );
  1339. }
  1340. //---------------------------------------------------------------------
  1341. void D3D9RenderSystem::setD3D9Light( size_t index, Light* lt )
  1342. {
  1343. // TODO PORT - D3D9 lights not supported
  1344. assert(false);
  1345. }
  1346. //---------------------------------------------------------------------
  1347. void D3D9RenderSystem::_setViewMatrix( const Matrix4 &m )
  1348. {
  1349. // save latest view matrix
  1350. mViewMatrix = m;
  1351. mViewMatrix[2][0] = -mViewMatrix[2][0];
  1352. mViewMatrix[2][1] = -mViewMatrix[2][1];
  1353. mViewMatrix[2][2] = -mViewMatrix[2][2];
  1354. mViewMatrix[2][3] = -mViewMatrix[2][3];
  1355. mDxViewMat = D3D9Mappings::makeD3DXMatrix( mViewMatrix );
  1356. HRESULT hr;
  1357. if( FAILED( hr = getActiveD3D9Device()->SetTransform( D3DTS_VIEW, &mDxViewMat ) ) )
  1358. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Cannot set D3D9 view matrix", "D3D9RenderSystem::_setViewMatrix" );
  1359. // also mark clip planes dirty
  1360. if (!mClipPlanes.empty())
  1361. mClipPlanesDirty = true;
  1362. }
  1363. //---------------------------------------------------------------------
  1364. void D3D9RenderSystem::_setProjectionMatrix( const Matrix4 &m )
  1365. {
  1366. // save latest matrix
  1367. mDxProjMat = D3D9Mappings::makeD3DXMatrix( m );
  1368. if( mActiveRenderTarget->requiresTextureFlipping() )
  1369. {
  1370. // Invert transformed y
  1371. mDxProjMat._12 = - mDxProjMat._12;
  1372. mDxProjMat._22 = - mDxProjMat._22;
  1373. mDxProjMat._32 = - mDxProjMat._32;
  1374. mDxProjMat._42 = - mDxProjMat._42;
  1375. }
  1376. HRESULT hr;
  1377. if( FAILED( hr = getActiveD3D9Device()->SetTransform( D3DTS_PROJECTION, &mDxProjMat ) ) )
  1378. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Cannot set D3D9 projection matrix", "D3D9RenderSystem::_setProjectionMatrix" );
  1379. // also mark clip planes dirty
  1380. if (!mClipPlanes.empty())
  1381. mClipPlanesDirty = true;
  1382. }
  1383. //---------------------------------------------------------------------
  1384. void D3D9RenderSystem::_setWorldMatrix( const Matrix4 &m )
  1385. {
  1386. // save latest matrix
  1387. mDxWorldMat = D3D9Mappings::makeD3DXMatrix( m );
  1388. HRESULT hr;
  1389. if( FAILED( hr = getActiveD3D9Device()->SetTransform( D3DTS_WORLD, &mDxWorldMat ) ) )
  1390. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Cannot set D3D9 world matrix", "D3D9RenderSystem::_setWorldMatrix" );
  1391. }
  1392. //---------------------------------------------------------------------
  1393. void D3D9RenderSystem::_setSurfaceParams( const ColourValue &ambient, const ColourValue &diffuse,
  1394. const ColourValue &specular, const ColourValue &emissive, Real shininess,
  1395. TrackVertexColourType tracking )
  1396. {
  1397. D3DMATERIAL9 material;
  1398. material.Diffuse = D3DXCOLOR( diffuse.r, diffuse.g, diffuse.b, diffuse.a );
  1399. material.Ambient = D3DXCOLOR( ambient.r, ambient.g, ambient.b, ambient.a );
  1400. material.Specular = D3DXCOLOR( specular.r, specular.g, specular.b, specular.a );
  1401. material.Emissive = D3DXCOLOR( emissive.r, emissive.g, emissive.b, emissive.a );
  1402. material.Power = shininess;
  1403. HRESULT hr = getActiveD3D9Device()->SetMaterial( &material );
  1404. if( FAILED( hr ) )
  1405. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting D3D material", "D3D9RenderSystem::_setSurfaceParams" );
  1406. if(tracking != TVC_NONE)
  1407. {
  1408. __SetRenderState(D3DRS_COLORVERTEX, TRUE);
  1409. __SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, (tracking&TVC_AMBIENT)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
  1410. __SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, (tracking&TVC_DIFFUSE)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
  1411. __SetRenderState(D3DRS_SPECULARMATERIALSOURCE, (tracking&TVC_SPECULAR)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
  1412. __SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, (tracking&TVC_EMISSIVE)?D3DMCS_COLOR1:D3DMCS_MATERIAL);
  1413. }
  1414. else
  1415. {
  1416. __SetRenderState(D3DRS_COLORVERTEX, FALSE);
  1417. }
  1418. }
  1419. //---------------------------------------------------------------------
  1420. void D3D9RenderSystem::_setPointParameters(Real size,
  1421. bool attenuationEnabled, Real constant, Real linear, Real quadratic,
  1422. Real minSize, Real maxSize)
  1423. {
  1424. if(attenuationEnabled)
  1425. {
  1426. // scaling required
  1427. __SetRenderState(D3DRS_POINTSCALEENABLE, TRUE);
  1428. __SetFloatRenderState(D3DRS_POINTSCALE_A, constant);
  1429. __SetFloatRenderState(D3DRS_POINTSCALE_B, linear);
  1430. __SetFloatRenderState(D3DRS_POINTSCALE_C, quadratic);
  1431. }
  1432. else
  1433. {
  1434. // no scaling required
  1435. __SetRenderState(D3DRS_POINTSCALEENABLE, FALSE);
  1436. }
  1437. __SetFloatRenderState(D3DRS_POINTSIZE, size);
  1438. __SetFloatRenderState(D3DRS_POINTSIZE_MIN, minSize);
  1439. if (maxSize == 0.0f)
  1440. maxSize = mCurrentCapabilities->getMaxPointSize();
  1441. __SetFloatRenderState(D3DRS_POINTSIZE_MAX, maxSize);
  1442. }
  1443. //---------------------------------------------------------------------
  1444. void D3D9RenderSystem::_setPointSpritesEnabled(bool enabled)
  1445. {
  1446. if (enabled)
  1447. {
  1448. __SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
  1449. }
  1450. else
  1451. {
  1452. __SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
  1453. }
  1454. }
  1455. //---------------------------------------------------------------------
  1456. void D3D9RenderSystem::_setTexture( size_t stage, bool enabled, const TexturePtr& tex )
  1457. {
  1458. HRESULT hr;
  1459. D3D9TexturePtr dt = tex;
  1460. if (enabled && !dt.isNull())
  1461. {
  1462. IDirect3DBaseTexture9 *pTex = dt->getTexture();
  1463. if (mTexStageDesc[stage].pTex != pTex)
  1464. {
  1465. hr = getActiveD3D9Device()->SetTexture(static_cast<DWORD>(stage), pTex);
  1466. if( hr != S_OK )
  1467. {
  1468. String str = "Unable to set texture in D3D9";
  1469. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9RenderSystem::_setTexture" );
  1470. }
  1471. // set stage desc.
  1472. mTexStageDesc[stage].pTex = pTex;
  1473. mTexStageDesc[stage].texType = D3D9Mappings::get(dt->getTextureType());
  1474. // Set gamma now too
  1475. if (dt->isHardwareGammaReadToBeUsed())
  1476. {
  1477. __SetSamplerState(static_cast<DWORD>(stage), D3DSAMP_SRGBTEXTURE, TRUE);
  1478. }
  1479. else
  1480. {
  1481. __SetSamplerState(static_cast<DWORD>(stage), D3DSAMP_SRGBTEXTURE, FALSE);
  1482. }
  1483. }
  1484. }
  1485. else
  1486. {
  1487. if (mTexStageDesc[stage].pTex != 0)
  1488. {
  1489. hr = getActiveD3D9Device()->SetTexture(static_cast<DWORD>(stage), 0);
  1490. if( hr != S_OK )
  1491. {
  1492. String str = "Unable to disable texture '" + StringConverter::toString(stage) + "' in D3D9";
  1493. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9RenderSystem::_setTexture" );
  1494. }
  1495. }
  1496. hr = __SetTextureStageState(static_cast<DWORD>(stage), D3DTSS_COLOROP, D3DTOP_DISABLE);
  1497. if( hr != S_OK )
  1498. {
  1499. String str = "Unable to disable texture '" + StringConverter::toString(stage) + "' in D3D9";
  1500. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9RenderSystem::_setTexture" );
  1501. }
  1502. // set stage desc. to defaults
  1503. mTexStageDesc[stage].pTex = 0;
  1504. mTexStageDesc[stage].autoTexCoordType = TEXCALC_NONE;
  1505. mTexStageDesc[stage].coordIndex = 0;
  1506. mTexStageDesc[stage].texType = D3D9Mappings::D3D_TEX_TYPE_NORMAL;
  1507. }
  1508. }
  1509. //---------------------------------------------------------------------
  1510. void D3D9RenderSystem::_setVertexTexture(size_t stage, const TexturePtr& tex)
  1511. {
  1512. if (tex.isNull())
  1513. {
  1514. if (mTexStageDesc[stage].pVertexTex != 0)
  1515. {
  1516. HRESULT hr = getActiveD3D9Device()->SetTexture(D3DVERTEXTEXTURESAMPLER0 + static_cast<DWORD>(stage), 0);
  1517. if( hr != S_OK )
  1518. {
  1519. String str = "Unable to disable vertex texture '"
  1520. + StringConverter::toString(stage) + "' in D3D9";
  1521. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9RenderSystem::_setVertexTexture" );
  1522. }
  1523. }
  1524. // set stage desc. to defaults
  1525. mTexStageDesc[stage].pVertexTex = 0;
  1526. }
  1527. else
  1528. {
  1529. D3D9TexturePtr dt = tex;
  1530. IDirect3DBaseTexture9 *pTex = dt->getTexture();
  1531. if (mTexStageDesc[stage].pVertexTex != pTex)
  1532. {
  1533. HRESULT hr = getActiveD3D9Device()->SetTexture(D3DVERTEXTEXTURESAMPLER0 + static_cast<DWORD>(stage), pTex);
  1534. if( hr != S_OK )
  1535. {
  1536. String str = "Unable to set vertex texture in D3D9";
  1537. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9RenderSystem::_setVertexTexture" );
  1538. }
  1539. // set stage desc.
  1540. mTexStageDesc[stage].pVertexTex = pTex;
  1541. }
  1542. }
  1543. }
  1544. //---------------------------------------------------------------------
  1545. void D3D9RenderSystem::_disableTextureUnit(size_t texUnit)
  1546. {
  1547. RenderSystem::_disableTextureUnit(texUnit);
  1548. // also disable vertex texture unit
  1549. static TexturePtr nullPtr;
  1550. _setVertexTexture(texUnit, nullPtr);
  1551. }
  1552. //---------------------------------------------------------------------
  1553. void D3D9RenderSystem::_setTextureCoordSet( size_t stage, size_t index )
  1554. {
  1555. // if vertex shader is being used, stage and index must match
  1556. if (mVertexProgramBound)
  1557. index = stage;
  1558. HRESULT hr;
  1559. // Record settings
  1560. mTexStageDesc[stage].coordIndex = index;
  1561. hr = __SetTextureStageState( static_cast<DWORD>(stage), D3DTSS_TEXCOORDINDEX, D3D9Mappings::get(mTexStageDesc[stage].autoTexCoordType, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) | index );
  1562. if( FAILED( hr ) )
  1563. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set texture coord. set index", "D3D9RenderSystem::_setTextureCoordSet" );
  1564. }
  1565. //---------------------------------------------------------------------
  1566. void D3D9RenderSystem::_setTextureCoordCalculation( size_t stage, TexCoordCalcMethod m,
  1567. const Frustum* frustum)
  1568. {
  1569. HRESULT hr;
  1570. // record the stage state
  1571. mTexStageDesc[stage].autoTexCoordType = m;
  1572. mTexStageDesc[stage].frustum = frustum;
  1573. hr = __SetTextureStageState( static_cast<DWORD>(stage), D3DTSS_TEXCOORDINDEX, D3D9Mappings::get(m, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) | mTexStageDesc[stage].coordIndex );
  1574. if(FAILED(hr))
  1575. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set texture auto tex.coord. generation mode", "D3D9RenderSystem::_setTextureCoordCalculation" );
  1576. }
  1577. //---------------------------------------------------------------------
  1578. void D3D9RenderSystem::_setTextureMipmapBias(size_t unit, float bias)
  1579. {
  1580. if (mCurrentCapabilities->hasCapability(RSC_MIPMAP_LOD_BIAS))
  1581. {
  1582. // ugh - have to pass float data through DWORD with no conversion
  1583. HRESULT hr = __SetSamplerState(static_cast<DWORD>(unit), D3DSAMP_MIPMAPLODBIAS,
  1584. *(DWORD*)&bias);
  1585. if(FAILED(hr))
  1586. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set texture mipmap bias",
  1587. "D3D9RenderSystem::_setTextureMipmapBias" );
  1588. }
  1589. }
  1590. //---------------------------------------------------------------------
  1591. void D3D9RenderSystem::_setTextureMatrix( size_t stage, const Matrix4& xForm )
  1592. {
  1593. HRESULT hr;
  1594. D3DXMATRIX d3dMat; // the matrix we'll maybe apply
  1595. Matrix4 newMat = xForm; // the matrix we'll apply after conv. to D3D format
  1596. // Cache texcoord calc method to register
  1597. TexCoordCalcMethod autoTexCoordType = mTexStageDesc[stage].autoTexCoordType;
  1598. // if a vertex program is bound, we mustn't set texture transforms
  1599. if (mVertexProgramBound)
  1600. {
  1601. hr = __SetTextureStageState( static_cast<DWORD>(stage), D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
  1602. if( FAILED( hr ) )
  1603. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to disable texture coordinate transform", "D3D9RenderSystem::_setTextureMatrix" );
  1604. return;
  1605. }
  1606. if (autoTexCoordType == TEXCALC_ENVIRONMENT_MAP)
  1607. {
  1608. if (mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().VertexProcessingCaps & D3DVTXPCAPS_TEXGEN_SPHEREMAP)
  1609. {
  1610. /** Invert the texture for the spheremap */
  1611. Matrix4 ogreMatEnvMap = Matrix4::IDENTITY;
  1612. // set env_map values
  1613. ogreMatEnvMap[1][1] = -1.0f;
  1614. // concatenate with the xForm
  1615. newMat = newMat.concatenate(ogreMatEnvMap);
  1616. }
  1617. else
  1618. {
  1619. /* If envmap is applied, but device doesn't support spheremap,
  1620. then we have to use texture transform to make the camera space normal
  1621. reference the envmap properly. This isn't exactly the same as spheremap
  1622. (it looks nasty on flat areas because the camera space normals are the same)
  1623. but it's the best approximation we have in the absence of a proper spheremap */
  1624. // concatenate with the xForm
  1625. newMat = newMat.concatenate(Matrix4::CLIPSPACE2DTOIMAGESPACE);
  1626. }
  1627. }
  1628. // If this is a cubic reflection, we need to modify using the view matrix
  1629. if (autoTexCoordType == TEXCALC_ENVIRONMENT_MAP_REFLECTION)
  1630. {
  1631. // Get transposed 3x3
  1632. // We want to transpose since that will invert an orthonormal matrix ie rotation
  1633. Matrix4 ogreViewTransposed;
  1634. ogreViewTransposed[0][0] = mViewMatrix[0][0];
  1635. ogreViewTransposed[0][1] = mViewMatrix[1][0];
  1636. ogreViewTransposed[0][2] = mViewMatrix[2][0];
  1637. ogreViewTransposed[0][3] = 0.0f;
  1638. ogreViewTransposed[1][0] = mViewMatrix[0][1];
  1639. ogreViewTransposed[1][1] = mViewMatrix[1][1];
  1640. ogreViewTransposed[1][2] = mViewMatrix[2][1];
  1641. ogreViewTransposed[1][3] = 0.0f;
  1642. ogreViewTransposed[2][0] = mViewMatrix[0][2];
  1643. ogreViewTransposed[2][1] = mViewMatrix[1][2];
  1644. ogreViewTransposed[2][2] = mViewMatrix[2][2];
  1645. ogreViewTransposed[2][3] = 0.0f;
  1646. ogreViewTransposed[3][0] = 0.0f;
  1647. ogreViewTransposed[3][1] = 0.0f;
  1648. ogreViewTransposed[3][2] = 0.0f;
  1649. ogreViewTransposed[3][3] = 1.0f;
  1650. newMat = newMat.concatenate(ogreViewTransposed);
  1651. }
  1652. if (autoTexCoordType == TEXCALC_PROJECTIVE_TEXTURE)
  1653. {
  1654. // Derive camera space to projector space transform
  1655. // To do this, we need to undo the camera view matrix, then
  1656. // apply the projector view & projection matrices
  1657. newMat = mViewMatrix.inverse();
  1658. if(mTexProjRelative)
  1659. {
  1660. Matrix4 viewMatrix;
  1661. mTexStageDesc[stage].frustum->calcViewMatrixRelative(mTexProjRelativeOrigin, viewMatrix);
  1662. newMat = viewMatrix * newMat;
  1663. }
  1664. else
  1665. {
  1666. newMat = mTexStageDesc[stage].frustum->getViewMatrix() * newMat;
  1667. }
  1668. newMat = mTexStageDesc[stage].frustum->getProjectionMatrix() * newMat;
  1669. newMat = Matrix4::CLIPSPACE2DTOIMAGESPACE * newMat;
  1670. newMat = xForm * newMat;
  1671. }
  1672. // need this if texture is a cube map, to invert D3D's z coord
  1673. if (autoTexCoordType != TEXCALC_NONE &&
  1674. autoTexCoordType != TEXCALC_PROJECTIVE_TEXTURE)
  1675. {
  1676. newMat[2][0] = -newMat[2][0];
  1677. newMat[2][1] = -newMat[2][1];
  1678. newMat[2][2] = -newMat[2][2];
  1679. newMat[2][3] = -newMat[2][3];
  1680. }
  1681. // convert our matrix to D3D format
  1682. d3dMat = D3D9Mappings::makeD3DXMatrix(newMat);
  1683. // set the matrix if it's not the identity
  1684. if (!D3DXMatrixIsIdentity(&d3dMat))
  1685. {
  1686. /* It's seems D3D automatically add a texture coordinate with value 1,
  1687. and fill up the remaining texture coordinates with 0 for the input
  1688. texture coordinates before pass to texture coordinate transformation.
  1689. NOTE: It's difference with D3DDECLTYPE enumerated type expand in
  1690. DirectX SDK documentation!
  1691. So we should prepare the texcoord transform, make the transformation
  1692. just like standardized vector expand, thus, fill w with value 1 and
  1693. others with 0.
  1694. */
  1695. if (autoTexCoordType == TEXCALC_NONE)
  1696. {
  1697. /* FIXME: The actually input texture coordinate dimensions should
  1698. be determine by texture coordinate vertex element. Now, just trust
  1699. user supplied texture type matchs texture coordinate vertex element.
  1700. */
  1701. if (mTexStageDesc[stage].texType == D3D9Mappings::D3D_TEX_TYPE_NORMAL)
  1702. {
  1703. /* It's 2D input texture coordinate:
  1704. texcoord in vertex buffer D3D expanded to We are adjusted to
  1705. --> -->
  1706. (u, v) (u, v, 1, 0) (u, v, 0, 1)
  1707. */
  1708. std::swap(d3dMat._31, d3dMat._41);
  1709. std::swap(d3dMat._32, d3dMat._42);
  1710. std::swap(d3dMat._33, d3dMat._43);
  1711. std::swap(d3dMat._34, d3dMat._44);
  1712. }
  1713. }
  1714. else
  1715. {
  1716. // All texgen generate 3D input texture coordinates.
  1717. }
  1718. // tell D3D the dimension of tex. coord.
  1719. int texCoordDim = D3DTTFF_COUNT2;
  1720. if (mTexStageDesc[stage].autoTexCoordType == TEXCALC_PROJECTIVE_TEXTURE)
  1721. {
  1722. /* We want texcoords (u, v, w, q) always get divided by q, but D3D
  1723. projected texcoords is divided by the last element (in the case of
  1724. 2D texcoord, is w). So we tweak the transform matrix, transform the
  1725. texcoords with w and q swapped: (u, v, q, w), and then D3D will
  1726. divide u, v by q. The w and q just ignored as it wasn't used by
  1727. rasterizer.
  1728. */
  1729. switch (mTexStageDesc[stage].texType)
  1730. {
  1731. case D3D9Mappings::D3D_TEX_TYPE_NORMAL:
  1732. std::swap(d3dMat._13, d3dMat._14);
  1733. std::swap(d3dMat._23, d3dMat._24);
  1734. std::swap(d3dMat._33, d3dMat._34);
  1735. std::swap(d3dMat._43, d3dMat._44);
  1736. texCoordDim = D3DTTFF_PROJECTED | D3DTTFF_COUNT3;
  1737. break;
  1738. case D3D9Mappings::D3D_TEX_TYPE_CUBE:
  1739. case D3D9Mappings::D3D_TEX_TYPE_VOLUME:
  1740. // Yes, we support 3D projective texture.
  1741. texCoordDim = D3DTTFF_PROJECTED | D3DTTFF_COUNT4;
  1742. break;
  1743. }
  1744. }
  1745. else
  1746. {
  1747. switch (mTexStageDesc[stage].texType)
  1748. {
  1749. case D3D9Mappings::D3D_TEX_TYPE_NORMAL:
  1750. texCoordDim = D3DTTFF_COUNT2;
  1751. break;
  1752. case D3D9Mappings::D3D_TEX_TYPE_CUBE:
  1753. case D3D9Mappings::D3D_TEX_TYPE_VOLUME:
  1754. texCoordDim = D3DTTFF_COUNT3;
  1755. break;
  1756. }
  1757. }
  1758. hr = __SetTextureStageState( static_cast<DWORD>(stage), D3DTSS_TEXTURETRANSFORMFLAGS, texCoordDim );
  1759. if (FAILED(hr))
  1760. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set texture coord. dimension", "D3D9RenderSystem::_setTextureMatrix" );
  1761. hr = getActiveD3D9Device()->SetTransform( (D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0 + stage), &d3dMat );
  1762. if (FAILED(hr))
  1763. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set texture matrix", "D3D9RenderSystem::_setTextureMatrix" );
  1764. }
  1765. else
  1766. {
  1767. // disable all of this
  1768. hr = __SetTextureStageState( static_cast<DWORD>(stage), D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
  1769. if( FAILED( hr ) )
  1770. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to disable texture coordinate transform", "D3D9RenderSystem::_setTextureMatrix" );
  1771. // Needless to sets texture transform here, it's never used at all
  1772. }
  1773. }
  1774. //---------------------------------------------------------------------
  1775. void D3D9RenderSystem::_setTextureAddressingMode( size_t stage,
  1776. const TextureUnitState::UVWAddressingMode& uvw )
  1777. {
  1778. HRESULT hr;
  1779. if( FAILED( hr = __SetSamplerState( static_cast<DWORD>(stage), D3DSAMP_ADDRESSU, D3D9Mappings::get(uvw.u, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) ) ) )
  1780. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set texture addressing mode for U", "D3D9RenderSystem::_setTextureAddressingMode" );
  1781. if( FAILED( hr = __SetSamplerState( static_cast<DWORD>(stage), D3DSAMP_ADDRESSV, D3D9Mappings::get(uvw.v, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) ) ) )
  1782. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set texture addressing mode for V", "D3D9RenderSystem::_setTextureAddressingMode" );
  1783. if( FAILED( hr = __SetSamplerState( static_cast<DWORD>(stage), D3DSAMP_ADDRESSW, D3D9Mappings::get(uvw.w, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) ) ) )
  1784. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set texture addressing mode for W", "D3D9RenderSystem::_setTextureAddressingMode" );
  1785. }
  1786. //-----------------------------------------------------------------------------
  1787. void D3D9RenderSystem::_setTextureBorderColour(size_t stage,
  1788. const ColourValue& colour)
  1789. {
  1790. HRESULT hr;
  1791. if( FAILED( hr = __SetSamplerState( static_cast<DWORD>(stage), D3DSAMP_BORDERCOLOR, colour.getAsARGB()) ) )
  1792. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set texture border colour", "D3D9RenderSystem::_setTextureBorderColour" );
  1793. }
  1794. //---------------------------------------------------------------------
  1795. void D3D9RenderSystem::_setTextureBlendMode( size_t stage, const LayerBlendModeEx& bm )
  1796. {
  1797. HRESULT hr = S_OK;
  1798. D3DTEXTURESTAGESTATETYPE tss;
  1799. D3DCOLOR manualD3D;
  1800. // choose type of blend.
  1801. if( bm.blendType == LBT_COLOUR )
  1802. tss = D3DTSS_COLOROP;
  1803. else if( bm.blendType == LBT_ALPHA )
  1804. tss = D3DTSS_ALPHAOP;
  1805. else
  1806. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  1807. "Invalid blend type", "D3D9RenderSystem::_setTextureBlendMode");
  1808. // set manual factor if required by operation
  1809. if (bm.operation == LBX_BLEND_MANUAL)
  1810. {
  1811. hr = __SetRenderState( D3DRS_TEXTUREFACTOR, D3DXCOLOR(0.0, 0.0, 0.0, bm.factor) );
  1812. if (FAILED(hr))
  1813. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set manual factor", "D3D9RenderSystem::_setTextureBlendMode" );
  1814. }
  1815. // set operation
  1816. hr = __SetTextureStageState( static_cast<DWORD>(stage), tss, D3D9Mappings::get(bm.operation, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps()) );
  1817. if (FAILED(hr))
  1818. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set operation", "D3D9RenderSystem::_setTextureBlendMode" );
  1819. // choose source 1
  1820. if( bm.blendType == LBT_COLOUR )
  1821. {
  1822. tss = D3DTSS_COLORARG1;
  1823. manualD3D = D3DXCOLOR( bm.colourArg1.r, bm.colourArg1.g, bm.colourArg1.b, bm.colourArg1.a );
  1824. mManualBlendColours[stage][0] = bm.colourArg1;
  1825. }
  1826. else if( bm.blendType == LBT_ALPHA )
  1827. {
  1828. tss = D3DTSS_ALPHAARG1;
  1829. manualD3D = D3DXCOLOR( mManualBlendColours[stage][0].r,
  1830. mManualBlendColours[stage][0].g,
  1831. mManualBlendColours[stage][0].b, bm.alphaArg1 );
  1832. }
  1833. else
  1834. {
  1835. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  1836. "Invalid blend type", "D3D9RenderSystem::_setTextureBlendMode");
  1837. }
  1838. // Set manual factor if required
  1839. if (bm.source1 == LBS_MANUAL)
  1840. {
  1841. if (mCurrentCapabilities->hasCapability(RSC_PERSTAGECONSTANT))
  1842. {
  1843. // Per-stage state
  1844. hr = __SetTextureStageState(static_cast<DWORD>(stage), D3DTSS_CONSTANT, manualD3D);
  1845. }
  1846. else
  1847. {
  1848. // Global state
  1849. hr = __SetRenderState( D3DRS_TEXTUREFACTOR, manualD3D );
  1850. }
  1851. if (FAILED(hr))
  1852. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set manual factor", "D3D9RenderSystem::_setTextureBlendMode" );
  1853. }
  1854. // set source 1
  1855. hr = __SetTextureStageState( static_cast<DWORD>(stage), tss, D3D9Mappings::get(bm.source1, mCurrentCapabilities->hasCapability(RSC_PERSTAGECONSTANT)) );
  1856. if (FAILED(hr))
  1857. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set source1", "D3D9RenderSystem::_setTextureBlendMode" );
  1858. // choose source 2
  1859. if( bm.blendType == LBT_COLOUR )
  1860. {
  1861. tss = D3DTSS_COLORARG2;
  1862. manualD3D = D3DXCOLOR( bm.colourArg2.r, bm.colourArg2.g, bm.colourArg2.b, bm.colourArg2.a );
  1863. mManualBlendColours[stage][1] = bm.colourArg2;
  1864. }
  1865. else if( bm.blendType == LBT_ALPHA )
  1866. {
  1867. tss = D3DTSS_ALPHAARG2;
  1868. manualD3D = D3DXCOLOR( mManualBlendColours[stage][1].r,
  1869. mManualBlendColours[stage][1].g,
  1870. mManualBlendColours[stage][1].b,
  1871. bm.alphaArg2 );
  1872. }
  1873. // Set manual factor if required
  1874. if (bm.source2 == LBS_MANUAL)
  1875. {
  1876. if (mCurrentCapabilities->hasCapability(RSC_PERSTAGECONSTANT))
  1877. {
  1878. // Per-stage state
  1879. hr = __SetTextureStageState(static_cast<DWORD>(stage), D3DTSS_CONSTANT, manualD3D);
  1880. }
  1881. else
  1882. {
  1883. hr = __SetRenderState( D3DRS_TEXTUREFACTOR, manualD3D );
  1884. }
  1885. if (FAILED(hr))
  1886. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set manual factor", "D3D9RenderSystem::_setTextureBlendMode" );
  1887. }
  1888. // Now set source 2
  1889. hr = __SetTextureStageState( static_cast<DWORD>(stage), tss, D3D9Mappings::get(bm.source2, mCurrentCapabilities->hasCapability(RSC_PERSTAGECONSTANT)) );
  1890. if (FAILED(hr))
  1891. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set source 2", "D3D9RenderSystem::_setTextureBlendMode" );
  1892. // Set interpolation factor if lerping
  1893. if (bm.operation == LBX_BLEND_DIFFUSE_COLOUR &&
  1894. mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().TextureOpCaps & D3DTEXOPCAPS_LERP)
  1895. {
  1896. // choose source 0 (lerp factor)
  1897. if( bm.blendType == LBT_COLOUR )
  1898. {
  1899. tss = D3DTSS_COLORARG0;
  1900. }
  1901. else if( bm.blendType == LBT_ALPHA )
  1902. {
  1903. tss = D3DTSS_ALPHAARG0;
  1904. }
  1905. hr = __SetTextureStageState(static_cast<DWORD>(stage), tss, D3DTA_DIFFUSE);
  1906. if (FAILED(hr))
  1907. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set lerp source 0",
  1908. "D3D9RenderSystem::_setTextureBlendMode" );
  1909. }
  1910. }
  1911. //---------------------------------------------------------------------
  1912. void D3D9RenderSystem::_setSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op )
  1913. {
  1914. HRESULT hr;
  1915. if( sourceFactor == SBF_ONE && destFactor == SBF_ZERO)
  1916. {
  1917. if (FAILED(hr = __SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)))
  1918. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha blending option", "D3D9RenderSystem::_setSceneBlending" );
  1919. }
  1920. else
  1921. {
  1922. if (FAILED(hr = __SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)))
  1923. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha blending option", "D3D9RenderSystem::_setSceneBlending" );
  1924. if (FAILED(hr = __SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE)))
  1925. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set separate alpha blending option", "D3D9RenderSystem::_setSceneBlending" );
  1926. if( FAILED( hr = __SetRenderState( D3DRS_SRCBLEND, D3D9Mappings::get(sourceFactor) ) ) )
  1927. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set source blend", "D3D9RenderSystem::_setSceneBlending" );
  1928. if( FAILED( hr = __SetRenderState( D3DRS_DESTBLEND, D3D9Mappings::get(destFactor) ) ) )
  1929. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set destination blend", "D3D9RenderSystem::_setSceneBlending" );
  1930. }
  1931. if (FAILED(hr = __SetRenderState(D3DRS_BLENDOP, D3D9Mappings::get(op))))
  1932. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set scene blending operation option", "D3D9RenderSystem::_setSceneBlendingOperation" );
  1933. if (FAILED(hr = __SetRenderState(D3DRS_BLENDOPALPHA, D3D9Mappings::get(op))))
  1934. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set scene blending operation option", "D3D9RenderSystem::_setSceneBlendingOperation" );
  1935. }
  1936. //---------------------------------------------------------------------
  1937. void D3D9RenderSystem::_setSeparateSceneBlending( SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha,
  1938. SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp )
  1939. {
  1940. HRESULT hr;
  1941. if( sourceFactor == SBF_ONE && destFactor == SBF_ZERO &&
  1942. sourceFactorAlpha == SBF_ONE && destFactorAlpha == SBF_ZERO)
  1943. {
  1944. if (FAILED(hr = __SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE)))
  1945. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha blending option", "D3D9RenderSystem::_setSceneBlending" );
  1946. }
  1947. else
  1948. {
  1949. if (FAILED(hr = __SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE)))
  1950. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha blending option", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1951. if (FAILED(hr = __SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE)))
  1952. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set separate alpha blending option", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1953. if( FAILED( hr = __SetRenderState( D3DRS_SRCBLEND, D3D9Mappings::get(sourceFactor) ) ) )
  1954. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set source blend", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1955. if( FAILED( hr = __SetRenderState( D3DRS_DESTBLEND, D3D9Mappings::get(destFactor) ) ) )
  1956. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set destination blend", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1957. if( FAILED( hr = __SetRenderState( D3DRS_SRCBLENDALPHA, D3D9Mappings::get(sourceFactorAlpha) ) ) )
  1958. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha source blend", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1959. if( FAILED( hr = __SetRenderState( D3DRS_DESTBLENDALPHA, D3D9Mappings::get(destFactorAlpha) ) ) )
  1960. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha destination blend", "D3D9RenderSystem::_setSeperateSceneBlending" );
  1961. }
  1962. if (FAILED(hr = __SetRenderState(D3DRS_BLENDOP, D3D9Mappings::get(op))))
  1963. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set scene blending operation option", "D3D9RenderSystem::_setSceneBlendingOperation" );
  1964. if (FAILED(hr = __SetRenderState(D3DRS_BLENDOPALPHA, D3D9Mappings::get(alphaOp))))
  1965. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha scene blending operation option", "D3D9RenderSystem::_setSceneBlendingOperation" );
  1966. }
  1967. //---------------------------------------------------------------------
  1968. void D3D9RenderSystem::_setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage )
  1969. {
  1970. HRESULT hr;
  1971. bool a2c = false;
  1972. static bool lasta2c = false;
  1973. if (func != CMPF_ALWAYS_PASS)
  1974. {
  1975. if( FAILED( hr = __SetRenderState( D3DRS_ALPHATESTENABLE, TRUE ) ) )
  1976. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to enable alpha testing",
  1977. "D3D9RenderSystem::_setAlphaRejectSettings" );
  1978. a2c = alphaToCoverage;
  1979. }
  1980. else
  1981. {
  1982. if( FAILED( hr = __SetRenderState( D3DRS_ALPHATESTENABLE, FALSE ) ) )
  1983. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to disable alpha testing",
  1984. "D3D9RenderSystem::_setAlphaRejectSettings" );
  1985. }
  1986. // Set always just be sure
  1987. if( FAILED( hr = __SetRenderState( D3DRS_ALPHAFUNC, D3D9Mappings::get(func) ) ) )
  1988. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha reject function", "D3D9RenderSystem::_setAlphaRejectSettings" );
  1989. if( FAILED( hr = __SetRenderState( D3DRS_ALPHAREF, value ) ) )
  1990. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set render state D3DRS_ALPHAREF", "D3D9RenderSystem::_setAlphaRejectSettings" );
  1991. // Alpha to coverage
  1992. if (getCapabilities()->hasCapability(RSC_ALPHA_TO_COVERAGE))
  1993. {
  1994. // Vendor-specific hacks on renderstate, gotta love 'em
  1995. if (getCapabilities()->getVendor() == GPU_NVIDIA)
  1996. {
  1997. if (a2c)
  1998. {
  1999. if( FAILED( hr = __SetRenderState( D3DRS_ADAPTIVETESS_Y, (D3DFORMAT)MAKEFOURCC('A', 'T', 'O', 'C') ) ) )
  2000. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha to coverage option", "D3D9RenderSystem::_setAlphaRejectSettings" );
  2001. }
  2002. else
  2003. {
  2004. if( FAILED( hr = __SetRenderState( D3DRS_ADAPTIVETESS_Y, D3DFMT_UNKNOWN ) ) )
  2005. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha to coverage option", "D3D9RenderSystem::_setAlphaRejectSettings" );
  2006. }
  2007. }
  2008. else if ((getCapabilities()->getVendor() == GPU_ATI))
  2009. {
  2010. if (a2c)
  2011. {
  2012. if( FAILED( hr = __SetRenderState( D3DRS_POINTSIZE, MAKEFOURCC('A','2','M','1') ) ) )
  2013. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha to coverage option", "D3D9RenderSystem::_setAlphaRejectSettings" );
  2014. }
  2015. else
  2016. {
  2017. // discovered this through trial and error, seems to work
  2018. if( FAILED( hr = __SetRenderState( D3DRS_POINTSIZE, MAKEFOURCC('A','2','M','0') ) ) )
  2019. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set alpha to coverage option", "D3D9RenderSystem::_setAlphaRejectSettings" );
  2020. }
  2021. }
  2022. // no hacks available for any other vendors?
  2023. lasta2c = a2c;
  2024. }
  2025. }
  2026. //---------------------------------------------------------------------
  2027. void D3D9RenderSystem::_setCullingMode( CullingMode mode )
  2028. {
  2029. mCullingMode = mode;
  2030. HRESULT hr;
  2031. bool flip = ((mActiveRenderTarget->requiresTextureFlipping() && !mInvertVertexWinding) ||
  2032. (!mActiveRenderTarget->requiresTextureFlipping() && mInvertVertexWinding));
  2033. if( FAILED (hr = __SetRenderState(D3DRS_CULLMODE,
  2034. D3D9Mappings::get(mode, flip))) )
  2035. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set culling mode", "D3D9RenderSystem::_setCullingMode" );
  2036. }
  2037. //---------------------------------------------------------------------
  2038. void D3D9RenderSystem::_setDepthBufferParams( bool depthTest, bool depthWrite, CompareFunction depthFunction )
  2039. {
  2040. _setDepthBufferCheckEnabled( depthTest );
  2041. _setDepthBufferWriteEnabled( depthWrite );
  2042. _setDepthBufferFunction( depthFunction );
  2043. }
  2044. //---------------------------------------------------------------------
  2045. void D3D9RenderSystem::_setDepthBufferCheckEnabled( bool enabled )
  2046. {
  2047. HRESULT hr;
  2048. if( enabled )
  2049. {
  2050. // Use w-buffer if available and enabled
  2051. if( mWBuffer && mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_WBUFFER )
  2052. hr = __SetRenderState( D3DRS_ZENABLE, D3DZB_USEW );
  2053. else
  2054. hr = __SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
  2055. }
  2056. else
  2057. hr = __SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );
  2058. if( FAILED( hr ) )
  2059. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting depth buffer test state", "D3D9RenderSystem::_setDepthBufferCheckEnabled" );
  2060. }
  2061. //---------------------------------------------------------------------
  2062. void D3D9RenderSystem::_setDepthBufferWriteEnabled( bool enabled )
  2063. {
  2064. HRESULT hr;
  2065. if( FAILED( hr = __SetRenderState( D3DRS_ZWRITEENABLE, enabled ) ) )
  2066. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting depth buffer write state", "D3D9RenderSystem::_setDepthBufferWriteEnabled" );
  2067. }
  2068. //---------------------------------------------------------------------
  2069. void D3D9RenderSystem::_setDepthBufferFunction( CompareFunction func )
  2070. {
  2071. HRESULT hr;
  2072. if( FAILED( hr = __SetRenderState( D3DRS_ZFUNC, D3D9Mappings::get(func) ) ) )
  2073. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting depth buffer test function", "D3D9RenderSystem::_setDepthBufferFunction" );
  2074. }
  2075. //---------------------------------------------------------------------
  2076. void D3D9RenderSystem::_setDepthBias(float constantBias, float slopeScaleBias)
  2077. {
  2078. if ((mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_DEPTHBIAS) != 0)
  2079. {
  2080. // Negate bias since D3D is backward
  2081. // D3D also expresses the constant bias as an absolute value, rather than
  2082. // relative to minimum depth unit, so scale to fit
  2083. constantBias = -constantBias / 250000.0f;
  2084. HRESULT hr = __SetRenderState(D3DRS_DEPTHBIAS, FLOAT2DWORD(constantBias));
  2085. if (FAILED(hr))
  2086. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting constant depth bias",
  2087. "D3D9RenderSystem::_setDepthBias");
  2088. }
  2089. if ((mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS) != 0)
  2090. {
  2091. // Negate bias since D3D is backward
  2092. slopeScaleBias = -slopeScaleBias;
  2093. HRESULT hr = __SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, FLOAT2DWORD(slopeScaleBias));
  2094. if (FAILED(hr))
  2095. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting slope scale depth bias",
  2096. "D3D9RenderSystem::_setDepthBias");
  2097. }
  2098. }
  2099. //---------------------------------------------------------------------
  2100. void D3D9RenderSystem::_setColourBufferWriteEnabled(bool red, bool green,
  2101. bool blue, bool alpha)
  2102. {
  2103. DWORD val = 0;
  2104. if (red)
  2105. val |= D3DCOLORWRITEENABLE_RED;
  2106. if (green)
  2107. val |= D3DCOLORWRITEENABLE_GREEN;
  2108. if (blue)
  2109. val |= D3DCOLORWRITEENABLE_BLUE;
  2110. if (alpha)
  2111. val |= D3DCOLORWRITEENABLE_ALPHA;
  2112. HRESULT hr = __SetRenderState(D3DRS_COLORWRITEENABLE, val);
  2113. if (FAILED(hr))
  2114. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting colour write enable flags",
  2115. "D3D9RenderSystem::_setColourBufferWriteEnabled");
  2116. }
  2117. //---------------------------------------------------------------------
  2118. void D3D9RenderSystem::_setFog( FogMode mode, const ColourValue& colour, Real densitiy, Real start, Real end )
  2119. {
  2120. HRESULT hr;
  2121. D3DRENDERSTATETYPE fogType, fogTypeNot;
  2122. if (mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().RasterCaps & D3DPRASTERCAPS_FOGTABLE)
  2123. {
  2124. fogType = D3DRS_FOGTABLEMODE;
  2125. fogTypeNot = D3DRS_FOGVERTEXMODE;
  2126. }
  2127. else
  2128. {
  2129. fogType = D3DRS_FOGVERTEXMODE;
  2130. fogTypeNot = D3DRS_FOGTABLEMODE;
  2131. }
  2132. if( mode == FOG_NONE)
  2133. {
  2134. // just disable
  2135. hr = __SetRenderState(fogType, D3DFOG_NONE );
  2136. hr = __SetRenderState(D3DRS_FOGENABLE, FALSE);
  2137. }
  2138. else
  2139. {
  2140. // Allow fog
  2141. hr = __SetRenderState( D3DRS_FOGENABLE, TRUE );
  2142. hr = __SetRenderState( fogTypeNot, D3DFOG_NONE );
  2143. hr = __SetRenderState( fogType, D3D9Mappings::get(mode) );
  2144. hr = __SetRenderState( D3DRS_FOGCOLOR, colour.getAsARGB() );
  2145. hr = __SetFloatRenderState( D3DRS_FOGSTART, start );
  2146. hr = __SetFloatRenderState( D3DRS_FOGEND, end );
  2147. hr = __SetFloatRenderState( D3DRS_FOGDENSITY, densitiy );
  2148. }
  2149. if( FAILED( hr ) )
  2150. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting render state", "D3D9RenderSystem::_setFog" );
  2151. }
  2152. //---------------------------------------------------------------------
  2153. void D3D9RenderSystem::_setPolygonMode(PolygonMode level)
  2154. {
  2155. HRESULT hr = __SetRenderState(D3DRS_FILLMODE, D3D9Mappings::get(level));
  2156. if (FAILED(hr))
  2157. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting polygon mode.", "D3D9RenderSystem::setPolygonMode");
  2158. }
  2159. //---------------------------------------------------------------------
  2160. void D3D9RenderSystem::setStencilCheckEnabled(bool enabled)
  2161. {
  2162. // Allow stencilling
  2163. HRESULT hr = __SetRenderState(D3DRS_STENCILENABLE, enabled);
  2164. if (FAILED(hr))
  2165. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error enabling / disabling stencilling.",
  2166. "D3D9RenderSystem::setStencilCheckEnabled");
  2167. }
  2168. //---------------------------------------------------------------------
  2169. void D3D9RenderSystem::setStencilBufferParams(CompareFunction func,
  2170. uint32 refValue, uint32 mask, StencilOperation stencilFailOp,
  2171. StencilOperation depthFailOp, StencilOperation passOp,
  2172. bool twoSidedOperation)
  2173. {
  2174. HRESULT hr;
  2175. bool flip;
  2176. // 2-sided operation
  2177. if (twoSidedOperation)
  2178. {
  2179. if (!mCurrentCapabilities->hasCapability(RSC_TWO_SIDED_STENCIL))
  2180. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "2-sided stencils are not supported",
  2181. "D3D9RenderSystem::setStencilBufferParams");
  2182. hr = __SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
  2183. if (FAILED(hr))
  2184. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting 2-sided stencil mode.",
  2185. "D3D9RenderSystem::setStencilBufferParams");
  2186. // NB: We should always treat CCW as front face for consistent with default
  2187. // culling mode. Therefore, we must take care with two-sided stencil settings.
  2188. flip = (mInvertVertexWinding && mActiveRenderTarget->requiresTextureFlipping()) ||
  2189. (!mInvertVertexWinding && !mActiveRenderTarget->requiresTextureFlipping());
  2190. // Set alternative versions of ops
  2191. // fail op
  2192. hr = __SetRenderState(D3DRS_CCW_STENCILFAIL, D3D9Mappings::get(stencilFailOp, !flip));
  2193. if (FAILED(hr))
  2194. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil fail operation (2-sided).",
  2195. "D3D9RenderSystem::setStencilBufferParams");
  2196. // depth fail op
  2197. hr = __SetRenderState(D3DRS_CCW_STENCILZFAIL, D3D9Mappings::get(depthFailOp, !flip));
  2198. if (FAILED(hr))
  2199. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil depth fail operation (2-sided).",
  2200. "D3D9RenderSystem::setStencilBufferParams");
  2201. // pass op
  2202. hr = __SetRenderState(D3DRS_CCW_STENCILPASS, D3D9Mappings::get(passOp, !flip));
  2203. if (FAILED(hr))
  2204. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil pass operation (2-sided).",
  2205. "D3D9RenderSystem::setStencilBufferParams");
  2206. }
  2207. else
  2208. {
  2209. hr = __SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
  2210. if (FAILED(hr))
  2211. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting 1-sided stencil mode.",
  2212. "D3D9RenderSystem::setStencilBufferParams");
  2213. flip = false;
  2214. }
  2215. // func
  2216. hr = __SetRenderState(D3DRS_STENCILFUNC, D3D9Mappings::get(func));
  2217. if (FAILED(hr))
  2218. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil buffer test function.",
  2219. "D3D9RenderSystem::setStencilBufferParams");
  2220. // reference value
  2221. hr = __SetRenderState(D3DRS_STENCILREF, refValue);
  2222. if (FAILED(hr))
  2223. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil buffer reference value.",
  2224. "D3D9RenderSystem::setStencilBufferParams");
  2225. // mask
  2226. hr = __SetRenderState(D3DRS_STENCILMASK, mask);
  2227. if (FAILED(hr))
  2228. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil buffer mask.",
  2229. "D3D9RenderSystem::setStencilBufferParams");
  2230. // fail op
  2231. hr = __SetRenderState(D3DRS_STENCILFAIL, D3D9Mappings::get(stencilFailOp, flip));
  2232. if (FAILED(hr))
  2233. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil fail operation.",
  2234. "D3D9RenderSystem::setStencilBufferParams");
  2235. // depth fail op
  2236. hr = __SetRenderState(D3DRS_STENCILZFAIL, D3D9Mappings::get(depthFailOp, flip));
  2237. if (FAILED(hr))
  2238. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil depth fail operation.",
  2239. "D3D9RenderSystem::setStencilBufferParams");
  2240. // pass op
  2241. hr = __SetRenderState(D3DRS_STENCILPASS, D3D9Mappings::get(passOp, flip));
  2242. if (FAILED(hr))
  2243. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error setting stencil pass operation.",
  2244. "D3D9RenderSystem::setStencilBufferParams");
  2245. }
  2246. //---------------------------------------------------------------------
  2247. void D3D9RenderSystem::_setTextureUnitFiltering(size_t unit, FilterType ftype,
  2248. FilterOptions filter)
  2249. {
  2250. HRESULT hr;
  2251. D3D9Mappings::eD3DTexType texType = mTexStageDesc[unit].texType;
  2252. hr = __SetSamplerState( static_cast<DWORD>(unit), D3D9Mappings::get(ftype),
  2253. D3D9Mappings::get(ftype, filter, mDeviceManager->getActiveDevice()->getD3D9DeviceCaps(), texType));
  2254. if (FAILED(hr))
  2255. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set texture filter ", "D3D9RenderSystem::_setTextureUnitFiltering");
  2256. }
  2257. //---------------------------------------------------------------------
  2258. DWORD D3D9RenderSystem::_getCurrentAnisotropy(size_t unit)
  2259. {
  2260. DWORD oldVal;
  2261. getActiveD3D9Device()->GetSamplerState(static_cast<DWORD>(unit), D3DSAMP_MAXANISOTROPY, &oldVal);
  2262. return oldVal;
  2263. }
  2264. //---------------------------------------------------------------------
  2265. void D3D9RenderSystem::_setTextureLayerAnisotropy(size_t unit, unsigned int maxAnisotropy)
  2266. {
  2267. if (static_cast<DWORD>(maxAnisotropy) > mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().MaxAnisotropy)
  2268. maxAnisotropy = mDeviceManager->getActiveDevice()->getD3D9DeviceCaps().MaxAnisotropy;
  2269. if (_getCurrentAnisotropy(unit) != maxAnisotropy)
  2270. __SetSamplerState( static_cast<DWORD>(unit), D3DSAMP_MAXANISOTROPY, maxAnisotropy );
  2271. }
  2272. //---------------------------------------------------------------------
  2273. HRESULT D3D9RenderSystem::__SetRenderState(D3DRENDERSTATETYPE state, DWORD value)
  2274. {
  2275. HRESULT hr;
  2276. DWORD oldVal;
  2277. if ( FAILED( hr = getActiveD3D9Device()->GetRenderState(state, &oldVal) ) )
  2278. return hr;
  2279. if ( oldVal == value )
  2280. return D3D_OK;
  2281. else
  2282. return getActiveD3D9Device()->SetRenderState(state, value);
  2283. }
  2284. //---------------------------------------------------------------------
  2285. HRESULT D3D9RenderSystem::__SetSamplerState(DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD value)
  2286. {
  2287. HRESULT hr;
  2288. DWORD oldVal;
  2289. if ( FAILED( hr = getActiveD3D9Device()->GetSamplerState(sampler, type, &oldVal) ) )
  2290. return hr;
  2291. if ( oldVal == value )
  2292. return D3D_OK;
  2293. else
  2294. return getActiveD3D9Device()->SetSamplerState(sampler, type, value);
  2295. }
  2296. //---------------------------------------------------------------------
  2297. HRESULT D3D9RenderSystem::__SetTextureStageState(DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
  2298. {
  2299. HRESULT hr;
  2300. DWORD oldVal;
  2301. // can only set fixed-function texture stage state
  2302. if (stage < 8)
  2303. {
  2304. if ( FAILED( hr = getActiveD3D9Device()->GetTextureStageState(stage, type, &oldVal) ) )
  2305. return hr;
  2306. if ( oldVal == value )
  2307. return D3D_OK;
  2308. else
  2309. return getActiveD3D9Device()->SetTextureStageState(stage, type, value);
  2310. }
  2311. else
  2312. {
  2313. return D3D_OK;
  2314. }
  2315. }
  2316. //---------------------------------------------------------------------
  2317. void D3D9RenderSystem::_setRenderTarget(RenderTarget *target)
  2318. {
  2319. mActiveRenderTarget = target;
  2320. HRESULT hr;
  2321. // If this is called without going through RenderWindow::update, then
  2322. // the device will not have been set. Calling it twice is safe, the
  2323. // implementation ensures nothing happens if the same device is set twice
  2324. if (std::find(mRenderWindows.begin(), mRenderWindows.end(), target) != mRenderWindows.end())
  2325. {
  2326. D3D9RenderWindow *window = static_cast<D3D9RenderWindow*>(target);
  2327. mDeviceManager->setActiveRenderTargetDevice(window->getDevice());
  2328. // also make sure we validate the device; if this never went
  2329. // through update() it won't be set
  2330. window->_validateDevice();
  2331. }
  2332. // Retrieve render surfaces (up to OGRE_MAX_MULTIPLE_RENDER_TARGETS)
  2333. IDirect3DSurface9* pBack[OGRE_MAX_MULTIPLE_RENDER_TARGETS];
  2334. memset(pBack, 0, sizeof(pBack));
  2335. target->getCustomAttribute( "DDBACKBUFFER", &pBack );
  2336. if (!pBack[0])
  2337. return;
  2338. IDirect3DSurface9* pDepth = NULL;
  2339. //Check if we saved a depth buffer for this target
  2340. TargetDepthStencilMap::iterator savedTexture = mCheckedOutTextures.find(target);
  2341. if (savedTexture != mCheckedOutTextures.end())
  2342. {
  2343. pDepth = savedTexture->second.surface;
  2344. }
  2345. if (!pDepth)
  2346. target->getCustomAttribute( "D3DZBUFFER", &pDepth );
  2347. if (!pDepth)
  2348. {
  2349. /// No depth buffer provided, use our own
  2350. /// Request a depth stencil that is compatible with the format, multisample type and
  2351. /// dimensions of the render target.
  2352. D3DSURFACE_DESC srfDesc;
  2353. if(FAILED(pBack[0]->GetDesc(&srfDesc)))
  2354. return; // ?
  2355. pDepth = _getDepthStencilFor(srfDesc.Format, srfDesc.MultiSampleType, srfDesc.MultiSampleQuality, srfDesc.Width, srfDesc.Height);
  2356. }
  2357. // Bind render targets
  2358. uint count = mCurrentCapabilities->getNumMultiRenderTargets();
  2359. for(uint x=0; x<count; ++x)
  2360. {
  2361. hr = getActiveD3D9Device()->SetRenderTarget(x, pBack[x]);
  2362. if (FAILED(hr))
  2363. {
  2364. String msg = DXGetErrorDescription(hr);
  2365. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to setRenderTarget : " + msg, "D3D9RenderSystem::_setViewport" );
  2366. }
  2367. }
  2368. hr = getActiveD3D9Device()->SetDepthStencilSurface(pDepth);
  2369. if (FAILED(hr))
  2370. {
  2371. String msg = DXGetErrorDescription(hr);
  2372. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to setDepthStencil : " + msg, "D3D9RenderSystem::_setViewport" );
  2373. }
  2374. }
  2375. //---------------------------------------------------------------------
  2376. void D3D9RenderSystem::_setViewport( Viewport *vp )
  2377. {
  2378. if( vp != mActiveViewport || vp->_isUpdated() )
  2379. {
  2380. mActiveViewport = vp;
  2381. // ok, it's different, time to set render target and viewport params
  2382. D3DVIEWPORT9 d3dvp;
  2383. HRESULT hr;
  2384. // Set render target
  2385. RenderTarget* target = vp->getTarget();
  2386. _setRenderTarget(target);
  2387. _setCullingMode( mCullingMode );
  2388. // set viewport dimensions
  2389. d3dvp.X = vp->getActualLeft();
  2390. d3dvp.Y = vp->getActualTop();
  2391. d3dvp.Width = vp->getActualWidth();
  2392. d3dvp.Height = vp->getActualHeight();
  2393. if (target->requiresTextureFlipping())
  2394. {
  2395. // Convert "top-left" to "bottom-left"
  2396. d3dvp.Y = target->getHeight() - d3dvp.Height - d3dvp.Y;
  2397. }
  2398. // Z-values from 0.0 to 1.0 (TODO: standardise with OpenGL)
  2399. d3dvp.MinZ = 0.0f;
  2400. d3dvp.MaxZ = 1.0f;
  2401. if( FAILED( hr = getActiveD3D9Device()->SetViewport( &d3dvp ) ) )
  2402. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set viewport.", "D3D9RenderSystem::_setViewport" );
  2403. // Set sRGB write mode
  2404. __SetRenderState(D3DRS_SRGBWRITEENABLE, target->isHardwareGammaEnabled());
  2405. vp->_clearUpdatedFlag();
  2406. }
  2407. }
  2408. //---------------------------------------------------------------------
  2409. void D3D9RenderSystem::_beginFrame()
  2410. {
  2411. HRESULT hr;
  2412. if( !mActiveViewport )
  2413. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Cannot begin frame - no viewport selected.", "D3D9RenderSystem::_beginFrame" );
  2414. if( FAILED( hr = getActiveD3D9Device()->BeginScene() ) )
  2415. {
  2416. String msg = DXGetErrorDescription(hr);
  2417. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error beginning frame :" + msg, "D3D9RenderSystem::_beginFrame" );
  2418. }
  2419. mLastVertexSourceCount = 0;
  2420. // Clear left overs of previous viewport.
  2421. // I.E: Viewport A can use 3 different textures and light states
  2422. // When trying to render viewport B these settings should be cleared, otherwise
  2423. // graphical artifacts might occur.
  2424. mDeviceManager->getActiveDevice()->clearDeviceStreams();
  2425. }
  2426. //---------------------------------------------------------------------
  2427. void D3D9RenderSystem::_endFrame()
  2428. {
  2429. HRESULT hr;
  2430. if( FAILED( hr = getActiveD3D9Device()->EndScene() ) )
  2431. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error ending frame", "D3D9RenderSystem::_endFrame" );
  2432. mDeviceManager->destroyInactiveRenderDevices();
  2433. }
  2434. //---------------------------------------------------------------------
  2435. struct D3D9RenderContext : public RenderSystem::RenderSystemContext
  2436. {
  2437. RenderTarget* target;
  2438. };
  2439. //---------------------------------------------------------------------
  2440. D3D9RenderSystem::ZBufferIdentifier D3D9RenderSystem::getZBufferIdentifier(RenderTarget* rt)
  2441. {
  2442. // Retrieve render surfaces (up to OGRE_MAX_MULTIPLE_RENDER_TARGETS)
  2443. IDirect3DSurface9* pBack[OGRE_MAX_MULTIPLE_RENDER_TARGETS];
  2444. memset(pBack, 0, sizeof(pBack));
  2445. rt->getCustomAttribute( "DDBACKBUFFER", &pBack );
  2446. assert(pBack[0]);
  2447. /// Request a depth stencil that is compatible with the format, multisample type and
  2448. /// dimensions of the render target.
  2449. D3DSURFACE_DESC srfDesc;
  2450. HRESULT hr = pBack[0]->GetDesc(&srfDesc);
  2451. assert(!(FAILED(hr)));
  2452. D3DFORMAT dsfmt = _getDepthStencilFormatFor(srfDesc.Format);
  2453. assert(dsfmt != D3DFMT_UNKNOWN);
  2454. /// Build identifier and return
  2455. ZBufferIdentifier zBufferIdentifier;
  2456. zBufferIdentifier.format = dsfmt;
  2457. zBufferIdentifier.multisampleType = srfDesc.MultiSampleType;
  2458. zBufferIdentifier.device = getActiveD3D9Device();
  2459. return zBufferIdentifier;
  2460. }
  2461. //---------------------------------------------------------------------
  2462. RenderSystem::RenderSystemContext* D3D9RenderSystem::_pauseFrame(void)
  2463. {
  2464. //Stop rendering
  2465. _endFrame();
  2466. D3D9RenderContext* context = new D3D9RenderContext;
  2467. context->target = mActiveRenderTarget;
  2468. //Don't do this to backbuffers. Is there a more elegant way to check?
  2469. if (!dynamic_cast<D3D9RenderWindow*>(mActiveRenderTarget))
  2470. {
  2471. //Get the matching z buffer identifier and queue
  2472. ZBufferIdentifier zBufferIdentifier = getZBufferIdentifier(mActiveRenderTarget);
  2473. ZBufferRefQueue& zBuffers = mZBufferHash[zBufferIdentifier];
  2474. #ifdef OGRE_DEBUG_MODE
  2475. //Check that queue handling works as expected
  2476. IDirect3DSurface9* pDepth;
  2477. getActiveD3D9Device()->GetDepthStencilSurface(&pDepth);
  2478. // Release immediately -> each get increase the ref count.
  2479. if (pDepth != NULL)
  2480. pDepth->Release();
  2481. assert(zBuffers.front().surface == pDepth);
  2482. #endif
  2483. //Store the depth buffer in the side and remove it from the queue
  2484. mCheckedOutTextures[mActiveRenderTarget] = zBuffers.front();
  2485. zBuffers.pop_front();
  2486. }
  2487. return context;
  2488. }
  2489. //---------------------------------------------------------------------
  2490. void D3D9RenderSystem::_resumeFrame(RenderSystemContext* context)
  2491. {
  2492. //Resume rendering
  2493. _beginFrame();
  2494. D3D9RenderContext* d3dContext = static_cast<D3D9RenderContext*>(context);
  2495. //Don't do this to backbuffers. Is there a more elegant way to check?
  2496. if (!dynamic_cast<D3D9RenderWindow*>(d3dContext->target))
  2497. {
  2498. ///Find the stored depth buffer
  2499. ZBufferIdentifier zBufferIdentifier = getZBufferIdentifier(d3dContext->target);
  2500. ZBufferRefQueue& zBuffers = mZBufferHash[zBufferIdentifier];
  2501. assert(mCheckedOutTextures.find(d3dContext->target) != mCheckedOutTextures.end());
  2502. //Return it to the general queue
  2503. zBuffers.push_front(mCheckedOutTextures[d3dContext->target]);
  2504. mCheckedOutTextures.erase(d3dContext->target);
  2505. }
  2506. delete context;
  2507. }
  2508. //---------------------------------------------------------------------
  2509. void D3D9RenderSystem::setVertexDeclaration(VertexDeclaration* decl)
  2510. {
  2511. HRESULT hr;
  2512. D3D9VertexDeclaration* d3ddecl =
  2513. static_cast<D3D9VertexDeclaration*>(decl);
  2514. if (FAILED(hr = getActiveD3D9Device()->SetVertexDeclaration(d3ddecl->getD3DVertexDeclaration())))
  2515. {
  2516. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set D3D9 vertex declaration",
  2517. "D3D9RenderSystem::setVertexDeclaration");
  2518. }
  2519. }
  2520. //---------------------------------------------------------------------
  2521. void D3D9RenderSystem::setVertexBufferBinding(VertexBufferBinding* binding)
  2522. {
  2523. HRESULT hr;
  2524. // TODO: attempt to detect duplicates
  2525. const VertexBufferBinding::VertexBufferBindingMap& binds = binding->getBindings();
  2526. VertexBufferBinding::VertexBufferBindingMap::const_iterator i, iend;
  2527. size_t source = 0;
  2528. iend = binds.end();
  2529. for (i = binds.begin(); i != iend; ++i, ++source)
  2530. {
  2531. // Unbind gap sources
  2532. for ( ; source < i->first; ++source)
  2533. {
  2534. hr = getActiveD3D9Device()->SetStreamSource(static_cast<UINT>(source), NULL, 0, 0);
  2535. if (FAILED(hr))
  2536. {
  2537. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to reset unused D3D9 stream source",
  2538. "D3D9RenderSystem::setVertexBufferBinding");
  2539. }
  2540. }
  2541. D3D9HardwareVertexBuffer* d3d9buf =
  2542. static_cast<D3D9HardwareVertexBuffer*>(i->second.get());
  2543. hr = getActiveD3D9Device()->SetStreamSource(
  2544. static_cast<UINT>(source),
  2545. d3d9buf->getD3D9VertexBuffer(),
  2546. 0, // no stream offset, this is handled in _render instead
  2547. static_cast<UINT>(d3d9buf->getVertexSize()) // stride
  2548. );
  2549. if (FAILED(hr))
  2550. {
  2551. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set D3D9 stream source for buffer binding",
  2552. "D3D9RenderSystem::setVertexBufferBinding");
  2553. }
  2554. }
  2555. // Unbind any unused sources
  2556. for (size_t unused = source; unused < mLastVertexSourceCount; ++unused)
  2557. {
  2558. hr = getActiveD3D9Device()->SetStreamSource(static_cast<UINT>(unused), NULL, 0, 0);
  2559. if (FAILED(hr))
  2560. {
  2561. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to reset unused D3D9 stream source",
  2562. "D3D9RenderSystem::setVertexBufferBinding");
  2563. }
  2564. }
  2565. mLastVertexSourceCount = source;
  2566. }
  2567. //---------------------------------------------------------------------
  2568. void D3D9RenderSystem::_render(const RenderOperation& op)
  2569. {
  2570. // Exit immediately if there is nothing to render
  2571. // This caused a problem on FireGL 8800
  2572. if (op.vertexData->vertexCount == 0)
  2573. return;
  2574. // Call super class
  2575. RenderSystem::_render(op);
  2576. // To think about: possibly remove setVertexDeclaration and
  2577. // setVertexBufferBinding from RenderSystem since the sequence is
  2578. // a bit too D3D9-specific?
  2579. setVertexDeclaration(op.vertexData->vertexDeclaration);
  2580. setVertexBufferBinding(op.vertexData->vertexBufferBinding);
  2581. // Determine rendering operation
  2582. D3DPRIMITIVETYPE primType = D3DPT_TRIANGLELIST;
  2583. DWORD primCount = 0;
  2584. switch( op.operationType )
  2585. {
  2586. case RenderOperation::OT_POINT_LIST:
  2587. primType = D3DPT_POINTLIST;
  2588. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount);
  2589. break;
  2590. case RenderOperation::OT_LINE_LIST:
  2591. primType = D3DPT_LINELIST;
  2592. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount) / 2;
  2593. break;
  2594. case RenderOperation::OT_LINE_STRIP:
  2595. primType = D3DPT_LINESTRIP;
  2596. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount) - 1;
  2597. break;
  2598. case RenderOperation::OT_TRIANGLE_LIST:
  2599. primType = D3DPT_TRIANGLELIST;
  2600. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount) / 3;
  2601. break;
  2602. case RenderOperation::OT_TRIANGLE_STRIP:
  2603. primType = D3DPT_TRIANGLESTRIP;
  2604. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount) - 2;
  2605. break;
  2606. case RenderOperation::OT_TRIANGLE_FAN:
  2607. primType = D3DPT_TRIANGLEFAN;
  2608. primCount = static_cast<DWORD>(op.useIndexes ? op.indexData->indexCount : op.vertexData->vertexCount) - 2;
  2609. break;
  2610. }
  2611. if (!primCount)
  2612. return;
  2613. // Issue the op
  2614. HRESULT hr;
  2615. if( op.useIndexes )
  2616. {
  2617. D3D9HardwareIndexBuffer* d3dIdxBuf =
  2618. static_cast<D3D9HardwareIndexBuffer*>(op.indexData->indexBuffer.get());
  2619. hr = getActiveD3D9Device()->SetIndices( d3dIdxBuf->getD3DIndexBuffer() );
  2620. if (FAILED(hr))
  2621. {
  2622. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to set index buffer", "D3D9RenderSystem::_render" );
  2623. }
  2624. do
  2625. {
  2626. // Update derived depth bias
  2627. if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
  2628. {
  2629. _setDepthBias(mDerivedDepthBiasBase +
  2630. mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
  2631. mDerivedDepthBiasSlopeScale);
  2632. }
  2633. // do indexed draw operation
  2634. hr = getActiveD3D9Device()->DrawIndexedPrimitive(
  2635. primType,
  2636. static_cast<INT>(op.vertexData->vertexStart),
  2637. 0, // Min vertex index - assume we can go right down to 0
  2638. static_cast<UINT>(op.vertexData->vertexCount),
  2639. static_cast<UINT>(op.indexData->indexStart),
  2640. static_cast<UINT>(primCount)
  2641. );
  2642. } while (updatePassIterationRenderState());
  2643. }
  2644. else
  2645. {
  2646. // nfz: gpu_iterate
  2647. do
  2648. {
  2649. // Update derived depth bias
  2650. if (mDerivedDepthBias && mCurrentPassIterationNum > 0)
  2651. {
  2652. _setDepthBias(mDerivedDepthBiasBase +
  2653. mDerivedDepthBiasMultiplier * mCurrentPassIterationNum,
  2654. mDerivedDepthBiasSlopeScale);
  2655. }
  2656. // Unindexed, a little simpler!
  2657. hr = getActiveD3D9Device()->DrawPrimitive(
  2658. primType,
  2659. static_cast<UINT>(op.vertexData->vertexStart),
  2660. static_cast<UINT>(primCount)
  2661. );
  2662. } while (updatePassIterationRenderState());
  2663. }
  2664. if( FAILED( hr ) )
  2665. {
  2666. String msg = DXGetErrorDescription(hr);
  2667. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Failed to DrawPrimitive : " + msg, "D3D9RenderSystem::_render" );
  2668. }
  2669. }
  2670. //---------------------------------------------------------------------
  2671. void D3D9RenderSystem::setNormaliseNormals(bool normalise)
  2672. {
  2673. __SetRenderState(D3DRS_NORMALIZENORMALS,
  2674. normalise ? TRUE : FALSE);
  2675. }
  2676. //---------------------------------------------------------------------
  2677. void D3D9RenderSystem::bindGpuProgram(GpuProgram* prg)
  2678. {
  2679. HRESULT hr;
  2680. switch (prg->getType())
  2681. {
  2682. case GPT_VERTEX_PROGRAM:
  2683. hr = getActiveD3D9Device()->SetVertexShader(
  2684. static_cast<D3D9GpuVertexProgram*>(prg)->getVertexShader());
  2685. if (FAILED(hr))
  2686. {
  2687. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error calling SetVertexShader", "D3D9RenderSystem::bindGpuProgram");
  2688. }
  2689. break;
  2690. case GPT_FRAGMENT_PROGRAM:
  2691. hr = getActiveD3D9Device()->SetPixelShader(
  2692. static_cast<D3D9GpuFragmentProgram*>(prg)->getPixelShader());
  2693. if (FAILED(hr))
  2694. {
  2695. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error calling SetPixelShader", "D3D9RenderSystem::bindGpuProgram");
  2696. }
  2697. break;
  2698. };
  2699. // Make sure texcoord index is equal to stage value, As SDK Doc suggests:
  2700. // "When rendering using vertex shaders, each stage's texture coordinate index must be set to its default value."
  2701. // This solves such an errors when working with the Debug runtime -
  2702. // "Direct3D9: (ERROR) :Stage 1 - Texture coordinate index in the stage must be equal to the stage index when programmable vertex pipeline is used".
  2703. for (unsigned int nStage=0; nStage < 8; ++nStage)
  2704. __SetTextureStageState(nStage, D3DTSS_TEXCOORDINDEX, nStage);
  2705. RenderSystem::bindGpuProgram(prg);
  2706. }
  2707. //---------------------------------------------------------------------
  2708. void D3D9RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  2709. {
  2710. HRESULT hr;
  2711. switch(gptype)
  2712. {
  2713. case GPT_VERTEX_PROGRAM:
  2714. mActiveVertexGpuProgramParameters.setNull();
  2715. hr = getActiveD3D9Device()->SetVertexShader(NULL);
  2716. if (FAILED(hr))
  2717. {
  2718. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error resetting SetVertexShader to NULL",
  2719. "D3D9RenderSystem::unbindGpuProgram");
  2720. }
  2721. break;
  2722. case GPT_FRAGMENT_PROGRAM:
  2723. mActiveFragmentGpuProgramParameters.setNull();
  2724. hr = getActiveD3D9Device()->SetPixelShader(NULL);
  2725. if (FAILED(hr))
  2726. {
  2727. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error resetting SetPixelShader to NULL",
  2728. "D3D9RenderSystem::unbindGpuProgram");
  2729. }
  2730. break;
  2731. };
  2732. RenderSystem::unbindGpuProgram(gptype);
  2733. }
  2734. //---------------------------------------------------------------------
  2735. void D3D9RenderSystem::bindGpuProgramParameters(GpuProgramType gptype,
  2736. GpuProgramParametersSharedPtr params, uint16 variability)
  2737. {
  2738. // special case pass iteration
  2739. if (variability == (uint16)GPV_PASS_ITERATION_NUMBER)
  2740. {
  2741. bindGpuProgramPassIterationParameters(gptype);
  2742. return;
  2743. }
  2744. if (variability & (uint16)GPV_GLOBAL)
  2745. {
  2746. // D3D9 doesn't support shared constant buffers, so use copy routine
  2747. params->_copySharedParams();
  2748. }
  2749. HRESULT hr;
  2750. GpuLogicalBufferStructPtr floatLogical = params->getFloatLogicalBufferStruct();
  2751. GpuLogicalBufferStructPtr intLogical = params->getIntLogicalBufferStruct();
  2752. switch(gptype)
  2753. {
  2754. case GPT_VERTEX_PROGRAM:
  2755. mActiveVertexGpuProgramParameters = params;
  2756. {
  2757. OGRE_LOCK_MUTEX(floatLogical->mutex)
  2758. for (GpuLogicalIndexUseMap::const_iterator i = floatLogical->map.begin();
  2759. i != floatLogical->map.end(); ++i)
  2760. {
  2761. if (i->second.variability & variability)
  2762. {
  2763. size_t logicalIndex = i->first;
  2764. const float* pFloat = params->getFloatPointer(i->second.physicalIndex);
  2765. size_t slotCount = i->second.currentSize / 4;
  2766. assert (i->second.currentSize % 4 == 0 && "Should not have any "
  2767. "elements less than 4 wide for D3D9");
  2768. if (FAILED(hr = getActiveD3D9Device()->SetVertexShaderConstantF(
  2769. (UINT)logicalIndex, pFloat, (UINT)slotCount)))
  2770. {
  2771. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2772. "Unable to upload vertex shader float parameters",
  2773. "D3D9RenderSystem::bindGpuProgramParameters");
  2774. }
  2775. }
  2776. }
  2777. }
  2778. // bind ints
  2779. {
  2780. OGRE_LOCK_MUTEX(intLogical->mutex)
  2781. for (GpuLogicalIndexUseMap::const_iterator i = intLogical->map.begin();
  2782. i != intLogical->map.end(); ++i)
  2783. {
  2784. if (i->second.variability & variability)
  2785. {
  2786. size_t logicalIndex = i->first;
  2787. const int* pInt = params->getIntPointer(i->second.physicalIndex);
  2788. size_t slotCount = i->second.currentSize / 4;
  2789. assert (i->second.currentSize % 4 == 0 && "Should not have any "
  2790. "elements less than 4 wide for D3D9");
  2791. if (FAILED(hr = getActiveD3D9Device()->SetVertexShaderConstantI(
  2792. static_cast<UINT>(logicalIndex), pInt, static_cast<UINT>(slotCount))))
  2793. {
  2794. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2795. "Unable to upload vertex shader int parameters",
  2796. "D3D9RenderSystem::bindGpuProgramParameters");
  2797. }
  2798. }
  2799. }
  2800. }
  2801. break;
  2802. case GPT_FRAGMENT_PROGRAM:
  2803. mActiveFragmentGpuProgramParameters = params;
  2804. {
  2805. OGRE_LOCK_MUTEX(floatLogical->mutex)
  2806. for (GpuLogicalIndexUseMap::const_iterator i = floatLogical->map.begin();
  2807. i != floatLogical->map.end(); ++i)
  2808. {
  2809. if (i->second.variability & variability)
  2810. {
  2811. size_t logicalIndex = i->first;
  2812. const float* pFloat = params->getFloatPointer(i->second.physicalIndex);
  2813. size_t slotCount = i->second.currentSize / 4;
  2814. assert (i->second.currentSize % 4 == 0 && "Should not have any "
  2815. "elements less than 4 wide for D3D9");
  2816. if (FAILED(hr = getActiveD3D9Device()->SetPixelShaderConstantF(
  2817. static_cast<UINT>(logicalIndex), pFloat, static_cast<UINT>(slotCount))))
  2818. {
  2819. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2820. "Unable to upload pixel shader float parameters",
  2821. "D3D9RenderSystem::bindGpuProgramParameters");
  2822. }
  2823. }
  2824. }
  2825. }
  2826. // bind ints
  2827. {
  2828. OGRE_LOCK_MUTEX(intLogical->mutex)
  2829. for (GpuLogicalIndexUseMap::const_iterator i = intLogical->map.begin();
  2830. i != intLogical->map.end(); ++i)
  2831. {
  2832. if (i->second.variability & variability)
  2833. {
  2834. size_t logicalIndex = i->first;
  2835. const int* pInt = params->getIntPointer(i->second.physicalIndex);
  2836. size_t slotCount = i->second.currentSize / 4;
  2837. assert (i->second.currentSize % 4 == 0 && "Should not have any "
  2838. "elements less than 4 wide for D3D9");
  2839. if (FAILED(hr = getActiveD3D9Device()->SetPixelShaderConstantI(
  2840. static_cast<UINT>(logicalIndex), pInt, static_cast<UINT>(slotCount))))
  2841. {
  2842. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2843. "Unable to upload pixel shader int parameters",
  2844. "D3D9RenderSystem::bindGpuProgramParameters");
  2845. }
  2846. }
  2847. }
  2848. }
  2849. break;
  2850. };
  2851. }
  2852. //---------------------------------------------------------------------
  2853. void D3D9RenderSystem::bindGpuProgramPassIterationParameters(GpuProgramType gptype)
  2854. {
  2855. HRESULT hr;
  2856. size_t physicalIndex = 0;
  2857. size_t logicalIndex = 0;
  2858. const float* pFloat;
  2859. switch(gptype)
  2860. {
  2861. case GPT_VERTEX_PROGRAM:
  2862. if (mActiveVertexGpuProgramParameters->hasPassIterationNumber())
  2863. {
  2864. physicalIndex = mActiveVertexGpuProgramParameters->getPassIterationNumberIndex();
  2865. logicalIndex = mActiveVertexGpuProgramParameters->getFloatLogicalIndexForPhysicalIndex(physicalIndex);
  2866. pFloat = mActiveVertexGpuProgramParameters->getFloatPointer(physicalIndex);
  2867. if (FAILED(hr = getActiveD3D9Device()->SetVertexShaderConstantF(
  2868. static_cast<UINT>(logicalIndex), pFloat, 1)))
  2869. {
  2870. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2871. "Unable to upload vertex shader multi pass parameters",
  2872. "D3D9RenderSystem::bindGpuProgramMultiPassParameters");
  2873. }
  2874. }
  2875. break;
  2876. case GPT_FRAGMENT_PROGRAM:
  2877. if (mActiveFragmentGpuProgramParameters->hasPassIterationNumber())
  2878. {
  2879. physicalIndex = mActiveFragmentGpuProgramParameters->getPassIterationNumberIndex();
  2880. logicalIndex = mActiveFragmentGpuProgramParameters->getFloatLogicalIndexForPhysicalIndex(physicalIndex);
  2881. pFloat = mActiveFragmentGpuProgramParameters->getFloatPointer(physicalIndex);
  2882. if (FAILED(hr = getActiveD3D9Device()->SetPixelShaderConstantF(
  2883. static_cast<UINT>(logicalIndex), pFloat, 1)))
  2884. {
  2885. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  2886. "Unable to upload pixel shader multi pass parameters",
  2887. "D3D9RenderSystem::bindGpuProgramMultiPassParameters");
  2888. }
  2889. }
  2890. break;
  2891. }
  2892. }
  2893. //---------------------------------------------------------------------
  2894. void D3D9RenderSystem::setClipPlanesImpl(const PlaneList& clipPlanes)
  2895. {
  2896. size_t i;
  2897. size_t numClipPlanes;
  2898. D3DXPLANE dx9ClipPlane;
  2899. DWORD mask = 0;
  2900. HRESULT hr;
  2901. numClipPlanes = clipPlanes.size();
  2902. for (i = 0; i < numClipPlanes; ++i)
  2903. {
  2904. const Plane& plane = clipPlanes[i];
  2905. dx9ClipPlane.a = plane.normal.x;
  2906. dx9ClipPlane.b = plane.normal.y;
  2907. dx9ClipPlane.c = plane.normal.z;
  2908. dx9ClipPlane.d = plane.d;
  2909. if (mVertexProgramBound)
  2910. {
  2911. // programmable clips in clip space (ugh)
  2912. // must transform worldspace planes by view/proj
  2913. D3DXMATRIX xform;
  2914. D3DXMatrixMultiply(&xform, &mDxViewMat, &mDxProjMat);
  2915. D3DXMatrixInverse(&xform, NULL, &xform);
  2916. D3DXMatrixTranspose(&xform, &xform);
  2917. D3DXPlaneTransform(&dx9ClipPlane, &dx9ClipPlane, &xform);
  2918. }
  2919. hr = getActiveD3D9Device()->SetClipPlane(static_cast<DWORD>(i), dx9ClipPlane);
  2920. if (FAILED(hr))
  2921. {
  2922. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set clip plane",
  2923. "D3D9RenderSystem::setClipPlanes");
  2924. }
  2925. mask |= (1 << i);
  2926. }
  2927. hr = __SetRenderState(D3DRS_CLIPPLANEENABLE, mask);
  2928. if (FAILED(hr))
  2929. {
  2930. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set render state for clip planes",
  2931. "D3D9RenderSystem::setClipPlanes");
  2932. }
  2933. }
  2934. //---------------------------------------------------------------------
  2935. void D3D9RenderSystem::setScissorTest(bool enabled, size_t left, size_t top, size_t right,
  2936. size_t bottom)
  2937. {
  2938. HRESULT hr;
  2939. if (enabled)
  2940. {
  2941. if (FAILED(hr = __SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE)))
  2942. {
  2943. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to enable scissor rendering state; " + getErrorDescription(hr),
  2944. "D3D9RenderSystem::setScissorTest");
  2945. }
  2946. RECT rect;
  2947. rect.left = static_cast<LONG>(left);
  2948. rect.top = static_cast<LONG>(top);
  2949. rect.bottom = static_cast<LONG>(bottom);
  2950. rect.right = static_cast<LONG>(right);
  2951. if (FAILED(hr = getActiveD3D9Device()->SetScissorRect(&rect)))
  2952. {
  2953. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to set scissor rectangle; " + getErrorDescription(hr),
  2954. "D3D9RenderSystem::setScissorTest");
  2955. }
  2956. }
  2957. else
  2958. {
  2959. if (FAILED(hr = __SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE)))
  2960. {
  2961. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Unable to disable scissor rendering state; " + getErrorDescription(hr),
  2962. "D3D9RenderSystem::setScissorTest");
  2963. }
  2964. }
  2965. }
  2966. //---------------------------------------------------------------------
  2967. void D3D9RenderSystem::clearFrameBuffer(unsigned int buffers,
  2968. const ColourValue& colour, Real depth, unsigned short stencil)
  2969. {
  2970. DWORD flags = 0;
  2971. if (buffers & FBT_COLOUR)
  2972. {
  2973. flags |= D3DCLEAR_TARGET;
  2974. }
  2975. if (buffers & FBT_DEPTH)
  2976. {
  2977. flags |= D3DCLEAR_ZBUFFER;
  2978. }
  2979. // Only try to clear the stencil buffer if supported
  2980. if (buffers & FBT_STENCIL && mCurrentCapabilities->hasCapability(RSC_HWSTENCIL))
  2981. {
  2982. flags |= D3DCLEAR_STENCIL;
  2983. }
  2984. HRESULT hr;
  2985. if( FAILED( hr = getActiveD3D9Device()->Clear(
  2986. 0,
  2987. NULL,
  2988. flags,
  2989. colour.getAsARGB(),
  2990. depth,
  2991. stencil ) ) )
  2992. {
  2993. String msg = DXGetErrorDescription(hr);
  2994. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error clearing frame buffer : "
  2995. + msg, "D3D9RenderSystem::clearFrameBuffer" );
  2996. }
  2997. }
  2998. //---------------------------------------------------------------------
  2999. void D3D9RenderSystem::_makeProjectionMatrix(Real left, Real right,
  3000. Real bottom, Real top, Real nearPlane, Real farPlane, Matrix4& dest,
  3001. bool forGpuProgram)
  3002. {
  3003. // Correct position for off-axis projection matrix
  3004. if (!forGpuProgram)
  3005. {
  3006. Real offsetX = left + right;
  3007. Real offsetY = top + bottom;
  3008. left -= offsetX;
  3009. right -= offsetX;
  3010. top -= offsetY;
  3011. bottom -= offsetY;
  3012. }
  3013. Real width = right - left;
  3014. Real height = top - bottom;
  3015. Real q, qn;
  3016. if (farPlane == 0)
  3017. {
  3018. q = 1 - Frustum::INFINITE_FAR_PLANE_ADJUST;
  3019. qn = nearPlane * (Frustum::INFINITE_FAR_PLANE_ADJUST - 1);
  3020. }
  3021. else
  3022. {
  3023. q = farPlane / ( farPlane - nearPlane );
  3024. qn = -q * nearPlane;
  3025. }
  3026. dest = Matrix4::ZERO;
  3027. dest[0][0] = 2 * nearPlane / width;
  3028. dest[0][2] = (right+left) / width;
  3029. dest[1][1] = 2 * nearPlane / height;
  3030. dest[1][2] = (top+bottom) / height;
  3031. if (forGpuProgram)
  3032. {
  3033. dest[2][2] = -q;
  3034. dest[3][2] = -1.0f;
  3035. }
  3036. else
  3037. {
  3038. dest[2][2] = q;
  3039. dest[3][2] = 1.0f;
  3040. }
  3041. dest[2][3] = qn;
  3042. }
  3043. // ------------------------------------------------------------------
  3044. void D3D9RenderSystem::setClipPlane (ushort index, Real A, Real B, Real C, Real D)
  3045. {
  3046. float plane[4] = { A, B, C, D };
  3047. getActiveD3D9Device()->SetClipPlane (index, plane);
  3048. }
  3049. // ------------------------------------------------------------------
  3050. void D3D9RenderSystem::enableClipPlane (ushort index, bool enable)
  3051. {
  3052. DWORD prev;
  3053. getActiveD3D9Device()->GetRenderState(D3DRS_CLIPPLANEENABLE, &prev);
  3054. __SetRenderState(D3DRS_CLIPPLANEENABLE, enable?
  3055. (prev | (1 << index)) : (prev & ~(1 << index)));
  3056. }
  3057. //---------------------------------------------------------------------
  3058. HardwareOcclusionQuery* D3D9RenderSystem::createHardwareOcclusionQuery()
  3059. {
  3060. D3D9HardwareOcclusionQuery* ret = new D3D9HardwareOcclusionQuery();
  3061. mHwOcclusionQueries.push_back(ret);
  3062. return ret;
  3063. }
  3064. //---------------------------------------------------------------------
  3065. Real D3D9RenderSystem::getHorizontalTexelOffset()
  3066. {
  3067. // D3D considers the origin to be in the center of a pixel
  3068. return -0.5f;
  3069. }
  3070. //---------------------------------------------------------------------
  3071. Real D3D9RenderSystem::getVerticalTexelOffset()
  3072. {
  3073. // D3D considers the origin to be in the center of a pixel
  3074. return -0.5f;
  3075. }
  3076. //---------------------------------------------------------------------
  3077. void D3D9RenderSystem::_applyObliqueDepthProjection(Matrix4& matrix, const Plane& plane,
  3078. bool forGpuProgram)
  3079. {
  3080. // Thanks to Eric Lenyel for posting this calculation at www.terathon.com
  3081. // Calculate the clip-space corner point opposite the clipping plane
  3082. // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
  3083. // transform it into camera space by multiplying it
  3084. // by the inverse of the projection matrix
  3085. /* generalised version
  3086. Vector4 q = matrix.inverse() *
  3087. Vector4(Math::Sign(plane.normal.x), Math::Sign(plane.normal.y), 1.0f, 1.0f);
  3088. */
  3089. Vector4 q;
  3090. q.x = Math::Sign(plane.normal.x) / matrix[0][0];
  3091. q.y = Math::Sign(plane.normal.y) / matrix[1][1];
  3092. q.z = 1.0F;
  3093. // flip the next bit from Lengyel since we're right-handed
  3094. if (forGpuProgram)
  3095. {
  3096. q.w = (1.0F - matrix[2][2]) / matrix[2][3];
  3097. }
  3098. else
  3099. {
  3100. q.w = (1.0F + matrix[2][2]) / matrix[2][3];
  3101. }
  3102. // Calculate the scaled plane vector
  3103. Vector4 clipPlane4d(plane.normal.x, plane.normal.y, plane.normal.z, plane.d);
  3104. Vector4 c = clipPlane4d * (1.0F / (clipPlane4d.dotProduct(q)));
  3105. // Replace the third row of the projection matrix
  3106. matrix[2][0] = c.x;
  3107. matrix[2][1] = c.y;
  3108. // flip the next bit from Lengyel since we're right-handed
  3109. if (forGpuProgram)
  3110. {
  3111. matrix[2][2] = c.z;
  3112. }
  3113. else
  3114. {
  3115. matrix[2][2] = -c.z;
  3116. }
  3117. matrix[2][3] = c.w;
  3118. }
  3119. //---------------------------------------------------------------------
  3120. Real D3D9RenderSystem::getMinimumDepthInputValue()
  3121. {
  3122. // Range [0.0f, 1.0f]
  3123. return 0.0f;
  3124. }
  3125. //---------------------------------------------------------------------
  3126. Real D3D9RenderSystem::getMaximumDepthInputValue()
  3127. {
  3128. // Range [0.0f, 1.0f]
  3129. // D3D inverts even identity view matrices, so maximum INPUT is -1.0
  3130. return -1.0f;
  3131. }
  3132. //---------------------------------------------------------------------
  3133. IDirect3D9* D3D9RenderSystem::getDirect3D9()
  3134. {
  3135. IDirect3D9* pDirect3D9 = msD3D9RenderSystem->mpD3D;
  3136. if (pDirect3D9 == NULL)
  3137. {
  3138. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
  3139. "Direct3D9 interface is NULL !!!",
  3140. "D3D9RenderSystem::getDirect3D9" );
  3141. }
  3142. return pDirect3D9;
  3143. }
  3144. //---------------------------------------------------------------------
  3145. UINT D3D9RenderSystem::getResourceCreationDeviceCount()
  3146. {
  3147. D3D9ResourceCreationPolicy creationPolicy = msD3D9RenderSystem->mResourceManager->getCreationPolicy();
  3148. if (creationPolicy == RCP_CREATE_ON_ACTIVE_DEVICE)
  3149. {
  3150. return 1;
  3151. }
  3152. else if (creationPolicy == RCP_CREATE_ON_ALL_DEVICES)
  3153. {
  3154. return msD3D9RenderSystem->mDeviceManager->getDeviceCount();
  3155. }
  3156. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
  3157. "Invalid resource creation policy !!!",
  3158. "D3D9RenderSystem::getResourceCreationDeviceCount" );
  3159. return 0;
  3160. }
  3161. //---------------------------------------------------------------------
  3162. IDirect3DDevice9* D3D9RenderSystem::getResourceCreationDevice(UINT index)
  3163. {
  3164. D3D9ResourceCreationPolicy creationPolicy = msD3D9RenderSystem->mResourceManager->getCreationPolicy();
  3165. IDirect3DDevice9* d3d9Device = NULL;
  3166. if (creationPolicy == RCP_CREATE_ON_ACTIVE_DEVICE)
  3167. {
  3168. d3d9Device = msD3D9RenderSystem->getActiveD3D9Device();
  3169. }
  3170. else if (creationPolicy == RCP_CREATE_ON_ALL_DEVICES)
  3171. {
  3172. d3d9Device = msD3D9RenderSystem->mDeviceManager->getDevice(index)->getD3D9Device();
  3173. }
  3174. else
  3175. {
  3176. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
  3177. "Invalid resource creation policy !!!",
  3178. "D3D9RenderSystem::getResourceCreationDevice" );
  3179. }
  3180. return d3d9Device;
  3181. }
  3182. //---------------------------------------------------------------------
  3183. IDirect3DDevice9* D3D9RenderSystem::getActiveD3D9Device()
  3184. {
  3185. D3D9Device* activeDevice = msD3D9RenderSystem->mDeviceManager->getActiveDevice();
  3186. IDirect3DDevice9* d3d9Device;
  3187. d3d9Device = activeDevice->getD3D9Device();
  3188. if (d3d9Device == NULL)
  3189. {
  3190. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
  3191. "Current d3d9 device is NULL !!!",
  3192. "D3D9RenderSystem::getActiveD3D9Device" );
  3193. }
  3194. return d3d9Device;
  3195. }
  3196. //---------------------------------------------------------------------
  3197. // Formats to try, in decreasing order of preference
  3198. D3DFORMAT ddDepthStencilFormats[]={
  3199. D3DFMT_D24FS8,
  3200. D3DFMT_D24S8,
  3201. D3DFMT_D24X4S4,
  3202. D3DFMT_D24X8,
  3203. D3DFMT_D15S1,
  3204. D3DFMT_D16,
  3205. D3DFMT_D32
  3206. };
  3207. #define NDSFORMATS (sizeof(ddDepthStencilFormats)/sizeof(D3DFORMAT))
  3208. D3DFORMAT D3D9RenderSystem::_getDepthStencilFormatFor(D3DFORMAT fmt)
  3209. {
  3210. /// Check if result is cached
  3211. DepthStencilHash::iterator i = mDepthStencilHash.find((unsigned int)fmt);
  3212. if(i != mDepthStencilHash.end())
  3213. return i->second;
  3214. /// If not, probe with CheckDepthStencilMatch
  3215. D3DFORMAT dsfmt = D3DFMT_UNKNOWN;
  3216. /// Get description of primary render target
  3217. D3D9Device* activeDevice = mDeviceManager->getActiveDevice();
  3218. IDirect3DSurface9* mSurface = activeDevice->getPrimaryWindow()->getRenderSurface();
  3219. D3DSURFACE_DESC srfDesc;
  3220. if(!FAILED(mSurface->GetDesc(&srfDesc)))
  3221. {
  3222. /// Probe all depth stencil formats
  3223. /// Break on first one that matches
  3224. for(size_t x=0; x<NDSFORMATS; ++x)
  3225. {
  3226. // Verify that the depth format exists
  3227. if (mpD3D->CheckDeviceFormat(
  3228. activeDevice->getAdapterNumber(),
  3229. activeDevice->getDeviceType(),
  3230. srfDesc.Format,
  3231. D3DUSAGE_DEPTHSTENCIL,
  3232. D3DRTYPE_SURFACE,
  3233. ddDepthStencilFormats[x]) != D3D_OK)
  3234. {
  3235. continue;
  3236. }
  3237. // Verify that the depth format is compatible
  3238. if(mpD3D->CheckDepthStencilMatch(
  3239. activeDevice->getAdapterNumber(),
  3240. activeDevice->getDeviceType(),
  3241. srfDesc.Format,
  3242. fmt, ddDepthStencilFormats[x]) == D3D_OK)
  3243. {
  3244. dsfmt = ddDepthStencilFormats[x];
  3245. break;
  3246. }
  3247. }
  3248. }
  3249. /// Cache result
  3250. mDepthStencilHash[(unsigned int)fmt] = dsfmt;
  3251. return dsfmt;
  3252. }
  3253. IDirect3DSurface9* D3D9RenderSystem::_getDepthStencilFor(D3DFORMAT fmt,
  3254. D3DMULTISAMPLE_TYPE multisample, DWORD multisample_quality, size_t width, size_t height)
  3255. {
  3256. D3DFORMAT dsfmt = _getDepthStencilFormatFor(fmt);
  3257. if(dsfmt == D3DFMT_UNKNOWN)
  3258. return 0;
  3259. IDirect3DSurface9 *surface = 0;
  3260. /// Check if result is cached
  3261. ZBufferIdentifier zBufferIdentifer;
  3262. zBufferIdentifer.format = dsfmt;
  3263. zBufferIdentifer.multisampleType = multisample;
  3264. zBufferIdentifer.device = getActiveD3D9Device();
  3265. ZBufferRefQueue& zBuffers = mZBufferHash[zBufferIdentifer];
  3266. if(!zBuffers.empty())
  3267. {
  3268. const ZBufferRef& zBuffer = zBuffers.front();
  3269. /// Check if size is larger or equal
  3270. if(zBuffer.width >= width && zBuffer.height >= height)
  3271. {
  3272. surface = zBuffer.surface;
  3273. }
  3274. else
  3275. {
  3276. /// If not, destroy current buffer
  3277. zBuffer.surface->Release();
  3278. zBuffers.pop_front();
  3279. }
  3280. }
  3281. if(!surface)
  3282. {
  3283. /// If not, create the depthstencil surface
  3284. HRESULT hr = getActiveD3D9Device()->CreateDepthStencilSurface(
  3285. static_cast<UINT>(width),
  3286. static_cast<UINT>(height),
  3287. dsfmt,
  3288. multisample,
  3289. multisample_quality,
  3290. TRUE, // discard true or false?
  3291. &surface,
  3292. NULL);
  3293. if(FAILED(hr))
  3294. {
  3295. String msg = DXGetErrorDescription(hr);
  3296. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error CreateDepthStencilSurface : " + msg, "D3D9RenderSystem::_getDepthStencilFor" );
  3297. }
  3298. /// And cache it
  3299. ZBufferRef zb;
  3300. zb.surface = surface;
  3301. zb.width = width;
  3302. zb.height = height;
  3303. zBuffers.push_front(zb);
  3304. }
  3305. return surface;
  3306. }
  3307. //---------------------------------------------------------------------
  3308. void D3D9RenderSystem::_cleanupDepthStencils(IDirect3DDevice9* d3d9Device)
  3309. {
  3310. for(ZBufferHash::iterator i = mZBufferHash.begin(); i != mZBufferHash.end();)
  3311. {
  3312. /// Release buffer
  3313. if (i->first.device == d3d9Device)
  3314. {
  3315. while (!i->second.empty())
  3316. {
  3317. IDirect3DSurface9* surface = i->second.front().surface;
  3318. surface->Release();
  3319. i->second.pop_front();
  3320. }
  3321. ZBufferHash::iterator deadi = i++;
  3322. mZBufferHash.erase(deadi);
  3323. }
  3324. else
  3325. {
  3326. ++i;
  3327. }
  3328. }
  3329. }
  3330. //---------------------------------------------------------------------
  3331. void D3D9RenderSystem::registerThread()
  3332. {
  3333. // nothing to do - D3D9 shares rendering context already
  3334. }
  3335. //---------------------------------------------------------------------
  3336. void D3D9RenderSystem::unregisterThread()
  3337. {
  3338. // nothing to do - D3D9 shares rendering context already
  3339. }
  3340. //---------------------------------------------------------------------
  3341. void D3D9RenderSystem::preExtraThreadsStarted()
  3342. {
  3343. // nothing to do - D3D9 shares rendering context already
  3344. }
  3345. //---------------------------------------------------------------------
  3346. void D3D9RenderSystem::postExtraThreadsStarted()
  3347. {
  3348. // nothing to do - D3D9 shares rendering context already
  3349. }
  3350. //---------------------------------------------------------------------
  3351. D3D9ResourceManager* D3D9RenderSystem::getResourceManager()
  3352. {
  3353. return msD3D9RenderSystem->mResourceManager;
  3354. }
  3355. //---------------------------------------------------------------------
  3356. D3D9DeviceManager* D3D9RenderSystem::getDeviceManager()
  3357. {
  3358. return msD3D9RenderSystem->mDeviceManager;
  3359. }
  3360. //---------------------------------------------------------------------
  3361. RenderSystemCapabilities* D3D9RenderSystem::createRenderSystemCapabilities() const
  3362. {
  3363. return mRealCapabilities;
  3364. }
  3365. //---------------------------------------------------------------------
  3366. unsigned int D3D9RenderSystem::getDisplayMonitorCount() const
  3367. {
  3368. return mpD3D->GetAdapterCount();
  3369. }
  3370. //---------------------------------------------------------------------
  3371. void D3D9RenderSystem::notifyOnDeviceLost(D3D9Device* device)
  3372. {
  3373. // you need to stop the physics or game engines after this event
  3374. fireEvent("DeviceLost");
  3375. }
  3376. //---------------------------------------------------------------------
  3377. void D3D9RenderSystem::notifyOnDeviceReset(D3D9Device* device)
  3378. {
  3379. // Reset state attributes.
  3380. mVertexProgramBound = false;
  3381. mFragmentProgramBound = false;
  3382. mLastVertexSourceCount = 0;
  3383. // Restore previous active device.
  3384. // Invalidate active view port.
  3385. mActiveViewport = NULL;
  3386. // Reset the texture stages, they will need to be rebound
  3387. for (size_t i = 0; i < OGRE_MAX_TEXTURE_LAYERS; ++i)
  3388. _setTexture(i, false, TexturePtr());
  3389. fireEvent("DeviceRestored");
  3390. }
  3391. //---------------------------------------------------------------------
  3392. bool D3D9RenderSystem::ZBufferIdentifierComparator::operator()( const ZBufferIdentifier& z0, const ZBufferIdentifier& z1 ) const
  3393. {
  3394. if (z0.device < z1.device)
  3395. return true;
  3396. if (z0.device == z1.device)
  3397. {
  3398. if (z0.format < z1.format)
  3399. return true;
  3400. if (z0.format == z1.format)
  3401. {
  3402. if (z0.multisampleType < z1.multisampleType)
  3403. return true;
  3404. }
  3405. }
  3406. return false;
  3407. }
  3408. //---------------------------------------------------------------------
  3409. void D3D9RenderSystem::determineFSAASettings(IDirect3DDevice9* d3d9Device,
  3410. size_t fsaa, const String& fsaaHint, D3DFORMAT d3dPixelFormat,
  3411. bool fullScreen, D3DMULTISAMPLE_TYPE *outMultisampleType, DWORD *outMultisampleQuality)
  3412. {
  3413. bool ok = false;
  3414. bool qualityHint = fsaaHint.find("Quality") != String::npos;
  3415. size_t origFSAA = fsaa;
  3416. D3D9DriverList* driverList = getDirect3DDrivers();
  3417. D3D9Driver* deviceDriver = mActiveD3DDriver;
  3418. D3D9Device* device = mDeviceManager->getDeviceFromD3D9Device(d3d9Device);
  3419. for (uint i = 0; i < driverList->count(); ++i)
  3420. {
  3421. D3D9Driver* currDriver = driverList->item(i);
  3422. if (currDriver->getAdapterNumber() == device->getAdapterNumber())
  3423. {
  3424. deviceDriver = currDriver;
  3425. break;
  3426. }
  3427. }
  3428. bool tryCSAA = false;
  3429. // NVIDIA, prefer CSAA if available for 8+
  3430. // it would be tempting to use getCapabilities()->getVendor() == GPU_NVIDIA but
  3431. // if this is the first window, caps will not be initialised yet
  3432. if (deviceDriver->getAdapterIdentifier().VendorId == 0x10DE &&
  3433. fsaa >= 8)
  3434. {
  3435. tryCSAA = true;
  3436. }
  3437. while (!ok)
  3438. {
  3439. // Deal with special cases
  3440. if (tryCSAA)
  3441. {
  3442. // see http://developer.nvidia.com/object/coverage-sampled-aa.html
  3443. switch(fsaa)
  3444. {
  3445. case 8:
  3446. if (qualityHint)
  3447. {
  3448. *outMultisampleType = D3DMULTISAMPLE_8_SAMPLES;
  3449. *outMultisampleQuality = 0;
  3450. }
  3451. else
  3452. {
  3453. *outMultisampleType = D3DMULTISAMPLE_4_SAMPLES;
  3454. *outMultisampleQuality = 2;
  3455. }
  3456. break;
  3457. case 16:
  3458. if (qualityHint)
  3459. {
  3460. *outMultisampleType = D3DMULTISAMPLE_8_SAMPLES;
  3461. *outMultisampleQuality = 2;
  3462. }
  3463. else
  3464. {
  3465. *outMultisampleType = D3DMULTISAMPLE_4_SAMPLES;
  3466. *outMultisampleQuality = 4;
  3467. }
  3468. break;
  3469. }
  3470. }
  3471. else // !CSAA
  3472. {
  3473. *outMultisampleType = (D3DMULTISAMPLE_TYPE)fsaa;
  3474. *outMultisampleQuality = 0;
  3475. }
  3476. HRESULT hr;
  3477. DWORD outQuality;
  3478. hr = mpD3D->CheckDeviceMultiSampleType(
  3479. deviceDriver->getAdapterNumber(),
  3480. D3DDEVTYPE_HAL,
  3481. d3dPixelFormat,
  3482. fullScreen,
  3483. *outMultisampleType,
  3484. &outQuality);
  3485. if (SUCCEEDED(hr) &&
  3486. (!tryCSAA || outQuality > *outMultisampleQuality))
  3487. {
  3488. ok = true;
  3489. }
  3490. else
  3491. {
  3492. // downgrade
  3493. if (tryCSAA && fsaa == 8)
  3494. {
  3495. // for CSAA, we'll try downgrading with quality mode at all samples.
  3496. // then try without quality, then drop CSAA
  3497. if (qualityHint)
  3498. {
  3499. // drop quality first
  3500. qualityHint = false;
  3501. }
  3502. else
  3503. {
  3504. // drop CSAA entirely
  3505. tryCSAA = false;
  3506. }
  3507. // return to original requested samples
  3508. fsaa = origFSAA;
  3509. }
  3510. else
  3511. {
  3512. // drop samples
  3513. --fsaa;
  3514. if (fsaa == 1)
  3515. {
  3516. // ran out of options, no FSAA
  3517. fsaa = 0;
  3518. ok = true;
  3519. }
  3520. }
  3521. }
  3522. } // while !ok
  3523. }
  3524. }