|
|
|
|
@ -1,10 +1,15 @@
|
|
|
|
|
#include "texture.h"
|
|
|
|
|
#include "../assert.h"
|
|
|
|
|
#include "../log.h"
|
|
|
|
|
#include <limits>
|
|
|
|
|
Log_SetChannel(GL);
|
|
|
|
|
|
|
|
|
|
namespace GL {
|
|
|
|
|
|
|
|
|
|
static constexpr u32 MAX_DIMENSIONS = std::numeric_limits<u16>::max();
|
|
|
|
|
static constexpr u8 MAX_LEVELS = std::numeric_limits<u8>::max();
|
|
|
|
|
static constexpr u8 MAX_SAMPLES = std::numeric_limits<u8>::max();
|
|
|
|
|
|
|
|
|
|
Texture::Texture() = default;
|
|
|
|
|
|
|
|
|
|
Texture::Texture(Texture&& moved)
|
|
|
|
|
@ -33,12 +38,33 @@ bool Texture::UseTextureStorage() const
|
|
|
|
|
return UseTextureStorage(IsMultisampled());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
|
|
|
|
|
const void* data, bool linear_filter, bool wrap)
|
|
|
|
|
bool Texture::Create(u32 width, u32 height, u32 layers, u32 levels, u32 samples, GLenum internal_format, GLenum format,
|
|
|
|
|
GLenum type, const void* data /* = nullptr */, bool linear_filter /* = false */,
|
|
|
|
|
bool wrap /* = false */)
|
|
|
|
|
{
|
|
|
|
|
glGetError();
|
|
|
|
|
|
|
|
|
|
const GLenum target = (samples > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
|
|
|
|
if (width > MAX_DIMENSIONS || height > MAX_DIMENSIONS || layers > MAX_DIMENSIONS || levels > MAX_DIMENSIONS ||
|
|
|
|
|
samples > MAX_SAMPLES)
|
|
|
|
|
{
|
|
|
|
|
Log_ErrorPrintf("Invalid dimensions: %ux%ux%u %u %u", width, height, layers, levels, samples);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (samples > 1 && levels > 1)
|
|
|
|
|
{
|
|
|
|
|
Log_ErrorPrintf("Multisampled textures can't have mip levels");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (layers > 1 && data)
|
|
|
|
|
{
|
|
|
|
|
Log_ErrorPrintf("Loading texture array data not currently supported");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const GLenum target = ((samples > 1) ? ((layers > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D_MULTISAMPLE_ARRAY) :
|
|
|
|
|
((layers > 1) ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D));
|
|
|
|
|
|
|
|
|
|
GLuint id;
|
|
|
|
|
glGenTextures(1, &id);
|
|
|
|
|
@ -48,27 +74,51 @@ bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format,
|
|
|
|
|
{
|
|
|
|
|
Assert(!data);
|
|
|
|
|
if (UseTextureStorage(true))
|
|
|
|
|
glTexStorage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
|
|
|
|
{
|
|
|
|
|
if (layers > 1)
|
|
|
|
|
glTexStorage3DMultisample(target, samples, internal_format, width, height, layers, GL_FALSE);
|
|
|
|
|
else
|
|
|
|
|
glTexStorage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
glTexImage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
|
|
|
|
{
|
|
|
|
|
if (layers > 1)
|
|
|
|
|
glTexImage3DMultisample(target, samples, internal_format, width, height, layers, GL_FALSE);
|
|
|
|
|
else
|
|
|
|
|
glTexImage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (UseTextureStorage(false))
|
|
|
|
|
{
|
|
|
|
|
glTexStorage2D(target, 1, internal_format, width, height);
|
|
|
|
|
if (data)
|
|
|
|
|
glTexSubImage2D(target, 0, 0, 0, width, height, format, type, data);
|
|
|
|
|
if (layers > 1)
|
|
|
|
|
glTexStorage3D(target, levels, internal_format, width, height, layers);
|
|
|
|
|
else
|
|
|
|
|
glTexStorage2D(target, levels, internal_format, width, height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glTexImage2D(target, 0, internal_format, width, height, 0, format, type, data);
|
|
|
|
|
for (u32 i = 0; i < levels; i++)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Fix data pointer here.
|
|
|
|
|
if (layers > 1)
|
|
|
|
|
glTexImage3D(target, i, internal_format, width, height, layers, 0, format, type, data);
|
|
|
|
|
else
|
|
|
|
|
glTexImage2D(target, i, internal_format, width, height, 0, format, type, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
|
|
if (layers > 1)
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This doesn't exist on GLES2.
|
|
|
|
|
@ -87,21 +137,67 @@ bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format,
|
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
|
|
m_id = id;
|
|
|
|
|
m_width = width;
|
|
|
|
|
m_height = height;
|
|
|
|
|
m_samples = samples;
|
|
|
|
|
m_width = static_cast<u16>(width);
|
|
|
|
|
m_height = static_cast<u16>(height);
|
|
|
|
|
m_layers = static_cast<u16>(layers);
|
|
|
|
|
m_levels = static_cast<u8>(levels);
|
|
|
|
|
m_samples = static_cast<u8>(samples);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::Replace(u32 width, u32 height, GLenum internal_format, GLenum format, GLenum type, const void* data)
|
|
|
|
|
{
|
|
|
|
|
Assert(IsValid() && m_samples == 1);
|
|
|
|
|
Assert(IsValid() && width < MAX_DIMENSIONS && height < MAX_DIMENSIONS && m_layers == 1 && m_samples == 1 &&
|
|
|
|
|
m_levels == 1);
|
|
|
|
|
|
|
|
|
|
const bool size_changed = (width != m_width || height != m_height);
|
|
|
|
|
|
|
|
|
|
m_width = width;
|
|
|
|
|
m_height = height;
|
|
|
|
|
m_width = static_cast<u16>(width);
|
|
|
|
|
m_height = static_cast<u16>(height);
|
|
|
|
|
m_levels = 1;
|
|
|
|
|
|
|
|
|
|
const GLenum target = GetGLTarget();
|
|
|
|
|
glBindTexture(target, m_id);
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, data);
|
|
|
|
|
if (UseTextureStorage())
|
|
|
|
|
{
|
|
|
|
|
if (size_changed)
|
|
|
|
|
{
|
|
|
|
|
if (m_layers > 0)
|
|
|
|
|
glTexStorage3D(target, m_levels, internal_format, m_width, m_height, m_levels);
|
|
|
|
|
else
|
|
|
|
|
glTexStorage2D(target, m_levels, internal_format, m_width, m_height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glTexSubImage2D(target, 0, 0, 0, m_width, m_height, format, type, data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glTexImage2D(target, 0, internal_format, width, height, 0, format, type, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::ReplaceImage(u32 layer, u32 level, GLenum format, GLenum type, const void* data)
|
|
|
|
|
{
|
|
|
|
|
Assert(IsValid() && !IsMultisampled());
|
|
|
|
|
|
|
|
|
|
const GLenum target = GetGLTarget();
|
|
|
|
|
if (IsTextureArray())
|
|
|
|
|
glTexSubImage3D(target, level, 0, 0, layer, m_width, m_height, 1, format, type, data);
|
|
|
|
|
else
|
|
|
|
|
glTexSubImage2D(target, level, 0, 0, m_width, m_height, format, type, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::ReplaceSubImage(u32 layer, u32 level, u32 x, u32 y, u32 width, u32 height, GLenum format, GLenum type,
|
|
|
|
|
const void* data)
|
|
|
|
|
{
|
|
|
|
|
Assert(IsValid() && !IsMultisampled());
|
|
|
|
|
|
|
|
|
|
const GLenum target = GetGLTarget();
|
|
|
|
|
if (IsTextureArray())
|
|
|
|
|
glTexSubImage3D(target, level, x, y, layer, width, height, 1, format, type, data);
|
|
|
|
|
else
|
|
|
|
|
glTexSubImage2D(target, level, x, y, width, height, format, type, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Texture::SetLinearFilter(bool enabled)
|
|
|
|
|
@ -154,6 +250,8 @@ void Texture::Destroy()
|
|
|
|
|
|
|
|
|
|
m_width = 0;
|
|
|
|
|
m_height = 0;
|
|
|
|
|
m_layers = 0;
|
|
|
|
|
m_levels = 0;
|
|
|
|
|
m_samples = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -170,7 +268,7 @@ void Texture::BindFramebuffer(GLenum target /*= GL_DRAW_FRAMEBUFFER*/)
|
|
|
|
|
|
|
|
|
|
void Texture::Unbind()
|
|
|
|
|
{
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
glBindTexture(GetGLTarget(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Texture& Texture::operator=(Texture&& moved)
|
|
|
|
|
@ -180,12 +278,16 @@ Texture& Texture::operator=(Texture&& moved)
|
|
|
|
|
m_id = moved.m_id;
|
|
|
|
|
m_width = moved.m_width;
|
|
|
|
|
m_height = moved.m_height;
|
|
|
|
|
m_layers = moved.m_layers;
|
|
|
|
|
m_levels = moved.m_levels;
|
|
|
|
|
m_samples = moved.m_samples;
|
|
|
|
|
m_fbo_id = moved.m_fbo_id;
|
|
|
|
|
|
|
|
|
|
moved.m_id = 0;
|
|
|
|
|
moved.m_width = 0;
|
|
|
|
|
moved.m_height = 0;
|
|
|
|
|
moved.m_layers = 0;
|
|
|
|
|
moved.m_levels = 0;
|
|
|
|
|
moved.m_samples = 0;
|
|
|
|
|
moved.m_fbo_id = 0;
|
|
|
|
|
return *this;
|
|
|
|
|
|