2024-04-10 08:43:56 +02:00
|
|
|
#include "Command.h"
|
2024-04-14 11:35:37 +02:00
|
|
|
#include "Buffer.h"
|
|
|
|
|
#include "Descriptor.h"
|
|
|
|
|
#include "Enums.h"
|
|
|
|
|
#include "Graphics/Enums.h"
|
2024-04-10 10:23:06 +02:00
|
|
|
#include "Graphics/Graphics.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"
|
|
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using namespace Seele::Metal;
|
|
|
|
|
|
2024-04-12 09:27:30 +02:00
|
|
|
Command::Command(PGraphics graphics, MTL::CommandBuffer* cmdBuffer)
|
2024-04-14 11:35:37 +02:00
|
|
|
: graphics(graphics), completed(new Event(graphics)), cmdBuffer(cmdBuffer), renderEncoder(nullptr) {}
|
2024-04-10 09:58:39 +02:00
|
|
|
|
|
|
|
|
Command::~Command() { cmdBuffer->release(); }
|
|
|
|
|
|
|
|
|
|
void Command::beginRenderPass(PRenderPass renderPass) {
|
2024-04-14 11:35:37 +02:00
|
|
|
renderEncoder = cmdBuffer->parallelRenderCommandEncoder(renderPass->getDescriptor());
|
2024-04-10 08:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 09:58:39 +02:00
|
|
|
void Command::endRenderPass() { renderEncoder->endEncoding(); }
|
|
|
|
|
|
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) {
|
|
|
|
|
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 10:23:06 +02:00
|
|
|
void Command::executeCommands(Array<Gfx::ORenderCommand> commands) {
|
2024-04-14 11:35:37 +02:00
|
|
|
for (auto& command : commands) {
|
2024-04-10 10:23:06 +02:00
|
|
|
auto cmd = Gfx::PRenderCommand(command).cast<RenderCommand>();
|
2024-04-10 09:58:39 +02:00
|
|
|
cmd->end();
|
|
|
|
|
}
|
2024-04-10 08:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 10:23:06 +02:00
|
|
|
void Command::executeCommands(Array<Gfx::OComputeCommand> commands) {
|
2024-04-14 11:35:37 +02:00
|
|
|
for (auto& command : commands) {
|
2024-04-10 10:23:06 +02:00
|
|
|
auto cmd = Gfx::PComputeCommand(command).cast<ComputeCommand>();
|
2024-04-10 09:58:39 +02:00
|
|
|
cmd->end();
|
|
|
|
|
}
|
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-14 11:35:37 +02:00
|
|
|
RenderCommand::RenderCommand(MTL::RenderCommandEncoder* encoder) : encoder(encoder) {}
|
2024-04-10 09:58:39 +02:00
|
|
|
|
|
|
|
|
RenderCommand::~RenderCommand() { encoder->release(); }
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
encoder->setVertexBuffer(buffer.cast<VertexBuffer>()->getHandle(), 0, i++);
|
|
|
|
|
}
|
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) {
|
|
|
|
|
//TODO:
|
|
|
|
|
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-14 11:35:37 +02:00
|
|
|
ComputeCommand::ComputeCommand(MTL::ComputeCommandEncoder* encoder) : encoder(encoder) {}
|
2024-04-10 09:58:39 +02:00
|
|
|
|
|
|
|
|
ComputeCommand::~ComputeCommand() { encoder->release(); }
|
|
|
|
|
|
|
|
|
|
void ComputeCommand::end() { encoder->endEncoding(); }
|
|
|
|
|
|
|
|
|
|
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-14 11:35:37 +02:00
|
|
|
encoder->setBuffer(set.cast<DescriptorSet>()->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-14 11:35:37 +02:00
|
|
|
void ComputeCommand::pushConstants(Gfx::PPipelineLayout, Gfx::SeShaderStageFlags, uint32 offset,
|
|
|
|
|
uint32 size, const void* data) {
|
|
|
|
|
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-14 11:35:37 +02:00
|
|
|
//TODO
|
|
|
|
|
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();
|
|
|
|
|
activeCommand = new Command(graphics, queue->commandBuffer());
|
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) {
|
|
|
|
|
return new RenderCommand(activeCommand->createRenderEncoder());
|
2024-04-10 08:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-14 11:35:37 +02:00
|
|
|
OComputeCommand CommandQueue::getComputeCommand(const std::string& name) {
|
|
|
|
|
return new ComputeCommand(activeCommand->createComputeEncoder());
|
2024-04-10 08:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 09:58:39 +02:00
|
|
|
void CommandQueue::submitCommands(PEvent signalSemaphore) {
|
2024-04-14 11:35:37 +02:00
|
|
|
activeCommand->end(signalSemaphore);
|
|
|
|
|
PEvent prevCmdEvent = activeCommand->getCompletedEvent();
|
|
|
|
|
activeCommand = new Command(graphics, queue->commandBuffer());
|
|
|
|
|
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
|
|
|
}
|