raymarch.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2011-2013 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::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] = 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::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. int64_t timeOffset = bx::getHPCounter();
  192. while (!entry::processEvents(width, height, debug, reset) )
  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, float(width)/float(height), 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()-timeOffset)/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] = { 0.0f, 0.0f, 0.0f, 0.0f };
  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. }