diff options
Diffstat (limited to 'src/gpu/mtl/GrMtlUtil.mm')
-rw-r--r-- | src/gpu/mtl/GrMtlUtil.mm | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/gpu/mtl/GrMtlUtil.mm b/src/gpu/mtl/GrMtlUtil.mm index 5a58a259d4..f544421adc 100644 --- a/src/gpu/mtl/GrMtlUtil.mm +++ b/src/gpu/mtl/GrMtlUtil.mm @@ -8,6 +8,15 @@ #include "GrMtlUtil.h" #include "GrTypesPriv.h" +#include "GrSurface.h" +#include "mtl/GrMtlGpu.h" +#include "mtl/GrMtlTexture.h" +#include "mtl/GrMtlRenderTarget.h" +#include "SkSLCompiler.h" + +#import <Metal/Metal.h> + +#define PRINT_MSL 0 // print out the MSL code generated bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format) { MTLPixelFormat dontCare; @@ -143,3 +152,78 @@ MTLTextureDescriptor* GrGetMTLTextureDescriptor(id<MTLTexture> mtlTexture) { texDesc.usage = mtlTexture.usage; return texDesc; } + +#if PRINT_MSL +void print_msl(const char* source) { + SkTArray<SkString> lines; + SkStrSplit(source, "\n", kStrict_SkStrSplitMode, &lines); + for (int i = 0; i < lines.count(); i++) { + SkString& line = lines[i]; + line.prependf("%4i\t", i + 1); + SkDebugf("%s\n", line.c_str()); + } +} +#endif + +id<MTLLibrary> GrCompileMtlShaderLibrary(const GrMtlGpu* gpu, + const char* shaderString, + SkSL::Program::Kind kind, + const SkSL::Program::Settings& settings, + SkSL::Program::Inputs* outInputs) { + std::unique_ptr<SkSL::Program> program = + gpu->shaderCompiler()->convertProgram(kind, + SkSL::String(shaderString), + settings); + + if (!program) { + SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); + SkASSERT(false); + } + + *outInputs = program->fInputs; + SkSL::String code; + if (!gpu->shaderCompiler()->toMetal(*program, &code)) { + SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str()); + SkASSERT(false); + return nil; + } + NSString* mtlCode = [[NSString alloc] initWithCString: code.c_str() + encoding: NSASCIIStringEncoding]; +#if PRINT_MSL + print_msl([mtlCode cStringUsingEncoding: NSASCIIStringEncoding]); +#endif + + MTLCompileOptions* defaultOptions = [[MTLCompileOptions alloc] init]; + NSError* error = nil; + id<MTLLibrary> compiledLibrary = [gpu->device() newLibraryWithSource: mtlCode + options: defaultOptions + error: &error]; + if (error) { + SkDebugf("Error compiling MSL shader: %s\n", + [[error localizedDescription] cStringUsingEncoding: NSASCIIStringEncoding]); + return nil; + } + return compiledLibrary; +} + +id<MTLTexture> GrGetMTLTextureFromSurface(GrSurface* surface, bool doResolve) { + id<MTLTexture> mtlTexture = nil; + + GrMtlRenderTarget* renderTarget = static_cast<GrMtlRenderTarget*>(surface->asRenderTarget()); + GrMtlTexture* texture; + if (renderTarget) { + if (doResolve) { + // TODO: do resolve and set mtlTexture to resolved texture. As of now, we shouldn't + // have any multisampled render targets. + SkASSERT(false); + } else { + mtlTexture = renderTarget->mtlRenderTexture(); + } + } else { + texture = static_cast<GrMtlTexture*>(surface->asTexture()); + if (texture) { + mtlTexture = texture->mtlTexture(); + } + } + return mtlTexture; +} |