Skip to content

Commit

Permalink
Bufferizable
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed May 6, 2018
1 parent 6780942 commit 3126da3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/vulkan/base/VulkanExampleBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/vulkan/base/VulkanModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 4 additions & 12 deletions src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/vulkan/basics/06 Specialization Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
26 changes: 8 additions & 18 deletions src/main/kotlin/vulkan/basics/07 Texture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand All @@ -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() {
Expand Down
26 changes: 9 additions & 17 deletions src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,31 @@ private class TextureCubemap : VulkanExampleBase() {
VertexComponent.NORMAL,
VertexComponent.UV)

private val models = object {
object models {
val skybox = Model()
val objects = ArrayList<Model>()
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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
30 changes: 12 additions & 18 deletions src/main/kotlin/vulkan/basics/09 Texture Array.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ fun main(args: Array<String>) {
}
}

/** 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
Expand Down Expand Up @@ -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<UboInstanceData>()
Expand All @@ -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)
}
}


Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 3126da3

Please sign in to comment.