Texture2DArray 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef OSG_TEXTURE2DARRAY
  14. #define OSG_TEXTURE2DARRAY 1
  15. #include <osg/Texture>
  16. #include <list>
  17. namespace osg {
  18. /** Texture2DArray state class which encapsulates OpenGL 2D array texture functionality.
  19. * Texture arrays were introduced with Shader Model 4.0 hardware.
  20. *
  21. * A 2D texture array does contain textures sharing the same properties (e.g. size, bitdepth,...)
  22. * in a layered structure. See http://www.opengl.org/registry/specs/EXT/texture_array.txt for more info.
  23. */
  24. class OSG_EXPORT Texture2DArray : public Texture
  25. {
  26. public :
  27. Texture2DArray();
  28. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  29. Texture2DArray(const Texture2DArray& cm,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  30. META_StateAttribute(osg, Texture2DArray, TEXTURE);
  31. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  32. virtual int compare(const StateAttribute& rhs) const;
  33. virtual GLenum getTextureTarget() const { return GL_TEXTURE_2D_ARRAY; }
  34. /** Texture2DArray is related to non fixed pipeline usage only so isn't appropriate to enable/disable.*/
  35. virtual bool getModeUsage(StateAttribute::ModeUsage&) const { return false; }
  36. /** Set the texture image for specified layer. */
  37. virtual void setImage(unsigned int layer, Image* image);
  38. template<class T> void setImage(unsigned int layer, const ref_ptr<T>& image) { setImage(layer, image.get()); }
  39. /** Get the texture image for specified layer. */
  40. virtual Image* getImage(unsigned int layer);
  41. /** Get the const texture image for specified layer. */
  42. virtual const Image* getImage(unsigned int layer) const;
  43. /** Get the number of images that are assigned to the Texture.
  44. * The number is equal to the texture depth. To get the maximum possible
  45. * image/layer count, you have to use the extension subclass, since it provides
  46. * graphic context dependent information.
  47. */
  48. virtual unsigned int getNumImages() const { return _images.size(); }
  49. /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
  50. virtual bool isDirty(unsigned int contextID) const
  51. {
  52. for(unsigned int i=0; i<_images.size(); ++i)
  53. {
  54. if (_images[i].valid() && _images[i]->getModifiedCount()!=_modifiedCount[i][contextID]) return true;
  55. }
  56. return false;
  57. }
  58. /** Check how often was a certain layer in the given context modified */
  59. inline unsigned int& getModifiedCount(unsigned int layer, unsigned int contextID) const
  60. {
  61. // get the modified count for the current contextID.
  62. return _modifiedCount[layer][contextID];
  63. }
  64. /** Set the texture width and height. If width or height are zero then
  65. * the respective size value is calculated from the source image sizes.
  66. * Depth parameter specifies the number of layers to be used.
  67. */
  68. void setTextureSize(int width, int height, int depth);
  69. void setTextureWidth(int width) { _textureWidth=width; }
  70. void setTextureHeight(int height) { _textureHeight=height; }
  71. void setTextureDepth(int depth);
  72. virtual int getTextureWidth() const { return _textureWidth; }
  73. virtual int getTextureHeight() const { return _textureHeight; }
  74. virtual int getTextureDepth() const { return _textureDepth; }
  75. class OSG_EXPORT SubloadCallback : public Referenced
  76. {
  77. public:
  78. virtual void load(const Texture2DArray& texture,State& state) const = 0;
  79. virtual void subload(const Texture2DArray& texture,State& state) const = 0;
  80. };
  81. void setSubloadCallback(SubloadCallback* cb) { _subloadCallback = cb;; }
  82. SubloadCallback* getSubloadCallback() { return _subloadCallback.get(); }
  83. const SubloadCallback* getSubloadCallback() const { return _subloadCallback.get(); }
  84. /** Set the number of mip map levels the texture has been created with.
  85. * Should only be called within an osg::Texture::apply() and custom OpenGL texture load.
  86. */
  87. void setNumMipmapLevels(unsigned int num) const { _numMipmapLevels=num; }
  88. /** Get the number of mip map levels the texture has been created with. */
  89. unsigned int getNumMipmapLevels() const { return _numMipmapLevels; }
  90. /** Copies a two-dimensional texture subimage, as per
  91. * glCopyTexSubImage3D. Updates a portion of an existing OpenGL
  92. * texture object from the current OpenGL background framebuffer
  93. * contents at position \a x, \a y with width \a width and height
  94. * \a height. Loads framebuffer data into the texture using offsets
  95. * \a xoffset and \a yoffset. \a zoffset specifies the layer of the texture
  96. * array to which the result is copied.
  97. */
  98. void copyTexSubImage2DArray(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height );
  99. /** Bind the texture if already compiled. Otherwise recompile.
  100. */
  101. virtual void apply(State& state) const;
  102. protected :
  103. virtual ~Texture2DArray();
  104. bool imagesValid() const;
  105. virtual void computeInternalFormat() const;
  106. GLsizei computeTextureDepth() const;
  107. void allocateMipmap(State& state) const;
  108. void applyTexImage2DArray_subload(State& state, Image* image, GLsizei layer, GLsizei inwidth, GLsizei inheight, GLsizei indepth, GLint inInternalFormat, GLsizei& numMipmapLevels) const;
  109. typedef std::vector< ref_ptr<Image> > Images;
  110. Images _images;
  111. // subloaded images can have different texture and image sizes.
  112. mutable GLsizei _textureWidth, _textureHeight, _textureDepth;
  113. // number of mip map levels the texture has been created with,
  114. mutable GLsizei _numMipmapLevels;
  115. ref_ptr<SubloadCallback> _subloadCallback;
  116. typedef buffered_value<unsigned int> ImageModifiedCount;
  117. mutable std::vector<ImageModifiedCount> _modifiedCount;
  118. };
  119. }
  120. #endif