Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / render / image.h
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.h"
21 #include "device/device_memory.h"
22
23 #include "util/util_image.h"
24 #include "util/util_string.h"
25 #include "util/util_thread.h"
26 #include "util/util_vector.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 class Device;
31 class DeviceScene;
32 class Progress;
33 class Scene;
34
35 class ImageManager {
36 public:
37         explicit ImageManager(const DeviceInfo& info);
38         ~ImageManager();
39
40         int add_image(const string& filename,
41                       void *builtin_data,
42                       bool animated,
43                       float frame,
44                       bool& is_float,
45                       bool& is_linear,
46                       InterpolationType interpolation,
47                       ExtensionType extension,
48                       bool use_alpha);
49         void remove_image(int flat_slot);
50         void remove_image(const string& filename,
51                           void *builtin_data,
52                           InterpolationType interpolation,
53                           ExtensionType extension,
54                           bool use_alpha);
55         void tag_reload_image(const string& filename,
56                               void *builtin_data,
57                               InterpolationType interpolation,
58                               ExtensionType extension,
59                               bool use_alpha);
60         ImageDataType get_image_metadata(const string& filename, void *builtin_data, bool& is_linear);
61
62         void device_prepare_update(DeviceScene *dscene);
63         void device_update(Device *device,
64                            DeviceScene *dscene,
65                            Scene *scene,
66                            Progress& progress);
67         void device_update_slot(Device *device,
68                                 DeviceScene *dscene,
69                                 Scene *scene,
70                                 int flat_slot,
71                                 Progress *progress);
72         void device_free(Device *device, DeviceScene *dscene);
73         void device_free_builtin(Device *device, DeviceScene *dscene);
74
75         void set_osl_texture_system(void *texture_system);
76         void set_pack_images(bool pack_images_);
77         bool set_animation_frame_update(int frame);
78
79         bool need_update;
80
81         /* NOTE: Here pixels_size is a size of storage, which equals to
82          *       width * height * depth.
83          *       Use this to avoid some nasty memory corruptions.
84          */
85         function<void(const string &filename,
86                       void *data,
87                       bool &is_float,
88                       int &width,
89                       int &height,
90                       int &depth,
91                       int &channels)> builtin_image_info_cb;
92         function<bool(const string &filename,
93                       void *data,
94                       unsigned char *pixels,
95                       const size_t pixels_size)> builtin_image_pixels_cb;
96         function<bool(const string &filename,
97                       void *data,
98                       float *pixels,
99                       const size_t pixels_size)> builtin_image_float_pixels_cb;
100
101         struct Image {
102                 string filename;
103                 void *builtin_data;
104
105                 bool use_alpha;
106                 bool need_load;
107                 bool animated;
108                 float frame;
109                 InterpolationType interpolation;
110                 ExtensionType extension;
111
112                 int users;
113         };
114
115 private:
116         int tex_num_images[IMAGE_DATA_NUM_TYPES];
117         int max_num_images;
118         bool has_half_images;
119         bool cuda_fermi_limits;
120
121         thread_mutex device_mutex;
122         int animation_frame;
123
124         vector<Image*> images[IMAGE_DATA_NUM_TYPES];
125         void *osl_texture_system;
126         bool pack_images;
127
128         bool file_load_image_generic(Image *img, ImageInput **in, int &width, int &height, int &depth, int &components);
129
130         template<TypeDesc::BASETYPE FileFormat,
131                  typename StorageType,
132                  typename DeviceType>
133         bool file_load_image(Image *img,
134                              ImageDataType type,
135                              int texture_limit,
136                              device_vector<DeviceType>& tex_img);
137
138         int max_flattened_slot(ImageDataType type);
139         int type_index_to_flattened_slot(int slot, ImageDataType type);
140         int flattened_slot_to_type_index(int flat_slot, ImageDataType *type);
141         string name_from_type(int type);
142
143         uint8_t pack_image_options(ImageDataType type, size_t slot);
144
145         void device_load_image(Device *device,
146                                DeviceScene *dscene,
147                                Scene *scene,
148                                ImageDataType type,
149                                int slot,
150                                Progress *progess);
151         void device_free_image(Device *device,
152                                DeviceScene *dscene,
153                                ImageDataType type,
154                                int slot);
155
156         template<typename T>
157         void device_pack_images_type(
158                 ImageDataType type,
159                 const vector<device_vector<T>*>& cpu_textures,
160                 device_vector<T> *device_image,
161                 uint4 *info);
162
163         void device_pack_images(Device *device,
164                                 DeviceScene *dscene,
165                                 Progress& progess);
166 };
167
168 CCL_NAMESPACE_END
169
170 #endif /* __IMAGE_H__ */
171