Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed May 8, 2018
1 parent 64de888 commit bfbe75a
Show file tree
Hide file tree
Showing 18 changed files with 814 additions and 84 deletions.
4 changes: 3 additions & 1 deletion .idea/Vulkan.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ kotlin {
}
}

//compileKotlin {
// kotlinOptions {
// jvmTarget = '1.8'
// }
//}
//
//allprojects {
//
// apply plugin: 'java'
// sourceCompatibility = 1.8
// targetCompatibility = 1.8
// apply plugin: 'kotlin'
// compileKotlin {
// kotlinOptions.jvmTarget = '1.8'
// }
//}

buildscript {

ext.kotlinVersion = '1.2.41'
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/vkn/extension functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.lwjgl.system.MemoryUtil.*
import org.lwjgl.system.Pointer
import org.lwjgl.vulkan.*
import java.nio.ByteBuffer
import kotlin.reflect.KMutableProperty0


/*
Expand Down Expand Up @@ -221,6 +222,12 @@ inline infix fun VkDevice.allocateMemory(allocateInfo: VkMemoryAllocateInfo): Vk
return memGetLong(pMemory)
}

inline fun VkDevice.allocateMemory(allocateInfo: VkMemoryAllocateInfo, memory: KMutableProperty0<VkDeviceMemory>) {
val pMemory = appBuffer.long
VK_CHECK_RESULT(VK10.nvkAllocateMemory(this, allocateInfo.adr, NULL, pMemory))
memory.set(memGetLong(pMemory))
}

inline fun VkDevice.bindBufferMemory(buffer: VkBuffer, memory: VkDeviceMemory, memoryOffset: VkDeviceSize = 0) { // TODO clean code?
VK_CHECK_RESULT(VK10.vkBindBufferMemory(this, buffer, memory, memoryOffset))
}
Expand All @@ -242,6 +249,12 @@ inline infix fun VkDevice.createBuffer(createInfo: VkBufferCreateInfo): VkBuffer
return memGetLong(pBuffer)
}

inline fun VkDevice.createBuffer(createInfo: VkBufferCreateInfo, buffer: KMutableProperty0<VkBuffer>) {
val pBuffer = appBuffer.long
VK_CHECK_RESULT(VK10.nvkCreateBuffer(this, createInfo.adr, NULL, pBuffer))
buffer.set(memGetLong(pBuffer))
}

inline infix fun VkDevice.createCommandPool(createInfo: VkCommandPoolCreateInfo): VkCommandPool {
val pCommandPool = appBuffer.long
VK_CHECK_RESULT(VK10.nvkCreateCommandPool(this, createInfo.adr, NULL, pCommandPool))
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/vkn/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import glm_.mat4x4.Mat4
import glm_.set
import glm_.vec3.Vec3
import glm_.vec4.Vec4
import gln.buf
import graphics.scenery.spirvcrossj.*
import imgui.Col
import org.lwjgl.PointerBuffer
import org.lwjgl.system.MemoryUtil
import org.lwjgl.system.MemoryUtil.*
Expand All @@ -22,6 +24,7 @@ import org.lwjgl.vulkan.VK10
import org.lwjgl.vulkan.VkDevice
import org.lwjgl.vulkan.VkPhysicalDevice
import uno.buffer.bufferBig
import uno.buffer.intBufferBig
import uno.kotlin.buffers.indices
import java.nio.ByteBuffer
import java.nio.FloatBuffer
Expand Down Expand Up @@ -377,6 +380,25 @@ fun bufferOf(vararg data: Bufferizable): ByteBuffer {
return res
}

fun bufferOf(data: Collection<Bufferizable>): ByteBuffer {
val size = data.sumBy { it.size }
val res = bufferBig(size)
val address = memAddress(res)
var offset = 0
for (i in data.indices) {
data.elementAt(i) to address + offset
offset += data.elementAt(i).size
}
return res
}

fun intArrayOf(ints: Collection<Int>): IntBuffer {
val buffer = intBufferBig(ints.size)
for(i in ints.indices)
buffer[i] = ints.elementAt(i)
return buffer
}

//object uboVS : Bufferizable() {
//
// var projectionMatrix = Mat4()
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/vkn/vk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ object vk {
return InstanceCreateInfo().also(block)
}

inline fun MappedMemoryRange(): VkMappedMemoryRange {
return VkMappedMemoryRange.create(ptr.advance(VkMappedMemoryRange.SIZEOF)).apply {
type = VkStructureType.MAPPED_MEMORY_RANGE
}
}
inline fun MappedMemoryRange(block: VkMappedMemoryRange.() -> Unit): VkMappedMemoryRange {
val res = VkMappedMemoryRange.create(ptr.advance(VkMappedMemoryRange.SIZEOF))
res.type = VkStructureType.MAPPED_MEMORY_RANGE
res.block()
return res
return MappedMemoryRange().also(block)
}

inline fun MemoryAllocateInfo(block: VkMemoryAllocateInfo.() -> Unit): VkMemoryAllocateInfo {
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/vkn/vulkan enum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,9 @@ enum class VkMemoryProperty(val i: Int) {
inline infix fun or(b: VkMemoryProperty): VkMemoryPropertyFlags = i or b.i
}

inline infix fun Int.has(b: VkMemoryProperty) = and(b.i) != 0
inline infix fun Int.hasnt(b: VkMemoryProperty) = and(b.i) == 0

typealias VkMemoryPropertyFlags = VkFlags

enum class VkMemoryHeapFlag(val i: Int) {
Expand Down
85 changes: 39 additions & 46 deletions src/main/kotlin/vulkan/base/VulkanDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package vulkan.base
import glfw_.appBuffer
import glm_.L
import glm_.i
import glm_.set
import glm_.size
import org.lwjgl.system.MemoryUtil.*
import org.lwjgl.vulkan.*
import org.lwjgl.vulkan.EXTDebugMarker.VK_EXT_DEBUG_MARKER_EXTENSION_NAME
import org.lwjgl.vulkan.KHRSwapchain.VK_KHR_SWAPCHAIN_EXTENSION_NAME
import org.lwjgl.vulkan.VK10.*
import uno.buffer.floatBufferBig
import uno.buffer.intBufferBig
import uno.buffer.use
import vkn.*
import vulkan.base.tools.DEFAULT_FENCE_TIMEOUT
import java.nio.ByteBuffer
Expand Down Expand Up @@ -255,47 +251,45 @@ constructor(
*
* @return VK_SUCCESS if buffer handle and memory have been created and (optionally passed) data has been copied
*/
// fun createBuffer(usageFlags: VkBufferUsageFlags, memoryPropertyFlags: VkMemoryPropertyFlags, size: VkDeviceSize,
// memory: VkDeviceMemory, data: Long = NULL): VkBuffer {
// // Create the buffer handle
// val bufferCreateInfo = vk.BufferCreateInfo {
// usage = usageFlags
// this.size = size
// sharingMode = VkSharingMode.EXCLUSIVE
// }
// val buffer = logicalDevice!! createBuffer bufferCreateInfo
//
// // Create the memory backing up the buffer handle
// val memReqs = logicalDevice!! getBufferMemoryRequirements buffer
// val memAlloc = vk.MemoryAllocateInfo {
// allocationSize = memReqs.size
// // Find a memory type index that fits the properties of the buffer
// memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags)
// }
// VK_CHECK_RESULT(vkAllocateMemory(logicalDevice, & memAlloc, nullptr, memory));
//
// // If a pointer to the buffer data has been passed, map the buffer and copy over the data
// if (data != nullptr) {
// void * mapped;
// VK_CHECK_RESULT(vkMapMemory(logicalDevice, *memory, 0, size, 0, & mapped));
// memcpy(mapped, data, size);
// // If host coherency hasn't been requested, do a manual flush to make writes visible
// if ((memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0)
// {
// VkMappedMemoryRange mappedRange = vks ::initializers::mappedMemoryRange();
// mappedRange.memory = * memory;
// mappedRange.offset = 0;
// mappedRange.size = size;
// vkFlushMappedMemoryRanges(logicalDevice, 1, & mappedRange);
// }
// vkUnmapMemory(logicalDevice, *memory);
// }
//
// // Attach the memory to the buffer object
// VK_CHECK_RESULT(vkBindBufferMemory(logicalDevice, *buffer, *memory, 0));
//
// return VK_SUCCESS;
// }
fun createBuffer(usageFlags: VkBufferUsageFlags, memoryPropertyFlags: VkMemoryPropertyFlags, size: VkDeviceSize,
buffer: KMutableProperty0<VkBuffer>, memory: KMutableProperty0<VkDeviceMemory>, data: Long = NULL) {

val dev = logicalDevice!!
// Create the buffer handle
val bufferCreateInfo = vk.BufferCreateInfo {
usage = usageFlags
this.size = size
sharingMode = VkSharingMode.EXCLUSIVE
}
dev.createBuffer(bufferCreateInfo, buffer)

// Create the memory backing up the buffer handle
val memReqs = dev getBufferMemoryRequirements buffer()
val memAlloc = vk.MemoryAllocateInfo {
allocationSize = memReqs.size
// Find a memory type index that fits the properties of the buffer
memoryTypeIndex = getMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags)
}
dev.allocateMemory(memAlloc, memory)

// If a pointer to the buffer data has been passed, map the buffer and copy over the data
if (data != NULL)
dev.mappingMemory(memory(), 0, size) { mapped ->
memCopy(data, mapped, size)
// If host coherency hasn't been requested, do a manual flush to make writes visible
if (memoryPropertyFlags hasnt VkMemoryProperty.HOST_COHERENT_BIT) {
val mappedRange = vk.MappedMemoryRange().also {
it.memory = memory()
it.offset = 0
it.size = size
}
dev.flushMappedMemoryRanges(mappedRange)
}
}

// Attach the memory to the buffer object
dev.bindBufferMemory(buffer(), memory())
}

