From aa74306ba26e2207012bae88ca4dbb768902f6ee Mon Sep 17 00:00:00 2001 From: Martin Piatka Date: Tue, 30 Jan 2024 15:33:29 +0100 Subject: [PATCH] gl_utils: Handle frames that are in PBO already --- src/video_display/opengl_conversions.cpp | 43 +++++++++++------------- src/video_display/opengl_utils.cpp | 31 +++++++++++++++++ src/video_display/opengl_utils.hpp | 9 ++++- 3 files changed, 58 insertions(+), 25 deletions(-) diff --git a/src/video_display/opengl_conversions.cpp b/src/video_display/opengl_conversions.cpp index 40beb713d..d90fcb8b3 100644 --- a/src/video_display/opengl_conversions.cpp +++ b/src/video_display/opengl_conversions.cpp @@ -134,7 +134,7 @@ public: virtual ~Rendering_convertor() { }; - void attach_texture(const Texture& tex) override final { + void attach_texture(Texture& tex) override final { fbuf.attach_texture(tex); } @@ -168,12 +168,12 @@ protected: class Loading_convertor : public Frame_convertor{ public: - void attach_texture(const Texture& tex) override final{ + void attach_texture(Texture& tex) override final{ this->tex = &tex; } protected: - const Texture *tex = nullptr; + Texture *tex = nullptr; }; class Yuv_convertor : public Rendering_convertor{ @@ -184,9 +184,12 @@ public: private: void prepare_input_tex(video_frame *f, bool pbo_frame) override final{ - input_tex.allocate((f->tiles[0].width + 1) / 2, f->tiles[0].height, GL_RGBA); - glBindTexture(GL_TEXTURE_2D, input_tex.get()); - input_tex.upload_frame(f, pbo_frame); + int w = (f->tiles[0].width + 1) / 2; + int h = f->tiles[0].height; + + input_tex.load_frame(w, h, GL_RGBA, + GL_RGBA, GL_UNSIGNED_BYTE, + f, pbo_frame); } }; @@ -310,16 +313,11 @@ public: private: void prepare_input_tex(video_frame *f, bool pbo_frame = false) override final{ - //TODO int w = vc_get_linesize(f->tiles[0].width, v210) / 4; int h = f->tiles[0].height; - input_tex.allocate(w, h, GL_RGB10_A2); - glBindTexture(GL_TEXTURE_2D, input_tex.get()); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, - w, h, 0, + input_tex.load_frame(w, h, GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, - f->tiles[0].data); + f, pbo_frame); } }; @@ -358,16 +356,11 @@ public: private: void prepare_input_tex(video_frame *f, bool pbo_frame = false) override final{ - //TODO int w = f->tiles[0].width; int h = f->tiles[0].height; - input_tex.allocate(w, h, GL_RGBA); - glBindTexture(GL_TEXTURE_2D, input_tex.get()); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - w, h, 0, + input_tex.load_frame(w, h, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT, - f->tiles[0].data); + f, pbo_frame); } }; @@ -513,8 +506,9 @@ public: void put_frame(video_frame *f, bool pbo_frame = false) override{ int w = f->tiles[0].width; int h = f->tiles[0].height; - glBindTexture(GL_TEXTURE_2D, tex->get()); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, f->tiles[0].data); + tex->load_frame(w, h, GL_RGB, + GL_RGB, GL_UNSIGNED_BYTE, + f, pbo_frame); } }; @@ -523,8 +517,9 @@ public: void put_frame(video_frame *f, bool pbo_frame = false) override{ int w = f->tiles[0].width; int h = f->tiles[0].height; - glBindTexture(GL_TEXTURE_2D, tex->get()); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, f->tiles[0].data); + tex->load_frame(w, h, GL_RGBA, + GL_RGBA, GL_UNSIGNED_BYTE, + f, pbo_frame); } }; diff --git a/src/video_display/opengl_utils.cpp b/src/video_display/opengl_utils.cpp index 3c8865063..9f516655e 100644 --- a/src/video_display/opengl_utils.cpp +++ b/src/video_display/opengl_utils.cpp @@ -366,6 +366,36 @@ void Texture::upload_frame(video_frame *f, bool pbo_frame){ } } +void Texture::load_frame(int w, int h, + GLint internal_format, + GLenum src_format, + GLenum type, + video_frame *f, + bool pbo_frame) +{ + char *src_data = f->tiles[0].data; + allocate(w, h, internal_format); + glBindTexture(GL_TEXTURE_2D, tex_id); + + if(pbo_frame){ + GlBuffer *pbo = static_cast(f->callbacks.dispose_udata); + + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo->get()); + glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); + f->tiles[0].data = nullptr; + src_data = nullptr; + } + + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, + w, h, 0, + src_format, type, + src_data); + + if(pbo_frame){ + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + } +} + void Texture::allocate(){ if(tex_id != 0) return; @@ -464,6 +494,7 @@ void main(){ quad = Model::get_quad(); texture.allocate(); uploader.attach_dst_texture(&texture); + uploader.enable_pbo(false); } void FlatVideoScene::put_frame(video_frame *f){ diff --git a/src/video_display/opengl_utils.hpp b/src/video_display/opengl_utils.hpp index 5391b97d4..6b1057c0f 100644 --- a/src/video_display/opengl_utils.hpp +++ b/src/video_display/opengl_utils.hpp @@ -179,6 +179,13 @@ public: */ void upload_frame(video_frame *f, bool pbo_frame); + void load_frame(int w, int h, + GLint internal_format, + GLenum src_format, + GLenum type, + video_frame *f, + bool pbo_frame); + Texture(const Texture&) = delete; Texture(Texture&& o) { swap(o); } Texture& operator=(const Texture&) = delete; @@ -346,7 +353,7 @@ public: * * @param tex texture to attach */ - virtual void attach_texture(const Texture& tex) = 0; + virtual void attach_texture(Texture& tex) = 0; void set_pbo(GlBuffer *pbo) { internal_pbo = pbo; } protected: