Skip to content

Commit 78b3b8d

Browse files
committed
port the createGraphicsPipeline function
1 parent 07c95d9 commit 78b3b8d

File tree

5 files changed

+261
-47
lines changed

5 files changed

+261
-47
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ https://www.twitch.tv/andrewrok
88
## Building
99

1010
```
11+
glslc -o vert.spv shader.vert
12+
glslc -o frag.spv shader.frag
1113
zig build run
1214
```
15+
16+
## TODO
17+
18+
* finish porting the code
19+
* make zig build compile the shaders from source

shaders/shader.frag

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 450
2+
#extension GL_ARB_separate_shader_objects : enable
3+
4+
layout(location = 0) in vec3 fragColor;
5+
6+
layout(location = 0) out vec4 outColor;
7+
8+
void main() {
9+
outColor = vec4(fragColor, 1.0);
10+
}

shaders/shader.vert

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 450
2+
#extension GL_ARB_separate_shader_objects : enable
3+
4+
out gl_PerVertex {
5+
vec4 gl_Position;
6+
};
7+
8+
layout(location = 0) out vec3 fragColor;
9+
10+
vec2 positions[3] = vec2[](
11+
vec2(0.0, -0.5),
12+
vec2(0.5, 0.5),
13+
vec2(-0.5, 0.5)
14+
);
15+
16+
vec3 colors[3] = vec3[](
17+
vec3(1.0, 0.0, 0.0),
18+
vec3(0.0, 1.0, 0.0),
19+
vec3(0.0, 0.0, 1.0)
20+
);
21+
22+
void main() {
23+
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
24+
fragColor = colors[gl_VertexIndex];
25+
}
26+
27+

src/main.zig

Lines changed: 200 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var swapChainImageFormat: c.VkFormat = undefined;
2828
var swapChainExtent: c.VkExtent2D = undefined;
2929
var swapChainImageViews: []c.VkImageView = undefined;
3030
var renderPass: c.VkRenderPass = undefined;
31+
var pipelineLayout: c.VkPipelineLayout = undefined;
32+
var graphicsPipeline: c.VkPipeline = undefined;
3133

