raze/source/common/rendering/vulkan/textures/vk_samplers.cpp
2020-05-31 10:53:11 +02:00

114 lines
3.6 KiB
C++

/*
** Vulkan backend
** Copyright (c) 2016-2020 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#include "volk/volk.h"
#include "c_cvars.h"
#include "v_video.h"
#include "hw_cvars.h"
#include "vulkan/system/vk_device.h"
#include "vulkan/system/vk_builders.h"
#include "vk_samplers.h"
#include "hw_material.h"
#include "i_interface.h"
struct VkTexFilter
{
VkFilter minFilter;
VkFilter magFilter;
VkSamplerMipmapMode mipfilter;
bool mipmapping;
} ;
VkTexFilter TexFilter[]={
{VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, false},
{VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, true},
{VK_FILTER_LINEAR, VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_NEAREST, false},
{VK_FILTER_LINEAR, VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_NEAREST, true},
{VK_FILTER_LINEAR, VK_FILTER_LINEAR, VK_SAMPLER_MIPMAP_MODE_LINEAR, true},
{VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_LINEAR, true},
{VK_FILTER_LINEAR, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_LINEAR, true},
};
struct VkTexClamp
{
VkSamplerAddressMode clamp_u;
VkSamplerAddressMode clamp_v;
};
VkTexClamp TexClamp[] ={
{ VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT },
{ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_REPEAT },
{ VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE },
{ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE },
{ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE },
{ VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT },
{ VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT }
};
VkSamplerManager::VkSamplerManager(VulkanDevice *dev)
{
vDevice = dev;
Create();
}
VkSamplerManager::~VkSamplerManager()
{
Destroy();
}
void VkSamplerManager::SetTextureFilterMode()
{
Destroy();
Create();
}
void VkSamplerManager::Create()
{
int filter = sysCallbacks && sysCallbacks->DisableTextureFilter && sysCallbacks->DisableTextureFilter()? 0 : gl_texture_filter;
for(int i = 0; i < 7; i++)
{
SamplerBuilder builder;
builder.setMagFilter(TexFilter[filter].magFilter);
builder.setMinFilter(TexFilter[filter].minFilter);
builder.setAddressMode(TexClamp[i].clamp_u, TexClamp[i].clamp_v, VK_SAMPLER_ADDRESS_MODE_REPEAT);
builder.setMipmapMode(TexFilter[filter].mipfilter);
if (i <= CLAMP_XY && TexFilter[filter].mipmapping)
{
builder.setAnisotropy(gl_texture_filter_anisotropic);
builder.setMaxLod(100.0f); // According to the spec this value is clamped so something high makes it usable for all textures.
}
else
{
builder.setMaxLod(0.25f);
}
mSamplers[i] = builder.create(vDevice);
mSamplers[i]->SetDebugName("VkSamplerManager.mSamplers");
}
}
void VkSamplerManager::Destroy()
{
for (int i = 0; i < 7; i++)
mSamplers[i].reset();
}