7
7
*/
8
8
9
9
#include " ui.hpp"
10
- #include " helpers.hpp"
11
10
12
- #include " ../../external/imgui/imgui.h"
11
+ #include " vks/helpers.hpp"
12
+ #include " vks/pipelines.hpp"
13
13
14
- using namespace vks ;
15
- using namespace vks ::ui;
14
+ #include " ../external/imgui/imgui.h"
15
+ #include " utils.hpp"
16
+
17
+ using namespace vkx ;
18
+ using namespace vkx ::ui;
16
19
17
20
void UIOverlay::create (const UIOverlayCreateInfo& createInfo) {
18
21
this ->createInfo = createInfo;
19
22
#if defined(__ANDROID__)
20
23
if (android::screenDensity >= ACONFIGURATION_DENSITY_XXHIGH) {
21
24
scale = 3 .5f ;
22
- }
23
- else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
25
+ } else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
24
26
scale = 2 .5f ;
25
- }
26
- else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
27
+ } else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
27
28
scale = 2 .0f ;
28
29
};
29
30
#endif
@@ -47,15 +48,14 @@ void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
47
48
prepareResources ();
48
49
if (createInfo.renderPass ) {
49
50
renderPass = createInfo.renderPass ;
50
- }
51
- else {
51
+ } else {
52
52
prepareRenderPass ();
53
53
}
54
54
preparePipeline ();
55
55
}
56
56
57
57
/* * Free up all Vulkan resources acquired by the UI overlay */
58
- UIOverlay::~UIOverlay () { }
58
+ UIOverlay::~UIOverlay () {}
59
59
60
60
void UIOverlay::destroy () {
61
61
if (commandPool) {
@@ -76,8 +76,7 @@ void UIOverlay::destroy() {
76
76
}
77
77
78
78
/* * Prepare all vulkan resources required to render the UI overlay */
79
- void UIOverlay::prepareResources ()
80
- {
79
+ void UIOverlay::prepareResources () {
81
80
ImGuiIO& io = ImGui::GetIO ();
82
81
83
82
// Create font texture
@@ -178,76 +177,44 @@ void UIOverlay::prepareResources()
178
177
179
178
/* * Prepare a separate pipeline for the UI overlay rendering decoupled from the main application */
180
179
void UIOverlay::preparePipeline () {
181
-
182
180
// Setup graphics pipeline for UI rendering
183
- vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState{ {}, vk::PrimitiveTopology::eTriangleList } ;
184
- vk::PipelineRasterizationStateCreateInfo rasterizationState ;
185
- rasterizationState.lineWidth = 1 . 0f ;
181
+ vks::pipelines::GraphicsPipelineBuilder pipelineBuilder (context. device , pipelineLayout, renderPass) ;
182
+ pipelineBuilder. depthStencilState = { false } ;
183
+ pipelineBuilder. rasterizationState .cullMode = vk::CullModeFlagBits::eNone ;
186
184
187
185
// Enable blending
188
- vk::PipelineColorBlendAttachmentState blendAttachmentState;
189
- blendAttachmentState.blendEnable = VK_TRUE;
190
- blendAttachmentState.colorWriteMask = vks::util::fullColorWriteMask ();
191
- blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
192
- blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
193
- blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
194
- blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
195
-
196
- std::vector<vk::PipelineColorBlendAttachmentState> blendStates (createInfo.attachmentCount );
186
+ pipelineBuilder.colorBlendState .blendAttachmentStates .resize (createInfo.attachmentCount );
197
187
for (uint32_t i = 0 ; i < createInfo.attachmentCount ; i++) {
198
- blendStates[i] = blendAttachmentState;
188
+ auto & blendAttachmentState = pipelineBuilder.colorBlendState .blendAttachmentStates [i];
189
+ blendAttachmentState.blendEnable = VK_TRUE;
190
+ blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
191
+ blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
192
+ blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
193
+ blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
199
194
}
200
195
201
- vk::PipelineColorBlendStateCreateInfo colorBlendState;
202
- colorBlendState.attachmentCount = static_cast <uint32_t >(blendStates.size ());
203
- colorBlendState.pAttachments = blendStates.data ();
204
-
205
- vk::PipelineDepthStencilStateCreateInfo depthStencilState;
206
-
207
- vk::PipelineViewportStateCreateInfo viewportState{ {}, 1 , nullptr , 1 , nullptr };
208
- vk::PipelineMultisampleStateCreateInfo multisampleState;
209
- multisampleState.rasterizationSamples = createInfo.rasterizationSamples ;
210
-
211
-
212
- std::vector<vk::DynamicState> dynamicStateEnables = {
213
- vk::DynamicState::eViewport,
214
- vk::DynamicState::eScissor,
215
- };
216
- vk::PipelineDynamicStateCreateInfo dynamicState{ {}, (uint32_t )dynamicStateEnables.size (), dynamicStateEnables.data () };
217
-
218
- vk::GraphicsPipelineCreateInfo pipelineCreateInfo;
219
- pipelineCreateInfo.layout = pipelineLayout;
220
- pipelineCreateInfo.renderPass = renderPass;
221
- pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
222
- pipelineCreateInfo.pRasterizationState = &rasterizationState;
223
- pipelineCreateInfo.pColorBlendState = &colorBlendState;
224
- pipelineCreateInfo.pMultisampleState = &multisampleState;
225
- pipelineCreateInfo.pViewportState = &viewportState;
226
- pipelineCreateInfo.pDepthStencilState = &depthStencilState;
227
- pipelineCreateInfo.pDynamicState = &dynamicState;
228
- pipelineCreateInfo.stageCount = static_cast <uint32_t >(createInfo.shaders .size ());
229
- pipelineCreateInfo.pStages = createInfo.shaders .data ();
196
+ pipelineBuilder.multisampleState .rasterizationSamples = createInfo.rasterizationSamples ;
230
197
198
+ // Load default shaders if not specified by example
199
+ if (!createInfo.shaders .empty ()) {
200
+ pipelineBuilder.shaderStages = createInfo.shaders ;
201
+ } else {
202
+ pipelineBuilder.loadShader (vkx::getAssetPath () + " shaders/base/uioverlay.vert.spv" , vk::ShaderStageFlagBits::eVertex);
203
+ pipelineBuilder.loadShader (getAssetPath () + " shaders/base/uioverlay.frag.spv" , vk::ShaderStageFlagBits::eFragment);
204
+ }
231
205
232
206
// Vertex bindings an attributes based on ImGui vertex definition
233
- vk::VertexInputBindingDescription vertexInputBindings{ 0 , sizeof (ImDrawVert), vk::VertexInputRate::eVertex };
234
- std::vector<vk::VertexInputAttributeDescription> vertexInputAttributes = {
235
- vk::VertexInputAttributeDescription { 0 , 0 , vk::Format::eR32G32Sfloat, offsetof (ImDrawVert, pos) }, // Location 0: Position
236
- vk::VertexInputAttributeDescription { 1 , 0 , vk::Format::eR32G32Sfloat, offsetof (ImDrawVert, uv) }, // Location 1: UV
237
- vk::VertexInputAttributeDescription { 2 , 0 , vk::Format::eR8G8B8Unorm, offsetof (ImDrawVert, col) }, // Location 0: Color
207
+ pipelineBuilder. vertexInputState . bindingDescriptions = { { 0 , sizeof (ImDrawVert), vk::VertexInputRate::eVertex } };
208
+ pipelineBuilder. vertexInputState . attributeDescriptions = {
209
+ { 0 , 0 , vk::Format::eR32G32Sfloat, offsetof (ImDrawVert, pos) }, // Location 0: Position
210
+ { 1 , 0 , vk::Format::eR32G32Sfloat, offsetof (ImDrawVert, uv) }, // Location 1: UV
211
+ { 2 , 0 , vk::Format::eR8G8B8Unorm, offsetof (ImDrawVert, col) }, // Location 0: Color
238
212
};
239
- vk::PipelineVertexInputStateCreateInfo vertexInputState;
240
- vertexInputState.vertexBindingDescriptionCount = 1 ;
241
- vertexInputState.pVertexBindingDescriptions = &vertexInputBindings;
242
- vertexInputState.vertexAttributeDescriptionCount = static_cast <uint32_t >(vertexInputAttributes.size ());
243
- vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data ();
244
- pipelineCreateInfo.pVertexInputState = &vertexInputState;
245
- pipeline = context.device .createGraphicsPipeline (context.pipelineCache , pipelineCreateInfo);
213
+ pipeline = pipelineBuilder.create (context.pipelineCache );
246
214
}
247
215
248
216
/* * Prepare a separate render pass for rendering the UI as an overlay */
249
- void UIOverlay::prepareRenderPass ()
250
- {
217
+ void UIOverlay::prepareRenderPass () {
251
218
vk::AttachmentDescription attachments[2 ] = {};
252
219
253
220
// Color attachment
@@ -265,8 +232,8 @@ void UIOverlay::prepareRenderPass()
265
232
attachments[1 ].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
266
233
attachments[1 ].finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
267
234
268
- vk::AttachmentReference colorReference { 0 , vk::ImageLayout::eColorAttachmentOptimal };
269
- vk::AttachmentReference depthReference { 1 , vk::ImageLayout::eDepthStencilAttachmentOptimal };
235
+ vk::AttachmentReference colorReference{ 0 , vk::ImageLayout::eColorAttachmentOptimal };
236
+ vk::AttachmentReference depthReference{ 1 , vk::ImageLayout::eDepthStencilAttachmentOptimal };
270
237
vk::SubpassDependency subpassDependencies[2 ];
271
238
272
239
// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass)
0 commit comments