update.cpp 7.9 KB

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