Skip to content

Commit

Permalink
09 Texture Array
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed Apr 29, 2018
1 parent fff7e8a commit f20de42
Show file tree
Hide file tree
Showing 14 changed files with 1,325 additions and 740 deletions.
26 changes: 15 additions & 11 deletions src/main/kotlin/vkn/extension functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ inline fun VkCommandBuffer.beginRenderPass(renderPassBegin: VkRenderPassBeginInf
VK10.nvkCmdBeginRenderPass(this, renderPassBegin.adr, contents.i)
}

inline fun VkCommandBuffer.bindDescriptorSet(pipelineBindPoint: VkPipelineBindPoint, layout: VkPipelineLayout,
descriptorSet: VkDescriptorSet, dynamicOffsets: Int? = null) {
vk.cmdBindDescriptorSet(this, pipelineBindPoint, layout, descriptorSet, dynamicOffsets)
inline fun VkCommandBuffer.bindDescriptorSets(pipelineBindPoint: VkPipelineBindPoint, layout: VkPipelineLayout,
descriptorSet: VkDescriptorSet, dynamicOffsets: Int? = null) {
vk.cmdBindDescriptorSets(this, pipelineBindPoint, layout, descriptorSet, dynamicOffsets)
}

inline fun VkCommandBuffer.bindIndexBuffer(buffer: VkBuffer, offset: VkDeviceSize, indexType: VkIndexType) {
Expand All @@ -39,7 +39,11 @@ inline fun VkCommandBuffer.bindPipeline(pipelineBindPoint: VkPipelineBindPoint,
VK10.vkCmdBindPipeline(this, pipelineBindPoint.i, pipeline)
}

inline fun VkCommandBuffer.bindVertexBuffer(firstBinding: Int, buffer: VkBuffer) {
inline infix fun VkCommandBuffer.bindVertexBuffers(buffer: VkBuffer) {
bindVertexBuffers(0, buffer)
}

inline fun VkCommandBuffer.bindVertexBuffers(firstBinding: Int, buffer: VkBuffer) {
val pBuffer = appBuffer.long
memPutLong(pBuffer, buffer)
val pOffset = appBuffer.long
Expand Down Expand Up @@ -518,7 +522,7 @@ inline infix fun VkDevice.unmapMemory(memory: VkDeviceMemory) {
VK10.vkUnmapMemory(this, memory)
}

inline infix fun VkDevice.updateDescriptorSet(descriptorWrites: VkWriteDescriptorSet) {
inline infix fun VkDevice.updateDescriptorSets(descriptorWrites: VkWriteDescriptorSet) {
VK10.nvkUpdateDescriptorSets(this, 1, descriptorWrites.adr, 0, NULL)
}

Expand Down Expand Up @@ -696,20 +700,20 @@ inline operator fun VkVertexInputBindingDescription.invoke(binding: Int, stride:
}


inline operator fun VkWriteDescriptorSet.invoke(dstSet: VkDescriptorSet, dstBinding: Int, descriptorType: VkDescriptorType,
bufferInfo: VkDescriptorBufferInfo.Buffer): VkWriteDescriptorSet {
inline operator fun VkWriteDescriptorSet.invoke(dstSet: VkDescriptorSet, descriptorType: VkDescriptorType, dstBinding: Int,
bufferInfo: VkDescriptorBufferInfo): VkWriteDescriptorSet {
this.dstSet = dstSet
this.dstBinding = dstBinding
this.descriptorType = descriptorType
this.bufferInfo = bufferInfo
this.bufferInfo_ = bufferInfo
return this
}

inline operator fun VkWriteDescriptorSet.invoke(dstSet: VkDescriptorSet, dstBinding: Int, descriptorType: VkDescriptorType,
bufferInfo: VkDescriptorBufferInfo): VkWriteDescriptorSet {
inline operator fun VkWriteDescriptorSet.invoke(dstSet: VkDescriptorSet, descriptorType: VkDescriptorType, dstBinding: Int,
imageInfo: VkDescriptorImageInfo): VkWriteDescriptorSet {
this.dstSet = dstSet
this.dstBinding = dstBinding
this.descriptorType = descriptorType
this.bufferInfo_ = bufferInfo
this.imageInfo_ = imageInfo
return this
}
20 changes: 1 addition & 19 deletions src/main/kotlin/vkn/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,7 @@ private fun IntVec.toByteBuffer(): ByteBuffer {
}


inline fun VkDescriptorPoolSize.Buffer.appyAt(index: Int, block: VkDescriptorPoolSize.() -> Unit): VkDescriptorPoolSize.Buffer {
get(index).block()
return this
}

inline fun VkSubpassDependency.Buffer.appyAt(index: Int, block: VkSubpassDependency.() -> Unit): VkSubpassDependency.Buffer {
get(index).block()
return this
}

inline fun VkVertexInputAttributeDescription.Buffer.appyAt(index: Int, block: VkVertexInputAttributeDescription.() -> Unit): VkVertexInputAttributeDescription.Buffer {
get(index).block()
return this
}

operator fun <T : Struct, SELF : StructBuffer<T, SELF>> StructBuffer<T, SELF>.set(index: Int, value: T) {
put(index, value)
}

inline fun <T, C : Iterable<T>> C.applyOnEach(action: T.() -> Unit): C = onEach(action)

fun getResource(resource: String): URL = ClassLoader.getSystemResource(resource)
}
10 changes: 6 additions & 4 deletions src/main/kotlin/vkn/vk alloc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ inline fun cVkPipelineShaderStageCreateInfo(block: VkPipelineShaderStageCreateIn
return res
}

inline fun cVkPipelineVertexInputStateCreateInfo(): VkPipelineVertexInputStateCreateInfo {
return VkPipelineVertexInputStateCreateInfo.calloc().apply {
type = VkStructureType.PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
}
}
inline fun cVkPipelineVertexInputStateCreateInfo(block: VkPipelineVertexInputStateCreateInfo.() -> Unit): VkPipelineVertexInputStateCreateInfo {
val res = VkPipelineVertexInputStateCreateInfo.calloc()
res.type = VkStructureType.PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO
res.block()
return res
return cVkPipelineVertexInputStateCreateInfo().also(block)
}
inline fun cVkPipelineViewportStateCreateInfo(block: VkPipelineViewportStateCreateInfo.() -> Unit): VkPipelineViewportStateCreateInfo {
val res = VkPipelineViewportStateCreateInfo.calloc()
Expand Down
114 changes: 101 additions & 13 deletions src/main/kotlin/vkn/vk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object vk {
res.type = VkStructureType.COMMAND_BUFFER_BEGIN_INFO
return res
}

inline fun CommandBufferBeginInfo(block: VkCommandBufferBeginInfo.() -> Unit): VkCommandBufferBeginInfo {
val res = CommandBufferBeginInfo()
res.block()
Expand Down Expand Up @@ -383,12 +384,16 @@ object vk {
return res
}

inline fun WriteDescriptorSet(capacity: Int, block: VkWriteDescriptorSet.() -> Unit): VkWriteDescriptorSet.Buffer = WriteDescriptorSet(capacity).also { it[0].block() }
inline fun WriteDescriptorSet(capacity: Int, block: VkWriteDescriptorSet.Buffer.() -> Unit): VkWriteDescriptorSet.Buffer = WriteDescriptorSet(capacity).also(block)

inline fun DescriptorImageInfo(capacity: Int): VkDescriptorImageInfo.Buffer {
return VkDescriptorImageInfo.create(ptr.advance(VkDescriptorImageInfo.SIZEOF * capacity), capacity)
}

inline fun DescriptorImageInfo(block: VkDescriptorImageInfo.() -> Unit): VkDescriptorImageInfo {
return VkDescriptorImageInfo.create(ptr.advance(VkDescriptorImageInfo.SIZEOF)).also(block)
}

inline fun DescriptorImageInfo(capacity: Int, block: VkDescriptorImageInfo.() -> Unit): VkDescriptorImageInfo.Buffer = DescriptorImageInfo(capacity).also { it[0].block() }


Expand All @@ -413,10 +418,10 @@ object vk {

inline fun DescriptorPoolSize(block: VkDescriptorPoolSize.() -> Unit): VkDescriptorPoolSize = VkDescriptorPoolSize.create(ptr.advance(VkDescriptorPoolSize.SIZEOF)).also(block)
inline fun DescriptorPoolSize(capacity: Int): VkDescriptorPoolSize.Buffer = VkDescriptorPoolSize.create(ptr.advance(VkDescriptorPoolSize.SIZEOF * capacity), capacity)
inline fun DescriptorPoolSize(capacity: Int, block: VkDescriptorPoolSize.() -> Unit): VkDescriptorPoolSize.Buffer = DescriptorPoolSize(capacity).also { it[0].block() }
inline fun DescriptorPoolSize(capacity: Int, block: VkDescriptorPoolSize.Buffer.() -> Unit): VkDescriptorPoolSize.Buffer = DescriptorPoolSize(capacity).also(block)

inline fun DescriptorSetLayoutBinding(capacity: Int): VkDescriptorSetLayoutBinding.Buffer = VkDescriptorSetLayoutBinding.create(ptr.advance(VkDescriptorSetLayoutBinding.SIZEOF * capacity), capacity)
inline fun DescriptorSetLayoutBinding(capacity: Int, block: VkDescriptorSetLayoutBinding.() -> Unit): VkDescriptorSetLayoutBinding.Buffer = DescriptorSetLayoutBinding(capacity).also { it[0].block() }
inline fun DescriptorSetLayoutBinding(capacity: Int, block: VkDescriptorSetLayoutBinding.Buffer.() -> Unit): VkDescriptorSetLayoutBinding.Buffer = DescriptorSetLayoutBinding(capacity).also(block)
inline fun DescriptorSetLayoutBinding(block: VkDescriptorSetLayoutBinding.() -> Unit): VkDescriptorSetLayoutBinding = VkDescriptorSetLayoutBinding.create(ptr.advance(VkDescriptorSetLayoutBinding.SIZEOF)).also(block)

inline fun ExtensionProperties(capacity: Int): VkExtensionProperties.Buffer = VkExtensionProperties.create(ptr.advance(VkExtensionProperties.SIZEOF * capacity), capacity)
Expand Down Expand Up @@ -450,7 +455,7 @@ object vk {

inline fun SubpassDescription(capacity: Int, block: VkSubpassDescription.() -> Unit): VkSubpassDescription.Buffer = VkSubpassDescription.create(ptr.advance(VkSubpassDescription.SIZEOF * capacity), capacity).also { it[0].block() }

inline fun SubresourceLayout(): VkSubresourceLayout = VkSubresourceLayout.create(ptr.advance(VkSubresourceLayout.SIZEOF ))
inline fun SubresourceLayout(): VkSubresourceLayout = VkSubresourceLayout.create(ptr.advance(VkSubresourceLayout.SIZEOF))
inline fun SubresourceLayout(capacity: Int, block: VkSubresourceLayout.() -> Unit): VkSubresourceLayout.Buffer = VkSubresourceLayout.create(ptr.advance(VkSubresourceLayout.SIZEOF * capacity), capacity).also { it[0].block() }

inline fun SurfaceCapabilitiesKHR(block: VkSurfaceCapabilitiesKHR.() -> Unit): VkSurfaceCapabilitiesKHR = VkSurfaceCapabilitiesKHR.create(ptr.advance(VkSurfaceCapabilitiesKHR.SIZEOF)).also(block)
Expand All @@ -472,31 +477,91 @@ object vk {
Function Constructors
*/

inline fun DescriptorImageInfo(sampler: VkSampler, imageView: VkImageView, imageLayout: VkImageLayout): VkDescriptorImageInfo {
return DescriptorImageInfo {
this.sampler = sampler
this.imageView = imageView
this.imageLayout = imageLayout
}
}

inline fun DescriptorPoolCreateInfo(poolSize: VkDescriptorPoolSize, maxSets: Int): VkDescriptorPoolCreateInfo {
return DescriptorPoolCreateInfo {
this.poolSize = poolSize
this.maxSets = maxSets
}
}

inline fun DescriptorPoolCreateInfo(poolSizes: VkDescriptorPoolSize.Buffer, maxSets: Int): VkDescriptorPoolCreateInfo {
return DescriptorPoolCreateInfo {
this.poolSizes = poolSizes
this.maxSets = maxSets
}
}

inline fun DescriptorPoolSize(type: VkDescriptorType, descriptorCount: Int): VkDescriptorPoolSize {
return DescriptorPoolSize {
this.type = type
this.descriptorCount = descriptorCount
}
}

inline fun DescriptorPoolSize(
type0: VkDescriptorType, descriptorCount0: Int,
type1: VkDescriptorType, descriptorCount1: Int): VkDescriptorPoolSize.Buffer {
return DescriptorPoolSize(2) {
this[0].apply {
type = type0
descriptorCount = descriptorCount0
}
this[1].apply {
type = type1
descriptorCount = descriptorCount1
}
}
}

inline fun DescriptorSetAllocateInfo(descriptorPool: VkDescriptorPool, setLayout: VkDescriptorSetLayout): VkDescriptorSetAllocateInfo {
return DescriptorSetAllocateInfo {
this.descriptorPool = descriptorPool
this.setLayout = setLayout
}
}

inline fun DescriptorSetLayoutBinding(type: VkDescriptorType, stageFlags: VkShaderStageFlags, binding: Int): VkDescriptorSetLayoutBinding {
inline fun DescriptorSetLayoutBinding(type: VkDescriptorType, stageFlags: VkShaderStageFlags, binding: Int, descriptorCount: Int = 1): VkDescriptorSetLayoutBinding {
return DescriptorSetLayoutBinding {
descriptorType = type
this.stageFlags = stageFlags
this.binding = binding
this.descriptorCount = descriptorCount
}
}

inline fun DescriptorSetLayoutBinding(
type0: VkDescriptorType, stageFlags0: VkShaderStageFlags, binding0: Int,
type1: VkDescriptorType, stageFlags1: VkShaderStageFlags, binding1: Int): VkDescriptorSetLayoutBinding.Buffer {

return DescriptorSetLayoutBinding(
type0, stageFlags0, binding0, 1,
type1, stageFlags1, binding1, 1)
}

inline fun DescriptorSetLayoutBinding(
type0: VkDescriptorType, stageFlags0: VkShaderStageFlags, binding0: Int, descriptorCount0: Int,
type1: VkDescriptorType, stageFlags1: VkShaderStageFlags, binding1: Int, descriptorCount1: Int): VkDescriptorSetLayoutBinding.Buffer {
return DescriptorSetLayoutBinding(2) {
this[0].apply {
descriptorType = type0
stageFlags = stageFlags0
binding = binding0
descriptorCount = descriptorCount0
}
this[1].apply {
descriptorType = type1
stageFlags = stageFlags1
binding = binding1
descriptorCount = descriptorCount1
}
}
}

Expand All @@ -506,6 +571,12 @@ object vk {
}
}

inline fun DescriptorSetLayoutCreateInfo(bindings: VkDescriptorSetLayoutBinding.Buffer): VkDescriptorSetLayoutCreateInfo {
return DescriptorSetLayoutCreateInfo {
this.bindings = bindings
}
}

inline fun GraphicsPipelineCreateInfo(layout: VkPipelineLayout, renderPass: VkRenderPass, flags: VkPipelineCreateFlags = 0): VkGraphicsPipelineCreateInfo {
return GraphicsPipelineCreateInfo {
this.layout = layout
Expand Down Expand Up @@ -543,12 +614,10 @@ object vk {

inline fun PipelineDynamicStateCreateInfo(dynamicStates: Collection<VkDynamicState>, flags: VkPipelineDynamicStateCreateFlags = 0): VkPipelineDynamicStateCreateInfo {
return PipelineDynamicStateCreateInfo {
this.dynamicStates = dynamicStates.run {
appBuffer.intBuffer(size).also {
for (i in indices)
it[i] = elementAt(i).i
}
}
val buf = appBuffer.intBuffer(dynamicStates.size)
for (i in dynamicStates.indices)
buf[i] = dynamicStates.elementAt(i).i
this.dynamicStates = buf
this.flags = flags
}
}
Expand Down Expand Up @@ -638,6 +707,25 @@ object vk {
}
}

inline fun WriteDescriptorSet(
dstSet0: VkDescriptorSet, type0: VkDescriptorType, binding0: Int, bufferInfo0: VkDescriptorBufferInfo,
dstSet1: VkDescriptorSet, type1: VkDescriptorType, binding1: Int, imageInfo1: VkDescriptorImageInfo): VkWriteDescriptorSet.Buffer {
return WriteDescriptorSet(2) {
this[0].apply {
this.dstSet = dstSet0
descriptorType = type0
dstBinding = binding0
bufferInfo_ = bufferInfo0
}
this[1].apply {
this.dstSet = dstSet1
descriptorType = type1
dstBinding = binding1
imageInfo_ = imageInfo1
}
}
}


/*
Full Constructors
Expand Down Expand Up @@ -835,8 +923,8 @@ object vk {
VK10.nvkCmdBeginRenderPass(commandBuffer, renderPassBegin.adr, contents.i)
}

inline fun cmdBindDescriptorSet(commandBuffer: VkCommandBuffer, pipelineBindPoint: VkPipelineBindPoint, layout: VkPipelineLayout,
descriptorSet: VkDescriptorSet, dynamicOffsets: Int? = null) {
inline fun cmdBindDescriptorSets(commandBuffer: VkCommandBuffer, pipelineBindPoint: VkPipelineBindPoint, layout: VkPipelineLayout,
descriptorSet: VkDescriptorSet, dynamicOffsets: Int? = null) {
val pDescriptorSets = appBuffer.long
memPutLong(pDescriptorSets, descriptorSet)
val dynamicOffsetCount: Int
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/vulkan/base/Vulkan Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Buffer {
lateinit var device: VkDevice
var buffer: VkBuffer = NULL
var memory: VkDeviceMemory = NULL
val descriptor = VkDescriptorBufferInfo.calloc()
val descriptor: VkDescriptorBufferInfo = VkDescriptorBufferInfo.calloc()
var size: VkDeviceSize = 0
var alignment: VkDeviceSize = 0
var mapped: PointerBuffer = memCallocPointer(1)
Expand All @@ -48,9 +48,9 @@ class Buffer {
device.mapMemory(memory, offset, size, 0, mapped.adr)
}

fun mapping(size: VkDeviceSize = VK_WHOLE_SIZE, offset: VkDeviceSize = 0, block: () -> Unit) {
fun mapping(size: VkDeviceSize = VK_WHOLE_SIZE, offset: VkDeviceSize = 0, block: (Long) -> Unit) {
map(size, offset)
block()
block(mapped[0])
unmap()
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/vulkan/base/VulkanDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ constructor(
}
// If a pointer to the buffer data has been passed, map the buffer and copy over the data
if (data != NULL)
buffer.mapping {
memCopy(data, buffer.mapped[0], size)
}
buffer.mapping { dst -> memCopy(data, dst, size) }

// Initialize a default descriptor that covers the whole buffer size
buffer.setupDescriptor()
Expand Down
8 changes: 2 additions & 6 deletions src/main/kotlin/vulkan/base/VulkanModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ class Model {
this.device = device.logicalDevice

val importer = Importer()
val scene = importer.readFile(filename, flags)
if (scene == null) {
val error = importer.errorString
tools.exitFatal("$error\n\nThe file may be part of the additional asset pack.\n\nRun \"download_assets.py\" " +
"in the repository root to download the latest version.", -1)
}
val scene = importer.readFile(filename, flags) ?: tools.exitFatal("${importer.errorString}\n\n" +
"The file may be part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.", -1)


parts.clear()
Expand Down
Loading

0 comments on commit f20de42

Please sign in to comment.