6879f2c78cd6 — Leonard Ritter 2 years ago
* scopes project support
A => _project.sc +2 -0
@@ 0,0 1,2 @@ 
+
+import .lib.tukan.use

          
M lib/tukan/ResourceGroup.sc +75 -61
@@ 26,6 26,79 @@ buffer buf-draw-cmd : DrawArraysIndirect
 
 ################################################################################
 
+struct IndirectCompute
+    let NUM_CMD_BUFFERS = 1
+
+    command-buffer : GL.Buffer
+    program : GL.Program
+
+    # shaderfunc returns sizex sizey sizez
+    inline __typecall (cls shaderfunc)
+        let self = (super-type.__typecall cls)
+        setup-ssbo self.command-buffer buf-compute-cmd NUM_CMD_BUFFERS
+        call
+            attach-shaders self.program
+                compute =
+                    fn setup-draw-arrays-command ()
+                        local_size 1 1 1
+                        buf-draw-cmd.command =
+                            DispatchIndirectCommand (shaderfunc)
+                        ;
+        self
+
+    inline setup (self setupfunc)
+        GL.UseProgram self.program
+        bind-ssbo self.command-buffer buf-compute-cmd none 0
+        static-if (none? setupfunc)
+        else
+            setupfunc;
+        GL.DispatchCompute 1 1 1
+        GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
+
+    inline execute (self)
+        GL.BindBuffer GL.DISPATCH_INDIRECT_BUFFER self.command-buffer
+        GL.DispatchComputeIndirect 0
+        #GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
+
+################################################################################
+
+struct IndirectDrawArrays
+    let NUM_CMD_BUFFERS = 1
+
+    command-buffer : GL.Buffer
+    program : GL.Program
+
+    # shaderfunc returns count, instancecount, first, baseinstance
+    inline __typecall (cls shaderfunc)
+        let self = (super-type.__typecall cls)
+        setup-ssbo self.command-buffer buf-draw-cmd NUM_CMD_BUFFERS
+        call
+            attach-shaders self.program
+                compute =
+                    fn setup-draw-arrays-command ()
+                        local_size 1 1 1
+                        buf-draw-cmd.command =
+                            DrawArraysIndirectCommand (shaderfunc)
+                        ;
+        self
+
+
+    inline setup (self setupfunc)
+        GL.UseProgram self.program
+        bind-ssbo self.command-buffer buf-draw-cmd none 0
+        static-if (none? setupfunc)
+        else
+            setupfunc;
+        GL.DispatchCompute 1 1 1
+        GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
+
+    inline execute (self mode)
+        #GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)
+        GL.BindBuffer GL.DRAW_INDIRECT_BUFFER self.command-buffer
+        GL.DrawArraysIndirect mode null
+
+################################################################################
+
 # prototype for initialization from usage
 
 type ResourceGroup

          
@@ 98,6 171,7 @@ type ResourceGroup
         ;
 
 run-stage;
+################################################################################
 
 type+ ResourceGroup
     inline static-array (self T size func)

          
@@ 117,68 191,8 @@ type+ ResourceGroup
     inline compute-program (self func ...)
         program self (compute = func) ...
 
-    # shaderfunc returns sizex sizey sizez
-    # provides a setup and execute function
-    inline indirect-compute-setup (self shaderfunc)
-        let NUM_CMD_BUFFERS = 1
-        let buf =
-            'static self GL.Buffer
-                inline ()
-                    let buf = (GL.Buffer)
-                    setup-ssbo buf buf-compute-cmd NUM_CMD_BUFFERS
-                    buf
-        let pg =
-            compute-program self
-                fn setup-compute-command ()
-                    local_size 1 1 1
-                    buf-compute-cmd.command =
-                        DispatchIndirectCommand (shaderfunc)
-                    ;
-        _
-            inline (setupfunc)
-                GL.UseProgram pg
-                bind-ssbo buf buf-compute-cmd none 0
-                static-if (none? setupfunc)
-                else
-                    setupfunc;
-                GL.DispatchCompute 1 1 1
-                GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
-            inline ()
-                GL.BindBuffer GL.DISPATCH_INDIRECT_BUFFER buf
-                GL.DispatchComputeIndirect 0
-                GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
 
