update.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <bx/uint32_t.h>
  8. #include "packrect.h"
  9. #include <list>
  10. struct PosTexcoordVertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. float m_u;
  16. float m_v;
  17. float m_w;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float)
  24. .end();
  25. };
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosTexcoordVertex::ms_decl;
  29. static PosTexcoordVertex s_cubeVertices[28] =
  30. {
  31. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  32. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  33. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  34. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  35. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  36. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  37. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  38. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  39. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  40. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  41. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  42. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  43. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  44. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  45. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  46. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  47. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  48. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  49. {-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f },
  50. { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f },
  51. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  52. {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f },
  53. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  54. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  55. {-1.0f, 1.0f, 1.0f, -2.0f, 2.0f, 2.0f },
  56. { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f },
  57. {-1.0f, -1.0f, 1.0f, -2.0f, -2.0f, 2.0f },
  58. { 1.0f, -1.0f, 1.0f, 2.0f, -2.0f, 2.0f },
  59. };
  60. static const uint16_t s_cubeIndices[36] =
  61. {
  62. 0, 1, 2, // 0
  63. 1, 3, 2,
  64. 4, 6, 5, // 2
  65. 5, 6, 7,
  66. 8, 10, 9, // 4
  67. 9, 10, 11,
  68. 12, 14, 13, // 6
  69. 14, 15, 13,
  70. 16, 18, 17, // 8
  71. 18, 19, 17,
  72. 20, 22, 21, // 10
  73. 21, 22, 23,
  74. };
  75. static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _side, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height, uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a = 0xff)
  76. {
  77. bgfx::TextureInfo ti;
  78. bgfx::calcTextureSize(ti, _width, _height, 1, 1, false, bgfx::TextureFormat::BGRA8);
  79. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  80. uint8_t* data = (uint8_t*)mem->data;
  81. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  82. {
  83. data[0] = _b;
  84. data[1] = _g;
  85. data[2] = _r;
  86. data[3] = _a;
  87. data += 4;
  88. }
  89. bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
  90. }
  91. int _main_(int /*_argc*/, char** /*_argv*/)
  92. {
  93. uint32_t width = 1280;
  94. uint32_t height = 720;
  95. uint32_t debug = BGFX_DEBUG_TEXT;
  96. uint32_t reset = BGFX_RESET_VSYNC;
  97. bgfx::init();
  98. bgfx::reset(width, height, reset);
  99. // Enable debug text.
  100. bgfx::setDebug(debug);
  101. // Set view 0 clear state.
  102. bgfx::setViewClear(0
  103. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  104. , 0x303030ff
  105. , 1.0f
  106. , 0
  107. );
  108. // Create vertex stream declaration.
  109. PosTexcoordVertex::init();
  110. bgfx::TextureHandle textures[] =
  111. {
  112. loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP),
  113. loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP),
  114. loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP),
  115. loadTexture("texture_compression_etc1.ktx", BGFX_TEXTURE_U_BORDER|BGFX_TEXTURE_V_BORDER|BGFX_TEXTURE_BORDER_COLOR(1) ),
  116. loadTexture("texture_compression_etc2.ktx"),
  117. loadTexture("texture_compression_ptc12.pvr"),
  118. loadTexture("texture_compression_ptc14.pvr"),
  119. loadTexture("texture_compression_ptc22.pvr"),
  120. loadTexture("texture_compression_ptc24.pvr"),
  121. };
  122. const bgfx::Memory* mem8 = bgfx::alloc(32*32*32);
  123. const bgfx::Memory* mem16f = bgfx::alloc(32*32*32*2);
  124. const bgfx::Memory* mem32f = bgfx::alloc(32*32*32*4);
  125. for (uint8_t zz = 0; zz < 32; ++zz)
  126. {
  127. for (uint8_t yy = 0; yy < 32; ++yy)
  128. {
  129. for (uint8_t xx = 0; xx < 32; ++xx)
  130. {
  131. const uint32_t offset = ( (zz*32+yy)*32+xx);
  132. const uint32_t val = xx ^ yy ^ zz;
  133. mem8->data[offset] = val<<3;
  134. *(uint16_t*)&mem16f->data[offset*2] = bx::halfFromFloat( (float)val/32.0f);
  135. *(float*)&mem32f->data[offset*4] = (float)val/32.0f;
  136. }
  137. }
  138. }
  139. const bgfx::Caps* caps = bgfx::getCaps();
  140. const bool texture3DSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_3D);
  141. const bool blitSupported = !!(caps->supported & BGFX_CAPS_TEXTURE_BLIT);
  142. uint32_t numTextures3d = 0;
  143. bgfx::TextureHandle textures3d[3] = {};
  144. if (texture3DSupported)
  145. {
  146. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R8]) )
  147. {
  148. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8);
  149. }
  150. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R16F]) )
  151. {
  152. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f);
  153. }
  154. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R32F]) )
  155. {
  156. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f);
  157. }
  158. }
  159. // Create static vertex buffer.
  160. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl);
  161. // Create static index buffer.
  162. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  163. // Create programs.
  164. bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update");
  165. bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp");
  166. bgfx::ProgramHandle program3d = BGFX_INVALID_HANDLE;
  167. if (texture3DSupported)
  168. {
  169. program3d = loadProgram("vs_update", "fs_update_3d");
  170. }
  171. // Create texture sampler uniforms.
  172. bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  173. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  174. // Create time uniform.
  175. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  176. const uint32_t textureSide = 2048;
  177. bgfx::TextureHandle textureCube[2];
  178. textureCube[0] = bgfx::createTextureCube(textureSide, 1
  179. , bgfx::TextureFormat::BGRA8
  180. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  181. );
  182. if (blitSupported)
  183. {
  184. textureCube[1] = bgfx::createTextureCube(textureSide, 1
  185. , bgfx::TextureFormat::BGRA8
  186. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT|BGFX_TEXTURE_BLIT_DST
  187. );
  188. }
  189. const uint32_t texture2dSize = 256;
  190. bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
  191. , bgfx::TextureFormat::BGRA8
  192. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  193. );
  194. uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  195. uint8_t rr = rand()%255;
  196. uint8_t gg = rand()%255;
  197. uint8_t bb = rand()%255;
  198. int64_t updateTime = 0;
  199. RectPackCubeT<256> cube(textureSide);
  200. uint32_t hit = 0;
  201. uint32_t miss = 0;
  202. std::list<PackCube> quads;
  203. int64_t timeOffset = bx::getHPCounter();
  204. while (!entry::processEvents(width, height, debug, reset) )
  205. {
  206. float borderColor[4] = { float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f, float(rand()%255)/255.0f };
  207. bgfx::setPaletteColor(1, borderColor);
  208. // Set view 0 and 1 viewport.
  209. bgfx::setViewRect(0, 0, 0, width, height);
  210. bgfx::setViewRect(1, 0, 0, width, height);
  211. // This dummy draw call is here to make sure that view 0 is cleared
  212. // if no other draw calls are submitted to view 0.
  213. bgfx::touch(0);
  214. int64_t now = bx::getHPCounter();
  215. static int64_t last = now;
  216. const int64_t frameTime = now - last;
  217. last = now;
  218. const int64_t freq = bx::getHPFrequency();
  219. const double toMs = 1000.0/double(freq);
  220. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  221. bgfx::setUniform(u_time, &time);
  222. // Use debug font to print information about this example.
  223. bgfx::dbgTextClear();
  224. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  225. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  226. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  227. if (now > updateTime)
  228. {
  229. PackCube face;
  230. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  231. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  232. if (cube.find(bw, bh, face) )
  233. {
  234. quads.push_back(face);
  235. ++hit;
  236. const Pack2D& rect = face.m_rect;
  237. updateTextureCubeRectBgra8(textureCube[0], face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  238. if (blitSupported)
  239. {
  240. bgfx::blit(0, textureCube[1], 0, rect.m_x, rect.m_y, face.m_side, textureCube[0], 0, rect.m_x, rect.m_y, face.m_side, rect.m_width, rect.m_height);
  241. }
  242. rr = rand()%255;
  243. gg = rand()%255;
  244. bb = rand()%255;
  245. }
  246. else
  247. {
  248. ++miss;
  249. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  250. {
  251. cube.clear(quads.front() );
  252. quads.pop_front();
  253. }
  254. }
  255. {
  256. // Fill rect.
  257. const uint32_t pitch = texture2dSize*4;
  258. const uint16_t tw = rand()%texture2dSize;
  259. const uint16_t th = rand()%texture2dSize;
  260. const uint16_t tx = rand()%(texture2dSize-tw);
  261. const uint16_t ty = rand()%(texture2dSize-th);
  262. uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4];
  263. uint8_t* next = dst + pitch;
  264. // Using makeRef to pass texture memory without copying.
  265. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  266. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  267. {
  268. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  269. {
  270. dst[0] = bb;
  271. dst[1] = gg;
  272. dst[2] = rr;
  273. dst[3] = 255;
  274. }
  275. }
  276. // Pitch here makes possible to pass data from source to destination
  277. // without need for textures and allocated memory to be the same size.
  278. bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch);
  279. }
  280. }
  281. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  282. float at[3] = { 0.0f, 0.0f, 0.0f };
  283. float eye[3] = { 0.0f, 0.0f, -5.0f };
  284. float view[16];
  285. float proj[16];
  286. bx::mtxLookAt(view, eye, at);
  287. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  288. // Set view and projection matrix for view 0.
  289. bgfx::setViewTransform(0, view, proj);
  290. for (uint32_t ii = 0; ii < 1 + uint32_t(blitSupported); ++ii)
  291. {
  292. float mtx[16];
  293. bx::mtxSRT(mtx, 1.0f, 1.0f, 1.0f, time, time*0.37f, 0.0f, -1.5f*blitSupported + ii*3.0f, 0.0f, 0.0f);
  294. // Set model matrix for rendering.
  295. bgfx::setTransform(mtx);
  296. // Set vertex and index buffer.
  297. bgfx::setVertexBuffer(vbh);
  298. bgfx::setIndexBuffer(ibh);
  299. // Bind texture.
  300. bgfx::setTexture(0, s_texCube, textureCube[ii]);
  301. // Set render states.
  302. bgfx::setState(BGFX_STATE_DEFAULT);
  303. // Submit primitive for rendering to view 0.
  304. bgfx::submit(0, program);
  305. }
  306. // Set view and projection matrix for view 1.
  307. const float aspectRatio = float(height)/float(width);
  308. const float size = 11.0f;
  309. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  310. bgfx::setViewTransform(1, NULL, proj);
  311. float mtx[16];
  312. bx::mtxTranslate(mtx, -size+2.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f);
  313. // Set model matrix for rendering.
  314. bgfx::setTransform(mtx);
  315. // Set vertex and index buffer.
  316. bgfx::setVertexBuffer(vbh);
  317. bgfx::setIndexBuffer(ibh);
  318. // Bind texture.
  319. bgfx::setTexture(0, s_texColor, texture2d);
  320. // Set render states.
  321. bgfx::setState(BGFX_STATE_DEFAULT);
  322. // Submit primitive for rendering to view 1.
  323. bgfx::submit(1, programCmp);
  324. const float xpos = -size+2.0f - BX_COUNTOF(textures)*0.1f*0.5f;
  325. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  326. {
  327. bx::mtxTranslate(mtx, xpos + ii*2.1f, size-6.5f, 0.0f);
  328. // Set model matrix for rendering.
  329. bgfx::setTransform(mtx);
  330. // Set vertex and index buffer.
  331. bgfx::setVertexBuffer(vbh);
  332. bgfx::setIndexBuffer(ibh, 0, 6);
  333. // Bind texture.
  334. bgfx::setTexture(0, s_texColor, textures[ii]);
  335. // Set render states.
  336. bgfx::setState(BGFX_STATE_DEFAULT);
  337. // Submit primitive for rendering to view 1.
  338. bgfx::submit(1, programCmp);
  339. }
  340. for (uint32_t ii = 0; ii < numTextures3d; ++ii)
  341. {
  342. bx::mtxTranslate(mtx, xpos + ii*2.1f, -size+6.5f, 0.0f);
  343. // Set model matrix for rendering.
  344. bgfx::setTransform(mtx);
  345. // Set vertex and index buffer.
  346. bgfx::setVertexBuffer(vbh);
  347. bgfx::setIndexBuffer(ibh, 0, 6);
  348. // Bind texture.
  349. bgfx::setTexture(0, s_texColor, textures3d[ii]);
  350. // Set render states.
  351. bgfx::setState(BGFX_STATE_DEFAULT);
  352. // Submit primitive for rendering to view 1.
  353. bgfx::submit(1, program3d);
  354. }
  355. for (uint32_t ii = 0; ii < 4; ++ii)
  356. {
  357. bx::mtxTranslate(mtx, xpos + (size-2.0f)*2.1f, -size+6.5f + ii*2.1f, 0.0f);
  358. // Set model matrix for rendering.
  359. bgfx::setTransform(mtx);
  360. // Set vertex and index buffer.
  361. bgfx::setVertexBuffer(vbh, 24, 4);
  362. bgfx::setIndexBuffer(ibh, 0, 6);
  363. // Bind texture.
  364. bgfx::setTexture(0, s_texColor, textures[ii]);
  365. // Set render states.
  366. bgfx::setState(BGFX_STATE_DEFAULT);
  367. // Submit primitive for rendering to view 1.
  368. bgfx::submit(1, programCmp);
  369. }
  370. // Advance to next frame. Rendering thread will be kicked to
  371. // process submitted rendering primitives.
  372. bgfx::frame();
  373. }
  374. // texture2dData is managed from main thread, and it's passed to renderer
  375. // just as MemoryRef. At this point render might be using it. We must wait
  376. // previous frame to finish before we can free it.
  377. bgfx::frame();
  378. // Cleanup.
  379. free(texture2dData);
  380. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  381. {
  382. bgfx::destroyTexture(textures[ii]);
  383. }
  384. for (uint32_t ii = 0; ii < numTextures3d; ++ii)
  385. {
  386. bgfx::destroyTexture(textures3d[ii]);
  387. }
  388. bgfx::destroyTexture(texture2d);
  389. bgfx::destroyTexture(textureCube[0]);
  390. if (blitSupported)
  391. {
  392. bgfx::destroyTexture(textureCube[1]);
  393. }
  394. bgfx::destroyIndexBuffer(ibh);
  395. bgfx::destroyVertexBuffer(vbh);
  396. if (bgfx::isValid(program3d) )
  397. {
  398. bgfx::destroyProgram(program3d);
  399. }
  400. bgfx::destroyProgram(programCmp);
  401. bgfx::destroyProgram(program);
  402. bgfx::destroyUniform(u_time);
  403. bgfx::destroyUniform(s_texColor);
  404. bgfx::destroyUniform(s_texCube);
  405. // Shutdown bgfx.
  406. bgfx::shutdown();
  407. return 0;
  408. }