Skip to content

Commit

Permalink
01 compute
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed Apr 29, 2018
1 parent f20de42 commit 86d550c
Show file tree
Hide file tree
Showing 7 changed files with 757 additions and 49 deletions.
33 changes: 23 additions & 10 deletions src/main/kotlin/vkn/direct fields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1550,16 +1550,29 @@ inline var VkGraphicsPipelineCreateInfo.basePipelineIndex
get() = VkGraphicsPipelineCreateInfo.nbasePipelineIndex(adr)
set(value) = VkGraphicsPipelineCreateInfo.nbasePipelineIndex(adr, value)

//
//typedef struct VkComputePipelineCreateInfo {
// VkStructureType sType;
// const void* pNext;
// VkPipelineCreateFlags flags;
// VkPipelineShaderStageCreateInfo stage;
// VkPipelineLayout layout;
// VkPipeline basePipelineHandle;
// int32_t basePipelineIndex;
//} VkComputePipelineCreateInfo;

inline var VkComputePipelineCreateInfo.type: VkStructureType
get() = VkStructureType of VkComputePipelineCreateInfo.nsType(adr)
set(value) = VkComputePipelineCreateInfo.nsType(adr, value.i)
inline var VkComputePipelineCreateInfo.next: Long
get() = VkComputePipelineCreateInfo.npNext(adr)
set(value) = VkComputePipelineCreateInfo.npNext(adr, value)
inline var VkComputePipelineCreateInfo.flags: VkPipelineCreateFlags
get() = VkComputePipelineCreateInfo.nflags(adr)
set(value) = VkComputePipelineCreateInfo.nflags(adr, value)
inline var VkComputePipelineCreateInfo.stage: VkPipelineShaderStageCreateInfo
get() = VkComputePipelineCreateInfo.nstage(adr)
set(value) = VkComputePipelineCreateInfo.nstage(adr, value)
inline var VkComputePipelineCreateInfo.layout: VkPipelineLayout
get() = VkComputePipelineCreateInfo.nlayout(adr)
set(value) = VkComputePipelineCreateInfo.nlayout(adr, value)
inline var VkComputePipelineCreateInfo.basePipelineHandle: VkPipeline
get() = VkComputePipelineCreateInfo.nbasePipelineHandle(adr)
set(value) = VkComputePipelineCreateInfo.nbasePipelineHandle(adr, value)
inline var VkComputePipelineCreateInfo.basePipelineIndex: Int
get() = VkComputePipelineCreateInfo.nbasePipelineIndex(adr)
set(value) = VkComputePipelineCreateInfo.nbasePipelineIndex(adr, value)


inline var VkPushConstantRange.stageFlags: VkShaderStageFlags
get() = VkPushConstantRange.nstageFlags(adr)
Expand Down
14 changes: 12 additions & 2 deletions src/main/kotlin/vkn/extension functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ inline infix fun VkCommandBuffer.setViewport(size: Vec2i) {
}

inline fun VkCommandBuffer.setViewport(size: Vec2i, minDepth: Float, maxDepth: Float) {
setViewport(size.x.f, size.y.f, minDepth, maxDepth)
}

inline fun VkCommandBuffer.setViewport(width: Float, height: Float, minDepth: Float = 0f, maxDepth: Float = 1f) {
setViewport(vk.Viewport {
width = size.x.f
height = size.y.f
this.width = width
this.height = height
this.minDepth = minDepth
this.maxDepth = maxDepth
})
Expand Down Expand Up @@ -226,6 +230,12 @@ inline infix fun VkDevice.createCommandPool(createInfo: VkCommandPoolCreateInfo)
return memGetLong(pCommandPool)
}

inline fun VkDevice.createComputePipelines(pipelineCache: VkPipelineCache, createInfo: VkComputePipelineCreateInfo): VkPipeline {
val pPipeline = appBuffer.long
VK_CHECK_RESULT(VK10.nvkCreateComputePipelines(this, pipelineCache, 1, createInfo.adr, NULL, pPipeline))
return memGetLong(pPipeline)
}

