Navigation Menu

Skip to content

Commit

Permalink
Create a pure GLTexture class
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronial committed May 15, 2019
1 parent ece5649 commit a4731a8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
51 changes: 23 additions & 28 deletions Image.cpp
Expand Up @@ -238,13 +238,12 @@ UploadReadyImage& UploadReadyImage::operator=(UploadReadyImage&& other) {
return *this;
}

GLTexture UploadReadyImage::upload() const {
GLImage UploadReadyImage::upload() const {
TRACK_CALL_TEXT("UploadReadyImage::upload");
IF_DEBUG(double preLoad = time());
// TODO: handle opengl errors?
GLuint glTexture;
glGenTextures(1, &glTexture);
glBindTexture(GL_TEXTURE_2D, glTexture);
GLTexture texture{};
texture.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16);
Expand All @@ -261,45 +260,42 @@ GLTexture UploadReadyImage::upload() const {
glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image.data.get());
IF_DEBUG(console::out() << "GLUpload " << (time() - preLoad) * 1000 << " ms");
return GLTexture(glTexture, static_cast<float>(originalAspect));
return GLImage(std::move(texture), static_cast<float>(originalAspect));
}

GLTexture::GLTexture(GLuint glTexture, float originalAspect)
: glTexture(glTexture), originalAspect(originalAspect) {}
GLTexture::GLTexture() {
glGenTextures(1, &glTexture);
}

