Skip to content

Commit

Permalink
preset sampler creation
Browse files Browse the repository at this point in the history
  • Loading branch information
K1ngst0m committed Oct 10, 2023
1 parent fb7ebc7 commit c5c7003
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 18 deletions.
11 changes: 10 additions & 1 deletion engine/api/gpuResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ enum class ResourceType
STORAGE_BUFFER = 6,
};

enum class SamplerPreset
{
Nearest,
Linear,
Anisotropic,
Mipmap,
Border,
};

struct Extent3D
{
uint32_t width = {0};
Expand All @@ -58,7 +67,7 @@ struct Extent3D
struct DebugLabel
{
std::string name;
float color[4];
float color[4];
};

struct DummyCreateInfo
Expand Down
48 changes: 48 additions & 0 deletions engine/api/vulkan/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,54 @@ VkResult Device::createSampler(const VkSamplerCreateInfo& createInfo, Sampler**
return VK_SUCCESS;
}

VkResult Device::createSampler(SamplerPreset preset, Sampler** ppSampler, bool immutable)
{
VkSamplerCreateInfo ci{.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
ci.magFilter = VK_FILTER_LINEAR;
ci.minFilter = VK_FILTER_LINEAR;
ci.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.anisotropyEnable = VK_FALSE;
ci.maxAnisotropy = 1.0f;
ci.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
ci.mipLodBias = 0.0f;
ci.minLod = 0.0f;
ci.maxLod = 1.0f;
ci.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;

switch (preset)
{
case SamplerPreset::Nearest:
ci.magFilter = VK_FILTER_NEAREST;
ci.minFilter = VK_FILTER_NEAREST;
break;
case SamplerPreset::Linear:
ci.magFilter = VK_FILTER_LINEAR;
ci.minFilter = VK_FILTER_LINEAR;
break;
case SamplerPreset::Anisotropic:
ci.anisotropyEnable = VK_TRUE;
ci.maxAnisotropy = 16.0f;
break;
case SamplerPreset::Mipmap:
ci.minFilter = VK_FILTER_LINEAR;
ci.magFilter = VK_FILTER_LINEAR;
ci.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
ci.minLod = 0.0f;
ci.maxLod = 8.0f;
break;
case SamplerPreset::Border:
ci.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
break;
}

return createSampler(ci, ppSampler, immutable);
}

void Device::destroySampler(Sampler* pSampler)
{
m_table.vkDestroySampler(getHandle(), pSampler->getHandle(), nullptr);
Expand Down
1 change: 1 addition & 0 deletions engine/api/vulkan/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Device : public ResourceHandle<VkDevice, DeviceCreateInfo>

public:
VkResult createSampler(const VkSamplerCreateInfo& createInfo, Sampler** ppSampler, bool immutable);
VkResult createSampler(SamplerPreset preset, Sampler** ppSampler, bool immutable);
VkResult createBuffer(const BufferCreateInfo& createInfo, Buffer** ppBuffer, const void* data = nullptr,
bool persistmentMap = false);
VkResult createImage(const ImageCreateInfo& createInfo, Image** ppImage);
Expand Down
65 changes: 48 additions & 17 deletions engine/api/vulkan/vkInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <vector>

#include "../gpuResource.h"

namespace aph::vk::init
{

Expand Down Expand Up @@ -104,23 +106,52 @@ inline VkImageCreateInfo imageCreateInfo()
return imageCreateInfo;
}

inline VkSamplerCreateInfo samplerCreateInfo()
{
VkSamplerCreateInfo samplerCreateInfo{};
samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerCreateInfo.magFilter = VK_FILTER_LINEAR;
samplerCreateInfo.minFilter = VK_FILTER_LINEAR;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.mipLodBias = 0.0f;
samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER;
samplerCreateInfo.minLod = 0.0f;
samplerCreateInfo.maxAnisotropy = 1.0f;
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;

return samplerCreateInfo;
inline VkSamplerCreateInfo samplerCreateInfo(SamplerPreset preset = SamplerPreset::Linear)
{
VkSamplerCreateInfo ci{.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
ci.magFilter = VK_FILTER_LINEAR;
ci.minFilter = VK_FILTER_LINEAR;
ci.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
ci.anisotropyEnable = VK_FALSE;
ci.maxAnisotropy = 1.0f;
ci.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
ci.mipLodBias = 0.0f;
ci.minLod = 0.0f;
ci.maxLod = 1.0f;
ci.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;

switch (preset)
{
case SamplerPreset::Nearest:
ci.magFilter = VK_FILTER_NEAREST;
ci.minFilter = VK_FILTER_NEAREST;
break;
case SamplerPreset::Linear:
ci.magFilter = VK_FILTER_LINEAR;
ci.minFilter = VK_FILTER_LINEAR;
break;
case SamplerPreset::Anisotropic:
ci.anisotropyEnable = VK_TRUE;
ci.maxAnisotropy = 16.0f;
break;
case SamplerPreset::Mipmap:
ci.minFilter = VK_FILTER_LINEAR;
ci.magFilter = VK_FILTER_LINEAR;
ci.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
ci.minLod = 0.0f;
ci.maxLod = 8.0f;
break;
case SamplerPreset::Border:
ci.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
ci.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
break;
}

return ci;
}

inline VkImageViewCreateInfo imageViewCreateInfo()
Expand Down

0 comments on commit c5c7003

Please sign in to comment.