Files
Seele/src/Engine/Graphics/Metal/Command.mm
T

232 lines
8.4 KiB
Plaintext
Raw Normal View History

2024-04-10 08:43:56 +02:00
#include "Command.h"
2024-04-14 11:35:37 +02:00
#include "Buffer.h"
2024-04-17 14:33:06 +02:00
#include "Containers/Array.h"
2024-04-14 11:35:37 +02:00
#include "Descriptor.h"
#include "Enums.h"
2024-04-17 14:33:06 +02:00
#include "Graphics/Command.h"
2024-04-14 11:35:37 +02:00
#include "Graphics/Enums.h"
2024-04-10 10:23:06 +02:00
#include "Graphics/Graphics.h"
2024-04-17 14:33:06 +02:00
#include "Graphics/Resources.h"
2024-04-14 11:35:37 +02:00
#include "Pipeline.h"
#include "Resources.h"
2024-04-10 08:43:56 +02:00
#include "Window.h"
2024-04-17 14:33:06 +02:00
#include <Metal/Metal.h>
2024-04-10 08:43:56 +02:00
using namespace Seele;
using namespace Seele::Metal;
2024-04-12 09:27:30 +02:00
Command::Command(PGraphics graphics, MTL::CommandBuffer* cmdBuffer)
2024-04-17 14:33:06 +02:00
: graphics(graphics), completed(new Event(graphics)), cmdBuffer(cmdBuffer) {}
2024-04-10 09:58:39 +02:00
2024-04-17 14:33:06 +02:00
Command::~Command() {}
2024-04-10 09:58:39 +02:00
void Command::beginRenderPass(PRenderPass renderPass) {
2024-04-17 14:33:06 +02:00
if (blitEncoder) {
blitEncoder->endEncoding();
blitEncoder = nullptr;
}
2024-04-18 13:33:35 +02:00
renderPass->updateRenderPass();
2024-04-17 14:33:06 +02:00
parallelEncoder = cmdBuffer->parallelRenderCommandEncoder(renderPass->getDescriptor());
2024-04-10 08:43:56 +02:00
}
2024-04-17 14:33:06 +02:00
void Command::endRenderPass() {
parallelEncoder->endEncoding();
parallelEncoder = nullptr;
}
2024-04-10 09:58:39 +02:00
2024-04-14 11:35:37 +02:00
void Command::present(MTL::Drawable* drawable) { cmdBuffer->presentDrawable(drawable); }
2024-04-10 08:43:56 +02:00
2024-04-10 09:58:39 +02:00
void Command::end(PEvent signal) {
2024-04-17 14:33:06 +02:00
assert(!parallelEncoder);
blitEncoder->endEncoding();
2024-04-10 09:58:39 +02:00
if (signal != nullptr) {
cmdBuffer->encodeSignalEvent(signal->getHandle(), 1);
}
cmdBuffer->encodeSignalEvent(completed->getHandle(), 1);
cmdBuffer->commit();
2024-04-10 08:43:56 +02:00
}
2024-04-10 15:50:20 +02:00
void Command::waitDeviceIdle() { cmdBuffer->waitUntilCompleted(); }
2024-04-10 08:43:56 +02:00
2024-04-14 11:35:37 +02:00
void Command::signalEvent(PEvent event) { cmdBuffer->encodeSignalEvent(event->getHandle(), 1); }
2024-04-10 08:43:56 +02:00
2024-04-14 11:35:37 +02:00
void Command::waitForEvent(PEvent event) { cmdBuffer->encodeWait(event->getHandle(), 1); }
2024-04-10 08:43:56 +02:00
2024-04-17 14:33:06 +02:00
RenderCommand::RenderCommand(MTL::RenderCommandEncoder* encoder, const std::string& name)
: encoder(encoder), name(name) {}
2024-04-10 09:58:39 +02:00
2024-04-17 14:33:06 +02:00
RenderCommand::~RenderCommand() {}
2024-04-10 09:58:39 +02:00
void RenderCommand::end() { encoder->endEncoding(); }
void RenderCommand::setViewport(Gfx::PViewport viewport) {
MTL::Viewport vp = viewport.cast<Viewport>()->getHandle();
MTL::ScissorRect rect = {
.x = static_cast<NS::UInteger>(vp.originX),
.y = static_cast<NS::UInteger>(vp.originY),
.width = static_cast<NS::UInteger>(vp.width),
.height = static_cast<NS::UInteger>(vp.height),
};
encoder->setViewport(vp);
encoder->setScissorRect(rect);
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
void RenderCommand::bindPipeline(Gfx::PGraphicsPipeline pipeline) {
2024-04-14 11:35:37 +02:00
boundPipeline = pipeline.cast<GraphicsPipeline>();
encoder->setRenderPipelineState(boundPipeline->getHandle());
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet) {
2024-04-14 11:35:37 +02:00
encoder->setVertexBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, descriptorSet->getSetIndex());
encoder->setFragmentBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, descriptorSet->getSetIndex());
encoder->setMeshBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, descriptorSet->getSetIndex());
encoder->setObjectBuffer(descriptorSet.cast<DescriptorSet>()->getBuffer(), 0, descriptorSet->getSetIndex());
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorSets) {
for (auto set : descriptorSets) {
bindDescriptor(set);
}
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::bindVertexBuffer(const Array<Gfx::PVertexBuffer>& buffers) {
uint32 i = 0;
for (auto buffer : buffers) {
2024-04-17 14:33:06 +02:00
encoder->setVertexBuffer(buffer.cast<VertexBuffer>()->getHandle(), 0, METAL_VERTEXBUFFER_OFFSET + i++);
2024-04-14 11:35:37 +02:00
}
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::bindIndexBuffer(Gfx::PIndexBuffer gfxIndexBuffer) {
boundIndexBuffer = gfxIndexBuffer.cast<IndexBuffer>();
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size,
const void* data) {
if (stage & Gfx::SE_SHADER_STAGE_VERTEX_BIT) {
encoder->setVertexBytes((char*)data + offset, size, 0);
}
if (stage & Gfx::SE_SHADER_STAGE_FRAGMENT_BIT) {
encoder->setFragmentBytes((char*)data + offset, size, 0);
}
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) {
encoder->drawPrimitives(boundPipeline->getPrimitive(), firstVertex, vertexCount, instanceCount, firstInstance);
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset,
2024-04-10 09:58:39 +02:00
uint32 firstInstance) {
2024-04-14 11:35:37 +02:00
encoder->drawIndexedPrimitives(boundPipeline->getPrimitive(), indexCount, cast(boundIndexBuffer->getIndexType()),
boundIndexBuffer->getHandle(), firstIndex, instanceCount, vertexOffset, firstInstance);
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) {
2024-04-15 13:48:34 +02:00
// TODO:
2024-04-14 11:35:37 +02:00
encoder->drawMeshThreadgroups(MTL::Size(groupX, groupY, groupZ), MTL::Size(128, 128, 128), MTL::Size(32, 32, 32));
}
2024-04-10 09:58:39 +02:00
2024-04-17 14:33:06 +02:00
ComputeCommand::ComputeCommand(MTL::CommandBuffer* cmdBuffer, const std::string& name)
: commandBuffer(cmdBuffer), encoder(cmdBuffer->computeCommandEncoder()), name(name) {}
2024-04-10 09:58:39 +02:00
2024-04-17 14:33:06 +02:00
ComputeCommand::~ComputeCommand() {}
2024-04-10 09:58:39 +02:00
2024-04-17 14:33:06 +02:00
void ComputeCommand::end() {
encoder->endEncoding();
commandBuffer->commit();
}
2024-04-10 09:58:39 +02:00
void ComputeCommand::bindPipeline(Gfx::PComputePipeline pipeline) {
2024-04-14 11:35:37 +02:00
encoder->setComputePipelineState(pipeline.cast<ComputePipeline>()->getHandle());
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet set) {
2024-04-15 13:48:34 +02:00
auto metalSet = set.cast<DescriptorSet>();
metalSet->bind();
encoder->setBuffer(metalSet->getBuffer(), 0, set->getSetIndex());
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) {
for (auto& set : sets) {
bindDescriptor(set);
}
2024-04-10 08:43:56 +02:00
}
2024-04-15 13:48:34 +02:00
void ComputeCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags, uint32 offset, uint32 size,
const void* data) {
2024-04-14 11:35:37 +02:00
encoder->setBytes((char*)data + offset, size, 0);
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) {
2024-04-15 13:48:34 +02:00
// TODO
2024-04-14 11:35:37 +02:00
encoder->dispatchThreadgroups(MTL::Size(threadX, threadY, threadZ), MTL::Size(32, 32, 32));
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
CommandQueue::CommandQueue(PGraphics graphics) : graphics(graphics) {
2024-04-14 11:35:37 +02:00
queue = graphics->getDevice()->newCommandQueue();
2024-04-17 14:33:06 +02:00
MTL::CommandBufferDescriptor* descriptor = MTL::CommandBufferDescriptor::alloc()->init();
descriptor->setErrorOptions(MTL::CommandBufferErrorOptionEncoderExecutionStatus);
activeCommand = new Command(graphics, queue->commandBuffer(descriptor));
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
CommandQueue::~CommandQueue() { queue->release(); }
2024-04-14 11:35:37 +02:00
ORenderCommand CommandQueue::getRenderCommand(const std::string& name) {
2024-04-17 14:33:06 +02:00
return new RenderCommand(activeCommand->createRenderEncoder(), name);
2024-04-10 08:43:56 +02:00
}
2024-04-14 11:35:37 +02:00
OComputeCommand CommandQueue::getComputeCommand(const std::string& name) {
2024-04-17 14:33:06 +02:00
return new ComputeCommand(queue->commandBuffer(), name);
}
void CommandQueue::executeCommands(Array<Gfx::ORenderCommand> commands) {
for (auto& command : commands) {
auto metalCmd = Gfx::PRenderCommand(command).cast<RenderCommand>();
metalCmd->end();
}
}
void CommandQueue::executeCommands(Array<Gfx::OComputeCommand> commands) {
submitCommands();
for (auto& command : commands) {
auto metalCmd = Gfx::PComputeCommand(command).cast<ComputeCommand>();
metalCmd->end();
}
2024-04-10 08:43:56 +02:00
}
2024-04-10 09:58:39 +02:00
void CommandQueue::submitCommands(PEvent signalSemaphore) {
2024-04-15 13:48:34 +02:00
activeCommand->getHandle()->addCompletedHandler(MTL::CommandBufferHandler([&](MTL::CommandBuffer* cmdBuffer) {
2024-04-17 14:33:06 +02:00
for (auto it = pendingCommands.begin(); it != pendingCommands.end(); it++) {
if ((*it)->getHandle() == cmdBuffer) {
2024-04-15 13:48:34 +02:00
pendingCommands.remove(it);
return;
}
}
}));
2024-04-14 11:35:37 +02:00
activeCommand->end(signalSemaphore);
2024-04-17 14:33:06 +02:00
activeCommand->waitDeviceIdle();
2024-04-14 11:35:37 +02:00
PEvent prevCmdEvent = activeCommand->getCompletedEvent();
2024-04-15 13:48:34 +02:00
pendingCommands.add(std::move(activeCommand));
2024-04-17 14:33:06 +02:00
MTL::CommandBufferDescriptor* descriptor = MTL::CommandBufferDescriptor::alloc()->init();
descriptor->setErrorOptions(MTL::CommandBufferErrorOptionEncoderExecutionStatus);
activeCommand = new Command(graphics, queue->commandBuffer(descriptor));
2024-04-14 11:35:37 +02:00
activeCommand->waitForEvent(prevCmdEvent);
2024-04-10 15:50:20 +02:00
}
IOCommandQueue::IOCommandQueue(PGraphics graphics) : graphics(graphics) {
MTL::IOCommandQueueDescriptor* desc = MTL::IOCommandQueueDescriptor::alloc()->init();
desc->setType(MTL::IOCommandQueueTypeConcurrent);
desc->setPriority(MTL::IOPriorityNormal);
2024-04-12 09:27:30 +02:00
queue = graphics->getDevice()->newIOCommandQueue(desc, nullptr);
2024-04-14 11:35:37 +02:00
// activeCommand = new Command(graphics, queue->commandBuffer());
2024-04-10 15:50:20 +02:00
desc->release();
}
IOCommandQueue::~IOCommandQueue() { queue->release(); }
void IOCommandQueue::submitCommands(PEvent signalSemaphore) {
2024-04-14 11:35:37 +02:00
// TODO: scratch buffer
activeCommand->end(signalSemaphore);
PEvent prevCmdEvent = activeCommand->getCompletedEvent();
// activeCommand = new Command(graphics, queue->commandBuffer());
activeCommand->waitForEvent(prevCmdEvent);
2024-04-10 08:43:56 +02:00
}