-    # shaderfunc returns count, instancecount, first, baseinstance
-    # provides a setup and execute function
-    inline indirect-draw-arrays-setup (self shaderfunc)
-        let NUM_CMD_BUFFERS = 1
-        let buf =
-            'static self GL.Buffer
-                inline ()
-                    let buf = (GL.Buffer)
-                    setup-ssbo buf buf-draw-cmd NUM_CMD_BUFFERS
-                    buf
-        let pg =
-            compute-program self
-                fn setup-draw-arrays-command ()
-                    local_size 1 1 1
-                    buf-draw-cmd.command =
-                        DrawArraysIndirectCommand (shaderfunc)
-                    ;
-        _
-            inline (setupfunc)
-                GL.UseProgram pg
-                bind-ssbo buf buf-draw-cmd none 0
-                static-if (none? setupfunc)
-                else
-                    setupfunc;
-                GL.DispatchCompute 1 1 1
-                GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
-            inline (mode)
-                GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)
-                GL.BindBuffer GL.DRAW_INDIRECT_BUFFER buf
-                GL.DrawArraysIndirect mode null
 
 do
-    let ResourceGroup
+    let ResourceGroup IndirectDrawArrays IndirectCompute
     locals;

          
M testing/test_cascade_dmc.sc +13 -11
@@ 1588,7 1588,7 @@ inline main ()
     fn per-frame-setup (size pg-test frame)
         let rg =
             'force-unwrap rg
-        from (methodsof rg) let static program compute-program indirect-draw-arrays-setup
+        from (methodsof rg) let static program compute-program
 
         GL.BindTextureUnit 0 fb-scene-color
         GL.Uniform smp-screen 0

          
@@ 1690,20 1690,22 @@ inline main ()
         if ((frame % 60) == 0)
             print-in-count;
 
-        vvv bind setup-draw-arrays exec-draw-arrays
-        indirect-draw-arrays-setup
-            inline ()
-                _
-                    deref vertex-in.count
-                    1
-                    0
-                    0
+        let ida =
+            static IndirectDrawArrays
+                inline ()
+                    IndirectDrawArrays
+                        inline ()
+                            _
+                                deref vertex-in.count
+                                1
+                                0
+                                0
 
         GL.BindBufferRange GL.SHADER_STORAGE_BUFFER
             BINDING_BUF_VERTEX_IN
             vertex_buffer
             \ 0:i64 (i64 vertex_buffer_sz)
-        setup-draw-arrays;
+        'setup ida
 
         do
             GL.BindFramebuffer GL.FRAMEBUFFER fb-scene

          
@@ 1726,7 1728,7 @@ inline main ()
             GL.BindTextureUnit 1 world
             GL.Uniform smp-world 1
             GL.BindVertexArray vao-empty
-            exec-draw-arrays GL.TRIANGLES
+            'execute ida GL.TRIANGLES
 
             GL.Disable GL.DEPTH_TEST
             GL.Disable GL.CULL_FACE

          
M testing/test_subjective.sc +0 -1
@@ 4,7 4,6 @@ using import Capture
 using import Option
 using import itertools
 
-import ..lib.tukan.use
 using import tukan.VertexPainter
 using import tukan.GLMain
 using import tukan.gl

          
