Basic commandbuffer implementation

This commit is contained in:
Dynamitos
2024-04-10 08:43:56 +02:00
parent a0161f1d83
commit d7b228d856
21 changed files with 435 additions and 49 deletions
+2
View File
@@ -1,5 +1,7 @@
target_sources(Engine
PRIVATE
Command.h
Command.mm
Enums.h
Enums.mm
Graphics.h
+88
View File
@@ -0,0 +1,88 @@
#pragma once
#include "Graphics/Command.h"
#include "Metal/MTLComputeCommandEncoder.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "MinimalEngine.h"
#include "RenderPass.h"
#include "Resources.h"
#include "Graphics.h"
namespace Seele {
namespace Metal {
DECLARE_REF(CommandQueue)
DECLARE_REF(ComputeCommand)
DECLARE_REF(RenderCommand)
class Command
{
public:
Command(PGraphics graphics, PCommandQueue owner);
~Command();
void beginRenderPass(PRenderPass renderPass);
void endRenderPass();
void executeCommands(const Array<Gfx::PRenderCommand>& commands);
void executeCommands(const Array<Gfx::PComputeCommand>& commands);
private:
PGraphics graphics;
PCommandQueue owner;
MTL::CommandBuffer* cmdBuffer;
MTL::ParallelRenderCommandEncoder* renderEncoder;
PFence fence;
PSemaphore semaphore;
};
DEFINE_REF(Command)
class RenderCommand : public Gfx::RenderCommand
{
public:
RenderCommand(MTL::RenderCommandEncoder* encoder);
virtual ~RenderCommand();
void end();
virtual void setViewport(Gfx::PViewport viewport) override;
virtual void bindPipeline(Gfx::PGraphicsPipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet descriptorSet) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) override;
virtual void bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) override;
virtual void bindIndexBuffer(Gfx::PIndexBuffer indexBuffer) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override;
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) override;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override;
private:
MTL::RenderCommandEncoder* encoder;
};
DEFINE_REF(RenderCommand)
class ComputeCommand : public Gfx::ComputeCommand
{
public:
ComputeCommand(MTL::ComputeCommandEncoder* encoder);
virtual ~ComputeCommand();
void end();
virtual void bindPipeline(Gfx::PComputePipeline pipeline) override;
virtual void bindDescriptor(Gfx::PDescriptorSet set) override;
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) override;
virtual void pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
private:
MTL::ComputeCommandEncoder* encoder;
};
DEFINE_REF(ComputeCommand)
class CommandQueue
{
public:
CommandQueue(PGraphics graphics);
~CommandQueue();
constexpr MTL::CommandQueue* getHandle()
{
return queue;
}
PCommand getCommands();
PRenderCommand getRenderCommand(const std::string& name);
PComputeCommand getComputeCommand(const std::string& name);
void submitCommands(PSemaphore signalSemaphore = nullptr);
private:
PGraphics graphics;
MTL::CommandQueue* queue;
Array<OCommand> allocatedCommands;
OCommand activeCommand;
};
}
}
+172
View File
@@ -0,0 +1,172 @@
#include "Command.h"
#include "Metal/MTLComputeCommandEncoder.hpp"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "Window.h"
using namespace Seele;
using namespace Seele::Metal;
Command::Command(PGraphics graphics, PCommandQueue owner)
: graphics(graphics)
, owner(owner)
, cmdBuffer(owner->getHandle()->commandBuffer())
, renderEncoder(nullptr)
{
}
Command::~Command()
{
cmdBuffer->release();
}
void Command::beginRenderPass(PRenderPass renderPass)
{
renderEncoder = cmdBuffer->parallelRenderCommandEncoder(renderPass->getDescriptor());
}
void Command::endRenderPass()
{
renderEncoder->endEncoding();
}
void Command::executeCommands(const Array<Gfx::PRenderCommand>& commands)
{
for(auto command : commands)
{
auto cmd = command.cast<RenderCommand>();
cmd->end();
}
}
void Command::executeCommands(const Array<Gfx::PComputeCommand>& commands)
{
for(auto command : commands)
{
auto cmd = command.cast<ComputeCommand>();
cmd->end();
}
}
RenderCommand::RenderCommand(MTL::RenderCommandEncoder* encoder)
: encoder(encoder)
{}
RenderCommand::~RenderCommand()
{
encoder->release();
}
void RenderCommand::end()
{
encoder->endEncoding();
}
void RenderCommand::setViewport(Gfx::PViewport viewport)
{
encoder->setViewport(viewport.cast<Viewport>()->getHandle());
}
void RenderCommand::bindPipeline(Gfx::PGraphicsPipeline pipeline)
{
encoder->setRenderPipelineState(pipeline.cast<GraphicsPipeline>()->getState());
}
void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet)
{
encoder->set?????
}
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets)
{
encoder->set?????
}
void RenderCommand::bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers)
{
encoder->setVertexBuffers();
}
void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer indexBuffer)
{
encoder->setObjectBuffer(??, NS::UInteger offset, NS::UInteger index)
}
void RenderCommand::pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
{
???
}
void RenderCommand::draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance)
{
encoder->drawPrimitives(???, firstVertex, vertexCount, instanceCount, firstInstance);
}
void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance)
{
encoder->drawIndexedPrimitives(???, indexCount, indexbuffer->getType(), indexBuffer->getBuffer(), firstIndex, instanceCount, vertexOffset, firstInstance);
}
void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ)
{
encoder->drawMeshThreads(MTL::Size threadsPerGrid, MTL::Size threadsPerObjectThreadgroup, MTL::Size threadsPerMeshThreadgroup)
}
ComputeCommand::ComputeCommand(MTL::ComputeCommandEncoder* encoder)
: encoder(encoder)
{}
ComputeCommand::~ComputeCommand()
{
encoder->release();
}
void ComputeCommand::end()
{
encoder->endEncoding();
}
void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline)
{
encoder->setComputePipelineState(pipeline);
}
void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set)
{
encoder->set???
}
void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& sets)
{
encoder->set???
}
void ComputeCommand::pushConstants(Gfx::PPipelineLayout layout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data)
{
???
}
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ)
{
encoder->dispatchThreadgroups(???);
}
CommandQueue::CommandQueue(PGraphics graphics)
: graphics(graphics)
{
queue = graphics->getDevice()->newCommandQueue();
}
CommandQueue::~CommandQueue()
{
queue->release();
}
PRenderCommand CommandQueue::getRenderCommand(const std::string& name)
{
}
PComputeCommand CommandQueue::getComputeCommand(const std::string& name)
{
}
+5 -2
View File
@@ -1,7 +1,6 @@
#pragma once
#include "Graphics/Enums.h"
#include "Metal/MTLPixelFormat.hpp"
#include "Metal/MTLTexture.hpp"
#include "Resources.h"
namespace Seele
{
@@ -9,5 +8,9 @@ namespace Metal
{
MTL::PixelFormat cast(Gfx::SeFormat format);
Gfx::SeFormat cast(MTL::PixelFormat format);
MTL::LoadAction cast(Gfx::SeAttachmentLoadOp loadOp);
Gfx::SeAttachmentLoadOp cast(MTL::LoadAction loadOp);
MTL::StoreAction cast(Gfx::SeAttachmentStoreOp storeOp);
Gfx::SeAttachmentStoreOp cast(MTL::StoreAction storeOp);
}
}
+49
View File
@@ -1,4 +1,5 @@
#include "Enums.h"
#include "Graphics/Enums.h"
#include <stdexcept>
using namespace Seele;
@@ -538,3 +539,51 @@ Gfx::SeFormat Seele::Metal::cast(MTL::PixelFormat format) {
throw std::logic_error("Not implemented");
}
}
MTL::LoadAction Seele::Metal::cast(Gfx::SeAttachmentLoadOp loadOp) {
switch (loadOp) {
case Seele::Gfx::SE_ATTACHMENT_LOAD_OP_LOAD:
return MTL::LoadActionLoad;
case Seele::Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR:
return MTL::LoadActionClear;
case Seele::Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE:
return MTL::LoadActionDontCare;
default:
throw std::logic_error("Not implemented");
}
}
Gfx::SeAttachmentLoadOp Seele::Metal::cast(MTL::LoadAction loadOp) {
switch (loadOp) {
case MTL::LoadActionDontCare:
return Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE;
case MTL::LoadActionLoad:
return Gfx::SE_ATTACHMENT_LOAD_OP_LOAD;
case MTL::LoadActionClear:
return Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR;
default:
throw std::logic_error("Not implemented");
}
}
MTL::StoreAction Seele::Metal::cast(Gfx::SeAttachmentStoreOp storeOp) {
switch (storeOp) {
case Seele::Gfx::SE_ATTACHMENT_STORE_OP_STORE:
return MTL::StoreActionStore;
case Seele::Gfx::SE_ATTACHMENT_STORE_OP_DONT_CARE:
return MTL::StoreActionDontCare;
default:
throw std::logic_error("Not implemented");
}
}
Gfx::SeAttachmentStoreOp Seele::Metal::cast(MTL::StoreAction storeOp) {
switch (storeOp) {
case MTL::StoreActionDontCare:
return Gfx::SE_ATTACHMENT_STORE_OP_DONT_CARE;
case MTL::StoreActionStore:
return Gfx::SE_ATTACHMENT_STORE_OP_STORE;
default:
throw std::logic_error("Not implemented");
}
}
+2
View File
@@ -1,4 +1,5 @@
#pragma once
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "Metal/Metal.hpp"
#include "Graphics/Graphics.h"
@@ -57,6 +58,7 @@ protected:
MTL::Device* device;
MTL::Library* library;
MTL::CommandQueue* queue;
MTL::RenderCommandEncoder* encoder;
};
DEFINE_REF(Graphics)
} // namespace Metal
+3 -2
View File
@@ -1,4 +1,5 @@
#include "Graphics.h"
#include "Graphics/Metal/RenderPass.h"
#include "Window.h"
using namespace Seele;
@@ -34,13 +35,13 @@ Gfx::OViewport Graphics::createViewport(Gfx::PWindow owner, const ViewportCreate
Gfx::ORenderPass Graphics::createRenderPass(Gfx::RenderTargetLayout layout, Array<Gfx::SubPassDependency> dependencies, Gfx::PViewport renderArea)
{
return new RenderPass(this, layout, dependencies, renderArea);
}
void Graphics::beginRenderPass(Gfx::PRenderPass renderPass)
{
}
void Graphics::endRenderPass()
{
+8
View File
@@ -1,6 +1,7 @@
#pragma once
#include "Graphics/RenderTarget.h"
#include "Graphics.h"
#include "Metal/MTLRenderPass.hpp"
namespace Seele
{
@@ -11,9 +12,16 @@ class RenderPass : public Gfx::RenderPass
public:
RenderPass(PGraphics graphics, Gfx::RenderTargetLayout layout, Array<Gfx::SubPassDependency> dependencies, Gfx::PViewport viewport);
virtual ~RenderPass();
MTL::RenderPassDescriptor* getDescriptor() const
{
return renderPass;
}
private:
PGraphics graphics;
Gfx::PViewport viewport;
MTL::RenderPassDescriptor* renderPass;
MTL::RenderPassDepthAttachmentDescriptor* depth;
MTL::RenderPassStencilAttachmentDescriptor* stencil;
};
DEFINE_REF(RenderPass)
} // namespace Metal
+53 -8
View File
@@ -1,17 +1,62 @@
#include "RenderPass.h"
#include "Enums.h"
#include "Graphics/RenderTarget.h"
#include "Metal/MTLRenderPass.hpp"
#include "Texture.h"
using namespace Seele;
using namespace Seele::Metal;
RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout layout, Array<Gfx::SubPassDependency> dependencies, Gfx::PViewport viewport)
: Gfx::RenderPass(layout, dependencies)
, graphics(graphics)
, viewport(viewport)
{
RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout layout,
Array<Gfx::SubPassDependency> dependencies,
Gfx::PViewport viewport)
: Gfx::RenderPass(layout, dependencies), graphics(graphics),
viewport(viewport) {
renderPass = MTL::RenderPassDescriptor::alloc()->init();
renderPass->setRenderTargetArrayLength(1);
renderPass->setRenderTargetWidth(viewport->getWidth());
renderPass->setRenderTargetHeight(viewport->getHeight());
renderPass->setDefaultRasterSampleCount(viewport->getSamples());
for (size_t i = 0; i < layout.colorAttachments.size(); ++i) {
const auto &color = layout.colorAttachments[i];
MTL::RenderPassColorAttachmentDescriptor *desc =
renderPass->colorAttachments()->object(i);
desc->setClearColor(MTL::ClearColor(
color.clear.color.float32[0], color.clear.color.float32[1],
color.clear.color.float32[2], color.clear.color.float32[3]));
desc->setLoadAction(cast(color.getLoadOp()));
desc->setStoreAction(cast(color.getStoreOp()));
desc->setLevel(0);
desc->setTexture(color.getTexture().cast<TextureBase>()->getTexture());
if (!layout.resolveAttachments.empty()) {
const auto &resolve = layout.resolveAttachments[i];
desc->setResolveLevel(0);
desc->setStoreAction(MTL::StoreActionStoreAndMultisampleResolve);
desc->setResolveTexture(
resolve.getTexture().cast<TextureBase>()->getTexture());
}
colorAttachments->setObject(desc, i);
}
if (layout.depthAttachment.getTexture() != nullptr) {
depth = MTL::RenderPassDepthAttachmentDescriptor::alloc()->init();
depth->setClearDepth(layout.depthAttachment.clear.depthStencil.depth);
depth->setLoadAction(cast(layout.depthAttachment.getLoadOp()));
depth->setStoreAction(cast(layout.depthAttachment.getStoreOp()));
depth->setTexture(
layout.depthAttachment.getTexture().cast<TextureBase>()->getTexture());
if (layout.depthResolveAttachment.getTexture() != nullptr) {
depth->setResolveTexture(layout.depthResolveAttachment.getTexture()
.cast<TextureBase>()
->getTexture());
depth->setStoreAction(MTL::StoreActionStoreAndMultisampleResolve);
}
renderPass->setDepthAttachment(depth);
}
// TODO: stencil
}
RenderPass::~RenderPass()
{
RenderPass::~RenderPass() {
depth->release();
stencil->release();
renderPass->release();
}
-2
View File
@@ -3,8 +3,6 @@
#include "Graphics/Enums.h"
#include "Graphics/Initializer.h"
#include "Graphics/Metal/Graphics.h"
#include "Metal/MTLCommandEncoder.hpp"
#include "Metal/MTLTexture.hpp"
using namespace Seele;
using namespace Seele::Metal;
+9 -2
View File
@@ -1,7 +1,9 @@
#pragma once
#include "Graphics.h"
#include "Graphics/Window.h"
#include "Metal/MTLRenderCommandEncoder.hpp"
#include "Resources.h"
#include "Texture.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
@@ -24,7 +26,7 @@ public:
virtual void pollInput() override;
virtual void beginFrame() override;
virtual void endFrame() override;
virtual Gfx::PTexture2D getBackBuffer() override;
virtual Gfx::PTexture2D getBackBuffer() const override;
virtual void onWindowCloseEvent() override;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) override;
virtual void setMouseMoveCallback(std::function<void(double, double)> callback) override;
@@ -48,6 +50,7 @@ private:
NSWindow* metalWindow;
CAMetalLayer* metalLayer;
CA::MetalDrawable* drawable;
OTexture2D backBuffer;
std::function<void(KeyCode, InputAction, KeyModifier)> keyCallback;
std::function<void(double, double)> mouseMoveCallback;
@@ -63,10 +66,14 @@ class Viewport : public Gfx::Viewport
public:
Viewport(PWindow owner, const ViewportCreateInfo& createInfo);
virtual ~Viewport();
constexpr MTL::Viewport getHandle() const
{
return viewport;
}
virtual void resize(uint32 newX, uint32 newY);
virtual void move(uint32 newOffset, uint32 newOffsetY);
private:
Rect viewport;
MTL::Viewport viewport;
};
} // namespace Metal
} // namespace Seele
+26 -10
View File
@@ -1,6 +1,9 @@
#include "Window.h"
#include "Foundation/NSSharedPtr.hpp"
#include "Graphics/Initializer.h"
#include "Graphics/Metal/Enums.h"
#include "Graphics/Texture.h"
#include "Metal/MTLTexture.hpp"
using namespace Seele;
using namespace Seele::Metal;
@@ -98,6 +101,17 @@ void Window::beginFrame()
{
@autoreleasepool {
drawable = (__bridge CA::MetalDrawable*)[metalLayer nextDrawable];
MTL::Texture* buf = drawable->texture();
backBuffer = new Texture2D(graphics, TextureCreateInfo {
.width = static_cast<uint32>(buf->width()),
.height = static_cast<uint32>(buf->height()),
.depth = static_cast<uint32>(buf->depth()),
.elements = static_cast<uint32>(buf->arrayLength()),
.mipLevels = static_cast<uint32>(buf->mipmapLevelCount()),
.format = cast(buf->pixelFormat()),
.usage = MTL::TextureUsageRenderTarget,
.samples = static_cast<uint32>(buf->sampleCount()),
}, buf);
}
}
@@ -109,9 +123,9 @@ void Window::onWindowCloseEvent()
{
}
Gfx::PTexture2D Window::getBackBuffer()
Gfx::PTexture2D Window::getBackBuffer() const
{
return nullptr; //drawable->texture()
return PTexture2D(backBuffer);
}
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback)
@@ -193,10 +207,12 @@ void Window::resize(int width, int height)
Viewport::Viewport(PWindow owner, const ViewportCreateInfo& createInfo)
: Gfx::Viewport(owner, createInfo)
{
viewport.size.x = sizeX;
viewport.size.y = sizeY;
viewport.offset.x = offsetX;
viewport.offset.y = offsetY;
viewport.width = sizeX;
viewport.height = sizeY;
viewport.originX = offsetX;
viewport.originY = offsetY;
viewport.znear = 0.0f;
viewport.zfar = 1.0f;
}
Viewport::~Viewport()
@@ -206,12 +222,12 @@ Viewport::~Viewport()
void Viewport::resize(uint32 newX, uint32 newY)
{
viewport.size.x = newX;
viewport.size.y = newY;
viewport.width = newX;
viewport.height = newY;
}
void Viewport::move(uint32 newOffset, uint32 newOffsetY)
{
viewport.offset.x = newOffset;
viewport.offset.y = newOffsetY;
viewport.originX = newOffset;
viewport.originY = newOffsetY;
}