Skip to content

Commit

Permalink
refine shader program creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingstom committed Nov 21, 2023
1 parent 8723aae commit 2c48db3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
1 change: 1 addition & 0 deletions engine/api/gpuResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ enum class BlendFactor : uint8_t

enum class PipelineType : uint8_t
{
Undefined,
Geometry,
Mesh,
Compute,
Expand Down
6 changes: 5 additions & 1 deletion engine/api/vulkan/commandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ void CommandBuffer::flushGraphicsCommand()
if(auto& pipeline = m_commandState.pPipeline; pipeline == nullptr)
{
APH_ASSERT(m_commandState.pProgram);
if (m_commandState.pProgram->getShader(ShaderStage::MS))
{

}
aph::vk::GraphicsPipelineCreateInfo createInfo{
.pProgram = m_commandState.pProgram,
};
Expand All @@ -392,7 +396,7 @@ void CommandBuffer::flushGraphicsCommand()
createInfo.vertexInput = m_commandState.pProgram->getVertexInput();
}

for(auto colorAttachment : m_commandState.colorAttachments)
for(const auto& colorAttachment : m_commandState.colorAttachments)
{
createInfo.color.push_back({
.format = colorAttachment.image->getFormat(),
Expand Down
43 changes: 33 additions & 10 deletions engine/api/vulkan/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,45 @@ VkFormat Device::getDepthFormat() const
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
}

Result Device::create(const ProgramCreateInfo& createInfo, ShaderProgram** ppPipeline, std::string_view debugName)
Result Device::create(const ProgramCreateInfo& createInfo, ShaderProgram** ppProgram, std::string_view debugName)
{
APH_PROFILER_SCOPE();
// TODO
if(createInfo.pVertex && createInfo.pFragment)
switch(createInfo.type)
{
case PipelineType::Geometry:
{
auto vs = createInfo.geometry.pVertex;
auto fs = createInfo.geometry.pFragment;
APH_ASSERT(vs);
APH_ASSERT(fs);

*ppProgram = m_resourcePool.program.allocate(this, vs, fs, createInfo.samplerBank);
}
break;
case PipelineType::Mesh:
{
*ppPipeline =
m_resourcePool.program.allocate(this, createInfo.pVertex, createInfo.pFragment, createInfo.samplerBank);
APH_ASSERT(false);
return Result::RuntimeError;
}
else if(createInfo.pCompute)
break;
case PipelineType::Compute:
{
*ppPipeline = m_resourcePool.program.allocate(this, createInfo.pCompute, createInfo.samplerBank);
auto cs = createInfo.compute.pCompute;
APH_ASSERT(cs);
*ppProgram = m_resourcePool.program.allocate(this, cs, createInfo.samplerBank);
}
else
break;
case PipelineType::RayTracing:
{
APH_ASSERT(false);
return Result::RuntimeError;
}
break;
default:
{
APH_ASSERT(false);
return Result::RuntimeError;
}
}
return Result::Success;
}
Expand Down Expand Up @@ -587,8 +610,8 @@ void Device::executeSingleCommands(Queue* queue, const CmdRecordCallBack&& func,
Fence* pFence)
{
APH_PROFILER_SCOPE();
CommandPool* commandPool = acquireCommandPool({queue, true});
CommandBuffer* cmd = nullptr;
CommandPool* commandPool = acquireCommandPool({queue, true});
CommandBuffer* cmd = nullptr;
APH_CHECK_RESULT(commandPool->allocate(1, &cmd));

_VR(cmd->begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT));
Expand Down
3 changes: 1 addition & 2 deletions engine/api/vulkan/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "shader.h"
#include "swapChain.h"
#include "syncPrimitive.h"
#include "vkUtils.h"

namespace aph::vk
{
Expand Down Expand Up @@ -57,7 +56,7 @@ class Device : public ResourceHandle<VkDevice, DeviceCreateInfo>
Result create(const ImageCreateInfo& createInfo, Image** ppImage, std::string_view debugName = "");
Result create(const ImageViewCreateInfo& createInfo, ImageView** ppImageView, std::string_view debugName = "");
Result create(const SwapChainCreateInfo& createInfo, SwapChain** ppSwapchain, std::string_view debugName = "");
Result create(const ProgramCreateInfo& createInfo, ShaderProgram** ppPipeline, std::string_view debugName = "");
Result create(const ProgramCreateInfo& createInfo, ShaderProgram** ppProgram, std::string_view debugName = "");

void destroy(Buffer* pBuffer);
void destroy(Image* pImage);
Expand Down
1 change: 1 addition & 0 deletions engine/api/vulkan/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ Pipeline* PipelineAllocator::create(const GraphicsPipelineCreateInfo& createInfo
break;
case PipelineType::Compute:
case PipelineType::RayTracing:
default:
APH_ASSERT(false);
return nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions engine/api/vulkan/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Shader::Shader(std::vector<uint32_t> code, HandleType handle, std::string entryp
}

ShaderProgram::ShaderProgram(Device* device, Shader* vs, Shader* fs, const ImmutableSamplerBank* samplerBank) :
m_pDevice(device)
m_pDevice(device), m_pipelineType(PipelineType::Geometry)
{
if(vs)
{
Expand All @@ -312,7 +312,7 @@ ShaderProgram::ShaderProgram(Device* device, Shader* vs, Shader* fs, const Immut
createVertexInput();
}

ShaderProgram::ShaderProgram(Device* device, Shader* cs, const ImmutableSamplerBank* samplerBank) : m_pDevice(device)
ShaderProgram::ShaderProgram(Device* device, Shader* cs, const ImmutableSamplerBank* samplerBank) : m_pDevice(device), m_pipelineType(PipelineType::Compute)
{
m_shaders[ShaderStage::CS] = cs;
combineLayout(samplerBank);
Expand Down
25 changes: 22 additions & 3 deletions engine/api/vulkan/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,27 @@ using ShaderMapList = HashMap<ShaderStage, Shader*>;

struct ProgramCreateInfo
{
Shader* pVertex = {};
Shader* pFragment = {};
Shader* pCompute = {};
union
{
struct
{
Shader* pMesh = {};
Shader* pTask = {};
} mesh;

struct
{
Shader* pCompute = {};
} compute;

struct
{
Shader* pVertex = {};
Shader* pFragment = {};
} geometry;
};

PipelineType type = {};
ImmutableSamplerBank* samplerBank = {};
};

Expand Down Expand Up @@ -137,6 +155,7 @@ class ShaderProgram
CombinedResourceLayout m_combineLayout = {};
SmallVector<VkDescriptorPoolSize> m_poolSize = {};
VertexInput m_vertexInput = {};
PipelineType m_pipelineType = {};
};

} // namespace aph::vk
Expand Down
15 changes: 12 additions & 3 deletions engine/resource/resourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,17 +551,26 @@ void ResourceLoader::load(const ShaderLoadInfo& info, vk::ShaderProgram** ppProg
{
APH_CHECK_RESULT(m_pDevice->create(
vk::ProgramCreateInfo{
.pVertex = shaderList[ShaderStage::VS],
.pFragment = shaderList[ShaderStage::FS],
.geometry{.pVertex = shaderList[ShaderStage::VS], .pFragment = shaderList[ShaderStage::FS]},
.type = PipelineType::Geometry,
},
ppProgram));
}
else if(shaderList.contains(ShaderStage::MS))
{
vk::ProgramCreateInfo ci{
.mesh{.pMesh = shaderList[ShaderStage::MS]},
.type = PipelineType::Geometry,
};
APH_CHECK_RESULT(m_pDevice->create(ci, ppProgram));
}
// cs
else if(shaderList.contains(ShaderStage::CS))
{
APH_CHECK_RESULT(m_pDevice->create(
vk::ProgramCreateInfo{
.pCompute = shaderList[ShaderStage::CS],
.compute{.pCompute = shaderList[ShaderStage::CS]},
.type = PipelineType::Compute,
},
ppProgram));
}
Expand Down

0 comments on commit 2c48db3

Please sign in to comment.