HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_TextureCache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RV_TextureCache.h (RV Library, C++)
7  *
8  * COMMENTS:
9  */
10 #ifndef RV_TextureCache_h
11 #define RV_TextureCache_h
12 
13 #include "RV_API.h"
14 #include "RV_Type.h"
15 #include "RV_VKImage.h"
16 
17 #include <PXL/PXL_Raster.h>
18 #include <UT/UT_Array.h>
19 #include <UT/UT_Cache.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_Map.h>
22 #include <UT/UT_MemoryResource.h>
23 #include <UT/UT_StringMap.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_UniquePtr.h>
26 #include <UT/UT_VectorTypes.h>
27 
28 #include <SYS/SYS_Types.h>
29 
30 #define RV_INVALID_OP_ID -1
31 
32 typedef void (*RV_TextureHDAResolverCB)(const char *optable_optype,
33  const char *pattern,
34  UT_StringArray &files);
35 
36 class RV_Render;
38 class RV_TextureResolver;
39 class RV_Instance;
40 
41 /// Global cache of vulkan textures sourced from files.
43 {
44 public:
45  /// Initialize the cache (only used by the system during startup)
46  static void init(const UT_StringHolder &device_name);
47  /// Destroy the cache (only used by the system during exit)
48  static void cleanup();
49 
50  /// Clear all images out of the cache.
51  static void clear();
52  /// Mark any textures as dirty if their file source has been modified
53  static void updateTextures();
54 
55  /// The serial version of the cache. Incremented when a texture is added
56  static exint cacheVersion();
57  /// The serial refresh count of the cache. Incremented when the user
58  /// explicity updates the textures
59  static exint cacheRefresh();
60 
61  static exint currentSizeBytes();
62 
63  static exint freeSomeMem(exint mem_in_bytes);
64 
65  /// If the texture is missing, one of these generic placeholders is used.
67  {
68  MISSING_BLACK_CLEAR, // 0,0,0,0
69  MISSING_BLACK_SOLID, // 0,0,0,1
70  MISSING_WHITE_SOLID, // 1,1,1,1
71  MISSING_RED_SOLID, // 1,0,0,1
72  MISSING_NORMAL_MAP, // 0.5, 0.5, 1, 0
73  MISSING_FALLBACK, // custom color
74 
75  MAX_PLACEHOLDER
76  };
77 
79  {
84  PRIORITY_CRITICAL
85  };
86 
87  /// Load the map (if needed) and bind the resulting texture to 'sampler_name'
88  /// in 'set'. If it cannot be loaded, bind a placeholder texture instead.
89  /// This method is equivalent to calling loadTextureRef() and then
90  /// bindTextureRef() or bindPlaceholder(). If the map is a reference to a
91  /// mofr with op:, op_id will contain its unique ID.
92  static bool bindTexture(RV_Render *r,
93  RV_TextureRef &map,
94  const RV_TextureParms &map_parms,
95  const UT_StringHolder &map_name,
96  const UT_StringHolder &relative_to_node,
97  RV_ImageDim tex_type,
99  const UT_StringHolder &sampler_name,
100  bool deferred_load,
101  bool &deferred_loaded,
102  int &op_id,
103  TexturePriority priority = PRIORITY_NORMAL,
104  PlaceholderType missing=MISSING_WHITE_SOLID,
105  UT_Vector4F *fallback_color = nullptr);
106 
107  /// Binds an existing texture ref to the set at 'sampler_name'. It should not
108  /// be a placeholder image (use bindPlaceholder instead).
109  static bool bindTextureRef(RV_Render *r,
112  const UT_StringHolder &sampler_name);
113 
114  /// Bind a small placeholder texture (1x1) to 'sampler_name' in 'set'.
115  /// The RGBA texture color is defined by the placeholder_type, or can be a
116  /// custom/ color if the type is FALLBACK. The texture is 8b unorm RGBA.
117  static bool bindPlaceholder(RV_Render *r,
118  RV_ImageDim tex_type,
120  const UT_StringHolder &sampler_name,
121  PlaceholderType placeholder_type,
122  const UT_Vector4F *fallbackcolor=nullptr);
123 
124  /// Load the map info a texture ref without binding it to a shader. If the
125  /// map could not be loaded, map.isPlaceholder() will be true.
126  /// If allow_deferred_load is true, background load the texture. Keep calling
127  /// this in subsequent redraws until defer_load_finished is true.
128  static bool loadTextureRef(RV_Render *r,
129  RV_TextureRef &map,
130  const RV_TextureParms &map_parms,
131  const UT_StringHolder &map_name,
132  const UT_StringHolder &relative_to_node,
133  RV_ImageDim tex_type,
134  bool allow_deferred_load,
135  bool &defer_load_finished,
136  int &op_id,
137  TexturePriority priority = PRIORITY_NORMAL,
138  UT_Vector3i *resolution = nullptr);
139 
140  /// Cache an existing 2D Image. The name should be prefixed with exp:
141  /// (explicitly cached).
142  static bool cacheAsTexture(RV_Render *r,
143  RV_TextureRef &map,
144  const PXL_Raster *source_image,
145  const RV_TextureParms &map_parms,
146  const UT_StringHolder &map_name);
147  /// Remove a cached 2D Image
148  static bool removeCachedTexture(RV_TextureRef &map);
149 
150  /// Cache an existing image. The cache takes ownership (use std::move for
151  /// the image). If the image is a UDIM, you can optionally pass the bounds.
152  /// The map name should be prefixed with exp: (explicitly cached).
153  static bool cacheImage(UT_UniquePtr<RV_VKImage> image,
154  RV_TextureRef &map,
155  const UT_StringHolder &map_name,
156  UT_DimRect *udim_bounds = nullptr);
157  /// Remove a cached image from the cache, taking ownership of it.
158  /// If udim_bounds is not null, write the bounds to it.
160  uncacheImage(RV_TextureRef &map,
161  UT_DimRect *udim_bounds = nullptr);
162 
163 
164  /// Check if the texture is out of date. Flag the ref and set `dirty` if so.
165  /// doesn't touch either if it's current.
166  static void checkTextureDirty(RV_TextureRef &map, bool &dirty);
167 
168  /// Check if the texture has an alpha channel
169  static void checkTextureHasAlpha(const RV_TextureRef &ref, bool &has_alpha);
170 
171  /// For resolving a bunch of files to a 2D array, this is the separator
172  /// character between the filenames.
173  static const char * getFileSeparator() { return "\x1f"; }
174 
175  static void setSingleTexMemLimitMB(exint mem_in_mb);
176  static exint singleTexMemLimitMB();
177 
178  static void setCacheSize(exint mem_in_bytes);
179 
180  /// Reduce all textures in the cache by at least this amount.
181  static void setReductionFactor(fpreal reduce);
182 
183  /// For resolving
184  static void installHDACallback(RV_TextureHDAResolverCB resolver_func);
185 
186  /// @internal
188  {
189  public:
190  ImageInfo();
191 
198  int udim_x = 0;
199  int udim_y = 0;
200  int udim_w = 0;
201  int udim_h = 0;
202  int width = 1;
203  int height = 1;
204  int depth = 1;
205  bool check_alpha = false;
206  bool alpha = false;
207  time_t modtime = 0;
208  fpreal optime = 0.0;
209  int op_id = RV_INVALID_OP_ID;
210  int version = 0;
211  bool valid = false;
212  bool loading = false;
213  fpreal downscale = 1.0;
214  };
215 
216 protected:
217  class Image
218  {
219  public:
220  RV_VKImage *image = nullptr;
221  int udim_x = 0;
222  int udim_y = 0;
223  int udim_w = 0;
224  int udim_h = 0;
225  int op_id = RV_INVALID_OP_ID;
226  };
227 
228  static RV_TextureCache::Image getTexture(exint id);
229 
230  exint resolveTexture(RV_Render *r,
231  const UT_StringHolder &map_name,
232  const UT_StringHolder &relative_to_node,
233  const RV_TextureParms &tex_parms,
234  RV_ImageDim tex_type,
235  bool force_update,
236  bool deferred_load,
237  bool &defer_loaded,
238  int &op_id,
239  bool &missing_texture,
241  TexturePriority priority,
243 
244  static RV_VKImage *getPlaceholder(RV_Render *r, RV_ImageDim tex_type,
245  PlaceholderType type =MISSING_WHITE_SOLID,
246  const UT_Vector4F *fallbackcolor =nullptr);
247 
248  RV_TextureCache::Image privGetTexture(exint id);
249 
251  ~RV_TextureCache() override;
252 
254 
255  // UT_Cache interface
256  const char *utGetCacheName() const override
257  { return "Vulkan Texture Cache"; }
258  int64 utGetCurrentSize() const override
259  { return myCurrentSize; }
260  int64 utReduceCacheSizeBy(int64 amount) override
261  { return freeTextures(amount, true, false); }
262  bool utHasMaxSize() const override { return true; }
263  int64 utGetMaxSize() const override;
264  void utSetMaxSize(int64) override { }
265 
266 
267 private:
268 
269  exint freeTextures(exint needed_to_free, bool free_used, bool must_free);
270  exint cachedSizeBytes() const;
271 
272  class GraphicsMemoryClient : public UT_MemoryClient
273  {
274  public:
275  GraphicsMemoryClient(RV_TextureCache &cache) : myCache(cache) {}
276 
277  const char *name() const override
278  { return "Vulkan Texture Cache";}
279 
280  bool freeMemoryRequest(const UT_MemoryResource *resource,
281  RequestSeverity severity,
282  Niceness nice_level,
283  exint size_in_bytes,
284  exint &freed_amount_in_bytes) override;
285  bool memoryUse(const UT_MemoryResource *resource,
286  exint &in_use_bytes,
287  exint &cached_bytes) override;
288 
289  ~GraphicsMemoryClient() override {}
290  UT_NON_COPYABLE(GraphicsMemoryClient)
291 
292  private:
293  RV_TextureCache &myCache;
294  };
295 
296  GraphicsMemoryClient myResource;
297  UT_Array< UT_Map<exint, ImageInfo > > myImages;
298  UT_Map<exint, RV_ImageDim> myTypeMap;
299 
300  struct ResolveEntry
301  {
302  exint id = 0;
303  TexturePriority priority = RV_TextureCache::PRIORITY_NORMAL;
305  };
306  UT_StringMap<ResolveEntry> myResolveMap;
307  UT_StringMap<fpreal> myReductionTable;
308 
310  UT_Array<UT_UniquePtr<RV_VKImage>> myPlaceholders;
311  UT_Array<UT_Map<int32, RV_VKImagePtr>> myFallbackTextures;
312  UT_Lock myLock;
313  UT_Set<exint> myDeferImages;
314 
315  exint myCurrentSize = 0;
316  fpreal myReductionFactor = 1.0;
317  UT_StringHolder myDeviceName;
318 };
319 
320 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
int64 utGetCurrentSize() const override
required - return the current cache size, in bytes
Unsorted map container.
Definition: UT_Map.h:109
void
Definition: png.h:1083
int64 exint
Definition: SYS_Types.h:125
Opaque reference to a texture stored in the RV_TextureCache.
Definition: RV_Type.h:203
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
UT_SharedPtr< RV_TextureRef::CacheData > refs
bool utHasMaxSize() const override
optional - override if the cache has a well defined maximum size
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
GLint ref
Definition: glcorearb.h:124
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
constexpr auto set(type rhs) -> int
Definition: core.h:610
T reduce(RangeT range, const T &identity, const FuncT &func, const JoinT &join)
Definition: Reduce.h:42
UT_Array< UT_UniquePtr< PXL_Raster > > rasters
#define RV_INVALID_OP_ID
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define RV_API
Definition: RV_API.h:10
long long int64
Definition: SYS_Types.h:116
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
GLenum GLenum severity
Definition: glcorearb.h:2539
void utSetMaxSize(int64) override
HUSD_API const char * resolution()
GLuint const GLchar * name
Definition: glcorearb.h:786
GLushort pattern
Definition: glad.h:2583
PlaceholderType
If the texture is missing, one of these generic placeholders is used.
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:44
Common base class for various caches.
Definition: UT_Cache.h:21
static const char * getFileSeparator()
FS_API bool cleanup(UT_StringArray &removed, UT_StringArray &error_files, exint &memory_freed, bool dry_run, const char *override_path=nullptr)
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
GT_API const UT_StringHolder version
Global cache of vulkan textures sourced from files.
fpreal64 fpreal
Definition: SYS_Types.h:278
GLint GLsizei width
Definition: glcorearb.h:103
Wrapper for a client of a limit memory resource.
GLboolean r
Definition: glcorearb.h:1222
void(* RV_TextureHDAResolverCB)(const char *optable_optype, const char *pattern, UT_StringArray &files)
RV_ImageDim
Definition: RV_Type.h:120
int64 utReduceCacheSizeBy(int64 amount) override
UT_UniquePtr< RV_VKImage > image