From 3126da339ce1f73b06214dd2247d6320339941fd Mon Sep 17 00:00:00 2001 From: elect86 Date: Sun, 6 May 2018 18:29:37 +0200 Subject: [PATCH] Bufferizable --- .../kotlin/vulkan/base/VulkanExampleBase.kt | 2 +- src/main/kotlin/vulkan/base/VulkanModel.kt | 2 +- .../vulkan/basics/03 Descriptor Sets.kt | 16 +++------- .../basics/06 Specialization Constants.kt | 2 ++ src/main/kotlin/vulkan/basics/07 Texture.kt | 26 +++++----------- .../vulkan/basics/08 Texture Cubemap.kt | 26 ++++++---------- .../kotlin/vulkan/basics/09 Texture Array.kt | 30 ++++++++----------- 7 files changed, 37 insertions(+), 67 deletions(-) diff --git a/src/main/kotlin/vulkan/base/VulkanExampleBase.kt b/src/main/kotlin/vulkan/base/VulkanExampleBase.kt index 1960e7e..81237e2 100644 --- a/src/main/kotlin/vulkan/base/VulkanExampleBase.kt +++ b/src/main/kotlin/vulkan/base/VulkanExampleBase.kt @@ -100,7 +100,7 @@ abstract class VulkanExampleBase { // Wraps the swap chain to present images (framebuffers) to the windowing system var swapChain = VulkanSwapChain() // Synchronization semaphores - private val semaphores = object { + object semaphores{ // Swap chain image presentation var presentComplete: VkSemaphore = NULL // Command buffer submission and execution diff --git a/src/main/kotlin/vulkan/base/VulkanModel.kt b/src/main/kotlin/vulkan/base/VulkanModel.kt index abdfd1a..220c056 100644 --- a/src/main/kotlin/vulkan/base/VulkanModel.kt +++ b/src/main/kotlin/vulkan/base/VulkanModel.kt @@ -79,7 +79,7 @@ class Model { val defaultFlags = Pp.FlipWindingOrder or Pp.Triangulate or Pp.PreTransformVertices or Pp.CalcTangentSpace or Pp.GenSmoothNormals - private val dim = object { + object dim { val min = Vec3(Float.MAX_VALUE) val max = Vec3(-Float.MAX_VALUE) val size = Vec3() diff --git a/src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt b/src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt index a33decf..e387abf 100644 --- a/src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt +++ b/src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt @@ -43,20 +43,13 @@ private class DescriptorSets : VulkanExampleBase() { VertexComponent.COLOR) class Cube { - class Matrices { + + class Matrices : Bufferizable() { var projection = Mat4() var view = Mat4() var model = Mat4() - fun pack() { - projection to buffer - view.to(buffer, Mat4.size) - model.to(buffer, Mat4.size * 2) - } - - val size = Mat4.size * 3 - val buffer = bufferBig(size) - val address = memAddress(buffer) + override val fieldOrder = arrayOf("projection", "view", "model") } val matrices = Matrices() @@ -362,9 +355,8 @@ private class DescriptorSets : VulkanExampleBase() { .rotateAssign(cube.rotation.x.rad, 1f, 0f, 0f) .rotateAssign(cube.rotation.y.rad, 0f, 1f, 0f) .rotateAssign(cube.rotation.z.rad, 0f, 0f, 1f) - pack() } - memCopy(cube.matrices.address, cube.uniformBuffer.mapped[0], cube.matrices.size.L) + cube.matrices to cube.uniformBuffer.mapped[0] } } diff --git a/src/main/kotlin/vulkan/basics/06 Specialization Constants.kt b/src/main/kotlin/vulkan/basics/06 Specialization Constants.kt index f0c99a7..b88d2d5 100644 --- a/src/main/kotlin/vulkan/basics/06 Specialization Constants.kt +++ b/src/main/kotlin/vulkan/basics/06 Specialization Constants.kt @@ -65,9 +65,11 @@ class SpecializationConstants : VulkanExampleBase() { // Same uniform buffer layout as shader object uboVS : Bufferizable() { + lateinit var projection: Mat4 lateinit var modelView: Mat4 val lightPos = Vec4(0f, -2f, 1f, 0f) + override val fieldOrder = arrayOf("projection", "modelView", "lightPos") } diff --git a/src/main/kotlin/vulkan/basics/07 Texture.kt b/src/main/kotlin/vulkan/basics/07 Texture.kt index 23aa55a..afc9bd0 100644 --- a/src/main/kotlin/vulkan/basics/07 Texture.kt +++ b/src/main/kotlin/vulkan/basics/07 Texture.kt @@ -60,7 +60,7 @@ private class Texture : VulkanExampleBase() { /** Contains all Vulkan objects that are required to store and use a texture * Note that this repository contains a texture class (VulkanTexture.hpp) that encapsulates texture loading functionality * in a class that is used in subsequent demos */ - private val texture = object { + object texture { var sampler: VkSampler = NULL var image: VkImage = NULL var imageLayout = VkImageLayout.UNDEFINED @@ -70,7 +70,7 @@ private class Texture : VulkanExampleBase() { var mipLevels = 0 } - private val vertices = object { + object vertices { val inputState = cVkPipelineVertexInputStateCreateInfo { } lateinit var bindingDescriptions: VkVertexInputBindingDescription.Buffer lateinit var attributeDescriptions: VkVertexInputAttributeDescription.Buffer @@ -82,24 +82,17 @@ private class Texture : VulkanExampleBase() { val uniformBufferVS = Buffer() - private val uboVS = object { + object uboVS : Bufferizable(){ + val projection = Mat4() val model = Mat4() val viewPos = Vec4() var lodBias = 0f - val size = Mat4.size * 2 + Vec4.size + Float.BYTES - val buffer = bufferBig(size) - val address = memAddress(buffer) - fun pack() { - projection to buffer - model.to(buffer, Mat4.size) - viewPos.to(buffer, Mat4.size * 2) - buffer.putFloat(Mat4.size * 2 + Vec4.size, lodBias) - } + override val fieldOrder = arrayOf("projection", "model", "viewPos", "lodBias") } - private val pipelines = object { + object pipelines { var solid: VkPipelineLayout = NULL } @@ -771,7 +764,7 @@ private class Texture : VulkanExampleBase() { VkBufferUsage.UNIFORM_BUFFER_BIT.i, VkMemoryProperty.HOST_VISIBLE_BIT or VkMemoryProperty.HOST_COHERENT_BIT, uniformBufferVS, - uboVS.buffer) + uboVS.size.L) updateUniformBuffers() } @@ -790,10 +783,7 @@ private class Texture : VulkanExampleBase() { uboVS.viewPos put Vec4(0f, 0f, -zoom, 0f) - uboVS.pack() - uniformBufferVS.mapping { dst -> - memCopy(uboVS.address, dst, uboVS.size.L) - } + uniformBufferVS.mapping { dst -> uboVS to dst } } override fun prepare() { diff --git a/src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt b/src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt index 63abd6d..5394416 100644 --- a/src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt +++ b/src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt @@ -45,38 +45,31 @@ private class TextureCubemap : VulkanExampleBase() { VertexComponent.NORMAL, VertexComponent.UV) - private val models = object { + object models { val skybox = Model() val objects = ArrayList() var objectIndex = 0 } - private val uniformBuffers = object { + object uniformBuffers { val `object` = Buffer() val skybox = Buffer() } - private val uboVS = object { + object uboVS : Bufferizable() { var projection = Mat4() var model = Mat4() var lodBias = 0f - val size = Mat4.size * 2 + Float.BYTES - val buffer = bufferBig(size) - val address = memAddress(buffer) - fun pack() { - projection to buffer - model.to(buffer, Mat4.size) - buffer.putFloat(Mat4.size * 2, lodBias) - } + override val fieldOrder = arrayOf("projection", "model", "lodBias") } - private val pipelines = object { + object pipelines { var skybox: VkPipeline = NULL var reflect: VkPipeline = NULL } - private val descriptorSets = object { + object descriptorSets { var `object`: VkDescriptorSet = NULL var skybox: VkDescriptorSet = NULL } @@ -368,7 +361,7 @@ private class TextureCubemap : VulkanExampleBase() { // Skybox models.skybox.loadFromFile("$assetPath/models/cube.obj", vertexLayout, 0.05f, vulkanDevice, queue) // Objects - val filenames = listOf("sphere.obj", "teapot.dae", "torusknot.obj", "venus.fbx") + val filenames = listOf("sphere.obj", "teapot.dae", "torusknot.obj") objectNames += listOf("Sphere", "Teapot", "Torusknot", "Venus") for (file in filenames) { val model = Model() @@ -533,8 +526,7 @@ private class TextureCubemap : VulkanExampleBase() { .rotateAssign(rotation.y.rad, 0f, 1f, 0f) .rotateAssign(rotation.z.rad, 0f, 0f, 1f) - uboVS.pack() - memCopy(uboVS.address, uniformBuffers.`object`.mapped[0], uboVS.size.L) + uboVS to uniformBuffers.`object`.mapped[0] // Skybox viewMatrix put 1f @@ -546,7 +538,7 @@ private class TextureCubemap : VulkanExampleBase() { .rotateAssign(rotation.y.rad, 0f, 1f, 0f) .rotateAssign(rotation.z.rad, 0f, 0f, 1f) - memCopy(uboVS.address, uniformBuffers.skybox.mapped[0], uboVS.size.L) + uboVS to uniformBuffers.skybox.mapped[0] } fun draw() { diff --git a/src/main/kotlin/vulkan/basics/09 Texture Array.kt b/src/main/kotlin/vulkan/basics/09 Texture Array.kt index 79f30e5..0a881cb 100644 --- a/src/main/kotlin/vulkan/basics/09 Texture Array.kt +++ b/src/main/kotlin/vulkan/basics/09 Texture Array.kt @@ -46,21 +46,21 @@ fun main(args: Array) { } } -/** Vertex layout for this example */ -private val Vertex = object { - // float pos[3]; -// float uv[2]; - val size = Vec3.size + Vec2.size -} - class TextureArray : VulkanExampleBase() { + /** Vertex layout for this example */ + object Vertex { + // float pos[3]; +// float uv[2]; + val size = Vec3.size + Vec2.size + } + // Number of array layers in texture array // Also used as instance count var layerCount = 0 val textureArray = Texture() - private var vertices = object { + object vertices { lateinit var inputState: VkPipelineVertexInputStateCreateInfo lateinit var bindingDescription: VkVertexInputBindingDescription lateinit var attributeDescriptions: VkVertexInputAttributeDescription.Buffer @@ -89,12 +89,12 @@ class TextureArray : VulkanExampleBase() { } } - private val uboVS = object { + object uboVS { // Global matrices - val matrices = object { + object matrices : Bufferizable() { var projection = Mat4() var view = Mat4() - val size = Mat4.size * 2 + override val fieldOrder = arrayOf("projection", "view") } // Seperate data for each instance val instance = ArrayList() @@ -108,11 +108,6 @@ class TextureArray : VulkanExampleBase() { lateinit var buffer: ByteBuffer var address = NULL - - fun pack() { - matrices.projection to buffer - matrices.view.to(buffer, Mat4.size) - } } @@ -547,9 +542,8 @@ class TextureArray : VulkanExampleBase() { .rotateAssign(rotation.y.rad, 0f, 1f, 0f) .rotateAssign(rotation.z.rad, 0f, 0f, 1f) - uboVS.pack() // Only update the matrices part of the uniform buffer - memCopy(uboVS.address, uniformBufferVS.mapped[0], uboVS.matrices.size.L) + uboVS.matrices to uniformBufferVS.mapped[0] } fun draw() {