GLTexture::GLTexture(GLTexture&& other) noexcept
: glTexture(other.glTexture), originalAspect(other.originalAspect) {
GLTexture::GLTexture(GLTexture&& other) noexcept : glTexture(other.glTexture) {
other.glTexture = 0;
}

GLTexture& GLTexture::operator=(GLTexture&& other) noexcept {
originalAspect = other.originalAspect;
glDelete();
reset();
glTexture = other.glTexture;
other.glTexture = 0;
return *this;
}

GLTexture::~GLTexture() noexcept {
if (glTexture != 0)
glDelete();
}

void GLTexture::bind() const {
glBindTexture(GL_TEXTURE_2D, glTexture);
reset();
}

void GLTexture::glDelete() noexcept {
IF_DEBUG(console::out() << "DELETE");
glDeleteTextures(1, &glTexture);
glTexture = 0;
void GLTexture::reset() noexcept {
if (glTexture != 0) {
IF_DEBUG(console::out() << "DELETE");
glDeleteTextures(1, &glTexture);
glTexture = 0;
}
}

float GLTexture::getAspect() const {
return originalAspect;
void GLTexture::bind() const {
PFC_ASSERT(glTexture != 0);
glBindTexture(GL_TEXTURE_2D, glTexture);
}

GLTexture loadSpinner() {
GLImage loadSpinner() {
LPCWSTR pName = MAKEINTRESOURCE(IDB_SPINNER);
auto hInst = core_api::get_my_instance();

Expand All @@ -318,15 +314,14 @@ GLTexture loadSpinner() {
if (data == nullptr) {
throw std::runtime_error{"Failed to load image buffer"};
}
GLuint glTexture;
glGenTextures(1, &glTexture);
glBindTexture(GL_TEXTURE_2D, glTexture);
GLTexture texture{};
texture.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
return GLTexture(glTexture, 1.0);
return GLImage(std::move(texture), 1.0);
}
23 changes: 17 additions & 6 deletions Image.h
Expand Up @@ -21,20 +21,31 @@ class Image {

class GLTexture {
public:
GLTexture(GLuint glTexture, float originalAspect);
GLTexture();
GLTexture(const GLTexture&) = delete;
GLTexture& operator=(const GLTexture&) = delete;
GLTexture(GLTexture&&) noexcept;
GLTexture& operator=(GLTexture&&) noexcept;
~GLTexture() noexcept;

void bind() const;
float getAspect() const;

private:
GLuint glTexture;
void reset() noexcept;
GLuint glTexture = 0;
};

class GLImage {
public:
GLImage(GLTexture glTexture, float originalAspect)
: glTexture(std::move(glTexture)), originalAspect(originalAspect){};

void bind() const { glTexture.bind(); };
float getAspect() const { return originalAspect; };

private:
GLTexture glTexture;
float originalAspect;
void glDelete() noexcept;
};

class UploadReadyImage {
Expand All @@ -47,7 +58,7 @@ class UploadReadyImage {
UploadReadyImage& operator=(UploadReadyImage&&);
~UploadReadyImage() = default;

GLTexture upload() const;
GLImage upload() const;

private:
Image image;
Expand All @@ -58,4 +69,4 @@ std::optional<UploadReadyImage> loadAlbumArt(const metadb_handle_ptr& track,
abort_callback& abort);
UploadReadyImage loadSpecialArt(WORD resource, pfc::string8 userImage);

GLTexture loadSpinner();
GLImage loadSpinner();
2 changes: 1 addition & 1 deletion Renderer.cpp
Expand Up @@ -332,7 +332,7 @@ void Renderer::drawCovers(bool showTarget) {
return;

struct Cover {
const GLTexture* tex;
const GLImage* tex;
float offset;
int index;
bool isTarget;
Expand Down
2 changes: 1 addition & 1 deletion Renderer.h
Expand Up @@ -33,7 +33,7 @@ class Renderer {
int winHeight = 1;

private:
GLTexture spinnerTexture;
GLImage spinnerTexture;
void getFrustrumSize(double& right, double& top, double& zNear, double& zFar);

bool vSyncEnabled;
Expand Down
6 changes: 3 additions & 3 deletions TextureCache.cpp
Expand Up @@ -18,7 +18,7 @@ void TextureCache::reloadSpecialTextures() {
noCoverTexture = loadSpecialArt(IDR_COVER_NO_IMG, cfgImgNoCover.c_str()).upload();
}

const GLTexture* TextureCache::getAlbumTexture(const std::string& albumName) {
const GLImage* TextureCache::getAlbumTexture(const std::string& albumName) {
auto entry = textureCache.find(albumName);
if (entry == textureCache.end())
return nullptr;
Expand All @@ -29,7 +29,7 @@ const GLTexture* TextureCache::getAlbumTexture(const std::string& albumName) {
}
}

GLTexture& TextureCache::getLoadingTexture() {
GLImage& TextureCache::getLoadingTexture() {
return loadingTexture;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ void TextureCache::uploadTextures() {
if (existing != textureCache.end()) {
textureCache.erase(existing);
}
std::optional<GLTexture> texture{};
std::optional<GLImage> texture{};
if (loaded->image)
texture = loaded->image->upload();
textureCache.emplace(loaded->meta, std::move(texture));
Expand Down
12 changes: 6 additions & 6 deletions TextureCache.h
Expand Up @@ -68,8 +68,8 @@ class TextureCache {
public:
TextureCache(EngineThread&, DbAlbumCollection&, ScriptedCoverPositions&);

const GLTexture* getAlbumTexture(const std::string& albumName);
GLTexture& getLoadingTexture();
const GLImage* getAlbumTexture(const std::string& albumName);
GLImage& getLoadingTexture();

void trimCache();
void clearCache();
Expand All @@ -87,15 +87,15 @@ class TextureCache {
unsigned int collectionVersion = 0;

void reloadSpecialTextures();
GLTexture noCoverTexture;
GLTexture loadingTexture;
GLImage noCoverTexture;
GLImage loadingTexture;

unsigned int cacheGeneration = 0;

struct CacheItem : TextureCacheMeta {
CacheItem(const TextureCacheMeta& meta, std::optional<GLTexture>&& texture)
CacheItem(const TextureCacheMeta& meta, std::optional<GLImage>&& texture)
: TextureCacheMeta(meta), texture(std::move(texture)){};
std::optional<GLTexture> texture;
std::optional<GLImage> texture;
};
using t_textureCache = bomi::multi_index_container<
CacheItem,
Expand Down

0 comments on commit a4731a8

Please sign in to comment.