renderer_gl.cpp 78 KB

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