Blender  V2.93
cycles/render/image.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __IMAGE_H__
18 #define __IMAGE_H__
19 
20 #include "device/device_memory.h"
21 
22 #include "render/colorspace.h"
23 
24 #include "util/util_string.h"
25 #include "util/util_thread.h"
26 #include "util/util_transform.h"
27 #include "util/util_unique_ptr.h"
28 #include "util/util_vector.h"
29 
31 
32 class Device;
33 class DeviceInfo;
34 class ImageHandle;
35 class ImageKey;
36 class ImageMetaData;
37 class ImageManager;
38 class Progress;
39 class RenderStats;
40 class Scene;
41 class ColorSpaceProcessor;
42 class VDBImageLoader;
43 
44 /* Image Parameters */
45 class ImageParams {
46  public:
47  bool animated;
51  ustring colorspace;
52  float frame;
53 
55  : animated(false),
60  frame(0.0f)
61  {
62  }
63 
64  bool operator==(const ImageParams &other) const
65  {
66  return (animated == other.animated && interpolation == other.interpolation &&
67  extension == other.extension && alpha_type == other.alpha_type &&
68  colorspace == other.colorspace && frame == other.frame);
69  }
70 };
71 
72 /* Image MetaData
73  *
74  * Information about the image that is available before the image pixels are loaded. */
76  public:
77  /* Set by ImageLoader.load_metadata(). */
78  int channels;
79  size_t width, height, depth;
80  size_t byte_size;
82 
83  /* Optional color space, defaults to raw. */
84  ustring colorspace;
86 
87  /* Optional transform for 3D images. */
90 
91  /* Automatically set. */
93 
94  ImageMetaData();
95  bool operator==(const ImageMetaData &other) const;
96  bool is_float() const;
97  void detect_colorspace();
98 };
99 
100 /* Information about supported features that Image loaders can use. */
102  public:
105 };
106 
107 /* Image loader base class, that can be subclassed to load image data
108  * from custom sources (file, memory, procedurally generated, etc). */
109 class ImageLoader {
110  public:
111  ImageLoader();
112  virtual ~ImageLoader(){};
113 
114  /* Load metadata without actual image yet, should be fast. */
115  virtual bool load_metadata(const ImageDeviceFeatures &features, ImageMetaData &metadata) = 0;
116 
117  /* Load actual image contents. */
118  virtual bool load_pixels(const ImageMetaData &metadata,
119  void *pixels,
120  const size_t pixels_size,
121  const bool associate_alpha) = 0;
122 
123  /* Name for logs and stats. */
124  virtual string name() const = 0;
125 
126  /* Optional for OSL texture cache. */
127  virtual ustring osl_filepath() const;
128 
129  /* Free any memory used for loading metadata and pixels. */
130  virtual void cleanup(){};
131 
132  /* Compare avoid loading the same image multiple times. */
133  virtual bool equals(const ImageLoader &other) const = 0;
134  static bool equals(const ImageLoader *a, const ImageLoader *b);
135 
136  virtual bool is_vdb_loader() const;
137 
138  /* Work around for no RTTI. */
139 };
140 
141 /* Image Handle
142  *
143  * Access handle for image in the image manager. Multiple shader nodes may
144  * share the same image, and this class handles reference counting for that. */
145 class ImageHandle {
146  public:
147  ImageHandle();
148  ImageHandle(const ImageHandle &other);
149  ImageHandle &operator=(const ImageHandle &other);
150  ~ImageHandle();
151 
152  bool operator==(const ImageHandle &other) const;
153 
154  void clear();
155 
156  bool empty();
157  int num_tiles();
158 
160  int svm_slot(const int tile_index = 0) const;
161  device_texture *image_memory(const int tile_index = 0) const;
162 
163  VDBImageLoader *vdb_loader(const int tile_index = 0) const;
164 
165  protected:
168 
169  friend class ImageManager;
170 };
171 
172 /* Image Manager
173  *
174  * Handles loading and storage of all images in the scene. This includes 2D
175  * texture images and 3D volume images. */
177  public:
178  explicit ImageManager(const DeviceInfo &info);
179  ~ImageManager();
180 
181  ImageHandle add_image(const string &filename, const ImageParams &params);
182  ImageHandle add_image(const string &filename,
183  const ImageParams &params,
184  const array<int> &tiles);
185  ImageHandle add_image(ImageLoader *loader, const ImageParams &params, const bool builtin = true);
186 
187  void device_update(Device *device, Scene *scene, Progress &progress);
188  void device_update_slot(Device *device, Scene *scene, int slot, Progress *progress);
189  void device_free(Device *device);
190 
191  void device_load_builtin(Device *device, Scene *scene, Progress &progress);
192  void device_free_builtin(Device *device);
193 
194  void set_osl_texture_system(void *texture_system);
195  bool set_animation_frame_update(int frame);
196 
197  void collect_statistics(RenderStats *stats);
198 
199  void tag_update();
200 
201  bool need_update() const;
202 
203  struct Image {
207 
208  float frame;
210  bool need_load;
211  bool builtin;
212 
213  string mem_name;
215 
216  int users;
218  };
219 
220  private:
221  bool need_update_;
222 
223  ImageDeviceFeatures features;
224 
225  thread_mutex device_mutex;
226  thread_mutex images_mutex;
227  int animation_frame;
228 
229  vector<Image *> images;
230  void *osl_texture_system;
231 
232  int add_image_slot(ImageLoader *loader, const ImageParams &params, const bool builtin);
233  void add_image_user(int slot);
234  void remove_image_user(int slot);
235 
236  void load_image_metadata(Image *img);
237 
238  template<TypeDesc::BASETYPE FileFormat, typename StorageType>
239  bool file_load_image(Image *img, int texture_limit);
240 
241  void device_load_image(Device *device, Scene *scene, int slot, Progress *progress);
242  void device_free_image(Device *device, int slot);
243 
244  friend class ImageHandle;
245 };
246 
248 
249 #endif /* __IMAGE_H__ */
Definition: device.h:293
~ImageHandle()
Definition: image.cpp:118
bool operator==(const ImageHandle &other) const
Definition: image.cpp:205
vector< int > tile_slots
VDBImageLoader * vdb_loader(const int tile_index=0) const
Definition: image.cpp:180
ImageHandle()
Definition: image.cpp:92
void clear()
Definition: image.cpp:123
device_texture * image_memory(const int tile_index=0) const
Definition: image.cpp:170
bool empty()
Definition: image.cpp:133
int num_tiles()
Definition: image.cpp:138
ImageManager * manager
ImageHandle & operator=(const ImageHandle &other)
Definition: image.cpp:105
ImageMetaData metadata()
Definition: image.cpp:143
int svm_slot(const int tile_index=0) const
Definition: image.cpp:154
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
Definition: image.cpp:277
virtual string name() const =0
virtual bool is_vdb_loader() const
Definition: image.cpp:292
ImageLoader()
Definition: image.cpp:273
bool set_animation_frame_update(int frame)
Definition: image.cpp:321
void tag_update()
Definition: image.cpp:895
~ImageManager()
Definition: image.cpp:310
ImageManager(const DeviceInfo &info)
Definition: image.cpp:299
void device_update(Device *device, Scene *scene, Progress &progress)
Definition: image.cpp:807
bool need_update() const
Definition: image.cpp:900
void device_free(Device *device)
Definition: image.cpp:879
void device_load_builtin(Device *device, Scene *scene, Progress &progress)
Definition: image.cpp:849
void device_free_builtin(Device *device)
Definition: image.cpp:869
ImageHandle add_image(const string &filename, const ImageParams &params)
Definition: image.cpp:368
void device_update_slot(Device *device, Scene *scene, int slot, Progress *progress)
Definition: image.cpp:836
void collect_statistics(RenderStats *stats)
Definition: image.cpp:887
void set_osl_texture_system(void *texture_system)
Definition: image.cpp:316
ImageDataType type
bool is_float() const
Definition: image.cpp:234
Transform transform_3d
void detect_colorspace()
Definition: image.cpp:240
bool operator==(const ImageMetaData &other) const
Definition: image.cpp:226
const char * colorspace_file_format
bool operator==(const ImageParams &other) const
ImageAlphaType alpha_type
InterpolationType interpolation
ExtensionType extension
ustring u_colorspace_raw
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define CCL_NAMESPACE_END
static unsigned a[3]
Definition: RandGen.cpp:92
ImageDataType
Definition: util_texture.h:51
ImageAlphaType
Definition: util_texture.h:68
@ IMAGE_ALPHA_AUTO
Definition: util_texture.h:73
InterpolationType
Definition: util_texture.h:38
@ INTERPOLATION_LINEAR
Definition: util_texture.h:40
ExtensionType
Definition: util_texture.h:84
@ EXTENSION_CLIP
Definition: util_texture.h:90
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: util_thread.h:40