update.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <stdio.h>
  13. #include <string.h>
  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, 2, 1, // 0
  54. 1, 2, 3,
  55. 4, 5, 6, // 2
  56. 5, 7, 6,
  57. 8, 9, 10, // 4
  58. 9, 11, 10,
  59. 12, 13, 14, // 6
  60. 14, 13, 15,
  61. 16, 17, 18, // 8
  62. 18, 17, 19,
  63. 20, 21, 22, // 10
  64. 21, 23, 22,
  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. bgfx::init();
  105. bgfx::reset(1280, 720);
  106. // Enable debug text.
  107. bgfx::setDebug(BGFX_DEBUG_TEXT);
  108. // Set view 0 default viewport.
  109. bgfx::setViewRect(0, 0, 0, 1280, 720);
  110. // Set view 0 clear state.
  111. bgfx::setViewClear(0
  112. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  113. , 0x303030ff
  114. , 1.0f
  115. , 0
  116. );
  117. // Setup root path for binary shaders. Shader binaries are different
  118. // for each renderer.
  119. switch (bgfx::getRendererType() )
  120. {
  121. default:
  122. case bgfx::RendererType::Direct3D9:
  123. s_shaderPath = "shaders/dx9/";
  124. break;
  125. case bgfx::RendererType::Direct3D11:
  126. s_shaderPath = "shaders/dx11/";
  127. break;
  128. case bgfx::RendererType::OpenGL:
  129. s_shaderPath = "shaders/glsl/";
  130. break;
  131. case bgfx::RendererType::OpenGLES2:
  132. case bgfx::RendererType::OpenGLES3:
  133. s_shaderPath = "shaders/gles/";
  134. break;
  135. }
  136. // Create vertex stream declaration.
  137. s_PosTexcoordDecl.begin();
  138. s_PosTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  139. s_PosTexcoordDecl.add(bgfx::Attrib::TexCoord0, 3, bgfx::AttribType::Float);
  140. s_PosTexcoordDecl.end();
  141. const bgfx::Memory* mem;
  142. // Create static vertex buffer.
  143. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  144. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosTexcoordDecl);
  145. // Create static index buffer.
  146. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  147. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  148. // Create texture sampler uniforms.
  149. bgfx::UniformHandle u_texCube = bgfx::createUniform("u_texCube", bgfx::UniformType::Uniform1iv);
  150. // Load vertex shader.
  151. mem = loadShader("vs_update");
  152. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  153. // Load fragment shader.
  154. mem = loadShader("fs_update");
  155. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  156. // Create program from shaders.
  157. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  158. // We can destroy vertex and fragment shader here since
  159. // their reference is kept inside bgfx after calling createProgram.
  160. // Vertex and fragment shader will be destroyed once program is
  161. // destroyed.
  162. bgfx::destroyVertexShader(vsh);
  163. bgfx::destroyFragmentShader(fsh);
  164. uint32_t blockSide = 0;
  165. uint32_t blockX = 0;
  166. uint32_t blockY = 0;
  167. const uint32_t blockWidth = 8;
  168. const uint32_t blockHeight = 8;
  169. const uint32_t textureSide = 256;
  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. bgfx::TextureInfo ti;
  178. bgfx::calcTextureSize(ti, blockWidth, blockHeight, 1, 1, bgfx::TextureFormat::BGRA8);
  179. uint8_t rr = rand()%255;
  180. uint8_t gg = rand()%255;
  181. uint8_t bb = rand()%255;
  182. int64_t updateTime = 0;
  183. while (entry::Event::Exit != entry::poll() )
  184. {
  185. // This dummy draw call is here to make sure that view 0 is cleared
  186. // if no other draw calls are submitted to view 0.
  187. bgfx::submit(0);
  188. int64_t now = bx::getHPCounter();
  189. static int64_t last = now;
  190. const int64_t frameTime = now - last;
  191. last = now;
  192. const int64_t freq = bx::getHPFrequency();
  193. const double toMs = 1000.0/double(freq);
  194. // Use debug font to print information about this example.
  195. bgfx::dbgTextClear();
  196. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/08-update");
  197. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating textures.");
  198. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  199. if (now > updateTime)
  200. {
  201. // updateTime = now + freq/10;
  202. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  203. uint8_t* data = (uint8_t*)mem->data;
  204. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  205. {
  206. data[0] = bb;
  207. data[1] = rr;
  208. data[2] = gg;
  209. data[3] = 0xff;
  210. data += 4;
  211. }
  212. bgfx::updateTextureCube(textureCube, blockSide, 0, blockX, blockY, blockWidth, blockHeight, mem);
  213. blockX += 8;
  214. if (blockX >= textureSide)
  215. {
  216. blockX = 0;
  217. blockY += 8;
  218. if (blockY >= textureSide)
  219. {
  220. rr = rand()%255;
  221. gg = rand()%255;
  222. bb = rand()%255;
  223. blockY = 0;
  224. ++blockSide;
  225. if (blockSide > 5)
  226. {
  227. blockSide = 0;
  228. }
  229. }
  230. }
  231. }
  232. float at[3] = { 0.0f, 0.0f, 0.0f };
  233. float eye[3] = { 0.0f, 0.0f, -5.0f };
  234. float view[16];
  235. float proj[16];
  236. mtxLookAt(view, eye, at);
  237. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  238. // Set view and projection matrix for view 0.
  239. bgfx::setViewTransform(0, view, proj);
  240. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  241. float mtx[16];
  242. mtxRotateXY(mtx, time, time*0.37f);
  243. // Set model matrix for rendering.
  244. bgfx::setTransform(mtx);
  245. // Set vertex and fragment shaders.
  246. bgfx::setProgram(program);
  247. // Set vertex and index buffer.
  248. bgfx::setVertexBuffer(vbh);
  249. bgfx::setIndexBuffer(ibh);
  250. // Bind texture.
  251. bgfx::setTexture(0, u_texCube, textureCube);
  252. // Set render states.
  253. bgfx::setState(BGFX_STATE_RGB_WRITE
  254. |BGFX_STATE_DEPTH_WRITE
  255. |BGFX_STATE_DEPTH_TEST_LESS
  256. );
  257. // Submit primitive for rendering to view 0.
  258. bgfx::submit(0);
  259. // Advance to next frame. Rendering thread will be kicked to
  260. // process submitted rendering primitives.
  261. bgfx::frame();
  262. }
  263. // Cleanup.
  264. bgfx::destroyIndexBuffer(ibh);
  265. bgfx::destroyVertexBuffer(vbh);
  266. bgfx::destroyProgram(program);
  267. // Shutdown bgfx.
  268. bgfx::shutdown();
  269. return 0;
  270. }