raymarch.cpp 7.9 KB

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