NAP
texture.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "surfacedescriptor.h"
9 #include "renderutils.h"
10 #include "textureutils.h"
11 
12 // External Includes
13 #include <nap/resource.h>
14 #include <glm/glm.hpp>
15 #include <nap/numeric.h>
16 #include <rtti/factory.h>
17 #include <nap/signalslot.h>
18 
19 namespace nap
20 {
21  // Forward Declares
22  class Bitmap;
23  class RenderService;
24  class Core;
25  class TextureLink2D;
26  class TextureLinkCube;
27 
31  class NAPAPI Texture : public Resource
32  {
33  friend class RenderService;
34  RTTI_ENABLE(Resource)
35  public:
36  // Constructor
37  Texture(Core& core);
38 
39  // Destructor
40  virtual ~Texture() { };
41 
45  virtual uint getLayerCount() const = 0;
46 
50  virtual uint getMipLevels() const = 0;
51 
55  virtual const ImageData& getHandle() const = 0;
56 
60  VkFormat getFormat() const { return mFormat; }
61 
65  const SurfaceDescriptor& getDescriptor() const { return mDescriptor; }
66 
70  RenderService& getRenderService() { return mRenderService; }
71 
75  const RenderService& getRenderService() const { return mRenderService; }
76 
80  virtual void onDestroy() override { textureDestroyed(); }
81 
83 
84  protected:
88  virtual ImageData& getHandle() = 0;
89 
90  // Hide default resource init. Use specialized initialization functions instead.
91  using Resource::init;
92 
96  virtual void clear(VkCommandBuffer commandBuffer);
97 
101  void requestClear();
102 
103  protected:
106  VkFormat mFormat = VK_FORMAT_UNDEFINED;
107  VkClearColorValue mClearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
108  };
109 
110 
117  class NAPAPI Texture2D : public Texture
118  {
119  friend class RenderService;
120  friend class Texture2DLink;
121  friend void utility::blit(VkCommandBuffer, Texture2D&, Texture2D&);
122  friend void utility::copy(VkCommandBuffer, Texture2D&, Texture2D&);
123  RTTI_ENABLE(Texture)
124 
125  public:
129  enum class EUsage
130  {
131  Static = 0,
132  DynamicRead = 1,
133  DynamicWrite = 2,
134  Internal = 3
135  };
136 
137  Texture2D(Core& core);
138  virtual ~Texture2D() override;
139 
151  bool init(const SurfaceDescriptor& descriptor, EUsage usage, int mipCount, const glm::vec4& clearColor, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState);
152 
164  bool init(const SurfaceDescriptor& descriptor, EUsage usage, int mipCount, void* initialData, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState);
165 
169  const glm::vec2 getSize() const { return { getWidth(), getHeight() }; }
170 
174  int getWidth() const { return mDescriptor.mWidth; }
175 
179  int getHeight() const { return mDescriptor.mHeight; }
180 
190  void update(const void* data, int width, int height, int pitch, ESurfaceChannels channels);
191 
198  void update(const void* data, const SurfaceDescriptor& surfaceDescriptor);
199 
203  virtual uint getLayerCount() const override { return 1; }
204 
208  virtual uint getMipLevels() const override { return mMipLevels; }
209 
213  virtual const ImageData& getHandle() const override { return mImageData; }
214 
220  void asyncGetData(Bitmap& bitmap);
221 
227  void asyncGetData(std::function<void(const void*, size_t)> copyFunction);
228 
229  // Don't allow implicit conversion from bool to int -> EVIL
230  bool init(const SurfaceDescriptor& descriptor, EUsage usage, bool mipCount, const glm::vec4& clearColor, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState) = delete;
231  bool init(const SurfaceDescriptor& descriptor, EUsage usage, bool mipCount, void* initialData, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState) = delete;
232 
233  protected:
237  virtual ImageData& getHandle() override { return mImageData; }
238 
248  bool initInternal(const SurfaceDescriptor& descriptor, EUsage usage, int mipCount, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState);
249 
253  void upload(VkCommandBuffer commandBuffer);
254 
258  void download(VkCommandBuffer commandBuffer);
259 
263  void notifyDownloadReady(int frameIndex);
264 
268  void clearDownloads();
269 
270  using TextureReadCallback = std::function<void(void* data, size_t sizeInBytes)>;
271 
273  std::vector<BufferData> mStagingBuffers;
274  int mCurrentStagingBufferIndex = -1;
275  size_t mImageSizeInBytes = -1;
276  std::vector<TextureReadCallback> mReadCallbacks;
277  std::vector<int> mDownloadStagingBufferIndices;
278  uint32 mMipLevels = 1;
280 
281  private:
286  bool initStagingBuffers(EUsage usage, size_t imageSizeBytes, utility::ErrorState& error);
287  };
288 
289 
302  class NAPAPI TextureCube : public Texture
303  {
304  friend class RenderService;
305  friend class TextureCubeLink;
306  RTTI_ENABLE(Texture)
307  public:
308  // The image layer count is equal to the number of sides of a cube
309  static constexpr const uint layerCount = 6;
310 
311  TextureCube(Core& core);
312  virtual ~TextureCube() override;
313 
324  bool init(const SurfaceDescriptor& descriptor, int mipCount, const glm::vec4& clearColor, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState);
325 
329  const glm::vec2 getSize() const { return { getWidth(), getHeight() }; }
330 
334  int getWidth() const { return mDescriptor.mWidth; }
335 
339  int getHeight() const { return mDescriptor.mHeight; }
340 
344  virtual uint getLayerCount() const override { return layerCount; }
345 
349  virtual uint getMipLevels() const override { return mMipLevels; }
350 
354  virtual const ImageData& getHandle() const override { return mImageData; }
355 
360  virtual ImageData& getHandle() override { return mImageData; }
361 
362  // Don't allow implicit conversion from bool to int -> EVIL
363  bool init(const SurfaceDescriptor& descriptor, bool mipCount, const glm::vec4& clearColor, VkImageUsageFlags requiredFlags, utility::ErrorState& errorState) = delete;
364 
365  protected:
367  uint32 mMipLevels = 1;
368  };
369 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::rtti::Object::init
virtual bool init(utility::ErrorState &errorState)
Definition: object.h:46
nap::Texture2D::getHandle
virtual ImageData & getHandle() override
Definition: texture.h:237
nap::Texture::~Texture
virtual ~Texture()
Definition: texture.h:40
nap::Texture2D::getLayerCount
virtual uint getLayerCount() const override
Definition: texture.h:203
nap::Texture
Definition: texture.h:31
nap::Texture2D::mStagingBuffers
std::vector< BufferData > mStagingBuffers
All vulkan staging buffers, 1 when static or using dynamic read, no. of frames in flight when dynamic...
Definition: texture.h:273
nap::TextureCube
Definition: texture.h:302
nap::EMemoryUsage::Static
@ Static
Buffer data is uploaded only once from the CPU to the GPU.
nap::EMemoryUsage::DynamicRead
@ DynamicRead
Buffer data is uploaded only once from the CPU to the GPU, and frequently read from GPU to CPU.
nap::Texture2D::mImageData
ImageData mImageData
2D Texture vulkan image buffers
Definition: texture.h:272
nap::ESurfaceChannels
ESurfaceChannels
Definition: surfacedescriptor.h:26
nap::TextureCube::getSize
const glm::vec2 getSize() const
Definition: texture.h:329
nap::Texture2D::mDownloadStagingBufferIndices
std::vector< int > mDownloadStagingBufferIndices
Staging buffer indices associated with a frameindex.
Definition: texture.h:277
nap::utility::blit
void NAPAPI blit(VkCommandBuffer commandBuffer, Texture2D &srcTexture, Texture2D &dstTexture)
nap::Texture::getDescriptor
const SurfaceDescriptor & getDescriptor() const
Definition: texture.h:65
nap::TextureCube::getHandle
virtual const ImageData & getHandle() const override
Definition: texture.h:354
nap::Texture::getRenderService
RenderService & getRenderService()
Definition: texture.h:70
nap::utility::ErrorState
Definition: errorstate.h:19
nap::utility::copy
void NAPAPI copy(VkCommandBuffer commandBuffer, Texture2D &srcTexture, Texture2D &dstTexture)
nap::Texture2D::EUsage
EUsage
Definition: texture.h:129
nap::Texture::textureDestroyed
nap::Signal textureDestroyed
Signal that is triggered before texture is destroyed.
Definition: texture.h:82
nap::uint32
uint32_t uint32
Definition: numeric.h:20
nap::Texture2D
Definition: texture.h:117
nap::ImageData
Definition: imagedata.h:23
nap::Signal
Definition: signalslot.h:25
nap::SurfaceDescriptor
Definition: surfacedescriptor.h:46
nap::RenderService
Definition: renderservice.h:198
nap::Core
Definition: core.h:82
nap::Texture::getFormat
VkFormat getFormat() const
Definition: texture.h:60
nap::EMemoryUsage::DynamicWrite
@ DynamicWrite
Buffer data is updated more than once from the CPU to the GPU.
nap::Texture2D::TextureReadCallback
std::function< void(void *data, size_t sizeInBytes)> TextureReadCallback
Definition: texture.h:270
nap::Texture::onDestroy
virtual void onDestroy() override
Definition: texture.h:80
nap::Texture2D::getMipLevels
virtual uint getMipLevels() const override
Definition: texture.h:208
nap::Texture::mRenderService
RenderService & mRenderService
Reference to the render service.
Definition: texture.h:104
nap::Bitmap
Definition: bitmap.h:31
nap::Texture2D::getWidth
int getWidth() const
Definition: texture.h:174
nap::TextureCube::getWidth
int getWidth() const
Definition: texture.h:334
nap
Definition: templateapp.h:17
nap::Texture::getRenderService
const RenderService & getRenderService() const
Definition: texture.h:75
nap::Texture2D::mReadCallbacks
std::vector< TextureReadCallback > mReadCallbacks
Number of callbacks based on number of frames in flight.
Definition: texture.h:276
nap::TextureCube::layerCount
static constexpr const uint layerCount
Definition: texture.h:309
nap::Resource
Definition: resource.h:19
nap::Texture2D::getHandle
virtual const ImageData & getHandle() const override
Definition: texture.h:213
nap::TextureCube::getMipLevels
virtual uint getMipLevels() const override
Definition: texture.h:349
nap::TextureCube::getHeight
int getHeight() const
Definition: texture.h:339
nap::Texture2D::getHeight
int getHeight() const
Definition: texture.h:179
nap::Texture2D::mUsage
EUsage mUsage
Intented texture usage.
Definition: texture.h:279
nap::Texture2D::getSize
const glm::vec2 getSize() const
Definition: texture.h:169
nap::TextureCube::getHandle
virtual ImageData & getHandle() override
Definition: texture.h:360
nap::Texture::mDescriptor
SurfaceDescriptor mDescriptor
Texture description.
Definition: texture.h:105
nap::TextureCube::getLayerCount
virtual uint getLayerCount() const override
Definition: texture.h:344