-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path11 Model Rendering.kt
535 lines (436 loc) · 18.9 KB
/
11 Model Rendering.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Vulkan Example - Model loading and rendering
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
package vulkan.basics
import assimp.AiPostProcessStepsFlags
import assimp.Importer
import assimp.or
import glm_.L
import glm_.func.rad
import glm_.glm
import glm_.mat4x4.Mat4
import glm_.size
import glm_.vec2.Vec2
import glm_.vec3.Vec3
import glm_.vec4.Vec4
import kool.adr
import org.lwjgl.system.MemoryUtil.NULL
import org.lwjgl.vulkan.VkDevice
import org.lwjgl.vulkan.VkPipelineVertexInputStateCreateInfo
import org.lwjgl.vulkan.VkVertexInputAttributeDescription
import org.lwjgl.vulkan.VkVertexInputBindingDescription
import vkk.*
import vulkan.useStaging
import vulkan.VERTEX_BUFFER_BIND_ID
import vulkan.assetPath
import vulkan.base.Buffer
import vulkan.base.Texture2D
import vulkan.base.VulkanExampleBase
import vulkan.base.tools
import assimp.AiPostProcessStep as Pp
fun main(args: Array<String>) {
ModelRendering().apply {
setupWindow()
initVulkan()
prepare()
renderLoop()
destroy()
}
}
private class ModelRendering : VulkanExampleBase() {
var wireframe = false
object textures {
val colorMap = Texture2D()
}
object vertices {
lateinit var inputState: VkPipelineVertexInputStateCreateInfo
lateinit var bindingDescriptions: VkVertexInputBindingDescription
lateinit var attributeDescriptions: VkVertexInputAttributeDescription.Buffer
}
// Vertex layout used in this example
// This must fit input locations of the vertex shader used to render the model
class Vertex : Bufferizable() {
lateinit var pos: Vec3
@Order(1)
lateinit var normal: Vec3
lateinit var uv: Vec2
@Order(3)
lateinit var color: Vec3
}
// Contains all Vulkan resources required to represent vertex and index buffers for a model
// This is for demonstration and learning purposes, the other examples use a model loader class for easy access
object model {
object vertices {
var buffer = VkBuffer(NULL)
var memory = VkDeviceMemory(NULL)
}
object indices {
var count = 0
var buffer = VkBuffer(NULL)
var memory = VkDeviceMemory(NULL)
}
// Destroys all Vulkan resources created for this model
fun destroy(device: VkDevice) = device.apply {
destroyBuffer(vertices.buffer)
freeMemory(vertices.memory)
destroyBuffer(indices.buffer)
freeMemory(indices.memory)
}
}
object uniformBuffers {
val scene = Buffer()
}
object uboVS : Bufferizable() {
lateinit var projection: Mat4
@Order(1)
lateinit var model: Mat4
@Order(2)
val lightPos = Vec4(25f, 5f, 5f, 1f)
}
object pipelines {
var solid = VkPipeline(NULL)
var wireframe = VkPipeline(NULL)
}
var pipelineLayout = VkPipelineLayout(NULL)
var descriptorSet = VkDescriptorSet(NULL)
var descriptorSetLayout = VkDescriptorSetLayout(NULL)
init {
zoom = -5.5f
zoomSpeed = 2.5f
rotationSpeed = 0.5f
rotation(-0.5f, -112.75f, 0f)
cameraPos(0.1f, 1.1f, 0f)
title = "Model rendering"
settings.overlay = false // TODO
}
override fun destroy() {
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
device.apply {
destroyPipeline(pipelines.solid)
if (pipelines.wireframe.L != NULL)
destroyPipeline(pipelines.wireframe)
destroyPipelineLayout(pipelineLayout)
destroyDescriptorSetLayout(descriptorSetLayout)
}
model.destroy(device)
textures.colorMap.destroy()
uniformBuffers.scene.destroy()
super.destroy()
}
override fun getEnabledFeatures() {
// Fill mode non solid is required for wireframe display
if (deviceFeatures.fillModeNonSolid)
enabledFeatures.fillModeNonSolid = true
}
override fun buildCommandBuffers() {
val cmdBufInfo = vk.CommandBufferBeginInfo()
val clearValues = vk.ClearValue(2).also {
it[0].color(defaultClearColor)
it[1].depthStencil(1f, 0)
}
val renderPassBeginInfo = vk.RenderPassBeginInfo {
renderPass = [email protected]
renderArea.apply {
offset(0)
extent(size)
}
this.clearValues = clearValues
}
for (i in drawCmdBuffers.indices) {
// Set target frame buffer
renderPassBeginInfo.framebuffer(frameBuffers[i].L)
drawCmdBuffers[i].apply {
begin(cmdBufInfo)
beginRenderPass(renderPassBeginInfo, VkSubpassContents.INLINE)
setViewport(size)
setScissor(size)
bindDescriptorSets(VkPipelineBindPoint.GRAPHICS, pipelineLayout, descriptorSet)
bindPipeline(VkPipelineBindPoint.GRAPHICS, if (wireframe) pipelines.wireframe else pipelines.solid)
// Bind mesh vertex buffer
bindVertexBuffers(VERTEX_BUFFER_BIND_ID, model.vertices.buffer)
// Bind mesh index buffer
bindIndexBuffer(model.indices.buffer, VkDeviceSize(0), VkIndexType.UINT32)
// Render mesh vertex buffer using it's indices
drawIndexed(model.indices.count, 1, 0, 0, 0)
drawUI()
endRenderPass()
end()
}
}
}
/** Load a model from file using the ASSIMP model loader and generate all resources required to render the model */
fun loadModel(filename: String) {
// Load the model from file using ASSIMP
// Flags for loading the mesh
val assimpFlags: AiPostProcessStepsFlags = Pp.FlipWindingOrder or Pp.Triangulate or Pp.PreTransformVertices
val scene = Importer().readFile(filename, assimpFlags)!!
// Generate vertex buffer from ASSIMP scene data
val scale = 1f
val vertices = ArrayList<Vertex>()
// Iterate through all meshes in the file and extract the vertex components
for (m in 0 until scene.numMeshes)
for (v in 0 until scene.meshes[m].numVertices) {
val vertex = Vertex().apply {
// Use glm make_* functions to convert ASSIMP vectors to glm vectors
pos = scene.meshes[m].vertices[v] * scale
normal = scene.meshes[m].normals[v]
// Texture coordinates and colors may have multiple channels, we only use the first [0] one
uv = Vec2(scene.meshes[m].textureCoords[0][v])
// Mesh may not have vertex colors
color = scene.meshes[m].colors.getOrNull(0)?.let { Vec3(it[v]) } ?: Vec3(1f)
// Vulkan uses a right-handed NDC (contrary to OpenGL), so simply flip Y-Axis
pos.y *= -1f
}
vertices += vertex
}
val vertexBuffer = bufferOf(vertices)
val vertexBufferSize = VkDeviceSize(vertexBuffer.size.L)
// Generate index buffer from ASSIMP scene data
val indices = ArrayList<Int>()
for (m in 0 until scene.numMeshes) {
val indexBase = indices.size
for (f in 0 until scene.meshes[m].numFaces)
// We assume that all faces are triangulated
for (i in 0..2)
indices += scene.meshes[m].faces[f][i] + indexBase
}
val indexBuffer = intArrayOf(indices)
val indexBufferSize = VkDeviceSize(indexBuffer.size.L)
model.indices.count = indices.size
// Static mesh should always be device local
if (useStaging) {
val vertexStaging = object {
var buffer = VkBuffer(NULL)
var memory = VkDeviceMemory(NULL)
}
val indexStaging = object {
var buffer = VkBuffer(NULL)
var memory= VkDeviceMemory (NULL)
}
// Create staging buffers
// Vertex data
vulkanDevice.createBuffer(
VkBufferUsage.TRANSFER_SRC_BIT.i,
VkMemoryProperty.HOST_VISIBLE_BIT or VkMemoryProperty.HOST_COHERENT_BIT,
vertexBufferSize,
vertexStaging::buffer,
vertexStaging::memory,
vertexBuffer.adr)
// Index data
vulkanDevice.createBuffer(
VkBufferUsage.TRANSFER_SRC_BIT.i,
VkMemoryProperty.HOST_VISIBLE_BIT or VkMemoryProperty.HOST_COHERENT_BIT,
indexBufferSize,
indexStaging::buffer,
indexStaging::memory,
indexBuffer.adr)
// Create device local buffers
// Vertex buffer
vulkanDevice.createBuffer(
VkBufferUsage.VERTEX_BUFFER_BIT or VkBufferUsage.TRANSFER_DST_BIT,
VkMemoryProperty.DEVICE_LOCAL_BIT.i,
vertexBufferSize,
model.vertices::buffer,
model.vertices::memory)
// Index buffer
vulkanDevice.createBuffer(
VkBufferUsage.INDEX_BUFFER_BIT or VkBufferUsage.TRANSFER_DST_BIT,
VkMemoryProperty.DEVICE_LOCAL_BIT.i,
indexBufferSize,
model.indices::buffer,
model.indices::memory)
// Copy from staging buffers
val copyCmd = super.createCommandBuffer(VkCommandBufferLevel.PRIMARY, true)
val copyRegion = vk.BufferCopy { size = vertexBufferSize }
copyCmd.copyBuffer(
vertexStaging.buffer,
model.vertices.buffer,
copyRegion)
copyRegion.size = indexBufferSize
copyCmd.copyBuffer(
indexStaging.buffer,
model.indices.buffer,
copyRegion)
super.flushCommandBuffer(copyCmd, queue, true)
device.apply {
destroyBuffer(vertexStaging.buffer)
freeMemory(vertexStaging.memory)
destroyBuffer(indexStaging.buffer)
freeMemory(indexStaging.memory)
}
} else {
// Vertex buffer
vulkanDevice.createBuffer(
VkBufferUsage.VERTEX_BUFFER_BIT.i,
VkMemoryProperty.HOST_VISIBLE_BIT.i,
vertexBufferSize,
model.vertices::buffer,
model.vertices::memory,
vertexBuffer.adr)
// Index buffer
vulkanDevice.createBuffer(
VkBufferUsage.INDEX_BUFFER_BIT.i,
VkMemoryProperty.HOST_VISIBLE_BIT.i,
indexBufferSize,
model.indices::buffer,
model.indices::memory,
indexBuffer.adr)
}
}
fun loadAssets() {
loadModel("$assetPath/models/voyager/voyager.dae")
val (texture, format) = when {
deviceFeatures.textureCompressionBC -> "voyager_bc3_unorm.ktx" to VkFormat.BC3_UNORM_BLOCK
deviceFeatures.textureCompressionASTC_LDR -> "voyager_astc_8x8_unorm.ktx" to VkFormat.ASTC_8x8_UNORM_BLOCK
deviceFeatures.textureCompressionETC2 -> "voyager_etc2_unorm.ktx" to VkFormat.ETC2_R8G8B8A8_UNORM_BLOCK
else -> tools.exitFatal("Device does not support any compressed texture format!", ERROR_FEATURE_NOT_PRESENT)
}
textures.colorMap.loadFromFile("$assetPath/models/voyager/$texture", format, vulkanDevice, queue)
}
fun setupVertexDescriptions() {
val vertex = Vertex()
// Binding description
vertices.bindingDescriptions = vk.VertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, vertex.size, VkVertexInputRate.VERTEX)
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions = vk.VertexInputAttributeDescription(
// Location 0 : Position
VERTEX_BUFFER_BIND_ID, 0, VkFormat.R32G32B32_SFLOAT, vertex.offsetOf("pos"),
// Location 1 : Normal
VERTEX_BUFFER_BIND_ID, 1, VkFormat.R32G32B32_SFLOAT, vertex.offsetOf("normal"),
// Location 2 : Texture coordinates
VERTEX_BUFFER_BIND_ID, 2, VkFormat.R32G32_SFLOAT, vertex.offsetOf("uv"),
// Location 3 : Color
VERTEX_BUFFER_BIND_ID, 3, VkFormat.R32G32B32_SFLOAT, vertex.offsetOf("color"))
vertices.inputState = vk.PipelineVertexInputStateCreateInfo {
vertexBindingDescription = vertices.bindingDescriptions
vertexAttributeDescriptions = vertices.attributeDescriptions
}
}
fun setupDescriptorPool() {
// Example uses one ubo and one combined image sampler
val poolSizes = vk.DescriptorPoolSize(
VkDescriptorType.UNIFORM_BUFFER, 1,
VkDescriptorType.COMBINED_IMAGE_SAMPLER, 1)
val descriptorPoolInfo = vk.DescriptorPoolCreateInfo(poolSizes, 1)
descriptorPool = device createDescriptorPool descriptorPoolInfo
}
fun setupDescriptorSetLayout() {
val setLayoutBindings = vk.DescriptorSetLayoutBinding(
// Binding 0 : Vertex shader uniform buffer
VkDescriptorType.UNIFORM_BUFFER, VkShaderStage.VERTEX_BIT.i, 0,
// Binding 1 : Fragment shader combined sampler
VkDescriptorType.COMBINED_IMAGE_SAMPLER, VkShaderStage.FRAGMENT_BIT.i, 1)
val descriptorLayout = vk.DescriptorSetLayoutCreateInfo(setLayoutBindings)
descriptorSetLayout = device createDescriptorSetLayout descriptorLayout
val pipelineLayoutCreateInfo = vk.PipelineLayoutCreateInfo(descriptorSetLayout)
pipelineLayout = device createPipelineLayout pipelineLayoutCreateInfo
}
fun setupDescriptorSet() {
val allocInfo = vk.DescriptorSetAllocateInfo(descriptorPool, descriptorSetLayout)
descriptorSet = device allocateDescriptorSets allocInfo
val texDescriptor = vk.DescriptorImageInfo(textures.colorMap.sampler, textures.colorMap.view, VkImageLayout.GENERAL)
val writeDescriptorSets = vk.WriteDescriptorSet(
// Binding 0 : Vertex shader uniform buffer
descriptorSet, VkDescriptorType.UNIFORM_BUFFER, 0, uniformBuffers.scene.descriptor,
// Binding 1 : Color map
descriptorSet, VkDescriptorType.COMBINED_IMAGE_SAMPLER, 1, texDescriptor)
device updateDescriptorSets writeDescriptorSets
}
fun preparePipelines() {
val inputAssemblyState = vk.PipelineInputAssemblyStateCreateInfo(VkPrimitiveTopology.TRIANGLE_LIST, 0, false)
val rasterizationState = vk.PipelineRasterizationStateCreateInfo(VkPolygonMode.FILL, VkCullMode.BACK_BIT.i, VkFrontFace.CLOCKWISE)
val blendAttachmentState = vk.PipelineColorBlendAttachmentState(0xf, false)
val colorBlendState = vk.PipelineColorBlendStateCreateInfo(blendAttachmentState)
val depthStencilState = vk.PipelineDepthStencilStateCreateInfo(true, true, VkCompareOp.LESS_OR_EQUAL)
val viewportState = vk.PipelineViewportStateCreateInfo(1, 1)
val multisampleState = vk.PipelineMultisampleStateCreateInfo(VkSampleCount.`1_BIT`)
val dynamicStateEnables = listOf(VkDynamicState.VIEWPORT, VkDynamicState.SCISSOR)
val dynamicState = vk.PipelineDynamicStateCreateInfo(dynamicStateEnables)
// Solid rendering pipeline
// Load shaders
val shaderStages = vk.PipelineShaderStageCreateInfo(2).also {
it[0].loadShader("$assetPath/shaders/mesh/mesh.vert.spv", VkShaderStage.VERTEX_BIT)
it[1].loadShader("$assetPath/shaders/mesh/mesh.frag.spv", VkShaderStage.FRAGMENT_BIT)
}
val pipelineCreateInfo = vk.GraphicsPipelineCreateInfo(pipelineLayout, renderPass).also {
it.vertexInputState = vertices.inputState
it.inputAssemblyState = inputAssemblyState
it.rasterizationState = rasterizationState
it.colorBlendState = colorBlendState
it.multisampleState = multisampleState
it.viewportState = viewportState
it.depthStencilState = depthStencilState
it.dynamicState = dynamicState
it.stages = shaderStages
}
pipelines.solid = device.createGraphicsPipelines(pipelineCache, pipelineCreateInfo)
// Wire frame rendering pipeline
if (deviceFeatures.fillModeNonSolid) {
rasterizationState.polygonMode = VkPolygonMode.LINE
rasterizationState.lineWidth = 1f
pipelines.wireframe = device.createGraphicsPipelines(pipelineCache, pipelineCreateInfo)
}
}
/** Prepare and initialize uniform buffer containing shader uniforms */
fun prepareUniformBuffers() {
// Vertex shader uniform buffer block
vulkanDevice.createBuffer(
VkBufferUsage.UNIFORM_BUFFER_BIT.i,
VkMemoryProperty.HOST_VISIBLE_BIT or VkMemoryProperty.HOST_COHERENT_BIT,
uniformBuffers.scene,
VkDeviceSize(uboVS.size.L))
// Map persistent
uniformBuffers.scene.map()
updateUniformBuffers()
}
fun updateUniformBuffers() {
uboVS.projection = glm.perspective(60f.rad, size.aspect, 0.1f, 256f)
val viewMatrix = glm.translate(Mat4(1f), Vec3(0f, 0f, zoom))
uboVS.model = viewMatrix * glm.translate(Mat4(1f), cameraPos)
.rotateAssign(rotation.x.rad, 1f, 0f, 0f)
.rotateAssign(rotation.y.rad, 0f, 1f, 0f)
.rotateAssign(rotation.z.rad, 0f, 0f, 1f)
uboVS to uniformBuffers.scene.mapped
}
fun draw() {
super.prepareFrame()
// Command buffer to be sumitted to the queue
submitInfo.commandBuffer = drawCmdBuffers[currentBuffer]
// Submit to queue
queue submit submitInfo
super.submitFrame()
}
override fun prepare() {
super.prepare()
loadAssets()
setupVertexDescriptions()
prepareUniformBuffers()
setupDescriptorSetLayout()
preparePipelines()
setupDescriptorPool()
setupDescriptorSet()
buildCommandBuffers()
prepared = true
window.show()
}
override fun render() {
if (!prepared)
return
draw()
}
override fun viewChanged() = updateUniformBuffers()
// virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
// {
// if (overlay->header("Settings")) {
// if (overlay->checkBox("Wireframe", &wireframe)) {
// buildCommandBuffers()
// }
// }
// }
}