3234
const QueueFamilyIndices = struct {
3335
graphicsFamily: ?u32,
@@ -82,9 +84,9 @@ pub fn main() !void {
8284

8385
while (c.glfwWindowShouldClose(window) == 0) {
8486
c.glfwPollEvents();
85-
//drawFrame();
87+
drawFrame();
8688
}
87-
//c.vkDeviceWaitIdle(device);
89+
try checkSuccess(c.vkDeviceWaitIdle(global_device));
8890

8991
std.debug.warn("TODO port the cleanup function");
9092
}
@@ -98,14 +100,209 @@ fn initVulkan(allocator: *Allocator, window: *c.GLFWwindow) !void {
98100
try createSwapChain(allocator);
99101
try createImageViews(allocator);
100102
try createRenderPass();
103+
try createGraphicsPipeline(allocator);
101104
// TODO
102-
//createGraphicsPipeline();
103105
//createFramebuffers();
104106
//createCommandPool();
105107
//createCommandBuffers();
106108
//createSyncObjects();
107109
}
108110

111+
fn createShaderModule(code: []align(@alignOf(u32)) const u8) !c.VkShaderModule {
112+
const createInfo = c.VkShaderModuleCreateInfo{
113+
.sType = c.VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
114+
.codeSize = code.len,
115+
.pCode = @bytesToSlice(u32, code).ptr,
116+
117+
.pNext = null,
118+
.flags = 0,
119+
};
120+
121+
var shaderModule: c.VkShaderModule = undefined;
122+
try checkSuccess(c.vkCreateShaderModule(global_device, &createInfo, null, &shaderModule));
123+
124+
return shaderModule;
125+
}
126+
127+
fn createGraphicsPipeline(allocator: *Allocator) !void {
128+
const vertShaderCode = try std.io.readFileAllocAligned(allocator, "shaders/vert.spv", @alignOf(u32));
129+
defer allocator.free(vertShaderCode);
130+
131+
const fragShaderCode = try std.io.readFileAllocAligned(allocator, "shaders/frag.spv", @alignOf(u32));
132+
defer allocator.free(fragShaderCode);
133+
134+
const vertShaderModule = try createShaderModule(vertShaderCode);
135+
const fragShaderModule = try createShaderModule(fragShaderCode);
136+
137+
const vertShaderStageInfo = c.VkPipelineShaderStageCreateInfo{
138+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
139+
.stage = c.VK_SHADER_STAGE_VERTEX_BIT,
140+
.module = vertShaderModule,
141+
.pName = c"main",
142+
143+
.pNext = null,
144+
.flags = 0,
145+
.pSpecializationInfo = null,
146+
};
147+
148+
const fragShaderStageInfo = c.VkPipelineShaderStageCreateInfo{
149+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
150+
.stage = c.VK_SHADER_STAGE_FRAGMENT_BIT,
151+
.module = fragShaderModule,
152+
.pName = c"main",
153+
.pNext = null,
154+
.flags = 0,
155+
156+
.pSpecializationInfo = null,
157+
};
158+
159+
const shaderStages = []c.VkPipelineShaderStageCreateInfo{ vertShaderStageInfo, fragShaderStageInfo };
160+
161+
const vertexInputInfo = c.VkPipelineVertexInputStateCreateInfo{
162+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
163+
.vertexBindingDescriptionCount = 0,
164+
.vertexAttributeDescriptionCount = 0,
165+
166+
.pVertexBindingDescriptions = null,
167+
.pVertexAttributeDescriptions = null,
168+
.pNext = null,
169+
.flags = 0,
170+
};
171+
172+
const inputAssembly = c.VkPipelineInputAssemblyStateCreateInfo{
173+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
174+
.topology = c.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
175+
.primitiveRestartEnable = c.VK_FALSE,
176+
.pNext = null,
177+
.flags = 0,
178+
};
179+
180+
const viewport = []c.VkViewport{c.VkViewport{
181+
.x = 0.0,
182+
.y = 0.0,
183+
.width = @intToFloat(f32, swapChainExtent.width),
184+
.height = @intToFloat(f32, swapChainExtent.height),
185+
.minDepth = 0.0,
186+
.maxDepth = 1.0,
187+
}};
188+
189+
const scissor = []c.VkRect2D{c.VkRect2D{
190+
.offset = c.VkOffset2D{ .x = 0, .y = 0 },
191+
.extent = swapChainExtent,
192+
}};
193+
194+
const viewportState = c.VkPipelineViewportStateCreateInfo{
195+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
196+
.viewportCount = 1,
197+
.pViewports = &viewport,
198+
.scissorCount = 1,
199+
.pScissors = &scissor,
200+
201+
.pNext = null,
202+
.flags = 0,
203+
};
204+
205+
const rasterizer = c.VkPipelineRasterizationStateCreateInfo{
206+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
207+
.depthClampEnable = c.VK_FALSE,
208+
.rasterizerDiscardEnable = c.VK_FALSE,
209+
.polygonMode = c.VK_POLYGON_MODE_FILL,
210+
.lineWidth = 1.0,
211+
.cullMode = @intCast(u32, @enumToInt(c.VK_CULL_MODE_BACK_BIT)),
212+
.frontFace = c.VK_FRONT_FACE_CLOCKWISE,
213+
.depthBiasEnable = c.VK_FALSE,
214+
215+
.pNext = null,
216+
.flags = 0,
217+
.depthBiasConstantFactor = 0,
218+
.depthBiasClamp = 0,
219+
.depthBiasSlopeFactor = 0,
220+
};
221+
222+
const multisampling = c.VkPipelineMultisampleStateCreateInfo{
223+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
224+
.sampleShadingEnable = c.VK_FALSE,
225+
.rasterizationSamples = c.VK_SAMPLE_COUNT_1_BIT,
226+
.pNext = null,
227+
.flags = 0,
228+
.minSampleShading = 0,
229+
.pSampleMask = null,
230+
.alphaToCoverageEnable = 0,
231+
.alphaToOneEnable = 0,
232+
};
233+
234+
const colorBlendAttachment = c.VkPipelineColorBlendAttachmentState{
235+
.colorWriteMask = c.VK_COLOR_COMPONENT_R_BIT | c.VK_COLOR_COMPONENT_G_BIT | c.VK_COLOR_COMPONENT_B_BIT | c.VK_COLOR_COMPONENT_A_BIT,
236+
.blendEnable = c.VK_FALSE,
237+
238+
.srcColorBlendFactor = c.VK_BLEND_FACTOR_ZERO,
239+
.dstColorBlendFactor = c.VK_BLEND_FACTOR_ZERO,
240+
.colorBlendOp = c.VK_BLEND_OP_ADD,
241+
.srcAlphaBlendFactor = c.VK_BLEND_FACTOR_ZERO,
242+
.dstAlphaBlendFactor = c.VK_BLEND_FACTOR_ZERO,
243+
.alphaBlendOp = c.VK_BLEND_OP_ADD,
244+
};
245+
246+
const colorBlending = c.VkPipelineColorBlendStateCreateInfo{
247+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
248+
.logicOpEnable = c.VK_FALSE,
249+
.logicOp = c.VK_LOGIC_OP_COPY,
250+
.attachmentCount = 1,
251+
.pAttachments = &colorBlendAttachment,
252+
.blendConstants = []f32{ 0, 0, 0, 0 },
253+
254+
.pNext = null,
255+
.flags = 0,
256+
};
257+
258+
const pipelineLayoutInfo = c.VkPipelineLayoutCreateInfo{
259+
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
260+
.setLayoutCount = 0,
261+
.pushConstantRangeCount = 0,
262+
.pNext = null,
263+
.flags = 0,
264+
.pSetLayouts = null,
265+
.pPushConstantRanges = null,
266+
};
267+
268+
try checkSuccess(c.vkCreatePipelineLayout(global_device, &pipelineLayoutInfo, null, &pipelineLayout));
269+
270+
const pipelineInfo = []c.VkGraphicsPipelineCreateInfo{c.VkGraphicsPipelineCreateInfo{
271+
.sType = c.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
272+
.stageCount = @intCast(u32, shaderStages.len),
273+
.pStages = &shaderStages,
274+
.pVertexInputState = &vertexInputInfo,
275+
.pInputAssemblyState = &inputAssembly,
276+
.pViewportState = &viewportState,
277+
.pRasterizationState = &rasterizer,
278+
.pMultisampleState = &multisampling,
279+
.pColorBlendState = &colorBlending,
280+
.layout = pipelineLayout,
281+
.renderPass = renderPass,
282+
.subpass = 0,
283+
.basePipelineHandle = null,
284+
285+
.pNext = null,
286+
.flags = 0,
287+
.pTessellationState = null,
288+
.pDepthStencilState = null,
289+
.pDynamicState = null,
290+
.basePipelineIndex = 0,
291+
}};
292+
293+
try checkSuccess(c.vkCreateGraphicsPipelines(
294+
global_device,
295+
null,
296+
@intCast(u32, pipelineInfo.len),
297+
&pipelineInfo,
298+
null,
299+
(*[1]c.VkPipeline)(&graphicsPipeline),
300+
));
301+
302+
c.vkDestroyShaderModule(global_device, fragShaderModule, null);
303+
c.vkDestroyShaderModule(global_device, vertShaderModule, null);
304+
}
305+
109306
fn createRenderPass() !void {
110307
const colorAttachment = c.VkAttachmentDescription{
111308
.format = swapChainImageFormat,

0 commit comments

Comments
 (0)