A => testing/unique_cube_bit_patterns.sc +138 -0
@@ 0,0 1,138 @@ 
+using import Set
+
+inline MASK (s...)
+    |
+        va-map
+            inline (i)
+                (va@ i s...) << (i * 3)
+            va-range 8
+inline UNPACK_MASK (m)
+    va-map
+        inline (i)
+            ((m >> (i * 3)) & 7)
+        va-range 8
+
+let IDENTITY_MASK = (MASK 0 1 2 3 4 5 6 7)
+
+inline remap (x m)
+    let x im = (unpack x)
+    tupleof
+        |
+            va-map
+                inline (i)
+                    ((x >> i) & 1) << ((m >> (i * 3)) & 7)
+                va-range 8
+        MASK
+            va-map
+                inline (i)
+                    i := ((im >> (i * 3)) & 7)
+                    (m >> (i * 3)) & 7
+                va-range 8
+
+fn rotate_cube_axis (i)
+    remap i (MASK 0 4 1 5 2 6 3 7)
+
+fn swap_yz (i)
+    remap i (MASK 0 1 4 5 2 3 6 7)
+
+fn flip_z (i)
+    remap i (MASK 4 5 6 7 0 1 2 3)
+fn flip_y (i)
+    remap i (MASK 2 3 0 1 6 7 4 5)
+fn flip_x (i)
+    remap i (MASK 1 0 3 2 5 4 7 6)
+
+inline mintuple (a b...)
+    va-lfold a
+        inline (k v s)
+            ? ((v @ 0) < (s @ 0)) v s
+        b...
+
+fn canonical_flip (i)
+    ix := (flip_x i)
+    iy := (flip_y i)
+    ixy := (flip_y ix)
+    iz := (flip_z i)
+    ixz := (flip_x iz)
+    iyz := (flip_y iz)
+    ixyz := (flip_y ixz)
+    mintuple i ix iy ixy iz ixz iyz ixyz
+
+inline canonicalize (i k)
+    # sign flip
+    let ni = (i ^ 0b11111111)
+    let f? = ((bitcount ni) < (bitcount i))
+    let i = (? f? ni i)
+    i := (tupleof i IDENTITY_MASK)
+    # axis rotation
+    yzx := (canonical_flip (rotate_cube_axis i))
+    zxy := (canonical_flip (rotate_cube_axis yzx))
+    xzy := (canonical_flip (swap_yz i))
+    yxz := (canonical_flip (swap_yz yzx))
+    zyx := (canonical_flip (swap_yz zxy))
+    let i = (mintuple i xzy yzx yxz zxy zyx)
+    _ i f?
+
+local patterns : (Set i32)
+local masks : (Set i32)
+
+#
+    0 1 2 3 4 5 6 7
+    0 1 4 5 2 3 6 7
+    0 2 4 6 1 3 5 7
+    0 4 1 5 2 6 3 7
+    1 0 5 4 3 2 7 6
+    1 3 0 2 5 7 4 6
+    1 3 5 7 0 2 4 6
+    1 5 0 4 3 7 2 6
+    2 0 3 1 6 4 7 5
+    2 0 6 4 3 1 7 5
+    2 3 6 7 0 1 4 5
+    2 6 3 7 0 4 1 5
+    3 1 2 0 7 5 6 4
+    3 1 7 5 2 0 6 4
+    3 2 7 6 1 0 5 4
+    3 7 2 6 1 5 0 4
+    4 0 5 1 6 2 7 3
+    4 5 0 1 6 7 2 3
+    4 6 0 2 5 7 1 3
+    4 6 5 7 0 2 1 3
+    5 1 4 0 7 3 6 2
+    5 4 1 0 7 6 3 2
+    5 7 1 3 4 6 0 2
+    5 7 4 6 1 3 0 2
+    6 2 7 3 4 0 5 1
+    6 4 2 0 7 5 3 1
+    6 4 7 5 2 0 3 1
+    6 7 2 3 4 5 0 1
+    7 3 6 2 5 1 4 0
+    7 5 3 1 6 4 2 0
+    7 5 6 4 3 1 2 0
+    7 6 3 2 5 4 1 0
+
+for i in (range 256)
+    i0 := i
+    let im f? = (canonicalize i 0)
+    let i m = (unpack im)
+    #if (not ((i @ 0) in patterns))
+    i1 := ((remap (tupleof i0 IDENTITY_MASK) m) @ 0)
+    i1 := i1 ^ (? f? 255 0)
+    assert (i == i1)
+    print (bin i) (bin i1) (UNPACK_MASK m)
+    #assert ((remap (tupleof i0 IDENTITY_MASK) m) @ 0 == i)
+    'insert patterns i
+    'insert masks m
+
+    ;
+
+print (countof patterns) "unique patterns"
+print (countof masks) "unique permutation maps"
+for m in masks
+    let k0 k1 k2 k3 = (UNPACK_MASK m)
+    print (UNPACK_MASK m)
+    let m2 = ((m & 0xfff) | (((MASK k3 k2 k1 k0 0 0 0 0) ^ 0xfff) << 12))
+    assert (m == m2)
+
+
+
+;
  No newline at end of file

          
A => testing/unique_tet_bit_patterns.sc +147 -0
@@ 0,0 1,147 @@ 
+using import Set
+using import itertools
+
+inline MASK (s...)
+    |
+        va-map
+            inline (i)
+                (va@ i s...) << (i * 2)
+            va-range 4
+inline UNPACK_MASK (m)
+    va-map
+        inline (i)
+            ((m >> (i * 2)) & 3)
+        va-range 4
+
+let IDENTITY_MASK = (MASK 0 1 2 3)
+
+inline remap (x m)
+    let x im = (unpack x)
+    tupleof
+        |
+            va-map
+                inline (i)
+                    ((x >> i) & 1) << ((m >> (i * 2)) & 3)
+                va-range 4
+        MASK
+            va-map
+                inline (i)
+                    i := ((im >> (i * 2)) & 3)
+                    (m >> (i * 2)) & 3
+                va-range 4
+
+inline canonicalize (i k)
+    # sign flip
+    let ni = (i ^ 0b1111)
+    let f? = ((bitcount ni) < (bitcount i))
+    let i = (? f? ni i)
+    i := (tupleof i IDENTITY_MASK)
+    # permutations
+    vvv bind i
+    fold (i) for x in (permutate-range 4)
+        j := (remap i (MASK (unpack x)))
+        ? ((j @ 0) < (i @ 0)) j i
+    _ i f?
+
+local patterns : (Set i32)
+local masks : (Set i32)
+
+for i in (range 16)
+    i0 := i
+    let im f? = (canonicalize i 0)
+    let i m = (unpack im)
+    #if (not ((i @ 0) in patterns))
+    i1 := ((remap (tupleof i0 IDENTITY_MASK) m) @ 0)
+    i1 := i1 ^ (? f? 15 0)
+    assert (i == i1)
+    print (bin i) (bin i1) (UNPACK_MASK m)
+    #assert ((remap (tupleof i0 IDENTITY_MASK) m) @ 0 == i)
+    'insert patterns i
+    'insert masks m
+
+    ;
+
+print (countof patterns) "unique patterns"
+for p in patterns
+    print (bin p)
+print (countof masks) "unique permutation maps"
+for m in masks
+    #let k0 k1 k2 k3 = (UNPACK_MASK m)
+    print (UNPACK_MASK m)
+    #let m2 = ((m & 0xfff) | (((MASK k3 k2 k1 k0 0 0 0 0) ^ 0xfff) << 12))
+    #assert (m == m2)
+
+fn edge (i)
+    g := (i >> 1) + 1
+    c := i & 1
+    m := c * ((g & 1) + 1)
+    #m := (((i >> 1) & 1) ^ 1) + 1
+    return m (g ^ m)
+
+fn edge2 (i)
+    g := (i % 3) + 1
+    c := (i // 3)
+    m := c * ((g & 1) + 1)
+    #m := (((i >> 1) & 1) ^ 1) + 1
+    return m (g ^ m)
+
+#
+    000 01
+    001 01
+    010 10
+    011 10
+    100 01
+    101 01
+
+fn edgegroup (a b)
+    g := a ^ b
+    m := (g & 1) + 1
+    c := ((a != 0) & (b != 0)) as i32
+    i := (g << 1) - 2 + c
+    G := (i >> 1) + 1
+    C := i & 1
+    _ a b (a ^ m) (b ^ m) ":" g G m i c C
+
+for i in (range 6)
+    print i ":" (edge2 i)
+
+print
+    edgegroup 0 1
+print
+    edgegroup 2 3
+print
+    edgegroup 0 2
+print
+    edgegroup 1 3
+print
+    edgegroup 0 3
+print
+    edgegroup 2 1
+
+#
+    0 1
+    0 2
+
+
+#
+    1 0 2 3
+    0 1 2 3
+    0 2 1 3
+    0 2 3 1
+    2 0 3 1
+    2 0 1 3
+    2 3 1 0
+    1 3 0 2
+    3 2 0 1
+
+
+0 1 : 0 1
+2 3 :     2 3
+0 2 : 0   2
+1 3 :   1   3
+0 3 : 0     3
+2 1 :   1 2
+
+
+
+;
  No newline at end of file