annotate OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp @ 2068:22a83fb9dd23 deep-learning

added AlignedMatrix and TimerLogger
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 May 2023 17:30:52 +0200
parents cf3d85eb291c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Lesser General Public License for more details.
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * License along with this program. If not, see
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * <http://www.gnu.org/licenses/>.
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "OpenGLTextureArray.h"
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #if defined(__EMSCRIPTEN__)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 # if !defined(ORTHANC_WEBGL2_HEAP_COMPAT)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 # error The macro ORTHANC_WEBGL2_HEAP_COMPAT must be defined
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 # endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include "OpenGLFramebuffer.h"
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "OpenGLTexture.h"
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <Images/Image.h>
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <OrthancException.h>
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <boost/lexical_cast.hpp>
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <cassert>
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace OrthancStone
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace OpenGL
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 OpenGLTextureArray::OpenGLTextureArray(IOpenGLContext& context) :
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 context_(context),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 texture_(0),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 width_(0),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 height_(0),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 depth_(0),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 format_(Orthanc::PixelFormat_Float32),
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 isLinearInterpolation_(false)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if (context.IsContextLost())
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 "OpenGL context has been lost");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 glGenTextures(1, &texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 ORTHANC_OPENGL_CHECK("glGenTextures()");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 if (texture_ == 0)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 "Cannot create an OpenGL texture array");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 OpenGLTextureArray::~OpenGLTextureArray()
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 assert(texture_ != 0);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 glDeleteTextures(1, &texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 void OpenGLTextureArray::Setup(Orthanc::PixelFormat format,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 unsigned int width,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 unsigned int height,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 unsigned int depth,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 bool isLinearInterpolation)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 glActiveTexture(GL_TEXTURE0);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 glBindTexture(GL_TEXTURE_2D_ARRAY, texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 ORTHANC_OPENGL_CHECK("glBindTexture(GL_TEXTURE_2D_ARRAY)");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 GLenum sourceFormat, internalFormat, pixelType;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 OpenGLTexture::ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, format);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, internalFormat, width, height, depth,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 0, sourceFormat, pixelType, NULL);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 ORTHANC_OPENGL_CHECK("glTexImage3D()");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 #if !defined(__EMSCRIPTEN__)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 /**
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 * glGetTexLevelParameteriv() was introduced in OpenGL ES 3.1,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 * but WebGL 2 only supports OpenGL ES 3.0, so it is not
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 * available in WebAssembly:
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 * https://registry.khronos.org/OpenGL-Refpages/es3.1/html/glGetTexLevelParameter.xhtml
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 **/
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 GLint w, h, d;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 glGetTexLevelParameteriv(GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_WIDTH, &w);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 glGetTexLevelParameteriv(GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_HEIGHT, &h);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 glGetTexLevelParameteriv(GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_DEPTH, &d);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 if (width != static_cast<unsigned int>(w) ||
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 height != static_cast<unsigned int>(h) ||
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 depth != static_cast<unsigned int>(d))
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 "Your GPU cannot create an array of textures of size " +
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 boost::lexical_cast<std::string>(width) + " x " +
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 boost::lexical_cast<std::string>(height) + " x " +
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 boost::lexical_cast<std::string>(depth));
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 #endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 format_ = format;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 width_ = width;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 height_ = height;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 depth_ = depth;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 isLinearInterpolation_ = isLinearInterpolation;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 GLint interpolation = (isLinearInterpolation ? GL_LINEAR : GL_NEAREST);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, interpolation);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, interpolation);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 void OpenGLTextureArray::SetClampingToZero()
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 #if defined(__EMSCRIPTEN__)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 /**
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 * This is because WebGL 2 derives from OpenGL ES 3.0, which
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 * doesn't support GL_CLAMP_TO_BORDER, as can be seen here:
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 * https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexParameter.xhtml
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 **/
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 "OpenGLTextureArray::SetClampingToZero() is not available in WebGL 2");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 #else
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 ORTHANC_OPENGL_CHECK("Entering OpenGLTextureArray::SetClampingToZero()");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 glBindTexture(GL_TEXTURE_2D_ARRAY, texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 GLfloat colorfv[4] = { 0, 0, 0, 0 };
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 glTexParameterfv(texture_, GL_TEXTURE_BORDER_COLOR, colorfv);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 ORTHANC_OPENGL_CHECK("Exiting OpenGLTextureArray::SetClampingToZero()");
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 #endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 void OpenGLTextureArray::Bind(GLint location) const
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 glActiveTexture(GL_TEXTURE0);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 glBindTexture(GL_TEXTURE_2D_ARRAY, texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 glUniform1i(location, 0 /* texture unit */);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 void OpenGLTextureArray::BindAsTextureUnit(GLint location,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 unsigned int unit) const
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 if (unit >= 32)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 assert(GL_TEXTURE0 + 1 == GL_TEXTURE1 &&
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 GL_TEXTURE0 + 31 == GL_TEXTURE31);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 glActiveTexture(GL_TEXTURE0 + unit);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 glBindTexture(GL_TEXTURE_2D_ARRAY, texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 glUniform1i(location, unit /* texture unit */);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 void OpenGLTextureArray::Upload(const Orthanc::ImageAccessor& image,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 unsigned int layer)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 if (image.GetWidth() != width_ ||
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 image.GetHeight() != height_)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }
2066
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
190 else if (layer >= depth_)
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
2066
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
194 else if (image.GetPitch() != Orthanc::GetBytesPerPixel(format_) * width_)
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
195 {
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
196 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Minimal pitch is required for upload");
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
197 }
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
198 else if (width_ != 0 &&
cf3d85eb291c added class OpenGLTextureVolume
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2065
diff changeset
199 height_ != 0)
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 GLenum sourceFormat, internalFormat, pixelType;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 OpenGLTexture::ConvertToOpenGLFormats(sourceFormat, internalFormat, pixelType, image.GetFormat());
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 glBindTexture(GL_TEXTURE_2D_ARRAY, texture_);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 #if defined(__EMSCRIPTEN__) && (ORTHANC_WEBGL2_HEAP_COMPAT == 1)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 // Check out "OpenGLTexture.cpp" for an explanation
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 EM_ASM({
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 var ptr = emscriptenWebGLGetTexPixelData($5, $4, $2, $3, $0, $1);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 GLctx.texSubImage3D(GLctx.TEXTURE_2D_ARRAY, 0, 0 /* x offset */, 0 /* y offset */,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 $6, $2, $3, 1 /* depth */, $4, $5, ptr);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 },
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 image.GetConstBuffer(), // $0
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 internalFormat, // $1
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 image.GetWidth(), // $2
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 image.GetHeight(), // $3
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 sourceFormat, // $4
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 pixelType, // $5
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 layer); // $6
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 #else
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0 /* x offset */, 0 /* y offset */, layer /* z offset */,
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 width_, height_, 1 /* depth */, sourceFormat, pixelType, image.GetConstBuffer());
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 #endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
228 size_t OpenGLTextureArray::GetMemoryBufferSize() const
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
229 {
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
230 return static_cast<size_t>(Orthanc::GetBytesPerPixel(format_)) * width_ * height_ * depth_;
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
231 }
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
232
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
233
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
234 void OpenGLTextureArray::Download(void* targetBuffer,
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
235 size_t targetSize) const
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
237 if (targetSize != GetMemoryBufferSize())
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
238 {
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
239 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
240 }
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
241 else if (targetSize == 0)
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 {
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
243 return;
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
244 }
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
245 else if (targetBuffer == NULL)
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
246 {
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
247 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
248 }
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
249 else
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
250 {
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 #if 1 || defined(__EMSCRIPTEN__)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 /**
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 * The "glGetTexImage()" function is unavailable in WebGL, it
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 * is necessary to use a framebuffer:
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 * https://stackoverflow.com/a/15064957
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 **/
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
257 OpenGLFramebuffer framebuffer(context_);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
258
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
259 const size_t sliceSize = targetSize / depth_;
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
261 Orthanc::Image tmp(GetFormat(), GetWidth(), GetHeight(), true);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
262 if (sliceSize != tmp.GetPitch() * height_)
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
263 {
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
265 }
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 for (unsigned int layer = 0; layer < depth_; layer++)
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
269 framebuffer.ReadTexture(tmp, *this, layer);
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
270 memcpy(reinterpret_cast<uint8_t*>(targetBuffer) + layer * sliceSize, tmp.GetBuffer(), sliceSize);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 #else
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
274 glBindTexture(GL_TEXTURE_3D, texture_);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
2062
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2060
diff changeset
276 switch (format_)
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 {
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 case Orthanc::PixelFormat_Grayscale8:
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
279 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, targetBuffer);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 break;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 case Orthanc::PixelFormat_RGB24:
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
283 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, targetBuffer);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 break;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 case Orthanc::PixelFormat_RGBA32:
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
287 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, targetBuffer);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 break;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 case Orthanc::PixelFormat_Float32:
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
291 glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_FLOAT, targetBuffer);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 break;
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 default:
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 #endif
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
302 void OpenGLTextureArray::Download(std::string& target) const
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 {
2065
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
304 target.resize(GetMemoryBufferSize());
15f2e52835a1 removed OpenGLTextureArray::DownloadedArray()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2063
diff changeset
305 Download(target);
2060
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
86e0e92a2e0d added OpenGLTextureArray and OpenGLFramebuffer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 }