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.

564 lines
18 KiB

  1. //
  2. // Lol Engine - Fractal tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdio>
  15. #include "core.h"
  16. #include "loldebug.h"
  17. using namespace lol;
  18. LOLFX_RESOURCE_DECLARE(11_fractal);
  19. class Fractal : public WorldEntity
  20. {
  21. public:
  22. Fractal(ivec2 const &size)
  23. {
  24. /* Ensure texture size is a multiple of 16 for better aligned
  25. * data access. Store the dimensions of a texel for our shader,
  26. * as well as the half-size of the screen. */
  27. m_size = size;
  28. m_size.x = (m_size.x + 15) & ~15;
  29. m_size.y = (m_size.y + 15) & ~15;
  30. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / m_size.xyxy;
  31. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * m_size.xyxy;
  32. /* Window size decides the world aspect ratio. For instance, 640×480
  33. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  34. #if !defined __native_client__
  35. m_window_size = Video::GetSize();
  36. #else
  37. /* FIXME: it's illegal to call this on the game thread! */
  38. m_window_size = ivec2(640, 480);
  39. #endif
  40. if (m_window_size.y < m_window_size.x)
  41. m_window2world = 0.5 / m_window_size.y;
  42. else
  43. m_window2world = 0.5 / m_window_size.x;
  44. m_texel2world = (dvec2)m_window_size / (dvec2)m_size * m_window2world;
  45. m_oldmouse = ivec2(0, 0);
  46. m_pixels.Resize(m_size.x * m_size.y);
  47. m_frame = -1;
  48. m_slices = 4;
  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. #if defined __CELLOS_LV2__ || defined _XBOX
  56. //m_center = rcmplx(-.22815528839841, -1.11514249704382);
  57. //m_center = rcmplx(0.001643721971153, 0.822467633298876);
  58. m_center = rcmplx("-0.65823419062254", "0.50221777363480");
  59. m_zoom_speed = -0.025;
  60. #else
  61. m_center = rcmplx(-0.75, 0.0);
  62. m_zoom_speed = 0.0;
  63. #endif
  64. m_translate = rcmplx(0.0, 0.0);
  65. m_radius = 5.0;
  66. m_ready = false;
  67. m_drag = false;
  68. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  69. {
  70. double f = (double)i / PALETTE_STEP;
  71. double r = 0.5 * lol::sin(f * 0.27 + 2.0) + 0.5;
  72. double g = 0.5 * lol::sin(f * 0.17 - 1.8) + 0.5;
  73. double b = 0.5 * lol::sin(f * 0.21 - 2.6) + 0.5;
  74. if (f < 7.0)
  75. {
  76. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  77. r *= f;
  78. g *= f;
  79. b *= f;
  80. }
  81. uint8_t red = r * 255.99f;
  82. uint8_t green = g * 255.99f;
  83. uint8_t blue = b * 255.99f;
  84. #if defined __CELLOS_LV2__ || defined _XBOX
  85. m_palette.Push(u8vec4(255, red, green, blue));
  86. #elif defined __native_client__
  87. m_palette.Push(u8vec4(red, green, blue, 255));
  88. #else
  89. m_palette.Push(u8vec4(blue, green, red, 255));
  90. #endif
  91. }
  92. #if !defined __native_client__
  93. m_centertext = new Text(NULL, "data/font/ascii.png");
  94. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  95. Ticker::Ref(m_centertext);
  96. m_mousetext = new Text(NULL, "data/font/ascii.png");
  97. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  98. Ticker::Ref(m_mousetext);
  99. m_zoomtext = new Text(NULL, "data/font/ascii.png");
  100. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  101. Ticker::Ref(m_zoomtext);
  102. #endif
  103. m_position = ivec3(0, 0, 0);
  104. m_bbox[0] = m_position;
  105. m_bbox[1] = ivec3(m_window_size, 0);
  106. Input::TrackMouse(this);
  107. /* Spawn worker threads and wait for their readiness. */
  108. for (int i = 0; i < MAX_THREADS; i++)
  109. m_threads[i] = new Thread(DoWorkHelper, this);
  110. for (int i = 0; i < MAX_THREADS; i++)
  111. m_spawnqueue.Pop();
  112. }
  113. ~Fractal()
  114. {
  115. /* Signal worker threads for completion and wait for
  116. * them to quit. */
  117. for (int i = 0; i < MAX_THREADS; i++)
  118. m_jobqueue.Push(-1);
  119. for (int i = 0; i < MAX_THREADS; i++)
  120. m_donequeue.Pop();
  121. Input::UntrackMouse(this);
  122. #if !defined __native_client__
  123. Ticker::Unref(m_centertext);
  124. Ticker::Unref(m_mousetext);
  125. Ticker::Unref(m_zoomtext);
  126. #endif
  127. }
  128. inline dcmplx TexelToWorldOffset(vec2 texel)
  129. {
  130. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  131. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  132. return m_radius * dcmplx(dx, dy);
  133. }
  134. inline dcmplx ScreenToWorldOffset(vec2 pixel)
  135. {
  136. /* No 0.5 offset here, because we want to be able to position the
  137. * mouse at (0,0) exactly. */
  138. double dx = pixel.x - m_window_size.x / 2;
  139. double dy = m_window_size.y / 2 - pixel.y;
  140. return m_radius * m_window2world * dcmplx(dx, dy);
  141. }
  142. virtual void TickGame(float seconds)
  143. {
  144. WorldEntity::TickGame(seconds);
  145. int prev_frame = (m_frame + 4) % 4;
  146. m_frame = (m_frame + 1) % 4;
  147. rcmplx worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  148. ivec3 buttons = Input::GetMouseButtons();
  149. #if !defined __CELLOS_LV2__ && !defined _XBOX
  150. if (buttons[1])
  151. {
  152. if (!m_drag)
  153. {
  154. m_oldmouse = m_mousepos;
  155. m_drag = true;
  156. }
  157. m_translate = ScreenToWorldOffset(m_oldmouse)
  158. - ScreenToWorldOffset(m_mousepos);
  159. /* XXX: the purpose of this hack is to avoid translating by
  160. * an exact number of pixels. If this were to happen, the step()
  161. * optimisation for i915 cards in our shader would behave
  162. * incorrectly because a quarter of the pixels in the image
  163. * would have tie rankings in the distance calculation. */
  164. m_translate *= real(1023.0 / 1024.0);
  165. m_oldmouse = m_mousepos;
  166. }
  167. else
  168. {
  169. m_drag = false;
  170. if (m_translate != rcmplx(0.0, 0.0))
  171. {
  172. m_translate *= real(std::pow(2.0, -seconds * 5.0));
  173. if ((double)m_translate.norm() < m_radius * 1e-4)
  174. m_translate = rcmplx(0.0, 0.0);
  175. }
  176. }
  177. if ((buttons[0] || buttons[2]) && m_mousepos.x != -1)
  178. {
  179. double zoom = buttons[0] ? -0.5 : 0.5;
  180. m_zoom_speed += zoom * seconds;
  181. if (m_zoom_speed / zoom > 5e-3f)
  182. m_zoom_speed = zoom * 5e-3f;
  183. }
  184. else if (m_zoom_speed)
  185. {
  186. m_zoom_speed *= std::pow(2.0, -seconds * 5.0);
  187. if (lol::abs(m_zoom_speed) < 1e-5 || m_drag)
  188. m_zoom_speed = 0.0;
  189. }
  190. #endif
  191. if (m_zoom_speed || m_translate != rcmplx(0.0, 0.0))
  192. {
  193. rcmplx oldcenter = m_center;
  194. double oldradius = m_radius;
  195. double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed);
  196. if (m_radius * zoom > 8.0)
  197. {
  198. m_zoom_speed *= -1.0;
  199. zoom = 8.0 / m_radius;
  200. }
  201. else if (m_radius * zoom < 1e-14)
  202. {
  203. m_zoom_speed *= -1.0;
  204. zoom = 1e-14 / m_radius;
  205. }
  206. m_radius *= zoom;
  207. #if !defined __CELLOS_LV2__ && !defined _XBOX
  208. m_center += m_translate;
  209. m_center = (m_center - worldmouse) * real(zoom) + worldmouse;
  210. worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  211. #endif
  212. /* Store the transformation properties to go from m_frame - 1
  213. * to m_frame. */
  214. m_deltashift[prev_frame] = (m_center - oldcenter) / real(oldradius);
  215. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  216. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  217. m_deltascale[prev_frame] = m_radius / oldradius;
  218. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 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. m_zoom_settings[cur_index][0] = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  238. m_zoom_settings[cur_index][1] = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  239. m_zoom_settings[cur_index][2] = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  240. }
  241. /* Precompute texture offset change instead of doing it in GLSL */
  242. for (int i = 0; i < 4; i++)
  243. {
  244. m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
  245. m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
  246. }
  247. #if !defined __native_client__
  248. char buf[256];
  249. std::sprintf(buf, "center: ");
  250. m_center.x.sprintf(buf + strlen(buf), 30);
  251. std::sprintf(buf + strlen(buf), " ");
  252. m_center.y.sprintf(buf + strlen(buf), 30);
  253. m_centertext->SetText(buf);
  254. std::sprintf(buf, " mouse: ");
  255. worldmouse.x.sprintf(buf + strlen(buf), 30);
  256. std::sprintf(buf + strlen(buf), " ");
  257. worldmouse.y.sprintf(buf + strlen(buf), 30);
  258. m_mousetext->SetText(buf);
  259. std::sprintf(buf, " zoom: %g", 1.0 / m_radius);
  260. m_zoomtext->SetText(buf);
  261. #endif
  262. if (m_dirty[m_frame])
  263. {
  264. m_dirty[m_frame]--;
  265. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  266. m_jobqueue.Push(i);
  267. }
  268. }
  269. static void *DoWorkHelper(void *data)
  270. {
  271. Fractal *that = (Fractal *)data;
  272. that->m_spawnqueue.Push(0);
  273. for ( ; ; )
  274. {
  275. int line = that->m_jobqueue.Pop();
  276. if (line == -1)
  277. break;
  278. that->DoWork(line);
  279. that->m_donequeue.Push(0);
  280. }
  281. that->m_donequeue.Push(0);
  282. return NULL;
  283. };
  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 *m_pixelstart = &m_pixels[0]
  293. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  294. dcmplx c = (dcmplx)m_center;
  295. for (int j = jmin; j < jmax; j += 2)
  296. for (int i = m_frame % 2; i < m_size.x; i += 2)
  297. {
  298. double xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  299. dcmplx z0 = c + TexelToWorldOffset(ivec2(i, j));
  300. //dcmplx r0(0.28693186889504513, 0.014286693904085048);
  301. //dcmplx r0(0.001643721971153, 0.822467633298876);
  302. //dcmplx r0(-1.207205434596, 0.315432814901);
  303. //dcmplx r0(-0.79192956889854, -0.14632423080102);
  304. //dcmplx r0(0.3245046418497685, 0.04855101129280834);
  305. dcmplx r0 = z0;
  306. x0 = z0.x; y0 = z0.y;
  307. xr = r0.x; yr = r0.y;
  308. int iter = MAX_ITERATIONS - 4;
  309. for (;;)
  310. {
  311. /* Unroll the loop: tests are more expensive to do at each
  312. * iteration than the few extra multiplications. */
  313. x1 = x0 * x0 - y0 * y0 + xr;
  314. y1 = x0 * y0 + x0 * y0 + yr;
  315. x2 = x1 * x1 - y1 * y1 + xr;
  316. y2 = x1 * y1 + x1 * y1 + yr;
  317. x3 = x2 * x2 - y2 * y2 + xr;
  318. y3 = x2 * y2 + x2 * y2 + yr;
  319. x0 = x3 * x3 - y3 * y3 + xr;
  320. y0 = x3 * y3 + x3 * y3 + yr;
  321. if (x0 * x0 + y0 * y0 >= maxsqlen)
  322. break;
  323. iter -= 4;
  324. if (iter < 4)
  325. break;
  326. }
  327. if (iter)
  328. {
  329. double n = x0 * x0 + y0 * y0;
  330. if (x1 * x1 + y1 * y1 >= maxsqlen)
  331. {
  332. iter += 3; n = x1 * x1 + y1 * y1;
  333. }
  334. else if (x2 * x2 + y2 * y2 >= maxsqlen)
  335. {
  336. iter += 2; n = x2 * x2 + y2 * y2;
  337. }
  338. else if (x3 * x3 + y3 * y3 >= maxsqlen)
  339. {
  340. iter += 1; n = x3 * x3 + y3 * y3;
  341. }
  342. if (n > maxsqlen * maxsqlen)
  343. n = maxsqlen * maxsqlen;
  344. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  345. double f = iter;
  346. union { double n; uint64_t x; } u = { n };
  347. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  348. k *= k1;
  349. /* Approximate log2(k) in [1,2]. */
  350. f += (- 0.344847817623168308695977510213252644185 * k
  351. + 2.024664188044341212602376988171727038739) * k
  352. - 1.674876738008591047163498125918330313237;
  353. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  354. }
  355. else
  356. {
  357. #if defined __CELLOS_LV2__ || defined _XBOX
  358. *m_pixelstart++ = u8vec4(255, 0, 0, 0);
  359. #else
  360. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  361. #endif
  362. }
  363. }
  364. }
  365. virtual void TickDraw(float seconds)
  366. {
  367. WorldEntity::TickDraw(seconds);
  368. static float const vertices[] =
  369. {
  370. 1.0f, 1.0f,
  371. -1.0f, 1.0f,
  372. -1.0f, -1.0f,
  373. -1.0f, -1.0f,
  374. 1.0f, -1.0f,
  375. 1.0f, 1.0f,
  376. };
  377. static float const texcoords[] =
  378. {
  379. 1.0f, 1.0f,
  380. 0.0f, 1.0f,
  381. 0.0f, 0.0f,
  382. 0.0f, 0.0f,
  383. 1.0f, 0.0f,
  384. 1.0f, 1.0f,
  385. };
  386. if (!m_ready)
  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 = new Texture(ivec2(m_size.x / 2, m_size.y * 2),
  391. PixelFormat::ABGR_8);
  392. /* Ensure the texture data is complete at least once, otherwise
  393. * uploading subimages will not work. */
  394. m_texture->SetData(&m_pixels[0]);
  395. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(11_fractal));
  396. m_vertexattrib = m_shader->GetAttribLocation("a_Vertex", VertexUsage::Position, 0);
  397. m_texattrib = m_shader->GetAttribLocation("a_TexCoord", VertexUsage::TexCoord, 0);
  398. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  399. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  400. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  401. m_vdecl =
  402. new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
  403. VertexStream<vec2>(VertexUsage::TexCoord));
  404. m_vbo = new VertexBuffer(sizeof(vertices));
  405. m_tbo = new VertexBuffer(sizeof(texcoords));
  406. void *tmp = m_vbo->Lock(0, 0);
  407. memcpy(tmp, vertices, sizeof(vertices));
  408. m_vbo->Unlock();
  409. tmp = m_tbo->Lock(0, 0);
  410. memcpy(tmp, texcoords, sizeof(texcoords));
  411. m_tbo->Unlock();
  412. /* FIXME: this object never cleans up */
  413. m_ready = true;
  414. }
  415. m_texture->Bind();
  416. if (m_dirty[m_frame])
  417. {
  418. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  419. m_donequeue.Pop();
  420. m_dirty[m_frame]--;
  421. #if defined __CELLOS_LV2__
  422. /* glTexSubImage2D is extremely slow on the PS3, to the point
  423. * that uploading the whole texture is 40 times faster. */
  424. m_texture->SetData(&m_pixels[0]);
  425. #else
  426. m_texture->SetSubData(ivec2(0, m_frame * m_size.y / 2),
  427. m_size / 2,
  428. &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
  429. #endif
  430. }
  431. m_shader->Bind();
  432. m_shader->SetUniform(m_texeluni, m_texel_settings);
  433. m_shader->SetUniform(m_screenuni, m_screen_settings);
  434. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  435. m_vdecl->Bind();
  436. m_vdecl->SetStream(m_vbo, m_vertexattrib);
  437. m_vdecl->SetStream(m_tbo, m_texattrib);
  438. m_texture->Bind();
  439. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  440. m_vdecl->Unbind();
  441. }
  442. private:
  443. static int const MAX_ITERATIONS = 340;
  444. static int const PALETTE_STEP = 32;
  445. static int const MAX_THREADS = 8;
  446. static int const MAX_LINES = 8;
  447. ivec2 m_size, m_window_size, m_oldmouse;
  448. double m_window2world;
  449. dvec2 m_texel2world;
  450. Array<u8vec4> m_pixels, m_palette;
  451. Shader *m_shader;
  452. ShaderAttrib m_vertexattrib, m_texattrib;
  453. ShaderUniform m_texeluni, m_screenuni, m_zoomuni;
  454. VertexDeclaration *m_vdecl;
  455. VertexBuffer *m_vbo, *m_tbo;
  456. Texture *m_texture;
  457. int m_frame, m_slices, m_dirty[4];
  458. bool m_ready, m_drag;
  459. rcmplx m_deltashift[4], m_center, m_translate;
  460. real m_deltascale[4];
  461. double m_zoom_speed, m_radius;
  462. vec4 m_texel_settings, m_screen_settings;
  463. mat4 m_zoom_settings;
  464. /* Worker threads */
  465. Thread *m_threads[MAX_THREADS];
  466. Queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
  467. /* Debug information */
  468. #if !defined __native_client__
  469. Text *m_centertext, *m_mousetext, *m_zoomtext;
  470. #endif
  471. };
  472. int main(int argc, char **argv)
  473. {
  474. ivec2 window_size(640, 480);
  475. System::Init(argc, argv);
  476. Application app("Tutorial 3: Fractal", window_size, 60.0f);
  477. new DebugFps(5, 5);
  478. new Fractal(window_size);
  479. //new DebugRecord("fractalol.ogm", 60.0f);
  480. app.Run();
  481. return EXIT_SUCCESS;
  482. }