Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/libs/common/hashing/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ namespace RR::Common
{
return ConstexprHash<DefaultHasher>(str, len);
}

// Pass-through hasher for values that are already a hash.
// Avoids double-hashing when using HashType as a map key.
struct PrehashedHasher
{
size_t operator()(HashType val) const noexcept { return val; }
};
}
3 changes: 2 additions & 1 deletion src/libs/effect_library/EffectFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace RR::EffectLibrary
struct Header
{
static constexpr uint32_t MAGIC = 0x4C584652;
static constexpr uint32_t VERSION = 1;
static constexpr uint32_t VERSION = 2;
uint32_t magic;
uint32_t version;
uint32_t stringsSectionSize;
Expand Down Expand Up @@ -202,6 +202,7 @@ namespace RR::EffectLibrary
GAPI::RasterizerDesc rasterizerDesc;
GAPI::DepthStencilDesc depthStencilDesc;
GAPI::ShaderStageMask shaderStages;
uint32_t rootBindingGroupIndex;
// List of shader indexes based on shaderStages mask
};

Expand Down
1 change: 1 addition & 0 deletions src/libs/effect_library/EffectLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ namespace RR::EffectLibrary
passDesc.rasterizerDesc = assetPassDesc.rasterizerDesc;
passDesc.depthStencilDesc = assetPassDesc.depthStencilDesc;
passDesc.blendDesc = assetPassDesc.blendDesc;
passDesc.rootBindingGroupIndex = assetPassDesc.rootBindingGroupIndex;

eastl::fixed_vector<GAPI::ShaderStage, eastl::to_underlying(GAPI::ShaderStage::Count), false> shaderStages;

Expand Down
37 changes: 37 additions & 0 deletions src/libs/gapi/BindingGroupLayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "BindingGroupLayout.hpp"

namespace RR::GAPI
{
void BindingGroupLayout::InitReflection(const BindingGroupReflectionDesc& desc)
{
bindingSpace = desc.bindingSpace;
uniformBufferSize = desc.uniformBufferSize;
uniformCbvSlot = desc.uniformCbvSlot;

for (const auto& f : desc.fields)
{
const uint32_t index = static_cast<uint32_t>(fields.size());
fields.push_back(f);
fieldMap[f.nameHash] = index;
}

for (const auto& r : desc.resources)
{
const uint32_t index = static_cast<uint32_t>(resourceSlots.size());
resourceSlots.push_back(r);
resourceMap[r.nameHash] = index;
}
}

uint32_t BindingGroupLayout::FindFieldIndex(Common::HashType nameHash) const
{
const auto it = fieldMap.find(nameHash);
return it != fieldMap.end() ? it->second : INVALID_SLOT;
}

uint32_t BindingGroupLayout::FindResourceSlotIndex(Common::HashType nameHash) const
{
const auto it = resourceMap.find(nameHash);
return it != resourceMap.end() ? it->second : INVALID_SLOT;
}
}
79 changes: 77 additions & 2 deletions src/libs/gapi/BindingGroupLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
#include "gapi/Shader.hpp"
#include "gapi/Limits.hpp"

#include "common/hashing/Hash.hpp"

#include <EASTL/fixed_vector.h>
#include <EASTL/span.h>
#include <EASTL/vector_map.h>

namespace RR::GAPI
{
Expand Down Expand Up @@ -37,6 +41,32 @@ namespace RR::GAPI
eastl::span<const BindingLayoutElement> elements;
};

// Reflection: a single uniform field within a CBV
struct FieldDesc
{
Common::HashType nameHash;
uint16_t offset; // byte offset in uniform buffer
uint16_t size; // byte size of field
};

// Reflection: a single resource slot (SRV, UAV, sampler)
struct ResourceSlotDesc
{
Common::HashType nameHash;
uint16_t binding; // binding index in bind group
uint16_t slotIndex; // dense index in EffectContext::resources[]
BindingType type;
};

