Skip to content
Snippets Groups Projects
Commit d1d3ae71 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Fix gl texture memory alignment

parent 5bd04054
No related branches found
No related tags found
No related merge requests found
...@@ -54,8 +54,11 @@ void GLTexture::make(int width, int height) { ...@@ -54,8 +54,11 @@ void GLTexture::make(int width, int height) {
free(); free();
} }
static constexpr int ALIGNMENT = 128;
width_ = width; width_ = width;
height_ = height; height_ = height;
stride_ = ((width*4) % ALIGNMENT != 0) ? ((width*4) + (ALIGNMENT - ((width*4) % ALIGNMENT))) / 4 : width;
if (width == 0 || height == 0) { if (width == 0 || height == 0) {
throw FTL_Error("Invalid texture size"); throw FTL_Error("Invalid texture size");
...@@ -64,6 +67,9 @@ void GLTexture::make(int width, int height) { ...@@ -64,6 +67,9 @@ void GLTexture::make(int width, int height) {
if (glid_ == std::numeric_limits<unsigned int>::max()) { if (glid_ == std::numeric_limits<unsigned int>::max()) {
glGenTextures(1, &glid_); glGenTextures(1, &glid_);
glBindTexture(GL_TEXTURE_2D, glid_); glBindTexture(GL_TEXTURE_2D, glid_);
glPixelStorei(GL_UNPACK_ROW_LENGTH, stride_);
//cv::Mat m(cv::Size(100,100), CV_8UC3); //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); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
if (type_ == Type::BGRA) { if (type_ == Type::BGRA) {
...@@ -78,13 +84,15 @@ void GLTexture::make(int width, int height) { ...@@ -78,13 +84,15 @@ void GLTexture::make(int width, int height) {
auto err = glGetError(); auto err = glGetError();
if (err != 0) LOG(ERROR) << "OpenGL Texture error: " << err; if (err != 0) LOG(ERROR) << "OpenGL Texture error: " << err;
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glGenBuffers(1, &glbuf_); glGenBuffers(1, &glbuf_);
// Make this the current UNPACK buffer (OpenGL is state-based) // Make this the current UNPACK buffer (OpenGL is state-based)
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glbuf_); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, glbuf_);
// Allocate data for the buffer. 4-channel 8-bit image // 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)); cudaSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_res_, glbuf_, cudaGraphicsRegisterFlagsWriteDiscard));
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
...@@ -110,7 +118,7 @@ cv::cuda::GpuMat GLTexture::map(cudaStream_t stream) { ...@@ -110,7 +118,7 @@ cv::cuda::GpuMat GLTexture::map(cudaStream_t stream) {
size_t size; size_t size;
cudaSafeCall(cudaGraphicsMapResources(1, &cuda_res_, stream)); cudaSafeCall(cudaGraphicsMapResources(1, &cuda_res_, stream));
cudaSafeCall(cudaGraphicsResourceGetMappedPointer(&devptr, &size, cuda_res_)); 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) { void GLTexture::unmap(cudaStream_t stream) {
...@@ -121,12 +129,14 @@ void GLTexture::unmap(cudaStream_t stream) { ...@@ -121,12 +129,14 @@ void GLTexture::unmap(cudaStream_t stream) {
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, glbuf_); glBindBuffer( GL_PIXEL_UNPACK_BUFFER, glbuf_);
// Select the appropriate texture // Select the appropriate texture
glBindTexture( GL_TEXTURE_2D, glid_); glBindTexture( GL_TEXTURE_2D, glid_);
glPixelStorei(GL_UNPACK_ROW_LENGTH, stride_);
// Make a texture from the buffer // Make a texture from the buffer
if (type_ == Type::BGRA) { if (type_ == Type::BGRA) {
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_BGRA, GL_UNSIGNED_BYTE, NULL); glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
} else { } else {
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_RED, GL_FLOAT, NULL); 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); glBindBuffer( GL_PIXEL_UNPACK_BUFFER, 0);
} }
......
...@@ -39,6 +39,7 @@ class GLTexture { ...@@ -39,6 +39,7 @@ class GLTexture {
unsigned int glbuf_; unsigned int glbuf_;
int width_; int width_;
int height_; int height_;
int stride_;
bool changed_; bool changed_;
Type type_; Type type_;
......
...@@ -522,11 +522,6 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) { ...@@ -522,11 +522,6 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) {
if (camera_) { if (camera_) {
imageSize = {camera_->width(), camera_->height()}; 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_->getChannel() != ftl::codecs::Channel::Left) { mImageID = rightEye_; }
if (camera_->getLeft().isValid() && imageSize[0] > 0) { if (camera_->getLeft().isValid() && imageSize[0] > 0) {
...@@ -565,7 +560,9 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) { ...@@ -565,7 +560,9 @@ void ftl::gui::Screen::draw(NVGcontext *ctx) {
//glDisable(GL_SCISSOR_TEST); //glDisable(GL_SCISSOR_TEST);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
camera_->drawOverlay(screenSize); camera_->drawOverlay(screenSize);
glDisable(GL_DEPTH_TEST);
} }
} else { } else {
// Must periodically render the cameras here to update any thumbnails. // Must periodically render the cameras here to update any thumbnails.
...@@ -587,6 +584,11 @@ void ftl::gui::Screen::drawFast() { ...@@ -587,6 +584,11 @@ void ftl::gui::Screen::drawFast() {
if (camera_) { if (camera_) {
camera_->captureFrame(); camera_->captureFrame();
glActiveTexture(GL_TEXTURE0);
mImageID = camera_->getLeft().texture();
leftEye_ = mImageID;
rightEye_ = camera_->getRight().texture();
#ifdef HAVE_OPENVR #ifdef HAVE_OPENVR
if (isVR() && camera_->width() > 0 && camera_->getLeft().isValid() && camera_->getRight().isValid()) { if (isVR() && camera_->width() > 0 && camera_->getLeft().isValid() && camera_->getRight().isValid()) {
......
...@@ -267,7 +267,6 @@ void Overlay::draw(ftl::rgbd::FrameSet &fs, ftl::rgbd::FrameState &state, const ...@@ -267,7 +267,6 @@ void Overlay::draw(ftl::rgbd::FrameSet &fs, ftl::rgbd::FrameState &state, const
glDisable(GL_LINE_SMOOTH); glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
//cv::flip(out, out, 0); //cv::flip(out, out, 0);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment