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 
28 class RV_Render;
30 class RV_TextureResolver;
31 class RV_Instance;
32 
33 /// Global cache of vulkan textures sourced from files.
35 {
36 public:
37  /// Initialize the cache (only used by the system during startup)
38  static void init();
39  /// Destroy the cache (only used by the system during exit)
40  static void cleanup();
41 
42  /// Clear all images out of the cache.
43  static void clear();
44  /// Mark any textures as dirty if their file source has been modified
45  static void updateTextures();
46 
47  /// The serial version of the cache. Incremented when a texture is added
48  static exint cacheVersion();
49  /// The serial refresh count of the cache. Incremented when the user
50  /// explicity updates the textures
51  static exint cacheRefresh();
52 
53  /// If the texture is missing, one of these generic placeholders is used.
55  {
56  MISSING_BLACK_CLEAR, // 0,0,0,0
57  MISSING_BLACK_SOLID, // 0,0,0,1
58  MISSING_WHITE_SOLID, // 1,1,1,1
59  MISSING_RED_SOLID, // 1,0,0,1
60  MISSING_NORMAL_MAP, // 0.5, 0.5, 1, 0
61  MISSING_FALLBACK, // custom color
62 
63  MAX_PLACEHOLDER
64  };
65 
66  /// Load the map (if needed) and bind the resulting texture to 'sampler_name'
67  /// in 'set'. If it cannot be loaded, bind a placeholder texture instead.
68  /// This method is equivalent to calling loadTextureRef() and then
69  /// bindTextureRef() or bindPlaceholder(). If the map is a reference to a
70  /// mofr with op:, op_id will contain its unique ID.
71  static bool bindTexture(RV_Render *r,
72  RV_TextureRef &map,
73  const RV_TextureParms &map_parms,
74  const UT_StringHolder &map_name,
75  const UT_StringHolder &relative_to_node,
76  RV_ImageDim tex_type,
78  const UT_StringHolder &sampler_name,
79  bool deferred_load,
80  bool &deferred_loaded,
81  int &op_id,
82  PlaceholderType missing=MISSING_WHITE_SOLID,
83  UT_Vector4F *fallback_color = nullptr);
84 
85  /// Binds an existing texture ref to the set at 'sampler_name'. It should not
86  /// be a placeholder image (use bindPlaceholder instead).
87  static bool bindTextureRef(RV_Render *r,
90  const UT_StringHolder &sampler_name);
91 
92  /// Bind a small placeholder texture (1x1) to 'sampler_name' in 'set'.
93  /// The RGBA texture color is defined by the placeholder_type, or can be a
94  /// custom/ color if the type is FALLBACK. The texture is 8b unorm RGBA.
95  static bool bindPlaceholder(RV_Render *r,
96  RV_ImageDim tex_type,
98  const UT_StringHolder &sampler_name,
99  PlaceholderType placeholder_type,
100  const UT_Vector4F *fallbackcolor=nullptr);
101 
102  /// Load the map info a texture ref without binding it to a shader. If the
103  /// map could not be loaded, map.isPlaceholder() will be true.
104  /// If allow_deferred_load is true, background load the texture. Keep calling
105  /// this in subsequent redraws until defer_load_finished is true.
106  static bool loadTextureRef(RV_Render *r,
107  RV_TextureRef &map,
108  const RV_TextureParms &map_parms,
109  const UT_StringHolder &map_name,
110  const UT_StringHolder &relative_to_node,
111  RV_ImageDim tex_type,
112  bool allow_deferred_load,
113  bool &defer_load_finished,
114  int &op_id,
115  UT_Vector3i *resolution = nullptr);
116 
117  /// Check if the texture is out of date. Flag the ref and set `dirty` if so.
118  /// doesn't touch either if it's current.
119  static void checkTextureDirty(RV_TextureRef &map, bool &dirty);
120 
121  /// Check if the texture has an alpha channel
122  static void checkTextureHasAlpha(const RV_TextureRef &ref, bool &has_alpha);
123 
124  /// For resolving a bunch of files to a 2D array, this is the separator
125  /// character between the filenames.
126  static const char * getFileSeparator() { return "\x1f"; }
127 
128  /// @internal
130  {
131  public:
132  ImageInfo();
133 
138  int udim_x = 0;
139  int udim_y = 0;
140  int udim_w = 0;
141  int udim_h = 0;
142  int width = 1;
143  int height = 1;
144  int depth = 1;
145  bool alpha = false;
146  time_t modtime = 0;
147  int op_id = 0;
148  int version = 0;
149  bool valid = false;
150  bool loading = false;
151  };
152 
153 protected:
154  class Image
155  {
156  public:
157  RV_VKImage *image = nullptr;
158  int udim_x = 0;
159  int udim_y = 0;
160  int udim_w = 0;
161  int udim_h = 0;
162  };
163 
164  static RV_TextureCache::Image getTexture(exint id);
165 
166  exint resolveTexture(RV_Render *r,
167  const UT_StringHolder &map_name,
168  const UT_StringHolder &relative_to_node,
169  const RV_TextureParms &tex_parms,
170  RV_ImageDim tex_type,
171  bool force_update,
172  bool deferred_load,
173  bool &defer_loaded,
174  int &op_id,
175  bool &missing_texture,
177 
178  static RV_VKImage *getPlaceholder(RV_Render *r, RV_ImageDim tex_type,
179  PlaceholderType type =MISSING_WHITE_SOLID,
180  const UT_Vector4F *fallbackcolor =nullptr);
181 
182  RV_TextureCache::Image privGetTexture(exint id);
183 
184  virtual ~RV_TextureCache();
185 
186 private:
187 
189  UT_Map<exint, RV_ImageDim> myTypeMap;
190  UT_StringMap<exint> myResolveMap;
191 
193  UT_Array< UT_UniquePtr<RV_VKImage> > myPlaceholders;
195 
196  exint myCurrentSize = 0;
197  //TODO:
198  //exint myMaxSize = 0;
199 };
200 
201 #endif
A collection of Vulkan UBO, SSBO, and Image shader bindings (descriptor set)
int64 exint
Definition: SYS_Types.h:125
Opaque reference to a texture stored in the RV_TextureCache.
Definition: RV_Type.h:175
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_API
Definition: RV_API.h:10
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
HUSD_API const char * resolution()
PlaceholderType
If the texture is missing, one of these generic placeholders is used.
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:36
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.
GLint GLsizei width
Definition: glcorearb.h:103
GLboolean r
Definition: glcorearb.h:1222
RV_ImageDim
Definition: RV_Type.h:111
type
Definition: core.h:1059
UT_UniquePtr< RV_VKImage > image