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.
 
 
 

797 lines
30 KiB

  1. //
  2. // Lol Engine - Fractal tutorial
  3. //
  4. // Copyright: (c) 2011 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include "core.h"
  15. #include "lolgl.h"
  16. #include "loldebug.h"
  17. using namespace std;
  18. using namespace lol;
  19. #if USE_SDL && defined __APPLE__
  20. # include <SDL_main.h>
  21. #endif
  22. #if defined _WIN32
  23. # undef main /* FIXME: still needed? */
  24. # include <direct.h>
  25. #endif
  26. #ifdef __CELLOS_LV2__
  27. static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
  28. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  29. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  30. #elif defined __native_client__
  31. static GLint const INTERNAL_FORMAT = GL_RGBA;
  32. static GLenum const TEXTURE_FORMAT = GL_RGBA;
  33. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE;
  34. #else
  35. /* Seems efficient for little endian textures */
  36. static GLint const INTERNAL_FORMAT = GL_RGBA;
  37. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  38. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  39. #endif
  40. class Fractal : public WorldEntity
  41. {
  42. public:
  43. Fractal(ivec2 const &size)
  44. {
  45. /* Ensure texture size is a multiple of 16 for better aligned
  46. * data access. Store the dimensions of a texel for our shader,
  47. * as well as the half-size of the screen. */
  48. m_size = size;
  49. m_size.x = (m_size.x + 15) & ~15;
  50. m_size.y = (m_size.y + 15) & ~15;
  51. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / (vec4)m_size.xyxy();
  52. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * (vec4)m_size.xyxy();
  53. /* Window size decides the world aspect ratio. For instance, 640×480
  54. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  55. #if !defined __native_client__
  56. m_window_size = Video::GetSize();
  57. #else
  58. /* FIXME: it's illegal to call this on the game thread! */
  59. m_window_size = ivec2(640, 480);
  60. #endif
  61. if (m_window_size.y < m_window_size.x)
  62. m_window2world = 0.5 / m_window_size.y;
  63. else
  64. m_window2world = 0.5 / m_window_size.x;
  65. m_texel2world = (vec2)m_window_size / (vec2)m_size
  66. * (vec2)m_window2world;
  67. m_oldmouse = ivec2(0, 0);
  68. m_pixels = new u8vec4[m_size.x * m_size.y];
  69. m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2];
  70. m_frame = -1;
  71. m_slices = 4;
  72. for (int i = 0; i < 4; i++)
  73. {
  74. m_deltashift[i] = 0.0;
  75. m_deltascale[i] = 1.0;
  76. m_dirty[i] = 2;
  77. }
  78. #if defined __CELLOS_LV2__
  79. //m_center = f64cmplx(-.22815528839841, -1.11514249704382);
  80. //m_center = f64cmplx(0.001643721971153, 0.822467633298876);
  81. m_center = f64cmplx(-0.65823419062254, 0.50221777363480);
  82. m_zoom_speed = -0.000025;
  83. #else
  84. m_center = -0.75;
  85. m_zoom_speed = 0.0;
  86. #endif
  87. m_translate = 0;
  88. m_radius = 5.0;
  89. m_ready = false;
  90. m_drag = false;
  91. m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP];
  92. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  93. {
  94. double f = (double)i / PALETTE_STEP;
  95. double r = 0.5 * sin(f * 0.27 + 2.0) + 0.5;
  96. double g = 0.5 * sin(f * 0.17 - 1.8) + 0.5;
  97. double b = 0.5 * sin(f * 0.21 - 2.6) + 0.5;
  98. if (f < 7.0)
  99. {
  100. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  101. r *= f;
  102. g *= f;
  103. b *= f;
  104. }
  105. uint8_t red = r * 255.99f;
  106. uint8_t green = g * 255.99f;
  107. uint8_t blue = b * 255.99f;
  108. #if defined __CELLOS_LV2__
  109. m_palette[i] = u8vec4(255, red, green, blue);
  110. #elif defined __native_client__
  111. m_palette[i] = u8vec4(red, green, blue, 255);
  112. #else
  113. m_palette[i] = u8vec4(blue, green, red, 255);
  114. #endif
  115. }
  116. #if !defined __native_client__
  117. m_centertext = new Text(NULL, "gfx/font/ascii.png");
  118. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  119. Ticker::Ref(m_centertext);
  120. m_mousetext = new Text(NULL, "gfx/font/ascii.png");
  121. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  122. Ticker::Ref(m_mousetext);
  123. m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
  124. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  125. Ticker::Ref(m_zoomtext);
  126. #endif
  127. position = ivec3(0, 0, 0);
  128. bbox[0] = position;
  129. bbox[1] = ivec3(m_window_size, 0);
  130. Input::TrackMouse(this);
  131. /* Spawn worker threads and wait for their readiness. */
  132. for (int i = 0; i < MAX_THREADS; i++)
  133. m_threads[i] = new Thread(DoWorkHelper, this);
  134. for (int i = 0; i < MAX_THREADS; i++)
  135. m_spawnqueue.Pop();
  136. }
  137. ~Fractal()
  138. {
  139. /* Signal worker threads for completion. */
  140. for (int i = 0; i < MAX_THREADS; i++)
  141. m_jobqueue.Push(-1);
  142. Input::UntrackMouse(this);
  143. #if !defined __native_client__
  144. Ticker::Unref(m_centertext);
  145. Ticker::Unref(m_mousetext);
  146. Ticker::Unref(m_zoomtext);
  147. #endif
  148. delete m_pixels;
  149. delete m_tmppixels;
  150. delete m_palette;
  151. }
  152. inline f64cmplx TexelToWorldOffset(vec2 texel)
  153. {
  154. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  155. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  156. return m_radius * f64cmplx(dx, dy);
  157. }
  158. inline f64cmplx ScreenToWorldOffset(vec2 pixel)
  159. {
  160. /* No 0.5 offset here, because we want to be able to position the
  161. * mouse at (0,0) exactly. */
  162. double dx = pixel.x - m_window_size.x / 2;
  163. double dy = m_window_size.y / 2 - pixel.y;
  164. return m_radius * m_window2world * f64cmplx(dx, dy);
  165. }
  166. virtual void TickGame(float deltams)
  167. {
  168. WorldEntity::TickGame(deltams);
  169. int prev_frame = m_frame;
  170. m_frame = (m_frame + 1) % 4;
  171. f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos);
  172. ivec3 buttons = Input::GetMouseButtons();
  173. #if !defined __CELLOS_LV2__
  174. if (buttons[1])
  175. {
  176. if (!m_drag)
  177. {
  178. m_oldmouse = mousepos;
  179. m_drag = true;
  180. }
  181. m_translate = ScreenToWorldOffset(m_oldmouse)
  182. - ScreenToWorldOffset(mousepos);
  183. /* XXX: the purpose of this hack is to avoid translating by
  184. * an exact number of pixels. If this were to happen, the step()
  185. * optimisation for i915 cards in our shader would behave
  186. * incorrectly because a quarter of the pixels in the image
  187. * would have tie rankings in the distance calculation. */
  188. m_translate *= 1023.0 / 1024.0;
  189. m_oldmouse = mousepos;
  190. }
  191. else
  192. {
  193. m_drag = false;
  194. if (m_translate != 0.0)
  195. {
  196. m_translate *= pow(2.0, -deltams * 0.005);
  197. if (m_translate.norm() / m_radius < 1e-4)
  198. m_translate = 0.0;
  199. }
  200. }
  201. if ((buttons[0] || buttons[2]) && mousepos.x != -1)
  202. {
  203. double zoom = buttons[0] ? -0.0005 : 0.0005;
  204. m_zoom_speed += deltams * zoom;
  205. if (m_zoom_speed / zoom > 5)
  206. m_zoom_speed = 5 * zoom;
  207. }
  208. else if (m_zoom_speed)
  209. {
  210. m_zoom_speed *= pow(2.0, -deltams * 0.005);
  211. if (abs(m_zoom_speed) < 1e-5 || m_drag)
  212. m_zoom_speed = 0.0;
  213. }
  214. #endif
  215. if (m_zoom_speed || m_translate != 0.0)
  216. {
  217. f64cmplx oldcenter = m_center;
  218. double oldradius = m_radius;
  219. double zoom = pow(2.0, deltams * m_zoom_speed);
  220. if (m_radius * zoom > 8.0)
  221. {
  222. m_zoom_speed *= -1.0;
  223. zoom = 8.0 / m_radius;
  224. }
  225. else if (m_radius * zoom < 1e-14)
  226. {
  227. m_zoom_speed *= -1.0;
  228. zoom = 1e-14 / m_radius;
  229. }
  230. m_radius *= zoom;
  231. #if !defined __CELLOS_LV2__
  232. m_center += m_translate;
  233. m_center = (m_center - worldmouse) * zoom + worldmouse;
  234. worldmouse = m_center + ScreenToWorldOffset(mousepos);
  235. #endif
  236. /* Store the transformation properties to go from m_frame - 1
  237. * to m_frame. */
  238. m_deltashift[prev_frame] = (m_center - oldcenter) / oldradius;
  239. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  240. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  241. m_deltascale[prev_frame] = m_radius / oldradius;
  242. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  243. }
  244. else
  245. {
  246. /* If settings didn't change, set transformation from previous
  247. * frame to identity. */
  248. m_deltashift[prev_frame] = 0.0;
  249. m_deltascale[prev_frame] = 1.0;
  250. }
  251. /* Transformation from current frame to current frame is always
  252. * identity. */
  253. m_zoom_settings[m_frame][0] = 0.0f;
  254. m_zoom_settings[m_frame][1] = 0.0f;
  255. m_zoom_settings[m_frame][2] = 1.0f;
  256. /* Compute transformation from other frames to current frame */
  257. for (int i = 0; i < 3; i++)
  258. {
  259. int prev_index = (m_frame + 4 - i) % 4;
  260. int cur_index = (m_frame + 3 - i) % 4;
  261. m_zoom_settings[cur_index][0] = m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  262. m_zoom_settings[cur_index][1] = m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  263. m_zoom_settings[cur_index][2] = m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  264. }
  265. /* Precompute texture offset change instead of doing it in GLSL */
  266. for (int i = 0; i < 4; i++)
  267. {
  268. m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
  269. m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
  270. }
  271. #if !defined __native_client__
  272. char buf[128];
  273. sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y);
  274. m_centertext->SetText(buf);
  275. sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y);
  276. m_mousetext->SetText(buf);
  277. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  278. m_zoomtext->SetText(buf);
  279. #endif
  280. if (m_dirty[m_frame])
  281. {
  282. m_dirty[m_frame]--;
  283. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  284. m_jobqueue.Push(i);
  285. }
  286. }
  287. static void *DoWorkHelper(void *data)
  288. {
  289. Fractal *that = (Fractal *)data;
  290. that->m_spawnqueue.Push(0);
  291. for ( ; ; )
  292. {
  293. int line = that->m_jobqueue.Pop();
  294. if (line == -1)
  295. break;
  296. that->DoWork(line);
  297. that->m_donequeue.Push(0);
  298. }
  299. return NULL;
  300. };
  301. void DoWork(int line)
  302. {
  303. double const maxsqlen = 1024;
  304. double const k1 = 1.0 / (1 << 10) / log2(maxsqlen);
  305. int jmin = ((m_frame + 1) % 4) / 2 + line;
  306. int jmax = jmin + MAX_LINES * 2;
  307. if (jmax > m_size.y)
  308. jmax = m_size.y;
  309. u8vec4 *m_pixelstart = m_pixels
  310. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  311. for (int j = jmin; j < jmax; j += 2)
  312. for (int i = m_frame % 2; i < m_size.x; i += 2)
  313. {
  314. f64cmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j));
  315. f64cmplx z1, z2, z3, r0 = z0;
  316. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  317. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  318. //f64cmplx r0(-1.207205434596, 0.315432814901);
  319. //f64cmplx r0(-0.79192956889854, -0.14632423080102);
  320. //f64cmplx r0(0.3245046418497685, 0.04855101129280834);
  321. int iter = MAX_ITERATIONS - 4;
  322. for (;;)
  323. {
  324. /* Unroll the loop: tests are more expensive to do at each
  325. * iteration than the few extra multiplications. */
  326. z1 = z0 * z0 + r0;
  327. z2 = z1 * z1 + r0;
  328. z3 = z2 * z2 + r0;
  329. z0 = z3 * z3 + r0;
  330. if (sqlen(z0) >= maxsqlen)
  331. break;
  332. iter -= 4;
  333. if (iter < 4)
  334. break;
  335. }
  336. if (iter)
  337. {
  338. double n = sqlen(z0);
  339. if (sqlen(z1) >= maxsqlen) { iter += 3; n = sqlen(z1); }
  340. else if (sqlen(z2) >= maxsqlen) { iter += 2; n = sqlen(z2); }
  341. else if (sqlen(z3) >= maxsqlen) { iter += 1; n = sqlen(z3); }
  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__
  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 deltams)
  366. {
  367. WorldEntity::TickDraw(deltams);
  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. glGenTextures(1, &m_texid);
  391. glBindTexture(GL_TEXTURE_2D, m_texid);
  392. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  393. m_size.x / 2, m_size.y * 2, 0,
  394. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  395. #if defined __CELLOS_LV2__
  396. /* We need this hint because by default the storage type is
  397. * GL_TEXTURE_SWIZZLED_GPU_SCE. */
  398. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  399. GL_TEXTURE_TILED_GPU_SCE);
  400. #endif
  401. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  402. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  403. m_shader = Shader::Create(
  404. #if !defined __CELLOS_LV2__
  405. #if !defined HAVE_GLES_2X
  406. "#version 120\n"
  407. #else
  408. "precision highp float;"
  409. #endif
  410. ""
  411. "uniform mat4 u_ZoomSettings;"
  412. "uniform vec4 u_TexelSize;"
  413. "uniform vec4 u_ScreenSize;"
  414. ""
  415. "attribute vec2 a_TexCoord;"
  416. "attribute vec2 a_Vertex;"
  417. ""
  418. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  419. ""
  420. "void main(void)"
  421. "{"
  422. " gl_Position = vec4(a_Vertex, 0.0, 1.0);"
  423. /* Center point in [-.5,.5], apply zoom and translation
  424. * transformation, and go back to texture coordinates
  425. * in [0,1]. That's the ideal point we would like to
  426. * compute the value for. Then add or remove half the
  427. * size of a texel: the distance from this new point to
  428. * the final point will be our error. */
  429. " vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625);"
  430. " vec4 zoomscale = vec4(u_ZoomSettings[0][2],"
  431. " u_ZoomSettings[1][2],"
  432. " u_ZoomSettings[2][2],"
  433. " u_ZoomSettings[3][2]);"
  434. " vec4 zoomtx = vec4(u_ZoomSettings[0][0],"
  435. " u_ZoomSettings[1][0],"
  436. " u_ZoomSettings[2][0],"
  437. " u_ZoomSettings[3][0]);"
  438. " vec4 zoomty = vec4(u_ZoomSettings[0][1],"
  439. " u_ZoomSettings[1][1],"
  440. " u_ZoomSettings[2][1],"
  441. " u_ZoomSettings[3][1]);"
  442. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  443. " + offsets.xyxy * u_TexelSize.x;"
  444. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  445. " + offsets.xyyx * u_TexelSize.y;"
  446. /* Precompute the multiple of one texel where our ideal
  447. * point lies. The fragment shader will call floor() on
  448. * this value. We add or remove a slight offset to avoid
  449. * rounding issues at the image's edges. */
  450. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  451. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  452. "}",
  453. #if !defined HAVE_GLES_2X
  454. "#version 120\n"
  455. #else
  456. "precision highp float;"
  457. #endif
  458. ""
  459. "uniform vec4 u_TexelSize;"
  460. "uniform sampler2D u_Texture;"
  461. ""
  462. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  463. ""
  464. "void main(void)"
  465. "{"
  466. " vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5);"
  467. " vec4 rx, ry, t0, dx, dy, dd;"
  468. /* Get a pixel coordinate from each slice into rx & ry */
  469. " rx = u_TexelSize.x + u_TexelSize.z * floor(v_IndexX);"
  470. " ry = u_TexelSize.y + u_TexelSize.w * floor(v_IndexY);"
  471. /* Compute inverse distance to expected pixel in dd,
  472. * and put zero if we fall outside the texture. */
  473. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  474. " dx = rx - v_CenterX;"
  475. " dy = ry - v_CenterY;"
  476. //" vec4 dd = t0 * (abs(dx) + abs(dy));"
  477. //" vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy)));"
  478. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  479. /* Modify Y coordinate to select proper quarter. */
  480. " ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);"
  481. ""
  482. #if 1
  483. "\n#if 0\n" /* XXX: disabled until we can autodetect i915 */
  484. /* t1.x <-- dd.x > dd.y */
  485. /* t1.y <-- dd.z > dd.w */
  486. " vec2 t1 = step(dd.xz, dd.yw);"
  487. /* ret.x <-- max(rx.x, rx.y) wrt. t1.x */
  488. /* ret.y <-- max(rx.z, rx.w) wrt. t1.y */
  489. /* ret.z <-- max(ry.x, ry.y) wrt. t1.x */
  490. /* ret.w <-- max(ry.z, ry.w) wrt. t1.y */
  491. " vec4 ret = mix(vec4(rx.xz, ry.xz),"
  492. " vec4(rx.yw, ry.yw), t1.xyxy);"
  493. /* dd.x <-- max(dd.x, dd.y) */
  494. /* dd.z <-- max(dd.z, dd.w) */
  495. " dd.xy = mix(dd.xz, dd.yw, t1);"
  496. /* t2 <-- dd.x > dd.z */
  497. " float t2 = step(dd.x, dd.y);"
  498. /* ret.x <-- max(ret.x, ret.y); */
  499. /* ret.y <-- max(ret.z, ret.yw; */
  500. " ret.xy = mix(ret.xz, ret.yw, t2);"
  501. "\n#else\n"
  502. /* Fallback for i915 cards -- the trick to reduce the
  503. * number of operations is to compute both step(a,b)
  504. * and step(b,a) and hope that their sum is 1. This is
  505. * almost always the case, and when it isn't we can
  506. * afford to have a few wrong pixels. However, a real
  507. * problem is when panning the image, because half the
  508. * screen is likely to flicker. To avoid this problem,
  509. * we cheat a little (see m_translate comment above). */
  510. " vec4 t1 = step(dd.xzyw, dd.ywxz);"
  511. " vec4 ret = vec4(rx.xz, ry.xz) * t1.zwzw"
  512. " + vec4(rx.yw, ry.yw) * t1.xyxy;"
  513. " dd.xy = dd.xz * t1.zw + dd.yw * t1.xy;"
  514. " vec2 t2 = step(dd.xy, dd.yx);"
  515. " ret.xy = ret.xz * t2.yy + ret.yw * t2.xx;"
  516. "\n#endif\n"
  517. /* Nearest neighbour */
  518. " gl_FragColor = texture2D(u_Texture, ret.xy);"
  519. #else
  520. /* Alternate version: some kind of linear interpolation */
  521. " vec4 p0 = texture2D(u_Texture, vec2(rx.x, ry.x));"
  522. " vec4 p1 = texture2D(u_Texture, vec2(rx.y, ry.y));"
  523. " vec4 p2 = texture2D(u_Texture, vec2(rx.z, ry.z));"
  524. " vec4 p3 = texture2D(u_Texture, vec2(rx.w, ry.w));"
  525. " gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)"
  526. " * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);"
  527. #endif
  528. "}"
  529. #else
  530. "void main(float4 a_Vertex : POSITION,"
  531. " float2 a_TexCoord : TEXCOORD0,"
  532. " uniform float4x4 u_ZoomSettings,"
  533. " uniform float4 u_TexelSize,"
  534. " uniform float4 u_ScreenSize,"
  535. " out float4 out_Position : POSITION,"
  536. " out float4 v_CenterX,"
  537. " out float4 v_CenterY,"
  538. " out float4 v_IndexX,"
  539. " out float4 v_IndexY)"
  540. "{"
  541. " out_Position = a_Vertex;"
  542. " float4 offsets = float4(0.5, -0.5, 0.015625, -0.015625);"
  543. " float4 zoomscale = float4(u_ZoomSettings[2][0],"
  544. " u_ZoomSettings[2][1],"
  545. " u_ZoomSettings[2][2],"
  546. " u_ZoomSettings[2][3]);"
  547. " float4 zoomtx = float4(u_ZoomSettings[0][0],"
  548. " u_ZoomSettings[0][1],"
  549. " u_ZoomSettings[0][2],"
  550. " u_ZoomSettings[0][3]);"
  551. " float4 zoomty = float4(u_ZoomSettings[1][0],"
  552. " u_ZoomSettings[1][1],"
  553. " u_ZoomSettings[1][2],"
  554. " u_ZoomSettings[1][3]);"
  555. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  556. " + offsets.xyxy * u_TexelSize.x;"
  557. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  558. " + offsets.xyyx * u_TexelSize.y;"
  559. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  560. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  561. "}",
  562. "void main(in float4 v_CenterX,"
  563. " in float4 v_CenterY,"
  564. " in float4 v_IndexX,"
  565. " in float4 v_IndexY,"
  566. " uniform float4 u_TexelSize2,"
  567. " uniform sampler2D u_Texture,"
  568. " out float4 out_FragColor : COLOR)"
  569. "{"
  570. " float4 v05 = float4(0.5, 0.5, 0.5, 0.5);"
  571. " float4 rx, ry, t0, dx, dy, dd;"
  572. " rx = u_TexelSize2.x + u_TexelSize2.z * floor(v_IndexX);"
  573. " ry = u_TexelSize2.y + u_TexelSize2.w * floor(v_IndexY);"
  574. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  575. " dx = rx - v_CenterX;"
  576. " dy = ry - v_CenterY;"
  577. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  578. " ry = ry * 0.25 + float4(0.0, 0.25, 0.5, 0.75);"
  579. " float2 t1 = step(dd.xz, dd.yw);"
  580. " float4 ret = lerp(float4(rx.xz, ry.xz),"
  581. " float4(rx.yw, ry.yw), t1.xyxy);"
  582. " dd.xy = lerp(dd.xz, dd.yw, t1);"
  583. " float t2 = step(dd.x, dd.y);"
  584. " ret.xy = lerp(ret.xz, ret.yw, t2);"
  585. " out_FragColor = tex2D(u_Texture, ret.xy);"
  586. "}"
  587. #endif
  588. );
  589. m_vertexattrib = m_shader->GetAttribLocation("a_Vertex");
  590. m_texattrib = m_shader->GetAttribLocation("a_TexCoord");
  591. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  592. #if defined __CELLOS_LV2__
  593. m_texeluni2 = m_shader->GetUniformLocation("u_TexelSize2");
  594. #endif
  595. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  596. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  597. m_ready = true;
  598. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  599. /* Method 1: store vertex buffer on the GPU memory */
  600. glGenBuffers(1, &m_vbo);
  601. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  602. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  603. GL_STATIC_DRAW);
  604. glGenBuffers(1, &m_tbo);
  605. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  606. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  607. GL_STATIC_DRAW);
  608. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  609. /* Method 2: upload vertex information at each frame */
  610. #else
  611. #endif
  612. /* FIXME: this object never cleans up */
  613. }
  614. #if !defined HAVE_GLES_2X
  615. glEnable(GL_TEXTURE_2D);
  616. #endif
  617. glBindTexture(GL_TEXTURE_2D, m_texid);
  618. if (m_dirty[m_frame])
  619. {
  620. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  621. m_donequeue.Pop();
  622. m_dirty[m_frame]--;
  623. #ifdef __CELLOS_LV2__
  624. /* glTexSubImage2D is extremely slow on the PS3, to the point
  625. * that uploading the whole texture is 40 times faster. */
  626. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  627. m_size.x / 2, m_size.y * 2, 0,
  628. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  629. #else
  630. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  631. m_size.x / 2, m_size.y / 2,
  632. TEXTURE_FORMAT, TEXTURE_TYPE,
  633. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  634. #endif
  635. }
  636. m_shader->Bind();
  637. m_shader->SetUniform(m_texeluni, m_texel_settings);
  638. #if defined __CELLOS_LV2__
  639. m_shader->SetUniform(m_texeluni2, m_texel_settings);
  640. #endif
  641. m_shader->SetUniform(m_screenuni, m_screen_settings);
  642. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  643. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  644. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  645. glEnableVertexAttribArray(m_vertexattrib);
  646. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  647. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  648. glEnableVertexAttribArray(m_texattrib);
  649. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  650. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  651. /* Never used for now */
  652. //glEnableVertexAttribArray(m_vertexattrib);
  653. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  654. #else
  655. glEnableClientState(GL_VERTEX_ARRAY);
  656. glVertexPointer(2, GL_FLOAT, 0, vertices);
  657. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  658. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  659. #endif
  660. glDrawArrays(GL_TRIANGLES, 0, 6);
  661. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  662. glDisableVertexAttribArray(m_vertexattrib);
  663. glDisableVertexAttribArray(m_texattrib);
  664. glBindBuffer(GL_ARRAY_BUFFER, 0);
  665. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  666. /* Never used for now */
  667. //glDisableVertexAttribArray(m_vertexattrib);
  668. //glDisableVertexAttribArray(m_texattrib);
  669. #else
  670. glDisableClientState(GL_VERTEX_ARRAY);
  671. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  672. #endif
  673. }
  674. private:
  675. static int const MAX_ITERATIONS = 340;
  676. static int const PALETTE_STEP = 32;
  677. static int const MAX_THREADS = 8;
  678. static int const MAX_LINES = 8;
  679. ivec2 m_size, m_window_size, m_oldmouse;
  680. double m_window2world;
  681. f64vec2 m_texel2world;
  682. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  683. Shader *m_shader;
  684. GLuint m_texid;
  685. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  686. GLuint m_vbo, m_tbo;
  687. GLuint m_tco;
  688. #endif
  689. int m_vertexattrib, m_texattrib, m_texeluni, m_screenuni, m_zoomuni;
  690. #if defined __CELLOS_LV2__
  691. int m_texeluni2;
  692. #endif
  693. int m_frame, m_slices, m_dirty[4];
  694. bool m_ready, m_drag;
  695. f64cmplx m_center, m_translate;
  696. double m_zoom_speed, m_radius;
  697. vec4 m_texel_settings, m_screen_settings;
  698. mat4 m_zoom_settings;
  699. f64cmplx m_deltashift[4];
  700. double m_deltascale[4];
  701. /* Worker threads */
  702. Thread *m_threads[MAX_THREADS];
  703. Queue m_spawnqueue, m_jobqueue, m_donequeue;
  704. /* Debug information */
  705. #if !defined __native_client__
  706. Text *m_centertext, *m_mousetext, *m_zoomtext;
  707. #endif
  708. };
  709. int main(int argc, char **argv)
  710. {
  711. #if defined _WIN32
  712. _chdir("../..");
  713. #endif
  714. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  715. new DebugFps(5, 5);
  716. new Fractal(ivec2(640, 480));
  717. //new DebugRecord("fractalol.ogm", 60.0f);
  718. app.Run();
  719. return EXIT_SUCCESS;
  720. }