update.cpp 7.9 KB

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