1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_TEXTURE2DARRAY
15#define OSG_TEXTURE2DARRAY 1
22/** Texture2DArray state class which encapsulates OpenGL 2D array texture functionality.
23 * Texture arrays were introduced with Shader Model 4.0 hardware.
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.
28class OSG_EXPORT Texture2DArray : public Texture
35 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
36 Texture2DArray(const Texture2DArray& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
38 META_StateAttribute(osg, Texture2DArray, TEXTURE);
40 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
41 virtual int compare(const StateAttribute& rhs) const;
43 virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D_ARRAY; }
45 /** Texture2DArray is related to non fixed pipeline usage only so isn't appropriate to enable/disable.*/
46 virtual bool getModeUsage(StateAttribute::ModeUsage&) const { return false; }
48 /** Set the texture image for specified layer. */
49 virtual void setImage(unsigned int layer, Image* image);
51 template<class T> void setImage(unsigned int layer, const ref_ptr<T>& image) { setImage(layer, image.get()); }
53 /** Get the texture image for specified layer. */
54 virtual Image* getImage(unsigned int layer);
56 /** Get the const texture image for specified layer. */
57 virtual const Image* getImage(unsigned int layer) const;
59 /** Get the number of images that are assigned to the Texture.
60 * The number is equal to the texture depth. To get the maximum possible
61 * image/layer count, you have to use the extension subclass, since it provides
62 * graphic context dependent information.
64 virtual unsigned int getNumImages() const { return _images.size(); }
66 /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
67 virtual bool isDirty(unsigned int contextID) const
69 for(unsigned int i=0; i<_images.size(); ++i)
71 if (_images[i].valid() && _images[i]->getModifiedCount()!=_modifiedCount[i][contextID]) return true;
76 /** Check how often was a certain layer in the given context modified */
77 inline unsigned int& getModifiedCount(unsigned int layer, unsigned int contextID) const
79 // get the modified count for the current contextID.
80 return _modifiedCount[layer][contextID];
83 /** Set the texture width and height. If width or height are zero then
84 * the respective size value is calculated from the source image sizes.
85 * Depth parameter specifies the number of layers to be used.
87 void setTextureSize(int width, int height, int depth);
89 void setTextureWidth(int width) { _textureWidth=width; }
90 void setTextureHeight(int height) { _textureHeight=height; }
91 void setTextureDepth(int depth);
93 virtual int getTextureWidth() const { return _textureWidth; }
94 virtual int getTextureHeight() const { return _textureHeight; }
95 virtual int getTextureDepth() const { return _textureDepth; }
97 class OSG_EXPORT SubloadCallback : public Referenced
100 virtual void load(const Texture2DArray& texture,State& state) const = 0;
101 virtual void subload(const Texture2DArray& texture,State& state) const = 0;
105 void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
107 SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
109 const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
113 /** Set the number of mip map levels the texture has been created with.
114 * Should only be called within an osg::Texture::apply() and custom OpenGL texture load.
116 void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
118 /** Get the number of mip map levels the texture has been created with. */
119 unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
121 /** Copies a two-dimensional texture subimage, as per
122 * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
123 * texture object from the current OpenGL background framebuffer
124 * contents at position \a x, \a y with width \a width and height
125 * \a height. Loads framebuffer data into the texture using offsets
126 * \a xoffset and \a yoffset. \a zoffset specifies the layer of the texture
127 * array to which the result is copied.
129 void copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height );
131 /** Bind the texture if already compiled. Otherwise recompile.
133 virtual void apply(State& state) const;
137 virtual ~Texture2DArray();
139 bool imagesValid() const;
141 virtual void computeInternalFormat() const;
142 GLsizei computeTextureDepth() const;
143 void allocateMipmap(State& state) const;
145 void applyTexImage2DArray_subload(State& state, Image* image, GLsizei layer, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const;
147 typedef std::vector< ref_ptr<Image> > Images;
150 // subloaded images can have different texture and image sizes.
151 mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
153 // number of mip map levels the texture has been created with,
154 mutable GLsizei _numMipmapLevels;
156 ref_ptr<SubloadCallback> _subloadCallback;
158 typedef buffered_value<unsigned int> ImageModifiedCount;
159 mutable std::vector<ImageModifiedCount> _modifiedCount;