renderer_d3d9.cpp 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BGFX_CONFIG_RENDERER_DIRECT3D9
  7. # include "renderer_d3d9.h"
  8. namespace bgfx
  9. {
  10. static wchar_t s_viewNameW[BGFX_CONFIG_MAX_VIEWS][256];
  11. static const D3DPRIMITIVETYPE s_primType[] =
  12. {
  13. D3DPT_TRIANGLELIST,
  14. D3DPT_LINELIST,
  15. D3DPT_POINTLIST,
  16. };
  17. static const D3DMULTISAMPLE_TYPE s_checkMsaa[] =
  18. {
  19. D3DMULTISAMPLE_NONE,
  20. D3DMULTISAMPLE_2_SAMPLES,
  21. D3DMULTISAMPLE_4_SAMPLES,
  22. D3DMULTISAMPLE_8_SAMPLES,
  23. D3DMULTISAMPLE_16_SAMPLES,
  24. };
  25. static Msaa s_msaa[] =
  26. {
  27. { D3DMULTISAMPLE_NONE, 0 },
  28. { D3DMULTISAMPLE_2_SAMPLES, 0 },
  29. { D3DMULTISAMPLE_4_SAMPLES, 0 },
  30. { D3DMULTISAMPLE_8_SAMPLES, 0 },
  31. { D3DMULTISAMPLE_16_SAMPLES, 0 },
  32. };
  33. struct Blend
  34. {
  35. D3DBLEND m_src;
  36. D3DBLEND m_dst;
  37. bool m_factor;
  38. };
  39. static const Blend s_blendFactor[] =
  40. {
  41. { (D3DBLEND)0, (D3DBLEND)0, false }, // ignored
  42. { D3DBLEND_ZERO, D3DBLEND_ZERO, false },
  43. { D3DBLEND_ONE, D3DBLEND_ONE, false },
  44. { D3DBLEND_SRCCOLOR, D3DBLEND_SRCCOLOR, false },
  45. { D3DBLEND_INVSRCCOLOR, D3DBLEND_INVSRCCOLOR, false },
  46. { D3DBLEND_SRCALPHA, D3DBLEND_SRCALPHA, false },
  47. { D3DBLEND_INVSRCALPHA, D3DBLEND_INVSRCALPHA, false },
  48. { D3DBLEND_DESTALPHA, D3DBLEND_DESTALPHA, false },
  49. { D3DBLEND_INVDESTALPHA, D3DBLEND_INVDESTALPHA, false },
  50. { D3DBLEND_DESTCOLOR, D3DBLEND_DESTCOLOR, false },
  51. { D3DBLEND_INVDESTCOLOR, D3DBLEND_INVDESTCOLOR, false },
  52. { D3DBLEND_SRCALPHASAT, D3DBLEND_ONE, false },
  53. { D3DBLEND_BLENDFACTOR, D3DBLEND_BLENDFACTOR, true },
  54. { D3DBLEND_INVBLENDFACTOR, D3DBLEND_INVBLENDFACTOR, true },
  55. };
  56. static const D3DBLENDOP s_blendEquation[] =
  57. {
  58. D3DBLENDOP_ADD,
  59. D3DBLENDOP_SUBTRACT,
  60. D3DBLENDOP_REVSUBTRACT,
  61. D3DBLENDOP_MIN,
  62. D3DBLENDOP_MAX,
  63. };
  64. static const D3DCMPFUNC s_depthFunc[] =
  65. {
  66. (D3DCMPFUNC)0, // ignored
  67. D3DCMP_LESS,
  68. D3DCMP_LESSEQUAL,
  69. D3DCMP_EQUAL,
  70. D3DCMP_GREATEREQUAL,
  71. D3DCMP_GREATER,
  72. D3DCMP_NOTEQUAL,
  73. D3DCMP_NEVER,
  74. D3DCMP_ALWAYS,
  75. };
  76. static const D3DCMPFUNC s_stencilFunc[] =
  77. {
  78. (D3DCMPFUNC)0, // ignored
  79. D3DCMP_LESS,
  80. D3DCMP_LESSEQUAL,
  81. D3DCMP_EQUAL,
  82. D3DCMP_GREATEREQUAL,
  83. D3DCMP_GREATER,
  84. D3DCMP_NOTEQUAL,
  85. D3DCMP_NEVER,
  86. D3DCMP_ALWAYS,
  87. };
  88. static const D3DSTENCILOP s_stencilOp[] =
  89. {
  90. D3DSTENCILOP_ZERO,
  91. D3DSTENCILOP_KEEP,
  92. D3DSTENCILOP_REPLACE,
  93. D3DSTENCILOP_INCR,
  94. D3DSTENCILOP_INCRSAT,
  95. D3DSTENCILOP_DECR,
  96. D3DSTENCILOP_DECRSAT,
  97. D3DSTENCILOP_INVERT,
  98. };
  99. static const D3DRENDERSTATETYPE s_stencilFuncRs[] =
  100. {
  101. D3DRS_STENCILFUNC,
  102. D3DRS_CCW_STENCILFUNC,
  103. };
  104. static const D3DRENDERSTATETYPE s_stencilFailRs[] =
  105. {
  106. D3DRS_STENCILFAIL,
  107. D3DRS_CCW_STENCILFAIL,
  108. };
  109. static const D3DRENDERSTATETYPE s_stencilZFailRs[] =
  110. {
  111. D3DRS_STENCILZFAIL,
  112. D3DRS_CCW_STENCILZFAIL,
  113. };
  114. static const D3DRENDERSTATETYPE s_stencilZPassRs[] =
  115. {
  116. D3DRS_STENCILPASS,
  117. D3DRS_CCW_STENCILPASS,
  118. };
  119. static const D3DCULL s_cullMode[] =
  120. {
  121. D3DCULL_NONE,
  122. D3DCULL_CW,
  123. D3DCULL_CCW,
  124. };
  125. static const D3DFORMAT s_checkColorFormats[] =
  126. {
  127. D3DFMT_UNKNOWN,
  128. D3DFMT_A8R8G8B8, D3DFMT_UNKNOWN,
  129. D3DFMT_R32F, D3DFMT_R16F, D3DFMT_G16R16, D3DFMT_A8R8G8B8, D3DFMT_UNKNOWN,
  130. D3DFMT_UNKNOWN, // terminator
  131. };
  132. static D3DFORMAT s_colorFormat[] =
  133. {
  134. D3DFMT_UNKNOWN, // ignored
  135. D3DFMT_A8R8G8B8,
  136. D3DFMT_A2B10G10R10,
  137. D3DFMT_A16B16G16R16,
  138. D3DFMT_A16B16G16R16F,
  139. D3DFMT_R16F,
  140. D3DFMT_R32F,
  141. };
  142. static const D3DFORMAT s_depthFormat[] =
  143. {
  144. D3DFMT_UNKNOWN, // ignored
  145. D3DFMT_D24S8,
  146. };
  147. static const D3DTEXTUREADDRESS s_textureAddress[] =
  148. {
  149. D3DTADDRESS_WRAP,
  150. D3DTADDRESS_MIRROR,
  151. D3DTADDRESS_CLAMP,
  152. };
  153. static const D3DTEXTUREFILTERTYPE s_textureFilter[] =
  154. {
  155. D3DTEXF_LINEAR,
  156. D3DTEXF_POINT,
  157. D3DTEXF_ANISOTROPIC,
  158. };
  159. struct TextureFormatInfo
  160. {
  161. D3DFORMAT m_fmt;
  162. uint8_t m_bpp;
  163. };
  164. static const TextureFormatInfo s_textureFormat[TextureFormat::Count] =
  165. {
  166. { D3DFMT_DXT1, 4 },
  167. { D3DFMT_DXT3, 8 },
  168. { D3DFMT_DXT5, 8 },
  169. { D3DFMT_ATI1, 4 },
  170. { D3DFMT_ATI2, 8 },
  171. { D3DFMT_UNKNOWN, 0 },
  172. { D3DFMT_L8, 8 },
  173. { D3DFMT_X8R8G8B8, 32 },
  174. { D3DFMT_A8R8G8B8, 32 },
  175. { D3DFMT_A16B16G16R16, 64 },
  176. { D3DFMT_A16B16G16R16F, 64 },
  177. { D3DFMT_R5G6B5, 16 },
  178. { D3DFMT_A4R4G4B4, 16 },
  179. { D3DFMT_A1R5G5B5, 16 },
  180. { D3DFMT_A2B10G10R10, 32 },
  181. };
  182. static ExtendedFormat s_extendedFormats[ExtendedFormat::Count] =
  183. {
  184. { D3DFMT_ATI1, 0, D3DRTYPE_TEXTURE, false },
  185. { D3DFMT_ATI2, 0, D3DRTYPE_TEXTURE, false },
  186. { D3DFMT_DF16, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, false },
  187. { D3DFMT_DF24, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, false },
  188. { D3DFMT_INST, 0, D3DRTYPE_SURFACE, false },
  189. { D3DFMT_INTZ, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, false },
  190. { D3DFMT_NULL, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, false },
  191. { D3DFMT_RESZ, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, false },
  192. { D3DFMT_RAWZ, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, false },
  193. };
  194. static const GUID IID_IDirect3D9 = { 0x81bdcbca, 0x64d4, 0x426d, { 0xae, 0x8d, 0xad, 0x1, 0x47, 0xf4, 0x27, 0x5c } };
  195. static const GUID IID_IDirect3DDevice9Ex = { 0xb18b10ce, 0x2649, 0x405a, { 0x87, 0xf, 0x95, 0xf7, 0x77, 0xd4, 0x31, 0x3a } };
  196. struct RendererContext
  197. {
  198. RendererContext()
  199. : m_flags(BGFX_RESET_NONE)
  200. , m_initialized(false)
  201. , m_amd(false)
  202. , m_nvidia(false)
  203. , m_instancing(false)
  204. , m_rtMsaa(false)
  205. {
  206. m_rt.idx = invalidHandle;
  207. }
  208. void init()
  209. {
  210. D3DFORMAT adapterFormat = D3DFMT_X8R8G8B8;
  211. // http://msdn.microsoft.com/en-us/library/windows/desktop/bb172588%28v=vs.85%29.aspx
  212. memset(&m_params, 0, sizeof(m_params) );
  213. m_params.BackBufferWidth = BGFX_DEFAULT_WIDTH;
  214. m_params.BackBufferHeight = BGFX_DEFAULT_HEIGHT;
  215. m_params.BackBufferFormat = adapterFormat;
  216. m_params.BackBufferCount = 1;
  217. m_params.MultiSampleType = D3DMULTISAMPLE_NONE;
  218. m_params.MultiSampleQuality = 0;
  219. m_params.EnableAutoDepthStencil = TRUE;
  220. m_params.AutoDepthStencilFormat = D3DFMT_D24S8;
  221. m_params.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
  222. #if BX_PLATFORM_WINDOWS
  223. m_params.FullScreen_RefreshRateInHz = 0;
  224. m_params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  225. m_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
  226. m_params.hDeviceWindow = g_bgfxHwnd;
  227. m_params.Windowed = true;
  228. RECT rect;
  229. GetWindowRect(g_bgfxHwnd, &rect);
  230. m_params.BackBufferWidth = rect.right-rect.left;
  231. m_params.BackBufferHeight = rect.bottom-rect.top;
  232. m_d3d9dll = bx::dlopen("d3d9.dll");
  233. BGFX_FATAL(NULL != m_d3d9dll, Fatal::UnableToInitialize, "Failed to load d3d9.dll.");
  234. #if BGFX_CONFIG_DEBUG_PIX
  235. m_D3DPERF_SetMarker = (D3DPERF_SetMarkerFunc)bx::dlsym(m_d3d9dll, "D3DPERF_SetMarker");
  236. m_D3DPERF_BeginEvent = (D3DPERF_BeginEventFunc)bx::dlsym(m_d3d9dll, "D3DPERF_BeginEvent");
  237. m_D3DPERF_EndEvent = (D3DPERF_EndEventFunc)bx::dlsym(m_d3d9dll, "D3DPERF_EndEvent");
  238. BX_CHECK(NULL != m_D3DPERF_SetMarker
  239. && NULL != m_D3DPERF_BeginEvent
  240. && NULL != m_D3DPERF_EndEvent
  241. , "Failed to initialize PIX events."
  242. );
  243. #endif // BGFX_CONFIG_DEBUG_PIX
  244. #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
  245. m_d3d9ex = NULL;
  246. Direct3DCreate9ExFn direct3DCreate9Ex = (Direct3DCreate9ExFn)bx::dlsym(m_d3d9dll, "Direct3DCreate9Ex");
  247. if (NULL != direct3DCreate9Ex)
  248. {
  249. direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d9ex);
  250. DX_CHECK(m_d3d9ex->QueryInterface(IID_IDirect3D9, (void**)&m_d3d9) );
  251. m_pool = D3DPOOL_DEFAULT;
  252. }
  253. else
  254. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  255. {
  256. Direct3DCreate9Fn direct3DCreate9 = (Direct3DCreate9Fn)bx::dlsym(m_d3d9dll, "Direct3DCreate9");
  257. BGFX_FATAL(NULL != direct3DCreate9, Fatal::UnableToInitialize, "Function Direct3DCreate9 not found.");
  258. m_d3d9 = direct3DCreate9(D3D_SDK_VERSION);
  259. m_pool = D3DPOOL_MANAGED;
  260. }
  261. BGFX_FATAL(m_d3d9, Fatal::UnableToInitialize, "Unable to create Direct3D.");
  262. m_adapter = D3DADAPTER_DEFAULT;
  263. m_deviceType = D3DDEVTYPE_HAL;
  264. uint32_t adapterCount = m_d3d9->GetAdapterCount();
  265. for (uint32_t ii = 0; ii < adapterCount; ++ii)
  266. {
  267. D3DADAPTER_IDENTIFIER9 identifier;
  268. HRESULT hr = m_d3d9->GetAdapterIdentifier(ii, 0, &identifier);
  269. if (SUCCEEDED(hr) )
  270. {
  271. BX_TRACE("Adapter #%d", ii);
  272. BX_TRACE("\tDriver: %s", identifier.Driver);
  273. BX_TRACE("\tDescription: %s", identifier.Description);
  274. BX_TRACE("\tDeviceName: %s", identifier.DeviceName);
  275. BX_TRACE("\tVendorId: 0x%08x, DeviceId: 0x%08x, SubSysId: 0x%08x, Revision: 0x%08x"
  276. , identifier.VendorId
  277. , identifier.DeviceId
  278. , identifier.SubSysId
  279. , identifier.Revision
  280. );
  281. #if BGFX_CONFIG_DEBUG_PERFHUD
  282. if (0 != strstr(identifier.Description, "PerfHUD") )
  283. {
  284. m_adapter = ii;
  285. m_deviceType = D3DDEVTYPE_REF;
  286. }
  287. #endif // BGFX_CONFIG_DEBUG_PERFHUD
  288. }
  289. }
  290. DX_CHECK(m_d3d9->GetAdapterIdentifier(m_adapter, 0, &m_identifier) );
  291. m_amd = m_identifier.VendorId == 0x1002;
  292. m_nvidia = m_identifier.VendorId == 0x10de;
  293. uint32_t behaviorFlags[] =
  294. {
  295. D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_PUREDEVICE,
  296. D3DCREATE_MIXED_VERTEXPROCESSING,
  297. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  298. };
  299. for (uint32_t ii = 0; ii < countof(behaviorFlags) && NULL == m_device; ++ii)
  300. {
  301. #if 0 // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  302. DX_CHECK(m_d3d9->CreateDeviceEx(m_adapter
  303. , m_deviceType
  304. , g_bgfxHwnd
  305. , behaviorFlags[ii]
  306. , &m_params
  307. , NULL
  308. , &m_device
  309. ) );
  310. #else
  311. DX_CHECK(m_d3d9->CreateDevice(m_adapter
  312. , m_deviceType
  313. , g_bgfxHwnd
  314. , behaviorFlags[ii]
  315. , &m_params
  316. , &m_device
  317. ) );
  318. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  319. }
  320. BGFX_FATAL(m_device, Fatal::UnableToInitialize, "Unable to create Direct3D9 device.");
  321. #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
  322. if (NULL != m_d3d9ex)
  323. {
  324. DX_CHECK(m_device->QueryInterface(IID_IDirect3DDevice9Ex, (void**)&m_deviceEx) );
  325. }
  326. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  327. DX_CHECK(m_device->GetDeviceCaps(&m_caps) );
  328. // For shit GPUs that can create DX9 device but can't do simple stuff. GTFO!
  329. BGFX_FATAL( (D3DPTEXTURECAPS_SQUAREONLY & m_caps.TextureCaps) == 0, Fatal::MinimumRequiredSpecs, "D3DPTEXTURECAPS_SQUAREONLY");
  330. BGFX_FATAL( (D3DPTEXTURECAPS_MIPMAP & m_caps.TextureCaps) == D3DPTEXTURECAPS_MIPMAP, Fatal::MinimumRequiredSpecs, "D3DPTEXTURECAPS_MIPMAP");
  331. BGFX_FATAL( (D3DPTEXTURECAPS_ALPHA & m_caps.TextureCaps) == D3DPTEXTURECAPS_ALPHA, Fatal::MinimumRequiredSpecs, "D3DPTEXTURECAPS_ALPHA");
  332. BGFX_FATAL(m_caps.VertexShaderVersion >= D3DVS_VERSION(2, 0) && m_caps.PixelShaderVersion >= D3DPS_VERSION(2, 1)
  333. , Fatal::MinimumRequiredSpecs
  334. , "Shader Model Version (vs: %x, ps: %x)."
  335. , m_caps.VertexShaderVersion
  336. , m_caps.PixelShaderVersion
  337. );
  338. BGFX_FATAL(m_caps.MaxTextureWidth >= 2048 && m_caps.MaxTextureHeight >= 2048
  339. , Fatal::MinimumRequiredSpecs
  340. , "Maximum texture size is below 2048 (w: %d, h: %d)."
  341. , m_caps.MaxTextureWidth
  342. , m_caps.MaxTextureHeight
  343. );
  344. BX_TRACE("Max vertex shader 3.0 instr. slots: %d", m_caps.MaxVertexShader30InstructionSlots);
  345. BX_TRACE("Max vertex shader constants: %d", m_caps.MaxVertexShaderConst);
  346. BX_TRACE("Max fragment shader 2.0 instr. slots: %d", m_caps.PS20Caps.NumInstructionSlots);
  347. BX_TRACE("Max fragment shader 3.0 instr. slots: %d", m_caps.MaxPixelShader30InstructionSlots);
  348. BX_TRACE("Extended formats:");
  349. for (uint32_t ii = 0; ii < ExtendedFormat::Count; ++ii)
  350. {
  351. ExtendedFormat& fmt = s_extendedFormats[ii];
  352. fmt.m_supported = SUCCEEDED(m_d3d9->CheckDeviceFormat(m_adapter, m_deviceType, adapterFormat, fmt.m_usage, fmt.m_type, fmt.m_fmt) );
  353. const char* fourcc = (const char*)&fmt.m_fmt;
  354. BX_TRACE("\t%2d: %c%c%c%c %s", ii, fourcc[0], fourcc[1], fourcc[2], fourcc[3], fmt.m_supported ? "supported" : "");
  355. BX_UNUSED(fourcc);
  356. }
  357. m_instancing = false
  358. || s_extendedFormats[ExtendedFormat::Inst].m_supported
  359. || (m_caps.VertexShaderVersion >= D3DVS_VERSION(3, 0) )
  360. ;
  361. if (m_amd
  362. && s_extendedFormats[ExtendedFormat::Inst].m_supported)
  363. {
  364. // AMD only
  365. m_device->SetRenderState(D3DRS_POINTSIZE, D3DFMT_INST);
  366. }
  367. uint32_t index = 1;
  368. for (const D3DFORMAT* fmt = &s_checkColorFormats[index]; *fmt != D3DFMT_UNKNOWN; ++fmt, ++index)
  369. {
  370. for (; *fmt != D3DFMT_UNKNOWN; ++fmt)
  371. {
  372. if (SUCCEEDED(m_d3d9->CheckDeviceFormat(m_adapter, m_deviceType, adapterFormat, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, *fmt) ) )
  373. {
  374. s_colorFormat[index] = *fmt;
  375. break;
  376. }
  377. }
  378. for (; *fmt != D3DFMT_UNKNOWN; ++fmt);
  379. }
  380. m_fmtDepth = D3DFMT_D24S8;
  381. #elif BX_PLATFORM_XBOX360
  382. m_params.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  383. m_params.DisableAutoBackBuffer = FALSE;
  384. m_params.DisableAutoFrontBuffer = FALSE;
  385. m_params.FrontBufferFormat = D3DFMT_X8R8G8B8;
  386. m_params.FrontBufferColorSpace = D3DCOLORSPACE_RGB;
  387. m_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  388. BX_TRACE("Creating D3D9 %p", m_d3d9);
  389. XVIDEO_MODE videoMode;
  390. XGetVideoMode(&videoMode);
  391. if (!videoMode.fIsWideScreen)
  392. {
  393. m_params.Flags |= D3DPRESENTFLAG_NO_LETTERBOX;
  394. }
  395. BX_TRACE("Creating device");
  396. DX_CHECK(m_d3d9->CreateDevice(m_adapter
  397. , m_deviceType
  398. , NULL
  399. , D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_BUFFER_2_FRAMES
  400. , &m_params
  401. , &m_device
  402. ) );
  403. BX_TRACE("Device %p", m_device);
  404. m_fmtDepth = D3DFMT_D24FS8;
  405. #endif // BX_PLATFORM_WINDOWS
  406. postReset();
  407. m_initialized = true;
  408. }
  409. void shutdown()
  410. {
  411. preReset();
  412. for (uint32_t ii = 0; ii < countof(m_indexBuffers); ++ii)
  413. {
  414. m_indexBuffers[ii].destroy();
  415. }
  416. for (uint32_t ii = 0; ii < countof(m_vertexBuffers); ++ii)
  417. {
  418. m_vertexBuffers[ii].destroy();
  419. }
  420. for (uint32_t ii = 0; ii < countof(m_vertexShaders); ++ii)
  421. {
  422. m_vertexShaders[ii].destroy();
  423. }
  424. for (uint32_t ii = 0; ii < countof(m_fragmentShaders); ++ii)
  425. {
  426. m_fragmentShaders[ii].destroy();
  427. }
  428. for (uint32_t ii = 0; ii < countof(m_textures); ++ii)
  429. {
  430. m_textures[ii].destroy();
  431. }
  432. for (uint32_t ii = 0; ii < countof(m_vertexDecls); ++ii)
  433. {
  434. m_vertexDecls[ii].destroy();
  435. }
  436. for (uint32_t ii = 0; ii < countof(m_renderTargets); ++ii)
  437. {
  438. m_renderTargets[ii].destroy();
  439. }
  440. #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
  441. if (NULL != m_d3d9ex)
  442. {
  443. DX_RELEASE(m_deviceEx, 1);
  444. DX_RELEASE(m_device, 0);
  445. DX_RELEASE(m_d3d9, 1);
  446. DX_RELEASE(m_d3d9ex, 0);
  447. }
  448. else
  449. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  450. {
  451. DX_RELEASE(m_device, 0);
  452. DX_RELEASE(m_d3d9, 0);
  453. }
  454. #if BX_PLATFORM_WINDOWS
  455. bx::dlclose(m_d3d9dll);
  456. #endif // BX_PLATFORM_WINDOWS
  457. m_initialized = false;
  458. }
  459. void updateMsaa()
  460. {
  461. for (uint32_t ii = 1, last = 0; ii < countof(s_checkMsaa); ++ii)
  462. {
  463. D3DMULTISAMPLE_TYPE msaa = s_checkMsaa[ii];
  464. DWORD quality;
  465. HRESULT hr = m_d3d9->CheckDeviceMultiSampleType(m_adapter
  466. , m_deviceType
  467. , m_params.BackBufferFormat
  468. , m_params.Windowed
  469. , msaa
  470. , &quality
  471. );
  472. if (SUCCEEDED(hr) )
  473. {
  474. s_msaa[ii].m_type = msaa;
  475. s_msaa[ii].m_quality = bx::uint32_imax(0, quality-1);
  476. last = ii;
  477. }
  478. else
  479. {
  480. s_msaa[ii] = s_msaa[last];
  481. }
  482. }
  483. }
  484. void updateResolution(const Resolution& _resolution)
  485. {
  486. if (m_params.BackBufferWidth != _resolution.m_width
  487. || m_params.BackBufferHeight != _resolution.m_height
  488. || m_flags != _resolution.m_flags)
  489. {
  490. m_flags = _resolution.m_flags;
  491. m_textVideoMem.resize(false, _resolution.m_width, _resolution.m_height);
  492. m_textVideoMem.clear();
  493. #if BX_PLATFORM_WINDOWS
  494. D3DDEVICE_CREATION_PARAMETERS dcp;
  495. DX_CHECK(m_device->GetCreationParameters(&dcp) );
  496. D3DDISPLAYMODE dm;
  497. DX_CHECK(m_d3d9->GetAdapterDisplayMode(dcp.AdapterOrdinal, &dm) );
  498. m_params.BackBufferFormat = dm.Format;
  499. #endif // BX_PLATFORM_WINDOWS
  500. m_params.BackBufferWidth = _resolution.m_width;
  501. m_params.BackBufferHeight = _resolution.m_height;
  502. m_params.FullScreen_RefreshRateInHz = BGFX_RESET_FULLSCREEN == (m_flags&BGFX_RESET_FULLSCREEN_MASK) ? 60 : 0;
  503. m_params.PresentationInterval = !!(m_flags&BGFX_RESET_VSYNC) ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
  504. updateMsaa();
  505. Msaa& msaa = s_msaa[(m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT];
  506. m_params.MultiSampleType = msaa.m_type;
  507. m_params.MultiSampleQuality = msaa.m_quality;
  508. preReset();
  509. DX_CHECK(m_device->Reset(&m_params) );
  510. postReset();
  511. }
  512. }
  513. void setRenderTarget(RenderTargetHandle _rt, bool _msaa = true)
  514. {
  515. if (_rt.idx == invalidHandle)
  516. {
  517. DX_CHECK(m_device->SetRenderTarget(0, m_backBufferColor) );
  518. DX_CHECK(m_device->SetDepthStencilSurface(m_backBufferDepthStencil) );
  519. }
  520. else
  521. {
  522. RenderTarget& renderTarget = m_renderTargets[_rt.idx];
  523. if (NULL != renderTarget.m_rt)
  524. {
  525. DX_CHECK(m_device->SetRenderTarget(0, renderTarget.m_rt) );
  526. }
  527. else
  528. {
  529. DX_CHECK(m_device->SetRenderTarget(0, renderTarget.m_color) );
  530. }
  531. DX_CHECK(m_device->SetDepthStencilSurface(NULL != renderTarget.m_depth ? renderTarget.m_depth : m_backBufferDepthStencil) );
  532. }
  533. if (m_rt.idx != invalidHandle
  534. && m_rt.idx != _rt.idx
  535. && m_rtMsaa)
  536. {
  537. RenderTarget& renderTarget = m_renderTargets[m_rt.idx];
  538. if (!renderTarget.m_depthOnly
  539. && renderTarget.m_rt != NULL)
  540. {
  541. renderTarget.resolve();
  542. }
  543. }
  544. m_rt = _rt;
  545. m_rtMsaa = _msaa;
  546. }
  547. void setShaderConstantF(uint8_t _flags, uint16_t _regIndex, const float* _val, uint16_t _numRegs)
  548. {
  549. if (_flags&BGFX_UNIFORM_FRAGMENTBIT)
  550. {
  551. DX_CHECK(m_device->SetPixelShaderConstantF(_regIndex, _val, _numRegs) );
  552. }
  553. else
  554. {
  555. DX_CHECK(m_device->SetVertexShaderConstantF(_regIndex, _val, _numRegs) );
  556. }
  557. }
  558. void reset()
  559. {
  560. preReset();
  561. HRESULT hr;
  562. do
  563. {
  564. hr = m_device->Reset(&m_params);
  565. } while (FAILED(hr) );
  566. postReset();
  567. }
  568. bool isLost(HRESULT _hr) const
  569. {
  570. return D3DERR_DEVICELOST == _hr
  571. || D3DERR_DRIVERINTERNALERROR == _hr
  572. #if !defined(D3D_DISABLE_9EX)
  573. || D3DERR_DEVICEHUNG == _hr
  574. || D3DERR_DEVICEREMOVED == _hr
  575. #endif // !defined(D3D_DISABLE_9EX)
  576. ;
  577. }
  578. void flip()
  579. {
  580. if (NULL != m_device)
  581. {
  582. #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
  583. if (NULL != m_deviceEx)
  584. {
  585. DX_CHECK(m_deviceEx->WaitForVBlank(0) );
  586. }
  587. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  588. HRESULT hr;
  589. hr = m_device->Present(NULL, NULL, NULL, NULL);
  590. #if BX_PLATFORM_WINDOWS
  591. if (isLost(hr) )
  592. {
  593. do
  594. {
  595. do
  596. {
  597. hr = m_device->TestCooperativeLevel();
  598. }
  599. while (D3DERR_DEVICENOTRESET != hr);
  600. reset();
  601. hr = m_device->TestCooperativeLevel();
  602. }
  603. while (FAILED(hr) );
  604. }
  605. else if (FAILED(hr) )
  606. {
  607. BX_TRACE("Present failed with err 0x%08x.", hr);
  608. }
  609. #endif // BX_PLATFORM_
  610. }
  611. }
  612. void preReset()
  613. {
  614. invalidateSamplerState();
  615. for (uint32_t stage = 0; stage < BGFX_STATE_TEX_COUNT; ++stage)
  616. {
  617. DX_CHECK(m_device->SetTexture(stage, NULL) );
  618. }
  619. DX_CHECK(m_device->SetRenderTarget(0, m_backBufferColor) );
  620. DX_CHECK(m_device->SetDepthStencilSurface(m_backBufferDepthStencil) );
  621. DX_CHECK(m_device->SetVertexShader(NULL) );
  622. DX_CHECK(m_device->SetPixelShader(NULL) );
  623. DX_CHECK(m_device->SetStreamSource(0, NULL, 0, 0) );
  624. DX_CHECK(m_device->SetIndices(NULL) );
  625. DX_RELEASE(m_backBufferColor, 0);
  626. DX_RELEASE(m_backBufferDepthStencil, 0);
  627. capturePreReset();
  628. for (uint32_t ii = 0; ii < countof(m_indexBuffers); ++ii)
  629. {
  630. m_indexBuffers[ii].preReset();
  631. }
  632. for (uint32_t ii = 0; ii < countof(m_vertexBuffers); ++ii)
  633. {
  634. m_vertexBuffers[ii].preReset();
  635. }
  636. for (uint32_t ii = 0; ii < countof(m_renderTargets); ++ii)
  637. {
  638. m_renderTargets[ii].preReset();
  639. }
  640. }
  641. void postReset()
  642. {
  643. DX_CHECK(m_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_backBufferColor) );
  644. DX_CHECK(m_device->GetDepthStencilSurface(&m_backBufferDepthStencil) );
  645. capturePostReset();
  646. for (uint32_t ii = 0; ii < countof(m_indexBuffers); ++ii)
  647. {
  648. m_indexBuffers[ii].postReset();
  649. }
  650. for (uint32_t ii = 0; ii < countof(m_vertexBuffers); ++ii)
  651. {
  652. m_vertexBuffers[ii].postReset();
  653. }
  654. for (uint32_t ii = 0; ii < countof(m_renderTargets); ++ii)
  655. {
  656. m_renderTargets[ii].postReset();
  657. }
  658. }
  659. void invalidateSamplerState()
  660. {
  661. for (uint32_t stage = 0; stage < BGFX_STATE_TEX_COUNT; ++stage)
  662. {
  663. m_samplerFlags[stage] = UINT32_MAX;
  664. }
  665. }
  666. void setSamplerState(uint8_t _stage, uint32_t _flags)
  667. {
  668. const uint32_t flags = _flags&(~BGFX_TEXTURE_RESERVED_MASK);
  669. if (m_samplerFlags[_stage] != flags)
  670. {
  671. m_samplerFlags[_stage] = flags;
  672. IDirect3DDevice9* device = m_device;
  673. D3DTEXTUREADDRESS tau = s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT];
  674. D3DTEXTUREADDRESS tav = s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT];
  675. D3DTEXTUREADDRESS taw = s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT];
  676. D3DTEXTUREFILTERTYPE minFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT];
  677. D3DTEXTUREFILTERTYPE magFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT];
  678. D3DTEXTUREFILTERTYPE mipFilter = s_textureFilter[(_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT];
  679. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_ADDRESSU, tau) );
  680. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_ADDRESSV, tav) );
  681. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_MINFILTER, minFilter) );
  682. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_MAGFILTER, magFilter) );
  683. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_MIPFILTER, mipFilter) );
  684. DX_CHECK(device->SetSamplerState(_stage, D3DSAMP_ADDRESSW, taw) );
  685. }
  686. }
  687. void capturePreReset()
  688. {
  689. if (NULL != m_captureSurface)
  690. {
  691. g_callback->captureEnd();
  692. }
  693. DX_RELEASE(m_captureSurface, 1);
  694. DX_RELEASE(m_captureTexture, 0);
  695. DX_RELEASE(m_captureResolve, 0);
  696. }
  697. void capturePostReset()
  698. {
  699. if (m_flags&BGFX_RESET_CAPTURE)
  700. {
  701. uint32_t width = m_params.BackBufferWidth;
  702. uint32_t height = m_params.BackBufferHeight;
  703. D3DFORMAT fmt = m_params.BackBufferFormat;
  704. DX_CHECK(m_device->CreateTexture(width
  705. , height
  706. , 1
  707. , 0
  708. , fmt
  709. , D3DPOOL_SYSTEMMEM
  710. , &m_captureTexture
  711. , NULL
  712. ) );
  713. DX_CHECK(m_captureTexture->GetSurfaceLevel(0
  714. , &m_captureSurface
  715. ) );
  716. if (m_params.MultiSampleType != D3DMULTISAMPLE_NONE)
  717. {
  718. DX_CHECK(m_device->CreateRenderTarget(width
  719. , height
  720. , fmt
  721. , D3DMULTISAMPLE_NONE
  722. , 0
  723. , false
  724. , &m_captureResolve
  725. , NULL
  726. ) );
  727. }
  728. g_callback->captureBegin(width, height, width*4, TextureFormat::BGRA8, false);
  729. }
  730. }
  731. void capture()
  732. {
  733. if (NULL != m_captureSurface)
  734. {
  735. IDirect3DSurface9* resolve = m_backBufferColor;
  736. if (NULL != m_captureResolve)
  737. {
  738. resolve = m_captureResolve;
  739. DX_CHECK(m_device->StretchRect(m_backBufferColor
  740. , 0
  741. , m_captureResolve
  742. , NULL
  743. , D3DTEXF_NONE
  744. ) );
  745. }
  746. HRESULT hr = m_device->GetRenderTargetData(resolve, m_captureSurface);
  747. if (SUCCEEDED(hr) )
  748. {
  749. D3DLOCKED_RECT rect;
  750. DX_CHECK(m_captureSurface->LockRect(&rect
  751. , NULL
  752. , D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY
  753. ) );
  754. g_callback->captureFrame(rect.pBits, m_params.BackBufferHeight*rect.Pitch);
  755. DX_CHECK(m_captureSurface->UnlockRect() );
  756. }
  757. }
  758. }
  759. void saveScreenShot(const char* _filePath)
  760. {
  761. #if BX_PLATFORM_WINDOWS
  762. IDirect3DSurface9* surface;
  763. D3DDEVICE_CREATION_PARAMETERS dcp;
  764. DX_CHECK(m_device->GetCreationParameters(&dcp) );
  765. D3DDISPLAYMODE dm;
  766. DX_CHECK(m_d3d9->GetAdapterDisplayMode(dcp.AdapterOrdinal, &dm) );
  767. DX_CHECK(m_device->CreateOffscreenPlainSurface(dm.Width
  768. , dm.Height
  769. , D3DFMT_A8R8G8B8
  770. , D3DPOOL_SCRATCH
  771. , &surface
  772. , NULL
  773. ) );
  774. DX_CHECK(m_device->GetFrontBufferData(0, surface) );
  775. D3DLOCKED_RECT rect;
  776. DX_CHECK(surface->LockRect(&rect
  777. , NULL
  778. , D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY
  779. ) );
  780. RECT rc;
  781. GetClientRect(g_bgfxHwnd, &rc);
  782. POINT point;
  783. point.x = rc.left;
  784. point.y = rc.top;
  785. ClientToScreen(g_bgfxHwnd, &point);
  786. uint8_t* data = (uint8_t*)rect.pBits;
  787. uint32_t bytesPerPixel = rect.Pitch/dm.Width;
  788. g_callback->screenShot(_filePath
  789. , m_params.BackBufferWidth
  790. , m_params.BackBufferHeight
  791. , rect.Pitch
  792. , &data[point.y*rect.Pitch+point.x*bytesPerPixel]
  793. , m_params.BackBufferHeight*rect.Pitch
  794. , false
  795. );
  796. DX_CHECK(surface->UnlockRect() );
  797. DX_RELEASE(surface, 0);
  798. #endif // BX_PLATFORM_WINDOWS
  799. }
  800. #if BX_PLATFORM_WINDOWS
  801. D3DCAPS9 m_caps;
  802. # if BGFX_CONFIG_DEBUG_PIX
  803. D3DPERF_SetMarkerFunc m_D3DPERF_SetMarker;
  804. D3DPERF_BeginEventFunc m_D3DPERF_BeginEvent;
  805. D3DPERF_EndEventFunc m_D3DPERF_EndEvent;
  806. # endif // BGFX_CONFIG_DEBUG_PIX
  807. #endif // BX_PLATFORM_WINDOWS
  808. #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
  809. IDirect3D9Ex* m_d3d9ex;
  810. IDirect3DDevice9Ex* m_deviceEx;
  811. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
  812. IDirect3D9* m_d3d9;
  813. IDirect3DDevice9* m_device;
  814. D3DPOOL m_pool;
  815. IDirect3DSurface9* m_backBufferColor;
  816. IDirect3DSurface9* m_backBufferDepthStencil;
  817. IDirect3DTexture9* m_captureTexture;
  818. IDirect3DSurface9* m_captureSurface;
  819. IDirect3DSurface9* m_captureResolve;
  820. IDirect3DVertexDeclaration9* m_instanceDataDecls[BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT];
  821. void* m_d3d9dll;
  822. uint32_t m_adapter;
  823. D3DDEVTYPE m_deviceType;
  824. D3DPRESENT_PARAMETERS m_params;
  825. uint32_t m_flags;
  826. D3DADAPTER_IDENTIFIER9 m_identifier;
  827. bool m_initialized;
  828. bool m_amd;
  829. bool m_nvidia;
  830. bool m_instancing;
  831. D3DFORMAT m_fmtDepth;
  832. IndexBuffer m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  833. VertexBuffer m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  834. Shader m_vertexShaders[BGFX_CONFIG_MAX_VERTEX_SHADERS];
  835. Shader m_fragmentShaders[BGFX_CONFIG_MAX_FRAGMENT_SHADERS];
  836. Program m_program[BGFX_CONFIG_MAX_PROGRAMS];
  837. Texture m_textures[BGFX_CONFIG_MAX_TEXTURES];
  838. VertexDeclaration m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS];
  839. RenderTarget m_renderTargets[BGFX_CONFIG_MAX_RENDER_TARGETS];
  840. UniformRegistry m_uniformReg;
  841. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  842. uint32_t m_samplerFlags[BGFX_STATE_TEX_COUNT];
  843. Texture* m_updateTexture;
  844. uint8_t* m_updateTextureBits;
  845. uint32_t m_updateTexturePitch;
  846. uint8_t m_updateTextureSide;
  847. uint8_t m_updateTextureMip;
  848. TextVideoMem m_textVideoMem;
  849. RenderTargetHandle m_rt;
  850. bool m_rtMsaa;
  851. };
  852. static RendererContext s_renderCtx;
  853. void IndexBuffer::create(uint32_t _size, void* _data)
  854. {
  855. m_size = _size;
  856. m_dynamic = NULL == _data;
  857. uint32_t usage = D3DUSAGE_WRITEONLY;
  858. D3DPOOL pool = s_renderCtx.m_pool;
  859. if (m_dynamic)
  860. {
  861. usage |= D3DUSAGE_DYNAMIC;
  862. pool = D3DPOOL_DEFAULT;
  863. }
  864. DX_CHECK(s_renderCtx.m_device->CreateIndexBuffer(m_size
  865. , usage
  866. , D3DFMT_INDEX16
  867. , pool
  868. , &m_ptr
  869. , NULL
  870. ) );
  871. if (NULL != _data)
  872. {
  873. update(0, _size, _data);
  874. }
  875. }
  876. void IndexBuffer::preReset()
  877. {
  878. if (m_dynamic)
  879. {
  880. DX_RELEASE(m_ptr, 0);
  881. }
  882. }
  883. void IndexBuffer::postReset()
  884. {
  885. if (m_dynamic)
  886. {
  887. DX_CHECK(s_renderCtx.m_device->CreateIndexBuffer(m_size
  888. , D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC
  889. , D3DFMT_INDEX16
  890. , D3DPOOL_DEFAULT
  891. , &m_ptr
  892. , NULL
  893. ) );
  894. }
  895. }
  896. void VertexBuffer::create(uint32_t _size, void* _data, VertexDeclHandle _declHandle)
  897. {
  898. m_size = _size;
  899. m_decl = _declHandle;
  900. m_dynamic = NULL == _data;
  901. uint32_t usage = D3DUSAGE_WRITEONLY;
  902. D3DPOOL pool = s_renderCtx.m_pool;
  903. if (m_dynamic)
  904. {
  905. usage |= D3DUSAGE_DYNAMIC;
  906. pool = D3DPOOL_DEFAULT;
  907. }
  908. DX_CHECK(s_renderCtx.m_device->CreateVertexBuffer(m_size
  909. , usage
  910. , 0
  911. , pool
  912. , &m_ptr
  913. , NULL
  914. ) );
  915. if (NULL != _data)
  916. {
  917. update(0, _size, _data);
  918. }
  919. }
  920. void VertexBuffer::preReset()
  921. {
  922. if (m_dynamic)
  923. {
  924. DX_RELEASE(m_ptr, 0);
  925. }
  926. }
  927. void VertexBuffer::postReset()
  928. {
  929. if (m_dynamic)
  930. {
  931. DX_CHECK(s_renderCtx.m_device->CreateVertexBuffer(m_size
  932. , D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC
  933. , 0
  934. , D3DPOOL_DEFAULT
  935. , &m_ptr
  936. , NULL
  937. ) );
  938. }
  939. }
  940. static const D3DVERTEXELEMENT9 s_attrib[Attrib::Count+1] =
  941. {
  942. { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  943. { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
  944. { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
  945. { 0, 0, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
  946. { 0, 0, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 1 },
  947. { 0, 0, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
  948. { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
  949. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
  950. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
  951. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2 },
  952. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3 },
  953. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4 },
  954. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 5 },
  955. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 6 },
  956. { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 7 },
  957. D3DDECL_END()
  958. };
  959. static const D3DDECLTYPE s_attribType[AttribType::Count][4][2] =
  960. {
  961. {
  962. { D3DDECLTYPE_UBYTE4, D3DDECLTYPE_UBYTE4N },
  963. { D3DDECLTYPE_UBYTE4, D3DDECLTYPE_UBYTE4N },
  964. { D3DDECLTYPE_UBYTE4, D3DDECLTYPE_UBYTE4N },
  965. { D3DDECLTYPE_UBYTE4, D3DDECLTYPE_UBYTE4N },
  966. },
  967. {
  968. { D3DDECLTYPE_SHORT2, D3DDECLTYPE_SHORT2N },
  969. { D3DDECLTYPE_SHORT2, D3DDECLTYPE_SHORT2N },
  970. { D3DDECLTYPE_SHORT4, D3DDECLTYPE_SHORT4N },
  971. { D3DDECLTYPE_SHORT4, D3DDECLTYPE_SHORT4N },
  972. },
  973. {
  974. { D3DDECLTYPE_FLOAT16_2, D3DDECLTYPE_FLOAT16_2 },
  975. { D3DDECLTYPE_FLOAT16_2, D3DDECLTYPE_FLOAT16_2 },
  976. { D3DDECLTYPE_FLOAT16_4, D3DDECLTYPE_FLOAT16_4 },
  977. { D3DDECLTYPE_FLOAT16_4, D3DDECLTYPE_FLOAT16_4 },
  978. },
  979. {
  980. { D3DDECLTYPE_FLOAT1, D3DDECLTYPE_FLOAT1 },
  981. { D3DDECLTYPE_FLOAT2, D3DDECLTYPE_FLOAT2 },
  982. { D3DDECLTYPE_FLOAT3, D3DDECLTYPE_FLOAT3 },
  983. { D3DDECLTYPE_FLOAT4, D3DDECLTYPE_FLOAT4 },
  984. },
  985. };
  986. static D3DVERTEXELEMENT9* fillVertexDecl(D3DVERTEXELEMENT9* _out, const VertexDecl& _decl)
  987. {
  988. D3DVERTEXELEMENT9* elem = _out;
  989. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  990. {
  991. if (0xff != _decl.m_attributes[attr])
  992. {
  993. uint8_t num;
  994. AttribType::Enum type;
  995. bool normalized;
  996. bool asInt;
  997. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  998. memcpy(elem, &s_attrib[attr], sizeof(D3DVERTEXELEMENT9) );
  999. elem->Type = s_attribType[type][num-1][normalized];
  1000. elem->Offset = _decl.m_offset[attr];
  1001. ++elem;
  1002. }
  1003. }
  1004. return elem;
  1005. }
  1006. static IDirect3DVertexDeclaration9* createVertexDecl(const VertexDecl& _decl, uint8_t _numInstanceData)
  1007. {
  1008. D3DVERTEXELEMENT9 vertexElements[Attrib::Count+1+BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT];
  1009. D3DVERTEXELEMENT9* elem = fillVertexDecl(vertexElements, _decl);
  1010. const D3DVERTEXELEMENT9 inst = { 1, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 };
  1011. for (uint32_t ii = 0; ii < _numInstanceData; ++ii)
  1012. {
  1013. memcpy(elem, &inst, sizeof(D3DVERTEXELEMENT9) );
  1014. elem->UsageIndex = 8-_numInstanceData+ii;
  1015. elem->Offset = ii*16;
  1016. ++elem;
  1017. }
  1018. memcpy(elem, &s_attrib[Attrib::Count], sizeof(D3DVERTEXELEMENT9) );
  1019. IDirect3DVertexDeclaration9* ptr;
  1020. DX_CHECK(s_renderCtx.m_device->CreateVertexDeclaration(vertexElements, &ptr) );
  1021. return ptr;
  1022. }
  1023. void VertexDeclaration::create(const VertexDecl& _decl)
  1024. {
  1025. memcpy(&m_decl, &_decl, sizeof(VertexDecl) );
  1026. dump(m_decl);
  1027. m_ptr = createVertexDecl(_decl, 0);
  1028. }
  1029. void Shader::create(bool _fragment, const Memory* _mem)
  1030. {
  1031. bx::MemoryReader reader(_mem->data, _mem->size);
  1032. uint32_t magic;
  1033. bx::read(&reader, magic);
  1034. uint32_t iohash;
  1035. bx::read(&reader, iohash);
  1036. uint16_t count;
  1037. bx::read(&reader, count);
  1038. m_numPredefined = 0;
  1039. BX_TRACE("Shader consts %d", count);
  1040. uint8_t fragmentBit = _fragment ? BGFX_UNIFORM_FRAGMENTBIT : 0;
  1041. if (0 < count)
  1042. {
  1043. m_constantBuffer = ConstantBuffer::create(1024);
  1044. for (uint32_t ii = 0; ii < count; ++ii)
  1045. {
  1046. uint8_t nameSize;
  1047. bx::read(&reader, nameSize);
  1048. char name[256];
  1049. bx::read(&reader, &name, nameSize);
  1050. name[nameSize] = '\0';
  1051. uint8_t type;
  1052. bx::read(&reader, type);
  1053. uint8_t num;
  1054. bx::read(&reader, num);
  1055. uint16_t regIndex;
  1056. bx::read(&reader, regIndex);
  1057. uint16_t regCount;
  1058. bx::read(&reader, regCount);
  1059. const char* kind = "invalid";
  1060. const void* data = NULL;
  1061. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  1062. if (PredefinedUniform::Count != predefined)
  1063. {
  1064. kind = "predefined";
  1065. m_predefined[m_numPredefined].m_loc = regIndex;
  1066. m_predefined[m_numPredefined].m_count = regCount;
  1067. m_predefined[m_numPredefined].m_type = predefined|fragmentBit;
  1068. m_numPredefined++;
  1069. }
  1070. else
  1071. {
  1072. const UniformInfo* info = s_renderCtx.m_uniformReg.find(name);
  1073. BX_CHECK(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  1074. if (NULL != info)
  1075. {
  1076. kind = "user";
  1077. data = info->m_data;
  1078. m_constantBuffer->writeUniformRef( (UniformType::Enum)(type|fragmentBit), regIndex, data, regCount);
  1079. }
  1080. }
  1081. BX_TRACE("\t%s: %s, type %2d, num %2d, r.index %3d, r.count %2d"
  1082. , kind
  1083. , name
  1084. , type
  1085. , num
  1086. , regIndex
  1087. , regCount
  1088. );
  1089. BX_UNUSED(kind);
  1090. }
  1091. m_constantBuffer->finish();
  1092. }
  1093. uint16_t shaderSize;
  1094. bx::read(&reader, shaderSize);
  1095. const DWORD* code = (const DWORD*)reader.getDataPtr();
  1096. if (_fragment)
  1097. {
  1098. DX_CHECK(s_renderCtx.m_device->CreatePixelShader(code, (IDirect3DPixelShader9**)&m_ptr) );
  1099. BGFX_FATAL(NULL != m_ptr, bgfx::Fatal::InvalidShader, "Failed to create fragment shader.");
  1100. }
  1101. else
  1102. {
  1103. DX_CHECK(s_renderCtx.m_device->CreateVertexShader(code, (IDirect3DVertexShader9**)&m_ptr) );
  1104. BGFX_FATAL(NULL != m_ptr, bgfx::Fatal::InvalidShader, "Failed to create vertex shader.");
  1105. }
  1106. }
  1107. void Texture::createTexture(uint32_t _width, uint32_t _height, uint8_t _numMips, D3DFORMAT _fmt)
  1108. {
  1109. m_type = Texture2D;
  1110. DX_CHECK(s_renderCtx.m_device->CreateTexture(_width
  1111. , _height
  1112. , _numMips
  1113. , 0
  1114. , _fmt
  1115. , s_renderCtx.m_pool
  1116. , &m_texture2d
  1117. , NULL
  1118. ) );
  1119. BGFX_FATAL(NULL != m_texture2d, Fatal::UnableToCreateTexture, "Failed to create texture (size: %dx%d, mips: %d, fmt: 0x%08x)."
  1120. , _width
  1121. , _height
  1122. , _numMips
  1123. , _fmt
  1124. );
  1125. }
  1126. void Texture::createVolumeTexture(uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _numMips, D3DFORMAT _fmt)
  1127. {
  1128. m_type = Texture3D;
  1129. DX_CHECK(s_renderCtx.m_device->CreateVolumeTexture(_width
  1130. , _height
  1131. , _depth
  1132. , _numMips
  1133. , 0
  1134. , _fmt
  1135. , s_renderCtx.m_pool
  1136. , &m_texture3d
  1137. , NULL
  1138. ) );
  1139. BGFX_FATAL(NULL != m_texture3d, Fatal::UnableToCreateTexture, "Failed to create volume texture (size: %dx%dx%d, mips: %d, fmt: 0x%08x)."
  1140. , _width
  1141. , _height
  1142. , _depth
  1143. , _numMips
  1144. , _fmt
  1145. );
  1146. }
  1147. void Texture::createCubeTexture(uint32_t _edge, uint32_t _numMips, D3DFORMAT _fmt)
  1148. {
  1149. m_type = TextureCube;
  1150. DX_CHECK(s_renderCtx.m_device->CreateCubeTexture(_edge
  1151. , _numMips
  1152. , 0
  1153. , _fmt
  1154. , s_renderCtx.m_pool
  1155. , &m_textureCube
  1156. , NULL
  1157. ) );
  1158. BGFX_FATAL(NULL != m_textureCube, Fatal::UnableToCreateTexture, "Failed to create cube texture (edge: %d, mips: %d, fmt: 0x%08x)."
  1159. , _edge
  1160. , _numMips
  1161. , _fmt
  1162. );
  1163. }
  1164. uint8_t* Texture::lock(uint8_t _side, uint8_t _lod, uint32_t& _pitch, uint32_t& _slicePitch, const Rect* _rect)
  1165. {
  1166. switch (m_type)
  1167. {
  1168. case Texture2D:
  1169. {
  1170. D3DLOCKED_RECT lockedRect;
  1171. if (NULL != _rect)
  1172. {
  1173. RECT rect;
  1174. rect.left = _rect->m_x;
  1175. rect.top = _rect->m_y;
  1176. rect.right = rect.left + _rect->m_width;
  1177. rect.bottom = rect.top + _rect->m_height;
  1178. DX_CHECK(m_texture2d->LockRect(_lod, &lockedRect, &rect, 0) );
  1179. }
  1180. else
  1181. {
  1182. DX_CHECK(m_texture2d->LockRect(_lod, &lockedRect, NULL, 0) );
  1183. }
  1184. _pitch = lockedRect.Pitch;
  1185. _slicePitch = 0;
  1186. return (uint8_t*)lockedRect.pBits;
  1187. }
  1188. case Texture3D:
  1189. {
  1190. D3DLOCKED_BOX box;
  1191. DX_CHECK(m_texture3d->LockBox(_lod, &box, NULL, 0) );
  1192. _pitch = box.RowPitch;
  1193. _slicePitch = box.SlicePitch;
  1194. return (uint8_t*)box.pBits;
  1195. }
  1196. case TextureCube:
  1197. {
  1198. D3DLOCKED_RECT lockedRect;
  1199. if (NULL != _rect)
  1200. {
  1201. RECT rect;
  1202. rect.left = _rect->m_x;
  1203. rect.top = _rect->m_y;
  1204. rect.right = rect.left + _rect->m_width;
  1205. rect.bottom = rect.top + _rect->m_height;
  1206. DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &lockedRect, &rect, 0) );
  1207. }
  1208. else
  1209. {
  1210. DX_CHECK(m_textureCube->LockRect(D3DCUBEMAP_FACES(_side), _lod, &lockedRect, NULL, 0) );
  1211. }
  1212. _pitch = lockedRect.Pitch;
  1213. _slicePitch = 0;
  1214. return (uint8_t*)lockedRect.pBits;
  1215. }
  1216. }
  1217. BX_CHECK(false, "You should not be here.");
  1218. return NULL;
  1219. }
  1220. void Texture::unlock(uint8_t _side, uint8_t _lod)
  1221. {
  1222. switch (m_type)
  1223. {
  1224. case Texture2D:
  1225. {
  1226. DX_CHECK(m_texture2d->UnlockRect(_lod) );
  1227. }
  1228. return;
  1229. case Texture3D:
  1230. {
  1231. DX_CHECK(m_texture3d->UnlockBox(_lod) );
  1232. }
  1233. return;
  1234. case TextureCube:
  1235. {
  1236. DX_CHECK(m_textureCube->UnlockRect(D3DCUBEMAP_FACES(_side), _lod) );
  1237. }
  1238. return;
  1239. }
  1240. BX_CHECK(false, "You should not be here.");
  1241. }
  1242. void Texture::dirty(uint8_t _side, const Rect& _rect, uint16_t _z, uint16_t _depth)
  1243. {
  1244. switch (m_type)
  1245. {
  1246. case Texture2D:
  1247. {
  1248. RECT rect;
  1249. rect.left = _rect.m_x;
  1250. rect.top = _rect.m_y;
  1251. rect.right = rect.left + _rect.m_width;
  1252. rect.bottom = rect.top + _rect.m_height;
  1253. DX_CHECK(m_texture2d->AddDirtyRect(&rect) );
  1254. }
  1255. return;
  1256. case Texture3D:
  1257. {
  1258. D3DBOX box;
  1259. box.Left = _rect.m_x;
  1260. box.Top = _rect.m_y;
  1261. box.Right = box.Left + _rect.m_width;
  1262. box.Bottom = box.Top + _rect.m_height;
  1263. box.Front = _z;
  1264. box.Back = box.Front + _depth;
  1265. DX_CHECK(m_texture3d->AddDirtyBox(&box) );
  1266. }
  1267. return;
  1268. case TextureCube:
  1269. {
  1270. RECT rect;
  1271. rect.left = _rect.m_x;
  1272. rect.top = _rect.m_y;
  1273. rect.right = rect.left + _rect.m_width;
  1274. rect.bottom = rect.top + _rect.m_height;
  1275. DX_CHECK(m_textureCube->AddDirtyRect(D3DCUBEMAP_FACES(_side), &rect) );
  1276. }
  1277. return;
  1278. }
  1279. BX_CHECK(false, "You should not be here.");
  1280. }
  1281. void Texture::create(const Memory* _mem, uint32_t _flags)
  1282. {
  1283. m_flags = _flags;
  1284. Dds dds;
  1285. if (parseDds(dds, _mem) )
  1286. {
  1287. m_format = dds.m_type;
  1288. const TextureFormatInfo& tfi = s_textureFormat[dds.m_type];
  1289. bool decompress = false
  1290. || (TextureFormat::BC4 == dds.m_type && !s_extendedFormats[ExtendedFormat::Ati1].m_supported)
  1291. || (TextureFormat::BC5 == dds.m_type && !s_extendedFormats[ExtendedFormat::Ati2].m_supported)
  1292. ;
  1293. D3DFORMAT format = decompress ? D3DFMT_A8R8G8B8 : tfi.m_fmt;
  1294. uint8_t bpp = decompress ? 32 : tfi.m_bpp;
  1295. if (dds.m_cubeMap)
  1296. {
  1297. createCubeTexture(dds.m_width, dds.m_numMips, format);
  1298. }
  1299. else if (dds.m_depth > 1)
  1300. {
  1301. createVolumeTexture(dds.m_width, dds.m_height, dds.m_depth, dds.m_numMips, format);
  1302. }
  1303. else
  1304. {
  1305. createTexture(dds.m_width, dds.m_height, dds.m_numMips, format);
  1306. }
  1307. if (decompress
  1308. || TextureFormat::Unknown < dds.m_type)
  1309. {
  1310. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1311. {
  1312. uint32_t width = dds.m_width;
  1313. uint32_t height = dds.m_height;
  1314. uint32_t depth = dds.m_depth;
  1315. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  1316. {
  1317. width = bx::uint32_max(1, width);
  1318. height = bx::uint32_max(1, height);
  1319. depth = bx::uint32_max(1, depth);
  1320. Mip mip;
  1321. if (getRawImageData(dds, side, lod, _mem, mip) )
  1322. {
  1323. uint32_t pitch;
  1324. uint32_t slicePitch;
  1325. uint8_t* bits = lock(side, lod, pitch, slicePitch);
  1326. if (width != mip.m_width
  1327. || height != mip.m_height)
  1328. {
  1329. uint32_t srcpitch = mip.m_width*bpp/8;
  1330. uint8_t* temp = (uint8_t*)g_realloc(NULL, srcpitch*mip.m_height);
  1331. mip.decode(temp);
  1332. uint32_t dstpitch = pitch;
  1333. for (uint32_t yy = 0; yy < height; ++yy)
  1334. {
  1335. uint8_t* src = &temp[yy*srcpitch];
  1336. uint8_t* dst = &bits[yy*dstpitch];
  1337. memcpy(dst, src, srcpitch);
  1338. }
  1339. g_free(temp);
  1340. }
  1341. else
  1342. {
  1343. mip.decode(bits);
  1344. }
  1345. unlock(side, lod);
  1346. }
  1347. width >>= 1;
  1348. height >>= 1;
  1349. depth >>= 1;
  1350. }
  1351. }
  1352. }
  1353. else
  1354. {
  1355. // For BC4 and B5 in DX9 LockRect returns wrong number of
  1356. // bytes. If actual mip size is used it causes memory corruption.
  1357. // http://www.aras-p.info/texts/D3D9GPUHacks.html#3dc
  1358. bool useMipSize = true
  1359. && dds.m_type != TextureFormat::BC4
  1360. && dds.m_type != TextureFormat::BC5
  1361. ;
  1362. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1363. {
  1364. uint32_t width = dds.m_width;
  1365. uint32_t height = dds.m_height;
  1366. uint32_t depth = dds.m_depth;
  1367. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  1368. {
  1369. width = bx::uint32_max(1, width);
  1370. height = bx::uint32_max(1, height);
  1371. depth = bx::uint32_max(1, depth);
  1372. Mip mip;
  1373. if (getRawImageData(dds, 0, lod, _mem, mip) )
  1374. {
  1375. uint32_t pitch;
  1376. uint32_t slicePitch;
  1377. uint8_t* dst = lock(side, lod, pitch, slicePitch);
  1378. uint32_t size = useMipSize ? mip.m_size : width*height*depth*bpp/8;
  1379. memcpy(dst, mip.m_data, size);
  1380. unlock(side, lod);
  1381. }
  1382. width >>= 1;
  1383. height >>= 1;
  1384. depth >>= 1;
  1385. }
  1386. }
  1387. }
  1388. }
  1389. else
  1390. {
  1391. bx::MemoryReader reader(_mem->data, _mem->size);
  1392. uint32_t magic;
  1393. bx::read(&reader, magic);
  1394. if (BGFX_CHUNK_MAGIC_TEX == magic)
  1395. {
  1396. TextureCreate tc;
  1397. bx::read(&reader, tc);
  1398. m_format = (TextureFormat::Enum)tc.m_format;
  1399. if (tc.m_cubeMap)
  1400. {
  1401. createCubeTexture(tc.m_width, tc.m_numMips, s_textureFormat[tc.m_format].m_fmt);
  1402. }
  1403. else if (tc.m_depth > 1)
  1404. {
  1405. createVolumeTexture(tc.m_width, tc.m_height, tc.m_depth, tc.m_numMips, s_textureFormat[tc.m_format].m_fmt);
  1406. }
  1407. else
  1408. {
  1409. createTexture(tc.m_width, tc.m_height, tc.m_numMips, s_textureFormat[tc.m_format].m_fmt);
  1410. }
  1411. if (NULL != tc.m_mem)
  1412. {
  1413. uint32_t bpp = s_textureFormat[tc.m_format].m_bpp;
  1414. uint8_t* data = tc.m_mem->data;
  1415. for (uint8_t side = 0, numSides = tc.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1416. {
  1417. uint32_t width = tc.m_width;
  1418. uint32_t height = tc.m_height;
  1419. uint32_t depth = tc.m_depth;
  1420. for (uint32_t lod = 0, num = tc.m_numMips; lod < num; ++lod)
  1421. {
  1422. width = bx::uint32_max(1, width);
  1423. height = bx::uint32_max(1, height);
  1424. depth = bx::uint32_max(1, depth);
  1425. uint32_t pitch;
  1426. uint32_t slicePitch;
  1427. uint8_t* dst = lock(side, lod, pitch, slicePitch);
  1428. uint32_t len = width*height*bpp/8;
  1429. memcpy(dst, data, len);
  1430. data += len;
  1431. unlock(side, lod);
  1432. width >>= 1;
  1433. height >>= 1;
  1434. depth >>= 1;
  1435. }
  1436. }
  1437. release(tc.m_mem);
  1438. }
  1439. }
  1440. else
  1441. {
  1442. //
  1443. }
  1444. }
  1445. }
  1446. void Texture::updateBegin(uint8_t _side, uint8_t _mip)
  1447. {
  1448. uint32_t slicePitch;
  1449. s_renderCtx.m_updateTextureSide = _side;
  1450. s_renderCtx.m_updateTextureMip = _mip;
  1451. s_renderCtx.m_updateTextureBits = lock(_side, _mip, s_renderCtx.m_updateTexturePitch, slicePitch);
  1452. }
  1453. void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
  1454. {
  1455. uint32_t bpp = s_textureFormat[m_format].m_bpp;
  1456. uint32_t srcpitch = _rect.m_width*bpp/8;
  1457. uint32_t dstpitch = s_renderCtx.m_updateTexturePitch;
  1458. uint8_t* bits = s_renderCtx.m_updateTextureBits + _rect.m_y*dstpitch + _rect.m_x*bpp/8;
  1459. if (srcpitch == dstpitch)
  1460. {
  1461. memcpy(bits, _mem->data, srcpitch*_rect.m_height);
  1462. }
  1463. else
  1464. {
  1465. for (uint32_t yy = 0, height = _rect.m_height; yy < height; ++yy)
  1466. {
  1467. uint8_t* src = &_mem->data[yy*srcpitch];
  1468. uint8_t* dst = &bits[yy*dstpitch];
  1469. memcpy(dst, src, srcpitch);
  1470. }
  1471. }
  1472. if (0 == _mip)
  1473. {
  1474. dirty(_side, _rect, _z, _depth);
  1475. }
  1476. }
  1477. void Texture::updateEnd()
  1478. {
  1479. unlock(s_renderCtx.m_updateTextureSide, s_renderCtx.m_updateTextureMip);
  1480. }
  1481. void Texture::commit(uint8_t _stage, uint32_t _flags)
  1482. {
  1483. s_renderCtx.setSamplerState(_stage, 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) ? _flags : m_flags);
  1484. DX_CHECK(s_renderCtx.m_device->SetTexture(_stage, m_ptr) );
  1485. }
  1486. void RenderTarget::create(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
  1487. {
  1488. m_width = _width;
  1489. m_height = _height;
  1490. m_flags = _flags;
  1491. m_textureFlags = (_textureFlags&(BGFX_TEXTURE_MIN_MASK|BGFX_TEXTURE_MAG_MASK) )
  1492. | BGFX_TEXTURE_U_CLAMP
  1493. | BGFX_TEXTURE_V_CLAMP
  1494. ;
  1495. createTextures();
  1496. }
  1497. void RenderTarget::createTextures()
  1498. {
  1499. if (0 != m_flags)
  1500. {
  1501. m_msaa = s_msaa[(m_flags&BGFX_RENDER_TARGET_MSAA_MASK)>>BGFX_RENDER_TARGET_MSAA_SHIFT];
  1502. uint32_t colorFormat = (m_flags&BGFX_RENDER_TARGET_COLOR_MASK)>>BGFX_RENDER_TARGET_COLOR_SHIFT;
  1503. uint32_t depthFormat = (m_flags&BGFX_RENDER_TARGET_DEPTH_MASK)>>BGFX_RENDER_TARGET_DEPTH_SHIFT;
  1504. m_depthOnly = (0 == colorFormat && 0 < depthFormat);
  1505. // CheckDeviceFormat D3DUSAGE_SRGBWRITE
  1506. if (m_depthOnly)
  1507. {
  1508. DX_CHECK(s_renderCtx.m_device->CreateRenderTarget(1
  1509. , 1
  1510. , D3DFMT_R5G6B5
  1511. , D3DMULTISAMPLE_NONE
  1512. , 0
  1513. , false
  1514. , &m_rt
  1515. , NULL
  1516. ) );
  1517. BGFX_FATAL(m_rt, Fatal::UnableToCreateRenderTarget, "Unable to create 1x1 render target.");
  1518. DX_CHECK(s_renderCtx.m_device->CreateTexture(m_width
  1519. , m_height
  1520. , 1
  1521. , D3DUSAGE_DEPTHSTENCIL
  1522. , D3DFMT_DF24 //s_depthFormat[depthFormat]
  1523. , D3DPOOL_DEFAULT
  1524. , &m_depthTexture
  1525. , NULL
  1526. ) );
  1527. BGFX_FATAL(m_depthTexture, Fatal::UnableToCreateRenderTarget, "Unable to create depth texture.");
  1528. DX_CHECK(m_depthTexture->GetSurfaceLevel(0, &m_depth) );
  1529. }
  1530. else
  1531. {
  1532. if (D3DMULTISAMPLE_NONE != m_msaa.m_type)
  1533. {
  1534. DX_CHECK(s_renderCtx.m_device->CreateRenderTarget(m_width
  1535. , m_height
  1536. , s_colorFormat[colorFormat]
  1537. , m_msaa.m_type
  1538. , m_msaa.m_quality
  1539. , false
  1540. , &m_rt
  1541. , NULL
  1542. ) );
  1543. BGFX_FATAL(m_rt, Fatal::UnableToCreateRenderTarget, "Unable to create MSAA render target.");
  1544. }
  1545. if (0 < colorFormat)
  1546. {
  1547. DX_CHECK(s_renderCtx.m_device->CreateTexture(m_width
  1548. , m_height
  1549. , 1
  1550. , D3DUSAGE_RENDERTARGET
  1551. , s_colorFormat[colorFormat]
  1552. , D3DPOOL_DEFAULT
  1553. , &m_colorTexture
  1554. , NULL
  1555. ) );
  1556. BGFX_FATAL(m_colorTexture, Fatal::UnableToCreateRenderTarget, "Unable to create color render target.");
  1557. DX_CHECK(m_colorTexture->GetSurfaceLevel(0, &m_color) );
  1558. }
  1559. if (0 < depthFormat)
  1560. {
  1561. DX_CHECK(s_renderCtx.m_device->CreateDepthStencilSurface(m_width
  1562. , m_height
  1563. , s_depthFormat[depthFormat] // s_renderCtx.m_fmtDepth
  1564. , m_msaa.m_type
  1565. , m_msaa.m_quality
  1566. , FALSE
  1567. , &m_depth
  1568. , NULL
  1569. ) );
  1570. BGFX_FATAL(m_depth, Fatal::UnableToCreateRenderTarget, "Unable to create depth stencil surface.");
  1571. }
  1572. }
  1573. }
  1574. }
  1575. void RenderTarget::destroyTextures()
  1576. {
  1577. if (0 != m_flags)
  1578. {
  1579. if (m_depthOnly)
  1580. {
  1581. DX_RELEASE(m_rt, 0);
  1582. DX_RELEASE(m_depth, 1);
  1583. DX_RELEASE(m_depthTexture, 0);
  1584. }
  1585. else
  1586. {
  1587. uint32_t colorFormat = (m_flags&BGFX_RENDER_TARGET_COLOR_MASK)>>BGFX_RENDER_TARGET_COLOR_SHIFT;
  1588. uint32_t depthFormat = (m_flags&BGFX_RENDER_TARGET_DEPTH_MASK)>>BGFX_RENDER_TARGET_DEPTH_SHIFT;
  1589. if (D3DMULTISAMPLE_NONE != m_msaa.m_type)
  1590. {
  1591. DX_RELEASE(m_rt, 0);
  1592. }
  1593. if (0 < colorFormat)
  1594. {
  1595. DX_RELEASE(m_color, 1);
  1596. DX_RELEASE(m_colorTexture, 0);
  1597. }
  1598. if (0 < depthFormat)
  1599. {
  1600. DX_RELEASE(m_depth, 0);
  1601. }
  1602. }
  1603. }
  1604. }
  1605. void RenderTarget::commit(uint8_t _stage, uint32_t _textureFlags)
  1606. {
  1607. s_renderCtx.setSamplerState(_stage, 0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _textureFlags) ? _textureFlags : m_textureFlags);
  1608. DX_CHECK(s_renderCtx.m_device->SetTexture(_stage, m_depthOnly ? m_depthTexture : m_colorTexture) );
  1609. }
  1610. void RenderTarget::resolve()
  1611. {
  1612. #if BX_PLATFORM_WINDOWS
  1613. DX_CHECK(s_renderCtx.m_device->StretchRect(m_rt
  1614. , NULL
  1615. , m_color
  1616. , NULL
  1617. , D3DTEXF_NONE
  1618. ) );
  1619. #endif // BX_PLATFORM_WINDOWS
  1620. }
  1621. void ConstantBuffer::commit()
  1622. {
  1623. reset();
  1624. do
  1625. {
  1626. uint32_t opcode = read();
  1627. if (UniformType::End == opcode)
  1628. {
  1629. break;
  1630. }
  1631. UniformType::Enum type;
  1632. uint16_t loc;
  1633. uint16_t num;
  1634. uint16_t copy;
  1635. decodeOpcode(opcode, type, loc, num, copy);
  1636. const char* data;
  1637. if (copy)
  1638. {
  1639. data = read(g_uniformTypeSize[type]*num);
  1640. }
  1641. else
  1642. {
  1643. memcpy(&data, read(sizeof(void*) ), sizeof(void*) );
  1644. }
  1645. #define CASE_IMPLEMENT_UNIFORM(_uniform, _glsuffix, _dxsuffix, _type) \
  1646. case UniformType::_uniform: \
  1647. { \
  1648. _type* value = (_type*)data; \
  1649. s_renderCtx.m_device->SetVertexShaderConstant##_dxsuffix(loc, value, num); \
  1650. } \
  1651. break; \
  1652. \
  1653. case UniformType::_uniform|BGFX_UNIFORM_FRAGMENTBIT: \
  1654. { \
  1655. _type* value = (_type*)data; \
  1656. s_renderCtx.m_device->SetPixelShaderConstant##_dxsuffix(loc, value, num); \
  1657. } \
  1658. break
  1659. switch ((int32_t)type)
  1660. {
  1661. CASE_IMPLEMENT_UNIFORM(Uniform1i, 1iv, I, int);
  1662. CASE_IMPLEMENT_UNIFORM(Uniform1f, 1fv, F, float);
  1663. CASE_IMPLEMENT_UNIFORM(Uniform1iv, 1iv, I, int);
  1664. CASE_IMPLEMENT_UNIFORM(Uniform1fv, 1fv, F, float);
  1665. CASE_IMPLEMENT_UNIFORM(Uniform2fv, 2fv, F, float);
  1666. CASE_IMPLEMENT_UNIFORM(Uniform3fv, 3fv, F, float);
  1667. CASE_IMPLEMENT_UNIFORM(Uniform4fv, 4fv, F, float);
  1668. CASE_IMPLEMENT_UNIFORM(Uniform3x3fv, Matrix3fv, F, float);
  1669. CASE_IMPLEMENT_UNIFORM(Uniform4x4fv, Matrix4fv, F, float);
  1670. case UniformType::End:
  1671. break;
  1672. default:
  1673. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", m_pos, opcode, type, loc, num, copy);
  1674. break;
  1675. }
  1676. #undef CASE_IMPLEMENT_UNIFORM
  1677. } while (true);
  1678. }
  1679. void TextVideoMemBlitter::setup()
  1680. {
  1681. uint32_t width = s_renderCtx.m_params.BackBufferWidth;
  1682. uint32_t height = s_renderCtx.m_params.BackBufferHeight;
  1683. RenderTargetHandle rt = BGFX_INVALID_HANDLE;
  1684. s_renderCtx.setRenderTarget(rt, false);
  1685. D3DVIEWPORT9 vp;
  1686. vp.X = 0;
  1687. vp.Y = 0;
  1688. vp.Width = width;
  1689. vp.Height = height;
  1690. vp.MinZ = 0.0f;
  1691. vp.MaxZ = 1.0f;
  1692. IDirect3DDevice9* device = s_renderCtx.m_device;
  1693. DX_CHECK(device->SetViewport(&vp) );
  1694. DX_CHECK(device->SetRenderState(D3DRS_STENCILENABLE, FALSE) );
  1695. DX_CHECK(device->SetRenderState(D3DRS_ZENABLE, FALSE) );
  1696. DX_CHECK(device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS) );
  1697. DX_CHECK(device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) );
  1698. DX_CHECK(device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE) );
  1699. DX_CHECK(device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER) );
  1700. DX_CHECK(device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE) );
  1701. DX_CHECK(device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID) );
  1702. Program& program = s_renderCtx.m_program[m_program.idx];
  1703. device->SetVertexShader( (IDirect3DVertexShader9*)program.m_vsh->m_ptr);
  1704. device->SetPixelShader( (IDirect3DPixelShader9*)program.m_fsh->m_ptr);
  1705. VertexBuffer& vb = s_renderCtx.m_vertexBuffers[m_vb->handle.idx];
  1706. VertexDeclaration& vertexDecl = s_renderCtx.m_vertexDecls[m_vb->decl.idx];
  1707. DX_CHECK(device->SetStreamSource(0, vb.m_ptr, 0, vertexDecl.m_decl.m_stride) );
  1708. DX_CHECK(device->SetVertexDeclaration(vertexDecl.m_ptr) );
  1709. IndexBuffer& ib = s_renderCtx.m_indexBuffers[m_ib->handle.idx];
  1710. DX_CHECK(device->SetIndices(ib.m_ptr) );
  1711. float proj[16];
  1712. mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  1713. PredefinedUniform& predefined = program.m_predefined[0];
  1714. uint8_t flags = predefined.m_type;
  1715. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, proj, 4);
  1716. s_renderCtx.m_textures[m_texture.idx].commit(0);
  1717. }
  1718. void TextVideoMemBlitter::render(uint32_t _numIndices)
  1719. {
  1720. uint32_t numVertices = _numIndices*4/6;
  1721. s_renderCtx.m_indexBuffers[m_ib->handle.idx].update(0, _numIndices*2, m_ib->data, true);
  1722. s_renderCtx.m_vertexBuffers[m_vb->handle.idx].update(0, numVertices*m_decl.m_stride, m_vb->data, true);
  1723. DX_CHECK(s_renderCtx.m_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST
  1724. , 0
  1725. , 0
  1726. , numVertices
  1727. , 0
  1728. , _numIndices/3
  1729. ) );
  1730. }
  1731. void Context::flip()
  1732. {
  1733. s_renderCtx.flip();
  1734. }
  1735. void Context::rendererInit()
  1736. {
  1737. s_renderCtx.init();
  1738. }
  1739. void Context::rendererShutdown()
  1740. {
  1741. s_renderCtx.shutdown();
  1742. }
  1743. void Context::rendererCreateIndexBuffer(IndexBufferHandle _handle, Memory* _mem)
  1744. {
  1745. s_renderCtx.m_indexBuffers[_handle.idx].create(_mem->size, _mem->data);
  1746. }
  1747. void Context::rendererDestroyIndexBuffer(IndexBufferHandle _handle)
  1748. {
  1749. s_renderCtx.m_indexBuffers[_handle.idx].destroy();
  1750. }
  1751. void Context::rendererCreateVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl)
  1752. {
  1753. s_renderCtx.m_vertexDecls[_handle.idx].create(_decl);
  1754. }
  1755. void Context::rendererDestroyVertexDecl(VertexDeclHandle _handle)
  1756. {
  1757. s_renderCtx.m_vertexDecls[_handle.idx].destroy();
  1758. }
  1759. void Context::rendererCreateVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle)
  1760. {
  1761. s_renderCtx.m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle);
  1762. }
  1763. void Context::rendererDestroyVertexBuffer(VertexBufferHandle _handle)
  1764. {
  1765. s_renderCtx.m_vertexBuffers[_handle.idx].destroy();
  1766. }
  1767. void Context::rendererCreateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size)
  1768. {
  1769. s_renderCtx.m_indexBuffers[_handle.idx].create(_size, NULL);
  1770. }
  1771. void Context::rendererUpdateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem)
  1772. {
  1773. s_renderCtx.m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  1774. }
  1775. void Context::rendererDestroyDynamicIndexBuffer(IndexBufferHandle _handle)
  1776. {
  1777. s_renderCtx.m_indexBuffers[_handle.idx].destroy();
  1778. }
  1779. void Context::rendererCreateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size)
  1780. {
  1781. VertexDeclHandle decl = BGFX_INVALID_HANDLE;
  1782. s_renderCtx.m_vertexBuffers[_handle.idx].create(_size, NULL, decl);
  1783. }
  1784. void Context::rendererUpdateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem)
  1785. {
  1786. s_renderCtx.m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  1787. }
  1788. void Context::rendererDestroyDynamicVertexBuffer(VertexBufferHandle _handle)
  1789. {
  1790. s_renderCtx.m_vertexBuffers[_handle.idx].destroy();
  1791. }
  1792. void Context::rendererCreateVertexShader(VertexShaderHandle _handle, Memory* _mem)
  1793. {
  1794. s_renderCtx.m_vertexShaders[_handle.idx].create(false, _mem);
  1795. }
  1796. void Context::rendererDestroyVertexShader(VertexShaderHandle _handle)
  1797. {
  1798. s_renderCtx.m_vertexShaders[_handle.idx].destroy();
  1799. }
  1800. void Context::rendererCreateFragmentShader(FragmentShaderHandle _handle, Memory* _mem)
  1801. {
  1802. s_renderCtx.m_fragmentShaders[_handle.idx].create(true, _mem);
  1803. }
  1804. void Context::rendererDestroyFragmentShader(FragmentShaderHandle _handle)
  1805. {
  1806. s_renderCtx.m_fragmentShaders[_handle.idx].destroy();
  1807. }
  1808. void Context::rendererCreateProgram(ProgramHandle _handle, VertexShaderHandle _vsh, FragmentShaderHandle _fsh)
  1809. {
  1810. s_renderCtx.m_program[_handle.idx].create(s_renderCtx.m_vertexShaders[_vsh.idx], s_renderCtx.m_fragmentShaders[_fsh.idx]);
  1811. }
  1812. void Context::rendererDestroyProgram(FragmentShaderHandle _handle)
  1813. {
  1814. s_renderCtx.m_program[_handle.idx].destroy();
  1815. }
  1816. void Context::rendererCreateTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags)
  1817. {
  1818. s_renderCtx.m_textures[_handle.idx].create(_mem, _flags);
  1819. }
  1820. void Context::rendererUpdateTextureBegin(TextureHandle _handle, uint8_t _side, uint8_t _mip)
  1821. {
  1822. s_renderCtx.m_updateTexture = &s_renderCtx.m_textures[_handle.idx];
  1823. s_renderCtx.m_updateTexture->updateBegin(_side, _mip);
  1824. }
  1825. void Context::rendererUpdateTexture(TextureHandle /*_handle*/, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
  1826. {
  1827. s_renderCtx.m_updateTexture->update(_side, _mip, _rect, _z, _depth, _mem);
  1828. }
  1829. void Context::rendererUpdateTextureEnd()
  1830. {
  1831. s_renderCtx.m_updateTexture->updateEnd();
  1832. s_renderCtx.m_updateTexture = NULL;
  1833. }
  1834. void Context::rendererDestroyTexture(TextureHandle _handle)
  1835. {
  1836. s_renderCtx.m_textures[_handle.idx].destroy();
  1837. }
  1838. void Context::rendererCreateRenderTarget(RenderTargetHandle _handle, uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
  1839. {
  1840. s_renderCtx.m_renderTargets[_handle.idx].create(_width, _height, _flags, _textureFlags);
  1841. }
  1842. void Context::rendererDestroyRenderTarget(RenderTargetHandle _handle)
  1843. {
  1844. s_renderCtx.m_renderTargets[_handle.idx].destroy();
  1845. }
  1846. void Context::rendererCreateUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name)
  1847. {
  1848. uint32_t size = BX_ALIGN_16(g_uniformTypeSize[_type]*_num);
  1849. void* data = g_realloc(NULL, size);
  1850. memset(data, 0, size);
  1851. s_renderCtx.m_uniforms[_handle.idx] = data;
  1852. s_renderCtx.m_uniformReg.add(_name, s_renderCtx.m_uniforms[_handle.idx]);
  1853. }
  1854. void Context::rendererDestroyUniform(UniformHandle _handle)
  1855. {
  1856. g_free(s_renderCtx.m_uniforms[_handle.idx]);
  1857. }
  1858. void Context::rendererSaveScreenShot(const char* _filePath)
  1859. {
  1860. s_renderCtx.saveScreenShot(_filePath);
  1861. }
  1862. void Context::rendererUpdateViewName(uint8_t _id, const char* _name)
  1863. {
  1864. mbstowcs(&s_viewNameW[_id][0], _name, countof(s_viewNameW[0]) );
  1865. }
  1866. void Context::rendererUpdateUniform(uint16_t _loc, const void* _data, uint32_t _size)
  1867. {
  1868. memcpy(s_renderCtx.m_uniforms[_loc], _data, _size);
  1869. }
  1870. void Context::rendererSetMarker(const char* _marker, uint32_t _size)
  1871. {
  1872. #if BGFX_CONFIG_DEBUG_PIX
  1873. uint32_t size = _size*sizeof(wchar_t);
  1874. wchar_t* name = (wchar_t*)alloca(size);
  1875. mbstowcs(name, _marker, size-2);
  1876. PIX_SETMARKER(D3DCOLOR_RGBA(0xff, 0xff, 0xff, 0xff), name);
  1877. #endif // BGFX_CONFIG_DEBUG_PIX
  1878. }
  1879. void Context::rendererSubmit()
  1880. {
  1881. IDirect3DDevice9* device = s_renderCtx.m_device;
  1882. PIX_BEGINEVENT(D3DCOLOR_RGBA(0xff, 0x00, 0x00, 0xff), L"rendererSubmit");
  1883. s_renderCtx.updateResolution(m_render->m_resolution);
  1884. int64_t elapsed = -bx::getHPCounter();
  1885. int64_t captureElapsed = 0;
  1886. device->BeginScene();
  1887. if (0 < m_render->m_iboffset)
  1888. {
  1889. TransientIndexBuffer* ib = m_render->m_transientIb;
  1890. s_renderCtx.m_indexBuffers[ib->handle.idx].update(0, m_render->m_iboffset, ib->data, true);
  1891. }
  1892. if (0 < m_render->m_vboffset)
  1893. {
  1894. TransientVertexBuffer* vb = m_render->m_transientVb;
  1895. s_renderCtx.m_vertexBuffers[vb->handle.idx].update(0, m_render->m_vboffset, vb->data, true);
  1896. }
  1897. m_render->sort();
  1898. RenderState currentState;
  1899. currentState.reset();
  1900. currentState.m_flags = BGFX_STATE_NONE;
  1901. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  1902. Matrix4 viewProj[BGFX_CONFIG_MAX_VIEWS];
  1903. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  1904. {
  1905. mtxMul(viewProj[ii].val, m_render->m_view[ii].val, m_render->m_proj[ii].val);
  1906. }
  1907. DX_CHECK(device->SetRenderState(D3DRS_FILLMODE, m_render->m_debug&BGFX_DEBUG_WIREFRAME ? D3DFILL_WIREFRAME : D3DFILL_SOLID) );
  1908. uint16_t programIdx = invalidHandle;
  1909. SortKey key;
  1910. uint8_t view = 0xff;
  1911. RenderTargetHandle rt = BGFX_INVALID_HANDLE;
  1912. float alphaRef = 0.0f;
  1913. uint32_t blendFactor = 0;
  1914. D3DPRIMITIVETYPE primType = D3DPT_TRIANGLELIST;
  1915. uint32_t primNumVerts = 3;
  1916. bool viewHasScissor = false;
  1917. Rect viewScissorRect;
  1918. uint32_t statsNumPrimsSubmitted = 0;
  1919. uint32_t statsNumIndices = 0;
  1920. uint32_t statsNumInstances = 0;
  1921. uint32_t statsNumPrimsRendered = 0;
  1922. s_renderCtx.invalidateSamplerState();
  1923. if (0 == (m_render->m_debug&BGFX_DEBUG_IFH) )
  1924. {
  1925. for (uint32_t item = 0, numItems = m_render->m_num; item < numItems; ++item)
  1926. {
  1927. key.decode(m_render->m_sortKeys[item]);
  1928. const RenderState& state = m_render->m_renderState[m_render->m_sortValues[item] ];
  1929. const uint64_t newFlags = state.m_flags;
  1930. uint64_t changedFlags = currentState.m_flags ^ state.m_flags;
  1931. currentState.m_flags = newFlags;
  1932. const uint64_t newStencil = state.m_stencil;
  1933. uint64_t changedStencil = currentState.m_stencil ^ state.m_stencil;
  1934. currentState.m_stencil = newStencil;
  1935. if (key.m_view != view)
  1936. {
  1937. currentState.clear();
  1938. currentState.m_scissor = !state.m_scissor;
  1939. changedFlags = BGFX_STATE_MASK;
  1940. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  1941. currentState.m_flags = newFlags;
  1942. currentState.m_stencil = newStencil;
  1943. PIX_ENDEVENT();
  1944. PIX_BEGINEVENT(D3DCOLOR_RGBA(0xff, 0x00, 0x00, 0xff), s_viewNameW[key.m_view]);
  1945. view = key.m_view;
  1946. programIdx = invalidHandle;
  1947. if (m_render->m_rt[view].idx != rt.idx)
  1948. {
  1949. rt = m_render->m_rt[view];
  1950. s_renderCtx.setRenderTarget(rt);
  1951. }
  1952. const Rect& rect = m_render->m_rect[view];
  1953. const Rect& scissorRect = m_render->m_scissor[view];
  1954. viewHasScissor = !scissorRect.isZero();
  1955. viewScissorRect = viewHasScissor ? scissorRect : rect;
  1956. D3DVIEWPORT9 vp;
  1957. vp.X = rect.m_x;
  1958. vp.Y = rect.m_y;
  1959. vp.Width = rect.m_width;
  1960. vp.Height = rect.m_height;
  1961. vp.MinZ = 0.0f;
  1962. vp.MaxZ = 1.0f;
  1963. DX_CHECK(device->SetViewport(&vp) );
  1964. Clear& clear = m_render->m_clear[view];
  1965. if (BGFX_CLEAR_NONE != clear.m_flags)
  1966. {
  1967. D3DCOLOR color = 0;
  1968. DWORD flags = 0;
  1969. if (BGFX_CLEAR_COLOR_BIT & clear.m_flags)
  1970. {
  1971. flags |= D3DCLEAR_TARGET;
  1972. uint32_t rgba = clear.m_rgba;
  1973. color = D3DCOLOR_RGBA(rgba>>24, (rgba>>16)&0xff, (rgba>>8)&0xff, rgba&0xff);
  1974. DX_CHECK(device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_ALPHA) );
  1975. }
  1976. if (BGFX_CLEAR_DEPTH_BIT & clear.m_flags)
  1977. {
  1978. flags |= D3DCLEAR_ZBUFFER;
  1979. DX_CHECK(device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE) );
  1980. }
  1981. if (BGFX_CLEAR_STENCIL_BIT & clear.m_flags)
  1982. {
  1983. flags |= D3DCLEAR_STENCIL;
  1984. }
  1985. if (0 != flags)
  1986. {
  1987. RECT rc;
  1988. rc.left = rect.m_x;
  1989. rc.top = rect.m_y;
  1990. rc.right = rect.m_x + rect.m_width;
  1991. rc.bottom = rect.m_y + rect.m_height;
  1992. DX_CHECK(device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE) );
  1993. DX_CHECK(device->SetScissorRect(&rc) );
  1994. DX_CHECK(device->Clear(0, NULL, flags, color, clear.m_depth, clear.m_stencil) );
  1995. DX_CHECK(device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE) );
  1996. }
  1997. }
  1998. DX_CHECK(device->SetRenderState(D3DRS_STENCILENABLE, FALSE) );
  1999. DX_CHECK(device->SetRenderState(D3DRS_ZENABLE, TRUE) );
  2000. DX_CHECK(device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS) );
  2001. DX_CHECK(device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) );
  2002. DX_CHECK(device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE) );
  2003. DX_CHECK(device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER) );
  2004. }
  2005. uint16_t scissor = state.m_scissor;
  2006. if (currentState.m_scissor != scissor)
  2007. {
  2008. currentState.m_scissor = scissor;
  2009. if (UINT16_MAX == scissor)
  2010. {
  2011. DX_CHECK(device->SetRenderState(D3DRS_SCISSORTESTENABLE, viewHasScissor) );
  2012. if (viewHasScissor)
  2013. {
  2014. RECT rc;
  2015. rc.left = viewScissorRect.m_x;
  2016. rc.top = viewScissorRect.m_y;
  2017. rc.right = viewScissorRect.m_x + viewScissorRect.m_width;
  2018. rc.bottom = viewScissorRect.m_y + viewScissorRect.m_height;
  2019. DX_CHECK(device->SetScissorRect(&rc) );
  2020. }
  2021. }
  2022. else
  2023. {
  2024. Rect scissorRect;
  2025. scissorRect.intersect(viewScissorRect, m_render->m_rectCache.m_cache[scissor]);
  2026. DX_CHECK(device->SetRenderState(D3DRS_SCISSORTESTENABLE, true) );
  2027. RECT rc;
  2028. rc.left = scissorRect.m_x;
  2029. rc.top = scissorRect.m_y;
  2030. rc.right = scissorRect.m_x + scissorRect.m_width;
  2031. rc.bottom = scissorRect.m_y + scissorRect.m_height;
  2032. DX_CHECK(device->SetScissorRect(&rc) );
  2033. }
  2034. }
  2035. if (0 != changedStencil)
  2036. {
  2037. bool enable = 0 != newStencil;
  2038. DX_CHECK(device->SetRenderState(D3DRS_STENCILENABLE, enable) );
  2039. if (0 != newStencil)
  2040. {
  2041. uint32_t fstencil = unpackStencil(0, newStencil);
  2042. uint32_t bstencil = unpackStencil(1, newStencil);
  2043. uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != fstencil;
  2044. DX_CHECK(device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, 0 != frontAndBack) );
  2045. uint32_t fchanged = unpackStencil(0, changedStencil);
  2046. if ( (BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & fchanged)
  2047. {
  2048. uint32_t ref = (fstencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  2049. DX_CHECK(device->SetRenderState(D3DRS_STENCILREF, ref) );
  2050. uint32_t rmask = (fstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  2051. DX_CHECK(device->SetRenderState(D3DRS_STENCILMASK, rmask) );
  2052. }
  2053. // uint32_t bchanged = unpackStencil(1, changedStencil);
  2054. // if (BGFX_STENCIL_FUNC_RMASK_MASK & bchanged)
  2055. // {
  2056. // uint32_t wmask = (bstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  2057. // DX_CHECK(device->SetRenderState(D3DRS_STENCILWRITEMASK, wmask) );
  2058. // }
  2059. for (uint32_t ii = 0, num = frontAndBack+1; ii < num; ++ii)
  2060. {
  2061. uint32_t stencil = unpackStencil(ii, newStencil);
  2062. uint32_t changed = unpackStencil(ii, changedStencil);
  2063. if ( (BGFX_STENCIL_TEST_MASK|BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & changed)
  2064. {
  2065. uint32_t func = (stencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT;
  2066. DX_CHECK(device->SetRenderState(s_stencilFuncRs[ii], s_stencilFunc[func]) );
  2067. }
  2068. if ( (BGFX_STENCIL_OP_FAIL_S_MASK|BGFX_STENCIL_OP_FAIL_Z_MASK|BGFX_STENCIL_OP_PASS_Z_MASK) & changed)
  2069. {
  2070. uint32_t sfail = (stencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT;
  2071. DX_CHECK(device->SetRenderState(s_stencilFailRs[ii], s_stencilOp[sfail]) );
  2072. uint32_t zfail = (stencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT;
  2073. DX_CHECK(device->SetRenderState(s_stencilZFailRs[ii], s_stencilOp[zfail]) );
  2074. uint32_t zpass = (stencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT;
  2075. DX_CHECK(device->SetRenderState(s_stencilZPassRs[ii], s_stencilOp[zpass]) );
  2076. }
  2077. }
  2078. }
  2079. }
  2080. if ( (0
  2081. | BGFX_STATE_CULL_MASK
  2082. | BGFX_STATE_DEPTH_WRITE
  2083. | BGFX_STATE_DEPTH_TEST_MASK
  2084. | BGFX_STATE_ALPHA_MASK
  2085. | BGFX_STATE_RGB_WRITE
  2086. | BGFX_STATE_BLEND_MASK
  2087. | BGFX_STATE_BLEND_EQUATION_MASK
  2088. | BGFX_STATE_ALPHA_REF_MASK
  2089. | BGFX_STATE_PT_MASK
  2090. | BGFX_STATE_POINT_SIZE_MASK
  2091. | BGFX_STATE_MSAA
  2092. ) & changedFlags)
  2093. {
  2094. if (BGFX_STATE_CULL_MASK & changedFlags)
  2095. {
  2096. uint32_t cull = (newFlags&BGFX_STATE_CULL_MASK)>>BGFX_STATE_CULL_SHIFT;
  2097. DX_CHECK(device->SetRenderState(D3DRS_CULLMODE, s_cullMode[cull]) );
  2098. }
  2099. if (BGFX_STATE_DEPTH_WRITE & changedFlags)
  2100. {
  2101. DX_CHECK(device->SetRenderState(D3DRS_ZWRITEENABLE, !!(BGFX_STATE_DEPTH_WRITE & newFlags) ) );
  2102. }
  2103. if (BGFX_STATE_DEPTH_TEST_MASK & changedFlags)
  2104. {
  2105. uint32_t func = (newFlags&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  2106. DX_CHECK(device->SetRenderState(D3DRS_ZENABLE, 0 != func) );
  2107. if (0 != func)
  2108. {
  2109. DX_CHECK(device->SetRenderState(D3DRS_ZFUNC, s_depthFunc[func]) );
  2110. }
  2111. }
  2112. if (BGFX_STATE_ALPHA_REF_MASK & changedFlags)
  2113. {
  2114. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  2115. alphaRef = ref/255.0f;
  2116. }
  2117. if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  2118. {
  2119. DX_CHECK(device->SetRenderState(D3DRS_POINTSIZE, castfu( (float)( (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) ) ) );
  2120. }
  2121. if (BGFX_STATE_MSAA & changedFlags)
  2122. {
  2123. DX_CHECK(device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, (newFlags&BGFX_STATE_MSAA) == BGFX_STATE_MSAA) );
  2124. }
  2125. if ( (BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE) & changedFlags)
  2126. {
  2127. uint32_t writeEnable = (newFlags&BGFX_STATE_ALPHA_WRITE) ? D3DCOLORWRITEENABLE_ALPHA : 0;
  2128. writeEnable |= (newFlags&BGFX_STATE_RGB_WRITE) ? D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE : 0;
  2129. DX_CHECK(device->SetRenderState(D3DRS_COLORWRITEENABLE, writeEnable) );
  2130. }
  2131. if ( (BGFX_STATE_BLEND_MASK|BGFX_STATE_BLEND_EQUATION_MASK) & changedFlags)
  2132. {
  2133. bool alphaBlendEnabled = !!(BGFX_STATE_BLEND_MASK & newFlags);
  2134. DX_CHECK(device->SetRenderState(D3DRS_ALPHABLENDENABLE, alphaBlendEnabled) );
  2135. // DX_CHECK(device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, alphaBlendEnabled) );
  2136. if (alphaBlendEnabled)
  2137. {
  2138. uint32_t blend = (newFlags&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT;
  2139. uint32_t equation = (newFlags&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT;
  2140. uint32_t src = blend&0xf;
  2141. uint32_t dst = (blend>>4)&0xf;
  2142. DX_CHECK(device->SetRenderState(D3DRS_SRCBLEND, s_blendFactor[src].m_src) );
  2143. DX_CHECK(device->SetRenderState(D3DRS_DESTBLEND, s_blendFactor[dst].m_dst) );
  2144. DX_CHECK(device->SetRenderState(D3DRS_BLENDOP, s_blendEquation[equation]) );
  2145. // DX_CHECK(device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_SRCALPHA) );
  2146. // DX_CHECK(device->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA) );
  2147. if ( (s_blendFactor[src].m_factor || s_blendFactor[dst].m_factor)
  2148. && blendFactor != state.m_rgba)
  2149. {
  2150. blendFactor = state.m_rgba;
  2151. D3DCOLOR color = D3DCOLOR_RGBA(blendFactor>>24, (blendFactor>>16)&0xff, (blendFactor>>8)&0xff, blendFactor&0xff);
  2152. DX_CHECK(device->SetRenderState(D3DRS_BLENDFACTOR, color) );
  2153. }
  2154. }
  2155. }
  2156. uint8_t primIndex = uint8_t( (newFlags&BGFX_STATE_PT_MASK)>>BGFX_STATE_PT_SHIFT);
  2157. primType = s_primType[primIndex];
  2158. primNumVerts = 3-primIndex;
  2159. }
  2160. bool programChanged = false;
  2161. bool constantsChanged = state.m_constBegin < state.m_constEnd;
  2162. rendererUpdateUniforms(m_render->m_constantBuffer, state.m_constBegin, state.m_constEnd);
  2163. if (key.m_program != programIdx)
  2164. {
  2165. programIdx = key.m_program;
  2166. if (invalidHandle == programIdx)
  2167. {
  2168. device->SetVertexShader(NULL);
  2169. device->SetPixelShader(NULL);
  2170. }
  2171. else
  2172. {
  2173. Program& program = s_renderCtx.m_program[programIdx];
  2174. device->SetVertexShader( (IDirect3DVertexShader9*)program.m_vsh->m_ptr);
  2175. device->SetPixelShader( (IDirect3DPixelShader9*)program.m_fsh->m_ptr);
  2176. }
  2177. programChanged =
  2178. constantsChanged = true;
  2179. }
  2180. if (invalidHandle != programIdx)
  2181. {
  2182. Program& program = s_renderCtx.m_program[programIdx];
  2183. if (constantsChanged)
  2184. {
  2185. program.commit();
  2186. }
  2187. for (uint32_t ii = 0, num = program.m_numPredefined; ii < num; ++ii)
  2188. {
  2189. PredefinedUniform& predefined = program.m_predefined[ii];
  2190. uint8_t flags = predefined.m_type&BGFX_UNIFORM_FRAGMENTBIT;
  2191. switch (predefined.m_type&(~BGFX_UNIFORM_FRAGMENTBIT) )
  2192. {
  2193. case PredefinedUniform::ViewRect:
  2194. {
  2195. float rect[4];
  2196. rect[0] = m_render->m_rect[view].m_x;
  2197. rect[1] = m_render->m_rect[view].m_y;
  2198. rect[2] = m_render->m_rect[view].m_width;
  2199. rect[3] = m_render->m_rect[view].m_height;
  2200. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, &rect[0], 1);
  2201. }
  2202. break;
  2203. case PredefinedUniform::ViewTexel:
  2204. {
  2205. float rect[4];
  2206. rect[0] = 1.0f/float(m_render->m_rect[view].m_width);
  2207. rect[1] = 1.0f/float(m_render->m_rect[view].m_height);
  2208. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, &rect[0], 1);
  2209. }
  2210. break;
  2211. case PredefinedUniform::View:
  2212. {
  2213. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, m_render->m_view[view].val, bx::uint32_min(4, predefined.m_count) );
  2214. }
  2215. break;
  2216. case PredefinedUniform::ViewProj:
  2217. {
  2218. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, viewProj[view].val, bx::uint32_min(4, predefined.m_count) );
  2219. }
  2220. break;
  2221. case PredefinedUniform::Model:
  2222. {
  2223. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2224. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, model.val, bx::uint32_min(state.m_num*4, predefined.m_count) );
  2225. }
  2226. break;
  2227. case PredefinedUniform::ModelView:
  2228. {
  2229. Matrix4 modelView;
  2230. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2231. mtxMul(modelView.val, model.val, m_render->m_view[view].val);
  2232. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, modelView.val, bx::uint32_min(4, predefined.m_count) );
  2233. }
  2234. break;
  2235. case PredefinedUniform::ModelViewProj:
  2236. {
  2237. Matrix4 modelViewProj;
  2238. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2239. mtxMul(modelViewProj.val, model.val, viewProj[view].val);
  2240. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, modelViewProj.val, bx::uint32_min(4, predefined.m_count) );
  2241. }
  2242. break;
  2243. case PredefinedUniform::ModelViewProjX:
  2244. {
  2245. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2246. static const BX_ALIGN_STRUCT_16(float) s_bias[16] =
  2247. {
  2248. 0.5f, 0.0f, 0.0f, 0.0f,
  2249. 0.0f, 0.5f, 0.0f, 0.0f,
  2250. 0.0f, 0.0f, 0.5f, 0.0f,
  2251. 0.5f, 0.5f, 0.5f, 1.0f,
  2252. };
  2253. uint8_t other = m_render->m_other[view];
  2254. Matrix4 viewProjBias;
  2255. mtxMul(viewProjBias.val, viewProj[other].val, s_bias);
  2256. Matrix4 modelViewProj;
  2257. mtxMul(modelViewProj.val, model.val, viewProjBias.val);
  2258. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, modelViewProj.val, bx::uint32_min(4, predefined.m_count) );
  2259. }
  2260. break;
  2261. case PredefinedUniform::ViewProjX:
  2262. {
  2263. static const BX_ALIGN_STRUCT_16(float) s_bias[16] =
  2264. {
  2265. 0.5f, 0.0f, 0.0f, 0.0f,
  2266. 0.0f, 0.5f, 0.0f, 0.0f,
  2267. 0.0f, 0.0f, 0.5f, 0.0f,
  2268. 0.5f, 0.5f, 0.5f, 1.0f,
  2269. };
  2270. uint8_t other = m_render->m_other[view];
  2271. Matrix4 viewProjBias;
  2272. mtxMul(viewProjBias.val, viewProj[other].val, s_bias);
  2273. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, viewProjBias.val, bx::uint32_min(4, predefined.m_count) );
  2274. }
  2275. break;
  2276. case PredefinedUniform::AlphaRef:
  2277. {
  2278. s_renderCtx.setShaderConstantF(flags, predefined.m_loc, &alphaRef, 1);
  2279. }
  2280. break;
  2281. default:
  2282. BX_CHECK(false, "predefined %d not handled", predefined.m_type);
  2283. break;
  2284. }
  2285. }
  2286. }
  2287. // if (BGFX_STATE_TEX_MASK & changedFlags)
  2288. {
  2289. uint64_t flag = BGFX_STATE_TEX0;
  2290. for (uint32_t stage = 0; stage < BGFX_STATE_TEX_COUNT; ++stage)
  2291. {
  2292. const Sampler& sampler = state.m_sampler[stage];
  2293. Sampler& current = currentState.m_sampler[stage];
  2294. if (current.m_idx != sampler.m_idx
  2295. || current.m_flags != sampler.m_flags
  2296. || programChanged)
  2297. {
  2298. if (invalidHandle != sampler.m_idx)
  2299. {
  2300. switch (sampler.m_flags&BGFX_SAMPLER_TYPE_MASK)
  2301. {
  2302. case BGFX_SAMPLER_TEXTURE:
  2303. s_renderCtx.m_textures[sampler.m_idx].commit(stage, sampler.m_flags);
  2304. break;
  2305. case BGFX_SAMPLER_RENDERTARGET_COLOR:
  2306. s_renderCtx.m_renderTargets[sampler.m_idx].commit(stage, sampler.m_flags);
  2307. break;
  2308. case BGFX_SAMPLER_RENDERTARGET_DEPTH:
  2309. // id = s_renderCtx.m_renderTargets[sampler.m_idx].m_depth.m_id;
  2310. break;
  2311. }
  2312. }
  2313. else
  2314. {
  2315. DX_CHECK(device->SetTexture(stage, NULL) );
  2316. }
  2317. }
  2318. current = sampler;
  2319. flag <<= 1;
  2320. }
  2321. }
  2322. if (programChanged
  2323. || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx
  2324. || currentState.m_instanceDataBuffer.idx != state.m_instanceDataBuffer.idx
  2325. || currentState.m_instanceDataOffset != state.m_instanceDataOffset
  2326. || currentState.m_instanceDataStride != state.m_instanceDataStride)
  2327. {
  2328. currentState.m_vertexBuffer = state.m_vertexBuffer;
  2329. currentState.m_instanceDataBuffer.idx = state.m_instanceDataBuffer.idx;
  2330. currentState.m_instanceDataOffset = state.m_instanceDataOffset;
  2331. currentState.m_instanceDataStride = state.m_instanceDataStride;
  2332. uint16_t handle = state.m_vertexBuffer.idx;
  2333. if (invalidHandle != handle)
  2334. {
  2335. const VertexBuffer& vb = s_renderCtx.m_vertexBuffers[handle];
  2336. uint16_t decl = vb.m_decl.idx == invalidHandle ? state.m_vertexDecl.idx : vb.m_decl.idx;
  2337. const VertexDeclaration& vertexDecl = s_renderCtx.m_vertexDecls[decl];
  2338. DX_CHECK(device->SetStreamSource(0, vb.m_ptr, 0, vertexDecl.m_decl.m_stride) );
  2339. if (invalidHandle != state.m_instanceDataBuffer.idx
  2340. && s_renderCtx.m_instancing)
  2341. {
  2342. const VertexBuffer& inst = s_renderCtx.m_vertexBuffers[state.m_instanceDataBuffer.idx];
  2343. DX_CHECK(device->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA|state.m_numInstances) );
  2344. DX_CHECK(device->SetStreamSourceFreq(1, D3DSTREAMSOURCE_INSTANCEDATA|1) );
  2345. DX_CHECK(device->SetStreamSource(1, inst.m_ptr, state.m_instanceDataOffset, state.m_instanceDataStride) );
  2346. IDirect3DVertexDeclaration9* ptr = createVertexDecl(vertexDecl.m_decl, state.m_instanceDataStride/16);
  2347. DX_CHECK(device->SetVertexDeclaration(ptr) );
  2348. DX_RELEASE(ptr, 0);
  2349. }
  2350. else
  2351. {
  2352. DX_CHECK(device->SetStreamSourceFreq(0, 1) );
  2353. DX_CHECK(device->SetStreamSource(1, NULL, 0, 0) );
  2354. DX_CHECK(device->SetVertexDeclaration(vertexDecl.m_ptr) );
  2355. }
  2356. }
  2357. else
  2358. {
  2359. DX_CHECK(device->SetStreamSource(0, NULL, 0, 0) );
  2360. DX_CHECK(device->SetStreamSource(1, NULL, 0, 0) );
  2361. }
  2362. }
  2363. if (currentState.m_indexBuffer.idx != state.m_indexBuffer.idx)
  2364. {
  2365. currentState.m_indexBuffer = state.m_indexBuffer;
  2366. uint16_t handle = state.m_indexBuffer.idx;
  2367. if (invalidHandle != handle)
  2368. {
  2369. const IndexBuffer& ib = s_renderCtx.m_indexBuffers[handle];
  2370. DX_CHECK(device->SetIndices(ib.m_ptr) );
  2371. }
  2372. else
  2373. {
  2374. DX_CHECK(device->SetIndices(NULL) );
  2375. }
  2376. }
  2377. if (invalidHandle != currentState.m_vertexBuffer.idx)
  2378. {
  2379. uint32_t numVertices = state.m_numVertices;
  2380. if (UINT32_C(0xffffffff) == numVertices)
  2381. {
  2382. const VertexBuffer& vb = s_renderCtx.m_vertexBuffers[currentState.m_vertexBuffer.idx];
  2383. uint16_t decl = vb.m_decl.idx == invalidHandle ? state.m_vertexDecl.idx : vb.m_decl.idx;
  2384. const VertexDeclaration& vertexDecl = s_renderCtx.m_vertexDecls[decl];
  2385. numVertices = vb.m_size/vertexDecl.m_decl.m_stride;
  2386. }
  2387. uint32_t numIndices = 0;
  2388. uint32_t numPrimsSubmitted = 0;
  2389. uint32_t numInstances = 0;
  2390. uint32_t numPrimsRendered = 0;
  2391. if (invalidHandle != state.m_indexBuffer.idx)
  2392. {
  2393. if (UINT32_MAX == state.m_numIndices)
  2394. {
  2395. numIndices = s_renderCtx.m_indexBuffers[state.m_indexBuffer.idx].m_size/2;
  2396. numPrimsSubmitted = numIndices/primNumVerts;
  2397. numInstances = state.m_numInstances;
  2398. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2399. DX_CHECK(device->DrawIndexedPrimitive(primType
  2400. , state.m_startVertex
  2401. , 0
  2402. , numVertices
  2403. , 0
  2404. , numPrimsSubmitted
  2405. ) );
  2406. }
  2407. else if (primNumVerts <= state.m_numIndices)
  2408. {
  2409. numIndices = state.m_numIndices;
  2410. numPrimsSubmitted = numIndices/primNumVerts;
  2411. numInstances = state.m_numInstances;
  2412. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2413. DX_CHECK(device->DrawIndexedPrimitive(primType
  2414. , state.m_startVertex
  2415. , 0
  2416. , numVertices
  2417. , state.m_startIndex
  2418. , numPrimsSubmitted
  2419. ) );
  2420. }
  2421. }
  2422. else
  2423. {
  2424. numPrimsSubmitted = numVertices/primNumVerts;
  2425. numInstances = state.m_numInstances;
  2426. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2427. DX_CHECK(device->DrawPrimitive(primType
  2428. , state.m_startVertex
  2429. , numPrimsSubmitted
  2430. ) );
  2431. }
  2432. statsNumPrimsSubmitted += numPrimsSubmitted;
  2433. statsNumIndices += numIndices;
  2434. statsNumInstances += numInstances;
  2435. statsNumPrimsRendered += numPrimsRendered;
  2436. }
  2437. }
  2438. PIX_ENDEVENT();
  2439. if (0 < m_render->m_num)
  2440. {
  2441. captureElapsed = -bx::getHPCounter();
  2442. s_renderCtx.capture();
  2443. captureElapsed += bx::getHPCounter();
  2444. }
  2445. }
  2446. int64_t now = bx::getHPCounter();
  2447. elapsed += now;
  2448. static int64_t last = now;
  2449. int64_t frameTime = now - last;
  2450. last = now;
  2451. static int64_t min = frameTime;
  2452. static int64_t max = frameTime;
  2453. min = min > frameTime ? frameTime : min;
  2454. max = max < frameTime ? frameTime : max;
  2455. if (m_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  2456. {
  2457. PIX_BEGINEVENT(D3DCOLOR_RGBA(0x40, 0x40, 0x40, 0xff), L"debugstats");
  2458. TextVideoMem& tvm = s_renderCtx.m_textVideoMem;
  2459. static int64_t next = now;
  2460. if (now >= next)
  2461. {
  2462. next = now + bx::getHPFrequency();
  2463. double freq = double(bx::getHPFrequency() );
  2464. double toMs = 1000.0/freq;
  2465. tvm.clear();
  2466. uint16_t pos = 0;
  2467. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x89 : 0x8f, " " BGFX_RENDERER_NAME " ");
  2468. const D3DADAPTER_IDENTIFIER9& identifier = s_renderCtx.m_identifier;
  2469. tvm.printf(0, pos++, 0x0f, " Device: %s (%s)", identifier.Description, identifier.Driver);
  2470. pos = 10;
  2471. tvm.printf(10, pos++, 0x8e, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS%s"
  2472. , double(frameTime)*toMs
  2473. , double(min)*toMs
  2474. , double(max)*toMs
  2475. , freq/frameTime
  2476. , !!(m_resolution.m_flags&BGFX_RESET_VSYNC) ? " (vsync)" : ""
  2477. );
  2478. double elapsedCpuMs = double(elapsed)*toMs;
  2479. tvm.printf(10, pos++, 0x8e, " Draw calls: %4d / CPU %3.4f [ms]"
  2480. , m_render->m_num
  2481. , elapsedCpuMs
  2482. );
  2483. tvm.printf(10, pos++, 0x8e, " Prims: %7d (#inst: %5d), submitted: %7d"
  2484. , statsNumPrimsRendered
  2485. , statsNumInstances
  2486. , statsNumPrimsSubmitted
  2487. );
  2488. double captureMs = double(captureElapsed)*toMs;
  2489. tvm.printf(10, pos++, 0x8e, " Capture: %3.4f [ms]", captureMs);
  2490. tvm.printf(10, pos++, 0x8e, " Indices: %7d", statsNumIndices);
  2491. tvm.printf(10, pos++, 0x8e, " DVB size: %7d", m_render->m_vboffset);
  2492. tvm.printf(10, pos++, 0x8e, " DIB size: %7d", m_render->m_iboffset);
  2493. uint8_t attr[2] = { 0x89, 0x8a };
  2494. uint8_t attrIndex = m_render->m_waitSubmit < m_render->m_waitRender;
  2495. tvm.printf(10, pos++, attr[attrIndex&1], "Submit wait: %3.4f [ms]", m_render->m_waitSubmit*toMs);
  2496. tvm.printf(10, pos++, attr[(attrIndex+1)&1], "Render wait: %3.4f [ms]", m_render->m_waitRender*toMs);
  2497. min = frameTime;
  2498. max = frameTime;
  2499. }
  2500. m_textVideoMemBlitter.blit(tvm);
  2501. PIX_ENDEVENT();
  2502. }
  2503. else if (m_render->m_debug & BGFX_DEBUG_TEXT)
  2504. {
  2505. PIX_BEGINEVENT(D3DCOLOR_RGBA(0x40, 0x40, 0x40, 0xff), L"debugtext");
  2506. m_textVideoMemBlitter.blit(m_render->m_textVideoMem);
  2507. PIX_ENDEVENT();
  2508. }
  2509. device->EndScene();
  2510. }
  2511. }
  2512. #endif // BGFX_CONFIG_RENDERER_DIRECT3D9