58 #define MAX_COLOR_BAND 128
140 printf(
"Too many color band in shader! Remove some Curve, Black Body or Color Ramp Node.\n");
144 memcpy(dst, pixels,
sizeof(
float) * (
CM_TABLE + 1) * 4);
238 const char *name =
"Material";
246 #define SSS_SAMPLES 65
247 #define SSS_EXPONENT 2.0f
261 for (
int i = 0; i <
count; i++) {
262 float o = ((
float)i) * step - 1.0f;
263 float sign = (o < 0.0f) ? -1.0f : 1.0f;
265 kd->kernel[i][3] = ofs;
269 #define GAUSS_TRUNCATE 12.46f
272 const float v = radius * radius * (0.25f * 0.25f);
281 #define BURLEY_TRUNCATE 16.0f
282 #define BURLEY_TRUNCATE_CDF 0.9963790093708328f
285 float exp_r_3_d =
expf(-
r / (3.0f * d));
286 float exp_r_d = exp_r_3_d * exp_r_3_d * exp_r_3_d;
287 return (exp_r_d + exp_r_3_d) / (4.0f * d);
292 float Rm = radius * (1.0f + sharpness);
298 const float y = 1.0f / (1.0f + sharpness);
299 float Rmy, ry, ryinv;
303 ryinv = (
r > 0.0f) ?
powf(
r,
y - 1.0f) : 0.0f;
305 const float Rmy5 = (Rmy * Rmy) * (Rmy * Rmy) * Rmy;
306 const float f = Rmy - ry;
307 const float num = f * (f * f) * (
y * ryinv);
309 return (10.0f * num) / (Rmy5 *
M_PI);
312 static float eval_profile(
float r,
short falloff_type,
float sharpness,
float param)
327 #define INTEGRAL_RESOLUTION 32
328 static float eval_integral(
float x0,
float x1,
short falloff_type,
float sharpness,
float param)
330 const float range = x1 - x0;
332 float integral = 0.0f;
337 integral +=
y * step;
342 #undef INTEGRAL_RESOLUTION
345 GPUSssKernelData *kd,
const float radii[3],
int sample_len,
int falloff_type,
float sharpness)
349 rad[0] =
MAX2(radii[0], 1e-15f);
350 rad[1] =
MAX2(radii[1], 1e-15f);
351 rad[2] =
MAX2(radii[2], 1e-15f);
358 const float A = 1.0f;
359 const float s = 1.9f -
A + 3.5f * (
A - 0.8f) * (
A - 0.8f);
382 float sum[3] = {0.0f, 0.0f, 0.0f};
385 for (
int i = 0; i < sample_len; i++) {
395 if (i == sample_len - 1) {
396 x1 = kd->
kernel[sample_len - 1][3] +
415 for (
int i = 0; i < 3; i++) {
418 for (
int j = 0; j < sample_len; j++) {
424 kd->
kernel[sample_len / 2][i] = 1.0f;
431 for (
int i = sample_len / 2; i > 0; i--) {
439 #define INTEGRAL_RESOLUTION 512
447 texels =
MEM_callocN(
sizeof(
float[4]) * resolution,
"compute_sss_translucence_kernel");
448 *
output = (
float *)texels;
451 for (
int i = 0; i < resolution - 1; i++) {
459 float area_accum = 0.0f;
460 for (
float r = 0.0f;
r < r_fp;
r += r_step) {
463 float dist =
hypotf(
r + r_step * 0.5f, d);
472 float r_next =
r + r_step;
473 float disk_area = (
M_PI * r_next * r_next) - (
M_PI *
r *
r);
477 area_accum += disk_area;
480 mul_v3_fl(texels[i], 1.0f / (area_accum));
484 for (
int j = resolution - 2; j > 0; j--) {
485 texels[j][0] /= (texels[0][0] > 0.0f) ? texels[0][0] : 1.0f;
486 texels[j][1] /= (texels[0][1] > 0.0f) ? texels[0][1] : 1.0f;
487 texels[j][2] /= (texels[0][2] > 0.0f) ? texels[0][2] : 1.0f;
491 texels[0][0] = (texels[0][0] > 0.0f) ? 1.0f : 0.0f;
492 texels[0][1] = (texels[0][1] > 0.0f) ? 1.0f : 0.0f;
493 texels[0][2] = (texels[0][2] > 0.0f) ? 1.0f : 0.0f;
496 mul_v3_fl(texels[resolution - 2], 0.25f);
498 mul_v3_fl(texels[resolution - 4], 0.75f);
500 #undef INTEGRAL_RESOLUTION
504 const short *falloff_type,
505 const float *sharpness)
508 material->sss_falloff = (falloff_type) ? *falloff_type : 0.0;
509 material->sss_sharpness = (sharpness) ? *sharpness : 0.0;
530 float sharpness =
material->sss_sharpness;
541 float *translucence_profile;
543 &kd, 64,
material->sss_falloff, sharpness, &translucence_profile);
550 "sss_tex_profile", 64, 1,
GPU_RGBA16F, translucence_profile);
558 if (tex_profile !=
NULL) {
559 *tex_profile =
material->sss_tex_profile;
584 return material->graph.volume_grids;
648 return (mat->
flag & flag) != 0;
652 const void *engine_type,
658 return current_material;
674 const void *engine_type,
676 const bool is_volume_shader,
677 const char *vert_code,
678 const char *geom_code,
679 const char *frag_lib,
685 bool has_volume_output, has_surface_output;
725 "#ifndef USE_ALPHA_BLEND\n"
typedef float(TangentPoint)[2]
General operations, lookup, etc. for materials.
void BKE_material_defaults_free_gpu(void)
#define BLI_STATIC_ASSERT_ALIGN(st, align)
unsigned int BLI_ghashutil_ptrhash(const void *key)
GSet * BLI_gset_new(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghashutil_ptrcmp(const void *a, const void *b)
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
#define LISTBASE_FOREACH(type, var, list)
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define BLI_string_joinN(...)
#define UNUSED_VARS_NDEBUG(...)
@ SHD_SUBSURFACE_RANDOM_WALK
void(* GPUMaterialEvalCallbackFn)(struct GPUMaterial *mat, int options, const char **vert_code, const char **geom_code, const char **frag_lib, const char **defines)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
struct GPUShader GPUShader
GPUTexture * GPU_texture_create_1d_array(const char *name, int w, int h, int mip_len, eGPUTextureFormat format, const float *data)
GPUTexture * GPU_texture_create_1d(const char *name, int w, int mip_len, eGPUTextureFormat format, const float *data)
struct GPUTexture GPUTexture
void GPU_texture_free(GPUTexture *tex)
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert * v
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
static T sum(const btAlignedObjectArray< T > &items)
CCL_NAMESPACE_BEGIN struct Options options
DEGForeachIDComponentCallback callback
void GPU_pass_release(GPUPass *pass)
GPUPass * GPU_generate_pass(GPUMaterial *material, GPUNodeGraph *graph, const char *vert_code, const char *geom_code, const char *frag_lib, const char *defines)
bool GPU_pass_compile(GPUPass *pass, const char *shname)
GPUShader * GPU_pass_shader_get(GPUPass *pass)
static void gpu_material_ramp_texture_build(GPUMaterial *mat)
static void gpu_material_free_single(GPUMaterial *material)
static void compute_sss_kernel(GPUSssKernelData *kd, const float radii[3], int sample_len, int falloff_type, float sharpness)
GSet * gpu_material_used_libraries(GPUMaterial *material)
GPUNodeGraph * gpu_material_node_graph(GPUMaterial *material)
GPUUniformAttrList * GPU_material_uniform_attributes(GPUMaterial *material)
void GPU_material_compile(GPUMaterial *mat)
void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link)
bool GPU_material_has_surface_output(GPUMaterial *mat)
GPUMaterial * GPU_material_from_nodetree(Scene *scene, struct Material *ma, struct bNodeTree *ntree, ListBase *gpumaterials, const void *engine_type, const int options, const bool is_volume_shader, const char *vert_code, const char *geom_code, const char *frag_lib, const char *defines, const char *name, GPUMaterialEvalCallbackFn callback)
static float eval_integral(float x0, float x1, short falloff_type, float sharpness, float param)
void GPU_materials_free(Main *bmain)
void GPU_material_free(ListBase *gpumaterial)
static float burley_profile(float r, float d)
struct GPUColorBandBuilder GPUColorBandBuilder
void GPU_material_add_output_link_aov(GPUMaterial *material, GPUNodeLink *link, int hash)
static float cubic_profile(float r, float radius, float sharpness)
GPUShader * GPU_material_get_shader(GPUMaterial *material)
ListBase GPU_material_textures(GPUMaterial *material)
GPUUniformBuf * GPU_material_uniform_buffer_get(GPUMaterial *material)
struct GPUUniformBuf * GPU_material_sss_profile_get(GPUMaterial *material, int sample_len, GPUTexture **tex_profile)
GPUTexture ** gpu_material_ramp_texture_row_set(GPUMaterial *mat, int size, float *pixels, float *row)
GPUPass * GPU_material_get_pass(GPUMaterial *material)
static void compute_sss_translucence_kernel(const GPUSssKernelData *kd, int resolution, short falloff_type, float sharpness, float **output)
static float eval_profile(float r, short falloff_type, float sharpness, float param)
GPUMaterial * GPU_material_from_nodetree_find(ListBase *gpumaterials, const void *engine_type, int options)
#define BURLEY_TRUNCATE_CDF
Scene * GPU_material_scene(GPUMaterial *material)
eGPUMaterialStatus GPU_material_status(GPUMaterial *mat)
bool GPU_material_is_volume_shader(GPUMaterial *mat)
struct GPUUniformBuf * GPU_material_create_sss_profile_ubo(void)
Material * GPU_material_get_material(GPUMaterial *material)
void GPU_material_flag_set(GPUMaterial *mat, eGPUMatFlag flag)
struct GPUSssKernelData GPUSssKernelData
ListBase GPU_material_volume_grids(GPUMaterial *material)
ListBase GPU_material_attributes(GPUMaterial *material)
static void sss_calculate_offsets(GPUSssKernelData *kd, int count, float exponent)
static float gaussian_profile(float r, float radius)
void GPU_material_sss_profile_create(GPUMaterial *material, float radii[3], const short *falloff_type, const float *sharpness)
#define INTEGRAL_RESOLUTION
void GPU_material_uniform_buffer_create(GPUMaterial *material, ListBase *inputs)
bool GPU_material_has_volume_output(GPUMaterial *mat)
bool GPU_material_flag_get(GPUMaterial *mat, eGPUMatFlag flag)
void gpu_node_graph_free(GPUNodeGraph *graph)
void gpu_node_graph_free_nodes(GPUNodeGraph *graph)
void * ntreeFreeLocalTree
void * DRW_deferred_shader_remove
void * ntreeGPUMaterialNodes
void(* MEM_freeN)(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
void *(* MEM_mallocN)(size_t len, const char *str)
static bNodeSocketTemplate inputs[]
float pixels[MAX_COLOR_BAND][CM_TABLE+1][4]
GPUColorBandBuilder * coba_builder
GPUTexture * sss_tex_profile
GPUUniformBuf * sss_profile
eGPUMaterialStatus status
float kernel[SSS_SAMPLES][4]