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.
 
 
 

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