From 367fd73c8137b3ffa0b030d2915a53d61d49316b Mon Sep 17 00:00:00 2001 From: Martin Piatka Date: Fri, 12 Jan 2024 16:00:01 +0100 Subject: [PATCH] gl_utils: Make it harder to use uinitialized texture --- src/video_display/opengl_utils.cpp | 7 ++++++- src/video_display/opengl_utils.hpp | 13 ++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/video_display/opengl_utils.cpp b/src/video_display/opengl_utils.cpp index bb747cf21..2a5bd01b2 100644 --- a/src/video_display/opengl_utils.cpp +++ b/src/video_display/opengl_utils.cpp @@ -367,6 +367,11 @@ void Texture::init(){ glGenBuffers(1, &pbo); } +void Texture::bind() { + init(); + glBindTexture(GL_TEXTURE_2D, tex_id); +} + void Framebuffer::attach_texture(GLuint tex){ glBindTexture(GL_TEXTURE_2D, tex); @@ -383,7 +388,7 @@ void Framebuffer::attach_texture(GLuint tex){ void FrameUploader::put_frame(video_frame *f, bool pbo_frame){ assert(tex); - glBindTexture(GL_TEXTURE_2D, tex->get()); + tex->bind(); tex->allocate(f->tiles[0].width, f->tiles[0].height, GL_RGB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); diff --git a/src/video_display/opengl_utils.hpp b/src/video_display/opengl_utils.hpp index d54f7ee05..10692566b 100644 --- a/src/video_display/opengl_utils.hpp +++ b/src/video_display/opengl_utils.hpp @@ -57,6 +57,7 @@ //#include #include #include +#include #include "types.h" @@ -149,9 +150,9 @@ public: ~Texture(); /** - * Returns the underlying OpenGL texture id + * Returns the underlying OpenGL texture id. */ - GLuint get() const { return tex_id; } + GLuint get() const { assert(tex_id); return tex_id; } /** * Allocates the the storage for the texture according to requested @@ -163,6 +164,13 @@ public: */ void allocate(int w, int h, GLint internal_fmt); + /** + * Creates the actual OpenGL texture objects if not yet created + */ + void init(); + + void bind(); + /** * Uploads video frame to the texture * @@ -189,7 +197,6 @@ public: std::swap(pbo, o.pbo); } private: - void init(); /** * Uploads image data to the texture