spirv: shade.lisp

the abstract programmable pipeline (src/core/shade.lisp) spelled in SPIR-V, at run time.

Other definitions

(defmacro sps-consts) source
(defmacro sps-consts () 128)
(defmacro sps-images) source
(defmacro sps-images () 16)
(defstruct spv-shader) source
(defstruct (spv-shader (:conc-name sps-))
  (m (make-spv-module) :type spv-module)
  (iface (make-shade-iface) :type shade-iface)
  (stage 0 :type u8)
  (main 0 :type u32)
  (glsl-id 0 :type u32)
  ;; types
  (t-void 0 :type u32) (t-fn 0 :type u32) (t-bool 0 :type u32) (t-f32 0 :type u32) (t-i32 0 :type u32)
  (t-u32 0 :type u32) (t-v2f32 0 :type u32) (t-v3f32 0 :type u32) (t-v4f32 0 :type u32)
  (t-v4bool 0 :type u32) (t-v4i32 0 :type u32) (t-v2u32 0 :type u32) (t-v2i32 0 :type u32) (t-v3u32 0 :type u32)
  (p-priv-v4f32 0 :type u32) (p-priv-v4i32 0 :type u32) (p-priv-i32 0 :type u32) (p-priv-bool 0 :type u32)
  (p-in-v4f32 0 :type u32) (p-out-v4f32 0 :type u32) (p-in-bool 0 :type u32) (p-out-f32 0 :type u32)
  (p-uni-v4f32 0 :type u32) (p-func-v4f32 0 :type u32)
  ;; constants: (kind << 32 | bits) -> id; kind 0 f32, 1 v4f32 splat, 2 i32, 3 u32, 4 bool
  (const-key (make-array 128 :element-type '(unsigned-byte 64) :initial-element 0)
   :type (simple-array (unsigned-byte 64) (128)))
  (const-id (make-array 128 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (128)))
  (const-n 0 :type u32)
  ;; banks of Private variables (0: not made yet)
  (temp (make-array 64 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (64)))
  (temp-mixed (make-array 64 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (64)))
  (cc (make-array 2 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (2)))
  (addr (make-array 2 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (2)))
  (out (make-array 16 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (16)))
  (half (make-array 96 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (96)))
  ;; interface variables per slot (0: not made yet)
  (in-var (make-array 32 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (32)))
  (out-var (make-array 32 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (32)))
  (bank-var (make-array 2 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (2)))
  ;; texture units: the variable and its sampled-image type
  (sampler-var (make-array 20 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (20)))
  (sampler-type (make-array 20 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (20)))
  (image-type (make-array 20 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (20)))
  ;; image types are declared once per dimensionality and depth: (dim * 2 + shadow) -> image, sampled image
  (dim-image (make-array 8 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (8)))
  (dim-sampled (make-array 8 :element-type '(unsigned-byte 32) :initial-element 0)
   :type (simple-array (unsigned-byte 32) (8)))
  ;; the block being written is terminated (a branch was emitted); a function is open
  (closed 0 :type u1)
  (fn-open 0 :type u1)
  (clip-capability 0 :type u1)
  ;; a test harness: the stage runs as a compute shader whose invocation g
  ;; reads input slot s from d[64 g + s] and writes output slot s to
  ;; d[64 g + 32 + s] of a storage buffer (set 2, binding 0); a kill sets
  ;; d[64 g + 63] to 1 and returns. Fragment stages without derivatives or
  ;; implicit levels can then be compared with a reference interpreter.
...

7 more lines: the source.

(defun sps-clear-module) source
(defun sps-clear-module (m)
  (declare (type spv-module m))
  (setf (spvm-cap-n m) 0 (spvm-ext-n m) 0 (spvm-import-n m) 0 (spvm-model-n m) 0 (spvm-entry-n m) 0
        (spvm-mode-n m) 0 (spvm-debug-n m) 0 (spvm-annot-n m) 0 (spvm-types-n m) 0 (spvm-code-n m) 0
        (spvm-bound m) 1 (spvm-overflow m) 0)
  nil)
(defmacro glsl-inst) source
(defmacro glsl-inst (name) (spvi-opcode (spv-find-inst (symbol-name name) "GLSL.std.450")))
(defmacro sps-glsl) source
(defmacro sps-glsl (sh name type &rest args)
  (let ((s (gensym "SH")))
    `(let ((,s ,sh))
       (spv-op (sps-m ,s) OpExtInst ,type :new (sps-glsl-set ,s) (glsl-inst ,name) ,@args))))
(defun sps-glsl-set) source
(defun sps-glsl-set (sh) (declare (type spv-shader sh)) (sps-glsl-id sh))
(defun sps-begin) source
(defun sps-begin (sh stage)
  "start the module of a STAGE (a shade-stage code) shader: header, types, main"
  (declare (type spv-shader sh) (type u8 stage))
  (let ((m (sps-m sh)))
    (sps-clear-module m)
    (shade-iface-reset (sps-iface sh) stage)
    (setf (sps-stage sh) stage (sps-const-n sh) 0 (sps-closed sh) 0 (sps-clip-capability sh) 0 (sps-failed sh) 0
          (sps-ssbo sh) 0 (sps-gid sh) 0)
    (dotimes (i 64) (setf (aref (sps-temp sh) i) 0 (aref (sps-temp-mixed sh) i) 0))
    (dotimes (i 2) (setf (aref (sps-cc sh) i) 0 (aref (sps-addr sh) i) 0 (aref (sps-bank-var sh) i) 0))
    (dotimes (i 16) (setf (aref (sps-out sh) i) 0))
    (dotimes (i 96) (setf (aref (sps-half sh) i) 0))
    (dotimes (i 32) (setf (aref (sps-in-var sh) i) 0 (aref (sps-out-var sh) i) 0))
    (dotimes (i 20) (setf (aref (sps-sampler-var sh) i) 0 (aref (sps-sampler-type sh) i) 0
                          (aref (sps-image-type sh) i) 0))
    (dotimes (i 8) (setf (aref (sps-dim-image sh) i) 0 (aref (sps-dim-sampled sh) i) 0))
    (spv-op m OpCapability 'Shader)
    (setf (sps-glsl-id sh) (spv-op m OpExtInstImport :new "GLSL.std.450"))
    (spv-op m OpMemoryModel 'Logical 'GLSL450)
    (setf (sps-main sh) (spv-id m))
    (let* ((void (spv-op m OpTypeVoid :new)) (f32 (spv-op m OpTypeFloat :new 32))
           (i32 (spv-op m OpTypeInt :new 32 1)) (u32 (spv-op m OpTypeInt :new 32 0))
           (bool (spv-op m OpTypeBool :new)) (v4 (spv-op m OpTypeVector :new f32 4))
           (v4i (spv-op m OpTypeVector :new i32 4)))
      (setf (sps-t-void sh) void (sps-t-fn sh) (spv-op m OpTypeFunction :new void)
            (sps-t-bool sh) bool (sps-t-f32 sh) f32 (sps-t-i32 sh) i32 (sps-t-u32 sh) u32
            (sps-t-v2f32 sh) (spv-op m OpTypeVector :new f32 2) (sps-t-v3f32 sh) (spv-op m OpTypeVector :new f32 3)
            (sps-t-v4f32 sh) v4 (sps-t-v4bool sh) (spv-op m OpTypeVector :new bool 4) (sps-t-v4i32 sh) v4i
            (sps-t-v2u32 sh) (spv-op m OpTypeVector :new u32 2) (sps-t-v2i32 sh) (spv-op m OpTypeVector :new i32 2)
            (sps-p-priv-v4f32 sh) (spv-op m OpTypePointer :new 'Private v4)
            (sps-p-priv-v4i32 sh) (spv-op m OpTypePointer :new 'Private v4i)
            (sps-p-priv-i32 sh) (spv-op m OpTypePointer :new 'Private i32)
            (sps-p-priv-bool sh) (spv-op m OpTypePointer :new 'Private bool)
            (sps-p-in-v4f32 sh) (spv-op m OpTypePointer :new 'Input v4)
            (sps-p-out-v4f32 sh) (spv-op m OpTypePointer :new 'Output v4)
            (sps-p-in-bool sh) (spv-op m OpTypePointer :new 'Input bool)
            (sps-p-out-f32 sh) (spv-op m OpTypePointer :new 'Output f32)
            (sps-p-uni-v4f32 sh) (spv-op m OpTypePointer :new 'Uniform v4)
            (sps-p-func-v4f32 sh) (spv-op m OpTypePointer :new 'Function v4)))
    (spv-op m OpFunction (sps-t-void sh) (sps-main sh) 'None (sps-t-fn sh))
    (spv-op m OpLabel :new)
    (setf (sps-fn-open sh) 1)
    nil))
(defun sps-end-function) source
(defun sps-end-function (sh)
  "end the function being written (a return if its last block is open)"
  (declare (type spv-shader sh))
  (when (= (sps-fn-open sh) 1)
    (when (= (sps-closed sh) 0) (spv-op (sps-m sh) OpReturn))
    (spv-op (sps-m sh) OpFunctionEnd)
    (setf (sps-fn-open sh) 0 (sps-closed sh) 1))
  nil)
(defun sps-begin-function) source
(defun sps-begin-function (sh id)
  "start the function ID (void, no parameters), after the one being written"
  (declare (type spv-shader sh) (type u32 id))
  (sps-end-function sh)
  (spv-op (sps-m sh) OpFunction (sps-t-void sh) id 'None (sps-t-fn sh))
  (spv-op (sps-m sh) OpLabel :new)
  (setf (sps-fn-open sh) 1 (sps-closed sh) 0)
  nil)
(defun sps-call) source
(defun sps-call (sh id)
  "call the function ID"
  (declare (type spv-shader sh) (type u32 id))
  (spv-op (sps-m sh) OpFunctionCall (sps-t-void sh) :new id)
  nil)
(defun sps-const) source
(defun sps-const (sh kind bits)
  "the id of a constant: KIND 0 f32, 1 v4f32 splat, 2 i32, 3 u32, 4 bool; BITS its value"
  (declare (type spv-shader sh) (type u8 kind) (type u32 bits))
  (let ((key (logior (ash kind 32) bits)) (m (sps-m sh)))
    (dotimes (i (sps-const-n sh))
      (when (= (aref (sps-const-key sh) i) key)
        (return-from sps-const (aref (sps-const-id sh) i))))
    (let ((id (case kind
                (0 (spv-op m OpConstant (sps-t-f32 sh) :new bits))
                (1 (let ((c (sps-const sh 0 bits)))
                     (spv-op m OpConstantComposite (sps-t-v4f32 sh) :new c c c c)))
                (2 (spv-op m OpConstant (sps-t-i32 sh) :new bits))
                (3 (spv-op m OpConstant (sps-t-u32 sh) :new bits))
                (otherwise (if (= bits 0)
                               (spv-op m OpConstantFalse (sps-t-bool sh) :new)
                               (spv-op m OpConstantTrue (sps-t-bool sh) :new))))))
      (when (< (sps-const-n sh) (sps-consts))
        (setf (aref (sps-const-key sh) (sps-const-n sh)) key (aref (sps-const-id sh) (sps-const-n sh)) id
              (sps-const-n sh) (+ (sps-const-n sh) 1)))
      id)))
(defun sps-f32) source
(defun sps-f32 (sh f) (declare (type spv-shader sh) (type single-float f)) (sps-const sh 0 (f32-bits f)))
(defun sps-splat) source
(defun sps-splat (sh f) (declare (type spv-shader sh) (type single-float f)) (sps-const sh 1 (f32-bits f)))
(defun sps-i32) source
(defun sps-i32 (sh i) (declare (type spv-shader sh) (type s32 i)) (sps-const sh 2 (wrap 32 i)))
(defun sps-u32) source
(defun sps-u32 (sh u) (declare (type spv-shader sh) (type u32 u)) (sps-const sh 3 u))
(defun sps-vec4-const) source
(defun sps-vec4-const (sh x y z w)
  "a v4f32 constant of four floats"
  (declare (type spv-shader sh) (type single-float x y z w))
  (spv-op (sps-m sh) OpConstantComposite (sps-t-v4f32 sh) :new (sps-f32 sh x) (sps-f32 sh y) (sps-f32 sh z)
          (sps-f32 sh w)))
(defun sps-swizzle) source
(defun sps-swizzle (sh v sel)
  "V swizzled: component k from (ldb (byte 2 (* 2 k)) SEL); the identity costs nothing"
  (declare (type spv-shader sh) (type u32 v) (type (unsigned-byte 8) sel))
  (if (= sel #b11100100)
      v
      (spv-op (sps-m sh) OpVectorShuffle (sps-t-v4f32 sh) :new v v (ldb (byte 2 0) sel) (ldb (byte 2 2) sel)
              (ldb (byte 2 4) sel) (ldb (byte 2 6) sel))))
(defun sps-merge) source
(defun sps-merge (sh old new mask)
  "NEW in the components of MASK (a constant mask, bit k = component k), OLD elsewhere"
  (declare (type spv-shader sh) (type u32 old new) (type (unsigned-byte 4) mask))
  (cond ((= mask 15) new)
        ((= mask 0) old)
        (t (spv-op (sps-m sh) OpVectorShuffle (sps-t-v4f32 sh) :new old new
                   (if (logbitp 0 mask) 4 0) (if (logbitp 1 mask) 5 1) (if (logbitp 2 mask) 6 2)
                   (if (logbitp 3 mask) 7 3)))))
(defun sps-select) source
(defun sps-select (sh cond a b)
  "A where the v4bool COND holds, B elsewhere"
  (declare (type spv-shader sh) (type u32 cond a b))
  (spv-op (sps-m sh) OpSelect (sps-t-v4f32 sh) :new cond a b))
(defun sps-abs) source
(defun sps-abs (sh v) (declare (type spv-shader sh) (type u32 v)) (sps-glsl sh FAbs (sps-t-v4f32 sh) v))
(defun sps-neg) source
(defun sps-neg (sh v)
  (declare (type spv-shader sh) (type u32 v))
  (spv-op (sps-m sh) OpFNegate (sps-t-v4f32 sh) :new v))
(defun sps-clamp) source
(defun sps-clamp (sh v lo hi)
  "V clamped to [LO, HI] (FClamp: a NaN is undefined)"
  (declare (type spv-shader sh) (type u32 v) (type single-float lo hi))
  (sps-glsl sh FClamp (sps-t-v4f32 sh) v (sps-splat sh lo) (sps-splat sh hi)))
(defun sps-sat) source
(defun sps-sat (sh v)
  "V clamped to [0, 1], a NaN to 0: max(v, 0) as a select (false for a NaN), then min"
  (declare (type spv-shader sh) (type u32 v))
  (let* ((m (sps-m sh)) (zero (sps-splat sh 0.0)) (one (sps-splat sh 1.0))
         (pos (spv-op m OpFOrdGreaterThan (sps-t-v4bool sh) :new v zero))
         (lo (sps-select sh pos v zero))
         (big (spv-op m OpFOrdGreaterThan (sps-t-v4bool sh) :new lo one)))
    (sps-select sh big one lo)))
(defun sps-component) source
(defun sps-component (sh v k)
  "component K of V as an f32 id"
  (declare (type spv-shader sh) (type u32 v) (type (unsigned-byte 2) k))
  (spv-op (sps-m sh) OpCompositeExtract (sps-t-f32 sh) :new v k))
(defun sps-splat-of) source
(defun sps-splat-of (sh f)
  "an f32 id in every component"
  (declare (type spv-shader sh) (type u32 f))
  (spv-op (sps-m sh) OpCompositeConstruct (sps-t-v4f32 sh) :new f f f f))
(defun sps-vec4-of) source
(defun sps-vec4-of (sh x y z w)
  "four f32 ids as a vec4"
  (declare (type spv-shader sh) (type u32 x y z w))
  (spv-op (sps-m sh) OpCompositeConstruct (sps-t-v4f32 sh) :new x y z w))
(defun sps-dot) source
(defun sps-dot (sh n a b)
  "the dot product of the first N components, summed in order (x y z w) -> an f32 id"
  (declare (type spv-shader sh) (type u32 a b) (type (unsigned-byte 3) n))
  (let* ((m (sps-m sh)) (f (sps-t-f32 sh))
         (s (spv-op m OpFMul f :new (sps-component sh a 0) (sps-component sh b 0))))
    (dotimes (k (- n 1) s)
      (setq s (spv-op m OpFAdd f :new s (spv-op m OpFMul f :new (sps-component sh a (+ k 1))
                                                (sps-component sh b (+ k 1))))))))
(defun sps-cond-lanes) source
(defun sps-cond-lanes (sh cc lt eq gt)
  "the v4bool of the lanes where CC (a swizzled vec4 id) compares with 0 as
   LT EQ GT ask; ordered, so a NaN is in no relation; 0 for never"
  (declare (type spv-shader sh) (type u32 cc) (type u1 lt eq gt))
  (let ((m (sps-m sh)) (b (sps-t-v4bool sh)) (z (sps-splat sh 0.0)))
    (case (logior lt (ash eq 1) (ash gt 2))
      (1 (spv-op m OpFOrdLessThan b :new cc z))
      (2 (spv-op m OpFOrdEqual b :new cc z))
      (3 (spv-op m OpFOrdLessThanEqual b :new cc z))
      (4 (spv-op m OpFOrdGreaterThan b :new cc z))
      (5 (spv-op m OpFOrdNotEqual b :new cc z))
      (6 (spv-op m OpFOrdGreaterThanEqual b :new cc z))
      (otherwise 0))))
(defun sps-any) source
(defun sps-any (sh lanes)
  "a bool id: any of the v4bool LANES"
  (declare (type spv-shader sh) (type u32 lanes))
  (spv-op (sps-m sh) OpAny (sps-t-bool sh) :new lanes))
(defun sps-bits) source
(defun sps-bits (sh v)
  "a vec4 as v4i32 bits"
  (declare (type spv-shader sh) (type u32 v))
  (spv-op (sps-m sh) OpBitcast (sps-t-v4i32 sh) :new v))
(defun sps-float-of-bits) source
(defun sps-float-of-bits (sh v)
  (declare (type spv-shader sh) (type u32 v))
  (spv-op (sps-m sh) OpBitcast (sps-t-v4f32 sh) :new v))
(defun sps-private) source
(defun sps-private (sh type)
  "a new Private variable of pointer type TYPE"
  (declare (type spv-shader sh) (type u32 type))
  (spv-op-in (sps-m sh) :types OpVariable type :new 'Private))
(defun sps-private-init) source
(defun sps-private-init (sh type init)
  "a new Private variable of pointer type TYPE initialised to the constant INIT"
  (declare (type spv-shader sh) (type u32 type init))
  (spv-op-in (sps-m sh) :types OpVariable type :new 'Private init))
(defun sps-bank-slot) source
(defun sps-bank-slot (sh vars i init)
  "variable I of a bank whose ids are VARS, made on first use with the constant INIT"
  (declare (type spv-shader sh) (type u32 i init))
  (when (= (aref vars i) 0)
    (setf (aref vars i) (sps-private-init sh (sps-p-priv-v4f32 sh) init)))
  (aref vars i))
(defun sps-reg-var) source
(defun sps-reg-var (sh bank i)
  (declare (type spv-shader sh) (type u8 bank) (type u32 i))
  (case bank
    (0 (sps-bank-slot sh (sps-temp sh) i (sps-splat sh 0.0)))
    (1 (sps-bank-slot sh (sps-cc sh) i (sps-splat sh 0.0)))
    (2 (sps-bank-slot sh (sps-out sh) i (sps-vec4-const sh 0.0 0.0 0.0 1.0)))
    (otherwise (sps-bank-slot sh (sps-half sh) i (sps-splat sh 0.0)))))
(defun sps-load) source
(defun sps-load (sh bank i)
  "register I of BANK (0 temporary, 1 condition, 2 output, 3 half) -> a vec4 id"
  (declare (type spv-shader sh) (type u8 bank) (type u32 i))
  (spv-op (sps-m sh) OpLoad (sps-t-v4f32 sh) :new (sps-reg-var sh bank i)))
(defun sps-store) source
(defun sps-store (sh bank i v)
  (declare (type spv-shader sh) (type u8 bank) (type u32 i v))
  (spv-op (sps-m sh) OpStore (sps-reg-var sh bank i) v)
  nil)
(defun sps-store-masked) source
(defun sps-store-masked (sh bank i v mask lanes)
  "register I of BANK := V in the components of MASK where the v4bool LANES
   holds (0: everywhere)"
  (declare (type spv-shader sh) (type u8 bank) (type u32 i v lanes) (type (unsigned-byte 4) mask))
  (unless (= mask 0)
    (let ((new (if (and (= mask 15) (= lanes 0)) v (sps-merge sh (sps-load sh bank i) v mask))))
      (sps-store sh bank i (if (= lanes 0) new (sps-select sh lanes new (sps-load sh bank i))))))
  nil)
(defun sps-addr-var) source
(defun sps-addr-var (sh i)
  (declare (type spv-shader sh) (type u32 i))
  (when (= (aref (sps-addr sh) i) 0)
    (let ((z (sps-i32 sh 0)))
      (setf (aref (sps-addr sh) i)
            (sps-private-init sh (sps-p-priv-v4i32 sh)
                              (spv-op (sps-m sh) OpConstantComposite (sps-t-v4i32 sh) :new z z z z)))))
  (aref (sps-addr sh) i))
(defun sps-harness-at) source
(defun sps-harness-at (sh offset)
  "a pointer to vec4 64 g + OFFSET of the harness's storage buffer (g: this invocation)"
  (declare (type spv-shader sh) (type u32 offset))
  (let ((m (sps-m sh)))
    (when (= (sps-ssbo sh) 0)
      (let* ((rt (spv-op m OpTypeRuntimeArray :new (sps-t-v4f32 sh)))
             (st (spv-op m OpTypeStruct :new rt))
             (ptr (spv-op m OpTypePointer :new 'Uniform st))
             (v3 (spv-op m OpTypeVector :new (sps-t-u32 sh) 3))
             (pg (spv-op m OpTypePointer :new 'Input v3)))
        (spv-op m OpDecorate rt 'ArrayStride 16)
        (spv-op m OpMemberDecorate st 0 'Offset 0)
        (spv-op m OpDecorate st 'BufferBlock)
        (setf (sps-ssbo sh) (spv-op-in m :types OpVariable ptr :new 'Uniform)
              (sps-gid sh) (spv-op-in m :types OpVariable pg :new 'Input))
        (spv-op m OpDecorate (sps-ssbo sh) 'DescriptorSet 2)
        (spv-op m OpDecorate (sps-ssbo sh) 'Binding 0)
        (spv-op m OpDecorate (sps-gid sh) 'BuiltIn 'GlobalInvocationId)
        (setf (sps-t-v3u32 sh) v3)))
    (let* ((g (spv-op m OpCompositeExtract (sps-t-u32 sh) :new (spv-op m OpLoad (sps-t-v3u32 sh) :new (sps-gid sh)) 0))
           (i (spv-op m OpIAdd (sps-t-u32 sh) :new (spv-op m OpIMul (sps-t-u32 sh) :new g (sps-u32 sh 64))
                      (sps-u32 sh offset))))
      (spv-op m OpAccessChain (sps-p-uni-v4f32 sh) :new (sps-ssbo sh) (sps-u32 sh 0) i))))
(defun sps-decorate-slot) source
(defun sps-decorate-slot (sh var kind index interp)
  (declare (type spv-shader sh) (type u32 var) (type u8 kind index interp))
  (let ((m (sps-m sh)))
    (cond ((= kind (gfx-code shade-slot position)) (spv-op m OpDecorate var 'BuiltIn 'Position))
          ((= kind (gfx-code shade-slot point-size)) (spv-op m OpDecorate var 'BuiltIn 'PointSize))
          ((= kind (gfx-code shade-slot frag-coord)) (spv-op m OpDecorate var 'BuiltIn 'FragCoord))
          ((= kind (gfx-code shade-slot front-facing)) (spv-op m OpDecorate var 'BuiltIn 'FrontFacing))
          ((= kind (gfx-code shade-slot depth)) (spv-op m OpDecorate var 'BuiltIn 'FragDepth))
          (t (spv-op m OpDecorate var 'Location index)
             (when (= interp (gfx-code shade-interp flat)) (spv-op m OpDecorate var 'Flat))
             (when (= interp (gfx-code shade-interp noperspective)) (spv-op m OpDecorate var 'NoPerspective))))
    nil))
(defun sps-input-var) source
(defun sps-input-var (sh slot)
  "the Input variable of interface slot SLOT (described in the iface), made on first use"
  (declare (type spv-shader sh) (type u8 slot))
  (let ((f (sps-iface sh)))
    (when (= (aref (sps-in-var sh) slot) 0)
      (let* ((kind (aref (sif-in-kind f) slot))
             (type (if (= kind (gfx-code shade-slot front-facing)) (sps-p-in-bool sh) (sps-p-in-v4f32 sh)))
             (var (spv-op-in (sps-m sh) :types OpVariable type :new 'Input)))
        (sps-decorate-slot sh var kind (aref (sif-in-index f) slot) (aref (sif-in-interp f) slot))
        (setf (aref (sps-in-var sh) slot) var (sif-in-used f) (logior (sif-in-used f) (ash 1 slot)))))
    (aref (sps-in-var sh) slot)))
(defun sps-input) source
(defun sps-input (sh slot)
  "interface input SLOT as a vec4 id (front-facing: 1.0 or -1.0 in every component)"
  (declare (type spv-shader sh) (type u8 slot))
  (when (= (sps-harness sh) 1)
    (let ((f (sps-iface sh)))
      (setf (sif-in-used f) (logior (sif-in-used f) (ash 1 slot))))
    (return-from sps-input (spv-op (sps-m sh) OpLoad (sps-t-v4f32 sh) :new (sps-harness-at sh slot))))
  (let ((var (sps-input-var sh slot)) (m (sps-m sh)))
    (if (= (aref (sif-in-kind (sps-iface sh)) slot) (gfx-code shade-slot front-facing))
        (let ((b (spv-op m OpLoad (sps-t-bool sh) :new var)))
          (spv-op m OpSelect (sps-t-v4f32 sh) :new
                  (spv-op m OpCompositeConstruct (sps-t-v4bool sh) :new b b b b)
                  (sps-splat sh 1.0) (sps-splat sh -1.0)))
        (spv-op m OpLoad (sps-t-v4f32 sh) :new var))))
(defun sps-output-var) source
(defun sps-output-var (sh slot)
  "the Output variable of interface slot SLOT, made on first use"
  (declare (type spv-shader sh) (type u8 slot))
  (let ((f (sps-iface sh)))
    (when (= (aref (sps-out-var sh) slot) 0)
      (let* ((kind (aref (sif-out-kind f) slot))
             (scalar (or (= kind (gfx-code shade-slot point-size)) (= kind (gfx-code shade-slot depth))))
             (var (if (= kind (gfx-code shade-slot clip-distance))
                      (sps-clip-var sh)
                      (spv-op-in (sps-m sh) :types OpVariable (if scalar (sps-p-out-f32 sh) (sps-p-out-v4f32 sh))
                                 :new 'Output))))
        (unless (= kind (gfx-code shade-slot clip-distance))
          (sps-decorate-slot sh var kind (aref (sif-out-index f) slot) 0))
        (setf (aref (sps-out-var sh) slot) var (sif-out-used f) (logior (sif-out-used f) (ash 1 slot)))))
    (aref (sps-out-var sh) slot)))
(defun sps-clip-var) source
(defun sps-clip-var (sh)
  "float ClipDistance[6], shared by every clip-distance slot"
  (declare (type spv-shader sh))
  (let ((f (sps-iface sh)))
    (dotimes (s 32)
      (when (and (/= (aref (sps-out-var sh) s) 0) (= (aref (sif-out-kind f) s) (gfx-code shade-slot clip-distance)))
        (return-from sps-clip-var (aref (sps-out-var sh) s)))))
  (let* ((m (sps-m sh))
         (arr (spv-op m OpTypeArray :new (sps-t-f32 sh) (sps-u32 sh 6)))
         (ptr (spv-op m OpTypePointer :new 'Output arr))
         (var (spv-op-in m :types OpVariable ptr :new 'Output)))
    (spv-op m OpCapability 'ClipDistance)
    (spv-op m OpDecorate var 'BuiltIn 'ClipDistance)
    var))
(defun sps-output) source
(defun sps-output (sh slot v)
  "interface output SLOT := V (a vec4 id; a scalar slot takes x)"
  (declare (type spv-shader sh) (type u8 slot) (type u32 v))
  (when (= (sps-harness sh) 1)
    (let ((f (sps-iface sh)))
      (setf (sif-out-used f) (logior (sif-out-used f) (ash 1 slot))))
    (spv-op (sps-m sh) OpStore (sps-harness-at sh (+ 32 slot)) v)
    (return-from sps-output nil))
  (let* ((kind (aref (sif-out-kind (sps-iface sh)) slot)) (var (sps-output-var sh slot)) (m (sps-m sh)))
    (cond ((= kind (gfx-code shade-slot clip-distance))
           (let ((p (spv-op m OpAccessChain (spv-op m OpTypePointer :new 'Output (sps-t-f32 sh)) :new var
                            (sps-i32 sh (aref (sif-out-index (sps-iface sh)) slot)))))
             (spv-op m OpStore p (sps-component sh v 0))))
          ((or (= kind (gfx-code shade-slot point-size)) (= kind (gfx-code shade-slot depth)))
           (spv-op m OpStore var (sps-component sh v 0)))
          (t (spv-op m OpStore var v)))
    nil))
(defun sps-bank-block) source
(defun sps-bank-block (sh bank)
  "the Uniform block of BANK: struct { vec4 c[size]; } in set 1, binding BANK"
  (declare (type spv-shader sh) (type (unsigned-byte 1) bank))
  (when (= (aref (sps-bank-var sh) bank) 0)
    (let* ((m (sps-m sh)) (n (max 1 (aref (sif-bank-size (sps-iface sh)) bank)))
           (arr (spv-op m OpTypeArray :new (sps-t-v4f32 sh) (sps-u32 sh n)))
           (st (spv-op m OpTypeStruct :new arr))
           (ptr (spv-op m OpTypePointer :new 'Uniform st))
           (var (spv-op-in m :types OpVariable ptr :new 'Uniform)))
      (spv-op m OpDecorate arr 'ArrayStride 16)
      (spv-op m OpMemberDecorate st 0 'Offset 0)
      (spv-op m OpDecorate st 'Block)
      (spv-op m OpDecorate var 'DescriptorSet 1)
      (spv-op m OpDecorate var 'Binding bank)
      (setf (aref (sps-bank-var sh) bank) var)))
  (aref (sps-bank-var sh) bank))
(defun sps-uniform) source
(defun sps-uniform (sh bank index)
  "vec4 INDEX (an i32 id) of uniform BANK -> its id"
  (declare (type spv-shader sh) (type (unsigned-byte 1) bank) (type u32 index))
  (let ((m (sps-m sh)))
    (spv-op m OpLoad (sps-t-v4f32 sh) :new
            (spv-op m OpAccessChain (sps-p-uni-v4f32 sh) :new (sps-bank-block sh bank) (sps-i32 sh 0) index))))
(defun sps-sampler) source
(defun sps-sampler (sh unit dim shadow)
  "the combined image sampler of texture UNIT (set 0, binding UNIT) of
   dimensionality DIM (shade-dim: 1D is sampled as 2D) -> the loaded
   sampled image id. A unit is declared once per module, its first use deciding."
  (declare (type spv-shader sh) (type u8 unit dim) (type u1 shadow))
  (let ((m (sps-m sh)) (f (sps-iface sh)))
    (when (= (aref (sps-sampler-var sh) unit) 0)
      (let* ((d (cond ((= dim (gfx-code shade-dim dim-3d)) (spv-enum Dim "3D"))
                      ((= dim (gfx-code shade-dim dim-cube)) (spv-enum Dim Cube))
                      (t (spv-enum Dim "2D"))))
             (k (+ (* 2 (min dim 3)) shadow))
             (image (if (= (aref (sps-dim-image sh) k) 0)
                        (setf (aref (sps-dim-image sh) k) (spv-op m OpTypeImage :new (sps-t-f32 sh) d shadow 0 0 1 'Unknown))
                        (aref (sps-dim-image sh) k)))
             (si (if (= (aref (sps-dim-sampled sh) k) 0)
                     (setf (aref (sps-dim-sampled sh) k) (spv-op m OpTypeSampledImage :new image))
                     (aref (sps-dim-sampled sh) k)))
             (ptr (spv-op m OpTypePointer :new 'UniformConstant si))
             (var (spv-op-in m :types OpVariable ptr :new 'UniformConstant)))
        (spv-op m OpDecorate var 'DescriptorSet 0)
        (spv-op m OpDecorate var 'Binding unit)
        (setf (aref (sps-sampler-var sh) unit) var (aref (sps-sampler-type sh) unit) si
              (aref (sps-image-type sh) unit) image
              (sif-sampler-used f) (logior (sif-sampler-used f) (ash 1 unit))
              (aref (sif-sampler-dim f) unit) dim
              (aref (sif-sampler-flags f) unit) (if (= shadow 1) +shade-sample-shadow+ 0))))
    (spv-op m OpLoad (aref (sps-sampler-type sh) unit) :new (aref (sps-sampler-var sh) unit))))
(defun sps-coords) source
(defun sps-coords (sh dim c project)
  "the coordinate vector a sample of DIM takes from the vec4 C: 2D x y (and
   the divisor w when projecting: x y w), 3D and cube x y z (and w)"
  (declare (type spv-shader sh) (type u8 dim) (type u32 c) (type u1 project))
  (let ((m (sps-m sh)) (three (or (= dim (gfx-code shade-dim dim-3d)) (= dim (gfx-code shade-dim dim-cube)))))
    (cond ((and three (= project 1)) c)
          (three (spv-op m OpVectorShuffle (sps-t-v3f32 sh) :new c c 0 1 2))
          ((= project 1) (spv-op m OpVectorShuffle (sps-t-v3f32 sh) :new c c 0 1 3))
          (t (spv-op m OpVectorShuffle (sps-t-v2f32 sh) :new c c 0 1)))))
(defun sps-sample) source
(defun sps-sample (sh unit dim coords lod-kind lod dx dy flags)
  "texture UNIT sampled at COORDS (a vec4 id): LOD-KIND (shade-lod) with LOD
   (an f32 id: bias or level) or the gradients DX DY (vec4 ids); FLAGS
   +shade-sample-project+, +shade-sample-shadow+ (the reference is the
   coordinates' z, or w for a cube) -> a vec4 id (a shadow compare in every
   component). In a vertex shader an implicit level is level 0."
  (declare (type spv-shader sh) (type u8 unit dim lod-kind flags) (type u32 coords lod dx dy))
  (let* ((m (sps-m sh)) (v4 (sps-t-v4f32 sh))
         ;; a cube has no projective sample (its direction needs no divide)
         (project (if (and (logtest flags +shade-sample-project+) (/= dim (gfx-code shade-dim dim-cube))) 1 0))
         (shadow (if (logtest flags +shade-sample-shadow+) 1 0))
         (si (sps-sampler sh unit dim shadow))
         (c (sps-coords sh dim coords project))
         (lod-kind (if (and (= (sps-stage sh) (gfx-code shade-stage vertex)) (= lod-kind (gfx-code shade-lod implicit)))
                       (gfx-code shade-lod explicit) lod-kind))
         (lod (if (= lod 0) (sps-f32 sh 0.0) lod)))
    (when (= (sps-stage sh) (gfx-code shade-stage fragment))
      (unless (= lod-kind (gfx-code shade-lod explicit)) (setf (sif-derivatives (sps-iface sh)) 1)))
    (if (= shadow 1)
        (let* ((ref (sps-component sh coords (if (= dim (gfx-code shade-dim dim-cube)) 3 2)))
               (r (cond ((= lod-kind (gfx-code shade-lod implicit))
                         (if (= project 1)
                             (spv-op m OpImageSampleProjDrefImplicitLod (sps-t-f32 sh) :new si c ref)
                             (spv-op m OpImageSampleDrefImplicitLod (sps-t-f32 sh) :new si c ref)))
                        ((= lod-kind (gfx-code shade-lod bias))
                         (if (= project 1)
                             (spv-op m OpImageSampleProjDrefImplicitLod (sps-t-f32 sh) :new si c ref 'Bias lod)
                             (spv-op m OpImageSampleDrefImplicitLod (sps-t-f32 sh) :new si c ref 'Bias lod)))
                        (t (if (= project 1)
                               (spv-op m OpImageSampleProjDrefExplicitLod (sps-t-f32 sh) :new si c ref 'Lod lod)
                               (spv-op m OpImageSampleDrefExplicitLod (sps-t-f32 sh) :new si c ref 'Lod lod))))))
          (sps-splat-of sh r))
        (cond ((= lod-kind (gfx-code shade-lod implicit))
               (if (= project 1)
                   (spv-op m OpImageSampleProjImplicitLod v4 :new si c)
                   (spv-op m OpImageSampleImplicitLod v4 :new si c)))
              ((= lod-kind (gfx-code shade-lod bias))
               (if (= project 1)
                   (spv-op m OpImageSampleProjImplicitLod v4 :new si c 'Bias lod)
                   (spv-op m OpImageSampleImplicitLod v4 :new si c 'Bias lod)))
              ((= lod-kind (gfx-code shade-lod gradient))
               (let ((gx (sps-coords sh dim dx 0)) (gy (sps-coords sh dim dy 0)))
                 (if (= project 1)
                     (spv-op m OpImageSampleProjExplicitLod v4 :new si c 'Grad gx gy)
                     (spv-op m OpImageSampleExplicitLod v4 :new si c 'Grad gx gy))))
              (t (if (= project 1)
                     (spv-op m OpImageSampleProjExplicitLod v4 :new si c 'Lod lod)
                     (spv-op m OpImageSampleExplicitLod v4 :new si c 'Lod lod)))))))
(defun sps-texture-size) source
(defun sps-texture-size (sh unit)
  "the level-0 size of texture UNIT (already declared) as a vec4 (w h 1 1) of floats"
  (declare (type spv-shader sh) (type u8 unit))
  (let* ((m (sps-m sh))
         (img (spv-op m OpImage (aref (sps-image-type sh) unit) :new
                      (spv-op m OpLoad (aref (sps-sampler-type sh) unit) :new (aref (sps-sampler-var sh) unit))))
         (size (spv-op m OpImageQuerySizeLod (sps-t-v2i32 sh) :new img (sps-i32 sh 0)))
         (sf (spv-op m OpConvertSToF (sps-t-v2f32 sh) :new size)))
    (spv-op m OpCapability 'ImageQuery)
    (spv-op m OpVectorShuffle (sps-t-v4f32 sh) :new sf (sps-vec4-const sh 1.0 1.0 1.0 1.0) 0 1 2 3)))
(defun sps-label) source
(defun sps-label (sh label)
  "start the block LABEL"
  (declare (type spv-shader sh) (type u32 label))
  (spv-op (sps-m sh) OpLabel label)
  (setf (sps-closed sh) 0)
  nil)
(defun sps-branch) source
(defun sps-branch (sh label)
  "end the block with a branch to LABEL"
  (declare (type spv-shader sh) (type u32 label))
  (spv-op (sps-m sh) OpBranch label)
  (setf (sps-closed sh) 1)
  nil)
(defun sps-if) source
(defun sps-if (sh cond then else merge)
  "end the block: a selection on the bool COND to THEN or ELSE, joining at MERGE"
  (declare (type spv-shader sh) (type u32 cond then else merge))
  (spv-op (sps-m sh) OpSelectionMerge merge 'None)
  (spv-op (sps-m sh) OpBranchConditional cond then else)
  (setf (sps-closed sh) 1)
  nil)
(defun sps-kill) source
(defun sps-kill (sh)
  (declare (type spv-shader sh))
  (if (= (sps-harness sh) 1)
      (progn (spv-op (sps-m sh) OpStore (sps-harness-at sh 63) (sps-splat sh 1.0))
             (spv-op (sps-m sh) OpReturn))
      (spv-op (sps-m sh) OpKill))
  (setf (sps-closed sh) 1 (sif-kills (sps-iface sh)) 1)
  nil)
(defmacro define-sps-finish) source
(defmacro define-sps-finish (&body body)
  `(defun sps-finish (sh out)
     "end main, write the entry point (its interface: every Input and Output
      variable made) and modes, and the module into OUT -> its word count, 0 on overflow"
     (declare (type spv-shader sh) (type (simple-array (unsigned-byte 32) (,(spv-total-words))) out))
     ,@body))

Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.