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"
|
2015-11-08 00:36:38 +00:00
|
|
|
#include "boo/graphicsdev/Metal.hpp"
|
|
|
|
#include "boo/IGraphicsContext.hpp"
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "Common.hpp"
|
2015-11-08 00:36:38 +00:00
|
|
|
#include <vector>
|
2017-03-05 07:54:58 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include "xxhash.h"
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2015-12-27 04:20:07 +00:00
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
#error ARC Required
|
|
|
|
#endif
|
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
#define MAX_UNIFORM_COUNT 8
|
|
|
|
#define MAX_TEXTURE_COUNT 8
|
|
|
|
|
|
|
|
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;
|
2017-03-05 07:54:58 +00:00
|
|
|
class MetalDataFactoryImpl;
|
2015-11-09 02:24:45 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
struct MetalShareableShader : IShareableShader<MetalDataFactoryImpl, MetalShareableShader>
|
|
|
|
{
|
|
|
|
id<MTLFunction> m_shader;
|
2017-03-05 23:34:24 +00:00
|
|
|
MetalShareableShader(MetalDataFactoryImpl& fac, uint64_t srcKey, id<MTLFunction> s)
|
|
|
|
: IShareableShader(fac, srcKey, 0), m_shader(s) {}
|
2017-03-05 07:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MetalDataFactoryImpl : public MetalDataFactory
|
|
|
|
{
|
|
|
|
friend struct MetalCommandQueue;
|
|
|
|
friend class MetalDataFactory::Context;
|
|
|
|
IGraphicsContext* m_parent;
|
|
|
|
static ThreadLocalPtr<struct MetalData> m_deferredData;
|
|
|
|
std::unordered_set<struct MetalData*> m_committedData;
|
|
|
|
std::unordered_set<struct MetalPool*> m_committedPools;
|
|
|
|
std::mutex m_committedMutex;
|
|
|
|
std::unordered_map<uint64_t, std::unique_ptr<MetalShareableShader>> m_sharedShaders;
|
|
|
|
struct MetalContext* m_ctx;
|
|
|
|
uint32_t m_sampleCount;
|
|
|
|
|
|
|
|
void destroyData(IGraphicsData*);
|
|
|
|
void destroyAllData();
|
|
|
|
void destroyPool(IGraphicsBufferPool*);
|
|
|
|
IGraphicsBufferD* newPoolBuffer(IGraphicsBufferPool* pool, BufferUse use,
|
|
|
|
size_t stride, size_t count);
|
|
|
|
void deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf);
|
|
|
|
public:
|
|
|
|
MetalDataFactoryImpl(IGraphicsContext* parent, MetalContext* ctx, uint32_t sampleCount);
|
|
|
|
~MetalDataFactoryImpl() {}
|
|
|
|
|
|
|
|
Platform platform() const {return Platform::Metal;}
|
|
|
|
const char* platformName() const {return "Metal";}
|
|
|
|
|
|
|
|
GraphicsDataToken commitTransaction(const std::function<bool(IGraphicsDataFactory::Context& ctx)>&);
|
|
|
|
GraphicsBufferPoolToken newBufferPool();
|
|
|
|
|
2017-03-05 23:34:24 +00:00
|
|
|
void _unregisterShareableShader(uint64_t srcKey, uint64_t binKey) { m_sharedShaders.erase(srcKey); }
|
2017-03-05 07:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ThreadLocalPtr<struct MetalData> MetalDataFactoryImpl::m_deferredData;
|
2017-03-14 07:02:53 +00:00
|
|
|
struct MetalData : IGraphicsDataPriv
|
2015-11-09 02:24:45 +00:00
|
|
|
{
|
|
|
|
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;
|
2015-11-28 04:07:53 +00:00
|
|
|
std::vector<std::unique_ptr<class MetalTextureSA>> m_SATexs;
|
2015-11-09 02:24:45 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
struct MetalPoolItem : IGraphicsDataPriv
|
|
|
|
{
|
|
|
|
std::unique_ptr<class MetalGraphicsBufferD> m_buf;
|
|
|
|
};
|
|
|
|
|
2016-12-10 02:31:50 +00:00
|
|
|
struct MetalPool : IGraphicsBufferPool
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
std::unordered_set<MetalPoolItem*> m_items;
|
|
|
|
~MetalPool()
|
|
|
|
{
|
|
|
|
for (auto& item : m_items)
|
|
|
|
item->decrement();
|
|
|
|
}
|
2016-12-10 02:31:50 +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;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalGraphicsBufferS(IGraphicsData* parent, BufferUse use, MetalContext* ctx,
|
|
|
|
const void* data, size_t stride, size_t count)
|
|
|
|
: boo::IGraphicsBufferS(parent), m_stride(stride), m_count(count), m_sz(stride * count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2016-01-11 22:26:40 +00:00
|
|
|
m_buf = [ctx->m_dev 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;
|
2016-01-11 22:26:40 +00:00
|
|
|
id<MTLBuffer> m_buf;
|
2015-11-09 02:24:45 +00:00
|
|
|
~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;
|
2017-03-05 07:54:58 +00:00
|
|
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalGraphicsBufferD(IGraphicsData* parent, MetalCommandQueue* q, BufferUse use,
|
|
|
|
MetalContext* ctx, size_t stride, size_t count)
|
|
|
|
: boo::IGraphicsBufferD(parent), m_q(q), m_stride(stride), m_count(count), m_sz(stride * count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
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];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-12-02 22:25:30 +00:00
|
|
|
void update(int b);
|
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;
|
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
|
|
|
|
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 MetalTextureS : public ITextureS
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
friend class MetalDataFactory;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureS(IGraphicsData* parent, 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)
|
2017-03-14 07:02:53 +00:00
|
|
|
: ITextureS(parent)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
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;
|
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;
|
2015-11-28 04:07:53 +00:00
|
|
|
break;
|
2016-02-16 19:41:16 +00:00
|
|
|
case TextureFormat::DXT1:
|
|
|
|
pfmt = MTLPixelFormatBC1_RGBA;
|
|
|
|
ppitchNum = 1;
|
|
|
|
ppitchDenom = 2;
|
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
|
2016-02-16 19:41:16 +00:00
|
|
|
bytesPerRow:width * ppitchNum / ppitchDenom];
|
|
|
|
dataIt += width * height * ppitchNum / ppitchDenom;
|
2017-01-29 03:56:17 +00:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2016-01-11 22:26:40 +00:00
|
|
|
}
|
2015-11-08 00:36:38 +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;
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-28 04:07:53 +00:00
|
|
|
class MetalTextureSA : public ITextureSA
|
|
|
|
{
|
|
|
|
friend class MetalDataFactory;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureSA(IGraphicsData* 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)
|
2017-03-14 07:02:53 +00:00
|
|
|
: 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;
|
|
|
|
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
|
|
|
|
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-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;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureD(IGraphicsData* parent, MetalCommandQueue* q, MetalContext* ctx,
|
|
|
|
size_t width, size_t height, TextureFormat fmt)
|
|
|
|
: boo::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;
|
|
|
|
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
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-12-02 22:25:30 +00:00
|
|
|
void update(int b);
|
2015-11-08 00:36:38 +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
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
};
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
#define MAX_BIND_TEXS 4
|
|
|
|
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t m_colorBindCount;
|
|
|
|
size_t m_depthBindCount;
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2016-02-26 08:01:46 +00:00
|
|
|
void Setup(MetalContext* ctx, size_t width, size_t height, size_t samples,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t colorBindCount, size_t depthBindCount)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2016-09-03 05:17:47 +00:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2016-09-12 05:28:54 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
if (colorBindCount > MAX_BIND_TEXS)
|
|
|
|
Log.report(logvisor::Fatal, "too many color bindings for render texture");
|
|
|
|
if (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 =
|
|
|
|
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
|
|
|
|
width:width height:height
|
|
|
|
mipmapped:NO];
|
|
|
|
desc.storageMode = MTLStorageModePrivate;
|
2015-11-09 02:24:45 +00:00
|
|
|
|
2016-01-11 22:26:40 +00:00
|
|
|
if (samples > 1)
|
|
|
|
{
|
|
|
|
desc.textureType = MTLTextureType2DMultisample;
|
|
|
|
desc.sampleCount = samples;
|
2016-02-27 01:38:13 +00:00
|
|
|
desc.usage = MTLTextureUsageRenderTarget;
|
2016-02-26 08:01:46 +00:00
|
|
|
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
|
2016-01-11 22:26:40 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
if (colorBindCount)
|
2016-02-26 08:01:46 +00:00
|
|
|
{
|
|
|
|
desc.usage = MTLTextureUsageShaderRead;
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<colorBindCount ; ++i)
|
|
|
|
m_colorBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
|
2016-02-26 08:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
desc.usage = MTLTextureUsageRenderTarget;
|
2016-01-11 22:26:40 +00:00
|
|
|
desc.pixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc];
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
if (depthBindCount)
|
|
|
|
{
|
|
|
|
desc.usage = MTLTextureUsageShaderRead;
|
|
|
|
for (int i=0 ; i<depthBindCount ; ++i)
|
|
|
|
m_depthBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
|
|
|
|
}
|
2016-01-11 22:26:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-26 08:01:46 +00:00
|
|
|
desc.textureType = MTLTextureType2D;
|
|
|
|
desc.sampleCount = 1;
|
2016-02-27 01:38:13 +00:00
|
|
|
desc.usage = MTLTextureUsageRenderTarget;
|
2016-02-26 08:01:46 +00:00
|
|
|
m_colorTex = [ctx->m_dev newTextureWithDescriptor:desc];
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
if (colorBindCount)
|
2016-02-26 08:01:46 +00:00
|
|
|
{
|
|
|
|
desc.usage = MTLTextureUsageShaderRead;
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<colorBindCount ; ++i)
|
|
|
|
m_colorBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
|
2016-02-26 08:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
desc.usage = MTLTextureUsageRenderTarget;
|
2016-01-11 22:26:40 +00:00
|
|
|
desc.pixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
m_depthTex = [ctx->m_dev newTextureWithDescriptor:desc];
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
if (depthBindCount)
|
|
|
|
{
|
|
|
|
desc.usage = MTLTextureUsageShaderRead;
|
|
|
|
for (int i=0 ; i<depthBindCount ; ++i)
|
|
|
|
m_depthBindTex[i] = [ctx->m_dev newTextureWithDescriptor:desc];
|
|
|
|
}
|
2016-02-26 08:01:46 +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-02-26 08:01:46 +00:00
|
|
|
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
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
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureR(IGraphicsData* parent, MetalContext* ctx, size_t width, size_t height, size_t samples,
|
|
|
|
size_t colorBindCount, size_t depthBindCount)
|
|
|
|
: boo::ITextureR(parent), m_width(width), m_height(height), m_samples(samples),
|
|
|
|
m_colorBindCount(colorBindCount),
|
|
|
|
m_depthBindCount(depthBindCount)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
if (samples == 0) m_samples = 1;
|
2017-03-14 07:02:53 +00:00
|
|
|
Setup(ctx, width, height, samples, colorBindCount, depthBindCount);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t samples() const {return m_samples;}
|
2016-02-26 08:01:46 +00:00
|
|
|
id<MTLTexture> m_colorTex;
|
2016-01-11 22:26:40 +00:00
|
|
|
id<MTLTexture> m_depthTex;
|
2017-03-14 07:02:53 +00:00
|
|
|
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;
|
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)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
|
|
|
if (width < 1)
|
|
|
|
width = 1;
|
|
|
|
if (height < 1)
|
|
|
|
height = 1;
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2017-03-14 07:02:53 +00:00
|
|
|
Setup(ctx, width, height, m_samples, m_colorBindCount, m_depthBindCount);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
};
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
static const size_t SEMANTIC_SIZE_TABLE[] =
|
|
|
|
{
|
2015-11-28 04:07:53 +00:00
|
|
|
0,
|
2015-11-08 00:36:38 +00:00
|
|
|
12,
|
2015-11-28 04:07:53 +00:00
|
|
|
16,
|
2015-11-08 00:36:38 +00:00
|
|
|
12,
|
2015-11-28 04:07:53 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-08 00:36:38 +00:00
|
|
|
4,
|
|
|
|
8,
|
2015-11-28 04:07:53 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-08 00:36:38 +00:00
|
|
|
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-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
|
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;
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalVertexFormat(IGraphicsData* parent, size_t elementCount, const VertexElementDescriptor* elements)
|
|
|
|
: boo::IVertexFormat(parent), m_elementCount(elementCount)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
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
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t offset = 0;
|
2015-11-28 04:07:53 +00:00
|
|
|
size_t instOffset = 0;
|
2015-11-08 00:36:38 +00:00
|
|
|
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];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
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,
|
2017-01-18 20:56:26 +00:00
|
|
|
MTLBlendFactorOneMinusDestinationAlpha,
|
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
|
|
|
MTLBlendFactorSource1Color,
|
|
|
|
MTLBlendFactorOneMinusSource1Color,
|
|
|
|
#else
|
|
|
|
MTLBlendFactorSourceColor,
|
|
|
|
MTLBlendFactorOneMinusSourceColor,
|
|
|
|
#endif
|
2015-11-08 00:36:38 +00:00
|
|
|
};
|
|
|
|
|
2016-03-24 08:05:19 +00:00
|
|
|
static const MTLPrimitiveType PRIMITIVE_TABLE[] =
|
|
|
|
{
|
|
|
|
MTLPrimitiveTypeTriangle,
|
|
|
|
MTLPrimitiveTypeTriangleStrip
|
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
#define COLOR_WRITE_MASK (MTLColorWriteMaskRed | MTLColorWriteMaskGreen | MTLColorWriteMaskBlue)
|
|
|
|
|
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;
|
2017-03-14 07:02:53 +00:00
|
|
|
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;
|
2016-03-24 08:05:19 +00:00
|
|
|
MTLPrimitiveType m_drawPrim;
|
2016-12-10 02:31:50 +00:00
|
|
|
const MetalVertexFormat* m_vtxFmt;
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalShareableShader::Token m_vert;
|
|
|
|
MetalShareableShader::Token m_frag;
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalShaderPipeline(IGraphicsData* parent,
|
|
|
|
MetalContext* ctx,
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalShareableShader::Token&& vert,
|
|
|
|
MetalShareableShader::Token&& frag,
|
2015-11-28 04:07:53 +00:00
|
|
|
const MetalVertexFormat* vtxFmt, NSUInteger targetSamples,
|
2016-03-24 08:05:19 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
|
2017-03-14 07:02:53 +00:00
|
|
|
ZTest depthTest, bool depthWrite, bool colorWrite,
|
|
|
|
bool alphaWrite, CullMode culling)
|
|
|
|
: boo::IShaderPipeline(parent),
|
|
|
|
m_drawPrim(PRIMITIVE_TABLE[int(prim)]), m_vtxFmt(vtxFmt),
|
2017-03-05 07:54:58 +00:00
|
|
|
m_vert(std::move(vert)), m_frag(std::move(frag))
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-10 21:07:29 +00:00
|
|
|
switch (culling)
|
|
|
|
{
|
|
|
|
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];
|
2017-03-05 07:54:58 +00:00
|
|
|
desc.vertexFunction = m_vert.get().m_shader;
|
|
|
|
desc.fragmentFunction = m_frag.get().m_shader;
|
2016-01-11 22:26:40 +00:00
|
|
|
desc.vertexDescriptor = vtxFmt->m_vdesc;
|
|
|
|
desc.sampleCount = targetSamples;
|
|
|
|
desc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
|
2017-03-14 07:02:53 +00:00
|
|
|
desc.colorAttachments[0].writeMask = (colorWrite ? COLOR_WRITE_MASK : 0) |
|
|
|
|
(alphaWrite ? MTLColorWriteMaskAlpha : 0);
|
2016-01-11 22:26:40 +00:00
|
|
|
desc.colorAttachments[0].blendingEnabled = dstFac != BlendFactor::Zero;
|
|
|
|
desc.colorAttachments[0].sourceRGBBlendFactor = BLEND_FACTOR_TABLE[int(srcFac)];
|
|
|
|
desc.colorAttachments[0].destinationRGBBlendFactor = BLEND_FACTOR_TABLE[int(dstFac)];
|
2017-03-14 07:02:53 +00:00
|
|
|
desc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
|
|
|
|
desc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorZero;
|
2016-01-11 22:26:40 +00:00
|
|
|
desc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float;
|
|
|
|
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];
|
2017-03-14 07:02:53 +00:00
|
|
|
switch (depthTest)
|
|
|
|
{
|
|
|
|
case ZTest::None:
|
|
|
|
default:
|
|
|
|
dsDesc.depthCompareFunction = MTLCompareFunctionAlways;
|
|
|
|
break;
|
|
|
|
case ZTest::LEqual:
|
2016-07-31 22:39:05 +00:00
|
|
|
dsDesc.depthCompareFunction = MTLCompareFunctionGreaterEqual;
|
2017-03-14 07:02:53 +00:00
|
|
|
break;
|
|
|
|
case ZTest::Greater:
|
|
|
|
dsDesc.depthCompareFunction = MTLCompareFunctionLess;
|
|
|
|
break;
|
|
|
|
case ZTest::Equal:
|
|
|
|
dsDesc.depthCompareFunction = MTLCompareFunctionEqual;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:26:40 +00:00
|
|
|
dsDesc.depthWriteEnabled = depthWrite;
|
|
|
|
m_dsState = [ctx->m_dev newDepthStencilStateWithDescriptor:dsDesc];
|
2015-11-08 00:36:38 +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)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
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-08 00:36:38 +00:00
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
};
|
2016-02-16 19:41:16 +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);
|
2016-01-11 22:26:40 +00:00
|
|
|
return cbuf->m_bufs[idx];
|
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);
|
2016-01-11 22:26:40 +00:00
|
|
|
return cbuf->m_buf;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
static id<MTLBuffer> GetBufferGPUResource(const IGraphicsBuffer* buf, int idx, size_t& strideOut)
|
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
{
|
|
|
|
const MetalGraphicsBufferD* cbuf = static_cast<const MetalGraphicsBufferD*>(buf);
|
|
|
|
strideOut = cbuf->m_stride;
|
|
|
|
return cbuf->m_bufs[idx];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const MetalGraphicsBufferS* cbuf = static_cast<const MetalGraphicsBufferS*>(buf);
|
|
|
|
strideOut = cbuf->m_stride;
|
|
|
|
return cbuf->m_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static id<MTLTexture> GetTextureGPUResource(const ITexture* tex, int idx, int bindIdx, bool depth)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-28 04:07:53 +00:00
|
|
|
switch (tex->type())
|
|
|
|
{
|
|
|
|
case TextureType::Dynamic:
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureD* ctex = static_cast<const MetalTextureD*>(tex);
|
2016-01-11 22:26:40 +00:00
|
|
|
return ctex->m_texs[idx];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-28 04:07:53 +00:00
|
|
|
case TextureType::Static:
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureS* ctex = static_cast<const MetalTextureS*>(tex);
|
2016-01-11 22:26:40 +00:00
|
|
|
return ctex->m_tex;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-28 04:07:53 +00:00
|
|
|
case TextureType::StaticArray:
|
|
|
|
{
|
|
|
|
const MetalTextureSA* ctex = static_cast<const MetalTextureSA*>(tex);
|
2016-01-11 22:26:40 +00:00
|
|
|
return ctex->m_tex;
|
2015-11-28 04:07:53 +00:00
|
|
|
}
|
|
|
|
case TextureType::Render:
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
const MetalTextureR* ctex = static_cast<const MetalTextureR*>(tex);
|
2017-03-14 07:02:53 +00:00
|
|
|
return depth ? ctex->m_depthBindTex[bindIdx] : ctex->m_colorBindTex[bindIdx];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-28 04:07:53 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
struct MetalShaderDataBinding : IShaderDataBindingPriv
|
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;
|
2015-11-28 04:07:53 +00:00
|
|
|
IGraphicsBuffer* m_instVbo;
|
2015-11-08 00:36:38 +00:00
|
|
|
IGraphicsBuffer* m_ibuf;
|
|
|
|
size_t m_ubufCount;
|
|
|
|
std::unique_ptr<IGraphicsBuffer*[]> m_ubufs;
|
2016-04-05 02:37:46 +00:00
|
|
|
std::unique_ptr<size_t[]> m_ubufOffs;
|
|
|
|
std::unique_ptr<bool[]> m_fubufs;
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t m_texCount;
|
2017-03-14 07:02:53 +00:00
|
|
|
struct BoundTex
|
|
|
|
{
|
|
|
|
ITexture* tex;
|
|
|
|
int idx;
|
|
|
|
bool depth;
|
|
|
|
};
|
|
|
|
std::unique_ptr<BoundTex[]> m_texs;
|
2016-12-10 02:31:50 +00:00
|
|
|
size_t m_baseVert;
|
|
|
|
size_t m_baseInst;
|
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
MetalShaderDataBinding(MetalData* d,
|
|
|
|
MetalContext* ctx,
|
2015-11-08 00:36:38 +00:00
|
|
|
IShaderPipeline* pipeline,
|
2015-11-28 04:07:53 +00:00
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbo, IGraphicsBuffer* ibuf,
|
2016-04-04 06:13:11 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs, const PipelineStage* ubufStages,
|
2016-03-30 21:07:12 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t texCount, ITexture** texs,
|
|
|
|
const int* texBindIdxs, const bool* depthBind,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2017-01-20 03:52:40 +00:00
|
|
|
: IShaderDataBindingPriv(d),
|
|
|
|
m_pipeline(static_cast<MetalShaderPipeline*>(pipeline)),
|
2015-11-08 00:36:38 +00:00
|
|
|
m_vbuf(vbuf),
|
2015-11-28 04:07:53 +00:00
|
|
|
m_instVbo(instVbo),
|
2015-11-08 00:36:38 +00:00
|
|
|
m_ibuf(ibuf),
|
|
|
|
m_ubufCount(ubufCount),
|
|
|
|
m_ubufs(new IGraphicsBuffer*[ubufCount]),
|
|
|
|
m_texCount(texCount),
|
2017-03-14 07:02:53 +00:00
|
|
|
m_texs(new BoundTex[texCount]),
|
2016-12-10 02:31:50 +00:00
|
|
|
m_baseVert(baseVert),
|
|
|
|
m_baseInst(baseInst)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
addDepData(m_pipeline->m_parentData);
|
|
|
|
|
2016-04-05 02:37:46 +00:00
|
|
|
if (ubufCount && ubufStages)
|
2016-03-30 21:07:12 +00:00
|
|
|
{
|
2016-04-05 02:37:46 +00:00
|
|
|
m_fubufs.reset(new bool[ubufCount]);
|
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
|
|
|
m_fubufs[i] = ubufStages[i] == PipelineStage::Fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ubufCount && ubufOffs && ubufSizes)
|
|
|
|
{
|
|
|
|
m_ubufOffs.reset(new size_t[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
|
2016-04-05 02:37:46 +00:00
|
|
|
m_ubufOffs[i] = ubufOffs[i];
|
2016-03-30 21:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
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
|
2015-11-08 00:36:38 +00:00
|
|
|
m_ubufs[i] = ubufs[i];
|
2017-03-14 07:02:53 +00:00
|
|
|
if (ubufs[i])
|
|
|
|
addDepData(ubufs[i]->m_parentData);
|
2016-03-30 21:07:12 +00:00
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
2016-03-30 21:07:12 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
m_texs[i] = {texs[i], texBindIdxs ? texBindIdxs[i] : 0, depthBind ? depthBind[i] : false};
|
|
|
|
if (texs[i])
|
|
|
|
addDepData(texs[i]->m_parentData);
|
2016-03-30 21:07:12 +00:00
|
|
|
}
|
2015-11-08 00:36:38 +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)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
m_pipeline->bind(enc);
|
2016-12-10 02:31:50 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t stride;
|
2015-11-28 04:07:53 +00:00
|
|
|
if (m_vbuf)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
|
|
|
id<MTLBuffer> buf = GetBufferGPUResource(m_vbuf, b, stride);
|
|
|
|
[enc setVertexBuffer:buf offset:stride * m_baseVert atIndex:0];
|
|
|
|
}
|
2015-11-28 04:07:53 +00:00
|
|
|
if (m_instVbo)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
|
|
|
id<MTLBuffer> buf = GetBufferGPUResource(m_instVbo, b, stride);
|
|
|
|
[enc setVertexBuffer:buf offset:stride * m_baseInst atIndex:1];
|
|
|
|
}
|
2016-04-05 02:37:46 +00:00
|
|
|
if (m_ubufOffs)
|
2016-03-30 21:07:12 +00:00
|
|
|
for (size_t i=0 ; i<m_ubufCount ; ++i)
|
2016-04-05 02:37:46 +00:00
|
|
|
{
|
|
|
|
if (m_fubufs && m_fubufs[i])
|
|
|
|
[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_ubufCount ; ++i)
|
2016-04-05 02:37:46 +00:00
|
|
|
{
|
|
|
|
if (m_fubufs && m_fubufs[i])
|
|
|
|
[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];
|
|
|
|
}
|
2015-11-09 02:24:45 +00:00
|
|
|
for (size_t i=0 ; i<m_texCount ; ++i)
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_texs[i].tex)
|
|
|
|
[enc setFragmentTexture:GetTextureGPUResource(m_texs[i].tex, b, m_texs[i].idx, m_texs[i].depth) 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-21 02:16:15 +00:00
|
|
|
Platform platform() const {return IGraphicsDataFactory::Platform::Metal;}
|
2015-11-09 02:24:45 +00:00
|
|
|
const char* platformName() const {return "Metal";}
|
|
|
|
MetalContext* m_ctx;
|
|
|
|
IWindow* m_parentWindow;
|
2015-11-08 00:36:38 +00:00
|
|
|
IGraphicsContext* m_parent;
|
2016-01-11 22:26:40 +00:00
|
|
|
id<MTLCommandBuffer> m_cmdBuf;
|
|
|
|
id<MTLRenderCommandEncoder> m_enc;
|
2015-12-05 01:12:52 +00:00
|
|
|
bool m_running = true;
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
size_t m_fillBuf = 0;
|
|
|
|
size_t 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-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 06:45:14 +00:00
|
|
|
@autoreleasepool
|
|
|
|
{
|
2016-01-11 22:26:40 +00:00
|
|
|
m_cmdBuf = [ctx->m_q commandBuffer];
|
2015-11-09 06:45:14 +00:00
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-12-05 01:12:52 +00:00
|
|
|
void stopRenderer()
|
|
|
|
{
|
|
|
|
m_running = false;
|
|
|
|
if (m_inProgress)
|
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;
|
2016-03-24 08:05:19 +00:00
|
|
|
MTLPrimitiveType m_currentPrimitive = MTLPrimitiveTypeTriangle;
|
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);
|
2016-01-11 22:26:40 +00:00
|
|
|
cbind->bind(m_enc, m_fillBuf);
|
2015-11-09 02:24:45 +00:00
|
|
|
m_boundData = cbind;
|
2016-03-24 08:05:19 +00:00
|
|
|
m_currentPrimitive = cbind->m_pipeline->m_drawPrim;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* m_boundTarget = nullptr;
|
2016-04-03 06:18:30 +00:00
|
|
|
void _setRenderTarget(ITextureR* target, bool clearColor, bool clearDepth)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalTextureR* ctarget = static_cast<MetalTextureR*>(target);
|
2015-11-18 23:55:25 +00:00
|
|
|
@autoreleasepool
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
[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];
|
2015-11-18 23:55:25 +00:00
|
|
|
}
|
2016-09-12 05:34:45 +00:00
|
|
|
if (ctarget == m_boundTarget)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
m_boundTarget = ctarget;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2016-04-03 06:18:30 +00:00
|
|
|
void setRenderTarget(ITextureR* target)
|
|
|
|
{
|
|
|
|
_setRenderTarget(target, false, false);
|
|
|
|
}
|
2016-09-12 05:28:54 +00:00
|
|
|
|
2016-09-03 05:17:47 +00:00
|
|
|
MTLViewport m_boundVp = {};
|
2016-04-05 02:37:46 +00:00
|
|
|
void setViewport(const SWindowRect& rect, float znear, float zfar)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2016-09-03 05:17:47 +00:00
|
|
|
m_boundVp = MTLViewport{double(rect.location[0]), double(rect.location[1]),
|
|
|
|
double(rect.size[0]), double(rect.size[1]), znear, zfar};
|
|
|
|
[m_enc setViewport:m_boundVp];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2016-09-03 05:17:47 +00:00
|
|
|
MTLScissorRect m_boundScissor = {};
|
2015-11-28 04:07:53 +00:00
|
|
|
void setScissor(const SWindowRect& rect)
|
|
|
|
{
|
2015-12-31 20:20:38 +00:00
|
|
|
if (m_boundTarget)
|
|
|
|
{
|
2016-02-27 01:38:13 +00:00
|
|
|
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, m_boundTarget->m_width, m_boundTarget->m_height));
|
2016-09-03 05:17:47 +00:00
|
|
|
m_boundScissor = MTLScissorRect{NSUInteger(intersectRect.location[0]),
|
2016-02-27 01:38:13 +00:00
|
|
|
NSUInteger(m_boundTarget->m_height - intersectRect.location[1] - intersectRect.size[1]),
|
|
|
|
NSUInteger(intersectRect.size[0]), NSUInteger(intersectRect.size[1])};
|
2016-09-03 05:17:47 +00:00
|
|
|
[m_enc setScissorRect:m_boundScissor];
|
2015-12-31 20:20:38 +00:00
|
|
|
}
|
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;
|
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);
|
|
|
|
}
|
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
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
float m_clearColor[4] = {0.0,0.0,0.0,0.0};
|
2015-11-08 00:36:38 +00:00
|
|
|
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
|
|
|
|
2015-11-08 00:36:38 +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);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
void draw(size_t start, size_t count)
|
|
|
|
{
|
2016-03-24 08:05:19 +00:00
|
|
|
[m_enc drawPrimitives:m_currentPrimitive vertexStart:start vertexCount:count];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
void drawIndexed(size_t start, size_t count)
|
|
|
|
{
|
2016-03-24 08:05:19 +00:00
|
|
|
[m_enc drawIndexedPrimitives:m_currentPrimitive
|
2016-01-11 22:26:40 +00:00
|
|
|
indexCount:count
|
|
|
|
indexType:MTLIndexTypeUInt32
|
|
|
|
indexBuffer:GetBufferGPUResource(m_boundData->m_ibuf, m_fillBuf)
|
|
|
|
indexBufferOffset:start*4];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2016-03-24 08:05:19 +00:00
|
|
|
[m_enc drawPrimitives:m_currentPrimitive
|
2016-02-24 21:07:48 +00:00
|
|
|
vertexStart:start vertexCount:count instanceCount:instCount];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2016-03-24 08:05:19 +00:00
|
|
|
[m_enc drawIndexedPrimitives:m_currentPrimitive
|
2016-01-11 22:26:40 +00:00
|
|
|
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
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin,
|
|
|
|
int bindIdx, bool color, bool depth)
|
2016-02-25 05:06:13 +00:00
|
|
|
{
|
2016-02-26 08:01:46 +00:00
|
|
|
MetalTextureR* tex = static_cast<MetalTextureR*>(texture);
|
2017-03-14 07:02:53 +00:00
|
|
|
@autoreleasepool
|
2016-02-26 08:01:46 +00:00
|
|
|
{
|
|
|
|
[m_enc endEncoding];
|
2017-03-14 07:02:53 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
if (color && tex->m_colorBindTex[bindIdx])
|
2016-02-26 08:01:46 +00:00
|
|
|
{
|
|
|
|
[blitEnc copyFromTexture:tex->m_colorTex
|
|
|
|
sourceSlice:0
|
|
|
|
sourceLevel:0
|
|
|
|
sourceOrigin:origin
|
2016-02-27 01:38:13 +00:00
|
|
|
sourceSize:MTLSizeMake(intersectRect.size[0], intersectRect.size[1], 1)
|
2017-03-14 07:02:53 +00:00
|
|
|
toTexture:tex->m_colorBindTex[bindIdx]
|
2016-02-26 08:01:46 +00:00
|
|
|
destinationSlice:0
|
|
|
|
destinationLevel:0
|
|
|
|
destinationOrigin:origin];
|
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
if (depth && tex->m_depthBindTex[bindIdx])
|
|
|
|
{
|
|
|
|
[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];
|
|
|
|
}
|
|
|
|
|
|
|
|
[blitEnc endEncoding];
|
|
|
|
m_enc = [m_cmdBuf renderCommandEncoderWithDescriptor: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-26 08:01:46 +00:00
|
|
|
}
|
2016-02-25 05:06:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 20:20:38 +00:00
|
|
|
MetalTextureR* m_needsDisplay = nullptr;
|
2015-11-08 00:36:38 +00:00
|
|
|
void resolveDisplay(ITextureR* source)
|
|
|
|
{
|
2015-12-31 20:20:38 +00:00
|
|
|
m_needsDisplay = static_cast<MetalTextureR*>(source);
|
2015-11-09 02:24:45 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
bool m_inProgress = false;
|
2015-11-08 00:36:38 +00:00
|
|
|
void execute()
|
|
|
|
{
|
2015-12-05 01:12:52 +00:00
|
|
|
if (!m_running)
|
|
|
|
return;
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-12-02 22:25:30 +00:00
|
|
|
/* Update dynamic data here */
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl* gfxF = static_cast<MetalDataFactoryImpl*>(m_parent->getDataFactory());
|
2015-12-18 21:33:53 +00:00
|
|
|
std::unique_lock<std::mutex> datalk(gfxF->m_committedMutex);
|
2015-12-02 22:25:30 +00:00
|
|
|
for (MetalData* d : gfxF->m_committedData)
|
|
|
|
{
|
|
|
|
for (std::unique_ptr<MetalGraphicsBufferD>& b : d->m_DBufs)
|
|
|
|
b->update(m_fillBuf);
|
|
|
|
for (std::unique_ptr<MetalTextureD>& t : d->m_DTexs)
|
|
|
|
t->update(m_fillBuf);
|
|
|
|
}
|
2016-12-10 02:31:50 +00:00
|
|
|
for (MetalPool* p : gfxF->m_committedPools)
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
for (auto& b : p->m_items)
|
|
|
|
b->m_buf->update(m_fillBuf);
|
2016-12-10 02:31:50 +00:00
|
|
|
}
|
2015-12-18 21:33:53 +00:00
|
|
|
datalk.unlock();
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-09 06:45:14 +00:00
|
|
|
@autoreleasepool
|
2015-11-08 00:36:38 +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
|
|
|
|
2015-12-31 20:20:38 +00:00
|
|
|
/* Wrap up and present if needed */
|
|
|
|
if (m_needsDisplay)
|
|
|
|
{
|
|
|
|
MetalContext::Window& w = m_ctx->m_windows[m_parentWindow];
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
std::unique_lock<std::mutex> lk(w.m_resizeLock);
|
|
|
|
if (w.m_needsResize)
|
2015-12-31 20:20:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
w.m_metalLayer.drawableSize = w.m_size;
|
|
|
|
w.m_needsResize = NO;
|
|
|
|
m_needsDisplay = nullptr;
|
|
|
|
return;
|
2015-12-31 20:20:38 +00:00
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
}
|
|
|
|
id<CAMetalDrawable> drawable = [w.m_metalLayer nextDrawable];
|
|
|
|
if (drawable)
|
|
|
|
{
|
|
|
|
id<MTLTexture> dest = drawable.texture;
|
|
|
|
if (m_needsDisplay->m_colorTex.width == dest.width &&
|
|
|
|
m_needsDisplay->m_colorTex.height == dest.height)
|
2015-12-31 20:20:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
id<MTLBlitCommandEncoder> blitEnc = [m_cmdBuf blitCommandEncoder];
|
|
|
|
[blitEnc copyFromTexture:m_needsDisplay->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];
|
2015-12-31 20:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_needsDisplay = nullptr;
|
|
|
|
}
|
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];
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-12-02 22:25:30 +00:00
|
|
|
void MetalGraphicsBufferD::update(int b)
|
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validSlots) == 0)
|
|
|
|
{
|
2016-01-11 22:26:40 +00:00
|
|
|
id<MTLBuffer> res = m_bufs[b];
|
2015-12-02 22:25:30 +00:00
|
|
|
memcpy(res.contents, m_cpuBuf.get(), m_sz);
|
|
|
|
m_validSlots |= slot;
|
|
|
|
}
|
|
|
|
}
|
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-12-02 22:25:30 +00:00
|
|
|
size_t bufSz = std::min(sz, m_sz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validSlots = 0;
|
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-12-02 22:25:30 +00:00
|
|
|
if (sz > m_sz)
|
|
|
|
return nullptr;
|
|
|
|
return m_cpuBuf.get();
|
|
|
|
}
|
|
|
|
void MetalGraphicsBufferD::unmap()
|
|
|
|
{
|
|
|
|
m_validSlots = 0;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 22:25:30 +00:00
|
|
|
void MetalTextureD::update(int b)
|
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validSlots) == 0)
|
|
|
|
{
|
2016-01-11 22:26:40 +00:00
|
|
|
id<MTLTexture> res = m_texs[b];
|
2015-12-02 22:25:30 +00:00
|
|
|
[res replaceRegion:MTLRegionMake2D(0, 0, m_width, m_height)
|
|
|
|
mipmapLevel:0 withBytes:m_cpuBuf.get() bytesPerRow:m_width*m_pxPitch];
|
|
|
|
m_validSlots |= slot;
|
|
|
|
}
|
|
|
|
}
|
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-12-02 22:25:30 +00:00
|
|
|
size_t bufSz = std::min(sz, m_cpuSz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validSlots = 0;
|
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-12-02 22:25:30 +00:00
|
|
|
if (sz > m_cpuSz)
|
|
|
|
return nullptr;
|
|
|
|
return m_cpuBuf.get();
|
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-12-02 22:25:30 +00:00
|
|
|
m_validSlots = 0;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl::MetalDataFactoryImpl(IGraphicsContext* parent, MetalContext* ctx, uint32_t sampleCount)
|
2016-02-24 21:07:48 +00:00
|
|
|
: m_parent(parent), m_ctx(ctx), m_sampleCount(sampleCount) {}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2016-03-30 21:07:12 +00:00
|
|
|
IGraphicsBufferS* MetalDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalGraphicsBufferS* retval = new MetalGraphicsBufferS(d, use, factory.m_ctx, data, stride, count);
|
|
|
|
d->m_SBufs.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-03-30 21:07:12 +00:00
|
|
|
IGraphicsBufferD* MetalDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
|
|
|
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalGraphicsBufferD* retval = new MetalGraphicsBufferD(d, q, use, factory.m_ctx, stride, count);
|
|
|
|
d->m_DBufs.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:07:12 +00:00
|
|
|
ITextureS* MetalDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureS* retval = new MetalTextureS(d, factory.m_ctx, width, height, mips, fmt, data, sz);
|
|
|
|
d->m_STexs.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2017-01-29 03:56:17 +00:00
|
|
|
ITextureSA* MetalDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
|
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureSA* retval = new MetalTextureSA(d, factory.m_ctx, width, height, layers, mips, fmt, data, sz);
|
|
|
|
d->m_SATexs.emplace_back(retval);
|
2015-11-28 04:07:53 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2016-03-30 21:07:12 +00:00
|
|
|
ITextureD* MetalDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
|
|
|
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureD* retval = new MetalTextureD(d, q, factory.m_ctx, width, height, fmt);
|
|
|
|
d->m_DTexs.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-03-30 21:07:12 +00:00
|
|
|
ITextureR* MetalDataFactory::Context::newRenderTexture(size_t width, size_t height,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t colorBindCount, size_t depthBindCount)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalTextureR* retval = new MetalTextureR(d, factory.m_ctx, width, height, factory.m_sampleCount,
|
|
|
|
colorBindCount, depthBindCount);
|
|
|
|
d->m_RTexs.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 02:31:50 +00:00
|
|
|
IVertexFormat* MetalDataFactory::Context::newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
|
|
|
MetalVertexFormat* retval = new struct MetalVertexFormat(d, elementCount, elements);
|
|
|
|
d->m_VFmts.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:07:12 +00:00
|
|
|
IShaderPipeline* MetalDataFactory::Context::newShaderPipeline(const char* vertSource, const char* fragSource,
|
|
|
|
IVertexFormat* vtxFmt, unsigned targetSamples,
|
|
|
|
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
|
2017-03-14 07:02:53 +00:00
|
|
|
ZTest depthTest, bool depthWrite, bool colorWrite,
|
|
|
|
bool alphaWrite, CullMode culling)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-20 05:08:19 +00:00
|
|
|
@autoreleasepool
|
2017-01-30 04:14:58 +00:00
|
|
|
{
|
2017-03-20 05:08:19 +00:00
|
|
|
MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
|
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
|
|
|
MTLCompileOptions* compOpts = [MTLCompileOptions new];
|
|
|
|
compOpts.languageVersion = MTLLanguageVersion1_1;
|
|
|
|
NSError* err = nullptr;
|
|
|
|
|
|
|
|
XXH64_state_t hashState;
|
|
|
|
uint64_t hashes[2];
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, vertSource, strlen(vertSource));
|
|
|
|
hashes[0] = XXH64_digest(&hashState);
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, fragSource, strlen(fragSource));
|
|
|
|
hashes[1] = XXH64_digest(&hashState);
|
|
|
|
|
|
|
|
MetalShareableShader::Token vertShader;
|
|
|
|
MetalShareableShader::Token fragShader;
|
|
|
|
auto vertFind = factory.m_sharedShaders.find(hashes[0]);
|
|
|
|
if (vertFind != factory.m_sharedShaders.end())
|
2017-03-05 07:54:58 +00:00
|
|
|
{
|
2017-03-20 05:08:19 +00:00
|
|
|
vertShader = vertFind->second->lock();
|
2017-03-05 07:54:58 +00:00
|
|
|
}
|
2017-03-20 05:08:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
id<MTLLibrary> vertShaderLib = [factory.m_ctx->m_dev newLibraryWithSource:@(vertSource)
|
|
|
|
options:compOpts
|
|
|
|
error:&err];
|
|
|
|
if (!vertShaderLib)
|
|
|
|
{
|
|
|
|
printf("%s\n", vertSource);
|
|
|
|
Log.report(logvisor::Fatal, "error compiling vert shader: %s", [[err localizedDescription] UTF8String]);
|
|
|
|
}
|
|
|
|
id<MTLFunction> vertFunc = [vertShaderLib newFunctionWithName:@"vmain"];
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-20 05:08:19 +00:00
|
|
|
auto it =
|
|
|
|
factory.m_sharedShaders.emplace(std::make_pair(hashes[0],
|
|
|
|
std::make_unique<MetalShareableShader>(factory, hashes[0], vertFunc))).first;
|
|
|
|
vertShader = it->second->lock();
|
|
|
|
}
|
|
|
|
auto fragFind = factory.m_sharedShaders.find(hashes[1]);
|
|
|
|
if (fragFind != factory.m_sharedShaders.end())
|
|
|
|
{
|
|
|
|
fragShader = fragFind->second->lock();
|
|
|
|
}
|
|
|
|
else
|
2017-03-05 07:54:58 +00:00
|
|
|
{
|
2017-03-20 05:08:19 +00:00
|
|
|
id<MTLLibrary> fragShaderLib = [factory.m_ctx->m_dev newLibraryWithSource:@(fragSource)
|
|
|
|
options:compOpts
|
|
|
|
error:&err];
|
|
|
|
if (!fragShaderLib)
|
|
|
|
{
|
|
|
|
printf("%s\n", fragSource);
|
|
|
|
Log.report(logvisor::Fatal, "error compiling frag shader: %s", [[err localizedDescription] UTF8String]);
|
|
|
|
}
|
|
|
|
id<MTLFunction> fragFunc = [fragShaderLib newFunctionWithName:@"fmain"];
|
|
|
|
|
|
|
|
auto it =
|
|
|
|
factory.m_sharedShaders.emplace(std::make_pair(hashes[1],
|
|
|
|
std::make_unique<MetalShareableShader>(factory, hashes[1], fragFunc))).first;
|
|
|
|
fragShader = it->second->lock();
|
2017-03-05 07:54:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 05:08:19 +00:00
|
|
|
MetalShaderPipeline* retval = new MetalShaderPipeline(d, factory.m_ctx, std::move(vertShader), std::move(fragShader),
|
|
|
|
static_cast<const MetalVertexFormat*>(vtxFmt), targetSamples,
|
|
|
|
srcFac, dstFac, prim, depthTest, depthWrite,
|
|
|
|
colorWrite, alphaWrite, culling);
|
|
|
|
d->m_SPs.emplace_back(retval);
|
|
|
|
return retval;
|
2017-01-30 04:14:58 +00:00
|
|
|
}
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IShaderDataBinding*
|
2016-03-30 21:07:12 +00:00
|
|
|
MetalDataFactory::Context::newShaderDataBinding(IShaderPipeline* pipeline,
|
|
|
|
IVertexFormat* vtxFormat,
|
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbo, IGraphicsBuffer* ibuf,
|
2016-04-04 06:13:11 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs, const PipelineStage* ubufStages,
|
2016-03-30 21:07:12 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t texCount, ITexture** texs,
|
|
|
|
const int* texBindIdxs, const bool* depthBind,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalShaderDataBinding* retval =
|
2017-03-05 07:54:58 +00:00
|
|
|
new MetalShaderDataBinding(MetalDataFactoryImpl::m_deferredData.get(),
|
|
|
|
factory.m_ctx, pipeline, vbuf, instVbo, ibuf,
|
2016-12-10 02:31:50 +00:00
|
|
|
ubufCount, ubufs, ubufStages, ubufOffs,
|
2017-03-14 07:02:53 +00:00
|
|
|
ubufSizes, texCount, texs, texBindIdxs,
|
|
|
|
depthBind, baseVert, baseInst);
|
2017-03-05 07:54:58 +00:00
|
|
|
MetalDataFactoryImpl::m_deferredData->m_SBinds.emplace_back(retval);
|
2015-11-09 02:24:45 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
GraphicsDataToken MetalDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2016-03-30 21:07:12 +00:00
|
|
|
if (m_deferredData.get())
|
|
|
|
Log.report(logvisor::Fatal, "nested commitTransaction usage detected");
|
|
|
|
m_deferredData.reset(new MetalData());
|
|
|
|
|
|
|
|
MetalDataFactory::Context ctx(*this);
|
|
|
|
if (!trans(ctx))
|
|
|
|
{
|
|
|
|
delete m_deferredData.get();
|
|
|
|
m_deferredData.reset();
|
2015-12-18 21:33:53 +00:00
|
|
|
return GraphicsDataToken(this, nullptr);
|
2016-03-30 21:07:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 21:33:53 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
MetalData* retval = m_deferredData.get();
|
|
|
|
m_deferredData.reset();
|
2015-11-09 02:24:45 +00:00
|
|
|
m_committedData.insert(retval);
|
2015-12-18 21:33:53 +00:00
|
|
|
return GraphicsDataToken(this, retval);
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-12-10 02:31:50 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
GraphicsBufferPoolToken MetalDataFactoryImpl::newBufferPool()
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
MetalPool* retval = new MetalPool;
|
|
|
|
m_committedPools.insert(retval);
|
|
|
|
return GraphicsBufferPoolToken(this, retval);
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void MetalDataFactoryImpl::destroyData(IGraphicsData* d)
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-12-18 21:33:53 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2015-11-09 02:24:45 +00:00
|
|
|
MetalData* data = static_cast<MetalData*>(d);
|
|
|
|
m_committedData.erase(data);
|
2017-01-20 03:52:40 +00:00
|
|
|
data->decrement();
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-12-10 02:31:50 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void MetalDataFactoryImpl::destroyAllData()
|
2015-11-08 00:36:38 +00:00
|
|
|
{
|
2015-12-18 21:33:53 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2017-01-20 03:52:40 +00:00
|
|
|
for (MetalData* data : m_committedData)
|
|
|
|
data->decrement();
|
2017-03-14 07:02:53 +00:00
|
|
|
for (MetalPool* pool : m_committedPools)
|
|
|
|
delete pool;
|
2015-11-09 02:24:45 +00:00
|
|
|
m_committedData.clear();
|
2016-12-10 02:31:50 +00:00
|
|
|
m_committedPools.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void MetalDataFactoryImpl::destroyPool(IGraphicsBufferPool* p)
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
MetalPool* pool = static_cast<MetalPool*>(p);
|
|
|
|
m_committedPools.erase(pool);
|
|
|
|
delete pool;
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
IGraphicsBufferD* MetalDataFactoryImpl::newPoolBuffer(IGraphicsBufferPool* p, BufferUse use,
|
|
|
|
size_t stride, size_t count)
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
MetalPool* pool = static_cast<MetalPool*>(p);
|
|
|
|
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(m_parent->getCommandQueue());
|
2017-03-14 07:02:53 +00:00
|
|
|
MetalPoolItem* item = new MetalPoolItem;
|
|
|
|
MetalGraphicsBufferD* retval = new MetalGraphicsBufferD(item, q, use, m_ctx, stride, count);
|
|
|
|
item->m_buf.reset(retval);
|
|
|
|
pool->m_items.emplace(item);
|
2016-12-10 02:31:50 +00:00
|
|
|
return retval;
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void MetalDataFactoryImpl::deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf)
|
2016-12-11 20:20:29 +00:00
|
|
|
{
|
|
|
|
MetalPool* pool = static_cast<MetalPool*>(p);
|
2017-03-14 07:02:53 +00:00
|
|
|
auto search = pool->m_items.find(static_cast<MetalPoolItem*>(buf->m_parentData));
|
|
|
|
if (search != pool->m_items.end())
|
|
|
|
{
|
|
|
|
(*search)->decrement();
|
|
|
|
pool->m_items.erase(search);
|
|
|
|
}
|
2016-12-11 20:20:29 +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
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
IGraphicsDataFactory* _NewMetalDataFactory(IGraphicsContext* parent, MetalContext* ctx, uint32_t sampleCount)
|
|
|
|
{
|
|
|
|
return new class MetalDataFactoryImpl(parent, ctx, sampleCount);
|
|
|
|
}
|
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
}
|
2015-11-16 22:03:46 +00:00
|
|
|
|
|
|
|
#endif
|