| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_TEXTURE2DARRAY |
|---|
| 15 | #define OSG_TEXTURE2DARRAY 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Texture> |
|---|
| 18 | #include <list> |
|---|
| 19 | |
|---|
| 20 | namespace osg { |
|---|
| 21 | |
|---|
| 22 | /** Texture2DArray state class which encapsulates OpenGL 2D array texture functionality. |
|---|
| 23 | * Texture arrays were introduced with Shader Model 4.0 hardware. |
|---|
| 24 | * |
|---|
| 25 | * A 2D texture array does contain textures sharing the same properties (e.g. size, bitdepth,...) |
|---|
| 26 | * in a layered structure. See http://www.opengl.org/registry/specs/EXT/texture_array.txt for more info. |
|---|
| 27 | */ |
|---|
| 28 | class OSG_EXPORT Texture2DArray : public Texture |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | public : |
|---|
| 32 | |
|---|
| 33 | Texture2DArray(); |
|---|
| 34 | |
|---|
| 35 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 36 | Texture2DArray(const Texture2DArray& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 37 | |
|---|
| 38 | META_StateAttribute(osg, Texture2DArray, TEXTURE); |
|---|
| 39 | |
|---|
| 40 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 41 | virtual int compare(const StateAttribute& rhs) const; |
|---|
| 42 | |
|---|
| 43 | virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D_ARRAY_EXT; } |
|---|
| 44 | |
|---|
| 45 | /** Set the texture image for specified layer. */ |
|---|
| 46 | virtual void setImage(unsigned int layer, Image* image); |
|---|
| 47 | |
|---|
| 48 | /** Get the texture image for specified layer. */ |
|---|
| 49 | virtual Image* getImage(unsigned int layer); |
|---|
| 50 | |
|---|
| 51 | /** Get the const texture image for specified layer. */ |
|---|
| 52 | virtual const Image* getImage(unsigned int layer) const; |
|---|
| 53 | |
|---|
| 54 | /** Get the number of images that are assigned to the Texture. |
|---|
| 55 | * The number is equal to the texture depth. To get the maximum possible |
|---|
| 56 | * image/layer count, you have to use the extension subclass, since it provides |
|---|
| 57 | * graphic context dependent information. |
|---|
| 58 | */ |
|---|
| 59 | virtual unsigned int getNumImages() const { return getTextureDepth(); } |
|---|
| 60 | |
|---|
| 61 | /** Check how often was a certain layer in the given context modified */ |
|---|
| 62 | inline unsigned int& getModifiedCount(unsigned int layer, unsigned int contextID) const |
|---|
| 63 | { |
|---|
| 64 | // get the modified count for the current contextID. |
|---|
| 65 | return _modifiedCount[layer][contextID]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** Set the texture width and height. If width or height are zero then |
|---|
| 69 | * the respective size value is calculated from the source image sizes. |
|---|
| 70 | * Depth parameter specifies the number of layers to be used. |
|---|
| 71 | */ |
|---|
| 72 | void setTextureSize(int width, int height, int depth); |
|---|
| 73 | |
|---|
| 74 | void setTextureWidth(int width) { _textureWidth=width; } |
|---|
| 75 | void setTextureHeight(int height) { _textureHeight=height; } |
|---|
| 76 | void setTextureDepth(int depth); |
|---|
| 77 | |
|---|
| 78 | virtual int getTextureWidth() const { return _textureWidth; } |
|---|
| 79 | virtual int getTextureHeight() const { return _textureHeight; } |
|---|
| 80 | virtual int getTextureDepth() const { return _textureDepth; } |
|---|
| 81 | |
|---|
| 82 | class OSG_EXPORT SubloadCallback : public Referenced |
|---|
| 83 | { |
|---|
| 84 | public: |
|---|
| 85 | virtual void load(const Texture2DArray& texture,State& state) const = 0; |
|---|
| 86 | virtual void subload(const Texture2DArray& texture,State& state) const = 0; |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; } |
|---|
| 91 | |
|---|
| 92 | SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); } |
|---|
| 93 | |
|---|
| 94 | const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** Set the number of mip map levels the the texture has been created with. |
|---|
| 99 | * Should only be called within an osg::Texture::apply() and custom OpenGL texture load. |
|---|
| 100 | */ |
|---|
| 101 | void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; } |
|---|
| 102 | |
|---|
| 103 | /** Get the number of mip map levels the the texture has been created with. */ |
|---|
| 104 | unsigned int getNumMipmapLevels() const { return _numMipmapLevels; } |
|---|
| 105 | |
|---|
| 106 | /** Copies a two-dimensional texture subimage, as per |
|---|
| 107 | * glCopyTexSubImage3D. Updates a portion of an existing OpenGL |
|---|
| 108 | * texture object from the current OpenGL background framebuffer |
|---|
| 109 | * contents at position \a x, \a y with width \a width and height |
|---|
| 110 | * \a height. Loads framebuffer data into the texture using offsets |
|---|
| 111 | * \a xoffset and \a yoffset. \a zoffset specifies the layer of the texture |
|---|
| 112 | * array to which the result is copied. |
|---|
| 113 | */ |
|---|
| 114 | void copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height ); |
|---|
| 115 | |
|---|
| 116 | /** Bind the texture if already compiled. Otherwise recompile. |
|---|
| 117 | */ |
|---|
| 118 | virtual void apply(State& state) const; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** Extensions class which encapsulates the querying of extensions and |
|---|
| 122 | * associated function pointers, and provides convenience wrappers to |
|---|
| 123 | * check for the extensions or use the associated functions. |
|---|
| 124 | */ |
|---|
| 125 | class OSG_EXPORT Extensions : public osg::Referenced |
|---|
| 126 | { |
|---|
| 127 | public: |
|---|
| 128 | Extensions(unsigned int contextID); |
|---|
| 129 | |
|---|
| 130 | Extensions(const Extensions& rhs); |
|---|
| 131 | |
|---|
| 132 | void lowestCommonDenominator(const Extensions& rhs); |
|---|
| 133 | |
|---|
| 134 | void setupGLExtensions(unsigned int contextID); |
|---|
| 135 | |
|---|
| 136 | void setTexture2DArraySupported(bool flag) { _isTexture2DArraySupported=flag; } |
|---|
| 137 | bool isTexture2DArraySupported() const { return _isTexture2DArraySupported; } |
|---|
| 138 | |
|---|
| 139 | void setTexture3DSupported(bool flag) { _isTexture3DSupported=flag; } |
|---|
| 140 | bool isTexture3DSupported() const { return _isTexture3DSupported; } |
|---|
| 141 | |
|---|
| 142 | void setMaxLayerCount(GLint count) { _maxLayerCount = count; } |
|---|
| 143 | GLint maxLayerCount() const { return _maxLayerCount; } |
|---|
| 144 | |
|---|
| 145 | void setMax2DSize(GLint size) { _max2DSize = size; } |
|---|
| 146 | GLint max2DSize() const { return _max2DSize; } |
|---|
| 147 | |
|---|
| 148 | void glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) const; |
|---|
| 149 | |
|---|
| 150 | void glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) const; |
|---|
| 151 | |
|---|
| 152 | void glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) const; |
|---|
| 153 | |
|---|
| 154 | bool isCompressedTexImage3DSupported() const { return _glCompressedTexImage3D!=0; } |
|---|
| 155 | void glCompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *data) const; |
|---|
| 156 | |
|---|
| 157 | bool isCompressedTexSubImage3DSupported() const { return _glCompressedTexSubImage3D!=0; } |
|---|
| 158 | void glCompressedTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data ) const; |
|---|
| 159 | |
|---|
| 160 | protected: |
|---|
| 161 | |
|---|
| 162 | ~Extensions() {} |
|---|
| 163 | |
|---|
| 164 | bool _isTexture2DArraySupported; |
|---|
| 165 | bool _isTexture3DSupported; |
|---|
| 166 | |
|---|
| 167 | GLint _maxLayerCount; |
|---|
| 168 | GLint _max2DSize; |
|---|
| 169 | |
|---|
| 170 | typedef void (GL_APIENTRY * GLTexImage3DProc) ( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); |
|---|
| 171 | typedef void (GL_APIENTRY * GLTexSubImage3DProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); |
|---|
| 172 | typedef void (GL_APIENTRY * CompressedTexImage3DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *data); |
|---|
| 173 | typedef void (GL_APIENTRY * CompressedTexSubImage3DArbProc) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data); |
|---|
| 174 | typedef void (GL_APIENTRY * GLCopyTexSubImageProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ); |
|---|
| 175 | |
|---|
| 176 | GLTexImage3DProc _glTexImage3D; |
|---|
| 177 | GLTexSubImage3DProc _glTexSubImage3D; |
|---|
| 178 | CompressedTexImage3DArbProc _glCompressedTexImage3D; |
|---|
| 179 | CompressedTexSubImage3DArbProc _glCompressedTexSubImage3D; |
|---|
| 180 | GLCopyTexSubImageProc _glCopyTexSubImage3D; |
|---|
| 181 | |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | /** Function to call to get the extension of a specified context. |
|---|
| 185 | * If the Extension object for that context has not yet been created |
|---|
| 186 | * and the 'createIfNotInitalized' flag been set to false then returns NULL. |
|---|
| 187 | * If 'createIfNotInitalized' is true then the Extensions object is |
|---|
| 188 | * automatically created. However, in this case the extension object will |
|---|
| 189 | * only be created with the graphics context associated with ContextID. |
|---|
| 190 | */ |
|---|
| 191 | static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized); |
|---|
| 192 | |
|---|
| 193 | /** The setExtensions method allows users to override the extensions across graphics contexts. |
|---|
| 194 | * Typically used when you have different extensions supported across graphics pipes |
|---|
| 195 | * but need to ensure that they all use the same low common denominator extensions. |
|---|
| 196 | */ |
|---|
| 197 | static void setExtensions(unsigned int contextID,Extensions* extensions); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | protected : |
|---|
| 201 | |
|---|
| 202 | virtual ~Texture2DArray(); |
|---|
| 203 | |
|---|
| 204 | bool imagesValid() const; |
|---|
| 205 | |
|---|
| 206 | virtual void computeInternalFormat() const; |
|---|
| 207 | void allocateMipmap(State& state) const; |
|---|
| 208 | |
|---|
| 209 | void applyTexImage2DArray_subload(State& state, Image* image, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const; |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Use std::vector to encapsulate referenced pointers to images of different layers. |
|---|
| 213 | * Vectors gives us a random access iterator. The overhead of non-used elements is negligible */ |
|---|
| 214 | std::vector<ref_ptr<Image> > _images; |
|---|
| 215 | |
|---|
| 216 | // subloaded images can have different texture and image sizes. |
|---|
| 217 | mutable GLsizei _textureWidth, _textureHeight, _textureDepth; |
|---|
| 218 | |
|---|
| 219 | // number of mip map levels the the texture has been created with, |
|---|
| 220 | mutable GLsizei _numMipmapLevels; |
|---|
| 221 | |
|---|
| 222 | ref_ptr<SubloadCallback> _subloadCallback; |
|---|
| 223 | |
|---|
| 224 | typedef buffered_value<unsigned int> ImageModifiedCount; |
|---|
| 225 | mutable std::vector<ImageModifiedCount> _modifiedCount; |
|---|
| 226 | }; |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | #endif |
|---|