vk: emit.lisp

the run-time emitter: abstract state -> Vulkan structs and calls.

Other definitions

(defun vk-emit-viewport) source
(defun vk-emit-viewport (a cb index x y width height min-depth max-depth)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 32) index)
           (type single-float x y width height min-depth max-depth))
  (let ((vp (vk-alloc a VkViewport)))
    (vk-set a VkViewport vp :x x :y y :width width :height height :min-depth min-depth :max-depth max-depth)
    (host-vk-cmd-set-viewport cb index 1 vp)))
(defun vk-emit-scissor) source
(defun vk-emit-scissor (a cb index x y width height)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 32) index width height)
           (type (signed-byte 32) x y))
  (let ((r (vk-alloc a VkRect2D)))
    (vk-set a VkRect2D r :offset.x x :offset.y y :extent.width width :extent.height height)
    (host-vk-cmd-set-scissor cb index 1 r)))
(defun vk-emit-depth) source
(defun vk-emit-depth (a cb test write compare)
  "the depth test: enables (u1) and the canonical compare-op"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 1) test write)
           (type (unsigned-byte 8) compare) (ignorable a))
  (host-vk-cmd-set-depth-test-enable cb test)
  (host-vk-cmd-set-depth-write-enable cb write)
  (host-vk-cmd-set-depth-compare-op cb (gfx->vk compare-op compare)))
(defun vk-emit-raster) source
(defun vk-emit-raster (a cb cull face)
  "cull mode and front face, canonical codes"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 8) cull face) (ignorable a))
  (host-vk-cmd-set-cull-mode cb (gfx->vk cull-mode cull))
  (host-vk-cmd-set-front-face cb (gfx->vk front-face face)))
(defun vk-emit-stencil-reference) source
(defun vk-emit-stencil-reference (a cb faces reference)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 2) faces)
           (type (unsigned-byte 32) reference) (ignorable a))
  (host-vk-cmd-set-stencil-reference cb faces reference))
(defun vk-emit-blend-constants) source
(defun vk-emit-blend-constants (a cb r g b alpha)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type single-float r g b alpha))
  (let ((k (vk-alloc a "float" 4)))
    (vk-set a "float" k r)
    (vk-set a "float" (vk-elt "float" k 1) g)
    (vk-set a "float" (vk-elt "float" k 2) b)
    (vk-set a "float" (vk-elt "float" k 3) alpha)
    (host-vk-cmd-set-blend-constants cb k)))
(defun vk-emit-blend-equation) source
(defun vk-emit-blend-equation (a cb gfx attachment)
  "the blend equation of colour attachment ATTACHMENT of state GFX"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type gfx gfx) (type (unsigned-byte 3) attachment))
  (let ((e (vk-alloc a VkColorBlendEquationEXT)))
    (vk-set a VkColorBlendEquationEXT e
            :src-color-blend-factor (gfx->vk blend-factor (gs blend attachment src-color))
            :dst-color-blend-factor (gfx->vk blend-factor (gs blend attachment dst-color))
            :color-blend-op (gfx->vk blend-op (gs blend attachment color-op))
            :src-alpha-blend-factor (gfx->vk blend-factor (gs blend attachment src-alpha))
            :dst-alpha-blend-factor (gfx->vk blend-factor (gs blend attachment dst-alpha))
            :alpha-blend-op (gfx->vk blend-op (gs blend attachment alpha-op)))
    (host-vk-cmd-set-color-blend-equation-ext cb attachment 1 e)))
(defun vk-emit-bind-pipeline) source
(defun vk-emit-bind-pipeline (a cb pipeline)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb pipeline) (ignorable a))
  (host-vk-cmd-bind-pipeline cb (vk-value VK_PIPELINE_BIND_POINT_GRAPHICS) pipeline))
(defun vk-emit-vertex-buffer) source
(defun vk-emit-vertex-buffer (a cb binding buffer offset)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb buffer offset) (type (unsigned-byte 32) binding))
  (let ((bufs (vk-alloc a VkBuffer)) (offs (vk-alloc a VkDeviceSize)))
    (vk-set a VkBuffer bufs buffer)
    (vk-set a VkDeviceSize offs offset)
    (host-vk-cmd-bind-vertex-buffers cb binding 1 bufs offs)))
(defun vk-emit-index-buffer) source
(defun vk-emit-index-buffer (a cb buffer offset type)
  "TYPE is the canonical index-type"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb buffer offset) (type (unsigned-byte 8) type)
           (ignorable a))
  (host-vk-cmd-bind-index-buffer cb buffer offset (gfx->vk index-type type)))
