您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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