Skip to content

Commit

Permalink
Use start index of the instanced transform instead of the buffer devi…
Browse files Browse the repository at this point in the history
…ce address, and additionally pass the transform buffer as storage buffer.
  • Loading branch information
stripe2933 committed Jan 27, 2025
1 parent 00e2736 commit 3332184
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 48 deletions.
7 changes: 4 additions & 3 deletions impl/MainApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,10 @@ void vk_gltf_viewer::MainApp::loadGltf(const std::filesystem::path &path) {
}));
gpu.device.updateDescriptorSets({
sharedData.assetDescriptorSet.getWriteOne<0>({ gltf->assetGpuBuffers.getPrimitiveBuffer(), 0, vk::WholeSize }),
sharedData.assetDescriptorSet.getWrite<1>(gltf->nodeBuffer.descriptorInfo),
sharedData.assetDescriptorSet.getWriteOne<2>({ gltf->assetGpuBuffers.materialBuffer, 0, vk::WholeSize }),
sharedData.assetDescriptorSet.getWrite<3>(imageInfos),
sharedData.assetDescriptorSet.getWrite<1>(gltf->nodeBuffer.getDescriptorInfo()),
sharedData.assetDescriptorSet.getWrite<2>(gltf->instancedNodeWorldTransformBuffer.getDescriptorInfo()),
sharedData.assetDescriptorSet.getWriteOne<3>({ gltf->assetGpuBuffers.materialBuffer, 0, vk::WholeSize }),
sharedData.assetDescriptorSet.getWrite<4>(imageInfos),
}, {});

// TODO: due to the ImGui's gamma correction issue, base color/emissive texture is rendered darker than it should be.
Expand Down
14 changes: 9 additions & 5 deletions interface/vulkan/buffer/InstancedNodeWorldTransforms.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ namespace vk_gltf_viewer::vulkan::buffer {
) : asset { asset },
instanceOffsets { createInstanceOffsets() },
buffer { createBuffer(gpu.allocator, adapter) },
bufferAddress { gpu.device.getBufferAddress({ buffer }) } { }
descriptorInfo { buffer, 0, vk::WholeSize } { }

