54c8417b746c — Leonard Ritter a month ago
* improvements to FIR
2 files changed, 196 insertions(+), 47 deletions(-)

M lib/tukan/FIR.sc
M testing/tukdag.sc
M lib/tukan/FIR.sc +86 -6
@@ 212,6 212,8 @@ define-type "clear" (RIFF "CLRI") (tuple
     stringcolor...
 define-type "sample" (RIFF "SAMP") (tuple (source = AnyId) (uv = AnyId))
     stringcolor...
+define-type "fetch" (RIFF "FTCH") (tuple (source = AnyId) (coord = AnyId))
+    stringcolor...
 
 # FIR Level 1
 ################################################################################

          
@@ 234,6 236,7 @@ define-type "computefn"     (RIFF "CMFN"
 define-type "rbind"         (RIFF "RBND") (tuple (source = AnyId) (target = AnyId))
 define-type "wbind"         (RIFF "WBND") (tuple (source = AnyId) (target = AnyId))
 define-type "undef"         (RIFF "UNDF") (tuple (type = AnyId))
+    dedup = false
     instrcolor...
 define-type "dispatch"      (RIFF "DISP") (tuple (x = AnyId) (y = AnyId) (z = AnyId) (func = AnyId) (bindings = (array AnyId)))
     mutinstrcolor...

          
@@ 289,6 292,8 @@ define-type "draw"          (RIFF "DRAI"
     mutinstrcolor...
 define-type "sampleimagelod" (RIFF "SILD") (tuple (source = AnyId) (uv = AnyId) (lod = AnyId))
     instrcolor...
+define-type "fetchimagelod" (RIFF "FILD") (tuple (source = AnyId) (coord = AnyId) (lod = AnyId))
+    instrcolor...
 
 # FIR Level 0
 ################################################################################

          
@@ 330,6 335,8 @@ define-type "uconst"    (RIFF "U32C") (t
 define-type "fconst"    (RIFF "F32C") (tuple (value = f32))
     constcolor...
 define-type "comp"      (RIFF "COMP") (tuple (index = u32) (value = AnyId))
+define-type "select"    (RIFF "SELC") (tuple (c = AnyId) (t = AnyId) (f = AnyId))
+
 let Operator1Type = (tuple (value = AnyId))
 let Operator2Type = (tuple (lhs = AnyId) (rhs = AnyId))
 inline define-op1 (name riff)

          
@@ 620,6 627,10 @@ struct FIRTyper
             execmodel
                 typevectype module (get self.source)
                 typeexecmodel module (get self.uv)
+        case fetch (self)
+            execmodel
+                typevectype module (get self.source)
+                typeexecmodel module (get self.coord)
         case sampleimagelod (self)
             execmodel
                 dispatch ('handleof module (typevectype module (get self.source)))

          
@@ 628,6 639,14 @@ struct FIRTyper
                 default
                     error "can't deduce element type"
                 typeexecmodel module (get self.uv)
+        case fetchimagelod (self)
+            execmodel
+                dispatch ('handleof module (typevectype module (get self.source)))
+                case vectype (self)
+                    vectype self.element 4
+                default
+                    error "can't deduce element type"
+                typeexecmodel module (get self.coord)
         case clearimage (self)
             get self.target
         case dispatch (self)

          
@@ 650,6 669,26 @@ struct FIRTyper
                 for i entry in (enumerate wbinds u32)
                     args @ i = (get (entry @ 0))
                 'commit module
+        case draw (self)
+            let vacount = ('vacount handle)
+            let bindings = self.bindings
+            local wbinds : (Array (tuple AnyId AnyId))
+            for i in (range vacount)
+                let bhandle = ('handleof module (bindings @ i))
+                dispatch bhandle
+                case wbind (self)
+                    'append wbinds (tupleof self.source self.target)
+                default;
+            let wcount = ((countof wbinds) as u32)
+            if (wcount == 1)
+                get (wbinds @ 0 @ 0)
+            else
+                let mrv =
+                    'alloc module TypeId.typeid_vatype wcount
+                let args = mrv.types
+                for i entry in (enumerate wbinds u32)
+                    args @ i = (get (entry @ 0))
+                'commit module
         case comp (self)
             let tid = (get self.value)
             if (tid == NoId)

          
@@ 721,12 760,16 @@ struct FIRTyper
 
             # n-ary operations
             pass TypeId.typeid_utof
+            pass TypeId.typeid_ftou
             pass TypeId.typeid_fadd
             pass TypeId.typeid_fsub
             pass TypeId.typeid_fmul
             pass TypeId.typeid_fdiv
+            pass TypeId.typeid_fueq
+            pass TypeId.typeid_foeq
             pass TypeId.typeid_sin
             pass TypeId.typeid_cos
+            pass TypeId.typeid_sqrt
             pass TypeId.typeid_add
             pass TypeId.typeid_sub
             pass TypeId.typeid_mul

          
@@ 746,9 789,8 @@ struct FIRTyper
             pass TypeId.typeid_fvec2
             pass TypeId.typeid_fvec3
             pass TypeId.typeid_fvec4
+            pass TypeId.typeid_select
             do
-                let count = handle.size
-                let data = handle.data
                 let dcount =
                     switch typeid
                     pass TypeId.typeid_uvec2

          
@@ 768,12 810,16 @@ struct FIRTyper
                     pass TypeId.typeid_fsub
                     pass TypeId.typeid_fmul
                     pass TypeId.typeid_fdiv
+                    pass TypeId.typeid_fueq
+                    pass TypeId.typeid_foeq
                     pass TypeId.typeid_sin
                     pass TypeId.typeid_cos
+                    pass TypeId.typeid_sqrt
                     pass TypeId.typeid_fvec2
                     pass TypeId.typeid_fvec3
                     pass TypeId.typeid_fvec4
                     do ElementType.F
+                    pass TypeId.typeid_ftou
                     pass TypeId.typeid_add
                     pass TypeId.typeid_sub
                     pass TypeId.typeid_mul

          
@@ 791,18 837,31 @@ struct FIRTyper
                     pass TypeId.typeid_uvec3
                     pass TypeId.typeid_uvec4
                     do ElementType.U
+                    case TypeId.typeid_select
+                        dispatch handle
+                        case select (self)
+                            let vt = (typevectype module (get self.t))
+                            dispatch ('handleof module vt)
+                            case vectype (self)
+                                copy self.element
+                            default
+                                ElementType.F # take any
+                        default
+                            trap;
                     default
                         error
                             .. "unhandled typeid " (repr typeid)
                 # try to find first exec model
                 let em =
-                    fold (em = ExecModel.All) for i in (range count)
-                        let nextem =
-                            typeexecmodel module (get (bitcast (data @ i) AnyId))
+                    fold (em = ExecModel.All) for srcid in ('sources handle)
+                        let nextem = (typeexecmodel module (copy srcid))
                         em | nextem
                 execmodel (vectype vte dcount) em
             pass TypeId.typeid_outputs
             pass TypeId.typeid_output
+            pass TypeId.typeid_position
+            pass TypeId.typeid_store
+            pass TypeId.typeid_block
             pass TypeId.typeid_imagewrite
             pass TypeId.typeid_computefn
             pass TypeId.typeid_rbind

          
@@ 816,12 875,14 @@ struct FIRTyper
             pass TypeId.typeid_vectype
             pass TypeId.typeid_texturetype
             pass TypeId.typeid_execmodel
+            pass TypeId.typeid_shaderfn
             do NoId
 
             # first value is type
             pass TypeId.typeid_undef
             pass TypeId.typeid_wimage
             pass TypeId.typeid_rattr
+            pass TypeId.typeid_wattr
             pass TypeId.typeid_uniform
             do
                 for srcid in ('sources handle)

          
@@ 1454,7 1515,7 @@ fn generate-IL (module rootid)
         inline vecop2 (self op)
             `(op [(get self.lhs)] [(get self.rhs)])
         inline veccmpop2 (self op)
-            `(op [(get self.lhs)] [(get self.rhs)])
+            `(? (op [(get self.lhs)] [(get self.rhs)]) 1:u32 0:u32)
         let handle = ('handleof module id)
         let vacount = ('vacount handle)
         vvv bind value

          
@@ 1527,6 1588,9 @@ fn generate-IL (module rootid)
         case fbtoib (self) (castvecop1 self bitcast u32)
         case ibtofb (self) (castvecop1 self bitcast f32)
 
+        case select (self)
+            `(? (icmp!= [(get self.c)] 0:u32) [(get self.t)] [(get self.f)])
+
         case comp (self)
             `([(get self.value)] @ [self.index])
         case fvec2 (self)

          
@@ 1690,6 1754,11 @@ fn generate-IL (module rootid)
             spice-quote
                 sample [(get self.source)] [(get self.uv)]
                     Lod = [(get self.lod)]
+        case fetchimagelod (self)
+            spice-quote
+                sample [(get self.source)] [(get self.coord)]
+                    Fetch = true
+                    Lod = [(get self.lod)]
         #case Op.WRITE
             # IMAGEWRITE attr index value : imagetype
             let a1 a2 a3 = (translate a1 a2 a3)

          
@@ 1815,6 1884,9 @@ fn generate-IL (module rootid)
                     DispatchComputeIndirect 0
 
             let body = (sc_expression_new)
+            from GL let MemoryBarrier FRAMEBUFFER_BARRIER_BIT
+            sc_expression_append body `(MemoryBarrier FRAMEBUFFER_BARRIER_BIT)
+
             sc_expression_append body `(UseProgram pg)
 
             local retargs : (Array Value)

          
@@ 2441,6 2513,14 @@ fn lower-FIR (module rootid)
             let numcomp = ('components imgformat)
             return
                 remapvector ctx module newid numcomp
+        case fetch (self)
+            from (methodsof module.builder) let uconst fetchimagelod
+            let imgtype = ('imagetypeof ctx.typer module self.source)
+            let imgformat = imgtype.format
+            let newid = (fetchimagelod self.source self.coord (uconst 0))
+            let numcomp = ('components imgformat)
+            return
+                remapvector ctx module newid numcomp
         #case overlay (self)
             let srcrangeid = ('rangeof ctx.typer module self.rangevalue)
             let rangevalue imgformat =

          
M testing/tukdag.sc +110 -41
@@ 147,10 147,11 @@ inline gen-level1-test ()
 
 inline gen-level2-test-geometry ()
     from (methodsof module.builder) let uvec fvec2 fvec3 fvec4 input output uconst
-        \ fconst comp and xor utof sin cos fadd fmul fdiv sample fsub ushr
+        \ fconst comp and or xor utof sin cos fadd fmul fdiv sample fsub ushr
         \ outputs sub unpack-comp udiv urem primitive fragment selectfragment
         \ clear depthtest overlay rasterize uvec2 vertexid instanceid vargs va
-        \ fragparams uvec4 tuple arrayof arrayat compute globalid
+        \ fragparams uvec4 tuple arrayof arrayat compute globalid fetch add
+        \ foeq select mul ftou sqrt
 
     let sw sh =
         unpack-comp (input SystemKey.ScreenSize) 2

          
@@ 162,7 163,20 @@ inline gen-level2-test-geometry ()
             do
                 let x y =
                     unpack-comp (globalid) 2
-                utof (xor (and x (uconst 1)) (and y (uconst 1)))
+                let phase = (fmul (utof inpit) (fconst 0.1))
+                let q = (utof (and (xor x y) (uconst 1)))
+                let invq = (fsub (fconst 1) q)
+                let u v =
+                    fsub (fmul (fdiv (utof x) (utof TS)) (fconst 2)) (fconst 1)
+                    fsub (fmul (fdiv (utof y) (utof TS)) (fconst 2)) (fconst 1)
+                let d =
+                    fmul (sqrt (fadd (fmul u u) (fmul v v))) (fconst 6)
+                let mixf =
+                    fadd
+                        fmul (sin (fsub d phase))
+                            fconst 0.5
+                        fconst 0.5
+                fadd (fmul q mixf) (fmul invq (fsub (fconst 1) mixf))
 
     inline rotate (x y r)
         let c = (cos r)

          
@@ 174,50 188,105 @@ inline gen-level2-test-geometry ()
         arrayof (uvec 1)
             uconst 0; uconst 2; uconst 4; uconst 6; uconst 7; uconst 2; uconst 3
             uconst 1; uconst 7; uconst 5; uconst 4; uconst 1; uconst 0; uconst 2
-    outputs
-        output SystemKey.Screen
-            rasterize
-                (uconst 14)
-                do
-                    let vx = (arrayat (vertexid) indices)
+
+    inline fop (n vec f)
+        inline (a b)
+            let a1 a2 a3 = (unpack-comp a n)
+            let b1 b2 b3 = (unpack-comp b n)
+            vec
+                f a1 b1
+                f a2 b2
+                f a3 b3
+    let fadd3 = (fop 3 fvec3 fadd)
+    let fdiv3 = (fop 3 fvec3 fdiv)
+
+    let cube =
+        rasterize
+            (uconst 14)
+            do
+                let vx = (arrayat (vertexid) indices)
 
-                    let u = (utof (and vx (uconst 1)))
-                    let v = (utof (and (ushr vx (uconst 1)) (uconst 1)))
-                    let w = (utof (and (ushr vx (uconst 2)) (uconst 1)))
+                let bu = (and vx (uconst 1))
+                let bv = (and (ushr vx (uconst 1)) (uconst 1))
+                let bw = (and (ushr vx (uconst 2)) (uconst 1))
+
+                let u = (utof bu)
+                let v = (utof bv)
+                let w = (utof bw)
+
+                let x = (fsub u (fconst 0.5))
+                let y = (fsub v (fconst 0.5))
+                let z = (fsub w (fconst 0.5))
 
-                    let x = (fsub u (fconst 0.5))
-                    let y = (fsub v (fconst 0.5))
-                    let z = (fsub w (fconst 0.5))
+                let it = (fdiv (utof inpit) (fconst 60.0))
+                let x y = (rotate x y it)
+                let x z = (rotate x z (fmul it (fconst 0.318)))
 
-                    let it = (fdiv (utof inpit) (fconst 60.0))
-                    let x y = (rotate x y it)
-                    let x z = (rotate x z (fmul it (fconst 0.318)))
+                let z = (fadd z (fconst 2.0))
+                let aspect = (fdiv (utof sh) (utof sw))
+                let x = (fmul x aspect)
+
+                let sc = (fconst 2)
+                let x = (fmul x sc)
+                let y = (fmul y sc)
 
-                    let z = (fadd z (fconst 2.0))
-                    let aspect = (fdiv (utof sh) (utof sw))
-                    let x = (fmul x aspect)
-
-                    let u2 = (utof (and (vertexid) (uconst 1)))
-                    let v2 = (utof (and (ushr (vertexid) (uconst 1)) (uconst 1)))
+                vargs
+                    fvec4 x y (fconst 1) z
+                    fvec3 u v w
+            PrimitiveMode.TriangleStrip
+            CullMode.Back
+            do
+                let r g b = (unpack-comp (va 1 (fragparams)) 3)
+                let x? = (or (foeq r (fconst 1)) (foeq r (fconst 0)))
+                let y? = (or (foeq g (fconst 1)) (foeq g (fconst 0)))
+                let z? = (or (foeq b (fconst 1)) (foeq b (fconst 0)))
+                let u =
+                    select z? r
+                        select x? g b
+                let v =
+                    select z? g
+                        select x? b r
+                inline softcube (u v)
+                    sample cube_texture (fvec2 u v)
+                inline hardcube (u v)
+                    let u =
+                        ftou (fmul u (utof TS))
+                    let v =
+                        ftou (fmul v (utof TS))
+                    fetch cube_texture (uvec2 u v)
+                let tex =
+                    hardcube u v
+                    #softcube u v
+                inline tform (x)
+                    fmul (fadd (fmul x (fconst 0.5)) (fconst 0.5)) tex
+                let r = (tform r)
+                let g = (tform g)
+                let b = (tform b)
+                fvec3 r g b
+            uvec4 (uconst 0) (uconst 0) sw sh
+            clear (input SystemKey.ScreenSize)
+                fvec3 (fconst 0.1) (fconst 0.1) (fconst 0.12)
 
-                    vargs
-                        fvec4 x y (fconst 1) z
-                        fvec3 u v w
-                        fvec2 u2 v2
-                PrimitiveMode.TriangleStrip
-                CullMode.Back
+    outputs
+        output SystemKey.Screen cube
+            #compute (input SystemKey.ScreenSize)
                 do
-                    let r g b = (unpack-comp (va 1 (fragparams)) 3)
-                    let u v = (unpack-comp (va 2 (fragparams)) 2)
-                    let tex =
-                        sample cube_texture (fvec2 u v)
-                    let r = (fmul r tex)
-                    let g = (fmul g tex)
-                    let b = (fmul b tex)
-                    fvec3 r g b
-                uvec4 (uconst 0) (uconst 0) sw sh
-                clear (input SystemKey.ScreenSize)
-                    fvec3 (fconst 0) (fconst 0) (fconst 1)
+                    let x y =
+                        unpack-comp (globalid) 2
+                    let D = (uconst 10)
+                    let col0 = (fetch cube (uvec2 (sub x D) (add y (uconst 0))))
+                    let col1 = (fetch cube (uvec2 (add x D) (add y (uconst 0))))
+                    let col2 = (fetch cube (uvec2 (add x (uconst 0)) (sub y D)))
+                    let col3 = (fetch cube (uvec2 (add x (uconst 0)) (add y D)))
+                    let col4 = (fetch cube (uvec2 (add x (uconst 0)) (add y (uconst 0))))
+                    let F5 = (fconst 5.0)
+                    fdiv3
+                        fadd3 col4
+                            fadd3
+                                fadd3 col0 col1
+                                fadd3 col2 col3
+                        fvec3 F5 F5 F5
+                    #fvec3 x y (fconst 0)
 
 inline gen-level2-test ()
     from (methodsof module.builder) let uvec fvec2 fvec3 fvec4 input output uconst