diff --git a/projects/cmd_sync_test/resources/shaders/voxelInfo.inc b/projects/cmd_sync_test/resources/shaders/voxelInfo.inc new file mode 100644 index 0000000000000000000000000000000000000000..c8ef9c6c7272a44b212a9578fd5aa234e23924c2 --- /dev/null +++ b/projects/cmd_sync_test/resources/shaders/voxelInfo.inc @@ -0,0 +1,4 @@ +struct VoxelInfo{ + vec3 offset; + float extent; +}; \ No newline at end of file diff --git a/projects/cmd_sync_test/resources/shaders/voxelVisualisation.vert b/projects/cmd_sync_test/resources/shaders/voxelVisualisation.vert index 270b9ac58a386b257a42eddc3d10530638bfa0e8..9884087de536eddd827ee443a30d64a8c65317a7 100644 --- a/projects/cmd_sync_test/resources/shaders/voxelVisualisation.vert +++ b/projects/cmd_sync_test/resources/shaders/voxelVisualisation.vert @@ -1,5 +1,8 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable +#extension GL_GOOGLE_include_directive : enable + +#include "voxelInfo.inc" layout(location = 0) out float passCubeHalf; @@ -9,8 +12,8 @@ layout( push_constant ) uniform constants{ layout(set=0, binding=0, r8) uniform image3D voxelImage; layout(set=0, binding=1) uniform voxelizationInfo{ - float extent; -} voxelInfo; + VoxelInfo voxelInfo; +}; void main() { @@ -21,7 +24,7 @@ void main() { int index2D = gl_VertexIndex % slicePixelCount; int y = index2D / voxelResolution; int x = index2D % voxelResolution; - vec3 position = (vec3(x, y, z) / voxelResolution - 0.5) * voxelInfo.extent + passCubeHalf; + vec3 position = (vec3(x, y, z) / voxelResolution - 0.5) * voxelInfo.extent + passCubeHalf + voxelInfo.offset; gl_Position = vec4(position, 1.0); if(imageLoad(voxelImage, ivec3(x,y,z)).x == 0){ diff --git a/projects/cmd_sync_test/resources/shaders/voxelVisualisation_vert.spv b/projects/cmd_sync_test/resources/shaders/voxelVisualisation_vert.spv index 52a320bb31926efffb96e7384f76836ca1690dec..da76418305c145be8297310747e894fcbc6a78a3 100644 Binary files a/projects/cmd_sync_test/resources/shaders/voxelVisualisation_vert.spv and b/projects/cmd_sync_test/resources/shaders/voxelVisualisation_vert.spv differ diff --git a/projects/cmd_sync_test/resources/shaders/voxelization.frag b/projects/cmd_sync_test/resources/shaders/voxelization.frag index 5f5d7eff538a2a971fa2d8ad872ee1154dea90d3..0b946fbb8664da0a2300725d26595ca7372f2ac2 100644 --- a/projects/cmd_sync_test/resources/shaders/voxelization.frag +++ b/projects/cmd_sync_test/resources/shaders/voxelization.frag @@ -1,15 +1,18 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable +#extension GL_GOOGLE_include_directive : enable + +#include "voxelInfo.inc" layout(location = 0) in vec3 passPos; layout(set=0, binding=0, r8) uniform image3D voxelImage; layout(set=0, binding=1) uniform voxelizationInfo{ - float extent; -} voxelInfo; + VoxelInfo voxelInfo; +}; -vec3 worldToVoxelCoordinates(vec3 world, float voxelExtent){ - return world / voxelExtent + 0.5f; +vec3 worldToVoxelCoordinates(vec3 world, VoxelInfo info){ + return (world - info.offset) / info.extent + 0.5f; } ivec3 voxelCoordinatesToUV(vec3 voxelCoordinates, ivec3 voxelImageResolution){ @@ -17,7 +20,7 @@ ivec3 voxelCoordinatesToUV(vec3 voxelCoordinates, ivec3 voxelImageResolution){ } void main() { - vec3 voxelCoordinates = worldToVoxelCoordinates(passPos, voxelInfo.extent); + vec3 voxelCoordinates = worldToVoxelCoordinates(passPos, voxelInfo); ivec3 voxeImageSize = imageSize(voxelImage); ivec3 UV = voxelCoordinatesToUV(voxelCoordinates, voxeImageSize); if(any(lessThan(UV, ivec3(0))) || any(greaterThanEqual(UV, voxeImageSize))){ diff --git a/projects/cmd_sync_test/resources/shaders/voxelization_frag.spv b/projects/cmd_sync_test/resources/shaders/voxelization_frag.spv index 814f72aa23d799583abc63f1facb2a29f045cf3c..23871e8e9b3e7b0e7beac2ab075e400c1338c04f 100644 Binary files a/projects/cmd_sync_test/resources/shaders/voxelization_frag.spv and b/projects/cmd_sync_test/resources/shaders/voxelization_frag.spv differ diff --git a/projects/cmd_sync_test/src/main.cpp b/projects/cmd_sync_test/src/main.cpp index ddc6420f7c71124575852789811ad1d64daa7722..132b62482bf9dcd68a3cc34f14c09ba86544caa8 100644 --- a/projects/cmd_sync_test/src/main.cpp +++ b/projects/cmd_sync_test/src/main.cpp @@ -219,13 +219,10 @@ int main(int argc, const char** argv) { const vkcv::PipelineHandle voxelizationPipe = core.createGraphicsPipeline(voxelizationPipeConfig); struct VoxelizationInfo { + glm::vec3 offset; float extent; }; vkcv::Buffer voxelizationInfoBuffer = core.createBuffer<VoxelizationInfo>(vkcv::BufferType::UNIFORM, sizeof(VoxelizationInfo)); - const float voxelizationExtent = 10.f; - VoxelizationInfo voxelizationInfo; - voxelizationInfo.extent = voxelizationExtent; - voxelizationInfoBuffer.fill({ voxelizationInfo }); vkcv::DescriptorWrites voxelizationDescriptorWrites; voxelizationDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(0, voxelImage.getHandle()) }; @@ -362,6 +359,17 @@ int main(int argc, const char** argv) { const glm::mat4 viewProjectionCamera = cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView(); + const float voxelizationExtent = 20.f; + VoxelizationInfo voxelizationInfo; + voxelizationInfo.extent = voxelizationExtent; + + // move voxel offset with camera in voxel sized steps + const glm::vec3 cameraPos = cameraManager.getCamera().getPosition(); + const float voxelSize = voxelizationExtent / voxelResolution; + voxelizationInfo.offset = glm::floor(cameraPos / voxelSize) * voxelSize; + + voxelizationInfoBuffer.fill({ voxelizationInfo }); + const float voxelizationHalfExtent = 0.5f * voxelizationExtent; const glm::mat4 voxelizationProjection = glm::ortho( -voxelizationHalfExtent, @@ -371,6 +379,9 @@ int main(int argc, const char** argv) { -voxelizationHalfExtent, voxelizationHalfExtent); + const glm::mat4 voxelizationView = glm::translate(glm::mat4(1.f), -voxelizationInfo.offset); + const glm::mat4 voxelizationViewProjection = voxelizationProjection * voxelizationView; + // compute positions and transform matrices std::vector<glm::vec3> instancePositions = { glm::vec3(0.f, -2.f, 0.f), @@ -398,7 +409,7 @@ int main(int argc, const char** argv) { for (const auto& m : modelMatrices) { mainPassMatrices.push_back({ viewProjectionCamera * m, m }); mvpLight.push_back(lightInfo.lightMatrix * m); - voxelizationMatrices.push_back({ voxelizationProjection * m, m }); + voxelizationMatrices.push_back({ voxelizationViewProjection * m, m }); } vkcv::PushConstantData pushConstantData((void*)mainPassMatrices.data(), 2 * sizeof(glm::mat4)); diff --git a/src/vkcv/ShaderProgram.cpp b/src/vkcv/ShaderProgram.cpp index 7c54c301d1301127273303128b0c10a9c2c53942..a33fa7127dc6071439b5b350a440f995b1440ab7 100644 --- a/src/vkcv/ShaderProgram.cpp +++ b/src/vkcv/ShaderProgram.cpp @@ -142,7 +142,8 @@ namespace vkcv { std::pair descriptor(comp.get_decoration(u.id, spv::DecorationDescriptorSet), DescriptorBinding(comp.get_decoration(u.id, spv::DecorationBinding), DescriptorType::UNIFORM_BUFFER, base_type.vecsize, shaderStage)); bindings.push_back(descriptor); - if (comp.get_decoration(u.id, spv::DecorationDescriptorSet) > maxSetID) maxSetID = comp.get_decoration(u.id, spv::DecorationDescriptorSet); + if ((int32_t)comp.get_decoration(u.id, spv::DecorationDescriptorSet) > maxSetID) + maxSetID = comp.get_decoration(u.id, spv::DecorationDescriptorSet); } for (uint32_t i = 0; i < resources.storage_buffers.size(); i++) {