Creating a sampler
A sampler in Vulkan transcends a simple object; it’s a crucial bridge between shader execution and image data. Beyond interpolation, it governs filtering, addressing modes, and mipmapping. Filters dictate interpolation between texels, while addressing modes control how coordinates map to image extents. Anisotropic filtering further enhances sampling fidelity. Mipmapping, a pyramid of downsampled image levels, is another facet managed by samplers. In essence, creating a sampler involves orchestrating these attributes to seamlessly harmonize image data and shader intricacies. In this recipe, you will learn how to create a sampler object in Vulkan.
Getting ready
Samplers are implemented by the VulkanCore::Sampler
class in the repository.
How to do it…
The properties of a sampler define how an image is interpreted in the pipeline, usually in a shader. The process is simple – instantiate a VkSamplerCreateInfo
structure and call vkCreateSampler
:
VkDevice device; // Valid Vulkan Device VkFilter minFilter; VkFilter maxFilter; float maxLod; // Max mip level const VkSamplerCreateInfo samplerInfo = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = minFilter, .minFilter = magFilter, .mipmapMode = maxLod > 0 ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST, .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, .mipLodBias = 0, .anisotropyEnable = VK_FALSE, .minLod = 0, .maxLod = maxLod, }; VkSampler sampler{VK_NULL_HANDLE}; VK_CHECK(vkCreateSampler(device, &samplerInfo, nullptr, &sampler));
A sampler is one of the simplest objects to create in Vulkan and one of the easiest to understand, as it describes very common computer graphics concepts.