diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2020-07-30 14:55:13 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2020-07-30 17:44:58 +0300 |
commit | da741013a1e1b2e7c4f61a9486588253a457afd2 (patch) | |
tree | b491c53eadf1fcc07e2a326d000b1c94fbf07f48 /source/blender/gpu/shaders | |
parent | 55401fbb3d0daa82c999e388c2b5b87e7113b091 (diff) |
EEVEE: GLSL refactor/cleanup
- add the use of DRWShaderLibrary to EEVEE's glsl codebase to reduce code
complexity and duplication.
- split bsdf_common_lib.glsl into multiple sub library which are now shared
with other engines.
- the surface shader code is now more organised and have its own files.
- change default world to use a material nodetree and make lookdev shader
more clear.
Reviewed By: jbakker
Differential Revision: https://developer.blender.org/D8306
Diffstat (limited to 'source/blender/gpu/shaders')
4 files changed, 101 insertions, 2 deletions
diff --git a/source/blender/gpu/shaders/gpu_shader_codegen_lib.glsl b/source/blender/gpu/shaders/gpu_shader_codegen_lib.glsl new file mode 100644 index 00000000000..f7bf3d33361 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_codegen_lib.glsl @@ -0,0 +1,87 @@ + +vec3 calc_barycentric_distances(vec3 pos0, vec3 pos1, vec3 pos2) +{ + vec3 edge21 = pos2 - pos1; + vec3 edge10 = pos1 - pos0; + vec3 edge02 = pos0 - pos2; + vec3 d21 = normalize(edge21); + vec3 d10 = normalize(edge10); + vec3 d02 = normalize(edge02); + + vec3 dists; + float d = dot(d21, edge02); + dists.x = sqrt(dot(edge02, edge02) - d * d); + d = dot(d02, edge10); + dists.y = sqrt(dot(edge10, edge10) - d * d); + d = dot(d10, edge21); + dists.z = sqrt(dot(edge21, edge21) - d * d); + return dists; +} + +vec2 calc_barycentric_co(int vertid) +{ + vec2 bary; + bary.x = float((vertid % 3) == 0); + bary.y = float((vertid % 3) == 1); + return bary; +} + +#ifdef HAIR_SHADER + +/* Hairs uv and col attributes are passed by bufferTextures. */ +# define DEFINE_ATTR(type, attr) uniform samplerBuffer attr +# define GET_ATTR(type, attr) hair_get_customdata_##type(attr) + +# define barycentric_get() hair_get_barycentric() +# define barycentric_resolve(bary) hair_resolve_barycentric(bary) + +vec3 orco_get(vec3 local_pos, mat4 modelmatinv, vec4 orco_madd[2], const samplerBuffer orco_samp) +{ + /* TODO: fix ORCO with modifiers. */ + vec3 orco = (modelmatinv * vec4(local_pos, 1.0)).xyz; + return orco_madd[0].xyz + orco * orco_madd[1].xyz; +} + +vec4 tangent_get(const samplerBuffer attr, mat3 normalmat) +{ + /* Unsupported */ + return vec4(0.0); +} + +#else /* MESH_SHADER */ + +# define DEFINE_ATTR(type, attr) in type attr +# define GET_ATTR(type, attr) attr + +/* Calculated in geom shader later with calc_barycentric_co. */ +# define barycentric_get() vec2(0) +# define barycentric_resolve(bary) bary + +vec3 orco_get(vec3 local_pos, mat4 modelmatinv, vec4 orco_madd[2], vec4 orco) +{ + /* If the object does not have any deformation, the orco layer calculation is done on the fly + * using the orco_madd factors. + * We know when there is no orco layer when orco.w is 1.0 because it uses the generic vertex + * attrib (which is [0,0,0,1]). */ + if (orco.w == 0.0) { + return orco.xyz * 0.5 + 0.5; + } + else { + return orco_madd[0].xyz + local_pos * orco_madd[1].xyz; + } +} + +vec4 tangent_get(vec4 attr, mat3 normalmat) +{ + vec4 tangent; + tangent.xyz = normalmat * attr.xyz; + tangent.w = attr.w; + float len_sqr = dot(tangent.xyz, tangent.xyz); + /* Normalize only if vector is not null. */ + if (len_sqr > 0.0) { + tangent.xyz *= inversesqrt(len_sqr); + } + return tangent; +} + +#endif diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_ambient_occlusion.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_ambient_occlusion.glsl index d6d6fbab971..eea8d19efce 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_ambient_occlusion.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_ambient_occlusion.glsl @@ -3,7 +3,7 @@ void node_ambient_occlusion( vec4 color, float distance, vec3 normal, out vec4 result_color, out float result_ao) { vec3 bent_normal; - vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); + vec4 rand = texelfetch_noise_tex(gl_FragCoord.xy); result_ao = occlusion_compute(normalize(normal), viewPosition, 1.0, rand, bent_normal); result_color = result_ao * color; } diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_hair_info.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_hair_info.glsl index 3b23ac976ae..6330daa4391 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_hair_info.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_hair_info.glsl @@ -1,3 +1,15 @@ + +float wang_hash_noise(uint s) +{ + s = (s ^ 61u) ^ (s >> 16u); + s *= 9u; + s = s ^ (s >> 4u); + s *= 0x27d4eb2du; + s = s ^ (s >> 15u); + + return fract(float(s) / 4294967296.0); +} + void node_hair_info(out float is_strand, out float intercept, out float thickness, diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_world_normals.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_world_normals.glsl index f9691beee6f..d33465fa846 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_world_normals.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_world_normals.glsl @@ -6,7 +6,7 @@ void world_normals_get(out vec3 N) vec3 B = normalize(cross(worldNormal, hairTangent)); float cos_theta; if (hairThicknessRes == 1) { - vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); + vec4 rand = texelfetch_noise_tex(gl_FragCoord.xy); /* Random cosine normal distribution on the hair surface. */ cos_theta = rand.x * 2.0 - 1.0; } |