22#if !__has_feature(objc_arc)
23#error This file must be compiled with Arc. Use -fobjc-arc flag
30 fPipelineStateCache = std::make_unique<PipelineStateCache>(gpu);
37 return fPipelineStateCache->refPipelineState(programDesc, programInfo, stat);
41 return fPipelineStateCache->precompileShader(
key,
data);
50 depthStencilState = fDepthStencilStates.
find(
key);
51 if (!depthStencilState) {
53 fDepthStencilStates.
add(depthStencilState);
56 return depthStencilState;
64 fSamplers.
add(sampler);
71 MTLPixelFormat colorFormat,
int sampleCount, MTLPixelFormat stencilFormat) {
72 if (!fMSAALoadLibrary) {
75 std::string shaderText;
77 "#include <metal_stdlib>\n"
78 "#include <simd/simd.h>\n"
79 "using namespace metal;\n"
82 " float4 position [[position]];\n"
86 " float4 uPosXform;\n"
87 " uint2 uTextureSize;\n"
90 "vertex VertexOutput vertexMain(constant VertexUniforms& uniforms [[buffer(0)]],\n"
91 " uint vertexID [[vertex_id]]) {\n"
92 " VertexOutput out;\n"
93 " float2 position = float2(float(vertexID >> 1), float(vertexID & 1));\n"
94 " out.position.xy = position * uniforms.uPosXform.xy + uniforms.uPosXform.zw;\n"
95 " out.position.zw = float2(0.0, 1.0);\n"
99 "fragment float4 fragmentMain(VertexOutput in [[stage_in]],\n"
100 " texture2d<half> colorMap [[texture(0)]]) {\n"
101 " uint2 coords = uint2(in.position.x, in.position.y);"
102 " half4 colorSample = colorMap.read(coords);\n"
103 " return float4(colorSample);\n"
109 if (!fMSAALoadLibrary) {
114 for (
int i = 0;
i < fMSAALoadPipelines.
size(); ++
i) {
115 if (fMSAALoadPipelines[
i].fColorFormat == colorFormat &&
116 fMSAALoadPipelines[
i].fSampleCount == sampleCount &&
117 fMSAALoadPipelines[
i].fStencilFormat == stencilFormat) {
118 return fMSAALoadPipelines[
i].fPipeline.get();
122 auto pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc]
init];
124 pipelineDescriptor.label =
@"loadMSAAFromResolve";
126 pipelineDescriptor.vertexFunction =
127 [fMSAALoadLibrary newFunctionWithName:
@"vertexMain"];
128 pipelineDescriptor.fragmentFunction =
129 [fMSAALoadLibrary newFunctionWithName:
@"fragmentMain"];
131 auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc]
init];
133 mtlColorAttachment.pixelFormat = colorFormat;
134 mtlColorAttachment.blendingEnabled =
FALSE;
135 mtlColorAttachment.writeMask = MTLColorWriteMaskAll;
137 pipelineDescriptor.colorAttachments[0] = mtlColorAttachment;
138 pipelineDescriptor.rasterSampleCount = sampleCount;
140 pipelineDescriptor.stencilAttachmentPixelFormat = stencilFormat;
144 [fGpu->
device() newRenderPipelineStateWithDescriptor: pipelineDescriptor
147 SkDebugf(
"Error creating pipeline: %s\n",
148 [[
error localizedDescription] cStringUsingEncoding: NSASCIIStringEncoding]);
153 fMSAALoadPipelines.
push_back({renderPipeline, colorFormat, sampleCount, stencilFormat});
154 return fMSAALoadPipelines[fMSAALoadPipelines.
size()-1].fPipeline.get();
158 fMSAALoadLibrary = nil;
159 fMSAALoadPipelines.
clear();
165 fDepthStencilStates.
reset();
167 fPipelineStateCache->release();
185GrMtlResourceProvider::PipelineStateCache::PipelineStateCache(
GrMtlGpu* gpu)
186 : fMap(gpu->getContext()->
priv().
options().fRuntimeProgramCacheSize)
189GrMtlResourceProvider::PipelineStateCache::~PipelineStateCache() {
193void GrMtlResourceProvider::PipelineStateCache::release() {
206 auto tmp = this->onRefPipelineState(
desc, programInfo, &stat);
214 return this->onRefPipelineState(
desc, programInfo, statPtr);
222 *stat = Stats::ProgramCacheResult::kHit;
223 std::unique_ptr<Entry>* entry = fMap.find(
desc);
224 if (entry && !(*entry)->fPipelineState) {
229 (*entry)->fPipelineState.reset(
232 if (!(*entry)->fPipelineState) {
234 SkDEBUGFAIL(
"Couldn't create pipelineState from precompiled shaders");
239 (*entry)->fPrecompiledLibraries.fVertexLibrary = nil;
240 (*entry)->fPrecompiledLibraries.fFragmentLibrary = nil;
243 *stat = Stats::ProgramCacheResult::kPartial;
247 if (!pipelineState) {
252 entry = fMap.insert(
desc, std::make_unique<Entry>(pipelineState));
253 *stat = Stats::ProgramCacheResult::kMiss;
254 return (*entry)->fPipelineState.get();
256 return (*entry)->fPipelineState.get();
259bool GrMtlResourceProvider::PipelineStateCache::precompileShader(
const SkData&
key,
266 std::unique_ptr<Entry>* entry = fMap.find(
desc);
277 fMap.insert(
desc, std::make_unique<Entry>(precompiledLibraries));
id< MTLLibrary > GrCompileMtlShaderLibrary(const GrMtlGpu *gpu, const std::string &msl, GrContextOptions::ShaderErrorHandler *errorHandler)
#define SkDEBUGFAIL(message)
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
GrContextOptions::ShaderErrorHandler * getShaderErrorHandler() const
GrDirectContextPriv priv()
GrDirectContext * getContext()
static Key GenerateKey(const GrStencilSettings &, GrSurfaceOrigin)
static GrMtlDepthStencil * Create(const GrMtlGpu *, const GrStencilSettings &, GrSurfaceOrigin)
id< MTLDevice > device() const
static GrMtlPipelineState * CreatePipelineState(GrMtlGpu *, const GrProgramDesc &, const GrProgramInfo &, const GrMtlPrecompiledLibraries *precompiledLibs=nullptr)
static bool PrecompileShaders(GrMtlGpu *, const SkData &, GrMtlPrecompiledLibraries *precompiledLibs)
static sk_sp< GrMtlRenderPipeline > Make(id< MTLRenderPipelineState > pso)
const GrMtlRenderPipeline * findOrCreateMSAALoadPipeline(MTLPixelFormat colorFormat, int sampleCount, MTLPixelFormat stencilFormat)
GrMtlPipelineState * findOrCreateCompatiblePipelineState(const GrProgramDesc &, const GrProgramInfo &, GrThreadSafePipelineBuilder::Stats::ProgramCacheResult *stat=nullptr)
bool precompileShader(const SkData &key, const SkData &data)
GrMtlDepthStencil * findOrCreateCompatibleDepthStencilState(const GrStencilSettings &, GrSurfaceOrigin)
GrMtlResourceProvider(GrMtlGpu *gpu)
GrMtlSampler * findOrCreateCompatibleSampler(GrSamplerState)
static Key GenerateKey(GrSamplerState)
static GrMtlSampler * Create(const GrMtlGpu *gpu, GrSamplerState)
static bool BuildFromData(GrProgramDesc *desc, const void *keyData, size_t keyLength)
void incNumInlineCompilationFailures()
void incNumPartialCompilationSuccesses()
void incNumCompilationFailures()
void incNumInlineProgramCacheResult(ProgramCacheResult stat)
void incNumCompilationSuccesses()
T * find(const Key &key) const
const EmbeddedViewParams * params
FlPixelBufferTexturePrivate * priv
const uint8_t uint32_t uint32_t GError ** error
id< MTLLibrary > fFragmentLibrary
id< MTLLibrary > fVertexLibrary
GrMtlPrecompiledLibraries fPrecompiledLibraries
Entry(const GrMtlPrecompiledLibraries &precompiledLibraries)
Entry(GrMtlPipelineState *pipelineState)
std::unique_ptr< GrMtlPipelineState > fPipelineState
std::shared_ptr< const fml::Mapping > data
#define TRACE_EVENT0(category_group, name)