static VkBool32 bufferObjectPrepare(IBufferSP& buffer, IDeviceMemorySP& deviceMemory, const IInitialResourcesSP& initialResources, const VkBufferCreateInfo& bufferCreateInfo, const VkMemoryPropertyFlags memoryPropertyFlag) { VkResult result; // buffer = bufferCreate(initialResources->getDevice()->getDevice(), bufferCreateInfo.flags, bufferCreateInfo.size, bufferCreateInfo.usage, bufferCreateInfo.sharingMode, bufferCreateInfo.queueFamilyIndexCount, bufferCreateInfo.pQueueFamilyIndices); if (!buffer.get()) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not create buffer."); return VK_FALSE; } VkMemoryRequirements memoryRequirements; buffer->getBufferMemoryRequirements(memoryRequirements); VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties; initialResources->getPhysicalDevice()->getPhysicalDeviceMemoryProperties(physicalDeviceMemoryProperties); deviceMemory = deviceMemoryCreate(initialResources->getDevice()->getDevice(), memoryRequirements, physicalDeviceMemoryProperties.memoryTypeCount, physicalDeviceMemoryProperties.memoryTypes, memoryPropertyFlag); if (!deviceMemory.get()) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not allocate memory."); return VK_FALSE; } result = buffer->bindBufferMemory(deviceMemory->getDeviceMemory(), 0); if (result != VK_SUCCESS) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not bind buffer memory."); return VK_FALSE; } return VK_TRUE; }
FontCacheEntry* FreeTypeFont::getGlyph(const ICommandBuffersSP& cmdBuffer, const char32_t character, const size_t size) { if (!face || !cmdBuffer.get()) { return nullptr; } // FontCacheEntry* fontCacheEntry = getGlyph(character, size); // Error case. if (!fontCacheEntry) { return nullptr; } // Character has no data e.g. space. if (!fontCacheEntry->glyphImageData.get()) { return fontCacheEntry; } // Character already created. if (fontCacheEntry->texture.get()) { return fontCacheEntry; } // VkImageTiling imageTiling; VkMemoryPropertyFlags memoryPropertyFlags; if (!initialResources->getPhysicalDevice()->getGetImageTilingAndMemoryProperty(imageTiling, memoryPropertyFlags, fontCacheEntry->glyphImageData->getFormat(), fontCacheEntry->glyphImageData->getImageType(), 0, fontCacheEntry->glyphImageData->getExtent3D(), fontCacheEntry->glyphImageData->getMipLevels(), 1, VK_SAMPLE_COUNT_1_BIT, fontCacheEntry->glyphImageData->getSize())) { return nullptr; } // VkImageCreateInfo imageCreateInfo; memset(&imageCreateInfo, 0, sizeof(VkImageCreateInfo)); imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCreateInfo.flags = 0; imageCreateInfo.imageType = fontCacheEntry->glyphImageData->getImageType(); imageCreateInfo.format = fontCacheEntry->glyphImageData->getFormat(); imageCreateInfo.extent = fontCacheEntry->glyphImageData->getExtent3D(); imageCreateInfo.mipLevels = fontCacheEntry->glyphImageData->getMipLevels(); imageCreateInfo.arrayLayers = 1; imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageCreateInfo.tiling = imageTiling; imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageCreateInfo.queueFamilyIndexCount = 0; imageCreateInfo.pQueueFamilyIndices = nullptr; imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; VkImageSubresourceRange subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; VkSamplerCreateInfo samplerCreateInfo; memset(&samplerCreateInfo, 0, sizeof(VkSamplerCreateInfo)); samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerCreateInfo.flags = 0; samplerCreateInfo.magFilter = VK_FILTER_NEAREST; samplerCreateInfo.minFilter = VK_FILTER_NEAREST; samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; samplerCreateInfo.mipLodBias = 0.0f; samplerCreateInfo.anisotropyEnable = VK_FALSE; samplerCreateInfo.maxAnisotropy = 1.0f; samplerCreateInfo.compareEnable = VK_FALSE; samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER; samplerCreateInfo.minLod = 0.0f; samplerCreateInfo.maxLod = 0.0f; samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; samplerCreateInfo.unnormalizedCoordinates = VK_FALSE; VkImageViewCreateInfo imageViewCreateInfo; memset(&imageViewCreateInfo, 0, sizeof(VkImageViewCreateInfo)); imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; imageViewCreateInfo.flags = 0; imageViewCreateInfo.image = VK_NULL_HANDLE; // Defined later. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; imageViewCreateInfo.format = fontCacheEntry->glyphImageData->getFormat(); imageViewCreateInfo.components = {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A}; imageViewCreateInfo.subresourceRange = subresourceRange; IImageSP stageImage; IBufferSP stageBuffer; IDeviceMemorySP stageDeviceMemory; fontCacheEntry->texture = textureCreate(stageImage, stageBuffer, stageDeviceMemory, initialResources, cmdBuffer, "FontCharacterTexture", fontCacheEntry->glyphImageData, imageCreateInfo, VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, subresourceRange, memoryPropertyFlags, samplerCreateInfo, imageViewCreateInfo); if (!fontCacheEntry->texture.get()) { return nullptr; } if (stageImage.get()) { allStageImages.append(stageImage); } if (stageBuffer.get()) { allStageBuffers.append(stageBuffer); } if (stageDeviceMemory.get()) { allStageDeviceMemories.append(stageDeviceMemory); } return fontCacheEntry; }