Skip to content

Instantly share code, notes, and snippets.

@attilaz
Created June 11, 2015 14:31
Show Gist options
  • Save attilaz/fb89da15f61cfd6eb339 to your computer and use it in GitHub Desktop.
Save attilaz/fb89da15f61cfd6eb339 to your computer and use it in GitHub Desktop.
metal wrapper
/*
C++ interface
*/
namespace mtl
{
class CommandQueue
{
public:
CommandQueue(id <MTLCommandQueue> commandQueue = nil) : m_commandQueue(commandQueue) {}
operator id <MTLCommandQueue>() { return m_commandQueue; }
id<MTLCommandBuffer> commandBuffer() { return [m_commandQueue commandBuffer]; }
private:
id <MTLCommandQueue> m_commandQueue;
};
class Library
{
public:
Library(id <MTLLibrary> library = nil) : m_library(library) {}
operator id <MTLLibrary>() { return m_library; }
id <MTLFunction> newFunctionWithName(const char* functionName) { return [m_library newFunctionWithName:@(functionName)]; }
private:
id <MTLLibrary> m_library;
};
class DepthStencilState
{
public:
DepthStencilState(id <MTLDepthStencilState> depthStencilState = nil) : m_depthStencilState(depthStencilState) {}
operator id <MTLDepthStencilState>() { return m_depthStencilState; }
private:
id <MTLDepthStencilState> m_depthStencilState;
};
class RenderPipelineState
{
public:
RenderPipelineState(id <MTLRenderPipelineState> renderPipelineState = nil) : m_renderPipelineState(renderPipelineState) {}
operator id <MTLRenderPipelineState>() { return m_renderPipelineState; }
private:
id <MTLRenderPipelineState> m_renderPipelineState;
};
class Buffer
{
public:
Buffer(id <MTLBuffer> buffer = nil) : m_buffer(buffer) {}
operator id <MTLBuffer>() { return m_buffer; }
void label(const char* lbl) { m_buffer.label = @(lbl); }
private:
id <MTLBuffer> m_buffer;
};
class Function
{
public:
Function(id <MTLFunction> function = nil) : m_function(function) {}
operator id <MTLFunction>() { return m_function; }
private:
id <MTLFunction> m_function;
};
class CommandBuffer
{
public:
CommandBuffer(id <MTLCommandBuffer> commandBuffer = nil) : m_commandBuffer(commandBuffer) {}
operator id <MTLCommandBuffer>() { return m_commandBuffer; }
id<MTLRenderCommandEncoder> renderCommandEncoderWithDescriptor( MTLRenderPassDescriptor *renderPassDescriptor){
return [m_commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
}
void presentDrawable(id<MTLDrawable> drawable) { [m_commandBuffer presentDrawable:drawable]; }
void commit() { [m_commandBuffer commit]; }
private:
id <MTLCommandBuffer> m_commandBuffer;
};
class RenderCommandEncoder
{
public:
RenderCommandEncoder(id <MTLRenderCommandEncoder> renderCommandEncoder = nil) : m_renderCommandEncoder(renderCommandEncoder) {}
operator id <MTLRenderCommandEncoder>() { return m_renderCommandEncoder; }
void pushDebugGroup(const char* string) { [m_renderCommandEncoder pushDebugGroup:@(string)]; }
void setFrontFacingWinding(MTLWinding frontFacingWinding) { [m_renderCommandEncoder setFrontFacingWinding:frontFacingWinding]; }
void setDepthStencilState(id<MTLDepthStencilState> depthStencilState) { [m_renderCommandEncoder setDepthStencilState:depthStencilState];}
void setRenderPipelineState(id<MTLRenderPipelineState> pipelineState) { [m_renderCommandEncoder setRenderPipelineState:pipelineState]; }
void setVertexBuffer(id<MTLBuffer> buffer, NSUInteger offset, NSUInteger index) { [m_renderCommandEncoder setVertexBuffer:buffer offset:offset atIndex:index]; }
void setFragmentTexture(id<MTLTexture> texture, NSUInteger index) { [m_renderCommandEncoder setFragmentTexture:texture atIndex:index]; }
void drawPrimitives(MTLPrimitiveType primitiveType, NSUInteger vertexStart, NSUInteger vertexCount, NSUInteger instanceCount) {
[m_renderCommandEncoder drawPrimitives:primitiveType vertexStart:vertexStart vertexCount:vertexCount instanceCount:instanceCount];}
void endEncoding() {[m_renderCommandEncoder endEncoding] ;}
void popDebugGroup() {[m_renderCommandEncoder popDebugGroup] ;}
private:
id <MTLRenderCommandEncoder> m_renderCommandEncoder;
};
class Device
{
public:
Device(id <MTLDevice> device = nil) : m_device(device) {}
operator id <MTLDevice>() { return m_device; }
id<MTLCommandQueue> newCommandQueue() { return [m_device newCommandQueue]; }
id<MTLLibrary> newDefaultLibrary() { return [m_device newDefaultLibrary]; }
id <MTLRenderPipelineState> newRenderPipelineStateWithDescriptor(MTLRenderPipelineDescriptor *descriptor, NSError ** error) { return [m_device newRenderPipelineStateWithDescriptor:descriptor error:error]; }
id<MTLDepthStencilState> newDepthStencilStateWithDescriptor(MTLDepthStencilDescriptor * descriptor) { return [m_device newDepthStencilStateWithDescriptor:descriptor]; }
id<MTLBuffer> newBufferWithLength(unsigned int length, MTLResourceOptions options) {
return [m_device newBufferWithLength:length options:options ]; }
private:
id <MTLDevice> m_device;
};
typedef MTLRenderPipelineDescriptor* RenderPipelineDescriptor;
MTLRenderPipelineDescriptor* newRenderPipelineDescriptor() { return [MTLRenderPipelineDescriptor new]; }
typedef MTLDepthStencilDescriptor* DepthStencilDescriptor;
MTLDepthStencilDescriptor* newDepthStencilDescriptor() { return [MTLDepthStencilDescriptor new]; }
typedef MTLRenderPassDescriptor* RenderPassDescriptor;
}
/*
C++ interface
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment