291368236a9d — Leonard Ritter 2 years ago
* resourcegroup
4 files changed, 162 insertions(+), 87 deletions(-)

M lib/tukan/ResourceGroup.sc
M lib/tukan/math.sc
M testing/test_subjective_mt.sc
M testing/test_tmt6.sc
M lib/tukan/ResourceGroup.sc +92 -4
@@ 1,7 1,31 @@ 
+
+using import struct
+using import glsl
 
 using import FunctionChain
 using import tukan.gl
 
+################################################################################
+
+let BINDING_BUF_DRAW_CMD = 32
+let BINDING_BUF_COMPUTE_CMD = 33
+
+struct DispatchIndirectCommandI plain
+    command : DispatchIndirectCommand
+
+struct DrawArraysIndirectCommandI plain
+    command : DrawArraysIndirectCommand
+
+buffer buf-compute-cmd : DispatchIndirectCommandI
+    binding = BINDING_BUF_COMPUTE_CMD
+    \ writeonly coherent restrict
+
+buffer buf-draw-cmd : DrawArraysIndirectCommandI
+    binding = BINDING_BUF_DRAW_CMD
+    \ writeonly coherent restrict
+
+################################################################################
+
 # prototype for initialization from usage
 
 type ResourceGroup

          
@@ 14,7 38,8 @@ type ResourceGroup
             type (do ...) < cls :: @u8
         else
             let self = (bitcast (malloc-array u8 cls.size) cls)
-            cls.setup self
+            static-if (not (none? cls.setup))
+                cls.setup self
             self
 
     spice static (self T ...)

          
@@ 65,9 90,10 @@ type ResourceGroup
         spice-quote
             ptrtoref (bitcast (getelementptr self offset) ptrT)
 
-    inline __drop (self)
+    fn __drop (self)
         let cls = (typeof self)
-        cls.teardown self
+        static-if (not (none? cls.teardown))
+            cls.teardown self
         free self
         ;
 

          
@@ 82,7 108,69 @@ type+ ResourceGroup
                         ...
 
     inline compute-program (self func ...)
