IBufferObjectSP VKTS_APIENTRY bufferObjectCreate(const IInitialResourcesSP& initialResources, const VkBufferCreateInfo& bufferCreateInfo, const VkMemoryPropertyFlags memoryPropertyFlag) { if (!initialResources.get()) { return IBufferObjectSP(); } // IBufferSP buffer; IDeviceMemorySP deviceMemory; if (!bufferObjectPrepare(buffer, deviceMemory, initialResources, bufferCreateInfo, memoryPropertyFlag)) { return IBufferObjectSP(); } // IBufferViewSP noBufferView; auto newInstance = new BufferObject(initialResources, buffer, noBufferView, deviceMemory); if (!newInstance) { buffer->destroy(); return IBufferObjectSP(); } return IBufferObjectSP(newInstance); }
void Image::copyImageToBuffer(const VkCommandBuffer cmdBuffer, IBufferSP& targetBuffer, const VkBufferImageCopy& bufferImageCopy) { if (!targetBuffer.get()) { return; } VkImageLayout sourceImageLayout = getImageLayout(bufferImageCopy.imageSubresource.mipLevel, bufferImageCopy.imageSubresource.baseArrayLayer); VkAccessFlags sourceAccessMask = getAccessMask(bufferImageCopy.imageSubresource.mipLevel, bufferImageCopy.imageSubresource.baseArrayLayer); VkAccessFlags targetAccessMask = targetBuffer->getAccessMask(); // Prepare source image for copy. VkImageSubresourceRange imageSubresourceRange = {bufferImageCopy.imageSubresource.aspectMask, bufferImageCopy.imageSubresource.mipLevel, 1, bufferImageCopy.imageSubresource.baseArrayLayer, bufferImageCopy.imageSubresource.layerCount}; cmdPipelineBarrier(cmdBuffer, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, imageSubresourceRange); // Prepare target buffer for copy. targetBuffer->cmdPipelineBarrier(cmdBuffer, VK_ACCESS_TRANSFER_WRITE_BIT); // Copy image by command. vkCmdCopyImageToBuffer(cmdBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, targetBuffer->getBuffer(), 1, &bufferImageCopy); // Revert back. targetBuffer->cmdPipelineBarrier(cmdBuffer, targetAccessMask); // Revert back. cmdPipelineBarrier(cmdBuffer, sourceAccessMask, sourceImageLayout, imageSubresourceRange); }
IBufferObjectSP VKTS_APIENTRY bufferObjectCreate(const IInitialResourcesSP& initialResources, const VkBufferCreateInfo& bufferCreateInfo, const VkMemoryPropertyFlags memoryPropertyFlag, const VkFormat format) { if (!initialResources.get()) { return IBufferObjectSP(); } // IBufferSP buffer; IDeviceMemorySP deviceMemory; if (!bufferObjectPrepare(buffer, deviceMemory, initialResources, bufferCreateInfo, memoryPropertyFlag)) { return IBufferObjectSP(); } // auto bufferView = bufferViewCreate(initialResources->getDevice()->getDevice(), 0, buffer->getBuffer(), format, 0, bufferCreateInfo.size); if (!bufferView.get()) { buffer->destroy(); return IBufferObjectSP(); } // auto newInstance = new BufferObject(initialResources, buffer, bufferView, deviceMemory); if (!newInstance) { bufferView->destroy(); buffer->destroy(); return IBufferObjectSP(); } return IBufferObjectSP(newInstance); }
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; }
IBufferObjectSP VKTS_APIENTRY bufferObjectCreate(IBufferSP& stageBuffer, IDeviceMemorySP& stageDeviceMemory, const IInitialResourcesSP& initialResources, const ICommandBuffersSP& cmdBuffer, const IBinaryBufferSP& binaryBuffer, const VkBufferCreateInfo& bufferCreateInfo, const VkMemoryPropertyFlags memoryPropertyFlag) { if (!initialResources.get() || !cmdBuffer.get() || !binaryBuffer.get()) { return IBufferObjectSP(); } // VkResult result; // IBufferSP buffer; IDeviceMemorySP deviceMemory; if (!bufferObjectPrepare(buffer, deviceMemory, initialResources, bufferCreateInfo, memoryPropertyFlag)) { return IBufferObjectSP(); } // if (memoryPropertyFlag & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { result = deviceMemory->upload(0, deviceMemory->getAllocationSize(), 0, binaryBuffer->getData(), binaryBuffer->getSize()); if (result != VK_SUCCESS) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not upload vertex data."); return IBufferObjectSP(); } } else { if (!bufferObjectPrepare(stageBuffer, stageDeviceMemory, initialResources, bufferCreateInfo, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not create vertex buffer."); return IBufferObjectSP(); } result = stageDeviceMemory->upload(0, stageDeviceMemory->getAllocationSize(), 0, binaryBuffer->getData(), binaryBuffer->getSize()); if (result != VK_SUCCESS) { logPrint(VKTS_LOG_ERROR, "BufferObject: Could not upload vertex data."); return IBufferObjectSP(); } VkBufferCopy bufferCopy; bufferCopy.srcOffset = 0; bufferCopy.dstOffset = 0; bufferCopy.size = binaryBuffer->getSize(); stageBuffer->copyBuffer(cmdBuffer->getCommandBuffer(), buffer->getBuffer(), buffer->getSize(), 1, &bufferCopy); } // IBufferViewSP noBufferView; auto newInstance = new BufferObject(initialResources, buffer, noBufferView, deviceMemory); if (!newInstance) { buffer->destroy(); return IBufferObjectSP(); } return IBufferObjectSP(newInstance); }
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; }