struct BindingGroupReflectionDesc
{
uint32_t bindingSpace = 0;
eastl::span<const FieldDesc> fields;
eastl::span<const ResourceSlotDesc> resources;
uint32_t uniformBufferSize = 0;
uint32_t uniformCbvSlot = ~0u;
};

class IBindingGroupLayout
{
public:
Expand All @@ -46,12 +76,57 @@ namespace RR::GAPI
class BindingGroupLayout final : public Resource<IBindingGroupLayout, true>
{
public:
static constexpr uint32_t INVALID_SLOT = ~0u;

BindingGroupLayout(const std::string& name)
: Resource(Type::BindingGroupLayout, name)
{
}
};

// Populate reflection metadata (call after GPU init).
void InitReflection(const BindingGroupReflectionDesc& desc);

}
uint32_t GetBindingSpace() const { return bindingSpace; }
uint32_t GetUniformBufferSize() const { return uniformBufferSize; }
uint32_t GetUniformCbvSlot() const { return uniformCbvSlot; }

uint32_t GetFieldCount() const { return static_cast<uint32_t>(fields.size()); }
const FieldDesc& GetField(uint32_t index) const
{
ASSERT(index < fields.size());
return fields[index];
}

uint32_t GetResourceSlotCount() const { return static_cast<uint32_t>(resourceSlots.size()); }
const ResourceSlotDesc& GetResourceSlot(uint32_t index) const
{
ASSERT(index < resourceSlots.size());
return resourceSlots[index];
}

// O(log n) lookup by precomputed name hash. Cache the index for hot paths.
uint32_t FindFieldIndex(Common::HashType nameHash) const;
uint32_t FindResourceSlotIndex(Common::HashType nameHash) const;

private:
using HashToIndex = eastl::pair<Common::HashType, uint32_t>;

template <size_t N, bool Overflow = true>
using Lookup = eastl::vector_map<
Common::HashType, uint32_t,
eastl::less<Common::HashType>,
EASTLAllocatorType,
eastl::fixed_vector<HashToIndex, N, Overflow>>;

uint32_t bindingSpace = 0;
uint32_t uniformBufferSize = 0;
uint32_t uniformCbvSlot = INVALID_SLOT;

eastl::fixed_vector<FieldDesc, 16, true> fields;
eastl::fixed_vector<ResourceSlotDesc, MAX_BINDINGS_PER_GROUP, false> resourceSlots;

Lookup<16> fieldMap; // nameHash -> field index
Lookup<MAX_BINDINGS_PER_GROUP, false> resourceMap; // nameHash -> slot index
};

}
1 change: 1 addition & 0 deletions src/libs/gapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project (gapi)
set(SRC
Buffer.cpp
Buffer.hpp
BindingGroupLayout.cpp
BindingGroupLayout.hpp
BindingGroup.hpp
CommandList.hpp
Expand Down
83 changes: 0 additions & 83 deletions src/libs/render/BindingBlockLayout.cpp

This file was deleted.

85 changes: 0 additions & 85 deletions src/libs/render/BindingBlockLayout.hpp

This file was deleted.

3 changes: 1 addition & 2 deletions src/libs/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ set(SRC
RenderTarget.cpp
Effect.hpp
Effect.cpp
EffectContext.hpp
EffectManager.cpp
EffectManager.hpp
BindingBlockLayout.cpp
BindingBlockLayout.hpp
Submission.cpp
Submission.hpp
)
Expand Down
3 changes: 3 additions & 0 deletions src/libs/render/CommandEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "math/ForwardDeclarations.hpp"
#include "render/Effect.hpp"
#include "render/EffectContext.hpp"
#include "common/NonCopyableMovable.hpp"

namespace RR::Render
Expand All @@ -18,6 +19,8 @@ namespace RR::Render
public:
RenderPassEncoder BeginRenderPass(const GAPI::RenderPassDesc& renderPass);

EffectContext CreateEffectContext(Effect* effect) { return EffectContext(effect); }

void Finish()
{
ASSERT_MSG(state == State::Open, "CommandEncoder was not started");
Expand Down
Loading