From 93ba0a4fc85f04bc1be9429983df1e57473b49a7 Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Mon, 14 Aug 2017 14:48:10 -0400 Subject: [PATCH] Switch SkSL to std::string On desktop, this saves just over 5% of the time in the SkSL compiler. As written, the code will now build either way, so it's much easier to switch back (or even have some platforms use SkString, if that's ever required). Bug: skia: Change-Id: I634f26a4f6fcb404e59bda6a5c6a21a9c6d73c0b Reviewed-on: https://skia-review.googlesource.com/34381 Reviewed-by: Brian Osman Commit-Queue: Brian Osman --- bench/GLBench.cpp | 2 +- fuzz/fuzz.cpp | 4 +- .../GrAlphaThresholdFragmentProcessor.cpp | 4 +- src/gpu/GrSKSLPrettyPrint.cpp | 7 +-- src/gpu/GrSKSLPrettyPrint.h | 4 +- src/gpu/effects/GrSimpleTextureEffect.cpp | 2 +- .../gl/builders/GrGLShaderStringBuilder.cpp | 6 +-- src/gpu/vk/GrVkUtil.cpp | 2 +- src/sksl/SkSLString.cpp | 2 +- src/sksl/SkSLString.h | 9 ++-- tests/GrSKSLPrettyPrintTest.cpp | 4 +- tests/SkSLErrorTest.cpp | 4 +- tests/SkSLFPTest.cpp | 2 +- tests/SkSLGLSLTest.cpp | 3 +- tests/SkSLMemoryLayoutTest.cpp | 48 +++++++++---------- tests/SkSLSPIRVTest.cpp | 2 +- 16 files changed, 54 insertions(+), 51 deletions(-) diff --git a/bench/GLBench.cpp b/bench/GLBench.cpp index f043c952f6..316cca6456 100644 --- a/bench/GLBench.cpp +++ b/bench/GLBench.cpp @@ -73,7 +73,7 @@ GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, Gr std::unique_ptr program = context->compiler()->convertProgram( type == GR_GL_VERTEX_SHADER ? SkSL::Program::kVertex_Kind : SkSL::Program::kFragment_Kind, - SkString(sksl), + SkSL::String(sksl), settings); if (!program || !context->compiler()->toGLSL(*program, &glsl)) { SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl, diff --git a/fuzz/fuzz.cpp b/fuzz/fuzz.cpp index f8897e6663..bb2b1c803e 100644 --- a/fuzz/fuzz.cpp +++ b/fuzz/fuzz.cpp @@ -543,8 +543,8 @@ static void fuzz_sksl2glsl(sk_sp bytes) { sk_sp caps = SkSL::ShaderCapsFactory::Default(); settings.fCaps = caps.get(); std::unique_ptr program = compiler.convertProgram(SkSL::Program::kFragment_Kind, - SkString((const char*) bytes->data()), - settings); + SkSL::String((const char*) bytes->data()), + settings); if (!program || !compiler.toGLSL(*program, &output)) { SkDebugf("[terminated] Couldn't compile input.\n"); return; diff --git a/src/effects/GrAlphaThresholdFragmentProcessor.cpp b/src/effects/GrAlphaThresholdFragmentProcessor.cpp index 8c3afb7aff..bfd237dbbc 100644 --- a/src/effects/GrAlphaThresholdFragmentProcessor.cpp +++ b/src/effects/GrAlphaThresholdFragmentProcessor.cpp @@ -39,9 +39,9 @@ public: kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "innerThreshold"); fOuterThresholdVar = args.fUniformHandler->addUniform( kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "outerThreshold"); - SkSL::String sk_TransformedCoords2D_0 = + SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); - SkSL::String sk_TransformedCoords2D_1 = + SkString sk_TransformedCoords2D_1 = fragBuilder->ensureCoords2D(args.fTransformedCoords[1]); fragBuilder->codeAppendf( "float4 _tmpVar1;float4 color = %stexture(%s, %s).%s%s;\nfloat4 mask_color = " diff --git a/src/gpu/GrSKSLPrettyPrint.cpp b/src/gpu/GrSKSLPrettyPrint.cpp index 65175c9b82..cb2da7589c 100644 --- a/src/gpu/GrSKSLPrettyPrint.cpp +++ b/src/gpu/GrSKSLPrettyPrint.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ #include "GrSKSLPrettyPrint.h" +#include "SkSLString.h" namespace GrSKSLPrettyPrint { @@ -12,7 +13,7 @@ class GLSLPrettyPrint { public: GLSLPrettyPrint() {} - SkString prettify(const char** strings, int* lengths, int count, bool countlines) { + SkSL::String prettify(const char** strings, int* lengths, int count, bool countlines) { fCountlines = countlines; fTabs = 0; fLinecount = 1; @@ -184,7 +185,7 @@ private: int fTabs, fLinecount; size_t fIndex, fLength; const char* fInput; - SkString fPretty; + SkSL::String fPretty; // Some helpers for parseUntil when we go over a string length bool fInParseUntilNewline; @@ -192,7 +193,7 @@ private: const char* fInParseUntilToken; }; -SkString PrettyPrint(const char** strings, int* lengths, int count, bool countlines) { +SkSL::String PrettyPrint(const char** strings, int* lengths, int count, bool countlines) { GLSLPrettyPrint pp; return pp.prettify(strings, lengths, count, countlines); } diff --git a/src/gpu/GrSKSLPrettyPrint.h b/src/gpu/GrSKSLPrettyPrint.h index 8fa4c1ec53..530fccee75 100644 --- a/src/gpu/GrSKSLPrettyPrint.h +++ b/src/gpu/GrSKSLPrettyPrint.h @@ -7,10 +7,10 @@ #ifndef GrSKSLPrettyPrint_DEFINED #define GrSKSLPrettyPrint_DEFINED -#include "SkString.h" +#include "SkSLString.h" namespace GrSKSLPrettyPrint { -SkString PrettyPrint(const char** strings, int* lengths, int count, bool countlines); +SkSL::String PrettyPrint(const char** strings, int* lengths, int count, bool countlines); }; #endif diff --git a/src/gpu/effects/GrSimpleTextureEffect.cpp b/src/gpu/effects/GrSimpleTextureEffect.cpp index 9a0017c8d1..63f41a814d 100644 --- a/src/gpu/effects/GrSimpleTextureEffect.cpp +++ b/src/gpu/effects/GrSimpleTextureEffect.cpp @@ -24,7 +24,7 @@ public: const GrSimpleTextureEffect& _outer = args.fFp.cast(); (void)_outer; fColorSpaceHelper.emitCode(args.fUniformHandler, _outer.colorXform().get()); - SkSL::String sk_TransformedCoords2D_0 = + SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); fragBuilder->codeAppendf( "float4 _tmpVar1;%s = %s * %stexture(%s, %s).%s%s;\n", args.fOutputColor, diff --git a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp index 3273218b5f..f7eae42ca0 100644 --- a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp +++ b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp @@ -37,10 +37,10 @@ static void print_shaders_line_by_line(const char** skslStrings, int* lengths, std::function println = [](const char* ln) { SkDebugf("%s\n", ln); }) { - SkString sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false); + SkSL::String sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false); println("SKSL:"); print_source_lines_with_numbers(sksl.c_str(), println); - if (!glsl.isEmpty()) { + if (0 != glsl.size()) { println("GLSL:"); print_source_lines_with_numbers(glsl.c_str(), println); } @@ -50,7 +50,7 @@ std::unique_ptr translate_to_glsl(const GrGLContext& context, GrG const char** skslStrings, int* lengths, int count, const SkSL::Program::Settings& settings, SkSL::String* glsl) { - SkString sksl; + SkSL::String sksl; #ifdef SK_DEBUG sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false); #else diff --git a/src/gpu/vk/GrVkUtil.cpp b/src/gpu/vk/GrVkUtil.cpp index 84406c8ee8..2ca701f60d 100644 --- a/src/gpu/vk/GrVkUtil.cpp +++ b/src/gpu/vk/GrVkUtil.cpp @@ -258,7 +258,7 @@ bool GrCompileVkShaderModule(const GrVkGpu* gpu, SkSL::Program::Inputs* outInputs) { std::unique_ptr program = gpu->shaderCompiler()->convertProgram( vk_shader_stage_to_skiasl_kind(stage), - SkString(shaderString), + SkSL::String(shaderString), settings); if (!program) { SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); diff --git a/src/sksl/SkSLString.cpp b/src/sksl/SkSLString.cpp index fb8fd56de8..722d61fbd0 100644 --- a/src/sksl/SkSLString.cpp +++ b/src/sksl/SkSLString.cpp @@ -24,7 +24,7 @@ String String::printf(const char* fmt, ...) { return result; } -#ifdef SKSL_STANDALONE +#ifdef SKSL_USE_STD_STRING void String::appendf(const char* fmt, ...) { va_list args; va_start(args, fmt); diff --git a/src/sksl/SkSLString.h b/src/sksl/SkSLString.h index 73ba74643e..2478c56bbf 100644 --- a/src/sksl/SkSLString.h +++ b/src/sksl/SkSLString.h @@ -8,8 +8,9 @@ #ifndef SKSL_STRING #define SKSL_STRING +#define SKSL_USE_STD_STRING -#ifdef SKSL_STANDALONE +#ifdef SKSL_USE_STD_STRING #define SKSL_STRING_BASE std::string #include #else @@ -27,7 +28,7 @@ public: String& operator=(const String&) = default; String& operator=(String&&) = default; -#ifndef SKSL_STANDALONE +#ifndef SKSL_USE_STD_STRING String(const SkString& s) : INHERITED(s) {} #endif @@ -40,7 +41,7 @@ public: static String printf(const char* fmt, ...); -#ifdef SKSL_STANDALONE +#ifdef SKSL_USE_STD_STRING void appendf(const char* fmt, ...); #endif void vappendf(const char* fmt, va_list va); @@ -83,7 +84,7 @@ long stol(String s); } // namespace -#ifdef SKSL_STANDALONE +#ifdef SKSL_USE_STD_STRING namespace std { template<> struct hash { size_t operator()(const SkSL::String& s) const { diff --git a/tests/GrSKSLPrettyPrintTest.cpp b/tests/GrSKSLPrettyPrintTest.cpp index ec5dddb023..c0de6c02a8 100644 --- a/tests/GrSKSLPrettyPrintTest.cpp +++ b/tests/GrSKSLPrettyPrintTest.cpp @@ -27,7 +27,7 @@ const SkString input6("\n}}a; little ; love; for ; leading; spaces;} " "an struct = { int a; int b; };" "int[5] arr = int[5](1,2,3,4,5);} some code at the bottom; for(;;) {} }"); -const SkString output1( +const SkSL::String output1( " 1\t#this is not a realshader\n" " 2\tvec4 some stuff;\n" " 3\toutside of a function;\n" @@ -103,7 +103,7 @@ DEF_TEST(GrSKSLPrettyPrint, r) { testStr.push_back(input6.c_str()); lengths.push_back((int)input6.size()); - SkString test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), + SkSL::String test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), testStr.count(), true); ASSERT(output1 == test); diff --git a/tests/SkSLErrorTest.cpp b/tests/SkSLErrorTest.cpp index df829874f6..b94cc82324 100644 --- a/tests/SkSLErrorTest.cpp +++ b/tests/SkSLErrorTest.cpp @@ -16,7 +16,7 @@ static void test_failure(skiatest::Reporter* r, const char* src, const char* err SkSL::Program::Settings settings; sk_sp caps = SkSL::ShaderCapsFactory::Default(); settings.fCaps = caps.get(); - compiler.convertProgram(SkSL::Program::kFragment_Kind, SkString(src), settings); + compiler.convertProgram(SkSL::Program::kFragment_Kind, SkSL::String(src), settings); SkSL::String skError(error); if (compiler.errorText() != skError) { SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, @@ -31,7 +31,7 @@ static void test_success(skiatest::Reporter* r, const char* src) { sk_sp caps = SkSL::ShaderCapsFactory::Default(); settings.fCaps = caps.get(); std::unique_ptr program = compiler.convertProgram(SkSL::Program::kFragment_Kind, - SkString(src), settings); + SkSL::String(src), settings); REPORTER_ASSERT(r, program); } diff --git a/tests/SkSLFPTest.cpp b/tests/SkSLFPTest.cpp index 0c3da0b5c3..1f983b704f 100644 --- a/tests/SkSLFPTest.cpp +++ b/tests/SkSLFPTest.cpp @@ -19,7 +19,7 @@ static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& cap SkSL::StringStream output; std::unique_ptr program = compiler.convertProgram( SkSL::Program::kFragmentProcessor_Kind, - SkString(src), + SkSL::String(src), settings); if (!program) { SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); diff --git a/tests/SkSLGLSLTest.cpp b/tests/SkSLGLSLTest.cpp index c1bf2bbbce..a7f6740442 100644 --- a/tests/SkSLGLSLTest.cpp +++ b/tests/SkSLGLSLTest.cpp @@ -23,7 +23,8 @@ static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Se SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { SkSL::Compiler compiler; SkSL::String output; - std::unique_ptr program = compiler.convertProgram(kind, SkString(src), settings); + std::unique_ptr program = compiler.convertProgram(kind, SkSL::String(src), + settings); if (!program) { SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); } diff --git a/tests/SkSLMemoryLayoutTest.cpp b/tests/SkSLMemoryLayoutTest.cpp index 0a5d382e8a..80188e61c7 100644 --- a/tests/SkSLMemoryLayoutTest.cpp +++ b/tests/SkSLMemoryLayoutTest.cpp @@ -54,40 +54,40 @@ DEF_TEST(SkSLMemoryLayout140Test, r) { // struct 1 std::vector fields1; - fields1.emplace_back(SkSL::Modifiers(), SkString("a"), context.fFloat3_Type.get()); - SkSL::Type s1(SkSL::Position(), SkString("s1"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fFloat3_Type.get()); + SkSL::Type s1(SkSL::Position(), SkSL::String("s1"), fields1); REPORTER_ASSERT(r, 16 == layout.size(s1)); REPORTER_ASSERT(r, 16 == layout.alignment(s1)); - fields1.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat_Type.get()); - SkSL::Type s2(SkSL::Position(), SkString("s2"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat_Type.get()); + SkSL::Type s2(SkSL::Position(), SkSL::String("s2"), fields1); REPORTER_ASSERT(r, 16 == layout.size(s2)); REPORTER_ASSERT(r, 16 == layout.alignment(s2)); - fields1.emplace_back(SkSL::Modifiers(), SkString("c"), context.fBool_Type.get()); - SkSL::Type s3(SkSL::Position(), SkString("s3"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("c"), context.fBool_Type.get()); + SkSL::Type s3(SkSL::Position(), SkSL::String("s3"), fields1); REPORTER_ASSERT(r, 32 == layout.size(s3)); REPORTER_ASSERT(r, 16 == layout.alignment(s3)); // struct 2 std::vector fields2; - fields2.emplace_back(SkSL::Modifiers(), SkString("a"), context.fInt_Type.get()); - SkSL::Type s4(SkSL::Position(), SkString("s4"), fields2); + fields2.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fInt_Type.get()); + SkSL::Type s4(SkSL::Position(), SkSL::String("s4"), fields2); REPORTER_ASSERT(r, 16 == layout.size(s4)); REPORTER_ASSERT(r, 16 == layout.alignment(s4)); - fields2.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat3_Type.get()); - SkSL::Type s5(SkSL::Position(), SkString("s5"), fields2); + fields2.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat3_Type.get()); + SkSL::Type s5(SkSL::Position(), SkSL::String("s5"), fields2); REPORTER_ASSERT(r, 32 == layout.size(s5)); REPORTER_ASSERT(r, 16 == layout.alignment(s5)); // arrays - SkSL::Type array1(SkString("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); + SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); REPORTER_ASSERT(r, 64 == layout.size(array1)); REPORTER_ASSERT(r, 16 == layout.alignment(array1)); REPORTER_ASSERT(r, 16 == layout.stride(array1)); - SkSL::Type array2(SkString("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); + SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); REPORTER_ASSERT(r, 64 == layout.size(array2)); REPORTER_ASSERT(r, 16 == layout.alignment(array2)); REPORTER_ASSERT(r, 16 == layout.stride(array2)); @@ -135,40 +135,40 @@ DEF_TEST(SkSLMemoryLayout430Test, r) { // struct 1 std::vector fields1; - fields1.emplace_back(SkSL::Modifiers(), SkString("a"), context.fFloat3_Type.get()); - SkSL::Type s1(SkSL::Position(), SkString("s1"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fFloat3_Type.get()); + SkSL::Type s1(SkSL::Position(), SkSL::String("s1"), fields1); REPORTER_ASSERT(r, 16 == layout.size(s1)); REPORTER_ASSERT(r, 16 == layout.alignment(s1)); - fields1.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat_Type.get()); - SkSL::Type s2(SkSL::Position(), SkString("s2"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat_Type.get()); + SkSL::Type s2(SkSL::Position(), SkSL::String("s2"), fields1); REPORTER_ASSERT(r, 16 == layout.size(s2)); REPORTER_ASSERT(r, 16 == layout.alignment(s2)); - fields1.emplace_back(SkSL::Modifiers(), SkString("c"), context.fBool_Type.get()); - SkSL::Type s3(SkSL::Position(), SkString("s3"), fields1); + fields1.emplace_back(SkSL::Modifiers(), SkSL::String("c"), context.fBool_Type.get()); + SkSL::Type s3(SkSL::Position(), SkSL::String("s3"), fields1); REPORTER_ASSERT(r, 32 == layout.size(s3)); REPORTER_ASSERT(r, 16 == layout.alignment(s3)); // struct 2 std::vector fields2; - fields2.emplace_back(SkSL::Modifiers(), SkString("a"), context.fInt_Type.get()); - SkSL::Type s4(SkSL::Position(), SkString("s4"), fields2); + fields2.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fInt_Type.get()); + SkSL::Type s4(SkSL::Position(), SkSL::String("s4"), fields2); REPORTER_ASSERT(r, 4 == layout.size(s4)); REPORTER_ASSERT(r, 4 == layout.alignment(s4)); - fields2.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat3_Type.get()); - SkSL::Type s5(SkSL::Position(), SkString("s5"), fields2); + fields2.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat3_Type.get()); + SkSL::Type s5(SkSL::Position(), SkSL::String("s5"), fields2); REPORTER_ASSERT(r, 32 == layout.size(s5)); REPORTER_ASSERT(r, 16 == layout.alignment(s5)); // arrays - SkSL::Type array1(SkString("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); + SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4); REPORTER_ASSERT(r, 16 == layout.size(array1)); REPORTER_ASSERT(r, 4 == layout.alignment(array1)); REPORTER_ASSERT(r, 4 == layout.stride(array1)); - SkSL::Type array2(SkString("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); + SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4); REPORTER_ASSERT(r, 64 == layout.size(array2)); REPORTER_ASSERT(r, 16 == layout.alignment(array2)); REPORTER_ASSERT(r, 16 == layout.stride(array2)); diff --git a/tests/SkSLSPIRVTest.cpp b/tests/SkSLSPIRVTest.cpp index 4d5457f339..ba4697d191 100644 --- a/tests/SkSLSPIRVTest.cpp +++ b/tests/SkSLSPIRVTest.cpp @@ -17,7 +17,7 @@ static void test_failure(skiatest::Reporter* r, const char* src, const char* err sk_sp caps = SkSL::ShaderCapsFactory::Default(); settings.fCaps = caps.get(); std::unique_ptr program = compiler.convertProgram(SkSL::Program::kFragment_Kind, - SkString(src), settings); + SkSL::String(src), settings); if (program) { SkSL::String ignored; compiler.toSPIRV(*program, &ignored);