update.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx.h>
  6. #include <bx/bx.h>
  7. #include <bx/timer.h>
  8. #include <bx/uint32_t.h>
  9. #include "../common/entry.h"
  10. #include "../common/dbg.h"
  11. #include "../common/math.h"
  12. #include "../common/processevents.h"
  13. #include "../common/packrect.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <list>
  17. struct PosColorVertex
  18. {
  19. float m_x;
  20. float m_y;
  21. float m_z;
  22. float m_u;
  23. float m_v;
  24. float m_w;
  25. };
  26. static bgfx::VertexDecl s_PosTexcoordDecl;
  27. static PosColorVertex s_cubeVertices[24] =
  28. {
  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, -1.0f, -1.0f, -1.0f },
  51. { 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f },
  52. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  53. };
  54. static const uint16_t s_cubeIndices[36] =
  55. {
  56. 0, 1, 2, // 0
  57. 1, 3, 2,
  58. 4, 6, 5, // 2
  59. 5, 6, 7,
  60. 8, 10, 9, // 4
  61. 9, 10, 11,
  62. 12, 14, 13, // 6
  63. 14, 15, 13,
  64. 16, 18, 17, // 8
  65. 18, 19, 17,
  66. 20, 22, 21, // 10
  67. 21, 22, 23,
  68. };
  69. static const char* s_shaderPath = NULL;
  70. static void shaderFilePath(char* _out, const char* _name)
  71. {
  72. strcpy(_out, s_shaderPath);
  73. strcat(_out, _name);
  74. strcat(_out, ".bin");
  75. }
  76. long int fsize(FILE* _file)
  77. {
  78. long int pos = ftell(_file);
  79. fseek(_file, 0L, SEEK_END);
  80. long int size = ftell(_file);
  81. fseek(_file, pos, SEEK_SET);
  82. return size;
  83. }
  84. static const bgfx::Memory* load(const char* _filePath)
  85. {
  86. FILE* file = fopen(_filePath, "rb");
  87. if (NULL != file)
  88. {
  89. uint32_t size = (uint32_t)fsize(file);
  90. const bgfx::Memory* mem = bgfx::alloc(size+1);
  91. size_t ignore = fread(mem->data, 1, size, file);
  92. BX_UNUSED(ignore);
  93. fclose(file);
  94. mem->data[mem->size-1] = '\0';
  95. return mem;
  96. }
  97. return NULL;
  98. }
  99. static const bgfx::Memory* loadShader(const char* _name)
  100. {
  101. char filePath[512];
  102. shaderFilePath(filePath, _name);
  103. return load(filePath);
  104. }
  105. int _main_(int /*_argc*/, char** /*_argv*/)
  106. {
  107. uint32_t width = 1280;
  108. uint32_t height = 720;
  109. uint32_t debug = BGFX_DEBUG_TEXT;
  110. uint32_t reset = BGFX_RESET_NONE;
  111. bgfx::init();
  112. bgfx::reset(width, height);
  113. // Enable debug text.
  114. bgfx::setDebug(debug);
  115. // Set view 0 clear state.
  116. bgfx::setViewClear(0
  117. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  118. , 0x303030ff
  119. , 1.0f
  120. , 0
  121. );
  122. // Setup root path for binary shaders. Shader binaries are different
  123. // for each renderer.
  124. switch (bgfx::getRendererType() )
  125. {
  126. default:
  127. case bgfx::RendererType::Direct3D9:
  128. s_shaderPath = "shaders/dx9/";
  129. break;
  130. case bgfx::RendererType::Direct3D11:
  131. s_shaderPath = "shaders/dx11/";
  132. break;
  133. case bgfx::RendererType::OpenGL:
  134. s_shaderPath = "shaders/glsl/";
  135. break;
  136. case bgfx::RendererType::OpenGLES2:
  137. case bgfx::RendererType::OpenGLES3:
  138. s_shaderPath = "shaders/gles/";
  139. break;
  140. }
  141. // Create vertex stream declaration.
  142. s_PosTexcoordDecl.begin();
  143. s_PosTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  144. s_PosTexcoordDecl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float);
  145. s_PosTexcoordDecl.end();
  146. const bgfx::Memory* mem;
  147. // Create static vertex buffer.
  148. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  149. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl);
  150. // Create static index buffer.
  151. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  152. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  153. // Create texture sampler uniforms.
  154. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  155. // Load vertex shader.
  156. mem = loadShader("vs_update");
  157. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  158. // Load fragment shader.
  159. mem = loadShader("fs_update");
  160. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  161. // Create program from shaders.
  162. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  163. // We can destroy vertex and fragment shader here since
  164. // their reference is kept inside bgfx after calling createProgram.
  165. // Vertex and fragment shader will be destroyed once program is
  166. // destroyed.
  167. bgfx::destroyVertexShader(vsh);
  168. bgfx::destroyFragmentShader(fsh);
  169. const uint32_t textureSide = 2048;
  170. bgfx::TextureHandle textureCube =
  171. bgfx::createTextureCube(6
  172. , textureSide
  173. , 1
  174. , bgfx::TextureFormat::BGRA8
  175. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  176. );
  177. uint8_t rr = rand()%255;
  178. uint8_t gg = rand()%255;
  179. uint8_t bb = rand()%255;
  180. int64_t updateTime = 0;
  181. RectPackCubeT<256> cube(textureSide);
  182. uint32_t hit = 0;
  183. uint32_t miss = 0;
  184. std::list<PackCube> quads;
  185. while (!processEvents(width, height, debug, reset) )
  186. {
  187. // Set view 0 default viewport.
  188. bgfx::setViewRect(0, 0, 0, width, height);
  189. // This dummy draw call is here to make sure that view 0 is cleared
  190. // if no other draw calls are submitted to view 0.
  191. bgfx::submit(0);
  192. int64_t now = bx::getHPCounter();
  193. static int64_t last = now;
  194. const int64_t frameTime = now - last;
  195. last = now;
  196. const int64_t freq = bx::getHPFrequency();
  197. const double toMs = 1000.0/double(freq);
  198. // Use debug font to print information about this example.
  199. bgfx::dbgTextClear();
  200. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  201. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  202. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  203. if (now > updateTime)
  204. {
  205. PackCube face;
  206. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  207. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  208. if (cube.find(bw, bh, face) )
  209. {
  210. quads.push_back(face);
  211. ++hit;
  212. bgfx::TextureInfo ti;
  213. const Pack2D& rect = face.m_rect;
  214. bgfx::calcTextureSize(ti, rect.m_width, rect.m_height, 1, 1, bgfx::TextureFormat::BGRA8);
  215. // updateTime = now + freq/10;
  216. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  217. uint8_t* data = (uint8_t*)mem->data;
  218. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  219. {
  220. data[0] = bb;
  221. data[1] = rr;
  222. data[2] = gg;
  223. data[3] = 0xff;
  224. data += 4;
  225. }
  226. bgfx::updateTextureCube(textureCube, face.m_side, 0, rect.m_x, rect.m_y, rect.m_width, rect.m_height, mem);
  227. rr = rand()%255;
  228. gg = rand()%255;
  229. bb = rand()%255;
  230. }
  231. else
  232. {
  233. ++miss;
  234. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  235. {
  236. const PackCube& face = quads.front();
  237. cube.clear(face);
  238. quads.pop_front();
  239. }
  240. }
  241. }
  242. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  243. float at[3] = { 0.0f, 0.0f, 0.0f };
  244. float eye[3] = { 0.0f, 0.0f, -5.0f };
  245. float view[16];
  246. float proj[16];
  247. mtxLookAt(view, eye, at);
  248. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  249. // Set view and projection matrix for view 0.
  250. bgfx::setViewTransform(0, view, proj);
  251. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  252. float mtx[16];
  253. mtxRotateXY(mtx, time, time*0.37f);
  254. // Set model matrix for rendering.
  255. bgfx::setTransform(mtx);
  256. // Set vertex and fragment shaders.
  257. bgfx::setProgram(program);
  258. // Set vertex and index buffer.
  259. bgfx::setVertexBuffer(vbh);
  260. bgfx::setIndexBuffer(ibh);
  261. // Bind texture.
  262. bgfx::setTexture(0, u_texCube, textureCube);
  263. // Set render states.
  264. bgfx::setState(BGFX_STATE_DEFAULT);
  265. // Submit primitive for rendering to view 0.
  266. bgfx::submit(0);
  267. // Advance to next frame. Rendering thread will be kicked to
  268. // process submitted rendering primitives.
  269. bgfx::frame();
  270. }
  271. // Cleanup.
  272. bgfx::destroyIndexBuffer(ibh);
  273. bgfx::destroyVertexBuffer(vbh);
  274. bgfx::destroyProgram(program);
  275. bgfx::destroyTexture(textureCube);
  276. bgfx::destroyUniform(u_texCube);
  277. // Shutdown bgfx.
  278. bgfx::shutdown();
  279. return 0;
  280. }