46#define STUDIOLIGHT_PASSNAME_DIFFUSE "diffuse"
47#define STUDIOLIGHT_PASSNAME_SPECULAR "specular"
71#define ITER_PIXELS(type, src, channels, width, height) \
73 float texel_size[2]; \
74 texel_size[0] = 1.0f / width; \
75 texel_size[1] = 1.0f / height; \
76 type(*pixel_)[channels] = (type(*)[channels])src; \
77 for (float y = 0.5 * texel_size[1]; y < 1.0; y += texel_size[1]) { \
78 for (float x = 0.5 * texel_size[0]; x < 1.0; x += texel_size[0], pixel_++) { \
79 type *pixel = *pixel_;
81#define ITER_PIXELS_END \
88#define IMB_SAFE_FREE(p) \
96#define GPU_TEXTURE_SAFE_FREE(p) \
99 GPU_texture_free(p); \
123#define STUDIOLIGHT_DELETE_ICON(s) \
126 BKE_icon_delete(s); \
138#undef STUDIOLIGHT_DELETE_ICON
156 if (is_used_in_viewport) {
185#define STUDIOLIGHT_FILE_VERSION 1
187#define READ_VAL(type, parser, id, val, lines) \
189 for (LinkNode *line = lines; line; line = line->next) { \
190 char *val_str, *str = static_cast<char *>(line->link); \
191 if ((val_str = strstr(str, id " "))) { \
192 val_str += sizeof(id); \
193 val = parser(val_str); \
198#define READ_FVAL(id, val, lines) READ_VAL(float, atof, id, val, lines)
199#define READ_IVAL(id, val, lines) READ_VAL(int, atoi, id, val, lines)
201#define READ_VEC3(id, val, lines) \
203 READ_FVAL(id ".x", val[0], lines); \
204 READ_FVAL(id ".y", val[1], lines); \
205 READ_FVAL(id ".z", val[2], lines); \
208#define READ_SOLIDLIGHT(sl, i, lines) \
210 READ_IVAL("light[" STRINGIFY(i) "].flag", sl[i].flag, lines); \
211 READ_FVAL("light[" STRINGIFY(i) "].smooth", sl[i].smooth, lines); \
212 READ_VEC3("light[" STRINGIFY(i) "].col", sl[i].col, lines); \
213 READ_VEC3("light[" STRINGIFY(i) "].spec", sl[i].spec, lines); \
214 READ_VEC3("light[" STRINGIFY(i) "].vec", sl[i].vec, lines); \
230#undef READ_SOLIDLIGHT
235#define WRITE_FVAL(str, id, val) BLI_dynstr_appendf(str, id " %f\n", val)
236#define WRITE_IVAL(str, id, val) BLI_dynstr_appendf(str, id " %d\n", val)
238#define WRITE_VEC3(str, id, val) \
240 WRITE_FVAL(str, id ".x", val[0]); \
241 WRITE_FVAL(str, id ".y", val[1]); \
242 WRITE_FVAL(str, id ".z", val[2]); \
245#define WRITE_SOLIDLIGHT(str, sl, i) \
247 WRITE_IVAL(str, "light[" STRINGIFY(i) "].flag", sl[i].flag); \
248 WRITE_FVAL(str, "light[" STRINGIFY(i) "].smooth", sl[i].smooth); \
249 WRITE_VEC3(str, "light[" STRINGIFY(i) "].col", sl[i].col); \
250 WRITE_VEC3(str, "light[" STRINGIFY(i) "].spec", sl[i].spec); \
251 WRITE_VEC3(str, "light[" STRINGIFY(i) "].vec", sl[i].vec); \
278#undef WRITE_SOLIDLIGHT
294 const char *pass_name,
295 const char *file_colorspace)
299 if (info.pass != pass_name) {
304 ImBuf *pass_ibuf = info.ibuf;
305 if (pass_ibuf ==
nullptr || pass_ibuf->
channels == 4) {
329 ImBuf *specular_ibuf =
nullptr;
330 ImBuf *diffuse_ibuf =
nullptr;
331 const bool failed = (ibuf ==
nullptr);
343 const char *file_colorspace = colorspace_settings.
name;
366 if (diffuse_ibuf ==
nullptr) {
369 const float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
370 const float magenta[4] = {1.0f, 0.0f, 1.0f, 1.0f};
372 nullptr, (failed || (specular_ibuf ==
nullptr)) ? magenta : black, 1, 1, 4);
378 if (specular_ibuf !=
nullptr) {
384 if (specular_ibuf !=
nullptr) {
400 "studiolight_radiance",
404 gpu::TextureFormat::SFLOAT_16_16_16_16,
421 const float (*offset4)[4] =
reinterpret_cast<const float (*)[4]
>(ibuf->
float_data());
422 float (*offset3)[3] =
reinterpret_cast<float (*)[3]
>(gpu_matcap_3components);
423 for (
size_t i = 0; i < ibuf_pixel_count; i++, offset4++, offset3++) {
431 gpu::TextureFormat::UFLOAT_11_11_10,
436 MEM_SAFE_DELETE(gpu_matcap_3components);
477static float brdf_approx(
float spec_color,
float roughness,
float NV)
481 float fresnel =
exp2(-8.35f * NV) * (1.0f - roughness);
482 return spec_color * (1.0f - fresnel) + fresnel;
488 float w_1 =
w + 1.0f;
489 return max_ff((
NL +
w) / (w_1 * w_1), 0.0f);
506 float gloss = 1.0f - roughness;
508 gloss *= 1.0f -
wrap;
509 float shininess =
exp2(10.0f * gloss + 1.0f);
513 float normalization_factor = shininess * 0.125f + 1.0f;
514 float spec_light =
powf(spec_angle, shininess) *
max_ff(
NL, 0.0f) * normalization_factor;
517 float w =
wrap * (1.0 - roughness) + roughness;
522 return spec_light * (1.0 - w2) + spec_env * w2;
528 float R[3],
I[3] = {0.0f, 0.0f, 1.0f},
N[3] = {normal[0], normal[2], -normal[1]};
529 const float roughness = 0.5f;
530 const float diffuse_color = 0.8f;
531 const float specular_color =
brdf_approx(0.05f, roughness,
N[2]);
532 float diff_light[3], spec_light[3];
553 mul_v3_fl(diff_light, diffuse_color * (1.0 - specular_color));
582 const char *subfolder,
593 for (i = 0; i < dirs_num; i++) {
594 if (dirs[i].type & S_IFREG) {
619 if (flagorder1 < flagorder2) {
622 if (flagorder1 > flagorder2) {
637 const float co[2] = {u - 0.5f,
v - 0.5f};
639 float alpha = 1.0f + (inner_edge - dist) / (outer_edge - inner_edge);
645#define STUDIOLIGHT_DIAMETER 0.95f
647#define RESCALE_COORD(x) (x / STUDIOLIGHT_DIAMETER - (1.0f - STUDIOLIGHT_DIAMETER) / 2.0f)
652 normal[0] = u * 2.0f - 1.0f;
653 normal[1] =
v * 2.0f - 1.0f;
654 float dist =
len_v2(normal);
667 if (alphamask != 0) {
668 float normal[3], direction[3];
669 const float incoming[3] = {0.0f, 0.0f, -1.0f};
673 std::swap(direction[1], direction[2]);
674 direction[1] = -direction[1];
681 *pixel =
rgb_to_cpack(srgb[0], srgb[1], srgb[2]) | alphamask;
704 float u = dx * diffuse_buffer->
x - 1.0f;
705 float v = dy * diffuse_buffer->
y - 1.0f;
708 if (specular_buffer) {
718 *pixel =
rgb_to_cpack(srgb[0], srgb[1], srgb[2]) | alphamask;
730 if (alphamask != 0) {
731 float normal[3],
color[3];
734 std::swap(normal[1], normal[2]);
735 normal[1] = -normal[1];
756 lights[0].
smooth = 0.526620f;
757 lights[0].
col[0] = 0.033103f;
758 lights[0].
col[1] = 0.033103f;
759 lights[0].
col[2] = 0.033103f;
760 lights[0].
spec[0] = 0.266761f;
761 lights[0].
spec[1] = 0.266761f;
762 lights[0].
spec[2] = 0.266761f;
763 lights[0].
vec[0] = -0.352546f;
764 lights[0].
vec[1] = 0.170931f;
765 lights[0].
vec[2] = -0.920051f;
768 lights[1].
smooth = 0.000000f;
769 lights[1].
col[0] = 0.521083f;
770 lights[1].
col[1] = 0.538226f;
771 lights[1].
col[2] = 0.538226f;
772 lights[1].
spec[0] = 0.599030f;
773 lights[1].
spec[1] = 0.599030f;
774 lights[1].
spec[2] = 0.599030f;
775 lights[1].
vec[0] = -0.408163f;
776 lights[1].
vec[1] = 0.346939f;
777 lights[1].
vec[2] = 0.844415f;
780 lights[2].
smooth = 0.478261f;
781 lights[2].
col[0] = 0.038403f;
782 lights[2].
col[1] = 0.034357f;
783 lights[2].
col[2] = 0.049530f;
784 lights[2].
spec[0] = 0.106102f;
785 lights[2].
spec[1] = 0.125981f;
786 lights[2].
spec[2] = 0.158523f;
787 lights[2].
vec[0] = 0.521739f;
788 lights[2].
vec[1] = 0.826087f;
789 lights[2].
vec[2] = 0.212999f;
792 lights[3].
smooth = 0.200000f;
793 lights[3].
col[0] = 0.090838f;
794 lights[3].
col[1] = 0.082080f;
795 lights[3].
col[2] = 0.072255f;
796 lights[3].
spec[0] = 0.106535f;
797 lights[3].
spec[1] = 0.084771f;
798 lights[3].
spec[2] = 0.066080f;
799 lights[3].
vec[0] = 0.624519f;
800 lights[3].
vec[1] = -0.562067f;
801 lights[3].
vec[2] = -0.542269f;
849 const char *default_name =
"";
859 if ((sl.flag & flag) &&
STREQ(sl.name, default_name)) {
865 if (sl.flag & flag) {
876 if (sl.flag & flag) {
891 if (sl.index ==
index) {
906 switch (icon_id_type) {
937 if ((sl->
flag & flag) == flag) {
975 const float light_ambient[3])
987 memcpy(sl->
light, light,
sizeof(*light) * 4);
988 memcpy(sl->
light_ambient, light_ambient,
sizeof(*light_ambient) * 3);
1001 memcpy(sl.
light,
U.light_param,
sizeof(*sl.
light) * 4);
#define STUDIOLIGHT_ICON_ID_TYPE_MATCAP
#define STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED
#define STUDIOLIGHT_ICON_ID_TYPE_RADIANCE
#define STUDIOLIGHT_ICON_SIZE
#define STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE
#define STUDIOLIGHT_MAX_LIGHT
A dynamically sized string ADT.
File and directory operations.
Some types for dealing with directories.
#define STRNCPY_RLEN(dst, src)
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define STREQLEN(a, b, n)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
std::string filename(const std::string_view &str, size_t pos)
BLI_INLINE Texture * wrap(MTLTexture *texture)
float4 interpolate_nearest_border_fl(const ImBuf *in, float u, float v)
MINLINE float min_ff(float a, float b)
void BKE_studiolight_preview(uint *icon_buffer, StudioLight *sl, int icon_id_type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static void studiolight_create_matcap_diffuse_gputexture(StudioLight *sl)
StudioLight * BKE_studiolight_create(const char *filepath, const SolidLight light[4], const float light_ambient[3])
@ STUDIOLIGHT_EXTERNAL_FILE
@ STUDIOLIGHT_USER_DEFINED
@ STUDIOLIGHT_TYPE_MATCAP
@ STUDIOLIGHT_MATCAP_SPECULAR_GPUTEXTURE
@ STUDIOLIGHT_TYPE_STUDIO
@ STUDIOLIGHT_MATCAP_DIFFUSE_GPUTEXTURE
@ STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE
@ STUDIOLIGHT_SPECULAR_HIGHLIGHT_PASS
@ STUDIOLIGHT_EXTERNAL_IMAGE_LOADED
char * STRNCPY(char(&dst)[N], const char *src)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
static const char * STUDIOLIGHT_MATCAP_FOLDER
static StudioLight * studiolight_add_file(const char *filepath, int flag)
void BKE_studiolight_init()
MINLINE void copy_v3_v3(float r[3], const float a[3])
static void studiolight_irradiance_preview(uint *icon_buffer, StudioLight *sl)
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
static StudioLight * studiolight_create(int flag)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
static void studiolight_lights_eval(StudioLight *sl, const float normal[3], float r_color[3])
static void studiolight_add_files_from_datafolder(const int folder_id, const char *subfolder, int flag)
void BKE_studiolight_refresh()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int last_studiolight_id
void BKE_studiolight_free()
static void studiolight_load_solid_light(StudioLight *sl)
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
static void studiolight_create_equirect_radiance_gputexture(StudioLight *sl)
static const char * STUDIOLIGHT_MATCAP_DEFAULT
BLI_INLINE float area_element(float x, float y)
int BKE_icon_ensure_studio_light(StudioLight *sl, int id_type)
void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id)
ATTR_WARN_UNUSED_RESULT const BMVert * v
const char * imb_ext_image[]
ExrReadHandle * IMB_exr_open_multilayer(const char *filepath)
unsigned int rgb_to_cpack(float r, float g, float b)
void BKE_studiolight_set_free_function(StudioLight *sl, StudioLightFreeFunction *free_function, void *data)
static ListBaseT< StudioLight > studiolights
static void studiolight_free_gpu_textures(StudioLight *sl)
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
void BKE_studiolight_default(SolidLight lights[4], float light_ambient[3])
MINLINE void mul_v3_fl(float r[3], float f)
struct StudioLight * BKE_studiolight_findindex(int index, int flag)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
struct StudioLight * BKE_studiolight_find(const char *name, int flag)
static float brdf_approx(float spec_color, float roughness, float NV)
VecBase< float, 4 > float4
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void studiolight_matcap_preview(uint *icon_buffer, StudioLight *sl, bool flipped)
void IMB_float_from_byte(ImBuf *ibuf)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
static void studiolight_write_solid_light(StudioLight *sl)
void IMB_freeImBuf(ImBuf *ibuf)
ListBaseT< StudioLight > & BKE_studiolight_listbase()
@ GPU_TEXTURE_USAGE_SHADER_READ
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
BMesh const char void * data
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
static const char * STUDIOLIGHT_WORLD_DEFAULT
static void studiolight_free(StudioLight *sl)
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void IMB_buffer_float_rgba_from_float(float *dest, const float *src, int src_channels, int width, int height)
static void studiolight_radiance_preview(uint *icon_buffer, StudioLight *sl)
static void direction_to_equirect(float r[2], const float dir[3])
static float blinn_specular(const float L[3], const float I[3], const float N[3], const float R[3], float NL, float roughness, float wrap)
static void studiolight_create_matcap_specular_gputexture(StudioLight *sl)
static const char * STUDIOLIGHT_LIGHTS_FOLDER
void reflect_v3_v3v3(float out[3], const float v[3], const float normal[3])
void IMB_exr_close(ExrReadHandle *handle)
static int studiolight_cmp(const void *a, const void *b)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void studiolight_free_image_buffers(StudioLight *sl)
void BKE_studiolight_remove(StudioLight *sl)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
struct StudioLight * BKE_studiolight_find_default(int flag)
void StudioLightFreeFunction(struct StudioLight *, void *data)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
StudioLight * BKE_studiolight_load(const char *filepath, int type)
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
static void studiolight_free_temp_resources(StudioLight *sl)
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
MINLINE float max_ff(float a, float b)
ImBuf * IMB_allocFromBuffer(const uint8_t *byte_buffer, const float *float_buffer, unsigned int w, unsigned int h, unsigned int channels)
static int studiolight_flag_cmp_order(const StudioLight *sl)
MINLINE float square_f(float a)
static void studiolight_load_equirect_image(StudioLight *sl)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
@ BLENDER_SYSTEM_DATAFILES
static const char * STUDIOLIGHT_WORLD_FOLDER
int BLI_dynstr_get_len(const DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_file_free_lines(struct LinkNode *lines)
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Vector< ExrPassInfo > IMB_exr_get_passes(ExrReadHandle *handle)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void studiolight_create_matcap_gputexture(StudioLightImage *sli)
static uint alpha_circle_mask(float u, float v, float inner_edge, float outer_edge)
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
float linearrgb_to_srgb(float c)
void IMB_exr_read_passes(ExrReadHandle *handle, MutableSpan< ExrPassInfo > entries, const char *src_colorspace=nullptr, bool predivide=false)
static float wrapped_lighting(float NL, float w)
static ImBuf * studiolight_read_matcap_pass(ExrReadHandle *handle, const char *pass_name, const char *file_colorspace)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
const char * IMB_colormanagement_working_space_get()
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
StudioLight * BKE_studiolight_studio_edit_get()
static float4 studiolight_calculate_radiance(const ImBuf *ibuf, const float direction[3])
static void sphere_normal_from_uv(float normal[3], float u, float v)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const float * float_data() const
float * float_data_for_write()
gpu::Texture * gputexture
StudioLightImage matcap_specular
StudioLightImage matcap_diffuse
std::string equirect_working_space
gpu::Texture * equirect_radiance_gputexture
void * free_function_data
struct ImBuf * equirect_radiance_buffer
SolidLight light[STUDIOLIGHT_MAX_LIGHT]
int icon_id_matcap_flipped
StudioLightFreeFunction * free_function
#define STUDIOLIGHT_FILE_VERSION
#define WRITE_IVAL(str, id, val)
#define STUDIOLIGHT_PASSNAME_DIFFUSE
#define WRITE_SOLIDLIGHT(str, sl, i)
#define ITER_PIXELS(type, src, channels, width, height)
#define WRITE_VEC3(str, id, val)
#define STUDIOLIGHT_DELETE_ICON(s)
#define STUDIOLIGHT_PASSNAME_SPECULAR
#define READ_VEC3(id, val, lines)
#define GPU_TEXTURE_SAFE_FREE(p)
#define READ_SOLIDLIGHT(sl, i, lines)