PeriDyno 1.0.0
Loading...
Searching...
No Matches
VulkanUIOverlay.cpp
Go to the documentation of this file.
1
2/*
3* UI overlay class using ImGui
4*
5* Copyright (C) 2017 by Sascha Willems - www.saschawillems.de
6*
7* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
8*/
9
10#include "VulkanUIOverlay.h"
11#include "VkSystem.h"
12
13namespace vks
14{
16 {
17#if defined(__ANDROID__)
18 if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_XXHIGH) {
19 scale = 3.5f;
20 }
21 else if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
22 scale = 2.5f;
23 }
24 else if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
25 scale = 2.0f;
26 };
27#endif
28
29 // Init ImGui
30 ImGui::CreateContext();
31 // Color scheme
32 ImGuiStyle& style = ImGui::GetStyle();
33 style.Colors[ImGuiCol_TitleBg] = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
34 style.Colors[ImGuiCol_TitleBgActive] = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
35 style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.0f, 0.0f, 0.0f, 0.1f);
36 style.Colors[ImGuiCol_MenuBarBg] = ImVec4(1.0f, 0.0f, 0.0f, 0.4f);
37 style.Colors[ImGuiCol_Header] = ImVec4(0.8f, 0.0f, 0.0f, 0.4f);
38 style.Colors[ImGuiCol_HeaderActive] = ImVec4(1.0f, 0.0f, 0.0f, 0.4f);
39 style.Colors[ImGuiCol_HeaderHovered] = ImVec4(1.0f, 0.0f, 0.0f, 0.4f);
40 style.Colors[ImGuiCol_FrameBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.8f);
41 style.Colors[ImGuiCol_CheckMark] = ImVec4(1.0f, 0.0f, 0.0f, 0.8f);
42 style.Colors[ImGuiCol_SliderGrab] = ImVec4(1.0f, 0.0f, 0.0f, 0.4f);
43 style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(1.0f, 0.0f, 0.0f, 0.8f);
44 style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(1.0f, 1.0f, 1.0f, 0.1f);
45 style.Colors[ImGuiCol_FrameBgActive] = ImVec4(1.0f, 1.0f, 1.0f, 0.2f);
46 style.Colors[ImGuiCol_Button] = ImVec4(1.0f, 0.0f, 0.0f, 0.4f);
47 style.Colors[ImGuiCol_ButtonHovered] = ImVec4(1.0f, 0.0f, 0.0f, 0.6f);
48 style.Colors[ImGuiCol_ButtonActive] = ImVec4(1.0f, 0.0f, 0.0f, 0.8f);
49 // Dimensions
50 ImGuiIO& io = ImGui::GetIO();
51 io.FontGlobalScale = scale;
52
54
55 vertexBuffer = std::make_shared<vks::Buffer>();
56 indexBuffer = std::make_shared<vks::Buffer>();
57 }
58
60
63 {
64 ImGuiIO& io = ImGui::GetIO();
65
66 // Create font texture
67 unsigned char* fontData;
68 int texWidth, texHeight;
69#if defined(__ANDROID__)
70 float scale = (float)vks::android::screenDensity / (float)ACONFIGURATION_DENSITY_MEDIUM;
71 AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, "Roboto-Medium.ttf", AASSET_MODE_STREAMING);
72 if (asset) {
73 size_t size = AAsset_getLength(asset);
74 assert(size > 0);
75 char *fontAsset = new char[size];
76 AAsset_read(asset, fontAsset, size);
77 AAsset_close(asset);
78 io.Fonts->AddFontFromMemoryTTF(fontAsset, size, 14.0f * scale);
79 delete[] fontAsset;
80 }
81#else
82 const std::string filename = getAssetPath() + "Roboto-Medium.ttf";
83 io.Fonts->AddFontFromFileTTF(filename.c_str(), 16.0f);
84#endif
85 io.Fonts->GetTexDataAsRGBA32(&fontData, &texWidth, &texHeight);
86 VkDeviceSize uploadSize = texWidth*texHeight * 4 * sizeof(char);
87
88 // Create target image for copy
89 VkImageCreateInfo imageInfo = vks::initializers::imageCreateInfo();
90 imageInfo.imageType = VK_IMAGE_TYPE_2D;
91 imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
92 imageInfo.extent.width = texWidth;
93 imageInfo.extent.height = texHeight;
94 imageInfo.extent.depth = 1;
95 imageInfo.mipLevels = 1;
96 imageInfo.arrayLayers = 1;
97 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
98 imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
99 imageInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
100 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
101 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
102 VK_CHECK_RESULT(vkCreateImage(ctx->deviceHandle(), &imageInfo, nullptr, &fontImage));
103 VkMemoryRequirements memReqs;
104 vkGetImageMemoryRequirements(ctx->deviceHandle(), fontImage, &memReqs);
105 VkMemoryAllocateInfo memAllocInfo = vks::initializers::memoryAllocateInfo();
106 memAllocInfo.allocationSize = memReqs.size;
107 memAllocInfo.memoryTypeIndex = ctx->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
108 VK_CHECK_RESULT(vkAllocateMemory(ctx->deviceHandle(), &memAllocInfo, nullptr, &fontMemory));
109 VK_CHECK_RESULT(vkBindImageMemory(ctx->deviceHandle(), fontImage, fontMemory, 0));
110
111 // Image view
112 VkImageViewCreateInfo viewInfo = vks::initializers::imageViewCreateInfo();
113 viewInfo.image = fontImage;
114 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
115 viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
116 viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
117 viewInfo.subresourceRange.levelCount = 1;
118 viewInfo.subresourceRange.layerCount = 1;
119 VK_CHECK_RESULT(vkCreateImageView(ctx->deviceHandle(), &viewInfo, nullptr, &fontView));
120
121 // Staging buffers for font data upload
122 std::shared_ptr<vks::Buffer> stagingBuffer = std::make_shared<vks::Buffer>();
123
124 VK_CHECK_RESULT(ctx->createBuffer(
125 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
126 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
127 stagingBuffer,
128 uploadSize));
129
130 stagingBuffer->map();
131 memcpy(stagingBuffer->mapped, fontData, uploadSize);
132 stagingBuffer->unmap();
133
134 // Copy buffer data to font image
135 VkCommandBuffer copyCmd = ctx->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
136
137 // Prepare for transfer
139 copyCmd,
140 fontImage,
141 VK_IMAGE_ASPECT_COLOR_BIT,
142 VK_IMAGE_LAYOUT_UNDEFINED,
143 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
144 VK_PIPELINE_STAGE_HOST_BIT,
145 VK_PIPELINE_STAGE_TRANSFER_BIT);
146
147 // Copy
148 VkBufferImageCopy bufferCopyRegion = {};
149 bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
150 bufferCopyRegion.imageSubresource.layerCount = 1;
151 bufferCopyRegion.imageExtent.width = texWidth;
152 bufferCopyRegion.imageExtent.height = texHeight;
153 bufferCopyRegion.imageExtent.depth = 1;
154
155 vkCmdCopyBufferToImage(
156 copyCmd,
157 stagingBuffer->buffer,
158 fontImage,
159 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
160 1,
161 &bufferCopyRegion
162 );
163
164 // Prepare for shader read
166 copyCmd,
167 fontImage,
168 VK_IMAGE_ASPECT_COLOR_BIT,
169 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
170 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
171 VK_PIPELINE_STAGE_TRANSFER_BIT,
172 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
173
174 ctx->flushCommandBuffer(copyCmd, ctx->graphicsQueueHandle(), true);
175
176 stagingBuffer->destroy();
177
178 // Font texture Sampler
179 VkSamplerCreateInfo samplerInfo = vks::initializers::samplerCreateInfo();
180 samplerInfo.magFilter = VK_FILTER_LINEAR;
181 samplerInfo.minFilter = VK_FILTER_LINEAR;
182 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
183 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
184 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
185 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
186 samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
187 VK_CHECK_RESULT(vkCreateSampler(ctx->deviceHandle(), &samplerInfo, nullptr, &sampler));
188
189 // Descriptor pool
190 std::vector<VkDescriptorPoolSize> poolSizes = {
191 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1)
192 };
193 VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
194 VK_CHECK_RESULT(vkCreateDescriptorPool(ctx->deviceHandle(), &descriptorPoolInfo, nullptr, &descriptorPool));
195
196 // Descriptor set layout
197 std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
198 vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0),
199 };
200 VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
201 VK_CHECK_RESULT(vkCreateDescriptorSetLayout(ctx->deviceHandle(), &descriptorLayout, nullptr, &descriptorSetLayout));
202
203 // Descriptor set
204 VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
205 VK_CHECK_RESULT(vkAllocateDescriptorSets(ctx->deviceHandle(), &allocInfo, &descriptorSet));
206 VkDescriptorImageInfo fontDescriptor = vks::initializers::descriptorImageInfo(
207 sampler,
208 fontView,
209 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
210 );
211 std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
212 vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &fontDescriptor)
213 };
214 vkUpdateDescriptorSets(ctx->deviceHandle(), static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
215 }
216
218 void UIOverlay::preparePipeline(const VkPipelineCache pipelineCache, const VkRenderPass renderPass)
219 {
220 // Pipeline layout
221 // Push constants for UI rendering parameters
222 VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(PushConstBlock), 0);
223 VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
224 pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
225 pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
226 VK_CHECK_RESULT(vkCreatePipelineLayout(ctx->deviceHandle(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
227
228 // Setup graphics pipeline for UI rendering
229 VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
230 vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
231
232 VkPipelineRasterizationStateCreateInfo rasterizationState =
233 vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE);
234
235 // Enable blending
236 VkPipelineColorBlendAttachmentState blendAttachmentState{};
237 blendAttachmentState.blendEnable = VK_TRUE;
238 blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
239 blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
240 blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
241 blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
242 blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
243 blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
244 blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
245
246 VkPipelineColorBlendStateCreateInfo colorBlendState =
248
249 VkPipelineDepthStencilStateCreateInfo depthStencilState =
250 vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, VK_COMPARE_OP_ALWAYS);
251
252 VkPipelineViewportStateCreateInfo viewportState =
254
255 VkPipelineMultisampleStateCreateInfo multisampleState =
257
258 std::vector<VkDynamicState> dynamicStateEnables = {
259 VK_DYNAMIC_STATE_VIEWPORT,
260 VK_DYNAMIC_STATE_SCISSOR
261 };
262 VkPipelineDynamicStateCreateInfo dynamicState =
264
265 VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass);
266
267 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
268 pipelineCreateInfo.pRasterizationState = &rasterizationState;
269 pipelineCreateInfo.pColorBlendState = &colorBlendState;
270 pipelineCreateInfo.pMultisampleState = &multisampleState;
271 pipelineCreateInfo.pViewportState = &viewportState;
272 pipelineCreateInfo.pDepthStencilState = &depthStencilState;
273 pipelineCreateInfo.pDynamicState = &dynamicState;
274 pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaders.size());
275 pipelineCreateInfo.pStages = shaders.data();
276 pipelineCreateInfo.subpass = subpass;
277
278 // Vertex bindings an attributes based on ImGui vertex definition
279 std::vector<VkVertexInputBindingDescription> vertexInputBindings = {
280 vks::initializers::vertexInputBindingDescription(0, sizeof(ImDrawVert), VK_VERTEX_INPUT_RATE_VERTEX),
281 };
282 std::vector<VkVertexInputAttributeDescription> vertexInputAttributes = {
283 vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(ImDrawVert, pos)), // Location 0: Position
284 vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(ImDrawVert, uv)), // Location 1: UV
285 vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R8G8B8A8_UNORM, offsetof(ImDrawVert, col)), // Location 0: Color
286 };
287 VkPipelineVertexInputStateCreateInfo vertexInputState = vks::initializers::pipelineVertexInputStateCreateInfo();
288 vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexInputBindings.size());
289 vertexInputState.pVertexBindingDescriptions = vertexInputBindings.data();
290 vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size());
291 vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data();
292
293 pipelineCreateInfo.pVertexInputState = &vertexInputState;
294
295 VK_CHECK_RESULT(vkCreateGraphicsPipelines(ctx->deviceHandle(), pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
296 }
297
300 {
301 ImDrawData* imDrawData = ImGui::GetDrawData();
302 bool updateCmdBuffers = false;
303
304 if (!imDrawData) { return false; };
305
306 // Note: Alignment is done inside buffer creation
307 VkDeviceSize vertexBufferSize = imDrawData->TotalVtxCount * sizeof(ImDrawVert);
308 VkDeviceSize indexBufferSize = imDrawData->TotalIdxCount * sizeof(ImDrawIdx);
309
310 // Update buffers only if vertex or index count has been changed compared to current buffer size
311 if ((vertexBufferSize == 0) || (indexBufferSize == 0)) {
312 return false;
313 }
314
315 // Vertex buffer
316 if ((vertexBuffer->buffer == VK_NULL_HANDLE) || (vertexCount != imDrawData->TotalVtxCount)) {
317 vertexBuffer->unmap();
318 vertexBuffer->destroy();
319 VK_CHECK_RESULT(ctx->createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, vertexBuffer, vertexBufferSize));
320 vertexCount = imDrawData->TotalVtxCount;
321 vertexBuffer->unmap();
322 vertexBuffer->map();
323 updateCmdBuffers = true;
324 }
325
326 // Index buffer
327 VkDeviceSize indexSize = imDrawData->TotalIdxCount * sizeof(ImDrawIdx);
328 if ((indexBuffer->buffer == VK_NULL_HANDLE) || (indexCount < imDrawData->TotalIdxCount)) {
329 indexBuffer->unmap();
330 indexBuffer->destroy();
331 VK_CHECK_RESULT(ctx->createBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indexBuffer, indexBufferSize));
332 indexCount = imDrawData->TotalIdxCount;
333 indexBuffer->map();
334 updateCmdBuffers = true;
335 }
336
337 // Upload data
338 ImDrawVert* vtxDst = (ImDrawVert*)vertexBuffer->mapped;
339 ImDrawIdx* idxDst = (ImDrawIdx*)indexBuffer->mapped;
340
341 for (int n = 0; n < imDrawData->CmdListsCount; n++) {
342 const ImDrawList* cmd_list = imDrawData->CmdLists[n];
343 memcpy(vtxDst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
344 memcpy(idxDst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
345 vtxDst += cmd_list->VtxBuffer.Size;
346 idxDst += cmd_list->IdxBuffer.Size;
347 }
348
349 // Flush to make writes visible to GPU
350 vertexBuffer->flush();
351 indexBuffer->flush();
352
353 return updateCmdBuffers;
354 }
355
356 void UIOverlay::draw(const VkCommandBuffer commandBuffer)
357 {
358 ImDrawData* imDrawData = ImGui::GetDrawData();
359 int32_t vertexOffset = 0;
360 int32_t indexOffset = 0;
361
362 if ((!imDrawData) || (imDrawData->CmdListsCount == 0)) {
363 return;
364 }
365
366 ImGuiIO& io = ImGui::GetIO();
367
368 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
369 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
370
371 pushConstBlock.scale = glm::vec2(2.0f / io.DisplaySize.x, 2.0f / io.DisplaySize.y);
372 pushConstBlock.translate = glm::vec2(-1.0f);
373 vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(PushConstBlock), &pushConstBlock);
374
375 VkDeviceSize offsets[1] = { 0 };
376 vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer->buffer, offsets);
377 vkCmdBindIndexBuffer(commandBuffer, indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT16);
378
379 for (int32_t i = 0; i < imDrawData->CmdListsCount; i++)
380 {
381 const ImDrawList* cmd_list = imDrawData->CmdLists[i];
382 for (int32_t j = 0; j < cmd_list->CmdBuffer.Size; j++)
383 {
384 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[j];
385 VkRect2D scissorRect;
386 scissorRect.offset.x = std::max((int32_t)(pcmd->ClipRect.x), 0);
387 scissorRect.offset.y = std::max((int32_t)(pcmd->ClipRect.y), 0);
388 scissorRect.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
389 scissorRect.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y);
390 vkCmdSetScissor(commandBuffer, 0, 1, &scissorRect);
391 vkCmdDrawIndexed(commandBuffer, pcmd->ElemCount, 1, indexOffset, vertexOffset, 0);
392 indexOffset += pcmd->ElemCount;
393 }
394 vertexOffset += cmd_list->VtxBuffer.Size;
395 }
396 }
397
398 void UIOverlay::resize(uint32_t width, uint32_t height)
399 {
400 ImGuiIO& io = ImGui::GetIO();
401 io.DisplaySize = ImVec2((float)(width), (float)(height));
402 }
403
405 {
406 ImGui::DestroyContext();
407 vertexBuffer->destroy();
408 indexBuffer->destroy();
409 vkDestroyImageView(ctx->deviceHandle(), fontView, nullptr);
410 vkDestroyImage(ctx->deviceHandle(), fontImage, nullptr);
411 vkFreeMemory(ctx->deviceHandle(), fontMemory, nullptr);
412 vkDestroySampler(ctx->deviceHandle(), sampler, nullptr);
413 vkDestroyDescriptorSetLayout(ctx->deviceHandle(), descriptorSetLayout, nullptr);
414 vkDestroyDescriptorPool(ctx->deviceHandle(), descriptorPool, nullptr);
415 vkDestroyPipelineLayout(ctx->deviceHandle(), pipelineLayout, nullptr);
416 vkDestroyPipeline(ctx->deviceHandle(), pipeline, nullptr);
417 }
418
419 bool UIOverlay::header(const char *caption)
420 {
421 return ImGui::CollapsingHeader(caption, ImGuiTreeNodeFlags_DefaultOpen);
422 }
423
424 bool UIOverlay::checkBox(const char *caption, bool *value)
425 {
426 bool res = ImGui::Checkbox(caption, value);
427 if (res) { updated = true; };
428 return res;
429 }
430
431 bool UIOverlay::checkBox(const char *caption, int32_t *value)
432 {
433 bool val = (*value == 1);
434 bool res = ImGui::Checkbox(caption, &val);
435 *value = val;
436 if (res) { updated = true; };
437 return res;
438 }
439
440 bool UIOverlay::inputFloat(const char *caption, float *value, float step, uint32_t precision)
441 {
442 bool res = ImGui::InputFloat(caption, value, step, step * 10.0f);
443 if (res) { updated = true; };
444 return res;
445 }
446
447 bool UIOverlay::sliderFloat(const char* caption, float* value, float min, float max)
448 {
449 bool res = ImGui::SliderFloat(caption, value, min, max);
450 if (res) { updated = true; };
451 return res;
452 }
453
454 bool UIOverlay::sliderInt(const char* caption, int32_t* value, int32_t min, int32_t max)
455 {
456 bool res = ImGui::SliderInt(caption, value, min, max);
457 if (res) { updated = true; };
458 return res;
459 }
460
461 bool UIOverlay::comboBox(const char *caption, int32_t *itemindex, std::vector<std::string> items)
462 {
463 if (items.empty()) {
464 return false;
465 }
466 std::vector<const char*> charitems;
467 charitems.reserve(items.size());
468 for (size_t i = 0; i < items.size(); i++) {
469 charitems.push_back(items[i].c_str());
470 }
471 uint32_t itemCount = static_cast<uint32_t>(charitems.size());
472 bool res = ImGui::Combo(caption, itemindex, &charitems[0], itemCount, itemCount);
473 if (res) { updated = true; };
474 return res;
475 }
476
477 bool UIOverlay::button(const char *caption)
478 {
479 bool res = ImGui::Button(caption);
480 if (res) { updated = true; };
481 return res;
482 }
483
484 void UIOverlay::text(const char *formatstr, ...)
485 {
486 va_list args;
487 va_start(args, formatstr);
488 ImGui::TextV(formatstr, args);
489 va_end(args);
490 }
491}
assert(queueCount >=1)
#define VK_CHECK_RESULT(f)
Definition VulkanTools.h:55
VkContext * currentContext()
Definition VkSystem.h:21
static VkSystem * instance()
Definition VkSystem.cpp:10
VkImageView fontView
bool inputFloat(const char *caption, float *value, float step, uint32_t precision)
VkDescriptorPool descriptorPool
std::shared_ptr< vks::Buffer > vertexBuffer
bool sliderFloat(const char *caption, float *value, float min, float max)
bool comboBox(const char *caption, int32_t *itemindex, std::vector< std::string > items)
VkDeviceMemory fontMemory
bool sliderInt(const char *caption, int32_t *value, int32_t min, int32_t max)
bool checkBox(const char *caption, bool *value)
void draw(const VkCommandBuffer commandBuffer)
struct vks::UIOverlay::PushConstBlock pushConstBlock
dyno::VkContext * ctx
bool button(const char *caption)
void resize(uint32_t width, uint32_t height)
void text(const char *formatstr,...)
VkDescriptorSetLayout descriptorSetLayout
bool header(const char *caption)
void preparePipeline(const VkPipelineCache pipelineCache, const VkRenderPass renderPass)
VkPipelineLayout pipelineLayout
VkPipeline pipeline
VkSampleCountFlagBits rasterizationSamples
VkDescriptorSet descriptorSet
std::shared_ptr< vks::Buffer > indexBuffer
std::vector< VkPipelineShaderStageCreateInfo > shaders
VkVertexInputAttributeDescription vertexInputAttributeDescription(uint32_t binding, uint32_t location, VkFormat format, uint32_t offset)
VkDescriptorSetLayoutBinding descriptorSetLayoutBinding(VkDescriptorType type, VkShaderStageFlags stageFlags, uint32_t binding, uint32_t descriptorCount=1)
VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(uint32_t attachmentCount, const VkPipelineColorBlendAttachmentState *pAttachments)
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo(const VkDescriptorSetLayoutBinding *pBindings, uint32_t bindingCount)
VkPipelineViewportStateCreateInfo pipelineViewportStateCreateInfo(uint32_t viewportCount, uint32_t scissorCount, VkPipelineViewportStateCreateFlags flags=0)
VkDescriptorPoolSize descriptorPoolSize(VkDescriptorType type, uint32_t descriptorCount)
VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo(VkPolygonMode polygonMode, VkCullModeFlags cullMode, VkFrontFace frontFace, VkPipelineRasterizationStateCreateFlags flags=0)
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo(uint32_t poolSizeCount, VkDescriptorPoolSize *pPoolSizes, uint32_t maxSets)
VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo(const VkDynamicState *pDynamicStates, uint32_t dynamicStateCount, VkPipelineDynamicStateCreateFlags flags=0)
VkVertexInputBindingDescription vertexInputBindingDescription(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate)
VkPushConstantRange pushConstantRange(VkShaderStageFlags stageFlags, uint32_t size, uint32_t offset)
VkWriteDescriptorSet writeDescriptorSet(VkDescriptorSet dstSet, VkDescriptorType type, uint32_t binding, VkDescriptorBufferInfo *bufferInfo, uint32_t descriptorCount=1)
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo(const VkDescriptorSetLayout *pSetLayouts, uint32_t setLayoutCount=1)
VkGraphicsPipelineCreateInfo pipelineCreateInfo()
VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo(VkSampleCountFlagBits rasterizationSamples, VkPipelineMultisampleStateCreateFlags flags=0)
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo(VkDescriptorPool descriptorPool, const VkDescriptorSetLayout *pSetLayouts, uint32_t descriptorSetCount)
VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(VkBool32 depthTestEnable, VkBool32 depthWriteEnable, VkCompareOp depthCompareOp)
VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo(VkPrimitiveTopology topology, VkPipelineInputAssemblyStateCreateFlags flags, VkBool32 primitiveRestartEnable)
VkMemoryAllocateInfo memoryAllocateInfo()
VkImageCreateInfo imageCreateInfo()
VkSamplerCreateInfo samplerCreateInfo()
VkImageViewCreateInfo imageViewCreateInfo()
VkPipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo()
VkDescriptorImageInfo descriptorImageInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
void setImageLayout(VkCommandBuffer cmdbuffer, VkImage image, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, VkImageSubresourceRange subresourceRange, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask)
#define max(x, y)
Definition svd3_cuda.h:41