Vulkan as an ISA: DEFVK, the reference model, canonical enumerations.
(defmacro vk-pipeline-capacity) source(defmacro vk-pipeline-capacity () 16)(defstruct vk-model) source(defstruct (vk-model (:conc-name vkm-))
(gfx (make-gfx) :type gfx)
(arena (make-vk-arena) :type vk-arena)
;; pipeline handle h (1-based) is element h-1: its baked state and the
;; dynamic states it leaves to commands (bits, see VK-DYNAMIC-BIT)
(pipes (let ((v (make-array (vk-pipeline-capacity))))
(dotimes (i (vk-pipeline-capacity) v) (setf (aref v i) (make-gfx))))
:type (simple-array gfx (16)))
(pipe-dynamic (make-array (vk-pipeline-capacity) :element-type '(unsigned-byte 64) :initial-element 0)
:type (simple-array (unsigned-byte 64) (16)))
(pipe-count 0 :type (unsigned-byte 8)))(defmacro defvk) source(defmacro defvk (name params &body body)
(let* ((cmd (vk-find-command name))
(regs (vk-semantic-params cmd))
(doc (and (stringp (car body)) (cdr body) (pop body)))
(cost (and (consp (car body)) (eq (caar body) :cost) (second (pop body))))
(fn (vk-sem-fn-name (vkc-name cmd))))
(unless (= (length params) (length regs))
(error "~A takes ~D parameters (~{~A~^ ~}), not ~D" (vkc-name cmd) (length regs)
(mapcar #'vkm-name regs) (length params)))
(let ((pairs (mapcar #'cons params regs)))
`(progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf (vkc-sem (vk-find-command ',name))
(list :params ',params :body ',body :cost ',cost :fn ',fn :doc ,doc
:writes (gfx-collect-writes ',body))))
(defun ,fn (vk ,@params)
(declare (type vk-model vk)
,@(mapcar (lambda (p) `(type ,(vk-param-type (cdr p)) ,(car p))) pairs)
(ignorable ,@params))
(let ((vk-mem (vkm-arena vk)) (gfx (vkm-gfx vk)))
(declare (ignorable vk-mem gfx))
,@(vk-rewrite body pairs)))
',fn))))(defun vk-sem-body) source(defun vk-sem-body (name)
"the semantics of command NAME as DEFVK recorded it: (params . body)"
(let ((s (vkc-sem (vk-find-command name))))
(cons (getf s :params) (getf s :body))))(defun vk-writes) source(defun vk-writes (name)
"the (group field) pairs of the graphics state that command NAME assigns"
(getf (vkc-sem (vk-find-command name)) :writes))(defmacro define-vk-enum) source(defmacro define-vk-enum (kind prefix)
(let ((pairs nil) (code 0))
(dolist (n (gfx-enum-names kind))
(let ((base (concatenate 'string prefix (substitute #\_ #\- (symbol-name n)))))
(dolist (suffix '("" "_BIT" "_EXT" "_KHR"))
(let ((v (vk-find-value (concatenate 'string base suffix) nil)))
(when v (push (cons v code) pairs) (return)))))
(incf code))
`(eval-when (:compile-toplevel :load-toplevel :execute)
(setq *vk-enum-maps* (cons '(,kind ,@(nreverse pairs)) (remove ',kind *vk-enum-maps* :key #'car)))
',kind)))(defmacro vk->gfx) source(defmacro vk->gfx (kind x)
"the canonical code of the Vulkan value X of enumeration KIND"
(let ((m (vk-enum-map kind)))
(if (every (lambda (p) (= (car p) (cdr p))) m)
x
`(case ,x ,@(mapcar (lambda (p) `(,(car p) ,(cdr p))) m) (otherwise 0)))))(defmacro gfx->vk) source(defmacro gfx->vk (kind x)
"the Vulkan value of the canonical code X of enumeration KIND (0 if Vulkan lacks it)"
(let ((m (vk-enum-map kind)))
(if (every (lambda (p) (= (car p) (cdr p))) m)
x
`(case ,x ,@(mapcar (lambda (p) `(,(cdr p) ,(car p))) m) (otherwise 0)))))(defmacro vk-bool) source(defmacro vk-bool (x) `(b2i (/= ,x 0)))(defmacro vk-dynamic-bit-of) source(defmacro vk-dynamic-bit-of (state)
"the bit number of the VkDynamicState named STATE"
(vk-dynamic-bit state))(defmacro vk-dynamic-mask) source(defmacro vk-dynamic-mask (value)
"the bit (as a mask) of the VkDynamicState VALUE, 0 for one outside the model"
`(case ,value
,@(let ((i -1))
(mapcar (lambda (d) (incf i) `(,(vk-find-value (car d)) ,(ash 1 i)))
(vk-dynamic-list)))
(otherwise 0)))Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.