Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

594 lignes
19 KiB

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