From fa597eb8d579e723ab531173fbb2a9d72151c435 Mon Sep 17 00:00:00 2001 From: Alexander Gauggel <agauggel@uni-koblenz.de> Date: Sat, 19 Jun 2021 10:56:17 +0200 Subject: [PATCH] [#81] Add normal and sun direction to voxelization --- .../voxelization/resources/shaders/lightInfo.inc | 6 ++++++ projects/voxelization/resources/shaders/shader.frag | 10 ++++------ .../resources/shaders/voxelization.frag | 13 ++++++++++++- .../resources/shaders/voxelization.geom | 3 +++ .../resources/shaders/voxelization.vert | 2 ++ projects/voxelization/src/Voxelization.cpp | 7 +++++-- projects/voxelization/src/Voxelization.hpp | 2 +- projects/voxelization/src/main.cpp | 2 +- 8 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 projects/voxelization/resources/shaders/lightInfo.inc diff --git a/projects/voxelization/resources/shaders/lightInfo.inc b/projects/voxelization/resources/shaders/lightInfo.inc new file mode 100644 index 00000000..4345d4f1 --- /dev/null +++ b/projects/voxelization/resources/shaders/lightInfo.inc @@ -0,0 +1,6 @@ +struct LightInfo{ + vec3 L; float padding; + vec3 sunColor; + float sunStrength; + mat4 lightMatrix; +}; \ No newline at end of file diff --git a/projects/voxelization/resources/shaders/shader.frag b/projects/voxelization/resources/shaders/shader.frag index 14d3f8fa..2597492b 100644 --- a/projects/voxelization/resources/shaders/shader.frag +++ b/projects/voxelization/resources/shaders/shader.frag @@ -3,6 +3,7 @@ #extension GL_GOOGLE_include_directive : enable #include "perMeshResources.inc" +#include "lightInfo.inc" layout(location = 0) in vec3 passNormal; layout(location = 1) in vec2 passUV; @@ -11,16 +12,13 @@ layout(location = 2) in vec3 passPos; layout(location = 0) out vec3 outColor; layout(set=0, binding=0) uniform sunBuffer { - vec3 L; float padding; - vec3 sunColor; - float sunStrength; - mat4 lightMatrix; + LightInfo lightInfo; }; layout(set=0, binding=1) uniform texture2D shadowMap; layout(set=0, binding=2) uniform sampler shadowMapSampler; float shadowTest(vec3 worldPos){ - vec4 lightPos = lightMatrix * vec4(worldPos, 1); + vec4 lightPos = lightInfo.lightMatrix * vec4(worldPos, 1); lightPos /= lightPos.w; lightPos.xy = lightPos.xy * 0.5 + 0.5; @@ -38,7 +36,7 @@ float shadowTest(vec3 worldPos){ void main() { vec3 N = normalize(passNormal); - vec3 sun = sunStrength * sunColor * clamp(dot(N, L), 0, 1); + vec3 sun = lightInfo.sunStrength * lightInfo.sunColor * clamp(dot(N, lightInfo.L), 0, 1); sun *= shadowTest(passPos); vec3 ambient = vec3(0.05); vec3 albedo = texture(sampler2D(albedoTexture, textureSampler), passUV).rgb; diff --git a/projects/voxelization/resources/shaders/voxelization.frag b/projects/voxelization/resources/shaders/voxelization.frag index e29e0ff1..fe2b6a0a 100644 --- a/projects/voxelization/resources/shaders/voxelization.frag +++ b/projects/voxelization/resources/shaders/voxelization.frag @@ -4,9 +4,11 @@ #include "voxel.inc" #include "perMeshResources.inc" +#include "lightInfo.inc" layout(location = 0) in vec3 passPos; layout(location = 1) out vec2 passUV; +layout(location = 2) in vec3 passN; layout(set=0, binding=0, std430) buffer voxelizationBuffer{ uint packedVoxelData[]; @@ -18,6 +20,10 @@ layout(set=0, binding=1) uniform voxelizationInfo{ layout(set=0, binding=2, r8) uniform image3D voxelImage; +layout(set=0, binding=3) uniform sunBuffer { + LightInfo lightInfo; +}; + vec3 worldToVoxelCoordinates(vec3 world, VoxelInfo info){ return (world - info.offset) / info.extent + 0.5f; } @@ -38,6 +44,11 @@ void main() { // for some reason the automatic mip level here does not work // biasing does not work either // as a workaround a fixed, high mip level is chosen - vec3 color = textureLod(sampler2D(albedoTexture, textureSampler), passUV, 10.f).rgb; + vec3 albedo = textureLod(sampler2D(albedoTexture, textureSampler), passUV, 10.f).rgb; + + vec3 N = normalize(passN); + float NoL = clamp(dot(N, lightInfo.L), 0, 1); + vec3 color = albedo * NoL; + atomicMax(packedVoxelData[flatIndex], packVoxelInfo(color)); } \ No newline at end of file diff --git a/projects/voxelization/resources/shaders/voxelization.geom b/projects/voxelization/resources/shaders/voxelization.geom index 19e31e2d..56542d96 100644 --- a/projects/voxelization/resources/shaders/voxelization.geom +++ b/projects/voxelization/resources/shaders/voxelization.geom @@ -6,9 +6,11 @@ layout (triangle_strip, max_vertices = 3) out; layout(location = 0) in vec3 passPosIn[3]; layout(location = 1) in vec2 passUVIn[3]; +layout(location = 2) in vec3 passNIn[3]; layout(location = 0) out vec3 passPos; layout(location = 1) out vec2 passUV; +layout(location = 2) out vec3 passN; void main() { // compute geometric normal, no normalization necessary @@ -29,6 +31,7 @@ void main() { gl_Position.z = gl_Position.z * 0.5 + 0.5; // xyz are kept in NDC range [-1, 1] so swizzling works, but vulkan needs final z in range [0, 1] passPos = passPosIn[i]; passUV = passUVIn[i]; + passN = passNIn[i]; EmitVertex(); } EndPrimitive(); diff --git a/projects/voxelization/resources/shaders/voxelization.vert b/projects/voxelization/resources/shaders/voxelization.vert index 7a43c08b..1302a424 100644 --- a/projects/voxelization/resources/shaders/voxelization.vert +++ b/projects/voxelization/resources/shaders/voxelization.vert @@ -7,6 +7,7 @@ layout(location = 2) in vec2 inUV; layout(location = 0) out vec3 passPos; layout(location = 1) out vec2 passUV; +layout(location = 2) out vec3 passN; layout( push_constant ) uniform constants{ mat4 mvp; @@ -17,4 +18,5 @@ void main() { gl_Position = mvp * vec4(inPosition, 1.0); passPos = (model * vec4(inPosition, 1)).xyz; passUV = inUV; + passN = inNormal; } \ No newline at end of file diff --git a/projects/voxelization/src/Voxelization.cpp b/projects/voxelization/src/Voxelization.cpp index b04fab9a..6c7d9967 100644 --- a/projects/voxelization/src/Voxelization.cpp +++ b/projects/voxelization/src/Voxelization.cpp @@ -62,7 +62,7 @@ const uint32_t voxelResolution = 128; uint32_t voxelCount = voxelResolution * voxelResolution * voxelResolution; const vk::Format voxelizationDummyFormat = vk::Format::eR8Unorm; -Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies) +Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies, vkcv::BufferHandle lightInfoBuffer) : m_corePtr(corePtr), m_voxelImage(m_corePtr->createImage(vk::Format::eR16G16B16A16Sfloat, voxelResolution, voxelResolution, voxelResolution, false, true)), @@ -100,7 +100,10 @@ Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies vkcv::DescriptorWrites voxelizationDescriptorWrites; voxelizationDescriptorWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0, m_voxelBuffer.getHandle()) }; - voxelizationDescriptorWrites.uniformBufferWrites = { vkcv::UniformBufferDescriptorWrite(1, m_voxelInfoBuffer.getHandle()) }; + voxelizationDescriptorWrites.uniformBufferWrites = { + vkcv::UniformBufferDescriptorWrite(1, m_voxelInfoBuffer.getHandle()), + vkcv::UniformBufferDescriptorWrite(3, lightInfoBuffer) + }; voxelizationDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_voxelImage.getHandle()) }; m_corePtr->writeDescriptorSet(m_voxelizationDescriptorSet, voxelizationDescriptorWrites); diff --git a/projects/voxelization/src/Voxelization.hpp b/projects/voxelization/src/Voxelization.hpp index f9b96998..270ac051 100644 --- a/projects/voxelization/src/Voxelization.hpp +++ b/projects/voxelization/src/Voxelization.hpp @@ -9,7 +9,7 @@ public: vk::Format colorBufferFormat; vk::Format depthBufferFormat; }; - Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies); + Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies, vkcv::BufferHandle lightInfoBuffer); void voxelizeMeshes( vkcv::CommandStreamHandle cmdStream, diff --git a/projects/voxelization/src/main.cpp b/projects/voxelization/src/main.cpp index ea4321a9..c916c5c9 100644 --- a/projects/voxelization/src/main.cpp +++ b/projects/voxelization/src/main.cpp @@ -319,7 +319,7 @@ int main(int argc, const char** argv) { voxelDependencies.colorBufferFormat = colorBufferFormat; voxelDependencies.depthBufferFormat = depthBufferFormat; voxelDependencies.vertexLayout = vertexLayout; - Voxelization voxelization(&core, voxelDependencies); + Voxelization voxelization(&core, voxelDependencies, lightBuffer.getHandle()); vkcv::gui::GUI gui(core, window); -- GitLab