Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

796 rindas
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 * 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 z1, z2, z3, 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. int iter = MAX_ITERATIONS - 4;
  321. for (;;)
  322. {
  323. /* Unroll the loop: tests are more expensive to do at each
  324. * iteration than the few extra multiplications. */
  325. z1 = z0 * z0 + r0;
  326. z2 = z1 * z1 + r0;
  327. z3 = z2 * z2 + r0;
  328. z0 = z3 * z3 + r0;
  329. if (z0.sqlen() >= maxsqlen)
  330. break;
  331. iter -= 4;
  332. if (iter < 4)
  333. break;
  334. }
  335. if (iter)
  336. {
  337. double n = z0.sqlen();
  338. if (z1.sqlen() >= maxsqlen) { iter += 3; n = z1.sqlen(); }
  339. else if (z2.sqlen() >= maxsqlen) { iter += 2; n = z2.sqlen(); }
  340. else if (z3.sqlen() >= maxsqlen) { iter += 1; n = z3.sqlen(); }
  341. if (n > maxsqlen * maxsqlen)
  342. n = maxsqlen * maxsqlen;
  343. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  344. double f = iter;
  345. union { double n; uint64_t x; } u = { n };
  346. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  347. k *= k1;
  348. /* Approximate log2(k) in [1,2]. */
  349. f += (- 0.344847817623168308695977510213252644185 * k
  350. + 2.024664188044341212602376988171727038739) * k
  351. - 1.674876738008591047163498125918330313237;
  352. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  353. }
  354. else
  355. {
  356. #if defined __CELLOS_LV2__
  357. *m_pixelstart++ = u8vec4(255, 0, 0, 0);
  358. #else
  359. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  360. #endif
  361. }
  362. }
  363. }
  364. virtual void TickDraw(float deltams)
  365. {
  366. WorldEntity::TickDraw(deltams);
  367. static float const vertices[] =
  368. {
  369. 1.0f, 1.0f,
  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. };
  376. static float const texcoords[] =
  377. {
  378. 1.0f, 1.0f,
  379. 0.0f, 1.0f,
  380. 0.0f, 0.0f,
  381. 0.0f, 0.0f,
  382. 1.0f, 0.0f,
  383. 1.0f, 1.0f,
  384. };
  385. if (!m_ready)
  386. {
  387. /* Create a texture of half the width and twice the height
  388. * so that we can upload four different subimages each frame. */
  389. glGenTextures(1, &m_texid);
  390. glBindTexture(GL_TEXTURE_2D, m_texid);
  391. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  392. m_size.x / 2, m_size.y * 2, 0,
  393. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  394. #if defined __CELLOS_LV2__
  395. /* We need this hint because by default the storage type is
  396. * GL_TEXTURE_SWIZZLED_GPU_SCE. */
  397. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  398. GL_TEXTURE_TILED_GPU_SCE);
  399. #endif
  400. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  401. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  402. m_shader = Shader::Create(
  403. #if !defined __CELLOS_LV2__
  404. #if !defined HAVE_GLES_2X
  405. "#version 120\n"
  406. #else
  407. "precision highp float;"
  408. #endif
  409. ""
  410. "uniform mat4 u_ZoomSettings;"
  411. "uniform vec4 u_TexelSize;"
  412. "uniform vec4 u_ScreenSize;"
  413. ""
  414. "attribute vec2 a_TexCoord;"
  415. "attribute vec2 a_Vertex;"
  416. ""
  417. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  418. ""
  419. "void main(void)"
  420. "{"
  421. " gl_Position = vec4(a_Vertex, 0.0, 1.0);"
  422. /* Center point in [-.5,.5], apply zoom and translation
  423. * transformation, and go back to texture coordinates
  424. * in [0,1]. That's the ideal point we would like to
  425. * compute the value for. Then add or remove half the
  426. * size of a texel: the distance from this new point to
  427. * the final point will be our error. */
  428. " vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625);"
  429. " vec4 zoomscale = vec4(u_ZoomSettings[0][2],"
  430. " u_ZoomSettings[1][2],"
  431. " u_ZoomSettings[2][2],"
  432. " u_ZoomSettings[3][2]);"
  433. " vec4 zoomtx = vec4(u_ZoomSettings[0][0],"
  434. " u_ZoomSettings[1][0],"
  435. " u_ZoomSettings[2][0],"
  436. " u_ZoomSettings[3][0]);"
  437. " vec4 zoomty = vec4(u_ZoomSettings[0][1],"
  438. " u_ZoomSettings[1][1],"
  439. " u_ZoomSettings[2][1],"
  440. " u_ZoomSettings[3][1]);"
  441. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  442. " + offsets.xyxy * u_TexelSize.x;"
  443. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  444. " + offsets.xyyx * u_TexelSize.y;"
  445. /* Precompute the multiple of one texel where our ideal
  446. * point lies. The fragment shader will call floor() on
  447. * this value. We add or remove a slight offset to avoid
  448. * rounding issues at the image's edges. */
  449. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  450. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  451. "}",
  452. #if !defined HAVE_GLES_2X
  453. "#version 120\n"
  454. #else
  455. "precision highp float;"
  456. #endif
  457. ""
  458. "uniform vec4 u_TexelSize;"
  459. "uniform sampler2D u_Texture;"
  460. ""
  461. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  462. ""
  463. "void main(void)"
  464. "{"
  465. " vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5);"
  466. " vec4 rx, ry, t0, dx, dy, dd;"
  467. /* Get a pixel coordinate from each slice into rx & ry */
  468. " rx = u_TexelSize.x + u_TexelSize.z * floor(v_IndexX);"
  469. " ry = u_TexelSize.y + u_TexelSize.w * floor(v_IndexY);"
  470. /* Compute inverse distance to expected pixel in dd,
  471. * and put zero if we fall outside the texture. */
  472. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  473. " dx = rx - v_CenterX;"
  474. " dy = ry - v_CenterY;"
  475. //" vec4 dd = t0 * (abs(dx) + abs(dy));"
  476. //" vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy)));"
  477. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  478. /* Modify Y coordinate to select proper quarter. */
  479. " ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);"
  480. ""
  481. #if 1
  482. "\n#if 0\n" /* XXX: disabled until we can autodetect i915 */
  483. /* t1.x <-- dd.x > dd.y */
  484. /* t1.y <-- dd.z > dd.w */
  485. " vec2 t1 = step(dd.xz, dd.yw);"
  486. /* ret.x <-- max(rx.x, rx.y) wrt. t1.x */
  487. /* ret.y <-- max(rx.z, rx.w) wrt. t1.y */
  488. /* ret.z <-- max(ry.x, ry.y) wrt. t1.x */
  489. /* ret.w <-- max(ry.z, ry.w) wrt. t1.y */
  490. " vec4 ret = mix(vec4(rx.xz, ry.xz),"
  491. " vec4(rx.yw, ry.yw), t1.xyxy);"
  492. /* dd.x <-- max(dd.x, dd.y) */
  493. /* dd.z <-- max(dd.z, dd.w) */
  494. " dd.xy = mix(dd.xz, dd.yw, t1);"
  495. /* t2 <-- dd.x > dd.z */
  496. " float t2 = step(dd.x, dd.y);"
  497. /* ret.x <-- max(ret.x, ret.y); */
  498. /* ret.y <-- max(ret.z, ret.yw; */
  499. " ret.xy = mix(ret.xz, ret.yw, t2);"
  500. "\n#else\n"
  501. /* Fallback for i915 cards -- the trick to reduce the
  502. * number of operations is to compute both step(a,b)
  503. * and step(b,a) and hope that their sum is 1. This is
  504. * almost always the case, and when it isn't we can
  505. * afford to have a few wrong pixels. However, a real
  506. * problem is when panning the image, because half the
  507. * screen is likely to flicker. To avoid this problem,
  508. * we cheat a little (see m_translate comment above). */
  509. " vec4 t1 = step(dd.xzyw, dd.ywxz);"
  510. " vec4 ret = vec4(rx.xz, ry.xz) * t1.zwzw"
  511. " + vec4(rx.yw, ry.yw) * t1.xyxy;"
  512. " dd.xy = dd.xz * t1.zw + dd.yw * t1.xy;"
  513. " vec2 t2 = step(dd.xy, dd.yx);"
  514. " ret.xy = ret.xz * t2.yy + ret.yw * t2.xx;"
  515. "\n#endif\n"
  516. /* Nearest neighbour */
  517. " gl_FragColor = texture2D(u_Texture, ret.xy);"
  518. #else
  519. /* Alternate version: some kind of linear interpolation */
  520. " vec4 p0 = texture2D(u_Texture, vec2(rx.x, ry.x));"
  521. " vec4 p1 = texture2D(u_Texture, vec2(rx.y, ry.y));"
  522. " vec4 p2 = texture2D(u_Texture, vec2(rx.z, ry.z));"
  523. " vec4 p3 = texture2D(u_Texture, vec2(rx.w, ry.w));"
  524. " gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)"
  525. " * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);"
  526. #endif
  527. "}"
  528. #else
  529. "void main(float4 a_Vertex : POSITION,"
  530. " float2 a_TexCoord : TEXCOORD0,"
  531. " uniform float4x4 u_ZoomSettings,"
  532. " uniform float4 u_TexelSize,"
  533. " uniform float4 u_ScreenSize,"
  534. " out float4 out_Position : POSITION,"
  535. " out float4 v_CenterX,"
  536. " out float4 v_CenterY,"
  537. " out float4 v_IndexX,"
  538. " out float4 v_IndexY)"
  539. "{"
  540. " out_Position = a_Vertex;"
  541. " float4 offsets = float4(0.5, -0.5, 0.015625, -0.015625);"
  542. " float4 zoomscale = float4(u_ZoomSettings[2][0],"
  543. " u_ZoomSettings[2][1],"
  544. " u_ZoomSettings[2][2],"
  545. " u_ZoomSettings[2][3]);"
  546. " float4 zoomtx = float4(u_ZoomSettings[0][0],"
  547. " u_ZoomSettings[0][1],"
  548. " u_ZoomSettings[0][2],"
  549. " u_ZoomSettings[0][3]);"
  550. " float4 zoomty = float4(u_ZoomSettings[1][0],"
  551. " u_ZoomSettings[1][1],"
  552. " u_ZoomSettings[1][2],"
  553. " u_ZoomSettings[1][3]);"
  554. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  555. " + offsets.xyxy * u_TexelSize.x;"
  556. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  557. " + offsets.xyyx * u_TexelSize.y;"
  558. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  559. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  560. "}",
  561. "void main(in float4 v_CenterX,"
  562. " in float4 v_CenterY,"
  563. " in float4 v_IndexX,"
  564. " in float4 v_IndexY,"
  565. " uniform float4 u_TexelSize2,"
  566. " uniform sampler2D u_Texture,"
  567. " out float4 out_FragColor : COLOR)"
  568. "{"
  569. " float4 v05 = float4(0.5, 0.5, 0.5, 0.5);"
  570. " float4 rx, ry, t0, dx, dy, dd;"
  571. " rx = u_TexelSize2.x + u_TexelSize2.z * floor(v_IndexX);"
  572. " ry = u_TexelSize2.y + u_TexelSize2.w * floor(v_IndexY);"
  573. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  574. " dx = rx - v_CenterX;"
  575. " dy = ry - v_CenterY;"
  576. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  577. " ry = ry * 0.25 + float4(0.0, 0.25, 0.5, 0.75);"
  578. " float2 t1 = step(dd.xz, dd.yw);"
  579. " float4 ret = lerp(float4(rx.xz, ry.xz),"
  580. " float4(rx.yw, ry.yw), t1.xyxy);"
  581. " dd.xy = lerp(dd.xz, dd.yw, t1);"
  582. " float t2 = step(dd.x, dd.y);"
  583. " ret.xy = lerp(ret.xz, ret.yw, t2);"
  584. " out_FragColor = tex2D(u_Texture, ret.xy);"
  585. "}"
  586. #endif
  587. );
  588. m_vertexattrib = m_shader->GetAttribLocation("a_Vertex");
  589. m_texattrib = m_shader->GetAttribLocation("a_TexCoord");
  590. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  591. #if defined __CELLOS_LV2__
  592. m_texeluni2 = m_shader->GetUniformLocation("u_TexelSize2");
  593. #endif
  594. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  595. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  596. m_ready = true;
  597. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  598. /* Method 1: store vertex buffer on the GPU memory */
  599. glGenBuffers(1, &m_vbo);
  600. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  601. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  602. GL_STATIC_DRAW);
  603. glGenBuffers(1, &m_tbo);
  604. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  605. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  606. GL_STATIC_DRAW);
  607. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  608. /* Method 2: upload vertex information at each frame */
  609. #else
  610. #endif
  611. /* FIXME: this object never cleans up */
  612. }
  613. #if !defined HAVE_GLES_2X
  614. glEnable(GL_TEXTURE_2D);
  615. #endif
  616. glBindTexture(GL_TEXTURE_2D, m_texid);
  617. if (m_dirty[m_frame])
  618. {
  619. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  620. m_donequeue.Pop();
  621. m_dirty[m_frame]--;
  622. #ifdef __CELLOS_LV2__
  623. /* glTexSubImage2D is extremely slow on the PS3, to the point
  624. * that uploading the whole texture is 40 times faster. */
  625. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  626. m_size.x / 2, m_size.y * 2, 0,
  627. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  628. #else
  629. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  630. m_size.x / 2, m_size.y / 2,
  631. TEXTURE_FORMAT, TEXTURE_TYPE,
  632. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  633. #endif
  634. }
  635. m_shader->Bind();
  636. m_shader->SetUniform(m_texeluni, m_texel_settings);
  637. #if defined __CELLOS_LV2__
  638. m_shader->SetUniform(m_texeluni2, m_texel_settings);
  639. #endif
  640. m_shader->SetUniform(m_screenuni, m_screen_settings);
  641. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  642. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  643. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  644. glEnableVertexAttribArray(m_vertexattrib);
  645. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  646. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  647. glEnableVertexAttribArray(m_texattrib);
  648. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  649. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  650. /* Never used for now */
  651. //glEnableVertexAttribArray(m_vertexattrib);
  652. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  653. #else
  654. glEnableClientState(GL_VERTEX_ARRAY);
  655. glVertexPointer(2, GL_FLOAT, 0, vertices);
  656. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  657. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  658. #endif
  659. glDrawArrays(GL_TRIANGLES, 0, 6);
  660. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  661. glDisableVertexAttribArray(m_vertexattrib);
  662. glDisableVertexAttribArray(m_texattrib);
  663. glBindBuffer(GL_ARRAY_BUFFER, 0);
  664. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  665. /* Never used for now */
  666. //glDisableVertexAttribArray(m_vertexattrib);
  667. //glDisableVertexAttribArray(m_texattrib);
  668. #else
  669. glDisableClientState(GL_VERTEX_ARRAY);
  670. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  671. #endif
  672. }
  673. private:
  674. static int const MAX_ITERATIONS = 340;
  675. static int const PALETTE_STEP = 32;
  676. static int const MAX_THREADS = 8;
  677. static int const MAX_LINES = 8;
  678. ivec2 m_size, m_window_size, m_oldmouse;
  679. double m_window2world;
  680. f64vec2 m_texel2world;
  681. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  682. Shader *m_shader;
  683. GLuint m_texid;
  684. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  685. GLuint m_vbo, m_tbo;
  686. GLuint m_tco;
  687. #endif
  688. int m_vertexattrib, m_texattrib, m_texeluni, m_screenuni, m_zoomuni;
  689. #if defined __CELLOS_LV2__
  690. int m_texeluni2;
  691. #endif
  692. int m_frame, m_slices, m_dirty[4];
  693. bool m_ready, m_drag;
  694. f64cmplx m_center, m_translate;
  695. double m_zoom_speed, m_radius;
  696. vec4 m_texel_settings, m_screen_settings;
  697. mat4 m_zoom_settings;
  698. f64cmplx m_deltashift[4];
  699. double m_deltascale[4];
  700. /* Worker threads */
  701. Thread *m_threads[MAX_THREADS];
  702. Queue m_spawnqueue, m_jobqueue, m_donequeue;
  703. /* Debug information */
  704. #if !defined __native_client__
  705. Text *m_centertext, *m_mousetext, *m_zoomtext;
  706. #endif
  707. };
  708. int main(int argc, char **argv)
  709. {
  710. #if defined _WIN32
  711. _chdir("../..");
  712. #endif
  713. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  714. new DebugFps(5, 5);
  715. new Fractal(ivec2(640, 480));
  716. //new DebugRecord("fractalol.ogm", 60.0f);
  717. app.Run();
  718. return EXIT_SUCCESS;
  719. }