update.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. updateTextureCubeRectBgra8(textureCube, 0, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  191. updateTextureCubeRectBgra8(textureCube, 1, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  192. updateTextureCubeRectBgra8(textureCube, 2, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  193. updateTextureCubeRectBgra8(textureCube, 3, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  194. updateTextureCubeRectBgra8(textureCube, 4, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  195. updateTextureCubeRectBgra8(textureCube, 5, 0, 0, textureSide, textureSide, 0xff, 0, 0);
  196. uint8_t rr = rand()%255;
  197. uint8_t gg = rand()%255;
  198. uint8_t bb = rand()%255;
  199. int64_t updateTime = 0;
  200. RectPackCubeT<256> cube(textureSide);
  201. uint32_t hit = 0;
  202. uint32_t miss = 0;
  203. std::list<PackCube> quads;
  204. int64_t timeOffset = bx::getHPCounter();
  205. while (!entry::processEvents(width, height, debug, reset) )
  206. {
  207. // Set view 0 default viewport.
  208. bgfx::setViewRect(0, 0, 0, width, height);
  209. // This dummy draw call is here to make sure that view 0 is cleared
  210. // if no other draw calls are submitted to view 0.
  211. bgfx::submit(0);
  212. int64_t now = bx::getHPCounter();
  213. static int64_t last = now;
  214. const int64_t frameTime = now - last;
  215. last = now;
  216. const int64_t freq = bx::getHPFrequency();
  217. const double toMs = 1000.0/double(freq);
  218. float time = (float)( (now - timeOffset)/double(bx::getHPFrequency() ) );
  219. // Use debug font to print information about this example.
  220. bgfx::dbgTextClear();
  221. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  222. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  223. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  224. if (now > updateTime)
  225. {
  226. PackCube face;
  227. uint32_t bw = bx::uint16_max(1, rand()%(textureSide/4) );
  228. uint32_t bh = bx::uint16_max(1, rand()%(textureSide/4) );
  229. if (cube.find(bw, bh, face) )
  230. {
  231. quads.push_back(face);
  232. ++hit;
  233. const Pack2D& rect = face.m_rect;
  234. updateTextureCubeRectBgra8(textureCube, face.m_side, rect.m_x, rect.m_y, rect.m_width, rect.m_height, 0, 0xff, 0);
  235. rr = rand()%255;
  236. gg = rand()%255;
  237. bb = rand()%255;
  238. }
  239. else
  240. {
  241. ++miss;
  242. for (uint32_t ii = 0, num = bx::uint32_min(10, (uint32_t)quads.size() ); ii < num; ++ii)
  243. {
  244. const PackCube& face = quads.front();
  245. cube.clear(face);
  246. quads.pop_front();
  247. }
  248. }
  249. }
  250. bgfx::dbgTextPrintf(0, 4, 0x0f, "hit: %d, miss %d", hit, miss);
  251. float at[3] = { 0.0f, 0.0f, 0.0f };
  252. float eye[3] = { 0.0f, 0.0f, -5.0f };
  253. float view[16];
  254. float proj[16];
  255. mtxLookAt(view, eye, at);
  256. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  257. // Set view and projection matrix for view 0.
  258. bgfx::setViewTransform(0, view, proj);
  259. float mtx[16];
  260. mtxRotateXY(mtx, time, time*0.37f);
  261. // Set model matrix for rendering.
  262. bgfx::setTransform(mtx);
  263. // Set vertex and fragment shaders.
  264. bgfx::setProgram(program);
  265. // Set vertex and index buffer.
  266. bgfx::setVertexBuffer(vbh);
  267. bgfx::setIndexBuffer(ibh);
  268. // Bind texture.
  269. bgfx::setTexture(0, u_texCube, textureCube);
  270. // Set render states.
  271. bgfx::setState(BGFX_STATE_DEFAULT);
  272. // Submit primitive for rendering to view 0.
  273. bgfx::submit(0);
  274. // Advance to next frame. Rendering thread will be kicked to
  275. // process submitted rendering primitives.
  276. bgfx::frame();
  277. }
  278. // Cleanup.
  279. bgfx::destroyIndexBuffer(ibh);
  280. bgfx::destroyVertexBuffer(vbh);
  281. bgfx::destroyProgram(program);
  282. bgfx::destroyTexture(textureCube);
  283. bgfx::destroyUniform(u_texCube);
  284. // Shutdown bgfx.
  285. bgfx::shutdown();
  286. return 0;
  287. }