(defun vk-emit-begin-rendering) source
(defun vk-emit-begin-rendering (a cb gfx clear)
  "begin rendering to the attachments and area of state GFX; with CLEAR (u1)
   the colour attachments load as the colour of (gs clear r..a) and depth as
   (gs clear depth), else they load their contents"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type gfx gfx) (type (unsigned-byte 1) clear))
  (let* ((n (gs color-target count))
         (colors (vk-alloc a VkRenderingAttachmentInfo n))
         (depth (if (= (gs depth-attachment view) 0) 0 (vk-alloc a VkRenderingAttachmentInfo)))
         (depth-only (= (gs depth-target stencil-format) 0))
         (info (vk-alloc a VkRenderingInfo))
         (load (if (= clear 1) (vk-value VK_ATTACHMENT_LOAD_OP_CLEAR) (vk-value VK_ATTACHMENT_LOAD_OP_LOAD))))
    (dotimes (i n)
      (vk-set a VkRenderingAttachmentInfo (vk-elt VkRenderingAttachmentInfo colors i)
              :image-view (gs attachment i view)
              :image-layout (vk-value VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)
              :load-op load :store-op (vk-value VK_ATTACHMENT_STORE_OP_STORE)
              :clear-value.color.float32.0 (gs clear r) :clear-value.color.float32.1 (gs clear g)
              :clear-value.color.float32.2 (gs clear b) :clear-value.color.float32.3 (gs clear a)))
    ;; a depth format with stencil: one view in DEPTH_STENCIL_ATTACHMENT_OPTIMAL is both attachments
    (unless (= depth 0)
      (vk-set a VkRenderingAttachmentInfo depth
              :image-view (gs depth-attachment view)
              :image-layout (if depth-only
                                (vk-value VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
                                (vk-value VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL))
              :load-op load :store-op (vk-value VK_ATTACHMENT_STORE_OP_STORE)
              :clear-value.depth-stencil.depth (gs clear depth)
              :clear-value.depth-stencil.stencil (gs clear stencil)))
    (vk-set a VkRenderingInfo info
            :render-area.offset.x (gs pass x) :render-area.offset.y (gs pass y)
            :render-area.extent.width (gs pass width) :render-area.extent.height (gs pass height)
            :layer-count (gs pass layers) :color-attachment-count n :p-color-attachments colors
            :p-depth-attachment depth :p-stencil-attachment (if depth-only 0 depth))
    (host-vk-cmd-begin-rendering cb info)))
(defun vk-emit-clear) source
(defun vk-emit-clear (a cb gfx)
  "the last clear of state GFX, (gs clear ...), inside the rendering: colour
   attachment (gs clear attachment), or depth and stencil, within its rectangle"
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type gfx gfx))
  (let ((att (vk-alloc a VkClearAttachment)) (rect (vk-alloc a VkClearRect)))
    (if (logbitp 0 (gs clear aspects))
        (vk-set a VkClearAttachment att
                :aspect-mask (vk-value VK_IMAGE_ASPECT_COLOR_BIT) :color-attachment (gs clear attachment)
                :clear-value.color.float32.0 (gs clear r) :clear-value.color.float32.1 (gs clear g)
                :clear-value.color.float32.2 (gs clear b) :clear-value.color.float32.3 (gs clear a))
        (vk-set a VkClearAttachment att
                :aspect-mask (gs clear aspects)       ; DEPTH_BIT 2, STENCIL_BIT 4, as the state's
                :clear-value.depth-stencil.depth (gs clear depth)
                :clear-value.depth-stencil.stencil (gs clear stencil)))
    (vk-set a VkClearRect rect
            :rect.offset.x (gs clear x) :rect.offset.y (gs clear y)
            :rect.extent.width (gs clear width) :rect.extent.height (gs clear height)
            :base-array-layer 0 :layer-count 1)
    (host-vk-cmd-clear-attachments cb 1 att 1 rect)))
(defun vk-emit-shader-module) source
(defun vk-emit-shader-module (a device code bytes)
  "a shader module of the SPIR-V at CODE (an arena address), BYTES long -> its handle, 0 on failure"
  (declare (type vk-arena a) (type (unsigned-byte 64) device code) (type (unsigned-byte 32) bytes))
  (let ((info (vk-alloc a VkShaderModuleCreateInfo)) (out (vk-alloc a VkShaderModule)))
    (when (= (vka-overflow a) 1)
      (return-from vk-emit-shader-module 0))
    (vk-set a VkShaderModuleCreateInfo info :code-size bytes :p-code code)
    (if (= (host-vk-create-shader-module device info 0 out) (vk-value VK_SUCCESS))
        (vk-read a VkShaderModule out)
        0)))
(defun vk-emit-end-rendering) source
(defun vk-emit-end-rendering (a cb)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (ignorable a))
  (host-vk-cmd-end-rendering cb))
(defun vk-emit-draw) source
(defun vk-emit-draw (a cb count instances first first-instance)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 32) count instances first first-instance)
           (ignorable a))
  (host-vk-cmd-draw cb count instances first first-instance))
(defun vk-emit-draw-indexed) source
(defun vk-emit-draw-indexed (a cb count instances first vertex-offset first-instance)
  (declare (type vk-arena a) (type (unsigned-byte 64) cb) (type (unsigned-byte 32) count instances first first-instance)
           (type (signed-byte 32) vertex-offset) (ignorable a))
  (host-vk-cmd-draw-indexed cb count instances first vertex-offset first-instance))