-        program self (compute = 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

          
M lib/tukan/math.sc +9 -0
@@ 16,6 16,15 @@ case (u : vec4, v : vec4, x : vec4)
 
 unlet _invmix
 
+inline... expmix (a : f32, b : f32, x : f32, w = 1.0)
+    c := (b / a) ** w
+    q := ((c ** x) - 1.0) / (c - 1.0)
+    a * (1.0 - q) + b * q
+
+inline... invexpmix (a : f32, b : f32, x : f32, w = 1.0)
+    c := (b / a) ** w
+    (log2 ((x * (1.0 - c) + a * c - b) / (a - b))) / (log2 c)
+
 # from http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts
 fn orthogonal (v)
     """"vec3 <- vec3

          
M testing/test_subjective_mt.sc +28 -7
@@ 18,6 18,7 @@ using import glsl
 using import Array
 using import Box
 using import struct
+using import Option
 import ..lib.tukan.use
 
 import tukan.voxel

          
@@ 39,8 40,12 @@ using import tukan.projection
 using import tukan.derivative
 using import tukan.isosurface
 using import tukan.hash
+using import tukan.ResourceGroup
 using import .testfragment
 
+let RG = (ResourceGroup "RG")
+from (import tukan.math) let expmix
+
 # reserve 10MB for each voxel buffer
     at 4 bytes per voxel
 let MAX_VOXELS = ((10 * (1 << 20)) // 4)

          
@@ 267,12 272,9 @@ fn normalmapf (p r)
     - (sdNormalFast mapf p r)
 
 let ONION_NEAR = 0.6
-let ONION_FAR = 100.0
+let ONION_FAR = 20.0
 let ONION_LAYERS = 32.0
 
-fn expmix (a b x C)
-    (b * (C - 1.0) + (exp2 ((log2 (b * (1.0 - C) + a * C)) * (1.0 - x) + (log2 b) * x))) / C
-
 fn map_onion_radius (p)
     #r := (clamp ((p.z * 0.5 + 0.5) * 0.5 + 0.5) 0.0 1.0)
     r := (clamp (p.z * 0.5 + 0.5) 0.0 1.0)

          
@@ 280,6 282,10 @@ fn map_onion_radius (p)
     #r := (1 + 2 * (sqrt pi) / (ONION_LAYERS * 0.5)) ** r
     #r := (exp2 (mix (log2 ONION_NEAR) (log2 ONION_FAR) r))
     r := (expmix ONION_NEAR ONION_FAR r 1.0)
+
+    #r := r * 0.9999
+    #r := r / (1.0 * (1.0 - r))
+
     _ ((unpack_normal_snorm p.xy) * r) (r * 2.5)
 
 fn map_onion (p)

          
@@ 291,7 297,7 @@ fn map_identity_radius (p) (_ p 1.0)
 
 let map_vertex map_vertex_rlimit = map_onion map_onion_radius
 #let map_vertex map_vertex_rlimit = map_identity map_identity_radius
-PROJECT_FINAL_VERTEX := true
+PROJECT_FINAL_VERTEX := false
 VISUALIZE_IDS := false
 
 fn subdivide-cell (key)

          
@@ 505,6 511,8 @@ fn rasterize-vert ()
         k1 := (tetverts >> k) & 7:u32
         k3 := (tetverts >> (k + 3:u32)) & 7:u32
 
+        xycenter := (coord.xy + (vec2 (d * 0.5)))
+
         local p : (array vec3 4)
         p @ 0 = coord
         p @ 1 = coord + ((vec3 ((uvec3 k1 (k1 >> 1:u32) (k1 >> 2:u32)) & 1:u32)) * d)

          
@@ 518,6 526,10 @@ fn rasterize-vert ()
                 ? ((signs & 4:u32) == 4:u32) -1.0 1.0
                 ? ((signs & 8:u32) == 8:u32) -1.0 1.0
 
+        if ((xycenter.x * xycenter.y) >= 0.0)
+            # flip
+            return;
+
         let c i = (tetfaces d)
         let shift = ((((c - 1) << 2) | vertex-index) * 2)
         let i0 i1 =

          
@@ 792,7 804,12 @@ inline main ()
             compute = supershader
             #debug = true
 
-    inline per-frame-setup (size)
+    global rg : (Option RG)
+
+    fn per-frame-setup (size pg-test frame)
+        let rg =
+            'force-unwrap rg
+        from (methodsof rg) let static program compute-program
 
         GL.BindTextureUnit 0 fb-scene-color
         GL.Uniform smp-screen 0

          
@@ 906,12 923,16 @@ inline main ()
             GL.Disable GL.CULL_FACE
             GL.BindFramebuffer GL.FRAMEBUFFER 0
 
+    let per-frame-setup =
+        static-typify per-frame-setup ivec2 GL.Program i32
+    rg = (RG)
+
     _ per-frame-setup shader
 
 fn program ()
     render-fragment-shader main
         #debug = true
-        size = (ivec2 512)
+        size = (ivec2 1024)
 
 
 static-if true

          
M testing/test_tmt6.sc +33 -76
@@ 171,43 171,9 @@ uniform u-level : i32
 uniform smp-screen : sampler2D
     location = UNIFORM_SCREEN_SAMPLER
 
-struct DispatchIndirectCommandI plain
-    command : DispatchIndirectCommand
-
-struct DrawArraysIndirectCommandI plain
-    command : DrawArraysIndirectCommand
-
-buffer buf-compute-cmd : DispatchIndirectCommandI
-    binding = BINDING_BUF_COMPUTE_CMD
-    \ writeonly coherent restrict
-
-buffer buf-draw-cmd : DrawArraysIndirectCommandI
-    binding = BINDING_BUF_DRAW_CMD
-    \ writeonly coherent restrict
-
 uniform u-divisor : i32
     location = UNIFORM_DIVISOR
 
-fn setup-compute-command ()
-    local_size 1 1 1
-    let divisor = ((deref u-divisor) as u32)
-    buf-compute-cmd.command =
-        DispatchIndirectCommand
-            ((deref buf-cells-in-info.count) + divisor - 1:u32) // divisor
-            1
-            1
-    ;
-
-fn setup-draw-arrays-command ()
-    local_size 1 1 1
-    buf-draw-cmd.command =
-        DrawArraysIndirectCommand
-            deref buf-cells-in-info.count
-            1
-            0
-            0
-    ;
-
 fn simple-sphere (p)
     (length p) - 0.5
 

          
@@ 1436,6 1402,7 @@ inline main ()
         let rg =
             'force-unwrap rg
         from (methodsof rg) let static program compute-program
+            \ indirect-compute-setup indirect-draw-arrays-setup
 
         struct Cells
             cell_info : (array GL.Buffer 2)

          
@@ 1491,7 1458,15 @@ inline main ()
                 cell_info_buffers @ i
                 \ 0:i64 (i64 cell_info_buffer_sz)
 
-        let pg-setup-compute = (compute-program setup-compute-command)
+        vvv bind setup-compute exec-compute
+        indirect-compute-setup
+            inline ()
+                let divisor = ((deref u-divisor) as u32)
+                _
+                    ((deref buf-cells-in-info.count) + divisor - 1:u32) // divisor
+                    1
+                    1
+
         let pg-supershader = (compute-program supershader)
 
         local idx = 0

          
@@ 1518,13 1493,6 @@ inline main ()
             GL.DispatchCompute ((((1 << level) ** 3) // THREADSIZE) as u32) 1 1
             GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
 
-        let compute_cmd_buffers =
-            static GL.Buffer
-                inline ()
-                    let buf = (GL.Buffer)
-                    setup-ssbo buf buf-compute-cmd NUM_CMD_BUFFERS
-                    buf
-
         inline refine-voxels ()
             let i0 = (deref idx)
             idx = ((idx + 1) & 1)

          
@@ 1532,20 1500,16 @@ inline main ()
             level = level + 1
             let level = (deref level)
 
-            GL.UseProgram pg-setup-compute
-            GL.Uniform u-divisor THREADSIZE
-            bind-read-buffer i0
-            bind-ssbo compute_cmd_buffers buf-compute-cmd none 0
-            GL.DispatchCompute 1 1 1
-            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
-
+            vvv setup-compute
+            inline ()
+                GL.Uniform u-divisor THREADSIZE
+                bind-read-buffer i0
             GL.UseProgram pg-supershader
             GL.Uniform u-program ProgramVoxelize
             GL.Uniform u-level level
             bind-read-buffer i0
             bind-write-buffer i1
-            GL.DispatchComputeIndirect 0
-            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
+            exec-compute;
 
         let vertex_buffer_sz = ((sizeof CubeData) * MAX_VOXELS)
         let vertex_buffer =

          
@@ 1561,13 1525,10 @@ inline main ()
             let i1 = (deref idx)
             level = level + 1
             let level = (deref level)
-            GL.UseProgram pg-setup-compute
-            GL.Uniform u-divisor THREADSIZE
-            #GL.Uniform u-divisor THREADSIZE
-            bind-read-buffer i0
-            bind-ssbo compute_cmd_buffers buf-compute-cmd none 0
-            GL.DispatchCompute 1 1 1
-            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
+            vvv setup-compute
+            inline ()
+                GL.Uniform u-divisor THREADSIZE
+                bind-read-buffer i0
 
             @@ compute-program
             fn simplify ()

          
@@ 1583,8 1544,7 @@ inline main ()
             bind-write-buffer i1
             GL.BindBufferRange GL.SHADER_STORAGE_BUFFER BINDING_BUF_VERTEX_ATTR1_OUT
                     \ vertex_buffer 0:i64 (i64 vertex_buffer_sz)
-            GL.DispatchComputeIndirect 0
-            GL.MemoryBarrier GL.SHADER_STORAGE_BARRIER_BIT
+            exec-compute;
 
         struct FB
             color = (GL.Texture GL.TEXTURE_2D)

          
@@ 1610,21 1570,19 @@ inline main ()
 
         inline draw-surface ()
             #print-in-count (deref idx)
-            GL.MemoryBarrier (GL.ALL_BARRIER_BITS as u32)
-            let draw_cmd_buffers =
-                static GL.Buffer
-                    inline ()
-                        let buf = (GL.Buffer)
-                        setup-ssbo buf buf-draw-cmd NUM_CMD_BUFFERS
-                        buf
-            GL.BindBuffer GL.DRAW_INDIRECT_BUFFER draw_cmd_buffers
 
-            GL.UseProgram
-                compute-program setup-draw-arrays-command
-            bind-read-buffer (deref idx)
-            bind-ssbo draw_cmd_buffers buf-draw-cmd none 0
-            GL.DispatchCompute 1 1 1
-            GL.MemoryBarrier GL.COMMAND_BARRIER_BIT
+            vvv bind setup-draw-arrays exec-draw-arrays
+            indirect-draw-arrays-setup
+                inline ()
+                    _
+                        deref buf-cells-in-info.count
+                        1
+                        0
+                        0
+
+            vvv setup-draw-arrays
+            inline ()
+                bind-read-buffer (deref idx)
             do
                 GL.BindFramebuffer GL.FRAMEBUFFER fb-scene
                 GL.Viewport 0 0 (i32 size.x) (i32 size.y)

          
@@ 1653,7 1611,7 @@ inline main ()
                 GL.Uniform u-level (deref level)
                 GL.BindVertexArray
                     static GL.VertexArray
-                GL.DrawArraysIndirect GL.POINTS null
+                exec-draw-arrays GL.POINTS
 
                 GL.Disable GL.BLEND
                 GL.Disable GL.DEPTH_TEST

          
@@ 1663,7 1621,6 @@ inline main ()
         # set pg-test uniform
         GL.Uniform smp-screen 0
         GL.UseProgram pg-supershader
-        GL.BindBuffer GL.DISPATCH_INDIRECT_BUFFER compute_cmd_buffers
         init-voxels 5
         refine-voxels;
         #refine-voxels;