From b0ecb164eeaf5ba6bcab000653695a9acd8d439c Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Mon, 18 Nov 2024 21:05:03 -0800 Subject: [PATCH] feat: update desciprot and textureo Signed-off-by: Michael Pollind --- Source/Metal/CommandAllocatorMTL.h | 4 +++- Source/Metal/CommandAllocatorMTL.mm | 5 +++-- Source/Metal/CommandBufferMTL.mm | 26 +++++++++++++++++++++--- Source/Metal/DescriptorMTL.h | 26 +++++++++++++++++------- Source/Metal/DescriptorMTL.mm | 31 +++++++++++++++++++++++++++++ Source/Metal/PipelineMTL.mm | 5 ++++- Source/Metal/TextureMTL.h | 7 +++---- 7 files changed, 86 insertions(+), 18 deletions(-) diff --git a/Source/Metal/CommandAllocatorMTL.h b/Source/Metal/CommandAllocatorMTL.h index d10a47a7..127c7adf 100644 --- a/Source/Metal/CommandAllocatorMTL.h +++ b/Source/Metal/CommandAllocatorMTL.h @@ -25,11 +25,13 @@ struct CommandAllocatorMTL { // NRI //================================================================================================================ + void SetDebugName(const char* name); Result CreateCommandBuffer(CommandBuffer*& commandBuffer); + void Reset(); private: - struct CommandQueueMTL* m_CommandQueue; DeviceMTL& m_Device; + struct CommandQueueMTL* m_CommandQueue; }; } diff --git a/Source/Metal/CommandAllocatorMTL.mm b/Source/Metal/CommandAllocatorMTL.mm index 9532664f..9fc54bde 100644 --- a/Source/Metal/CommandAllocatorMTL.mm +++ b/Source/Metal/CommandAllocatorMTL.mm @@ -1,7 +1,7 @@ #include "SharedMTL.h" #include "CommandAllocatorMTL.h" - +#include "CommandBufferMTL.h" #include "CommandQueueMTL.h" using namespace nri; @@ -20,8 +20,9 @@ //================================================================================================================ Result CommandAllocatorMTL::CreateCommandBuffer(CommandBuffer*& commandBuffer) { - + CommandBufferMTL* commandBufferImpl = Allocate(m_Device.GetStdAllocator(), m_Device); + commandBuffer = (CommandBuffer*)commandBufferImpl; return Result::SUCCESS; } diff --git a/Source/Metal/CommandBufferMTL.mm b/Source/Metal/CommandBufferMTL.mm index 8cbfb0ab..5cf14718 100644 --- a/Source/Metal/CommandBufferMTL.mm +++ b/Source/Metal/CommandBufferMTL.mm @@ -23,7 +23,7 @@ } Result CommandBufferMTL::Begin(const DescriptorPool* descriptorPool) { - [m_Handle computeCommandEncoderWithDescriptor: NULL]; + m_ComputeEncoder = [m_Handle computeCommandEncoderWithDescriptor: NULL]; } Result CommandBufferMTL::End() { @@ -93,15 +93,32 @@ } void CommandBufferMTL::BeginRendering(const AttachmentsDesc& attachmentsDesc) { MTLRenderPassDescriptor* renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor]; + for(uint32_t i = 0; i < attachmentsDesc.colorNum; i++) { + DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.colors[i]; + + renderPassDescriptor.colorAttachments[i].texture = descriptorMTL.GetImageView() ; + renderPassDescriptor.colorAttachments[i].clearColor = MTLClearColorMake(0, 0, 0, 1); + renderPassDescriptor.colorAttachments[i].loadAction = MTLLoadActionClear; + renderPassDescriptor.colorAttachments[i].storeAction = MTLStoreActionStore; + + } + + if(attachmentsDesc.depthStencil) { + DescriptorMTL& descriptorMTL = *(DescriptorMTL*)attachmentsDesc.depthStencil; + renderPassDescriptor.depthAttachment.texture = descriptorMTL.GetImageView(); + // renderPassDescriptor.depthAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); + renderPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear; + renderPassDescriptor.depthAttachment.storeAction = MTLStoreActionStore; + } //renderPassDescriptor.colorAttachments[ //renderPassDescriptor.colorAttachments - m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: NULL]; + m_RendererEncoder = [m_Handle renderCommandEncoderWithDescriptor: renderPassDescriptor]; } void CommandBufferMTL::EndRendering() { [m_RendererEncoder endEncoding]; @@ -110,8 +127,11 @@ } void CommandBufferMTL::SetViewports(const Viewport* viewports, uint32_t viewportNum) { //MTLViewport* mtlViewports = StackAlloc(MTLViewport, viewportNum); + + Scratch mtlViewports = AllocateScratch(m_Device, MTLViewport, viewportNum); + - // [m_RendererEncoder setViewports:<#(const MTLViewport * _Nonnull)#> count:<#(NSUInteger)#> + [m_RendererEncoder setViewports:mtlViewports count:viewportNum]; } void CommandBufferMTL::SetScissors(const Rect* rects, uint32_t rectNum) { NSCAssert(m_RendererEncoder, @"encoder set"); diff --git a/Source/Metal/DescriptorMTL.h b/Source/Metal/DescriptorMTL.h index 0cce5fea..0977fb38 100644 --- a/Source/Metal/DescriptorMTL.h +++ b/Source/Metal/DescriptorMTL.h @@ -6,20 +6,32 @@ namespace nri { struct DeviceMTL; +enum class DescriptorTypeMTL { + NONE, + IMAGE_VIEW +}; struct DescriptorMTL { - +public: inline DescriptorMTL (DeviceMTL& device) : m_Device(device) { } - - inline Result Create(const Texture1DViewDesc& textureViewDesc) {} - inline Result Create(const Texture2DViewDesc& textureViewDesc){} - inline Result Create(const Texture3DViewDesc& textureViewDesc){} - inline Result Create(const SamplerDesc& samplerDesc){} - + + inline id GetImageView() { + return m_texture; + } + + + Result Create(const BufferViewDesc& bufferViewDesc); + Result Create(const Texture1DViewDesc& textureViewDesc); + Result Create(const Texture2DViewDesc& textureViewDesc); + Result Create(const Texture3DViewDesc& textureViewDesc); + Result Create(const SamplerDesc& samplerDesc); private: DeviceMTL& m_Device; + + id m_texture; + DescriptorTypeMTL m_type = DescriptorTypeMTL::NONE; }; diff --git a/Source/Metal/DescriptorMTL.mm b/Source/Metal/DescriptorMTL.mm index e69de29b..595a5d39 100644 --- a/Source/Metal/DescriptorMTL.mm +++ b/Source/Metal/DescriptorMTL.mm @@ -0,0 +1,31 @@ +#include "SharedMTL.h" + +#include "DescriptorMTL.h" + +#include "BufferMTL.h" + +namespace nri { + +Result DescriptorMTL::Create(const BufferViewDesc& bufferViewDesc) { + // m_Type = DescriptorTypeVK::BUFFER_VIEW; + const BufferMTL& buffer = *(const BufferMTL*)bufferViewDesc.buffer; + + return Result::SUCCESS; + +} +Result DescriptorMTL::Create(const Texture1DViewDesc& textureViewDesc) { + return Result::SUCCESS; + +} +Result DescriptorMTL::Create(const Texture2DViewDesc& textureViewDesc) { + + return Result::SUCCESS; +} +Result DescriptorMTL::Create(const Texture3DViewDesc& textureViewDesc){ + return Result::SUCCESS; +} +Result DescriptorMTL::Create(const SamplerDesc& samplerDesc){ + return Result::SUCCESS; +} + +} diff --git a/Source/Metal/PipelineMTL.mm b/Source/Metal/PipelineMTL.mm index f5002f7e..9a74abc0 100644 --- a/Source/Metal/PipelineMTL.mm +++ b/Source/Metal/PipelineMTL.mm @@ -63,7 +63,6 @@ } else if(shader.stage & nri::StageBits::FRAGMENT_SHADER) { renderPipelineDesc.fragmentFunction = entryPointFunc; } - } // Depth-stencil const DepthAttachmentDesc& da = graphicsPipelineDesc.outputMerger.depth; @@ -92,7 +91,11 @@ if (graphicsPipelineDesc.multisample) { // TODO: multisampling + // } + + // renderPipelineDesc.rasterSampleCount = pCreateInfo->pMultisampleState->pSampleMask[0]; + // Blending diff --git a/Source/Metal/TextureMTL.h b/Source/Metal/TextureMTL.h index a6a4f1ea..a88c981d 100644 --- a/Source/Metal/TextureMTL.h +++ b/Source/Metal/TextureMTL.h @@ -14,9 +14,9 @@ struct TextureMTL { } ~TextureMTL(); - //inline id GetHandle() const { - // return m_Handle; - //} + inline id GetHandle() const { + return m_Handle; + } inline DeviceMTL& GetDevice() const { return m_Device; @@ -27,7 +27,6 @@ struct TextureMTL { } Result Create(const TextureDesc& textureDesc); - //Result Create(const TextureMTLDesc& textureDesc); private: // Result CreateFromTextureDesc(const TextureDesc& textureDesc);