[[nodiscard]] vk::DeviceAddress getTransformStartAddress(std::size_t nodeIndex) const noexcept {
return bufferAddress + sizeof(fastgltf::math::fmat4x4) * instanceOffsets[nodeIndex];
[[nodiscard]] std::uint32_t getStartIndex(std::size_t nodeIndex) const noexcept {
return instanceOffsets[nodeIndex];
}

[[nodiscard]] const vk::DescriptorBufferInfo &getDescriptorInfo() const noexcept {
return descriptorInfo;
}

/**
Expand Down Expand Up @@ -121,7 +125,7 @@ namespace vk_gltf_viewer::vulkan::buffer {
* Be careful that there is no transform matrix related about node C, because it is meshless.
*/
vku::MappedBuffer buffer;
vk::DeviceAddress bufferAddress;
vk::DescriptorBufferInfo descriptorInfo;

[[nodiscard]] std::vector<std::uint32_t> createInstanceOffsets() const {
std::vector<std::uint32_t> result;
Expand Down Expand Up @@ -154,7 +158,7 @@ namespace vk_gltf_viewer::vulkan::buffer {
vku::MappedBuffer result { allocator, vk::BufferCreateInfo {
{},
sizeof(fastgltf::math::fmat4x4) * instanceOffsets.back(),
vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eShaderDeviceAddress,
vk::BufferUsageFlagBits::eStorageBuffer,
} };

const std::span data = result.asRange<fastgltf::math::fmat4x4>();
Expand Down
19 changes: 11 additions & 8 deletions interface/vulkan/buffer/Nodes.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ export import :vulkan.buffer.InstancedNodeWorldTransforms;

namespace vk_gltf_viewer::vulkan::buffer {
export class Nodes {
/**
* @brief Buffer with the start address of the instanced node world transform buffer.
*/
vku::AllocatedBuffer buffer;

public:
vk::DescriptorBufferInfo descriptorInfo;

Nodes(
const fastgltf::Asset &asset,
const InstancedNodeWorldTransforms &instancedNodeWorldTransformBuffer,
const Gpu &gpu [[clang::lifetimebound]]
) : buffer { createBuffer(asset, instancedNodeWorldTransformBuffer, gpu) },
descriptorInfo { buffer, 0, vk::WholeSize } { }

[[nodiscard]] const vk::DescriptorBufferInfo &getDescriptorInfo() const noexcept {
return descriptorInfo;
}

private:
/**
* @brief Buffer with the start index of the instanced node world transform buffer.
*/
vku::AllocatedBuffer buffer;
vk::DescriptorBufferInfo descriptorInfo;

[[nodiscard]] vku::AllocatedBuffer createBuffer(
const fastgltf::Asset &asset,
const InstancedNodeWorldTransforms &instancedNodeWorldTransformBuffer,
Expand All @@ -31,7 +34,7 @@ namespace vk_gltf_viewer::vulkan::buffer {
vku::AllocatedBuffer buffer = vku::MappedBuffer {
gpu.allocator,
std::from_range, ranges::views::upto(asset.nodes.size()) | std::views::transform([&](std::size_t nodeIndex) {
return instancedNodeWorldTransformBuffer.getTransformStartAddress(nodeIndex);
return instancedNodeWorldTransformBuffer.getStartIndex(nodeIndex);
}),
gpu.isUmaDevice ? vk::BufferUsageFlagBits::eStorageBuffer : vk::BufferUsageFlagBits::eTransferSrc,
}.unmap();
Expand Down
4 changes: 3 additions & 1 deletion interface/vulkan/descriptor_set_layout/Asset.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import std;
export import vku;

namespace vk_gltf_viewer::vulkan::dsl {
export struct Asset : vku::DescriptorSetLayout<vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eCombinedImageSampler> {
export struct Asset : vku::DescriptorSetLayout<vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eStorageBuffer, vk::DescriptorType::eCombinedImageSampler> {
Asset(const vk::raii::Device &device [[clang::lifetimebound]], std::uint32_t textureCount)
: DescriptorSetLayout { device, vk::StructureChain {
vk::DescriptorSetLayoutCreateInfo {
vk::DescriptorSetLayoutCreateFlagBits::eUpdateAfterBindPool,
vku::unsafeProxy(getBindings(
{ 1, vk::ShaderStageFlagBits::eVertex },
{ 1, vk::ShaderStageFlagBits::eVertex },
{ 1, vk::ShaderStageFlagBits::eVertex },
{ 1, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment },
Expand All @@ -20,6 +21,7 @@ namespace vk_gltf_viewer::vulkan::dsl {
{},
{},
{},
{},
vk::DescriptorBindingFlagBits::eUpdateAfterBind,
}),
},
Expand Down
7 changes: 4 additions & 3 deletions shaders/depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
#include "indexing.glsl"
#include "types.glsl"

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (location = 0) flat out uint outNodeIndex;

layout (set = 0, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 0, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 0, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};

layout (push_constant) uniform PushConstant {
Expand Down
3 changes: 2 additions & 1 deletion shaders/indexing.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define PRIMITIVE_INDEX gl_BaseInstance & 0xFFFFU
#define PRIMITIVE primitives[PRIMITIVE_INDEX]
#define NODE_INDEX gl_BaseInstance >> 16U
#define TRANSFORM Node(nodes[NODE_INDEX]).transforms[gl_InstanceIndex - gl_BaseInstance]
#define INSTANCE_INDEX gl_InstanceIndex - gl_BaseInstance
#define TRANSFORM instancedTransforms[instancedTransformStartIndices[NODE_INDEX] + INSTANCE_INDEX]
#define MATERIAL_INDEX PRIMITIVE.materialIndex
#define MATERIAL materials[MATERIAL_INDEX]

Expand Down
7 changes: 4 additions & 3 deletions shaders/jump_flood_seed.vert
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "indexing.glsl"
#include "types.glsl"

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (set = 0, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 0, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 0, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};

layout (push_constant) uniform PushConstant {
Expand Down
4 changes: 2 additions & 2 deletions shaders/mask_depth.frag
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ layout (location = 2) in FRAG_VARIADIC_IN {

layout (location = 0) out uint outNodeIndex;

layout (set = 0, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 0, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};
layout (set = 0, binding = 3) uniform sampler2D textures[];
layout (set = 0, binding = 4) uniform sampler2D textures[];

void main(){
float baseColorAlpha = MATERIAL.baseColorFactor.a;
Expand Down
9 changes: 5 additions & 4 deletions shaders/mask_depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (location = 0) flat out uint outNodeIndex;
layout (location = 1) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
Expand All @@ -36,9 +34,12 @@ layout (set = 0, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 0, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 0, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};
layout (set = 0, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 0, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};

Expand Down
4 changes: 2 additions & 2 deletions shaders/mask_jump_flood_seed.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ layout (location = 1) in FRAG_VARIDIC_IN {

layout (location = 0) out uvec2 outCoordinate;

layout (set = 0, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 0, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};
layout (set = 0, binding = 3) uniform sampler2D textures[];
layout (set = 0, binding = 4) uniform sampler2D textures[];

void main(){
float baseColorAlpha = MATERIAL.baseColorFactor.a;
Expand Down
9 changes: 5 additions & 4 deletions shaders/mask_jump_flood_seed.vert
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (location = 0) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
layout (location = 1) out VS_VARIADIC_OUT {
Expand All @@ -35,9 +33,12 @@ layout (set = 0, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 0, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 0, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};
layout (set = 0, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 0, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};

Expand Down
4 changes: 2 additions & 2 deletions shaders/primitive.frag
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ layout (set = 0, binding = 0, scalar) uniform SphericalHarmonicsBuffer {
layout (set = 0, binding = 1) uniform samplerCube prefilteredmap;
layout (set = 0, binding = 2) uniform sampler2D brdfmap;

layout (set = 1, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 1, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};
layout (set = 1, binding = 3) uniform sampler2D textures[];
layout (set = 1, binding = 4) uniform sampler2D textures[];

layout (push_constant, std430) uniform PushConstant {
mat4 projectionView;
Expand Down
9 changes: 5 additions & 4 deletions shaders/primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ layout (constant_id = 0) const uint PACKED_TEXCOORD_COMPONENT_TYPES = 0x06060606
layout (constant_id = 1) const uint COLOR_COMPONENT_COUNT = 0;
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (location = 0) out vec3 outPosition;
layout (location = 1) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
Expand Down Expand Up @@ -50,9 +48,12 @@ layout (set = 1, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 1, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 1, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};
layout (set = 1, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 1, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};

Expand Down
4 changes: 2 additions & 2 deletions shaders/unlit_primitive.frag
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ layout (location = 0) out vec4 outColor;
layout (location = 1) out float outRevealage;
#endif

layout (set = 1, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 1, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};
layout (set = 1, binding = 3) uniform sampler2D textures[];
layout (set = 1, binding = 4) uniform sampler2D textures[];

#if ALPHA_MODE == 0 || ALPHA_MODE == 2
layout (early_fragment_tests) in;
Expand Down
9 changes: 5 additions & 4 deletions shaders/unlit_primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_COUNT = 0;
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT

layout (std430, buffer_reference, buffer_reference_align = 64) readonly buffer Node { mat4 transforms[]; };

layout (location = 0) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
layout (location = 1) out VS_VARIADIC_OUT {
Expand All @@ -37,9 +35,12 @@ layout (set = 1, binding = 0) readonly buffer PrimitiveBuffer {
Primitive primitives[];
};
layout (set = 1, binding = 1, std430) readonly buffer NodeBuffer {
Node nodes[];
uint instancedTransformStartIndices[];
};
layout (set = 1, binding = 2) readonly buffer InstancedTransformBuffer {
mat4 instancedTransforms[];
};
layout (set = 1, binding = 2, std430) readonly buffer MaterialBuffer {
layout (set = 1, binding = 3, std430) readonly buffer MaterialBuffer {
Material materials[];
};

Expand Down

0 comments on commit 3332184

Please sign in to comment.