13b70c9483b6 — Leonard Ritter a month ago
* changed `uvec` and `fvec` types to `vectype`
2 files changed, 168 insertions(+), 86 deletions(-)

M lib/tukan/FIR.sc
M testing/tukdag.sc
M lib/tukan/FIR.sc +104 -79
@@ 122,6 122,12 @@ enum ImageFormat : u32
                     va-range 3 6
             va-range (va-countof comp-sym...)
 
+    fn setformat (self fmt)
+        let mask = (7:u32 << 4:u32)
+        let bits =
+            (fmt & 7:u32) << 4:u32
+        bitcast ((self & (~ mask)) | bits) this-type
+
     fn setcomponents (self count)
         let bits =
             switch count

          
@@ 171,13 177,13 @@ enum PrimitiveType : u32
     Triangle
     TriangleStrip
     TriangleFan
-define-type "primitive" (RIFF "PRIM") (tuple (mode = PrimitiveType) (vertexcount = AnyId) (instancecount = AnyId))
+define-type "primitive" (RIFF "PRIM") (tuple (mode = PrimitiveType) (vertex = AnyId))
     stringcolor...
 enum FragmentType : u32
     Undefined
     Flat
     Smooth
-define-type "fragment"    (RIFF "FRAG") (tuple (kind = FragmentType) (value = AnyId))
+define-type "fragment"    (RIFF "FRAG") (tuple (primitive = AnyId) (attr = AnyId) (kind = FragmentType))
     stringcolor...
 enum DepthTestType : u32
     False

          
@@ 190,9 196,7 @@ enum DepthTestType : u32
     LessEqual
 define-type "depthtest" (RIFF "ZTST") (tuple (depthfunc = DepthTestType) (value = AnyId) (default = AnyId))
     stringcolor...
-define-type "overlay"  (RIFF "NOZT") (tuple (value = AnyId))
-    stringcolor...
-define-type "selectfragment" (RIFF "SLFR") (tuple (test = AnyId) (value = AnyId) (default = AnyId))
+define-type "overlay"  (RIFF "NOZT") (tuple (range = AnyId) (fragment = AnyId))
     stringcolor...
 define-type "clear" (RIFF "CLRI") (tuple (range = AnyId) (value = AnyId))
     stringcolor...

          
@@ 209,6 213,7 @@ enum ImageFlags : u32
     multisampled = 2:u32
     arrayed-multisampled = 3:u32
 struct ImageType plain
+    type : AnyId # vector type
     dim : ImageDim
     format : ImageFormat
     flags : ImageFlags

          
