2020-05-31 08:53:11 +00:00
|
|
|
/*
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2022-12-11 17:30:01 +00:00
|
|
|
#include <zvulkan/vulkanobjects.h>
|
|
|
|
#include <zvulkan/vulkandevice.h>
|
|
|
|
#include <zvulkan/vulkanbuilders.h>
|
2020-05-31 08:53:11 +00:00
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "v_video.h"
|
|
|
|
#include "hw_cvars.h"
|
2022-12-11 17:30:01 +00:00
|
|
|
#include "vulkan/system/vk_renderdevice.h"
|
2022-07-02 08:09:59 +00:00
|
|
|
#include "vulkan/system/vk_commandbuffer.h"
|
2020-05-31 08:53:11 +00:00
|
|
|
#include "vk_samplers.h"
|
|
|
|
#include "hw_material.h"
|
|
|
|
#include "i_interface.h"
|
2022-07-02 08:09:59 +00:00
|
|
|
#include "hwrenderer/postprocessing/hw_postprocess.h"
|
2020-05-31 08:53:11 +00:00
|
|
|
|
|
|
|
struct VkTexFilter
|
|
|
|
{
|
|
|
|
VkFilter minFilter;
|
|
|
|
VkFilter magFilter;
|
|
|
|
VkSamplerMipmapMode mipfilter;
|
|
|
|
bool mipmapping;
|
2022-07-02 08:09:59 +00:00
|
|
|
};
|
2020-05-31 08:53:11 +00:00
|
|
|
|
2022-07-02 08:09:59 +00:00
|
|
|
static VkTexFilter TexFilter[] =
|
|
|
|
{
|
2020-05-31 08:53:11 +00:00
|
|
|
{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},
|
2022-07-02 08:09:59 +00:00
|
|
|
{VK_FILTER_LINEAR, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_LINEAR, true}
|
2020-05-31 08:53:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VkTexClamp
|
|
|
|
{
|
|
|
|
VkSamplerAddressMode clamp_u;
|
|
|
|
VkSamplerAddressMode clamp_v;
|
|
|
|
};
|
|
|
|
|
2022-07-02 08:09:59 +00:00
|
|
|
static VkTexClamp TexClamp[] =
|
|
|
|
{
|
2020-05-31 08:53:11 +00:00
|
|
|
{ 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 },
|
|
|
|
};
|
|
|
|
|
2022-12-11 17:30:01 +00:00
|
|
|
VkSamplerManager::VkSamplerManager(VulkanRenderDevice* fb) : fb(fb)
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
CreateHWSamplers();
|
|
|
|
CreateShadowmapSampler();
|
|
|
|
CreateLightmapSampler();
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VkSamplerManager::~VkSamplerManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:59 +00:00
|
|
|
void VkSamplerManager::ResetHWSamplers()
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
DeleteHWSamplers();
|
|
|
|
CreateHWSamplers();
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:59 +00:00
|
|
|
void VkSamplerManager::CreateHWSamplers()
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
2020-10-03 15:04:45 +00:00
|
|
|
int filter = sysCallbacks.DisableTextureFilter && sysCallbacks.DisableTextureFilter()? 0 : gl_texture_filter;
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-06-07 13:02:54 +00:00
|
|
|
for (int i = CLAMP_NONE; i <= CLAMP_XY; i++)
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
|
|
|
SamplerBuilder builder;
|
2022-07-02 08:09:59 +00:00
|
|
|
builder.MagFilter(TexFilter[filter].magFilter);
|
|
|
|
builder.MinFilter(TexFilter[filter].minFilter);
|
|
|
|
builder.AddressMode(TexClamp[i].clamp_u, TexClamp[i].clamp_v, VK_SAMPLER_ADDRESS_MODE_REPEAT);
|
|
|
|
builder.MipmapMode(TexFilter[filter].mipfilter);
|
2020-06-07 13:02:54 +00:00
|
|
|
if (TexFilter[filter].mipmapping)
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
builder.Anisotropy(gl_texture_filter_anisotropic);
|
|
|
|
builder.MaxLod(100.0f); // According to the spec this value is clamped so something high makes it usable for all textures.
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
builder.MaxLod(0.25f);
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|
2022-07-02 08:09:59 +00:00
|
|
|
builder.DebugName("VkSamplerManager.mSamplers");
|
2022-12-11 17:30:01 +00:00
|
|
|
mSamplers[i] = builder.Create(fb->device.get());
|
2020-06-07 13:02:54 +00:00
|
|
|
}
|
2022-07-02 08:09:59 +00:00
|
|
|
|
|
|
|
mSamplers[CLAMP_XY_NOMIP] = SamplerBuilder()
|
|
|
|
.MagFilter(TexFilter[filter].magFilter)
|
|
|
|
.MinFilter(TexFilter[filter].magFilter)
|
|
|
|
.AddressMode(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_REPEAT)
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST)
|
|
|
|
.MaxLod(0.25f)
|
|
|
|
.DebugName("VkSamplerManager.mSamplers")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2022-07-02 08:09:59 +00:00
|
|
|
|
2020-06-07 13:02:54 +00:00
|
|
|
for (int i = CLAMP_NOFILTER; i <= CLAMP_NOFILTER_XY; i++)
|
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
mSamplers[i] = SamplerBuilder()
|
|
|
|
.MagFilter(VK_FILTER_NEAREST)
|
|
|
|
.MinFilter(VK_FILTER_NEAREST)
|
|
|
|
.AddressMode(TexClamp[i - CLAMP_NOFILTER].clamp_u, TexClamp[i - CLAMP_NOFILTER].clamp_v, VK_SAMPLER_ADDRESS_MODE_REPEAT)
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST)
|
|
|
|
.MaxLod(0.25f)
|
|
|
|
.DebugName("VkSamplerManager.mSamplers")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2020-06-07 13:02:54 +00:00
|
|
|
}
|
2022-07-02 08:09:59 +00:00
|
|
|
|
2020-06-07 13:02:54 +00:00
|
|
|
// CAMTEX is repeating with texture filter and no mipmap
|
2022-07-02 08:09:59 +00:00
|
|
|
mSamplers[CLAMP_CAMTEX] = SamplerBuilder()
|
|
|
|
.MagFilter(TexFilter[filter].magFilter)
|
|
|
|
.MinFilter(TexFilter[filter].magFilter)
|
|
|
|
.AddressMode(VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT, VK_SAMPLER_ADDRESS_MODE_REPEAT)
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST)
|
|
|
|
.MaxLod(0.25f)
|
|
|
|
.DebugName("VkSamplerManager.mSamplers")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2022-07-02 08:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkSamplerManager::DeleteHWSamplers()
|
|
|
|
{
|
|
|
|
for (auto& sampler : mSamplers)
|
2020-06-07 13:02:54 +00:00
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
if (sampler)
|
|
|
|
fb->GetCommands()->DrawDeleteList->Add(std::move(sampler));
|
2020-06-07 13:02:54 +00:00
|
|
|
}
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:59 +00:00
|
|
|
VulkanSampler* VkSamplerManager::Get(PPFilterMode filter, PPWrapMode wrap)
|
|
|
|
{
|
|
|
|
int index = (((int)filter) << 1) | (int)wrap;
|
|
|
|
auto& sampler = mPPSamplers[index];
|
|
|
|
if (sampler)
|
|
|
|
return sampler.get();
|
|
|
|
|
|
|
|
sampler = SamplerBuilder()
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST)
|
|
|
|
.MinFilter(filter == PPFilterMode::Nearest ? VK_FILTER_NEAREST : VK_FILTER_LINEAR)
|
|
|
|
.MagFilter(filter == PPFilterMode::Nearest ? VK_FILTER_NEAREST : VK_FILTER_LINEAR)
|
|
|
|
.AddressMode(wrap == PPWrapMode::Clamp ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : VK_SAMPLER_ADDRESS_MODE_REPEAT)
|
|
|
|
.DebugName("VkPostprocess.mSamplers")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2022-07-02 08:09:59 +00:00
|
|
|
|
|
|
|
return sampler.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VkSamplerManager::CreateShadowmapSampler()
|
|
|
|
{
|
|
|
|
ShadowmapSampler = SamplerBuilder()
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_NEAREST)
|
|
|
|
.MinFilter(VK_FILTER_NEAREST)
|
|
|
|
.MagFilter(VK_FILTER_NEAREST)
|
|
|
|
.AddressMode(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)
|
|
|
|
.DebugName("VkRenderBuffers.ShadowmapSampler")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2022-07-02 08:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VkSamplerManager::CreateLightmapSampler()
|
2020-05-31 08:53:11 +00:00
|
|
|
{
|
2022-07-02 08:09:59 +00:00
|
|
|
LightmapSampler = SamplerBuilder()
|
|
|
|
.MipmapMode(VK_SAMPLER_MIPMAP_MODE_LINEAR)
|
|
|
|
.MinFilter(VK_FILTER_LINEAR)
|
|
|
|
.MagFilter(VK_FILTER_LINEAR)
|
|
|
|
.AddressMode(VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)
|
|
|
|
.DebugName("VkRenderBuffers.LightmapSampler")
|
2022-12-11 17:30:01 +00:00
|
|
|
.Create(fb->device.get());
|
2020-05-31 08:53:11 +00:00
|
|
|
}
|