Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed May 7, 2018
1 parent baea143 commit 885b903
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 387 deletions.
7 changes: 5 additions & 2 deletions src/main/kotlin/vkn/direct fields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ inline var VkImageCreateInfo.extent: VkExtent3D

/** JVM custom */
inline fun VkImageCreateInfo.extent(extent: Vec3i) {
extent.set(extent.x, extent.y, extent.z)
this.extent.set(extent.x, extent.y, extent.z)
}

inline var VkImageCreateInfo.mipLevels
Expand Down Expand Up @@ -2231,7 +2231,10 @@ inline var VkBufferImageCopy.imageOffset: VkOffset3D
inline var VkBufferImageCopy.imageExtent: VkExtent3D
get() = VkBufferImageCopy.nimageExtent(adr)
set(value) = VkBufferImageCopy.nimageExtent(adr, value)

/** JVM custom */
inline fun VkBufferImageCopy.imageExtent(extent: Vec3i) {
this.imageExtent.set(extent.x, extent.y, extent.z)
}

//typedef union VkClearColorValue {
// float float32[4];
Expand Down
58 changes: 53 additions & 5 deletions src/main/kotlin/vkn/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.nio.file.Path
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.defaultType
import kotlin.reflect.full.findAnnotation


//fun pointerBufferOf(vararg strings: String): PointerBuffer {
Expand Down Expand Up @@ -299,10 +300,25 @@ object WithAddress {

abstract class Bufferizable {

abstract val fieldOrder: Array<String>
@Retention(AnnotationRetention.RUNTIME)
annotation class Order(val value: Int)

val fieldOrderDefault: Array<String> by lazy {
val properties = this::class.declaredMemberProperties
val parts = properties.partition { it.findAnnotation<Order>() == null }
val plain = parts.first.sortedBy { it.name }
val annotated = parts.second.associateBy { it.findAnnotation<Order>()!!.value }
val list = ArrayList<KProperty1<*, *>>()
var plainIdx = 0
for (i in properties.indices)
list += annotated[i] ?: plain[plainIdx++]
list.map { it.name }.toTypedArray()
}

open val size: Int by lazy {
open var fieldOrder = emptyArray<String>()
get() = if (field.isEmpty()) fieldOrderDefault else field

open val size: Int by lazy {
fieldOrder.sumBy { field ->
val member = this::class.declaredMemberProperties.find { it.name == field }!!
when (member.returnType) {
Expand All @@ -329,7 +345,6 @@ abstract class Bufferizable {
infix fun from(address: Long): Unit = TODO()

val data: Array<BufferizableData> by lazy {

Array(fieldOrder.size) {
val field = fieldOrder[it]
val member = this::class.declaredMemberProperties.find { it.name == field }!!
Expand All @@ -355,7 +370,7 @@ fun bufferOf(vararg data: Bufferizable): ByteBuffer {
val res = bufferBig(size)
val address = memAddress(res)
var offset = 0
for(i in data.indices) {
for (i in data.indices) {
data[i] to address + offset
offset += data[i].size
}
Expand Down Expand Up @@ -383,4 +398,37 @@ fun bufferOf(vararg data: Bufferizable): ByteBuffer {
// println(member.returnType)
// println(member.get(uboVS) as Mat4)
// println(member.returnType == Mat4::class.defaultType)
//}
//}

class FiledOrder {
@Retention(AnnotationRetention.RUNTIME)
annotation class Order(val value: Int)

@Order(3)
var field1: Int = 0
@Order(1)
var field2: Int = 0
// no annotation
var field4: Int = 0
var field3: Int = 0

@Order(1)
fun start() {
}

@Order(2)
fun end() {
}
}

fun main(args: Array<String>) {
val properties = FiledOrder::class.declaredMemberProperties
val parts = properties.partition { it.findAnnotation<FiledOrder.Order>() == null }
val plain = parts.first.sortedBy { it.name }
val annotated = parts.second.associateBy { it.findAnnotation<FiledOrder.Order>()!!.value }
val list = ArrayList<KProperty1<*, *>>()
var plainIdx = 0
for (i in properties.indices)
list += annotated[i] ?: plain[plainIdx++]
println(list)
}
3 changes: 1 addition & 2 deletions src/main/kotlin/vulkan/basics/01b Triangle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ private class Triangle : VulkanExampleBase() {
object uboVS : Bufferizable() {

var projectionMatrix = Mat4()
@Order(1)
var modelMatrix = Mat4()
var viewMatrix = Mat4()

override val fieldOrder = arrayOf("projectionMatrix", "modelMatrix", "viewMatrix")
}

/** The pipeline layout is used by a pipline to access the descriptor sets
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vulkan/basics/02 Pipelines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ private class Pipelines : VulkanExampleBase() {
/** Same uniform buffer layout as shader */
object uboVS : Bufferizable() {
var projection = Mat4()
@Order(1)
var modelView = Mat4()
@Order(2)
val lightPos = Vec4(0f, 2f, 1f, 0f)

override val fieldOrder = arrayOf("projection", "modelView", "lightPos")
}

var pipelineLayout: VkPipelineLayout = NULL
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/vulkan/basics/03 Descriptor Sets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private class DescriptorSets : VulkanExampleBase() {
var view = Mat4()
var model = Mat4()

override val fieldOrder = arrayOf("projection", "view", "model")
override var fieldOrder = arrayOf("projection", "view", "model")
}

val matrices = Matrices()
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/vulkan/basics/04 Dynamic Uniform Buffers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ private class DynamicUniformBuffers : VulkanExampleBase() {
}

object uboVS : Bufferizable() {

val projection = Mat4()
val view = Mat4()

override val fieldOrder = arrayOf("projection", "view")
}

// Store random per-object rotations
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vulkan/basics/05 Push Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ private class PushConstants : VulkanExampleBase() {
object uboVS: Bufferizable() {

lateinit var projection: Mat4
@Order(1)
lateinit var model: Mat4
@Order(2)
val lightPos = Vec4(0f, 0, -2f, 1)

override val fieldOrder = arrayOf("projection", "model", "lightPos")
}

object pipelines {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vulkan/basics/06 Specialization Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class SpecializationConstants : VulkanExampleBase() {
object uboVS : Bufferizable() {

lateinit var projection: Mat4
@Order(1)
lateinit var modelView: Mat4
@Order(2)
val lightPos = Vec4(0f, -2f, 1f, 0f)

override val fieldOrder = arrayOf("projection", "modelView", "lightPos")
}

var pipelineLayout: VkPipelineLayout = NULL
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vulkan/basics/07 Texture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ private class Texture : VulkanExampleBase() {
object uboVS : Bufferizable(){

val projection = Mat4()
@Order(1)
val model = Mat4()
val viewPos = Vec4()
@Order(3)
var lodBias = 0f

override val fieldOrder = arrayOf("projection", "model", "viewPos", "lodBias")
}

object pipelines {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/vulkan/basics/08 Texture Cubemap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ private class TextureCubemap : VulkanExampleBase() {

object uboVS : Bufferizable() {
var projection = Mat4()
@Order(1)
var model = Mat4()
@Order(2)
var lodBias = 0f

override val fieldOrder = arrayOf("projection", "model", "lodBias")
}

object pipelines {
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/vulkan/basics/09 Texture Array.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class TextureArray : VulkanExampleBase() {
object matrices : Bufferizable() {
var projection = Mat4()
var view = Mat4()
override val fieldOrder = arrayOf("projection", "view")
}
// Seperate data for each instance
val instance = ArrayList<UboInstanceData>()
Expand Down
Loading

0 comments on commit 885b903

Please sign in to comment.