| 1 | // Copyright 2013 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/OGL/SamplerCache.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | |
| 8 | #include "Common/CommonTypes.h" |
| 9 | #include "VideoBackends/OGL/OGLConfig.h" |
| 10 | #include "VideoCommon/VideoConfig.h" |
| 11 | |
| 12 | namespace OGL |
| 13 | { |
| 14 | std::unique_ptr<SamplerCache> g_sampler_cache; |
| 15 | |
| 16 | SamplerCache::SamplerCache() |
| 17 | { |
| 18 | glGenSamplers(1, &m_point_sampler); |
| 19 | glGenSamplers(1, &m_linear_sampler); |
| 20 | glSamplerParameteri(m_point_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 21 | glSamplerParameteri(m_point_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 22 | glSamplerParameteri(m_point_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 23 | glSamplerParameteri(m_point_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 24 | glSamplerParameteri(m_linear_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 25 | glSamplerParameteri(m_linear_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 26 | glSamplerParameteri(m_linear_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 27 | glSamplerParameteri(m_linear_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 28 | } |
| 29 | |
| 30 | SamplerCache::~SamplerCache() |
| 31 | { |
| 32 | Clear(); |
| 33 | glDeleteSamplers(1, &m_point_sampler); |
| 34 | glDeleteSamplers(1, &m_linear_sampler); |
| 35 | } |
| 36 | |
| 37 | void SamplerCache::BindNearestSampler(int stage) |
| 38 | { |
| 39 | glBindSampler(stage, m_point_sampler); |
| 40 | } |
| 41 | |
| 42 | void SamplerCache::BindLinearSampler(int stage) |
| 43 | { |
| 44 | glBindSampler(stage, m_linear_sampler); |
| 45 | } |
| 46 | |
| 47 | void SamplerCache::SetSamplerState(u32 stage, const SamplerState& state) |
| 48 | { |
| 49 | if (m_active_samplers[stage].first == state && m_active_samplers[stage].second != 0) |
| 50 | return; |
| 51 | |
| 52 | auto it = m_cache.find(state); |
| 53 | if (it == m_cache.end()) |
| 54 | { |
| 55 | GLuint sampler; |
| 56 | glGenSamplers(1, &sampler); |
| 57 | SetParameters(sampler, state); |
| 58 | it = m_cache.emplace(state, sampler).first; |
| 59 | } |
| 60 | |
| 61 | m_active_samplers[stage].first = state; |
| 62 | m_active_samplers[stage].second = it->second; |
| 63 | glBindSampler(stage, it->second); |
| 64 | } |
| 65 | |
| 66 | void SamplerCache::InvalidateBinding(u32 stage) |
| 67 | { |
| 68 | m_active_samplers[stage].second = 0; |
| 69 | } |
| 70 | |
| 71 | void SamplerCache::SetParameters(GLuint sampler_id, const SamplerState& params) |
| 72 | { |
| 73 | GLenum min_filter; |
| 74 | GLenum mag_filter = (params.tm0.mag_filter == FilterMode::Near) ? GL_NEAREST : GL_LINEAR; |
| 75 | if (params.tm0.mipmap_filter == FilterMode::Linear) |
| 76 | { |
| 77 | min_filter = (params.tm0.min_filter == FilterMode::Near) ? GL_NEAREST_MIPMAP_LINEAR : |
| 78 | GL_LINEAR_MIPMAP_LINEAR; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | min_filter = (params.tm0.min_filter == FilterMode::Near) ? GL_NEAREST_MIPMAP_NEAREST : |
| 83 | GL_LINEAR_MIPMAP_NEAREST; |
| 84 | } |
| 85 | |
| 86 | glSamplerParameteri(sampler_id, GL_TEXTURE_MIN_FILTER, min_filter); |
| 87 | glSamplerParameteri(sampler_id, GL_TEXTURE_MAG_FILTER, mag_filter); |
| 88 | |
| 89 | static constexpr std::array<GLenum, 3> address_modes = { |
| 90 | {GL_CLAMP_TO_EDGE, GL_REPEAT, GL_MIRRORED_REPEAT}}; |
| 91 | |
| 92 | glSamplerParameteri(sampler_id, GL_TEXTURE_WRAP_S, |
| 93 | address_modes[static_cast<u32>(params.tm0.wrap_u.Value())]); |
| 94 | glSamplerParameteri(sampler_id, GL_TEXTURE_WRAP_T, |
| 95 | address_modes[static_cast<u32>(params.tm0.wrap_v.Value())]); |
| 96 | |
| 97 | glSamplerParameterf(sampler_id, GL_TEXTURE_MIN_LOD, params.tm1.min_lod / 16.f); |
| 98 | glSamplerParameterf(sampler_id, GL_TEXTURE_MAX_LOD, params.tm1.max_lod / 16.f); |
| 99 | |
| 100 | if (g_backend_info.bSupportsLodBiasInSampler) |
| 101 | { |
| 102 | glSamplerParameterf(sampler_id, GL_TEXTURE_LOD_BIAS, params.tm0.lod_bias / 256.f); |
| 103 | } |
| 104 | |
| 105 | if (params.tm0.anisotropic_filtering != 0 && g_ogl_config.bSupportsAniso) |
| 106 | { |
| 107 | glSamplerParameterf(sampler_id, GL_TEXTURE_MAX_ANISOTROPY_EXT, |
| 108 | static_cast<float>(1 << params.tm0.anisotropic_filtering)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void SamplerCache::Clear() |
| 113 | { |
| 114 | for (auto& p : m_cache) |
| 115 | glDeleteSamplers(1, &p.second); |
| 116 | for (auto& p : m_active_samplers) |
| 117 | p.second = 0; |
| 118 | m_cache.clear(); |
| 119 | } |
| 120 | } // namespace OGL |