From d1d3ae71b2f97268d6be2a56bfa3916c449d76ba Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nicolas.pope@utu.fi> Date: Thu, 27 Feb 2020 16:27:12 +0200 Subject: [PATCH] Fix gl texture memory alignment --- applications/gui/src/gltexture.cpp | 14 ++++++++++++-- applications/gui/src/gltexture.hpp | 1 + applications/gui/src/screen.cpp | 14 ++++++++------ components/renderers/cpp/src/overlay.cpp | 1 - 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/applications/gui/src/gltexture.cpp b/applications/gui/src/gltexture.cpp index 5084c947c..8b54e22ef 100644 --- a/applications/gui/src/gltexture.cpp +++ b/applications/gui/src/gltexture.cpp @@ -54,8 +54,11 @@ void GLTexture::make(int width, int height) { free(); } + static constexpr int ALIGNMENT = 128; + width_ = width; height_ = height; + stride_ = ((width*4) % ALIGNMENT != 0) ? ((width*4) + (ALIGNMENT - ((width*4) % ALIGNMENT))) / 4 : width; if (width == 0 || height == 0) { throw FTL_Error("Invalid texture size"); @@ -64,6 +67,9 @@ void GLTexture::make(int width, int height) { if (glid_ == std::numeric_limits<unsigned int>::max()) { glGenTextures(1, &glid_); glBindTexture(GL_TEXTURE_2D, glid_); + + glPixelStorei(GL_UNPACK_ROW_LENGTH, stride_); + //cv::Mat m(cv::Size(100,100), CV_8UC3); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, nullptr); if (type_ == Type::BGRA) { @@ -78,13 +84,15 @@ void GLTexture::make(int width, int height) { auto err = glGetError(); if (err != 0) LOG(ERROR) << "OpenGL Texture error: " << err; + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glBindTexture(GL_TEXTURE_2D, 0); glGenBuffers(1, &glbuf_); // Make this the current UNPACK buffer (OpenGL is state-based) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glbuf_); // Allocate data for the buffer. 4-channel 8-bit image - glBufferData(GL_PIXEL_UNPACK_BUFFER, width * height * 4, NULL, GL_DYNAMIC_COPY); + glBufferData(GL_PIXEL_UNPACK_BUFFER, stride_ * height * 4, NULL, GL_DYNAMIC_COPY); cudaSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_res_, glbuf_, cudaGraphicsRegisterFlagsWriteDiscard)); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); @@ -110,7 +118,7 @@ cv::cuda::GpuMat GLTexture::map(cudaStream_t stream) { size_t size; cudaSafeCall(cudaGraphicsMapResources(1, &cuda_res_, stream)); cudaSafeCall(cudaGraphicsResourceGetMappedPointer(&devptr, &size, cuda_res_)); - return cv::cuda::GpuMat(height_, width_, (type_ == Type::BGRA) ? CV_8UC4 : CV_32F, devptr); + return cv::cuda::GpuMat(height_, width_, (type_ == Type::BGRA) ? CV_8UC4 : CV_32F, devptr, stride_*4); } void GLTexture::unmap(cudaStream_t stream) { @@ -121,12 +129,14 @@ void GLTexture::unmap(cudaStream_t stream) { glBindBuffer( GL_PIXEL_UNPACK_BUFFER, glbuf_); // Select the appropriate texture glBindTexture( GL_TEXTURE_2D, glid_); + glPixelStorei(GL_UNPACK_ROW_LENGTH, stride_); // Make a texture from the buffer if (type_ == Type::BGRA) { glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_BGRA, GL_UNSIGNED_BYTE, NULL); } else { glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_RED, GL_FLOAT, NULL); } + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0); } diff --git a/applications/gui/src/gltexture.hpp b/applications/gui/src/gltexture.hpp index c263a055a..759f349cf 100644 --- a/applications/gui/src/gltexture.hpp +++ b/applications/gui/src/gltexture.hpp @@ -39,6 +39,7 @@ class GLTexture { unsigned int glbuf_; int width_; int height_; + int stride_; bool changed_; Type type_; diff --git a/applications/gui/src/screen.cpp b/applications/gui/src/screen.cpp index 7073e6aec..17f3291ef 100644 --- a/applications/gui/src/screen.cpp +++ b/applications/gui/src/screen.cpp @@ -522,11 +522,6 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) { if (camera_) { imageSize = {camera_->width(), camera_->height()}; - glActiveTexture(GL_TEXTURE0); - mImageID = camera_->getLeft().texture(); - leftEye_ = mImageID; - rightEye_ = camera_->getRight().texture(); - //if (camera_->getChannel() != ftl::codecs::Channel::Left) { mImageID = rightEye_; } if (camera_->getLeft().isValid() && imageSize[0] > 0) { @@ -565,7 +560,9 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) { //glDisable(GL_SCISSOR_TEST); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - camera_->drawOverlay(screenSize); + camera_->drawOverlay(screenSize); + + glDisable(GL_DEPTH_TEST); } } else { // Must periodically render the cameras here to update any thumbnails. @@ -587,6 +584,11 @@ void ftl::gui::Screen::drawFast() { if (camera_) { camera_->captureFrame(); + glActiveTexture(GL_TEXTURE0); + mImageID = camera_->getLeft().texture(); + leftEye_ = mImageID; + rightEye_ = camera_->getRight().texture(); + #ifdef HAVE_OPENVR if (isVR() && camera_->width() > 0 && camera_->getLeft().isValid() && camera_->getRight().isValid()) { diff --git a/components/renderers/cpp/src/overlay.cpp b/components/renderers/cpp/src/overlay.cpp index 436163f54..368569a1d 100644 --- a/components/renderers/cpp/src/overlay.cpp +++ b/components/renderers/cpp/src/overlay.cpp @@ -267,7 +267,6 @@ void Overlay::draw(ftl::rgbd::FrameSet &fs, ftl::rgbd::FrameState &state, const glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); //cv::flip(out, out, 0); } -- GitLab