2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-10-05 18:18:49 +00:00
|
|
|
#define SK_OPTS_NS skslc_standalone
|
2021-10-28 18:35:40 +00:00
|
|
|
#include "src/core/SkOpts.h"
|
2020-10-05 18:18:49 +00:00
|
|
|
#include "src/opts/SkChecksum_opts.h"
|
2020-12-17 21:02:08 +00:00
|
|
|
#include "src/opts/SkVM_opts.h"
|
2020-10-05 18:18:49 +00:00
|
|
|
|
2021-02-08 18:49:53 +00:00
|
|
|
#include "src/gpu/GrShaderUtils.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLCompiler.h"
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/SkSLDehydrator.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLFileOutputStream.h"
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/SkSLStringStream.h"
|
2020-09-15 19:37:24 +00:00
|
|
|
#include "src/sksl/SkSLUtil.h"
|
2021-04-13 14:41:57 +00:00
|
|
|
#include "src/sksl/codegen/SkSLPipelineStageCodeGenerator.h"
|
|
|
|
#include "src/sksl/codegen/SkSLVMCodeGenerator.h"
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/ir/SkSLUnresolvedFunction.h"
|
2021-10-18 14:39:20 +00:00
|
|
|
#include "src/sksl/ir/SkSLVarDeclarations.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2020-11-20 21:28:50 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
|
|
|
|
2020-10-05 18:18:49 +00:00
|
|
|
#include <fstream>
|
2020-11-13 21:13:18 +00:00
|
|
|
#include <limits.h>
|
2020-10-02 17:41:21 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
void SkDebugf(const char format[], ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2020-10-05 18:18:49 +00:00
|
|
|
namespace SkOpts {
|
|
|
|
decltype(hash_fn) hash_fn = skslc_standalone::hash_fn;
|
2020-12-17 21:02:08 +00:00
|
|
|
decltype(interpret_skvm) interpret_skvm = skslc_standalone::interpret_skvm;
|
2020-10-05 18:18:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 23:54:33 +00:00
|
|
|
enum class ResultCode {
|
|
|
|
kSuccess = 0,
|
|
|
|
kCompileError = 1,
|
|
|
|
kInputError = 2,
|
|
|
|
kOutputError = 3,
|
2020-11-20 21:28:50 +00:00
|
|
|
kConfigurationError = 4,
|
2020-11-19 23:54:33 +00:00
|
|
|
};
|
|
|
|
|
2020-12-17 21:02:08 +00:00
|
|
|
static std::unique_ptr<SkWStream> as_SkWStream(SkSL::OutputStream& s) {
|
|
|
|
struct Adapter : public SkWStream {
|
|
|
|
public:
|
|
|
|
Adapter(SkSL::OutputStream& out) : fOut(out), fBytesWritten(0) {}
|
|
|
|
|
|
|
|
bool write(const void* buffer, size_t size) override {
|
|
|
|
fOut.write(buffer, size);
|
|
|
|
fBytesWritten += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void flush() override {}
|
|
|
|
size_t bytesWritten() const override { return fBytesWritten; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkSL::OutputStream& fOut;
|
|
|
|
size_t fBytesWritten;
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::make_unique<Adapter>(s);
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:03:38 +00:00
|
|
|
// Given the path to a file (e.g. src/gpu/effects/GrFooFragmentProcessor.fp) and the expected
|
|
|
|
// filename prefix and suffix (e.g. "Gr" and ".fp"), returns the "base name" of the
|
|
|
|
// file (in this case, 'FooFragmentProcessor'). If no match, returns the empty string.
|
2020-11-11 22:29:28 +00:00
|
|
|
static SkSL::String base_name(const SkSL::String& fpPath, const char* prefix, const char* suffix) {
|
2017-06-29 14:03:38 +00:00
|
|
|
SkSL::String result;
|
2020-11-11 22:29:28 +00:00
|
|
|
const char* end = &*fpPath.end();
|
2017-06-29 14:03:38 +00:00
|
|
|
const char* fileName = end;
|
|
|
|
// back up until we find a slash
|
|
|
|
while (fileName != fpPath && '/' != *(fileName - 1) && '\\' != *(fileName - 1)) {
|
|
|
|
--fileName;
|
|
|
|
}
|
|
|
|
if (!strncmp(fileName, prefix, strlen(prefix)) &&
|
|
|
|
!strncmp(end - strlen(suffix), suffix, strlen(suffix))) {
|
|
|
|
result.append(fileName + strlen(prefix), end - fileName - strlen(prefix) - strlen(suffix));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-16 21:43:11 +00:00
|
|
|
// Given a string containing an SkSL program, searches for a #pragma settings comment, like so:
|
|
|
|
// /*#pragma settings Default Sharpen*/
|
|
|
|
// The passed-in Settings object will be updated accordingly. Any number of options can be provided.
|
2020-11-11 22:29:28 +00:00
|
|
|
static bool detect_shader_settings(const SkSL::String& text,
|
2020-11-02 17:26:22 +00:00
|
|
|
SkSL::Program::Settings* settings,
|
|
|
|
const SkSL::ShaderCapsClass** caps) {
|
2020-09-16 21:46:37 +00:00
|
|
|
using Factory = SkSL::ShaderCapsFactory;
|
|
|
|
|
2020-09-16 21:43:11 +00:00
|
|
|
// Find a matching comment and isolate the name portion.
|
|
|
|
static constexpr char kPragmaSettings[] = "/*#pragma settings ";
|
|
|
|
const char* settingsPtr = strstr(text.c_str(), kPragmaSettings);
|
|
|
|
if (settingsPtr != nullptr) {
|
|
|
|
// Subtract one here in order to preserve the leading space, which is necessary to allow
|
|
|
|
// consumeSuffix to find the first item.
|
|
|
|
settingsPtr += strlen(kPragmaSettings) - 1;
|
|
|
|
|
|
|
|
const char* settingsEnd = strstr(settingsPtr, "*/");
|
|
|
|
if (settingsEnd != nullptr) {
|
|
|
|
SkSL::String settingsText{settingsPtr, size_t(settingsEnd - settingsPtr)};
|
|
|
|
|
|
|
|
// Apply settings as requested. Since they can come in any order, repeat until we've
|
|
|
|
// consumed them all.
|
|
|
|
for (;;) {
|
|
|
|
const size_t startingLength = settingsText.length();
|
|
|
|
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" AddAndTrueToLoopCondition")) {
|
|
|
|
static auto s_addAndTrueCaps = Factory::AddAndTrueToLoopCondition();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_addAndTrueCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" CannotUseFractForNegativeValues")) {
|
|
|
|
static auto s_negativeFractCaps = Factory::CannotUseFractForNegativeValues();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_negativeFractCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-09-18 18:42:58 +00:00
|
|
|
if (settingsText.consumeSuffix(" CannotUseFragCoord")) {
|
|
|
|
static auto s_noFragCoordCaps = Factory::CannotUseFragCoord();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_noFragCoordCaps.get();
|
2020-09-18 18:42:58 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" CannotUseMinAndAbsTogether")) {
|
|
|
|
static auto s_minAbsCaps = Factory::CannotUseMinAndAbsTogether();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_minAbsCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-09-16 21:43:11 +00:00
|
|
|
if (settingsText.consumeSuffix(" Default")) {
|
2020-09-16 21:46:37 +00:00
|
|
|
static auto s_defaultCaps = Factory::Default();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_defaultCaps.get();
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" EmulateAbsIntFunction")) {
|
|
|
|
static auto s_emulateAbsIntCaps = Factory::EmulateAbsIntFunction();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_emulateAbsIntCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2021-09-15 14:28:27 +00:00
|
|
|
if (settingsText.consumeSuffix(" FramebufferFetchSupport")) {
|
|
|
|
static auto s_fbFetchSupport = Factory::FramebufferFetchSupport();
|
|
|
|
*caps = s_fbFetchSupport.get();
|
|
|
|
}
|
2020-09-18 18:42:58 +00:00
|
|
|
if (settingsText.consumeSuffix(" IncompleteShortIntPrecision")) {
|
|
|
|
static auto s_incompleteShortIntCaps = Factory::IncompleteShortIntPrecision();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_incompleteShortIntCaps.get();
|
2020-09-18 18:42:58 +00:00
|
|
|
}
|
2020-10-09 02:18:10 +00:00
|
|
|
if (settingsText.consumeSuffix(" MustGuardDivisionEvenAfterExplicitZeroCheck")) {
|
|
|
|
static auto s_div0Caps = Factory::MustGuardDivisionEvenAfterExplicitZeroCheck();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_div0Caps.get();
|
2020-10-09 02:18:10 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" MustForceNegatedAtanParamToFloat")) {
|
|
|
|
static auto s_negativeAtanCaps = Factory::MustForceNegatedAtanParamToFloat();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_negativeAtanCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2021-06-08 00:51:42 +00:00
|
|
|
if (settingsText.consumeSuffix(" MustForceNegatedLdexpParamToMultiply")) {
|
|
|
|
static auto s_negativeLdexpCaps =
|
|
|
|
Factory::MustForceNegatedLdexpParamToMultiply();
|
|
|
|
*caps = s_negativeLdexpCaps.get();
|
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" RemovePowWithConstantExponent")) {
|
|
|
|
static auto s_powCaps = Factory::RemovePowWithConstantExponent();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_powCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-09-18 18:42:58 +00:00
|
|
|
if (settingsText.consumeSuffix(" RewriteDoWhileLoops")) {
|
|
|
|
static auto s_rewriteLoopCaps = Factory::RewriteDoWhileLoops();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_rewriteLoopCaps.get();
|
2020-09-18 18:42:58 +00:00
|
|
|
}
|
2021-09-24 15:33:54 +00:00
|
|
|
if (settingsText.consumeSuffix(" RewriteSwitchStatements")) {
|
|
|
|
static auto s_rewriteSwitchCaps = Factory::RewriteSwitchStatements();
|
|
|
|
*caps = s_rewriteSwitchCaps.get();
|
|
|
|
}
|
2021-03-23 21:12:03 +00:00
|
|
|
if (settingsText.consumeSuffix(" RewriteMatrixVectorMultiply")) {
|
|
|
|
static auto s_rewriteMatVecMulCaps = Factory::RewriteMatrixVectorMultiply();
|
|
|
|
*caps = s_rewriteMatVecMulCaps.get();
|
|
|
|
}
|
2021-05-21 21:27:57 +00:00
|
|
|
if (settingsText.consumeSuffix(" RewriteMatrixComparisons")) {
|
|
|
|
static auto s_rewriteMatrixComparisons = Factory::RewriteMatrixComparisons();
|
|
|
|
*caps = s_rewriteMatrixComparisons.get();
|
2021-05-20 22:00:39 +00:00
|
|
|
}
|
2020-09-18 16:03:42 +00:00
|
|
|
if (settingsText.consumeSuffix(" ShaderDerivativeExtensionString")) {
|
|
|
|
static auto s_derivativeCaps = Factory::ShaderDerivativeExtensionString();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_derivativeCaps.get();
|
2020-09-18 16:03:42 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
if (settingsText.consumeSuffix(" UnfoldShortCircuitAsTernary")) {
|
|
|
|
static auto s_ternaryCaps = Factory::UnfoldShortCircuitAsTernary();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_ternaryCaps.get();
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-09-16 21:43:11 +00:00
|
|
|
if (settingsText.consumeSuffix(" UsesPrecisionModifiers")) {
|
2020-09-16 21:46:37 +00:00
|
|
|
static auto s_precisionCaps = Factory::UsesPrecisionModifiers();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_precisionCaps.get();
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" Version110")) {
|
2020-09-16 21:46:37 +00:00
|
|
|
static auto s_version110Caps = Factory::Version110();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_version110Caps.get();
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" Version450Core")) {
|
2020-09-16 21:46:37 +00:00
|
|
|
static auto s_version450CoreCaps = Factory::Version450Core();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_version450CoreCaps.get();
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
2021-08-04 17:27:05 +00:00
|
|
|
if (settingsText.consumeSuffix(" AllowNarrowingConversions")) {
|
|
|
|
settings->fAllowNarrowingConversions = true;
|
|
|
|
}
|
2020-09-16 21:43:11 +00:00
|
|
|
if (settingsText.consumeSuffix(" ForceHighPrecision")) {
|
|
|
|
settings->fForceHighPrecision = true;
|
|
|
|
}
|
2021-08-05 14:55:03 +00:00
|
|
|
if (settingsText.consumeSuffix(" NoES2Restrictions")) {
|
|
|
|
settings->fEnforceES2Restrictions = false;
|
|
|
|
}
|
2020-10-12 16:33:27 +00:00
|
|
|
if (settingsText.consumeSuffix(" NoInline")) {
|
|
|
|
settings->fInlineThreshold = 0;
|
|
|
|
}
|
2020-11-13 21:13:18 +00:00
|
|
|
if (settingsText.consumeSuffix(" InlineThresholdMax")) {
|
|
|
|
settings->fInlineThreshold = INT_MAX;
|
|
|
|
}
|
2020-09-16 21:43:11 +00:00
|
|
|
if (settingsText.consumeSuffix(" Sharpen")) {
|
|
|
|
settings->fSharpenTextures = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingsText.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (settingsText.length() == startingLength) {
|
|
|
|
printf("Unrecognized #pragma settings: %s\n", settingsText.c_str());
|
2020-11-11 22:29:28 +00:00
|
|
|
return false;
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
|
|
|
|
return true;
|
2020-09-16 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
/**
|
2020-11-11 22:29:28 +00:00
|
|
|
* Displays a usage banner; used when the command line arguments don't make sense.
|
2016-07-01 15:22:01 +00:00
|
|
|
*/
|
2020-11-11 22:29:28 +00:00
|
|
|
static void show_usage() {
|
2020-11-18 17:04:33 +00:00
|
|
|
printf("usage: skslc <input> <output> <flags>\n"
|
|
|
|
" skslc <worklist>\n"
|
2020-11-11 22:29:28 +00:00
|
|
|
"\n"
|
|
|
|
"Allowed flags:\n"
|
|
|
|
"--settings: honor embedded /*#pragma settings*/ comments.\n"
|
|
|
|
"--nosettings: ignore /*#pragma settings*/ comments\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a single input.
|
|
|
|
*/
|
2020-11-19 23:54:33 +00:00
|
|
|
ResultCode processCommand(std::vector<SkSL::String>& args) {
|
2020-09-16 21:46:37 +00:00
|
|
|
bool honorSettings = true;
|
2020-11-11 22:29:28 +00:00
|
|
|
if (args.size() == 4) {
|
|
|
|
// Handle four-argument case: `skslc in.sksl out.glsl --settings`
|
|
|
|
const SkSL::String& settingsArg = args[3];
|
|
|
|
if (settingsArg == "--settings") {
|
2020-09-16 21:46:37 +00:00
|
|
|
honorSettings = true;
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (settingsArg == "--nosettings") {
|
2020-09-16 21:46:37 +00:00
|
|
|
honorSettings = false;
|
|
|
|
} else {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("unrecognized flag: %s\n\n", settingsArg.c_str());
|
|
|
|
show_usage();
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (args.size() != 3) {
|
|
|
|
show_usage();
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
|
2021-02-16 18:29:15 +00:00
|
|
|
SkSL::ProgramKind kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
const SkSL::String& inputPath = args[1];
|
2021-06-10 15:21:59 +00:00
|
|
|
if (inputPath.ends_with(".vert")) {
|
2021-02-16 18:29:15 +00:00
|
|
|
kind = SkSL::ProgramKind::kVertex;
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (inputPath.ends_with(".frag") || inputPath.ends_with(".sksl")) {
|
2021-02-16 18:29:15 +00:00
|
|
|
kind = SkSL::ProgramKind::kFragment;
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (inputPath.ends_with(".rtb")) {
|
2021-06-16 15:33:13 +00:00
|
|
|
kind = SkSL::ProgramKind::kRuntimeBlender;
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (inputPath.ends_with(".rtcf")) {
|
2021-04-12 13:49:20 +00:00
|
|
|
kind = SkSL::ProgramKind::kRuntimeColorFilter;
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (inputPath.ends_with(".rts")) {
|
2021-04-12 13:49:20 +00:00
|
|
|
kind = SkSL::ProgramKind::kRuntimeShader;
|
2016-07-01 15:22:01 +00:00
|
|
|
} else {
|
2021-08-27 15:21:12 +00:00
|
|
|
printf("input filename must end in '.vert', '.frag', '.rtb', '.rtcf', "
|
2021-04-12 13:49:20 +00:00
|
|
|
"'.rts', or '.sksl'\n");
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 22:29:28 +00:00
|
|
|
std::ifstream in(inputPath);
|
2020-09-16 21:43:11 +00:00
|
|
|
SkSL::String text((std::istreambuf_iterator<char>(in)),
|
|
|
|
std::istreambuf_iterator<char>());
|
2016-07-01 15:22:01 +00:00
|
|
|
if (in.rdstate()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error reading '%s'\n", inputPath.c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2020-09-15 19:37:24 +00:00
|
|
|
|
2016-12-12 20:33:30 +00:00
|
|
|
SkSL::Program::Settings settings;
|
2021-10-21 18:51:50 +00:00
|
|
|
SkSL::StandaloneShaderCaps standaloneCaps;
|
|
|
|
const SkSL::ShaderCapsClass* caps = &standaloneCaps;
|
2020-09-16 21:46:37 +00:00
|
|
|
if (honorSettings) {
|
2020-11-11 22:29:28 +00:00
|
|
|
if (!detect_shader_settings(text, &settings, &caps)) {
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
|
2021-07-07 13:41:17 +00:00
|
|
|
// This tells the compiler where the rt-flip uniform will live should it be required. For
|
|
|
|
// testing purposes we don't care where that is, but the compiler will report an error if we
|
2021-08-12 20:09:21 +00:00
|
|
|
// leave them at their default invalid values, or if the offset overlaps another uniform.
|
|
|
|
settings.fRTFlipOffset = 16384;
|
2021-07-07 13:41:17 +00:00
|
|
|
settings.fRTFlipSet = 0;
|
|
|
|
settings.fRTFlipBinding = 0;
|
|
|
|
|
2020-11-11 22:29:28 +00:00
|
|
|
const SkSL::String& outputPath = args[2];
|
|
|
|
auto emitCompileError = [&](SkSL::FileOutputStream& out, const char* errorText) {
|
2020-11-19 23:54:33 +00:00
|
|
|
// Overwrite the compiler output, if any, with an error message.
|
|
|
|
out.close();
|
|
|
|
SkSL::FileOutputStream errorStream(outputPath);
|
|
|
|
errorStream.writeText("### Compilation failed:\n\n");
|
|
|
|
errorStream.writeText(errorText);
|
|
|
|
errorStream.close();
|
|
|
|
// Also emit the error directly to stdout.
|
|
|
|
puts(errorText);
|
2020-11-11 22:29:28 +00:00
|
|
|
};
|
|
|
|
|
2021-02-11 20:46:11 +00:00
|
|
|
auto compileProgram = [&](const auto& writeFn) -> ResultCode {
|
2020-11-11 22:29:28 +00:00
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2021-02-11 20:46:11 +00:00
|
|
|
SkSL::Compiler compiler(caps);
|
2016-11-21 15:39:35 +00:00
|
|
|
if (!out.isValid()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kOutputError;
|
2016-11-21 15:39:35 +00:00
|
|
|
}
|
2016-12-12 20:33:30 +00:00
|
|
|
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
|
2020-11-20 19:45:00 +00:00
|
|
|
if (!program || !writeFn(compiler, *program, out)) {
|
2020-11-11 22:29:28 +00:00
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kCompileError;
|
2016-10-12 13:39:56 +00:00
|
|
|
}
|
2017-03-31 17:56:23 +00:00
|
|
|
if (!out.close()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-03-31 17:56:23 +00:00
|
|
|
}
|
2020-11-20 19:45:00 +00:00
|
|
|
return ResultCode::kSuccess;
|
|
|
|
};
|
|
|
|
|
2021-06-10 15:21:59 +00:00
|
|
|
if (outputPath.ends_with(".spirv")) {
|
2020-11-20 19:45:00 +00:00
|
|
|
return compileProgram(
|
|
|
|
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
|
|
|
|
return compiler.toSPIRV(program, out);
|
|
|
|
});
|
2021-08-27 15:21:12 +00:00
|
|
|
} else if (outputPath.ends_with(".asm.frag") || outputPath.ends_with(".asm.vert")) {
|
2020-11-20 21:28:50 +00:00
|
|
|
return compileProgram(
|
|
|
|
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
|
|
|
|
// Compile program to SPIR-V assembly in a string-stream.
|
|
|
|
SkSL::StringStream assembly;
|
|
|
|
if (!compiler.toSPIRV(program, assembly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Convert the string-stream to a SPIR-V disassembly.
|
|
|
|
spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
|
|
|
|
const SkSL::String& spirv(assembly.str());
|
|
|
|
std::string disassembly;
|
|
|
|
if (!tools.Disassemble((const uint32_t*)spirv.data(),
|
|
|
|
spirv.size() / 4, &disassembly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Finally, write the disassembly to our output stream.
|
|
|
|
out.write(disassembly.data(), disassembly.size());
|
|
|
|
return true;
|
|
|
|
});
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (outputPath.ends_with(".glsl")) {
|
2020-11-20 19:45:00 +00:00
|
|
|
return compileProgram(
|
|
|
|
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
|
|
|
|
return compiler.toGLSL(program, out);
|
|
|
|
});
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (outputPath.ends_with(".metal")) {
|
2020-11-20 19:45:00 +00:00
|
|
|
return compileProgram(
|
|
|
|
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
|
|
|
|
return compiler.toMetal(program, out);
|
|
|
|
});
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (outputPath.ends_with(".skvm")) {
|
2020-12-02 14:27:10 +00:00
|
|
|
return compileProgram(
|
2020-12-17 21:02:08 +00:00
|
|
|
[](SkSL::Compiler&, SkSL::Program& program, SkSL::OutputStream& out) {
|
2020-12-22 17:46:57 +00:00
|
|
|
skvm::Builder builder{skvm::Features{}};
|
2020-12-17 21:02:08 +00:00
|
|
|
if (!SkSL::testingOnly_ProgramToSkVMShader(program, &builder)) {
|
2020-12-02 14:27:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-17 21:02:08 +00:00
|
|
|
std::unique_ptr<SkWStream> redirect = as_SkWStream(out);
|
|
|
|
builder.done().dump(redirect.get());
|
2020-12-02 14:27:10 +00:00
|
|
|
return true;
|
|
|
|
});
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (outputPath.ends_with(".stage")) {
|
2021-02-11 20:46:11 +00:00
|
|
|
return compileProgram(
|
2021-02-08 18:49:53 +00:00
|
|
|
[](SkSL::Compiler&, SkSL::Program& program, SkSL::OutputStream& out) {
|
|
|
|
class Callbacks : public SkSL::PipelineStage::Callbacks {
|
|
|
|
public:
|
|
|
|
using String = SkSL::String;
|
|
|
|
|
2021-02-17 20:41:57 +00:00
|
|
|
String getMangledName(const char* name) override {
|
|
|
|
return String(name) + "_0";
|
|
|
|
}
|
|
|
|
|
2021-02-08 18:49:53 +00:00
|
|
|
String declareUniform(const SkSL::VarDeclaration* decl) override {
|
|
|
|
fOutput += decl->description();
|
2021-06-10 15:21:59 +00:00
|
|
|
return String(decl->var().name());
|
2021-02-08 18:49:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 21:52:53 +00:00
|
|
|
void defineFunction(const char* decl,
|
|
|
|
const char* body,
|
|
|
|
bool /*isMain*/) override {
|
|
|
|
fOutput += String(decl) + "{" + body + "}";
|
2021-02-08 18:49:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 19:20:44 +00:00
|
|
|
void declareFunction(const char* decl) override {
|
|
|
|
fOutput += String(decl) + ";";
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:19:27 +00:00
|
|
|
void defineStruct(const char* definition) override {
|
|
|
|
fOutput += definition;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:39:06 +00:00
|
|
|
void declareGlobal(const char* declaration) override {
|
|
|
|
fOutput += declaration;
|
|
|
|
}
|
|
|
|
|
2021-07-23 19:50:39 +00:00
|
|
|
String sampleShader(int index, String coords) override {
|
2021-09-02 13:26:27 +00:00
|
|
|
return "child_" + SkSL::to_string(index) + ".eval(" + coords + ")";
|
2021-07-23 19:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String sampleColorFilter(int index, String color) override {
|
2021-09-02 13:26:27 +00:00
|
|
|
return "child_" + SkSL::to_string(index) + ".eval(" + color + ")";
|
2021-02-08 18:49:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 19:51:05 +00:00
|
|
|
String sampleBlender(int index, String src, String dst) override {
|
2021-09-02 13:26:27 +00:00
|
|
|
return "child_" + SkSL::to_string(index) + ".eval(" + src + ", " +
|
2021-07-23 19:51:05 +00:00
|
|
|
dst + ")";
|
|
|
|
}
|
|
|
|
|
Reland "Better first-class shader & color filter support in runtime effects"
This is a reland of adadb95a9f1ef21ccc5264c7d0bdc83b56cf91e9
... adds a temporary workaround for some Android framework code.
Original change's description:
> Better first-class shader & color filter support in runtime effects
>
> This does a few things, because they're all intertwined:
>
> 1) SkRuntimeEffect's API now includes details about children (which Skia
> stage was declared, not just the name). The factories verify that the
> declared types in the SkSL match up with the C++ types being passed.
> Today, we still only support adding children of the same type, so the
> checks are simple. Once we allow mixing types, we'll be testing the
> declared type against the actual C++ type supplied for each slot.
> 2) Adds sample variants that supply the input color to the child. This
> is now the only way to invoke a colorFilter child. Internally, we
> support passing a color when invoking a child shader, but I'm not
> exposing that. It's not clearly part of the semantics of the Skia
> pipeline, and is almost never useful. It also exposes users to
> several inconsistencies (skbug.com/11942).
> 3) Because of #2, it's possible that we can't compute a reusable program
> to filter individual colors. In that case, we don't set the constant
> output for constant input optimization, and filterColor4f falls back
> to the slower base-class implementation.
>
> Bug: skia:11813 skia:11942
> Change-Id: I06c41e1b35056e486f3163a72acf6b9535d7fed4
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/401917
> Commit-Queue: Brian Osman <brianosman@google.com>
> Reviewed-by: Mike Klein <mtklein@google.com>
Bug: skia:11813 skia:11942
Change-Id: I2c31b147ed86fa8c4dddefb7066bc1d07fe0d285
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/404637
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2021-04-21 13:57:19 +00:00
|
|
|
String fOutput;
|
2021-02-08 18:49:53 +00:00
|
|
|
};
|
2021-06-09 20:57:45 +00:00
|
|
|
// The .stage output looks almost like valid SkSL, but not quite.
|
|
|
|
// The PipelineStageGenerator bridges the gap between the SkSL in `program`,
|
|
|
|
// and the C++ FP builder API (see GrSkSLFP). In that API, children don't need
|
|
|
|
// to be declared (so they don't emit declarations here). Children are sampled
|
|
|
|
// by index, not name - so all children here are just "child_N".
|
|
|
|
// The input color and coords have names in the original SkSL (as parameters to
|
|
|
|
// main), but those are ignored here. References to those variables become
|
|
|
|
// "_coords" and "_inColor". At runtime, those variable names are irrelevant
|
|
|
|
// when the new SkSL is emitted inside the FP - references to those variables
|
|
|
|
// are replaced with strings from EmitArgs, and might be varyings or differently
|
|
|
|
// named parameters.
|
2021-02-08 18:49:53 +00:00
|
|
|
Callbacks callbacks;
|
2021-06-09 21:24:31 +00:00
|
|
|
SkSL::PipelineStage::ConvertProgram(program, "_coords", "_inColor",
|
|
|
|
"_canvasColor", &callbacks);
|
2021-02-08 18:49:53 +00:00
|
|
|
out.writeString(GrShaderUtils::PrettyPrint(callbacks.fOutput));
|
|
|
|
return true;
|
|
|
|
});
|
2021-06-10 15:21:59 +00:00
|
|
|
} else if (outputPath.ends_with(".dehydrated.sksl")) {
|
2020-11-11 22:29:28 +00:00
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +00:00
|
|
|
SkSL::Compiler compiler(caps);
|
2020-07-28 18:46:53 +00:00
|
|
|
if (!out.isValid()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kOutputError;
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
2021-02-25 15:35:49 +00:00
|
|
|
SkSL::LoadedModule module =
|
|
|
|
compiler.loadModule(kind, SkSL::Compiler::MakeModulePath(inputPath.c_str()),
|
|
|
|
/*base=*/nullptr, /*dehydrate=*/true);
|
2020-07-28 18:46:53 +00:00
|
|
|
SkSL::Dehydrator dehydrator;
|
2020-11-18 20:38:39 +00:00
|
|
|
dehydrator.write(*module.fSymbols);
|
|
|
|
dehydrator.write(module.fElements);
|
2020-11-11 22:29:28 +00:00
|
|
|
SkSL::String baseName = base_name(inputPath, "", ".sksl");
|
2020-07-28 18:46:53 +00:00
|
|
|
SkSL::StringStream buffer;
|
|
|
|
dehydrator.finish(buffer);
|
|
|
|
const SkSL::String& data = buffer.str();
|
2020-10-08 17:56:46 +00:00
|
|
|
out.printf("static uint8_t SKSL_INCLUDE_%s[] = {", baseName.c_str());
|
2020-07-28 18:46:53 +00:00
|
|
|
for (size_t i = 0; i < data.length(); ++i) {
|
2020-10-08 17:56:46 +00:00
|
|
|
out.printf("%s%d,", dehydrator.prefixAtOffset(i), uint8_t(data[i]));
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
|
|
|
out.printf("};\n");
|
2020-10-08 17:56:46 +00:00
|
|
|
out.printf("static constexpr size_t SKSL_INCLUDE_%s_LENGTH = sizeof(SKSL_INCLUDE_%s);\n",
|
|
|
|
baseName.c_str(), baseName.c_str());
|
2020-07-28 18:46:53 +00:00
|
|
|
if (!out.close()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kOutputError;
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
} else {
|
2021-02-25 15:35:49 +00:00
|
|
|
printf("expected output path to end with one of: .glsl, .metal, .spirv, .asm.frag, .skvm, "
|
2021-08-27 15:21:12 +00:00
|
|
|
".stage, .asm.vert (got '%s')\n", outputPath.c_str());
|
2020-11-20 21:28:50 +00:00
|
|
|
return ResultCode::kConfigurationError;
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kSuccess;
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:04:33 +00:00
|
|
|
/**
|
|
|
|
* Processes multiple inputs in a single invocation of skslc.
|
|
|
|
*/
|
2020-11-19 23:54:33 +00:00
|
|
|
ResultCode processWorklist(const char* worklistPath) {
|
2020-11-18 17:04:33 +00:00
|
|
|
SkSL::String inputPath(worklistPath);
|
2021-06-10 15:21:59 +00:00
|
|
|
if (!inputPath.ends_with(".worklist")) {
|
2020-11-18 17:04:33 +00:00
|
|
|
printf("expected .worklist file, found: %s\n\n", worklistPath);
|
|
|
|
show_usage();
|
2020-11-20 21:28:50 +00:00
|
|
|
return ResultCode::kConfigurationError;
|
2020-11-18 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The worklist contains one line per argument to pass to skslc. When a blank line is reached,
|
|
|
|
// those arguments will be passed to `processCommand`.
|
2020-11-19 23:54:33 +00:00
|
|
|
auto resultCode = ResultCode::kSuccess;
|
2020-11-18 17:04:33 +00:00
|
|
|
std::vector<SkSL::String> args = {"skslc"};
|
|
|
|
std::ifstream in(worklistPath);
|
|
|
|
for (SkSL::String line; std::getline(in, line); ) {
|
|
|
|
if (in.rdstate()) {
|
|
|
|
printf("error reading '%s'\n", worklistPath);
|
2020-11-19 23:54:33 +00:00
|
|
|
return ResultCode::kInputError;
|
2020-11-18 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!line.empty()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
// We found an argument. Remember it.
|
2020-11-18 17:04:33 +00:00
|
|
|
args.push_back(std::move(line));
|
2020-11-11 22:29:28 +00:00
|
|
|
} else {
|
2020-11-18 17:04:33 +00:00
|
|
|
// We found a blank line. If we have any arguments stored up, process them as a command.
|
|
|
|
if (!args.empty()) {
|
2020-11-19 23:54:33 +00:00
|
|
|
ResultCode outcome = processCommand(args);
|
|
|
|
resultCode = std::max(resultCode, outcome);
|
2020-11-11 22:29:28 +00:00
|
|
|
|
|
|
|
// Clear every argument except the first ("skslc").
|
|
|
|
args.resize(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:04:33 +00:00
|
|
|
// If the worklist ended with a list of arguments but no blank line, process those now.
|
2020-11-11 22:29:28 +00:00
|
|
|
if (args.size() > 1) {
|
2020-11-19 23:54:33 +00:00
|
|
|
ResultCode outcome = processCommand(args);
|
|
|
|
resultCode = std::max(resultCode, outcome);
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 23:54:33 +00:00
|
|
|
// Return the "worst" status we encountered. For our purposes, compilation errors are the least
|
|
|
|
// serious, because they are expected to occur in unit tests. Other types of errors are not
|
|
|
|
// expected at all during a build.
|
|
|
|
return resultCode;
|
2020-11-18 17:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char** argv) {
|
|
|
|
if (argc == 2) {
|
|
|
|
// Worklists are the only two-argument case for skslc, and we don't intend to support
|
|
|
|
// nested worklists, so we can process them here.
|
2020-11-19 23:54:33 +00:00
|
|
|
return (int)processWorklist(argv[1]);
|
2020-11-18 17:04:33 +00:00
|
|
|
} else {
|
|
|
|
// Process non-worklist inputs.
|
|
|
|
std::vector<SkSL::String> args;
|
|
|
|
for (int index=0; index<argc; ++index) {
|
|
|
|
args.push_back(argv[index]);
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:54:33 +00:00
|
|
|
return (int)processCommand(args);
|
2020-11-18 17:04:33 +00:00
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|