renderer_gl.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  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_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # include <bx/timer.h>
  9. # include <bx/uint32_t.h>
  10. namespace bgfx
  11. {
  12. struct Extension
  13. {
  14. enum Enum
  15. {
  16. EXT_texture_filter_anisotropic,
  17. EXT_texture_format_BGRA8888,
  18. EXT_texture_compression_s3tc,
  19. EXT_texture_compression_dxt1,
  20. CHROMIUM_texture_compression_dxt3,
  21. CHROMIUM_texture_compression_dxt5,
  22. ARB_texture_float,
  23. OES_texture_float,
  24. OES_texture_float_linear,
  25. OES_texture_half_float,
  26. OES_texture_half_float_linear,
  27. EXT_texture_type_2_10_10_10_REV,
  28. EXT_texture_sRGB,
  29. ARB_texture_swizzle,
  30. EXT_texture_swizzle,
  31. OES_standard_derivatives,
  32. ARB_get_program_binary,
  33. OES_get_program_binary,
  34. EXT_framebuffer_blit,
  35. ARB_timer_query,
  36. EXT_timer_query,
  37. ARB_framebuffer_sRGB,
  38. EXT_framebuffer_sRGB,
  39. ARB_multisample,
  40. CHROMIUM_framebuffer_multisample,
  41. ANGLE_translated_shader_source,
  42. ARB_instanced_arrays,
  43. ANGLE_instanced_arrays,
  44. ARB_half_float_vertex,
  45. OES_vertex_half_float,
  46. ARB_vertex_type_2_10_10_10_rev,
  47. OES_vertex_type_10_10_10_2,
  48. EXT_occlusion_query_boolean,
  49. ARB_vertex_array_object,
  50. OES_vertex_array_object,
  51. ATI_meminfo,
  52. NVX_gpu_memory_info,
  53. Count
  54. };
  55. const char* m_name;
  56. bool m_supported;
  57. bool m_initialize;
  58. };
  59. static Extension s_extension[Extension::Count] =
  60. {
  61. { "GL_EXT_texture_filter_anisotropic", false, true },
  62. { "GL_EXT_texture_format_BGRA8888", false, true },
  63. { "GL_EXT_texture_compression_s3tc", false, true },
  64. { "GL_EXT_texture_compression_dxt1", false, true },
  65. { "GL_CHROMIUM_texture_compression_dxt3", false, true },
  66. { "GL_CHROMIUM_texture_compression_dxt5", false, true },
  67. { "GL_ARB_texture_float", false, true },
  68. { "GL_OES_texture_float", false, true },
  69. { "GL_OES_texture_float_linear", false, true },
  70. { "GL_OES_texture_half_float", false, true },
  71. { "GL_OES_texture_half_float_linear", false, true },
  72. { "GL_EXT_texture_type_2_10_10_10_REV", false, true },
  73. { "GL_EXT_texture_sRGB", false, true },
  74. { "GL_ARB_texture_swizzle", false, true },
  75. { "GL_EXT_texture_swizzle", false, true },
  76. { "GL_OES_standard_derivatives", false, true },
  77. { "GL_ARB_get_program_binary", false, true },
  78. { "GL_OES_get_program_binary", false, false },
  79. { "GL_EXT_framebuffer_blit", false, true },
  80. { "GL_ARB_timer_query", false, true },
  81. { "GL_EXT_timer_query", false, true },
  82. { "GL_ARB_framebuffer_sRGB", false, true },
  83. { "GL_EXT_framebuffer_sRGB", false, true },
  84. { "GL_ARB_multisample", false, true },
  85. { "GL_CHROMIUM_framebuffer_multisample", false, true },
  86. { "GL_ANGLE_translated_shader_source", false, true },
  87. { "GL_ARB_instanced_arrays", false, true },
  88. { "GL_ANGLE_instanced_arrays", false, true },
  89. { "GL_ARB_half_float_vertex", false, true },
  90. { "GL_OES_vertex_half_float", false, true },
  91. { "GL_ARB_vertex_type_2_10_10_10_rev", false, true },
  92. { "GL_OES_vertex_type_10_10_10_2", false, true },
  93. { "GL_EXT_occlusion_query_boolean", false, true },
  94. { "GL_ARB_vertex_array_object", false, true },
  95. { "OES_vertex_array_object", false, true },
  96. { "GL_ATI_meminfo", false, true },
  97. { "GL_NVX_gpu_memory_info", false, true },
  98. };
  99. static void GL_APIENTRY stubVertexAttribDivisor(GLuint /*_index*/, GLuint /*_divisor*/)
  100. {
  101. }
  102. static void GL_APIENTRY stubDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei /*_primcount*/)
  103. {
  104. glDrawArrays(_mode, _first, _count);
  105. }
  106. static void GL_APIENTRY stubDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei /*_primcount*/)
  107. {
  108. glDrawElements(_mode, _count, _type, _indices);
  109. }
  110. #if BGFX_CONFIG_RENDERER_OPENGLES3
  111. # define s_vertexAttribDivisor glVertexAttribDivisor
  112. # define s_drawArraysInstanced glDrawArraysInstanced
  113. # define s_drawElementsInstanced glDrawElementsInstanced
  114. #else
  115. static PFNGLVERTEXATTRIBDIVISORBGFXPROC s_vertexAttribDivisor = stubVertexAttribDivisor;
  116. static PFNGLDRAWARRAYSINSTANCEDBGFXPROC s_drawArraysInstanced = stubDrawArraysInstanced;
  117. static PFNGLDRAWELEMENTSINSTANCEDBGFXPROC s_drawElementsInstanced = stubDrawElementsInstanced;
  118. #endif // BGFX_CONFIG_RENDERER_OPENGLES3
  119. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  120. static void rgbaToBgra(uint8_t* _data, uint32_t _width, uint32_t _height)
  121. {
  122. uint32_t dstpitch = _width*4;
  123. for (uint32_t yy = 0; yy < _height; ++yy)
  124. {
  125. uint8_t* dst = &_data[yy*dstpitch];
  126. for (uint32_t xx = 0; xx < _width; ++xx)
  127. {
  128. uint8_t tmp = dst[0];
  129. dst[0] = dst[2];
  130. dst[2] = tmp;
  131. dst += 4;
  132. }
  133. }
  134. }
  135. struct RendererContext
  136. {
  137. RendererContext()
  138. : m_capture(NULL)
  139. , m_captureSize(0)
  140. , m_maxAnisotropy(0.0f)
  141. , m_dxtSupport(false)
  142. , m_vaoSupport(false)
  143. , m_programBinarySupport(false)
  144. , m_textureSwizzleSupport(false)
  145. , m_flip(false)
  146. , m_postSwapBuffers(NULL)
  147. , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  148. {
  149. memset(&m_resolution, 0, sizeof(m_resolution) );
  150. }
  151. void updateResolution(const Resolution& _resolution)
  152. {
  153. if (m_resolution.m_width != _resolution.m_width
  154. || m_resolution.m_height != _resolution.m_height
  155. || m_resolution.m_flags != _resolution.m_flags)
  156. {
  157. m_textVideoMem.resize(false, _resolution.m_width, _resolution.m_height);
  158. m_textVideoMem.clear();
  159. m_resolution = _resolution;
  160. setRenderContextSize(_resolution.m_width, _resolution.m_height);
  161. updateCapture();
  162. }
  163. }
  164. void setRenderContextSize(uint32_t _width, uint32_t _height)
  165. {
  166. if (_width != 0
  167. || _height != 0)
  168. {
  169. if (!m_glctx.isValid() )
  170. {
  171. m_glctx.create(_width, _height);
  172. }
  173. else
  174. {
  175. m_glctx.resize(_width, _height);
  176. }
  177. }
  178. m_flip = true;
  179. }
  180. void flip()
  181. {
  182. if (m_flip)
  183. {
  184. m_glctx.swap();
  185. }
  186. if (NULL != m_postSwapBuffers)
  187. {
  188. m_postSwapBuffers(m_resolution.m_width, m_resolution.m_height);
  189. }
  190. }
  191. void invalidateCache()
  192. {
  193. m_vaoCache.invalidate();
  194. }
  195. void updateCapture()
  196. {
  197. if (m_resolution.m_flags&BGFX_RESET_CAPTURE)
  198. {
  199. m_captureSize = m_resolution.m_width*m_resolution.m_height*4;
  200. m_capture = g_realloc(m_capture, m_captureSize);
  201. g_callback->captureBegin(m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, TextureFormat::BGRA8, true);
  202. }
  203. else
  204. {
  205. if (NULL != m_capture)
  206. {
  207. g_callback->captureEnd();
  208. g_free(m_capture);
  209. m_capture = NULL;
  210. m_captureSize = 0;
  211. }
  212. }
  213. }
  214. void capture()
  215. {
  216. if (NULL != m_capture)
  217. {
  218. GLint fmt = s_extension[Extension::EXT_texture_format_BGRA8888].m_supported ? GL_BGRA_EXT : GL_RGBA;
  219. GL_CHECK(glReadPixels(0
  220. , 0
  221. , m_resolution.m_width
  222. , m_resolution.m_height
  223. , fmt
  224. , GL_UNSIGNED_BYTE
  225. , m_capture
  226. ) );
  227. g_callback->captureFrame(m_capture, m_captureSize);
  228. }
  229. }
  230. void saveScreenShot(Memory* _mem)
  231. {
  232. uint32_t length = m_resolution.m_width*m_resolution.m_height*4;
  233. uint8_t* data = (uint8_t*)g_realloc(NULL, length);
  234. GLint fmt = s_extension[Extension::EXT_texture_format_BGRA8888].m_supported ? GL_BGRA_EXT : GL_RGBA;
  235. uint32_t width = m_resolution.m_width;
  236. uint32_t height = m_resolution.m_height;
  237. GL_CHECK(glReadPixels(0
  238. , 0
  239. , width
  240. , height
  241. , fmt
  242. , GL_UNSIGNED_BYTE
  243. , data
  244. ) );
  245. if (GL_RGBA == fmt)
  246. {
  247. rgbaToBgra(data, width, height);
  248. }
  249. g_callback->screenShot( (const char*)_mem->data
  250. , width
  251. , height
  252. , width*4
  253. , data
  254. , length
  255. , true
  256. );
  257. g_free(data);
  258. }
  259. void init()
  260. {
  261. m_glctx.create(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
  262. #if BGFX_CONFIG_RENDERER_OPENGL
  263. m_queries.create();
  264. #endif // BGFX_CONFIG_RENDERER_OPENGL
  265. }
  266. void shutdown()
  267. {
  268. invalidateCache();
  269. #if BGFX_CONFIG_RENDERER_OPENGL
  270. m_queries.destroy();
  271. #endif // BGFX_CONFIG_RENDERER_OPENGL
  272. m_glctx.destroy();
  273. }
  274. IndexBuffer m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  275. VertexBuffer m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  276. Shader m_vertexShaders[BGFX_CONFIG_MAX_VERTEX_SHADERS];
  277. Shader m_fragmentShaders[BGFX_CONFIG_MAX_FRAGMENT_SHADERS];
  278. Program m_program[BGFX_CONFIG_MAX_PROGRAMS];
  279. Texture m_textures[BGFX_CONFIG_MAX_TEXTURES];
  280. VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS];
  281. RenderTarget m_renderTargets[BGFX_CONFIG_MAX_RENDER_TARGETS];
  282. UniformRegistry m_uniformReg;
  283. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  284. #if BGFX_CONFIG_RENDERER_OPENGL
  285. Queries m_queries;
  286. #endif // BGFX_CONFIG_RENDERER_OPENGL
  287. VaoCache m_vaoCache;
  288. TextVideoMem m_textVideoMem;
  289. Resolution m_resolution;
  290. void* m_capture;
  291. uint32_t m_captureSize;
  292. float m_maxAnisotropy;
  293. bool m_dxtSupport;
  294. bool m_vaoSupport;
  295. bool m_programBinarySupport;
  296. bool m_textureSwizzleSupport;
  297. bool m_flip;
  298. PostSwapBuffersFn m_postSwapBuffers;
  299. uint64_t m_hash;
  300. GlContext m_glctx;
  301. };
  302. RendererContext s_renderCtx;
  303. #if BX_PLATFORM_NACL
  304. static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
  305. {
  306. s_renderCtx.m_glctx.m_instancedArrays->VertexAttribDivisorANGLE(s_renderCtx.m_glctx.m_context, _index, _divisor);
  307. }
  308. static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
  309. {
  310. s_renderCtx.m_glctx.m_instancedArrays->DrawArraysInstancedANGLE(s_renderCtx.m_glctx.m_context, _mode, _first, _count, _primcount);
  311. }
  312. static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
  313. {
  314. s_renderCtx.m_glctx.m_instancedArrays->DrawElementsInstancedANGLE(s_renderCtx.m_glctx.m_context, _mode, _count, _type, _indices, _primcount);
  315. }
  316. void naclSetIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  317. {
  318. s_renderCtx.m_glctx.m_instance = _instance;
  319. s_renderCtx.m_glctx.m_instInterface = _instInterface;
  320. s_renderCtx.m_glctx.m_graphicsInterface = _graphicsInterface;
  321. s_renderCtx.m_postSwapBuffers = _postSwapBuffers;
  322. s_renderCtx.m_glctx.m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  323. s_renderCtx.setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
  324. if (NULL != s_renderCtx.m_glctx.m_instancedArrays)
  325. {
  326. s_vertexAttribDivisor = naclVertexAttribDivisor;
  327. s_drawArraysInstanced = naclDrawArraysInstanced;
  328. s_drawElementsInstanced = naclDrawElementsInstanced;
  329. }
  330. }
  331. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
  332. {
  333. renderFrame();
  334. }
  335. #endif // BX_PLATFORM_
  336. static const GLenum s_primType[] =
  337. {
  338. GL_TRIANGLES,
  339. GL_LINES,
  340. GL_POINTS,
  341. };
  342. static const char* s_attribName[Attrib::Count] =
  343. {
  344. "a_position",
  345. "a_normal",
  346. "a_tangent",
  347. "a_color0",
  348. "a_color1",
  349. "a_indices",
  350. "a_weight",
  351. "a_texcoord0",
  352. "a_texcoord1",
  353. "a_texcoord2",
  354. "a_texcoord3",
  355. "a_texcoord4",
  356. "a_texcoord5",
  357. "a_texcoord6",
  358. "a_texcoord7",
  359. };
  360. static const char* s_instanceDataName[BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT] =
  361. {
  362. "i_data0",
  363. "i_data1",
  364. "i_data2",
  365. "i_data3",
  366. "i_data4",
  367. };
  368. static const GLenum s_attribType[AttribType::Count] =
  369. {
  370. GL_UNSIGNED_BYTE,
  371. GL_UNSIGNED_SHORT,
  372. GL_HALF_FLOAT,
  373. GL_FLOAT,
  374. };
  375. static const GLenum s_blendFactor[][2] =
  376. {
  377. { 0, 0 }, // ignored
  378. { GL_ZERO, GL_ZERO },
  379. { GL_ONE, GL_ONE },
  380. { GL_SRC_COLOR, GL_SRC_COLOR },
  381. { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR },
  382. { GL_SRC_ALPHA, GL_SRC_ALPHA },
  383. { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
  384. { GL_DST_ALPHA, GL_DST_ALPHA },
  385. { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA },
  386. { GL_DST_COLOR, GL_DST_COLOR },
  387. { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR },
  388. { GL_SRC_ALPHA_SATURATE, GL_ONE },
  389. };
  390. static const GLenum s_depthFunc[] =
  391. {
  392. 0, // ignored
  393. GL_LESS,
  394. GL_LEQUAL,
  395. GL_EQUAL,
  396. GL_GEQUAL,
  397. GL_GREATER,
  398. GL_NOTEQUAL,
  399. GL_NEVER,
  400. GL_ALWAYS,
  401. };
  402. static const GLenum s_stencilFunc[] =
  403. {
  404. 0, // ignored
  405. GL_LESS,
  406. GL_LEQUAL,
  407. GL_EQUAL,
  408. GL_GEQUAL,
  409. GL_GREATER,
  410. GL_NOTEQUAL,
  411. GL_NEVER,
  412. GL_ALWAYS,
  413. };
  414. static const GLenum s_stencilOp[] =
  415. {
  416. GL_ZERO,
  417. GL_KEEP,
  418. GL_REPLACE,
  419. GL_INCR_WRAP,
  420. GL_INCR,
  421. GL_DECR_WRAP,
  422. GL_DECR,
  423. GL_INVERT,
  424. };
  425. static const GLenum s_stencilFace[] =
  426. {
  427. GL_FRONT_AND_BACK,
  428. GL_FRONT,
  429. GL_BACK,
  430. };
  431. // Specifies the internal format of the texture.
  432. // Must be one of the following symbolic constants:
  433. // GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA.
  434. static const GLenum s_colorFormat[] =
  435. {
  436. 0, // ignored
  437. GL_RGBA,
  438. GL_RGBA,
  439. };
  440. static const GLenum s_depthFormat[] =
  441. {
  442. 0, // ignored
  443. 0,
  444. };
  445. static const GLenum s_textureAddress[] =
  446. {
  447. GL_REPEAT,
  448. GL_MIRRORED_REPEAT,
  449. GL_CLAMP_TO_EDGE,
  450. };
  451. static const GLenum s_textureFilter[] =
  452. {
  453. GL_LINEAR,
  454. GL_NEAREST,
  455. GL_LINEAR,
  456. };
  457. struct TextureFormatInfo
  458. {
  459. GLenum m_internalFmt;
  460. GLenum m_fmt;
  461. GLenum m_type;
  462. uint8_t m_bpp;
  463. };
  464. static TextureFormatInfo s_textureFormat[TextureFormat::Count] =
  465. {
  466. { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, 4 },
  467. { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, 4 },
  468. { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, 4 },
  469. { GL_ZERO, GL_ZERO, GL_ZERO, 0 },
  470. { GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8 },
  471. { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32 },
  472. { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32 },
  473. #if BGFX_CONFIG_RENDERER_OPENGL
  474. { GL_RGBA16, GL_RGBA, GL_HALF_FLOAT, 64 },
  475. #else
  476. { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 64 },
  477. #endif // BGFX_CONFIG_RENDERER_OPENGL
  478. { GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16 },
  479. { GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16 },
  480. { GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16 },
  481. { GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 32 },
  482. };
  483. const char* glslTypeName(GLuint _type)
  484. {
  485. #define GLSL_TYPE(_ty) case _ty: return #_ty
  486. switch (_type)
  487. {
  488. GLSL_TYPE(GL_FLOAT);
  489. GLSL_TYPE(GL_FLOAT_VEC2);
  490. GLSL_TYPE(GL_FLOAT_VEC3);
  491. GLSL_TYPE(GL_FLOAT_VEC4);
  492. GLSL_TYPE(GL_FLOAT_MAT2);
  493. GLSL_TYPE(GL_FLOAT_MAT3);
  494. GLSL_TYPE(GL_FLOAT_MAT4);
  495. // GLSL_TYPE(GL_FLOAT_MAT2x3);
  496. // GLSL_TYPE(GL_FLOAT_MAT2x4);
  497. // GLSL_TYPE(GL_FLOAT_MAT3x2);
  498. // GLSL_TYPE(GL_FLOAT_MAT3x4);
  499. // GLSL_TYPE(GL_FLOAT_MAT4x2);
  500. // GLSL_TYPE(GL_FLOAT_MAT4x3);
  501. // GLSL_TYPE(GL_SAMPLER_1D);
  502. GLSL_TYPE(GL_SAMPLER_2D);
  503. GLSL_TYPE(GL_SAMPLER_3D);
  504. GLSL_TYPE(GL_SAMPLER_CUBE);
  505. // GLSL_TYPE(GL_SAMPLER_1D_SHADOW);
  506. // GLSL_TYPE(GL_SAMPLER_2D_SHADOW);
  507. }
  508. #undef GLSL_TYPE
  509. return "UNKNOWN GLSL TYPE!";
  510. }
  511. const char* glEnumName(GLenum _enum)
  512. {
  513. #define GLENUM(_ty) case _ty: return #_ty
  514. switch (_enum)
  515. {
  516. GLENUM(GL_TEXTURE);
  517. GLENUM(GL_RENDERBUFFER);
  518. }
  519. #undef GLENUM
  520. return "UNKNOWN GLENUM!";
  521. }
  522. UniformType::Enum convertGlType(GLenum _type)
  523. {
  524. switch (_type)
  525. {
  526. case GL_FLOAT:
  527. return UniformType::Uniform1fv;
  528. case GL_FLOAT_VEC2:
  529. return UniformType::Uniform2fv;
  530. case GL_FLOAT_VEC3:
  531. return UniformType::Uniform3fv;
  532. case GL_FLOAT_VEC4:
  533. return UniformType::Uniform4fv;
  534. case GL_FLOAT_MAT2:
  535. break;
  536. case GL_FLOAT_MAT3:
  537. return UniformType::Uniform3x3fv;
  538. case GL_FLOAT_MAT4:
  539. return UniformType::Uniform4x4fv;
  540. // case GL_FLOAT_MAT2x3:
  541. // case GL_FLOAT_MAT2x4:
  542. // case GL_FLOAT_MAT3x2:
  543. // case GL_FLOAT_MAT3x4:
  544. // case GL_FLOAT_MAT4x2:
  545. // case GL_FLOAT_MAT4x3:
  546. // break;
  547. // case GL_SAMPLER_1D:
  548. case GL_SAMPLER_2D:
  549. case GL_SAMPLER_3D:
  550. case GL_SAMPLER_CUBE:
  551. // case GL_SAMPLER_1D_SHADOW:
  552. // case GL_SAMPLER_2D_SHADOW:
  553. return UniformType::Uniform1iv;
  554. };
  555. return UniformType::End;
  556. }
  557. void Program::create(const Shader& _vsh, const Shader& _fsh)
  558. {
  559. m_id = glCreateProgram();
  560. BX_TRACE("program create: %d: %d, %d", m_id, _vsh.m_id, _fsh.m_id);
  561. bool cached = false;
  562. uint64_t id = (uint64_t(_vsh.m_hash)<<32) | _fsh.m_hash;
  563. id ^= s_renderCtx.m_hash;
  564. if (s_renderCtx.m_programBinarySupport)
  565. {
  566. uint32_t length = g_callback->cacheReadSize(id);
  567. cached = length > 0;
  568. if (cached)
  569. {
  570. void* data = g_realloc(NULL, length);
  571. if (g_callback->cacheRead(id, data, length) )
  572. {
  573. bx::MemoryReader reader(data, length);
  574. GLenum format;
  575. bx::read(&reader, format);
  576. GL_CHECK(glProgramBinary(m_id, format, reader.getDataPtr(), (GLsizei)reader.remaining() ) );
  577. }
  578. g_free(data);
  579. }
  580. #if BGFX_CONFIG_RENDERER_OPENGL
  581. GL_CHECK(glProgramParameteri(m_id, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE) );
  582. #endif // BGFX_CONFIG_RENDERER_OPENGL
  583. }
  584. if (!cached)
  585. {
  586. GL_CHECK(glAttachShader(m_id, _vsh.m_id) );
  587. GL_CHECK(glAttachShader(m_id, _fsh.m_id) );
  588. GL_CHECK(glLinkProgram(m_id) );
  589. GLint linked = 0;
  590. GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) );
  591. if (0 == linked)
  592. {
  593. char log[1024];
  594. GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) );
  595. BX_TRACE("%d: %s", linked, log);
  596. GL_CHECK(glDeleteProgram(m_id) );
  597. return;
  598. }
  599. if (s_renderCtx.m_programBinarySupport)
  600. {
  601. GLint programLength;
  602. GLenum format;
  603. GL_CHECK(glGetProgramiv(m_id, GL_PROGRAM_BINARY_LENGTH, &programLength) );
  604. if (0 < programLength)
  605. {
  606. uint32_t length = programLength + 4;
  607. uint8_t* data = (uint8_t*)g_realloc(NULL, length);
  608. GL_CHECK(glGetProgramBinary(m_id, programLength, NULL, &format, &data[4]) );
  609. *(uint32_t*)data = format;
  610. g_callback->cacheWrite(id, data, length);
  611. g_free(data);
  612. }
  613. }
  614. }
  615. init();
  616. }
  617. void Program::destroy()
  618. {
  619. GL_CHECK(glUseProgram(0) );
  620. GL_CHECK(glDeleteProgram(m_id) );
  621. }
  622. void Program::init()
  623. {
  624. GLint activeAttribs;
  625. GLint activeUniforms;
  626. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTES, &activeAttribs) );
  627. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORMS, &activeUniforms) );
  628. GLint max0, max1;
  629. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max0) );
  630. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max1) );
  631. GLint maxLength = uint32_max(max0, max1);
  632. char* name = (char*)g_realloc(NULL, maxLength + 1);
  633. BX_TRACE("Program %d", m_id);
  634. BX_TRACE("Attributes:");
  635. for (int32_t ii = 0; ii < activeAttribs; ++ii)
  636. {
  637. GLint size;
  638. GLenum type;
  639. GL_CHECK(glGetActiveAttrib(m_id, ii, maxLength + 1, NULL, &size, &type, name) );
  640. BX_TRACE("\t%s %s is at location %d"
  641. , glslTypeName(type)
  642. , name
  643. , glGetAttribLocation(m_id, name)
  644. );
  645. }
  646. m_numPredefined = 0;
  647. m_constantBuffer = ConstantBuffer::create(1024);
  648. m_numSamplers = 0;
  649. BX_TRACE("Uniforms:");
  650. for (int32_t ii = 0; ii < activeUniforms; ++ii)
  651. {
  652. GLint num;
  653. GLenum gltype;
  654. GL_CHECK(glGetActiveUniform(m_id, ii, maxLength + 1, NULL, &num, &gltype, name) );
  655. GLint loc = glGetUniformLocation(m_id, name);
  656. int offset = 0;
  657. char* array = strchr(name, '[');
  658. if (NULL != array)
  659. {
  660. BX_TRACE("--- %s", name);
  661. *array = '\0';
  662. array++;
  663. char* end = strchr(array, ']');
  664. *end = '\0';
  665. offset = atoi(array);
  666. }
  667. if (GL_SAMPLER_2D == gltype)
  668. {
  669. BX_TRACE("Sampler %d at %d.", m_numSamplers, loc);
  670. m_sampler[m_numSamplers] = loc;
  671. m_numSamplers++;
  672. }
  673. const void* data = NULL;
  674. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  675. if (PredefinedUniform::Count != predefined)
  676. {
  677. m_predefined[m_numPredefined].m_loc = loc;
  678. m_predefined[m_numPredefined].m_type = predefined;
  679. m_predefined[m_numPredefined].m_count = num;
  680. m_numPredefined++;
  681. }
  682. else
  683. {
  684. const UniformInfo* info = s_renderCtx.m_uniformReg.find(name);
  685. if (NULL != info)
  686. {
  687. data = info->m_data;
  688. UniformType::Enum type = convertGlType(gltype);
  689. m_constantBuffer->writeUniformRef(type, 0, data, num);
  690. m_constantBuffer->write(loc);
  691. BX_TRACE("store %s %p", name, data);
  692. }
  693. }
  694. BX_TRACE("\tuniform %s %s%s is at location %d, size %d (%p), offset %d"
  695. , glslTypeName(gltype)
  696. , name
  697. , PredefinedUniform::Count != predefined ? "*" : ""
  698. , loc
  699. , num
  700. , data
  701. , offset
  702. );
  703. BX_UNUSED(offset);
  704. }
  705. m_constantBuffer->finish();
  706. g_free(name);
  707. memset(m_attributes, 0xff, sizeof(m_attributes) );
  708. uint32_t used = 0;
  709. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  710. {
  711. GLint loc = glGetAttribLocation(m_id, s_attribName[ii]);
  712. if (-1 != loc)
  713. {
  714. BX_TRACE("attr %s: %d", s_attribName[ii], loc);
  715. m_attributes[ii] = loc;
  716. m_used[used++] = ii;
  717. }
  718. }
  719. m_used[used] = Attrib::Count;
  720. used = 0;
  721. for (uint32_t ii = 0; ii < countof(s_instanceDataName); ++ii)
  722. {
  723. GLuint loc = glGetAttribLocation(m_id, s_instanceDataName[ii]);
  724. if (GLuint(-1) != loc )
  725. {
  726. BX_TRACE("instance data %s: %d", s_instanceDataName[ii], loc);
  727. m_instanceData[used++] = loc;
  728. }
  729. }
  730. m_instanceData[used] = 0xffff;
  731. }
  732. void Program::bindAttributes(const VertexDecl& _vertexDecl, uint32_t _baseVertex) const
  733. {
  734. uint32_t enabled = 0;
  735. for (uint32_t ii = 0; Attrib::Count != m_used[ii]; ++ii)
  736. {
  737. Attrib::Enum attr = Attrib::Enum(m_used[ii]);
  738. GLint loc = m_attributes[attr];
  739. uint8_t num;
  740. AttribType::Enum type;
  741. bool normalized;
  742. bool asInt;
  743. _vertexDecl.decode(attr, num, type, normalized, asInt);
  744. if (-1 != loc
  745. && 0xff != _vertexDecl.m_attributes[attr])
  746. {
  747. GL_CHECK(glEnableVertexAttribArray(loc) );
  748. enabled |= 1<<attr;
  749. GL_CHECK(s_vertexAttribDivisor(loc, 0) );
  750. uint32_t baseVertex = _baseVertex*_vertexDecl.m_stride + _vertexDecl.m_offset[attr];
  751. GL_CHECK(glVertexAttribPointer(loc, num, s_attribType[type], normalized, _vertexDecl.m_stride, (void*)(uintptr_t)baseVertex) );
  752. }
  753. else
  754. {
  755. // GL_CHECK(glDisableVertexAttribArray(loc) );
  756. switch (num)
  757. {
  758. case 1:
  759. GL_CHECK(glVertexAttrib1f(loc, 0.0f) );
  760. break;
  761. case 2:
  762. GL_CHECK(glVertexAttrib2f(loc, 0.0f, 0.0f) );
  763. break;
  764. case 3:
  765. GL_CHECK(glVertexAttrib3f(loc, 0.0f, 0.0f, 0.0f) );
  766. break;
  767. case 4:
  768. GL_CHECK(glVertexAttrib4f(loc, 0.0f, 0.0f, 0.0f, 0.0f) );
  769. break;
  770. default:
  771. BX_CHECK(false, "You should not be here!");
  772. break;
  773. }
  774. }
  775. }
  776. }
  777. void Program::bindInstanceData(uint32_t _stride, uint32_t _baseVertex) const
  778. {
  779. uint32_t baseVertex = _baseVertex;
  780. for (uint32_t ii = 0; 0xffff != m_instanceData[ii]; ++ii)
  781. {
  782. GLint loc = m_instanceData[ii];
  783. GL_CHECK(glEnableVertexAttribArray(loc) );
  784. GL_CHECK(glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, _stride, (void*)(uintptr_t)baseVertex) );
  785. GL_CHECK(s_vertexAttribDivisor(loc, 1) );
  786. baseVertex += 16;
  787. }
  788. }
  789. static void texImage(GLenum _target, GLint _level, GLint _internalFormat, GLsizei _width, GLsizei _height, GLsizei _depth, GLint _border, GLenum _format, GLenum _type, const GLvoid* _pixels)
  790. {
  791. #if BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  792. if (_target == GL_TEXTURE_3D)
  793. {
  794. GL_CHECK(glTexImage3D(_target
  795. , _level
  796. , _internalFormat
  797. , _width
  798. , _height
  799. , _depth
  800. , _border
  801. , _format
  802. , _type
  803. , _pixels
  804. ) );
  805. }
  806. else
  807. #endif // BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  808. {
  809. BX_UNUSED(_depth);
  810. GL_CHECK(glTexImage2D(_target
  811. , _level
  812. , _internalFormat
  813. , _width
  814. , _height
  815. , _border
  816. , _format
  817. , _type
  818. , _pixels
  819. ) );
  820. }
  821. }
  822. static void compressedTexImage(GLenum _target, GLint _level, GLenum _internalformat, GLsizei _width, GLsizei _height, GLsizei _depth, GLint _border, GLsizei _imageSize, const GLvoid* _data)
  823. {
  824. #if BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  825. if (_target == GL_TEXTURE_3D)
  826. {
  827. GL_CHECK(glCompressedTexImage3D(_target
  828. , _level
  829. , _internalformat
  830. , _width
  831. , _height
  832. , _depth
  833. , _border
  834. , _imageSize
  835. , _data
  836. ) );
  837. }
  838. else
  839. #endif // BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  840. {
  841. BX_UNUSED(_depth);
  842. GL_CHECK(glCompressedTexImage2D(_target
  843. , _level
  844. , _internalformat
  845. , _width
  846. , _height
  847. , _border
  848. , _imageSize
  849. , _data
  850. ) );
  851. }
  852. }
  853. void Texture::create(const Memory* _mem, uint32_t _flags)
  854. {
  855. Dds dds;
  856. uint8_t numMips = 0;
  857. if (parseDds(dds, _mem) )
  858. {
  859. numMips = dds.m_numMips;
  860. if (dds.m_cubeMap)
  861. {
  862. m_target = GL_TEXTURE_CUBE_MAP;
  863. }
  864. #if BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  865. else if (dds.m_depth > 1)
  866. {
  867. m_target = GL_TEXTURE_3D;
  868. }
  869. #endif // BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  870. else
  871. {
  872. m_target = GL_TEXTURE_2D;
  873. }
  874. GL_CHECK(glGenTextures(1, &m_id) );
  875. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  876. GL_CHECK(glBindTexture(m_target, m_id) );
  877. const TextureFormatInfo& tfi = s_textureFormat[dds.m_type];
  878. GLenum internalFmt = tfi.m_internalFmt;
  879. m_fmt = tfi.m_fmt;
  880. m_type = tfi.m_type;
  881. GLenum target = m_target;
  882. if (dds.m_cubeMap)
  883. {
  884. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  885. }
  886. if (!s_renderCtx.m_dxtSupport
  887. || TextureFormat::Unknown < dds.m_type)
  888. {
  889. bool decompress = TextureFormat::Unknown > dds.m_type;
  890. if (decompress)
  891. {
  892. const TextureFormatInfo& tfi = s_textureFormat[TextureFormat::BGRA8];
  893. internalFmt = tfi.m_internalFmt;
  894. m_fmt = tfi.m_fmt;
  895. m_type = tfi.m_type;
  896. }
  897. bool swizzle = GL_RGBA == m_fmt;
  898. #if BGFX_CONFIG_RENDERER_OPENGL
  899. if (swizzle
  900. && s_renderCtx.m_textureSwizzleSupport)
  901. {
  902. swizzle = false;
  903. GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
  904. GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) );
  905. }
  906. #endif // BGFX_CONFIG_RENDERER_OPENGL
  907. uint8_t* bits = (uint8_t*)g_realloc(NULL, dds.m_width*dds.m_height*4);
  908. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  909. {
  910. uint32_t width = dds.m_width;
  911. uint32_t height = dds.m_height;
  912. uint32_t depth = dds.m_depth;
  913. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  914. {
  915. width = uint32_max(1, width);
  916. height = uint32_max(1, height);
  917. depth = uint32_max(1, depth);
  918. Mip mip;
  919. if (getRawImageData(dds, side, lod, _mem, mip) )
  920. {
  921. mip.decode(bits);
  922. if (swizzle)
  923. {
  924. rgbaToBgra(bits, width, height);
  925. }
  926. texImage(target+side
  927. , lod
  928. , internalFmt
  929. , width
  930. , height
  931. , depth
  932. , 0
  933. , m_fmt
  934. , m_type
  935. , bits
  936. );
  937. }
  938. width >>= 1;
  939. height >>= 1;
  940. depth >>= 1;
  941. }
  942. }
  943. g_free(bits);
  944. }
  945. else
  946. {
  947. m_compressed = true;
  948. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  949. {
  950. uint32_t width = dds.m_width;
  951. uint32_t height = dds.m_height;
  952. uint32_t depth = dds.m_depth;
  953. for (uint32_t ii = 0, num = dds.m_numMips; ii < num; ++ii)
  954. {
  955. width = uint32_max(1, width);
  956. height = uint32_max(1, height);
  957. depth = uint32_max(1, depth);
  958. Mip mip;
  959. if (getRawImageData(dds, side, ii, _mem, mip) )
  960. {
  961. compressedTexImage(target+side
  962. , ii
  963. , internalFmt
  964. , width
  965. , height
  966. , depth
  967. , 0
  968. , mip.m_size
  969. , mip.m_data
  970. );
  971. }
  972. width >>= 1;
  973. height >>= 1;
  974. depth >>= 1;
  975. }
  976. }
  977. }
  978. }
  979. else
  980. {
  981. bx::MemoryReader reader(_mem->data, _mem->size);
  982. uint32_t magic;
  983. bx::read(&reader, magic);
  984. if (BGFX_CHUNK_MAGIC_TEX == magic)
  985. {
  986. TextureCreate tc;
  987. bx::read(&reader, tc);
  988. if (tc.m_cubeMap)
  989. {
  990. m_target = GL_TEXTURE_CUBE_MAP;
  991. }
  992. #if BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  993. else if (tc.m_depth > 1)
  994. {
  995. m_target = GL_TEXTURE_3D;
  996. }
  997. #endif // BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES3
  998. else
  999. {
  1000. m_target = GL_TEXTURE_2D;
  1001. }
  1002. GL_CHECK(glGenTextures(1, &m_id) );
  1003. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  1004. GL_CHECK(glBindTexture(m_target, m_id) );
  1005. const TextureFormatInfo& tfi = s_textureFormat[tc.m_format];
  1006. GLenum internalFmt = tfi.m_internalFmt;
  1007. m_fmt = tfi.m_fmt;
  1008. m_type = tfi.m_type;
  1009. m_compressed = tc.m_format < TextureFormat::Unknown;
  1010. GLenum target = m_target;
  1011. if (tc.m_cubeMap)
  1012. {
  1013. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  1014. }
  1015. uint32_t bpp = tfi.m_bpp;
  1016. uint8_t* data = NULL != tc.m_mem ? tc.m_mem->data : NULL;
  1017. uint32_t min = m_compressed ? 4 : 1;
  1018. bool swizzle = GL_RGBA == m_fmt;
  1019. #if BGFX_CONFIG_RENDERER_OPENGL
  1020. if (swizzle
  1021. && s_renderCtx.m_textureSwizzleSupport)
  1022. {
  1023. swizzle = false;
  1024. GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
  1025. GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) );
  1026. }
  1027. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1028. for (uint8_t side = 0, numSides = tc.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1029. {
  1030. uint32_t width = tc.m_width;
  1031. uint32_t height = tc.m_height;
  1032. uint32_t depth = tc.m_depth;
  1033. for (uint32_t lod = 0, num = tc.m_numMips; lod < num; ++lod)
  1034. {
  1035. width = uint32_max(width, min);
  1036. height = uint32_max(height, min);
  1037. depth = uint32_max(1, depth);
  1038. uint32_t size = width*height*bpp/8;
  1039. if (m_compressed)
  1040. {
  1041. compressedTexImage(target+side
  1042. , lod
  1043. , internalFmt
  1044. , width
  1045. , height
  1046. , depth
  1047. , 0
  1048. , size
  1049. , data
  1050. );
  1051. }
  1052. else
  1053. {
  1054. if (swizzle)
  1055. {
  1056. rgbaToBgra(data, width, height);
  1057. }
  1058. texImage(target+side
  1059. , lod
  1060. , internalFmt
  1061. , width
  1062. , height
  1063. , depth
  1064. , 0
  1065. , m_fmt
  1066. , m_type
  1067. , data
  1068. );
  1069. }
  1070. if (NULL != data)
  1071. {
  1072. data += size;
  1073. }
  1074. width >>= 1;
  1075. height >>= 1;
  1076. depth >>= 1;
  1077. }
  1078. }
  1079. if (NULL != tc.m_mem)
  1080. {
  1081. release(tc.m_mem);
  1082. }
  1083. }
  1084. else
  1085. {
  1086. //
  1087. }
  1088. }
  1089. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]) );
  1090. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]) );
  1091. #if BGFX_CONFIG_RENDERER_OPENGL
  1092. if (m_target == GL_TEXTURE_3D)
  1093. {
  1094. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_R, s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]) );
  1095. }
  1096. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1097. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, s_textureFilter[(_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT]) );
  1098. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, s_textureFilter[(_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT]) );
  1099. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, 1 < numMips ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR) );
  1100. if (0 != (_flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) )
  1101. && 0.0f < s_renderCtx.m_maxAnisotropy)
  1102. {
  1103. glTexParameterf(m_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, s_renderCtx.m_maxAnisotropy);
  1104. }
  1105. GL_CHECK(glBindTexture(m_target, 0) );
  1106. }
  1107. void Texture::createColor(uint32_t _width, uint32_t _height, GLenum _min, GLenum _mag)
  1108. {
  1109. GLenum internalFormat = /*_fp ? GL_RGBA16F_ARB :*/ GL_RGBA;
  1110. GLenum type = /*_fp ? GL_HALF_FLOAT_ARB :*/ GL_UNSIGNED_BYTE;
  1111. m_target = /*0 != _depth ? GL_TEXTURE_3D :*/ GL_TEXTURE_2D;
  1112. GL_CHECK(glGenTextures(1, &m_id) );
  1113. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  1114. GL_CHECK(glBindTexture(m_target, m_id) );
  1115. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, _min) );
  1116. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, _mag) );
  1117. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
  1118. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
  1119. GL_CHECK(glTexImage2D(m_target
  1120. , 0
  1121. , internalFormat
  1122. , _width
  1123. , _height
  1124. , 0
  1125. , GL_RGBA
  1126. , type
  1127. , NULL
  1128. ) );
  1129. GL_CHECK(glBindTexture(m_target, 0) );
  1130. }
  1131. void Texture::createDepth(uint32_t _width, uint32_t _height)
  1132. {
  1133. m_target = GL_TEXTURE_2D;
  1134. GL_CHECK(glGenTextures(1, &m_id) );
  1135. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  1136. GL_CHECK(glBindTexture(m_target, m_id) );
  1137. // glTexParameteri(m_target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  1138. // glTexParameteri(m_target, GL_DEPTH_TEXTURE_MODE, GL_NONE);
  1139. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
  1140. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR) );
  1141. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
  1142. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
  1143. // OpenGL ES 2.0 doesn't support GL_DEPTH_COMPONENT... this will fail.
  1144. GL_CHECK(glTexImage2D(m_target
  1145. , 0
  1146. , GL_DEPTH_COMPONENT
  1147. , _width
  1148. , _height
  1149. , 0
  1150. , GL_DEPTH_COMPONENT
  1151. , GL_FLOAT
  1152. , NULL
  1153. ) );
  1154. GL_CHECK(glBindTexture(m_target, 0) );
  1155. }
  1156. void Texture::destroy()
  1157. {
  1158. if (0 != m_id)
  1159. {
  1160. GL_CHECK(glBindTexture(m_target, 0) );
  1161. GL_CHECK(glDeleteTextures(1, &m_id) );
  1162. m_id = 0;
  1163. }
  1164. }
  1165. void Texture::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
  1166. {
  1167. GL_CHECK(glBindTexture(m_target, m_id) );
  1168. GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  1169. switch (m_target)
  1170. {
  1171. case GL_TEXTURE_2D:
  1172. if (m_compressed)
  1173. {
  1174. GL_CHECK(glCompressedTexSubImage2D(m_target
  1175. , _mip
  1176. , _rect.m_x
  1177. , _rect.m_y
  1178. , _rect.m_width
  1179. , _rect.m_height
  1180. , m_fmt
  1181. , _mem->size
  1182. , _mem->data
  1183. ) );
  1184. }
  1185. else
  1186. {
  1187. GL_CHECK(glTexSubImage2D(m_target
  1188. , _mip
  1189. , _rect.m_x
  1190. , _rect.m_y
  1191. , _rect.m_width
  1192. , _rect.m_height
  1193. , m_fmt
  1194. , m_type
  1195. , _mem->data
  1196. ) );
  1197. }
  1198. break;
  1199. case GL_TEXTURE_CUBE_MAP:
  1200. if (m_compressed)
  1201. {
  1202. GL_CHECK(glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+_side
  1203. , _mip
  1204. , _rect.m_x
  1205. , _rect.m_y
  1206. , _rect.m_width
  1207. , _rect.m_height
  1208. , m_fmt
  1209. , _mem->size
  1210. , _mem->data
  1211. ) );
  1212. }
  1213. else
  1214. {
  1215. GL_CHECK(glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+_side
  1216. , _mip
  1217. , _rect.m_x
  1218. , _rect.m_y
  1219. , _rect.m_width
  1220. , _rect.m_height
  1221. , m_fmt
  1222. , m_type
  1223. , _mem->data
  1224. ) );
  1225. }
  1226. break;
  1227. #if BGFX_CONFIG_RENDERER_OPENGL
  1228. case GL_TEXTURE_3D:
  1229. if (m_compressed)
  1230. {
  1231. GL_CHECK(glCompressedTexSubImage3D(m_target
  1232. , _mip
  1233. , _rect.m_x
  1234. , _rect.m_y
  1235. , _z
  1236. , _rect.m_width
  1237. , _rect.m_height
  1238. , _depth
  1239. , m_fmt
  1240. , _mem->size
  1241. , _mem->data
  1242. ) );
  1243. }
  1244. else
  1245. {
  1246. GL_CHECK(glTexSubImage3D(m_target
  1247. , _mip
  1248. , _rect.m_x
  1249. , _rect.m_y
  1250. , _z
  1251. , _rect.m_width
  1252. , _rect.m_height
  1253. , _depth
  1254. , m_fmt
  1255. , m_type
  1256. , _mem->data
  1257. ) );
  1258. }
  1259. break;
  1260. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1261. }
  1262. }
  1263. void RenderTarget::create(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
  1264. {
  1265. BX_TRACE("Create render target %dx%d 0x%02x", _width, _height, _flags);
  1266. m_width = _width;
  1267. m_height = _height;
  1268. // m_msaa = s_msaa[(m_flags&BGFX_RENDER_TARGET_MSAA_MASK)>>BGFX_RENDER_TARGET_MSAA_SHIFT];
  1269. uint32_t colorFormat = (_flags&BGFX_RENDER_TARGET_COLOR_MASK)>>BGFX_RENDER_TARGET_COLOR_SHIFT;
  1270. uint32_t depthFormat = (_flags&BGFX_RENDER_TARGET_DEPTH_MASK)>>BGFX_RENDER_TARGET_DEPTH_SHIFT;
  1271. GLenum minFilter = s_textureFilter[(_textureFlags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT];
  1272. GLenum magFilter = s_textureFilter[(_textureFlags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT];
  1273. if (0 < colorFormat)
  1274. {
  1275. m_color.createColor(_width, _height, minFilter, magFilter);
  1276. }
  1277. #if 0 // GLES can't create texture with depth texture format...
  1278. if (0 < depthFormat)
  1279. {
  1280. m_depth.createDepth(_width, _height);
  1281. }
  1282. #endif //
  1283. GL_CHECK(glGenFramebuffers(1, &m_fbo) );
  1284. BX_CHECK(0 != m_fbo, "Failed to generate framebuffer id.");
  1285. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo) );
  1286. if (0 < colorFormat)
  1287. {
  1288. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  1289. , GL_COLOR_ATTACHMENT0
  1290. , m_color.m_target
  1291. , m_color.m_id
  1292. , 0
  1293. ) );
  1294. }
  1295. if (0 < depthFormat)
  1296. {
  1297. if (0 < colorFormat)
  1298. {
  1299. #if BGFX_CONFIG_RENDERER_OPENGL
  1300. GLenum depthComponent = GL_DEPTH_COMPONENT32;
  1301. #else
  1302. GLenum depthComponent = GL_DEPTH_COMPONENT16;
  1303. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1304. GL_CHECK(glGenRenderbuffers(1, &m_rbo) );
  1305. BX_CHECK(0 != m_rbo, "Failed to generate renderbuffer id.");
  1306. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_rbo) );
  1307. GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER, depthComponent, _width, _height) );
  1308. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
  1309. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  1310. , GL_DEPTH_ATTACHMENT
  1311. , GL_RENDERBUFFER
  1312. , m_rbo
  1313. ) );
  1314. }
  1315. else
  1316. {
  1317. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  1318. , GL_DEPTH_ATTACHMENT
  1319. , m_depth.m_target
  1320. , m_depth.m_id
  1321. , 0
  1322. ) );
  1323. }
  1324. }
  1325. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
  1326. , "glCheckFramebufferStatus failed 0x%08x"
  1327. , glCheckFramebufferStatus(GL_FRAMEBUFFER)
  1328. );
  1329. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1330. }
  1331. void RenderTarget::destroy()
  1332. {
  1333. GL_CHECK(glDeleteFramebuffers(1, &m_fbo) );
  1334. if (0 != m_rbo)
  1335. {
  1336. GL_CHECK(glDeleteRenderbuffers(1, &m_rbo) );
  1337. }
  1338. m_color.destroy();
  1339. m_depth.destroy();
  1340. }
  1341. void ConstantBuffer::commit()
  1342. {
  1343. reset();
  1344. do
  1345. {
  1346. uint32_t opcode = read();
  1347. if (UniformType::End == opcode)
  1348. {
  1349. break;
  1350. }
  1351. UniformType::Enum type;
  1352. uint16_t ignore;
  1353. uint16_t num;
  1354. uint16_t copy;
  1355. decodeOpcode(opcode, type, ignore, num, copy);
  1356. const char* data;
  1357. if (copy)
  1358. {
  1359. data = read(g_uniformTypeSize[type]*num);
  1360. }
  1361. else
  1362. {
  1363. memcpy(&data, read(sizeof(void*) ), sizeof(void*) );
  1364. }
  1365. uint32_t loc = read();
  1366. #define CASE_IMPLEMENT_UNIFORM(_uniform, _glsuffix, _dxsuffix, _type) \
  1367. case UniformType::_uniform: \
  1368. { \
  1369. _type* value = (_type*)data; \
  1370. GL_CHECK(glUniform##_glsuffix(loc, num, value) ); \
  1371. } \
  1372. break;
  1373. #define CASE_IMPLEMENT_UNIFORM_T(_uniform, _glsuffix, _dxsuffix, _type) \
  1374. case UniformType::_uniform: \
  1375. { \
  1376. _type* value = (_type*)data; \
  1377. GL_CHECK(glUniform##_glsuffix(loc, num, GL_FALSE, value) ); \
  1378. } \
  1379. break;
  1380. switch (type)
  1381. {
  1382. // case ConstantType::Uniform1iv:
  1383. // {
  1384. // int* value = (int*)data;
  1385. // BX_TRACE("Uniform1iv sampler %d, loc %d (num %d, copy %d)", *value, loc, num, copy);
  1386. // GL_CHECK(glUniform1iv(loc, num, value) );
  1387. // }
  1388. // break;
  1389. CASE_IMPLEMENT_UNIFORM(Uniform1i, 1iv, I, int);
  1390. CASE_IMPLEMENT_UNIFORM(Uniform1f, 1fv, F, float);
  1391. CASE_IMPLEMENT_UNIFORM(Uniform1iv, 1iv, I, int);
  1392. CASE_IMPLEMENT_UNIFORM(Uniform1fv, 1fv, F, float);
  1393. CASE_IMPLEMENT_UNIFORM(Uniform2fv, 2fv, F, float);
  1394. CASE_IMPLEMENT_UNIFORM(Uniform3fv, 3fv, F, float);
  1395. CASE_IMPLEMENT_UNIFORM(Uniform4fv, 4fv, F, float);
  1396. CASE_IMPLEMENT_UNIFORM_T(Uniform3x3fv, Matrix3fv, F, float);
  1397. CASE_IMPLEMENT_UNIFORM_T(Uniform4x4fv, Matrix4fv, F, float);
  1398. case UniformType::End:
  1399. break;
  1400. default:
  1401. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", m_pos, opcode, type, loc, num, copy);
  1402. break;
  1403. }
  1404. #undef CASE_IMPLEMENT_UNIFORM
  1405. #undef CASE_IMPLEMENT_UNIFORM_T
  1406. } while (true);
  1407. }
  1408. void TextVideoMemBlitter::setup()
  1409. {
  1410. if (s_renderCtx.m_vaoSupport)
  1411. {
  1412. GL_CHECK(glBindVertexArray(0) );
  1413. }
  1414. uint32_t width = s_renderCtx.m_resolution.m_width;
  1415. uint32_t height = s_renderCtx.m_resolution.m_height;
  1416. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1417. GL_CHECK(glViewport(0, 0, width, height) );
  1418. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  1419. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  1420. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  1421. GL_CHECK(glDisable(GL_CULL_FACE) );
  1422. GL_CHECK(glDisable(GL_BLEND) );
  1423. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  1424. #if BGFX_CONFIG_RENDERER_OPENGL
  1425. GL_CHECK(glDisable(GL_ALPHA_TEST) );
  1426. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1427. Program& program = s_renderCtx.m_program[m_program.idx];
  1428. GL_CHECK(glUseProgram(program.m_id) );
  1429. GL_CHECK(glUniform1i(program.m_sampler[0], 0) );
  1430. float proj[16];
  1431. mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  1432. GL_CHECK(glUniformMatrix4fv(program.m_predefined[0].m_loc
  1433. , 1
  1434. , GL_FALSE
  1435. , proj
  1436. ) );
  1437. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  1438. GL_CHECK(glBindTexture(GL_TEXTURE_2D, s_renderCtx.m_textures[m_texture.idx].m_id) );
  1439. }
  1440. void TextVideoMemBlitter::render(uint32_t _numIndices)
  1441. {
  1442. uint32_t numVertices = _numIndices*4/6;
  1443. s_renderCtx.m_indexBuffers[m_ib->handle.idx].update(0, _numIndices*2, m_ib->data);
  1444. s_renderCtx.m_vertexBuffers[m_vb->handle.idx].update(0, numVertices*m_decl.m_stride, m_vb->data);
  1445. VertexBuffer& vb = s_renderCtx.m_vertexBuffers[m_vb->handle.idx];
  1446. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  1447. IndexBuffer& ib = s_renderCtx.m_indexBuffers[m_ib->handle.idx];
  1448. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  1449. Program& program = s_renderCtx.m_program[m_program.idx];
  1450. program.bindAttributes(m_decl, 0);
  1451. GL_CHECK(glDrawElements(GL_TRIANGLES
  1452. , _numIndices
  1453. , GL_UNSIGNED_SHORT
  1454. , (void*)0
  1455. ) );
  1456. }
  1457. void Context::flip()
  1458. {
  1459. s_renderCtx.flip();
  1460. }
  1461. GLint glGet(GLenum _pname)
  1462. {
  1463. GLint result;
  1464. GL_CHECK(glGetIntegerv(_pname, &result) );
  1465. return result;
  1466. }
  1467. void Context::rendererInit()
  1468. {
  1469. s_renderCtx.init();
  1470. #if BGFX_CONFIG_DEBUG
  1471. GLint numCmpFormats;
  1472. GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCmpFormats) );
  1473. BX_TRACE("GL_NUM_COMPRESSED_TEXTURE_FORMATS %d", numCmpFormats);
  1474. if (0 < numCmpFormats)
  1475. {
  1476. GLint* formats = (GLint*)alloca(sizeof(GLint)*numCmpFormats);
  1477. GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats) );
  1478. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  1479. {
  1480. BX_TRACE("\t%3d: %8x", ii, formats[ii]);
  1481. }
  1482. }
  1483. # define GL_GET(_pname, _min) BX_TRACE(#_pname " %d (min: %d)", glGet(_pname), _min)
  1484. # if BX_PLATFORM_OSX
  1485. GL_GET(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, 16 * 4);
  1486. GL_GET(GL_MAX_VERTEX_UNIFORM_COMPONENTS, 128 * 4);
  1487. GL_GET(GL_MAX_VARYING_FLOATS, 8 * 4);
  1488. # else
  1489. GL_GET(GL_MAX_FRAGMENT_UNIFORM_VECTORS, 16);
  1490. GL_GET(GL_MAX_VERTEX_UNIFORM_VECTORS, 128);
  1491. GL_GET(GL_MAX_VARYING_VECTORS, 8);
  1492. # endif
  1493. GL_GET(GL_MAX_VERTEX_ATTRIBS, 8);
  1494. GL_GET(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, 8);
  1495. GL_GET(GL_MAX_CUBE_MAP_TEXTURE_SIZE, 16);
  1496. GL_GET(GL_MAX_TEXTURE_IMAGE_UNITS, 8);
  1497. GL_GET(GL_MAX_TEXTURE_SIZE, 64);
  1498. GL_GET(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, 0);
  1499. GL_GET(GL_MAX_RENDERBUFFER_SIZE, 1);
  1500. const char* version = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
  1501. GL_CHECK(version = version); // check if error is generated by glGetString.
  1502. BX_TRACE("GLSL version: %s", version);
  1503. #endif // BGFX_CONFIG_DEBUG
  1504. const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
  1505. if (NULL != extensions)
  1506. {
  1507. char name[1024];
  1508. const char* pos = extensions;
  1509. const char* end = extensions + strlen(extensions);
  1510. while (pos < end)
  1511. {
  1512. uint32_t len;
  1513. const char* space = strchr(pos, ' ');
  1514. if (NULL != space)
  1515. {
  1516. len = uint32_min(sizeof(name), (uint32_t)(space - pos) );
  1517. }
  1518. else
  1519. {
  1520. len = uint32_min(sizeof(name), (uint32_t)strlen(pos) );
  1521. }
  1522. strncpy(name, pos, len);
  1523. name[len] = '\0';
  1524. bool supported = false;
  1525. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  1526. {
  1527. Extension& extension = s_extension[ii];
  1528. if (!extension.m_supported
  1529. && extension.m_initialize)
  1530. {
  1531. if (0 == strcmp(name, extension.m_name) )
  1532. {
  1533. extension.m_supported = true;
  1534. supported = true;
  1535. break;
  1536. }
  1537. }
  1538. }
  1539. BX_TRACE("GL_EXTENSION%s: %s", supported ? " (supported)" : "", name);
  1540. BX_UNUSED(supported);
  1541. pos += len+1;
  1542. }
  1543. BX_TRACE("Supported extensions:");
  1544. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  1545. {
  1546. if (s_extension[ii].m_supported)
  1547. {
  1548. BX_TRACE("\t%2d: %s", ii, s_extension[ii].m_name);
  1549. }
  1550. }
  1551. s_renderCtx.m_dxtSupport = true
  1552. && s_extension[Extension::EXT_texture_compression_dxt1].m_supported
  1553. && s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported
  1554. && s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported
  1555. ;
  1556. s_renderCtx.m_dxtSupport |=
  1557. s_extension[Extension::EXT_texture_compression_s3tc].m_supported
  1558. ;
  1559. s_renderCtx.m_vaoSupport = !!BGFX_CONFIG_RENDERER_OPENGLES3
  1560. || s_extension[Extension::ARB_vertex_array_object].m_supported
  1561. || s_extension[Extension::OES_vertex_array_object].m_supported
  1562. ;
  1563. s_renderCtx.m_programBinarySupport = !!BGFX_CONFIG_RENDERER_OPENGLES3
  1564. || s_extension[Extension::ARB_get_program_binary].m_supported
  1565. || s_extension[Extension::OES_get_program_binary].m_supported
  1566. ;
  1567. s_renderCtx.m_textureSwizzleSupport = false
  1568. || s_extension[Extension::ARB_texture_swizzle].m_supported
  1569. || s_extension[Extension::EXT_texture_swizzle].m_supported
  1570. ;
  1571. if (s_extension[Extension::EXT_texture_filter_anisotropic].m_supported)
  1572. {
  1573. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &s_renderCtx.m_maxAnisotropy);
  1574. }
  1575. if (s_extension[Extension::EXT_texture_format_BGRA8888].m_supported)
  1576. {
  1577. s_textureFormat[TextureFormat::BGRX8].m_fmt = GL_BGRA_EXT;
  1578. s_textureFormat[TextureFormat::BGRA8].m_fmt = GL_BGRA_EXT;
  1579. }
  1580. #if !BGFX_CONFIG_RENDERER_OPENGLES3
  1581. if (NULL != glVertexAttribDivisor
  1582. && NULL != glDrawArraysInstanced
  1583. && NULL != glDrawElementsInstanced)
  1584. {
  1585. s_vertexAttribDivisor = glVertexAttribDivisor;
  1586. s_drawArraysInstanced = glDrawArraysInstanced;
  1587. s_drawElementsInstanced = glDrawElementsInstanced;
  1588. }
  1589. else
  1590. {
  1591. s_vertexAttribDivisor = stubVertexAttribDivisor;
  1592. s_drawArraysInstanced = stubDrawArraysInstanced;
  1593. s_drawElementsInstanced = stubDrawElementsInstanced;
  1594. }
  1595. #endif // !BGFX_CONFIG_RENDERER_OPENGLES3
  1596. }
  1597. }
  1598. void Context::rendererShutdown()
  1599. {
  1600. s_renderCtx.shutdown();
  1601. }
  1602. void Context::rendererCreateIndexBuffer(IndexBufferHandle _handle, Memory* _mem)
  1603. {
  1604. s_renderCtx.m_indexBuffers[_handle.idx].create(_mem->size, _mem->data);
  1605. }
  1606. void Context::rendererDestroyIndexBuffer(IndexBufferHandle _handle)
  1607. {
  1608. s_renderCtx.m_indexBuffers[_handle.idx].destroy();
  1609. }
  1610. void Context::rendererCreateVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl)
  1611. {
  1612. VertexDecl& decl = s_renderCtx.m_vertexDecls[_handle.idx];
  1613. memcpy(&decl, &_decl, sizeof(VertexDecl) );
  1614. dump(decl);
  1615. }
  1616. void Context::rendererDestroyVertexDecl(VertexDeclHandle /*_handle*/)
  1617. {
  1618. }
  1619. void Context::rendererCreateVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle)
  1620. {
  1621. s_renderCtx.m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle);
  1622. }
  1623. void Context::rendererDestroyVertexBuffer(VertexBufferHandle _handle)
  1624. {
  1625. s_renderCtx.m_vertexBuffers[_handle.idx].destroy();
  1626. }
  1627. void Context::rendererCreateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size)
  1628. {
  1629. s_renderCtx.m_indexBuffers[_handle.idx].create(_size, NULL);
  1630. }
  1631. void Context::rendererUpdateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem)
  1632. {
  1633. s_renderCtx.m_indexBuffers[_handle.idx].update(_offset, uint32_min(_size, _mem->size), _mem->data);
  1634. }
  1635. void Context::rendererDestroyDynamicIndexBuffer(IndexBufferHandle _handle)
  1636. {
  1637. s_renderCtx.m_indexBuffers[_handle.idx].destroy();
  1638. }
  1639. void Context::rendererCreateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size)
  1640. {
  1641. VertexDeclHandle decl = BGFX_INVALID_HANDLE;
  1642. s_renderCtx.m_vertexBuffers[_handle.idx].create(_size, NULL, decl);
  1643. }
  1644. void Context::rendererUpdateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem)
  1645. {
  1646. s_renderCtx.m_vertexBuffers[_handle.idx].update(_offset, uint32_min(_size, _mem->size), _mem->data);
  1647. }
  1648. void Context::rendererDestroyDynamicVertexBuffer(VertexBufferHandle _handle)
  1649. {
  1650. s_renderCtx.m_vertexBuffers[_handle.idx].destroy();
  1651. }
  1652. void Context::rendererCreateVertexShader(VertexShaderHandle _handle, Memory* _mem)
  1653. {
  1654. s_renderCtx.m_vertexShaders[_handle.idx].create(GL_VERTEX_SHADER, _mem);
  1655. }
  1656. void Context::rendererDestroyVertexShader(VertexShaderHandle _handle)
  1657. {
  1658. s_renderCtx.m_vertexShaders[_handle.idx].destroy();
  1659. }
  1660. void Context::rendererCreateFragmentShader(FragmentShaderHandle _handle, Memory* _mem)
  1661. {
  1662. s_renderCtx.m_fragmentShaders[_handle.idx].create(GL_FRAGMENT_SHADER, _mem);
  1663. }
  1664. void Context::rendererDestroyFragmentShader(FragmentShaderHandle _handle)
  1665. {
  1666. s_renderCtx.m_fragmentShaders[_handle.idx].destroy();
  1667. }
  1668. void Context::rendererCreateProgram(ProgramHandle _handle, VertexShaderHandle _vsh, FragmentShaderHandle _fsh)
  1669. {
  1670. s_renderCtx.m_program[_handle.idx].create(s_renderCtx.m_vertexShaders[_vsh.idx], s_renderCtx.m_fragmentShaders[_fsh.idx]);
  1671. }
  1672. void Context::rendererDestroyProgram(FragmentShaderHandle _handle)
  1673. {
  1674. s_renderCtx.m_program[_handle.idx].destroy();
  1675. }
  1676. void Context::rendererCreateTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags)
  1677. {
  1678. s_renderCtx.m_textures[_handle.idx].create(_mem, _flags);
  1679. }
  1680. void Context::rendererUpdateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem)
  1681. {
  1682. s_renderCtx.m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _mem);
  1683. }
  1684. void Context::rendererDestroyTexture(TextureHandle _handle)
  1685. {
  1686. s_renderCtx.m_textures[_handle.idx].destroy();
  1687. }
  1688. void Context::rendererCreateRenderTarget(RenderTargetHandle _handle, uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags)
  1689. {
  1690. s_renderCtx.m_renderTargets[_handle.idx].create(_width, _height, _flags, _textureFlags);
  1691. }
  1692. void Context::rendererDestroyRenderTarget(RenderTargetHandle _handle)
  1693. {
  1694. s_renderCtx.m_renderTargets[_handle.idx].destroy();
  1695. }
  1696. void Context::rendererCreateUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name)
  1697. {
  1698. uint32_t size = g_uniformTypeSize[_type]*_num;
  1699. void* data = g_realloc(NULL, size);
  1700. memset(data, 0, size);
  1701. s_renderCtx.m_uniforms[_handle.idx] = data;
  1702. s_renderCtx.m_uniformReg.add(_name, s_renderCtx.m_uniforms[_handle.idx]);
  1703. }
  1704. void Context::rendererDestroyUniform(UniformHandle _handle)
  1705. {
  1706. g_free(s_renderCtx.m_uniforms[_handle.idx]);
  1707. }
  1708. void Context::rendererSaveScreenShot(Memory* _mem)
  1709. {
  1710. s_renderCtx.saveScreenShot(_mem);
  1711. }
  1712. void Context::rendererUpdateUniform(uint16_t _loc, const void* _data, uint32_t _size)
  1713. {
  1714. memcpy(s_renderCtx.m_uniforms[_loc], _data, _size);
  1715. }
  1716. void Context::rendererSubmit()
  1717. {
  1718. s_renderCtx.updateResolution(m_render->m_resolution);
  1719. int64_t elapsed = -bx::getHPCounter();
  1720. int64_t captureElapsed = 0;
  1721. #if BGFX_CONFIG_RENDERER_OPENGL
  1722. if (m_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  1723. {
  1724. s_renderCtx.m_queries.begin(0, GL_TIME_ELAPSED);
  1725. }
  1726. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1727. if (0 < m_render->m_iboffset)
  1728. {
  1729. TransientIndexBuffer* ib = m_render->m_transientIb;
  1730. s_renderCtx.m_indexBuffers[ib->handle.idx].update(0, m_render->m_iboffset, ib->data);
  1731. }
  1732. if (0 < m_render->m_vboffset)
  1733. {
  1734. TransientVertexBuffer* vb = m_render->m_transientVb;
  1735. s_renderCtx.m_vertexBuffers[vb->handle.idx].update(0, m_render->m_vboffset, vb->data);
  1736. }
  1737. m_render->sort();
  1738. RenderState currentState;
  1739. currentState.reset();
  1740. currentState.m_flags = BGFX_STATE_NONE;
  1741. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  1742. Matrix4 viewProj[BGFX_CONFIG_MAX_VIEWS];
  1743. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  1744. {
  1745. mtxMul(viewProj[ii].val, m_render->m_view[ii].val, m_render->m_proj[ii].val);
  1746. }
  1747. uint16_t programIdx = invalidHandle;
  1748. SortKey key;
  1749. uint8_t view = 0xff;
  1750. RenderTargetHandle rt = BGFX_INVALID_HANDLE;
  1751. int32_t height = m_render->m_resolution.m_height;
  1752. float alphaRef = 0.0f;
  1753. GLenum primType = m_render->m_debug&BGFX_DEBUG_WIREFRAME ? GL_LINES : GL_TRIANGLES;
  1754. uint32_t primNumVerts = 3;
  1755. uint32_t baseVertex = 0;
  1756. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1757. uint32_t statsNumPrimsSubmitted = 0;
  1758. uint32_t statsNumIndices = 0;
  1759. uint32_t statsNumInstances = 0;
  1760. uint32_t statsNumPrimsRendered = 0;
  1761. if (0 == (m_render->m_debug&BGFX_DEBUG_IFH) )
  1762. {
  1763. for (uint32_t item = 0, numItems = m_render->m_num; item < numItems; ++item)
  1764. {
  1765. key.decode(m_render->m_sortKeys[item]);
  1766. const RenderState& state = m_render->m_renderState[m_render->m_sortValues[item] ];
  1767. const uint64_t newFlags = state.m_flags;
  1768. uint64_t changedFlags = currentState.m_flags ^ state.m_flags;
  1769. currentState.m_flags = newFlags;
  1770. const uint64_t newStencil = state.m_stencil;
  1771. uint64_t changedStencil = currentState.m_stencil ^ state.m_stencil;
  1772. currentState.m_stencil = newStencil;
  1773. if (key.m_view != view)
  1774. {
  1775. currentState.clear();
  1776. changedFlags = BGFX_STATE_MASK;
  1777. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  1778. currentState.m_flags = newFlags;
  1779. GREMEDY_SETMARKER("view");
  1780. view = key.m_view;
  1781. programIdx = invalidHandle;
  1782. if (m_render->m_rt[view].idx != rt.idx)
  1783. {
  1784. rt = m_render->m_rt[view];
  1785. if (rt.idx == invalidHandle)
  1786. {
  1787. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1788. height = m_render->m_resolution.m_height;
  1789. }
  1790. else
  1791. {
  1792. RenderTarget& renderTarget = s_renderCtx.m_renderTargets[rt.idx];
  1793. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, renderTarget.m_fbo) );
  1794. height = renderTarget.m_height;
  1795. }
  1796. }
  1797. Rect& rect = m_render->m_rect[view];
  1798. GL_CHECK(glViewport(rect.m_x, height-rect.m_height-rect.m_y, rect.m_width, rect.m_height) );
  1799. Clear& clear = m_render->m_clear[view];
  1800. if (BGFX_CLEAR_NONE != clear.m_flags)
  1801. {
  1802. GLuint flags = 0;
  1803. if (BGFX_CLEAR_COLOR_BIT & clear.m_flags)
  1804. {
  1805. flags |= GL_COLOR_BUFFER_BIT;
  1806. uint32_t rgba = clear.m_rgba;
  1807. float rr = (rgba>>24)/255.0f;
  1808. float gg = ( (rgba>>16)&0xff)/255.0f;
  1809. float bb = ( (rgba>>8)&0xff)/255.0f;
  1810. float aa = (rgba&0xff)/255.0f;
  1811. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  1812. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  1813. }
  1814. if (BGFX_CLEAR_DEPTH_BIT & clear.m_flags)
  1815. {
  1816. flags |= GL_DEPTH_BUFFER_BIT;
  1817. GL_CHECK(glClearDepth(clear.m_depth) );
  1818. GL_CHECK(glDepthMask(GL_TRUE) );
  1819. }
  1820. if (BGFX_CLEAR_STENCIL_BIT & clear.m_flags)
  1821. {
  1822. flags |= GL_STENCIL_BUFFER_BIT;
  1823. GL_CHECK(glClearStencil(clear.m_stencil) );
  1824. }
  1825. if (0 != flags)
  1826. {
  1827. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  1828. GL_CHECK(glScissor(rect.m_x, height-rect.m_height-rect.m_y, rect.m_width, rect.m_height) );
  1829. GL_CHECK(glClear(flags) );
  1830. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  1831. }
  1832. }
  1833. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  1834. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  1835. GL_CHECK(glDepthFunc(GL_LESS) );
  1836. GL_CHECK(glEnable(GL_CULL_FACE) );
  1837. GL_CHECK(glDisable(GL_BLEND) );
  1838. }
  1839. if (0 != changedStencil)
  1840. {
  1841. if (0 != newStencil)
  1842. {
  1843. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  1844. uint32_t bstencil = unpackStencil(1, newStencil);
  1845. uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != unpackStencil(0, newStencil);
  1846. // uint32_t bchanged = unpackStencil(1, changedStencil);
  1847. // if (BGFX_STENCIL_FUNC_RMASK_MASK & bchanged)
  1848. // {
  1849. // uint32_t wmask = (bstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  1850. // GL_CHECK(glStencilMask(wmask) );
  1851. // }
  1852. for (uint32_t ii = 0, num = frontAndBack+1; ii < num; ++ii)
  1853. {
  1854. uint32_t stencil = unpackStencil(ii, newStencil);
  1855. uint32_t changed = unpackStencil(ii, changedStencil);
  1856. GLenum face = s_stencilFace[frontAndBack+ii];
  1857. if ( (BGFX_STENCIL_TEST_MASK|BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & changed)
  1858. {
  1859. GLint ref = (stencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  1860. GLint mask = (stencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  1861. uint32_t func = (stencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT;
  1862. GL_CHECK(glStencilFuncSeparate(face, s_stencilFunc[func], ref, mask));
  1863. }
  1864. if ( (BGFX_STENCIL_OP_FAIL_S_MASK|BGFX_STENCIL_OP_FAIL_Z_MASK|BGFX_STENCIL_OP_PASS_Z_MASK) & changed)
  1865. {
  1866. uint32_t sfail = (stencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT;
  1867. uint32_t zfail = (stencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT;
  1868. uint32_t zpass = (stencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT;
  1869. GL_CHECK(glStencilOpSeparate(face, s_stencilOp[sfail], s_stencilOp[zfail], s_stencilOp[zpass]) );
  1870. }
  1871. }
  1872. }
  1873. else
  1874. {
  1875. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  1876. }
  1877. }
  1878. if ( (BGFX_STATE_CULL_MASK|BGFX_STATE_DEPTH_WRITE|BGFX_STATE_DEPTH_TEST_MASK
  1879. |BGFX_STATE_ALPHA_MASK|BGFX_STATE_RGB_WRITE|BGFX_STATE_BLEND_MASK
  1880. |BGFX_STATE_ALPHA_REF_MASK|BGFX_STATE_PT_MASK|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  1881. {
  1882. if (BGFX_STATE_CULL_MASK & changedFlags)
  1883. {
  1884. if (BGFX_STATE_CULL_CW & newFlags)
  1885. {
  1886. GL_CHECK(glEnable(GL_CULL_FACE) );
  1887. GL_CHECK(glCullFace(GL_BACK) );
  1888. }
  1889. else if (BGFX_STATE_CULL_CCW & newFlags)
  1890. {
  1891. GL_CHECK(glEnable(GL_CULL_FACE) );
  1892. GL_CHECK(glCullFace(GL_FRONT) );
  1893. }
  1894. else
  1895. {
  1896. GL_CHECK(glDisable(GL_CULL_FACE) );
  1897. }
  1898. }
  1899. if (BGFX_STATE_DEPTH_WRITE & changedFlags)
  1900. {
  1901. GL_CHECK(glDepthMask(!!(BGFX_STATE_DEPTH_WRITE & newFlags) ) );
  1902. }
  1903. if (BGFX_STATE_DEPTH_TEST_MASK & changedFlags)
  1904. {
  1905. uint32_t func = (newFlags&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  1906. if (0 != func)
  1907. {
  1908. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  1909. GL_CHECK(glDepthFunc(s_depthFunc[func]) );
  1910. }
  1911. else
  1912. {
  1913. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  1914. }
  1915. }
  1916. if ( (BGFX_STATE_ALPHA_TEST|BGFX_STATE_ALPHA_REF_MASK) & changedFlags)
  1917. {
  1918. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  1919. alphaRef = ref/255.0f;
  1920. #if BGFX_CONFIG_RENDERER_OPENGL
  1921. if (BGFX_STATE_ALPHA_TEST & newFlags)
  1922. {
  1923. GL_CHECK(glEnable(GL_ALPHA_TEST) );
  1924. }
  1925. else
  1926. {
  1927. GL_CHECK(glDisable(GL_ALPHA_TEST) );
  1928. }
  1929. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1930. }
  1931. #if BGFX_CONFIG_RENDERER_OPENGL
  1932. if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  1933. {
  1934. float pointSize = (float)(uint32_max(1, (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) );
  1935. GL_CHECK(glPointSize(pointSize) );
  1936. }
  1937. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1938. if ( (BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE) & changedFlags)
  1939. {
  1940. GLboolean alpha = !!(newFlags&BGFX_STATE_ALPHA_WRITE);
  1941. GLboolean rgb = !!(newFlags&BGFX_STATE_RGB_WRITE);
  1942. GL_CHECK(glColorMask(rgb, rgb, rgb, alpha) );
  1943. }
  1944. if (BGFX_STATE_BLEND_MASK & changedFlags)
  1945. {
  1946. if (BGFX_STATE_BLEND_MASK & newFlags)
  1947. {
  1948. uint32_t blend = (newFlags&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT;
  1949. uint32_t src = blend&0xf;
  1950. uint32_t dst = (blend>>4)&0xf;
  1951. GL_CHECK(glEnable(GL_BLEND) );
  1952. GL_CHECK(glBlendFunc(s_blendFactor[src][0], s_blendFactor[dst][1]) );
  1953. }
  1954. else
  1955. {
  1956. GL_CHECK(glDisable(GL_BLEND) );
  1957. }
  1958. }
  1959. uint8_t primIndex = uint8_t( (newFlags&BGFX_STATE_PT_MASK)>>BGFX_STATE_PT_SHIFT);
  1960. primType = m_render->m_debug&BGFX_DEBUG_WIREFRAME ? GL_LINES : s_primType[primIndex];
  1961. primNumVerts = 3-primIndex;
  1962. }
  1963. bool programChanged = false;
  1964. bool constantsChanged = state.m_constBegin < state.m_constEnd;
  1965. bool bindAttribs = false;
  1966. rendererUpdateUniforms(m_render->m_constantBuffer, state.m_constBegin, state.m_constEnd);
  1967. if (key.m_program != programIdx)
  1968. {
  1969. programIdx = key.m_program;
  1970. GLuint id = invalidHandle == programIdx ? 0 : s_renderCtx.m_program[programIdx].m_id;
  1971. GL_CHECK(glUseProgram(id) );
  1972. programChanged =
  1973. constantsChanged =
  1974. bindAttribs = true;
  1975. }
  1976. if (invalidHandle != programIdx)
  1977. {
  1978. Program& program = s_renderCtx.m_program[programIdx];
  1979. if (constantsChanged)
  1980. {
  1981. program.commit();
  1982. }
  1983. for (uint32_t ii = 0, num = program.m_numPredefined; ii < num; ++ii)
  1984. {
  1985. PredefinedUniform& predefined = program.m_predefined[ii];
  1986. switch (predefined.m_type)
  1987. {
  1988. case PredefinedUniform::ViewRect:
  1989. {
  1990. float rect[4];
  1991. rect[0] = m_render->m_rect[view].m_x;
  1992. rect[1] = m_render->m_rect[view].m_y;
  1993. rect[2] = m_render->m_rect[view].m_width;
  1994. rect[3] = m_render->m_rect[view].m_height;
  1995. GL_CHECK(glUniform4fv(predefined.m_loc
  1996. , 1
  1997. , &rect[0]
  1998. ) );
  1999. }
  2000. break;
  2001. case PredefinedUniform::ViewTexel:
  2002. {
  2003. float rect[4];
  2004. rect[0] = 1.0f/float(m_render->m_rect[view].m_width);
  2005. rect[1] = 1.0f/float(m_render->m_rect[view].m_height);
  2006. GL_CHECK(glUniform4fv(predefined.m_loc
  2007. , 1
  2008. , &rect[0]
  2009. ) );
  2010. }
  2011. break;
  2012. case PredefinedUniform::View:
  2013. {
  2014. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2015. , 1
  2016. , GL_FALSE
  2017. , m_render->m_view[view].val
  2018. ) );
  2019. }
  2020. break;
  2021. case PredefinedUniform::ViewProj:
  2022. {
  2023. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2024. , 1
  2025. , GL_FALSE
  2026. , viewProj[view].val
  2027. ) );
  2028. }
  2029. break;
  2030. case PredefinedUniform::Model:
  2031. {
  2032. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2033. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2034. , uint32_min(predefined.m_count, state.m_num)
  2035. , GL_FALSE
  2036. , model.val
  2037. ) );
  2038. }
  2039. break;
  2040. case PredefinedUniform::ModelView:
  2041. {
  2042. Matrix4 modelView;
  2043. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2044. mtxMul(modelView.val, model.val, m_render->m_view[view].val);
  2045. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2046. , 1
  2047. , GL_FALSE
  2048. , modelView.val
  2049. ) );
  2050. }
  2051. break;
  2052. case PredefinedUniform::ModelViewProj:
  2053. {
  2054. Matrix4 modelViewProj;
  2055. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2056. mtxMul(modelViewProj.val, model.val, viewProj[view].val);
  2057. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2058. , 1
  2059. , GL_FALSE
  2060. , modelViewProj.val
  2061. ) );
  2062. }
  2063. break;
  2064. case PredefinedUniform::ModelViewProjX:
  2065. {
  2066. const Matrix4& model = m_render->m_matrixCache.m_cache[state.m_matrix];
  2067. static const BX_ALIGN_STRUCT_16(float) s_bias[16] =
  2068. {
  2069. 0.5f, 0.0f, 0.0f, 0.0f,
  2070. 0.0f, 0.5f, 0.0f, 0.0f,
  2071. 0.0f, 0.0f, 0.5f, 0.0f,
  2072. 0.5f, 0.5f, 0.5f, 1.0f,
  2073. };
  2074. uint8_t other = m_render->m_other[view];
  2075. Matrix4 viewProjBias;
  2076. mtxMul(viewProjBias.val, viewProj[other].val, s_bias);
  2077. Matrix4 modelViewProj;
  2078. mtxMul(modelViewProj.val, model.val, viewProjBias.val);
  2079. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2080. , 1
  2081. , GL_FALSE
  2082. , modelViewProj.val
  2083. ) );
  2084. }
  2085. break;
  2086. case PredefinedUniform::ViewProjX:
  2087. {
  2088. static const BX_ALIGN_STRUCT_16(float) s_bias[16] =
  2089. {
  2090. 0.5f, 0.0f, 0.0f, 0.0f,
  2091. 0.0f, 0.5f, 0.0f, 0.0f,
  2092. 0.0f, 0.0f, 0.5f, 0.0f,
  2093. 0.5f, 0.5f, 0.5f, 1.0f,
  2094. };
  2095. uint8_t other = m_render->m_other[view];
  2096. Matrix4 viewProjBias;
  2097. mtxMul(viewProjBias.val, viewProj[other].val, s_bias);
  2098. GL_CHECK(glUniformMatrix4fv(predefined.m_loc
  2099. , 1
  2100. , GL_FALSE
  2101. , viewProjBias.val
  2102. ) );
  2103. }
  2104. break;
  2105. case PredefinedUniform::AlphaRef:
  2106. {
  2107. GL_CHECK(glUniform1f(predefined.m_loc, alphaRef) );
  2108. }
  2109. break;
  2110. case PredefinedUniform::Count:
  2111. break;
  2112. }
  2113. }
  2114. // if (BGFX_STATE_TEX_MASK & changedFlags)
  2115. {
  2116. uint64_t flag = BGFX_STATE_TEX0;
  2117. for (uint32_t stage = 0; stage < BGFX_STATE_TEX_COUNT; ++stage)
  2118. {
  2119. const Sampler& sampler = state.m_sampler[stage];
  2120. Sampler& current = currentState.m_sampler[stage];
  2121. if (current.m_idx != sampler.m_idx
  2122. || current.m_flags != sampler.m_flags
  2123. || programChanged)
  2124. {
  2125. if (invalidHandle != sampler.m_idx)
  2126. {
  2127. GL_CHECK(glActiveTexture(GL_TEXTURE0+stage) );
  2128. switch (sampler.m_flags&BGFX_SAMPLER_TYPE_MASK)
  2129. {
  2130. case BGFX_SAMPLER_TEXTURE:
  2131. {
  2132. const Texture& texture = s_renderCtx.m_textures[sampler.m_idx];
  2133. GL_CHECK(glBindTexture(texture.m_target, texture.m_id) );
  2134. }
  2135. break;
  2136. case BGFX_SAMPLER_RENDERTARGET_COLOR:
  2137. {
  2138. const RenderTarget& rt = s_renderCtx.m_renderTargets[sampler.m_idx];
  2139. GL_CHECK(glBindTexture(rt.m_color.m_target, rt.m_color.m_id) );
  2140. }
  2141. break;
  2142. case BGFX_SAMPLER_RENDERTARGET_DEPTH:
  2143. {
  2144. const RenderTarget& rt = s_renderCtx.m_renderTargets[sampler.m_idx];
  2145. GL_CHECK(glBindTexture(rt.m_depth.m_target, rt.m_depth.m_id) );
  2146. }
  2147. break;
  2148. }
  2149. }
  2150. }
  2151. current = sampler;
  2152. flag <<= 1;
  2153. }
  2154. }
  2155. if (s_renderCtx.m_vaoSupport)
  2156. {
  2157. if (programChanged
  2158. || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx
  2159. || baseVertex != state.m_startVertex
  2160. || currentState.m_indexBuffer.idx != state.m_indexBuffer.idx)
  2161. {
  2162. uint64_t hash = (uint64_t(state.m_vertexBuffer.idx)<<48)
  2163. | (uint64_t(state.m_indexBuffer.idx)<<32)
  2164. | (uint64_t(state.m_instanceDataBuffer.idx)<<16)
  2165. | programIdx
  2166. ;
  2167. hash ^= state.m_startVertex;
  2168. currentState.m_vertexBuffer = state.m_vertexBuffer;
  2169. currentState.m_indexBuffer = state.m_indexBuffer;
  2170. baseVertex = state.m_startVertex;
  2171. GLuint id = s_renderCtx.m_vaoCache.find(hash);
  2172. if (UINT32_MAX != id)
  2173. {
  2174. GL_CHECK(glBindVertexArray(id) );
  2175. }
  2176. else
  2177. {
  2178. id = s_renderCtx.m_vaoCache.add(hash);
  2179. GL_CHECK(glBindVertexArray(id) );
  2180. if (invalidHandle != state.m_vertexBuffer.idx)
  2181. {
  2182. VertexBuffer& vb = s_renderCtx.m_vertexBuffers[state.m_vertexBuffer.idx];
  2183. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  2184. uint16_t decl = vb.m_decl.idx == invalidHandle ? state.m_vertexDecl.idx : vb.m_decl.idx;
  2185. const Program& program = s_renderCtx.m_program[programIdx];
  2186. program.bindAttributes(s_renderCtx.m_vertexDecls[decl], state.m_startVertex);
  2187. if (invalidHandle != state.m_instanceDataBuffer.idx)
  2188. {
  2189. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, s_renderCtx.m_vertexBuffers[state.m_instanceDataBuffer.idx].m_id) );
  2190. program.bindInstanceData(state.m_instanceDataStride, state.m_instanceDataOffset);
  2191. }
  2192. }
  2193. else
  2194. {
  2195. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  2196. }
  2197. if (invalidHandle != state.m_indexBuffer.idx)
  2198. {
  2199. IndexBuffer& ib = s_renderCtx.m_indexBuffers[state.m_indexBuffer.idx];
  2200. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  2201. }
  2202. else
  2203. {
  2204. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  2205. }
  2206. }
  2207. }
  2208. }
  2209. else
  2210. {
  2211. if (programChanged
  2212. || currentState.m_vertexBuffer.idx != state.m_vertexBuffer.idx)
  2213. {
  2214. currentState.m_vertexBuffer = state.m_vertexBuffer;
  2215. uint16_t handle = state.m_vertexBuffer.idx;
  2216. if (invalidHandle != handle)
  2217. {
  2218. VertexBuffer& vb = s_renderCtx.m_vertexBuffers[handle];
  2219. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  2220. bindAttribs = true;
  2221. }
  2222. else
  2223. {
  2224. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  2225. }
  2226. }
  2227. if (currentState.m_indexBuffer.idx != state.m_indexBuffer.idx)
  2228. {
  2229. currentState.m_indexBuffer = state.m_indexBuffer;
  2230. uint16_t handle = state.m_indexBuffer.idx;
  2231. if (invalidHandle != handle)
  2232. {
  2233. IndexBuffer& ib = s_renderCtx.m_indexBuffers[handle];
  2234. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  2235. }
  2236. else
  2237. {
  2238. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  2239. }
  2240. }
  2241. if (invalidHandle != currentState.m_vertexBuffer.idx)
  2242. {
  2243. if (baseVertex != state.m_startVertex
  2244. || bindAttribs)
  2245. {
  2246. baseVertex = state.m_startVertex;
  2247. const VertexBuffer& vb = s_renderCtx.m_vertexBuffers[state.m_vertexBuffer.idx];
  2248. uint16_t decl = vb.m_decl.idx == invalidHandle ? state.m_vertexDecl.idx : vb.m_decl.idx;
  2249. const Program& program = s_renderCtx.m_program[programIdx];
  2250. program.bindAttributes(s_renderCtx.m_vertexDecls[decl], state.m_startVertex);
  2251. if (invalidHandle != state.m_instanceDataBuffer.idx)
  2252. {
  2253. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, s_renderCtx.m_vertexBuffers[state.m_instanceDataBuffer.idx].m_id) );
  2254. program.bindInstanceData(state.m_instanceDataStride, state.m_instanceDataOffset);
  2255. }
  2256. }
  2257. }
  2258. }
  2259. if (invalidHandle != currentState.m_vertexBuffer.idx)
  2260. {
  2261. uint32_t numVertices = state.m_numVertices;
  2262. if (UINT32_C(0xffffffff) == numVertices)
  2263. {
  2264. const VertexBuffer& vb = s_renderCtx.m_vertexBuffers[currentState.m_vertexBuffer.idx];
  2265. uint16_t decl = vb.m_decl.idx == invalidHandle ? state.m_vertexDecl.idx : vb.m_decl.idx;
  2266. const VertexDecl& vertexDecl = s_renderCtx.m_vertexDecls[decl];
  2267. numVertices = vb.m_size/vertexDecl.m_stride;
  2268. }
  2269. uint32_t numIndices = 0;
  2270. uint32_t numPrimsSubmitted = 0;
  2271. uint32_t numInstances = 0;
  2272. uint32_t numPrimsRendered = 0;
  2273. if (invalidHandle != state.m_indexBuffer.idx)
  2274. {
  2275. if (UINT32_MAX == state.m_numIndices)
  2276. {
  2277. numIndices = s_renderCtx.m_indexBuffers[state.m_indexBuffer.idx].m_size/2;
  2278. numPrimsSubmitted = numIndices/primNumVerts;
  2279. numInstances = state.m_numInstances;
  2280. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2281. GL_CHECK(s_drawElementsInstanced(primType
  2282. , numIndices
  2283. , GL_UNSIGNED_SHORT
  2284. , (void*)0
  2285. , state.m_numInstances
  2286. ) );
  2287. }
  2288. else if (primNumVerts <= state.m_numIndices)
  2289. {
  2290. numIndices = state.m_numIndices;
  2291. numPrimsSubmitted = numIndices/primNumVerts;
  2292. numInstances = state.m_numInstances;
  2293. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2294. GL_CHECK(s_drawElementsInstanced(primType
  2295. , numIndices
  2296. , GL_UNSIGNED_SHORT
  2297. , (void*)(uintptr_t)(state.m_startIndex*2)
  2298. , state.m_numInstances
  2299. ) );
  2300. }
  2301. }
  2302. else
  2303. {
  2304. numPrimsSubmitted = numVertices/primNumVerts;
  2305. numInstances = state.m_numInstances;
  2306. numPrimsRendered = numPrimsSubmitted*state.m_numInstances;
  2307. GL_CHECK(s_drawArraysInstanced(primType
  2308. , 0
  2309. , numVertices
  2310. , state.m_numInstances
  2311. ) );
  2312. }
  2313. statsNumPrimsSubmitted += numPrimsSubmitted;
  2314. statsNumIndices += numIndices;
  2315. statsNumInstances += numInstances;
  2316. statsNumPrimsRendered += numPrimsRendered;
  2317. }
  2318. }
  2319. }
  2320. if (0 < m_render->m_num)
  2321. {
  2322. captureElapsed = -bx::getHPCounter();
  2323. s_renderCtx.capture();
  2324. captureElapsed += bx::getHPCounter();
  2325. }
  2326. }
  2327. int64_t now = bx::getHPCounter();
  2328. elapsed += now;
  2329. static int64_t last = now;
  2330. int64_t frameTime = now - last;
  2331. last = now;
  2332. static int64_t min = frameTime;
  2333. static int64_t max = frameTime;
  2334. min = min > frameTime ? frameTime : min;
  2335. max = max < frameTime ? frameTime : max;
  2336. if (m_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  2337. {
  2338. double elapsedGpuMs = 0.0;
  2339. #if BGFX_CONFIG_RENDERER_OPENGL
  2340. s_renderCtx.m_queries.end(GL_TIME_ELAPSED);
  2341. uint64_t elapsedGl = s_renderCtx.m_queries.getResult(0);
  2342. elapsedGpuMs = double(elapsedGl)/1e6;
  2343. #endif // BGFX_CONFIG_RENDERER_OPENGL
  2344. TextVideoMem& tvm = s_renderCtx.m_textVideoMem;
  2345. static int64_t next = now;
  2346. if (now >= next)
  2347. {
  2348. next = now + bx::getHPFrequency();
  2349. double freq = double(bx::getHPFrequency() );
  2350. double toMs = 1000.0/freq;
  2351. tvm.clear();
  2352. uint16_t pos = 10;
  2353. tvm.printf(0, 0, 0x8f, " " BGFX_RENDERER_NAME " ");
  2354. tvm.printf(10, pos++, 0x8e, " Frame CPU: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS"
  2355. , double(frameTime)*toMs
  2356. , double(min)*toMs
  2357. , double(max)*toMs
  2358. , freq/frameTime
  2359. );
  2360. double elapsedCpuMs = double(elapsed)*toMs;
  2361. tvm.printf(10, pos++, 0x8e, " Draw calls: %4d / CPU %3.4f [ms] %c GPU %3.4f [ms]"
  2362. , m_render->m_num
  2363. , elapsedCpuMs
  2364. , elapsedCpuMs > elapsedGpuMs ? '>' : '<'
  2365. , elapsedGpuMs
  2366. );
  2367. tvm.printf(10, pos++, 0x8e, " Prims: %7d (#inst: %5d), submitted: %7d"
  2368. , statsNumPrimsRendered
  2369. , statsNumInstances
  2370. , statsNumPrimsSubmitted
  2371. );
  2372. double captureMs = double(captureElapsed)*toMs;
  2373. tvm.printf(10, pos++, 0x8e, " Capture: %3.4f [ms]", captureMs);
  2374. tvm.printf(10, pos++, 0x8e, " Indices: %7d", statsNumIndices);
  2375. tvm.printf(10, pos++, 0x8e, " DVB size: %7d", m_render->m_vboffset);
  2376. tvm.printf(10, pos++, 0x8e, " DIB size: %7d", m_render->m_iboffset);
  2377. #if BGFX_CONFIG_RENDERER_OPENGL
  2378. if (s_extension[Extension::ATI_meminfo].m_supported)
  2379. {
  2380. GLint vboFree[4];
  2381. GL_CHECK(glGetIntegerv(GL_VBO_FREE_MEMORY_ATI, vboFree) );
  2382. GLint texFree[4];
  2383. GL_CHECK(glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, texFree) );
  2384. GLint rbfFree[4];
  2385. GL_CHECK(glGetIntegerv(GL_RENDERBUFFER_FREE_MEMORY_ATI, rbfFree) );
  2386. pos++;
  2387. tvm.printf(10, pos++, 0x8c, " -------------| free| free b| aux| aux fb");
  2388. tvm.printf(10, pos++, 0x8e, " VBO: %7d, %7d, %7d, %7d", vboFree[0], vboFree[1], vboFree[2], vboFree[3]);
  2389. tvm.printf(10, pos++, 0x8e, " Texture: %7d, %7d, %7d, %7d", texFree[0], texFree[1], texFree[2], texFree[3]);
  2390. tvm.printf(10, pos++, 0x8e, " Render Buffer: %7d, %7d, %7d, %7d", rbfFree[0], rbfFree[1], rbfFree[2], rbfFree[3]);
  2391. }
  2392. else if (s_extension[Extension::NVX_gpu_memory_info].m_supported)
  2393. {
  2394. GLint dedicated;
  2395. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dedicated) );
  2396. GLint totalAvail;
  2397. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalAvail) );
  2398. GLint currAvail;
  2399. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &currAvail) );
  2400. GLint evictedCount;
  2401. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX, &evictedCount) );
  2402. GLint evictedMemory;
  2403. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX, &evictedMemory) );
  2404. pos++;
  2405. tvm.printf(10, pos++, 0x8c, "----------|");
  2406. tvm.printf(10, pos++, 0x8e, " Dedicated: %7d", dedicated);
  2407. tvm.printf(10, pos++, 0x8e, " Available: %7d (%7d)", currAvail, totalAvail);
  2408. tvm.printf(10, pos++, 0x8e, " Eviction: %7d / %7d", evictedCount, evictedMemory);
  2409. }
  2410. #endif // BGFX_CONFIG_RENDERER_OPENGL
  2411. uint8_t attr[2] = { 0x89, 0x8a };
  2412. uint8_t attrIndex = m_render->m_waitSubmit < m_render->m_waitRender;
  2413. pos++;
  2414. tvm.printf(10, pos++, attr[attrIndex&1], "Submit wait: %3.4f [ms]", double(m_render->m_waitSubmit)*toMs);
  2415. tvm.printf(10, pos++, attr[(attrIndex+1)&1], "Render wait: %3.4f [ms]", double(m_render->m_waitRender)*toMs);
  2416. min = frameTime;
  2417. max = frameTime;
  2418. }
  2419. m_textVideoMemBlitter.blit(tvm);
  2420. }
  2421. else if (m_render->m_debug & BGFX_DEBUG_TEXT)
  2422. {
  2423. m_textVideoMemBlitter.blit(m_render->m_textVideoMem);
  2424. }
  2425. GREMEDY_FRAMETERMINATOR();
  2426. }
  2427. }
  2428. #endif // (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)