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