17#if defined(__ANDROID__)
18 if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_XXHIGH) {
21 else if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
24 else if (vks::android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
30 ImGui::CreateContext();
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);
50 ImGuiIO& io = ImGui::GetIO();
51 io.FontGlobalScale =
scale;
64 ImGuiIO& io = ImGui::GetIO();
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);
73 size_t size = AAsset_getLength(asset);
75 char *fontAsset =
new char[size];
76 AAsset_read(asset, fontAsset, size);
78 io.Fonts->AddFontFromMemoryTTF(fontAsset, size, 14.0f *
scale);
82 const std::string filename = getAssetPath() +
"Roboto-Medium.ttf";
83 io.Fonts->AddFontFromFileTTF(filename.c_str(), 16.0f);
85 io.Fonts->GetTexDataAsRGBA32(&fontData, &texWidth, &texHeight);
86 VkDeviceSize uploadSize = texWidth*texHeight * 4 *
sizeof(char);
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;
103 VkMemoryRequirements memReqs;
104 vkGetImageMemoryRequirements(
ctx->deviceHandle(),
fontImage, &memReqs);
106 memAllocInfo.allocationSize = memReqs.size;
107 memAllocInfo.memoryTypeIndex =
ctx->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
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;
122 std::shared_ptr<vks::Buffer> stagingBuffer = std::make_shared<vks::Buffer>();
125 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
126 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
130 stagingBuffer->map();
131 memcpy(stagingBuffer->mapped, fontData, uploadSize);
132 stagingBuffer->unmap();
135 VkCommandBuffer copyCmd =
ctx->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY,
true);
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);
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;
155 vkCmdCopyBufferToImage(
157 stagingBuffer->buffer,
159 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
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);
174 ctx->flushCommandBuffer(copyCmd,
ctx->graphicsQueueHandle(),
true);
176 stagingBuffer->destroy();
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;
190 std::vector<VkDescriptorPoolSize> poolSizes = {
197 std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
209 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
211 std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
214 vkUpdateDescriptorSets(
ctx->deviceHandle(),
static_cast<uint32_t
>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0,
nullptr);
224 pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
225 pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
229 VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
232 VkPipelineRasterizationStateCreateInfo rasterizationState =
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;
246 VkPipelineColorBlendStateCreateInfo colorBlendState =
249 VkPipelineDepthStencilStateCreateInfo depthStencilState =
252 VkPipelineViewportStateCreateInfo viewportState =
255 VkPipelineMultisampleStateCreateInfo multisampleState =
258 std::vector<VkDynamicState> dynamicStateEnables = {
259 VK_DYNAMIC_STATE_VIEWPORT,
260 VK_DYNAMIC_STATE_SCISSOR
262 VkPipelineDynamicStateCreateInfo dynamicState =
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;
279 std::vector<VkVertexInputBindingDescription> vertexInputBindings = {
282 std::vector<VkVertexInputAttributeDescription> vertexInputAttributes = {
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();
293 pipelineCreateInfo.pVertexInputState = &vertexInputState;
301 ImDrawData* imDrawData = ImGui::GetDrawData();
302 bool updateCmdBuffers =
false;
304 if (!imDrawData) {
return false; };
307 VkDeviceSize vertexBufferSize = imDrawData->TotalVtxCount *
sizeof(ImDrawVert);
308 VkDeviceSize indexBufferSize = imDrawData->TotalIdxCount *
sizeof(ImDrawIdx);
311 if ((vertexBufferSize == 0) || (indexBufferSize == 0)) {
323 updateCmdBuffers =
true;
327 VkDeviceSize indexSize = imDrawData->TotalIdxCount *
sizeof(ImDrawIdx);
334 updateCmdBuffers =
true;
339 ImDrawIdx* idxDst = (ImDrawIdx*)
indexBuffer->mapped;
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;
353 return updateCmdBuffers;
358 ImDrawData* imDrawData = ImGui::GetDrawData();
359 int32_t vertexOffset = 0;
360 int32_t indexOffset = 0;
362 if ((!imDrawData) || (imDrawData->CmdListsCount == 0)) {
366 ImGuiIO& io = ImGui::GetIO();
368 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline);
371 pushConstBlock.scale = glm::vec2(2.0f / io.DisplaySize.x, 2.0f / io.DisplaySize.y);
375 VkDeviceSize offsets[1] = { 0 };
376 vkCmdBindVertexBuffers(commandBuffer, 0, 1, &
vertexBuffer->buffer, offsets);
377 vkCmdBindIndexBuffer(commandBuffer,
indexBuffer->buffer, 0, VK_INDEX_TYPE_UINT16);
379 for (int32_t i = 0; i < imDrawData->CmdListsCount; i++)
381 const ImDrawList* cmd_list = imDrawData->CmdLists[i];
382 for (int32_t j = 0; j < cmd_list->CmdBuffer.Size; j++)
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;
394 vertexOffset += cmd_list->VtxBuffer.Size;