Skip to content

Commit

Permalink
Embed the component type and count in IndexedAttributeBufferInfo as 8…
Browse files Browse the repository at this point in the history
…-bit unsigned integers.
  • Loading branch information
stripe2933 committed Jan 28, 2025
1 parent b7e2a0c commit 872851c
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 69 deletions.
7 changes: 3 additions & 4 deletions impl/gltf/AssetGpuBuffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ bool vk_gltf_viewer::gltf::AssetGpuBuffers::updatePrimitiveMaterial(
return false;
},
[&](vk::Buffer primitiveBuffer) {
transferCommandBuffer.updateBuffer(
transferCommandBuffer.updateBuffer<std::uint32_t>(
primitiveBuffer,
sizeof(GpuPrimitive) * orderedPrimitiveIndex + offsetof(GpuPrimitive, materialIndex),
sizeof(GpuPrimitive::materialIndex),
&paddedMaterialIndex);
paddedMaterialIndex);
return true;
}
}, primitiveBuffer);
Expand Down Expand Up @@ -72,7 +71,7 @@ std::variant<vku::AllocatedBuffer, vku::MappedBuffer> vk_gltf_viewer::gltf::Asse
const auto tangentInfo = primitiveInfo.tangentInfo.value_or(AssetPrimitiveInfo::AttributeBufferInfo{});

// If color is not presented, it is not used in the shader. Therefore, it is okay to pass nullptr into shaders.
const auto colorInfo = primitiveInfo.colorInfo.value_or(AssetPrimitiveInfo::ColorAttributeBufferInfo{});
const auto colorInfo = primitiveInfo.colorInfo.value_or(AssetPrimitiveInfo::AttributeBufferInfo{});

return GpuPrimitive {
.pPositionBuffer = primitiveInfo.positionInfo.address,
Expand Down
14 changes: 7 additions & 7 deletions impl/vulkan/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ auto vk_gltf_viewer::vulkan::Frame::update(const ExecutionTask &task) -> UpdateR
return primitiveInfo.texcoordsInfo.attributeInfos.at(textureInfo.texCoordIndex).componentType;
}),
.colorComponentCountAndType = primitiveInfo.colorInfo.transform([](const auto &info) {
return std::pair { info.numComponent, info.componentType };
return std::pair { info.componentCount, info.componentType };
}),
.baseColorTextureTransform = material.pbrData.baseColorTexture
.transform(fetchTextureTransform)
Expand All @@ -153,9 +153,9 @@ auto vk_gltf_viewer::vulkan::Frame::update(const ExecutionTask &task) -> UpdateR
return info.componentType;
})
| std::views::take(4) // Avoid bad_alloc for static_vector.
| std::ranges::to<boost::container::static_vector<fastgltf::ComponentType, 4>>(),
| std::ranges::to<boost::container::static_vector<std::uint8_t, 4>>(),
.colorComponentCountAndType = primitiveInfo.colorInfo.transform([](const auto &info) {
return std::pair { info.numComponent, info.componentType };
return std::pair { info.componentCount, info.componentType };
}),
.fragmentShaderGeneratedTBN = !primitiveInfo.normalInfo.has_value(),
.baseColorTextureTransform = material.pbrData.baseColorTexture
Expand Down Expand Up @@ -185,9 +185,9 @@ auto vk_gltf_viewer::vulkan::Frame::update(const ExecutionTask &task) -> UpdateR
return info.componentType;
})
| std::views::take(4) // Avoid bad_alloc for static_vector.
| std::ranges::to<boost::container::static_vector<fastgltf::ComponentType, 4>>(),
| std::ranges::to<boost::container::static_vector<std::uint8_t, 4>>(),
.colorComponentCountAndType = primitiveInfo.colorInfo.transform([](const auto &info) {
return std::pair { info.numComponent, info.componentType };
return std::pair { info.componentCount, info.componentType };
}),
.fragmentShaderGeneratedTBN = !primitiveInfo.normalInfo.has_value(),
});
Expand All @@ -214,7 +214,7 @@ auto vk_gltf_viewer::vulkan::Frame::update(const ExecutionTask &task) -> UpdateR
}),
.colorAlphaComponentType = primitiveInfo.colorInfo.and_then([](const auto &info) {
// Alpha value exists only if COLOR_0 is Vec4 type.
return value_if(info.numComponent == 4, info.componentType);
return value_if(info.componentCount == 4, info.componentType);
}),
});
}
Expand Down Expand Up @@ -248,7 +248,7 @@ auto vk_gltf_viewer::vulkan::Frame::update(const ExecutionTask &task) -> UpdateR
}),
.colorAlphaComponentType = primitiveInfo.colorInfo.and_then([](const auto &info) {
// Alpha value exists only if COLOR_0 is Vec4 type.
return value_if(info.numComponent == 4, info.componentType);
return value_if(info.componentCount == 4, info.componentType);
}),
});
}
Expand Down
5 changes: 3 additions & 2 deletions interface/gltf/AssetGpuBuffers.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,9 @@ namespace vk_gltf_viewer::gltf {
if (!std::in_range<std::uint8_t>(byteStride)) throw AssetProcessError::TooLargeAccessorByteStride;
return {
.address = bufferDeviceAddressMappings.at(*accessor.bufferViewIndex) + accessor.byteOffset,
.componentType = static_cast<std::uint8_t>(getGLComponentType(accessor.componentType) - getGLComponentType(fastgltf::ComponentType::Byte)),
.componentCount = static_cast<std::uint8_t>(getNumComponents(accessor.type)),
.byteStride = static_cast<std::uint8_t>(byteStride),
.componentType = accessor.componentType,
};
};

