vk: cost.lisp

what Vulkan commands cost, and the cheapest way to make a state change.

Other definitions

(defparameter *vk-cost-classes*) source
(defparameter *vk-cost-classes*
  '((:dynamic . 1) (:bind . 2) (:draw . 4) (:other . 4) (:pipeline . 25) (:pass . 60) (:create . 100000)))
(defvar *vk-cost-hook*) source
(defvar *vk-cost-hook* nil "nil, or a function (command class default-cost) -> cost")
(defun vk-dynamic-command-p) source
(defun vk-dynamic-command-p (name)
  (let ((key (vk-key name)))
    (some (lambda (d) (string= (vk-key (cdr d)) key)) (vk-dynamic-list))))
(defun vk-cost-class) source
(defun vk-cost-class (name)
  (let* ((c (vk-find-command name)) (n (vkc-name c)) (tasks (or (vkc-tasks c) "")))
    (flet ((starts (prefix) (and (>= (length n) (length prefix)) (string= prefix (subseq n 0 (length prefix))))))
      (or (getf (vkc-sem c) :cost)
          (cond ((string= n "vkCmdBindPipeline") :pipeline)
                ((starts "vkCmdBind") :bind)
                ((vk-dynamic-command-p n) :dynamic)
                ((and (starts "vkCreate") (search "Pipelines" n)) :create)
                ((or (starts "vkCmdBeginRender") (starts "vkCmdEndRender")) :pass)
                ((search "action" tasks) :draw)
                (t :other))))))
(defun vk-cost) source
(defun vk-cost (name)
  "the relative cost of one call of command NAME"
  (let* ((class (vk-cost-class name))
         (default (or (cdr (assoc class *vk-cost-classes*)) 4)))
    (if *vk-cost-hook* (funcall *vk-cost-hook* (vkc-name (vk-find-command name)) class default) default)))
(defun vk-field-writers) source
(defun vk-field-writers (group field)
  "the commands with semantics whose DEFVK assigns (gs GROUP ... FIELD)"
  (let ((key (list (symbol-name group) (symbol-name field))) (out nil))
    (dolist (c *vk-command-list* (nreverse out))
      (when (and (vkc-sem c)
                 (member key (mapcar (lambda (w) (mapcar #'symbol-name w)) (getf (vkc-sem c) :writes))
                         :test #'equal))
        (push (vkc-name c) out)))))
(defun vk-command-dynamic-state) source
(defun vk-command-dynamic-state (name)
  "the VK_DYNAMIC_STATE_ that lets command NAME override a pipeline, or nil"
  (car (find (vk-key name) (vk-dynamic-list) :key (lambda (d) (vk-key (cdr d))) :test #'string=)))
(defun vk-pipeline-field-p) source
(defun vk-pipeline-field-p (gf)
  "does pipeline creation set field GF (group field)?"
  (member (mapcar #'symbol-name gf) (mapcar (lambda (w) (mapcar #'symbol-name w)) *vk-pipeline-writes*)
          :test #'equal))
(defun vk-state-command-p) source
(defun vk-state-command-p (name)
  "a state command outside every dynamic state (it never interacts with a pipeline)?"
  (and (not (vk-command-dynamic-state name))
       (member (vk-cost-class name) '(:other :dynamic))
       (search "state" (or (vkc-tasks (vk-find-command name)) ""))))
(defun vk-state-change-plan) source
(defun vk-state-change-plan (fields &key dynamic (pipeline-cached t) commands)
  "FIELDS: ((group field) ...) that change; DYNAMIC: the VK_DYNAMIC_STATE_
   names the bound pipeline enables; PIPELINE-CACHED: whether a pipeline
   with the new baked state exists; COMMANDS: the state commands outside
   dynamic states the device has (extensions). -> (:cost n :commands
   (names...) :pipeline nil, :switch or :create :pass nil or :restart
   :shader-key (fields) :data (fields)). Each field goes to its cheapest
   allowed writer; a baked field that no enabled dynamic state covers
   needs a pipeline switch."
  (let ((commands-out nil) (pipeline nil) (pass nil) (shader-key nil) (data nil))
    (dolist (gf fields)
      (let* ((f (gfx-find-field (first gf) (second gf)))
             (all (vk-field-writers (first gf) (second gf)))
             (writers (remove-if-not
                       (lambda (c)
                         (and (not (member (vk-cost-class c) '(:pipeline :create :pass)))
                              (or (gff-binding f)
                                  (let ((d (vk-command-dynamic-state c)))
                                    (if d
                                        (member d dynamic :test #'string-equal)
                                        (and (vk-state-command-p c)
                                             (member c commands :test #'string-equal)))))))
                       all))
             (best (car (stable-sort (copy-list writers) #'< :key #'vk-cost)))
             (pipeline-needed (if pipeline-cached :switch :create)))
        (cond (best
               (unless (some (lambda (w) (member w commands-out :test #'string=)) writers) ; already chosen
                 (push best commands-out)))
              ((and all (every (lambda (c) (eq (vk-cost-class c) :pass)) all))
               (setq pass :restart))
              ((vk-pipeline-field-p gf)
               (setq pipeline pipeline-needed))
              ((eq (gff-part f) :vertex-input)
               (push gf data))
              (t
               (push gf shader-key)
               (setq pipeline pipeline-needed)))))
    (setq commands-out (nreverse commands-out))
    (list :cost (+ (reduce #'+ (mapcar #'vk-cost commands-out) :initial-value 0)
                   (case pipeline
                     (:switch (vk-cost "vkCmdBindPipeline"))
                     (:create (+ (vk-cost "vkCmdBindPipeline") (vk-cost "vkCreateGraphicsPipelines")))
                     (t 0))
                   (if pass (+ (vk-cost "vkCmdEndRendering") (vk-cost "vkCmdBeginRendering")) 0))
          :commands commands-out :pipeline pipeline :pass pass
          :shader-key (nreverse shader-key) :data (nreverse data))))

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