update.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 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)
  103. {
  104. bgfx::TextureInfo ti;
  105. bgfx::calcTextureSize(ti, _width, _height, 1, 1, bgfx::TextureFormat::BGRA8);
  106. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  107. uint8_t* data = (uint8_t*)mem->data;
  108. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  109. {
  110. data[0] = _b;
  111. data[1] = _g;
  112. data[2] = _r;
  113. data[3] = _a;
  114. data += 4;
  115. }
  116. bgfx::updateTextureCube(_handle, _side, 0, _x, _y, _width, _height, mem);
  117. }
  118. int _main_(int /*_argc*/, char** /*_argv*/)
  119. {
  120. uint32_t width = 1280;
  121. uint32_t height = 720;
  122. uint32_t debug = BGFX_DEBUG_TEXT;
  123. uint32_t reset = BGFX_RESET_VSYNC;
  124. bgfx::init();
  125. bgfx::reset(width, height, reset);
  126. // Enable debug text.
  127. bgfx::setDebug(debug);
  128. // Set view 0 clear state.
  129. bgfx::setViewClear(0
  130. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  131. , 0x303030ff
  132. , 1.0f
  133. , 0
  134. );
  135. // Setup root path for binary shaders. Shader binaries are different
  136. // for each renderer.
  137. switch (bgfx::getRendererType() )
  138. {
  139. default:
  140. case bgfx::RendererType::Direct3D9:
  141. s_shaderPath = "shaders/dx9/";
  142. break;
  143. case bgfx::RendererType::Direct3D11:
  144. s_shaderPath = "shaders/dx11/";
  145. break;
  146. case bgfx::RendererType::OpenGL:
  147. s_shaderPath = "shaders/glsl/";
  148. break;
  149. case bgfx::RendererType::OpenGLES2:
  150. case bgfx::RendererType::OpenGLES3:
  151. s_shaderPath = "shaders/gles/";
  152. break;
  153. }
  154. // Create vertex stream declaration.
  155. s_PosTexcoordDecl.begin();
  156. s_PosTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  157. s_PosTexcoordDecl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float);
  158. s_PosTexcoordDecl.end();
  159. const bgfx::Memory* mem;
  160. // Create static vertex buffer.
  161. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  162. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl);
  163. // Create static index buffer.
  164. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  165. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  166. // Create texture sampler uniforms.
  167. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  168. // Load vertex shader.
  169. mem = loadShader("vs_update");
  170. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  171. // Load fragment shader.
  172. mem = loadShader("fs_update");
  173. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  174. // Create program from shaders.
  175. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  176. // We can destroy vertex and fragment shader here since
  177. // their reference is kept inside bgfx after calling createProgram.
  178. // Vertex and fragment shader will be destroyed once program is
  179. // destroyed.
  180. bgfx::destroyVertexShader(vsh);
  181. bgfx::destroyFragmentShader(fsh);
  182. const uint32_t textureSide = 2048;
  183. bgfx::TextureHandle textureCube =
  184. bgfx::createTextureCube(6
  185. , textureSide
  186. , 1
  187. , bgfx::TextureFormat::BGRA8
  188. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  189. );
  190. uint8_t rr = rand()%255;
  191. uint8_t gg = rand()%255;
  192. uint8_t bb = rand()%255;
  193. int64_t updateTime = 0;
  194. RectPackCubeT<256> cube(textureSide);
  195. uint32_t hit = 0;
  196. uint32_t miss = 0;
  197. std::list<PackCube> quads;
  198. int64_t timeOffset = bx::getHPCounter();
  199. while (!entry::processEvents(width, height, debug, reset) )
  200. {
  201. // Set view 0 default viewport.
  202. bgfx::setViewRect(0, 0, 0, width, height);
  203. // This dummy draw call is here to make sure that view 0 is cleared
  204. // if no other draw calls are submitted to view 0.
  205. bgfx::submit(0);
  206. int64_t now = bx::getHPCounter();
  207. static int64_t last = now;
  208. const int64_t frameTime = now - last;
  209. last = now;
  210. const int64_t freq = bx::getHPFrequency();
  211. const double toMs = 1000.0/double(freq);
  212. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  213. // Use debug font to print information about this example.
  214. bgfx::dbgTextClear();
  215. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  216. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  217. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  218. if (now > updateTime)
  219. {
  220. PackCube face;
  221. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  222. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  223. if (cube.find(bw, bh, face) )
  224. {
  225. quads.push_back(face);
  226. ++hit;
  227. const Pack2D& rect = face.m_rect;
  228. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, rr, gg, bb);
  229. rr = rand()%255;
  230. gg = rand()%255;
  231. bb = rand()%255;
  232. }
  233. else
  234. {
  235. ++miss;
  236. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  237. {
  238. const PackCube& face = quads.front();
  239. cube.clear(face);
  240. quads.pop_front();
  241. }
  242. }
  243. }
  244. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  245. float at[3] = { 0.0f, 0.0f, 0.0f };
  246. float eye[3] = { 0.0f, 0.0f, -5.0f };
  247. float view[16];
  248. float proj[16];
  249. mtxLookAt(view, eye, at);
  250. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  251. // Set view and projection matrix for view 0.
  252. bgfx::setViewTransform(0, view, proj);
  253. float mtx[16];
  254. mtxRotateXY(mtx, time, time*0.37f);
  255. // Set model matrix for rendering.
  256. bgfx::setTransform(mtx);
  257. // Set vertex and fragment shaders.
  258. bgfx::setProgram(program);
  259. // Set vertex and index buffer.
  260. bgfx::setVertexBuffer(vbh);
  261. bgfx::setIndexBuffer(ibh);
  262. // Bind texture.
  263. bgfx::setTexture(0, u_texCube, textureCube);
  264. // Set render states.
  265. bgfx::setState(BGFX_STATE_DEFAULT);
  266. // Submit primitive for rendering to view 0.
  267. bgfx::submit(0);
  268. // Advance to next frame. Rendering thread will be kicked to
  269. // process submitted rendering primitives.
  270. bgfx::frame();
  271. }
  272. // Cleanup.
  273. bgfx::destroyIndexBuffer(ibh);
  274. bgfx::destroyVertexBuffer(vbh);
  275. bgfx::destroyProgram(program);
  276. bgfx::destroyTexture(textureCube);
  277. bgfx::destroyUniform(u_texCube);
  278. // Shutdown bgfx.
  279. bgfx::shutdown();
  280. return 0;
  281. }