Recent

Author Topic: Can /my/ AI help me with pascal coding?  (Read 7707 times)

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #60 on: June 09, 2026, 12:12:06 pm »
Hi!
To make any sense of this mess, the Google AI monster reversed the fractal fragment shader back into pure Pascal. With -CfSSE3 enabled, this number-crunching algorithm actually hauls ass pretty well, and the output is almost a 1:1 match.But there is a catch: for some damn reason, FPC 3.2.2 leaves the background completely black (though everything works totally fine in "plasma cube" mode). This glitch might be specific to the 32-bit build.Wishing you all a great day, and may you find good mutual understanding with AI!

Code: Pascal  [Select][+][-]
  1. {$FPUTYPE SSE3}{$mode delphi}{$apptype gui}
  2. program julia_fractal_cpu;
  3.  
  4. uses GL, GLU, GLEXT, Glut;
  5.  
  6. const
  7.   WIDTH = 512;
  8.   HEIGHT = 512;
  9.  
  10. type
  11.   TVec3 = record
  12.     R, G, B: Single;
  13.   end;
  14.  
  15.   TSampler2D = array[0..255, 0..2] of Single;
  16.  
  17. var
  18.   FractalTexID: GLuint;
  19.  
  20.   u_pal: TSampler2D;
  21.  
  22.   FrameBufferRGB_F32: array[0..HEIGHT-1, 0..WIDTH-1, 0..2] of Single;
  23.  
  24.   Cx: Single = -0.7;
  25.   Cy: Single = 0.27015;
  26.   Zoom: Single = 3.0;
  27.  
  28. procedure GenPalette;
  29. var
  30.   i: Integer;
  31. begin
  32.   //randomize;
  33.   for i := 0 to 255 do
  34.   begin
  35.     u_pal[i, 0] := random(1000) / 1000.0; // R (0.0 .. 1.0)
  36.     u_pal[i, 1] := random(1000) / 1000.0; // G (0.0 .. 1.0)
  37.     u_pal[i, 2] := random(1000) / 1000.0; // B (0.0 .. 1.0)
  38.   end;
  39. end;
  40.  
  41.  
  42. procedure InitFractalTexture;
  43. begin
  44.   glGenTextures(1, @FractalTexID);
  45.   glBindTexture(GL_TEXTURE_2D, FractalTexID);
  46.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  47.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  48.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  49.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  50.  
  51.  
  52.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, WIDTH, HEIGHT, 0, GL_RGB, GL_FLOAT, nil);
  53. end;
  54.  
  55.  
  56. function texture2D(const sampler: TSampler2D; TexCoordX, TexCoordY: Single): TVec3;inline;
  57. var
  58.   RawValue: Single;
  59.   MapPos: Single;
  60.   Idx1, Idx2: Integer;
  61.   FracPart: Single;
  62. begin
  63.   //  vec2(x, y) * 0.5 + vec2(0.5, 0.5)
  64.   RawValue := TexCoordX * 0.5 + 0.5;
  65.  
  66.   // Clamp
  67.   if RawValue < 0.0 then RawValue := 0.0;
  68.   if RawValue > 1.0 then RawValue := 1.0;
  69.  
  70.  
  71.   MapPos := RawValue * 255.0;
  72.  
  73.  
  74.   Idx1 := Trunc(MapPos);
  75.   Idx2 := Idx1 + 1;
  76.   if Idx2 > 255 then Idx2 := 255;
  77.  
  78.   FracPart := MapPos - Idx1;
  79.  
  80.   Result.R := sampler[Idx1, 0] + FracPart * (sampler[Idx2, 0] - sampler[Idx1, 0]);
  81.   Result.G := sampler[Idx1, 1] + FracPart * (sampler[Idx2, 1] - sampler[Idx1, 1]);
  82.   Result.B := sampler[Idx1, 2] + FracPart * (sampler[Idx2, 2] - sampler[Idx1, 2]);
  83. end;
  84.  
  85.  
  86. procedure RenderJuliaToFloatRGBBuffer;
  87. var
  88.   px, py, i: Integer;
  89.   vTexCoordX, vTexCoordY: Single;
  90.   x, y, t: Single;
  91.   Color: TVec3;
  92. begin
  93.   for py := 0 to HEIGHT - 1 do
  94.   begin
  95.     vTexCoordY := py / (HEIGHT - 1);
  96.     for px := 0 to WIDTH - 1 do
  97.     begin
  98.       vTexCoordX := px / (WIDTH - 1);
  99.  
  100.       //  float x = (vTexCoord.x - 0.5) * u_zoom;
  101.       x := (vTexCoordX - 0.5) * Zoom;
  102.       y := (vTexCoordY - 0.5) * Zoom;
  103.  
  104.       for i := 0 to 19 do
  105.       begin                       // PLAZMA CUBE: (x * x) + (y * y)
  106.         t := (x * x) - (y * y) + Cx; // float t = (x * x) - (y * y) + u_cx;
  107.         y := (x + x) * y + Cy;       // y = (x + x) * y + u_cy;
  108.         x := t;                      // x = t;
  109.       end;
  110.  
  111.       // gl_FragColor = texture2D(u_pal, vec2(x, y) * 0.5 + vec2(0.5, 0.5));
  112.       Color := texture2D(u_pal, x, y);
  113.  
  114.  
  115.       FrameBufferRGB_F32[py, px, 0] := Color.R;
  116.       FrameBufferRGB_F32[py, px, 1] := Color.G;
  117.       FrameBufferRGB_F32[py, px, 2] := Color.B;
  118.     end;
  119.   end;
  120.  
  121.   glBindTexture(GL_TEXTURE_2D, FractalTexID);
  122.   glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_RGB, GL_FLOAT, @FrameBufferRGB_F32);
  123. end;
  124.  
  125. procedure DisplayProc; cdecl;
  126. begin
  127.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  128.  
  129.   RenderJuliaToFloatRGBBuffer;
  130.  
  131.   glEnable(GL_TEXTURE_2D);
  132.   glBindTexture(GL_TEXTURE_2D, FractalTexID);
  133.  
  134.   glBegin(GL_QUADS);
  135.     glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
  136.     glTexCoord2f(1.0, 0.0); glVertex2f( 1.0, -1.0);
  137.     glTexCoord2f(1.0, 1.0); glVertex2f( 1.0,  1.0);
  138.     glTexCoord2f(0.0, 1.0); glVertex2f(-1.0,  1.0);
  139.   glEnd();
  140.  
  141.   glDisable(GL_TEXTURE_2D);
  142.   glBindTexture(GL_TEXTURE_2D, 0);
  143.  
  144.   glutSwapBuffers();
  145. end;
  146.  
  147. procedure ReshapeProc(w, h: Integer); cdecl;
  148. begin
  149.   glViewport(0, 0, w, h);
  150. end;
  151.  
  152. procedure PassiveMotionProc(x, y: Integer); cdecl;
  153. begin
  154.   Cx := (x / glutGet(GLUT_WINDOW_WIDTH)) * 2.0 - 1.0;
  155.   Cy := ((glutGet(GLUT_WINDOW_HEIGHT) - y) / glutGet(GLUT_WINDOW_HEIGHT)) * 2.0 - 1.0;
  156.   glutPostRedisplay();
  157. end;
  158.  
  159. procedure KeyboardProc(key: Byte; x, y: Integer); cdecl;
  160. begin
  161.   case Chr(key) of
  162.     '+', '=': Zoom := Zoom * 0.85;
  163.     '-':      Zoom := Zoom * 1.15;
  164.     'r','R':  GenPalette;
  165.     #27:      Halt(0);
  166.   end;
  167.   glutPostRedisplay();
  168. end;
  169.  
  170. begin
  171.   glutInit(@argc, argv);
  172.   glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH);
  173.   glutInitWindowSize(WIDTH, HEIGHT);
  174.   glutCreateWindow('Julia Set OpenGL 2.0 (CPU)'+' - FPC '+{$I %FPCVERSION%});
  175.   glutDisplayFunc(@DisplayProc);
  176.   glutReshapeFunc(@ReshapeProc);
  177.   glutKeyboardFunc(@KeyboardProc);
  178.   glutPassiveMotionFunc(@PassiveMotionProc);
  179.  
  180.   GenPalette;
  181.   InitFractalTexture;
  182.  
  183.   glClearColor(0.0, 0.0, 0.0, 1.0);
  184.   glutMainLoop();
  185. end.
  186.  
  187.  

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #61 on: June 12, 2026, 04:26:18 pm »
Looks like the bug is in the JP instruction after COMISS. No parity check is needed here! Please fix it so this beautiful plasma fractal works fine.

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #62 on: June 13, 2026, 06:49:37 pm »
The JP instruction is involved in FPU operations, but its usage is closer to range checking or integer overflow. Professional C compilers (like GCC or Intel) don't use it. Maybe I missed something in the Free Pascal options, but at least I found a way to fix this JP instruction

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #63 on: June 14, 2026, 02:25:12 pm »
It wouldn't be a bad idea to use Gemini here, but I'm not sure it would grasp all the details... after all, it's a search assistant, and our co-coding is more like an 'acting up for the fun of it' kind of thing for me, packed with its own very specific quirks in terms of breakthroughs and eye-openers. Anyway, passing along a wish from Gemini the neuro-jester: 'May the night burn hot with working solutions, and may the morning echo with a victorious pig squeal...

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #64 on: June 15, 2026, 05:10:10 pm »
Given a snippet of optimization code, Gemini easily demonstrated and thoroughly explained the JP fix. Armed with this masterclass, we managed to resolve another 'major issue' in the 32-bit SSE3 codegen — the presence of the x87 fisttp instruction

