You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

586 lines
19 KiB

  1. //
  2. // Lol Engine — Fractal tutorial
  3. //
  4. // Copyright © 2011—2020 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <memory>
  16. #include <sstream>
  17. #include <lol/engine.h>
  18. #include <lol/real>
  19. #include <lol/vector>
  20. #include <lol/transform>
  21. #include <lol/color>
  22. #include "loldebug.h"
  23. #define USE_REAL 0
  24. using namespace lol;
  25. LOLFX_RESOURCE_DECLARE(11_fractal);
  26. class Fractal : public WorldEntity
  27. {
  28. public:
  29. Fractal(ivec2 const &size)
  30. {
  31. /* Ensure texture size is a multiple of 16 for better aligned
  32. * data access. Store the dimensions of a texel for our shader,
  33. * as well as the half-size of the screen. */
  34. m_size = size;
  35. m_size.x = (m_size.x + 15) & ~15;
  36. m_size.y = (m_size.y + 15) & ~15;
  37. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / (vec4)m_size.xyxy;
  38. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * (vec4)m_size.xyxy;
  39. /* Window size decides the world aspect ratio. For instance, 640×480
  40. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  41. m_window_size = Video::GetSize();
  42. if (m_window_size.y < m_window_size.x)
  43. m_window2world = 0.5 / m_window_size.y;
  44. else
  45. m_window2world = 0.5 / m_window_size.x;
  46. m_texel2world = (dvec2)m_window_size / (dvec2)m_size * m_window2world;
  47. m_oldmouse = ivec2(0, 0);
  48. m_pixels.resize(m_size.x * m_size.y);
  49. for (int i = 0; i < 4; i++)
  50. {
  51. m_deltashift[i] = real("0");
  52. m_deltascale[i] = real("1");
  53. m_dirty[i] = 2;
  54. }
  55. m_view.center = rcmplx(-0.75, 0.0);
  56. m_zoom_speed = 0.0;
  57. m_view.translate = rcmplx(0.0, 0.0);
  58. m_view.radius = 5.0;
  59. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  60. {
  61. double f = (double)i / PALETTE_STEP;
  62. vec3 hsv(lol::fmod(i * 0.001f, 1.f),
  63. 0.3 * lol::sin(f * 0.27 + 2.0) + 0.3,
  64. 0.3 * lol::sin(f * 0.21 - 2.6) + 0.6);
  65. vec3 rgb = color::hsv_to_rgb(hsv);
  66. if (f < 7.0)
  67. {
  68. rgb *= f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  69. }
  70. uint8_t red = (uint8_t)(rgb.r * 256);
  71. uint8_t green = (uint8_t)(rgb.g * 256);
  72. uint8_t blue = (uint8_t)(rgb.b * 256);
  73. m_palette.push_back(u8vec4(blue, green, red, 255));
  74. }
  75. m_zoomtext = new Text("", "data/font/ascii.png");
  76. m_zoomtext->SetPos(vec3(5, (float)m_window_size.y - 15, 1));
  77. Ticker::Ref(m_zoomtext);
  78. m_centertext = new Text("", "data/font/ascii.png");
  79. m_centertext->SetPos(vec3(5, (float)m_window_size.y - 29, 1));
  80. Ticker::Ref(m_centertext);
  81. m_mousetext = new Text("", "data/font/ascii.png");
  82. m_mousetext->SetPos(vec3(5, (float)m_window_size.y - 43, 1));
  83. Ticker::Ref(m_mousetext);
  84. m_position = vec3::zero;
  85. m_aabb.aa = m_position;
  86. m_aabb.bb = vec3((vec2)m_window_size, 0);
  87. if (thread::has_threads())
  88. {
  89. // Spawn worker threads and wait for their readiness
  90. for (int i = 0; i < MAX_THREADS; i++)
  91. m_threads[i] = new thread(std::bind(&Fractal::DoWorkHelper, this, std::placeholders::_1));
  92. for (int i = 0; i < MAX_THREADS; i++)
  93. m_spawnqueue.pop();
  94. }
  95. }
  96. ~Fractal()
  97. {
  98. if (thread::has_threads())
  99. {
  100. // Signal worker threads for completion and wait for them to quit
  101. for (int i = 0; i < MAX_THREADS; i++)
  102. m_jobqueue.push(-1);
  103. for (int i = 0; i < MAX_THREADS; i++)
  104. m_donequeue.pop();
  105. }
  106. Ticker::Unref(m_centertext);
  107. Ticker::Unref(m_mousetext);
  108. Ticker::Unref(m_zoomtext);
  109. }
  110. inline f128cmplx TexelToWorldOffset(vec2 texel)
  111. {
  112. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  113. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  114. return m_view.radius * f128cmplx(dx, dy);
  115. }
  116. inline f128cmplx ScreenToWorldOffset(vec2 pixel)
  117. {
  118. /* No 0.5 offset here, because we want to be able to position the
  119. * mouse at (0,0) exactly. */
  120. double dx = pixel.x - m_window_size.x / 2;
  121. double dy = m_window_size.y / 2 - pixel.y;
  122. return m_view.radius * m_window2world * f128cmplx(dx, dy);
  123. }
  124. virtual void tick_game(float seconds)
  125. {
  126. WorldEntity::tick_game(seconds);
  127. auto mouse = input::mouse();
  128. auto keyboard = input::keyboard();
  129. vec2 mousepos(mouse->axis(input::axis::ScreenX),
  130. mouse->axis(input::axis::ScreenY));
  131. int prev_frame = (m_frame + 4) % 4;
  132. m_frame = (m_frame + 1) % 4;
  133. if (keyboard->key_pressed(input::key::SC_Space))
  134. {
  135. m_julia = !m_julia;
  136. if (m_julia)
  137. {
  138. m_saved_view = m_view;
  139. m_view.r0 = m_view.center + rcmplx(ScreenToWorldOffset(mousepos));
  140. }
  141. else
  142. {
  143. m_view = m_saved_view;
  144. }
  145. for (auto & flag : m_dirty)
  146. flag = 2;
  147. }
  148. rcmplx worldmouse = m_view.center + rcmplx(ScreenToWorldOffset(mousepos));
  149. if (mouse->button(input::button::BTN_Middle))
  150. {
  151. if (!m_drag)
  152. {
  153. m_oldmouse = (ivec2)mousepos;
  154. m_drag = true;
  155. }
  156. m_view.translate = rcmplx(ScreenToWorldOffset((vec2)m_oldmouse)
  157. - ScreenToWorldOffset(mousepos));
  158. /* XXX: the purpose of this hack is to avoid translating by
  159. * an exact number of pixels. If this were to happen, the step()
  160. * optimisation for i915 cards in our shader would behave
  161. * incorrectly because a quarter of the pixels in the image
  162. * would have tied rankings in the distance calculation. */
  163. m_view.translate *= real(1023.0 / 1024.0);
  164. m_oldmouse = (ivec2)mousepos;
  165. }
  166. else
  167. {
  168. m_drag = false;
  169. if (m_view.translate != rcmplx(0.0, 0.0))
  170. {
  171. m_view.translate *= real(std::pow(2.0, -seconds * 5.0));
  172. if ((double)norm(m_view.translate) < m_view.radius * 1e-4)
  173. m_view.translate = rcmplx(0.0, 0.0);
  174. }
  175. }
  176. bool hold_right = mouse->button(input::button::BTN_Right);
  177. bool hold_left = mouse->button(input::button::BTN_Left);
  178. if ((hold_right || hold_left) && mousepos.x != -1)
  179. {
  180. double zoom = hold_right ? -0.5 : 0.5;
  181. m_zoom_speed += zoom * seconds;
  182. if (m_zoom_speed / zoom > 5e-3f)
  183. m_zoom_speed = zoom * 5e-3f;
  184. }
  185. else if (m_zoom_speed)
  186. {
  187. m_zoom_speed *= std::pow(2.0, -seconds * 5.0);
  188. if (lol::fabs(m_zoom_speed) < 1e-5 || m_drag)
  189. m_zoom_speed = 0.0;
  190. }
  191. if (m_zoom_speed || m_view.translate != rcmplx(0.0, 0.0))
  192. {
  193. rcmplx oldcenter = m_view.center;
  194. double oldradius = m_view.radius;
  195. double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed);
  196. if (m_view.radius * zoom > 8.0)
  197. {
  198. m_zoom_speed *= -1.0;
  199. zoom = 8.0 / m_view.radius;
  200. }
  201. else if (m_view.radius * zoom < MAX_ZOOM)
  202. {
  203. m_zoom_speed *= -1.0;
  204. zoom = MAX_ZOOM / m_view.radius;
  205. }
  206. m_view.radius *= zoom;
  207. m_view.center += m_view.translate;
  208. m_view.center = (m_view.center - worldmouse) * real(zoom) + worldmouse;
  209. worldmouse = m_view.center
  210. + rcmplx(ScreenToWorldOffset(mousepos));
  211. /* Store the transformation properties to go from m_frame - 1
  212. * to m_frame. */
  213. m_deltashift[prev_frame] = (m_view.center - oldcenter) / real(oldradius);
  214. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  215. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  216. m_deltascale[prev_frame] = m_view.radius / oldradius;
  217. for (auto & flag : m_dirty)
  218. flag = 2;
  219. }
  220. else
  221. {
  222. /* If settings didn't change, set transformation from previous
  223. * frame to identity. */
  224. m_deltashift[prev_frame] = real::R_0();
  225. m_deltascale[prev_frame] = real::R_1();
  226. }
  227. /* Transformation from current frame to current frame is always
  228. * identity. */
  229. m_zoom_settings[m_frame][0] = 0.0f;
  230. m_zoom_settings[m_frame][1] = 0.0f;
  231. m_zoom_settings[m_frame][2] = 1.0f;
  232. /* Compute transformation from other frames to current frame */
  233. for (int i = 0; i < 3; i++)
  234. {
  235. int prev_index = (m_frame + 4 - i) % 4;
  236. int cur_index = (m_frame + 3 - i) % 4;
  237. auto t0 = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  238. auto t1 = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  239. auto t2 = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  240. m_zoom_settings[cur_index][0] = float(t0);
  241. m_zoom_settings[cur_index][1] = float(t1);
  242. m_zoom_settings[cur_index][2] = float(t2);
  243. }
  244. /* Precompute texture offset change instead of doing it in GLSL */
  245. for (int i = 0; i < 4; i++)
  246. {
  247. m_zoom_settings[i][0] += 0.5f * (1.0f - m_zoom_settings[i][2]);
  248. m_zoom_settings[i][1] -= 0.5f * (1.0f - m_zoom_settings[i][2]);
  249. }
  250. m_centertext->SetText("center: " + m_view.center.x.str(30) + " " + m_view.center.y.str(30));
  251. m_mousetext->SetText(" mouse: " + worldmouse.x.str(30) + " " + worldmouse.y.str(30));
  252. std::stringstream ss;
  253. ss << '[' << (m_julia ? "Julia" : "Mandelbrot") << "] zoom: " << 1.0 / m_view.radius;
  254. m_zoomtext->SetText(ss.str());
  255. if (m_dirty[m_frame])
  256. {
  257. m_dirty[m_frame]--;
  258. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  259. {
  260. if (thread::has_threads())
  261. m_jobqueue.push(i);
  262. else
  263. DoWork(i);
  264. }
  265. }
  266. }
  267. void DoWorkHelper(thread *)
  268. {
  269. m_spawnqueue.push(0);
  270. for ( ; ; )
  271. {
  272. int line = m_jobqueue.pop();
  273. if (line == -1)
  274. break;
  275. DoWork(line);
  276. m_donequeue.push(0);
  277. }
  278. m_donequeue.push(0);
  279. };
  280. void DoWork(int line)
  281. {
  282. double const maxsqlen = 1024;
  283. double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0));
  284. int jmin = ((m_frame + 1) % 4) / 2 + line;
  285. int jmax = jmin + MAX_LINES * 2;
  286. if (jmax > m_size.y)
  287. jmax = m_size.y;
  288. u8vec4 *pixelstart = m_pixels.data()
  289. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  290. #if USE_REAL
  291. rcmplx c = (rcmplx)m_view.center;
  292. rcmplx jr0 = (rcmplx)m_view.r0;
  293. #else
  294. f128cmplx c = (f128cmplx)m_view.center;
  295. f128cmplx jr0 = (f128cmplx)m_view.r0;
  296. #endif
  297. for (int j = jmin; j < jmax; j += 2)
  298. for (int i = m_frame % 2; i < m_size.x; i += 2)
  299. {
  300. #if USE_REAL
  301. real xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  302. real sqx0, sqy0, sqx1, sqy1, sqx2, sqy2, sqx3, sqy3;
  303. rcmplx z0 = c + rcmplx(TexelToWorldOffset(vec2(ivec2(i, j))));
  304. rcmplx r0 = m_julia ? jr0 : z0;
  305. #else
  306. ldouble xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  307. ldouble sqx0, sqy0, sqx1, sqy1, sqx2, sqy2, sqx3, sqy3;
  308. f128cmplx z0 = c + TexelToWorldOffset(vec2(ivec2(i, j)));
  309. f128cmplx r0 = m_julia ? jr0 : z0;
  310. #endif
  311. x0 = z0.x; y0 = z0.y;
  312. xr = r0.x; yr = r0.y;
  313. sqx0 = x0 * x0; sqy0 = y0 * y0;
  314. int iter = MAX_ITERATIONS - 4;
  315. for (;;)
  316. {
  317. /* Unroll the loop: tests are more expensive to do at each
  318. * iteration than the few extra multiplications, at least
  319. * with floats/doubles. */
  320. x1 = sqx0 - sqy0 + xr; y1 = x0 * y0 + x0 * y0 + yr;
  321. sqx1 = x1 * x1; sqy1 = y1 * y1;
  322. x2 = sqx1 - sqy1 + xr; y2 = x1 * y1 + x1 * y1 + yr;
  323. sqx2 = x2 * x2; sqy2 = y2 * y2;
  324. x3 = sqx2 - sqy2 + xr; y3 = x2 * y2 + x2 * y2 + yr;
  325. sqx3 = x3 * x3; sqy3 = y3 * y3;
  326. x0 = sqx3 - sqy3 + xr; y0 = x3 * y3 + x3 * y3 + yr;
  327. sqx0 = x0 * x0; sqy0 = y0 * y0;
  328. if ((double)sqx0 + (double)sqy0 >= maxsqlen)
  329. break;
  330. iter -= 4;
  331. if (iter < 4)
  332. break;
  333. }
  334. if (iter)
  335. {
  336. double n = (double)sqx0 + (double)sqy0;
  337. if ((double)sqx1 + (double)sqy1 >= maxsqlen)
  338. {
  339. iter += 3; n = (double)sqx1 + (double)sqy1;
  340. }
  341. else if ((double)sqx2 + (double)sqy2 >= maxsqlen)
  342. {
  343. iter += 2; n = (double)sqx2 + (double)sqy2;
  344. }
  345. else if ((double)sqx3 + (double)sqy3 >= maxsqlen)
  346. {
  347. iter += 1; n = (double)sqx3 + (double)sqy3;
  348. }
  349. if (n > maxsqlen * maxsqlen)
  350. n = maxsqlen * maxsqlen;
  351. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  352. double f = iter;
  353. union { double n; uint64_t x; } u = { (double)n };
  354. double k = (double)(u.x >> 42) - (((1 << 10) - 1) << 10);
  355. k *= k1;
  356. /* Approximate log2(k) in [1,2]. */
  357. f += (- 0.344847817623168308695977510213252644185 * k
  358. + 2.024664188044341212602376988171727038739) * k
  359. - 1.674876738008591047163498125918330313237;
  360. *pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  361. }
  362. else
  363. {
  364. *pixelstart++ = u8vec4(0, 0, 0, 255);
  365. }
  366. }
  367. }
  368. virtual bool init_draw() override
  369. {
  370. float const vertices[] =
  371. {
  372. 1.0f, 1.0f,
  373. -1.0f, 1.0f,
  374. -1.0f, -1.0f,
  375. -1.0f, -1.0f,
  376. 1.0f, -1.0f,
  377. 1.0f, 1.0f,
  378. };
  379. float const texcoords[] =
  380. {
  381. 1.0f, 1.0f,
  382. 0.0f, 1.0f,
  383. 0.0f, 0.0f,
  384. 0.0f, 0.0f,
  385. 1.0f, 0.0f,
  386. 1.0f, 1.0f,
  387. };
  388. /* Create a texture of half the width and twice the height
  389. * so that we can upload four different subimages each frame. */
  390. m_texture = std::make_shared<Texture>(ivec2(m_size.x / 2, m_size.y * 2),
  391. PixelFormat::RGBA_8);
  392. /* Ensure the texture data is complete at least once, otherwise
  393. * uploading subimages will not work. */
  394. m_texture->SetData(m_pixels.data());
  395. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(11_fractal));
  396. m_vertexattrib = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  397. m_texattrib = m_shader->GetAttribLocation(VertexUsage::TexCoord, 0);
  398. m_texuni = m_shader->GetUniformLocation("u_texture");
  399. m_texeluni = m_shader->GetUniformLocation("u_texel_size");
  400. m_screenuni = m_shader->GetUniformLocation("u_screen_size");
  401. m_zoomuni = m_shader->GetUniformLocation("u_zoom_settings");
  402. m_vdecl = std::make_shared<VertexDeclaration>(
  403. VertexStream<vec2>(VertexUsage::Position),
  404. VertexStream<vec2>(VertexUsage::TexCoord));
  405. m_vbo = std::make_shared<VertexBuffer>(sizeof(vertices));
  406. m_vbo->set_data(vertices, sizeof(vertices));
  407. m_tbo = std::make_shared<VertexBuffer>(sizeof(texcoords));
  408. m_tbo->set_data(texcoords, sizeof(texcoords));
  409. return true;
  410. }
  411. virtual void tick_draw(float seconds, Scene &scene) override
  412. {
  413. WorldEntity::tick_draw(seconds, scene);
  414. m_texture->Bind();
  415. if (m_dirty[m_frame])
  416. {
  417. if (thread::has_threads())
  418. {
  419. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  420. m_donequeue.pop();
  421. }
  422. m_dirty[m_frame]--;
  423. m_texture->SetSubData(ivec2(0, m_frame * m_size.y / 2),
  424. m_size / 2,
  425. &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
  426. }
  427. m_shader->Bind();
  428. m_shader->SetUniform(m_texuni, m_texture->GetTextureUniform(), 0);
  429. m_shader->SetUniform(m_texeluni, m_texel_settings);
  430. m_shader->SetUniform(m_screenuni, m_screen_settings);
  431. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  432. m_vdecl->Bind();
  433. m_vdecl->SetStream(m_vbo, m_vertexattrib);
  434. m_vdecl->SetStream(m_tbo, m_texattrib);
  435. m_texture->Bind();
  436. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  437. m_vdecl->Unbind();
  438. }
  439. virtual bool release_draw() override
  440. {
  441. m_shader.reset();
  442. m_vdecl.reset();
  443. m_vbo.reset();
  444. m_tbo.reset();
  445. m_texture.reset();
  446. return true;
  447. }
  448. private:
  449. static int const MAX_ITERATIONS = 400;
  450. static int const PALETTE_STEP = 32;
  451. static int const MAX_THREADS = 8;
  452. static int const MAX_LINES = 8;
  453. // 1e-14 for doubles, 1e-17 for long doubles
  454. static double constexpr MAX_ZOOM = 1e-17;
  455. ivec2 m_size, m_window_size, m_oldmouse;
  456. double m_window2world;
  457. dvec2 m_texel2world;
  458. std::vector<u8vec4> m_pixels, m_palette;
  459. std::shared_ptr<Shader> m_shader;
  460. ShaderAttrib m_vertexattrib, m_texattrib;
  461. ShaderUniform m_texuni, m_texeluni, m_screenuni, m_zoomuni;
  462. std::shared_ptr<VertexDeclaration> m_vdecl;
  463. std::shared_ptr<VertexBuffer> m_vbo, m_tbo;
  464. std::shared_ptr<Texture> m_texture;
  465. int m_frame = -1, m_slices = 4, m_dirty[4];
  466. bool m_drag = false;
  467. struct view_settings
  468. {
  469. rcmplx center, translate, r0;
  470. double radius;
  471. };
  472. view_settings m_view, m_saved_view;
  473. rcmplx m_deltashift[4];
  474. real m_deltascale[4];
  475. double m_zoom_speed;
  476. bool m_julia = false;
  477. vec4 m_texel_settings, m_screen_settings;
  478. mat4 m_zoom_settings;
  479. // Worker threads
  480. thread *m_threads[MAX_THREADS];
  481. queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
  482. // Debug information
  483. Text *m_centertext, *m_mousetext, *m_zoomtext;
  484. };
  485. int main(int argc, char **argv)
  486. {
  487. ivec2 window_size(640, 480);
  488. sys::init(argc, argv);
  489. Application app("Tutorial 11: Fractal", window_size, 60.0f);
  490. new DebugFps(5, 5);
  491. new Fractal(window_size);
  492. //new DebugRecord("fractalol.ogm", 60.0f);
  493. app.Run();
  494. return EXIT_SUCCESS;
  495. }