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.

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