Cleanup: specify default studiolight name, instead using first alphabetically.
[blender.git] / source / blender / blenkernel / intern / studiolight.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  *
27  */
28
29 /** \file blender/blenkernel/intern/studiolight.c
30  *  \ingroup bke
31  */
32
33 #include "BKE_studiolight.h"
34
35 #include "BKE_appdir.h"
36 #include "BKE_icons.h"
37
38 #include "BLI_dynstr.h"
39 #include "BLI_fileops.h"
40 #include "BLI_fileops_types.h"
41 #include "BLI_listbase.h"
42 #include "BLI_linklist.h"
43 #include "BLI_math.h"
44 #include "BLI_math_color.h"
45 #include "BLI_path_util.h"
46 #include "BLI_rand.h"
47 #include "BLI_string.h"
48 #include "BLI_string_utils.h"
49
50 #include "DNA_listBase.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "GPU_texture.h"
56
57 #include "MEM_guardedalloc.h"
58
59
60 /* Statics */
61 static ListBase studiolights;
62 static int last_studiolight_id = 0;
63 #define STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE 96
64 #define STUDIOLIGHT_IRRADIANCE_EQUIRECT_HEIGHT 32
65 #define STUDIOLIGHT_IRRADIANCE_EQUIRECT_WIDTH (STUDIOLIGHT_IRRADIANCE_EQUIRECT_HEIGHT * 2)
66
67 /*
68  * The method to calculate the irradiance buffers
69  * The irradiance buffer is only shown in the background when in LookDev.
70  *
71  * STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE is very slow, but very accurate
72  * STUDIOLIGHT_IRRADIANCE_METHOD_SPHERICAL_HARMONICS is faster but has artifacts
73  * Cannot have both enabled at the same time!!!
74  */
75 // #define STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
76 #define STUDIOLIGHT_IRRADIANCE_METHOD_SPHERICAL_HARMONICS
77
78 /* Temporarily disabled due to the creation of textures with -nan(ind)s */
79 #define STUDIOLIGHT_SH_WINDOWING 0.0f /* 0.0 is disabled */
80
81 /*
82  * Disable this option so caches are not loaded from disk
83  * Do not checkin with this commented out
84  */
85 #define STUDIOLIGHT_LOAD_CACHED_FILES
86
87 static const char *STUDIOLIGHT_LIGHTS_FOLDER = "studiolights/studio/";
88 static const char *STUDIOLIGHT_WORLD_FOLDER = "studiolights/world/";
89 static const char *STUDIOLIGHT_MATCAP_FOLDER = "studiolights/matcap/";
90
91 static const char *STUDIOLIGHT_WORLD_DEFAULT = "forest.exr";
92 static const char *STUDIOLIGHT_MATCAP_DEFAULT = "basic_1.exr";
93
94 /* ITER MACRO */
95
96 /** Iter on all pixel giving texel center position and pixel pointer.
97  * Arguments
98  *   type : type of src.
99  *   src : source buffer.
100  *   channels : number of channels per pixel.
101  *
102  * Others
103  *   x, y : normalized UV coordinate [0..1] of the current pixel center.
104  *   texel_size[2] : UV size of a pixel in this texture.
105  *   pixel[] : pointer to the current pixel.
106  */
107 #define ITER_PIXELS(type, src, channels, width, height)                                \
108 {                                                                                      \
109         float texel_size[2];                                                               \
110         texel_size[0] = 1.0f / width;                                                      \
111         texel_size[1] = 1.0f / height;                                                     \
112         type (*pixel_)[channels] = (type (*)[channels])src;                                \
113         for (float y = 0.5 * texel_size[1]; y < 1.0; y += texel_size[1]) {                 \
114                 for (float x = 0.5 * texel_size[0]; x < 1.0; x += texel_size[0], pixel_++) {   \
115                         type *pixel = *pixel_;
116
117 #define ITER_PIXELS_END                                                              \
118                 }                                                                            \
119         }                                                                                \
120 } ((void)0)
121
122 /* FUNCTIONS */
123 #define IMB_SAFE_FREE(p) do { \
124 if (p) {                      \
125         IMB_freeImBuf(p);         \
126         p = NULL;                 \
127 }                             \
128 } while (0)
129
130 #define GPU_TEXTURE_SAFE_FREE(p) do { \
131 if (p) {                              \
132         GPU_texture_free(p);              \
133         p = NULL;                         \
134 }                                     \
135 } while (0)
136
137 static void studiolight_free(struct StudioLight *sl)
138 {
139 #define STUDIOLIGHT_DELETE_ICON(s) {  \
140         if (s != 0) {                          \
141                 BKE_icon_delete(s);           \
142                 s = 0;                        \
143         }                                 \
144 }
145         if (sl->free_function) {
146                 sl->free_function(sl, sl->free_function_data);
147         }
148         STUDIOLIGHT_DELETE_ICON(sl->icon_id_radiance);
149         STUDIOLIGHT_DELETE_ICON(sl->icon_id_irradiance);
150         STUDIOLIGHT_DELETE_ICON(sl->icon_id_matcap);
151         STUDIOLIGHT_DELETE_ICON(sl->icon_id_matcap_flipped);
152 #undef STUDIOLIGHT_DELETE_ICON
153
154         for (int index = 0; index < 6; index++) {
155                 IMB_SAFE_FREE(sl->radiance_cubemap_buffers[index]);
156         }
157         GPU_TEXTURE_SAFE_FREE(sl->equirect_radiance_gputexture);
158         GPU_TEXTURE_SAFE_FREE(sl->equirect_irradiance_gputexture);
159         IMB_SAFE_FREE(sl->equirect_radiance_buffer);
160         IMB_SAFE_FREE(sl->equirect_irradiance_buffer);
161         MEM_SAFE_FREE(sl->path_irr_cache);
162         MEM_SAFE_FREE(sl->path_sh_cache);
163         MEM_SAFE_FREE(sl);
164 }
165
166 static struct StudioLight *studiolight_create(int flag)
167 {
168         struct StudioLight *sl = MEM_callocN(sizeof(*sl), __func__);
169         sl->path[0] = 0x00;
170         sl->name[0] = 0x00;
171         sl->path_irr_cache = NULL;
172         sl->path_sh_cache = NULL;
173         sl->free_function = NULL;
174         sl->flag = flag;
175         sl->index = ++last_studiolight_id;
176         if (flag & STUDIOLIGHT_TYPE_STUDIO) {
177                 sl->icon_id_irradiance = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE);
178         }
179         else if (flag & STUDIOLIGHT_TYPE_MATCAP) {
180                 sl->icon_id_matcap = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP);
181                 sl->icon_id_matcap_flipped = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED);
182         }
183         else {
184                 sl->icon_id_radiance = BKE_icon_ensure_studio_light(sl, STUDIOLIGHT_ICON_ID_TYPE_RADIANCE);
185         }
186
187         for (int index = 0; index < 6; index++) {
188                 sl->radiance_cubemap_buffers[index] = NULL;
189         }
190
191         return sl;
192 }
193
194 #define STUDIOLIGHT_FILE_VERSION 1
195
196 #define READ_VAL(type, parser, id, val, lines) do { \
197         for (LinkNode *line = lines; line; line = line->next) { \
198                 char *val_str, *str = line->link; \
199                 if ((val_str = strstr(str, id " "))) { \
200                         val_str += sizeof(id); /* Skip id + spacer. */ \
201                         val = parser(val_str); \
202                 } \
203         } \
204 } while (0)
205
206 #define READ_FVAL(id, val, lines) READ_VAL(float, atof, id, val, lines)
207 #define READ_IVAL(id, val, lines) READ_VAL(int, atoi, id, val, lines)
208
209 #define READ_VEC3(id, val, lines) do { \
210         READ_FVAL(id ".x", val[0], lines); \
211         READ_FVAL(id ".y", val[1], lines); \
212         READ_FVAL(id ".z", val[2], lines); \
213 } while (0)
214
215 #define READ_SOLIDLIGHT(sl, i, lines) do { \
216         READ_IVAL("light[" STRINGIFY(i) "].flag", sl[i].flag, lines); \
217         READ_FVAL("light[" STRINGIFY(i) "].smooth", sl[i].smooth, lines); \
218         READ_VEC3("light[" STRINGIFY(i) "].col", sl[i].col, lines); \
219         READ_VEC3("light[" STRINGIFY(i) "].spec", sl[i].spec, lines); \
220         READ_VEC3("light[" STRINGIFY(i) "].vec", sl[i].vec, lines); \
221 } while (0)
222
223 static void studiolight_load_solid_light(StudioLight *sl)
224 {
225         LinkNode *lines = BLI_file_read_as_lines(sl->path);
226         if (lines) {
227                 READ_VEC3("light_ambient", sl->light_ambient, lines);
228                 READ_SOLIDLIGHT(sl->light, 0, lines);
229                 READ_SOLIDLIGHT(sl->light, 1, lines);
230                 READ_SOLIDLIGHT(sl->light, 2, lines);
231                 READ_SOLIDLIGHT(sl->light, 3, lines);
232         }
233         BLI_file_free_lines(lines);
234 }
235
236 #undef READ_SOLIDLIGHT
237 #undef READ_VEC3
238 #undef READ_IVAL
239 #undef READ_FVAL
240
241 #define WRITE_FVAL(str, id, val) (BLI_dynstr_appendf(str, id " %f\n", val))
242 #define WRITE_IVAL(str, id, val) (BLI_dynstr_appendf(str, id " %d\n", val))
243
244 #define WRITE_VEC3(str, id, val) do { \
245         WRITE_FVAL(str, id ".x", val[0]); \
246         WRITE_FVAL(str, id ".y", val[1]); \
247         WRITE_FVAL(str, id ".z", val[2]); \
248 } while (0)
249
250 #define WRITE_SOLIDLIGHT(str, sl, i) do { \
251         WRITE_IVAL(str, "light[" STRINGIFY(i) "].flag", sl[i].flag); \
252         WRITE_FVAL(str, "light[" STRINGIFY(i) "].smooth", sl[i].smooth); \
253         WRITE_VEC3(str, "light[" STRINGIFY(i) "].col", sl[i].col); \
254         WRITE_VEC3(str, "light[" STRINGIFY(i) "].spec", sl[i].spec); \
255         WRITE_VEC3(str, "light[" STRINGIFY(i) "].vec", sl[i].vec); \
256 } while (0)
257
258 static void studiolight_write_solid_light(StudioLight *sl)
259 {
260         FILE *fp = BLI_fopen(sl->path, "wb");
261         if (fp) {
262                 DynStr *str = BLI_dynstr_new();
263
264                 /* Very dumb ascii format. One value per line separated by a space. */
265                 WRITE_IVAL(str, "version", STUDIOLIGHT_FILE_VERSION);
266                 WRITE_VEC3(str, "light_ambient", sl->light_ambient);
267                 WRITE_SOLIDLIGHT(str, sl->light, 0);
268                 WRITE_SOLIDLIGHT(str, sl->light, 1);
269                 WRITE_SOLIDLIGHT(str, sl->light, 2);
270                 WRITE_SOLIDLIGHT(str, sl->light, 3);
271
272                 char *cstr = BLI_dynstr_get_cstring(str);
273
274                 fwrite(cstr, BLI_dynstr_get_len(str), 1, fp);
275                 fclose(fp);
276
277                 MEM_freeN(cstr);
278                 BLI_dynstr_free(str);
279         }
280 }
281
282 #undef WRITE_SOLIDLIGHT
283 #undef WRITE_VEC3
284 #undef WRITE_IVAL
285 #undef WRITE_FVAL
286
287 static void direction_to_equirect(float r[2], const float dir[3])
288 {
289         r[0] = (atan2f(dir[1], dir[0]) - M_PI) / -(M_PI * 2);
290         r[1] = (acosf(dir[2] / 1.0) - M_PI) / -M_PI;
291 }
292
293 static void equirect_to_direction(float r[3], float u, float v)
294 {
295         float phi = (-(M_PI * 2)) * u + M_PI;
296         float theta = -M_PI * v + M_PI;
297         float sin_theta = sinf(theta);
298         r[0] = sin_theta * cosf(phi);
299         r[1] = sin_theta * sinf(phi);
300         r[2] = cosf(theta);
301 }
302
303 static void UNUSED_FUNCTION(direction_to_cube_face_uv)(float r_uv[2], int *r_face, const float dir[3])
304 {
305         if (fabsf(dir[0]) > fabsf(dir[1]) && fabsf(dir[0]) > fabsf(dir[2])) {
306                 bool is_pos = (dir[0] > 0.0f);
307                 *r_face = is_pos ? STUDIOLIGHT_X_POS : STUDIOLIGHT_X_NEG;
308                 r_uv[0] = dir[2] / fabsf(dir[0]) * (is_pos ?  1 : -1);
309                 r_uv[1] = dir[1] / fabsf(dir[0]) * (is_pos ? -1 : -1);
310         }
311         else if (fabsf(dir[1]) > fabsf(dir[0]) && fabsf(dir[1]) > fabsf(dir[2])) {
312                 bool is_pos = (dir[1] > 0.0f);
313                 *r_face = is_pos ? STUDIOLIGHT_Y_POS : STUDIOLIGHT_Y_NEG;
314                 r_uv[0] = dir[0] / fabsf(dir[1]) * (is_pos ?  1 :  1);
315                 r_uv[1] = dir[2] / fabsf(dir[1]) * (is_pos ? -1 :  1);
316         }
317         else {
318                 bool is_pos = (dir[2] > 0.0f);
319                 *r_face = is_pos ? STUDIOLIGHT_Z_NEG : STUDIOLIGHT_Z_POS;
320                 r_uv[0] = dir[0] / fabsf(dir[2]) * (is_pos ? -1 :  1);
321                 r_uv[1] = dir[1] / fabsf(dir[2]) * (is_pos ? -1 : -1);
322         }
323         r_uv[0] = r_uv[0] * 0.5f + 0.5f;
324         r_uv[1] = r_uv[1] * 0.5f + 0.5f;
325 }
326
327 static void cube_face_uv_to_direction(float r_dir[3], float x, float y, int face)
328 {
329         const float conversion_matrices[6][3][3] = {
330                 {{ 0.0f, 0.0f,  1.0f}, {0.0f, -1.0f,  0.0f}, { 1.0f,  0.0f,  0.0f}},
331                 {{ 0.0f, 0.0f, -1.0f}, {0.0f, -1.0f,  0.0f}, {-1.0f,  0.0f,  0.0f}},
332                 {{ 1.0f, 0.0f,  0.0f}, {0.0f,  0.0f, -1.0f}, { 0.0f,  1.0f,  0.0f}},
333                 {{ 1.0f, 0.0f,  0.0f}, {0.0f,  0.0f,  1.0f}, { 0.0f, -1.0f,  0.0f}},
334                 {{ 1.0f, 0.0f,  0.0f}, {0.0f, -1.0f,  0.0f}, { 0.0f,  0.0f, -1.0f}},
335                 {{-1.0f, 0.0f,  0.0f}, {0.0f, -1.0f,  0.0f}, { 0.0f,  0.0f,  1.0f}}
336         };
337
338         copy_v3_fl3(r_dir, x * 2.0f - 1.0f, y * 2.0f - 1.0f, 1.0f);
339         mul_m3_v3(conversion_matrices[face], r_dir);
340         normalize_v3(r_dir);
341 }
342
343 static void studiolight_load_equirect_image(StudioLight *sl)
344 {
345         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
346                 ImBuf *ibuf = NULL;
347                 ibuf = IMB_loadiffname(sl->path, 0, NULL);
348                 if (ibuf == NULL) {
349                         float *colbuf = MEM_mallocN(sizeof(float[4]), __func__);
350                         copy_v4_fl4(colbuf, 1.0f, 0.0f, 1.0f, 1.0f);
351                         ibuf = IMB_allocFromBuffer(NULL, colbuf, 1, 1);
352                 }
353                 IMB_float_from_rect(ibuf);
354                 sl->equirect_radiance_buffer = ibuf;
355         }
356         sl->flag |= STUDIOLIGHT_EXTERNAL_IMAGE_LOADED;
357 }
358
359 static void studiolight_create_equirect_radiance_gputexture(StudioLight *sl)
360 {
361         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
362                 char error[256];
363                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
364                 ImBuf *ibuf = sl->equirect_radiance_buffer;
365
366                 if (sl->flag & STUDIOLIGHT_TYPE_MATCAP) {
367                         float *gpu_matcap_3components = MEM_callocN(sizeof(float[3]) * ibuf->x * ibuf->y, __func__);
368
369                         float (*offset4)[4] = (float (*)[4])ibuf->rect_float;
370                         float (*offset3)[3] = (float (*)[3])gpu_matcap_3components;
371                         for (int i = 0; i < ibuf->x * ibuf->y; i++, offset4++, offset3++) {
372                                 copy_v3_v3(*offset3, *offset4);
373                         }
374
375                         sl->equirect_radiance_gputexture = GPU_texture_create_nD(
376                                 ibuf->x, ibuf->y, 0, 2, gpu_matcap_3components, GPU_R11F_G11F_B10F, GPU_DATA_FLOAT, 0, false, error);
377
378                         MEM_SAFE_FREE(gpu_matcap_3components);
379                 }
380                 else {
381                         sl->equirect_radiance_gputexture = GPU_texture_create_2D(
382                                 ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, error);
383                         GPUTexture *tex = sl->equirect_radiance_gputexture;
384                         GPU_texture_bind(tex, 0);
385                         GPU_texture_filter_mode(tex, true);
386                         GPU_texture_wrap_mode(tex, true);
387                         GPU_texture_unbind(tex);
388                 }
389         }
390         sl->flag |= STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE;
391 }
392
393 static void studiolight_create_equirect_irradiance_gputexture(StudioLight *sl)
394 {
395         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
396                 char error[256];
397                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EQUIRECT_IRRADIANCE_IMAGE_CALCULATED);
398                 ImBuf *ibuf = sl->equirect_irradiance_buffer;
399                 sl->equirect_irradiance_gputexture = GPU_texture_create_2D(
400                         ibuf->x, ibuf->y, GPU_RGBA16F, ibuf->rect_float, error);
401                 GPUTexture *tex = sl->equirect_irradiance_gputexture;
402                 GPU_texture_bind(tex, 0);
403                 GPU_texture_filter_mode(tex, true);
404                 GPU_texture_wrap_mode(tex, true);
405                 GPU_texture_unbind(tex);
406         }
407         sl->flag |= STUDIOLIGHT_EQUIRECT_IRRADIANCE_GPUTEXTURE;
408 }
409
410 static void studiolight_calculate_radiance(ImBuf *ibuf, float color[4], const float direction[3])
411 {
412         float uv[2];
413         direction_to_equirect(uv, direction);
414         nearest_interpolation_color_wrap(ibuf, NULL, color, uv[0] * ibuf->x, uv[1] * ibuf->y);
415 }
416
417 static void studiolight_calculate_radiance_buffer(
418         ImBuf *ibuf, float *colbuf,
419         const int index_x, const int index_y, const int index_z,
420         const float xsign, const float ysign, const float zsign)
421 {
422         ITER_PIXELS(float, colbuf, 4,
423                     STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE,
424                     STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE)
425         {
426                 float direction[3];
427                 direction[index_x] = xsign * (x - 0.5f);
428                 direction[index_y] = ysign * (y - 0.5f);
429                 direction[index_z] = zsign * 0.5f;
430                 normalize_v3(direction);
431                 studiolight_calculate_radiance(ibuf, pixel, direction);
432         }
433         ITER_PIXELS_END;
434 }
435
436 static void studiolight_calculate_radiance_cubemap_buffers(StudioLight *sl)
437 {
438         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
439                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
440                 ImBuf *ibuf = sl->equirect_radiance_buffer;
441                 if (ibuf) {
442                         float *colbuf = MEM_mallocN(SQUARE(STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE) * sizeof(float[4]), __func__);
443
444                         /* front */
445                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 0, 2, 1,  1, -1,  1);
446                         sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_POS] = IMB_allocFromBuffer(
447                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
448
449                         /* back */
450                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 0, 2, 1,  1,  1, -1);
451                         sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_NEG] = IMB_allocFromBuffer(
452                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
453
454                         /* left */
455                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 2, 1, 0,  1, -1,  1);
456                         sl->radiance_cubemap_buffers[STUDIOLIGHT_X_POS] = IMB_allocFromBuffer(
457                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
458
459                         /* right */
460                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 2, 1, 0, -1, -1, -1);
461                         sl->radiance_cubemap_buffers[STUDIOLIGHT_X_NEG] = IMB_allocFromBuffer(
462                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
463
464                         /* top */
465                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 0, 1, 2, -1, -1,  1);
466                         sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_NEG] = IMB_allocFromBuffer(
467                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
468
469                         /* bottom */
470                         studiolight_calculate_radiance_buffer(ibuf, colbuf, 0, 1, 2,  1, -1, -1);
471                         sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_POS] = IMB_allocFromBuffer(
472                                 NULL, colbuf, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE, STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE);
473
474 #if 0
475                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_X_POS], "/tmp/studiolight_radiance_left.png", IB_rectfloat);
476                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_X_NEG], "/tmp/studiolight_radiance_right.png", IB_rectfloat);
477                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_POS], "/tmp/studiolight_radiance_front.png", IB_rectfloat);
478                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_NEG], "/tmp/studiolight_radiance_back.png", IB_rectfloat);
479                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_POS], "/tmp/studiolight_radiance_bottom.png", IB_rectfloat);
480                         IMB_saveiff(sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_NEG], "/tmp/studiolight_radiance_top.png", IB_rectfloat);
481 #endif
482                         MEM_freeN(colbuf);
483                 }
484         }
485         sl->flag |= STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED;
486 }
487
488 /*
489  * Spherical Harmonics
490  */
491 BLI_INLINE float area_element(float x, float y)
492 {
493         return atan2(x * y, sqrtf(x * x + y * y + 1));
494 }
495
496 BLI_INLINE float texel_solid_angle(float x, float y, float halfpix)
497 {
498         float v1x = (x - halfpix) * 2.0f - 1.0f;
499         float v1y = (y - halfpix) * 2.0f - 1.0f;
500         float v2x = (x + halfpix) * 2.0f - 1.0f;
501         float v2y = (y + halfpix) * 2.0f - 1.0f;
502
503         return area_element(v1x, v1y) - area_element(v1x, v2y) - area_element(v2x, v1y) + area_element(v2x, v2y);
504 }
505
506 static void studiolight_calculate_cubemap_vector_weight(float normal[3], float *weight, int face, float x, float y)
507 {
508         const float halfpix = 0.5f / STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE;
509         cube_face_uv_to_direction(normal, x, y, face);
510         *weight = texel_solid_angle(x, y, halfpix);
511 }
512
513 static void studiolight_spherical_harmonics_calculate_coefficients(StudioLight *sl, float (*sh)[3])
514 {
515         float weight_accum = 0.0f;
516         memset(sh, 0, sizeof(float) * 3 * STUDIOLIGHT_SH_COEFS_LEN);
517
518         for (int face = 0; face < 6; face++) {
519                 ITER_PIXELS(float, sl->radiance_cubemap_buffers[face]->rect_float, 4,
520                             STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE,
521                             STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE)
522                 {
523                         float color[3], cubevec[3], weight;
524                         studiolight_calculate_cubemap_vector_weight(cubevec, &weight, face, x, y);
525                         mul_v3_v3fl(color, pixel, weight);
526                         weight_accum += weight;
527
528                         int i = 0;
529                         /* L0 */
530                         madd_v3_v3fl(sh[i++], color, 0.2822095f);
531 #if STUDIOLIGHT_SH_BANDS > 1 /* L1 */
532                         const float nx = cubevec[0];
533                         const float ny = cubevec[1];
534                         const float nz = cubevec[2];
535                         madd_v3_v3fl(sh[i++], color, -0.488603f * nz);
536                         madd_v3_v3fl(sh[i++], color, 0.488603f * ny);
537                         madd_v3_v3fl(sh[i++], color, -0.488603f * nx);
538 #endif
539 #if STUDIOLIGHT_SH_BANDS > 2 /* L2 */
540                         const float nx2 = SQUARE(nx);
541                         const float ny2 = SQUARE(ny);
542                         const float nz2 = SQUARE(nz);
543                         madd_v3_v3fl(sh[i++], color, 1.092548f * nx * nz);
544                         madd_v3_v3fl(sh[i++], color, -1.092548f * nz * ny);
545                         madd_v3_v3fl(sh[i++], color, 0.315392f * (3.0f * ny2 - 1.0f));
546                         madd_v3_v3fl(sh[i++], color, 1.092548f * nx * ny);
547                         madd_v3_v3fl(sh[i++], color, 0.546274f * (nx2 - nz2));
548 #endif
549                         /* Bypass L3 Because final irradiance does not need it. */
550 #if STUDIOLIGHT_SH_BANDS > 4 /* L4 */
551                         const float nx4 = SQUARE(nx2);
552                         const float ny4 = SQUARE(ny2);
553                         const float nz4 = SQUARE(nz2);
554                         madd_v3_v3fl(sh[i++], color, 2.5033429417967046f * nx * nz * (nx2 - nz2));
555                         madd_v3_v3fl(sh[i++], color, -1.7701307697799304f * nz * ny * (3.0f * nx2 - nz2));
556                         madd_v3_v3fl(sh[i++], color, 0.9461746957575601f * nz * nx * (-1.0f + 7.0f * ny2));
557                         madd_v3_v3fl(sh[i++], color, -0.6690465435572892f * nz * ny * (-3.0f + 7.0f * ny2));
558                         madd_v3_v3fl(sh[i++], color, (105.0f * ny4 - 90.0f * ny2 + 9.0f) / 28.359261614f);
559                         madd_v3_v3fl(sh[i++], color, -0.6690465435572892f * nx * ny * (-3.0f + 7.0f * ny2));
560                         madd_v3_v3fl(sh[i++], color, 0.9461746957575601f * (nx2 - nz2) * (-1.0f + 7.0f * ny2));
561                         madd_v3_v3fl(sh[i++], color, -1.7701307697799304f * nx * ny * (nx2 - 3.0f * nz2));
562                         madd_v3_v3fl(sh[i++], color, 0.6258357354491761f * (nx4 - 6.0f * nz2 * nx2 + nz4));
563 #endif
564                 }
565                 ITER_PIXELS_END;
566         }
567
568         /* The sum of solid angle should be equal to the solid angle of the sphere (4 PI),
569          * so normalize in order to make our weightAccum exactly match 4 PI. */
570         for (int i = 0; i < STUDIOLIGHT_SH_COEFS_LEN; ++i) {
571                 mul_v3_fl(sh[i], M_PI * 4.0f / weight_accum);
572         }
573 }
574
575 /* Take monochrome SH as input */
576 static float studiolight_spherical_harmonics_lambda_get(float *sh, float max_laplacian)
577 {
578         /* From Peter-Pike Sloan's Stupid SH Tricks http://www.ppsloan.org/publications/StupidSH36.pdf */
579         float table_l[STUDIOLIGHT_SH_BANDS];
580         float table_b[STUDIOLIGHT_SH_BANDS];
581
582         float lambda = 0.0f;
583
584         table_l[0] = 0.0f;
585         table_b[0] = 0.0f;
586         int index = 1;
587         for (int level = 1; level < STUDIOLIGHT_SH_BANDS; level++) {
588                 table_l[level] = (float)(SQUARE(level) * SQUARE(level + 1));
589
590                 float b = 0.0f;
591                 for (int m = -1; m <= level; m++) {
592                         b += SQUARE(sh[index++]);
593                 }
594                 table_b[level] = b;
595         }
596
597         float squared_lamplacian = 0.0f;
598         for (int level = 1; level < STUDIOLIGHT_SH_BANDS; level++) {
599                 squared_lamplacian += table_l[level] * table_b[level];
600         }
601
602         const float target_squared_laplacian = max_laplacian * max_laplacian;
603         if (squared_lamplacian <= target_squared_laplacian) {
604                 return lambda;
605         }
606
607         const int no_iterations = 10000000;
608         for (int i = 0; i < no_iterations; ++i) {
609                 float f = 0.0f;
610                 float fd = 0.0f;
611
612                 for (int level = 1; level < STUDIOLIGHT_SH_BANDS; ++level) {
613                         f += table_l[level] * table_b[level] / SQUARE(1.0f + lambda * table_l[level]);
614                         fd += (2.0f * SQUARE(table_l[level]) * table_b[level]) / CUBE(1.0f + lambda * table_l[level]);
615                 }
616
617                 f = target_squared_laplacian - f;
618
619                 float delta = -f / fd;
620                 lambda += delta;
621
622                 if (ABS(delta) < 1e-6f) {
623                         break;
624                 }
625         }
626
627         return lambda;
628 }
629
630 static void studiolight_spherical_harmonics_apply_windowing(float (*sh)[3], float max_laplacian)
631 {
632         if (max_laplacian <= 0.0f)
633                 return;
634
635         float sh_r[STUDIOLIGHT_SH_COEFS_LEN];
636         float sh_g[STUDIOLIGHT_SH_COEFS_LEN];
637         float sh_b[STUDIOLIGHT_SH_COEFS_LEN];
638         for (int i = 0; i < STUDIOLIGHT_SH_COEFS_LEN; i++) {
639                 sh_r[i] = sh[i][0];
640                 sh_g[i] = sh[i][1];
641                 sh_b[i] = sh[i][2];
642         }
643         float lambda_r = studiolight_spherical_harmonics_lambda_get(sh_r, max_laplacian);
644         float lambda_g = studiolight_spherical_harmonics_lambda_get(sh_g, max_laplacian);
645         float lambda_b = studiolight_spherical_harmonics_lambda_get(sh_b, max_laplacian);
646
647         /* Apply windowing lambda */
648         int index = 0;
649         for (int level = 0; level < STUDIOLIGHT_SH_BANDS; level++) {
650                 float s[3];
651                 s[0] = 1.0f / (1.0f + lambda_r * SQUARE(level) * SQUARE(level + 1.0f));
652                 s[1] = 1.0f / (1.0f + lambda_g * SQUARE(level) * SQUARE(level + 1.0f));
653                 s[2] = 1.0f / (1.0f + lambda_b * SQUARE(level) * SQUARE(level + 1.0f));
654
655                 for (int m = -1; m <= level; m++) {
656                         mul_v3_v3(sh[index++], s);
657                 }
658         }
659 }
660
661 static float studiolight_spherical_harmonics_geomerics_eval(const float normal[3], float sh0, float sh1, float sh2, float sh3)
662 {
663         /* Use Geomerics non-linear SH. */
664         /* http://www.geomerics.com/wp-content/uploads/2015/08/CEDEC_Geomerics_ReconstructingDiffuseLighting1.pdf */
665         float R0 = sh0 * M_1_PI;
666
667         float R1[3] = {-sh3, sh2, -sh1};
668         mul_v3_fl(R1, 0.5f * M_1_PI * 1.5f); /* 1.5f is to improve the contrast a bit. */
669         float lenR1 = len_v3(R1);
670         mul_v3_fl(R1, 1.0f / lenR1);
671         float q = 0.5f * (1.0f + dot_v3v3(R1, normal));
672
673         float p = 1.0f + 2.0f * lenR1 / R0;
674         float a = (1.0f - lenR1 / R0) / (1.0f + lenR1 / R0);
675
676         return R0 * (a + (1.0f - a) * (p + 1.0f) * powf(q, p));
677 }
678
679 BLI_INLINE void studiolight_spherical_harmonics_eval(StudioLight *sl, float color[3], const float normal[3])
680 {
681 #if STUDIOLIGHT_SH_BANDS == 2
682         float (*sh)[3] = (float (*)[3])sl->spherical_harmonics_coefs;
683         for (int i = 0; i < 3; ++i) {
684                 color[i] = studiolight_spherical_harmonics_geomerics_eval(normal, sh[0][i], sh[1][i], sh[2][i], sh[3][i]);
685         }
686         return;
687 #else
688
689         /* L0 */
690         mul_v3_v3fl(color, sl->spherical_harmonics_coefs[0], 0.282095f);
691 #  if STUDIOLIGHT_SH_BANDS > 1 /* L1 */
692         const float nx = normal[0];
693         const float ny = normal[1];
694         const float nz = normal[2];
695         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[1], -0.488603f * nz);
696         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[2],  0.488603f * ny);
697         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[3], -0.488603f * nx);
698 #  endif
699 #  if STUDIOLIGHT_SH_BANDS > 2 /* L2 */
700         const float nx2 = SQUARE(nx);
701         const float ny2 = SQUARE(ny);
702         const float nz2 = SQUARE(nz);
703         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[4], 1.092548f * nx * nz);
704         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[5], -1.092548f * nz * ny);
705         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[6], 0.315392f * (3.0f * ny2 - 1.0f));
706         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[7], -1.092548 * nx * ny);
707         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[8], 0.546274 * (nx2 - nz2));
708 #  endif
709         /* L3 coefs are 0 */
710 #  if STUDIOLIGHT_SH_BANDS > 4 /* L4 */
711         const float nx4 = SQUARE(nx2);
712         const float ny4 = SQUARE(ny2);
713         const float nz4 = SQUARE(nz2);
714         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[9],   2.5033429417967046f * nx * nz * (nx2 - nz2));
715         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[10], -1.7701307697799304f * nz * ny * (3.0f * nx2 - nz2));
716         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[11],  0.9461746957575601f * nz * nx * (-1.0f + 7.0f * ny2));
717         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[12], -0.6690465435572892f * nz * ny * (-3.0f + 7.0f * ny2));
718         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[13],  (105.0f * ny4 - 90.0f * ny2 + 9.0f) / 28.359261614f);
719         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[14], -0.6690465435572892f * nx * ny * (-3.0f + 7.0f * ny2));
720         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[15],  0.9461746957575601f * (nx2 - nz2) * (-1.0f + 7.0f * ny2));
721         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[16], -1.7701307697799304f * nx * ny * (nx2 - 3.0f * nz2));
722         madd_v3_v3fl(color, sl->spherical_harmonics_coefs[17],  0.6258357354491761f * (nx4 - 6.0f * nz2 * nx2 + nz4));
723 #  endif
724 #endif
725 }
726
727 /* This modify the radiance into irradiance. */
728 static void studiolight_spherical_harmonics_apply_band_factors(StudioLight *sl, float (*sh)[3])
729 {
730         static float sl_sh_band_factors[5] = {
731                 1.0f,
732                 2.0f / 3.0f,
733                 1.0f / 4.0f,
734                 0.0f,
735                 -1.0f / 24.0f
736         };
737
738         int index = 0, dst_idx = 0;
739         for (int band = 0; band < STUDIOLIGHT_SH_BANDS; band++) {
740                 for (int m = 0; m < SQUARE(band + 1) - SQUARE(band); m++) {
741                         /* Skip L3 */
742                         if (band != 3) {
743                                 mul_v3_v3fl(sl->spherical_harmonics_coefs[dst_idx++], sh[index], sl_sh_band_factors[band]);
744                         }
745                         index++;
746                 }
747         }
748 }
749
750 static void studiolight_calculate_diffuse_light(StudioLight *sl)
751 {
752         /* init light to black */
753         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
754                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED);
755
756                 float sh_coefs[STUDIOLIGHT_SH_COEFS_LEN][3];
757                 studiolight_spherical_harmonics_calculate_coefficients(sl, sh_coefs);
758                 studiolight_spherical_harmonics_apply_windowing(sh_coefs, STUDIOLIGHT_SH_WINDOWING);
759                 studiolight_spherical_harmonics_apply_band_factors(sl, sh_coefs);
760
761                 if (sl->flag & STUDIOLIGHT_USER_DEFINED) {
762                         FILE *fp = BLI_fopen(sl->path_sh_cache, "wb");
763                         if (fp) {
764                                 fwrite(sl->spherical_harmonics_coefs, sizeof(sl->spherical_harmonics_coefs), 1, fp);
765                                 fclose(fp);
766                         }
767                 }
768         }
769         sl->flag |= STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED;
770 }
771
772 BLI_INLINE void studiolight_evaluate_specular_radiance_buffer(
773         ImBuf *radiance_buffer, const float normal[3], float color[3],
774         int xoffset, int yoffset, int zoffset, float zsign)
775 {
776         if (radiance_buffer == NULL) {
777                 return;
778         }
779
780         float accum[3] = {0.0f, 0.0f, 0.0f};
781         float accum_weight = 0.00001f;
782         ITER_PIXELS(float, radiance_buffer->rect_float, 4,
783                     STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE,
784                     STUDIOLIGHT_RADIANCE_CUBEMAP_SIZE)
785         {
786                 float direction[3];
787                 direction[zoffset] = zsign * 0.5f;
788                 direction[xoffset] = x - 0.5f;
789                 direction[yoffset] = y - 0.5f;
790                 normalize_v3(direction);
791                 float weight = dot_v3v3(direction, normal) > 0.95f ? 1.0f : 0.0f;
792                 // float solid_angle = texel_solid_angle(x, y, texel_size[0] * 0.5f);
793                 madd_v3_v3fl(accum, pixel, weight);
794                 accum_weight += weight;
795         }
796         ITER_PIXELS_END;
797
798         madd_v3_v3fl(color, accum, 1.0f / accum_weight);
799 }
800
801 #ifdef STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
802 static void studiolight_irradiance_eval(StudioLight *sl, float color[3], const float normal[3])
803 {
804         copy_v3_fl(color, 0.0f);
805
806         /* XXX: This is madness, iterating over all cubemap pixels for each destination pixels
807          * even if their weight is 0.0f.
808          * It should use hemisphere, cosine sampling at least. */
809
810         /* back */
811         studiolight_evaluate_specular_radiance_buffer(
812                 sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_POS], normal, color, 0, 2, 1, 1);
813         /* front */
814         studiolight_evaluate_specular_radiance_buffer(
815                 sl->radiance_cubemap_buffers[STUDIOLIGHT_Y_NEG], normal, color, 0, 2, 1, -1);
816
817         /* left */
818         studiolight_evaluate_specular_radiance_buffer(
819                 sl->radiance_cubemap_buffers[STUDIOLIGHT_X_POS], normal, color, 1, 2, 0, 1);
820         /* right */
821         studiolight_evaluate_specular_radiance_buffer(
822                 sl->radiance_cubemap_buffers[STUDIOLIGHT_X_NEG], normal, color, 1, 2, 0, -1);
823
824         /* top */
825         studiolight_evaluate_specular_radiance_buffer(
826                 sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_POS], normal, color, 0, 1, 2, 1);
827         /* bottom */
828         studiolight_evaluate_specular_radiance_buffer(
829                 sl->radiance_cubemap_buffers[STUDIOLIGHT_Z_NEG], normal, color, 0, 1, 2, -1);
830
831         mul_v3_fl(color, 1.0 / M_PI);
832 }
833 #endif
834
835 static float brdf_approx(float spec_color, float roughness, float NV)
836 {
837         /* Very rough own approx. We don't need it to be correct, just fast.
838          * Just simulate fresnel effect with roughness attenuation. */
839         float fresnel = exp2(-8.35f * NV) * (1.0f - roughness);
840         return spec_color * (1.0f - fresnel) + fresnel;
841 }
842
843 /* NL need to be unclamped. w in [0..1] range. */
844 static float wrapped_lighting(float NL, float w)
845 {
846         float w_1 = w + 1.0f;
847         return max_ff((NL + w) / (w_1 * w_1), 0.0f);
848 }
849
850 static float blinn_specular(
851         const float L[3], const float I[3], const float N[3], float R[3], float NL, float roughness, float wrap)
852 {
853         float half_dir[3];
854         float wrapped_NL = dot_v3v3(L, R);
855         add_v3_v3v3(half_dir, L, I);
856         normalize_v3(half_dir);
857         float spec_angle = max_ff(dot_v3v3(half_dir, N), 0.0f);
858
859         float gloss = 1.0f - roughness;
860         /* Reduce gloss for smooth light. (simulate bigger light) */
861         gloss *= 1.0f - wrap;
862         float shininess = exp2(10.0f * gloss + 1.0f);
863
864         /* Pi is already divided in the lamp power.
865          * normalization_factor = (shininess + 8.0) / (8.0 * M_PI) */
866         float normalization_factor = shininess * 0.125f + 1.0f;
867         float spec_light = powf(spec_angle, shininess) * max_ff(NL, 0.0f) * normalization_factor;
868
869         /* Simulate Env. light. */
870         float w = wrap * (1.0 - roughness) + roughness;
871         float spec_env = wrapped_lighting(wrapped_NL, w);
872
873         float w2 = wrap * wrap;
874
875         return spec_light * (1.0 - w2) + spec_env * w2;
876 }
877
878 /* Keep in sync with the glsl shader function get_world_lighting() */
879 static void studiolight_lights_eval(StudioLight *sl, float color[3], const float normal[3])
880 {
881         float R[3], I[3] = {0.0f, 0.0f, 1.0f}, N[3] = {normal[0], normal[2], -normal[1]};
882         const float roughness = 0.5f;
883         const float diffuse_color = 0.8f;
884         const float specular_color = brdf_approx(0.05f, roughness, N[2]);
885         float diff_light[3], spec_light[3];
886
887         /* Ambient lighting */
888         copy_v3_v3(diff_light, sl->light_ambient);
889         copy_v3_v3(spec_light, sl->light_ambient);
890
891         reflect_v3_v3v3(R, I, N);
892         for (int i = 0; i < 3; ++i) {
893                 SolidLight *light = &sl->light[i];
894                 if (light->flag) {
895                         /* Diffuse lighting */
896                         float NL = dot_v3v3(light->vec, N);
897                         float diff = wrapped_lighting(NL, light->smooth);
898                         madd_v3_v3fl(diff_light, light->col, diff);
899                         /* Specular lighting */
900                         float spec = blinn_specular(light->vec, I, N, R, NL, roughness, light->smooth);
901                         madd_v3_v3fl(spec_light, light->spec, spec);
902                 }
903         }
904
905         /* Multiply result by surface colors. */
906         mul_v3_fl(diff_light, diffuse_color * (1.0 - specular_color));
907         mul_v3_fl(spec_light, specular_color);
908
909         add_v3_v3v3(color, diff_light, spec_light);
910 }
911
912 static bool studiolight_load_irradiance_equirect_image(StudioLight *sl)
913 {
914 #ifdef STUDIOLIGHT_LOAD_CACHED_FILES
915         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
916                 ImBuf *ibuf = NULL;
917                 ibuf = IMB_loadiffname(sl->path_irr_cache, 0, NULL);
918                 if (ibuf) {
919                         IMB_float_from_rect(ibuf);
920                         sl->equirect_irradiance_buffer = ibuf;
921                         sl->flag |= STUDIOLIGHT_EQUIRECT_IRRADIANCE_IMAGE_CALCULATED;
922                         return true;
923                 }
924         }
925 #else
926         UNUSED_VARS(sl);
927 #endif
928         return false;
929 }
930
931 static bool studiolight_load_spherical_harmonics_coefficients(StudioLight *sl)
932 {
933 #ifdef STUDIOLIGHT_LOAD_CACHED_FILES
934         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
935                 FILE *fp = BLI_fopen(sl->path_sh_cache, "rb");
936                 if (fp) {
937                         if (fread((void *)(sl->spherical_harmonics_coefs), sizeof(sl->spherical_harmonics_coefs), 1, fp)) {
938                                 sl->flag |= STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED;
939                                 fclose(fp);
940                                 return true;
941                         }
942                         fclose(fp);
943                 }
944         }
945 #else
946         UNUSED_VARS(sl);
947 #endif
948         return false;
949 }
950
951 static void studiolight_calculate_irradiance_equirect_image(StudioLight *sl)
952 {
953         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
954 #ifdef STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
955                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED);
956 #else
957                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED);
958 #endif
959
960                 float *colbuf = MEM_mallocN(STUDIOLIGHT_IRRADIANCE_EQUIRECT_WIDTH * STUDIOLIGHT_IRRADIANCE_EQUIRECT_HEIGHT * sizeof(float[4]), __func__);
961
962                 ITER_PIXELS(float, colbuf, 4,
963                             STUDIOLIGHT_IRRADIANCE_EQUIRECT_WIDTH,
964                             STUDIOLIGHT_IRRADIANCE_EQUIRECT_HEIGHT)
965                 {
966                         float dir[3];
967                         equirect_to_direction(dir, x, y);
968 #ifdef STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
969                         studiolight_irradiance_eval(sl, pixel, dir);
970 #else
971                         studiolight_spherical_harmonics_eval(sl, pixel, dir);
972 #endif
973                         pixel[3] = 1.0f;
974                 }
975                 ITER_PIXELS_END;
976
977                 sl->equirect_irradiance_buffer = IMB_allocFromBuffer(
978                         NULL, colbuf,
979                         STUDIOLIGHT_IRRADIANCE_EQUIRECT_WIDTH,
980                         STUDIOLIGHT_IRRADIANCE_EQUIRECT_HEIGHT);
981                 MEM_freeN(colbuf);
982
983 #ifdef STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
984                 /*
985                  * Only store cached files when using STUDIOLIGHT_IRRADIANCE_METHOD_RADIANCE
986                  */
987                 if (sl->flag & STUDIOLIGHT_USER_DEFINED) {
988                         IMB_saveiff(sl->equirect_irradiance_buffer, sl->path_irr_cache, IB_rectfloat);
989                 }
990 #endif
991         }
992         sl->flag |= STUDIOLIGHT_EQUIRECT_IRRADIANCE_IMAGE_CALCULATED;
993 }
994
995 static StudioLight *studiolight_add_file(const char *path, int flag)
996 {
997         char filename[FILE_MAXFILE];
998         BLI_split_file_part(path, filename, FILE_MAXFILE);
999
1000         if ((((flag & STUDIOLIGHT_TYPE_STUDIO) != 0) && BLI_path_extension_check(filename, ".sl")) ||
1001             BLI_path_extension_check_array(filename, imb_ext_image))
1002         {
1003                 StudioLight *sl = studiolight_create(STUDIOLIGHT_EXTERNAL_FILE | flag);
1004                 BLI_strncpy(sl->name, filename, FILE_MAXFILE);
1005                 BLI_strncpy(sl->path, path, FILE_MAXFILE);
1006
1007                 if ((flag & STUDIOLIGHT_TYPE_STUDIO) != 0) {
1008                         studiolight_load_solid_light(sl);
1009                 }
1010                 else {
1011                         sl->path_irr_cache = BLI_string_joinN(path, ".irr");
1012                         sl->path_sh_cache = BLI_string_joinN(path, ".sh2");
1013                 }
1014                 BLI_addtail(&studiolights, sl);
1015                 return sl;
1016         }
1017         return NULL;
1018 }
1019
1020 static void studiolight_add_files_from_datafolder(const int folder_id, const char *subfolder, int flag)
1021 {
1022         struct direntry *dir;
1023         const char *folder = BKE_appdir_folder_id(folder_id, subfolder);
1024         if (folder) {
1025                 uint totfile = BLI_filelist_dir_contents(folder, &dir);
1026                 int i;
1027                 for (i = 0; i < totfile; i++) {
1028                         if ((dir[i].type & S_IFREG)) {
1029                                 studiolight_add_file(dir[i].path, flag);
1030                         }
1031                 }
1032                 BLI_filelist_free(dir, totfile);
1033                 dir = NULL;
1034         }
1035 }
1036
1037 static int studiolight_flag_cmp_order(const StudioLight *sl)
1038 {
1039         /* Internal studiolights before external studio lights */
1040         if (sl->flag & STUDIOLIGHT_EXTERNAL_FILE) {
1041                 return 1;
1042         }
1043         return 0;
1044 }
1045
1046 static int studiolight_cmp(const void *a, const void *b)
1047 {
1048         const StudioLight *sl1 = a;
1049         const StudioLight *sl2 = b;
1050
1051         const int flagorder1 = studiolight_flag_cmp_order(sl1);
1052         const int flagorder2 = studiolight_flag_cmp_order(sl2);
1053
1054         if (flagorder1 < flagorder2) {
1055                 return -1;
1056         }
1057         else if (flagorder1 > flagorder2) {
1058                 return 1;
1059         }
1060         else {
1061                 return BLI_strcasecmp(sl1->name, sl2->name);
1062         }
1063 }
1064
1065 /* icons */
1066
1067 /* Takes normalized uvs as parameter (range from 0 to 1).
1068  * inner_edge and outer_edge are distances (from the center)
1069  * in uv space for the alpha mask falloff. */
1070 static uint alpha_circle_mask(float u, float v, float inner_edge, float outer_edge)
1071 {
1072         /* Coords from center. */
1073         float co[2] = {u - 0.5f, v - 0.5f};
1074         float dist = len_v2(co);
1075         float alpha = 1.0f + (inner_edge - dist) / (outer_edge - inner_edge);
1076         uint mask = (uint)floorf(255.0f * min_ff(max_ff(alpha, 0.0f), 1.0f));
1077         return mask << 24;
1078 }
1079
1080 /* Percentage of the icon that the preview sphere covers. */
1081 #define STUDIOLIGHT_DIAMETER 0.95f
1082 /* Rescale coord around (0.5, 0.5) by STUDIOLIGHT_DIAMETER. */
1083 #define RESCALE_COORD(x) (x / STUDIOLIGHT_DIAMETER - (1.0f - STUDIOLIGHT_DIAMETER) / 2.0f)
1084
1085 /* Remaps normalized UV [0..1] to a sphere normal around (0.5, 0.5) */
1086 static void sphere_normal_from_uv(float normal[3], float u, float v)
1087 {
1088         normal[0] = u * 2.0f - 1.0f;
1089         normal[1] = v * 2.0f - 1.0f;
1090         float dist = len_v2(normal);
1091         normal[2] = sqrtf(1.0f - SQUARE(dist));
1092 }
1093
1094 static void studiolight_radiance_preview(uint *icon_buffer, StudioLight *sl)
1095 {
1096         BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
1097
1098         ITER_PIXELS(uint, icon_buffer, 1,
1099                     STUDIOLIGHT_ICON_SIZE,
1100                     STUDIOLIGHT_ICON_SIZE)
1101         {
1102                 float dy = RESCALE_COORD(y);
1103                 float dx = RESCALE_COORD(x);
1104
1105                 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
1106                 if (alphamask != 0) {
1107                         float normal[3], direction[3], color[4];
1108                         float incoming[3] = {0.0f, 0.0f, -1.0f};
1109                         sphere_normal_from_uv(normal, dx, dy);
1110                         reflect_v3_v3v3(direction, incoming, normal);
1111                         /* We want to see horizon not poles. */
1112                         SWAP(float, direction[1], direction[2]);
1113                         direction[1] = -direction[1];
1114
1115                         studiolight_calculate_radiance(sl->equirect_radiance_buffer, color, direction);
1116
1117                         *pixel = rgb_to_cpack(
1118                                 linearrgb_to_srgb(color[0]),
1119                                 linearrgb_to_srgb(color[1]),
1120                                 linearrgb_to_srgb(color[2])) | alphamask;
1121                 }
1122                 else {
1123                         *pixel = 0x0;
1124                 }
1125         }
1126         ITER_PIXELS_END;
1127 }
1128
1129 static void studiolight_matcap_preview(uint *icon_buffer, StudioLight *sl, bool flipped)
1130 {
1131         BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EXTERNAL_IMAGE_LOADED);
1132
1133         ImBuf *ibuf = sl->equirect_radiance_buffer;
1134
1135         ITER_PIXELS(uint, icon_buffer, 1,
1136                     STUDIOLIGHT_ICON_SIZE,
1137                     STUDIOLIGHT_ICON_SIZE)
1138         {
1139                 float dy = RESCALE_COORD(y);
1140                 float dx = RESCALE_COORD(x);
1141                 if (flipped) {
1142                         dx = 1.0f - dx;
1143                 }
1144
1145                 float color[4];
1146                 nearest_interpolation_color(ibuf, NULL, color, dx * ibuf->x - 1.0f, dy * ibuf->y - 1.0f);
1147
1148                 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
1149
1150                 *pixel = rgb_to_cpack(
1151                         linearrgb_to_srgb(color[0]),
1152                         linearrgb_to_srgb(color[1]),
1153                         linearrgb_to_srgb(color[2])) | alphamask;
1154         }
1155         ITER_PIXELS_END;
1156 }
1157
1158 static void studiolight_irradiance_preview(uint *icon_buffer, StudioLight *sl)
1159 {
1160         ITER_PIXELS(uint, icon_buffer, 1,
1161                     STUDIOLIGHT_ICON_SIZE,
1162                     STUDIOLIGHT_ICON_SIZE)
1163         {
1164                 float dy = RESCALE_COORD(y);
1165                 float dx = RESCALE_COORD(x);
1166
1167                 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
1168                 if (alphamask != 0) {
1169                         float normal[3], color[3];
1170                         sphere_normal_from_uv(normal, dx, dy);
1171                         /* We want to see horizon not poles. */
1172                         SWAP(float, normal[1], normal[2]);
1173                         normal[1] = -normal[1];
1174
1175                         studiolight_lights_eval(sl, color, normal);
1176
1177                         *pixel = rgb_to_cpack(
1178                                 linearrgb_to_srgb(color[0]),
1179                                 linearrgb_to_srgb(color[1]),
1180                                 linearrgb_to_srgb(color[2])) | alphamask;
1181                 }
1182                 else {
1183                         *pixel = 0x0;
1184                 }
1185         }
1186         ITER_PIXELS_END;
1187 }
1188
1189 /* API */
1190 void BKE_studiolight_init(void)
1191 {
1192         /* Add default studio light */
1193         StudioLight *sl = studiolight_create(
1194                 STUDIOLIGHT_INTERNAL | STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED | STUDIOLIGHT_TYPE_STUDIO);
1195         BLI_strncpy(sl->name, "Default", FILE_MAXFILE);
1196
1197         copy_v4_fl4(sl->light_ambient, 0.025000, 0.025000, 0.025000, 1.000000);
1198
1199         copy_v4_fl4(sl->light[0].vec, -0.580952, 0.228571, 0.781185, 0.0);
1200         copy_v4_fl4(sl->light[0].col, 0.900000, 0.900000, 0.900000, 1.000000);
1201         copy_v4_fl4(sl->light[0].spec, 0.318547, 0.318547, 0.318547, 1.000000);
1202         sl->light[0].flag = 1;
1203         sl->light[0].smooth = 0.1;
1204
1205         copy_v4_fl4(sl->light[1].vec, 0.788218, 0.593482, -0.162765, 0.0);
1206         copy_v4_fl4(sl->light[1].col, 0.267115, 0.269928, 0.358840, 1.000000);
1207         copy_v4_fl4(sl->light[1].spec, 0.090838, 0.090838, 0.090838, 1.000000);
1208         sl->light[1].flag = 1;
1209         sl->light[1].smooth = 0.25;
1210
1211         copy_v4_fl4(sl->light[2].vec, 0.696472, -0.696472, -0.172785, 0.0);
1212         copy_v4_fl4(sl->light[2].col, 0.293216, 0.304662, 0.401968, 1.000000);
1213         copy_v4_fl4(sl->light[2].spec, 0.069399, 0.020331, 0.020331, 1.000000);
1214         sl->light[2].flag = 1;
1215         sl->light[2].smooth = 0.5;
1216
1217         copy_v4_fl4(sl->light[3].vec, 0.021053, -0.989474, 0.143173, 0.0);
1218         copy_v4_fl4(sl->light[3].col, 0.0, 0.0, 0.0, 1.0);
1219         copy_v4_fl4(sl->light[3].spec, 0.072234, 0.082253, 0.162642, 1.000000);
1220         sl->light[3].flag = 1;
1221         sl->light[3].smooth = 0.7;
1222
1223         BLI_addtail(&studiolights, sl);
1224
1225         /* go over the preset folder and add a studiolight for every image with its path */
1226         /* for portable installs (where USER and SYSTEM paths are the same), only go over LOCAL datafiles once */
1227         /* Also reserve icon space for it. */
1228         if (!BKE_appdir_app_is_portable_install()) {
1229                 studiolight_add_files_from_datafolder(BLENDER_USER_DATAFILES,
1230                                                       STUDIOLIGHT_LIGHTS_FOLDER,
1231                                                       STUDIOLIGHT_TYPE_STUDIO | STUDIOLIGHT_USER_DEFINED);
1232                 studiolight_add_files_from_datafolder(BLENDER_USER_DATAFILES,
1233                                                       STUDIOLIGHT_WORLD_FOLDER,
1234                                                       STUDIOLIGHT_TYPE_WORLD | STUDIOLIGHT_USER_DEFINED);
1235                 studiolight_add_files_from_datafolder(BLENDER_USER_DATAFILES,
1236                                                       STUDIOLIGHT_MATCAP_FOLDER,
1237                                                       STUDIOLIGHT_TYPE_MATCAP | STUDIOLIGHT_USER_DEFINED);
1238         }
1239         studiolight_add_files_from_datafolder(BLENDER_SYSTEM_DATAFILES, STUDIOLIGHT_LIGHTS_FOLDER, STUDIOLIGHT_TYPE_STUDIO);
1240         studiolight_add_files_from_datafolder(BLENDER_SYSTEM_DATAFILES, STUDIOLIGHT_WORLD_FOLDER,  STUDIOLIGHT_TYPE_WORLD);
1241         studiolight_add_files_from_datafolder(BLENDER_SYSTEM_DATAFILES, STUDIOLIGHT_MATCAP_FOLDER, STUDIOLIGHT_TYPE_MATCAP);
1242
1243         /* sort studio lights on filename. */
1244         BLI_listbase_sort(&studiolights, studiolight_cmp);
1245 }
1246
1247 void BKE_studiolight_free(void)
1248 {
1249         struct StudioLight *sl;
1250         while ((sl = BLI_pophead(&studiolights))) {
1251                 studiolight_free(sl);
1252         }
1253 }
1254
1255 struct StudioLight *BKE_studiolight_find_default(int flag)
1256 {
1257         const char *default_name = "";
1258
1259         if (flag & STUDIOLIGHT_TYPE_WORLD) {
1260                 default_name = STUDIOLIGHT_WORLD_DEFAULT;
1261         }
1262         else if (flag & STUDIOLIGHT_TYPE_MATCAP) {
1263                 default_name = STUDIOLIGHT_MATCAP_DEFAULT;
1264         }
1265
1266         LISTBASE_FOREACH(StudioLight *, sl, &studiolights) {
1267                 if ((sl->flag & flag) && STREQ(sl->name, default_name)) {
1268                         return sl;
1269                 }
1270         }
1271
1272         LISTBASE_FOREACH(StudioLight *, sl, &studiolights) {
1273                 if ((sl->flag & flag)) {
1274                         return sl;
1275                 }
1276         }
1277         return NULL;
1278 }
1279
1280 struct StudioLight *BKE_studiolight_find(const char *name, int flag)
1281 {
1282         LISTBASE_FOREACH(StudioLight *, sl, &studiolights) {
1283                 if (STREQLEN(sl->name, name, FILE_MAXFILE)) {
1284                         if ((sl->flag & flag)) {
1285                                 return sl;
1286                         }
1287                         else {
1288                                 /* flags do not match, so use default */
1289                                 return BKE_studiolight_find_default(flag);
1290                         }
1291                 }
1292         }
1293         /* When not found, use the default studio light */
1294         return BKE_studiolight_find_default(flag);
1295 }
1296
1297 struct StudioLight *BKE_studiolight_findindex(int index, int flag)
1298 {
1299         LISTBASE_FOREACH(StudioLight *, sl, &studiolights) {
1300                 if (sl->index == index) {
1301                         return sl;
1302                 }
1303         }
1304         /* When not found, use the default studio light */
1305         return BKE_studiolight_find_default(flag);
1306 }
1307
1308 struct ListBase *BKE_studiolight_listbase(void)
1309 {
1310         return &studiolights;
1311 }
1312
1313 void BKE_studiolight_preview(uint *icon_buffer, StudioLight *sl, int icon_id_type)
1314 {
1315         switch (icon_id_type) {
1316                 case STUDIOLIGHT_ICON_ID_TYPE_RADIANCE:
1317                 default:
1318                 {
1319                         studiolight_radiance_preview(icon_buffer, sl);
1320                         break;
1321                 }
1322                 case STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE:
1323                 {
1324                         studiolight_irradiance_preview(icon_buffer, sl);
1325                         break;
1326                 }
1327                 case STUDIOLIGHT_ICON_ID_TYPE_MATCAP:
1328                 {
1329                         studiolight_matcap_preview(icon_buffer, sl, false);
1330                         break;
1331                 }
1332                 case STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED:
1333                 {
1334                         studiolight_matcap_preview(icon_buffer, sl, true);
1335                         break;
1336                 }
1337         }
1338 }
1339
1340 /* Ensure state of Studiolights */
1341 void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
1342 {
1343         if ((sl->flag & flag) == flag) {
1344                 return;
1345         }
1346
1347         if ((flag & STUDIOLIGHT_EXTERNAL_IMAGE_LOADED)) {
1348                 studiolight_load_equirect_image(sl);
1349         }
1350         if ((flag & STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED)) {
1351                 studiolight_calculate_radiance_cubemap_buffers(sl);
1352         }
1353         if ((flag & STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED)) {
1354                 if (!studiolight_load_spherical_harmonics_coefficients(sl)) {
1355                         studiolight_calculate_diffuse_light(sl);
1356                 }
1357         }
1358         if ((flag & STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE)) {
1359                 studiolight_create_equirect_radiance_gputexture(sl);
1360         }
1361         if ((flag & STUDIOLIGHT_EQUIRECT_IRRADIANCE_GPUTEXTURE)) {
1362                 studiolight_create_equirect_irradiance_gputexture(sl);
1363         }
1364         if ((flag & STUDIOLIGHT_EQUIRECT_IRRADIANCE_IMAGE_CALCULATED)) {
1365                 if (!studiolight_load_irradiance_equirect_image(sl)) {
1366                         studiolight_calculate_irradiance_equirect_image(sl);
1367                 }
1368         }
1369 }
1370
1371 /*
1372  * Python API Functions
1373  */
1374 void BKE_studiolight_remove(StudioLight *sl)
1375 {
1376         if (sl->flag & STUDIOLIGHT_USER_DEFINED) {
1377                 BLI_remlink(&studiolights, sl);
1378                 studiolight_free(sl);
1379         }
1380 }
1381
1382 StudioLight *BKE_studiolight_load(const char *path, int type)
1383 {
1384         StudioLight *sl = studiolight_add_file(path, type | STUDIOLIGHT_USER_DEFINED);
1385         return sl;
1386 }
1387
1388 StudioLight *BKE_studiolight_create(const char *path, const SolidLight light[4], const float light_ambient[3])
1389 {
1390         StudioLight *sl = studiolight_create(STUDIOLIGHT_EXTERNAL_FILE | STUDIOLIGHT_USER_DEFINED | STUDIOLIGHT_TYPE_STUDIO);
1391
1392         char filename[FILE_MAXFILE];
1393         BLI_split_file_part(path, filename, FILE_MAXFILE);
1394         BLI_snprintf(sl->path, FILE_MAXFILE, "%s%s", path, ".sl");
1395         BLI_snprintf(sl->name, FILE_MAXFILE, "%s%s", filename, ".sl");
1396
1397         memcpy(sl->light, light, sizeof(*light) * 4);
1398         memcpy(sl->light_ambient, light_ambient, sizeof(*light_ambient) * 3);
1399
1400         studiolight_write_solid_light(sl);
1401
1402         BLI_addtail(&studiolights, sl);
1403         return sl;
1404 }
1405
1406 /* Only useful for workbench while editing the userprefs. */
1407 StudioLight *BKE_studiolight_studio_edit_get(void)
1408 {
1409         static StudioLight sl = {0};
1410         sl.flag = STUDIOLIGHT_TYPE_STUDIO;
1411
1412         memcpy(sl.light, U.light_param, sizeof(*sl.light) * 4);
1413         memcpy(sl.light_ambient, U.light_ambient, sizeof(*sl.light_ambient) * 3);
1414
1415         return &sl;
1416 }
1417
1418 void BKE_studiolight_refresh(void)
1419 {
1420         BKE_studiolight_free();
1421         BKE_studiolight_init();
1422 }
1423
1424 void BKE_studiolight_set_free_function(StudioLight *sl, StudioLightFreeFunction *free_function, void *data)
1425 {
1426         sl->free_function = free_function;
1427         sl->free_function_data = data;
1428 }
1429
1430 void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id)
1431 {
1432         BLI_assert(sl != NULL);
1433         if (sl->icon_id_radiance == icon_id) {
1434                 sl->icon_id_radiance = 0;
1435         }
1436         if (sl->icon_id_irradiance == icon_id) {
1437                 sl->icon_id_irradiance = 0;
1438         }
1439         if (sl->icon_id_matcap == icon_id) {
1440                 sl->icon_id_matcap = 0;
1441         }
1442         if (sl->icon_id_matcap_flipped == icon_id) {
1443                 sl->icon_id_matcap_flipped = 0;
1444         }
1445 }