Thaddy

  • Hero Member
  • *****
  • Posts: 19388
  • Glad to be alive.
Re: Can /my/ AI help me with pascal coding?
« Reply #65 on: June 15, 2026, 05:45:52 pm »
I don't see why you bother with SSE3? I don't think it is being looked at in any way.
You can of course file a bug report, but SSE3 is not really relevant anymore.
« Last Edit: June 15, 2026, 05:47:34 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #66 on: June 15, 2026, 08:25:36 pm »
Thaddy, This was just an example of how AI can help with certain aspects of Pascal programming. And it's worth noting that this also somewhat affects x64 platforms... or maybe it doesn't affect them at all.

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #67 on: June 18, 2026, 12:56:29 am »
In this case, COMISS was also an issue, and it can easily be replaced with MAXSS/MINSS.

In general, this technique is also relevant for high-performance neural networks, although we need a better way to handle full SIMD instruction utilization.

schuler

  • Sr. Member
  • ****
  • Posts: 367
Re: Can /my/ AI help me with pascal coding?
« Reply #68 on: June 20, 2026, 03:59:41 pm »
Hi
Cool, I've just returned from holiday in sunny Glücksburg-Germany and I'll have to get back in the race... Will test - Thanks mate.
Regards Benny

In 2 or 3 weeks from now, I will have nearly all pas-sqlite3 automated testing solved including the original TCL based testing. Then, it will be time for human testing. I will need help with testing in 2 or 3 weeks.
« Last Edit: June 20, 2026, 04:04:31 pm by schuler »

cdbc

  • Hero Member
  • *****
  • Posts: 2856
    • http://www.cdbc.dk
Re: Can /my/ AI help me with pascal coding?
« Reply #69 on: June 20, 2026, 04:11:40 pm »
Hi Joao, Cool  8)
You just say when and I'll help you test  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

microxa

  • New Member
  • *
  • Posts: 35
Re: Can /my/ AI help me with pascal coding?
« Reply #70 on: June 22, 2026, 07:22:07 am »
We had a brief with Gemini and came to the conclusion to reuse the good old (and mostly obsolete) {$MMX+} directive to explicitly mark computational SIMD SSE blocks. On the other hand, we discovered register involvement of the P flag in floating-point comparison functions, which we deemed a small issue. Because of this, we'll have to stick with version 2.6.4 as the most stable/suitable for advanced Pascal retro coding.

 

TinyPortal © 2005-2018