-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01a Triangle Verbose.kt
1294 lines (1105 loc) · 61.2 KB
/
01a Triangle Verbose.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vulkan.basics
import glm_.*
import glm_.func.rad
import glm_.mat4x4.Mat4
import glm_.vec3.Vec3
import kool.bufferBig
import kool.cap
import kool.free
import org.lwjgl.system.MemoryUtil
import org.lwjgl.system.MemoryUtil.NULL
import org.lwjgl.vulkan.*
import org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
import org.lwjgl.vulkan.VK10.*
import uno.buffer.floatBufferOf
import uno.buffer.intBufferOf
import uno.buffer.toBuffer
import uno.glfw.glfw
import vkk.*
import vulkan.UINT64_MAX
import vulkan.useStaging
import vulkan.assetPath
import vulkan.base.VulkanExampleBase
import vulkan.base.tools.DEFAULT_FENCE_TIMEOUT
import java.io.File
import java.nio.ByteBuffer
fun main(args: Array<String>) {
TriangleVerbose().apply {
setupWindow()
initVulkan()
prepare()
renderLoop()
destroy()
}
}
private class TriangleVerbose : VulkanExampleBase() {
init {
zoom = -2.5f
title = "Vulkan Example - Basic indexed triangle (verbose)"
// Values not set here are initialized in the base class constructor
}
/** Vertex layout used in this example */
object Vertex {
// float position [3];
// float color [3];
val size = Vec3.size * 2
val offsetPosition = 0
val offsetColor = Vec3.size
}
/** Vertex buffer and attributes */
object vertices {
/** Handle to the device memory for this buffer */
var memory: Long = NULL
/** Handle to the Vulkan buffer object that the memory is bound to */
var buffer: Long = NULL
}
/** Index buffer */
object indices {
var memory: Long = NULL
var buffer: Long = NULL
var count = 0
}
/** Uniform buffer block object */
object uniformBufferVS {
var memory: Long = NULL
var buffer: Long = NULL
lateinit var descriptor: VkDescriptorBufferInfo.Buffer
}
/*
For simplicity we use the same uniform block layout as in the shader:
layout(set = 0, binding = 0) uniform UBO {
mat4 projectionMatrix;
mat4 modelMatrix;
mat4 viewMatrix;
} ubo;
This way we can just memcopy the ubo data to the ubo
Note: You should use data types that align with the GPU in order to avoid manual padding (vec4, mat4) */
object uboVS {
var projectionMatrix = Mat4()
var modelMatrix = Mat4()
var viewMatrix = Mat4()
fun pack() {
projectionMatrix to buffer
modelMatrix.to(buffer, Mat4.size)
viewMatrix.to(buffer, Mat4.size * 2)
}
val size = Mat4.size * 3
val buffer = bufferBig(size)
val address = MemoryUtil.memAddress(buffer)
}
/** The pipeline layout is used by a pipline to access the descriptor sets
* It defines interface (without binding any actual data) between the shader stages used by the pipeline and the
* shader resources
* A pipeline layout can be shared among multiple pipelines as long as their interfaces match */
var pipelineLayout: Long = NULL
/** Pipelines (often called "pipeline state objects") are used to bake all states that affect a pipeline
* While in OpenGL every state can be changed at (almost) any time, Vulkan requires to layout the graphics
* (and compute) pipeline states upfront
* So for each combination of non-dynamic pipeline states you need a new pipeline (there are a few exceptions to
* this not discussed here)
* Even though this adds a new dimension of planing ahead, it's a great opportunity for performance optimizations
* by the driver */
var pipeline: Long = NULL
/** The descriptor set layout describes the shader binding layout (without actually referencing descriptor)
* Like the pipeline layout it's pretty much a blueprint and can be used with different descriptor sets as long as
* their layout matches */
var descriptorSetLayout: Long = NULL
/** The descriptor set stores the resources bound to the binding points in a shader
* It connects the binding points of the different shaders with the buffers and images used for those bindings */
var descriptorSet: Long = NULL
/* Synchronization primitives
Synchronization is an important concept of Vulkan that OpenGL mostly hid away.
Getting this right is crucial to using Vulkan. */
/** Semaphores
* Used to coordinate operations within the graphics queue and ensure correct command ordering */
var presentCompleteSemaphore: Long = NULL
var renderCompleteSemaphore: Long = NULL
/** Fences
* Used to check the completion of queue operations (e.g. command buffer execution) */
// val waitFences = ArrayList<VkFence>()
override fun destroy() {
window.destroy()
window.onWindowClosed()
glfw.terminate()
/* Clean up used Vulkan resources
Note: Inherited destructor cleans up resources stored in base class */
vkDestroyPipeline(device, pipeline, null)
vkDestroyPipelineLayout(device, pipelineLayout, null)
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, null)
vkDestroyBuffer(device, vertices.buffer, null)
vkFreeMemory(device, vertices.memory, null)
vkDestroyBuffer(device, indices.buffer, null)
vkFreeMemory(device, indices.memory, null)
vkDestroyBuffer(device, uniformBufferVS.buffer, null)
vkFreeMemory(device, uniformBufferVS.memory, null)
vkDestroySemaphore(device, presentCompleteSemaphore, null)
vkDestroySemaphore(device, renderCompleteSemaphore, null)
for (fence in waitFences)
vkDestroyFence(device, fence.L, null)
super.destroy()
}
/** This function is used to request a device memory type that supports all the property flags we request
* (e.g. device local, host visibile)
* Upon success it will return the index of the memory type that fits our requestes memory properties
* This is necessary as implementations can offer an arbitrary number of memory types with different
* memory properties.
* You can check http://vulkan.gpuinfo.org/ for details on different memory configurations */
fun getMemoryTypeIndex(typeBits_: Int, properties: Int): Int {
var typeBits = typeBits_
// Iterate over all memory types available for the device used in this example
for (i in 0 until deviceMemoryProperties.memoryTypeCount()) {
if ((typeBits and 1) == 1 && (deviceMemoryProperties.memoryTypes(i).propertyFlags() and properties) == properties)
return i
typeBits = typeBits ushr 1
}
throw Error("Could not find a suitable memory type!")
}
/** Create the Vulkan synchronization primitives used in this example */
fun prepareSynchronizationPrimitives() {
// Semaphores (Used for correct command ordering)
val semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO)
.pNext(NULL)
// Semaphore used to ensures that image presentation is complete before starting to submit again
val pPresentCompleteSemaphore = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateSemaphore(device, semaphoreCreateInfo, null, pPresentCompleteSemaphore))
presentCompleteSemaphore = pPresentCompleteSemaphore[0]
// Semaphore used to ensures that all commands submitted have been finished before submitting the image to the queue
val pRenderCompleteSemaphore = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateSemaphore(device, semaphoreCreateInfo, null, pRenderCompleteSemaphore))
renderCompleteSemaphore = pRenderCompleteSemaphore[0]
// Fences (Used to check draw command buffer completion)
val fenceCreateInfo = VkFenceCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_FENCE_CREATE_INFO)
.pNext(NULL)
// Create in signaled state so we don't wait on first render of each command buffer
.flags(VK_FENCE_CREATE_SIGNALED_BIT)
val pFence = MemoryUtil.memAllocLong(1)
waitFences = vkFenceArrayBig(drawCmdBuffers.size)
for (i in drawCmdBuffers.indices) {
VK_CHECK_RESULT(vkCreateFence(device, fenceCreateInfo, null, pFence))
waitFences[i] = VkFence(pFence[0])
}
semaphoreCreateInfo.free()
pPresentCompleteSemaphore.free()
pRenderCompleteSemaphore.free()
fenceCreateInfo.free()
pFence.free()
}
/** Get a new command buffer from the command pool
* If begin is true, the command buffer is also started so we can start adding commands */
fun getCommandBuffer(begin: Boolean): VkCommandBuffer {
val cmdBufAllocateInfo = VkCommandBufferAllocateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO)
.pNext(NULL)
.commandPool(cmdPool.L)
.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY)
.commandBufferCount(1)
val pCmdBuffer = MemoryUtil.memAllocPointer(1)
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, cmdBufAllocateInfo, pCmdBuffer))
val cmdBuffer = VkCommandBuffer(pCmdBuffer[0], device)
// If requested, also start the new command buffer
if (begin) {
val cmdBufInfo = VkCommandBufferBeginInfo.calloc()
.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO)
.pNext(NULL)
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffer, cmdBufInfo))
cmdBufInfo.free()
}
cmdBufAllocateInfo.free()
pCmdBuffer.free()
return cmdBuffer
}
/** End the command buffer and submit it to the queue
* Uses a fence to ensure command buffer has finished executing before deleting it */
fun flushCommandBuffer(commandBuffer: VkCommandBuffer) {
assert(commandBuffer.address() != NULL)
VK_CHECK_RESULT(vkEndCommandBuffer(commandBuffer))
val pCommandBuffer = MemoryUtil.memAllocPointer(1)
pCommandBuffer[0] = commandBuffer
val submitInfo = VkSubmitInfo.calloc()
.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO)
.pNext(NULL)
.pCommandBuffers(pCommandBuffer)
// Create fence to ensure that the command buffer has finished executing
val fenceCreateInfo = VkFenceCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_FENCE_CREATE_INFO)
.pNext(NULL)
.flags(0)
val pFence = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateFence(device, fenceCreateInfo, null, pFence))
val fence = pFence[0]
// Submit to the queue
VK_CHECK_RESULT(vkQueueSubmit(queue, submitInfo, fence))
// Wait for the fence to signal that command buffer has finished executing
VK_CHECK_RESULT(vkWaitForFences(device, fence, true, DEFAULT_FENCE_TIMEOUT))
vkDestroyFence(device, fence, null)
vkFreeCommandBuffers(device, cmdPool.L, commandBuffer)
fenceCreateInfo.free()
pCommandBuffer.free()
pFence.free()
}
/** Build separate command buffers for every framebuffer image
* Unlike in OpenGL all rendering commands are recorded once into command buffers that are then resubmitted to the queue
* This allows to generate work upfront and from multiple threads, one of the biggest advantages of Vulkan */
override fun buildCommandBuffers() {
val cmdBufInfo = VkCommandBufferBeginInfo.calloc()
.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO)
.pNext(NULL)
// Set clear values for all framebuffer attachments with loadOp set to clear
// We use two attachments (color and depth) that are cleared at the start of the subpass and as such we need to set clear values for both
val clearValues = VkClearValue.calloc(2)
clearValues[0].color()
.float32(0, 0f)
.float32(1, 0f)
.float32(2, 0.2f)
.float32(3, 1f)
clearValues[1].depthStencil()
.depth(1f)
.stencil(0)
val renderPassBeginInfo = VkRenderPassBeginInfo.calloc()
.sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO)
.pNext(NULL)
.renderPass(renderPass.L)
renderPassBeginInfo.renderArea().apply {
offset()
.x(0)
.y(0)
extent()
.width(size.x)
.height(size.y)
}
renderPassBeginInfo.pClearValues(clearValues)
for (i in drawCmdBuffers.indices) {
// Set target frame buffer
renderPassBeginInfo.framebuffer(frameBuffers[i].L)
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], cmdBufInfo))
// Start the first sub pass specified in our default render pass setup by the base class
// This will clear the color and depth attachment
vkCmdBeginRenderPass(drawCmdBuffers[i], renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE)
// Update dynamic viewport state
val viewport = VkViewport.calloc(1)
.width(size.x.f)
.height(size.y.f)
.minDepth(0f)
.maxDepth(1f)
vkCmdSetViewport(drawCmdBuffers[i], 0, viewport)
// Update dynamic scissor state
val scissor = VkRect2D.calloc(1).apply {
extent()
.width(size.x)
.height(size.y)
offset()
.x(0)
.y(0)
}
vkCmdSetScissor(drawCmdBuffers[i], 0, scissor)
val pDescriptorSet = MemoryUtil.memAllocLong(1)
pDescriptorSet[0] = descriptorSet
// Bind descriptor sets describing shader binding points
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, pDescriptorSet, null)
// Bind the rendering pipeline
// The pipeline (state object) contains all states of the rendering pipeline, binding it will set all the states specified at pipeline creation time
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline)
// Bind triangle vertex buffer (contains position and colors)
val offset = 0L
val pBuffer = MemoryUtil.memAllocLong(1)
pBuffer[0] = vertices.buffer
val pOffset = MemoryUtil.memAllocLong(1)
pOffset[0] = offset
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, pBuffer, pOffset)
// Bind triangle index buffer
vkCmdBindIndexBuffer(drawCmdBuffers[i], indices.buffer, 0, VK_INDEX_TYPE_UINT32)
// Draw indexed triangle
vkCmdDrawIndexed(drawCmdBuffers[i], indices.count, 1, 0, 0, 1)
vkCmdEndRenderPass(drawCmdBuffers[i])
// Ending the render pass will add an implicit barrier transitioning the frame buffer color attachment to
// VK_IMAGE_LAYOUT_PRESENT_SRC_KHR for presenting it to the windowing system
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]))
viewport.free()
scissor.free()
pDescriptorSet.free()
pBuffer.free()
pOffset.free()
}
cmdBufInfo.free()
clearValues.free()
renderPassBeginInfo.free()
}
fun draw() {
// Get next image in the swap chain (back/front buffer)
currentBuffer = swapChain acquireNextImage VkSemaphore(presentCompleteSemaphore)
// Use a fence to wait until the command buffer has finished execution before using it again
VK_CHECK_RESULT(vkWaitForFences(device, waitFences[currentBuffer].L, true, UINT64_MAX))
VK_CHECK_RESULT(vkResetFences(device, waitFences[currentBuffer].L))
// Pipeline stage at which the queue submission will wait (via pWaitSemaphores)
val waitStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
val pWaitStageMask = MemoryUtil.memAllocInt(1)
pWaitStageMask[0] = waitStageMask
val pWaitSemaphore = MemoryUtil.memAllocLong(1)
pWaitSemaphore[0] = presentCompleteSemaphore
val pSignalSemaphore = MemoryUtil.memAllocLong(1)
pSignalSemaphore[0] = renderCompleteSemaphore
val pCommandBuffer = MemoryUtil.memAllocPointer(1)
pCommandBuffer[0] = drawCmdBuffers[currentBuffer]
// The submit info structure specifices a command buffer queue submission batch
val submitInfo = VkSubmitInfo.calloc()
.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO)
.pNext(NULL)
// Pointer to the list of pipeline stages that the semaphore waits will occur at
.pWaitDstStageMask(pWaitStageMask)
// Semaphore(s) to wait upon before the submitted command buffer starts executing
.pWaitSemaphores(pWaitSemaphore)
// One wait semaphore
.waitSemaphoreCount(1)
// Semaphore(s) to be signaled when command buffers have completed
.pSignalSemaphores(pSignalSemaphore)
// One signal semaphore + Command buffers(s) to execute in this batch (submission)
.pCommandBuffers(pCommandBuffer)
// Submit to the graphics queue passing a wait fence
VK_CHECK_RESULT(vkQueueSubmit(queue, submitInfo, waitFences[currentBuffer].L))
/* Present the current buffer to the swap chain
Pass the semaphore signaled by the command buffer submission from the submit info as the wait semaphore
for swap chain presentation
This ensures that the image is not presented to the windowing system until all commands have been submitted */
swapChain.queuePresent(queue, currentBuffer, VkSemaphore(renderCompleteSemaphore))
pWaitStageMask.free()
pWaitSemaphore.free()
pSignalSemaphore.free()
pCommandBuffer.free()
submitInfo.free()
}
/** Prepare vertex and index buffers for an indexed triangle
* Also uploads them to device local memory using staging and initializes vertex input and attribute binding
* to match the vertex shader */
fun prepareVertices() {
/* A note on memory management in Vulkan in general:
This is a very complex topic and while it's fine for an example application to to small individual memory
allocations that is not what should be done a real-world application, where you should allocate large
chunks of memory at once instead. */
// Setup vertices
val vertexBuffer = floatBufferOf(
// position color
+1f, +1f, +0f, 1f, 0f, 0f,
-1f, +1f, +0f, 0f, 1f, 0f,
+0f, -1f, +0f, 0f, 0f, 1f)
val vertexBufferSize = vertexBuffer.size.L
// Setup indices
val indexBuffer = intBufferOf(0, 1, 2)
indices.count = indexBuffer.cap
val indexBufferSize = indexBuffer.size.L
val memAlloc = VkMemoryAllocateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO)
.pNext(NULL)
val memReqs = VkMemoryRequirements.calloc()
val memoryPropertiesFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT or VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
val pData = MemoryUtil.memAllocPointer(1)
if (useStaging) {
/* Static data like vertex and index buffer should be stored on the device memory for optimal (and fastest)
access by the GPU
To achieve this we use so-called "staging buffers" :
- Create a buffer that's visible to the host (and can be mapped)
- Copy the data to this buffer
- Create another buffer that's local on the device (VRAM) with the same size
- Copy the data from the host to the device using a command buffer
- Delete the host visible (staging) buffer
- Use the device local buffers for rendering */
class StagingBuffer {
var memory = NULL
var buffer = NULL
}
val stagingBuffers = object {
val vertices = StagingBuffer()
val indices = StagingBuffer()
}
// Vertex buffer
val vertexBufferInfo = VkBufferCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.pNext(NULL)
.size(vertexBufferSize)
// Buffer is used as the copy source
.usage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT)
// Create a host-visible buffer to copy the vertex data to (staging buffer)
val pBuffer = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateBuffer(device, vertexBufferInfo, null, pBuffer))
stagingBuffers.vertices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, stagingBuffers.vertices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
// Request a host visible memory type that can be used to copy our data do
// Also request it to be coherent, so that writes are visible to the GPU right after unmapping the buffer
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), memoryPropertiesFlags))
val pMemory = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
stagingBuffers.vertices.memory = pMemory[0]
// Map and copy
VK_CHECK_RESULT(vkMapMemory(device, stagingBuffers.vertices.memory, 0, memAlloc.allocationSize(), 0, pData))
MemoryUtil.memCopy(MemoryUtil.memAddress(vertexBuffer), pData[0], vertexBufferSize)
vkUnmapMemory(device, stagingBuffers.vertices.memory)
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffers.vertices.buffer, stagingBuffers.vertices.memory, 0))
// Create a device local buffer to which the (host local) vertex data will be copied and which will be used for rendering
vertexBufferInfo.usage(VkBufferUsage.VERTEX_BUFFER_BIT or VkBufferUsage.TRANSFER_DST_BIT)
VK_CHECK_RESULT(vkCreateBuffer(device, vertexBufferInfo, null, pBuffer))
vertices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, vertices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
vertices.memory = pMemory[0]
VK_CHECK_RESULT(vkBindBufferMemory(device, vertices.buffer, vertices.memory, 0))
// Index buffer
val indexbufferInfo = VkBufferCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.pNext(NULL)
.size(indexBufferSize)
.usage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT)
// Copy index data to a buffer visible to the host (staging buffer)
VK_CHECK_RESULT(vkCreateBuffer(device, indexbufferInfo, null, pBuffer))
stagingBuffers.indices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, stagingBuffers.indices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(),
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT or VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
stagingBuffers.indices.memory = pMemory[0]
VK_CHECK_RESULT(vkMapMemory(device, stagingBuffers.indices.memory, 0, indexBufferSize, 0, pData))
MemoryUtil.memCopy(MemoryUtil.memAddress(indexBuffer), pData[0], indexBufferSize)
vkUnmapMemory(device, stagingBuffers.indices.memory)
VK_CHECK_RESULT(vkBindBufferMemory(device, stagingBuffers.indices.buffer, stagingBuffers.indices.memory, 0))
// Create destination buffer with device only visibility
indexbufferInfo.usage(VK_BUFFER_USAGE_INDEX_BUFFER_BIT or VK_BUFFER_USAGE_TRANSFER_DST_BIT)
VK_CHECK_RESULT(vkCreateBuffer(device, indexbufferInfo, null, pBuffer))
indices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, indices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
indices.memory = pMemory[0]
VK_CHECK_RESULT(vkBindBufferMemory(device, indices.buffer, indices.memory, 0))
/* Buffer copies have to be submitted to a queue, so we need a command buffer for them
Note: Some devices offer a dedicated transfer queue (with only the transfer bit set) that may be faster
when doing lots of copies */
val copyCmd = getCommandBuffer(true)
// Put buffer region copies into command buffer
val copyRegion = VkBufferCopy.calloc(1)
// Vertex buffer
copyRegion.size(vertexBufferSize)
vkCmdCopyBuffer(copyCmd, stagingBuffers.vertices.buffer, vertices.buffer, copyRegion)
// Index buffer
copyRegion.size(indexBufferSize)
vkCmdCopyBuffer(copyCmd, stagingBuffers.indices.buffer, indices.buffer, copyRegion)
// Flushing the command buffer will also submit it to the queue and uses a fence to ensure that all commands have been executed before returning
flushCommandBuffer(copyCmd)
// Destroy staging buffers
// Note: Staging buffer must not be deleted before the copies have been submitted and executed
vkDestroyBuffer(device, stagingBuffers.vertices.buffer, null)
vkFreeMemory(device, stagingBuffers.vertices.memory, null)
vkDestroyBuffer(device, stagingBuffers.indices.buffer, null)
vkFreeMemory(device, stagingBuffers.indices.memory, null)
vertexBufferInfo.free()
pBuffer.free()
pMemory.free()
} else {
/* Don't use staging
Create host-visible buffers only and use these for rendering. This is not advised and will usually
result in lower rendering performance */
// Vertex buffer
val vertexBufferInfo = VkBufferCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.pNext(NULL)
.size(vertexBufferSize)
.usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT)
// Copy vertex data to a buffer visible to the host
val pBuffer = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateBuffer(device, vertexBufferInfo, null, pBuffer))
vertices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, vertices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
// VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT is host visible memory, and VK_MEMORY_PROPERTY_HOST_COHERENT_BIT makes sure writes are directly visible
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), memoryPropertiesFlags))
val pMemory = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
vertices.memory = pMemory[0]
VK_CHECK_RESULT(vkMapMemory(device, vertices.memory, 0, memAlloc.allocationSize(), 0, pData))
MemoryUtil.memCopy(MemoryUtil.memAddress(vertexBuffer), pData[0], vertexBufferSize)
vkUnmapMemory(device, vertices.memory)
VK_CHECK_RESULT(vkBindBufferMemory(device, vertices.buffer, vertices.memory, 0))
// Index buffer
val indexBufferInfo = VkBufferCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.pNext(NULL)
.size(indexBufferSize)
.usage(VK_BUFFER_USAGE_INDEX_BUFFER_BIT)
// Copy index data to a buffer visible to the host
VK_CHECK_RESULT(vkCreateBuffer(device, indexBufferInfo, null, pBuffer))
indices.buffer = pBuffer[0]
vkGetBufferMemoryRequirements(device, indices.buffer, memReqs)
memAlloc.allocationSize(memReqs.size())
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), memoryPropertiesFlags))
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
indices.memory = pMemory[0]
VK_CHECK_RESULT(vkMapMemory(device, indices.memory, 0, indexBufferSize, 0, pData))
MemoryUtil.memCopy(MemoryUtil.memAddress(indexBuffer), pData[0], indexBufferSize)
vkUnmapMemory(device, indices.memory)
VK_CHECK_RESULT(vkBindBufferMemory(device, indices.buffer, indices.memory, 0))
vertexBufferInfo.free()
pBuffer.free()
pMemory.free()
}
vertexBuffer.free()
indexBuffer.free()
memAlloc.free()
memReqs.free()
pData.free()
}
fun setupDescriptorPool() {
// We need to tell the API the number of max. requested descriptors per type
val typeCounts = VkDescriptorPoolSize.calloc(1)
// This example only uses one descriptor type (uniform buffer) and only requests one descriptor of this type
.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
.descriptorCount(1)
// For additional types you need to add new entries in the type count list
// E.g. for two combined image samplers :
// typeCounts[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
// typeCounts[1].descriptorCount = 2;
// Create the global descriptor pool
// All descriptors used in this example are allocated from this pool
val descriptorPoolInfo = VkDescriptorPoolCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO)
.pNext(NULL)
.pPoolSizes(typeCounts)
// Set the max. number of descriptor sets that can be requested from this pool (requesting beyond this limit will result in an error)
.maxSets(1)
val pDescriptorPool = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateDescriptorPool(device, descriptorPoolInfo, null, pDescriptorPool))
descriptorPool = VkDescriptorPool(pDescriptorPool[0])
typeCounts.free()
descriptorPoolInfo.free()
pDescriptorPool.free()
}
fun setupDescriptorSetLayout() {
/* Setup layout of descriptors used in this example
Basically connects the different shader stages to descriptors for binding uniform buffers, image samplers, etc.
So every shader binding should map to one descriptor set layout binding */
// Binding 0: Uniform buffer (Vertex shader)
val layoutBinding = VkDescriptorSetLayoutBinding.calloc(1)
.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
.descriptorCount(1)
.stageFlags(VK_SHADER_STAGE_VERTEX_BIT)
.pImmutableSamplers(null)
val descriptorLayout = VkDescriptorSetLayoutCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO)
.pNext(NULL)
.pBindings(layoutBinding)
val pDescriptorSetLayout = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, descriptorLayout, null, pDescriptorSetLayout))
descriptorSetLayout = pDescriptorSetLayout[0]
// Create the pipeline layout that is used to generate the rendering pipelines that are based on this descriptor set layout
// In a more complex scenario you would have different pipeline layouts for different descriptor set layouts that could be reused
val pipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO)
.pNext(NULL)
.pSetLayouts(pDescriptorSetLayout)
val pPipelineLayout = MemoryUtil.memCallocLong(1)
VK_CHECK_RESULT(vkCreatePipelineLayout(device, pipelineLayoutCreateInfo, null, pPipelineLayout))
pipelineLayout = pPipelineLayout[0]
layoutBinding.free()
descriptorLayout.free()
pDescriptorSetLayout.free()
pipelineLayoutCreateInfo.free()
pPipelineLayout.free()
}
fun setupDescriptorSet() {
// Allocate a new descriptor set from the global descriptor pool
val pDescriptorSetLayour = MemoryUtil.memAllocLong(1)
pDescriptorSetLayour[0] = descriptorSetLayout
val allocInfo = VkDescriptorSetAllocateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO)
.descriptorPool(descriptorPool.L)
.pSetLayouts(pDescriptorSetLayour)
val pDescriptorSet = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, allocInfo, pDescriptorSet))
descriptorSet = pDescriptorSet[0]
/* Update the descriptor set determining the shader binding points
For every binding point used in a shader there needs to be one descriptor set matching that binding point */
val writeDescriptorSet = VkWriteDescriptorSet.calloc(1)
// Binding 0 : Uniform buffer
.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET)
.dstSet(descriptorSet)
.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
.pBufferInfo(uniformBufferVS.descriptor)
// Binds this uniform buffer to binding point 0
.dstBinding(0)
vkUpdateDescriptorSets(device, writeDescriptorSet, null)
pDescriptorSetLayour.free()
allocInfo.free()
pDescriptorSet.free()
writeDescriptorSet.free()
}
/** Create the depth (and stencil) buffer attachments used by our framebuffers
* Note: Override of virtual function in the base class and called from within VulkanExampleBase::prepare */
override fun setupDepthStencil() {
// Create an optimal image used as the depth stencil attachment
val image = VkImageCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO)
.pNext(NULL)
.imageType(VK_IMAGE_TYPE_2D)
.format(depthFormat.i)
// Use example's height and width
image.extent.set(size.x, size.y, 1)
image
.mipLevels(1)
.arrayLayers(1)
.samples(VK_SAMPLE_COUNT_1_BIT)
.tiling(VK_IMAGE_TILING_OPTIMAL)
.usage(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT or VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED)
val pImage = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateImage(device, image, null, pImage))
depthStencil.image = VkImage(pImage[0])
// Allocate memory for the image (device local) and bind it to our image
val memAlloc = VkMemoryAllocateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO)
.pNext(NULL)
val memReqs = VkMemoryRequirements.calloc()
vkGetImageMemoryRequirements(device, depthStencil.image.L, memReqs)
memAlloc.allocationSize(memReqs.size())
memAlloc.memoryTypeIndex(getMemoryTypeIndex(memReqs.memoryTypeBits(), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
val pMemory = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkAllocateMemory(device, memAlloc, null, pMemory))
depthStencil.mem = VkDeviceMemory(pMemory[0])
VK_CHECK_RESULT(vkBindImageMemory(device, depthStencil.image.L, depthStencil.mem.L, 0))
/* Create a view for the depth stencil image
Images aren't directly accessed in Vulkan, but rather through views described by a subresource range
This allows for multiple views of one image with differing ranges (e.g. for different layers) */
val depthStencilView = VkImageViewCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO)
.pNext(NULL)
.viewType(VK_IMAGE_VIEW_TYPE_2D)
.format(depthFormat.i)
depthStencilView.subresourceRange()
.aspectMask(VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT)
.baseMipLevel(0)
.levelCount(1)
.baseArrayLayer(0)
.layerCount(1)
depthStencilView.image(depthStencil.image.L)
val pImageView = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateImageView(device, depthStencilView, null, pImageView))
depthStencil.view = VkImageView(pImageView[0])
image.free()
pImage.free()
memAlloc.free()
memReqs.free()
pMemory.free()
depthStencilView.free()
pImageView.free()
}
/** Create a frame buffer for each swap chain image
* Note: Override of virtual function in the base class and called from within VulkanExampleBase::prepare */
override fun setupFrameBuffer() {
// Create a frame buffer for every image in the swapchain
frameBuffers = initVkFramebufferArray(swapChain.imageCount) { i ->
val attachments = MemoryUtil.memAllocLong(2)
attachments[0] = swapChain.buffers[i].view.L // Color attachment is the view of the swapchain image
attachments[1] = depthStencil.view.L // Depth/Stencil attachment is the same for all frame buffers
val frameBufferCreateInfo = VkFramebufferCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO)
.pNext(NULL)
// All frame buffers use the same renderpass setup
.renderPass(renderPass.L)
.pAttachments(attachments)
.width(size.x)
.height(size.y)
.layers(1)
// Create the framebuffer
val pFramebuffer = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateFramebuffer(device, frameBufferCreateInfo, null, pFramebuffer))
VkFramebuffer(pFramebuffer[0]).also {
pFramebuffer.free()
}
}
}
/** Render pass setup
* Render passes are a new concept in Vulkan. They describe the attachments used during rendering and may contain
* multiple subpasses with attachment dependencies
* This allows the driver to know up-front what the rendering will look like and is a good opportunity to optimize
* especially on tile-based renderers (with multiple subpasses)
* Using sub pass dependencies also adds implicit layout transitions for the attachment used, so we don't need
* to add explicit image memory barriers to transform them
* Note: Override of virtual function in the base class and called from within VulkanExampleBase::prepare */
override fun setupRenderPass() {
// This example will use a single render pass with one subpass
// Descriptors for the attachments used by this renderpass
val attachments = VkAttachmentDescription.calloc(2)
// Color attachment
attachments[0]
.format(swapChain.colorFormat.i) // Use the color format selected by the swapchain
.samples(VK_SAMPLE_COUNT_1_BIT) // We don't use multi sampling in this example
.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR) // Clear this attachment at the start of the render pass
.storeOp(VK_ATTACHMENT_STORE_OP_STORE) // Keep it's contents after the render pass is finished (for displaying it)
.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE) // We don't use stencil, so don't care for load
.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE) // Same for store
.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED) // Layout at render pass start. Initial doesn't matter, so we use undefined
.finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) // Layout to which the attachment is transitioned when the render pass is finished
// As we want to present the color buffer to the swapchain, we transition to PRESENT_KHR
// Depth attachment
attachments[1]
.format(depthFormat.i) // A proper depth format is selected in the example base
.samples(VK_SAMPLE_COUNT_1_BIT)
.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR) // Clear depth at start of first subpass
.storeOp(VK_ATTACHMENT_STORE_OP_DONT_CARE) // We don't need depth after render pass has finished (DONT_CARE may result in better performance)
.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE) // No stencil
.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE) // No Stencil
.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED) // Layout at render pass start. Initial doesn't matter, so we use undefined
.finalLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) // Transition to depth/stencil attachment
// Setup attachment references
val colorReference = VkAttachmentReference.calloc(1)
.attachment(0) // Attachment 0 is color
.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) // Attachment layout used as color during the subpass
val depthReference = VkAttachmentReference.calloc()
.attachment(1) // Attachment 1 is color
.layout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) // Attachment used as depth/stemcil used during the subpass
// Setup a single subpass reference
val subpassDescription = VkSubpassDescription.calloc(1)
.pipelineBindPoint(VK_PIPELINE_BIND_POINT_GRAPHICS)
.colorAttachmentCount(1)
.pColorAttachments(colorReference) // Reference to the color attachment in slot 0
.pDepthStencilAttachment(depthReference) // Reference to the depth attachment in slot 1
.pInputAttachments(null) // (Input attachments not used by this example)
.pPreserveAttachments(null) // (Preserve attachments not used by this example)
.pResolveAttachments(null) // Resolve attachments are resolved at the end of a sub pass and can be used for e.g. multi sampling
/* Setup subpass dependencies
These will add the implicit ttachment layout transitionss specified by the attachment descriptions
The actual usage layout is preserved through the layout specified in the attachment reference
Each subpass dependency will introduce a memory and execution dependency between the source and dest subpass described by
srcStageMask, dstStageMask, srcAccessMask, dstAccessMask (and dependencyFlags is set)
Note: VK_SUBPASS_EXTERNAL is a special constant that refers to all commands executed outside of the actual renderpass) */
val dependencies = VkSubpassDependency.calloc(2)
/* First dependency at the start of the renderpass
Does the transition from final to initial layout */
dependencies[0]
.srcSubpass(VK_SUBPASS_EXTERNAL) // Producer of the dependency
.dstSubpass(0) // Consumer is our single subpass that will wait for the execution depdendency
.srcStageMask(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT)
.dstStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)
.srcAccessMask(VK_ACCESS_MEMORY_READ_BIT)
.dstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_READ_BIT or VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT)
.dependencyFlags(VK_DEPENDENCY_BY_REGION_BIT)
/* Second dependency at the end the renderpass
Does the transition from the initial to the final layout */
dependencies[1]
.srcSubpass(0) // Producer of the dependency is our single subpass
.dstSubpass(VK_SUBPASS_EXTERNAL) // Consumer are all commands outside of the renderpass
.srcStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)
.dstStageMask(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT)
.srcAccessMask(VK_ACCESS_COLOR_ATTACHMENT_READ_BIT or VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT)
.dstAccessMask(VK_ACCESS_MEMORY_READ_BIT)
.dependencyFlags(VK_DEPENDENCY_BY_REGION_BIT)
// Create the actual renderpass
val renderPassInfo = VkRenderPassCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO)
.pNext(NULL)
.pAttachments(attachments) // Descriptions of the attachments used by the render pass
.pSubpasses(subpassDescription) // Description of that subpass
.pDependencies(dependencies) // Subpass dependencies used by the render pass
val pRenderPass = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateRenderPass(device, renderPassInfo, null, pRenderPass))
renderPass = VkRenderPass(pRenderPass[0])
attachments.free()
colorReference.free()
depthReference.free()
subpassDescription.free()
dependencies.free()
renderPassInfo.free()
pRenderPass.free()
}
/** Vulkan loads it's shaders from an immediate binary representation called SPIR-V
* Shaders are compiled offline from e.g. GLSL using the reference glslang compiler
* This function loads such a shader from a binary file and returns a shader module structure */
fun loadSPIRVShader(filename: String): Long {
var shaderCode: ByteBuffer? = null
val file = File(filename)
if (file.exists() && file.canRead()) {
val bytes = file.readBytes()
// Copy file contents into a buffer
shaderCode = bytes.toBuffer()
assert(bytes.isNotEmpty())
}
if (shaderCode != null) {
// Create a new shader module that will be used for pipeline creation
val moduleCreateInfo = VkShaderModuleCreateInfo.calloc()
.sType(VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO)
.pNext(NULL)
.pCode(shaderCode)
val pShaderModule = MemoryUtil.memAllocLong(1)
VK_CHECK_RESULT(vkCreateShaderModule(device, moduleCreateInfo, null, pShaderModule))
val shaderModule = pShaderModule[0]
shaderCode.free()
moduleCreateInfo.free()
pShaderModule.free()
return shaderModule
} else {
System.err.println("Error: Could not open shader file \"$filename\"")
return NULL
}
}
fun preparePipelines() {
/* Create the graphics pipeline used in this example