MCPcopy Create free account
hub / github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator / CreateTexture

Function CreateTexture

src/VulkanSample.cpp:769–901  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

767}
768
769static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
770{
771 // Create staging buffer.
772
773 const VkDeviceSize imageSize = sizeX * sizeY * 4;
774
775 VkBufferCreateInfo stagingBufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
776 stagingBufInfo.size = imageSize;
777 stagingBufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
778
779 VmaAllocationCreateInfo stagingBufAllocCreateInfo = {};
780 stagingBufAllocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
781 stagingBufAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
782
783 VkBuffer stagingBuf = VK_NULL_HANDLE;
784 VmaAllocation stagingBufAlloc = VK_NULL_HANDLE;
785 VmaAllocationInfo stagingBufAllocInfo = {};
786 ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &stagingBufInfo, &stagingBufAllocCreateInfo, &stagingBuf, &stagingBufAlloc, &stagingBufAllocInfo) );
787
788 char* const pImageData = (char*)stagingBufAllocInfo.pMappedData;
789 uint8_t* pRowData = (uint8_t*)pImageData;
790 for(uint32_t y = 0; y < sizeY; ++y)
791 {
792 uint32_t* pPixelData = (uint32_t*)pRowData;
793 for(uint32_t x = 0; x < sizeX; ++x)
794 {
795 *pPixelData =
796 ((x & 0x18) == 0x08 ? 0x000000FF : 0x00000000) |
797 ((x & 0x18) == 0x10 ? 0x0000FFFF : 0x00000000) |
798 ((y & 0x18) == 0x08 ? 0x0000FF00 : 0x00000000) |
799 ((y & 0x18) == 0x10 ? 0x00FF0000 : 0x00000000);
800 ++pPixelData;
801 }
802 pRowData += sizeX * 4;
803 }
804
805 // No need to flush stagingImage memory because CPU_ONLY memory is always HOST_COHERENT.
806
807 // Create g_hTextureImage in GPU memory.
808
809 VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
810 imageInfo.imageType = VK_IMAGE_TYPE_2D;
811 imageInfo.extent.width = sizeX;
812 imageInfo.extent.height = sizeY;
813 imageInfo.extent.depth = 1;
814 imageInfo.mipLevels = 1;
815 imageInfo.arrayLayers = 1;
816 imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
817 imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
818 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
819 imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
820 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
821 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
822 imageInfo.flags = 0;
823
824 VmaAllocationCreateInfo imageAllocCreateInfo = {};
825 imageAllocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
826

Callers 1

InitializeApplicationFunction · 0.85

Calls 6

vmaCreateBufferFunction · 0.85
vmaCreateImageFunction · 0.85
SetDebugUtilsObjectNameFunction · 0.85
BeginSingleTimeCommandsFunction · 0.85
EndSingleTimeCommandsFunction · 0.85
vmaDestroyBufferFunction · 0.85

Tested by

no test coverage detected