2019-11-03 05:22:37 +00:00
|
|
|
#include "texture.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "../assert.h"
|
|
|
|
#include "../log.h"
|
2019-09-12 14:18:13 +00:00
|
|
|
Log_SetChannel(GL);
|
|
|
|
|
|
|
|
namespace GL {
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
Texture::Texture() = default;
|
|
|
|
|
|
|
|
Texture::Texture(Texture&& moved)
|
2020-10-30 14:38:06 +00:00
|
|
|
: m_id(moved.m_id), m_width(moved.m_width), m_height(moved.m_height), m_samples(moved.m_samples),
|
|
|
|
m_fbo_id(moved.m_fbo_id)
|
2019-09-12 14:18:13 +00:00
|
|
|
{
|
2019-12-10 14:01:29 +00:00
|
|
|
moved.m_id = 0;
|
|
|
|
moved.m_width = 0;
|
|
|
|
moved.m_height = 0;
|
2020-10-30 14:38:06 +00:00
|
|
|
moved.m_samples = 0;
|
2019-12-10 14:01:29 +00:00
|
|
|
moved.m_fbo_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture::~Texture()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2020-10-30 14:38:06 +00:00
|
|
|
bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
|
|
|
|
const void* data, bool linear_filter, bool wrap)
|
2019-12-10 14:01:29 +00:00
|
|
|
{
|
|
|
|
glGetError();
|
|
|
|
|
2020-10-30 14:38:06 +00:00
|
|
|
const GLenum target = (samples > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
GLuint id;
|
|
|
|
glGenTextures(1, &id);
|
2020-10-30 14:38:06 +00:00
|
|
|
glBindTexture(target, id);
|
|
|
|
|
|
|
|
if (samples > 1)
|
|
|
|
{
|
2020-12-25 08:01:39 +00:00
|
|
|
Assert(!data);
|
2020-10-31 05:11:53 +00:00
|
|
|
if (GLAD_GL_ARB_texture_storage || GLAD_GL_ES_VERSION_3_1)
|
|
|
|
glTexStorage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
|
|
|
else
|
|
|
|
glTexImage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
2020-10-30 14:38:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-25 08:01:39 +00:00
|
|
|
if ((GLAD_GL_ARB_texture_storage || GLAD_GL_ES_VERSION_3_0) && !data)
|
2020-10-31 05:11:53 +00:00
|
|
|
glTexStorage2D(target, 1, internal_format, width, height);
|
|
|
|
else
|
|
|
|
glTexImage2D(target, 0, internal_format, width, height, 0, format, type, data);
|
2020-10-30 14:38:06 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 1);
|
2019-10-22 14:35:08 +00:00
|
|
|
|
2020-01-08 22:46:52 +00:00
|
|
|
GLenum error = glGetError();
|
|
|
|
if (error != GL_NO_ERROR)
|
2019-10-22 14:35:08 +00:00
|
|
|
{
|
2020-01-08 22:46:52 +00:00
|
|
|
Log_ErrorPrintf("Failed to create texture: 0x%X", error);
|
2019-12-10 14:01:29 +00:00
|
|
|
glDeleteTextures(1, &id);
|
|
|
|
return false;
|
2019-10-22 14:35:08 +00:00
|
|
|
}
|
2019-12-10 14:01:29 +00:00
|
|
|
|
|
|
|
if (IsValid())
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
m_id = id;
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2020-10-30 14:38:06 +00:00
|
|
|
m_samples = samples;
|
2019-12-10 14:01:29 +00:00
|
|
|
return true;
|
2019-09-12 14:18:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 08:01:39 +00:00
|
|
|
void Texture::Replace(u32 width, u32 height, GLenum internal_format, GLenum format, GLenum type, const void* data)
|
|
|
|
{
|
|
|
|
Assert(IsValid() && m_samples == 1);
|
|
|
|
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, data);
|
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
void Texture::SetLinearFilter(bool enabled)
|
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
Assert(!IsMultisampled());
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
Bind();
|
|
|
|
|
2020-10-30 14:38:06 +00:00
|
|
|
const GLenum target = GetGLTarget();
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
2019-12-10 14:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Texture::CreateFramebuffer()
|
|
|
|
{
|
|
|
|
if (!IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetError();
|
|
|
|
|
|
|
|
GLuint fbo_id;
|
|
|
|
glGenFramebuffers(1, &fbo_id);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_id);
|
|
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_id, 0);
|
|
|
|
if (glGetError() != GL_NO_ERROR || glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
{
|
|
|
|
glDeleteFramebuffers(1, &fbo_id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_fbo_id != 0)
|
|
|
|
glDeleteFramebuffers(1, &m_fbo_id);
|
|
|
|
|
|
|
|
m_fbo_id = fbo_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::Destroy()
|
2019-09-12 14:18:13 +00:00
|
|
|
{
|
2019-10-22 14:35:08 +00:00
|
|
|
if (m_fbo_id != 0)
|
2019-12-10 14:01:29 +00:00
|
|
|
{
|
2019-10-22 14:35:08 +00:00
|
|
|
glDeleteFramebuffers(1, &m_fbo_id);
|
2019-12-10 14:01:29 +00:00
|
|
|
m_fbo_id = 0;
|
|
|
|
}
|
|
|
|
if (m_id != 0)
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &m_id);
|
|
|
|
m_id = 0;
|
|
|
|
}
|
2019-10-22 14:35:08 +00:00
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
2020-10-30 14:38:06 +00:00
|
|
|
m_samples = 0;
|
2019-09-12 14:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::Bind()
|
|
|
|
{
|
2020-10-30 14:38:06 +00:00
|
|
|
glBindTexture(GetGLTarget(), m_id);
|
2019-09-12 14:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:35:08 +00:00
|
|
|
void Texture::BindFramebuffer(GLenum target /*= GL_DRAW_FRAMEBUFFER*/)
|
|
|
|
{
|
|
|
|
DebugAssert(m_fbo_id != 0);
|
|
|
|
glBindFramebuffer(target, m_fbo_id);
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:07:31 +00:00
|
|
|
void Texture::Unbind()
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
2019-12-10 14:01:29 +00:00
|
|
|
Texture& Texture::operator=(Texture&& moved)
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
m_id = moved.m_id;
|
|
|
|
m_width = moved.m_width;
|
|
|
|
m_height = moved.m_height;
|
2020-10-30 14:38:06 +00:00
|
|
|
m_samples = moved.m_samples;
|
2019-12-10 14:01:29 +00:00
|
|
|
m_fbo_id = moved.m_fbo_id;
|
|
|
|
|
|
|
|
moved.m_id = 0;
|
|
|
|
moved.m_width = 0;
|
|
|
|
moved.m_height = 0;
|
2020-10-30 14:38:06 +00:00
|
|
|
moved.m_samples = 0;
|
2019-12-10 14:01:29 +00:00
|
|
|
moved.m_fbo_id = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:03:25 +00:00
|
|
|
void Texture::GetTextureSubImage(GLuint texture, GLint level, GLint xoffset, GLint yoffset, GLint zoffset,
|
|
|
|
GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type,
|
|
|
|
GLsizei bufSize, void* pixels)
|
|
|
|
{
|
2020-05-02 06:47:31 +00:00
|
|
|
if (GLAD_GL_VERSION_4_5 || GLAD_GL_ARB_get_texture_sub_image)
|
2020-03-15 14:03:25 +00:00
|
|
|
{
|
|
|
|
glGetTextureSubImage(texture, level, xoffset, yoffset, zoffset, width, height, depth, format, type, bufSize,
|
|
|
|
pixels);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:34:13 +00:00
|
|
|
GLenum target = GL_READ_FRAMEBUFFER;
|
|
|
|
GLenum target_binding = GL_READ_FRAMEBUFFER_BINDING;
|
|
|
|
if (GLAD_GL_ES_VERSION_2_0 && !GLAD_GL_ES_VERSION_3_0)
|
|
|
|
{
|
|
|
|
// GLES2 doesn't have GL_READ_FRAMEBUFFER.
|
|
|
|
target = GL_FRAMEBUFFER;
|
|
|
|
target_binding = GL_FRAMEBUFFER_BINDING;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:03:25 +00:00
|
|
|
Assert(depth == 1);
|
|
|
|
|
|
|
|
GLuint old_read_fbo;
|
2021-01-25 15:34:13 +00:00
|
|
|
glGetIntegerv(target_binding, reinterpret_cast<GLint*>(&old_read_fbo));
|
2020-03-15 14:03:25 +00:00
|
|
|
|
|
|
|
GLuint temp_fbo;
|
|
|
|
glGenFramebuffers(1, &temp_fbo);
|
2021-01-25 15:34:13 +00:00
|
|
|
glBindFramebuffer(target, temp_fbo);
|
2020-03-15 14:03:25 +00:00
|
|
|
if (zoffset > 0 && (GLAD_GL_VERSION_3_0 || GLAD_GL_ES_VERSION_3_0))
|
2021-01-25 15:34:13 +00:00
|
|
|
glFramebufferTextureLayer(target, GL_COLOR_ATTACHMENT0, texture, level, zoffset);
|
2020-03-15 14:03:25 +00:00
|
|
|
else
|
2021-01-25 15:34:13 +00:00
|
|
|
glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
|
2020-03-15 14:03:25 +00:00
|
|
|
|
2021-01-25 15:34:13 +00:00
|
|
|
DebugAssert(glCheckFramebufferStatus(target) == GL_FRAMEBUFFER_COMPLETE);
|
2020-03-15 14:03:25 +00:00
|
|
|
glReadPixels(xoffset, yoffset, width, height, format, type, pixels);
|
|
|
|
|
2021-01-25 15:34:13 +00:00
|
|
|
glBindFramebuffer(target, old_read_fbo);
|
2020-03-15 14:03:25 +00:00
|
|
|
glDeleteFramebuffers(1, &temp_fbo);
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:34:13 +00:00
|
|
|
} // namespace GL
|