fun createBuffer(usageFlags: VkBufferUsageFlags, memoryPropertyFlags: VkMemoryPropertyFlags, buffer: Buffer, bytes: ByteBuffer) {
createBuffer(usageFlags, memoryPropertyFlags, buffer, bytes.size.L, memAddress(bytes))
Expand All @@ -310,7 +304,6 @@ constructor(
}



/**
* Create a buffer on the device
*
Expand Down
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 @@ -757,7 +757,7 @@ abstract class VulkanExampleBase {

/** End the command buffer, submit it to the queue and free (if requested)
* Note : Waits for the queue to become idle */
fun flushCommandBuffer(commandBuffer: VkCommandBuffer?, queue: VkQueue, free: Boolean) {
fun flushCommandBuffer(commandBuffer: VkCommandBuffer, queue: VkQueue, free: Boolean) {

if (commandBuffer == null) return

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/vulkan/basics/04 Dynamic Uniform Buffers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ private class DynamicUniformBuffers : VulkanExampleBase() {

uniformBuffers.view.destroy()
uniformBuffers.dynamic.destroy()

super.destroy()
}

override fun buildCommandBuffers() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/vulkan/basics/05 Push Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ private class PushConstants : VulkanExampleBase() {
models.scene.destroy()

uniformBuffer.destroy()

super.destroy()
}

fun reBuildCommandBuffers() {
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 @@ -104,6 +104,8 @@ class SpecializationConstants : VulkanExampleBase() {
models.cube.destroy()
textures.colormap.destroy()
uniformBuffer.destroy()

super.destroy()
}

override fun buildCommandBuffers() {
Expand Down
Loading

0 comments on commit bfbe75a

Please sign in to comment.