@@ 296,14 301,29 @@ define-type "sampleimagelod" (RIFF "SILD
 # FIR Level 0
 ################################################################################
 
-define-type "fvec"      (RIFF "FVEC") (tuple (count = u32))
-    typecolor...
-define-type "uvec"      (RIFF "UVEC") (tuple (count = u32))
+enum ElementType : u32
+    U = ImageFormat.U
+    F = ImageFormat.F
+struct VectorType plain
+    element : ElementType
+    count : u32
+
+    @@ memo
+    inline __as (cls T)
+        static-if (T == ImageFormat)
+            inline (self)
+                'setcomponents
+                    'setformat ImageFormat.R32U (self.element as u32)
+                    self.count
+
+define-type "vectype"   (RIFF "VECT") VectorType
     typecolor...
 define-type "mrv"       (RIFF "VART") (tuple (types = (array AnyId)))
     typecolor...
 define-type "args"      (RIFF "ARGS") (tuple (args = (array AnyId)))
     typecolor...
+define-type "va"        (RIFF "VA  ") (tuple (index = u32) (args = AnyId))
+    typecolor...
 define-type "fvec2"     (RIFF "FVC2") (tuple (x = AnyId) (y = AnyId))
     instrcolor...
 define-type "fvec3"     (RIFF "FVC3") (tuple (x = AnyId) (y = AnyId) (z = AnyId))

          
@@ 405,6 425,18 @@ define-op1 "cos"    "FCOS"
 ################################################################################
 
 #
+    types:
+
+    vector types:
+        vectype U <1-4>
+        vectype F <1-4>
+    ranged type: (in shaders only)
+        rangetype (vector-type) (range)
+    imagetype
+    texturetype
+    primitivetype
+    fragmenttype
+
     expressions can be of these contexts:
 
     * constant

          
@@ 570,10 602,9 @@ struct FIRTyper
                 generator = id
                 stage-import = (copy ctx.rootstage.stage-import)
         case primitive (self)
-            let vertexcount = (get self.vertexcount)
-            let instancecount = (get self.instancecount)
+            let vertex = (get self.vertex)
             StageInfo
-                sample-index = (max vertexcount.sample-index instancecount.sample-index)
+                sample-index = vertex.sample-index
                 generator = id
                 stage-import = (copy ctx.rootstage.stage-import)
         case sampleimagelod (self)

          
@@ 704,7 735,7 @@ struct FIRTyper
                     'repr module tid
 
     fn type-value (ctx module id)
-        from (methodsof module.builder) let uvec fvec
+        from (methodsof module.builder) let uvec fvec vectype
         #report "typing" ('repr module id)
         inline get (id)
             try (copy ('get ctx.types id))

          
@@ 727,15 758,14 @@ struct FIRTyper
         case sample (self)
             get self.source
         case sampleimagelod (self)
-            let typeid count = ('vectordesc ctx module (get self.source))
-            switch typeid
-            case TypeId.typeid_fvec (fvec 4)
-            case TypeId.typeid_uvec (uvec 4)
+            dispatch ('handleof module ('typevectype ctx module (get self.source)))
+            case vectype (self)
+                vectype self.element 4
             default
                 error "can't deduce element type"
         case clearimage (self)
             get self.target
-        case selectfragment (self)
+        #case selectfragment (self)
             let typeid count = ('vectordesc ctx module (get self.default))
             switch typeid
             case TypeId.typeid_fvec (fvec count)

          
@@ 797,7 827,6 @@ struct FIRTyper
             pass TypeId.typeid_computefn
             pass TypeId.typeid_rbind
             pass TypeId.typeid_wbind
-            pass TypeId.typeid_overlay
             do NoId
 
             case TypeId.typeid_uvec2 (uvec 2)

          
@@ 811,8 840,7 @@ struct FIRTyper
             pass TypeId.typeid_image
             pass TypeId.typeid_sampler
             pass TypeId.typeid_mrv
-            pass TypeId.typeid_fvec
-            pass TypeId.typeid_uvec
+            pass TypeId.typeid_vectype
             pass TypeId.typeid_imagestorage
             do NoId
 

          
@@ 830,6 858,7 @@ struct FIRTyper
             # type of first value
             pass TypeId.typeid_fragment
             pass TypeId.typeid_comp
+            pass TypeId.typeid_overlay
             do
                 for srcid in ('sources handle)
                     if true

          
@@ 841,40 870,23 @@ struct FIRTyper
         'set ctx.types id type
         ;
 
-    fn vectordesc-imagetype (imagetype)
-        let format = imagetype.format
-        let count = ('components format)
-        let typeid =
-            switch ('format format)
-            pass ImageFormat.U
-            pass ImageFormat.S
-            do TypeId.typeid_uvec
-            pass ImageFormat.F
-            pass ImageFormat.UNORM
-            pass ImageFormat.SNORM
-            do TypeId.typeid_fvec
+    fn typevectype (ctx module tid)
+        loop (tid)
+            let handle = ('handleof module tid)
+            if (handle.typeid == TypeId.typeid_vectype)
+                return tid
+            dispatch handle
+            case imagestorage (self)
+                repeat (copy self.type)
+            case image (self)
+                repeat (copy self.type)
+            case sampler (self)
+                repeat (copy self.type)
             default
-                trap;
-        return typeid count
+                return NoId
 
-    fn vectordesc (ctx module tid)
-        let handle = ('handleof module tid)
-        dispatch handle
-        case fvec (self)
-            return TypeId.typeid_fvec (copy self.count)
-        case uvec (self)
-            return TypeId.typeid_uvec (copy self.count)
-        case imagestorage (self)
-            return (this-function ctx module (copy self.type))
-        case image (self)
-            return (vectordesc-imagetype self)
-        case sampler (self)
-            return (vectordesc-imagetype self)
-        default
-            return handle.typeid 0:u32
-
-    fn element-typeof (ctx module source)
-        return (vectordesc ctx module ('typeof ctx module source))
+    fn vectypeof (ctx module source)
+        return (typevectype ctx module ('typeof ctx module source))
 
     fn... typeof (ctx, module : FIR, id : AnyId)
         try

          
@@ 1568,22 1580,28 @@ fn generate-IL (module rootid)
             `(uvec3 [(get self.x)] [(get self.y)] [(get self.z)])
         case uvec4 (self)
             `(uvec4 [(get self.x)] [(get self.y)] [(get self.z)] [(get self.w)])
-        case fvec (self)
-            switch self.count
-            case 1 `f32
-            case 2 `vec2
-            case 3 `vec3
-            case 4 `vec4
-            default
+        case vectype (self)
+            if ((self.count > 4) or (self.count < 1))
                 error "unsupported vector size"
-        case uvec (self)
-            switch self.count
-            case 1 `u32
-            case 2 `uvec2
-            case 3 `uvec3
-            case 4 `uvec4
+            switch self.element
+            case ElementType.U
+                switch self.count
+                case 1 `u32
+                case 2 `uvec2
+                case 3 `uvec3
+                case 4 `uvec4
+                default
+                    trap;
+            case ElementType.F
+                switch self.count
+                case 1 `f32
+                case 2 `vec2
+                case 3 `vec3
+                case 4 `vec4
+                default
+                    trap;
             default
-                error "unsupported vector size"
+                trap;
         case globalid (self)
             `(deref gl_GlobalInvocationID)
         case vertexid (self)

          
@@ 1940,10 1958,7 @@ fn fold-constant-expression (typer self 
             let val = (copy self.value)
             let tid = ('typeof typer module val)
             dispatch ('handleof module tid)
-            case fvec (self)
-                if (self.count == 1)
-                    return val
-            case uvec (self)
+            case vectype (self)
                 if (self.count == 1)
                     return val
             default;

          
@@ 2139,7 2154,14 @@ fn lower-FIR (module rootid)
             error "range expected"
 
     fn remapvector (ctx module source numcomp)
-        let typeid sourcecomp = ('element-typeof ctx.typer module source)
+        let vect = ('vectypeof ctx.typer module source)
+        let vect =
+            dispatch ('handleof module vect)
+            case vectype (self) (copy self)
+            default
+                trap;
+        let ET = vect.element
+        let sourcecomp = vect.count
         #report "remapping" ('repr module source) "from" sourcecomp "to" numcomp
         from (methodsof module.builder) let comp
         inline getter (const)

          
@@ 2150,8 2172,8 @@ fn lower-FIR (module rootid)
         if (sourcecomp == numcomp)
             source
         else
-            switch typeid
-            case TypeId.typeid_fvec
+            switch ET
+            case ElementType.F
                 from (methodsof module.builder) let fvec2 fvec3 fvec4 fconst comp
                 let get = (getter fconst)
                 switch numcomp

          
@@ 2161,7 2183,7 @@ fn lower-FIR (module rootid)
                 case 4 (fvec4 (get 0) (get 1) (get 2) (get 3))
                 default
                     trap;
-            case TypeId.typeid_uvec
+            case ElementType.U
                 from (methodsof module.builder) let uvec2 uvec3 uvec4 uconst comp
                 let get = (getter uconst)
                 switch numcomp

          
@@ 2180,16 2202,14 @@ fn lower-FIR (module rootid)
         let tid = ('typeof ctx.typer module source)
         let imgformat =
             dispatch ('handleof module tid)
-            case fvec (self)
-                'setcomponents ImageFormat.R32F self.count
-            case uvec (self)
-                'setcomponents ImageFormat.R32U self.count
+            case vectype (self)
+                self as ImageFormat
             default
                 error "source must be float or integer"
         from (methodsof module.builder) let image imagestorage
         let met = (range-metrics module rangeid)
         let cx cy cz = (unpack met.capacity)
-        let imgtype = (image ImageDim.2D imgformat ImageFlags.none)
+        let imgtype = (image tid ImageDim.2D imgformat ImageFlags.none)
         _ (imagestorage imgtype cx cy cz 1 0) imgtype imgformat met
 
     fn lower-expression (ctx module source)

          
@@ 2346,6 2366,11 @@ type+ FIR
         default false
 
 type+ FIR.BuilderType
+    inline uvec (self count)
+        'vectype self ElementType.U count
+    inline fvec (self count)
+        'vectype self ElementType.F count
+
     inline unpack-comp (self value n)
         va-map
             inline (i)

          
M testing/tukdag.sc +64 -7
@@ 31,7 31,7 @@ inline gen-level1-test-geometry ()
     let ssx = (uniform (fvec 1) 0)
     let ssy = (uniform (fvec 1) 1)
     let u_it = (uniform (fvec 1) 2)
-    let imgtype = (image ImageDim.2D ImageFormat.RGBA8UNORM ImageFlags.none)
+    let imgtype = (image (fvec 4) ImageDim.2D ImageFormat.RGBA8UNORM ImageFlags.none)
     let img = (wimage imgtype 0)
     let z = (fadd (fmul (sin (fdiv (utof inpit) (fconst 60.0))) (fconst 0.5)) (fconst 0.5))
     let angle = (fdiv (utof inpit) (fconst 60.0))

          
@@ 105,7 105,7 @@ inline gen-level1-test ()
     let ssx = (uniform (fvec 1) 0)
     let ssy = (uniform (fvec 1) 1)
     let u_it = (uniform (fvec 1) 2)
-    let imgtype = (image ImageDim.2D ImageFormat.RGBA8UNORM ImageFlags.none)
+    let imgtype = (image (fvec 4) ImageDim.2D ImageFormat.RGBA8UNORM ImageFlags.none)
     let img = (wimage imgtype 0)
     let z = (fadd (fmul (sin (fdiv (utof inpit) (fconst 60.0))) (fconst 0.5)) (fconst 0.5))
     let angle = (fdiv (utof inpit) (fconst 60.0))

          
@@ 145,7 145,7 @@ inline gen-level1-test ()
                         imagestorage imgtype 4096 4096 1 1 0
                     img
 
-inline gen-level2-test ()
+inline gen-level2-test-geometry ()
     from (methodsof module.builder) let uvec fvec2 fvec3 fvec4 input output uconst
         \ fconst range comp and xor utof sin cos fadd fmul fdiv sample fsub
         \ outputs sub unpack-comp udiv urem primitive fragment selectfragment

          
@@ 199,6 199,56 @@ inline gen-level2-test ()
                     clear screenrange
                         fvec3 (uconst 0) (uconst 0) (uconst 1)
 
+    do
+        let vxgen = (range (uconst 4))
+        let vertexid = (comp 0 vxgen)
+        let u = (utof (urem vertexid (uconst 2)))
+        let v = (utof (udiv vertexid (uconst 2)))
+
+        let quad =
+            do
+                let x = (fsub u (fconst 0.5))
+                let y = (fsub v (fconst 0.5))
+                fvec4 x y (fconst 0) (fconst 1)
+        let quadcolor = (fvec3 u v (fconst 0))
+
+        let prim = (primitive PrimitiveType.TriangleStrip quad)
+
+        outputs
+            output SystemKey.Screen
+                overlay
+                    clear screenrange
+                        fvec3 (uconst 0) (uconst 0) (uconst 1)
+                    fragment prim quadcolor FragmentType.Smooth
+
+inline gen-level2-test ()
+    from (methodsof module.builder) let uvec fvec2 fvec3 fvec4 input output uconst
+        \ fconst range comp and xor utof sin cos fadd fmul fdiv sample fsub
+        \ outputs sub unpack-comp udiv urem primitive fragment selectfragment
+        \ clear depthtest overlay
+
+    let inpit = (input SystemKey.Iteration)
+
+    let TS = (uconst 32)
+    let checkers_texture1 =
+        do
+            let pos = (range TS TS)
+            let x y =
+                unpack-comp pos 2
+            utof (xor (and x (uconst 1)) (and y (uconst 1)))
+
+    let TS = (uconst 16)
+    let checkers_texture2 =
+        do
+            let pos = (range TS TS)
+            let x y =
+                unpack-comp pos 2
+            utof (xor (and x (uconst 1)) (and y (uconst 1)))
+
+    let w h =
+        unpack-comp (input SystemKey.ScreenSize) 2
+    let screenrange = (range w h)
+
     outputs
         output SystemKey.Screen
             do

          
@@ 225,7 275,7 @@ inline cleanup (rootid)
         local newmodule : FIR
         let newid = ('translate newmodule module rootid)
         module = newmodule
-        rootid
+        newid
 inline run (rootid)
     do
         let fsetup fdrive fdrop = ('gen-templates module rootid)

          
@@ 238,11 288,18 @@ inline graphviz ()
             module-dir .. "/tukdag"
 
 #let prog = (gen-level1-test-geometry)
-#let prog = (gen-level1-test)
-let prog =
+let prog = (gen-level1-test)
+#let prog =
+    do
+        let prog = (gen-level2-test-geometry)
+        let prog = (cleanup prog)
+        'dump module prog
+        print "lowering..."
+        'lower module prog
+#let prog =
     do
         let prog = (gen-level2-test)
-        cleanup prog
+        let prog = (cleanup prog)
         'dump module prog
         print "lowering..."
         'lower module prog