bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. struct PosNormalTangentTexcoordVertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_normal;
  13. uint32_t m_tangent;
  14. int16_t m_u;
  15. int16_t m_v;
  16. static void init()
  17. {
  18. ms_decl
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  22. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  23. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
  24. .end();
  25. }
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
  29. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  30. {
  31. union
  32. {
  33. uint32_t ui32;
  34. uint8_t arr[4];
  35. } un;
  36. un.arr[0] = _x;
  37. un.arr[1] = _y;
  38. un.arr[2] = _z;
  39. un.arr[3] = _w;
  40. return un.ui32;
  41. }
  42. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  43. {
  44. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  45. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  46. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  47. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  48. return packUint32(xx, yy, zz, ww);
  49. }
  50. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  51. {
  52. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  53. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  54. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  55. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  56. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  57. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  58. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  59. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  60. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  61. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  62. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  63. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  64. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  65. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  66. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  67. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  68. { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  69. { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  70. { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  71. { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  72. {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  73. {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  74. {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  75. {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  76. };
  77. static const uint16_t s_cubeIndices[36] =
  78. {
  79. 0, 2, 1,
  80. 1, 2, 3,
  81. 4, 5, 6,
  82. 5, 7, 6,
  83. 8, 10, 9,
  84. 9, 10, 11,
  85. 12, 13, 14,
  86. 13, 15, 14,
  87. 16, 18, 17,
  88. 17, 18, 19,
  89. 20, 21, 22,
  90. 21, 23, 22,
  91. };
  92. class Bump : public entry::AppI
  93. {
  94. void init(int _argc, char** _argv) BX_OVERRIDE
  95. {
  96. Args args(_argc, _argv);
  97. m_width = 1280;
  98. m_height = 720;
  99. m_debug = BGFX_DEBUG_TEXT;
  100. m_reset = BGFX_RESET_VSYNC;
  101. bgfx::init(args.m_type, args.m_pciId);
  102. bgfx::reset(m_width, m_height, m_reset);
  103. // Enable debug text.
  104. bgfx::setDebug(m_debug);
  105. // Set view 0 clear state.
  106. bgfx::setViewClear(0
  107. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  108. , 0x303030ff
  109. , 1.0f
  110. , 0
  111. );
  112. // Get renderer capabilities info.
  113. const bgfx::Caps* caps = bgfx::getCaps();
  114. m_instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  115. // Create vertex stream declaration.
  116. PosNormalTangentTexcoordVertex::init();
  117. calcTangents(s_cubeVertices
  118. , BX_COUNTOF(s_cubeVertices)
  119. , PosNormalTangentTexcoordVertex::ms_decl
  120. , s_cubeIndices
  121. , BX_COUNTOF(s_cubeIndices)
  122. );
  123. // Create static vertex buffer.
  124. m_vbh = bgfx::createVertexBuffer(
  125. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  126. , PosNormalTangentTexcoordVertex::ms_decl
  127. );
  128. // Create static index buffer.
  129. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  130. // Create texture sampler uniforms.
  131. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  132. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  133. m_numLights = 4;
  134. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
  135. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, m_numLights);
  136. // Create program from shaders.
  137. m_program = loadProgram(m_instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  138. // Load diffuse texture.
  139. m_textureColor = loadTexture("fieldstone-rgba.dds");
  140. // Load normal texture.
  141. m_textureNormal = loadTexture("fieldstone-n.dds");
  142. m_timeOffset = bx::getHPCounter();
  143. }
  144. virtual int shutdown() BX_OVERRIDE
  145. {
  146. // Cleanup.
  147. bgfx::destroyIndexBuffer(m_ibh);
  148. bgfx::destroyVertexBuffer(m_vbh);
  149. bgfx::destroyProgram(m_program);
  150. bgfx::destroyTexture(m_textureColor);
  151. bgfx::destroyTexture(m_textureNormal);
  152. bgfx::destroyUniform(s_texColor);
  153. bgfx::destroyUniform(s_texNormal);
  154. bgfx::destroyUniform(u_lightPosRadius);
  155. bgfx::destroyUniform(u_lightRgbInnerR);
  156. // Shutdown bgfx.
  157. bgfx::shutdown();
  158. return 0;
  159. }
  160. bool update() BX_OVERRIDE
  161. {
  162. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  163. {
  164. // Set view 0 default viewport.
  165. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  166. // This dummy draw call is here to make sure that view 0 is cleared
  167. // if no other draw calls are submitted to view 0.
  168. bgfx::touch(0);
  169. int64_t now = bx::getHPCounter();
  170. static int64_t last = now;
  171. const int64_t frameTime = now - last;
  172. last = now;
  173. const double freq = double(bx::getHPFrequency() );
  174. const double toMs = 1000.0/freq;
  175. float time = (float)( (now-m_timeOffset)/freq);
  176. // Use debug font to print information about this example.
  177. bgfx::dbgTextClear();
  178. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
  179. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
  180. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  181. float at[3] = { 0.0f, 0.0f, 0.0f };
  182. float eye[3] = { 0.0f, 0.0f, -7.0f };
  183. // Set view and projection matrix for view 0.
  184. const bgfx::HMD* hmd = bgfx::getHMD();
  185. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  186. {
  187. float view[16];
  188. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  189. float proj[16];
  190. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  191. bgfx::setViewTransform(0, view, proj);
  192. // Set view 0 default viewport.
  193. //
  194. // Use HMD's width/height since HMD's internal frame buffer size
  195. // might be much larger than window size.
  196. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  197. }
  198. else
  199. {
  200. float view[16];
  201. bx::mtxLookAt(view, eye, at);
  202. float proj[16];
  203. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  204. bgfx::setViewTransform(0, view, proj);
  205. // Set view 0 default viewport.
  206. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  207. }
  208. float lightPosRadius[4][4];
  209. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  210. {
  211. lightPosRadius[ii][0] = sinf( (time*(0.1f + ii*0.17f) + ii*bx::piHalf*1.37f ) )*3.0f;
  212. lightPosRadius[ii][1] = cosf( (time*(0.2f + ii*0.29f) + ii*bx::piHalf*1.49f ) )*3.0f;
  213. lightPosRadius[ii][2] = -2.5f;
  214. lightPosRadius[ii][3] = 3.0f;
  215. }
  216. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  217. float lightRgbInnerR[4][4] =
  218. {
  219. { 1.0f, 0.7f, 0.2f, 0.8f },
  220. { 0.7f, 0.2f, 1.0f, 0.8f },
  221. { 0.2f, 1.0f, 0.7f, 0.8f },
  222. { 1.0f, 0.4f, 0.2f, 0.8f },
  223. };
  224. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  225. const uint16_t instanceStride = 64;
  226. const uint16_t numInstances = 3;
  227. if (m_instancingSupported)
  228. {
  229. // Write instance data for 3x3 cubes.
  230. for (uint32_t yy = 0; yy < 3; ++yy)
  231. {
  232. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  233. if (NULL != idb)
  234. {
  235. uint8_t* data = idb->data;
  236. for (uint32_t xx = 0; xx < 3; ++xx)
  237. {
  238. float* mtx = (float*)data;
  239. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  240. mtx[12] = -3.0f + float(xx)*3.0f;
  241. mtx[13] = -3.0f + float(yy)*3.0f;
  242. mtx[14] = 0.0f;
  243. data += instanceStride;
  244. }
  245. // Set instance data buffer.
  246. bgfx::setInstanceDataBuffer(idb, numInstances);
  247. // Set vertex and index buffer.
  248. bgfx::setVertexBuffer(m_vbh);
  249. bgfx::setIndexBuffer(m_ibh);
  250. // Bind textures.
  251. bgfx::setTexture(0, s_texColor, m_textureColor);
  252. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  253. // Set render states.
  254. bgfx::setState(0
  255. | BGFX_STATE_RGB_WRITE
  256. | BGFX_STATE_ALPHA_WRITE
  257. | BGFX_STATE_DEPTH_WRITE
  258. | BGFX_STATE_DEPTH_TEST_LESS
  259. | BGFX_STATE_MSAA
  260. );
  261. // Submit primitive for rendering to view 0.
  262. bgfx::submit(0, m_program);
  263. }
  264. }
  265. }
  266. else
  267. {
  268. for (uint32_t yy = 0; yy < 3; ++yy)
  269. {
  270. for (uint32_t xx = 0; xx < 3; ++xx)
  271. {
  272. float mtx[16];
  273. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  274. mtx[12] = -3.0f + float(xx)*3.0f;
  275. mtx[13] = -3.0f + float(yy)*3.0f;
  276. mtx[14] = 0.0f;
  277. // Set transform for draw call.
  278. bgfx::setTransform(mtx);
  279. // Set vertex and index buffer.
  280. bgfx::setVertexBuffer(m_vbh);
  281. bgfx::setIndexBuffer(m_ibh);
  282. // Bind textures.
  283. bgfx::setTexture(0, s_texColor, m_textureColor);
  284. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  285. // Set render states.
  286. bgfx::setState(0
  287. | BGFX_STATE_RGB_WRITE
  288. | BGFX_STATE_ALPHA_WRITE
  289. | BGFX_STATE_DEPTH_WRITE
  290. | BGFX_STATE_DEPTH_TEST_LESS
  291. | BGFX_STATE_MSAA
  292. );
  293. // Submit primitive for rendering to view 0.
  294. bgfx::submit(0, m_program);
  295. }
  296. }
  297. }
  298. // Advance to next frame. Rendering thread will be kicked to
  299. // process submitted rendering primitives.
  300. bgfx::frame();
  301. return true;
  302. }
  303. return false;
  304. }
  305. bgfx::VertexBufferHandle m_vbh;
  306. bgfx::IndexBufferHandle m_ibh;
  307. bgfx::UniformHandle s_texColor;
  308. bgfx::UniformHandle s_texNormal;
  309. bgfx::UniformHandle u_lightPosRadius;
  310. bgfx::UniformHandle u_lightRgbInnerR;
  311. bgfx::ProgramHandle m_program;
  312. bgfx::TextureHandle m_textureColor;
  313. bgfx::TextureHandle m_textureNormal;
  314. uint16_t m_numLights;
  315. bool m_instancingSupported;
  316. uint32_t m_width;
  317. uint32_t m_height;
  318. uint32_t m_debug;
  319. uint32_t m_reset;
  320. int64_t m_timeOffset;
  321. };
  322. ENTRY_IMPLEMENT_MAIN(Bump);