Skip to content

Attachment.kt

src/jvmMain/kotlin/afield/graphics/vulkan/device/Attachment.kt · 50 lines

package afield.graphics.vulkan.device

import org.lwjgl.vulkan.VK10
import afield.graphics.texture.Image
import afield.graphics.texture.Image.ImageData
import afield.graphics.texture.ImageView
import afield.graphics.texture.ImageView.ImageViewData

An image plus image view sized and formatted to be used as a framebuffer attachment.

Attachment picks the view's aspect from the usage flags: a depth-stencil usage makes a depth view and sets isDepthAttachment, a colour usage makes a colour view. cleanup destroys the view and then the image.

class Attachment(device: Device, width: Int, height: Int, format: Int, usage: Int)

val image: Image

val imageView: ImageView

var isDepthAttachment: Boolean = false

init

    init {
        val role = if ((usage and VK10.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) > 0) "DepthAttachment" else "Attachment"
        val imageData =
            ImageData().width(width).height(height).usage(usage).format(format)
                .debugName("$role image ${width}x${height}")
        image = Image(device, imageData)

        var aspectMask = 0
        if ((usage and VK10.VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) > 0) {
            aspectMask = VK10.VK_IMAGE_ASPECT_COLOR_BIT
            isDepthAttachment = false
        }
        if ((usage and VK10.VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) > 0) {
            aspectMask = VK10.VK_IMAGE_ASPECT_DEPTH_BIT
            isDepthAttachment = true
        }

        val imageViewData = ImageViewData().format(image.format).aspectMask(aspectMask)
            .debugName("$role view ${width}x${height}")
        imageView = ImageView(device, image.vkImage, imageViewData).source(image)
    }

fun cleanup()

    fun cleanup() {
        imageView.cleanup()
        image.cleanup()
    }
}