diff --git a/src/Engine/Graphics/Metal/Buffer.h b/src/Engine/Graphics/Metal/Buffer.h new file mode 100644 index 0000000..3189d00 --- /dev/null +++ b/src/Engine/Graphics/Metal/Buffer.h @@ -0,0 +1,46 @@ +#pragma once +#include "Graphics/Buffer.h" +#include "Graphics/Initializer.h" + +namespace Seele { +namespace Metal { +DECLARE_REF(Graphics) +class Buffer +{ + +}; +DEFINE_REF(Buffer) +class VertexBuffer : public Gfx::VertexBuffer, public Buffer +{ +public: + VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo& createInfo); + virtual ~VertexBuffer(); +private: +}; +DEFINE_REF(VertexBuffer) +class IndexBuffer : public Gfx::IndexBuffer, public Buffer +{ +public: + IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& createInfo); + virtual ~IndexBuffer(); +private: +}; +DEFINE_REF(IndexBuffer) +class UniformBuffer : public Gfx::UniformBuffer, public Buffer +{ +public: + UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo& createInfo); + virtual ~UniformBuffer(); +private: +}; +DEFINE_REF(UniformBuffer) +class ShaderBuffer : public Gfx::ShaderBuffer, public Buffer +{ +public: + ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo& createInfo); + virtual ~ShaderBuffer(); +private: +}; +DEFINE_REF(ShaderBuffer) +} +} \ No newline at end of file diff --git a/src/Engine/Graphics/Metal/Buffer.mm b/src/Engine/Graphics/Metal/Buffer.mm new file mode 100644 index 0000000..1e2a7fc --- /dev/null +++ b/src/Engine/Graphics/Metal/Buffer.mm @@ -0,0 +1 @@ +#include "Buffer.h" \ No newline at end of file diff --git a/src/Engine/Graphics/Metal/CMakeLists.txt b/src/Engine/Graphics/Metal/CMakeLists.txt index afc99d4..03c9cf2 100644 --- a/src/Engine/Graphics/Metal/CMakeLists.txt +++ b/src/Engine/Graphics/Metal/CMakeLists.txt @@ -1,5 +1,7 @@ target_sources(Engine PRIVATE + Buffer.h + Buffer.mm Command.h Command.mm Descriptor.h diff --git a/src/Engine/Graphics/Metal/Command.h b/src/Engine/Graphics/Metal/Command.h index a7d366f..834a99b 100644 --- a/src/Engine/Graphics/Metal/Command.h +++ b/src/Engine/Graphics/Metal/Command.h @@ -2,6 +2,7 @@ #include "Graphics/Command.h" #include "Metal/MTLComputeCommandEncoder.hpp" #include "Metal/MTLDrawable.hpp" +#include "Metal/MTLIOCommandQueue.hpp" #include "Metal/MTLRenderCommandEncoder.hpp" #include "MinimalEngine.h" #include "RenderPass.h" @@ -30,6 +31,10 @@ public: MTL::RenderCommandEncoder* createRenderEncoder() { return renderEncoder->renderCommandEncoder(); } MTL::ComputeCommandEncoder* createComputeEncoder() { return cmdBuffer->computeCommandEncoder(); } PEvent getCompletedEvent() const { return completed; } + constexpr MTL::CommandBuffer* getHandle() const + { + return cmdBuffer; + } private: PGraphics graphics; PCommandQueue owner; @@ -91,5 +96,22 @@ private: MTL::CommandQueue* queue; OCommand activeCommand; }; +class IOCommandQueue +{ +public: + IOCommandQueue(PGraphics graphics); + ~IOCommandQueue(); + constexpr MTL::IOCommandQueue* getHandle() + { + return queue; + } + PCommand getCommands(); + void submitCommands(PEvent signal = nullptr); +private: + PGraphics graphics; + MTL::IOCommandQueue* queue; + OCommand activeCommand; +}; +DEFINE_REF(IOCommandQueue) } } \ No newline at end of file diff --git a/src/Engine/Graphics/Metal/Command.mm b/src/Engine/Graphics/Metal/Command.mm index b45668b..64f731a 100644 --- a/src/Engine/Graphics/Metal/Command.mm +++ b/src/Engine/Graphics/Metal/Command.mm @@ -1,6 +1,7 @@ #include "Command.h" #include "Graphics/Graphics.h" #include "Graphics/Metal/Resources.h" +#include "Metal/MTLIOCommandQueue.hpp" #include "Metal/MTLRenderCommandEncoder.hpp" #include "Window.h" @@ -33,22 +34,20 @@ void Command::end(PEvent signal) { } void Command::executeCommands(Array commands) { - for (auto& command : commands) { + for (auto &command : commands) { auto cmd = Gfx::PRenderCommand(command).cast(); cmd->end(); } } void Command::executeCommands(Array commands) { - for (auto& command : commands) { + for (auto &command : commands) { auto cmd = Gfx::PComputeCommand(command).cast(); cmd->end(); } } -void Command::waitDeviceIdle() { - cmdBuffer->waitUntilCompleted(); -} +void Command::waitDeviceIdle() { cmdBuffer->waitUntilCompleted(); } void Command::signalEvent(PEvent event) { cmdBuffer->encodeSignalEvent(event->getHandle(), 1); @@ -167,7 +166,26 @@ OComputeCommand CommandQueue::getComputeCommand(const std::string &name) { void CommandQueue::submitCommands(PEvent signalSemaphore) { activeCommand->end(signalSemaphore); - MTL::Event* prevCmdEvent = activeCommand->getCompletedEvent(); + MTL::Event *prevCmdEvent = activeCommand->getCompletedEvent(); + activeCommand = new Command(graphics, this); + activeCommand->waitForEvent(prevCmdEvent); +} + +IOCommandQueue::IOCommandQueue(PGraphics graphics) : graphics(graphics) { + MTL::IOCommandQueueDescriptor* desc = MTL::IOCommandQueueDescriptor::alloc()->init(); + desc->setType(MTL::IOCommandQueueTypeConcurrent); + desc->setPriority(MTL::IOPriorityNormal); + queue = graphics->getDevice()->newIOCommandQueue(desc); + activeCommand = new Command(graphics, this); + desc->release(); +} + +IOCommandQueue::~IOCommandQueue() { queue->release(); } + +void IOCommandQueue::submitCommands(PEvent signalSemaphore) { + //TODO: scratch buffer + activeCommand->end(signalSemaphore); + MTL::Event *prevCmdEvent = activeCommand->getCompletedEvent(); activeCommand = new Command(graphics, this); activeCommand->waitForEvent(prevCmdEvent); } diff --git a/src/Engine/Graphics/Metal/Descriptor.h b/src/Engine/Graphics/Metal/Descriptor.h index 97b80ad..e10f36d 100644 --- a/src/Engine/Graphics/Metal/Descriptor.h +++ b/src/Engine/Graphics/Metal/Descriptor.h @@ -1,23 +1,53 @@ #pragma once #include "Graphics/Descriptor.h" +#include "Graphics/Initializer.h" +#include "MinimalEngine.h" namespace Seele { namespace Metal { DECLARE_REF(Graphics) +DECLARE_REF(DescriptorPool) class DescriptorLayout : public Gfx::DescriptorLayout { public: DescriptorLayout(PGraphics graphics, const std::string& name); virtual ~DescriptorLayout(); - virtual void create(); + virtual void create() override; private: PGraphics graphics; }; class PipelineLayout : public Gfx::PipelineLayout { public: + PipelineLayout(PGraphics graphics, Gfx::PPipelineLayout baseLayout) + : Gfx::PipelineLayout(baseLayout) + , graphics(graphics) + {} private: + PGraphics graphics; +}; +DEFINE_REF(PipelineLayout) +class DescriptorSet : public Gfx::DescriptorSet +{ +public: + DescriptorSet(PGraphics graphics, PDescriptorPool owner) + : graphics(graphics) + , owner(owner) + {} + virtual ~DescriptorSet(); +private: + PGraphics graphics; + PDescriptorPool owner; +}; +DEFINE_REF(DescriptorSet) + +class DescriptorPool : public Gfx::DescriptorPool +{ +public: + DescriptorPool(PGraphics graphics); +private: + PGraphics graphics; }; } } \ No newline at end of file diff --git a/src/Engine/Graphics/Metal/Graphics.h b/src/Engine/Graphics/Metal/Graphics.h index 9ae78a3..2384083 100644 --- a/src/Engine/Graphics/Metal/Graphics.h +++ b/src/Engine/Graphics/Metal/Graphics.h @@ -1,4 +1,5 @@ #pragma once +#include "Graphics/Metal/Command.h" #include "Metal/Metal.hpp" #include "Graphics/Graphics.h" @@ -55,10 +56,12 @@ public: MTL::Device* getDevice() const { return device; } PCommandQueue getQueue() const { return queue; } + PIOCommandQueue getIOQueue() const { return ioQueue; } protected: MTL::Device* device; MTL::Library* library; OCommandQueue queue; + OIOCommandQueue ioQueue; }; DEFINE_REF(Graphics) } // namespace Metal diff --git a/src/Engine/Graphics/Metal/Graphics.mm b/src/Engine/Graphics/Metal/Graphics.mm index fcee20b..78d788c 100644 --- a/src/Engine/Graphics/Metal/Graphics.mm +++ b/src/Engine/Graphics/Metal/Graphics.mm @@ -22,6 +22,7 @@ void Graphics::init(GraphicsInitializer) library = device->newDefaultLibrary(); assert(library); queue = new CommandQueue(this); + ioQueue = new IOCommandQueue(this); } Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo) diff --git a/src/Engine/Graphics/Metal/Texture.mm b/src/Engine/Graphics/Metal/Texture.mm index 3b09abe..4416dbe 100644 --- a/src/Engine/Graphics/Metal/Texture.mm +++ b/src/Engine/Graphics/Metal/Texture.mm @@ -3,6 +3,7 @@ #include "Graphics/Enums.h" #include "Graphics/Initializer.h" #include "Graphics/Metal/Graphics.h" +#include "Metal/MTLTypes.hpp" using namespace Seele; using namespace Seele::Metal; @@ -44,6 +45,11 @@ TextureBase::TextureBase(PGraphics graphics, MTL::TextureType type, descriptor->release(); } + if(createInfo.sourceData.data != nullptr) + { + MTL::Region region(0, 0, 0, width, height, depth); + texture->replaceRegion(region, 0, createInfo.sourceData.data, createInfo.sourceData.size / (depth / height)); + } } TextureBase::~TextureBase() { @@ -55,7 +61,10 @@ TextureBase::~TextureBase() { void TextureBase::executePipelineBarrier(Gfx::SeAccessFlags, Gfx::SePipelineStageFlags, Gfx::SeAccessFlags, - Gfx::SePipelineStageFlags) {} + Gfx::SePipelineStageFlags) { + + +} void TextureBase::changeLayout(Gfx::SeImageLayout, Gfx::SeAccessFlags, Gfx::SePipelineStageFlags, Gfx::SeAccessFlags, diff --git a/src/Engine/Graphics/Vulkan/Descriptor.h b/src/Engine/Graphics/Vulkan/Descriptor.h index cbe6782..c5bd0c5 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.h +++ b/src/Engine/Graphics/Vulkan/Descriptor.h @@ -14,7 +14,7 @@ class DescriptorLayout : public Gfx::DescriptorLayout public: DescriptorLayout(PGraphics graphics, const std::string& name); virtual ~DescriptorLayout(); - virtual void create(); + virtual void create() override; constexpr VkDescriptorSetLayout getHandle() const { return layoutHandle;