raymarch.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 PosColorTexCoord0Vertex
  13. {
  14. float m_x;
  15. float m_y;
  16. float m_z;
  17. uint32_t m_abgr;
  18. float m_u;
  19. float m_v;
  20. };
  21. static bgfx::VertexDecl s_PosColorTexCoord0Decl;
  22. static const char* s_shaderPath = NULL;
  23. static bool s_flipV = false;
  24. static void shaderFilePath(char* _out, const char* _name)
  25. {
  26. strcpy(_out, s_shaderPath);
  27. strcat(_out, _name);
  28. strcat(_out, ".bin");
  29. }
  30. long int fsize(FILE* _file)
  31. {
  32. long int pos = ftell(_file);
  33. fseek(_file, 0L, SEEK_END);
  34. long int size = ftell(_file);
  35. fseek(_file, pos, SEEK_SET);
  36. return size;
  37. }
  38. static const bgfx::Memory* load(const char* _filePath)
  39. {
  40. FILE* file = fopen(_filePath, "rb");
  41. if (NULL != file)
  42. {
  43. uint32_t size = (uint32_t)fsize(file);
  44. const bgfx::Memory* mem = bgfx::alloc(size+1);
  45. size_t ignore = fread(mem->data, 1, size, file);
  46. BX_UNUSED(ignore);
  47. fclose(file);
  48. mem->data[mem->size-1] = '\0';
  49. return mem;
  50. }
  51. return NULL;
  52. }
  53. static const bgfx::Memory* loadShader(const char* _name)
  54. {
  55. char filePath[512];
  56. shaderFilePath(filePath, _name);
  57. return load(filePath);
  58. }
  59. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  60. {
  61. const bgfx::Memory* mem;
  62. // Load vertex shader.
  63. mem = loadShader(_vsName);
  64. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  65. // Load fragment shader.
  66. mem = loadShader(_fsName);
  67. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  68. // Create program from shaders.
  69. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  70. // We can destroy vertex and fragment shader here since
  71. // their reference is kept inside bgfx after calling createProgram.
  72. // Vertex and fragment shader will be destroyed once program is
  73. // destroyed.
  74. bgfx::destroyVertexShader(vsh);
  75. bgfx::destroyFragmentShader(fsh);
  76. return program;
  77. }
  78. bool allocTransientBuffers(bgfx::TransientVertexBuffer* _tvb, const bgfx::VertexDecl& _decl, uint16_t _numVertices, bgfx::TransientIndexBuffer* _tib, uint16_t _numIndices)
  79. {
  80. if (bgfx::checkAvailTransientVertexBuffer(_numVertices, _decl)
  81. && bgfx::checkAvailTransientIndexBuffer(_numIndices) )
  82. {
  83. bgfx::allocTransientVertexBuffer(_tvb, _numVertices, _decl);
  84. bgfx::allocTransientIndexBuffer(_tib, _numIndices);
  85. return true;
  86. }
  87. return false;
  88. }
  89. void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height)
  90. {
  91. bgfx::TransientVertexBuffer tvb;
  92. bgfx::TransientIndexBuffer tib;
  93. if (allocTransientBuffers(&tvb, s_PosColorTexCoord0Decl, 4, &tib, 6) )
  94. {
  95. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)tvb.data;
  96. float zz = 0.0f;
  97. const float minx = _x;
  98. const float maxx = _x + _width;
  99. const float miny = _y;
  100. const float maxy = _y + _height;
  101. float minu = -1.0f;
  102. float minv = -1.0f;
  103. float maxu = 1.0f;
  104. float maxv = 1.0f;
  105. vertex[0].m_x = minx;
  106. vertex[0].m_y = miny;
  107. vertex[0].m_z = zz;
  108. vertex[0].m_abgr = 0xff0000ff;
  109. vertex[0].m_u = minu;
  110. vertex[0].m_v = minv;
  111. vertex[1].m_x = maxx;
  112. vertex[1].m_y = miny;
  113. vertex[1].m_z = zz;
  114. vertex[1].m_abgr = 0xff00ff00;
  115. vertex[1].m_u = maxu;
  116. vertex[1].m_v = minv;
  117. vertex[2].m_x = maxx;
  118. vertex[2].m_y = maxy;
  119. vertex[2].m_z = zz;
  120. vertex[2].m_abgr = 0xffff0000;
  121. vertex[2].m_u = maxu;
  122. vertex[2].m_v = maxv;
  123. vertex[3].m_x = minx;
  124. vertex[3].m_y = maxy;
  125. vertex[3].m_z = zz;
  126. vertex[3].m_abgr = 0xffffffff;
  127. vertex[3].m_u = minu;
  128. vertex[3].m_v = maxv;
  129. uint16_t* indices = (uint16_t*)tib.data;
  130. indices[0] = 0;
  131. indices[1] = 1;
  132. indices[2] = 2;
  133. indices[3] = 0;
  134. indices[4] = 2;
  135. indices[5] = 3;
  136. bgfx::setProgram(_program);
  137. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE|BGFX_STATE_DEPTH_TEST_LESS|BGFX_STATE_DEPTH_WRITE);
  138. bgfx::setIndexBuffer(&tib);
  139. bgfx::setVertexBuffer(&tvb);
  140. bgfx::submit(_view);
  141. }
  142. }
  143. int _main_(int _argc, char** _argv)
  144. {
  145. bgfx::init();
  146. bgfx::reset(1280, 720);
  147. // Enable debug text.
  148. bgfx::setDebug(BGFX_DEBUG_TEXT);
  149. // Set view 0 default viewport.
  150. bgfx::setViewRect(0, 0, 0, 1280, 720);
  151. // Set view 1 default viewport.
  152. bgfx::setViewRect(1, 0, 0, 1280, 720);
  153. // Set view 0 clear state.
  154. bgfx::setViewClear(0
  155. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  156. , 0x303030ff
  157. , 1.0f
  158. , 0
  159. );
  160. // Setup root path for binary shaders. Shader binaries are different
  161. // for each renderer.
  162. switch (bgfx::getRendererType() )
  163. {
  164. default:
  165. case bgfx::RendererType::Direct3D9:
  166. s_shaderPath = "shaders/dx9/";
  167. break;
  168. case bgfx::RendererType::Direct3D11:
  169. s_shaderPath = "shaders/dx11/";
  170. break;
  171. case bgfx::RendererType::OpenGL:
  172. s_shaderPath = "shaders/glsl/";
  173. s_flipV = true;
  174. break;
  175. case bgfx::RendererType::OpenGLES2:
  176. case bgfx::RendererType::OpenGLES3:
  177. s_shaderPath = "shaders/gles/";
  178. s_flipV = true;
  179. break;
  180. }
  181. // Create vertex stream declaration.
  182. s_PosColorTexCoord0Decl.begin();
  183. s_PosColorTexCoord0Decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  184. s_PosColorTexCoord0Decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  185. s_PosColorTexCoord0Decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  186. s_PosColorTexCoord0Decl.end();
  187. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f);
  188. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Uniform4x4fv);
  189. bgfx::UniformHandle u_lightDir = bgfx::createUniform("u_lightDir", bgfx::UniformType::Uniform3fv);
  190. bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching");
  191. while (true)
  192. {
  193. // This dummy draw call is here to make sure that view 0 is cleared
  194. // if no other draw calls are submitted to viewZ 0.
  195. bgfx::submit(0);
  196. int64_t now = bx::getHPCounter();
  197. static int64_t last = now;
  198. const int64_t frameTime = now - last;
  199. last = now;
  200. const double freq = double(bx::getHPFrequency() );
  201. const double toMs = 1000.0/freq;
  202. // Use debug font to print information about this example.
  203. bgfx::dbgTextClear();
  204. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch");
  205. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.");
  206. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  207. float at[3] = { 0.0f, 0.0f, 0.0f };
  208. float eye[3] = { 0.0f, 0.0f, -15.0f };
  209. float view[16];
  210. float proj[16];
  211. mtxLookAt(view, eye, at);
  212. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  213. // Set view and projection matrix for view 1.
  214. bgfx::setViewTransform(0, view, proj);
  215. float ortho[16];
  216. mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f);
  217. // Set view and projection matrix for view 0.
  218. bgfx::setViewTransform(1, NULL, ortho);
  219. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  220. float vp[16];
  221. mtxMul(vp, view, proj);
  222. float mtx[16];
  223. mtxRotateXY(mtx
  224. , time
  225. , time*0.37f
  226. );
  227. float mtxInv[16];
  228. mtxInverse(mtxInv, mtx);
  229. float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
  230. float lightDirModelN[4];
  231. vec3Norm(lightDirModelN, lightDirModel);
  232. float lightDir[4];
  233. vec4MulMtx(lightDir, lightDirModelN, mtxInv);
  234. bgfx::setUniform(u_lightDir, lightDir);
  235. float mvp[16];
  236. mtxMul(mvp, mtx, vp);
  237. float invMvp[16];
  238. mtxInverse(invMvp, mvp);
  239. bgfx::setUniform(u_mtx, invMvp);
  240. bgfx::setUniform(u_time, &time);
  241. renderScreenSpaceQuad(1, raymarching, 0.0f, 0.0f, 1280.0f, 720.0f);
  242. // Advance to next frame. Rendering thread will be kicked to
  243. // process submitted rendering primitives.
  244. bgfx::frame();
  245. }
  246. // Cleanup.
  247. bgfx::destroyProgram(raymarching);
  248. bgfx::destroyUniform(u_time);
  249. bgfx::destroyUniform(u_mtx);
  250. bgfx::destroyUniform(u_lightDir);
  251. // Shutdown bgfx.
  252. bgfx::shutdown();
  253. return 0;
  254. }