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 | // Horrible
int SetupGraphicsPipeline()
{
VkResult res;
// Load Vertex Shader (SPIR-V) to memory
unsigned long size;
void* pVertex = Win32_FileLoader(L"vert.spv", &size);
VkShaderModuleCreateInfo moduleInfo = {};
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleInfo.codeSize = size;
moduleInfo.pCode = (uint32_t*)pVertex;
// Create Vertex Shader Module
VkShaderModule vertexModule;
res = vkCreateShaderModule(_VkSystem.device, &moduleInfo, 0, &vertexModule);
if (res != VK_SUCCESS)
return 1;
// Setup Stage info for the Vertex Shader
VkPipelineShaderStageCreateInfo stageInfo[2] = {};
stageInfo[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
stageInfo[0].pName = "main";
stageInfo[0].module = vertexModule;
VkPipelineLayoutCreateInfo layoutInfo = {};
layoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
// Create the Pipeline Layout
VkPipelineLayout pipeLayout;
res = vkCreatePipelineLayout(_VkSystem.device, &layoutInfo, 0, &pipeLayout);
if (res != VK_SUCCESS)
return 1;
// Load Fragment Shader (SPIR-V) to memory
void* pFrag = Win32_FileLoader(L"frag.spv", &size);
moduleInfo = {};
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleInfo.codeSize = size;
moduleInfo.pCode = (uint32_t*)pFrag;
// Create Fragment Shader Module
VkShaderModule fragModule;
res = vkCreateShaderModule(_VkSystem.device, &moduleInfo, 0, &fragModule);
if (res != VK_SUCCESS)
return 1;
// Setup Stage info for the Fragment Shader
stageInfo[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
stageInfo[1].pName = "main";
stageInfo[1].module = fragModule;
VkViewport viewPort = {};
viewPort.width = 1280;
viewPort.height = 720;
VkRect2D scissors = {};
scissors.extent.width = viewPort.width;
scissors.extent.height = viewPort.height;
// Setup Viewport state information
VkPipelineViewportStateCreateInfo viewportInfo = {};
viewportInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportInfo.viewportCount = viewportInfo.scissorCount = 1;
viewportInfo.pViewports = (const VkViewport*)&viewportInfo;
viewportInfo.pScissors = &scissors;
// Setup Rasterization State Information
VkPipelineRasterizationStateCreateInfo rasterInfo = {};
rasterInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterInfo.polygonMode = VK_POLYGON_MODE_FILL;
rasterInfo.cullMode = VK_CULL_MODE_NONE;
rasterInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterInfo.rasterizerDiscardEnable = 1;
rasterInfo.lineWidth = 1.0f;
// Setup Input Assembly State Information
VkPipelineInputAssemblyStateCreateInfo assemblyInfo = {};
assemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
assemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
// Setup Vertex Input Attribute Description
VkVertexInputAttributeDescription vertAttribDesc = {};
vertAttribDesc.binding = 0;
vertAttribDesc.format = VK_FORMAT_R32G32_SFLOAT;
// Setup Vertex Input Binding Description
VkVertexInputBindingDescription vertBindDesc = {};
vertBindDesc.binding = 0;
vertBindDesc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
// Setup Vertex Input State Information
VkPipelineVertexInputStateCreateInfo vertInfo = {};
vertInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertInfo.pVertexAttributeDescriptions = &vertAttribDesc;
vertInfo.vertexAttributeDescriptionCount = 1;
vertInfo.pVertexBindingDescriptions = &vertBindDesc;
vertInfo.vertexBindingDescriptionCount = 1;
VkPipelineMultisampleStateCreateInfo multiInfo = {};
multiInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multiInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendStateCreateInfo blendInfo = {};
blendInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
// Setup Graphics Pipeline Information
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.renderPass = _VkSystem.renderPass;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = stageInfo;
pipelineInfo.pVertexInputState = &vertInfo;
pipelineInfo.pInputAssemblyState = &assemblyInfo;
pipelineInfo.pRasterizationState = &rasterInfo;
pipelineInfo.layout = pipeLayout;
pipelineInfo.pViewportState = &viewportInfo;
pipelineInfo.pColorBlendState = &blendInfo;
pipelineInfo.pMultisampleState = &multiInfo;
// Create Graphics Pipeline
VkPipeline graphicPipeline;
res = vkCreateGraphicsPipelines(_VkSystem.device, 0, 1, &pipelineInfo, 0, &graphicPipeline);
if (res != VK_SUCCESS)
return 1;
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.size = sizeof(float) * 6;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkBuffer buffer;
res = vkCreateBuffer(_VkSystem.device, &bufferInfo, 0, &buffer);
if (res != VK_SUCCESS)
return 1;
VkMemoryRequirements memoryReq;
vkGetBufferMemoryRequirements(_VkSystem.device, buffer, &memoryReq);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memoryReq.size;
allocInfo.memoryTypeIndex = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
VkDeviceMemory devMemory = {};
res = vkAllocateMemory(_VkSystem.device, &allocInfo, 0, &devMemory);
if (res != VK_SUCCESS)
return 1;
res = vkBindBufferMemory(_VkSystem.device, buffer, devMemory, 0);
if (res != VK_SUCCESS)
return 1;
void* mappedMemory = 0;
res = vkMapMemory(_VkSystem.device, devMemory, 0, allocInfo.allocationSize, 0, &mappedMemory);
if (res != VK_SUCCESS)
return 1;
// Vertex Data
float vertData[] = { -1.f,1.f , 0.f,-1.f, 1.f,1.f };
memcpy(mappedMemory, vertData, sizeof(vertData));
vkUnmapMemory(_VkSystem.device, devMemory);
_VkSystem.graphicsPipeline = graphicPipeline;
_VkSystem.buffer = buffer;
return res;
}
int Vulkan_Render(float color)
{
VkResult res;
//vkDeviceWaitIdle(_VkSystem.device);
vkWaitForFences(_VkSystem.device, 1, &_VkSystem.fence, 1, -1);
vkResetFences(_VkSystem.device, 1, &_VkSystem.fence);
res = vkAcquireNextImageKHR(_VkSystem.device, _VkSystem.swapchainKHR, -1, _VkSystem.semaphoreNextImage, VK_NULL_HANDLE, &_VkSystem.imageIndex);
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
// Begin CommandBuffer Recording
vkBeginCommandBuffer(_VkSystem.commandBuffer, &beginInfo);
static int temp;
if (!temp)
{
SetupGraphicsPipeline();
temp = 1;
}
VkDeviceSize devSize = {};
vkCmdBindPipeline(_VkSystem.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, _VkSystem.graphicsPipeline);
vkCmdBindVertexBuffers(_VkSystem.commandBuffer, 0, 1, &_VkSystem.buffer, &devSize);
// Begin RenderPass
BeginRenderPass(_VkSystem.renderPass, _VkSystem.frameBuffers[_VkSystem.imageIndex].frameBuffer,
_VkSystem.commandBuffer, 0, 0, 0);
vkCmdDraw(_VkSystem.commandBuffer, 3, 1, 0, 0);
// End RenderPass
vkCmdEndRenderPass(_VkSystem.commandBuffer);
// End CommandBuffer Recording
res = vkEndCommandBuffer(_VkSystem.commandBuffer);
if (res != VK_SUCCESS)
return 1;
if (SubmitToQueue(_VkSystem.queue.queue, _VkSystem.commandBuffer, _VkSystem.fence,
_VkSystem.semaphoreNextImage, _VkSystem.queue.semaphoreSubmit) != VK_SUCCESS)
return 1;
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pSwapchains = &_VkSystem.swapchainKHR;
presentInfo.swapchainCount = 1;
presentInfo.pWaitSemaphores = &_VkSystem.queue.semaphoreSubmit;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pImageIndices = &_VkSystem.imageIndex;
res = vkQueuePresentKHR(_VkSystem.queue.queue, &presentInfo);
if (res != VK_SUCCESS)
return 1;
//vkDestroySemaphore(_VkSystem.device, semaphore, 0);
}
|