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
|
|
|
|
#include "src/opts/SkChecksum_opts.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/SkSLIRGenerator.h"
|
|
|
|
#include "src/sksl/SkSLStringStream.h"
|
2020-09-15 19:37:24 +00:00
|
|
|
#include "src/sksl/SkSLUtil.h"
|
2020-07-28 18:46:53 +00:00
|
|
|
#include "src/sksl/ir/SkSLEnum.h"
|
|
|
|
#include "src/sksl/ir/SkSLUnresolvedFunction.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2020-10-05 18:18:49 +00:00
|
|
|
#include <fstream>
|
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-11-13 20:52:07 +00:00
|
|
|
enum class ResultCode {
|
|
|
|
kSuccess = 0,
|
|
|
|
kCompileError = 1,
|
|
|
|
kInputError = 2,
|
|
|
|
kOutputError = 3,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
2020-10-08 22:39:08 +00:00
|
|
|
if (settingsText.consumeSuffix(" BlendModesFailRandomlyForAllZeroVec")) {
|
|
|
|
static auto s_blendZeroCaps = Factory::BlendModesFailRandomlyForAllZeroVec();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_blendZeroCaps.get();
|
2020-10-08 22:39:08 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-09-18 18:42:58 +00:00
|
|
|
if (settingsText.consumeSuffix(" FragCoordsOld")) {
|
|
|
|
static auto s_fragCoordsOld = Factory::FragCoordsOld();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_fragCoordsOld.get();
|
2020-09-18 18:42:58 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" FragCoordsNew")) {
|
|
|
|
static auto s_fragCoordsNew = Factory::FragCoordsNew();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_fragCoordsNew.get();
|
2020-09-18 18:42:58 +00:00
|
|
|
}
|
2020-09-18 16:04:04 +00:00
|
|
|
if (settingsText.consumeSuffix(" GeometryShaderExtensionString")) {
|
|
|
|
static auto s_geometryExtCaps = Factory::GeometryShaderExtensionString();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_geometryExtCaps.get();
|
2020-09-18 16:04:04 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" GeometryShaderSupport")) {
|
|
|
|
static auto s_geometryShaderCaps = Factory::GeometryShaderSupport();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_geometryShaderCaps.get();
|
2020-09-18 16:04:04 +00:00
|
|
|
}
|
|
|
|
if (settingsText.consumeSuffix(" GSInvocationsExtensionString")) {
|
|
|
|
static auto s_gsInvocationCaps = Factory::GSInvocationsExtensionString();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_gsInvocationCaps.get();
|
2020-09-18 16:04:04 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-09-18 16:04:04 +00:00
|
|
|
if (settingsText.consumeSuffix(" NoGSInvocationsSupport")) {
|
|
|
|
static auto s_noGSInvocations = Factory::NoGSInvocationsSupport();
|
2020-11-02 17:26:22 +00:00
|
|
|
*caps = s_noGSInvocations.get();
|
2020-09-18 16:04:04 +00:00
|
|
|
}
|
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
|
|
|
}
|
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
|
|
|
}
|
2020-09-18 16:03:42 +00:00
|
|
|
if (settingsText.consumeSuffix(" FlipY")) {
|
|
|
|
settings->fFlipY = true;
|
|
|
|
}
|
2020-09-16 21:43:11 +00:00
|
|
|
if (settingsText.consumeSuffix(" ForceHighPrecision")) {
|
|
|
|
settings->fForceHighPrecision = true;
|
|
|
|
}
|
2020-10-12 16:33:27 +00:00
|
|
|
if (settingsText.consumeSuffix(" NoInline")) {
|
|
|
|
settings->fInlineThreshold = 0;
|
|
|
|
}
|
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-13 20:52:07 +00:00
|
|
|
printf("usage: skslc <input> <output> <flags> -- <input2> <output2> <flags> -- ...\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-13 20:52:07 +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-13 20:52:07 +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-13 20:52:07 +00:00
|
|
|
return ResultCode::kInputError;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|
2020-09-16 21:46:37 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
SkSL::Program::Kind kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
const SkSL::String& inputPath = args[1];
|
|
|
|
if (inputPath.endsWith(".vert")) {
|
2016-07-01 15:22:01 +00:00
|
|
|
kind = SkSL::Program::kVertex_Kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (inputPath.endsWith(".frag") || inputPath.endsWith(".sksl")) {
|
2016-07-01 15:22:01 +00:00
|
|
|
kind = SkSL::Program::kFragment_Kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (inputPath.endsWith(".geom")) {
|
2017-02-16 21:37:32 +00:00
|
|
|
kind = SkSL::Program::kGeometry_Kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (inputPath.endsWith(".fp")) {
|
2017-06-29 14:03:38 +00:00
|
|
|
kind = SkSL::Program::kFragmentProcessor_Kind;
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (inputPath.endsWith(".stage")) {
|
2018-07-31 13:44:36 +00:00
|
|
|
kind = SkSL::Program::kPipelineStage_Kind;
|
2016-07-01 15:22:01 +00:00
|
|
|
} else {
|
2020-07-28 18:46:53 +00:00
|
|
|
printf("input filename must end in '.vert', '.frag', '.geom', '.fp', '.stage', or "
|
|
|
|
"'.sksl'\n");
|
2020-11-13 20:52:07 +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-13 20:52:07 +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;
|
2020-11-02 17:26:22 +00:00
|
|
|
const SkSL::ShaderCapsClass* caps = &SkSL::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-13 20:52:07 +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
|
|
|
|
|
|
|
const SkSL::String& outputPath = args[2];
|
|
|
|
auto emitCompileError = [&](SkSL::FileOutputStream& out, const char* errorText) {
|
2020-11-13 20:52:07 +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
|
|
|
};
|
|
|
|
|
|
|
|
if (outputPath.endsWith(".spirv")) {
|
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +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-13 20:52:07 +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);
|
|
|
|
if (!program || !compiler.toSPIRV(*program, out)) {
|
2020-11-11 22:29:28 +00:00
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-13 20:52:07 +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-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-03-31 17:56:23 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (outputPath.endsWith(".glsl")) {
|
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +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-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2016-10-12 13:39:56 +00:00
|
|
|
}
|
2016-12-12 20:33:30 +00:00
|
|
|
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
|
|
|
|
if (!program || !compiler.toGLSL(*program, out)) {
|
2020-11-11 22:29:28 +00:00
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-13 20:52:07 +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-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-03-31 17:56:23 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (outputPath.endsWith(".metal")) {
|
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +00:00
|
|
|
SkSL::Compiler compiler(caps);
|
2017-10-13 20:17:45 +00:00
|
|
|
if (!out.isValid()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-10-13 20:17:45 +00:00
|
|
|
}
|
|
|
|
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
|
|
|
|
if (!program || !compiler.toMetal(*program, out)) {
|
2020-11-11 22:29:28 +00:00
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kCompileError;
|
2017-10-13 20:17:45 +00:00
|
|
|
}
|
|
|
|
if (!out.close()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-10-13 20:17:45 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (outputPath.endsWith(".h")) {
|
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +00:00
|
|
|
SkSL::Compiler compiler(caps, SkSL::Compiler::kPermitInvalidStaticTests_Flag);
|
2017-06-29 14:03:38 +00:00
|
|
|
if (!out.isValid()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
|
|
|
settings.fReplaceSettings = false;
|
|
|
|
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
|
2020-11-11 22:29:28 +00:00
|
|
|
if (!program || !compiler.toH(*program, base_name(inputPath.c_str(), "Gr", ".fp"), out)) {
|
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kCompileError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
|
|
|
if (!out.close()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (outputPath.endsWith(".cpp")) {
|
|
|
|
SkSL::FileOutputStream out(outputPath);
|
2020-11-02 17:26:22 +00:00
|
|
|
SkSL::Compiler compiler(caps, SkSL::Compiler::kPermitInvalidStaticTests_Flag);
|
2017-06-29 14:03:38 +00:00
|
|
|
if (!out.isValid()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
|
|
|
settings.fReplaceSettings = false;
|
|
|
|
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
|
2020-11-11 22:29:28 +00:00
|
|
|
if (!program || !compiler.toCPP(*program, base_name(inputPath.c_str(), "Gr", ".fp"), out)) {
|
|
|
|
emitCompileError(out, compiler.errorText().c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kCompileError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
|
|
|
if (!out.close()) {
|
2020-11-11 22:29:28 +00:00
|
|
|
printf("error writing '%s'\n", outputPath.c_str());
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2017-06-29 14:03:38 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
} else if (outputPath.endsWith(".dehydrated.sksl")) {
|
|
|
|
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-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
2020-11-11 22:29:28 +00:00
|
|
|
auto [symbols, elements] = compiler.loadModule(
|
|
|
|
kind, SkSL::Compiler::MakeModulePath(inputPath.c_str()), nullptr);
|
2020-07-28 18:46:53 +00:00
|
|
|
SkSL::Dehydrator dehydrator;
|
|
|
|
dehydrator.write(*symbols);
|
|
|
|
dehydrator.write(elements);
|
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-13 20:52:07 +00:00
|
|
|
return ResultCode::kOutputError;
|
2020-07-28 18:46:53 +00:00
|
|
|
}
|
2016-10-12 13:39:56 +00:00
|
|
|
} else {
|
2017-10-13 20:17:45 +00:00
|
|
|
printf("expected output filename to end with '.spirv', '.glsl', '.cpp', '.h', or '.metal'");
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kInputError; // the "output filename" is still an input argument
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
2020-11-13 20:52:07 +00:00
|
|
|
return ResultCode::kSuccess;
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:52:07 +00:00
|
|
|
int main(int argc, const char** argv) {
|
|
|
|
// Search the command line for -- delimiters. When a -- is reached, we process one command.
|
|
|
|
std::vector<SkSL::String> args = {argv[0]};
|
|
|
|
auto resultCode = ResultCode::kSuccess;
|
|
|
|
for (int index = 1; index < argc; ++index) {
|
|
|
|
SkSL::String arg = argv[index];
|
|
|
|
if (arg != "--") {
|
2020-11-11 22:29:28 +00:00
|
|
|
// We found an argument. Remember it.
|
2020-11-13 20:52:07 +00:00
|
|
|
args.push_back(std::move(arg));
|
2020-11-11 22:29:28 +00:00
|
|
|
} else {
|
2020-11-13 20:52:07 +00:00
|
|
|
// We found a delimiter. If we have any arguments stored up, process them as a command.
|
|
|
|
if (args.size() > 1) {
|
|
|
|
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-13 20:52:07 +00:00
|
|
|
// Execute the final command in the batch.
|
2020-11-11 22:29:28 +00:00
|
|
|
if (args.size() > 1) {
|
2020-11-13 20:52:07 +00:00
|
|
|
ResultCode outcome = processCommand(args);
|
|
|
|
resultCode = std::max(resultCode, outcome);
|
2020-11-11 22:29:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:52:07 +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 (int) resultCode;
|
2016-07-01 15:22:01 +00:00
|
|
|
}
|