Skip to content

Commit

Permalink
add non callback pipeline+api to begin/end cmd buf
Browse files Browse the repository at this point in the history
  • Loading branch information
tadgem committed Jul 12, 2024
1 parent 9c7e94b commit 96501e1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
28 changes: 16 additions & 12 deletions examples/pipeline-abstraction/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ using VkRecordCommandCallback = std::function<void(VkCommandBuffer&, uint32_t, V

struct ViewData
{
Pipeline<VkRecordCommandCallback> m_Pipeline;
PipelineT<VkRecordCommandCallback> m_Pipeline;
View m_View;
Mesh m_ViewQuad;
};

Pipeline<VkRecordCommandCallback> CreateViewPipeline(VulkanAPI& vk, LvkIm3dState& im3dState, ShaderProgram& gbufferProg, ShaderProgram& lightPassProg)
PipelineT<VkRecordCommandCallback> CreateViewPipeline(VulkanAPI& vk, LvkIm3dState& im3dState, ShaderProgram& gbufferProg, ShaderProgram& lightPassProg)
{
Pipeline<VkRecordCommandCallback> p{};
PipelineT<VkRecordCommandCallback> p{};
auto* gbuffer = p.AddFramebuffer(vk);
gbuffer->AddColourAttachment(vk, ResolutionScale::Full, 1, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_IMAGE_ASPECT_COLOR_BIT);
Expand All @@ -53,10 +53,12 @@ Pipeline<VkRecordCommandCallback> CreateViewPipeline(VulkanAPI& vk, LvkIm3dState

// create gbuffer pipeline
VkPipelineLayout gbufferPipelineLayout;
auto gbufferBindingDescriptions = Vector<VkVertexInputBindingDescription>{VertexDataPosNormalUv::GetBindingDescription() };
auto gbufferAttributeDescriptions = VertexDataPosNormalUv::GetAttributeDescriptions();
VkPipeline gbufferPipeline = vk.CreateRasterizationGraphicsPipeline(
gbufferProg,
Vector<VkVertexInputBindingDescription>{VertexDataPosNormalUv::GetBindingDescription() },
VertexDataPosNormalUv::GetAttributeDescriptions(),
gbufferBindingDescriptions,
gbufferAttributeDescriptions,
gbuffer->m_RenderPass,
vk.m_SwapChainImageExtent.width, vk.m_SwapChainImageExtent.height,
VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, false,
Expand All @@ -65,10 +67,12 @@ Pipeline<VkRecordCommandCallback> CreateViewPipeline(VulkanAPI& vk, LvkIm3dState
// create present graphics pipeline
// Pipeline stage?
VkPipelineLayout lightPassPipelineLayout;
auto lightPassBindingDescriptions = Vector<VkVertexInputBindingDescription>{VertexDataPosUv::GetBindingDescription() };
auto lightPassAttributeDescriptions = VertexDataPosUv::GetAttributeDescriptions();
VkPipeline lightPassPipeline = vk.CreateRasterizationGraphicsPipeline(
lightPassProg,
Vector<VkVertexInputBindingDescription>{VertexDataPosUv::GetBindingDescription() },
VertexDataPosUv::GetAttributeDescriptions(),
lightPassBindingDescriptions,
lightPassAttributeDescriptions,
lightPassImage->m_RenderPass,
vk.m_SwapChainImageExtent.width, vk.m_SwapChainImageExtent.height,
VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false,
Expand Down Expand Up @@ -211,7 +215,7 @@ Pipeline<VkRecordCommandCallback> CreateViewPipeline(VulkanAPI& vk, LvkIm3dState

ViewData CreateView(VulkanAPI& vk, LvkIm3dState im3dState, ShaderProgram gbufferProg, ShaderProgram lightPassProg)
{
Pipeline pipeline = CreateViewPipeline(vk, im3dState, gbufferProg, lightPassProg);
PipelineT<VkRecordCommandCallback> pipeline = CreateViewPipeline(vk, im3dState, gbufferProg, lightPassProg);

static Vector<VertexDataPosUv> screenQuadVerts = {
{ { -1.0f, -1.0f , 0.0f}, { 0.0f, 0.0f } },
Expand Down Expand Up @@ -526,14 +530,14 @@ void OnIm3D()
int main() {
VulkanAPI_SDL vk;
bool enableMSAA = false;
vk.Start(1920, 1080, enableMSAA);
vk.Start("Pipeline Example",1920, 1080, enableMSAA);
auto im3dState = LoadIm3D(vk);

DeferredLightData lightDataCpu{};
FillExampleLightData(lightDataCpu);

ShaderProgram gbufferProg = ShaderProgram::Create(vk, "shaders/gbuffer.vert.spv", "shaders/gbuffer.frag.spv");
ShaderProgram lightPassProg = ShaderProgram::Create(vk, "shaders/lights.vert.spv", "shaders/lights.frag.spv");
ShaderProgram gbufferProg = ShaderProgram::Create(vk, "../shaders/gbuffer.vert.spv", "../shaders/gbuffer.frag.spv");
ShaderProgram lightPassProg = ShaderProgram::Create(vk, "../shaders/lights.vert.spv", "../shaders/lights.frag.spv");

ViewData viewA = CreateView(vk, im3dState, gbufferProg, lightPassProg);
viewA.m_View.m_Camera.Position = { -40.0, 10.0f, 30.0f };
Expand All @@ -544,7 +548,7 @@ int main() {

// create vertex and index buffer
// allocate materials instead of raw buffers etc.
RenderModel m = CreateRenderModelGbuffer(vk, "assets/sponza/sponza.gltf", gbufferProg);
RenderModel m = CreateRenderModelGbuffer(vk, "../assets/sponza/sponza.gltf", gbufferProg);

while (vk.ShouldRun())
{
Expand Down
51 changes: 50 additions & 1 deletion lvk/include/lvk/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace lvk
VkPipelineLayout m_PipelineLayout;
};

template<typename _Ty>
class Pipeline
{
public:
Expand All @@ -27,6 +26,56 @@ namespace lvk
Optional<Framebuffer*> m_OutputFramebuffer;
LvkIm3dViewState* m_Im3dState = nullptr;

Framebuffer* AddFramebuffer(VulkanAPI& vk)
{
m_FBs.push_back(new Framebuffer());
return m_FBs[m_FBs.size() - 1];
}

void SetOutputFramebuffer(Framebuffer* fb)
{
m_OutputFramebuffer = fb;
}

Framebuffer* GetOutputFramebuffer()
{
if (m_OutputFramebuffer.has_value())
{
return m_OutputFramebuffer.value();
}
return nullptr;
}

Material* AddMaterial(VulkanAPI& vk, ShaderProgram& prog)
{
m_PipelineMaterials.push_back(new Material(Material::Create(vk, prog)));
return m_PipelineMaterials.back();
}

VkPipelineData* AddPipeline(VulkanAPI& vk, VkPipeline pipeline, VkPipelineLayout layout)
{
m_PipelineDatas.emplace_back(new VkPipelineData(pipeline, layout));
return m_PipelineDatas.back();
}

LvkIm3dViewState* AddIm3d(VulkanAPI& vk, LvkIm3dState im3dState)
{
m_Im3dState = new LvkIm3dViewState(AddIm3dForViewport(vk, im3dState, m_OutputFramebuffer.value()->m_RenderPass, false));
return m_Im3dState;
}
};

template<typename _Ty>
class PipelineT
{
public:
Vector<Framebuffer*> m_FBs;
Vector<Material*> m_PipelineMaterials;
Vector<VkPipelineData*> m_PipelineDatas;

Optional<Framebuffer*> m_OutputFramebuffer;
LvkIm3dViewState* m_Im3dState = nullptr;

Optional<_Ty> m_CommandCallback;

Framebuffer* AddFramebuffer(VulkanAPI& vk)
Expand Down
2 changes: 2 additions & 0 deletions lvk/include/lvk/VulkanAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ namespace lvk
void CopyBufferToImage(VkBuffer& src, VkImage& image, uint32_t width, uint32_t height);
VkCommandBuffer BeginSingleTimeCommands();
void EndSingleTimeCommands(VkCommandBuffer& commandBuffer);
VkCommandBuffer& BeginGraphicsCommands(uint32_t frameIndex);
void EndGraphicsCommands(uint32_t frameIndex);
void RecordGraphicsCommands(std::function<void(VkCommandBuffer&, uint32_t)> graphicsCommandsCallback);
void TransitionImageLayout(VkImage image, VkFormat format, uint32_t numMips, VkImageLayout oldLayout, VkImageLayout newLayout);
void GenerateMips(VkImage image, VkFormat format, uint32_t imageWidth, uint32_t imageHeight, uint32_t numMips, VkFilter filterMethod);
Expand Down
16 changes: 16 additions & 0 deletions lvk/src/lvk/VulkanAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,22 @@ void lvk::VulkanAPI::EndSingleTimeCommands(VkCommandBuffer& commandBuffer)
vkFreeCommandBuffers(m_LogicalDevice , m_GraphicsQueueCommandPool, 1, &commandBuffer);
}

VkCommandBuffer& VulkanAPI::BeginGraphicsCommands(uint32_t frameIndex) {
VkCommandBufferBeginInfo commandBufferBeginInfo{};
commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
commandBufferBeginInfo.flags = 0;
commandBufferBeginInfo.pInheritanceInfo = nullptr;

VK_CHECK(vkBeginCommandBuffer(m_CommandBuffers[frameIndex], &commandBufferBeginInfo));
return m_CommandBuffers[frameIndex];
}

void VulkanAPI::EndGraphicsCommands(uint32_t frameIndex) {
VK_CHECK(vkEndCommandBuffer(m_CommandBuffers[frameIndex]));
}



void lvk::VulkanAPI::RecordGraphicsCommands(std::function<void(VkCommandBuffer&, uint32_t)> graphicsCommandsCallback)
{
for (uint32_t i = 0; i < m_CommandBuffers.size(); i++)
Expand Down

0 comments on commit 96501e1

Please sign in to comment.