(defmacro vk-dynamic-value) source
(defmacro vk-dynamic-value (bit)
  "the VkDynamicState of dynamic-state bit BIT (VK-DYNAMIC-BIT)"
  `(case ,bit
     ,@(let ((i -1))
         (mapcar (lambda (d) (incf i) `(,i ,(vk-find-value (car d)))) (vk-dynamic-list)))
     (otherwise 0)))
(defmacro vk-dynamic-count) source
(defmacro vk-dynamic-count () (length (vk-dynamic-list)))
(defun vk-emit-graphics-pipeline) source
(defun vk-emit-graphics-pipeline (a device layout gfx dynamic)
  "Create a graphics pipeline holding the state GFX, with the dynamic states
   in the bit set DYNAMIC (VK-DYNAMIC-BIT) left to commands -> its handle (0 on failure)"
  (declare (type vk-arena a) (type (unsigned-byte 64) device layout dynamic) (type gfx gfx))
  (let ((info (vk-alloc a VkGraphicsPipelineCreateInfo))
        (stages (vk-alloc a VkPipelineShaderStageCreateInfo 2))
        (entry (vk-alloc a "uint32_t" 2))
        (vi (vk-alloc a VkPipelineVertexInputStateCreateInfo))
        (bindings (vk-alloc a VkVertexInputBindingDescription 16))
        (attributes (vk-alloc a VkVertexInputAttributeDescription 16))
        (ia (vk-alloc a VkPipelineInputAssemblyStateCreateInfo))
        (ts (vk-alloc a VkPipelineTessellationStateCreateInfo))
        (vs (vk-alloc a VkPipelineViewportStateCreateInfo))
        (viewports (vk-alloc a VkViewport 16))
        (scissors (vk-alloc a VkRect2D 16))
        (rs (vk-alloc a VkPipelineRasterizationStateCreateInfo))
        (clip (vk-alloc a VkPipelineRasterizationDepthClipStateCreateInfoEXT))
        (provoking (vk-alloc a VkPipelineRasterizationProvokingVertexStateCreateInfoEXT))
        (line (vk-alloc a VkPipelineRasterizationLineStateCreateInfo))
        (ms (vk-alloc a VkPipelineMultisampleStateCreateInfo))
        (mask (vk-alloc a VkSampleMask))
        (ds (vk-alloc a VkPipelineDepthStencilStateCreateInfo))
        (cb (vk-alloc a VkPipelineColorBlendStateCreateInfo))
        (atts (vk-alloc a VkPipelineColorBlendAttachmentState 8))
        (cw (vk-alloc a VkPipelineColorWriteCreateInfoEXT))
        (writes (vk-alloc a VkBool32 8))
        (dyn (vk-alloc a VkPipelineDynamicStateCreateInfo))
        (states (vk-alloc a VkDynamicState (vk-dynamic-count)))
        (rend (vk-alloc a VkPipelineRenderingCreateInfo))
        (formats (vk-alloc a VkFormat 8))
        (out (vk-alloc a VkPipeline))
        (nstates 0) (nbind 0) (nattr 0))
    (declare (type (unsigned-byte 32) nstates nbind nattr))
    (when (= (vka-overflow a) 1)
      (return-from vk-emit-graphics-pipeline 0))
    ;; shaders: the program's modules, entry point "main"
    (vk-set a "uint32_t" entry #x6E69616D)              ; "main", then a zero word
    (vk-set a VkPipelineShaderStageCreateInfo stages
            :stage (vk-value VK_SHADER_STAGE_VERTEX_BIT) :module (gs program vertex) :p-name entry)
    (vk-set a VkPipelineShaderStageCreateInfo (vk-elt VkPipelineShaderStageCreateInfo stages 1)
            :stage (vk-value VK_SHADER_STAGE_FRAGMENT_BIT) :module (gs program fragment) :p-name entry)
    ;; vertex input
    (dotimes (b (gs vertex-binding count))
      (vk-set a VkVertexInputBindingDescription (vk-elt VkVertexInputBindingDescription bindings nbind)
              :binding b :stride (gs vertex-binding b stride)
              :input-rate (gfx->vk input-rate (gs vertex-binding b input-rate)))
      (setq nbind (+ nbind 1)))
    (dotimes (l 16)
      (when (= (gs vertex-attribute l enable) 1)
        (vk-set a VkVertexInputAttributeDescription (vk-elt VkVertexInputAttributeDescription attributes nattr)
                :location l :binding (gs vertex-attribute l binding) :format (gs vertex-attribute l format)
                :offset (gs vertex-attribute l offset))
        (setq nattr (+ nattr 1))))
    (vk-set a VkPipelineVertexInputStateCreateInfo vi
            :vertex-binding-description-count nbind :p-vertex-binding-descriptions bindings
            :vertex-attribute-description-count nattr :p-vertex-attribute-descriptions attributes)
    ;; input assembly, tessellation
    (vk-set a VkPipelineInputAssemblyStateCreateInfo ia
            :topology (gfx->vk topology (gs primitive topology)) :primitive-restart-enable (gs primitive restart))
    (vk-set a VkPipelineTessellationStateCreateInfo ts :patch-control-points (gs primitive patch-points))
...

93 more lines: the source.


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