Implemented Sampler with Clamp to Edge (because I like this) and anisotrpic filtering to the graphics cards max

This commit is contained in:
Ano-sys
2025-04-05 14:40:32 +02:00
parent 45b362be82
commit 80012fc7e7
2 changed files with 77 additions and 33 deletions
+71 -33
View File
@@ -196,6 +196,7 @@ namespace vapp{
} }
VkPhysicalDeviceFeatures deviceFeatures{}; VkPhysicalDeviceFeatures deviceFeatures{};
deviceFeatures.samplerAnisotropy = VK_TRUE;
VkDeviceCreateInfo createInfo{}; VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
@@ -263,15 +264,6 @@ namespace vapp{
} }
bool Vulkan::isDeviceSuitable(VkPhysicalDevice device){ bool Vulkan::isDeviceSuitable(VkPhysicalDevice device){
/*
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.geometryShader;
*/
QueueFamilyIndices indices = findQueueFamilies(device); QueueFamilyIndices indices = findQueueFamilies(device);
bool extensionSupported = checkDeviceExtensionSupport(device); bool extensionSupported = checkDeviceExtensionSupport(device);
bool swapChainAdequate = false; bool swapChainAdequate = false;
@@ -279,7 +271,11 @@ namespace vapp{
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device); SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty(); swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
} }
return indices.isComplete() && extensionSupported && swapChainAdequate;
VkPhysicalDeviceFeatures supportedFeatures;
vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
return indices.isComplete() && extensionSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
} }
void Vulkan::createSurface(){ void Vulkan::createSurface(){
@@ -314,7 +310,7 @@ namespace vapp{
VkSurfaceFormatKHR Vulkan::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats){ VkSurfaceFormatKHR Vulkan::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats){
for(const VkSurfaceFormatKHR availableFormat : availableFormats){ for(const VkSurfaceFormatKHR availableFormat : availableFormats){
if(availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == if(availableFormat.format == VK_FORMAT_R8G8B8A8_SRGB && availableFormat.colorSpace ==
VK_COLOR_SPACE_SRGB_NONLINEAR_KHR){ VK_COLOR_SPACE_SRGB_NONLINEAR_KHR){
return availableFormat; return availableFormat;
} }
@@ -411,28 +407,8 @@ namespace vapp{
void Vulkan::createImageViews(){ void Vulkan::createImageViews(){
swapChainImageViews.resize(swapChainImages.size()); swapChainImageViews.resize(swapChainImages.size());
for(size_t i = 0; i < swapChainImages.size(); i++){ for(uint32_t i = 0; i < swapChainImages.size(); i++){
VkImageViewCreateInfo createInfo{}; swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat);
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = swapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = swapChainImageFormat;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
if(vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS){
throw std::runtime_error("Func: createImageViews\nError: Failed to create Image Views!\n");
}
} }
} }
@@ -866,6 +842,63 @@ namespace vapp{
vkFreeMemory(device, stagingBufferMemory, nullptr); vkFreeMemory(device, stagingBufferMemory, nullptr);
} }
VkImageView Vulkan::createImageView(VkImage image, VkFormat format){
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_SRGB;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
VkImageView imageView;
if(vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS){
throw std::runtime_error("Func: createTextureImageView\nError: Failed to create Texture Image View!\n");
}
return imageView;
}
void Vulkan::createTextureImageView(){
textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB);
}
void Vulkan::createTextureSampler(){
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
// INFO: Here can preferences be used -> instead of REPEAT I use CLAMP_TO_EDGE to get this nice eye cancer
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
// INFO: filter user preferences here
samplerInfo.anisotropyEnable = VK_TRUE;
VkPhysicalDeviceProperties properties{};
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE;
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 0.0f;
if(vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS){
throw std::runtime_error("Func: createTextureSampler\nError: Failed to create Texture Sampler!\n");
}
}
uint32_t Vulkan::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties){ uint32_t Vulkan::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties){
VkPhysicalDeviceMemoryProperties memProperties; VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties); vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
@@ -1266,6 +1299,8 @@ namespace vapp{
createFrameBuffers(); createFrameBuffers();
createCommandPool(); createCommandPool();
createTextureImage(); createTextureImage();
createTextureImageView();
createTextureSampler();
createVertexBuffer(); createVertexBuffer();
createIndexBuffer(); createIndexBuffer();
createUniformBuffers(); createUniformBuffers();
@@ -1288,6 +1323,9 @@ namespace vapp{
// Vulkan // Vulkan
cleanupSwapChain(); cleanupSwapChain();
vkDestroySampler(device, textureSampler, nullptr);
vkDestroyImageView(device, textureImageView, nullptr);
vkDestroyImage(device, textureImage, nullptr); vkDestroyImage(device, textureImage, nullptr);
vkFreeMemory(device, textureImageMemory, nullptr); vkFreeMemory(device, textureImageMemory, nullptr);
+6
View File
@@ -152,6 +152,9 @@ namespace vapp{
VkImage textureImage; VkImage textureImage;
VkDeviceMemory textureImageMemory; VkDeviceMemory textureImageMemory;
VkImageView textureImageView;
VkSampler textureSampler;
#pragma endregion #pragma endregion
bool checkValidationLayerSupport(); bool checkValidationLayerSupport();
@@ -183,6 +186,9 @@ namespace vapp{
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height); void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory &imageMemory); void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory &imageMemory);
void createTextureImage(); void createTextureImage();
VkImageView createImageView(VkImage image, VkFormat format);
void createTextureImageView();
void createTextureSampler();
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory); void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size); void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);