update.cpp 8.5 KB

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