# HG changeset patch # User Sebastien Jodogne # Date 1683213494 -7200 # Node ID cf3d85eb291c2365a42cf01140548933fa93897c # Parent 15f2e52835a11e30ac70daaef698771be7edccd7 added class OpenGLTextureVolume diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Resources/CMake/OrthancStoneConfiguration.cmake --- a/OrthancStone/Resources/CMake/OrthancStoneConfiguration.cmake Thu May 04 16:59:28 2023 +0200 +++ b/OrthancStone/Resources/CMake/OrthancStoneConfiguration.cmake Thu May 04 17:18:14 2023 +0200 @@ -503,6 +503,8 @@ ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLTexture.cpp ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLTextureArray.h ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLTextureArray.cpp + ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLTextureVolume.h + ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLTextureVolume.cpp ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLFramebuffer.h ${ORTHANC_STONE_ROOT}/OpenGL/OpenGLFramebuffer.cpp ${ORTHANC_STONE_ROOT}/OpenGL/ImageProcessingProgram.h diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Sources/OpenGL/ImageProcessingProgram.cpp --- a/OrthancStone/Sources/OpenGL/ImageProcessingProgram.cpp Thu May 04 16:59:28 2023 +0200 +++ b/OrthancStone/Sources/OpenGL/ImageProcessingProgram.cpp Thu May 04 17:18:14 2023 +0200 @@ -179,6 +179,17 @@ } + void ImageProcessingProgram::Use(OpenGLTextureVolume& target, + unsigned int z, + OpenGLFramebuffer& framebuffer, + bool checkStatus) + { + program_.Use(checkStatus); + framebuffer.SetTarget(target, z); + SetupPosition(); + } + + void ImageProcessingProgram::Render() { glClearColor(0.0, 0.0, 0.0, 1.0); diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Sources/OpenGL/OpenGLFramebuffer.cpp --- a/OrthancStone/Sources/OpenGL/OpenGLFramebuffer.cpp Thu May 04 16:59:28 2023 +0200 +++ b/OrthancStone/Sources/OpenGL/OpenGLFramebuffer.cpp Thu May 04 17:18:14 2023 +0200 @@ -31,6 +31,7 @@ #include "OpenGLTexture.h" #include "OpenGLTextureArray.h" +#include "OpenGLTextureVolume.h" #include @@ -216,6 +217,25 @@ } + void OpenGLFramebuffer::SetTarget(OpenGLTextureVolume& target, + unsigned int z) + { + if (z >= target.GetDepth()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + else + { + // Warning: "glFramebufferTexture3D()" is not available in WebGL 2 + glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target.GetId(), 0, z); + ORTHANC_OPENGL_CHECK("glFramebufferTextureLayer()"); + + SetupTextureTarget(); + glViewport(0, 0, target.GetWidth(), target.GetHeight()); + } + } + + void OpenGLFramebuffer::ReadTexture(Orthanc::ImageAccessor& target, const OpenGLTexture& source) { @@ -261,5 +281,33 @@ ReadContent(target); } } + + + void OpenGLFramebuffer::ReadTexture(Orthanc::ImageAccessor& target, + const OpenGLTextureVolume& source, + unsigned int z) + { + if (target.GetWidth() != source.GetWidth() || + target.GetHeight() != source.GetHeight()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); + } + else if (target.GetFormat() != source.GetFormat()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); + } + else if (z >= source.GetDepth()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + else + { + // Warning: "glFramebufferTexture3D()" is not available in WebGL 2 + glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, source.GetId(), 0, z); + ORTHANC_OPENGL_CHECK("glFramebufferTextureLayer()"); + + ReadContent(target); + } + } } } diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp --- a/OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp Thu May 04 16:59:28 2023 +0200 +++ b/OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp Thu May 04 17:18:14 2023 +0200 @@ -187,14 +187,16 @@ { throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); } - - if (layer >= depth_) + else if (layer >= depth_) { throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); } - - if (width_ != 0 && - height_ != 0) + else if (image.GetPitch() != Orthanc::GetBytesPerPixel(format_) * width_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Minimal pitch is required for upload"); + } + else if (width_ != 0 && + height_ != 0) { GLenum sourceFormat, internalFormat, pixelType; OpenGLTexture::ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, image.GetFormat()); diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Sources/OpenGL/OpenGLTextureVolume.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancStone/Sources/OpenGL/OpenGLTextureVolume.cpp Thu May 04 17:18:14 2023 +0200 @@ -0,0 +1,304 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2022 Osimis S.A., Belgium + * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "OpenGLTextureVolume.h" + +#include "OpenGLFramebuffer.h" +#include "OpenGLTexture.h" + +#include +#include + +#include +#include + + +namespace OrthancStone +{ + namespace OpenGL + { + OpenGLTextureVolume::OpenGLTextureVolume(IOpenGLContext& context) : + context_(context), + texture_(0), + width_(0), + height_(0), + depth_(0), + format_(Orthanc::PixelFormat_Float32), + isLinearInterpolation_(false) + { + if (context.IsContextLost()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "OpenGL context has been lost"); + } + + glGenTextures(1, &texture_); + ORTHANC_OPENGL_CHECK("glGenTextures()"); + + if (texture_ == 0) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "Cannot create an OpenGL texture array"); + } + } + + + OpenGLTextureVolume::~OpenGLTextureVolume() + { + assert(texture_ != 0); + glDeleteTextures(1, &texture_); + } + + + void OpenGLTextureVolume::Setup(Orthanc::PixelFormat format, + unsigned int width, + unsigned int height, + unsigned int depth, + bool isLinearInterpolation) + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_3D, texture_); + ORTHANC_OPENGL_CHECK("glBindTexture(GL_TEXTURE_3D)"); + + GLenum sourceFormat, internalFormat, pixelType; + OpenGLTexture::ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, format); + + glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, + 0, sourceFormat, pixelType, NULL); + ORTHANC_OPENGL_CHECK("glTexImage3D()"); + +#if !defined(__EMSCRIPTEN__) + /** + * glGetTexLevelParameteriv() was introduced in OpenGL ES 3.1, + * but WebGL 2 only supports OpenGL ES 3.0, so it is not + * available in WebAssembly: + * https://registry.khronos.org/OpenGL-Refpages/es3.1/html/glGetTexLevelParameter.xhtml + **/ + GLint w, h, d; + glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &w); + glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, &h); + glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_DEPTH, &d); + if (width != static_cast(w) || + height != static_cast(h) || + depth != static_cast(d)) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "Your GPU cannot create a 3D texture of size " + + boost::lexical_cast(width) + " x " + + boost::lexical_cast(height) + " x " + + boost::lexical_cast(depth)); + } +#endif + + format_ = format; + width_ = width; + height_ = height; + depth_ = depth; + isLinearInterpolation_ = isLinearInterpolation; + + GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, interpolation); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, interpolation); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + } + + + void OpenGLTextureVolume::SetClampingToZero() + { +#if defined(__EMSCRIPTEN__) + /** + * This is because WebGL 2 derives from OpenGL ES 3.0, which + * doesn't support GL_CLAMP_TO_BORDER, as can be seen here: + * https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexParameter.xhtml + **/ + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "OpenGLTextureArray::SetClampingToZero() is not available in WebGL 2"); +#else + ORTHANC_OPENGL_CHECK("Entering OpenGLTextureArray::SetClampingToZero()"); + + glBindTexture(GL_TEXTURE_3D, texture_); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + + GLfloat colorfv[4] = { 0, 0, 0, 0 }; + glTexParameterfv(texture_, GL_TEXTURE_BORDER_COLOR, colorfv); + + ORTHANC_OPENGL_CHECK("Exiting OpenGLTextureArray::SetClampingToZero()"); +#endif + } + + + void OpenGLTextureVolume::Bind(GLint location) const + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_3D, texture_); + glUniform1i(location, 0 /* texture unit */); + } + + + void OpenGLTextureVolume::BindAsTextureUnit(GLint location, + unsigned int unit) const + { + if (unit >= 32) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + + assert(GL_TEXTURE0 + 1 == GL_TEXTURE1 && + GL_TEXTURE0 + 31 == GL_TEXTURE31); + + glActiveTexture(GL_TEXTURE0 + unit); + glBindTexture(GL_TEXTURE_3D, texture_); + glUniform1i(location, unit /* texture unit */); + } + + + void OpenGLTextureVolume::Upload(const Orthanc::ImageAccessor& image, + unsigned int z) + { + if (image.GetWidth() != width_ || + image.GetHeight() != height_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); + } + else if (z >= depth_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + else if (image.GetPitch() != Orthanc::GetBytesPerPixel(format_) * width_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Minimal pitch is required for upload"); + } + else if (width_ != 0 && + height_ != 0) + { + GLenum sourceFormat, internalFormat, pixelType; + OpenGLTexture::ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, image.GetFormat()); + + glBindTexture(GL_TEXTURE_3D, texture_); + +#if defined(__EMSCRIPTEN__) && (ORTHANC_WEBGL2_HEAP_COMPAT == 1) + // Check out "OpenGLTexture.cpp" for an explanation + EM_ASM({ + var ptr = emscriptenWebGLGetTexPixelData($5, $4, $2, $3, $0, $1); + GLctx.texSubImage3D(GLctx.TEXTURE_3D, 0, 0 /* x offset */, 0 /* y offset */, + $6, $2, $3, 1 /* depth */, $4, $5, ptr); + }, + image.GetConstBuffer(), // $0 + internalFormat, // $1 + image.GetWidth(), // $2 + image.GetHeight(), // $3 + sourceFormat, // $4 + pixelType, // $5 + z); // $6 +#else + glTexSubImage3D(GL_TEXTURE_3D, 0, 0 /* x offset */, 0 /* y offset */, z /* z offset */, + width_, height_, 1 /* depth */, sourceFormat, pixelType, image.GetConstBuffer()); +#endif + } + } + + + size_t OpenGLTextureVolume::GetMemoryBufferSize() const + { + return static_cast(Orthanc::GetBytesPerPixel(format_)) * width_ * height_ * depth_; + } + + + void OpenGLTextureVolume::Download(void* targetBuffer, + size_t targetSize) const + { + if (targetSize != GetMemoryBufferSize()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + else if (targetSize == 0) + { + return; + } + else if (targetBuffer == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + else + { +#if 1 || defined(__EMSCRIPTEN__) + /** + * The "glGetTexImage()" function is unavailable in WebGL, it + * is necessary to use a framebuffer: + * https://stackoverflow.com/a/15064957 + **/ + OpenGLFramebuffer framebuffer(context_); + + const size_t sliceSize = targetSize / depth_; + + Orthanc::Image tmp(GetFormat(), GetWidth(), GetHeight(), true); + if (sliceSize != tmp.GetPitch() * height_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + for (unsigned int z = 0; z < depth_; z++) + { + framebuffer.ReadTexture(tmp, *this, z); + memcpy(reinterpret_cast(targetBuffer) + z * sliceSize, tmp.GetBuffer(), sliceSize); + } + +#else + glBindTexture(GL_TEXTURE_3D, texture_); + + switch (format_) + { + case Orthanc::PixelFormat_Grayscale8: + glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, targetBuffer); + break; + + case Orthanc::PixelFormat_RGB24: + glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, targetBuffer); + break; + + case Orthanc::PixelFormat_RGBA32: + glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, targetBuffer); + break; + + case Orthanc::PixelFormat_Float32: + glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_FLOAT, targetBuffer); + break; + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + } +#endif + } + } + + + void OpenGLTextureVolume::Download(std::string& target) const + { + target.resize(GetMemoryBufferSize()); + Download(target); + } + } +} diff -r 15f2e52835a1 -r cf3d85eb291c OrthancStone/Sources/OpenGL/OpenGLTextureVolume.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancStone/Sources/OpenGL/OpenGLTextureVolume.h Thu May 04 17:18:14 2023 +0200 @@ -0,0 +1,118 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2022 Osimis S.A., Belgium + * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "OpenGLIncludes.h" +#include "IOpenGLContext.h" + +#include + + +namespace OrthancStone +{ + namespace OpenGL + { + class OpenGLTextureVolume : public boost::noncopyable + { + friend class OpenGLFramebuffer; + + private: + OpenGL::IOpenGLContext& context_; + GLuint texture_; + unsigned int width_; + unsigned int height_; + unsigned int depth_; + Orthanc::PixelFormat format_; + bool isLinearInterpolation_; + + /** + * Returns the low-level OpenGL handle of the texture + * array. Beware to never change the size of the texture using + * this handle! + **/ + GLuint GetId() const + { + return texture_; + } + + public: + OpenGLTextureVolume(IOpenGLContext& context); + + ~OpenGLTextureVolume(); + + unsigned int GetWidth() const + { + return width_; + } + + unsigned int GetHeight() const + { + return height_; + } + + unsigned int GetDepth() const + { + return depth_; + } + + Orthanc::PixelFormat GetFormat() const + { + return format_; + } + + bool IsLinearInterpolation() const + { + return isLinearInterpolation_; + } + + void Setup(Orthanc::PixelFormat format, + unsigned int width, + unsigned int height, + unsigned int depth, + bool isLinearInterpolation); + + /** + * By default, textures are mirrored at the borders. This + * function will set out-of-image access to zero. + **/ + void SetClampingToZero(); + + void Bind(GLint location) const; + + void BindAsTextureUnit(GLint location, + unsigned int unit) const; + + void Upload(const Orthanc::ImageAccessor& image, + unsigned int z); + + size_t GetMemoryBufferSize() const; + + // "targetSize" must be equal to "GetMemoryBufferSize()" + void Download(void* targetBuffer, + size_t targetSize) const; + + void Download(std::string& target) const; + }; + } +}