update.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright 2011-2014 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.begin();
  21. ms_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  22. ms_decl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float);
  23. ms_decl.end();
  24. };
  25. static bgfx::VertexDecl ms_decl;
  26. };
  27. bgfx::VertexDecl PosTexcoordVertex::ms_decl;
  28. static PosTexcoordVertex s_cubeVertices[28] =
  29. {
  30. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  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, -2.0f, 2.0f, 2.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. };
  59. static const uint16_t s_cubeIndices[36] =
  60. {
  61. 0, 1, 2, // 0
  62. 1, 3, 2,
  63. 4, 6, 5, // 2
  64. 5, 6, 7,
  65. 8, 10, 9, // 4
  66. 9, 10, 11,
  67. 12, 14, 13, // 6
  68. 14, 15, 13,
  69. 16, 18, 17, // 8
  70. 18, 19, 17,
  71. 20, 22, 21, // 10
  72. 21, 22, 23,
  73. };
  74. 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)
  75. {
  76. bgfx::TextureInfo ti;
  77. bgfx::calcTextureSize(ti, _width, _height, 1, 1, bgfx::TextureFormat::BGRA8);
  78. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  79. uint8_t* data = (uint8_t*)mem->data;
  80. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  81. {
  82. data[0] = _b;
  83. data[1] = _g;
  84. data[2] = _r;
  85. data[3] = _a;
  86. data += 4;
  87. }
  88. bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
  89. }
  90. int _main_(int /*_argc*/, char** /*_argv*/)
  91. {
  92. uint32_t width = 1280;
  93. uint32_t height = 720;
  94. uint32_t debug = BGFX_DEBUG_TEXT;
  95. uint32_t reset = BGFX_RESET_VSYNC;
  96. bgfx::init();
  97. bgfx::reset(width, height, reset);
  98. // Enable debug text.
  99. bgfx::setDebug(debug);
  100. // Set view 0 clear state.
  101. bgfx::setViewClear(0
  102. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  103. , 0x303030ff
  104. , 1.0f
  105. , 0
  106. );
  107. // Create vertex stream declaration.
  108. PosTexcoordVertex::init();
  109. bgfx::TextureHandle textures[] =
  110. {
  111. loadTexture("texture_compression_bc1.dds", BGFX_TEXTURE_U_CLAMP|BGFX_TEXTURE_V_CLAMP),
  112. loadTexture("texture_compression_bc2.dds", BGFX_TEXTURE_U_CLAMP),
  113. loadTexture("texture_compression_bc3.dds", BGFX_TEXTURE_V_CLAMP),
  114. loadTexture("texture_compression_etc1.ktx"),
  115. loadTexture("texture_compression_etc2.ktx"),
  116. loadTexture("texture_compression_ptc12.pvr"),
  117. loadTexture("texture_compression_ptc14.pvr"),
  118. loadTexture("texture_compression_ptc22.pvr"),
  119. loadTexture("texture_compression_ptc24.pvr"),
  120. };
  121. // Create static vertex buffer.
  122. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ), PosTexcoordVertex::ms_decl);
  123. // Create static index buffer.
  124. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  125. // Create texture sampler uniforms.
  126. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  127. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  128. bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update");
  129. bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp");
  130. const uint32_t textureSide = 2048;
  131. bgfx::TextureHandle textureCube = bgfx::createTextureCube(textureSide, 1
  132. , bgfx::TextureFormat::BGRA8
  133. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  134. );
  135. const uint32_t texture2dSize = 256;
  136. bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
  137. , bgfx::TextureFormat::BGRA8
  138. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  139. );
  140. uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  141. uint8_t rr = rand()%255;
  142. uint8_t gg = rand()%255;
  143. uint8_t bb = rand()%255;
  144. int64_t updateTime = 0;
  145. RectPackCubeT<256> cube(textureSide);
  146. uint32_t hit = 0;
  147. uint32_t miss = 0;
  148. std::list<PackCube> quads;
  149. int64_t timeOffset = bx::getHPCounter();
  150. while (!entry::processEvents(width, height, debug, reset) )
  151. {
  152. // Set view 0 and 1 viewport.
  153. bgfx::setViewRectMask(0x3, 0, 0, width, height);
  154. // This dummy draw call is here to make sure that view 0 is cleared
  155. // if no other draw calls are submitted to view 0.
  156. bgfx::submit(0);
  157. int64_t now = bx::getHPCounter();
  158. static int64_t last = now;
  159. const int64_t frameTime = now - last;
  160. last = now;
  161. const int64_t freq = bx::getHPFrequency();
  162. const double toMs = 1000.0/double(freq);
  163. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  164. // Use debug font to print information about this example.
  165. bgfx::dbgTextClear();
  166. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  167. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  168. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  169. if (now > updateTime)
  170. {
  171. PackCube face;
  172. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  173. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  174. if (cube.find(bw, bh, face) )
  175. {
  176. quads.push_back(face);
  177. ++hit;
  178. const Pack2D& rect = face.m_rect;
  179. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  180. rr = rand()%255;
  181. gg = rand()%255;
  182. bb = rand()%255;
  183. }
  184. else
  185. {
  186. ++miss;
  187. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  188. {
  189. const PackCube& face = quads.front();
  190. cube.clear(face);
  191. quads.pop_front();
  192. }
  193. }
  194. {
  195. // Fill rect.
  196. const uint32_t pitch = texture2dSize*4;
  197. const uint16_t tw = rand()%texture2dSize;
  198. const uint16_t th = rand()%texture2dSize;
  199. const uint16_t tx = rand()%(texture2dSize-tw);
  200. const uint16_t ty = rand()%(texture2dSize-th);
  201. uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4];
  202. uint8_t* next = dst + pitch;
  203. // Using makeRef to pass texture memory without copying.
  204. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  205. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  206. {
  207. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  208. {
  209. dst[0] = bb;
  210. dst[1] = gg;
  211. dst[2] = rr;
  212. dst[3] = 255;
  213. }
  214. }
  215. // Pitch here makes possible to pass data from source to destination
  216. // without need for textures and allocated memory to be the same size.
  217. bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch);
  218. }
  219. }
  220. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  221. float at[3] = { 0.0f, 0.0f, 0.0f };
  222. float eye[3] = { 0.0f, 0.0f, -5.0f };
  223. float view[16];
  224. float proj[16];
  225. mtxLookAt(view, eye, at);
  226. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  227. // Set view and projection matrix for view 0.
  228. bgfx::setViewTransform(0, view, proj);
  229. float mtx[16];
  230. mtxRotateXY(mtx, time, time*0.37f);
  231. // Set model matrix for rendering.
  232. bgfx::setTransform(mtx);
  233. // Set vertex and fragment shaders.
  234. bgfx::setProgram(program);
  235. // Set vertex and index buffer.
  236. bgfx::setVertexBuffer(vbh);
  237. bgfx::setIndexBuffer(ibh);
  238. // Bind texture.
  239. bgfx::setTexture(0, u_texCube, textureCube);
  240. // Set render states.
  241. bgfx::setState(BGFX_STATE_DEFAULT);
  242. // Submit primitive for rendering to view 0.
  243. bgfx::submit(0);
  244. // Set view and projection matrix for view 1.
  245. const float aspectRatio = float(height)/float(width);
  246. const float size = 10.0f;
  247. mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  248. bgfx::setViewTransform(1, NULL, proj);
  249. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f);
  250. // Set model matrix for rendering.
  251. bgfx::setTransform(mtx);
  252. // Set vertex and fragment shaders.
  253. bgfx::setProgram(programCmp);
  254. // Set vertex and index buffer.
  255. bgfx::setVertexBuffer(vbh);
  256. bgfx::setIndexBuffer(ibh);
  257. // Bind texture.
  258. bgfx::setTexture(0, u_texColor, texture2d);
  259. // Set render states.
  260. bgfx::setState(BGFX_STATE_DEFAULT);
  261. // Submit primitive for rendering to view 0.
  262. bgfx::submit(1);
  263. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  264. {
  265. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f + ii*2.1f, 4.0f, 0.0f);
  266. // Set model matrix for rendering.
  267. bgfx::setTransform(mtx);
  268. // Set vertex and fragment shaders.
  269. bgfx::setProgram(programCmp);
  270. // Set vertex and index buffer.
  271. bgfx::setVertexBuffer(vbh);
  272. bgfx::setIndexBuffer(ibh, 0, 6);
  273. // Bind texture.
  274. bgfx::setTexture(0, u_texColor, textures[ii]);
  275. // Set render states.
  276. bgfx::setState(BGFX_STATE_DEFAULT);
  277. // Submit primitive for rendering to view 0.
  278. bgfx::submit(1);
  279. }
  280. for (uint32_t ii = 0; ii < 3; ++ii)
  281. {
  282. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f + 8*2.1f, -4.0f + ii*2.1f, 0.0f);
  283. // Set model matrix for rendering.
  284. bgfx::setTransform(mtx);
  285. // Set vertex and fragment shaders.
  286. bgfx::setProgram(programCmp);
  287. // Set vertex and index buffer.
  288. bgfx::setVertexBuffer(vbh, 24, 4);
  289. bgfx::setIndexBuffer(ibh, 0, 6);
  290. // Bind texture.
  291. bgfx::setTexture(0, u_texColor, textures[ii]);
  292. // Set render states.
  293. bgfx::setState(BGFX_STATE_DEFAULT);
  294. // Submit primitive for rendering to view 0.
  295. bgfx::submit(1);
  296. }
  297. // Advance to next frame. Rendering thread will be kicked to
  298. // process submitted rendering primitives.
  299. bgfx::frame();
  300. }
  301. // texture2dData is managed from main thread, and it's passed to renderer
  302. // just as MemoryRef. At this point render might be using it. We must wait
  303. // previous frame to finish before we can free it.
  304. bgfx::frame();
  305. // Cleanup.
  306. free(texture2dData);
  307. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  308. {
  309. bgfx::destroyTexture(textures[ii]);
  310. }
  311. bgfx::destroyTexture(texture2d);
  312. bgfx::destroyTexture(textureCube);
  313. bgfx::destroyIndexBuffer(ibh);
  314. bgfx::destroyVertexBuffer(vbh);
  315. bgfx::destroyProgram(programCmp);
  316. bgfx::destroyProgram(program);
  317. bgfx::destroyUniform(u_texColor);
  318. bgfx::destroyUniform(u_texCube);
  319. // Shutdown bgfx.
  320. bgfx::shutdown();
  321. return 0;
  322. }