Results 1 to 8 of 8

Thread: Opengl - get rid of glu dll

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    Thanks for sharing your code!

    I wasn't aware of the 'absolute' keyword - you learn something new every day.

    For that kind of access I either typecast or use the 'case' syntax with records (example here from Jink) :

    Code:
    TVec4 = packed record
          case UInt8 of
          0:(XYZ : TVec3);
          1:(XY, ZW : TVec2);
          2:(X,Y,Z,W : JFloat);
          3:(V : packed array[0..3] of JFloat);
    end;
    
    TJMatrix = packed record
          case UInt8 of
          0:(_11, _12, _13, _14,
             _21, _22, _23, _24,
             _31, _32, _33, _34,
             _41, _42, _43, _44 : JFloat);
          1 : (M : packed array[0..15] of JFloat) ;
          2 : (RC : packed array[0..3, 0..3] of JFloat);
          3 : (Rows : packed array[0..3] of TVec4);
    end;
    I'll certainly make use of the 'absolute' keyword in future to make things a little more succinct
    Last edited by phibermon; 11-03-2018 at 04:22 PM.
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  2. #2
    if it's a simple case like dealing with a variable in a single function like this, it makes things kinda easier, otherwise i make use of case within records too, everything has its purpose
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  3. #3
    I wasn't aware of the 'absolute' keyword
    Be wary it doesn't always work. If you have register variables optimization on, the compiler sometimes gets confused.
    Example: my attempt to implement Carmack's fast inverse square root.
    Code:
        function FastInverseSquareRoot(a: float): float; inline;
        var
          i: longint absolute Result;
        begin
          Result:= a;
          i:= $5f3759df - (i shr 1);
          Result*= 1.5 - (a * 0.5 * Result * Result);
          Result*= 1.5 - (a * 0.5 * Result * Result);
        end;
    - it failed craptacularly. The compiler did not give an error but generated totally rubbish code seqence (I turned on ASM output to check).

    So I had to settle on
    Code:
        // https://en.wikipedia.org/wiki/Fast_inverse_square_root
        function FastInverseSquareRoot(a: float): float; inline;
        var
          i: longint;// absolute Result; //code generator FAILS to marry SSE2 and general-purpose registers
        begin
          //Result:= a;
          i:= longint(pointer(@a)^);
          i:= $5f3759df - (i shr 1);
          Result:= float(pointer(@i)^);
          Result*= 1.5 - (a * 0.5 * Result * Result);
          Result*= 1.5 - (a * 0.5 * Result * Result);
        end;
    P.S. So I'd group absolute together with goto: necessary for backward compatibility but not recommended to use unless you have a very good understanding of processes involved.
    Last edited by Chebmaster; 13-03-2018 at 08:28 AM.

  4. #4
    I have my own variant of that:

    function NSQRT(X: single): single; {$ifdef fpc} inline; {$endif}
    var
    XHalf: single;
    I: integer Absolute X;
    X2: single Absolute I;
    XB: single;
    begin
    XB := X;
    XHalf := 0.5 * X;
    I := $5f375a86 - (I shr 1);
    // ($5f375a86 for best range, $5F375A84 for whole fpu range, 5F3759DF for carmacks range)
    X := X2 * (1.5 - XHalf * X2 * X2);
    Result := XB * X;
    end;
    Perhaps not as optimal, but look at how 2 variables are on same absolute variable address, iirc this version works with optimizations on.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  5. #5
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    I'd not come across this approximation either - thanks very much for the info both! exactly the kind of esoteric little trick I love to explore

    Reading up, it was apparently used before our lord and saviour Carmack did, as was the famous 'Carmack's Reverse' technique for shadow volumes.

    But John is a master of applied mathematics in the field of 3D - had he been born 10 years earlier we'd probably of seen Doom on the Amiga at full speed because he found some trick you could pull with the floppy drive
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  6. #6
    exactly the kind of esoteric little trick I love to explore
    Just don't forget: on modern hardware this vintage method has the same speed(x86) or is three times *slower*(Raspberry Pi) than honest 1/sqrt(x) while the dedicated SSE instruction RSQRTPS leaves it in the dust, being more than 8 times faster (although it is not deterministic) because it does that trick *in hardware* operating on 4 floats in parallel.

    See my research results @ http://openarena.ws/board/index.php?topic=5379.0

    So, be wary of stale methodologies and remember: modern hardware demands different optimizations!

  7. #7
    Quote Originally Posted by Chebmaster View Post
    Just don't forget: on modern hardware this vintage method has the same speed(x86) or is three times *slower*(Raspberry Pi) than honest 1/sqrt(x) while the dedicated SSE instruction RSQRTPS leaves it in the dust, being more than 8 times faster (although it is not deterministic) because it does that trick *in hardware* operating on 4 floats in parallel.

    See my research results @ http://openarena.ws/board/index.php?topic=5379.0

    So, be wary of stale methodologies and remember: modern hardware demands different optimizations!
    This is gold, i didn't even consider this, and yeah we did come into era of 8 and 16 core processor with massive IPCs and internal optimizations, i guess it is time to put some of these old methods to rest.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •