update.cpp 14 KB

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