Skip to content

Commit

Permalink
The material system is now more functional!
Browse files Browse the repository at this point in the history
  • Loading branch information
cullvox committed Jun 12, 2024
1 parent a72abd1 commit 0d0594d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
11 changes: 10 additions & 1 deletion Source/Engine/Graphics/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ void MaterialBase::UpdateUniforms() {

void* mapped = nullptr;
buffer.Map(&mapped);
std::memcpy(static_cast<intptr_t*>(mapped) + (blockSize * _currentFrame), static_cast<intptr_t*>(mapped) + (blockSize * previousFrame), blockSize);

void* dstData = static_cast<intptr_t*>(mapped) + (blockSize * _currentFrame);
void* newData = static_cast<intptr_t*>(mapped) + (blockSize * previousFrame);

std::memcpy(dstData, newData, blockSize);

// std::memcpy((intptr_t*)mapped + (blockSize * _currentFrame), (intptr_t*)mapped + (blockSize * previousFrame), blockSize);


buffer.Unmap();
buffer.Flush(blockSize * _currentFrame, blockSize);
} break;
Expand Down Expand Up @@ -224,6 +232,7 @@ void MaterialBase::BuildMaterialData(VkDescriptorSetLayout layout) {
void MaterialBase::SetBindingDirty(uint32_t binding) {
assert(_data.contains(binding) && "Binding must exist to set it dirty!");

_perFrameData[_currentFrame].dirty[binding] = false; /* This current frame is now the current data and is no longer dirty. */
for (uint32_t i = (_currentFrame + 1) % GraphicsConfig::numFramesInFlight; i != _currentFrame; i = (i + 1) % GraphicsConfig::numFramesInFlight) {
_perFrameData[i].dirty[binding] = true;
}
Expand Down
12 changes: 8 additions & 4 deletions Source/Engine/Graphics/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ void MaterialBase::SetGenericUniform(const std::string& name, T value)
VkDeviceSize blockSize = buffer.GetSize() / GraphicsConfig::numFramesInFlight;
// uint32_t previousFrame = (_currentFrame - 1) % GraphicsConfig::numFramesInFlight;

void* mapped = nullptr;
buffer.Map(&mapped);
std::memcpy(static_cast<intptr_t*>(mapped) + (blockSize * _currentFrame), &value, blockSize);
auto offset = (blockSize * _currentFrame) + meta.GetOffset();

char* uniform = nullptr;
buffer.Map((void**)&uniform);
uniform += offset;
std::memcpy(uniform, &value, sizeof(T));
buffer.Unmap();
buffer.Flush(blockSize * _currentFrame, blockSize);

buffer.Flush(offset, sizeof(T));

SetBindingDirty(meta.GetBinding()); /* The buffer data must be copied to the other parts of the dynamic uniform buffer. */
}
Expand Down
7 changes: 3 additions & 4 deletions Source/Engine/Graphics/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ PipelineReflection::~PipelineReflection() {}
PipelineReflection& PipelineReflection::operator=(const PipelineReflection& rhs) = default;
PipelineReflection& PipelineReflection::operator=(PipelineReflection&& rhs) = default;

std::unordered_map<uint32_t, DescriptorSetReflection>& PipelineReflection::GetDescriptorSetReflections() {
std::map<uint32_t, DescriptorSetReflection>& PipelineReflection::GetDescriptorSetReflections() {
return _descriptorSetMetadata;
}

std::vector<PushConstantReflection>& PipelineReflection::GetPushConstantReflections() {
return _pushConstantMetadata;
}

const std::unordered_map<uint32_t, DescriptorSetReflection>& PipelineReflection::GetDescriptorSetReflections() const {
const std::map<uint32_t, DescriptorSetReflection>& PipelineReflection::GetDescriptorSetReflections() const {
return _descriptorSetMetadata;
}

Expand Down Expand Up @@ -187,8 +187,7 @@ Pipeline::Pipeline(Device* device, RenderPass* pass, uint32_t subpass, const Pip
_descriptorSetLayouts.reserve(descriptorSetMetadata.size());

// Extract descriptor set layout bindings and create a layout.
for (auto& pair : descriptorSetMetadata)
{
for (auto& pair : descriptorSetMetadata) {
auto& meta = pair.second;

// Acquire a layout from cache or create a new descriptor set layout.
Expand Down
6 changes: 3 additions & 3 deletions Source/Engine/Graphics/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class PipelineReflection {
PipelineReflection& operator=(const PipelineReflection& rhs);
PipelineReflection& operator=(PipelineReflection&& rhs);

std::unordered_map<uint32_t, DescriptorSetReflection>& GetDescriptorSetReflections();
std::map<uint32_t, DescriptorSetReflection>& GetDescriptorSetReflections();
std::vector<PushConstantReflection>& GetPushConstantReflections();
const std::unordered_map<uint32_t, DescriptorSetReflection>& GetDescriptorSetReflections() const;
const std::map<uint32_t, DescriptorSetReflection>& GetDescriptorSetReflections() const;
const std::vector<PushConstantReflection>& GetPushConstantReflections() const;

private:
void ReflectMembers(BlockMeta& reflection, uint32_t binding, const SpvReflectBlockVariable& block, std::string parent = "");

std::unordered_map<uint32_t, DescriptorSetReflection> _descriptorSetMetadata;
std::map<uint32_t, DescriptorSetReflection> _descriptorSetMetadata;
std::vector<PushConstantReflection> _pushConstantMetadata;
};

Expand Down
2 changes: 1 addition & 1 deletion Source/Game/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ int main(int argc, const char** argv)
glm::vec3 position{ sinf(bl::Time::current() / 1000.f) * 10.f, 0.0f, 10.0f };
glm::vec3 velocity{ cosf(bl::Time::current() / 1000.f) * 1 / 100.f, 0.0f, 0.0f };

glm::vec4 color = { randomValue(), 0.5f, randomValue(), 1.0f };
glm::vec4 color = { 1.f, 0.5f, 0.f, 1.0f };

glm::vec4 val{};

Expand Down
2 changes: 1 addition & 1 deletion imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Pos=14,191
Size=534,640

[Window][Dear ImGui Demo]
Pos=1174,550
Pos=2024,466
Size=359,330

[Window][Settings]
Expand Down

0 comments on commit 0d0594d

Please sign in to comment.