update.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <stdio.h>
  14. #include <string.h>
  15. struct PosColorVertex
  16. {
  17. float m_x;
  18. float m_y;
  19. float m_z;
  20. float m_u;
  21. float m_v;
  22. float m_w;
  23. };
  24. static bgfx::VertexDecl s_PosTexcoordDecl;
  25. static PosColorVertex s_cubeVertices[24] =
  26. {
  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. { 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f },
  51. };
  52. static const uint16_t s_cubeIndices[36] =
  53. {
  54. 0, 2, 1, // 0
  55. 1, 2, 3,
  56. 4, 5, 6, // 2
  57. 5, 7, 6,
  58. 8, 9, 10, // 4
  59. 9, 11, 10,
  60. 12, 13, 14, // 6
  61. 14, 13, 15,
  62. 16, 17, 18, // 8
  63. 18, 17, 19,
  64. 20, 21, 22, // 10
  65. 21, 23, 22,
  66. };
  67. static const char* s_shaderPath = NULL;
  68. static void shaderFilePath(char* _out, const char* _name)
  69. {
  70. strcpy(_out, s_shaderPath);
  71. strcat(_out, _name);
  72. strcat(_out, ".bin");
  73. }
  74. long int fsize(FILE* _file)
  75. {
  76. long int pos = ftell(_file);
  77. fseek(_file, 0L, SEEK_END);
  78. long int size = ftell(_file);
  79. fseek(_file, pos, SEEK_SET);
  80. return size;
  81. }
  82. static const bgfx::Memory* load(const char* _filePath)
  83. {
  84. FILE* file = fopen(_filePath, "rb");
  85. if (NULL != file)
  86. {
  87. uint32_t size = (uint32_t)fsize(file);
  88. const bgfx::Memory* mem = bgfx::alloc(size+1);
  89. size_t ignore = fread(mem->data, 1, size, file);
  90. BX_UNUSED(ignore);
  91. fclose(file);
  92. mem->data[mem->size-1] = '\0';
  93. return mem;
  94. }
  95. return NULL;
  96. }
  97. static const bgfx::Memory* loadShader(const char* _name)
  98. {
  99. char filePath[512];
  100. shaderFilePath(filePath, _name);
  101. return load(filePath);
  102. }
  103. int _main_(int _argc, char** _argv)
  104. {
  105. uint32_t width = 1280;
  106. uint32_t height = 720;
  107. uint32_t debug = BGFX_DEBUG_TEXT;
  108. bgfx::init();
  109. bgfx::reset(width, height);
  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. uint32_t blockSide = 0;
  167. uint32_t blockX = 0;
  168. uint32_t blockY = 0;
  169. const uint32_t blockWidth = 8;
  170. const uint32_t blockHeight = 8;
  171. const uint32_t textureSide = 256;
  172. bgfx::TextureHandle textureCube =
  173. bgfx::createTextureCube(6
  174. , textureSide
  175. , 1
  176. , bgfx::TextureFormat::BGRA8
  177. , BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
  178. );
  179. bgfx::TextureInfo ti;
  180. bgfx::calcTextureSize(ti, blockWidth, blockHeight, 1, 1, bgfx::TextureFormat::BGRA8);
  181. uint8_t rr = rand()%255;
  182. uint8_t gg = rand()%255;
  183. uint8_t bb = rand()%255;
  184. int64_t updateTime = 0;
  185. while (!processEvents(width, height, debug) )
  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. // updateTime = now + freq/10;
  206. const bgfx::Memory* mem = bgfx::alloc(ti.storageSize);
  207. uint8_t* data = (uint8_t*)mem->data;
  208. for (uint32_t ii = 0, num = ti.storageSize*8/ti.bitsPerPixel; ii < num; ++ii)
  209. {
  210. data[0] = bb;
  211. data[1] = rr;
  212. data[2] = gg;
  213. data[3] = 0xff;
  214. data += 4;
  215. }
  216. bgfx::updateTextureCube(textureCube, blockSide, 0, blockX, blockY, blockWidth, blockHeight, mem);
  217. blockX += 8;
  218. if (blockX >= textureSide)
  219. {
  220. blockX = 0;
  221. blockY += 8;
  222. if (blockY >= textureSide)
  223. {
  224. rr = rand()%255;
  225. gg = rand()%255;
  226. bb = rand()%255;
  227. blockY = 0;
  228. ++blockSide;
  229. if (blockSide > 5)
  230. {
  231. blockSide = 0;
  232. }
  233. }
  234. }
  235. }
  236. float at[3] = { 0.0f, 0.0f, 0.0f };
  237. float eye[3] = { 0.0f, 0.0f, -5.0f };
  238. float view[16];
  239. float proj[16];
  240. mtxLookAt(view, eye, at);
  241. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  242. // Set view and projection matrix for view 0.
  243. bgfx::setViewTransform(0, view, proj);
  244. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  245. float mtx[16];
  246. mtxRotateXY(mtx, time, time*0.37f);
  247. // Set model matrix for rendering.
  248. bgfx::setTransform(mtx);
  249. // Set vertex and fragment shaders.
  250. bgfx::setProgram(program);
  251. // Set vertex and index buffer.
  252. bgfx::setVertexBuffer(vbh);
  253. bgfx::setIndexBuffer(ibh);
  254. // Bind texture.
  255. bgfx::setTexture(0, u_texCube, textureCube);
  256. // Set render states.
  257. bgfx::setState(BGFX_STATE_RGB_WRITE
  258. |BGFX_STATE_DEPTH_WRITE
  259. |BGFX_STATE_DEPTH_TEST_LESS
  260. );
  261. // Submit primitive for rendering to view 0.
  262. bgfx::submit(0);
  263. // Advance to next frame. Rendering thread will be kicked to
  264. // process submitted rendering primitives.
  265. bgfx::frame();
  266. }
  267. // Cleanup.
  268. bgfx::destroyIndexBuffer(ibh);
  269. bgfx::destroyVertexBuffer(vbh);
  270. bgfx::destroyProgram(program);
  271. // Shutdown bgfx.
  272. bgfx::shutdown();
  273. return 0;
  274. }