diff --git a/projects/voxelization/resources/shaders/lightInfo.inc b/projects/voxelization/resources/shaders/lightInfo.inc new file mode 100644 index 0000000000000000000000000000000000000000..4345d4f1504d27df7392b34bcaf17efdcfecef33 --- /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 14d3f8fa7dca5bc3c595f7ed8dbde8712ad7561a..2597492bc7a59c671fdf081aa2b30283127edb01 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 e29e0ff185e40eb97c74dcffa6084f369dd8f803..fe2b6a0ac9b443145735085a20a3c8cee4012e3c 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 19e31e2d2d032b5a9e5c273f6420c6449be9203e..56542d960d65db6ca12c5f84837cb0c0a9ff0ded 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 7a43c08b64d3df384d3a7e627d789db9be99f680..1302a42441b5b9c8ea7d24f97d29b684e4d64993 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 b04fab9a2b61430d24bd7171d9fd3637dd428699..6c7d996774faa0b4f30eefbf9ffbdbf21f72bfa1 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 f9b96998b39e24a3481d130efa68ebaa813b8256..270ac0514c78d6f7e9a59f123cd55279306d4105 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 ea4321a9f27fa424dddd7fa311a42e158e60bd0b..c916c5c958cf5ca843d08c277b1a384ea56b3567 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);