MCPcopy Create free account
hub / github.com/SFML/SFML / setupTextureImage

Method setupTextureImage

examples/vulkan/Vulkan.cpp:1786–2076  ·  view source on GitHub ↗

Create an image for our texture data

Source from the content-addressed store, hash-verified

1784
1785 // Create an image for our texture data
1786 void setupTextureImage()
1787 {
1788 // Load the image data
1789 sf::Image imageData;
1790 if (!imageData.loadFromFile("resources/logo.png"))
1791 {
1792 vulkanAvailable = false;
1793 return;
1794 }
1795
1796 // Create a staging buffer to transfer the data with
1797 const VkDeviceSize imageSize = imageData.getSize().x * imageData.getSize().y * 4;
1798
1799 VkBuffer stagingBuffer = {};
1800 VkDeviceMemory stagingBufferMemory = {};
1801 createBuffer(imageSize,
1802 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1803 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1804 stagingBuffer,
1805 stagingBufferMemory);
1806
1807 void* ptr = nullptr;
1808
1809 // Map the buffer into our address space
1810 if (vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &ptr) != VK_SUCCESS)
1811 {
1812 vkFreeMemory(device, stagingBufferMemory, nullptr);
1813 vkDestroyBuffer(device, stagingBuffer, nullptr);
1814
1815 vulkanAvailable = false;
1816 return;
1817 }
1818
1819 // Copy the image data into the buffer
1820 std::memcpy(ptr, imageData.getPixelsPtr(), static_cast<std::size_t>(imageSize));
1821
1822 // Unmap the buffer
1823 vkUnmapMemory(device, stagingBufferMemory);
1824
1825 // Create a GPU local image
1826 if (!createImage(imageData.getSize().x,
1827 imageData.getSize().y,
1828 VK_FORMAT_R8G8B8A8_UNORM,
1829 VK_IMAGE_TILING_OPTIMAL,
1830 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1831 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1832 textureImage,
1833 textureImageMemory))
1834 {
1835 vkFreeMemory(device, stagingBufferMemory, nullptr);
1836 vkDestroyBuffer(device, stagingBuffer, nullptr);
1837
1838 vulkanAvailable = false;
1839 return;
1840 }
1841
1842 // Create a command buffer
1843 VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();

Callers

nothing calls this directly

Calls 8

getPixelsPtrMethod · 0.80
VkSubmitInfoClass · 0.70
VkBufferImageCopyClass · 0.70
loadFromFileMethod · 0.45
getSizeMethod · 0.45

Tested by

no test coverage detected