Skip to content

Commit

Permalink
feat: add swapchain
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Dec 28, 2024
1 parent 15d093d commit eda3ff0
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 53 deletions.
2 changes: 0 additions & 2 deletions Source/Metal/BufferMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
}
}



Result BufferMTL::Create(const BufferDesc& bufferDesc) {
m_Desc = bufferDesc;
}
Expand Down
8 changes: 5 additions & 3 deletions Source/Metal/CommandBufferMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ struct CommandBufferMTL {

BarrierBits m_barrierFlags = BarrierBits::NONE;

MTLRenderPassDescriptor* m_renderPassDescriptor = nil;

uint16_t m_numViewports = 0;
uint16_t m_numScissors = 0;
CommandBufferDirtyBits m_DirtyBits = CommandBufferDirtyBits::NONE;

MTLRenderPassDescriptor* m_renderPassDescriptor = nil;
struct CmdIndexBuffer m_indexBuffer;
uint32_t m_dirtyVertexBufferBits = 0;
struct CmdVertexBuffer m_vertexBuffers[32];
struct ShadingRateDesc m_shadingRateDesc;
uint16_t m_numViewports = 0;
uint16_t m_numScissors = 0;
MTLViewport m_viewports[16];
MTLScissorRect m_Scissors[16];
Color32f m_BlendColor;
Expand Down
11 changes: 4 additions & 7 deletions Source/Metal/CommandBufferMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@
}

