From ff56b1811d2ba83095f1211d0948a499b77762f1 Mon Sep 17 00:00:00 2001 From: Lava Block <the@lava-block.com> Date: Sat, 22 May 2021 13:01:43 +0200 Subject: [PATCH] formatting --- liblava/resource/format.cpp | 3 +- res/lamp/lamp.frag | 61 +++++++++++++++++------------------ res/lamp/lamp.vert | 8 ++--- res/light/data.inc | 9 ++---- res/light/gbuffer.frag | 28 +++++++---------- res/light/gbuffer.vert | 24 +++++++------- res/light/lighting.frag | 63 ++++++++++++++----------------------- res/light/lighting.vert | 5 ++- res/spawn/spawn.frag | 11 +++---- res/spawn/spawn.vert | 28 ++++++++--------- res/tool/imgui/imgui.frag | 7 ++--- res/tool/imgui/imgui.vert | 14 ++++----- res/triangle/triangle.frag | 7 ++--- res/triangle/triangle.vert | 12 +++---- 14 files changed, 123 insertions(+), 157 deletions(-) diff --git a/liblava/resource/format.cpp b/liblava/resource/format.cpp index 37d936d9..b910c030 100644 --- a/liblava/resource/format.cpp +++ b/liblava/resource/format.cpp @@ -28,8 +28,7 @@ bool lava::format_depth_stencil(VkFormat format) { return format_depth(format) || format_stencil(format); } -bool lava::format_srgb(VkFormat format) -{ +bool lava::format_srgb(VkFormat format) { switch (format) { case VK_FORMAT_R8_SRGB: case VK_FORMAT_R8G8_SRGB: diff --git a/res/lamp/lamp.frag b/res/lamp/lamp.frag index 6c822e53..b5a44e02 100644 --- a/res/lamp/lamp.frag +++ b/res/lamp/lamp.frag @@ -2,12 +2,12 @@ layout(location = 0) out vec4 fragColor; -layout(push_constant) uniform uPushConstant -{ +layout(push_constant) uniform uPushConstant { vec2 uResolution; vec2 uTimeDepth; vec4 uColor; -} pc; +} +pc; // Barberella ... by Weyland Yutani, dedicated to Jane Fonda and Verner Panton // Based on Metatunnel by FRequency, really old, might not work on your gpu @@ -17,40 +17,41 @@ layout(push_constant) uniform uPushConstant float h(vec3 q) // distance function { - float f=1.; + float f = 1.; // blobs - f*=distance(q,vec3(-sin(pc.uTimeDepth.x*.181)*.5,sin(pc.uTimeDepth.x*.253),1.)); - f*=distance(q,vec3(-sin(pc.uTimeDepth.x*.252)*.5,sin(pc.uTimeDepth.x*.171),1.)); - f*=distance(q,vec3(-sin(pc.uTimeDepth.x*.133)*.5,sin(pc.uTimeDepth.x*.283),1.)); - f*=distance(q,vec3(-sin(pc.uTimeDepth.x*.264)*.5,sin(pc.uTimeDepth.x*.145),1.)); + f *= distance(q, vec3(-sin(pc.uTimeDepth.x * 0.181) * 0.5, sin(pc.uTimeDepth.x * 0.253), 1.0)); + f *= distance(q, vec3(-sin(pc.uTimeDepth.x * 0.252) * 0.5, sin(pc.uTimeDepth.x * 0.171), 1.0)); + f *= distance(q, vec3(-sin(pc.uTimeDepth.x * 0.133) * 0.5, sin(pc.uTimeDepth.x * 0.283), 1.0)); + f *= distance(q, vec3(-sin(pc.uTimeDepth.x * 0.264) * 0.5, sin(pc.uTimeDepth.x * 0.145), 1.0)); // room - f*=(cos(q.y))*(cos(q.z)+1.)*(cos(q.x+cos(q.z*3.))+1.)-.21+cos(q.z*6.+pc.uTimeDepth.x/20.)*cos(q.x*5.)*cos(q.y*4.5)*.3; + f *= (cos(q.y)) * (cos(q.z) + 1.0) * (cos(q.x + cos(q.z * 3.0)) + 1.0) - 0.21 + cos(q.z * 6.0 + pc.uTimeDepth.x / 20.0) * cos(q.x * 5.0) * cos(q.y * 4.5) * 0.3; return f; } -void main() -{ - vec2 p=-1.+2.*gl_FragCoord.xy / pc.uResolution.xy; - vec3 o=vec3(p.x,p.y*1.25-0.3,0.); - vec3 d=vec3(p.x+cos(pc.uTimeDepth.x/20.)*0.3,p.y,1.)/64.; - vec4 c=vec4(0.); - float t=0.; - for(int i=0;i<25;i++) // march +void main() { + vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / pc.uResolution.xy; + vec3 o = vec3(p.x, p.y * 1.25 - 0.3, 0.0); + vec3 d = vec3(p.x + cos(pc.uTimeDepth.x / 20.0) * 0.3, p.y, 1.0) / 64.0; + vec4 c = vec4(0.0); + float t = 0.0; + for (int i = 0; i < 25; ++i) // march { - if(h(o+d*t)<.4) - { - t-=5.; - for(int j=0;j<5;j++) { if(h(o+d*t)>.4) t+=1.; } // scatter - vec3 e=vec3(.01,.0,.0); - vec3 n=vec3(.0); - n.x=h(o+d*t)-h(vec3(o+d*t+e.xyy)); - n.y=h(o+d*t)-h(vec3(o+d*t+e.yxy)); - n.z=h(o+d*t)-h(vec3(o+d*t+e.yyx)); - n=normalize(n); - c+=max(dot(vec3(.0,.0,-.15),n),.0)+max(dot(vec3(.0,-.15,.15),n),.0)*.155; + if (h(o + d * t) < 0.4) { + t -= 5.0; + for (int j = 0; j < 5; ++j) { + if (h(o + d * t) > 0.4) + t += 1.; + } // scatter + vec3 e = vec3(0.01, 0.0, 0.0); + vec3 n = vec3(0.0); + n.x = h(o + d * t) - h(vec3(o + d * t + e.xyy)); + n.y = h(o + d * t) - h(vec3(o + d * t + e.yxy)); + n.z = h(o + d * t) - h(vec3(o + d * t + e.yyx)); + n = normalize(n); + c += max(dot(vec3(0.0, 0.0, -0.15), n), 0.0) + max(dot(vec3(0.0, -0.15, 0.15), n), 0.0) * 0.155; break; } - t+=5.; + t += 5.0; } - fragColor=c+vec4(pc.uColor.r,pc.uColor.g,pc.uColor.b,pc.uColor.a)*(t*pc.uTimeDepth.y); // fleshtones + fragColor = c + vec4(pc.uColor.r, pc.uColor.g, pc.uColor.b, pc.uColor.a) * (t * pc.uTimeDepth.y); // fleshtones } diff --git a/res/lamp/lamp.vert b/res/lamp/lamp.vert index ec71a851..ac8146c1 100644 --- a/res/lamp/lamp.vert +++ b/res/lamp/lamp.vert @@ -1,12 +1,10 @@ #version 450 core -out gl_PerVertex -{ +out gl_PerVertex { vec4 gl_Position; }; -void main() -{ +void main() { vec2 outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); - gl_Position = vec4(outUV * 2.0f + -1.0f, 0.0f, 1.0f); + gl_Position = vec4(outUV * 2.0 + -1.0, 0.0, 1.0); } diff --git a/res/light/data.inc b/res/light/data.inc index a61b8bbb..d5379a76 100644 --- a/res/light/data.inc +++ b/res/light/data.inc @@ -1,5 +1,4 @@ -struct UboData -{ +struct UboData { vec3 camPos; uint lightCount; mat4 view; @@ -8,16 +7,14 @@ struct UboData uvec2 resolution; }; -struct PushConstantData -{ +struct PushConstantData { mat4 model; vec3 color; float metallic; uint enableNormalMapping; }; -struct LightData -{ +struct LightData { vec3 position; float radius; vec3 intensity; diff --git a/res/light/gbuffer.frag b/res/light/gbuffer.frag index 17904f11..86368138 100644 --- a/res/light/gbuffer.frag +++ b/res/light/gbuffer.frag @@ -3,26 +3,24 @@ #include "data.inc" -layout (location = 0) in vec3 inPos; -layout (location = 1) in vec2 inUV; -layout (location = 2) in vec3 inNormal; +layout(location = 0) in vec3 inPos; +layout(location = 1) in vec2 inUV; +layout(location = 2) in vec3 inNormal; -layout(push_constant) uniform PushConstants -{ +layout(push_constant) uniform PushConstants { PushConstantData pc; }; -layout (binding = 1) uniform sampler2D samplerNormal; -layout (binding = 2) uniform sampler2D samplerRoughness; +layout(binding = 1) uniform sampler2D samplerNormal; +layout(binding = 2) uniform sampler2D samplerRoughness; -layout (location = 0) out vec4 outAlbedo; -layout (location = 1) out vec4 outNormal; -layout (location = 2) out vec2 outMetallicRoughness; +layout(location = 0) out vec4 outAlbedo; +layout(location = 1) out vec4 outNormal; +layout(location = 2) out vec2 outMetallicRoughness; // Schlüter 2013. Normal Mapping Without Precomputed Tangents. // http://www.thetenthplanet.de/archives/1180 -mat3 cotangentFrame(vec3 N, vec3 pos, vec2 uv) -{ +mat3 cotangentFrame(vec3 N, vec3 pos, vec2 uv) { vec3 dPx = dFdx(pos); vec3 dPy = dFdy(pos); vec2 dTx = dFdx(uv); @@ -39,11 +37,9 @@ mat3 cotangentFrame(vec3 N, vec3 pos, vec2 uv) return mat3(T * invmax, B * invmax, N); } -void main() -{ +void main() { vec3 normal = normalize(inNormal); - if(pc.enableNormalMapping != 0) - { + if (pc.enableNormalMapping != 0) { mat3 TBN = cotangentFrame(normal, inPos, inUV); vec3 tangentNormal = texture(samplerNormal, inUV).xyz * 2.0 - 1.0; normal = normalize(TBN * tangentNormal); diff --git a/res/light/gbuffer.vert b/res/light/gbuffer.vert index c1a53258..7916f9fd 100644 --- a/res/light/gbuffer.vert +++ b/res/light/gbuffer.vert @@ -3,32 +3,30 @@ #include "data.inc" -layout (location = 0) in vec3 inPos; -layout (location = 1) in vec2 inUV; -layout (location = 2) in vec3 inNormal; +layout(location = 0) in vec3 inPos; +layout(location = 1) in vec2 inUV; +layout(location = 2) in vec3 inNormal; -layout(push_constant) uniform PushConstants -{ +layout(push_constant) uniform PushConstants { PushConstantData pc; }; -layout (binding = 0) uniform Ubo -{ +layout(binding = 0) uniform Ubo { UboData ubo; }; -layout (location = 0) out vec3 outPos; -layout (location = 1) out vec2 outUV; -layout (location = 2) out vec3 outNormal; +layout(location = 0) out vec3 outPos; +layout(location = 1) out vec2 outUV; +layout(location = 2) out vec3 outNormal; -void main() -{ +void main() { outPos = vec3(pc.model * vec4(inPos, 1.0)); outUV = inUV; + outNormal = normalize(mat3(pc.model) * inNormal); // correctly render double-sided materials vec3 V = normalize(ubo.camPos - outPos); - if(dot(outNormal, V) < 0.0) + if (dot(outNormal, V) < 0.0) outNormal = -outNormal; gl_Position = ubo.projection * ubo.view * vec4(outPos, 1.0); diff --git a/res/light/lighting.frag b/res/light/lighting.frag index a6e800d0..54273367 100644 --- a/res/light/lighting.frag +++ b/res/light/lighting.frag @@ -3,18 +3,16 @@ #include "data.inc" -layout (binding = 0) uniform sampler2D samplerGbufferAlbedo; -layout (binding = 1) uniform sampler2D samplerGbufferNormal; -layout (binding = 2) uniform sampler2D samplerGbufferMetallicRoughness; -layout (binding = 3) uniform sampler2D samplerGbufferDepth; +layout(binding = 0) uniform sampler2D samplerGbufferAlbedo; +layout(binding = 1) uniform sampler2D samplerGbufferNormal; +layout(binding = 2) uniform sampler2D samplerGbufferMetallicRoughness; +layout(binding = 3) uniform sampler2D samplerGbufferDepth; -layout (binding = 4) uniform Ubo -{ +layout(binding = 4) uniform Ubo { UboData ubo; }; -layout (binding = 5) restrict readonly buffer Sbo_Lights -{ +layout(binding = 5) restrict readonly buffer Sbo_Lights { LightData lights[]; }; @@ -28,8 +26,7 @@ layout(location = 0) out vec4 outFragColor; #define INV_PI 0.31831 -struct Material -{ +struct Material { vec3 diffuse; vec3 f0; float a; @@ -37,16 +34,14 @@ struct Material // Schlick approximation to Fresnel equation using F90 = 1 // Schlick 1994. An Inexpensive BRDF Model for Physically based Rendering. -vec3 F_Schlick(float VoH, vec3 F0) -{ +vec3 F_Schlick(float VoH, vec3 F0) { float f = pow(1.0 - VoH, 5.0); return f + F0 * (1.0 - f); } // GGX Normal Distribution Function // Bruce Walter et al. 2007. Microfacet Models for Refraction through Rough Surfaces. -float D_GGX(float NoH, float a) -{ +float D_GGX(float NoH, float a) { a = NoH * a; float k = a / (1.0 - NoH * NoH + a * a); return k * k * INV_PI; @@ -55,8 +50,7 @@ float D_GGX(float NoH, float a) // GGX Geometric Shadowing/Occlusion Function, based on Smith approach // height-correlated joint masking-shadowing function // Heitz 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. -float V_SmithGGXCorrelated(float NoV, float NoL, float a) -{ +float V_SmithGGXCorrelated(float NoV, float NoL, float a) { float a2 = a * a; float GGXV = NoL * sqrt(NoV * NoV * (1.0 - a2) + a2); float GGXL = NoV * sqrt(NoL * NoL * (1.0 - a2) + a2); @@ -64,14 +58,12 @@ float V_SmithGGXCorrelated(float NoV, float NoL, float a) } // Lambertian diffuse BRDF -vec3 diffuseBrdf(Material mat, float VoH) -{ +vec3 diffuseBrdf(Material mat, float VoH) { vec3 F = F_Schlick(VoH, mat.f0); return (1.0 - F) * mat.diffuse * INV_PI; } -vec3 specularBrdf(Material mat, float NoH, float VoH, float NoV, float NoL) -{ +vec3 specularBrdf(Material mat, float NoH, float VoH, float NoV, float NoL) { float D = D_GGX(NoH, mat.a); vec3 F = F_Schlick(VoH, mat.f0); float V = V_SmithGGXCorrelated(NoV, NoL, mat.a); @@ -80,8 +72,7 @@ vec3 specularBrdf(Material mat, float NoH, float VoH, float NoV, float NoL) } // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#appendix-b-brdf-implementation -vec3 brdf(Material mat, vec3 v, vec3 l, vec3 n) -{ +vec3 brdf(Material mat, vec3 v, vec3 l, vec3 n) { vec3 h = normalize(l + v); float NoV = abs(dot(n, v)) + 1e-5; @@ -93,22 +84,19 @@ vec3 brdf(Material mat, vec3 v, vec3 l, vec3 n) } // inverse square falloff -float distanceAttenuation(float distance) -{ +float distanceAttenuation(float distance) { return 1.0 / max(distance * distance, 0.01 * 0.01); } // windowing function with smooth transition to 0 // radius is arbitrary // Karis 2014. Real Shading in Unreal Engine 4. -float distanceAttenuation(float distance, float radius) -{ +float distanceAttenuation(float distance, float radius) { float nom = clamp(1.0 - pow(distance / radius, 4.0), 0.0, 1.0); return nom * nom * distanceAttenuation(distance); } -vec3 screenToEye(vec3 fragCoord, uvec2 resolution, mat4 invProjection) -{ +vec3 screenToEye(vec3 fragCoord, uvec2 resolution, mat4 invProjection) { vec3 ndc = vec3( 2.0 * fragCoord.xy / vec2(resolution) - 1.0, // -> [-1;1] fragCoord.z // [0;1] @@ -120,19 +108,17 @@ vec3 screenToEye(vec3 fragCoord, uvec2 resolution, mat4 invProjection) // luminance-only fit of ACES LDR output transform // Narkowicz 2016. ACES Filmic Tone Mapping Curve. // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ -vec3 tonemap(vec3 x) -{ +vec3 tonemap(vec3 x) { return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); } -void main() -{ +void main() { vec2 uv = gl_FragCoord.xy / vec2(ubo.resolution); vec3 albedo = texture(samplerGbufferAlbedo, uv).rgb; vec2 metallicRoughness = texture(samplerGbufferMetallicRoughness, uv).xy; float metallic = metallicRoughness.x; float roughness = metallicRoughness.y; - + // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-pbr_mat const vec3 dielectricSpecular = vec3(0.04, 0.04, 0.04); @@ -152,20 +138,19 @@ void main() vec3 N = texture(samplerGbufferNormal, uv).xyz; vec3 radiance = vec3(0.0); - for(uint i = 0; i < ubo.lightCount; i++) - { + for (uint i = 0; i < ubo.lightCount; ++i) { LightData light = lights[i]; float dist = distance(light.position, pos); float attenuation = distanceAttenuation(dist, light.radius); - if(attenuation > 0.0) - { + if (attenuation > 0.0) { vec3 L = normalize(light.position - pos); vec3 radiance_in = light.intensity * attenuation; + float NoL = clamp(dot(N, L), 0.0, 1.0); - if(NoL > 0.0) + if (NoL > 0.0) radiance += brdf(mat, V, L, N) * radiance_in * NoL; } } - + outFragColor = vec4(tonemap(radiance), 1.0); } diff --git a/res/light/lighting.vert b/res/light/lighting.vert index 7bec00d3..06854601 100644 --- a/res/light/lighting.vert +++ b/res/light/lighting.vert @@ -1,7 +1,6 @@ #version 450 core -void main() -{ +void main() { vec2 uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); - gl_Position = vec4(uv * 2.0f + -1.0f, 0.0f, 1.0f); + gl_Position = vec4(uv * 2.0 + -1.0, 0.0, 1.0); } diff --git a/res/spawn/spawn.frag b/res/spawn/spawn.frag index f1274fb2..fefb49bb 100644 --- a/res/spawn/spawn.frag +++ b/res/spawn/spawn.frag @@ -1,13 +1,12 @@ #version 450 core -layout (binding = 2) uniform sampler2D samplerColor; +layout(binding = 2) uniform sampler2D samplerColor; -layout (location = 0) in vec4 inColor; -layout (location = 1) in vec2 inUV; +layout(location = 0) in vec4 inColor; +layout(location = 1) in vec2 inUV; -layout (location = 0) out vec4 outFragColor; +layout(location = 0) out vec4 outFragColor; -void main() -{ +void main() { outFragColor = inColor * texture(samplerColor, inUV); } diff --git a/res/spawn/spawn.vert b/res/spawn/spawn.vert index 2aeba39b..31ba2874 100644 --- a/res/spawn/spawn.vert +++ b/res/spawn/spawn.vert @@ -1,30 +1,28 @@ #version 450 core -layout (location = 0) in vec3 inPos; -layout (location = 1) in vec4 inColor; -layout (location = 2) in vec2 inUV; +layout(location = 0) in vec3 inPos; +layout(location = 1) in vec4 inColor; +layout(location = 2) in vec2 inUV; -layout (binding = 0) uniform Ubo_Camera -{ +layout(binding = 0) uniform Ubo_Camera { mat4 projection; mat4 view; -} ubo_camera; +} +ubo_camera; -layout (binding = 1) uniform Ubo_Spawn -{ +layout(binding = 1) uniform Ubo_Spawn { mat4 model; -} ubo_spawn; +} +ubo_spawn; -layout (location = 0) out vec4 outColor; -layout (location = 1) out vec2 outUV; +layout(location = 0) out vec4 outColor; +layout(location = 1) out vec2 outUV; -out gl_PerVertex -{ +out gl_PerVertex { vec4 gl_Position; }; -void main() -{ +void main() { outColor = inColor; outUV = inUV; diff --git a/res/tool/imgui/imgui.frag b/res/tool/imgui/imgui.frag index 0a3c8dca..090ee1fa 100644 --- a/res/tool/imgui/imgui.frag +++ b/res/tool/imgui/imgui.frag @@ -2,14 +2,13 @@ layout(location = 0) out vec4 fColor; -layout(set=0, binding=0) uniform sampler2D sTexture; +layout(set = 0, binding = 0) uniform sampler2D sTexture; -layout(location = 0) in struct{ +layout(location = 0) in struct { vec4 Color; vec2 UV; } In; -void main() -{ +void main() { fColor = In.Color * texture(sTexture, In.UV.st); } diff --git a/res/tool/imgui/imgui.vert b/res/tool/imgui/imgui.vert index 88fe3647..9a577d77 100644 --- a/res/tool/imgui/imgui.vert +++ b/res/tool/imgui/imgui.vert @@ -4,23 +4,23 @@ layout(location = 0) in vec2 aPos; layout(location = 1) in vec2 aUV; layout(location = 2) in vec4 aColor; -layout(push_constant) uniform uPushConstant{ +layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; -} pc; +} +pc; -out gl_PerVertex{ +out gl_PerVertex { vec4 gl_Position; }; -layout(location = 0) out struct{ +layout(location = 0) out struct { vec4 Color; vec2 UV; } Out; -void main() -{ +void main() { Out.Color = aColor; Out.UV = aUV; - gl_Position = vec4(aPos*pc.uScale+pc.uTranslate, 0, 1); + gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1); } diff --git a/res/triangle/triangle.frag b/res/triangle/triangle.frag index be3c04f6..bde0d385 100644 --- a/res/triangle/triangle.frag +++ b/res/triangle/triangle.frag @@ -1,10 +1,9 @@ #version 450 core -layout (location = 0) in vec4 inColor; +layout(location = 0) in vec4 inColor; -layout (location = 0) out vec4 outFragColor; +layout(location = 0) out vec4 outFragColor; -void main() -{ +void main() { outFragColor = inColor; } diff --git a/res/triangle/triangle.vert b/res/triangle/triangle.vert index 45a9d5c0..e44440ee 100644 --- a/res/triangle/triangle.vert +++ b/res/triangle/triangle.vert @@ -1,17 +1,15 @@ #version 450 core -layout (location = 0) in vec3 inPos; -layout (location = 1) in vec4 inColor; +layout(location = 0) in vec3 inPos; +layout(location = 1) in vec4 inColor; -layout (location = 0) out vec4 outColor; +layout(location = 0) out vec4 outColor; -out gl_PerVertex -{ +out gl_PerVertex { vec4 gl_Position; }; -void main() -{ +void main() { outColor = inColor; gl_Position = vec4(inPos.xyz, 1.0); } -- GitLab