raymarch.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include "entry/entry.h"
  9. #include "fpumath.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::ShaderHandle vsh = bgfx::createShader(mem);
  65. // Load fragment shader.
  66. mem = loadShader(_fsName);
  67. bgfx::ShaderHandle fsh = bgfx::createShader(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::destroyShader(vsh);
  75. bgfx::destroyShader(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] = 2;
  132. indices[2] = 1;
  133. indices[3] = 0;
  134. indices[4] = 3;
  135. indices[5] = 2;
  136. bgfx::setProgram(_program);
  137. bgfx::setState(BGFX_STATE_DEFAULT);
  138. bgfx::setIndexBuffer(&tib);
  139. bgfx::setVertexBuffer(&tvb);
  140. bgfx::submit(_view);
  141. }
  142. }
  143. int _main_(int /*_argc*/, char** /*_argv*/)
  144. {
  145. uint32_t width = 1280;
  146. uint32_t height = 720;
  147. uint32_t debug = BGFX_DEBUG_TEXT;
  148. uint32_t reset = BGFX_RESET_VSYNC;
  149. bgfx::init();
  150. bgfx::reset(width, height, reset);
  151. // Enable debug text.
  152. bgfx::setDebug(debug);
  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::OpenGLES:
  176. s_shaderPath = "shaders/gles/";
  177. s_flipV = true;
  178. break;
  179. }
  180. // Create vertex stream declaration.
  181. s_PosColorTexCoord0Decl.begin();
  182. s_PosColorTexCoord0Decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  183. s_PosColorTexCoord0Decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  184. s_PosColorTexCoord0Decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  185. s_PosColorTexCoord0Decl.end();
  186. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f);
  187. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Uniform4x4fv);
  188. bgfx::UniformHandle u_lightDir = bgfx::createUniform("u_lightDir", bgfx::UniformType::Uniform3fv);
  189. bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching");
  190. int64_t timeOffset = bx::getHPCounter();
  191. while (!entry::processEvents(width, height, debug, reset) )
  192. {
  193. // Set view 0 default viewport.
  194. bgfx::setViewRect(0, 0, 0, width, height);
  195. // Set view 1 default viewport.
  196. bgfx::setViewRect(1, 0, 0, width, height);
  197. // This dummy draw call is here to make sure that view 0 is cleared
  198. // if no other draw calls are submitted to viewZ 0.
  199. bgfx::submit(0);
  200. int64_t now = bx::getHPCounter();
  201. static int64_t last = now;
  202. const int64_t frameTime = now - last;
  203. last = now;
  204. const double freq = double(bx::getHPFrequency() );
  205. const double toMs = 1000.0/freq;
  206. // Use debug font to print information about this example.
  207. bgfx::dbgTextClear();
  208. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch");
  209. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.");
  210. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  211. float at[3] = { 0.0f, 0.0f, 0.0f };
  212. float eye[3] = { 0.0f, 0.0f, -15.0f };
  213. float view[16];
  214. float proj[16];
  215. mtxLookAt(view, eye, at);
  216. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  217. // Set view and projection matrix for view 1.
  218. bgfx::setViewTransform(0, view, proj);
  219. float ortho[16];
  220. mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f);
  221. // Set view and projection matrix for view 0.
  222. bgfx::setViewTransform(1, NULL, ortho);
  223. float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) );
  224. float vp[16];
  225. mtxMul(vp, view, proj);
  226. float mtx[16];
  227. mtxRotateXY(mtx
  228. , time
  229. , time*0.37f
  230. );
  231. float mtxInv[16];
  232. mtxInverse(mtxInv, mtx);
  233. float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
  234. float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  235. vec3Norm(lightDirModelN, lightDirModel);
  236. float lightDir[4];
  237. vec4MulMtx(lightDir, lightDirModelN, mtxInv);
  238. bgfx::setUniform(u_lightDir, lightDir);
  239. float mvp[16];
  240. mtxMul(mvp, mtx, vp);
  241. float invMvp[16];
  242. mtxInverse(invMvp, mvp);
  243. bgfx::setUniform(u_mtx, invMvp);
  244. bgfx::setUniform(u_time, &time);
  245. renderScreenSpaceQuad(1, raymarching, 0.0f, 0.0f, 1280.0f, 720.0f);
  246. // Advance to next frame. Rendering thread will be kicked to
  247. // process submitted rendering primitives.
  248. bgfx::frame();
  249. }
  250. // Cleanup.
  251. bgfx::destroyProgram(raymarching);
  252. bgfx::destroyUniform(u_time);
  253. bgfx::destroyUniform(u_mtx);
  254. bgfx::destroyUniform(u_lightDir);
  255. // Shutdown bgfx.
  256. bgfx::shutdown();
  257. return 0;
  258. }