void CommandBufferMTL::EndCurrentEncoders() {

if(m_RendererEncoder) {
[m_RendererEncoder endEncoding];
m_RendererEncoder = nil;
Expand Down Expand Up @@ -394,9 +393,7 @@
}

void CommandBufferMTL::DrawIndirect(const Buffer& buffer, uint64_t offset, uint32_t drawNum, uint32_t stride, const Buffer* countBuffer, uint64_t countBufferOffset) {
// TODO: implement count Buffer
NSCAssert(!countBuffer, @"count buffer not supported");

InsertBarriers();
[m_RendererEncoder
drawPrimitives: m_CurrentPipeline->GetPrimitiveType()
indirectBuffer:((BufferMTL&)buffer).GetHandle()
Expand Down Expand Up @@ -444,17 +441,17 @@

void CommandBufferMTL::BeginAnnotation(const char* name) {
if(m_RendererEncoder) {
[m_RendererEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]];
[m_RendererEncoder pushDebugGroup: [NSString stringWithUTF8String:name]];
return;
}

if(m_ComputeEncoder) {
[m_ComputeEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]];
[m_ComputeEncoder pushDebugGroup: [NSString stringWithUTF8String:name]];
return;
}

if(m_BlitEncoder) {
[m_BlitEncoder insertDebugSignpost: [NSString stringWithUTF8String:name]];
[m_BlitEncoder pushDebugGroup: [NSString stringWithUTF8String:name]];
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Metal/CommandQueueMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ struct CommandQueueMTL {
inline id<MTLCommandQueue> GetHandle() const {
return m_Handle;
}


void SetDebugName(const char* name);
void Submit(const QueueSubmitDesc& queueSubmitDesc, const SwapChain* swapChain);
Result WaitForIdle();
Result UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum);
Result WaitForIdle();

Result Create(CommandQueueType type);

Expand Down
10 changes: 10 additions & 0 deletions Source/Metal/CommandQueueMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "CommandQueueMTL.h"
#include "CommandBufferMTL.h"

#include "HelperDataUpload.h"

using namespace nri;

CommandQueueMTL::~CommandQueueMTL() {
Expand Down Expand Up @@ -39,6 +41,14 @@
[m_Handle setLabel:[NSString stringWithUTF8String:name]];
}



NRI_INLINE Result CommandQueueMTL::UploadData(const TextureUploadDesc* textureUploadDescs, uint32_t textureUploadDescNum, const BufferUploadDesc* bufferUploadDescs, uint32_t bufferUploadDescNum) {
HelperDataUpload helperDataUpload(m_Device.GetCoreInterface(), (Device&)m_Device, (CommandQueue&)*this);

return helperDataUpload.UploadData(textureUploadDescs, textureUploadDescNum, bufferUploadDescs, bufferUploadDescNum);
}

Result CommandQueueMTL::WaitForIdle() {

id<MTLCommandBuffer> waitCmdBuf = [m_Handle commandBufferWithUnretainedReferences];
Expand Down
23 changes: 23 additions & 0 deletions Source/Metal/ConversionMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,28 @@ inline MTLPixelFormat GetFormatMTL(Format format, bool demoteSrgb = false) {
return (MTLPixelFormat)NRIFormatToMTLFormat(format);
}


constexpr TextureType GetTextureType(MTLTextureType type) {
switch(type) {
case MTLTextureType1D:
case MTLTextureType1DArray:
return TextureType::TEXTURE_1D;
case MTLTextureType2D:
case MTLTextureType2DArray:
case MTLTextureType2DMultisample:
case MTLTextureType2DMultisampleArray:
case MTLTextureTypeCube:
case MTLTextureTypeCubeArray:
return TextureType::TEXTURE_2D;
case MTLTextureType3D:
return TextureType::TEXTURE_3D;
default:
break;
}

return TextureType::MAX_NUM;
}


};

11 changes: 10 additions & 1 deletion Source/Metal/DeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ struct DeviceMTL final : public DeviceBase {
return result;
}


inline const CoreInterface& GetCoreInterface() const {
return m_CoreInterface;
}

//void GetMemoryTypeInfo(MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;
void GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;
void GetMemoryDesc(const TextureDesc& textureDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const;
Expand All @@ -50,7 +55,6 @@ struct DeviceMTL final : public DeviceBase {

id<MTLRenderPipelineState> GetClearPipeline();


void Destruct() override;
Result FillFunctionTable(CoreInterface& table) const override;
Result FillFunctionTable(HelperInterface& table) const override;
Expand All @@ -61,11 +65,16 @@ struct DeviceMTL final : public DeviceBase {
Result FillFunctionTable(SwapChainInterface& table) const override;
Result FillFunctionTable(ResourceAllocatorInterface& table) const override;

Result BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum);
Result BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum);


Result Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationVKDesc, bool isWrapper);
private:
//Lock m_Lock;
id<MTLDevice> m_Device;
std::array<CommandQueueMTL*, (uint32_t)CommandQueueType::MAX_NUM> m_CommandQueues = {};
CoreInterface m_CoreInterface = {};
DeviceDesc m_Desc = {};
MTLGPUFamily m_Family;
bool m_OwnsNativeObjects = true;
Expand Down
82 changes: 66 additions & 16 deletions Source/Metal/DeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using namespace nri;


static bool FindMTLGpuFamily(id<MTLDevice> device,
const MTLGPUFamily *families, size_t len,
MTLGPUFamily* current) {
Expand Down Expand Up @@ -47,22 +46,41 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property
return value;
}




DeviceMTL::DeviceMTL(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator)
: DeviceBase(callbacks, stdAllocator) {
: DeviceBase(callbacks, stdAllocator) {


for (uint32_t i = 0; i < m_CommandQueues.size(); i++)
Destroy(GetStdAllocator(), m_CommandQueues[i]);

m_Desc.graphicsAPI = GraphicsAPI::MTL;
m_Desc.nriVersionMajor = NRI_VERSION_MAJOR;
m_Desc.nriVersionMinor = NRI_VERSION_MINOR;

// NSString* clearVert = [NSString stringWithCString: " \
// #include <metal_stdlib> \
// using namespace metal; \
// typedef struct { \
// float4 a_position [[attribute(0)]]; \
// } AttributesPos; \
// typedef struct { \
// float4 colors[9]; \
// } ClearColorsIn; \
// typedef struct { \
// float4 v_position [[position]]; \
// uint layer [[render_target_array_index]]; \
// } VaryingsPos; \
// vertex VaryingsPos vertClear(AttributesPos attributes [[stage_in]], constant ClearColorsIn& ccIn [[buffer(0)]]) { \
// VaryingsPos varyings; \
// varyings.v_position = float4(attributes.a_position.x, -attributes.a_position.y, ccIn.colors[4].r, 1.0); \
// varyings.layer = uint(attributes.a_position.w); \
// return varyings; \
// } \
// " encoding: NSASCIIStringEncoding];

}


void DeviceMTL::Destruct() {
for (uint32_t i = 0; i < m_CommandQueues.size(); i++)
Destroy(GetStdAllocator(), m_CommandQueues[i]);
Destroy(GetStdAllocator(), this);
}

Expand All @@ -89,14 +107,14 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property

void DeviceMTL::GetMemoryDesc(const BufferDesc& bufferDesc, MemoryLocation memoryLocation, MemoryDesc& memoryDesc) const {
MemoryTypeInfo memoryTypeInfo;
memoryTypeInfo.options = DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.cacheMode = DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.storageMode = DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.options = (uint32_t)DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.cacheMode = (uint32_t)DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.storageMode = (uint32_t)DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
MTLTextureDescriptor* mtlTextureDesc = [[MTLTextureDescriptor alloc] init];
MTLSizeAndAlign sizeAlign = [m_Device heapBufferSizeAndAlignWithLength: bufferDesc.size options: (MTLResourceOptions)memoryTypeInfo.options];

memoryDesc.size = sizeAlign.size;
memoryDesc.alignment = sizeAlign.align;
memoryDesc.alignment = (uint32_t)sizeAlign.align;
memoryDesc.type = memoryTypeInfo.value;

}
Expand All @@ -105,14 +123,14 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property
MTLTextureDescriptor* mtlTextureDesc = [[MTLTextureDescriptor alloc] init];

MemoryTypeInfo memoryTypeInfo;
memoryTypeInfo.options = DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.cacheMode = DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.storageMode = DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.options = (uint32_t)DEFAULT_MEMORY_RESOURCE_OPTION_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.cacheMode = (uint32_t)DEFAULT_CACHE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
memoryTypeInfo.storageMode = (uint32_t)DEFAULT_STORAGE_MODE_MEMORY_LOCATION[(size_t)memoryLocation];
nri::fillMTLTextureDescriptor(textureDesc, mtlTextureDesc);
const MTLSizeAndAlign sizeAlign = [m_Device heapTextureSizeAndAlignWithDescriptor: mtlTextureDesc];

memoryDesc.size = sizeAlign.size;
memoryDesc.alignment = sizeAlign.align;
memoryDesc.alignment = (uint32_t)sizeAlign.align;
memoryDesc.type = memoryTypeInfo.value;
}

Expand Down Expand Up @@ -141,6 +159,38 @@ static uint32_t GetEntryProperty(io_registry_entry_t entry, CFStringRef property
return result;
}

Result DeviceMTL::BindBufferMemory(const BufferMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) {
if (!memoryBindingDescNum)
return Result::SUCCESS;

for (uint32_t i = 0; i < memoryBindingDescNum; i++) {
const BufferMemoryBindingDesc& memoryBindingDesc = memoryBindingDescs[i];

BufferMTL& bufferImpl = *(BufferMTL*)memoryBindingDesc.buffer;
MemoryMTL& memoryImpl = *(MemoryMTL*)memoryBindingDesc.memory;

bufferImpl.FinishMemoryBinding(memoryImpl, memoryBindingDesc.offset);
}

return Result::SUCCESS;
}

Result DeviceMTL::BindTextureMemory(const TextureMemoryBindingDesc* memoryBindingDescs, uint32_t memoryBindingDescNum) {
if (!memoryBindingDescNum)
return Result::SUCCESS;

for (uint32_t i = 0; i < memoryBindingDescNum; i++) {
const TextureMemoryBindingDesc& memoryBindingDesc = memoryBindingDescs[i];

TextureMTL& textureImpl = *(TextureMTL*)memoryBindingDesc.texture;
MemoryMTL& memoryImpl = *(MemoryMTL*)memoryBindingDesc.memory;

textureImpl.FinishMemoryBinding(memoryImpl, memoryBindingDesc.offset);
}

return Result::SUCCESS;
}


Result DeviceMTL::Create(const DeviceCreationDesc& deviceCreationDesc, const DeviceCreationMTLDesc& deviceCreationMTLDesc, bool isWrapper) {
m_OwnsNativeObjects = !isWrapper;
Expand Down
3 changes: 3 additions & 0 deletions Source/Metal/FencMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
m_Handle = dispatch_semaphore_create(initialValue);
}

void FenceMTL::SetDebugName(const char* name) {

}

void FenceMTL::Wait(uint64_t value) {
dispatch_semaphore_wait(m_Handle, DISPATCH_TIME_FOREVER);
Expand Down
2 changes: 1 addition & 1 deletion Source/Metal/FenceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct FenceMTL {

Result Create(uint64_t initialValue);

//void SetDebugName(const char* name);
void SetDebugName(const char* name);
//uint64_t GetFenceValue() const;
void Wait(uint64_t value);
private:
Expand Down
Loading

0 comments on commit eda3ff0

Please sign in to comment.