2015-11-16 22:03:46 +00:00
|
|
|
#include "../mac/CocoaCommon.hpp"
|
|
|
|
#if BOO_HAS_METAL
|
2015-11-08 00:36:38 +00:00
|
|
|
#include <LogVisor/LogVisor.hpp>
|
|
|
|
#include "boo/graphicsdev/Metal.hpp"
|
|
|
|
#include "boo/IGraphicsContext.hpp"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define MAX_UNIFORM_COUNT 8
|
|
|
|
#define MAX_TEXTURE_COUNT 8
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
static LogVisor::LogModule Log("boo::Metal");
|
2015-11-09 02:24:45 +00:00
|
|
|
struct MetalCommandQueue;
|
|
|
|
|
|
|
|
struct MetalData : IGraphicsData
|
|
|
|
{
|
|
|
|
std::vector<std::unique_ptr<class MetalShaderPipeline>> m_SPs;
|
|
|
|
std::vector<std::unique_ptr<struct MetalShaderDataBinding>> m_SBinds;
|
|
|
|
std::vector<std::unique_ptr<class MetalGraphicsBufferS>> m_SBufs;
|
|
|
|
std::vector<std::unique_ptr<class MetalGraphicsBufferD>> m_DBufs;
|
|
|
|
std::vector<std::unique_ptr<class MetalTextureS>> m_STexs;
|
|
|
|
std::vector<std::unique_ptr<class MetalTextureD>> m_DTexs;
|
|
|
|
std::vector<std::unique_ptr<class MetalTextureR>> m_RTexs;
|
|
|
|
std::vector<std::unique_ptr<struct MetalVertexFormat>> m_VFmts;
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-09 06:45:14 +00:00
|
|
|
#define MTL_STATIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeShared
|
2015-11-09 02:24:45 +00:00
|
|
|
#define MTL_DYNAMIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeShared
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
class MetalGraphicsBufferS : public IGraphicsBufferS
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
friend struct MetalCommandQueue;
|
|
|
|
MetalGraphicsBufferS(BufferUse use, MetalContext* ctx, const void* data, size_t stride, size_t count)
|
|
|
|
: m_stride(stride), m_count(count), m_sz(stride * count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_buf = [ctx->m_dev.get() newBufferWithBytes:data length:m_sz options:MTL_STATIC];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
2015-11-09 02:24:45 +00:00
|
|
|
size_t m_sz;
|
|
|
|
NSPtr<id<MTLBuffer>> m_buf;
|
|
|
|
~MetalGraphicsBufferS() = default;
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
class MetalGraphicsBufferD : public IGraphicsBufferD
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
friend struct MetalCommandQueue;
|
|
|
|
MetalCommandQueue* m_q;
|
|
|
|
MetalGraphicsBufferD(MetalCommandQueue* q, BufferUse use, MetalContext* ctx, size_t stride, size_t count)
|
|
|
|
: m_q(q), m_stride(stride), m_count(count), m_sz(stride * count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_bufs[0] = [ctx->m_dev.get() newBufferWithLength:m_sz options:MTL_DYNAMIC];
|
|
|
|
m_bufs[1] = [ctx->m_dev.get() newBufferWithLength:m_sz options:MTL_DYNAMIC];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
2015-11-09 02:24:45 +00:00
|
|
|
size_t m_sz;
|
|
|
|
NSPtr<id<MTLBuffer>> m_bufs[2];
|
|
|
|
MetalGraphicsBufferD() = default;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
|
|
|
void load(const void* data, size_t sz);
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
size_t m_mappedSz;
|
2015-11-08 00:36:38 +00:00
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
class MetalTextureS : public ITextureS
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
MetalTextureS(MetalContext* ctx, size_t width, size_t height, size_t mips,
|
2015-11-08 00:36:38 +00:00
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<MTLTextureDescriptor*> desc =
|
|
|
|
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
|
|
|
|
width:width height:height
|
|
|
|
mipmapped:(mips>1)?YES:NO];
|
|
|
|
desc.get().usage = MTLTextureUsageShaderRead;
|
|
|
|
desc.get().mipmapLevelCount = mips;
|
|
|
|
m_tex = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
|
|
|
const uint8_t* dataIt = reinterpret_cast<const uint8_t*>(data);
|
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[m_tex.get() replaceRegion:MTLRegionMake2D(0, 0, width, height)
|
|
|
|
mipmapLevel:i
|
|
|
|
withBytes:dataIt
|
|
|
|
bytesPerRow:width * 4];
|
|
|
|
dataIt += width * height * 4;
|
2015-11-08 00:36:38 +00:00
|
|
|
width /= 2;
|
|
|
|
height /= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLTexture>> m_tex;
|
|
|
|
~MetalTextureS() = default;
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
class MetalTextureD : public ITextureD
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
friend struct MetalCommandQueue;
|
|
|
|
MetalCommandQueue* m_q;
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
2015-11-09 02:24:45 +00:00
|
|
|
void* m_mappedBuf;
|
|
|
|
MetalTextureD(MetalCommandQueue* q, MetalContext* ctx, size_t width, size_t height, TextureFormat fmt)
|
|
|
|
: m_q(q), m_width(width), m_height(height)
|
|
|
|
{
|
|
|
|
NSPtr<MTLTextureDescriptor*> desc =
|
|
|
|
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
|
|
|
|
width:width height:height
|
|
|
|
mipmapped:NO];
|
|
|
|
desc.get().usage = MTLTextureUsageShaderRead;
|
|
|
|
m_texs[0] = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
|
|
|
m_texs[1] = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
public:
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLTexture>> m_texs[2];
|
|
|
|
~MetalTextureD() = default;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
};
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
class MetalTextureR : public ITextureR
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
friend struct MetalCommandQueue;
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
|
|
|
size_t m_samples = 0;
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
void Setup(MetalContext* ctx, size_t width, size_t height, size_t samples)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<MTLTextureDescriptor*> desc =
|
2015-11-09 06:45:14 +00:00
|
|
|
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
|
2015-11-09 02:24:45 +00:00
|
|
|
width:width height:height
|
|
|
|
mipmapped:NO];
|
2015-11-09 06:45:14 +00:00
|
|
|
@autoreleasepool
|
|
|
|
{
|
|
|
|
m_passDesc = [[MTLRenderPassDescriptor renderPassDescriptor] retain];
|
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
desc.get().usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
|
|
|
|
desc.get().storageMode = MTLStorageModePrivate;
|
|
|
|
|
|
|
|
m_tex = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
|
|
|
|
if (samples > 1)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
desc.get().textureType = MTLTextureType2DMultisample;
|
|
|
|
desc.get().sampleCount = samples;
|
|
|
|
m_msaaTex = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
desc.get().pixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
m_depthTex = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
m_passDesc.get().colorAttachments[0].texture = m_msaaTex.get();
|
|
|
|
m_passDesc.get().colorAttachments[0].resolveTexture = m_tex.get();
|
|
|
|
m_passDesc.get().colorAttachments[0].loadAction = MTLLoadActionClear;
|
|
|
|
m_passDesc.get().colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
m_passDesc.get().depthAttachment.texture = m_depthTex.get();
|
|
|
|
m_passDesc.get().depthAttachment.loadAction = MTLLoadActionClear;
|
|
|
|
m_passDesc.get().depthAttachment.storeAction = MTLStoreActionDontCare;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
desc.get().pixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
m_depthTex = [ctx->m_dev.get() newTextureWithDescriptor:desc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
m_passDesc.get().colorAttachments[0].texture = m_tex.get();
|
|
|
|
m_passDesc.get().colorAttachments[0].loadAction = MTLLoadActionClear;
|
|
|
|
m_passDesc.get().colorAttachments[0].storeAction = MTLStoreActionStore;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
m_passDesc.get().depthAttachment.texture = m_depthTex.get();
|
|
|
|
m_passDesc.get().depthAttachment.loadAction = MTLLoadActionClear;
|
|
|
|
m_passDesc.get().depthAttachment.storeAction = MTLStoreActionDontCare;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR(MetalContext* ctx, size_t width, size_t height, size_t samples)
|
2015-11-08 00:36:38 +00:00
|
|
|
: m_width(width), m_height(height), m_samples(samples)
|
|
|
|
{
|
|
|
|
if (samples == 0) m_samples = 1;
|
|
|
|
Setup(ctx, width, height, samples);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t samples() const {return m_samples;}
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLTexture>> m_tex;
|
|
|
|
NSPtr<id<MTLTexture>> m_msaaTex;
|
|
|
|
NSPtr<id<MTLTexture>> m_depthTex;
|
|
|
|
NSPtr<MTLRenderPassDescriptor*> m_passDesc;
|
|
|
|
~MetalTextureR() = default;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
void resize(MetalContext* ctx, size_t width, size_t height)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
if (width < 1)
|
|
|
|
width = 1;
|
|
|
|
if (height < 1)
|
|
|
|
height = 1;
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
Setup(ctx, width, height, m_samples);
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
id<MTLTexture> getRenderColorRes() {if (m_samples > 1) return m_msaaTex.get(); return m_tex.get();}
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
2015-11-09 02:24:45 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
static const size_t SEMANTIC_SIZE_TABLE[] =
|
|
|
|
{
|
|
|
|
12,
|
|
|
|
12,
|
|
|
|
4,
|
|
|
|
8,
|
|
|
|
16
|
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
static const MTLVertexFormat SEMANTIC_TYPE_TABLE[] =
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MTLVertexFormatFloat3,
|
|
|
|
MTLVertexFormatFloat3,
|
|
|
|
MTLVertexFormatUChar4Normalized,
|
|
|
|
MTLVertexFormatFloat2,
|
|
|
|
MTLVertexFormatFloat4
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
struct MetalVertexFormat : IVertexFormat
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
size_t m_elementCount;
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<MTLVertexDescriptor*> m_vdesc;
|
|
|
|
MetalVertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
|
|
|
: m_elementCount(elementCount)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
size_t stride = 0;
|
|
|
|
for (size_t i=0 ; i<elementCount ; ++i)
|
|
|
|
{
|
|
|
|
const VertexElementDescriptor* elemin = &elements[i];
|
|
|
|
stride += SEMANTIC_SIZE_TABLE[elemin->semantic];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_vdesc = [MTLVertexDescriptor vertexDescriptor];
|
2015-11-09 06:45:14 +00:00
|
|
|
MTLVertexBufferLayoutDescriptor* layoutDesc = m_vdesc.get().layouts[0];
|
|
|
|
layoutDesc.stride = stride;
|
|
|
|
layoutDesc.stepFunction = MTLVertexStepFunctionPerVertex;
|
|
|
|
layoutDesc.stepRate = 1;
|
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
for (size_t i=0 ; i<elementCount ; ++i)
|
|
|
|
{
|
|
|
|
const VertexElementDescriptor* elemin = &elements[i];
|
2015-11-09 02:24:45 +00:00
|
|
|
MTLVertexAttributeDescriptor* attrDesc = m_vdesc.get().attributes[i];
|
|
|
|
attrDesc.format = SEMANTIC_TYPE_TABLE[elemin->semantic];
|
|
|
|
attrDesc.offset = offset;
|
|
|
|
attrDesc.bufferIndex = 0;
|
2015-11-08 00:36:38 +00:00
|
|
|
offset += SEMANTIC_SIZE_TABLE[elemin->semantic];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
static const MTLBlendFactor BLEND_FACTOR_TABLE[] =
|
|
|
|
{
|
|
|
|
MTLBlendFactorZero,
|
|
|
|
MTLBlendFactorOne,
|
|
|
|
MTLBlendFactorSourceColor,
|
|
|
|
MTLBlendFactorOneMinusSourceColor,
|
|
|
|
MTLBlendFactorDestinationColor,
|
|
|
|
MTLBlendFactorOneMinusDestinationColor,
|
|
|
|
MTLBlendFactorSourceAlpha,
|
|
|
|
MTLBlendFactorOneMinusSourceAlpha,
|
|
|
|
MTLBlendFactorDestinationAlpha,
|
|
|
|
MTLBlendFactorOneMinusDestinationAlpha
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
class MetalShaderPipeline : public IShaderPipeline
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
|
|
|
MTLCullMode m_cullMode = MTLCullModeNone;
|
|
|
|
|
|
|
|
MetalShaderPipeline(MetalContext* ctx, id<MTLFunction> vert, id<MTLFunction> frag,
|
|
|
|
const MetalVertexFormat* vtxFmt, MetalTextureR* target,
|
2015-11-08 00:36:38 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac,
|
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
if (backfaceCulling)
|
|
|
|
m_cullMode = MTLCullModeBack;
|
|
|
|
|
|
|
|
NSPtr<MTLRenderPipelineDescriptor*> desc = [MTLRenderPipelineDescriptor new];
|
|
|
|
desc.get().vertexFunction = vert;
|
|
|
|
desc.get().fragmentFunction = frag;
|
|
|
|
desc.get().vertexDescriptor = vtxFmt->m_vdesc.get();
|
|
|
|
desc.get().sampleCount = target->samples();
|
2015-11-09 06:45:14 +00:00
|
|
|
desc.get().colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
|
2015-11-09 02:24:45 +00:00
|
|
|
desc.get().colorAttachments[0].blendingEnabled = dstFac != BlendFactorZero;
|
|
|
|
desc.get().colorAttachments[0].sourceRGBBlendFactor = BLEND_FACTOR_TABLE[srcFac];
|
|
|
|
desc.get().colorAttachments[0].destinationRGBBlendFactor = BLEND_FACTOR_TABLE[dstFac];
|
|
|
|
desc.get().depthAttachmentPixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
desc.get().inputPrimitiveTopology = MTLPrimitiveTopologyClassTriangle;
|
|
|
|
NSError* err = nullptr;
|
|
|
|
m_state = [ctx->m_dev.get() newRenderPipelineStateWithDescriptor:desc.get() error:&err];
|
|
|
|
if (err)
|
|
|
|
Log.report(LogVisor::FatalError, "error making shader pipeline: %s",
|
|
|
|
[[err localizedDescription] UTF8String]);
|
|
|
|
|
|
|
|
NSPtr<MTLDepthStencilDescriptor*> dsDesc = [MTLDepthStencilDescriptor new];
|
|
|
|
if (depthTest)
|
|
|
|
dsDesc.get().depthCompareFunction = MTLCompareFunctionLessEqual;
|
|
|
|
dsDesc.get().depthWriteEnabled = depthWrite;
|
|
|
|
m_dsState = [ctx->m_dev.get() newDepthStencilStateWithDescriptor:dsDesc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
public:
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLRenderPipelineState>> m_state;
|
|
|
|
NSPtr<id<MTLDepthStencilState>> m_dsState;
|
|
|
|
~MetalShaderPipeline() = default;
|
|
|
|
MetalShaderPipeline& operator=(const MetalShaderPipeline&) = delete;
|
|
|
|
MetalShaderPipeline(const MetalShaderPipeline&) = delete;
|
|
|
|
|
|
|
|
void bind(id<MTLRenderCommandEncoder> enc)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[enc setRenderPipelineState:m_state.get()];
|
|
|
|
[enc setDepthStencilState:m_dsState.get()];
|
|
|
|
[enc setCullMode:m_cullMode];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static id<MTLBuffer> GetBufferGPUResource(const IGraphicsBuffer* buf, int idx)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalGraphicsBufferD* cbuf = static_cast<const MetalGraphicsBufferD*>(buf);
|
|
|
|
return cbuf->m_bufs[idx].get();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalGraphicsBufferS* cbuf = static_cast<const MetalGraphicsBufferS*>(buf);
|
|
|
|
return cbuf->m_buf.get();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
static id<MTLTexture> GetTextureGPUResource(const ITexture* tex, int idx)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
if (tex->type() == ITexture::TextureDynamic)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureD* ctex = static_cast<const MetalTextureD*>(tex);
|
|
|
|
return ctex->m_texs[idx].get();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
else if (tex->type() == ITexture::TextureStatic)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureS* ctex = static_cast<const MetalTextureS*>(tex);
|
|
|
|
return ctex->m_tex.get();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
else if (tex->type() == ITexture::TextureRender)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureR* ctex = static_cast<const MetalTextureR*>(tex);
|
|
|
|
return ctex->m_tex.get();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
struct MetalShaderDataBinding : IShaderDataBinding
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderPipeline* m_pipeline;
|
2015-11-08 00:36:38 +00:00
|
|
|
IGraphicsBuffer* m_vbuf;
|
|
|
|
IGraphicsBuffer* m_ibuf;
|
|
|
|
size_t m_ubufCount;
|
|
|
|
std::unique_ptr<IGraphicsBuffer*[]> m_ubufs;
|
|
|
|
size_t m_texCount;
|
|
|
|
std::unique_ptr<ITexture*[]> m_texs;
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderDataBinding(MetalContext* ctx,
|
2015-11-08 00:36:38 +00:00
|
|
|
IShaderPipeline* pipeline,
|
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* ibuf,
|
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs,
|
|
|
|
size_t texCount, ITexture** texs)
|
2015-11-09 02:24:45 +00:00
|
|
|
: m_pipeline(static_cast<MetalShaderPipeline*>(pipeline)),
|
2015-11-08 00:36:38 +00:00
|
|
|
m_vbuf(vbuf),
|
|
|
|
m_ibuf(ibuf),
|
|
|
|
m_ubufCount(ubufCount),
|
|
|
|
m_ubufs(new IGraphicsBuffer*[ubufCount]),
|
|
|
|
m_texCount(texCount),
|
|
|
|
m_texs(new ITexture*[texCount])
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
|
|
|
m_ubufs[i] = ubufs[i];
|
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
|
|
|
m_texs[i] = texs[i];
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
void bind(id<MTLRenderCommandEncoder> enc, int b)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_pipeline->bind(enc);
|
|
|
|
[enc setVertexBuffer:GetBufferGPUResource(m_vbuf, b) offset:0 atIndex:0];
|
|
|
|
for (size_t i=0 ; i<m_ubufCount ; ++i)
|
|
|
|
[enc setVertexBuffer:GetBufferGPUResource(m_ubufs[i], b) offset:0 atIndex:i+1];
|
|
|
|
for (size_t i=0 ; i<m_texCount ; ++i)
|
|
|
|
[enc setFragmentTexture:GetTextureGPUResource(m_texs[i], b) atIndex:i];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
struct MetalCommandQueue : IGraphicsCommandQueue
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
Platform platform() const {return IGraphicsDataFactory::PlatformMetal;}
|
|
|
|
const char* platformName() const {return "Metal";}
|
|
|
|
MetalContext* m_ctx;
|
|
|
|
IWindow* m_parentWindow;
|
2015-11-08 00:36:38 +00:00
|
|
|
IGraphicsContext* m_parent;
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLCommandBuffer>> m_cmdBuf;
|
|
|
|
NSPtr<id<MTLRenderCommandEncoder>> m_enc;
|
2015-11-08 00:36:38 +00:00
|
|
|
|
|
|
|
size_t m_fillBuf = 0;
|
|
|
|
size_t m_drawBuf = 0;
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalCommandQueue(MetalContext* ctx, IWindow* parentWindow, IGraphicsContext* parent)
|
|
|
|
: m_ctx(ctx), m_parentWindow(parentWindow), m_parent(parent)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 06:45:14 +00:00
|
|
|
@autoreleasepool
|
|
|
|
{
|
|
|
|
m_cmdBuf = [[ctx->m_q.get() commandBufferWithUnretainedReferences] retain];
|
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderDataBinding* m_boundData = nullptr;
|
2015-11-08 00:36:38 +00:00
|
|
|
void setShaderDataBinding(IShaderDataBinding* binding)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderDataBinding* cbind = static_cast<MetalShaderDataBinding*>(binding);
|
|
|
|
cbind->bind(m_enc.get(), m_fillBuf);
|
|
|
|
m_boundData = cbind;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* m_boundTarget = nullptr;
|
2015-11-08 00:36:38 +00:00
|
|
|
void setRenderTarget(ITextureR* target)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* ctarget = static_cast<MetalTextureR*>(target);
|
|
|
|
[m_enc.get() endEncoding];
|
|
|
|
m_enc = [m_cmdBuf.get() renderCommandEncoderWithDescriptor:ctarget->m_passDesc.get()];
|
2015-11-08 00:36:38 +00:00
|
|
|
m_boundTarget = ctarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setViewport(const SWindowRect& rect)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MTLViewport vp = {double(rect.location[0]), double(rect.location[1]),
|
|
|
|
double(rect.size[0]), double(rect.size[1]), 0.0, 1.0};
|
|
|
|
[m_enc.get() setViewport:vp];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
std::unordered_map<MetalTextureR*, std::pair<size_t, size_t>> m_texResizes;
|
2015-11-08 00:36:38 +00:00
|
|
|
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* ctex = static_cast<MetalTextureR*>(tex);
|
2015-11-08 00:36:38 +00:00
|
|
|
m_texResizes[ctex] = std::make_pair(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
float m_clearColor[4] = {0.0,0.0,0.0,1.0};
|
|
|
|
void setClearColor(const float rgba[4])
|
|
|
|
{
|
|
|
|
m_clearColor[0] = rgba[0];
|
|
|
|
m_clearColor[1] = rgba[1];
|
|
|
|
m_clearColor[2] = rgba[2];
|
|
|
|
m_clearColor[3] = rgba[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearTarget(bool render=true, bool depth=true)
|
|
|
|
{
|
|
|
|
if (!m_boundTarget)
|
|
|
|
return;
|
2015-11-09 02:24:45 +00:00
|
|
|
setRenderTarget(m_boundTarget);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MTLPrimitiveType m_primType = MTLPrimitiveTypeTriangle;
|
2015-11-08 00:36:38 +00:00
|
|
|
void setDrawPrimitive(Primitive prim)
|
|
|
|
{
|
|
|
|
if (prim == PrimitiveTriangles)
|
2015-11-09 02:24:45 +00:00
|
|
|
m_primType = MTLPrimitiveTypeTriangle;
|
2015-11-08 00:36:38 +00:00
|
|
|
else if (prim == PrimitiveTriStrips)
|
2015-11-09 02:24:45 +00:00
|
|
|
m_primType = MTLPrimitiveTypeTriangleStrip;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw(size_t start, size_t count)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[m_enc.get() drawPrimitives:m_primType vertexStart:start vertexCount:count];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawIndexed(size_t start, size_t count)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[m_enc.get() drawIndexedPrimitives:m_primType
|
|
|
|
indexCount:count
|
|
|
|
indexType:MTLIndexTypeUInt32
|
|
|
|
indexBuffer:GetBufferGPUResource(m_boundData->m_ibuf, m_fillBuf)
|
|
|
|
indexBufferOffset:start*4];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[m_enc.get() drawPrimitives:m_primType vertexStart:start vertexCount:count instanceCount:instCount];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
[m_enc.get() drawIndexedPrimitives:m_primType
|
|
|
|
indexCount:count
|
|
|
|
indexType:MTLIndexTypeUInt32
|
|
|
|
indexBuffer:GetBufferGPUResource(m_boundData->m_ibuf, m_fillBuf)
|
|
|
|
indexBufferOffset:start*4
|
|
|
|
instanceCount:instCount];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resolveDisplay(ITextureR* source)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalContext::Window& w = m_ctx->m_windows[m_parentWindow];
|
|
|
|
|
|
|
|
MetalTextureR* csource = static_cast<MetalTextureR*>(source);
|
|
|
|
[m_enc.get() endEncoding];
|
2015-11-09 06:45:14 +00:00
|
|
|
m_enc.reset();
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<CAMetalDrawable>> drawable = [w.m_metalLayer nextDrawable];
|
2015-11-09 06:45:14 +00:00
|
|
|
NSPtr<id<MTLTexture>> dest = drawable.get().texture;
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<id<MTLBlitCommandEncoder>> blitEnc = [m_cmdBuf.get() blitCommandEncoder];
|
|
|
|
[blitEnc.get() copyFromTexture:csource->m_tex.get()
|
|
|
|
sourceSlice:0
|
|
|
|
sourceLevel:0
|
|
|
|
sourceOrigin:MTLOriginMake(0, 0, 0)
|
2015-11-09 06:45:14 +00:00
|
|
|
sourceSize:MTLSizeMake(dest.get().width, dest.get().height, 1)
|
|
|
|
toTexture:dest.get()
|
2015-11-09 02:24:45 +00:00
|
|
|
destinationSlice:0
|
|
|
|
destinationLevel:0
|
|
|
|
destinationOrigin:MTLOriginMake(0, 0, 0)];
|
|
|
|
[blitEnc.get() endEncoding];
|
|
|
|
[m_cmdBuf.get() presentDrawable:drawable.get()];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool m_inProgress = false;
|
2015-11-08 00:36:38 +00:00
|
|
|
void execute()
|
|
|
|
{
|
2015-11-09 06:45:14 +00:00
|
|
|
@autoreleasepool
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 06:45:14 +00:00
|
|
|
/* Abandon if in progress (renderer too slow) */
|
|
|
|
if (m_inProgress)
|
|
|
|
{
|
|
|
|
m_cmdBuf = [[m_ctx->m_q.get() commandBufferWithUnretainedReferences] retain];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform texture resizes */
|
|
|
|
if (m_texResizes.size())
|
|
|
|
{
|
|
|
|
for (const auto& resize : m_texResizes)
|
|
|
|
resize.first->resize(m_ctx, resize.second.first, resize.second.second);
|
|
|
|
m_texResizes.clear();
|
|
|
|
m_cmdBuf = [[m_ctx->m_q.get() commandBufferWithUnretainedReferences] retain];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_drawBuf = m_fillBuf;
|
|
|
|
++m_fillBuf;
|
|
|
|
if (m_fillBuf == 2)
|
|
|
|
m_fillBuf = 0;
|
|
|
|
|
|
|
|
[m_cmdBuf.get() addCompletedHandler:^(id<MTLCommandBuffer> buf) {m_inProgress = false;}];
|
|
|
|
m_inProgress = true;
|
|
|
|
[m_cmdBuf.get() commit];
|
|
|
|
m_cmdBuf = [[m_ctx->m_q.get() commandBufferWithUnretainedReferences] retain];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
void MetalGraphicsBufferD::load(const void* data, size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
id<MTLBuffer> res = m_bufs[m_q->m_fillBuf].get();
|
|
|
|
memcpy(res.contents, data, sz);
|
|
|
|
[res didModifyRange:NSMakeRange(0, sz)];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
void* MetalGraphicsBufferD::map(size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_mappedSz = sz;
|
|
|
|
id<MTLBuffer> res = m_bufs[m_q->m_fillBuf].get();
|
|
|
|
return res.contents;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
void MetalGraphicsBufferD::unmap()
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
id<MTLBuffer> res = m_bufs[m_q->m_fillBuf].get();
|
|
|
|
[res didModifyRange:NSMakeRange(0, m_mappedSz)];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
void MetalTextureD::load(const void* data, size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
id<MTLTexture> res = m_texs[m_q->m_fillBuf].get();
|
|
|
|
[res replaceRegion:MTLRegionMake2D(0, 0, m_width, m_height)
|
|
|
|
mipmapLevel:0 withBytes:data bytesPerRow:m_width*4];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
void* MetalTextureD::map(size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_mappedBuf = malloc(sz);
|
|
|
|
return m_mappedBuf;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
void MetalTextureD::unmap()
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
id<MTLTexture> res = m_texs[m_q->m_fillBuf].get();
|
|
|
|
[res replaceRegion:MTLRegionMake2D(0, 0, m_width, m_height)
|
|
|
|
mipmapLevel:0 withBytes:m_mappedBuf bytesPerRow:m_width*4];
|
|
|
|
free(m_mappedBuf);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalDataFactory::MetalDataFactory(IGraphicsContext* parent, MetalContext* ctx)
|
2015-11-09 06:45:14 +00:00
|
|
|
: m_parent(parent), m_deferredData(new struct MetalData()), m_ctx(ctx) {}
|
2015-11-08 00:36:38 +00:00
|
|
|
|
|
|
|
IGraphicsBufferS* MetalDataFactory::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalGraphicsBufferS* retval = new MetalGraphicsBufferS(use, m_ctx, data, stride, count);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_SBufs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
IGraphicsBufferS* MetalDataFactory::newStaticBuffer(BufferUse use, std::unique_ptr<uint8_t[]>&& data, size_t stride, size_t count)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
std::unique_ptr<uint8_t[]> d = std::move(data);
|
|
|
|
MetalGraphicsBufferS* retval = new MetalGraphicsBufferS(use, m_ctx, d.get(), stride, count);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_SBufs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
IGraphicsBufferD* MetalDataFactory::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
MetalGraphicsBufferD* retval = new MetalGraphicsBufferD(q, use, m_ctx, stride, count);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_DBufs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ITextureS* MetalDataFactory::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureS* retval = new MetalTextureS(m_ctx, width, height, mips, fmt, data, sz);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_STexs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
ITextureS* MetalDataFactory::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
std::unique_ptr<uint8_t[]>&& data, size_t sz)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
std::unique_ptr<uint8_t[]> d = std::move(data);
|
|
|
|
MetalTextureS* retval = new MetalTextureS(m_ctx, width, height, mips, fmt, d.get(), sz);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_STexs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
ITextureD* MetalDataFactory::newDynamicTexture(size_t width, size_t height, TextureFormat fmt)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
MetalTextureD* retval = new MetalTextureD(q, m_ctx, width, height, fmt);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_DTexs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
ITextureR* MetalDataFactory::newRenderTexture(size_t width, size_t height, size_t samples)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* retval = new MetalTextureR(m_ctx, width, height, samples);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_RTexs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IVertexFormat* MetalDataFactory::newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalVertexFormat* retval = new struct MetalVertexFormat(elementCount, elements);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_VFmts.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IShaderPipeline* MetalDataFactory::newShaderPipeline(const char* vertSource, const char* fragSource,
|
2015-11-09 02:24:45 +00:00
|
|
|
IVertexFormat* vtxFmt, ITextureR* target,
|
2015-11-08 00:36:38 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac,
|
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
NSPtr<MTLCompileOptions*> compOpts = [MTLCompileOptions new];
|
|
|
|
compOpts.get().languageVersion = MTLLanguageVersion1_1;
|
|
|
|
NSError* err = nullptr;
|
|
|
|
|
|
|
|
NSPtr<id<MTLLibrary>> vertShaderLib = [m_ctx->m_dev.get() newLibraryWithSource:@(vertSource)
|
|
|
|
options:compOpts.get()
|
|
|
|
error:&err];
|
|
|
|
if (err)
|
|
|
|
Log.report(LogVisor::FatalError, "error compiling vert shader: %s", [[err localizedDescription] UTF8String]);
|
2015-11-09 06:45:14 +00:00
|
|
|
NSPtr<id<MTLFunction>> vertFunc = [vertShaderLib.get() newFunctionWithName:@"vmain"];
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
NSPtr<id<MTLLibrary>> fragShaderLib = [m_ctx->m_dev.get() newLibraryWithSource:@(fragSource)
|
|
|
|
options:compOpts.get()
|
|
|
|
error:&err];
|
|
|
|
if (err)
|
|
|
|
Log.report(LogVisor::FatalError, "error compiling frag shader: %s", [[err localizedDescription] UTF8String]);
|
2015-11-09 06:45:14 +00:00
|
|
|
NSPtr<id<MTLFunction>> fragFunc = [fragShaderLib.get() newFunctionWithName:@"fmain"];
|
2015-11-09 02:24:45 +00:00
|
|
|
|
|
|
|
MetalShaderPipeline* retval = new MetalShaderPipeline(m_ctx, vertFunc.get(), fragFunc.get(),
|
|
|
|
static_cast<const MetalVertexFormat*>(vtxFmt),
|
|
|
|
static_cast<MetalTextureR*>(target),
|
|
|
|
srcFac, dstFac, depthTest, depthWrite, backfaceCulling);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_SPs.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IShaderDataBinding*
|
|
|
|
MetalDataFactory::newShaderDataBinding(IShaderPipeline* pipeline,
|
|
|
|
IVertexFormat* vtxFormat,
|
2015-11-09 02:24:45 +00:00
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* ibuf,
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs,
|
|
|
|
size_t texCount, ITexture** texs)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderDataBinding* retval =
|
|
|
|
new MetalShaderDataBinding(m_ctx, pipeline, vbuf, ibuf, ubufCount, ubufs, texCount, texs);
|
|
|
|
static_cast<MetalData*>(m_deferredData)->m_SBinds.emplace_back(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetalDataFactory::reset()
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
delete static_cast<MetalData*>(m_deferredData);
|
|
|
|
m_deferredData = new struct MetalData();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
IGraphicsData* MetalDataFactory::commit()
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalData* retval = static_cast<MetalData*>(m_deferredData);
|
|
|
|
m_deferredData = new struct MetalData();
|
|
|
|
m_committedData.insert(retval);
|
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
void MetalDataFactory::destroyData(IGraphicsData* d)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalData* data = static_cast<MetalData*>(d);
|
|
|
|
m_committedData.erase(data);
|
|
|
|
delete data;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
void MetalDataFactory::destroyAllData()
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
for (IGraphicsData* data : m_committedData)
|
|
|
|
delete static_cast<MetalData*>(data);
|
|
|
|
m_committedData.clear();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
IGraphicsCommandQueue* _NewMetalCommandQueue(MetalContext* ctx, IWindow* parentWindow,
|
2015-11-08 00:36:38 +00:00
|
|
|
IGraphicsContext* parent)
|
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
return new struct MetalCommandQueue(ctx, parentWindow, parent);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-16 22:03:46 +00:00
|
|
|
|
|
|
|
#endif
|