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