Skip to content

Commit

Permalink
Rename Material to Pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed Nov 9, 2024
1 parent 4933efb commit 4d69c67
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Dynamo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <Clock.hpp>
#include <Display.hpp>
#include <ECS/World.hpp>
#include <Graphics/Material.hpp>
#include <Graphics/Mesh.hpp>
#include <Graphics/Model.hpp>
#include <Graphics/Pipeline.hpp>
#include <Graphics/Renderer.hpp>
#include <Graphics/Shader.hpp>
#include <Graphics/Texture.hpp>
Expand Down
6 changes: 3 additions & 3 deletions src/Graphics/Model.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Graphics/Material.hpp>
#include <Graphics/Mesh.hpp>
#include <Graphics/Pipeline.hpp>

namespace Dynamo::Graphics {
/**
Expand All @@ -16,10 +16,10 @@ namespace Dynamo::Graphics {
Mesh mesh;

/**
* @brief Model material properties.
* @brief Model pipeline.
*
*/
Material material;
Pipeline pipeline;

/**
* @brief Group index used in sorting.
Expand Down
8 changes: 4 additions & 4 deletions src/Graphics/Material.hpp → src/Graphics/Pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace Dynamo::Graphics {
/**
* @brief Material handle.
* @brief Pipeline handle.
*
*/
DYN_DEFINE_ID_TYPE(Material);
DYN_DEFINE_ID_TYPE(Pipeline);

/**
* @brief Uniform variable handle.
Expand Down Expand Up @@ -62,10 +62,10 @@ namespace Dynamo::Graphics {
};

/**
* @brief Material descriptor.
* @brief Pipeline descriptor.
*
*/
struct MaterialDescriptor {
struct PipelineDescriptor {
/**
* @brief Vertex shader handle.
*
Expand Down
46 changes: 23 additions & 23 deletions src/Graphics/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Dynamo::Graphics {
_meshes = MeshRegistry(_device, _physical, _transfer_pool);
_uniforms = UniformRegistry(_device, _physical, _transfer_pool);
_textures = TextureRegistry(_device, _physical, _transfer_pool);
_materials = MaterialRegistry(_device, _physical, root_asset_directory + "/vulkan_cache.bin");
_pipelines = PipelineRegistry(_device, _physical, root_asset_directory + "/vulkan_cache.bin");

// Setup the color buffer
TextureDescriptor color_descriptor;
Expand Down Expand Up @@ -68,11 +68,11 @@ namespace Dynamo::Graphics {
vkDeviceWaitIdle(_device);

// Cache built pipelines
_materials.write_to_disk();
_pipelines.write_to_disk();

// High-level objects
_frame_contexts.destroy();
_materials.destroy();
_pipelines.destroy();
_textures.destroy(_memory);
_uniforms.destroy(_memory);
_meshes.destroy(_memory);
Expand Down Expand Up @@ -173,20 +173,20 @@ namespace Dynamo::Graphics {

void Renderer::destroy_texture(Texture texture) { _textures.destroy(texture, _memory); }

Material Renderer::build_material(const MaterialDescriptor &descriptor) {
return _materials.build(descriptor, _forwardpass, _swapchain, _shaders, _uniforms, _memory);
Pipeline Renderer::build_pipeline(const PipelineDescriptor &descriptor) {
return _pipelines.build(descriptor, _forwardpass, _swapchain, _shaders, _uniforms, _memory);
}

void Renderer::destroy_material(Material material) {
void Renderer::destroy_pipeline(Pipeline pipeline) {
// Free allocated descriptor / push constant uniforms
MaterialInstance &instance = _materials.get(material);
PipelineInstance &instance = _pipelines.get(pipeline);
for (Uniform uniform : instance.uniforms) {
_uniforms.destroy(uniform, _memory);
}
}

std::optional<Uniform> Renderer::get_uniform(Material material, const std::string &name) {
MaterialInstance &instance = _materials.get(material);
std::optional<Uniform> Renderer::get_uniform(Pipeline pipeline, const std::string &name) {
PipelineInstance &instance = _pipelines.get(pipeline);
for (Uniform uniform : instance.uniforms) {
const UniformVariable &var = _uniforms.get(uniform);
if (var.name == name) {
Expand Down Expand Up @@ -235,10 +235,10 @@ namespace Dynamo::Graphics {

VkResult_check("Begin Command Recording", vkBeginCommandBuffer(frame.command_buffer, &begin_info));

// Sort models by group, then material, then geometry
// Sort models by group, then pipeline, then geometry
std::sort(_models.begin(), _models.end(), [](const Model &a, const Model &b) {
return a.group < b.group ||
(a.group == b.group && (a.material < b.material || (a.material == b.material && a.mesh < b.mesh)));
(a.group == b.group && (a.pipeline < b.pipeline || (a.pipeline == b.pipeline && a.mesh < b.mesh)));
});

// Submit commands
Expand Down Expand Up @@ -273,12 +273,12 @@ namespace Dynamo::Graphics {

for (Model model : _models) {
const MeshInstance &mesh = _meshes.get(model.mesh);
const MaterialInstance &material = _materials.get(model.material);
const PipelineInstance &pipeline = _pipelines.get(model.pipeline);

// Rebind pipeline if changed
if (prev_pipeline != material.pipeline) {
prev_pipeline = material.pipeline;
vkCmdBindPipeline(frame.command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, material.pipeline);
if (prev_pipeline != pipeline.handle) {
prev_pipeline = pipeline.handle;
vkCmdBindPipeline(frame.command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle);
}

// Rebind mesh if changed
Expand All @@ -296,24 +296,24 @@ namespace Dynamo::Graphics {

// Bind descriptor sets
// TODO: Avoid costly descriptor set rebinding
if (material.descriptor_sets.size()) {
if (pipeline.descriptor_sets.size()) {
vkCmdBindDescriptorSets(frame.command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
material.layout,
pipeline.layout,
0,
material.descriptor_sets.size(),
material.descriptor_sets.data(),
pipeline.descriptor_sets.size(),
pipeline.descriptor_sets.data(),
0,
nullptr);
}

// Push constants
for (unsigned i = 0; i < material.push_constant_ranges.size(); i++) {
VkPushConstantRange range = material.push_constant_ranges[i];
unsigned offset = material.push_constant_offsets[i];
for (unsigned i = 0; i < pipeline.push_constant_ranges.size(); i++) {
VkPushConstantRange range = pipeline.push_constant_ranges[i];
unsigned offset = pipeline.push_constant_offsets[i];
void *data = _uniforms.get_push_constant_data(offset);
vkCmdPushConstants(frame.command_buffer,
material.layout,
pipeline.layout,
range.stageFlags,
range.offset,
range.size,
Expand Down
22 changes: 11 additions & 11 deletions src/Graphics/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <Graphics/Model.hpp>
#include <Graphics/Texture.hpp>
#include <Graphics/Vulkan/FrameContext.hpp>
#include <Graphics/Vulkan/MaterialRegistry.hpp>
#include <Graphics/Vulkan/MeshRegistry.hpp>
#include <Graphics/Vulkan/PhysicalDevice.hpp>
#include <Graphics/Vulkan/PipelineRegistry.hpp>
#include <Graphics/Vulkan/ShaderRegistry.hpp>
#include <Graphics/Vulkan/Swapchain.hpp>
#include <Graphics/Vulkan/TextureRegistry.hpp>
Expand Down Expand Up @@ -41,7 +41,7 @@ namespace Dynamo::Graphics {
MemoryPool _memory;
MeshRegistry _meshes;
ShaderRegistry _shaders;
MaterialRegistry _materials;
PipelineRegistry _pipelines;
UniformRegistry _uniforms;
TextureRegistry _textures;

Expand Down Expand Up @@ -140,28 +140,28 @@ namespace Dynamo::Graphics {
void destroy_texture(Texture texture);

/**
* @brief Build a material.
* @brief Build a graphics pipeline.
*
* @param descriptor
* @return Material
* @return Pipeline
*/
Material build_material(const MaterialDescriptor &descriptor);
Pipeline build_pipeline(const PipelineDescriptor &descriptor);

/**
* @brief Free material instance resources.
* @brief Free pipeline instance resources.
*
* @param material
* @param pipeline
*/
void destroy_material(Material material);
void destroy_pipeline(Pipeline pipeline);

/**
* @brief Get a reference to a uniform from a material.
* @brief Get a reference to a uniform from a pipeline.
*
* @param material
* @param pipeline
* @param name
* @return std::optional<Uniform>
*/
std::optional<Uniform> get_uniform(Material material, const std::string &name);
std::optional<Uniform> get_uniform(Pipeline pipeline, const std::string &name);

/**
* @brief Write to a uniform.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <Graphics/Vulkan/MaterialRegistry.hpp>
#include <Graphics/Vulkan/PipelineRegistry.hpp>
#include <Graphics/Vulkan/Utils.hpp>

namespace Dynamo::Graphics::Vulkan {
MaterialRegistry::MaterialRegistry(VkDevice device, const PhysicalDevice &physical, const std::string &filename) :
PipelineRegistry::PipelineRegistry(VkDevice device, const PhysicalDevice &physical, const std::string &filename) :
_device(device), _physical(&physical) {
std::ifstream ifstream;
ifstream.open(filename, std::ios::app | std::ios::binary);
Expand All @@ -23,13 +23,13 @@ namespace Dynamo::Graphics::Vulkan {
_ofstream.open(filename, std::ios::trunc | std::ios::binary);
}

Material MaterialRegistry::build(const MaterialDescriptor &descriptor,
Pipeline PipelineRegistry::build(const PipelineDescriptor &descriptor,
VkRenderPass renderpass,
const Swapchain &swapchain,
const ShaderRegistry &shaders,
UniformRegistry &uniforms,
MemoryPool &memory) {
MaterialInstance instance;
PipelineInstance instance;

const ShaderModule &vertex_module = shaders.get(descriptor.vertex);
const ShaderModule &fragment_module = shaders.get(descriptor.fragment);
Expand Down Expand Up @@ -100,35 +100,35 @@ namespace Dynamo::Graphics::Vulkan {
pipeline_settings.depth_test_op = convert_compare_op(descriptor.depth_test_op);
auto pipeline_it = _pipelines.find(pipeline_settings);
if (pipeline_it != _pipelines.end()) {
instance.pipeline = pipeline_it->second;
instance.handle = pipeline_it->second;
} else {
instance.pipeline = VkPipeline_create(_device,
_pipeline_cache,
pipeline_settings.layout,
renderpass,
pipeline_settings.vertex,
pipeline_settings.fragment,
pipeline_settings.topology,
pipeline_settings.fill,
pipeline_settings.cull,
pipeline_settings.samples,
pipeline_settings.color_mask,
pipeline_settings.depth_test,
pipeline_settings.depth_write,
pipeline_settings.depth_test_op,
vertex_module.bindings.data(),
vertex_module.bindings.size(),
vertex_module.attributes.data(),
vertex_module.attributes.size());
_pipelines.emplace(pipeline_settings, instance.pipeline);
instance.handle = VkPipeline_create(_device,
_pipeline_cache,
pipeline_settings.layout,
renderpass,
pipeline_settings.vertex,
pipeline_settings.fragment,
pipeline_settings.topology,
pipeline_settings.fill,
pipeline_settings.cull,
pipeline_settings.samples,
pipeline_settings.color_mask,
pipeline_settings.depth_test,
pipeline_settings.depth_write,
pipeline_settings.depth_test_op,
vertex_module.bindings.data(),
vertex_module.bindings.size(),
vertex_module.attributes.data(),
vertex_module.attributes.size());
_pipelines.emplace(pipeline_settings, instance.handle);
}

return _instances.insert(instance);
}

MaterialInstance &MaterialRegistry::get(Material material) { return _instances.get(material); }
PipelineInstance &PipelineRegistry::get(Pipeline pipeline) { return _instances.get(pipeline); }

void MaterialRegistry::destroy() {
void PipelineRegistry::destroy() {
// Clean up pipelines
vkDestroyPipelineCache(_device, _pipeline_cache, nullptr);
for (const auto &[key, pipeline] : _pipelines) {
Expand All @@ -146,7 +146,7 @@ namespace Dynamo::Graphics::Vulkan {
_instances.clear();
}

void MaterialRegistry::write_to_disk() {
void PipelineRegistry::write_to_disk() {
size_t size;
vkGetPipelineCacheData(_device, _pipeline_cache, &size, nullptr);
std::vector<char> buffer(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <vulkan/vulkan_core.h>

#include <Graphics/Material.hpp>
#include <Graphics/Pipeline.hpp>
#include <Graphics/Vulkan/ShaderRegistry.hpp>
#include <Graphics/Vulkan/Swapchain.hpp>
#include <Graphics/Vulkan/UniformRegistry.hpp>
Expand Down Expand Up @@ -99,16 +99,16 @@ namespace Dynamo::Graphics::Vulkan {
};
};

struct MaterialInstance {
struct PipelineInstance {
VkPipelineLayout layout;
VkPipeline pipeline;
VkPipeline handle;
std::vector<Uniform> uniforms;
std::vector<VkDescriptorSet> descriptor_sets;
std::vector<VkPushConstantRange> push_constant_ranges;
std::vector<unsigned> push_constant_offsets;
};

class MaterialRegistry {
class PipelineRegistry {
VkDevice _device;
const PhysicalDevice *_physical;

Expand All @@ -118,20 +118,20 @@ namespace Dynamo::Graphics::Vulkan {
std::unordered_map<PipelineLayoutSettings, VkPipelineLayout, PipelineLayoutSettings::Hash> _layouts;
std::unordered_map<GraphicsPipelineSettings, VkPipeline, GraphicsPipelineSettings::Hash> _pipelines;

SparseArray<Material, MaterialInstance> _instances;
SparseArray<Pipeline, PipelineInstance> _instances;

public:
MaterialRegistry(VkDevice device, const PhysicalDevice &physical, const std::string &filename);
MaterialRegistry() = default;
PipelineRegistry(VkDevice device, const PhysicalDevice &physical, const std::string &filename);
PipelineRegistry() = default;

Material build(const MaterialDescriptor &descriptor,
Pipeline build(const PipelineDescriptor &descriptor,
VkRenderPass renderpass,
const Swapchain &swapchain,
const ShaderRegistry &shaders,
UniformRegistry &uniforms,
MemoryPool &memory);

MaterialInstance &get(Material material);
PipelineInstance &get(Pipeline pipeline);

void destroy();

Expand Down
2 changes: 1 addition & 1 deletion src/Graphics/Vulkan/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <vulkan/vulkan_core.h>

#include <Display.hpp>
#include <Graphics/Material.hpp>
#include <Graphics/Pipeline.hpp>
#include <Graphics/Texture.hpp>
#include <Graphics/Vulkan/PhysicalDevice.hpp>
#include <Utils/Log.hpp>
Expand Down

0 comments on commit 4d69c67

Please sign in to comment.