-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathspirv_assembly.cpp
192 lines (167 loc) · 7.27 KB
/
spirv_assembly.cpp
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
/*
* Vulkan Samples
*
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
VULKAN_SAMPLE_SHORT_DESCRIPTION
Demonstrate how to use SPIR-V shaders with inline assembly.
*/
#include <util_init.hpp>
#include <assert.h>
#include <string.h>
#include <cstdlib>
#include "cube_data.h"
// clang-format off
// This sample is based on the template, but instead of using inline GLSL and calls to
// glslang to generate SPIR-V binaries, we use inline assembly and pass it to the
// SPIRV-Tools assembler. This is one of many ways to generate SPIR-V binaries,
// which is the only shader representation accepted by Vulkan.
// The SPIR-V assembly in spirv_assembly.vert and spirv_assembly.frag was generated by:
// Populating template.vert and template.frag with contents of inlined GLSL from template sample
// Running the following commands on Linux:
// ./glslang/build/Standalone/bin/glslangValidator -V ./API-Samples/template.vert -o ./API-Samples/template.vert.spv
// ./glslang/build/Standalone/bin/glslangValidator -V ./API-Samples/template.frag -o ./API-Samples/template.frag.spv
// ./spirv-tools/build/spirv-dis ./API-Samples/template.vert.spv | sed -e 's/\"/\\\"/g' -e 's/.*/\"&\\n\"/'
// ./spirv-tools/build/spirv-dis ./API-Samples/template.frag.spv | sed -e 's/\"/\\\"/g' -e 's/.*/\"&\\n\"/'
// clang-format on
int sample_main(int argc, char *argv[]) {
VkResult U_ASSERT_ONLY res;
struct sample_info info = {};
char sample_title[] = "SPIR-V Assembly";
const bool depthPresent = true;
process_command_line_args(info, argc, argv);
init_global_layer_properties(info);
init_instance_extension_names(info);
init_device_extension_names(info);
init_instance(info, sample_title);
init_enumerate_device(info);
init_window_size(info, 500, 500);
init_connection(info);
init_window(info);
init_swapchain_extension(info);
init_device(info);
init_command_pool(info);
init_command_buffer(info);
execute_begin_command_buffer(info);
init_device_queue(info);
init_swap_chain(info);
init_depth_buffer(info);
init_texture(info);
init_uniform_buffer(info);
init_descriptor_and_pipeline_layouts(info, true);
init_renderpass(info, depthPresent);
/* VULKAN_KEY_START */
// Init the assembler context
#include "spirv_assembly.vert.h"
#include "spirv_assembly.frag.h"
info.shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
info.shaderStages[0].pNext = NULL;
info.shaderStages[0].pSpecializationInfo = NULL;
info.shaderStages[0].flags = 0;
info.shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
info.shaderStages[0].pName = "main";
VkShaderModuleCreateInfo moduleCreateInfo;
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleCreateInfo.pNext = NULL;
moduleCreateInfo.flags = 0;
// Use wordCount and code pointers from the spv_binary
moduleCreateInfo.codeSize = sizeof(spirv_assembly_vert);
moduleCreateInfo.pCode = spirv_assembly_vert;
res = vkCreateShaderModule(info.device, &moduleCreateInfo, NULL, &info.shaderStages[0].module);
assert(res == VK_SUCCESS);
info.shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
info.shaderStages[1].pNext = NULL;
info.shaderStages[1].pSpecializationInfo = NULL;
info.shaderStages[1].flags = 0;
info.shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
info.shaderStages[1].pName = "main";
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleCreateInfo.pNext = NULL;
moduleCreateInfo.flags = 0;
// Use wordCount and code pointers from the spv_binary
moduleCreateInfo.codeSize = sizeof(spirv_assembly_frag);
moduleCreateInfo.pCode = spirv_assembly_frag;
res = vkCreateShaderModule(info.device, &moduleCreateInfo, NULL, &info.shaderStages[1].module);
assert(res == VK_SUCCESS);
/* VULKAN_KEY_END */
init_framebuffers(info, depthPresent);
init_vertex_buffer(info, g_vb_texture_Data, sizeof(g_vb_texture_Data), sizeof(g_vb_texture_Data[0]), true);
init_descriptor_pool(info, true);
init_descriptor_set(info, true);
init_pipeline_cache(info);
init_pipeline(info, depthPresent);
init_presentable_image(info);
VkClearValue clear_values[2];
init_clear_color_and_depth(info, clear_values);
VkRenderPassBeginInfo rp_begin;
init_render_pass_begin_info(info, rp_begin);
rp_begin.clearValueCount = 2;
rp_begin.pClearValues = clear_values;
vkCmdBeginRenderPass(info.cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
vkCmdBindDescriptorSets(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline_layout, 0, NUM_DESCRIPTOR_SETS,
info.desc_set.data(), 0, NULL);
const VkDeviceSize offsets[1] = {0};
vkCmdBindVertexBuffers(info.cmd, 0, 1, &info.vertex_buffer.buf, offsets);
init_viewports(info);
init_scissors(info);
vkCmdDraw(info.cmd, 12 * 3, 1, 0, 0);
vkCmdEndRenderPass(info.cmd);
res = vkEndCommandBuffer(info.cmd);
assert(res == VK_SUCCESS);
VkFence drawFence = {};
init_fence(info, drawFence);
VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo submit_info = {};
init_submit_info(info, submit_info, pipe_stage_flags);
/* Queue the command buffer for execution */
res = vkQueueSubmit(info.graphics_queue, 1, &submit_info, drawFence);
assert(res == VK_SUCCESS);
/* Now present the image in the window */
VkPresentInfoKHR present = {};
init_present_info(info, present);
/* Make sure command buffer is finished before presenting */
do {
res = vkWaitForFences(info.device, 1, &drawFence, VK_TRUE, FENCE_TIMEOUT);
} while (res == VK_TIMEOUT);
assert(res == VK_SUCCESS);
res = vkQueuePresentKHR(info.present_queue, &present);
assert(res == VK_SUCCESS);
wait_seconds(1);
if (info.save_images) write_ppm(info, "spirv_assembly");
vkDestroyFence(info.device, drawFence, NULL);
vkDestroySemaphore(info.device, info.imageAcquiredSemaphore, NULL);
destroy_pipeline(info);
destroy_pipeline_cache(info);
destroy_textures(info);
destroy_descriptor_pool(info);
destroy_vertex_buffer(info);
destroy_framebuffers(info);
destroy_shaders(info);
destroy_renderpass(info);
destroy_descriptor_and_pipeline_layouts(info);
destroy_uniform_buffer(info);
destroy_depth_buffer(info);
destroy_swap_chain(info);
destroy_command_buffer(info);
destroy_command_pool(info);
destroy_device(info);
destroy_window(info);
destroy_instance(info);
return 0;
}