boo/lib/graphicsdev/Metal.mm

1893 lines
73 KiB
Plaintext
Raw Normal View History

2015-11-16 22:03:46 +00:00
#include "../mac/CocoaCommon.hpp"
#if BOO_HAS_METAL
2016-03-08 21:18:38 +00:00
#include "logvisor/logvisor.hpp"
#include "boo/IApplication.hpp"
#include "boo/graphicsdev/Metal.hpp"
#include "boo/IGraphicsContext.hpp"
#include "Common.hpp"
#include <vector>
#include <unordered_map>
#include <unordered_set>
2018-10-07 02:49:22 +00:00
#include "xxhash/xxhash.h"
2015-12-27 04:20:07 +00:00
#if !__has_feature(objc_arc)
#error ARC Required
#endif
#define MAX_UNIFORM_COUNT 8
#define MAX_TEXTURE_COUNT 8
2018-01-21 23:07:34 +00:00
static const char* GammaVS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertData\n"
"{\n"
" float4 posIn [[ attribute(0) ]];\n"
" float4 uvIn [[ attribute(1) ]];\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float2 uv;\n"
"};\n"
"\n"
"vertex VertToFrag vmain(VertData v [[ stage_in ]])\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.uv = v.uvIn.xy;\n"
" vtf.pos = v.posIn;\n"
" return vtf;\n"
"}\n";
static const char* GammaFS =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct VertToFrag\n"
"{\n"
" float4 pos [[ position ]];\n"
" float2 uv;\n"
"};\n"
"\n"
"fragment float4 fmain(VertToFrag vtf [[ stage_in ]],\n"
2018-06-12 01:13:34 +00:00
" sampler clampSamp [[ sampler(3) ]],\n"
2018-01-21 23:07:34 +00:00
" texture2d<float> screenTex [[ texture(0) ]],\n"
" texture2d<float> gammaLUT [[ texture(1) ]])\n"
"{\n"
" uint4 tex = uint4(saturate(screenTex.sample(clampSamp, vtf.uv)) * float4(65535.0));\n"
" float4 colorOut;\n"
" for (int i=0 ; i<3 ; ++i)\n"
" colorOut[i] = gammaLUT.read(uint2(tex[i] % 256, tex[i] / 256)).r;\n"
" return colorOut;\n"
"}\n";
namespace boo
{
2016-03-08 21:18:38 +00:00
static logvisor::Module Log("boo::Metal");
2015-11-09 02:24:45 +00:00
struct MetalCommandQueue;
class MetalDataFactoryImpl;
2015-11-09 02:24:45 +00:00
class MetalDataFactoryImpl : public MetalDataFactory, public GraphicsDataFactoryHead
{
friend struct MetalCommandQueue;
friend class MetalDataFactory::Context;
IGraphicsContext* m_parent;
struct MetalContext* m_ctx;
2018-06-12 01:13:34 +00:00
bool m_hasTessellation = false;
2018-01-21 23:07:34 +00:00
float m_gamma = 1.f;
ObjToken<IShaderPipeline> m_gammaShader;
ObjToken<ITextureD> m_gammaLUT;
ObjToken<IGraphicsBufferS> m_gammaVBO;
ObjToken<IShaderDataBinding> m_gammaBinding;
void SetupGammaResources()
{
2018-06-12 01:13:34 +00:00
m_hasTessellation = [m_ctx->m_dev supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v2];
2018-01-21 23:07:34 +00:00
commitTransaction([this](IGraphicsDataFactory::Context& ctx)
{
2018-10-16 03:13:57 +00:00
auto vertexMetal = MetalDataFactory::CompileMetal(GammaVS, PipelineStage::Vertex);
auto vertexShader = ctx.newShaderStage(vertexMetal, PipelineStage::Vertex);
auto fragmentMetal = MetalDataFactory::CompileMetal(GammaFS, PipelineStage::Fragment);
auto fragmentShader = ctx.newShaderStage(fragmentMetal, PipelineStage::Fragment);
2018-01-21 23:07:34 +00:00
const VertexElementDescriptor vfmt[] = {
2018-10-16 03:13:57 +00:00
{VertexSemantic::Position4},
{VertexSemantic::UV4}
2018-01-21 23:07:34 +00:00
};
2018-10-16 03:13:57 +00:00
AdditionalPipelineInfo info =
{
BlendFactor::One, BlendFactor::Zero,
Primitive::TriStrips, ZTest::None, false, true, false, CullMode::None
};
m_gammaShader = ctx.newShaderPipeline(vertexShader, fragmentShader, vfmt, info);
2018-01-21 23:07:34 +00:00
m_gammaLUT = ctx.newDynamicTexture(256, 256, TextureFormat::I16, TextureClampMode::ClampToEdge);
setDisplayGamma(1.f);
const struct Vert {
float pos[4];
float uv[4];
} verts[4] = {
2018-01-21 23:30:01 +00:00
{{-1.f, 1.f, 0.f, 1.f}, {0.f, 0.f, 0.f, 0.f}},
{{ 1.f, 1.f, 0.f, 1.f}, {1.f, 0.f, 0.f, 0.f}},
{{-1.f, -1.f, 0.f, 1.f}, {0.f, 1.f, 0.f, 0.f}},
{{ 1.f, -1.f, 0.f, 1.f}, {1.f, 1.f, 0.f, 0.f}}
2018-01-21 23:07:34 +00:00
};
m_gammaVBO = ctx.newStaticBuffer(BufferUse::Vertex, verts, 32, 4);
ObjToken<ITexture> texs[] = {{}, m_gammaLUT.get()};
2018-10-16 03:13:57 +00:00
m_gammaBinding = ctx.newShaderDataBinding(m_gammaShader, m_gammaVBO.get(), {}, {},
2018-01-21 23:07:34 +00:00
0, nullptr, nullptr, 2, texs, nullptr, nullptr);
return true;
2018-05-26 21:29:19 +00:00
} BooTrace);
2018-01-21 23:07:34 +00:00
}
public:
2018-01-07 05:17:14 +00:00
MetalDataFactoryImpl(IGraphicsContext* parent, MetalContext* ctx)
2018-10-16 03:13:57 +00:00
: m_parent(parent), m_ctx(ctx) {}
~MetalDataFactoryImpl() = default;
Platform platform() const { return Platform::Metal; }
const char* platformName() const { return "Metal"; }
2018-05-26 21:29:19 +00:00
void commitTransaction(const std::function<bool(IGraphicsDataFactory::Context& ctx)>& __BooTraceArgs);
ObjToken<IGraphicsBufferD> newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs);
2018-06-12 01:13:34 +00:00
2018-01-21 23:07:34 +00:00
void setDisplayGamma(float gamma)
{
if (m_ctx->m_pixelFormat == MTLPixelFormatRGBA16Float)
m_gamma = gamma * 2.2f;
else
m_gamma = gamma;
if (m_gamma != 1.f)
UpdateGammaLUT(m_gammaLUT.get(), m_gamma);
}
2018-06-12 01:13:34 +00:00
bool isTessellationSupported(uint32_t& maxPatchSize)
{
maxPatchSize = 32;
return m_hasTessellation;
}
};
2017-11-12 05:14:10 +00:00
#define MTL_STATIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeManaged
#define MTL_DYNAMIC MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeManaged
class MetalGraphicsBufferS : public GraphicsDataNode<IGraphicsBufferS>
{
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
friend struct MetalCommandQueue;
MetalGraphicsBufferS(const ObjToken<BaseGraphicsData>& parent, BufferUse use, MetalContext* ctx,
const void* data, size_t stride, size_t count)
: GraphicsDataNode<IGraphicsBufferS>(parent), m_stride(stride), m_count(count), m_sz(stride * count)
{
2016-01-11 22:26:40 +00:00
m_buf = [ctx->m_dev newBufferWithBytes:data length:m_sz options:MTL_STATIC];
}
public:
size_t m_stride;
size_t m_count;
2015-11-09 02:24:45 +00:00
size_t m_sz;
2016-01-11 22:26:40 +00:00
id<MTLBuffer> m_buf;
2015-11-09 02:24:45 +00:00
~MetalGraphicsBufferS() = default;
};
template<class DataCls>
class MetalGraphicsBufferD : public GraphicsDataNode<IGraphicsBufferD, DataCls>
{
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
friend class MetalDataFactoryImpl;
2015-11-09 02:24:45 +00:00
friend struct MetalCommandQueue;
MetalCommandQueue* m_q;
2015-12-02 22:25:30 +00:00
std::unique_ptr<uint8_t[]> m_cpuBuf;
int m_validSlots = 0;
MetalGraphicsBufferD(const ObjToken<DataCls>& parent, MetalCommandQueue* q, BufferUse use,
MetalContext* ctx, size_t stride, size_t count)
: GraphicsDataNode<IGraphicsBufferD, DataCls>(parent), m_q(q), m_stride(stride),
m_count(count), m_sz(stride * count)
{
2015-12-02 22:25:30 +00:00
m_cpuBuf.reset(new uint8_t[m_sz]);
2016-01-11 22:26:40 +00:00
m_bufs[0] = [ctx->m_dev newBufferWithLength:m_sz options:MTL_DYNAMIC];
m_bufs[1] = [ctx->m_dev newBufferWithLength:m_sz options:MTL_DYNAMIC];
}
public:
size_t m_stride;
size_t m_count;
2015-11-09 02:24:45 +00:00
size_t m_sz;
2016-01-11 22:26:40 +00:00
id<MTLBuffer> m_bufs[2];
2015-11-09 02:24:45 +00:00
MetalGraphicsBufferD() = default;
2016-02-16 19:41:16 +00:00
void update(int b)
{
int slot = 1 << b;
if ((slot & m_validSlots) == 0)
{
id<MTLBuffer> res = m_bufs[b];
memcpy(res.contents, m_cpuBuf.get(), m_sz);
2017-11-12 05:14:10 +00:00
[res didModifyRange:NSMakeRange(0, m_sz)];
m_validSlots |= slot;
}
}
void load(const void* data, size_t sz)
{
size_t bufSz = std::min(sz, m_sz);
memcpy(m_cpuBuf.get(), data, bufSz);
m_validSlots = 0;
}
void* map(size_t sz)
{
if (sz > m_sz)
return nullptr;
return m_cpuBuf.get();
}
void unmap()
{
m_validSlots = 0;
}
};
class MetalTextureS : public GraphicsDataNode<ITextureS>
{
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
MetalTextureS(const ObjToken<BaseGraphicsData>& parent, MetalContext* ctx, size_t width, size_t height,
size_t mips, TextureFormat fmt, const void* data, size_t sz)
: GraphicsDataNode<ITextureS>(parent)
{
2015-11-28 04:07:53 +00:00
MTLPixelFormat pfmt = MTLPixelFormatRGBA8Unorm;
2016-02-16 19:41:16 +00:00
NSUInteger ppitchNum = 4;
NSUInteger ppitchDenom = 1;
2017-12-02 05:49:07 +00:00
NSUInteger bytesPerRow = width * ppitchNum;
2015-11-28 04:07:53 +00:00
switch (fmt)
{
case TextureFormat::I8:
pfmt = MTLPixelFormatR8Unorm;
2016-02-16 19:41:16 +00:00
ppitchNum = 1;
2017-12-02 05:49:07 +00:00
bytesPerRow = width * ppitchNum;
2015-11-28 04:07:53 +00:00
break;
2018-01-21 23:07:34 +00:00
case TextureFormat::I16:
pfmt = MTLPixelFormatR16Unorm;
ppitchNum = 2;
bytesPerRow = width * ppitchNum;
break;
2016-02-16 19:41:16 +00:00
case TextureFormat::DXT1:
pfmt = MTLPixelFormatBC1_RGBA;
ppitchNum = 1;
ppitchDenom = 2;
2017-12-02 05:49:07 +00:00
bytesPerRow = width * 8 / 4; // Metal wants this in blocks, not bytes
2015-11-28 04:07:53 +00:00
default: break;
}
2016-02-16 19:41:16 +00:00
2015-11-18 23:55:25 +00:00
@autoreleasepool
{
2016-01-11 22:26:40 +00:00
MTLTextureDescriptor* desc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pfmt
width:width height:height
mipmapped:(mips>1)?YES:NO];
desc.usage = MTLTextureUsageShaderRead;
desc.mipmapLevelCount = mips;
m_tex = [ctx->m_dev newTextureWithDescriptor:desc];
const uint8_t* dataIt = reinterpret_cast<const uint8_t*>(data);
for (size_t i=0 ; i<mips ; ++i)
{
[m_tex replaceRegion:MTLRegionMake2D(0, 0, width, height)
mipmapLevel:i
withBytes:dataIt
2017-12-02 05:49:07 +00:00
bytesPerRow:bytesPerRow];
2016-02-16 19:41:16 +00:00
dataIt += width * height * ppitchNum / ppitchDenom;
2017-01-29 03:56:17 +00:00
if (width > 1)
2017-12-02 05:49:07 +00:00
{
2017-01-29 03:56:17 +00:00
width /= 2;
2017-12-02 05:49:07 +00:00
bytesPerRow /= 2;
}
2017-01-29 03:56:17 +00:00
if (height > 1)
height /= 2;
2016-01-11 22:26:40 +00:00
}
}
}
public:
2016-01-11 22:26:40 +00:00
id<MTLTexture> m_tex;
2015-11-09 02:24:45 +00:00
~MetalTextureS() = default;
};
2016-02-16 19:41:16 +00:00
class MetalTextureSA : public GraphicsDataNode<ITextureSA>
2015-11-28 04:07:53 +00:00
{
friend class MetalDataFactory;
MetalTextureSA(const ObjToken<BaseGraphicsData>& parent, MetalContext* ctx, size_t width,
size_t height, size_t layers, size_t mips,
2015-11-28 04:07:53 +00:00
TextureFormat fmt, const void* data, size_t sz)
: GraphicsDataNode<ITextureSA>(parent)
2015-11-28 04:07:53 +00:00
{
MTLPixelFormat pfmt = MTLPixelFormatRGBA8Unorm;
NSUInteger ppitch = 4;
switch (fmt)
{
case TextureFormat::I8:
pfmt = MTLPixelFormatR8Unorm;
ppitch = 1;
break;
2018-01-21 23:07:34 +00:00
case TextureFormat::I16:
pfmt = MTLPixelFormatR16Unorm;
ppitch = 2;
break;
2015-11-28 04:07:53 +00:00
default: break;
}
2016-02-16 19:41:16 +00:00
2015-11-28 04:07:53 +00:00
@autoreleasepool
{
2016-01-11 22:26:40 +00:00
MTLTextureDescriptor* desc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pfmt
width:width height:height
2017-01-29 03:56:17 +00:00
mipmapped:(mips>1)?YES:NO];
2016-01-11 22:26:40 +00:00
desc.textureType = MTLTextureType2DArray;
desc.arrayLength = layers;
2017-01-29 03:56:17 +00:00
desc.mipmapLevelCount = mips;
2016-01-11 22:26:40 +00:00
desc.usage = MTLTextureUsageShaderRead;
m_tex = [ctx->m_dev newTextureWithDescriptor:desc];
const uint8_t* dataIt = reinterpret_cast<const uint8_t*>(data);
2017-01-29 03:56:17 +00:00
for (size_t i=0 ; i<mips ; ++i)
2016-01-11 22:26:40 +00:00
{
2017-01-29 03:56:17 +00:00
for (size_t j=0 ; j<layers ; ++j)
{
[m_tex replaceRegion:MTLRegionMake2D(0, 0, width, height)
mipmapLevel:i
slice:j
withBytes:dataIt
bytesPerRow:width * ppitch
bytesPerImage:width * height * ppitch];
dataIt += width * height * ppitch;
}
if (width > 1)
width /= 2;
if (height > 1)
height /= 2;
2016-01-11 22:26:40 +00:00
}
2015-11-28 04:07:53 +00:00
}
}
public:
2016-01-11 22:26:40 +00:00
id<MTLTexture> m_tex;
2015-11-28 04:07:53 +00:00
~MetalTextureSA() = default;
};
2016-02-16 19:41:16 +00:00
class MetalTextureD : public GraphicsDataNode<ITextureD>
{
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
friend struct MetalCommandQueue;
MetalCommandQueue* m_q;
size_t m_width = 0;
size_t m_height = 0;
2015-12-02 22:25:30 +00:00
std::unique_ptr<uint8_t[]> m_cpuBuf;
size_t m_cpuSz;
size_t m_pxPitch;
int m_validSlots = 0;
MetalTextureD(const ObjToken<BaseGraphicsData>& parent, MetalCommandQueue* q, MetalContext* ctx,
size_t width, size_t height, TextureFormat fmt)
: GraphicsDataNode<ITextureD>(parent), m_q(q), m_width(width), m_height(height)
2015-11-09 02:24:45 +00:00
{
2015-12-02 22:25:30 +00:00
MTLPixelFormat format;
switch (fmt)
{
case TextureFormat::RGBA8:
format = MTLPixelFormatRGBA8Unorm;
m_pxPitch = 4;
break;
case TextureFormat::I8:
format = MTLPixelFormatR8Unorm;
m_pxPitch = 1;
break;
2018-01-21 23:07:34 +00:00
case TextureFormat::I16:
format = MTLPixelFormatR16Unorm;
m_pxPitch = 2;
break;
2015-12-02 22:25:30 +00:00
default:
2016-03-08 21:18:38 +00:00
Log.report(logvisor::Fatal, "unsupported tex format");
2015-12-02 22:25:30 +00:00
}
2016-02-16 19:41:16 +00:00
2015-12-02 22:25:30 +00:00
m_cpuSz = width * height * m_pxPitch;
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
2016-02-16 19:41:16 +00:00
2015-11-18 23:55:25 +00:00
@autoreleasepool
{
2016-01-11 22:26:40 +00:00
MTLTextureDescriptor* desc =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format
width:width height:height
mipmapped:NO];
desc.usage = MTLTextureUsageShaderRead;
m_texs[0] = [ctx->m_dev newTextureWithDescriptor:desc];
m_texs[1] = [ctx->m_dev newTextureWithDescriptor:desc];
2015-11-18 23:55:25 +00:00
}
}
public:
2016-01-11 22:26:40 +00:00
id<MTLTexture> m_texs[2];
2015-11-09 02:24:45 +00:00
~MetalTextureD() = default;
2016-02-16 19:41:16 +00:00
void update(int b)
{
int slot = 1 << b;
if ((slot & m_validSlots) == 0)
{
id<MTLTexture> res = m_texs[b];
[res replaceRegion:MTLRegionMake2D(0, 0, m_width, m_height)
mipmapLevel:0 withBytes:m_cpuBuf.get() bytesPerRow:m_width*m_pxPitch];
m_validSlots |= slot;
}
}
void load(const void* data, size_t sz)
{
size_t bufSz = std::min(sz, m_cpuSz);
memcpy(m_cpuBuf.get(), data, bufSz);
m_validSlots = 0;
}
void* map(size_t sz)
{
if (sz > m_cpuSz)
return nullptr;
return m_cpuBuf.get();
}
void unmap()
{
m_validSlots = 0;
}
};
2016-02-16 19:41:16 +00:00
#define MAX_BIND_TEXS 4
class MetalTextureR : public GraphicsDataNode<ITextureR>
{
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
friend struct MetalCommandQueue;
size_t m_width = 0;
size_t m_height = 0;
size_t m_samples = 0;
size_t m_colorBindCount;
size_t m_depthBindCount;
2016-02-16 19:41:16 +00:00
2017-10-01 04:23:28 +00:00
void Setup(MetalContext* ctx)
{
2017-10-01 04:23:28 +00:00
if (m_colorBindCount > MAX_BIND_TEXS)
Log.report(logvisor::Fatal, "too many color bindings for render texture");
2017-10-01 04:23:28 +00:00
if (m_depthBindCount > MAX_BIND_TEXS)
Log.report(logvisor::Fatal, "too many depth bindings for render texture");
2015-11-09 06:45:14 +00:00
@autoreleasepool
{
2016-01-11 22:26:40 +00:00
MTLTextureDescriptor* desc =
2018-01-16 06:29:43 +00:00
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:ctx->m_pixelFormat
2017-10-01 04:23:28 +00:00
width:m_width height:m_height
2016-01-11 22:26:40 +00:00
mipmapped:NO];
desc.storageMode = MTLStorageModePrivate;
2015-11-09 02:24:45 +00:00
2017-10-01 04:23:28 +00:00
if (m_samples > 1)
2016-01-11 22:26:40 +00:00
{
desc.textureType = MTLTextureType2DMultisample;
2017-10-01 04:23:28 +00:00
desc.sampleCount = m_samples;
desc.usage = MTLTextureUsageRenderTarget;
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
2016-01-11 22:26:40 +00:00
desc.pixelFormat = MTLPixelFormatDepth32Float;
m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc];
}
else
{
desc.textureType = MTLTextureType2D;
desc.sampleCount = 1;
desc.usage = MTLTextureUsageRenderTarget;
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
2016-01-11 22:26:40 +00:00
desc.pixelFormat = MTLPixelFormatDepth32Float;
m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc];
2017-10-01 04:23:28 +00:00
}
2017-10-01 04:23:28 +00:00
desc.textureType = MTLTextureType2D;
desc.sampleCount = 1;
desc.usage = MTLTextureUsageShaderRead;
if (m_colorBindCount)
{
2018-01-16 06:29:43 +00:00
desc.pixelFormat = ctx->m_pixelFormat;
2017-10-01 04:23:28 +00:00
for (int i=0 ; i<m_colorBindCount ; ++i)
2018-01-07 05:17:14 +00:00
{
2017-10-01 04:23:28 +00:00
m_colorBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
if (m_samples > 1)
{
m_blitColor[i] = [MTLRenderPassDescriptor renderPassDescriptor];
m_blitColor[i].colorAttachments[0].texture = m_colorTex;
m_blitColor[i].colorAttachments[0].loadAction = MTLLoadActionLoad;
m_blitColor[i].colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve;
m_blitColor[i].colorAttachments[0].resolveTexture = m_colorBindTex[i];
}
2018-01-07 05:17:14 +00:00
}
2017-10-01 04:23:28 +00:00
}
if (m_depthBindCount)
{
desc.pixelFormat = MTLPixelFormatDepth32Float;
for (int i=0 ; i<m_depthBindCount ; ++i)
2018-01-07 05:17:14 +00:00
{
2017-10-01 04:23:28 +00:00
m_depthBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
if (m_samples > 1)
{
m_blitDepth[i] = [MTLRenderPassDescriptor renderPassDescriptor];
m_blitDepth[i].depthAttachment.texture = m_colorTex;
m_blitDepth[i].depthAttachment.loadAction = MTLLoadActionLoad;
m_blitDepth[i].depthAttachment.storeAction = MTLStoreActionMultisampleResolve;
m_blitDepth[i].depthAttachment.resolveTexture = m_depthBindTex[i];
}
2018-01-07 05:17:14 +00:00
}
}
2016-01-11 22:26:40 +00:00
2016-04-03 06:18:30 +00:00
{
m_passDesc = [MTLRenderPassDescriptor renderPassDescriptor];
m_passDesc.colorAttachments[0].texture = m_colorTex;
m_passDesc.colorAttachments[0].loadAction = MTLLoadActionLoad;
m_passDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
m_passDesc.depthAttachment.texture = m_depthTex;
m_passDesc.depthAttachment.loadAction = MTLLoadActionLoad;
m_passDesc.depthAttachment.storeAction = MTLStoreActionStore;
2016-04-03 06:22:03 +00:00
m_passDesc.depthAttachment.clearDepth = 0.f;
2016-04-03 06:18:30 +00:00
}
{
m_clearDepthPassDesc = [MTLRenderPassDescriptor renderPassDescriptor];
2016-01-11 22:26:40 +00:00
2016-04-03 06:18:30 +00:00
m_clearDepthPassDesc.colorAttachments[0].texture = m_colorTex;
m_clearDepthPassDesc.colorAttachments[0].loadAction = MTLLoadActionLoad;
m_clearDepthPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
2016-04-03 06:18:30 +00:00
m_clearDepthPassDesc.depthAttachment.texture = m_depthTex;
m_clearDepthPassDesc.depthAttachment.loadAction = MTLLoadActionClear;
m_clearDepthPassDesc.depthAttachment.storeAction = MTLStoreActionStore;
m_clearDepthPassDesc.depthAttachment.clearDepth = 0.f;
}
{
m_clearColorPassDesc = [MTLRenderPassDescriptor renderPassDescriptor];
m_clearColorPassDesc.colorAttachments[0].texture = m_colorTex;
m_clearColorPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
m_clearColorPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
m_clearDepthPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
2016-04-03 06:18:30 +00:00
m_clearColorPassDesc.depthAttachment.texture = m_depthTex;
m_clearColorPassDesc.depthAttachment.loadAction = MTLLoadActionLoad;
m_clearColorPassDesc.depthAttachment.storeAction = MTLStoreActionStore;
2016-04-03 06:22:03 +00:00
m_clearColorPassDesc.depthAttachment.clearDepth = 0.f;
2016-04-03 06:18:30 +00:00
}
{
m_clearBothPassDesc = [MTLRenderPassDescriptor renderPassDescriptor];
m_clearBothPassDesc.colorAttachments[0].texture = m_colorTex;
m_clearBothPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear;
m_clearBothPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore;
m_clearBothPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
2016-04-03 06:18:30 +00:00
m_clearBothPassDesc.depthAttachment.texture = m_depthTex;
m_clearBothPassDesc.depthAttachment.loadAction = MTLLoadActionClear;
m_clearBothPassDesc.depthAttachment.storeAction = MTLStoreActionStore;
2016-04-03 06:22:03 +00:00
m_clearBothPassDesc.depthAttachment.clearDepth = 0.f;
2016-04-03 06:18:30 +00:00
}
}
}
2016-02-16 19:41:16 +00:00
MetalTextureR(const ObjToken<BaseGraphicsData>& parent, MetalContext* ctx, size_t width, size_t height,
size_t samples, size_t colorBindCount, size_t depthBindCount)
: GraphicsDataNode<ITextureR>(parent), m_width(width), m_height(height), m_samples(samples),
m_colorBindCount(colorBindCount),
m_depthBindCount(depthBindCount)
{
if (samples == 0) m_samples = 1;
2017-10-01 04:23:28 +00:00
Setup(ctx);
}
public:
size_t samples() const {return m_samples;}
id<MTLTexture> m_colorTex;
2016-01-11 22:26:40 +00:00
id<MTLTexture> m_depthTex;
id<MTLTexture> m_colorBindTex[MAX_BIND_TEXS] = {};
id<MTLTexture> m_depthBindTex[MAX_BIND_TEXS] = {};
2016-01-11 22:26:40 +00:00
MTLRenderPassDescriptor* m_passDesc;
2016-04-03 06:18:30 +00:00
MTLRenderPassDescriptor* m_clearDepthPassDesc;
MTLRenderPassDescriptor* m_clearColorPassDesc;
MTLRenderPassDescriptor* m_clearBothPassDesc;
2018-01-07 05:17:14 +00:00
MTLRenderPassDescriptor* m_blitColor[MAX_BIND_TEXS] = {};
MTLRenderPassDescriptor* m_blitDepth[MAX_BIND_TEXS] = {};
2015-11-09 02:24:45 +00:00
~MetalTextureR() = default;
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
void resize(MetalContext* ctx, size_t width, size_t height)
{
if (width < 1)
width = 1;
if (height < 1)
height = 1;
m_width = width;
m_height = height;
2017-10-01 04:23:28 +00:00
Setup(ctx);
}
};
2016-02-16 19:41:16 +00:00
static const size_t SEMANTIC_SIZE_TABLE[] =
{
2015-11-28 04:07:53 +00:00
0,
12,
2015-11-28 04:07:53 +00:00
16,
12,
2015-11-28 04:07:53 +00:00
16,
16,
4,
8,
2015-11-28 04:07:53 +00:00
16,
16,
16
};
2015-11-09 02:24:45 +00:00
static const MTLVertexFormat SEMANTIC_TYPE_TABLE[] =
{
2015-11-28 04:07:53 +00:00
MTLVertexFormatInvalid,
2015-11-09 02:24:45 +00:00
MTLVertexFormatFloat3,
2015-11-28 04:07:53 +00:00
MTLVertexFormatFloat4,
2015-11-09 02:24:45 +00:00
MTLVertexFormatFloat3,
2015-11-28 04:07:53 +00:00
MTLVertexFormatFloat4,
MTLVertexFormatFloat4,
2015-11-09 02:24:45 +00:00
MTLVertexFormatUChar4Normalized,
MTLVertexFormatFloat2,
2015-11-28 04:07:53 +00:00
MTLVertexFormatFloat4,
MTLVertexFormatFloat4,
2015-11-09 02:24:45 +00:00
MTLVertexFormatFloat4
};
2018-10-16 03:13:57 +00:00
struct MetalVertexFormat
{
size_t m_elementCount;
2016-01-11 22:26:40 +00:00
MTLVertexDescriptor* m_vdesc;
2016-12-10 02:31:50 +00:00
size_t m_stride = 0;
size_t m_instStride = 0;
2018-10-16 03:13:57 +00:00
MetalVertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
: m_elementCount(elementCount)
{
2015-11-09 02:24:45 +00:00
for (size_t i=0 ; i<elementCount ; ++i)
{
const VertexElementDescriptor* elemin = &elements[i];
2015-11-28 04:07:53 +00:00
int semantic = int(elemin->semantic & VertexSemantic::SemanticMask);
if ((elemin->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
2016-12-10 02:31:50 +00:00
m_instStride += SEMANTIC_SIZE_TABLE[semantic];
2015-11-28 04:07:53 +00:00
else
2016-12-10 02:31:50 +00:00
m_stride += SEMANTIC_SIZE_TABLE[semantic];
2015-11-09 02:24:45 +00:00
}
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
m_vdesc = [MTLVertexDescriptor vertexDescriptor];
2016-01-11 22:26:40 +00:00
MTLVertexBufferLayoutDescriptor* layoutDesc = m_vdesc.layouts[0];
2016-12-10 02:31:50 +00:00
layoutDesc.stride = m_stride;
2015-11-09 06:45:14 +00:00
layoutDesc.stepFunction = MTLVertexStepFunctionPerVertex;
layoutDesc.stepRate = 1;
2016-02-16 19:41:16 +00:00
2016-01-11 22:26:40 +00:00
layoutDesc = m_vdesc.layouts[1];
2016-12-10 02:31:50 +00:00
layoutDesc.stride = m_instStride;
2015-11-28 04:07:53 +00:00
layoutDesc.stepFunction = MTLVertexStepFunctionPerInstance;
layoutDesc.stepRate = 1;
2016-02-16 19:41:16 +00:00
size_t offset = 0;
2015-11-28 04:07:53 +00:00
size_t instOffset = 0;
for (size_t i=0 ; i<elementCount ; ++i)
{
const VertexElementDescriptor* elemin = &elements[i];
2016-01-11 22:26:40 +00:00
MTLVertexAttributeDescriptor* attrDesc = m_vdesc.attributes[i];
2015-11-28 04:07:53 +00:00
int semantic = int(elemin->semantic & VertexSemantic::SemanticMask);
if ((elemin->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
{
attrDesc.offset = instOffset;
attrDesc.bufferIndex = 1;
instOffset += SEMANTIC_SIZE_TABLE[semantic];
}
else
{
attrDesc.offset = offset;
attrDesc.bufferIndex = 0;
offset += SEMANTIC_SIZE_TABLE[semantic];
}
attrDesc.format = SEMANTIC_TYPE_TABLE[semantic];
}
}
2018-06-12 01:13:34 +00:00
2018-10-16 03:13:57 +00:00
MTLStageInputOutputDescriptor* makeTessellationComputeLayout() const
2018-06-12 01:13:34 +00:00
{
MTLStageInputOutputDescriptor* ret = [MTLStageInputOutputDescriptor stageInputOutputDescriptor];
MTLBufferLayoutDescriptor* layoutDesc = ret.layouts[0];
layoutDesc.stride = m_stride;
layoutDesc.stepFunction = MTLStepFunctionThreadPositionInGridX;
layoutDesc.stepRate = 1;
for (size_t i=0 ; i<m_elementCount ; ++i)
{
MTLVertexAttributeDescriptor* origAttrDesc = m_vdesc.attributes[i];
MTLAttributeDescriptor* attrDesc = ret.attributes[i];
attrDesc.format = MTLAttributeFormat(origAttrDesc.format);
attrDesc.offset = origAttrDesc.offset;
attrDesc.bufferIndex = origAttrDesc.bufferIndex;
}
return ret;
}
2018-10-16 03:13:57 +00:00
MTLVertexDescriptor* makeTessellationVertexLayout() const
2018-06-12 01:13:34 +00:00
{
MTLVertexDescriptor* ret = [MTLVertexDescriptor vertexDescriptor];
MTLVertexBufferLayoutDescriptor* layoutDesc = ret.layouts[0];
layoutDesc.stride = m_stride;
layoutDesc.stepFunction = MTLVertexStepFunctionPerPatch;
layoutDesc.stepRate = 1;
for (size_t i=0 ; i<m_elementCount ; ++i)
{
MTLVertexAttributeDescriptor* origAttrDesc = m_vdesc.attributes[i];
MTLVertexAttributeDescriptor* attrDesc = ret.attributes[i];
attrDesc.format = origAttrDesc.format;
attrDesc.offset = origAttrDesc.offset;
attrDesc.bufferIndex = origAttrDesc.bufferIndex;
}
return ret;
}
};
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
static const MTLBlendFactor BLEND_FACTOR_TABLE[] =
{
MTLBlendFactorZero,
MTLBlendFactorOne,
MTLBlendFactorSourceColor,
MTLBlendFactorOneMinusSourceColor,
MTLBlendFactorDestinationColor,
MTLBlendFactorOneMinusDestinationColor,
MTLBlendFactorSourceAlpha,
MTLBlendFactorOneMinusSourceAlpha,
MTLBlendFactorDestinationAlpha,
MTLBlendFactorOneMinusDestinationAlpha,
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
MTLBlendFactorSource1Color,
MTLBlendFactorOneMinusSource1Color,
#else
MTLBlendFactorSourceColor,
MTLBlendFactorOneMinusSourceColor,
#endif
};
static const MTLPrimitiveType PRIMITIVE_TABLE[] =
{
MTLPrimitiveTypeTriangle,
2018-06-12 01:13:34 +00:00
MTLPrimitiveTypeTriangleStrip,
MTLPrimitiveTypePoint /* Actually patches */
};
#define COLOR_WRITE_MASK (MTLColorWriteMaskRed | MTLColorWriteMaskGreen | MTLColorWriteMaskBlue)
2018-10-16 03:13:57 +00:00
class MetalShaderStage : public GraphicsDataNode<IShaderStage>
{
friend class MetalDataFactory;
id<MTLFunction> m_shader;
MetalShaderStage(const boo::ObjToken<BaseGraphicsData>& parent, MetalContext* ctx,
const uint8_t* data, size_t size, PipelineStage stage)
: GraphicsDataNode<IShaderStage>(parent)
{
NSError* err = nullptr;
id<MTLLibrary> shaderLib;
if (data[0] == 1)
{
dispatch_data_t d = dispatch_data_create(data + 1, size - 1, nullptr, nullptr);
shaderLib = [ctx->m_dev newLibraryWithData:d error:&err];
}
else
{
MTLCompileOptions* compOpts = [MTLCompileOptions new];
compOpts.languageVersion = MTLLanguageVersion1_2;
shaderLib = [ctx->m_dev newLibraryWithSource:@((const char*)(data + 1))
options:compOpts
error:&err];
if (!shaderLib)
printf("%s\n", data + 1);
}
if (!shaderLib)
Log.report(logvisor::Fatal, "error creating library: %s", [[err localizedDescription] UTF8String]);
NSString* funcName;
switch (stage)
{
case PipelineStage::Vertex:
default:
funcName = @"vmain"; break;
case PipelineStage::Fragment:
funcName = @"fmain"; break;
case PipelineStage::Geometry:
funcName = @"gmain"; break;
case PipelineStage::Control:
funcName = @"cmain"; break;
case PipelineStage::Evaluation:
funcName = @"emain"; break;
}
m_shader = [shaderLib newFunctionWithName:funcName];
}
public:
id<MTLFunction> shader() const { return m_shader; }
};
class MetalShaderPipeline : public GraphicsDataNode<IShaderPipeline>
{
2018-06-12 01:13:34 +00:00
protected:
2015-11-09 02:24:45 +00:00
friend class MetalDataFactory;
friend struct MetalCommandQueue;
2016-12-10 02:31:50 +00:00
friend struct MetalShaderDataBinding;
2015-11-09 02:24:45 +00:00
MTLCullMode m_cullMode = MTLCullModeNone;
MTLPrimitiveType m_drawPrim;
2018-10-16 03:13:57 +00:00
uint32_t m_patchSize;
2016-02-16 19:41:16 +00:00
2018-10-16 03:13:57 +00:00
MetalShaderPipeline(const boo::ObjToken<BaseGraphicsData>& parent)
: GraphicsDataNode<IShaderPipeline>(parent) {}
2018-06-12 01:13:34 +00:00
2018-10-16 03:13:57 +00:00
virtual void setupExtraStages(MetalContext* ctx, MTLRenderPipelineDescriptor* desc,
ObjToken<IShaderStage> compute, const MetalVertexFormat& cVtxFmt) {}
2018-06-12 01:13:34 +00:00
virtual void draw(MetalCommandQueue& q, size_t start, size_t count);
virtual void drawIndexed(MetalCommandQueue& q, size_t start, size_t count);
virtual void drawInstances(MetalCommandQueue& q, size_t start, size_t count, size_t instCount);
virtual void drawInstancesIndexed(MetalCommandQueue& q, size_t start, size_t count, size_t instCount);
void setup(MetalContext* ctx,
2018-10-16 03:13:57 +00:00
NSUInteger targetSamples,
ObjToken<IShaderStage> vertex,
ObjToken<IShaderStage> fragment,
ObjToken<IShaderStage> compute,
const VertexFormatInfo& vtxFmt,
const AdditionalPipelineInfo& info)
{
2018-10-16 03:13:57 +00:00
m_drawPrim = PRIMITIVE_TABLE[int(info.prim)];
m_patchSize = info.patchSize;
2018-06-12 01:13:34 +00:00
2018-10-16 03:13:57 +00:00
switch (info.culling)
2017-03-10 21:07:29 +00:00
{
case CullMode::None:
default:
m_cullMode = MTLCullModeNone;
break;
case CullMode::Backface:
2015-11-09 02:24:45 +00:00
m_cullMode = MTLCullModeBack;
2017-03-10 21:07:29 +00:00
break;
case CullMode::Frontface:
m_cullMode = MTLCullModeFront;
break;
}
2016-02-16 19:41:16 +00:00
2016-01-11 22:26:40 +00:00
MTLRenderPipelineDescriptor* desc = [MTLRenderPipelineDescriptor new];
2018-10-16 03:13:57 +00:00
desc.vertexFunction = vertex.cast<MetalShaderStage>()->shader();
desc.fragmentFunction = fragment.cast<MetalShaderStage>()->shader();
MetalVertexFormat cVtxFmt(vtxFmt.elementCount, vtxFmt.elements);
2018-06-12 01:13:34 +00:00
desc.vertexDescriptor = cVtxFmt.m_vdesc;
2018-10-16 03:13:57 +00:00
setupExtraStages(ctx, desc, compute, cVtxFmt);
2016-01-11 22:26:40 +00:00
desc.sampleCount = targetSamples;
2018-01-16 06:29:43 +00:00
desc.colorAttachments[0].pixelFormat = ctx->m_pixelFormat;
2018-10-16 03:13:57 +00:00
desc.colorAttachments[0].writeMask = (info.colorWrite ? COLOR_WRITE_MASK : 0) |
(info.alphaWrite ? MTLColorWriteMaskAlpha : 0);
desc.colorAttachments[0].blendingEnabled = info.dstFac != BlendFactor::Zero;
if (info.srcFac == BlendFactor::Subtract || info.dstFac == BlendFactor::Subtract)
{
2018-01-30 07:49:00 +00:00
desc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
desc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOne;
desc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationReverseSubtract;
2018-10-16 03:13:57 +00:00
if (info.overwriteAlpha)
{
desc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
desc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorZero;
desc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
}
else
{
desc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
desc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOne;
desc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationReverseSubtract;
}
}
else
{
2018-10-16 03:13:57 +00:00
desc.colorAttachments[0].sourceRGBBlendFactor = BLEND_FACTOR_TABLE[int(info.srcFac)];
desc.colorAttachments[0].destinationRGBBlendFactor = BLEND_FACTOR_TABLE[int(info.dstFac)];
desc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
2018-10-16 03:13:57 +00:00
if (info.overwriteAlpha)
{
desc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
desc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorZero;
}
else
{
2018-10-16 03:13:57 +00:00
desc.colorAttachments[0].sourceAlphaBlendFactor = BLEND_FACTOR_TABLE[int(info.srcFac)];
desc.colorAttachments[0].destinationAlphaBlendFactor = BLEND_FACTOR_TABLE[int(info.dstFac)];
}
desc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
}
2018-10-16 03:13:57 +00:00
desc.depthAttachmentPixelFormat = info.depthAttachment ? MTLPixelFormatDepth32Float : MTLPixelFormatInvalid;
2016-01-11 22:26:40 +00:00
desc.inputPrimitiveTopology = MTLPrimitiveTopologyClassTriangle;
2015-11-09 02:24:45 +00:00
NSError* err = nullptr;
2016-01-11 22:26:40 +00:00
m_state = [ctx->m_dev newRenderPipelineStateWithDescriptor:desc error:&err];
2015-11-09 02:24:45 +00:00
if (err)
2016-03-08 21:18:38 +00:00
Log.report(logvisor::Fatal, "error making shader pipeline: %s",
2015-11-09 02:24:45 +00:00
[[err localizedDescription] UTF8String]);
2016-02-16 19:41:16 +00:00
2016-01-11 22:26:40 +00:00
MTLDepthStencilDescriptor* dsDesc = [MTLDepthStencilDescriptor new];
2018-10-16 03:13:57 +00:00
switch (info.depthTest)
{
case ZTest::None:
default:
dsDesc.depthCompareFunction = MTLCompareFunctionAlways;
break;
case ZTest::LEqual:
dsDesc.depthCompareFunction = MTLCompareFunctionGreaterEqual;
break;
case ZTest::Greater:
dsDesc.depthCompareFunction = MTLCompareFunctionLess;
break;
case ZTest::GEqual:
dsDesc.depthCompareFunction = MTLCompareFunctionLessEqual;
break;
case ZTest::Equal:
dsDesc.depthCompareFunction = MTLCompareFunctionEqual;
break;
}
2018-10-16 03:13:57 +00:00
dsDesc.depthWriteEnabled = info.depthWrite;
2016-01-11 22:26:40 +00:00
m_dsState = [ctx->m_dev newDepthStencilStateWithDescriptor:dsDesc];
}
2018-06-12 01:13:34 +00:00
public:
2016-01-11 22:26:40 +00:00
id<MTLRenderPipelineState> m_state;
id<MTLDepthStencilState> m_dsState;
2015-11-09 02:24:45 +00:00
~MetalShaderPipeline() = default;
MetalShaderPipeline& operator=(const MetalShaderPipeline&) = delete;
MetalShaderPipeline(const MetalShaderPipeline&) = delete;
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
void bind(id<MTLRenderCommandEncoder> enc)
{
2016-01-11 22:26:40 +00:00
[enc setRenderPipelineState:m_state];
[enc setDepthStencilState:m_dsState];
2015-11-09 02:24:45 +00:00
[enc setCullMode:m_cullMode];
}
2015-11-09 02:24:45 +00:00
};
2016-02-16 19:41:16 +00:00
2018-06-12 01:13:34 +00:00
class MetalTessellationShaderPipeline : public MetalShaderPipeline
{
friend class MetalDataFactory;
friend struct MetalCommandQueue;
friend struct MetalShaderDataBinding;
2018-10-16 03:13:57 +00:00
MetalTessellationShaderPipeline(const ObjToken<BaseGraphicsData>& parent)
: MetalShaderPipeline(parent) {}
2018-06-12 01:13:34 +00:00
2018-10-16 03:13:57 +00:00
void setupExtraStages(MetalContext* ctx, MTLRenderPipelineDescriptor* desc,
ObjToken<IShaderStage> compute, const MetalVertexFormat& cVtxFmt)
2018-06-12 01:13:34 +00:00
{
desc.maxTessellationFactor = 16;
desc.tessellationFactorScaleEnabled = NO;
desc.tessellationFactorFormat = MTLTessellationFactorFormatHalf;
desc.tessellationControlPointIndexType = MTLTessellationControlPointIndexTypeNone;
desc.tessellationFactorStepFunction = MTLTessellationFactorStepFunctionPerPatch;
desc.tessellationOutputWindingOrder = MTLWindingClockwise;
desc.tessellationPartitionMode = MTLTessellationPartitionModeInteger;
desc.vertexDescriptor = cVtxFmt.makeTessellationVertexLayout();
MTLComputePipelineDescriptor* compDesc = [MTLComputePipelineDescriptor new];
2018-10-16 03:13:57 +00:00
compDesc.computeFunction = compute.cast<MetalShaderStage>()->shader();
2018-06-12 01:13:34 +00:00
compDesc.stageInputDescriptor = cVtxFmt.makeTessellationComputeLayout();
NSError* err = nullptr;
m_computeState = [ctx->m_dev newComputePipelineStateWithDescriptor:compDesc options:MTLPipelineOptionNone
reflection:nil error:&err];
if (err)
Log.report(logvisor::Fatal, "error making compute pipeline: %s",
[[err localizedDescription] UTF8String]);
}
void draw(MetalCommandQueue& q, size_t start, size_t count);
void drawIndexed(MetalCommandQueue& q, size_t start, size_t count);
void drawInstances(MetalCommandQueue& q, size_t start, size_t count, size_t instCount);
void drawInstancesIndexed(MetalCommandQueue& q, size_t start, size_t count, size_t instCount);
public:
id<MTLComputePipelineState> m_computeState;
~MetalTessellationShaderPipeline() = default;
};
static id<MTLBuffer> GetBufferGPUResource(const ObjToken<IGraphicsBuffer>& buf, int idx)
{
if (buf->dynamic())
{
const MetalGraphicsBufferD<BaseGraphicsData>* cbuf = buf.cast<MetalGraphicsBufferD<BaseGraphicsData>>();
2016-01-11 22:26:40 +00:00
return cbuf->m_bufs[idx];
}
else
{
const MetalGraphicsBufferS* cbuf = buf.cast<MetalGraphicsBufferS>();
2016-01-11 22:26:40 +00:00
return cbuf->m_buf;
}
}
static id<MTLTexture> GetTextureGPUResource(const ObjToken<ITexture>& tex, int idx, int bindIdx, bool depth)
{
2015-11-28 04:07:53 +00:00
switch (tex->type())
{
case TextureType::Dynamic:
{
const MetalTextureD* ctex = tex.cast<MetalTextureD>();
2016-01-11 22:26:40 +00:00
return ctex->m_texs[idx];
}
2015-11-28 04:07:53 +00:00
case TextureType::Static:
{
const MetalTextureS* ctex = tex.cast<MetalTextureS>();
2016-01-11 22:26:40 +00:00
return ctex->m_tex;
}
2015-11-28 04:07:53 +00:00
case TextureType::StaticArray:
{
const MetalTextureSA* ctex = tex.cast<MetalTextureSA>();
2016-01-11 22:26:40 +00:00
return ctex->m_tex;
2015-11-28 04:07:53 +00:00
}
case TextureType::Render:
{
const MetalTextureR* ctex = tex.cast<MetalTextureR>();
return depth ? ctex->m_depthBindTex[bindIdx] : ctex->m_colorBindTex[bindIdx];
}
2015-11-28 04:07:53 +00:00
default: break;
}
2015-11-09 02:24:45 +00:00
return nullptr;
}
struct MetalShaderDataBinding : GraphicsDataNode<IShaderDataBinding>
{
ObjToken<IShaderPipeline> m_pipeline;
ObjToken<IGraphicsBuffer> m_vbuf;
ObjToken<IGraphicsBuffer> m_instVbo;
ObjToken<IGraphicsBuffer> m_ibuf;
std::vector<ObjToken<IGraphicsBuffer>> m_ubufs;
std::vector<size_t> m_ubufOffs;
std::vector<bool> m_fubufs;
struct BoundTex
{
ObjToken<ITexture> tex;
int idx;
bool depth;
};
std::vector<BoundTex> m_texs;
2016-12-10 02:31:50 +00:00
size_t m_baseVert;
size_t m_baseInst;
MetalShaderDataBinding(const ObjToken<BaseGraphicsData>& d,
MetalContext* ctx,
const ObjToken<IShaderPipeline>& pipeline,
const ObjToken<IGraphicsBuffer>& vbuf,
const ObjToken<IGraphicsBuffer>& instVbo,
const ObjToken<IGraphicsBuffer>& ibuf,
size_t ubufCount, const ObjToken<IGraphicsBuffer>* ubufs, const PipelineStage* ubufStages,
2016-03-30 21:07:12 +00:00
const size_t* ubufOffs, const size_t* ubufSizes,
size_t texCount, const ObjToken<ITexture>* texs,
const int* texBindIdxs, const bool* depthBind,
size_t baseVert, size_t baseInst)
: GraphicsDataNode<IShaderDataBinding>(d),
m_pipeline(pipeline),
m_vbuf(vbuf),
2015-11-28 04:07:53 +00:00
m_instVbo(instVbo),
m_ibuf(ibuf),
2016-12-10 02:31:50 +00:00
m_baseVert(baseVert),
m_baseInst(baseInst)
{
2016-04-05 02:37:46 +00:00
if (ubufCount && ubufStages)
2016-03-30 21:07:12 +00:00
{
m_fubufs.reserve(ubufCount);
2016-04-05 02:37:46 +00:00
for (size_t i=0 ; i<ubufCount ; ++i)
m_fubufs.push_back(ubufStages[i] == PipelineStage::Fragment);
2016-04-05 02:37:46 +00:00
}
if (ubufCount && ubufOffs && ubufSizes)
{
m_ubufOffs.reserve(ubufCount);
2016-03-30 21:07:12 +00:00
for (size_t i=0 ; i<ubufCount ; ++i)
{
#ifndef NDEBUG
if (ubufOffs[i] % 256)
Log.report(logvisor::Fatal, "non-256-byte-aligned uniform-offset %d provided to newShaderDataBinding", int(i));
#endif
m_ubufOffs.push_back(ubufOffs[i]);
2016-03-30 21:07:12 +00:00
}
}
m_ubufs.reserve(ubufCount);
for (size_t i=0 ; i<ubufCount ; ++i)
2016-03-30 21:07:12 +00:00
{
#ifndef NDEBUG
if (!ubufs[i])
Log.report(logvisor::Fatal, "null uniform-buffer %d provided to newShaderDataBinding", int(i));
#endif
m_ubufs.push_back(ubufs[i]);
2016-03-30 21:07:12 +00:00
}
m_texs.reserve(texCount);
for (size_t i=0 ; i<texCount ; ++i)
2016-03-30 21:07:12 +00:00
{
m_texs.push_back({texs[i], texBindIdxs ? texBindIdxs[i] : 0, depthBind ? depthBind[i] : false});
2016-03-30 21:07:12 +00:00
}
}
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
void bind(id<MTLRenderCommandEncoder> enc, int b)
{
m_pipeline.cast<MetalShaderPipeline>()->bind(enc);
2016-12-10 02:31:50 +00:00
2015-11-28 04:07:53 +00:00
if (m_vbuf)
{
2018-01-05 03:00:58 +00:00
id<MTLBuffer> buf = GetBufferGPUResource(m_vbuf, b);
[enc setVertexBuffer:buf offset:0 atIndex:0];
}
2015-11-28 04:07:53 +00:00
if (m_instVbo)
{
2018-01-05 03:00:58 +00:00
id<MTLBuffer> buf = GetBufferGPUResource(m_instVbo, b);
[enc setVertexBuffer:buf offset:0 atIndex:1];
}
if (m_ubufOffs.size())
for (size_t i=0 ; i<m_ubufs.size() ; ++i)
2016-04-05 02:37:46 +00:00
{
if (m_fubufs.size() && m_fubufs[i])
2016-04-05 02:37:46 +00:00
[enc setFragmentBuffer:GetBufferGPUResource(m_ubufs[i], b) offset:m_ubufOffs[i] atIndex:i+2];
2016-04-04 06:13:11 +00:00
else
2016-04-05 02:37:46 +00:00
[enc setVertexBuffer:GetBufferGPUResource(m_ubufs[i], b) offset:m_ubufOffs[i] atIndex:i+2];
}
2016-03-30 21:07:12 +00:00
else
for (size_t i=0 ; i<m_ubufs.size() ; ++i)
2016-04-05 02:37:46 +00:00
{
if (m_fubufs.size() && m_fubufs[i])
2016-04-05 02:37:46 +00:00
[enc setFragmentBuffer:GetBufferGPUResource(m_ubufs[i], b) offset:0 atIndex:i+2];
else
[enc setVertexBuffer:GetBufferGPUResource(m_ubufs[i], b) offset:0 atIndex:i+2];
}
for (size_t i=0 ; i<m_texs.size() ; ++i)
if (m_texs[i].tex)
2018-06-12 01:13:34 +00:00
{
[enc setFragmentTexture:GetTextureGPUResource(m_texs[i].tex, b, m_texs[i].idx,
m_texs[i].depth) atIndex:i];
[enc setVertexTexture:GetTextureGPUResource(m_texs[i].tex, b, m_texs[i].idx,
m_texs[i].depth) atIndex:i];
}
}
void bindCompute(id<MTLComputeCommandEncoder> enc, int b)
{
if (m_vbuf)
{
id<MTLBuffer> buf = GetBufferGPUResource(m_vbuf, b);
[enc setBuffer:buf offset:0 atIndex:0];
}
if (m_instVbo)
{
id<MTLBuffer> buf = GetBufferGPUResource(m_instVbo, b);
[enc setBuffer:buf offset:0 atIndex:1];
}
}
};
2015-11-09 02:24:45 +00:00
struct MetalCommandQueue : IGraphicsCommandQueue
{
Platform platform() const { return IGraphicsDataFactory::Platform::Metal; }
const char* platformName() const { return "Metal"; }
2015-11-09 02:24:45 +00:00
MetalContext* m_ctx;
IWindow* m_parentWindow;
IGraphicsContext* m_parent;
2016-01-11 22:26:40 +00:00
id<MTLCommandBuffer> m_cmdBuf;
id<MTLRenderCommandEncoder> m_enc;
2018-06-12 01:13:34 +00:00
id<MTLSamplerState> m_samplers[5];
2015-12-05 01:12:52 +00:00
bool m_running = true;
2016-02-16 19:41:16 +00:00
int m_fillBuf = 0;
int m_drawBuf = 0;
2016-02-16 19:41:16 +00:00
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-09 06:45:14 +00:00
@autoreleasepool
{
2016-01-11 22:26:40 +00:00
m_cmdBuf = [ctx->m_q commandBuffer];
2018-01-07 05:17:14 +00:00
MTLSamplerDescriptor* sampDesc = [MTLSamplerDescriptor new];
sampDesc.rAddressMode = MTLSamplerAddressModeRepeat;
sampDesc.sAddressMode = MTLSamplerAddressModeRepeat;
sampDesc.tAddressMode = MTLSamplerAddressModeRepeat;
sampDesc.minFilter = MTLSamplerMinMagFilterLinear;
sampDesc.magFilter = MTLSamplerMinMagFilterLinear;
sampDesc.mipFilter = MTLSamplerMipFilterLinear;
sampDesc.maxAnisotropy = ctx->m_anisotropy;
sampDesc.borderColor = MTLSamplerBorderColorOpaqueWhite;
m_samplers[0] = [ctx->m_dev newSamplerStateWithDescriptor:sampDesc];
sampDesc.rAddressMode = MTLSamplerAddressModeClampToBorderColor;
sampDesc.sAddressMode = MTLSamplerAddressModeClampToBorderColor;
sampDesc.tAddressMode = MTLSamplerAddressModeClampToBorderColor;
m_samplers[1] = [ctx->m_dev newSamplerStateWithDescriptor:sampDesc];
2018-06-12 01:13:34 +00:00
sampDesc.rAddressMode = MTLSamplerAddressModeClampToBorderColor;
sampDesc.sAddressMode = MTLSamplerAddressModeClampToBorderColor;
sampDesc.tAddressMode = MTLSamplerAddressModeClampToBorderColor;
sampDesc.borderColor = MTLSamplerBorderColorOpaqueBlack;
m_samplers[2] = [ctx->m_dev newSamplerStateWithDescriptor:sampDesc];
2018-01-07 05:17:14 +00:00
sampDesc.rAddressMode = MTLSamplerAddressModeClampToEdge;
sampDesc.sAddressMode = MTLSamplerAddressModeClampToEdge;
sampDesc.tAddressMode = MTLSamplerAddressModeClampToEdge;
2018-06-12 01:13:34 +00:00
m_samplers[3] = [ctx->m_dev newSamplerStateWithDescriptor:sampDesc];
2018-02-01 23:12:42 +00:00
sampDesc.rAddressMode = MTLSamplerAddressModeClampToEdge;
sampDesc.sAddressMode = MTLSamplerAddressModeClampToEdge;
sampDesc.tAddressMode = MTLSamplerAddressModeClampToEdge;
sampDesc.minFilter = MTLSamplerMinMagFilterNearest;
sampDesc.magFilter = MTLSamplerMinMagFilterNearest;
2018-06-12 01:13:34 +00:00
m_samplers[4] = [ctx->m_dev newSamplerStateWithDescriptor:sampDesc];
2015-11-09 06:45:14 +00:00
}
}
2016-02-16 19:41:16 +00:00
2018-01-21 23:07:34 +00:00
void startRenderer()
{
static_cast<MetalDataFactoryImpl*>(m_parent->getDataFactory())->SetupGammaResources();
}
2015-12-05 01:12:52 +00:00
void stopRenderer()
{
m_running = false;
if (m_inProgress && m_cmdBuf.status != MTLCommandBufferStatusNotEnqueued)
2016-01-11 22:26:40 +00:00
[m_cmdBuf waitUntilCompleted];
2015-12-05 01:12:52 +00:00
}
2016-02-16 19:41:16 +00:00
2015-12-05 01:12:52 +00:00
~MetalCommandQueue()
{
if (m_running) stopRenderer();
}
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
MetalShaderDataBinding* m_boundData = nullptr;
void setShaderDataBinding(const ObjToken<IShaderDataBinding>& binding)
{
@autoreleasepool
{
MetalShaderDataBinding* cbind = binding.cast<MetalShaderDataBinding>();
cbind->bind(m_enc, m_fillBuf);
m_boundData = cbind;
2018-06-12 01:13:34 +00:00
[m_enc setFragmentSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
[m_enc setVertexSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
}
}
2016-02-16 19:41:16 +00:00
2017-11-12 05:14:10 +00:00
ObjToken<ITextureR> m_boundTarget;
void _setRenderTarget(const ObjToken<ITextureR>& target, bool clearColor, bool clearDepth)
{
2015-11-18 23:55:25 +00:00
@autoreleasepool
{
MetalTextureR* ctarget = target.cast<MetalTextureR>();
[m_enc endEncoding];
2016-04-03 06:18:30 +00:00
if (clearColor && clearDepth)
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:ctarget->m_clearBothPassDesc];
else if (clearColor)
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:ctarget->m_clearColorPassDesc];
else if (clearDepth)
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:ctarget->m_clearDepthPassDesc];
else
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:ctarget->m_passDesc];
[m_enc setFrontFacingWinding:MTLWindingCounterClockwise];
2017-11-12 05:14:10 +00:00
if (ctarget == m_boundTarget.get())
{
if (m_boundVp.width || m_boundVp.height)
[m_enc setViewport:m_boundVp];
if (m_boundScissor.width || m_boundScissor.height)
[m_enc setScissorRect:m_boundScissor];
}
else
2017-11-12 05:14:10 +00:00
m_boundTarget = target;
2015-11-18 23:55:25 +00:00
}
}
2016-02-16 19:41:16 +00:00
void setRenderTarget(const ObjToken<ITextureR>& target)
2016-04-03 06:18:30 +00:00
{
_setRenderTarget(target, false, false);
}
2016-09-12 05:28:54 +00:00
MTLViewport m_boundVp = {};
2016-04-05 02:37:46 +00:00
void setViewport(const SWindowRect& rect, float znear, float zfar)
{
m_boundVp = MTLViewport{double(rect.location[0]), double(rect.location[1]),
2017-11-12 05:14:10 +00:00
double(rect.size[0]), double(rect.size[1]), 1.f - zfar, 1.f - znear};
[m_enc setViewport:m_boundVp];
}
2016-02-16 19:41:16 +00:00
MTLScissorRect m_boundScissor = {};
2015-11-28 04:07:53 +00:00
void setScissor(const SWindowRect& rect)
{
if (m_boundTarget)
{
2017-11-12 05:14:10 +00:00
MetalTextureR* ctarget = m_boundTarget.cast<MetalTextureR>();
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, ctarget->m_width, ctarget->m_height));
m_boundScissor = MTLScissorRect{NSUInteger(intersectRect.location[0]),
2017-11-12 05:14:10 +00:00
NSUInteger(ctarget->m_height - intersectRect.location[1] - intersectRect.size[1]),
NSUInteger(intersectRect.size[0]), NSUInteger(intersectRect.size[1])};
[m_enc setScissorRect:m_boundScissor];
}
2015-11-28 04:07:53 +00:00
}
2016-02-16 19:41:16 +00:00
2015-11-09 02:24:45 +00:00
std::unordered_map<MetalTextureR*, std::pair<size_t, size_t>> m_texResizes;
void resizeRenderTexture(const ObjToken<ITextureR>& tex, size_t width, size_t height)
{
MetalTextureR* ctex = tex.cast<MetalTextureR>();
m_texResizes[ctex] = std::make_pair(width, height);
}
2015-12-21 00:40:52 +00:00
void schedulePostFrameHandler(std::function<void(void)>&& func)
{
func();
}
2016-02-16 19:41:16 +00:00
2015-11-18 23:55:25 +00:00
void flushBufferUpdates() {}
2016-02-16 19:41:16 +00:00
float m_clearColor[4] = {0.f,0.f,0.f,0.f};
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];
}
2016-02-16 19:41:16 +00:00
void clearTarget(bool render=true, bool depth=true)
{
if (!m_boundTarget)
return;
2016-04-03 06:18:30 +00:00
_setRenderTarget(m_boundTarget, render, depth);
}
2016-02-16 19:41:16 +00:00
void draw(size_t start, size_t count)
{
2018-06-12 01:13:34 +00:00
m_boundData->m_pipeline.cast<MetalShaderPipeline>()->draw(*this, start, count);
}
2016-02-16 19:41:16 +00:00
void drawIndexed(size_t start, size_t count)
{
2018-06-12 01:13:34 +00:00
m_boundData->m_pipeline.cast<MetalShaderPipeline>()->drawIndexed(*this, start, count);
}
2016-02-16 19:41:16 +00:00
void drawInstances(size_t start, size_t count, size_t instCount)
{
2018-06-12 01:13:34 +00:00
m_boundData->m_pipeline.cast<MetalShaderPipeline>()->drawInstances(*this, start, count, instCount);
}
2016-02-16 19:41:16 +00:00
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
{
2018-06-12 01:13:34 +00:00
m_boundData->m_pipeline.cast<MetalShaderPipeline>()->drawInstancesIndexed(*this, start, count, instCount);
}
2016-02-16 19:41:16 +00:00
2018-01-21 23:07:34 +00:00
void _resolveBindTexture(MetalTextureR* tex, const SWindowRect& rect, bool tlOrigin,
int bindIdx, bool color, bool depth)
2016-02-25 05:06:13 +00:00
{
2018-01-21 23:07:34 +00:00
if (tex->samples() > 1)
{
2018-01-21 23:07:34 +00:00
if (color && tex->m_colorBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitColor[bindIdx]] endEncoding];
if (depth && tex->m_depthBindTex[bindIdx])
[[m_cmdBuf renderCommandEncoderWithDescriptor:tex->m_blitDepth[bindIdx]] endEncoding];
}
else
{
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
NSUInteger y = tlOrigin ? intersectRect.location[1] : int(tex->m_height) -
intersectRect.location[1] - intersectRect.size[1];
MTLOrigin origin = {NSUInteger(intersectRect.location[0]), y, 0};
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
2018-01-21 23:07:34 +00:00
if (color && tex->m_colorBindTex[bindIdx])
{
2018-01-21 23:07:34 +00:00
[blitEnc copyFromTexture:tex->m_colorTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_colorBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
2018-01-21 23:07:34 +00:00
if (depth && tex->m_depthBindTex[bindIdx])
{
2018-01-21 23:07:34 +00:00
[blitEnc copyFromTexture:tex->m_depthTex
sourceSlice:0
sourceLevel:0
sourceOrigin:origin
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
toTexture:tex->m_depthBindTex[bindIdx]
destinationSlice:0
destinationLevel:0
destinationOrigin:origin];
}
2018-01-21 23:07:34 +00:00
[blitEnc endEncoding];
}
}
2018-01-21 23:07:34 +00:00
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
int bindIdx, bool color, bool depth, bool clearDepth)
{
MetalTextureR* tex = texture.cast<MetalTextureR>();
@autoreleasepool
{
[m_enc endEncoding];
2018-01-21 23:07:34 +00:00
_resolveBindTexture(tex, rect, tlOrigin, bindIdx, color, depth);
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor:clearDepth ? tex->m_clearDepthPassDesc : tex->m_passDesc];
[m_enc setFrontFacingWinding:MTLWindingCounterClockwise];
if (m_boundVp.width || m_boundVp.height)
[m_enc setViewport:m_boundVp];
if (m_boundScissor.width || m_boundScissor.height)
[m_enc setScissorRect:m_boundScissor];
}
2016-02-25 05:06:13 +00:00
}
ObjToken<ITextureR> m_needsDisplay;
void resolveDisplay(const ObjToken<ITextureR>& source)
{
m_needsDisplay = source;
2015-11-09 02:24:45 +00:00
}
2016-02-16 19:41:16 +00:00
2018-06-12 01:13:34 +00:00
id<MTLBuffer> m_tessFactorBuffer = nullptr;
id<MTLBuffer> ensureTessFactorBuffer(size_t patchCount)
{
size_t targetLength = sizeof(MTLQuadTessellationFactorsHalf) * patchCount;
if (!m_tessFactorBuffer)
{
m_tessFactorBuffer = [m_ctx->m_dev newBufferWithLength:targetLength * 2 options:MTLResourceStorageModePrivate];
}
else if (m_tessFactorBuffer.length < targetLength)
{
targetLength *= 2;
id<MTLBuffer> newBuf = [m_ctx->m_dev newBufferWithLength:targetLength options:MTLResourceStorageModePrivate];
id<MTLBlitCommandEncoder> enc = [m_cmdBuf blitCommandEncoder];
[enc copyFromBuffer:m_tessFactorBuffer sourceOffset:0 toBuffer:newBuf destinationOffset:0 size:m_tessFactorBuffer.length];
[enc endEncoding];
m_tessFactorBuffer = newBuf;
}
return m_tessFactorBuffer;
}
void dispatchTessKernel(id<MTLComputePipelineState> computeState, size_t patchStart,
size_t patchCount, uint32_t patchSize)
{
struct KernelPatchInfo
{
uint32_t numPatches; // total number of patches to process.
// we need this because this value may
// not be a multiple of threadgroup size.
uint16_t numPatchesInThreadGroup; // number of patches processed by a
// thread-group
uint16_t numControlPointsPerPatch;
} patchInfo = {uint32_t(patchCount), 32, uint16_t(patchSize)};
[m_enc endEncoding];
m_enc = nullptr;
id<MTLBuffer> tessFactorBuf = ensureTessFactorBuffer(patchStart + patchCount);
id<MTLComputeCommandEncoder> computeEnc = [m_cmdBuf computeCommandEncoder];
[computeEnc setComputePipelineState:computeState];
m_boundData->bindCompute(computeEnc, m_fillBuf);
[computeEnc setStageInRegion:MTLRegionMake1D(patchStart, patchCount)];
[computeEnc setBytes:&patchInfo length:sizeof(patchInfo) atIndex:2];
[computeEnc setBuffer:tessFactorBuf
offset:patchStart * sizeof(MTLQuadTessellationFactorsHalf) atIndex:3];
[computeEnc dispatchThreads:MTLSizeMake(patchCount, 1, 1) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
[computeEnc endEncoding];
_setRenderTarget(m_boundTarget, false, false);
m_boundData->bind(m_enc, m_fillBuf);
[m_enc setFragmentSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
[m_enc setVertexSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
[m_enc setTessellationFactorBuffer:m_tessFactorBuffer offset:0 instanceStride:0];
}
2015-11-09 02:24:45 +00:00
bool m_inProgress = false;
2018-01-07 05:17:14 +00:00
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_resolvePasses;
2018-01-21 23:07:34 +00:00
std::unordered_map<uintptr_t, MTLRenderPassDescriptor*> m_gammaPasses;
void execute()
{
2015-12-05 01:12:52 +00:00
if (!m_running)
return;
2016-02-16 19:41:16 +00:00
@autoreleasepool
2015-12-02 22:25:30 +00:00
{
/* Update dynamic data here */
MetalDataFactoryImpl* gfxF = static_cast<MetalDataFactoryImpl*>(m_parent->getDataFactory());
std::unique_lock<std::recursive_mutex> datalk(gfxF->m_dataMutex);
if (gfxF->m_dataHead)
{
for (BaseGraphicsData& d : *gfxF->m_dataHead)
{
if (d.m_DBufs)
for (IGraphicsBufferD& b : *d.m_DBufs)
static_cast<MetalGraphicsBufferD<BaseGraphicsData>&>(b).update(m_fillBuf);
if (d.m_DTexs)
for (ITextureD& t : *d.m_DTexs)
static_cast<MetalTextureD&>(t).update(m_fillBuf);
}
}
if (gfxF->m_poolHead)
{
for (BaseGraphicsPool& p : *gfxF->m_poolHead)
{
if (p.m_DBufs)
for (IGraphicsBufferD& b : *p.m_DBufs)
static_cast<MetalGraphicsBufferD<BaseGraphicsData>&>(b).update(m_fillBuf);
}
}
datalk.unlock();
2016-02-16 19:41:16 +00:00
2016-01-11 22:26:40 +00:00
[m_enc endEncoding];
m_enc = nullptr;
2016-02-16 19:41:16 +00:00
2015-11-09 06:45:14 +00:00
/* Abandon if in progress (renderer too slow) */
if (m_inProgress)
{
2016-01-11 22:26:40 +00:00
m_cmdBuf = [m_ctx->m_q commandBuffer];
2015-11-09 06:45:14 +00:00
return;
}
2016-02-16 19:41:16 +00:00
2015-11-09 06:45:14 +00:00
/* 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();
2016-01-11 22:26:40 +00:00
m_cmdBuf = [m_ctx->m_q commandBuffer];
2015-11-09 06:45:14 +00:00
return;
}
2016-02-16 19:41:16 +00:00
/* Wrap up and present if needed */
if (m_needsDisplay)
{
MetalContext::Window& w = m_ctx->m_windows[m_parentWindow];
{
std::unique_lock<std::mutex> lk(w.m_resizeLock);
if (w.m_needsResize)
{
w.m_metalLayer.drawableSize = w.m_size;
w.m_needsResize = NO;
m_needsDisplay.reset();
return;
}
}
id<CAMetalDrawable> drawable = [w.m_metalLayer nextDrawable];
if (drawable)
{
MetalTextureR* src = m_needsDisplay.cast<MetalTextureR>();
id<MTLTexture> dest = drawable.texture;
if (src->m_colorTex.width == dest.width &&
src->m_colorTex.height == dest.height)
{
2018-01-21 23:07:34 +00:00
if (gfxF->m_gamma != 1.f)
{
2018-01-21 23:07:34 +00:00
SWindowRect rect(0, 0, src->m_width, src->m_height);
_resolveBindTexture(src, rect, true, 0, true, false);
uintptr_t key = uintptr_t(dest);
auto passSearch = m_gammaPasses.find(key);
if (passSearch == m_gammaPasses.end())
{
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
2018-01-21 23:07:34 +00:00
desc.colorAttachments[0].texture = dest;
desc.colorAttachments[0].loadAction = MTLLoadActionLoad;
2018-01-21 23:07:34 +00:00
desc.colorAttachments[0].storeAction = MTLStoreActionStore;
passSearch = m_gammaPasses.insert(std::make_pair(key, desc)).first;
}
2018-01-21 23:07:34 +00:00
id<MTLRenderCommandEncoder> enc = [m_cmdBuf renderCommandEncoderWithDescriptor:passSearch->second];
MetalShaderDataBinding* gammaBinding = gfxF->m_gammaBinding.cast<MetalShaderDataBinding>();
gammaBinding->m_texs[0].tex = m_needsDisplay.get();
gammaBinding->bind(enc, m_drawBuf);
2018-06-12 01:13:34 +00:00
[enc setFragmentSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
[enc setVertexSamplerStates:m_samplers withRange:NSMakeRange(0, 5)];
2018-01-21 23:07:34 +00:00
[enc drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
gammaBinding->m_texs[0].tex.reset();
[enc endEncoding];
}
else
{
2018-01-21 23:07:34 +00:00
if (src->samples() > 1)
{
uintptr_t key = uintptr_t(src->m_colorTex) ^ uintptr_t(dest);
auto passSearch = m_resolvePasses.find(key);
if (passSearch == m_resolvePasses.end())
{
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
desc.colorAttachments[0].texture = src->m_colorTex;
desc.colorAttachments[0].loadAction = MTLLoadActionLoad;
desc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve;
desc.colorAttachments[0].resolveTexture = dest;
passSearch = m_resolvePasses.insert(std::make_pair(key, desc)).first;
}
[[m_cmdBuf renderCommandEncoderWithDescriptor:passSearch->second] endEncoding];
}
else
{
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
[blitEnc copyFromTexture:src->m_colorTex
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(dest.width, dest.height, 1)
toTexture:dest
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(0, 0, 0)];
[blitEnc endEncoding];
}
}
[m_cmdBuf presentDrawable:drawable];
}
}
m_needsDisplay.reset();
}
2016-02-16 19:41:16 +00:00
2015-11-09 06:45:14 +00:00
m_drawBuf = m_fillBuf;
2015-12-02 22:25:30 +00:00
m_fillBuf ^= 1;
2016-02-16 19:41:16 +00:00
2016-01-11 22:26:40 +00:00
[m_cmdBuf addCompletedHandler:^(id<MTLCommandBuffer> buf) {m_inProgress = false;}];
2015-11-09 06:45:14 +00:00
m_inProgress = true;
2016-01-11 22:26:40 +00:00
[m_cmdBuf commit];
m_cmdBuf = [m_ctx->m_q commandBuffer];
}
}
};
2016-02-16 19:41:16 +00:00
2018-06-12 01:13:34 +00:00
void MetalShaderPipeline::draw(MetalCommandQueue& q, size_t start, size_t count)
{
[q.m_enc drawPrimitives:m_drawPrim
vertexStart:start + q.m_boundData->m_baseVert
vertexCount:count];
}
void MetalShaderPipeline::drawIndexed(MetalCommandQueue& q, size_t start, size_t count)
{
[q.m_enc drawIndexedPrimitives:m_drawPrim
indexCount:count
indexType:MTLIndexTypeUInt32
indexBuffer:GetBufferGPUResource(q.m_boundData->m_ibuf, q.m_fillBuf)
indexBufferOffset:start*4
instanceCount:1
baseVertex:q.m_boundData->m_baseVert
baseInstance:0];
}
void MetalShaderPipeline::drawInstances(MetalCommandQueue& q, size_t start, size_t count, size_t instCount)
{
[q.m_enc drawPrimitives:m_drawPrim
vertexStart:start + q.m_boundData->m_baseVert
vertexCount:count
instanceCount:instCount
baseInstance:q.m_boundData->m_baseInst];
}
void MetalShaderPipeline::drawInstancesIndexed(MetalCommandQueue& q, size_t start, size_t count, size_t instCount)
{
[q.m_enc drawIndexedPrimitives:m_drawPrim
indexCount:count
indexType:MTLIndexTypeUInt32
indexBuffer:GetBufferGPUResource(q.m_boundData->m_ibuf, q.m_fillBuf)
indexBufferOffset:start*4
instanceCount:instCount
baseVertex:q.m_boundData->m_baseVert
baseInstance:q.m_boundData->m_baseInst];
}
void MetalTessellationShaderPipeline::draw(MetalCommandQueue& q, size_t start, size_t count)
{
q.dispatchTessKernel(m_computeState, start, count, m_patchSize);
[q.m_enc drawPatches:m_patchSize
patchStart:start
patchCount:count
patchIndexBuffer:nullptr
patchIndexBufferOffset:0
instanceCount:1
baseInstance:0];
}
void MetalTessellationShaderPipeline::drawIndexed(MetalCommandQueue& q, size_t start, size_t count)
{
q.dispatchTessKernel(m_computeState, start, count, m_patchSize);
[q.m_enc drawIndexedPatches:m_patchSize
patchStart:0
patchCount:count
patchIndexBuffer:nullptr
patchIndexBufferOffset:0
controlPointIndexBuffer:GetBufferGPUResource(q.m_boundData->m_ibuf, q.m_fillBuf)
controlPointIndexBufferOffset:start*4
instanceCount:1
baseInstance:0];
}
void MetalTessellationShaderPipeline::drawInstances(MetalCommandQueue& q, size_t start, size_t count, size_t instCount)
{
q.dispatchTessKernel(m_computeState, start, count, m_patchSize);
[q.m_enc drawPatches:m_patchSize
patchStart:start
patchCount:count
patchIndexBuffer:nullptr
patchIndexBufferOffset:0
instanceCount:instCount
baseInstance:0];
}
void MetalTessellationShaderPipeline::drawInstancesIndexed(MetalCommandQueue& q, size_t start, size_t count, size_t instCount)
{
q.dispatchTessKernel(m_computeState, start, count, m_patchSize);
[q.m_enc drawIndexedPatches:m_patchSize
patchStart:0
patchCount:count
patchIndexBuffer:nullptr
patchIndexBufferOffset:0
controlPointIndexBuffer:GetBufferGPUResource(q.m_boundData->m_ibuf, q.m_fillBuf)
controlPointIndexBufferOffset:start*4
instanceCount:instCount
baseInstance:0];
}
2018-05-26 21:29:19 +00:00
MetalDataFactory::Context::Context(MetalDataFactory& parent __BooTraceArgs)
: m_parent(parent), m_data(new BaseGraphicsData(static_cast<MetalDataFactoryImpl&>(parent) __BooTraceArgsUse)) {}
2016-02-16 19:41:16 +00:00
MetalDataFactory::Context::~Context() {}
2016-02-16 19:41:16 +00:00
ObjToken<IGraphicsBufferS>
MetalDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return {new MetalGraphicsBufferS(m_data, use, factory.m_ctx, data, stride, count)};
}
}
ObjToken<IGraphicsBufferD>
MetalDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
return {new MetalGraphicsBufferD<BaseGraphicsData>(m_data, q, use, factory.m_ctx, stride, count)};
}
}
ObjToken<ITextureS>
MetalDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
TextureClampMode clampMode, const void* data, size_t sz)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return {new MetalTextureS(m_data, factory.m_ctx, width, height, mips, fmt, data, sz)};
}
}
ObjToken<ITextureSA>
MetalDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
TextureFormat fmt, TextureClampMode clampMode,
const void* data, size_t sz)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return {new MetalTextureSA(m_data, factory.m_ctx, width, height, layers, mips, fmt, data, sz)};
}
2015-11-28 04:07:53 +00:00
}
ObjToken<ITextureD>
MetalDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt,
TextureClampMode clampMode)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
return {new MetalTextureD(m_data, q, factory.m_ctx, width, height, fmt)};
}
}
ObjToken<ITextureR>
MetalDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode,
size_t colorBindCount, size_t depthBindCount)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
2018-01-07 05:17:14 +00:00
return {new MetalTextureR(m_data, factory.m_ctx, width, height, factory.m_ctx->m_sampleCount,
colorBindCount, depthBindCount)};
}
}
2018-10-16 03:13:57 +00:00
ObjToken<IShaderStage>
MetalDataFactory::Context::newShaderStage(const uint8_t* data, size_t size, PipelineStage stage)
{
2017-03-20 05:08:19 +00:00
@autoreleasepool
{
2017-03-20 05:08:19 +00:00
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
2018-10-16 03:13:57 +00:00
return {new MetalShaderStage(m_data, factory.m_ctx, data, size, stage)};
2018-06-12 01:13:34 +00:00
}
}
2016-02-16 19:41:16 +00:00
2018-10-16 03:13:57 +00:00
ObjToken<IShaderPipeline>
MetalDataFactory::Context::newShaderPipeline(ObjToken<IShaderStage> vertex, ObjToken<IShaderStage> fragment,
ObjToken<IShaderStage> geometry, ObjToken<IShaderStage> control,
ObjToken<IShaderStage> evaluation, const VertexFormatInfo& vtxFmt,
const AdditionalPipelineInfo& additionalInfo)
2018-06-12 01:13:34 +00:00
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
2018-10-16 03:13:57 +00:00
MetalShaderPipeline* ret;
if (evaluation)
{
2018-10-16 03:13:57 +00:00
ret = new MetalTessellationShaderPipeline(m_data);
ret->setup(factory.m_ctx, additionalInfo.depthAttachment ? factory.m_ctx->m_sampleCount : 1,
evaluation, fragment, control, vtxFmt, additionalInfo);
}
2018-10-16 03:13:57 +00:00
else
{
2018-10-16 03:13:57 +00:00
ret = new MetalShaderPipeline(m_data);
ret->setup(factory.m_ctx, additionalInfo.depthAttachment ? factory.m_ctx->m_sampleCount : 1,
vertex, fragment, {}, vtxFmt, additionalInfo);
}
2018-06-12 01:13:34 +00:00
return {ret};
}
}
ObjToken<IShaderDataBinding>
MetalDataFactory::Context::newShaderDataBinding(const ObjToken<IShaderPipeline>& pipeline,
const ObjToken<IGraphicsBuffer>& vbo,
const ObjToken<IGraphicsBuffer>& instVbo,
const ObjToken<IGraphicsBuffer>& ibo,
size_t ubufCount, const ObjToken<IGraphicsBuffer>* ubufs, const PipelineStage* ubufStages,
2016-03-30 21:07:12 +00:00
const size_t* ubufOffs, const size_t* ubufSizes,
size_t texCount, const ObjToken<ITexture>* texs,
const int* texBindIdxs, const bool* depthBind,
size_t baseVert, size_t baseInst)
{
@autoreleasepool
{
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return {new MetalShaderDataBinding(m_data,
factory.m_ctx, pipeline, vbo, instVbo, ibo,
ubufCount, ubufs, ubufStages, ubufOffs,
ubufSizes, texCount, texs, texBindIdxs,
depthBind, baseVert, baseInst)};
}
}
2018-05-26 21:29:19 +00:00
void MetalDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans __BooTraceArgs)
{
2018-05-26 21:29:19 +00:00
MetalDataFactory::Context ctx(*this __BooTraceArgsUse);
trans(ctx);
}
2016-12-10 02:31:50 +00:00
2018-05-26 21:29:19 +00:00
ObjToken<IGraphicsBufferD> MetalDataFactoryImpl::newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs)
2016-12-10 02:31:50 +00:00
{
2018-05-26 21:29:19 +00:00
ObjToken<BaseGraphicsPool> pool(new BaseGraphicsPool(*this __BooTraceArgsUse));
2016-12-10 02:31:50 +00:00
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(m_parent->getCommandQueue());
return {new MetalGraphicsBufferD<BaseGraphicsPool>(pool, q, use, m_ctx, stride, count)};
2016-12-11 20:20:29 +00:00
}
2018-05-26 21:29:19 +00:00
std::unique_ptr<IGraphicsCommandQueue> _NewMetalCommandQueue(MetalContext* ctx, IWindow* parentWindow,
IGraphicsContext* parent)
{
2018-05-26 21:29:19 +00:00
return std::make_unique<MetalCommandQueue>(ctx, parentWindow, parent);
}
2016-02-16 19:41:16 +00:00
2018-05-26 21:29:19 +00:00
std::unique_ptr<IGraphicsDataFactory> _NewMetalDataFactory(IGraphicsContext* parent, MetalContext* ctx)
{
2018-05-26 21:29:19 +00:00
return std::make_unique<MetalDataFactoryImpl>(parent, ctx);
}
2018-10-16 03:13:57 +00:00
std::vector<uint8_t> MetalDataFactory::CompileMetal(const char* source, PipelineStage stage)
{
size_t strSz = strlen(source) + 1;
std::vector<uint8_t> ret(strSz + 1);
memcpy(ret.data() + 1, source, strSz);
return ret;
}
}
2015-11-16 22:03:46 +00:00
#endif