jGL
Loading...
Searching...
No Matches
vkSampler.h
Go to the documentation of this file.
1#ifndef VKSAMPLER
2#define VKSAMPLER
3
5
6namespace jGL::Vulkan
7{
8 class Sampler
9 {
10
11 public:
12
14 (
15 const Device & device,
16 VkFilter filter = VK_FILTER_LINEAR,
17 uint32_t binding = 0
18 )
19 : device(device), binding(binding)
20 {
21 VkSamplerCreateInfo samplerInfo{};
22 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
23 samplerInfo.magFilter = filter;
24 samplerInfo.minFilter = filter;
25 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
26 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
27 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
28 samplerInfo.anisotropyEnable = VK_TRUE;
29 samplerInfo.maxAnisotropy = device.getLimits().maxSamplerAnisotropy;
30 samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
31 // texels are [0,1]
32 samplerInfo.unnormalizedCoordinates = VK_FALSE;
33 // can have a texel comparison on sampling
34 samplerInfo.compareEnable = VK_FALSE;
35 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
36 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
37 samplerInfo.mipLodBias = 0.0f;
38 samplerInfo.minLod = 0.0f;
39 samplerInfo.maxLod = 0.0f;
40
41 if (vkCreateSampler(device.getVkDevice(), &samplerInfo, nullptr, &sampler) != VK_SUCCESS) {
42 throw std::runtime_error("Failed to create texture sampler");
43 }
44 }
45
47 {
48 vkDestroySampler(device.getVkDevice(), sampler, nullptr);
49 }
50
51 const VkSampler & getVkSampler() const { return sampler; }
52
53 void setBinding(uint32_t b) { binding = b; }
54
55 VkDescriptorSetLayoutBinding getLayout() const
56 {
57 VkDescriptorSetLayoutBinding layout {};
58 layout.binding = binding;
59 layout.descriptorCount = 1;
60 layout.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
61 layout.pImmutableSamplers = nullptr;
62 layout.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
63 return layout;
64 }
65
66 private:
67
68 const Device & device;
69
70 VkSampler sampler;
71
72 uint32_t binding;
73
74 };
75}
76
77#endif /* VKSAMPLER */
Definition device.h:10
const VkPhysicalDeviceLimits & getLimits() const
Definition device.h:25
const VkDevice & getVkDevice() const
Definition device.h:24
Definition vkSampler.h:9
const VkSampler & getVkSampler() const
Definition vkSampler.h:51
VkDescriptorSetLayoutBinding getLayout() const
Definition vkSampler.h:55
~Sampler()
Definition vkSampler.h:46
void setBinding(uint32_t b)
Definition vkSampler.h:53
Sampler(const Device &device, VkFilter filter=VK_FILTER_LINEAR, uint32_t binding=0)
Definition vkSampler.h:14
Definition buffer.h:10