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_NonCopyable.h>
20 #include <UT/UT_Map.h>
21 #include <UT/UT_StringMap.h>
22 #include <UT/UT_UniquePtr.h>
23 #include <UT/UT_VectorTypes.h>
24 
25 #include <SYS/SYS_Types.h>
26 
27 #define RV_INVALID_OP_ID -1
28 
29 typedef void (*RV_TextureHDAResolverCB)(const char *optable_optype,
30  const char *pattern,
31  UT_StringArray &files);
32 
33 class RV_Render;
35 class RV_TextureResolver;
36 class RV_Instance;
37 
38 /// Global cache of vulkan textures sourced from files.
40 {
41 public:
42  /// Initialize the cache (only used by the system during startup)
43  static void init();
44  /// Destroy the cache (only used by the system during exit)
45  static void cleanup();
46 
47  /// Clear all images out of the cache.
48  static void clear();
49  /// Mark any textures as dirty if their file source has been modified
50  static void updateTextures();
51 
52  /// The serial version of the cache. Incremented when a texture is added
53  static exint cacheVersion();
54  /// The serial refresh count of the cache. Incremented when the user
55  /// explicity updates the textures
56  static exint cacheRefresh();
57 
58  /// If the texture is missing, one of these generic placeholders is used.
60  {
61  MISSING_BLACK_CLEAR, // 0,0,0,0
62  MISSING_BLACK_SOLID, // 0,0,0,1
63  MISSING_WHITE_SOLID, // 1,1,1,1
64  MISSING_RED_SOLID, // 1,0,0,1
65  MISSING_NORMAL_MAP, // 0.5, 0.5, 1, 0
66  MISSING_FALLBACK, // custom color
67 
68  MAX_PLACEHOLDER
69  };
70 
71  /// Load the map (if needed) and bind the resulting texture to 'sampler_name'
72  /// in 'set'. If it cannot be loaded, bind a placeholder texture instead.
73  /// This method is equivalent to calling loadTextureRef() and then
74  /// bindTextureRef() or bindPlaceholder(). If the map is a reference to a
75  /// mofr with op:, op_id will contain its unique ID.
76  static bool bindTexture(RV_Render *r,
77  RV_TextureRef &map,
78  const RV_TextureParms &map_parms,
79  const UT_StringHolder &map_name,
80  const UT_StringHolder &relative_to_node,
81  RV_ImageDim tex_type,
83  const UT_StringHolder &sampler_name,
84  bool deferred_load,
85  bool &deferred_loaded,
86  int &op_id,
87  PlaceholderType missing=MISSING_WHITE_SOLID,
88  UT_Vector4F *fallback_color = nullptr);
89 
90  /// Binds an existing texture ref to the set at 'sampler_name'. It should not
91  /// be a placeholder image (use bindPlaceholder instead).
92  static bool bindTextureRef(RV_Render *r,
95  const UT_StringHolder &sampler_name);
96 
97  /// Bind a small placeholder texture (1x1) to 'sampler_name' in 'set'.
98  /// The RGBA texture color is defined by the placeholder_type, or can be a
99  /// custom/ color if the type is FALLBACK. The texture is 8b unorm RGBA.
100  static bool bindPlaceholder(RV_Render *r,
101  RV_ImageDim tex_type,
103  const UT_StringHolder &sampler_name,
104  PlaceholderType placeholder_type,
105  const UT_Vector4F *fallbackcolor=nullptr);
106 
107  /// Load the map info a texture ref without binding it to a shader. If the
108  /// map could not be loaded, map.isPlaceholder() will be true.
109  /// If allow_deferred_load is true, background load the texture. Keep calling
110  /// this in subsequent redraws until defer_load_finished is true.
111  static bool loadTextureRef(RV_Render *r,
112  RV_TextureRef &map,
113  const RV_TextureParms &map_parms,
114  const UT_StringHolder &map_name,
115  const UT_StringHolder &relative_to_node,
116  RV_ImageDim tex_type,
117  bool allow_deferred_load,
118  bool &defer_load_finished,
119  int &op_id,
120  UT_Vector3i *resolution = nullptr);
121 
122  /// Cache an existing 2D Image. The name should be prefixed with exp:
123  /// (explicitly cached).
124  static bool cacheAsTexture(RV_Render *r,
125  RV_TextureRef &map,
126  const PXL_Raster *source_image,
127  const RV_TextureParms &map_parms,
128  const UT_StringHolder &map_name);
129  /// Remove a cached 2D Image
130  static bool removeCachedTexture(RV_TextureRef &map);
131 
132  /// Cache an existing image. The cache takes ownership (use std::move for
133  /// the image). If the image is a UDIM, you can optionally pass the bounds.
134  /// The map name should be prefixed with exp: (explicitly cached).
135  static bool cacheImage(UT_UniquePtr<RV_VKImage> image,
136  RV_TextureRef &map,
137  const UT_StringHolder &map_name,
138  UT_DimRect *udim_bounds = nullptr);
139  /// Remove a cached image from the cache, taking ownership of it.
140  /// If udim_bounds is not null, write the bounds to it.
142  uncacheImage(RV_TextureRef &map,
143  UT_DimRect *udim_bounds = nullptr);
144 
145 
146  /// Check if the texture is out of date. Flag the ref and set `dirty` if so.
147  /// doesn't touch either if it's current.
148  static void checkTextureDirty(RV_TextureRef &map, bool &dirty);
149 
150  /// Check if the texture has an alpha channel
151  static void checkTextureHasAlpha(const RV_TextureRef &ref, bool &has_alpha);
152 
153  /// For resolving a bunch of files to a 2D array, this is the separator
154  /// character between the filenames.
155  static const char * getFileSeparator() { return "\x1f"; }
156 
157  /// For resolving
158  static void installHDACallback(RV_TextureHDAResolverCB resolver_func);
159 
160  /// @internal
162  {
163  public:
164  ImageInfo();
165 
171  int udim_x = 0;
172  int udim_y = 0;
173  int udim_w = 0;
174  int udim_h = 0;
175  int width = 1;
176  int height = 1;
177  int depth = 1;
178  bool check_alpha = false;
179  bool alpha = false;
180  time_t modtime = 0;
181  fpreal optime = 0.0;
182  int op_id = RV_INVALID_OP_ID;
183  int version = 0;
184  bool valid = false;
185  bool loading = false;
186  };
187 
188 protected:
189  class Image
190  {
191  public:
192  RV_VKImage *image = nullptr;
193  int udim_x = 0;
194  int udim_y = 0;
195  int udim_w = 0;
196  int udim_h = 0;
197  int op_id = RV_INVALID_OP_ID;
198  };
199 
200  static RV_TextureCache::Image getTexture(exint id);
201 
202  exint resolveTexture(RV_Render *r,
203  const UT_StringHolder &map_name,
204  const UT_StringHolder &relative_to_node,
205  const RV_TextureParms &tex_parms,
206  RV_ImageDim tex_type,
207  bool force_update,
208  bool deferred_load,
209  bool &defer_loaded,
210  int &op_id,
211  bool &missing_texture,
213 
214  static RV_VKImage *getPlaceholder(RV_Render *r, RV_ImageDim tex_type,
215  PlaceholderType type =MISSING_WHITE_SOLID,
216  const UT_Vector4F *fallbackcolor =nullptr);
217 
218  RV_TextureCache::Image privGetTexture(exint id);
219 
220  virtual ~RV_TextureCache();
221 
222 private:
223 
225  UT_Map<exint, RV_ImageDim> myTypeMap;
226  UT_StringMap<exint> myResolveMap;
227 
229  UT_Array< UT_UniquePtr<RV_VKImage> > myPlaceholders;
230  UT_Array< UT_Map<int32, RV_VKImagePtr > > myFallbackTextures;
231 
232  exint myCurrentSize = 0;
233  //TODO:
234  //exint myMaxSize = 0;
235 };
236 
237 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
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:182
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
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 ref
Definition: glcorearb.h:124
UT_Array< UT_UniquePtr< PXL_Raster > > rasters
#define RV_INVALID_OP_ID
#define RV_API
Definition: RV_API.h:10
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
HUSD_API const char * resolution()
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:38
static const char * getFileSeparator()
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:277
GLint GLsizei width
Definition: glcorearb.h:103
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:117
type
Definition: core.h:1059
UT_UniquePtr< RV_VKImage > image