Skip to content

Commit a08b780

Browse files
authored
Unify samplte state anisotropy range (axmolengine#2959)
1 parent bad0904 commit a08b780

File tree

8 files changed

+17
-18
lines changed

8 files changed

+17
-18
lines changed

axmol/rhi/RHITypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ struct SamplerDesc
379379
SamplerAddressMode tAddressMode : 2 = SamplerAddressMode::CLAMP;
380380
SamplerAddressMode wAddressMode : 2 = SamplerAddressMode::CLAMP;
381381
CompareFunc compareFunc : 4 = CompareFunc::NEVER;
382-
uint32_t anisotropy : 4 = 1;
382+
uint32_t anisotropy : 4 = 0;
383383
uint32_t reserved : 12 = 0;
384384
};
385385
static_assert(sizeof(SamplerDesc) == 4, "incompatible type: SamplerDesc");

axmol/rhi/SamplerCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void SamplerCache::createBuiltinSamplers()
194194
d.minFilter = SamplerFilter::MIN_ANISOTROPIC;
195195
d.magFilter = SamplerFilter::MAG_LINEAR;
196196
d.mipFilter = SamplerFilter::MIP_LINEAR;
197-
d.anisotropy = 16;
197+
d.anisotropy = 0xF;
198198

199199
d.sAddressMode = SamplerAddressMode::CLAMP;
200200
createBuiltinSampler(SamplerIndex::AnisoClamp, d);

axmol/rhi/d3d11/Driver11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ SamplerHandle DriverImpl::createSampler(const SamplerDesc& desc)
342342
if (desc.minFilter == SamplerFilter::MIN_ANISOTROPIC)
343343
{
344344
sd.Filter = D3D11_FILTER_ANISOTROPIC;
345-
sd.MaxAnisotropy = desc.anisotropy ? desc.anisotropy : 1;
345+
sd.MaxAnisotropy = std::clamp(desc.anisotropy + 1u, 1u, 16u);
346346
}
347347
else
348348
{

axmol/rhi/d3d12/Driver12.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ SamplerHandle DriverImpl::createSampler(const SamplerDesc& desc)
613613
else
614614
sd.Filter = D3D12_FILTER_ANISOTROPIC;
615615

616-
sd.MaxAnisotropy = desc.anisotropy ? desc.anisotropy : 1;
616+
sd.MaxAnisotropy = std::clamp(desc.anisotropy + 1u, 1u, 16u);
617617
}
618618
else
619619
{

axmol/rhi/metal/DriverMTL.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ bool supportS3TC(FeatureSet featureSet)
568568
samplerDesc.compareFunction = toMTLCompareFunc(desc.compareFunc);
569569

570570
// --- Anisotropy ---
571-
samplerDesc.maxAnisotropy = std::clamp(desc.anisotropy, 1u, 16u);
571+
samplerDesc.maxAnisotropy = std::clamp(desc.anisotropy + 1u, 1u, 16u);
572572

573573
// --- Create Sampler ---
574574
id<MTLSamplerState> sampler = [_mtlDevice newSamplerStateWithDescriptor:samplerDesc];

axmol/rhi/opengl/DriverGL.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ DriverImpl::DriverImpl()
167167
// supported");
168168
// }
169169

170+
#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
171+
if (hasExtension("GL_EXT_texture_filter_anisotropic"))
172+
{
173+
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &_cap.maxAnisotropy);
174+
}
175+
#endif
176+
170177
// default FBO
171178
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_defaultFBO);
172179

@@ -334,20 +341,11 @@ SamplerHandle DriverImpl::createSampler(const SamplerDesc& desc)
334341
}
335342

336343
// --- Anisotropy ---
337-
#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
338-
if (desc.anisotropy > 1)
344+
if (desc.anisotropy > 0 && _cap.maxAnisotropy > 1.0f)
339345
{
340-
GLfloat aniso = static_cast<GLfloat>(desc.anisotropy);
341-
GLfloat maxAniso = 0.0f;
342-
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
343-
if (maxAniso > 0.0f)
344-
{
345-
if (aniso > maxAniso)
346-
aniso = maxAniso;
347-
glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
348-
}
346+
GLfloat aniso = std::clamp(static_cast<GLfloat>(desc.anisotropy + 1), 1.0f, _cap.maxAnisotropy);
347+
glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
349348
}
350-
#endif
351349

352350
return reinterpret_cast<SamplerHandle>(static_cast<uintptr_t>(sampler));
353351
}

axmol/rhi/opengl/DriverGL.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct DriverCapImpl
4141
bool textureCompressionAstc{false};
4242
bool textureCompressionEtc2{false};
4343
bool vertexAttribBinding{false};
44+
float maxAnisotropy{0.0f};
4445
};
4546

4647
/**

axmol/rhi/vulkan/DriverVK.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ SamplerHandle DriverImpl::createSampler(const SamplerDesc& desc)
588588

589589
// Anisotropy
590590
info.anisotropyEnable = (desc.minFilter == SamplerFilter::MIN_ANISOTROPIC) ? VK_TRUE : VK_FALSE;
591-
info.maxAnisotropy = (desc.anisotropy > 0 ? static_cast<float>(desc.anisotropy) : 1.0f);
591+
info.maxAnisotropy = static_cast<float>(std::clamp(desc.anisotropy + 1u, 1u, 16u));
592592

593593
info.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
594594
info.unnormalizedCoordinates = VK_FALSE;

0 commit comments

Comments
 (0)