KDE/app/src/main/cpp/Texture.cpp

116 lines
3.1 KiB
C++

#include "Texture.h"
#include "Profiling.h"
#include "Renderer.h"
#ifdef __APPLE__
#include "../m/MetalGfxInterface.h"
#endif
#if defined(__ANDROID__) || defined(_WIN32)
Texture::Texture() : m_texture(0) {}
Texture::Texture(unsigned int width, unsigned int height, const void* data) : m_texture(0)
{
ZoneScoped;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
width_px = width;
height_px = height;
}
Texture::Texture(unsigned int width, unsigned int height, const void* data, GLint format) : m_texture(0)
{
ZoneScoped;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_R8,
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
0,
GL_RED,
GL_UNSIGNED_BYTE,
data);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
width_px = width;
height_px = height;
}
void Texture::destroy()
{
ZoneScoped;
glDeleteTextures(1, &m_texture);
}
void Texture::bind() const
{
ZoneScoped;
glBindTexture(GL_TEXTURE_2D, m_texture);
}
#else
Texture::Texture() : m_texture(nullptr) {}
Texture::Texture(unsigned int width, unsigned int height, const void* data)
: width_px(width), height_px(height)
{
m_texture = metalGfxCreateTexture(Renderer::ptr->getGfx(), data, width, height, MetalGfxTextureFormat_RGBA);
}
Texture::Texture(unsigned int width, unsigned int height, const void* data, int dummy)
: width_px(width), height_px(height)
{
m_texture = metalGfxCreateTexture(Renderer::ptr->getGfx(), data, width, height, MetalGfxTextureFormat_Red);
}
void Texture::destroy()
{
metalGfxDestroyTexture(m_texture);
m_texture = nullptr;
}
void Texture::bind() const
{
/* This is a no-op under metal? */
}
#endif
void Texture::getTextureSize(unsigned int* w, unsigned int* h)
{
ZoneScoped;
if (w != nullptr) {
*w = width_px;
}
if (h != nullptr) {
*h = height_px;
}
}