inline infix fun VkDevice.createDescriptorPool(createInfo: VkDescriptorPoolCreateInfo): VkDescriptorPool {
val pDescriptorPool = appBuffer.long
VK_CHECK_RESULT(VK10.nvkCreateDescriptorPool(this, createInfo.adr, NULL, pDescriptorPool))
Expand Down
65 changes: 58 additions & 7 deletions src/main/kotlin/vkn/vk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import glm_.vec2.Vec2i
import org.lwjgl.system.MemoryUtil
import org.lwjgl.system.MemoryUtil.*
import org.lwjgl.system.Pointer
import org.lwjgl.system.Struct
import org.lwjgl.vulkan.*
import org.lwjgl.vulkan.VK10.VK_QUEUE_FAMILY_IGNORED
import vkn.VkPhysicalDeviceArrayList.resize
Expand Down Expand Up @@ -69,6 +70,13 @@ object vk {
return res
}

inline fun ComputePipelineCreateInfo(block: VkComputePipelineCreateInfo.() -> Unit): VkComputePipelineCreateInfo {
val res = VkComputePipelineCreateInfo.create(ptr.advance(VkComputePipelineCreateInfo.SIZEOF))
res.type = VkStructureType.COMPUTE_PIPELINE_CREATE_INFO
res.block()
return res
}

inline fun DebugReportCallbackCreateInfoEXT(block: VkDebugReportCallbackCreateInfoEXT.() -> Unit): VkDebugReportCallbackCreateInfoEXT {
val res = VkDebugReportCallbackCreateInfoEXT.create(ptr.advance(VkDebugReportCallbackCreateInfoEXT.SIZEOF))
res.type = VkStructureType.DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT
Expand Down Expand Up @@ -477,6 +485,13 @@ object vk {
Function Constructors
*/

inline fun ComputePipelineCreateInfo(layout: VkPipelineLayout, flags: VkPipelineCreateFlags = 0): VkComputePipelineCreateInfo {
return ComputePipelineCreateInfo {
this.layout = layout
this.flags = flags
}
}

inline fun DescriptorImageInfo(sampler: VkSampler, imageView: VkImageView, imageLayout: VkImageLayout): VkDescriptorImageInfo {
return DescriptorImageInfo {
this.sampler = sampler
Expand Down Expand Up @@ -521,6 +536,26 @@ object vk {
}
}

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

inline fun DescriptorSetAllocateInfo(descriptorPool: VkDescriptorPool, setLayout: VkDescriptorSetLayout): VkDescriptorSetAllocateInfo {
return DescriptorSetAllocateInfo {
this.descriptorPool = descriptorPool
Expand Down Expand Up @@ -577,6 +612,12 @@ object vk {
}
}

inline fun FenceCreateInfo( flags: VkFenceCreateFlags = 0): VkFenceCreateInfo {
return FenceCreateInfo {
this.flags = flags
}
}

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

inline fun PipelineLayoutCreateInfo(setLayout: VkDescriptorSetLayout): VkPipelineLayoutCreateInfo {
return PipelineLayoutCreateInfo {
setLayouts = appBuffer longBufferOf setLayout
this.setLayout = setLayout
}
}

Expand Down Expand Up @@ -690,9 +731,13 @@ object vk {
}

inline fun Viewport(size: Vec2i, minDepth: Float = 0f, maxDepth: Float = 1f): VkViewport {
return Viewport(size.x.f, size.y.f, minDepth, maxDepth)
}

inline fun Viewport(width: Float, height: Float, minDepth: Float = 0f, maxDepth: Float = 1f): VkViewport {
return Viewport {
width = size.x.f
height = size.y.f
this.width = width
this.height = height
this.minDepth = minDepth
this.maxDepth = maxDepth
}
Expand All @@ -708,20 +753,26 @@ object vk {
}

inline fun WriteDescriptorSet(
dstSet0: VkDescriptorSet, type0: VkDescriptorType, binding0: Int, bufferInfo0: VkDescriptorBufferInfo,
dstSet1: VkDescriptorSet, type1: VkDescriptorType, binding1: Int, imageInfo1: VkDescriptorImageInfo): VkWriteDescriptorSet.Buffer {
dstSet0: VkDescriptorSet, type0: VkDescriptorType, binding0: Int, info0: Struct,
dstSet1: VkDescriptorSet, type1: VkDescriptorType, binding1: Int, info1: Struct): VkWriteDescriptorSet.Buffer {
return WriteDescriptorSet(2) {
this[0].apply {
this.dstSet = dstSet0
descriptorType = type0
dstBinding = binding0
bufferInfo_ = bufferInfo0
if (info0 is VkDescriptorBufferInfo)
bufferInfo_ = info0
else
imageInfo_ = info0 as VkDescriptorImageInfo
}
this[1].apply {
this.dstSet = dstSet1
descriptorType = type1
dstBinding = binding1
imageInfo_ = imageInfo1
if (info1 is VkDescriptorBufferInfo)
bufferInfo_ = info1
else
imageInfo_ = info1 as VkDescriptorImageInfo
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/vulkan/base/VulkanDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,29 @@ constructor(

queueFamilyIndices.compute =
if (requestedQueueTypes has VkQueueFlag.COMPUTE_BIT) { // Dedicated compute queue
if (queueFamilyIndices.compute != queueFamilyIndices.graphics) {
val compute = getQueueFamilyIndex(VkQueueFlag.COMPUTE_BIT)
if (compute != queueFamilyIndices.graphics) {
// If compute family index differs, we need an additional queue create info for the compute queue
queueCreateInfos += vk.DeviceQueueCreateInfo {
queueFamilyIndex = queueFamilyIndices.compute
queueFamilyIndex = compute
queuePriorities = defaultQueuePriority
}
}
getQueueFamilyIndex(VkQueueFlag.COMPUTE_BIT)
compute
} else queueFamilyIndices.graphics // Else we use the same queue


queueFamilyIndices.transfer =
if (requestedQueueTypes has VkQueueFlag.TRANSFER_BIT) { // Dedicated transfer queue
if (queueFamilyIndices.transfer != queueFamilyIndices.graphics && queueFamilyIndices.transfer != queueFamilyIndices.compute) {
val transfer = getQueueFamilyIndex(VkQueueFlag.TRANSFER_BIT)
if (transfer != queueFamilyIndices.graphics && transfer != queueFamilyIndices.compute) {
// If compute family index differs, we need an additional queue create info for the compute queue
queueCreateInfos += vk.DeviceQueueCreateInfo {
queueFamilyIndex = queueFamilyIndices.transfer
queueFamilyIndex = transfer
queuePriorities = defaultQueuePriority
}
}
getQueueFamilyIndex(VkQueueFlag.TRANSFER_BIT)
transfer
} else queueFamilyIndices.graphics // Else we use the same queue

// Create the logical device representation
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/vulkan/base/VulkanExampleBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,10 @@ abstract class VulkanExampleBase {

/** Create swap chain images */
fun setupSwapChain() = swapChain.create(size, settings.vsync)
//
// // Check if command buffers are valid (!= VK_NULL_HANDLE)
// bool checkCommandBuffers();

/** Check if command buffers are valid (!= NULL) */
fun checkCommandBuffers() = drawCmdBuffers.all { it.adr != NULL }

/** Create command buffers for drawing commands */
fun createCommandBuffers() {
// Create one command buffer for each swap chain image and reuse for rendering
Expand Down Expand Up @@ -824,7 +825,7 @@ abstract class VulkanExampleBase {
throw RuntimeException("glslang failed to initialize.")
spirvCrossLoaded = true
}

type = VkStructureType.PIPELINE_SHADER_STAGE_CREATE_INFO
this.stage = stage
module = when {
isSpirV -> device loadShader fileName
Expand Down
40 changes: 20 additions & 20 deletions src/main/kotlin/vulkan/basics/09 Texture Array.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package vulkan.basics

import glfw_.appBuffer
import gli_.Texture2dArray
import gli_.gli
import glm_.L
Expand Down Expand Up @@ -60,9 +61,9 @@ class TextureArray : VulkanExampleBase() {
val textureArray = Texture()

private var vertices = object {
val inputState = cVkPipelineVertexInputStateCreateInfo()
val bindingDescription = VkVertexInputBindingDescription.calloc()
val attributeDescriptions = VkVertexInputAttributeDescription.calloc(2)
lateinit var inputState: VkPipelineVertexInputStateCreateInfo
lateinit var bindingDescription: VkVertexInputBindingDescription
lateinit var attributeDescriptions: VkVertexInputAttributeDescription.Buffer
}

val vertexBuffer = Buffer()
Expand Down Expand Up @@ -365,14 +366,14 @@ class TextureArray : VulkanExampleBase() {

fun generateQuad() {
// Setup vertices for a single uv-mapped quad made from two triangles
val vertices = floatBufferOf(
val vertices = appBuffer.floatBufferOf(
+2.5f, +2.5f, 0f, 1f, 1f,
-2.5f, +2.5f, 0f, 0f, 1f,
-2.5f, -2.5f, 0f, 0f, 0f,
+2.5f, -2.5f, 0f, 1f, 0f)

// Setup indices
val indices = intBufferOf(0, 1, 2, 2, 3, 0)
val indices = appBuffer.intBufferOf(0, 1, 2, 2, 3, 0)
indexCount = indices.capacity

// Create buffers
Expand All @@ -393,16 +394,17 @@ class TextureArray : VulkanExampleBase() {

fun setupVertexDescriptions() {
// Binding description
vertices.bindingDescription(VERTEX_BUFFER_BIND_ID, Vertex.size, VkVertexInputRate.VERTEX)
vertices.bindingDescription = vk.VertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, Vertex.size, VkVertexInputRate.VERTEX)

// Attribute descriptions
// Describes memory layout and shader positions
// Location 0 : Position
vertices.attributeDescriptions[0](0, VERTEX_BUFFER_BIND_ID, VkFormat.R32G32B32_SFLOAT, 0)
// Location 1 : Texture coordinates
vertices.attributeDescriptions[1](1, VERTEX_BUFFER_BIND_ID, VkFormat.R32G32_SFLOAT, Vec3.size)
vertices.attributeDescriptions = vk.VertexInputAttributeDescription(
// Location 0 : Position
VERTEX_BUFFER_BIND_ID, 0, VkFormat.R32G32B32_SFLOAT, 0,
// Location 1 : Texture coordinates
VERTEX_BUFFER_BIND_ID, 1, VkFormat.R32G32_SFLOAT, Vec3.size)

vertices.inputState.apply {
vertices.inputState = vk.PipelineVertexInputStateCreateInfo {
vertexBindingDescription = vertices.bindingDescription
vertexAttributeDescriptions = vertices.attributeDescriptions
}
Expand Down Expand Up @@ -445,12 +447,11 @@ class TextureArray : VulkanExampleBase() {
// Image descriptor for the texture array
val textureDescriptor = vk.DescriptorImageInfo(textureArray.sampler, textureArray.view, textureArray.imageLayout)

val writeDescriptorSets = vk.WriteDescriptorSet(2).also {
val writeDescriptorSets = vk.WriteDescriptorSet(
// Binding 0 : Vertex shader uniform buffer
it[0](descriptorSet, VkDescriptorType.UNIFORM_BUFFER, 0, uniformBufferVS.descriptor)
descriptorSet, VkDescriptorType.UNIFORM_BUFFER, 0, uniformBufferVS.descriptor,
// Binding 1 : Fragment shader cubemap sampler
it[1](descriptorSet, VkDescriptorType.COMBINED_IMAGE_SAMPLER, 1, textureDescriptor)
}
descriptorSet, VkDescriptorType.COMBINED_IMAGE_SAMPLER, 1, textureDescriptor)

device updateDescriptorSets writeDescriptorSets
}
Expand Down Expand Up @@ -551,17 +552,17 @@ class TextureArray : VulkanExampleBase() {
memCopy(uboVS.address, uniformBufferVS.mapped[0], uboVS.matrices.size.L)
}

fun draw() {
fun draw() {

super.prepareFrame()

submitInfo.commandBuffer = drawCmdBuffers [currentBuffer]
submitInfo.commandBuffer = drawCmdBuffers[currentBuffer]
queue submit submitInfo

super.submitFrame()
}

override fun prepare() {
override fun prepare() {

super.prepare()
loadTextures()
Expand All @@ -577,8 +578,7 @@ class TextureArray : VulkanExampleBase() {
window.show()
}

override fun render()
{
override fun render() {
if (!prepared)
return
draw()
Expand Down
Loading

0 comments on commit 86d550c

Please sign in to comment.