vk: arena.lisp

Vulkan structs in a byte buffer, by their host layout.

Other definitions

(defmacro vk-arena-capacity) source
(defmacro vk-arena-capacity () 65536)
(defstruct vk-arena) source
(defstruct (vk-arena (:conc-name vka-))
  (bytes (make-array (vk-arena-capacity) :element-type '(unsigned-byte 8) :initial-element 0)
   :type (simple-array (unsigned-byte 8) (65536)))
  (top 16 :type (unsigned-byte 32))
  (base 0 :type (unsigned-byte 64))
  ;; set when an allocation did not fit: what was built since is incomplete
  (overflow 0 :type (unsigned-byte 1)))
(defun vka-init) source
(defun vka-init (a)
  "Make A empty and learn its host address"
  (declare (type vk-arena a))
  (setf (vka-base a) (host-vk-arena-base (vka-bytes a)))
  (setf (vka-top a) 16 (vka-overflow a) 0)
  nil)
(defun vka-reset) source
(defun vka-reset (a)
  "Free everything in A (once the commands that read it have been recorded)"
  (declare (type vk-arena a))
  (setf (vka-top a) 16 (vka-overflow a) 0)
  nil)
(defun vka-off) source
(defun vka-off (a addr)
  "the offset in A of the host address ADDR"
  (declare (type vk-arena a) (type (unsigned-byte 64) addr))
  (- addr (vka-base a)))
(defun vka-alloc) source
(defun vka-alloc (a size align)
  "SIZE zeroed bytes aligned to ALIGN -> their address, or 0 (and OVERFLOW set) if A is full"
  (declare (type vk-arena a) (type (unsigned-byte 32) size align))
  (let ((off (* align (floor (+ (vka-top a) (- align 1)) align))))
    (if (> (+ off size) (vk-arena-capacity))
        (progn (setf (vka-overflow a) 1) 0)
        (let ((b (vka-bytes a)))
          (dotimes (i size) (setf (aref b (+ off i)) 0))
          (setf (vka-top a) (+ off size))
          (+ (vka-base a) off)))))
(defmacro define-arena-access) source
(defmacro define-arena-access (&rest widths)
  `(progn
     ,@(mapcan
        (lambda (w)
          (let ((get (intern (format nil "VKA-GET~D" w) :isa.vk)) (put (intern (format nil "VKA-PUT~D" w) :isa.vk)))
            `((defun ,get (a off)
                (declare (type vk-arena a) (type (unsigned-byte 32) off))
                (let ((b (vka-bytes a)))
                  (logior ,@(loop for k below (floor w 8)
                                  collect `(ash (aref b (+ off ,k)) ,(* 8 k))))))
              (defun ,put (a off value)
                (declare (type vk-arena a) (type (unsigned-byte 32) off) (type (unsigned-byte ,w) value))
                (let ((b (vka-bytes a)))
                  ,@(loop for k below (floor w 8)
                          collect `(setf (aref b (+ off ,k)) (ldb (byte 8 ,(* 8 k)) value))))
                nil))))
        widths)))
(defmacro vk-elt) source
(defmacro vk-elt (type addr i)
  "the address of element I of the array of TYPE at ADDR"
  (let ((size (vk-member-layout (make-vk-member :type (vkt-name* type) :ptr 0))))
    (if (eql i 0) addr `(+ ,addr (* ,i ,size)))))
(defmacro vk-alloc) source
(defmacro vk-alloc (a type &optional (count 1))
  "COUNT zeroed TYPEs in arena A, each with its sType set -> the address of the first"
  (let* ((name (vkt-name* type))
         (size (vk-member-layout (make-vk-member :type name :ptr 0)))
         (align (nth-value 1 (vk-member-layout (make-vk-member :type name :ptr 0))))
         (ty (and (not (assoc name *vk-c-scalars* :test #'string=)) (vk-find-type name)))
         (stype (and ty (eq (vkt-kind ty) :struct) (vkt-stype ty)))
         (aa (gensym "A")) (p (gensym "P")) (i (gensym "I")))
    `(let* ((,aa ,a) (,p (vka-alloc ,aa (* ,count ,size) ,align)))
       ,@(when stype
           `((unless (= ,p 0)
               (dotimes (,i ,count)
                 (vka-put32 ,aa (+ (vka-off ,aa ,p) (* ,i ,size)) ,(vk-find-value stype))))))
       ,p)))
(defmacro vk-read) source
(defmacro vk-read (a type addr &optional path)
  "the member PATH (a keyword) of the TYPE at ADDR in arena A, or the TYPE itself"
  (let ((aa (if (symbolp a) a (error "VK-READ wants the arena as a variable"))))
    (multiple-value-bind (off ctype ptr dims)
        (if path (vk-resolve aa type addr path) (values `(vka-off ,aa ,addr) (vkt-name* type) 0 nil))
      (let ((class (vk-scalar-class ctype ptr)))
        (cond (dims `(+ (vka-base ,aa) ,off))                       ; an array: its address
              ((eq class :ptr) `(vka-get64 ,aa ,off))
              ((eq class :struct) `(+ (vka-base ,aa) ,off))
              ((eq class :float) `(bits-f32 (vka-get32 ,aa ,off)))
              (t (destructuring-bind (bytes signed) (cdr class)
                   (let ((get `(,(intern (format nil "VKA-GET~D" (* 8 bytes)) :isa.vk) ,aa ,off)))
                     (if signed `(sext ,(* 8 bytes) ,get) get)))))))))
(defmacro vk-set) source
(defmacro vk-set (a type addr &rest keys-and-values)
  "store member values into the TYPE at ADDR: (vk-set a VkViewport p :x 0.0 :width 640.0);
   with a single value and no key, store the TYPE itself (a float, a handle, ...)"
  (let ((aa (gensym "A")) (p (gensym "P")))
    (flet ((store (off ctype ptr dims v)
             (let ((class (vk-scalar-class ctype ptr)))
               (when (or dims (eq class :struct))
                 (error "VK-SET: ~A is an aggregate; set its members" ctype))
               (cond ((eq class :ptr) `(vka-put64 ,aa ,off ,v))
                     ((eq class :float) `(vka-put32 ,aa ,off (f32-bits ,v)))
                     (t (let ((w (* 8 (second class))))
                          `(,(intern (format nil "VKA-PUT~D" w) :isa.vk) ,aa ,off (ldb (byte ,w 0) ,v))))))))
      `(let ((,aa ,a) (,p ,addr))
         ,@(if (and keys-and-values (null (cdr keys-and-values)))
               (list (store `(vka-off ,aa ,p) (vkt-name* type) 0 nil (car keys-and-values)))
               (do ((kv keys-and-values (cddr kv)) (forms nil))
                   ((null kv) (nreverse forms))
                 (multiple-value-bind (off ctype ptr dims) (vk-resolve aa type p (first kv))
                   (push (store off ctype ptr dims (second kv)) forms))))
         nil))))

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