Blender  V3.3
cycles/scene/image.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __IMAGE_H__
5 #define __IMAGE_H__
6 
7 #include "device/memory.h"
8 
9 #include "scene/colorspace.h"
10 
11 #include "util/string.h"
12 #include "util/thread.h"
13 #include "util/transform.h"
14 #include "util/unique_ptr.h"
15 #include "util/vector.h"
16 
18 
19 class Device;
20 class DeviceInfo;
21 class ImageHandle;
22 class ImageKey;
23 class ImageMetaData;
24 class ImageManager;
25 class Progress;
26 class RenderStats;
27 class Scene;
28 class ColorSpaceProcessor;
29 class VDBImageLoader;
30 
31 /* Image Parameters */
32 class ImageParams {
33  public:
34  bool animated;
38  ustring colorspace;
39  float frame;
40 
42  : animated(false),
47  frame(0.0f)
48  {
49  }
50 
51  bool operator==(const ImageParams &other) const
52  {
53  return (animated == other.animated && interpolation == other.interpolation &&
54  extension == other.extension && alpha_type == other.alpha_type &&
55  colorspace == other.colorspace && frame == other.frame);
56  }
57 };
58 
59 /* Image MetaData
60  *
61  * Information about the image that is available before the image pixels are loaded. */
63  public:
64  /* Set by ImageLoader.load_metadata(). */
65  int channels;
66  size_t width, height, depth;
67  size_t byte_size;
69 
70  /* Optional color space, defaults to raw. */
71  ustring colorspace;
73 
74  /* Optional transform for 3D images. */
77 
78  /* Automatically set. */
80 
81  ImageMetaData();
82  bool operator==(const ImageMetaData &other) const;
83  bool is_float() const;
84  void detect_colorspace();
85 };
86 
87 /* Information about supported features that Image loaders can use. */
89  public:
91 };
92 
93 /* Image loader base class, that can be subclassed to load image data
94  * from custom sources (file, memory, procedurally generated, etc). */
95 class ImageLoader {
96  public:
97  ImageLoader();
98  virtual ~ImageLoader(){};
99 
100  /* Load metadata without actual image yet, should be fast. */
101  virtual bool load_metadata(const ImageDeviceFeatures &features, ImageMetaData &metadata) = 0;
102 
103  /* Load actual image contents. */
104  virtual bool load_pixels(const ImageMetaData &metadata,
105  void *pixels,
106  const size_t pixels_size,
107  const bool associate_alpha) = 0;
108 
109  /* Name for logs and stats. */
110  virtual string name() const = 0;
111 
112  /* Optional for OSL texture cache. */
113  virtual ustring osl_filepath() const;
114 
115  /* Optional for tiled textures loaded externally. */
116  virtual int get_tile_number() const;
117 
118  /* Free any memory used for loading metadata and pixels. */
119  virtual void cleanup(){};
120 
121  /* Compare avoid loading the same image multiple times. */
122  virtual bool equals(const ImageLoader &other) const = 0;
123  static bool equals(const ImageLoader *a, const ImageLoader *b);
124 
125  virtual bool is_vdb_loader() const;
126 
127  /* Work around for no RTTI. */
128 };
129 
130 /* Image Handle
131  *
132  * Access handle for image in the image manager. Multiple shader nodes may
133  * share the same image, and this class handles reference counting for that. */
134 class ImageHandle {
135  public:
136  ImageHandle();
137  ImageHandle(const ImageHandle &other);
138  ImageHandle &operator=(const ImageHandle &other);
139  ~ImageHandle();
140 
141  bool operator==(const ImageHandle &other) const;
142 
143  void clear();
144 
145  bool empty() const;
146  int num_tiles() const;
147 
149  int svm_slot(const int tile_index = 0) const;
150  vector<int4> get_svm_slots() const;
151  device_texture *image_memory(const int tile_index = 0) const;
152 
153  VDBImageLoader *vdb_loader(const int tile_index = 0) const;
154 
155  protected:
158 
159  friend class ImageManager;
160 };
161 
162 /* Image Manager
163  *
164  * Handles loading and storage of all images in the scene. This includes 2D
165  * texture images and 3D volume images. */
167  public:
168  explicit ImageManager(const DeviceInfo &info);
169  ~ImageManager();
170 
171  ImageHandle add_image(const string &filename, const ImageParams &params);
172  ImageHandle add_image(const string &filename,
173  const ImageParams &params,
174  const array<int> &tiles);
175  ImageHandle add_image(ImageLoader *loader, const ImageParams &params, const bool builtin = true);
177 
178  void device_update(Device *device, Scene *scene, Progress &progress);
179  void device_update_slot(Device *device, Scene *scene, int slot, Progress *progress);
180  void device_free(Device *device);
181 
182  void device_load_builtin(Device *device, Scene *scene, Progress &progress);
183  void device_free_builtin(Device *device);
184 
185  void set_osl_texture_system(void *texture_system);
186  bool set_animation_frame_update(int frame);
187 
188  void collect_statistics(RenderStats *stats);
189 
190  void tag_update();
191 
192  bool need_update() const;
193 
194  struct Image {
198 
199  float frame;
201  bool need_load;
202  bool builtin;
203 
204  string mem_name;
206 
207  int users;
209  };
210 
211  private:
212  bool need_update_;
213 
214  ImageDeviceFeatures features;
215 
216  thread_mutex device_mutex;
217  thread_mutex images_mutex;
218  int animation_frame;
219 
220  vector<Image *> images;
221  void *osl_texture_system;
222 
223  int add_image_slot(ImageLoader *loader, const ImageParams &params, const bool builtin);
224  void add_image_user(int slot);
225  void remove_image_user(int slot);
226 
227  void load_image_metadata(Image *img);
228 
229  template<TypeDesc::BASETYPE FileFormat, typename StorageType>
230  bool file_load_image(Image *img, int texture_limit);
231 
232  void device_load_image(Device *device, Scene *scene, int slot, Progress *progress);
233  void device_free_image(Device *device, int slot);
234 
235  friend class ImageHandle;
236 };
237 
239 
240 #endif /* __IMAGE_H__ */
int num_tiles() const
bool operator==(const ImageHandle &other) const
vector< int > tile_slots
VDBImageLoader * vdb_loader(const int tile_index=0) const
device_texture * image_memory(const int tile_index=0) const
bool empty() const
ImageManager * manager
ImageHandle & operator=(const ImageHandle &other)
Definition: scene/image.cpp:96
ImageMetaData metadata()
int svm_slot(const int tile_index=0) const
vector< int4 > get_svm_slots() const
virtual bool load_pixels(const ImageMetaData &metadata, void *pixels, const size_t pixels_size, const bool associate_alpha)=0
virtual bool load_metadata(const ImageDeviceFeatures &features, ImageMetaData &metadata)=0
virtual void cleanup()
virtual bool equals(const ImageLoader &other) const =0
virtual ~ImageLoader()
virtual ustring osl_filepath() const
virtual int get_tile_number() const
virtual string name() const =0
virtual bool is_vdb_loader() const
bool set_animation_frame_update(int frame)
void tag_update()
ImageManager(const DeviceInfo &info)
void device_update(Device *device, Scene *scene, Progress &progress)
bool need_update() const
void device_free(Device *device)
void device_load_builtin(Device *device, Scene *scene, Progress &progress)
void device_free_builtin(Device *device)
ImageHandle add_image(const string &filename, const ImageParams &params)
void device_update_slot(Device *device, Scene *scene, int slot, Progress *progress)
void collect_statistics(RenderStats *stats)
void set_osl_texture_system(void *texture_system)
ImageDataType type
bool is_float() const
Transform transform_3d
void detect_colorspace()
bool operator==(const ImageMetaData &other) const
const char * colorspace_file_format
bool operator==(const ImageParams &other) const
ImageAlphaType alpha_type
InterpolationType interpolation
ExtensionType extension
ustring u_colorspace_raw
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
const int tile_index
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
device_texture * mem
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27
ImageDataType
Definition: util/texture.h:29
ImageAlphaType
Definition: util/texture.h:48
@ IMAGE_ALPHA_AUTO
Definition: util/texture.h:53
InterpolationType
Definition: util/texture.h:19
@ INTERPOLATION_LINEAR
Definition: util/texture.h:21
ExtensionType
Definition: util/texture.h:61
@ EXTENSION_CLIP
Definition: util/texture.h:67