update.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include <bx/uint32_t.h>
  9. #include "fpumath.h"
  10. #include "packrect.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <list>
  14. struct PosColorVertex
  15. {
  16. float m_x;
  17. float m_y;
  18. float m_z;
  19. float m_u;
  20. float m_v;
  21. float m_w;
  22. };
  23. static bgfx::VertexDecl s_PosTexcoordDecl;
  24. static PosColorVertex s_cubeVertices[24] =
  25. {
  26. {-1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
  27. { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
  28. {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f },
  29. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  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. };
  51. static const uint16_t s_cubeIndices[36] =
  52. {
  53. 0, 1, 2, // 0
  54. 1, 3, 2,
  55. 4, 6, 5, // 2
  56. 5, 6, 7,
  57. 8, 10, 9, // 4
  58. 9, 10, 11,
  59. 12, 14, 13, // 6
  60. 14, 15, 13,
  61. 16, 18, 17, // 8
  62. 18, 19, 17,
  63. 20, 22, 21, // 10
  64. 21, 22, 23,
  65. };
  66. static const char* s_shaderPath = NULL;
  67. static void shaderFilePath(char* _out, const char* _name)
  68. {
  69. strcpy(_out, s_shaderPath);
  70. strcat(_out, _name);
  71. strcat(_out, ".bin");
  72. }
  73. long int fsize(FILE* _file)
  74. {
  75. long int pos = ftell(_file);
  76. fseek(_file, 0L, SEEK_END);
  77. long int size = ftell(_file);
  78. fseek(_file, pos, SEEK_SET);
  79. return size;
  80. }
  81. static const bgfx::Memory* load(const char* _filePath)
  82. {
  83. FILE* file = fopen(_filePath, "rb");
  84. if (NULL != file)
  85. {
  86. uint32_t size = (uint32_t)fsize(file);
  87. const bgfx::Memory* mem = bgfx::alloc(size+1);
  88. size_t ignore = fread(mem->data, 1, size, file);
  89. BX_UNUSED(ignore);
  90. fclose(file);
  91. mem->data[mem->size-1] = '\0';
  92. return mem;
  93. }
  94. return NULL;
  95. }
  96. static const bgfx::Memory* loadShader(const char* _name)
  97. {
  98. char filePath[512];
  99. shaderFilePath(filePath, _name);
  100. return load(filePath);
  101. }
  102. static bgfx::ProgramHandle loadProgram(const char* _vshName, const char* _fshName)
  103. {
  104. const bgfx::Memory* mem;
  105. mem = loadShader(_vshName);
  106. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  107. mem = loadShader(_fshName);
  108. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  109. // Create program from shaders.
  110. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  111. // We can destroy vertex and fragment shader here since
  112. // their reference is kept inside bgfx after calling createProgram.
  113. // Vertex and fragment shader will be destroyed once program is
  114. // destroyed.
  115. bgfx::destroyVertexShader(vsh);
  116. bgfx::destroyFragmentShader(fsh);
  117. return program;
  118. }
  119. static const bgfx::Memory* loadTexture(const char* _name)
  120. {
  121. char filePath[512];
  122. strcpy(filePath, "textures/");
  123. strcat(filePath, _name);
  124. return load(filePath);
  125. }
  126. 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)
  127. {
  128. bgfx::TextureInfo ti;
  129. bgfx::calcTextureSize(ti, _width, _height, 1, 1, bgfx::TextureFormat::BGRA8);
  130. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  131. uint8_t* data = (uint8_t*)mem->data;
  132. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  133. {
  134. data[0] = _b;
  135. data[1] = _g;
  136. data[2] = _r;
  137. data[3] = _a;
  138. data += 4;
  139. }
  140. bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
  141. }
  142. int _main_(int /*_argc*/, char** /*_argv*/)
  143. {
  144. uint32_t width = 1280;
  145. uint32_t height = 720;
  146. uint32_t debug = BGFX_DEBUG_TEXT;
  147. uint32_t reset = BGFX_RESET_VSYNC;
  148. bgfx::init();
  149. bgfx::reset(width, height, reset);
  150. // Enable debug text.
  151. bgfx::setDebug(debug);
  152. // Set view 0 clear state.
  153. bgfx::setViewClear(0
  154. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  155. , 0x303030ff
  156. , 1.0f
  157. , 0
  158. );
  159. // Setup root path for binary shaders. Shader binaries are different
  160. // for each renderer.
  161. switch (bgfx::getRendererType() )
  162. {
  163. default:
  164. case bgfx::RendererType::Direct3D9:
  165. s_shaderPath = "shaders/dx9/";
  166. break;
  167. case bgfx::RendererType::Direct3D11:
  168. s_shaderPath = "shaders/dx11/";
  169. break;
  170. case bgfx::RendererType::OpenGL:
  171. s_shaderPath = "shaders/glsl/";
  172. break;
  173. case bgfx::RendererType::OpenGLES2:
  174. case bgfx::RendererType::OpenGLES3:
  175. s_shaderPath = "shaders/gles/";
  176. break;
  177. }
  178. // Create vertex stream declaration.
  179. s_PosTexcoordDecl.begin();
  180. s_PosTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  181. s_PosTexcoordDecl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float);
  182. s_PosTexcoordDecl.end();
  183. const bgfx::Memory* mem;
  184. ///
  185. mem = loadTexture("texture_compression_bc1.dds");
  186. bgfx::TextureHandle textureBc1 = bgfx::createTexture(mem);
  187. ///
  188. mem = loadTexture("texture_compression_bc2.dds");
  189. bgfx::TextureHandle textureBc2 = bgfx::createTexture(mem);
  190. ///
  191. mem = loadTexture("texture_compression_bc3.dds");
  192. bgfx::TextureHandle textureBc3 = bgfx::createTexture(mem);
  193. ///
  194. mem = loadTexture("texture_compression_etc1.ktx");
  195. bgfx::TextureHandle textureEtc1 = bgfx::createTexture(mem);
  196. ///
  197. mem = loadTexture("texture_compression_etc2.ktx");
  198. bgfx::TextureHandle textureEtc2 = bgfx::createTexture(mem);
  199. ///
  200. mem = loadTexture("texture_compression_ptc12.pvr");
  201. bgfx::TextureHandle texturePtc12 = bgfx::createTexture(mem);
  202. ///
  203. mem = loadTexture("texture_compression_ptc14.pvr");
  204. bgfx::TextureHandle texturePtc14 = bgfx::createTexture(mem);
  205. ///
  206. mem = loadTexture("texture_compression_ptc22.pvr");
  207. bgfx::TextureHandle texturePtc22 = bgfx::createTexture(mem);
  208. ///
  209. mem = loadTexture("texture_compression_ptc24.pvr");
  210. bgfx::TextureHandle texturePtc24 = bgfx::createTexture(mem);
  211. bgfx::TextureHandle textures[] =
  212. {
  213. textureBc1,
  214. textureBc2,
  215. textureBc3,
  216. textureEtc1,
  217. textureEtc2,
  218. texturePtc12,
  219. texturePtc14,
  220. texturePtc22,
  221. texturePtc24,
  222. };
  223. // Create static vertex buffer.
  224. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  225. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl);
  226. // Create static index buffer.
  227. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  228. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  229. // Create texture sampler uniforms.
  230. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  231. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  232. bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update");
  233. bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp");
  234. const uint32_t textureSide = 2048;
  235. bgfx::TextureHandle textureCube = bgfx::createTextureCube(textureSide, 1
  236. , bgfx::TextureFormat::BGRA8
  237. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  238. );
  239. const uint32_t texture2dSize = 256;
  240. bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
  241. , bgfx::TextureFormat::BGRA8
  242. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  243. );
  244. uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  245. uint8_t rr = rand()%255;
  246. uint8_t gg = rand()%255;
  247. uint8_t bb = rand()%255;
  248. int64_t updateTime = 0;
  249. RectPackCubeT<256> cube(textureSide);
  250. uint32_t hit = 0;
  251. uint32_t miss = 0;
  252. std::list<PackCube> quads;
  253. int64_t timeOffset = bx::getHPCounter();
  254. while (!entry::processEvents(width, height, debug, reset) )
  255. {
  256. // Set view 0 and 1 viewport.
  257. bgfx::setViewRectMask(0x3, 0, 0, width, height);
  258. // This dummy draw call is here to make sure that view 0 is cleared
  259. // if no other draw calls are submitted to view 0.
  260. bgfx::submit(0);
  261. int64_t now = bx::getHPCounter();
  262. static int64_t last = now;
  263. const int64_t frameTime = now - last;
  264. last = now;
  265. const int64_t freq = bx::getHPFrequency();
  266. const double toMs = 1000.0/double(freq);
  267. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  268. // Use debug font to print information about this example.
  269. bgfx::dbgTextClear();
  270. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  271. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  272. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  273. if (now > updateTime)
  274. {
  275. PackCube face;
  276. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  277. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  278. if (cube.find(bw, bh, face) )
  279. {
  280. quads.push_back(face);
  281. ++hit;
  282. const Pack2D& rect = face.m_rect;
  283. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  284. rr = rand()%255;
  285. gg = rand()%255;
  286. bb = rand()%255;
  287. }
  288. else
  289. {
  290. ++miss;
  291. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  292. {
  293. const PackCube& face = quads.front();
  294. cube.clear(face);
  295. quads.pop_front();
  296. }
  297. }
  298. {
  299. // Fill rect.
  300. const uint32_t pitch = texture2dSize*4;
  301. const uint16_t tw = rand()%texture2dSize;
  302. const uint16_t th = rand()%texture2dSize;
  303. const uint16_t tx = rand()%(texture2dSize-tw);
  304. const uint16_t ty = rand()%(texture2dSize-th);
  305. uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4];
  306. uint8_t* next = dst + pitch;
  307. // Using makeRef to pass texture memory without copying.
  308. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  309. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  310. {
  311. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  312. {
  313. dst[0] = bb;
  314. dst[1] = gg;
  315. dst[2] = rr;
  316. dst[3] = 255;
  317. }
  318. }
  319. // Pitch here makes possible to pass data from source to destination
  320. // without need for textures and allocated memory to be the same size.
  321. bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch);
  322. }
  323. }
  324. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  325. float at[3] = { 0.0f, 0.0f, 0.0f };
  326. float eye[3] = { 0.0f, 0.0f, -5.0f };
  327. float view[16];
  328. float proj[16];
  329. mtxLookAt(view, eye, at);
  330. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  331. // Set view and projection matrix for view 0.
  332. bgfx::setViewTransform(0, view, proj);
  333. float mtx[16];
  334. mtxRotateXY(mtx, time, time*0.37f);
  335. // Set model matrix for rendering.
  336. bgfx::setTransform(mtx);
  337. // Set vertex and fragment shaders.
  338. bgfx::setProgram(program);
  339. // Set vertex and index buffer.
  340. bgfx::setVertexBuffer(vbh);
  341. bgfx::setIndexBuffer(ibh);
  342. // Bind texture.
  343. bgfx::setTexture(0, u_texCube, textureCube);
  344. // Set render states.
  345. bgfx::setState(BGFX_STATE_DEFAULT);
  346. // Submit primitive for rendering to view 0.
  347. bgfx::submit(0);
  348. // Set view and projection matrix for view 1.
  349. const float aspectRatio = float(height)/float(width);
  350. const float size = 10.0f;
  351. mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  352. bgfx::setViewTransform(1, NULL, proj);
  353. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f);
  354. // Set model matrix for rendering.
  355. bgfx::setTransform(mtx);
  356. // Set vertex and fragment shaders.
  357. bgfx::setProgram(programCmp);
  358. // Set vertex and index buffer.
  359. bgfx::setVertexBuffer(vbh);
  360. bgfx::setIndexBuffer(ibh);
  361. // Bind texture.
  362. bgfx::setTexture(0, u_texColor, texture2d);
  363. // Set render states.
  364. bgfx::setState(BGFX_STATE_DEFAULT);
  365. // Submit primitive for rendering to view 0.
  366. bgfx::submit(1);
  367. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  368. {
  369. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f + ii*2.1f, 4.0f, 0.0f);
  370. // Set model matrix for rendering.
  371. bgfx::setTransform(mtx);
  372. // Set vertex and fragment shaders.
  373. bgfx::setProgram(programCmp);
  374. // Set vertex and index buffer.
  375. bgfx::setVertexBuffer(vbh);
  376. bgfx::setIndexBuffer(ibh);
  377. // Bind texture.
  378. bgfx::setTexture(0, u_texColor, textures[ii]);
  379. // Set render states.
  380. bgfx::setState(BGFX_STATE_DEFAULT);
  381. // Submit primitive for rendering to view 0.
  382. bgfx::submit(1);
  383. }
  384. // Advance to next frame. Rendering thread will be kicked to
  385. // process submitted rendering primitives.
  386. bgfx::frame();
  387. }
  388. // Cleanup.
  389. free(texture2dData);
  390. bgfx::destroyTexture(textureBc1);
  391. bgfx::destroyTexture(textureBc2);
  392. bgfx::destroyTexture(textureBc3);
  393. bgfx::destroyTexture(textureEtc1);
  394. bgfx::destroyTexture(textureEtc2);
  395. bgfx::destroyTexture(texturePtc12);
  396. bgfx::destroyTexture(texturePtc14);
  397. bgfx::destroyTexture(texturePtc22);
  398. bgfx::destroyTexture(texturePtc24);
  399. bgfx::destroyTexture(texture2d);
  400. bgfx::destroyTexture(textureCube);
  401. bgfx::destroyIndexBuffer(ibh);
  402. bgfx::destroyVertexBuffer(vbh);
  403. bgfx::destroyProgram(programCmp);
  404. bgfx::destroyProgram(program);
  405. bgfx::destroyUniform(u_texColor);
  406. bgfx::destroyUniform(u_texCube);
  407. // Shutdown bgfx.
  408. bgfx::shutdown();
  409. return 0;
  410. }