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 <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2017-08-14 14:48:10 -04:00 committed by Skia Commit-Bot
parent ba8275148a
commit 93ba0a4fc8
16 changed files with 54 additions and 51 deletions

View File

@ -73,7 +73,7 @@ GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, Gr
std::unique_ptr<SkSL::Program> 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,

View File

@ -543,7 +543,7 @@ static void fuzz_sksl2glsl(sk_sp<SkData> bytes) {
sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
SkString((const char*) bytes->data()),
SkSL::String((const char*) bytes->data()),
settings);
if (!program || !compiler.toGLSL(*program, &output)) {
SkDebugf("[terminated] Couldn't compile input.\n");

View File

@ -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 = "

View File

@ -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);
}

View File

@ -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

View File

@ -24,7 +24,7 @@ public:
const GrSimpleTextureEffect& _outer = args.fFp.cast<GrSimpleTextureEffect>();
(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,

View File

@ -37,10 +37,10 @@ static void print_shaders_line_by_line(const char** skslStrings, int* lengths,
std::function<void(const char*)> 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<SkSL::Program> 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

View File

@ -258,7 +258,7 @@ bool GrCompileVkShaderModule(const GrVkGpu* gpu,
SkSL::Program::Inputs* outInputs) {
std::unique_ptr<SkSL::Program> 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());

View File

@ -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);

View File

@ -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 <string>
#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<SkSL::String> {
size_t operator()(const SkSL::String& s) const {

View File

@ -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);

View File

@ -16,7 +16,7 @@ static void test_failure(skiatest::Reporter* r, const char* src, const char* err
SkSL::Program::Settings settings;
sk_sp<GrShaderCaps> 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<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
SkString(src), settings);
SkSL::String(src), settings);
REPORTER_ASSERT(r, program);
}

View File

@ -19,7 +19,7 @@ static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& cap
SkSL::StringStream output;
std::unique_ptr<SkSL::Program> 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());

View File

@ -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<SkSL::Program> program = compiler.convertProgram(kind, SkString(src), settings);
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
settings);
if (!program) {
SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
}

View File

@ -54,40 +54,40 @@ DEF_TEST(SkSLMemoryLayout140Test, r) {
// struct 1
std::vector<SkSL::Type::Field> 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<SkSL::Type::Field> 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<SkSL::Type::Field> 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<SkSL::Type::Field> 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));

View File

@ -17,7 +17,7 @@ static void test_failure(skiatest::Reporter* r, const char* src, const char* err
sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
SkString(src), settings);
SkSL::String(src), settings);
if (program) {
SkSL::String ignored;
compiler.toSPIRV(*program, &ignored);