update.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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"),
  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. uint32_t numTextures3d = 0;
  142. bgfx::TextureHandle textures3d[3] = {};
  143. if (texture3DSupported)
  144. {
  145. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R8]) )
  146. {
  147. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R8, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem8);
  148. }
  149. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R16F]) )
  150. {
  151. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R16F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem16f);
  152. }
  153. if (0 != (BGFX_CAPS_FORMAT_TEXTURE_COLOR & caps->formats[bgfx::TextureFormat::R32F]) )
  154. {
  155. textures3d[numTextures3d++] = bgfx::createTexture3D(32, 32, 32, 0, bgfx::TextureFormat::R32F, BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP|BGFX_TEXTURE_W_CLAMP, mem32f);
  156. }
  157. }
  158. // Create static vertex buffer.
  159. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl);
  160. // Create static index buffer.
  161. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  162. // Create texture sampler uniforms.
  163. bgfx::UniformHandle s_texCube = bgfx::createUniform("s_texCube", bgfx::UniformType::Int1);
  164. bgfx::UniformHandle s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  165. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  166. bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update");
  167. bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp");
  168. bgfx::ProgramHandle program3d = BGFX_INVALID_HANDLE;
  169. if (texture3DSupported)
  170. {
  171. program3d = loadProgram("vs_update", "fs_update_3d");
  172. }
  173. const uint32_t textureSide = 2048;
  174. bgfx::TextureHandle textureCube = bgfx::createTextureCube(textureSide, 1
  175. , bgfx::TextureFormat::BGRA8
  176. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  177. );
  178. const uint32_t texture2dSize = 256;
  179. bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
  180. , bgfx::TextureFormat::BGRA8
  181. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  182. );
  183. uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  184. uint8_t rr = rand()%255;
  185. uint8_t gg = rand()%255;
  186. uint8_t bb = rand()%255;
  187. int64_t updateTime = 0;
  188. RectPackCubeT<256> cube(textureSide);
  189. uint32_t hit = 0;
  190. uint32_t miss = 0;
  191. std::list<PackCube> quads;
  192. int64_t timeOffset = bx::getHPCounter();
  193. while (!entry::processEvents(width, height, debug, reset) )
  194. {
  195. // Set view 0 and 1 viewport.
  196. bgfx::setViewRect(0, 0, 0, width, height);
  197. bgfx::setViewRect(1, 0, 0, width, height);
  198. // This dummy draw call is here to make sure that view 0 is cleared
  199. // if no other draw calls are submitted to view 0.
  200. bgfx::touch(0);
  201. int64_t now = bx::getHPCounter();
  202. static int64_t last = now;
  203. const int64_t frameTime = now - last;
  204. last = now;
  205. const int64_t freq = bx::getHPFrequency();
  206. const double toMs = 1000.0/double(freq);
  207. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  208. bgfx::setUniform(u_time, &time);
  209. // Use debug font to print information about this example.
  210. bgfx::dbgTextClear();
  211. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  212. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  213. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  214. if (now > updateTime)
  215. {
  216. PackCube face;
  217. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  218. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  219. if (cube.find(bw, bh, face) )
  220. {
  221. quads.push_back(face);
  222. ++hit;
  223. const Pack2D& rect = face.m_rect;
  224. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  225. rr = rand()%255;
  226. gg = rand()%255;
  227. bb = rand()%255;
  228. }
  229. else
  230. {
  231. ++miss;
  232. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  233. {
  234. cube.clear(quads.front() );
  235. quads.pop_front();
  236. }
  237. }
  238. {
  239. // Fill rect.
  240. const uint32_t pitch = texture2dSize*4;
  241. const uint16_t tw = rand()%texture2dSize;
  242. const uint16_t th = rand()%texture2dSize;
  243. const uint16_t tx = rand()%(texture2dSize-tw);
  244. const uint16_t ty = rand()%(texture2dSize-th);
  245. uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4];
  246. uint8_t* next = dst + pitch;
  247. // Using makeRef to pass texture memory without copying.
  248. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  249. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  250. {
  251. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  252. {
  253. dst[0] = bb;
  254. dst[1] = gg;
  255. dst[2] = rr;
  256. dst[3] = 255;
  257. }
  258. }
  259. // Pitch here makes possible to pass data from source to destination
  260. // without need for textures and allocated memory to be the same size.
  261. bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch);
  262. }
  263. }
  264. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  265. float at[3] = { 0.0f, 0.0f, 0.0f };
  266. float eye[3] = { 0.0f, 0.0f, -5.0f };
  267. float view[16];
  268. float proj[16];
  269. bx::mtxLookAt(view, eye, at);
  270. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  271. // Set view and projection matrix for view 0.
  272. bgfx::setViewTransform(0, view, proj);
  273. float mtx[16];
  274. bx::mtxRotateXY(mtx, time, time*0.37f);
  275. // Set model matrix for rendering.
  276. bgfx::setTransform(mtx);
  277. // Set vertex and index buffer.
  278. bgfx::setVertexBuffer(vbh);
  279. bgfx::setIndexBuffer(ibh);
  280. // Bind texture.
  281. bgfx::setTexture(0, s_texCube, textureCube);
  282. // Set render states.
  283. bgfx::setState(BGFX_STATE_DEFAULT);
  284. // Submit primitive for rendering to view 0.
  285. bgfx::submit(0, program);
  286. // Set view and projection matrix for view 1.
  287. const float aspectRatio = float(height)/float(width);
  288. const float size = 10.0f;
  289. bx::mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  290. bgfx::setViewTransform(1, NULL, proj);
  291. bx::mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f);
  292. // Set model matrix for rendering.
  293. bgfx::setTransform(mtx);
  294. // Set vertex and index buffer.
  295. bgfx::setVertexBuffer(vbh);
  296. bgfx::setIndexBuffer(ibh);
  297. // Bind texture.
  298. bgfx::setTexture(0, s_texColor, texture2d);
  299. // Set render states.
  300. bgfx::setState(BGFX_STATE_DEFAULT);
  301. // Submit primitive for rendering to view 1.
  302. bgfx::submit(1, programCmp);
  303. const float xpos = -8.0f - BX_COUNTOF(textures)*0.1f*0.5f;
  304. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  305. {
  306. bx::mtxTranslate(mtx, xpos + ii*2.1f, 4.0f, 0.0f);
  307. // Set model matrix for rendering.
  308. bgfx::setTransform(mtx);
  309. // Set vertex and index buffer.
  310. bgfx::setVertexBuffer(vbh);
  311. bgfx::setIndexBuffer(ibh, 0, 6);
  312. // Bind texture.
  313. bgfx::setTexture(0, s_texColor, textures[ii]);
  314. // Set render states.
  315. bgfx::setState(BGFX_STATE_DEFAULT);
  316. // Submit primitive for rendering to view 1.
  317. bgfx::submit(1, programCmp);
  318. }
  319. for (uint32_t ii = 0; ii < numTextures3d; ++ii)
  320. {
  321. bx::mtxTranslate(mtx, xpos + ii*2.1f, -4.0f, 0.0f);
  322. // Set model matrix for rendering.
  323. bgfx::setTransform(mtx);
  324. // Set vertex and index buffer.
  325. bgfx::setVertexBuffer(vbh);
  326. bgfx::setIndexBuffer(ibh, 0, 6);
  327. // Bind texture.
  328. bgfx::setTexture(0, s_texColor, textures3d[ii]);
  329. // Set render states.
  330. bgfx::setState(BGFX_STATE_DEFAULT);
  331. // Submit primitive for rendering to view 1.
  332. bgfx::submit(1, program3d);
  333. }
  334. for (uint32_t ii = 0; ii < 3; ++ii)
  335. {
  336. bx::mtxTranslate(mtx, xpos + 8*2.1f, -4.0f + ii*2.1f, 0.0f);
  337. // Set model matrix for rendering.
  338. bgfx::setTransform(mtx);
  339. // Set vertex and index buffer.
  340. bgfx::setVertexBuffer(vbh, 24, 4);
  341. bgfx::setIndexBuffer(ibh, 0, 6);
  342. // Bind texture.
  343. bgfx::setTexture(0, s_texColor, textures[ii]);
  344. // Set render states.
  345. bgfx::setState(BGFX_STATE_DEFAULT);
  346. // Submit primitive for rendering to view 1.
  347. bgfx::submit(1, programCmp);
  348. }
  349. // Advance to next frame. Rendering thread will be kicked to
  350. // process submitted rendering primitives.
  351. bgfx::frame();
  352. }
  353. // texture2dData is managed from main thread, and it's passed to renderer
  354. // just as MemoryRef. At this point render might be using it. We must wait
  355. // previous frame to finish before we can free it.
  356. bgfx::frame();
  357. // Cleanup.
  358. free(texture2dData);
  359. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  360. {
  361. bgfx::destroyTexture(textures[ii]);
  362. }
  363. for (uint32_t ii = 0; ii < numTextures3d; ++ii)
  364. {
  365. bgfx::destroyTexture(textures3d[ii]);
  366. }
  367. bgfx::destroyTexture(texture2d);
  368. bgfx::destroyTexture(textureCube);
  369. bgfx::destroyIndexBuffer(ibh);
  370. bgfx::destroyVertexBuffer(vbh);
  371. if (bgfx::isValid(program3d) )
  372. {
  373. bgfx::destroyProgram(program3d);
  374. }
  375. bgfx::destroyProgram(programCmp);
  376. bgfx::destroyProgram(program);
  377. bgfx::destroyUniform(u_time);
  378. bgfx::destroyUniform(s_texColor);
  379. bgfx::destroyUniform(s_texCube);
  380. // Shutdown bgfx.
  381. bgfx::shutdown();
  382. return 0;
  383. }