Overte C++ Documentation
GLTexture.h
1 //
2 // Created by Bradley Austin Davis on 2016/05/15
3 // Copyright 2013-2016 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #ifndef hifi_gpu_gl_GLTexture_h
9 #define hifi_gpu_gl_GLTexture_h
10 
11 #include <QtCore/QThreadPool>
12 #include <QtConcurrent>
13 
14 #include "GLShared.h"
15 #include "GLBackend.h"
16 #include "GLTexelFormat.h"
17 #include <thread>
18 
19 namespace gpu { namespace gl {
20 
21 struct GLFilterMode {
22  GLint minFilter;
23  GLint magFilter;
24 };
25 
26 class GLTextureTransferEngine {
27 public:
28  using Pointer = std::shared_ptr<GLTextureTransferEngine>;
29 
30  virtual ~GLTextureTransferEngine() = default;
31 
33  virtual void manageMemory() = 0;
34  virtual void shutdown() = 0;
35 
38  bool allowCreate() const { return _frameTexturesCreated < MAX_RESOURCE_TEXTURES_PER_FRAME; }
41  void addMemoryManagedTexture(const TexturePointer& texturePointer);
42 
43 protected:
44  // Fetch all the currently active textures as strong pointers, while clearing the
45  // empty weak pointers out of _registeredTextures
46  std::vector<TexturePointer> getAllTextures();
47  void resetFrameTextureCreated() { _frameTexturesCreated = 0; }
48 
49 private:
50  static const size_t MAX_RESOURCE_TEXTURES_PER_FRAME{ 2 };
51  size_t _frameTexturesCreated{ 0 };
52  std::list<TextureWeakPointer> _registeredTextures;
53 };
54 
67 class TransferJob {
68 public:
69  using Pointer = std::shared_ptr<TransferJob>;
70  using Queue = std::queue<Pointer>;
71  using Lambda = std::function<void(const TexturePointer&)>;
72 private:
73  Texture::PixelsPointer _mipData;
74  size_t _transferOffset{ 0 };
75  size_t _transferSize{ 0 };
76  uint16_t _sourceMip{ 0 };
77  bool _bufferingRequired{ true };
78  Lambda _transferLambda{ [](const TexturePointer&) {} };
79  Lambda _bufferingLambda{ [](const TexturePointer&) {} };
80 public:
81  TransferJob(const TransferJob& other) = delete;
82  TransferJob(uint16_t sourceMip, const std::function<void()>& transferLambda);
83  TransferJob(const Texture& texture, uint16_t sourceMip, uint16_t targetMip, uint8_t face, uint32_t lines = 0, uint32_t lineOffset = 0);
84  ~TransferJob();
85  const uint16_t& sourceMip() const { return _sourceMip; }
86  const size_t& size() const { return _transferSize; }
87  bool bufferingRequired() const { return _bufferingRequired; }
88  void buffer(const TexturePointer& texture) { _bufferingLambda(texture); }
89  void transfer(const TexturePointer& texture) { _transferLambda(texture); }
90 };
91 
92 using TransferJobPointer = std::shared_ptr<TransferJob>;
93 using TransferQueue = std::queue<TransferJobPointer>;
94 
95 class GLVariableAllocationSupport {
96  friend class GLBackend;
97 
98 public:
99  GLVariableAllocationSupport();
100  virtual ~GLVariableAllocationSupport();
101  virtual void populateTransferQueue(TransferQueue& pendingTransfers) = 0;
102 
103  void sanityCheck() const;
104  uint16 populatedMip() const { return _populatedMip; }
105  bool canPromote() const { return _allocatedMip > _minAllocatedMip; }
106  bool canDemote() const { return _allocatedMip < _maxAllocatedMip; }
107  bool hasPendingTransfers() const { return _populatedMip > _allocatedMip; }
108 
109  virtual size_t promote() = 0;
110  virtual size_t demote() = 0;
111 
112  static const uvec3 MAX_TRANSFER_DIMENSIONS;
113  static const uvec3 INITIAL_MIP_TRANSFER_DIMENSIONS;
114  static const size_t MAX_TRANSFER_SIZE;
115  static const size_t MAX_BUFFER_SIZE;
116 
117 protected:
118  // THe amount of memory currently allocated
119  Size _size { 0 };
120 
121  // The amount of memory currnently populated
122  void incrementPopulatedSize(Size delta) const;
123  void decrementPopulatedSize(Size delta) const;
124  mutable Size _populatedSize { 0 };
125 
126  // The allocated mip level, relative to the number of mips in the gpu::Texture object
127  // The relationship between a given glMip to the original gpu::Texture mip is always
128  // glMip + _allocatedMip
129  uint16 _allocatedMip { 0 };
130  // The populated mip level, relative to the number of mips in the gpu::Texture object
131  // This must always be >= the allocated mip
132  uint16 _populatedMip { 0 };
133  // The highest (lowest resolution) mip that we will support, relative to the number
134  // of mips in the gpu::Texture object
135  uint16 _maxAllocatedMip { 0 };
136  // The lowest (highest resolution) mip that we will support, relative to the number
137  // of mips in the gpu::Texture object
138  uint16 _minAllocatedMip { 0 };
139 };
140 
141 class GLTexture : public GLObject<Texture> {
142  using Parent = GLObject<Texture>;
143  friend class GLBackend;
144  friend class GLVariableAllocationSupport;
145 public:
146  static const uint16_t INVALID_MIP { (uint16_t)-1 };
147  static const uint8_t INVALID_FACE { (uint8_t)-1 };
148 
149  ~GLTexture();
150 
151  const GLuint& _texture { _id };
152  const std::string _source;
153  const GLenum _target;
154  GLTexelFormat _texelFormat;
155 
156  static const std::vector<GLenum>& getFaceTargets(GLenum textureType);
157  static uint8_t getFaceCount(GLenum textureType);
158  static GLenum getGLTextureType(const Texture& texture);
159  virtual Size size() const = 0;
160  virtual Size copyMipFaceLinesFromTexture(uint16_t mip, uint8_t face, const uvec3& size, uint32_t yOffset, GLenum internalFormat, GLenum format, GLenum type, Size sourceSize, const void* sourcePointer) const = 0;
161  virtual Size copyMipFaceFromTexture(uint16_t sourceMip, uint16_t targetMip, uint8_t face) const final;
162 
163  static const uint8_t TEXTURE_2D_NUM_FACES = 1;
164  static const uint8_t TEXTURE_CUBE_NUM_FACES = 6;
165  static const GLenum CUBE_FACE_LAYOUT[TEXTURE_CUBE_NUM_FACES];
166  static const GLFilterMode FILTER_MODES[Sampler::NUM_FILTERS];
167  static const GLenum WRAP_MODES[Sampler::NUM_WRAP_MODES];
168 
169 protected:
170  virtual void generateMips() const = 0;
171  virtual void syncSampler() const = 0;
172 
173  virtual void copyTextureMipsInGPUMem(GLuint srcId, GLuint destId, uint16_t srcMipOffset, uint16_t destMipOffset, uint16_t populatedMips) {} // Only relevant for Variable Allocation textures
174 
175  GLTexture(const std::weak_ptr<gl::GLBackend>& backend, const Texture& texture, GLuint id);
176 };
177 
178 class GLExternalTexture : public GLTexture {
179  using Parent = GLTexture;
180  friend class GLBackend;
181 public:
182  ~GLExternalTexture();
183 protected:
184  GLExternalTexture(const std::weak_ptr<gl::GLBackend>& backend, const Texture& texture, GLuint id);
185  void generateMips() const override {}
186  void syncSampler() const override {}
187  Size copyMipFaceLinesFromTexture(uint16_t mip, uint8_t face, const uvec3& size, uint32_t yOffset, GLenum internalFormat, GLenum format, GLenum type, Size sourceSize, const void* sourcePointer) const override { return 0;}
188 
189  Size size() const override { return 0; }
190 };
191 
192 } }
193 
194 #endif
A simple object wrapper for an OpenGL texture.
Definition: material-networking/src/material-networking/TextureCache.h:39
Definition: GLTexture.h:67