1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Prefer immutable texture storage when available

This commit is contained in:
sfan5 2025-04-06 20:25:03 +02:00
parent 427a7e4998
commit d5bf094f9a
7 changed files with 42 additions and 24 deletions

View file

@ -283,6 +283,17 @@ inline s32 s32_clamp(s32 value, s32 low, s32 high)
return clamp(value, low, high);
}
// integer log2 of an integer. returning 0 if denormal
inline s32 u32_log2(u32 in)
{
s32 ret = 0;
while (in > 1) {
in >>= 1;
ret++;
}
return ret;
}
/*
float IEEE-754 bit representation

View file

@ -363,7 +363,7 @@ inline SColor CImage::getPixelBox(s32 x, s32 y, s32 fx, s32 fy, s32 bias) const
}
}
s32 sdiv = s32_log2_s32(fx * fy);
s32 sdiv = core::u32_log2(fx * fy);
a = core::s32_clamp((a >> sdiv) + bias, 0, 255);
r = core::s32_clamp((r >> sdiv) + bias, 0, 255);

View file

@ -14,20 +14,18 @@ namespace video
class COpenGLCoreFeature
{
public:
COpenGLCoreFeature() :
BlendOperation(false), ColorAttachment(0), MultipleRenderTarget(0), MaxTextureUnits(1)
{
}
COpenGLCoreFeature() = default;
virtual ~COpenGLCoreFeature()
{
}
bool BlendOperation;
bool BlendOperation = false;
bool TexStorage = false;
u8 ColorAttachment;
u8 MultipleRenderTarget;
u8 MaxTextureUnits;
u8 ColorAttachment = 0;
u8 MultipleRenderTarget = 0;
u8 MaxTextureUnits = 0;
};
}

View file

@ -503,10 +503,22 @@ protected:
return;
}
u32 levels = 1;
if (HasMipMaps) {
levels = core::u32_log2(core::max_(Size.Width, Size.Height)) + 1;
}
// reference: <https://www.khronos.org/opengl/wiki/Texture_Storage>
switch (Type) {
case ETT_2D:
if (Driver->getFeature().TexStorage) {
GL.TexStorage2D(TextureType, levels, InternalFormat,
Size.Width, Size.Height);
} else {
GL.TexImage2D(TextureType, 0, InternalFormat,
Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
}
TEST_GL_ERROR(Driver);
break;
case ETT_2D_MS: {
@ -531,8 +543,14 @@ protected:
}
case ETT_CUBEMAP:
for (u32 i = 0; i < 6; i++) {
GL.TexImage2D(getTextureTarget(i), 0, InternalFormat,
GLenum target = getTextureTarget(i);
if (Driver->getFeature().TexStorage) {
GL.TexStorage2D(target, levels, InternalFormat,
Size.Width, Size.Height);
} else {
GL.TexImage2D(target, 0, InternalFormat,
Size.Width, Size.Height, 0, PixelFormat, PixelType, 0);
}
TEST_GL_ERROR(Driver);
}
break;

View file

@ -78,6 +78,7 @@ void COpenGL3Driver::initFeatures()
// COGLESCoreExtensionHandler::Feature
static_assert(MATERIAL_MAX_TEXTURES <= 16, "Only up to 16 textures are guaranteed");
Feature.BlendOperation = true;
Feature.TexStorage = isVersionAtLeast(4, 2) || queryExtension("GL_ARB_texture_storage");
Feature.ColorAttachment = GetInteger(GL_MAX_COLOR_ATTACHMENTS);
Feature.MaxTextureUnits = MATERIAL_MAX_TEXTURES;
Feature.MultipleRenderTarget = GetInteger(GL_MAX_DRAW_BUFFERS);

View file

@ -131,6 +131,7 @@ void COpenGLES2Driver::initFeatures()
// COGLESCoreExtensionHandler::Feature
static_assert(MATERIAL_MAX_TEXTURES <= 8, "Only up to 8 textures are guaranteed");
Feature.BlendOperation = true;
Feature.TexStorage = Version.Major >= 3 || queryExtension("GL_ARB_texture_storage");
Feature.ColorAttachment = 1;
if (MRTSupported)
Feature.ColorAttachment = GetInteger(GL_MAX_COLOR_ATTACHMENTS);

View file

@ -93,17 +93,6 @@ inline void memset16(void *dest, const u16 value, size_t bytesize)
}
}
// integer log2 of an integer. returning 0 as denormal
static inline s32 s32_log2_s32(u32 in)
{
s32 ret = 0;
while (in > 1) {
in >>= 1;
ret++;
}
return ret;
}
// ------------------ Video---------------------------------------
/*!
Pixel = dest * ( 1 - alpha ) + source * alpha