Expand Down Expand Up @@ -359,7 +360,7 @@ namespace vk_gltf_viewer::gltf {
primitiveInfo.texcoordsInfo.attributeInfos[index] = getAttributeBufferInfo();
}
else if (attributeName == "COLOR_0"sv) {
primitiveInfo.colorInfo.emplace(getAttributeBufferInfo(), getNumComponents(accessor.type));
primitiveInfo.colorInfo.emplace(getAttributeBufferInfo());
}
}
}
Expand Down
21 changes: 18 additions & 3 deletions interface/gltf/AssetPrimitiveInfo.cppm
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module;

#include <cstddef>

export module vk_gltf_viewer:gltf.AssetPrimitiveInfo;

import std;
Expand All @@ -7,8 +11,19 @@ export import vulkan_hpp;
namespace vk_gltf_viewer::gltf {
export struct AssetPrimitiveInfo {
struct IndexBufferInfo { vk::DeviceSize offset; vk::IndexType type; };
struct AttributeBufferInfo { vk::DeviceAddress address; std::uint8_t byteStride; fastgltf::ComponentType componentType; };
struct ColorAttributeBufferInfo final : AttributeBufferInfo { std::uint8_t numComponent; };

struct AttributeBufferInfo {
vk::DeviceAddress address;
std::uint8_t componentType;
std::uint8_t componentCount;
std::uint8_t byteStride;
char _padding_[5];
};
static_assert(sizeof(AttributeBufferInfo) == 16);
static_assert(offsetof(AttributeBufferInfo, componentType) == 8);
static_assert(offsetof(AttributeBufferInfo, componentCount) == 9);
static_assert(offsetof(AttributeBufferInfo, byteStride) == 10);

struct IndexedAttributeBufferInfos { vk::DeviceAddress pMappingBuffer; std::vector<AttributeBufferInfo> attributeInfos; };

std::uint16_t index;
Expand All @@ -17,6 +32,6 @@ namespace vk_gltf_viewer::gltf {
std::optional<AttributeBufferInfo> normalInfo;
std::optional<AttributeBufferInfo> tangentInfo;
IndexedAttributeBufferInfos texcoordsInfo;
std::optional<ColorAttributeBufferInfo> colorInfo;
std::optional<AttributeBufferInfo> colorInfo;
};
}
8 changes: 4 additions & 4 deletions interface/vulkan/pipeline/DepthRenderer.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import :vulkan.specialization_constants.SpecializationMap;
namespace vk_gltf_viewer::vulkan::inline pipeline {
class MaskDepthRendererSpecialization {
public:
std::optional<fastgltf::ComponentType> baseColorTexcoordComponentType;
std::optional<fastgltf::ComponentType> colorAlphaComponentType;
std::optional<std::uint8_t> baseColorTexcoordComponentType;
std::optional<std::uint8_t> colorAlphaComponentType;
shader_type::TextureTransform baseColorTextureTransform = shader_type::TextureTransform::None;

[[nodiscard]] bool operator==(const MaskDepthRendererSpecialization&) const = default;
Expand Down Expand Up @@ -88,10 +88,10 @@ namespace vk_gltf_viewer::vulkan::inline pipeline {
VertexShaderSpecializationData result{};

if (baseColorTexcoordComponentType) {
result.texcoordComponentType = getGLComponentType(*baseColorTexcoordComponentType);
result.texcoordComponentType = *baseColorTexcoordComponentType;
}
if (colorAlphaComponentType) {
result.colorComponentType = getGLComponentType(*colorAlphaComponentType);
result.colorComponentType = *colorAlphaComponentType;
}

return result;
Expand Down
8 changes: 4 additions & 4 deletions interface/vulkan/pipeline/JumpFloodSeedRenderer.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import :vulkan.specialization_constants.SpecializationMap;
namespace vk_gltf_viewer::vulkan::inline pipeline {
class MaskJumpFloodSeedRendererSpecialization {
public:
std::optional<fastgltf::ComponentType> baseColorTexcoordComponentType;
std::optional<fastgltf::ComponentType> colorAlphaComponentType;
std::optional<std::uint8_t> baseColorTexcoordComponentType;
std::optional<std::uint8_t> colorAlphaComponentType;
shader_type::TextureTransform baseColorTextureTransform = shader_type::TextureTransform::None;

[[nodiscard]] bool operator==(const MaskJumpFloodSeedRendererSpecialization&) const = default;
Expand Down Expand Up @@ -88,10 +88,10 @@ namespace vk_gltf_viewer::vulkan::inline pipeline {
VertexShaderSpecializationData result{};

if (baseColorTexcoordComponentType) {
result.texcoordComponentType = getGLComponentType(*baseColorTexcoordComponentType);
result.texcoordComponentType = *baseColorTexcoordComponentType;
}
if (colorAlphaComponentType) {
result.colorComponentType = getGLComponentType(*colorAlphaComponentType);
result.colorComponentType = *colorAlphaComponentType;
}

return result;
Expand Down
28 changes: 7 additions & 21 deletions interface/vulkan/pipeline/PrimitiveRenderer.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import :vulkan.specialization_constants.SpecializationMap;
namespace vk_gltf_viewer::vulkan::inline pipeline {
export class PrimitiveRendererSpecialization {
public:
boost::container::static_vector<fastgltf::ComponentType, 4> texcoordComponentTypes;
std::optional<std::pair<std::uint8_t, fastgltf::ComponentType>> colorComponentCountAndType;
boost::container::static_vector<std::uint8_t, 4> texcoordComponentTypes;
std::optional<std::pair<std::uint8_t, std::uint8_t>> colorComponentCountAndType;
bool fragmentShaderGeneratedTBN;
shader_type::TextureTransform baseColorTextureTransform = shader_type::TextureTransform::None;
shader_type::TextureTransform metallicRoughnessTextureTransform = shader_type::TextureTransform::None;
Expand Down Expand Up @@ -175,34 +175,20 @@ namespace vk_gltf_viewer::vulkan::inline pipeline {
VertexShaderSpecializationData result{};

for (auto [i, componentType] : texcoordComponentTypes | ranges::views::enumerate) {
assert(ranges::one_of(componentType, fastgltf::ComponentType::UnsignedByte, fastgltf::ComponentType::UnsignedShort, fastgltf::ComponentType::Float));

/* Change the i-th byte (from LSB) to the componentType - fastgltf::ComponentType::Byte, which can be
* represented by the 1-byte integer.
*
* fastgltf::ComponentType::Byte - fastgltf::ComponentType::Byte = 0
* fastgltf::ComponentType::UnsignedByte - fastgltf::ComponentType::Byte = 1
* fastgltf::ComponentType::Short - fastgltf::ComponentType::Byte = 2
* fastgltf::ComponentType::UnsignedShort - fastgltf::ComponentType::Byte = 3
* fastgltf::ComponentType::Int - fastgltf::ComponentType::Byte = 4
* fastgltf::ComponentType::UnsignedInt - fastgltf::ComponentType::Byte = 5
* fastgltf::ComponentType::Float - fastgltf::ComponentType::Byte = 6
* fastgltf::ComponentType::Double - fastgltf::ComponentType::Byte = 10
*/
assert(ranges::one_of(componentType, 1 /* UNSIGNED_BYTE */, 3 /* UNSIGNED_SHORT */, 6 /* FLOAT */));

// Step 1: clear the i-th byte (=[8*(i+1):8*i] bits)
result.packedTexcoordComponentTypes &= ~(0xFFU << (8 * i));

// Step 2: set the i-th byte to the componentType - fastgltf::ComponentType::Byte
result.packedTexcoordComponentTypes
|= (getGLComponentType(componentType) - getGLComponentType(fastgltf::ComponentType::Byte)) << (8 * i);
// Step 2: set the i-th byte to the componentType
result.packedTexcoordComponentTypes |= static_cast<std::uint32_t>(componentType) << (8 * i);
}

if (colorComponentCountAndType) {
assert(ranges::one_of(colorComponentCountAndType->first, 3, 4));
assert(ranges::one_of(colorComponentCountAndType->second, fastgltf::ComponentType::UnsignedByte, fastgltf::ComponentType::UnsignedShort, fastgltf::ComponentType::Float));
assert(ranges::one_of(colorComponentCountAndType->second, 1 /* UNSIGNED_BYTE */, 3 /* UNSIGNED_SHORT */, 6 /* FLOAT */));
result.colorComponentCount = colorComponentCountAndType->first;
result.colorComponentType = getGLComponentType(colorComponentCountAndType->second);
result.colorComponentType = colorComponentCountAndType->second;
}

return result;
Expand Down
10 changes: 5 additions & 5 deletions interface/vulkan/pipeline/UnlitPrimitiveRenderer.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import :vulkan.specialization_constants.SpecializationMap;
namespace vk_gltf_viewer::vulkan::inline pipeline {
export class UnlitPrimitiveRendererSpecialization {
public:
std::optional<fastgltf::ComponentType> baseColorTexcoordComponentType;
std::optional<std::pair<std::uint8_t, fastgltf::ComponentType>> colorComponentCountAndType;
std::optional<std::uint8_t> baseColorTexcoordComponentType;
std::optional<std::pair<std::uint8_t, std::uint8_t>> colorComponentCountAndType;
shader_type::TextureTransform baseColorTextureTransform = shader_type::TextureTransform::None;
fastgltf::AlphaMode alphaMode;

Expand Down Expand Up @@ -166,14 +166,14 @@ namespace vk_gltf_viewer::vulkan::inline pipeline {
VertexShaderSpecializationData result{};

if (baseColorTexcoordComponentType) {
result.texcoordComponentType = getGLComponentType(*baseColorTexcoordComponentType);
result.texcoordComponentType = *baseColorTexcoordComponentType;
}

if (colorComponentCountAndType) {
assert(ranges::one_of(colorComponentCountAndType->first, 3, 4));
assert(ranges::one_of(colorComponentCountAndType->second, fastgltf::ComponentType::UnsignedByte, fastgltf::ComponentType::UnsignedShort, fastgltf::ComponentType::Float));
assert(ranges::one_of(colorComponentCountAndType->second, 1 /* UNSIGNED_BYTE */, 3 /* UNSIGNED_SHORT */, 6 /* FLOAT */));
result.colorComponentCount = colorComponentCountAndType->first;
result.colorComponentType = getGLComponentType(colorComponentCountAndType->second);
result.colorComponentType = colorComponentCountAndType->second;
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions shaders/mask_depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#define HAS_VARIADIC_OUT HAS_BASE_COLOR_TEXTURE || HAS_COLOR_ALPHA_ATTRIBUTE

layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 6; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 6; // FLOAT

layout (location = 0) flat out uint outNodeIndex;
layout (location = 1) flat out uint outMaterialIndex;
Expand Down
4 changes: 2 additions & 2 deletions shaders/mask_jump_flood_seed.vert
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#define HAS_VARIADIC_OUT HAS_BASE_COLOR_TEXTURE || HAS_COLOR_ALPHA_ATTRIBUTE

layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 6; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_TYPE = 6; // FLOAT

layout (location = 0) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
Expand Down
2 changes: 1 addition & 1 deletion shaders/primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

layout (constant_id = 0) const uint PACKED_TEXCOORD_COMPONENT_TYPES = 0x06060606; // [FLOAT, FLOAT, FLOAT, FLOAT]
layout (constant_id = 1) const uint COLOR_COMPONENT_COUNT = 0;
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 6; // FLOAT

layout (location = 0) out vec3 outPosition;
layout (location = 1) flat out uint outMaterialIndex;
Expand Down
3 changes: 3 additions & 0 deletions shaders/types.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ struct Material {

struct IndexedAttributeMappingInfo {
uint64_t bytesPtr;
uint8_t componentType;
uint8_t componentCount;
uint8_t stride;
uint8_t _padding_[5];
};

layout (std430, buffer_reference, buffer_reference_align = 16) readonly buffer IndexedAttributeMappingInfos { IndexedAttributeMappingInfo data[]; };
Expand Down
4 changes: 2 additions & 2 deletions shaders/unlit_primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#define HAS_VARIADIC_OUT HAS_BASE_COLOR_TEXTURE || HAS_COLOR_ATTRIBUTE

layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 0) const uint TEXCOORD_COMPONENT_TYPE = 6; // FLOAT
layout (constant_id = 1) const uint COLOR_COMPONENT_COUNT = 0;
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 5126; // FLOAT
layout (constant_id = 2) const uint COLOR_COMPONENT_TYPE = 6; // FLOAT

layout (location = 0) flat out uint outMaterialIndex;
#if HAS_VARIADIC_OUT
Expand Down
Loading

0 comments on commit 872851c

Please sign in to comment.