forked from Dominaezzz/kgl-vulkan-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.kt
225 lines (195 loc) · 7.83 KB
/
Main.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
import com.kgl.vulkan.Vk
import com.kgl.vulkan.enums.*
import com.kgl.vulkan.handles.*
import com.kgl.vulkan.utils.or
import kotlinx.io.core.use
import sample.utils.BaseApplication
import sample.utils.readAllBytes
fun main(args: Array<String>) {
TriangleApplication().use {
it.mainLoop()
}
}
const val RESOURCE_DIR = "."
class TriangleApplication : BaseApplication() {
private val vertShaderModule: ShaderModule
private val fragShaderModule: ShaderModule
private val pipelineLayout: PipelineLayout
private val renderPass: RenderPass
private lateinit var swapchainFramebuffers: List<Framebuffer>
private lateinit var graphicsPipeline: Pipeline
private lateinit var commandBuffers: List<CommandBuffer>
init {
val vertShaderCode = readAllBytes("$RESOURCE_DIR/shaders/shader.vert.spv")
val fragShaderCode = readAllBytes("$RESOURCE_DIR/shaders/shader.frag.spv")
vertShaderModule = device.createShaderModule(vertShaderCode.asUByteArray())
fragShaderModule = device.createShaderModule(fragShaderCode.asUByteArray())
pipelineLayout = device.createPipelineLayout()
renderPass = device.createRenderPass {
attachments {
description {
format = surfaceFormat.format
samples = SampleCount.`1`
loadOp = AttachmentLoadOp.CLEAR
storeOp = AttachmentStoreOp.STORE
stencilLoadOp = AttachmentLoadOp.DONT_CARE
stencilStoreOp = AttachmentStoreOp.DONT_CARE
initialLayout = ImageLayout.UNDEFINED
finalLayout = ImageLayout.PRESENT_SRC_KHR
}
}
subpasses {
description {
pipelineBindPoint = PipelineBindPoint.GRAPHICS
colorAttachments {
reference {
attachment = 0U
layout = ImageLayout.COLOR_ATTACHMENT_OPTIMAL
}
}
}
}
dependencies {
dependency {
srcSubpass = Vk.SUBPASS_EXTERNAL
dstSubpass = 0U
srcStageMask = PipelineStage.COLOR_ATTACHMENT_OUTPUT
srcAccessMask = null
dstStageMask = PipelineStage.COLOR_ATTACHMENT_OUTPUT
dstAccessMask = Access.COLOR_ATTACHMENT_READ or Access.COLOR_ATTACHMENT_WRITE
}
}
}
onRecreateSwapchain(swapchain)
}
override fun onRecreateSwapchain(swapchain: SwapchainKHR) {
val swapchainWidth = swapchain.imageExtent.width
val swapchainHeight = swapchain.imageExtent.height
graphicsPipeline = device.createGraphicsPipelines {
pipeline(pipelineLayout, renderPass) {
subpass = 0U
basePipelineIndex = -1
stages {
stage(ShaderStage.VERTEX, vertShaderModule) {
name = "main"
}
stage(ShaderStage.FRAGMENT, fragShaderModule) {
name = "main"
}
}
vertexInputState {
}
inputAssemblyState {
topology = PrimitiveTopology.TRIANGLE_LIST
primitiveRestartEnable = false
}
viewportState {
viewports {
viewport {
x = 0f
y = 0f
width = swapchainWidth.toInt().toFloat()
height = swapchainHeight.toInt().toFloat()
minDepth = 0f
maxDepth = 1f
}
}
scissors {
rect2D {
offset(0, 0)
extent(swapchainWidth, swapchainHeight)
}
}
}
rasterizationState {
depthClampEnable = false
rasterizerDiscardEnable = false
polygonMode = PolygonMode.FILL
lineWidth = 1.0f
cullMode = CullMode.BACK
frontFace = FrontFace.CLOCKWISE
depthBiasEnable = false
depthBiasConstantFactor = 0.0f
depthBiasClamp = 0.0f
depthBiasSlopeFactor = 0.0f
}
multisampleState {
sampleShadingEnable = false
rasterizationSamples = SampleCount.`1`
minSampleShading = 1.0f
alphaToCoverageEnable = false
alphaToOneEnable = false
}
colorBlendState {
logicOpEnable = false
attachments {
state {
colorWriteMask = ColorComponent.R or ColorComponent.B or ColorComponent.G or ColorComponent.A
blendEnable = true
srcColorBlendFactor = BlendFactor.SRC_ALPHA
dstColorBlendFactor = BlendFactor.ONE_MINUS_SRC_ALPHA
colorBlendOp = BlendOp.ADD
srcAlphaBlendFactor = BlendFactor.ONE
dstAlphaBlendFactor = BlendFactor.ZERO
alphaBlendOp = BlendOp.ADD
}
}
blendConstants(0f, 0f, 0f, 0f)
}
}
}.single()
swapchainFramebuffers = swapchainImageViews.map { imageView ->
device.createFramebuffer(renderPass, listOf(imageView)) {
width = swapchainWidth
height = swapchainHeight
layers = 1u
}
}
commandBuffers = commandPool.allocate(CommandBufferLevel.PRIMARY, swapchainFramebuffers.size.toUInt())
commandBuffers.forEachIndexed { index, commandBuffer ->
with(commandBuffer) {
begin { flags = CommandBufferUsage.SIMULTANEOUS_USE }
beginRenderPass(renderPass, swapchainFramebuffers[index], SubpassContents.INLINE) {
clearValues {
clearValue {
color(0f, 0f, 0f, 1f)
}
}
renderArea {
offset(0, 0)
extent(swapchainWidth, swapchainHeight)
}
}
bindPipeline(PipelineBindPoint.GRAPHICS, graphicsPipeline)
draw(3U, 1U, 0U, 0U)
endRenderPass()
end()
}
}
}
override fun onDestroySwapchain() {
swapchainFramebuffers.forEach { it.close() }
graphicsPipeline.close()
commandPool.free(commandBuffers)
}
override fun renderFrame(imageIndex: Int) {
device.waitForFences(listOf(inFlightFences[currentFrame]), true)
device.resetFences(listOf(inFlightFences[currentFrame]))
graphicsQueue.submit(inFlightFences[currentFrame]) {
info(
listOf(imageAvailableSemaphores[currentFrame] to PipelineStage.COLOR_ATTACHMENT_OUTPUT),
listOf(commandBuffers[imageIndex]),
listOf(renderFinishedSemaphores[currentFrame])
)
}
}
override fun close() {
device.waitIdle()
onDestroySwapchain()
renderPass.close()
pipelineLayout.close()
fragShaderModule.close()
vertShaderModule.close()
super.close()
}
}