update.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. bgfx::TextureHandle textures[] =
  184. {
  185. bgfx::createTexture(loadTexture("texture_compression_bc1.dds") ),
  186. bgfx::createTexture(loadTexture("texture_compression_bc2.dds") ),
  187. bgfx::createTexture(loadTexture("texture_compression_bc3.dds") ),
  188. bgfx::createTexture(loadTexture("texture_compression_etc1.ktx") ),
  189. bgfx::createTexture(loadTexture("texture_compression_etc2.ktx") ),
  190. bgfx::createTexture(loadTexture("texture_compression_ptc12.pvr") ),
  191. bgfx::createTexture(loadTexture("texture_compression_ptc14.pvr") ),
  192. bgfx::createTexture(loadTexture("texture_compression_ptc22.pvr") ),
  193. bgfx::createTexture(loadTexture("texture_compression_ptc24.pvr") ),
  194. };
  195. const bgfx::Memory* mem;
  196. // Create static vertex buffer.
  197. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  198. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl);
  199. // Create static index buffer.
  200. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  201. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  202. // Create texture sampler uniforms.
  203. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  204. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  205. bgfx::ProgramHandle program = loadProgram("vs_update", "fs_update");
  206. bgfx::ProgramHandle programCmp = loadProgram("vs_update", "fs_update_cmp");
  207. const uint32_t textureSide = 2048;
  208. bgfx::TextureHandle textureCube = bgfx::createTextureCube(textureSide, 1
  209. , bgfx::TextureFormat::BGRA8
  210. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  211. );
  212. const uint32_t texture2dSize = 256;
  213. bgfx::TextureHandle texture2d = bgfx::createTexture2D(texture2dSize, texture2dSize, 1
  214. , bgfx::TextureFormat::BGRA8
  215. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  216. );
  217. uint8_t* texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
  218. uint8_t rr = rand()%255;
  219. uint8_t gg = rand()%255;
  220. uint8_t bb = rand()%255;
  221. int64_t updateTime = 0;
  222. RectPackCubeT<256> cube(textureSide);
  223. uint32_t hit = 0;
  224. uint32_t miss = 0;
  225. std::list<PackCube> quads;
  226. int64_t timeOffset = bx::getHPCounter();
  227. while (!entry::processEvents(width, height, debug, reset) )
  228. {
  229. // Set view 0 and 1 viewport.
  230. bgfx::setViewRectMask(0x3, 0, 0, width, height);
  231. // This dummy draw call is here to make sure that view 0 is cleared
  232. // if no other draw calls are submitted to view 0.
  233. bgfx::submit(0);
  234. int64_t now = bx::getHPCounter();
  235. static int64_t last = now;
  236. const int64_t frameTime = now - last;
  237. last = now;
  238. const int64_t freq = bx::getHPFrequency();
  239. const double toMs = 1000.0/double(freq);
  240. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  241. // Use debug font to print information about this example.
  242. bgfx::dbgTextClear();
  243. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  244. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  245. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  246. if (now > updateTime)
  247. {
  248. PackCube face;
  249. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  250. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  251. if (cube.find(bw, bh, face) )
  252. {
  253. quads.push_back(face);
  254. ++hit;
  255. const Pack2D& rect = face.m_rect;
  256. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  257. rr = rand()%255;
  258. gg = rand()%255;
  259. bb = rand()%255;
  260. }
  261. else
  262. {
  263. ++miss;
  264. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  265. {
  266. const PackCube& face = quads.front();
  267. cube.clear(face);
  268. quads.pop_front();
  269. }
  270. }
  271. {
  272. // Fill rect.
  273. const uint32_t pitch = texture2dSize*4;
  274. const uint16_t tw = rand()%texture2dSize;
  275. const uint16_t th = rand()%texture2dSize;
  276. const uint16_t tx = rand()%(texture2dSize-tw);
  277. const uint16_t ty = rand()%(texture2dSize-th);
  278. uint8_t* dst = &texture2dData[(ty*texture2dSize+tx)*4];
  279. uint8_t* next = dst + pitch;
  280. // Using makeRef to pass texture memory without copying.
  281. const bgfx::Memory* mem = bgfx::makeRef(dst, tw*th*4);
  282. for (uint32_t yy = 0; yy < th; ++yy, dst = next, next += pitch)
  283. {
  284. for (uint32_t xx = 0; xx < tw; ++xx, dst += 4)
  285. {
  286. dst[0] = bb;
  287. dst[1] = gg;
  288. dst[2] = rr;
  289. dst[3] = 255;
  290. }
  291. }
  292. // Pitch here makes possible to pass data from source to destination
  293. // without need for textures and allocated memory to be the same size.
  294. bgfx::updateTexture2D(texture2d, 0, tx, ty, tw, th, mem, pitch);
  295. }
  296. }
  297. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  298. float at[3] = { 0.0f, 0.0f, 0.0f };
  299. float eye[3] = { 0.0f, 0.0f, -5.0f };
  300. float view[16];
  301. float proj[16];
  302. mtxLookAt(view, eye, at);
  303. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  304. // Set view and projection matrix for view 0.
  305. bgfx::setViewTransform(0, view, proj);
  306. float mtx[16];
  307. mtxRotateXY(mtx, time, time*0.37f);
  308. // Set model matrix for rendering.
  309. bgfx::setTransform(mtx);
  310. // Set vertex and fragment shaders.
  311. bgfx::setProgram(program);
  312. // Set vertex and index buffer.
  313. bgfx::setVertexBuffer(vbh);
  314. bgfx::setIndexBuffer(ibh);
  315. // Bind texture.
  316. bgfx::setTexture(0, u_texCube, textureCube);
  317. // Set render states.
  318. bgfx::setState(BGFX_STATE_DEFAULT);
  319. // Submit primitive for rendering to view 0.
  320. bgfx::submit(0);
  321. // Set view and projection matrix for view 1.
  322. const float aspectRatio = float(height)/float(width);
  323. const float size = 10.0f;
  324. mtxOrtho(proj, -size, size, size*aspectRatio, -size*aspectRatio, 0.0f, 1000.0f);
  325. bgfx::setViewTransform(1, NULL, proj);
  326. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f, 1.9f, 0.0f);
  327. // Set model matrix for rendering.
  328. bgfx::setTransform(mtx);
  329. // Set vertex and fragment shaders.
  330. bgfx::setProgram(programCmp);
  331. // Set vertex and index buffer.
  332. bgfx::setVertexBuffer(vbh);
  333. bgfx::setIndexBuffer(ibh);
  334. // Bind texture.
  335. bgfx::setTexture(0, u_texColor, texture2d);
  336. // Set render states.
  337. bgfx::setState(BGFX_STATE_DEFAULT);
  338. // Submit primitive for rendering to view 0.
  339. bgfx::submit(1);
  340. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  341. {
  342. mtxTranslate(mtx, -8.0f - BX_COUNTOF(textures)*0.1f*0.5f + ii*2.1f, 4.0f, 0.0f);
  343. // Set model matrix for rendering.
  344. bgfx::setTransform(mtx);
  345. // Set vertex and fragment shaders.
  346. bgfx::setProgram(programCmp);
  347. // Set vertex and index buffer.
  348. bgfx::setVertexBuffer(vbh);
  349. bgfx::setIndexBuffer(ibh);
  350. // Bind texture.
  351. bgfx::setTexture(0, u_texColor, textures[ii]);
  352. // Set render states.
  353. bgfx::setState(BGFX_STATE_DEFAULT);
  354. // Submit primitive for rendering to view 0.
  355. bgfx::submit(1);
  356. }
  357. // Advance to next frame. Rendering thread will be kicked to
  358. // process submitted rendering primitives.
  359. bgfx::frame();
  360. }
  361. // Cleanup.
  362. free(texture2dData);
  363. for (uint32_t ii = 0; ii < BX_COUNTOF(textures); ++ii)
  364. {
  365. bgfx::destroyTexture(textures[ii]);
  366. }
  367. bgfx::destroyTexture(texture2d);
  368. bgfx::destroyTexture(textureCube);
  369. bgfx::destroyIndexBuffer(ibh);
  370. bgfx::destroyVertexBuffer(vbh);
  371. bgfx::destroyProgram(programCmp);
  372. bgfx::destroyProgram(program);
  373. bgfx::destroyUniform(u_texColor);
  374. bgfx::destroyUniform(u_texCube);
  375. // Shutdown bgfx.
  376. bgfx::shutdown();
  377. return 0;
  378. }