added sk_FragCoord support to skslc

BUG=skia:

Change-Id: If78a4d08121699f87659f0d2e35f3edbf1867401
Reviewed-on: https://skia-review.googlesource.com/5408
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2016-12-09 17:22:59 -05:00 committed by Skia Commit-Bot
parent f44703a87f
commit ce33f10677
33 changed files with 641 additions and 402 deletions

View File

@ -68,13 +68,14 @@ void GLBench::onDraw(int loops, SkCanvas* canvas) {
GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, GrGLenum type) { GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, GrGLenum type) {
const GrGLInterface* gl = context->interface(); const GrGLInterface* gl = context->interface();
SkString glsl; SkString glsl;
bool result = context->compiler()->toGLSL(type == GR_GL_VERTEX_SHADER SkSL::Program::Settings settings;
? SkSL::Program::kVertex_Kind settings.fCaps = context->caps()->shaderCaps();
std::unique_ptr<SkSL::Program> program = context->compiler()->convertProgram(
type == GR_GL_VERTEX_SHADER ? SkSL::Program::kVertex_Kind
: SkSL::Program::kFragment_Kind, : SkSL::Program::kFragment_Kind,
SkString(sksl), SkString(sksl),
*context->caps()->shaderCaps(), settings);
&glsl); if (!program || !context->compiler()->toGLSL(*program, &glsl)) {
if (!result) {
SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl, SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl,
context->compiler()->errorText().c_str()); context->compiler()->errorText().c_str());
} }

View File

@ -444,10 +444,13 @@ int fuzz_color_deserialize(sk_sp<SkData> bytes) {
int fuzz_sksl2glsl(sk_sp<SkData> bytes) { int fuzz_sksl2glsl(sk_sp<SkData> bytes) {
SkSL::Compiler compiler; SkSL::Compiler compiler;
SkString output; SkString output;
bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, SkSL::Program::Settings settings;
SkString((const char*)bytes->data()), *SkSL::ShaderCapsFactory::Default(), &output); sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
if (!result) { std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
SkString((const char*) bytes->data()),
settings);
if (!program || !compiler.toGLSL(*program, &output)) {
SkDebugf("[terminated] Couldn't compile input.\n"); SkDebugf("[terminated] Couldn't compile input.\n");
return 1; return 1;
} }

View File

@ -27,6 +27,7 @@
#include "SkMipMap.h" #include "SkMipMap.h"
#include "SkPixmap.h" #include "SkPixmap.h"
#include "SkStrokeRec.h" #include "SkStrokeRec.h"
#include "SkSLCompiler.h"
#include "SkTemplates.h" #include "SkTemplates.h"
#include "SkTypes.h" #include "SkTypes.h"
#include "../private/GrGLSL.h" #include "../private/GrGLSL.h"
@ -399,13 +400,20 @@ bool GrGLGpu::createPLSSetupProgram() {
str = vshaderTxt.c_str(); str = vshaderTxt.c_str();
length = SkToInt(vshaderTxt.size()); length = SkToInt(vshaderTxt.size());
SkSL::Program::Settings settings;
settings.fCaps = shaderCaps;
SkSL::Program::Inputs inputs;
GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram, GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats); GR_GL_VERTEX_SHADER, &str, &length, 1, &fStats,
settings, &inputs);
SkASSERT(inputs.isEmpty());
str = fshaderTxt.c_str(); str = fshaderTxt.c_str();
length = SkToInt(fshaderTxt.size()); length = SkToInt(fshaderTxt.size());
GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram, GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fPLSSetupProgram.fProgram,
GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats); GR_GL_FRAGMENT_SHADER, &str, &length, 1, &fStats,
settings, &inputs);
SkASSERT(inputs.isEmpty());
GL_CALL(LinkProgram(fPLSSetupProgram.fProgram)); GL_CALL(LinkProgram(fPLSSetupProgram.fProgram));
@ -3855,15 +3863,20 @@ bool GrGLGpu::createCopyProgram(GrTexture* srcTex) {
str = vshaderTxt.c_str(); str = vshaderTxt.c_str();
length = SkToInt(vshaderTxt.size()); length = SkToInt(vshaderTxt.size());
SkSL::Program::Settings settings;
settings.fCaps = shaderCaps;
SkSL::Program::Inputs inputs;
GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
GR_GL_VERTEX_SHADER, &str, &length, 1, GR_GL_VERTEX_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
str = fshaderTxt.c_str(); str = fshaderTxt.c_str();
length = SkToInt(fshaderTxt.size()); length = SkToInt(fshaderTxt.size());
GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram, GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fCopyPrograms[progIdx].fProgram,
GR_GL_FRAGMENT_SHADER, &str, &length, 1, GR_GL_FRAGMENT_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram)); GL_CALL(LinkProgram(fCopyPrograms[progIdx].fProgram));
@ -4008,15 +4021,20 @@ bool GrGLGpu::createMipmapProgram(int progIdx) {
str = vshaderTxt.c_str(); str = vshaderTxt.c_str();
length = SkToInt(vshaderTxt.size()); length = SkToInt(vshaderTxt.size());
SkSL::Program::Settings settings;
settings.fCaps = shaderCaps;
SkSL::Program::Inputs inputs;
GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
GR_GL_VERTEX_SHADER, &str, &length, 1, GR_GL_VERTEX_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
str = fshaderTxt.c_str(); str = fshaderTxt.c_str();
length = SkToInt(fshaderTxt.size()); length = SkToInt(fshaderTxt.size());
GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram, GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fMipmapPrograms[progIdx].fProgram,
GR_GL_FRAGMENT_SHADER, &str, &length, 1, GR_GL_FRAGMENT_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram)); GL_CALL(LinkProgram(fMipmapPrograms[progIdx].fProgram));
@ -4097,15 +4115,20 @@ bool GrGLGpu::createWireRectProgram() {
str = vshaderTxt.c_str(); str = vshaderTxt.c_str();
length = SkToInt(vshaderTxt.size()); length = SkToInt(vshaderTxt.size());
SkSL::Program::Settings settings;
settings.fCaps = this->caps()->shaderCaps();
SkSL::Program::Inputs inputs;
GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram, GrGLuint vshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
GR_GL_VERTEX_SHADER, &str, &length, 1, GR_GL_VERTEX_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
str = fshaderTxt.c_str(); str = fshaderTxt.c_str();
length = SkToInt(fshaderTxt.size()); length = SkToInt(fshaderTxt.size());
GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram, GrGLuint fshader = GrGLCompileAndAttachShader(*fGLContext, fWireRectProgram.fProgram,
GR_GL_FRAGMENT_SHADER, &str, &length, 1, GR_GL_FRAGMENT_SHADER, &str, &length, 1,
&fStats); &fStats, settings, &inputs);
SkASSERT(inputs.isEmpty());
GL_CALL(LinkProgram(fWireRectProgram.fProgram)); GL_CALL(LinkProgram(fWireRectProgram.fProgram));

View File

@ -69,7 +69,9 @@ const GrCaps* GrGLProgramBuilder::caps() const {
bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader, bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader,
GrGLuint programId, GrGLuint programId,
GrGLenum type, GrGLenum type,
SkTDArray<GrGLuint>* shaderIds) { SkTDArray<GrGLuint>* shaderIds,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* outInputs) {
GrGLGpu* gpu = this->gpu(); GrGLGpu* gpu = this->gpu();
GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(), GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
programId, programId,
@ -77,7 +79,9 @@ bool GrGLProgramBuilder::compileAndAttachShaders(GrGLSLShaderBuilder& shader,
shader.fCompilerStrings.begin(), shader.fCompilerStrings.begin(),
shader.fCompilerStringLengths.begin(), shader.fCompilerStringLengths.begin(),
shader.fCompilerStrings.count(), shader.fCompilerStrings.count(),
gpu->stats()); gpu->stats(),
settings,
outInputs);
if (!shaderId) { if (!shaderId) {
return false; return false;
@ -100,8 +104,13 @@ GrGLProgram* GrGLProgramBuilder::finalize() {
this->finalizeShaders(); this->finalizeShaders();
// compile shaders and bind attributes / uniforms // compile shaders and bind attributes / uniforms
SkSL::Program::Settings settings;
settings.fCaps = this->gpu()->glCaps().shaderCaps();
settings.fFlipY = this->pipeline().getRenderTarget()->origin() != kTopLeft_GrSurfaceOrigin;
SkSL::Program::Inputs inputs;
SkTDArray<GrGLuint> shadersToDelete; SkTDArray<GrGLuint> shadersToDelete;
if (!this->compileAndAttachShaders(fVS, programID, GR_GL_VERTEX_SHADER, &shadersToDelete)) { if (!this->compileAndAttachShaders(fVS, programID, GR_GL_VERTEX_SHADER, &shadersToDelete,
settings, &inputs)) {
this->cleanupProgram(programID, shadersToDelete); this->cleanupProgram(programID, shadersToDelete);
return nullptr; return nullptr;
} }
@ -117,16 +126,22 @@ GrGLProgram* GrGLProgramBuilder::finalize() {
} }
if (primProc.willUseGeoShader() && if (primProc.willUseGeoShader() &&
!this->compileAndAttachShaders(fGS, programID, GR_GL_GEOMETRY_SHADER, &shadersToDelete)) { !this->compileAndAttachShaders(fGS, programID, GR_GL_GEOMETRY_SHADER, &shadersToDelete,
settings, &inputs)) {
this->cleanupProgram(programID, shadersToDelete); this->cleanupProgram(programID, shadersToDelete);
return nullptr; return nullptr;
} }
if (!this->compileAndAttachShaders(fFS, programID, GR_GL_FRAGMENT_SHADER, &shadersToDelete)) { if (!this->compileAndAttachShaders(fFS, programID, GR_GL_FRAGMENT_SHADER, &shadersToDelete,
settings, &inputs)) {
this->cleanupProgram(programID, shadersToDelete); this->cleanupProgram(programID, shadersToDelete);
return nullptr; return nullptr;
} }
if (inputs.fRTHeight) {
this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
}
this->bindProgramResourceLocations(programID); this->bindProgramResourceLocations(programID);
GL_CALL(LinkProgram(programID)); GL_CALL(LinkProgram(programID));

View File

@ -14,6 +14,7 @@
#include "gl/GrGLVaryingHandler.h" #include "gl/GrGLVaryingHandler.h"
#include "glsl/GrGLSLProgramBuilder.h" #include "glsl/GrGLSLProgramBuilder.h"
#include "glsl/GrGLSLProgramDataManager.h" #include "glsl/GrGLSLProgramDataManager.h"
#include "ir/SkSLProgram.h"
class GrFragmentProcessor; class GrFragmentProcessor;
class GrGLContextInfo; class GrGLContextInfo;
@ -46,7 +47,9 @@ private:
bool compileAndAttachShaders(GrGLSLShaderBuilder& shader, bool compileAndAttachShaders(GrGLSLShaderBuilder& shader,
GrGLuint programId, GrGLuint programId,
GrGLenum type, GrGLenum type,
SkTDArray<GrGLuint>* shaderIds); SkTDArray<GrGLuint>* shaderIds,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* outInputs);
GrGLProgram* finalize(); GrGLProgram* finalize();
void bindProgramResourceLocations(GrGLuint programID); void bindProgramResourceLocations(GrGLuint programID);
bool checkLinkStatus(GrGLuint programID); bool checkLinkStatus(GrGLuint programID);

View File

@ -44,7 +44,9 @@ GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx,
const char** strings, const char** strings,
int* lengths, int* lengths,
int count, int count,
GrGpu::Stats* stats) { GrGpu::Stats* stats,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* outInputs) {
const GrGLInterface* gli = glCtx.interface(); const GrGLInterface* gli = glCtx.interface();
GrGLuint shaderId; GrGLuint shaderId;
@ -65,21 +67,20 @@ GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx,
SkString glsl; SkString glsl;
if (type == GR_GL_VERTEX_SHADER || type == GR_GL_FRAGMENT_SHADER) { if (type == GR_GL_VERTEX_SHADER || type == GR_GL_FRAGMENT_SHADER) {
SkSL::Compiler& compiler = *glCtx.compiler(); SkSL::Compiler& compiler = *glCtx.compiler();
SkDEBUGCODE(bool result = )compiler.toGLSL(type == GR_GL_VERTEX_SHADER std::unique_ptr<SkSL::Program> program;
? SkSL::Program::kVertex_Kind program = compiler.convertProgram(
type == GR_GL_VERTEX_SHADER ? SkSL::Program::kVertex_Kind
: SkSL::Program::kFragment_Kind, : SkSL::Program::kFragment_Kind,
sksl, sksl,
*glCtx.caps()->shaderCaps(), settings);
&glsl); if (!program || !compiler.toGLSL(*program, &glsl)) {
#ifdef SK_DEBUG
if (!result) {
SkDebugf("SKSL compilation error\n----------------------\n"); SkDebugf("SKSL compilation error\n----------------------\n");
SkDebugf("SKSL:\n"); SkDebugf("SKSL:\n");
dump_string(sksl); dump_string(sksl);
SkDebugf("\nErrors:\n%s\n", compiler.errorText().c_str()); SkDebugf("\nErrors:\n%s\n", compiler.errorText().c_str());
SkDEBUGFAIL("SKSL compilation failed!\n"); SkDEBUGFAIL("SKSL compilation failed!\n");
} }
#endif *outInputs = program->fInputs;
} else { } else {
// TODO: geometry shader support in sksl. // TODO: geometry shader support in sksl.
SkASSERT(type == GR_GL_GEOMETRY_SHADER); SkASSERT(type == GR_GL_GEOMETRY_SHADER);

View File

@ -20,6 +20,8 @@ GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx,
const char** strings, const char** strings,
int* lengths, int* lengths,
int count, int count,
GrGpu::Stats*); GrGpu::Stats*,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* inputs);
#endif #endif

View File

@ -132,49 +132,7 @@ SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords)
const char* GrGLSLFragmentShaderBuilder::fragmentPosition() { const char* GrGLSLFragmentShaderBuilder::fragmentPosition() {
SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;) SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;)
return "sk_FragCoord";
const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
// We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
// to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
// declaration varies in earlier GLSL specs. So it is simpler to omit it.
if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
fSetupFragPosition = true;
return "gl_FragCoord";
} else if (const char* extension = shaderCaps->fragCoordConventionsExtensionString()) {
if (!fSetupFragPosition) {
if (shaderCaps->generation() < k150_GrGLSLGeneration) {
this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature,
extension);
}
fInputs.push_back().set(kVec4f_GrSLType,
"gl_FragCoord",
GrShaderVar::kIn_TypeModifier,
kDefault_GrSLPrecision,
"origin_upper_left");
fSetupFragPosition = true;
}
return "gl_FragCoord";
} else {
static const char* kTempName = "tmpXYFragCoord";
static const char* kCoordName = "fragCoordYDown";
if (!fSetupFragPosition) {
const char* rtHeightName;
fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName);
// The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
// Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
// depending on the surrounding code, accessing .xy with a uniform involved can
// do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand
// (and only accessing .xy) seems to "fix" things.
this->codePrependf("\thighp vec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n", kCoordName,
kTempName, rtHeightName, kTempName);
this->codePrependf("highp vec2 %s = gl_FragCoord.xy;", kTempName);
fSetupFragPosition = true;
}
SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid());
return kCoordName;
}
} }
const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const { const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const {

View File

@ -470,13 +470,13 @@ void GrGLSLProgramBuilder::addRTAdjustmentUniform(GrSLPrecision precision,
outName); outName);
} }
void GrGLSLProgramBuilder::addRTHeightUniform(const char* name, const char** outName) { void GrGLSLProgramBuilder::addRTHeightUniform(const char* name) {
SkASSERT(!fUniformHandles.fRTHeightUni.isValid()); SkASSERT(!fUniformHandles.fRTHeightUni.isValid());
GrGLSLUniformHandler* uniformHandler = this->uniformHandler(); GrGLSLUniformHandler* uniformHandler = this->uniformHandler();
fUniformHandles.fRTHeightUni = fUniformHandles.fRTHeightUni =
uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag, uniformHandler->internalAddUniformArray(kFragment_GrShaderFlag,
kFloat_GrSLType, kDefault_GrSLPrecision, kFloat_GrSLType, kDefault_GrSLPrecision,
name, false, 0, outName); name, false, 0, nullptr);
} }
void GrGLSLProgramBuilder::cleanupFragmentProcessors() { void GrGLSLProgramBuilder::cleanupFragmentProcessors() {

View File

@ -72,7 +72,7 @@ public:
// Used to add a uniform for the RenderTarget height (used for frag position) without mangling // Used to add a uniform for the RenderTarget height (used for frag position) without mangling
// the name of the uniform inside of a stage. // the name of the uniform inside of a stage.
void addRTHeightUniform(const char* name, const char** outName); void addRTHeightUniform(const char* name);
// Generates a name for a variable. The generated string will be name prefixed by the prefix // Generates a name for a variable. The generated string will be name prefixed by the prefix
// char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless

View File

@ -65,19 +65,21 @@ bool GrVkCopyManager::createCopyProgram(GrVkGpu* gpu) {
"}" "}"
); );
if (!GrCompileVkShaderModule(gpu, vertShaderText.c_str(), SkSL::Program::Settings settings;
VK_SHADER_STAGE_VERTEX_BIT, SkSL::Program::Inputs inputs;
&fVertShaderModule, &fShaderStageInfo[0])) { if (!GrCompileVkShaderModule(gpu, vertShaderText.c_str(), VK_SHADER_STAGE_VERTEX_BIT,
&fVertShaderModule, &fShaderStageInfo[0], settings, &inputs)) {
this->destroyResources(gpu); this->destroyResources(gpu);
return false; return false;
} }
SkASSERT(inputs.isEmpty());
if (!GrCompileVkShaderModule(gpu, fragShaderText.c_str(), if (!GrCompileVkShaderModule(gpu, fragShaderText.c_str(), VK_SHADER_STAGE_FRAGMENT_BIT,
VK_SHADER_STAGE_FRAGMENT_BIT, &fFragShaderModule, &fShaderStageInfo[1], settings, &inputs)) {
&fFragShaderModule, &fShaderStageInfo[1])) {
this->destroyResources(gpu); this->destroyResources(gpu);
return false; return false;
} }
SkASSERT(inputs.isEmpty());
VkDescriptorSetLayout dsLayout[2]; VkDescriptorSetLayout dsLayout[2];

View File

@ -36,9 +36,7 @@
#include "vk/GrVkInterface.h" #include "vk/GrVkInterface.h"
#include "vk/GrVkTypes.h" #include "vk/GrVkTypes.h"
#if USE_SKSL
#include "SkSLCompiler.h" #include "SkSLCompiler.h"
#endif
#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
@ -119,11 +117,7 @@ GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
} }
#endif #endif
#if USE_SKSL
fCompiler = new SkSL::Compiler(); fCompiler = new SkSL::Compiler();
#else
fCompiler = shaderc_compiler_initialize();
#endif
fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice, fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice,
backendCtx->fFeatures, backendCtx->fExtensions)); backendCtx->fFeatures, backendCtx->fExtensions));
@ -193,11 +187,7 @@ GrVkGpu::~GrVkGpu() {
VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
#if USE_SKSL
delete fCompiler; delete fCompiler;
#else
shaderc_compiler_release(fCompiler);
#endif
#ifdef SK_ENABLE_VK_LAYERS #ifdef SK_ENABLE_VK_LAYERS
if (fCallback) { if (fCallback) {

View File

@ -8,8 +8,6 @@
#ifndef GrVkGpu_DEFINED #ifndef GrVkGpu_DEFINED
#define GrVkGpu_DEFINED #define GrVkGpu_DEFINED
#define USE_SKSL 1
#include "GrGpu.h" #include "GrGpu.h"
#include "GrGpuFactory.h" #include "GrGpuFactory.h"
#include "vk/GrVkBackendContext.h" #include "vk/GrVkBackendContext.h"
@ -20,15 +18,6 @@
#include "GrVkResourceProvider.h" #include "GrVkResourceProvider.h"
#include "GrVkVertexBuffer.h" #include "GrVkVertexBuffer.h"
#include "GrVkUtil.h" #include "GrVkUtil.h"
#if USE_SKSL
namespace SkSL {
class Compiler;
}
#else
#include "shaderc/shaderc.h"
#endif
#include "vk/GrVkDefines.h" #include "vk/GrVkDefines.h"
class GrPipeline; class GrPipeline;
@ -43,6 +32,10 @@ class GrVkSecondaryCommandBuffer;
class GrVkTexture; class GrVkTexture;
struct GrVkInterface; struct GrVkInterface;
namespace SkSL {
class Compiler;
}
class GrVkGpu : public GrGpu { class GrVkGpu : public GrGpu {
public: public:
static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options, static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options,
@ -120,15 +113,9 @@ public:
bool byRegion, bool byRegion,
VkImageMemoryBarrier* barrier) const; VkImageMemoryBarrier* barrier) const;
#if USE_SKSL
SkSL::Compiler* shaderCompiler() const { SkSL::Compiler* shaderCompiler() const {
return fCompiler; return fCompiler;
} }
#else
shaderc_compiler_t shadercCompiler() const {
return fCompiler;
}
#endif
void onResolveRenderTarget(GrRenderTarget* target) override; void onResolveRenderTarget(GrRenderTarget* target) override;
@ -272,13 +259,9 @@ private:
VkDebugReportCallbackEXT fCallback; VkDebugReportCallbackEXT fCallback;
#endif #endif
#if USE_SKSL // compiler used for compiling sksl into spirv. We only want to create the compiler once since
// there is significant overhead to the first compile of any compiler.
SkSL::Compiler* fCompiler; SkSL::Compiler* fCompiler;
#else
// Shaderc compiler used for compiling glsl in spirv. We only want to create the compiler once
// since there is significant overhead to the first compile of any compiler.
shaderc_compiler_t fCompiler;
#endif
typedef GrGpu INHERITED; typedef GrGpu INHERITED;
}; };

View File

@ -58,11 +58,11 @@ void GrVkPipelineStateBuilder::finalizeFragmentSecondaryColor(GrShaderVar& outpu
outputColor.addLayoutQualifier("location = 0, index = 1"); outputColor.addLayoutQualifier("location = 0, index = 1");
} }
bool GrVkPipelineStateBuilder::CreateVkShaderModule(const GrVkGpu* gpu, bool GrVkPipelineStateBuilder::createVkShaderModule(VkShaderStageFlagBits stage,
VkShaderStageFlagBits stage,
const GrGLSLShaderBuilder& builder, const GrGLSLShaderBuilder& builder,
VkShaderModule* shaderModule, VkShaderModule* shaderModule,
VkPipelineShaderStageCreateInfo* stageInfo) { VkPipelineShaderStageCreateInfo* stageInfo,
const SkSL::Program::Settings& settings) {
SkString shaderString; SkString shaderString;
for (int i = 0; i < builder.fCompilerStrings.count(); ++i) { for (int i = 0; i < builder.fCompilerStrings.count(); ++i) {
if (builder.fCompilerStrings[i]) { if (builder.fCompilerStrings[i]) {
@ -70,7 +70,17 @@ bool GrVkPipelineStateBuilder::CreateVkShaderModule(const GrVkGpu* gpu,
shaderString.append("\n"); shaderString.append("\n");
} }
} }
return GrCompileVkShaderModule(gpu, shaderString.c_str(), stage, shaderModule, stageInfo);
SkSL::Program::Inputs inputs;
bool result = GrCompileVkShaderModule(fGpu, shaderString.c_str(), stage, shaderModule,
stageInfo, settings, &inputs);
if (!result) {
return false;
}
if (inputs.fRTHeight) {
this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
}
return result;
} }
GrVkPipelineState* GrVkPipelineStateBuilder::finalize(const GrStencilSettings& stencil, GrVkPipelineState* GrVkPipelineStateBuilder::finalize(const GrStencilSettings& stencil,
@ -117,20 +127,22 @@ GrVkPipelineState* GrVkPipelineStateBuilder::finalize(const GrStencilSettings& s
this->finalizeShaders(); this->finalizeShaders();
VkPipelineShaderStageCreateInfo shaderStageInfo[2]; VkPipelineShaderStageCreateInfo shaderStageInfo[2];
SkAssertResult(CreateVkShaderModule(fGpu, SkSL::Program::Settings settings;
VK_SHADER_STAGE_VERTEX_BIT, settings.fFlipY = this->pipeline().getRenderTarget()->origin() != kTopLeft_GrSurfaceOrigin;
SkAssertResult(this->createVkShaderModule(VK_SHADER_STAGE_VERTEX_BIT,
fVS, fVS,
&vertShaderModule, &vertShaderModule,
&shaderStageInfo[0])); &shaderStageInfo[0],
settings));
// TODO: geometry shader support. // TODO: geometry shader support.
SkASSERT(!this->primitiveProcessor().willUseGeoShader()); SkASSERT(!this->primitiveProcessor().willUseGeoShader());
SkAssertResult(CreateVkShaderModule(fGpu, SkAssertResult(this->createVkShaderModule(VK_SHADER_STAGE_FRAGMENT_BIT,
VK_SHADER_STAGE_FRAGMENT_BIT,
fFS, fFS,
&fragShaderModule, &fragShaderModule,
&shaderStageInfo[1])); &shaderStageInfo[1],
settings));
GrVkPipeline* pipeline = resourceProvider.createPipeline(fPipeline, GrVkPipeline* pipeline = resourceProvider.createPipeline(fPipeline,
stencil, stencil,

View File

@ -14,6 +14,7 @@
#include "GrVkPipelineState.h" #include "GrVkPipelineState.h"
#include "GrVkUniformHandler.h" #include "GrVkUniformHandler.h"
#include "GrVkVaryingHandler.h" #include "GrVkVaryingHandler.h"
#include "SkSLCompiler.h"
#include "vk/GrVkDefines.h" #include "vk/GrVkDefines.h"
@ -55,11 +56,11 @@ private:
const GrVkRenderPass& renderPass, const GrVkRenderPass& renderPass,
const GrVkPipelineState::Desc&); const GrVkPipelineState::Desc&);
static bool CreateVkShaderModule(const GrVkGpu* gpu, bool createVkShaderModule(VkShaderStageFlagBits stage,
VkShaderStageFlagBits stage,
const GrGLSLShaderBuilder& builder, const GrGLSLShaderBuilder& builder,
VkShaderModule* shaderModule, VkShaderModule* shaderModule,
VkPipelineShaderStageCreateInfo* stageInfo); VkPipelineShaderStageCreateInfo* stageInfo,
const SkSL::Program::Settings& settings);
GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; } GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; } const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }

View File

@ -8,9 +8,7 @@
#include "GrVkUtil.h" #include "GrVkUtil.h"
#include "vk/GrVkGpu.h" #include "vk/GrVkGpu.h"
#if USE_SKSL
#include "SkSLCompiler.h" #include "SkSLCompiler.h"
#endif
bool GrPixelConfigToVkFormat(GrPixelConfig config, VkFormat* format) { bool GrPixelConfigToVkFormat(GrPixelConfig config, VkFormat* format) {
VkFormat dontCare; VkFormat dontCare;
@ -262,7 +260,6 @@ bool GrSampleCountToVkSampleCount(uint32_t samples, VkSampleCountFlagBits* vkSam
} }
} }
#if USE_SKSL
SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage) { SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage) {
if (VK_SHADER_STAGE_VERTEX_BIT == stage) { if (VK_SHADER_STAGE_VERTEX_BIT == stage) {
return SkSL::Program::kVertex_Kind; return SkSL::Program::kVertex_Kind;
@ -270,85 +267,49 @@ SkSL::Program::Kind vk_shader_stage_to_skiasl_kind(VkShaderStageFlagBits stage)
SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage);
return SkSL::Program::kFragment_Kind; return SkSL::Program::kFragment_Kind;
} }
#else
shaderc_shader_kind vk_shader_stage_to_shaderc_kind(VkShaderStageFlagBits stage) { VkShaderStageFlagBits skiasl_kind_to_vk_shader_stage(SkSL::Program::Kind kind) {
if (VK_SHADER_STAGE_VERTEX_BIT == stage) { if (SkSL::Program::kVertex_Kind == kind) {
return shaderc_glsl_vertex_shader; return VK_SHADER_STAGE_VERTEX_BIT;
} }
SkASSERT(VK_SHADER_STAGE_FRAGMENT_BIT == stage); SkASSERT(SkSL::Program::kFragment_Kind == kind);
return shaderc_glsl_fragment_shader; return VK_SHADER_STAGE_FRAGMENT_BIT;
} }
#endif
bool GrCompileVkShaderModule(const GrVkGpu* gpu, bool GrCompileVkShaderModule(const GrVkGpu* gpu,
const char* shaderString, const char* shaderString,
VkShaderStageFlagBits stage, VkShaderStageFlagBits stage,
VkShaderModule* shaderModule, VkShaderModule* shaderModule,
VkPipelineShaderStageCreateInfo* stageInfo) { VkPipelineShaderStageCreateInfo* stageInfo,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* outInputs) {
std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
vk_shader_stage_to_skiasl_kind(stage),
SkString(shaderString),
settings);
if (!program) {
SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
SkASSERT(false);
}
*outInputs = program->fInputs;
SkString code;
if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) {
SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str());
return false;
}
VkShaderModuleCreateInfo moduleCreateInfo; VkShaderModuleCreateInfo moduleCreateInfo;
memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo)); memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo));
moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleCreateInfo.pNext = nullptr; moduleCreateInfo.pNext = nullptr;
moduleCreateInfo.flags = 0; moduleCreateInfo.flags = 0;
#if USE_SKSL
SkString code;
#else
shaderc_compilation_result_t result = nullptr;
#endif
if (gpu->vkCaps().canUseGLSLForShaderModule()) {
moduleCreateInfo.codeSize = strlen(shaderString);
moduleCreateInfo.pCode = (const uint32_t*)shaderString;
} else {
#if USE_SKSL
bool result = gpu->shaderCompiler()->toSPIRV(vk_shader_stage_to_skiasl_kind(stage),
SkString(shaderString),
&code);
if (!result) {
SkDebugf("%s\n", gpu->shaderCompiler()->errorText().c_str());
return false;
}
moduleCreateInfo.codeSize = code.size(); moduleCreateInfo.codeSize = code.size();
moduleCreateInfo.pCode = (const uint32_t*)code.c_str(); moduleCreateInfo.pCode = (const uint32_t*)code.c_str();
#else
shaderc_compiler_t compiler = gpu->shadercCompiler();
shaderc_compile_options_t options = shaderc_compile_options_initialize();
shaderc_shader_kind shadercStage = vk_shader_stage_to_shaderc_kind(stage);
result = shaderc_compile_into_spv(compiler,
shaderString,
strlen(shaderString),
shadercStage,
"shader",
"main",
options);
shaderc_compile_options_release(options);
#ifdef SK_DEBUG
if (shaderc_result_get_num_errors(result)) {
SkDebugf("%s\n", shaderString);
SkDebugf("%s\n", shaderc_result_get_error_message(result));
return false;
}
#endif // SK_DEBUG
moduleCreateInfo.codeSize = shaderc_result_get_length(result);
moduleCreateInfo.pCode = (const uint32_t*)shaderc_result_get_bytes(result);
#endif // USE_SKSL
}
VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device(), VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device(),
&moduleCreateInfo, &moduleCreateInfo,
nullptr, nullptr,
shaderModule)); shaderModule));
if (!gpu->vkCaps().canUseGLSLForShaderModule()) {
#if !USE_SKSL
shaderc_result_release(result);
#endif
}
if (err) { if (err) {
return false; return false;
} }
@ -357,7 +318,7 @@ bool GrCompileVkShaderModule(const GrVkGpu* gpu,
stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stageInfo->pNext = nullptr; stageInfo->pNext = nullptr;
stageInfo->flags = 0; stageInfo->flags = 0;
stageInfo->stage = stage; stageInfo->stage = skiasl_kind_to_vk_shader_stage(program->fKind);
stageInfo->module = *shaderModule; stageInfo->module = *shaderModule;
stageInfo->pName = "main"; stageInfo->pName = "main";
stageInfo->pSpecializationInfo = nullptr; stageInfo->pSpecializationInfo = nullptr;

View File

@ -12,6 +12,7 @@
#include "GrTypes.h" #include "GrTypes.h"
#include "vk/GrVkDefines.h" #include "vk/GrVkDefines.h"
#include "vk/GrVkInterface.h" #include "vk/GrVkInterface.h"
#include "ir/SkSLProgram.h"
class GrVkGpu; class GrVkGpu;
@ -48,6 +49,8 @@ bool GrCompileVkShaderModule(const GrVkGpu* gpu,
const char* shaderString, const char* shaderString,
VkShaderStageFlagBits stage, VkShaderStageFlagBits stage,
VkShaderModule* shaderModule, VkShaderModule* shaderModule,
VkPipelineShaderStageCreateInfo* stageInfo); VkPipelineShaderStageCreateInfo* stageInfo,
const SkSL::Program::Settings& settings,
SkSL::Program::Inputs* outInputs);
#endif #endif

View File

@ -18,9 +18,20 @@ namespace SkSL {
*/ */
class CodeGenerator { class CodeGenerator {
public: public:
CodeGenerator(const Program* program, ErrorReporter* errors, SkWStream* out)
: fProgram(*program)
, fErrors(*errors)
, fOut(out) {}
virtual ~CodeGenerator() {} virtual ~CodeGenerator() {}
virtual void generateCode(const Program& program, ErrorReporter& errors, SkWStream& out) = 0; virtual bool generateCode() = 0;
protected:
const Program& fProgram;
ErrorReporter& fErrors;
SkWStream* fOut;
}; };
} // namespace } // namespace

View File

@ -9,6 +9,7 @@
#include "ast/SkSLASTPrecision.h" #include "ast/SkSLASTPrecision.h"
#include "SkSLCFGGenerator.h" #include "SkSLCFGGenerator.h"
#include "SkSLGLSLCodeGenerator.h"
#include "SkSLIRGenerator.h" #include "SkSLIRGenerator.h"
#include "SkSLParser.h" #include "SkSLParser.h"
#include "SkSLSPIRVCodeGenerator.h" #include "SkSLSPIRVCodeGenerator.h"
@ -392,10 +393,10 @@ void Compiler::internalConvertProgram(SkString text,
} }
std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text, std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString text,
std::unordered_map<SkString, CapValue> caps) { const Program::Settings& settings) {
fErrorText = ""; fErrorText = "";
fErrorCount = 0; fErrorCount = 0;
fIRGenerator->start(&caps); fIRGenerator->start(&settings);
std::vector<std::unique_ptr<ProgramElement>> elements; std::vector<std::unique_ptr<ProgramElement>> elements;
Modifiers::Flag ignored; Modifiers::Flag ignored;
switch (kind) { switch (kind) {
@ -409,13 +410,54 @@ std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, SkString t
fIRGenerator->fSymbolTable->markAllFunctionsBuiltin(); fIRGenerator->fSymbolTable->markAllFunctionsBuiltin();
Modifiers::Flag defaultPrecision; Modifiers::Flag defaultPrecision;
this->internalConvertProgram(text, &defaultPrecision, &elements); this->internalConvertProgram(text, &defaultPrecision, &elements);
auto result = std::unique_ptr<Program>(new Program(kind, defaultPrecision, std::move(elements), auto result = std::unique_ptr<Program>(new Program(kind, settings, defaultPrecision, &fContext,
fIRGenerator->fSymbolTable)); std::move(elements),
fIRGenerator->fSymbolTable,
fIRGenerator->fInputs));
fIRGenerator->finish(); fIRGenerator->finish();
this->writeErrorCount(); this->writeErrorCount();
if (fErrorCount) {
return nullptr;
}
return result;
}
bool Compiler::toSPIRV(const Program& program, SkWStream& out) {
SPIRVCodeGenerator cg(&fContext, &program, this, &out);
bool result = cg.generateCode();
this->writeErrorCount();
return result; return result;
} }
bool Compiler::toSPIRV(const Program& program, SkString* out) {
SkDynamicMemoryWStream buffer;
bool result = this->toSPIRV(program, buffer);
if (result) {
sk_sp<SkData> data(buffer.detachAsData());
*out = SkString((const char*) data->data(), data->size());
}
return result;
}
bool Compiler::toGLSL(const Program& program, SkWStream& out) {
GLSLCodeGenerator cg(&fContext, &program, this, &out);
bool result = cg.generateCode();
this->writeErrorCount();
return result;
}
bool Compiler::toGLSL(const Program& program, SkString* out) {
SkDynamicMemoryWStream buffer;
bool result = this->toGLSL(program, buffer);
if (result) {
sk_sp<SkData> data(buffer.detachAsData());
*out = SkString((const char*) data->data(), data->size());
}
return result;
}
void Compiler::error(Position position, SkString msg) { void Compiler::error(Position position, SkString msg) {
fErrorCount++; fErrorCount++;
fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n"; fErrorText += "error: " + position.description() + ": " + msg.c_str() + "\n";
@ -436,70 +478,4 @@ void Compiler::writeErrorCount() {
} }
} }
bool Compiler::toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out) {
std::unordered_map<SkString, CapValue> capsMap;
auto program = this->convertProgram(kind, text, capsMap);
if (fErrorCount == 0) {
SkSL::SPIRVCodeGenerator cg(&fContext);
cg.generateCode(*program.get(), *this, out);
this->writeErrorCount();
}
return fErrorCount == 0;
}
bool Compiler::toSPIRV(Program::Kind kind, const SkString& text, SkString* out) {
SkDynamicMemoryWStream buffer;
bool result = this->toSPIRV(kind, text, buffer);
if (result) {
sk_sp<SkData> data(buffer.detachAsData());
*out = SkString((const char*) data->data(), data->size());
}
return result;
}
static void fill_caps(const GrShaderCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) {
#define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name())));
CAP(fbFetchSupport);
CAP(fbFetchNeedsCustomOutput);
CAP(bindlessTextureSupport);
CAP(dropsTileOnZeroDivide);
CAP(flatInterpolationSupport);
CAP(noperspectiveInterpolationSupport);
CAP(multisampleInterpolationSupport);
CAP(sampleVariablesSupport);
CAP(sampleMaskOverrideCoverageSupport);
CAP(externalTextureSupport);
CAP(texelFetchSupport);
CAP(imageLoadStoreSupport);
CAP(mustEnableAdvBlendEqs);
CAP(mustEnableSpecificAdvBlendEqs);
CAP(mustDeclareFragmentShaderOutput);
CAP(canUseAnyFunctionInShader);
#undef CAP
}
bool Compiler::toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps,
SkWStream& out) {
std::unordered_map<SkString, CapValue> capsMap;
fill_caps(caps, &capsMap);
auto program = this->convertProgram(kind, text, capsMap);
if (fErrorCount == 0) {
SkSL::GLSLCodeGenerator cg(&fContext, &caps);
cg.generateCode(*program.get(), *this, out);
this->writeErrorCount();
}
return fErrorCount == 0;
}
bool Compiler::toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps,
SkString* out) {
SkDynamicMemoryWStream buffer;
bool result = this->toGLSL(kind, text, caps, buffer);
if (result) {
sk_sp<SkData> data(buffer.detachAsData());
*out = SkString((const char*) data->data(), data->size());
}
return result;
}
} // namespace } // namespace

View File

@ -16,9 +16,9 @@
#include "SkSLContext.h" #include "SkSLContext.h"
#include "SkSLErrorReporter.h" #include "SkSLErrorReporter.h"
#include "SkSLIRGenerator.h" #include "SkSLIRGenerator.h"
#include "SkSLGLSLCodeGenerator.h"
#define SK_FRAGCOLOR_BUILTIN 10001 #define SK_FRAGCOLOR_BUILTIN 10001
#define SK_FRAGCOORD_BUILTIN 15
namespace SkSL { namespace SkSL {
@ -39,17 +39,15 @@ public:
~Compiler(); ~Compiler();
std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text, std::unique_ptr<Program> convertProgram(Program::Kind kind, SkString text,
std::unordered_map<SkString, CapValue> caps); const Program::Settings& settings);
bool toSPIRV(Program::Kind kind, const SkString& text, SkWStream& out); bool toSPIRV(const Program& program, SkWStream& out);
bool toSPIRV(Program::Kind kind, const SkString& text, SkString* out); bool toSPIRV(const Program& program, SkString* out);
bool toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps, bool toGLSL(const Program& program, SkWStream& out);
SkWStream& out);
bool toGLSL(Program::Kind kind, const SkString& text, const GrShaderCaps& caps, bool toGLSL(const Program& program, SkString* out);
SkString* out);
void error(Position position, SkString msg) override; void error(Position position, SkString msg) override;
@ -57,6 +55,10 @@ public:
void writeErrorCount(); void writeErrorCount();
int errorCount() override {
return fErrorCount;
}
private: private:
void addDefinition(const Expression* lvalue, const Expression* expr, void addDefinition(const Expression* lvalue, const Expression* expr,
std::unordered_map<const Variable*, const Expression*>* definitions); std::unordered_map<const Variable*, const Expression*>* definitions);

View File

@ -24,6 +24,8 @@ public:
} }
virtual void error(Position position, SkString msg) = 0; virtual void error(Position position, SkString msg) = 0;
virtual int errorCount() = 0;
}; };
} // namespace } // namespace

View File

@ -11,14 +11,13 @@
#include "GLSL.std.450.h" #include "GLSL.std.450.h"
#include "SkSLCompiler.h"
#include "ir/SkSLExpressionStatement.h" #include "ir/SkSLExpressionStatement.h"
#include "ir/SkSLExtension.h" #include "ir/SkSLExtension.h"
#include "ir/SkSLIndexExpression.h" #include "ir/SkSLIndexExpression.h"
#include "ir/SkSLModifiersDeclaration.h" #include "ir/SkSLModifiersDeclaration.h"
#include "ir/SkSLVariableReference.h" #include "ir/SkSLVariableReference.h"
#define SK_FRAGCOLOR_BUILTIN 10001
namespace SkSL { namespace SkSL {
void GLSLCodeGenerator::write(const char* s) { void GLSLCodeGenerator::write(const char* s) {
@ -137,7 +136,7 @@ static bool is_abs(Expression& expr) {
// turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a // turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a
// Tegra3 compiler bug. // Tegra3 compiler bug.
void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) { void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) {
ASSERT(!fCaps.canUseMinAndAbsTogether()); ASSERT(!fProgram.fSettings.fCaps->canUseMinAndAbsTogether());
SkString tmpVar1 = "minAbsHackVar" + to_string(fVarCount++); SkString tmpVar1 = "minAbsHackVar" + to_string(fVarCount++);
SkString tmpVar2 = "minAbsHackVar" + to_string(fVarCount++); SkString tmpVar2 = "minAbsHackVar" + to_string(fVarCount++);
this->fFunctionHeader += " " + absExpr.fType.name() + " " + tmpVar1 + ";\n"; this->fFunctionHeader += " " + absExpr.fType.name() + " " + tmpVar1 + ";\n";
@ -150,7 +149,8 @@ void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherEx
} }
void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
if (!fCaps.canUseMinAndAbsTogether() && c.fFunction.fName == "min" && c.fFunction.fBuiltin) { if (!fProgram.fSettings.fCaps->canUseMinAndAbsTogether() && c.fFunction.fName == "min" &&
c.fFunction.fBuiltin) {
ASSERT(c.fArguments.size() == 2); ASSERT(c.fArguments.size() == 2);
if (is_abs(*c.fArguments[0])) { if (is_abs(*c.fArguments[0])) {
this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]);
@ -163,7 +163,8 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
return; return;
} }
} }
if (fCaps.mustForceNegatedAtanParamToFloat() && c.fFunction.fName == "atan" && if (fProgram.fSettings.fCaps->mustForceNegatedAtanParamToFloat() &&
c.fFunction.fName == "atan" &&
c.fFunction.fBuiltin && c.fArguments.size() == 2 && c.fFunction.fBuiltin && c.fArguments.size() == 2 &&
c.fArguments[1]->fKind == Expression::kPrefix_Kind) { c.fArguments[1]->fKind == Expression::kPrefix_Kind) {
const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1]; const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1];
@ -177,10 +178,10 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
} }
} }
if (!fFoundDerivatives && (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") && if (!fFoundDerivatives && (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") &&
c.fFunction.fBuiltin && fCaps.shaderDerivativeExtensionString()) { c.fFunction.fBuiltin && fProgram.fSettings.fCaps->shaderDerivativeExtensionString()) {
ASSERT(fCaps.shaderDerivativeSupport()); ASSERT(fProgram.fSettings.fCaps->shaderDerivativeSupport());
fHeader.writeText("#extension "); fHeader.writeText("#extension ");
fHeader.writeText(fCaps.shaderDerivativeExtensionString()); fHeader.writeText(fProgram.fSettings.fCaps->shaderDerivativeExtensionString());
fHeader.writeText(" : require\n"); fHeader.writeText(" : require\n");
fFoundDerivatives = true; fFoundDerivatives = true;
} }
@ -235,7 +236,7 @@ void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
break; break;
} }
this->write("texture"); this->write("texture");
if (fCaps.generation() < k130_GrGLSLGeneration) { if (fProgram.fSettings.fCaps->generation() < k130_GrGLSLGeneration) {
this->write(dim); this->write(dim);
} }
if (proj) { if (proj) {
@ -266,14 +267,55 @@ void GLSLCodeGenerator::writeConstructor(const Constructor& c) {
this->write(")"); this->write(")");
} }
void GLSLCodeGenerator::writeFragCoord() {
// We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers
// to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the
// declaration varies in earlier GLSL specs. So it is simpler to omit it.
if (!fProgram.fSettings.fFlipY) {
fSetupFragPosition = true;
this->write("gl_FragCoord");
} else if (const char* extension =
fProgram.fSettings.fCaps->fragCoordConventionsExtensionString()) {
if (!fSetupFragPosition) {
if (fProgram.fSettings.fCaps->generation() < k150_GrGLSLGeneration) {
fHeader.writeText("#extension ");
fHeader.writeText(extension);
fHeader.writeText(" : require\n");
}
fHeader.writeText("layout(origin_upper_left) in vec4 gl_FragCoord;\n");
fSetupFragPosition = true;
}
this->write("gl_FragCoord");
} else {
if (!fSetupFragPosition) {
// The Adreno compiler seems to be very touchy about access to "gl_FragCoord".
// Accessing glFragCoord.zw can cause a program to fail to link. Additionally,
// depending on the surrounding code, accessing .xy with a uniform involved can
// do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand
// (and only accessing .xy) seems to "fix" things.
fHeader.writeText("uniform float " SKSL_RTHEIGHT_NAME ";\n"
"highp vec2 _sktmpCoord = gl_FragCoord.xy;\n"
"highp vec4 sk_FragCoord = vec4(_sktmpCoord.x, " SKSL_RTHEIGHT_NAME
" - _sktmpCoord.y, 1.0, 1.0);\n");
fSetupFragPosition = true;
}
this->write("sk_FragCoord");
}
}
void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) { switch (ref.fVariable.fModifiers.fLayout.fBuiltin) {
if (fCaps.mustDeclareFragmentShaderOutput()) { case SK_FRAGCOLOR_BUILTIN:
if (fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
this->write("sk_FragColor"); this->write("sk_FragColor");
} else { } else {
this->write("gl_FragColor"); this->write("gl_FragColor");
} }
} else { break;
case SK_FRAGCOORD_BUILTIN:
this->writeFragCoord();
break;
default:
this->write(ref.fVariable.fName); this->write(ref.fVariable.fName);
} }
} }
@ -484,14 +526,16 @@ void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
(modifiers.fFlags & Modifiers::kOut_Flag)) { (modifiers.fFlags & Modifiers::kOut_Flag)) {
this->write("inout "); this->write("inout ");
} else if (modifiers.fFlags & Modifiers::kIn_Flag) { } else if (modifiers.fFlags & Modifiers::kIn_Flag) {
if (globalContext && fCaps.generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { if (globalContext &&
fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
this->write(fProgramKind == Program::kVertex_Kind ? "attribute " this->write(fProgramKind == Program::kVertex_Kind ? "attribute "
: "varying "); : "varying ");
} else { } else {
this->write("in "); this->write("in ");
} }
} else if (modifiers.fFlags & Modifiers::kOut_Flag) { } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
if (globalContext && fCaps.generation() < GrGLSLGeneration::k130_GrGLSLGeneration) { if (globalContext &&
fProgram.fSettings.fCaps->generation() < GrGLSLGeneration::k130_GrGLSLGeneration) {
this->write("varying "); this->write("varying ");
} else { } else {
this->write("out "); this->write("out ");
@ -503,7 +547,7 @@ void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
if (modifiers.fFlags & Modifiers::kConst_Flag) { if (modifiers.fFlags & Modifiers::kConst_Flag) {
this->write("const "); this->write("const ");
} }
if (fCaps.usesPrecisionModifiers()) { if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) {
if (modifiers.fFlags & Modifiers::kLowp_Flag) { if (modifiers.fFlags & Modifiers::kLowp_Flag) {
this->write("lowp "); this->write("lowp ");
} }
@ -554,9 +598,9 @@ void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g
this->writeExpression(*var.fValue, kTopLevel_Precedence); this->writeExpression(*var.fValue, kTopLevel_Precedence);
} }
if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) { if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) {
if (fCaps.imageLoadStoreExtensionString()) { if (fProgram.fSettings.fCaps->imageLoadStoreExtensionString()) {
fHeader.writeText("#extension "); fHeader.writeText("#extension ");
fHeader.writeText(fCaps.imageLoadStoreExtensionString()); fHeader.writeText(fProgram.fSettings.fCaps->imageLoadStoreExtensionString());
fHeader.writeText(" : require\n"); fHeader.writeText(" : require\n");
} }
fFoundImageDecl = true; fFoundImageDecl = true;
@ -670,23 +714,22 @@ void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) {
this->write(";"); this->write(";");
} }
void GLSLCodeGenerator::generateCode(const Program& program, ErrorReporter& errors, bool GLSLCodeGenerator::generateCode() {
SkWStream& out) { SkWStream* rawOut = fOut;
ASSERT(fOut == nullptr);
fOut = &fHeader; fOut = &fHeader;
fProgramKind = program.fKind; fProgramKind = fProgram.fKind;
this->write(fCaps.versionDeclString()); this->write(fProgram.fSettings.fCaps->versionDeclString());
this->writeLine(); this->writeLine();
for (const auto& e : program.fElements) { for (const auto& e : fProgram.fElements) {
if (e->fKind == ProgramElement::kExtension_Kind) { if (e->fKind == ProgramElement::kExtension_Kind) {
this->writeExtension((Extension&) *e); this->writeExtension((Extension&) *e);
} }
} }
SkDynamicMemoryWStream body; SkDynamicMemoryWStream body;
fOut = &body; fOut = &body;
if (fCaps.usesPrecisionModifiers()) { if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) {
this->write("precision "); this->write("precision ");
switch (program.fDefaultPrecision) { switch (fProgram.fDefaultPrecision) {
case Modifiers::kLowp_Flag: case Modifiers::kLowp_Flag:
this->write("lowp"); this->write("lowp");
break; break;
@ -702,7 +745,7 @@ void GLSLCodeGenerator::generateCode(const Program& program, ErrorReporter& erro
} }
this->writeLine(" float;"); this->writeLine(" float;");
} }
for (const auto& e : program.fElements) { for (const auto& e : fProgram.fElements) {
switch (e->fKind) { switch (e->fKind) {
case ProgramElement::kExtension_Kind: case ProgramElement::kExtension_Kind:
break; break;
@ -715,9 +758,9 @@ void GLSLCodeGenerator::generateCode(const Program& program, ErrorReporter& erro
this->writeVarDeclarations(decl, true); this->writeVarDeclarations(decl, true);
this->writeLine(); this->writeLine();
} else if (builtin == SK_FRAGCOLOR_BUILTIN && } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
fCaps.mustDeclareFragmentShaderOutput()) { fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) {
this->write("out "); this->write("out ");
if (fCaps.usesPrecisionModifiers()) { if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) {
this->write("mediump "); this->write("mediump ");
} }
this->writeLine("vec4 sk_FragColor;"); this->writeLine("vec4 sk_FragColor;");
@ -742,8 +785,9 @@ void GLSLCodeGenerator::generateCode(const Program& program, ErrorReporter& erro
} }
fOut = nullptr; fOut = nullptr;
write_data(*fHeader.detachAsData(), out); write_data(*fHeader.detachAsData(), *rawOut);
write_data(*body.detachAsData(), out); write_data(*body.detachAsData(), *rawOut);
return true;
} }
} }

View File

@ -12,7 +12,6 @@
#include <tuple> #include <tuple>
#include <unordered_map> #include <unordered_map>
#include "GrShaderCaps.h"
#include "SkStream.h" #include "SkStream.h"
#include "SkSLCodeGenerator.h" #include "SkSLCodeGenerator.h"
#include "ir/SkSLBinaryExpression.h" #include "ir/SkSLBinaryExpression.h"
@ -72,11 +71,12 @@ public:
kTopLevel_Precedence = 18 kTopLevel_Precedence = 18
}; };
GLSLCodeGenerator(const Context* context, const GrShaderCaps* caps) GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
: fContext(*context) SkWStream* out)
, fCaps(*caps) {} : INHERITED(program, errors, out)
, fContext(*context) {}
void generateCode(const Program& program, ErrorReporter& errors, SkWStream& out) override; virtual bool generateCode() override;
private: private:
void write(const char* s); void write(const char* s);
@ -109,6 +109,8 @@ private:
void writeVarDeclarations(const VarDeclarations& decl, bool global); void writeVarDeclarations(const VarDeclarations& decl, bool global);
void writeFragCoord();
void writeVariableReference(const VariableReference& ref); void writeVariableReference(const VariableReference& ref);
void writeExpression(const Expression& expr, Precedence parentPrecedence); void writeExpression(const Expression& expr, Precedence parentPrecedence);
@ -156,8 +158,6 @@ private:
void writeReturnStatement(const ReturnStatement& r); void writeReturnStatement(const ReturnStatement& r);
const Context& fContext; const Context& fContext;
const GrShaderCaps& fCaps;
SkWStream* fOut = nullptr;
SkDynamicMemoryWStream fHeader; SkDynamicMemoryWStream fHeader;
SkString fFunctionHeader; SkString fFunctionHeader;
Program::Kind fProgramKind; Program::Kind fProgramKind;
@ -171,6 +171,9 @@ private:
// true if we have run into usages of dFdx / dFdy // true if we have run into usages of dFdx / dFdy
bool fFoundDerivatives = false; bool fFoundDerivatives = false;
bool fFoundImageDecl = false; bool fFoundImageDecl = false;
bool fSetupFragPosition = false;
typedef CodeGenerator INHERITED;
}; };
} }

View File

@ -9,6 +9,7 @@
#include "limits.h" #include "limits.h"
#include "SkSLCompiler.h"
#include "ast/SkSLASTBoolLiteral.h" #include "ast/SkSLASTBoolLiteral.h"
#include "ast/SkSLASTFieldSuffix.h" #include "ast/SkSLASTFieldSuffix.h"
#include "ast/SkSLASTFloatLiteral.h" #include "ast/SkSLASTFloatLiteral.h"
@ -84,7 +85,6 @@ IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> sy
ErrorReporter& errorReporter) ErrorReporter& errorReporter)
: fContext(*context) : fContext(*context)
, fCurrentFunction(nullptr) , fCurrentFunction(nullptr)
, fCapsMap(nullptr)
, fSymbolTable(std::move(symbolTable)) , fSymbolTable(std::move(symbolTable))
, fLoopLevel(0) , fLoopLevel(0)
, fErrors(errorReporter) {} , fErrors(errorReporter) {}
@ -97,14 +97,40 @@ void IRGenerator::popSymbolTable() {
fSymbolTable = fSymbolTable->fParent; fSymbolTable = fSymbolTable->fParent;
} }
void IRGenerator::start(std::unordered_map<SkString, CapValue>* caps) { static void fill_caps(const GrShaderCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) {
this->fCapsMap = caps; #define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name())));
CAP(fbFetchSupport);
CAP(fbFetchNeedsCustomOutput);
CAP(bindlessTextureSupport);
CAP(dropsTileOnZeroDivide);
CAP(flatInterpolationSupport);
CAP(noperspectiveInterpolationSupport);
CAP(multisampleInterpolationSupport);
CAP(sampleVariablesSupport);
CAP(sampleMaskOverrideCoverageSupport);
CAP(externalTextureSupport);
CAP(texelFetchSupport);
CAP(imageLoadStoreSupport);
CAP(mustEnableAdvBlendEqs);
CAP(mustEnableSpecificAdvBlendEqs);
CAP(mustDeclareFragmentShaderOutput);
CAP(canUseAnyFunctionInShader);
#undef CAP
}
void IRGenerator::start(const Program::Settings* settings) {
fSettings = settings;
fCapsMap.clear();
if (settings->fCaps) {
fill_caps(*settings->fCaps, &fCapsMap);
}
this->pushSymbolTable(); this->pushSymbolTable();
fInputs.reset();
} }
void IRGenerator::finish() { void IRGenerator::finish() {
this->popSymbolTable(); this->popSymbolTable();
this->fCapsMap = nullptr; fSettings = nullptr;
} }
std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) { std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) {
@ -600,6 +626,11 @@ std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier&
case Symbol::kVariable_Kind: { case Symbol::kVariable_Kind: {
const Variable* var = (const Variable*) result; const Variable* var = (const Variable*) result;
this->markReadFrom(*var); this->markReadFrom(*var);
if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
fSettings->fFlipY &&
(!fSettings->fCaps || !fSettings->fCaps->fragCoordConventionsExtensionString())) {
fInputs.fRTHeight = true;
}
return std::unique_ptr<VariableReference>(new VariableReference(identifier.fPosition, return std::unique_ptr<VariableReference>(new VariableReference(identifier.fPosition,
*var)); *var));
} }
@ -1336,9 +1367,8 @@ std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expressi
} }
std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) { std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) {
ASSERT(fCapsMap); auto found = fCapsMap.find(name);
auto found = fCapsMap->find(name); if (found == fCapsMap.end()) {
if (found == fCapsMap->end()) {
fErrors.error(position, "unknown capability flag '" + name + "'"); fErrors.error(position, "unknown capability flag '" + name + "'");
return nullptr; return nullptr;
} }

View File

@ -40,6 +40,7 @@
#include "ir/SkSLInterfaceBlock.h" #include "ir/SkSLInterfaceBlock.h"
#include "ir/SkSLModifiers.h" #include "ir/SkSLModifiers.h"
#include "ir/SkSLModifiersDeclaration.h" #include "ir/SkSLModifiersDeclaration.h"
#include "ir/SkSLProgram.h"
#include "ir/SkSLSymbolTable.h" #include "ir/SkSLSymbolTable.h"
#include "ir/SkSLStatement.h" #include "ir/SkSLStatement.h"
#include "ir/SkSLType.h" #include "ir/SkSLType.h"
@ -87,12 +88,14 @@ public:
std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration( std::unique_ptr<ModifiersDeclaration> convertModifiersDeclaration(
const ASTModifiersDeclaration& m); const ASTModifiersDeclaration& m);
Program::Inputs fInputs;
private: private:
/** /**
* Prepare to compile a program. Pushes a new symbol table and installs the caps so that * Prepare to compile a program. Resets state, pushes a new symbol table, and installs the
* references to sk_Caps.<cap> can be resolved. * settings.
*/ */
void start(std::unordered_map<SkString, CapValue>* caps); void start(const Program::Settings* settings);
/** /**
* Performs cleanup after compilation is complete. * Performs cleanup after compilation is complete.
@ -153,7 +156,8 @@ private:
const Context& fContext; const Context& fContext;
const FunctionDeclaration* fCurrentFunction; const FunctionDeclaration* fCurrentFunction;
const std::unordered_map<SkString, CapValue>* fCapsMap; const Program::Settings* fSettings;
std::unordered_map<SkString, CapValue> fCapsMap;
std::shared_ptr<SymbolTable> fSymbolTable; std::shared_ptr<SymbolTable> fSymbolTable;
int fLoopLevel; int fLoopLevel;
ErrorReporter& fErrors; ErrorReporter& fErrors;

View File

@ -37,6 +37,9 @@ int main(int argc, const char** argv) {
printf("error reading '%s'\n", argv[1]); printf("error reading '%s'\n", argv[1]);
exit(2); exit(2);
} }
SkSL::Program::Settings settings;
sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
SkString name(argv[2]); SkString name(argv[2]);
if (name.endsWith(".spirv")) { if (name.endsWith(".spirv")) {
SkFILEWStream out(argv[2]); SkFILEWStream out(argv[2]);
@ -45,7 +48,8 @@ int main(int argc, const char** argv) {
printf("error writing '%s'\n", argv[2]); printf("error writing '%s'\n", argv[2]);
exit(4); exit(4);
} }
if (!compiler.toSPIRV(kind, text, out)) { std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
if (!program || !compiler.toSPIRV(*program, out)) {
printf("%s", compiler.errorText().c_str()); printf("%s", compiler.errorText().c_str());
exit(3); exit(3);
} }
@ -56,7 +60,8 @@ int main(int argc, const char** argv) {
printf("error writing '%s'\n", argv[2]); printf("error writing '%s'\n", argv[2]);
exit(4); exit(4);
} }
if (!compiler.toGLSL(kind, text, *SkSL::ShaderCapsFactory::Default(), out)) { std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
if (!program || !compiler.toGLSL(*program, out)) {
printf("%s", compiler.errorText().c_str()); printf("%s", compiler.errorText().c_str());
exit(3); exit(3);
} }

View File

@ -994,12 +994,12 @@ void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memor
const Layout& fieldLayout = type.fields()[i].fModifiers.fLayout; const Layout& fieldLayout = type.fields()[i].fModifiers.fLayout;
if (fieldLayout.fOffset >= 0) { if (fieldLayout.fOffset >= 0) {
if (fieldLayout.fOffset <= (int) offset) { if (fieldLayout.fOffset <= (int) offset) {
fErrors->error(type.fPosition, fErrors.error(type.fPosition,
"offset of field '" + type.fields()[i].fName + "' must be at " "offset of field '" + type.fields()[i].fName + "' must be at "
"least " + to_string((int) offset)); "least " + to_string((int) offset));
} }
if (fieldLayout.fOffset % alignment) { if (fieldLayout.fOffset % alignment) {
fErrors->error(type.fPosition, fErrors.error(type.fPosition,
"offset of field '" + type.fields()[i].fName + "' must be a multiple" "offset of field '" + type.fields()[i].fName + "' must be a multiple"
" of " + to_string((int) alignment)); " of " + to_string((int) alignment));
} }
@ -1847,11 +1847,64 @@ std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const
} }
SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, SkWStream& out) { SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, SkWStream& out) {
SpvId result = this->nextId();
auto entry = fVariableMap.find(&ref.fVariable); auto entry = fVariableMap.find(&ref.fVariable);
ASSERT(entry != fVariableMap.end()); ASSERT(entry != fVariableMap.end());
SpvId var = entry->second; SpvId var = entry->second;
SpvId result = this->nextId();
this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result, var, out); this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result, var, out);
if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
fProgram.fSettings.fFlipY) {
// need to remap to a top-left coordinate system
if (fRTHeightStructId == (SpvId) -1) {
// height variable hasn't been written yet
std::shared_ptr<SymbolTable> st(new SymbolTable(fErrors));
ASSERT(fRTHeightFieldIndex == (SpvId) -1);
std::vector<Type::Field> fields;
fields.emplace_back(Modifiers(), SkString(SKSL_RTHEIGHT_NAME),
fContext.fFloat_Type.get());
SkString name("sksl_synthetic_uniforms");
Type intfStruct(Position(), name, fields);
Layout layout(-1, -1, 1, -1, -1, -1, -1, false, false, false, Layout::Format::kUnspecified,
false);
Variable intfVar(Position(), Modifiers(layout, Modifiers::kUniform_Flag), name,
intfStruct, Variable::kGlobal_Storage);
InterfaceBlock intf(Position(), intfVar, st);
fRTHeightStructId = this->writeInterfaceBlock(intf);
fRTHeightFieldIndex = 0;
}
ASSERT(fRTHeightFieldIndex != (SpvId) -1);
// write vec4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, 0.0, 1.0)
SpvId xId = this->nextId();
this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId,
result, 0, out);
IntLiteral fieldIndex(fContext, Position(), fRTHeightFieldIndex);
SpvId fieldIndexId = this->writeIntLiteral(fieldIndex);
SpvId heightRead = this->nextId();
this->writeOpCode(SpvOpAccessChain, 5, out);
this->writeWord(this->getPointerType(*fContext.fFloat_Type, SpvStorageClassUniform), out);
this->writeWord(heightRead, out);
this->writeWord(fRTHeightStructId, out);
this->writeWord(fieldIndexId, out);
SpvId rawYId = this->nextId();
this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId,
result, 1, out);
SpvId flippedYId = this->nextId();
this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId,
heightRead, rawYId, out);
FloatLiteral zero(fContext, Position(), 0.0);
SpvId zeroId = writeFloatLiteral(zero);
FloatLiteral one(fContext, Position(), 1.0);
SpvId oneId = writeFloatLiteral(one);
SpvId flipped = this->nextId();
this->writeOpCode(SpvOpCompositeConstruct, 7, out);
this->writeWord(this->getType(*fContext.fVec4_Type), out);
this->writeWord(flipped, out);
this->writeWord(xId, out);
this->writeWord(flippedYId, out);
this->writeWord(zeroId, out);
this->writeWord(oneId, out);
return flipped;
}
return result; return result;
} }
@ -2442,12 +2495,22 @@ SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
MemoryLayout layout = intf.fVariable.fModifiers.fLayout.fPushConstant ? MemoryLayout layout = intf.fVariable.fModifiers.fLayout.fPushConstant ?
MemoryLayout(MemoryLayout::k430_Standard) : MemoryLayout(MemoryLayout::k430_Standard) :
fDefaultLayout; fDefaultLayout;
SpvId type = this->getType(intf.fVariable.fType, layout);
SpvId result = this->nextId(); SpvId result = this->nextId();
this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationBuffer); const Type* type = &intf.fVariable.fType;
if (fProgram.fInputs.fRTHeight) {
ASSERT(fRTHeightStructId == (SpvId) -1);
ASSERT(fRTHeightFieldIndex == (SpvId) -1);
std::vector<Type::Field> fields = type->fields();
fRTHeightStructId = result;
fRTHeightFieldIndex = fields.size();
fields.emplace_back(Modifiers(), SkString(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get());
type = new Type(type->fPosition, type->name(), fields);
}
SpvId typeId = this->getType(*type, layout);
this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers); SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
SpvId ptrType = this->nextId(); SpvId ptrType = this->nextId();
this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConstantBuffer); this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer); this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
this->writeLayout(intf.fVariable.fModifiers.fLayout, result); this->writeLayout(intf.fVariable.fModifiers.fLayout, result);
fVariableMap[&intf.fVariable] = result; fVariableMap[&intf.fVariable] = result;
@ -2734,6 +2797,7 @@ void SPIRVCodeGenerator::writeInstructions(const Program& program, SkWStream& ou
} }
} }
write_data(*fExtraGlobalsBuffer.detachAsData(), out);
write_data(*fNameBuffer.detachAsData(), out); write_data(*fNameBuffer.detachAsData(), out);
write_data(*fDecorationBuffer.detachAsData(), out); write_data(*fDecorationBuffer.detachAsData(), out);
write_data(*fConstantBuffer.detachAsData(), out); write_data(*fConstantBuffer.detachAsData(), out);
@ -2741,18 +2805,17 @@ void SPIRVCodeGenerator::writeInstructions(const Program& program, SkWStream& ou
write_data(*body.detachAsData(), out); write_data(*body.detachAsData(), out);
} }
void SPIRVCodeGenerator::generateCode(const Program& program, ErrorReporter& errors, bool SPIRVCodeGenerator::generateCode() {
SkWStream& out) { ASSERT(!fErrors.errorCount());
fErrors = &errors; this->writeWord(SpvMagicNumber, *fOut);
this->writeWord(SpvMagicNumber, out); this->writeWord(SpvVersion, *fOut);
this->writeWord(SpvVersion, out); this->writeWord(SKSL_MAGIC, *fOut);
this->writeWord(SKSL_MAGIC, out);
SkDynamicMemoryWStream buffer; SkDynamicMemoryWStream buffer;
this->writeInstructions(program, buffer); this->writeInstructions(fProgram, buffer);
this->writeWord(fIdCount, out); this->writeWord(fIdCount, *fOut);
this->writeWord(0, out); // reserved, always zero this->writeWord(0, *fOut); // reserved, always zero
write_data(*buffer.detachAsData(), out); write_data(*buffer.detachAsData(), *fOut);
fErrors = nullptr; return 0 == fErrors.errorCount();
} }
} }

View File

@ -62,18 +62,21 @@ public:
virtual void store(SpvId value, SkWStream& out) = 0; virtual void store(SpvId value, SkWStream& out) = 0;
}; };
SPIRVCodeGenerator(const Context* context) SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
: fContext(*context) SkWStream* out)
: INHERITED(program, errors, out)
, fContext(*context)
, fDefaultLayout(MemoryLayout::k140_Standard) , fDefaultLayout(MemoryLayout::k140_Standard)
, fCapabilities(1 << SpvCapabilityShader) , fCapabilities(1 << SpvCapabilityShader)
, fIdCount(1) , fIdCount(1)
, fBoolTrue(0) , fBoolTrue(0)
, fBoolFalse(0) , fBoolFalse(0)
, fSetupFragPosition(false)
, fCurrentBlock(0) { , fCurrentBlock(0) {
this->setupIntrinsics(); this->setupIntrinsics();
} }
void generateCode(const Program& program, ErrorReporter& errors, SkWStream& out) override; bool generateCode() override;
private: private:
enum IntrinsicKind { enum IntrinsicKind {
@ -236,7 +239,6 @@ private:
const Context& fContext; const Context& fContext;
const MemoryLayout fDefaultLayout; const MemoryLayout fDefaultLayout;
ErrorReporter* fErrors;
uint64_t fCapabilities; uint64_t fCapabilities;
SpvId fIdCount; SpvId fIdCount;
@ -250,6 +252,7 @@ private:
SkDynamicMemoryWStream fCapabilitiesBuffer; SkDynamicMemoryWStream fCapabilitiesBuffer;
SkDynamicMemoryWStream fGlobalInitializersBuffer; SkDynamicMemoryWStream fGlobalInitializersBuffer;
SkDynamicMemoryWStream fConstantBuffer; SkDynamicMemoryWStream fConstantBuffer;
SkDynamicMemoryWStream fExtraGlobalsBuffer;
SkDynamicMemoryWStream fExternalFunctionsBuffer; SkDynamicMemoryWStream fExternalFunctionsBuffer;
SkDynamicMemoryWStream fVariableBuffer; SkDynamicMemoryWStream fVariableBuffer;
SkDynamicMemoryWStream fNameBuffer; SkDynamicMemoryWStream fNameBuffer;
@ -261,13 +264,18 @@ private:
std::unordered_map<uint64_t, SpvId> fUIntConstants; std::unordered_map<uint64_t, SpvId> fUIntConstants;
std::unordered_map<float, SpvId> fFloatConstants; std::unordered_map<float, SpvId> fFloatConstants;
std::unordered_map<double, SpvId> fDoubleConstants; std::unordered_map<double, SpvId> fDoubleConstants;
bool fSetupFragPosition;
// label of the current block, or 0 if we are not in a block // label of the current block, or 0 if we are not in a block
SpvId fCurrentBlock; SpvId fCurrentBlock;
std::stack<SpvId> fBreakTarget; std::stack<SpvId> fBreakTarget;
std::stack<SpvId> fContinueTarget; std::stack<SpvId> fContinueTarget;
SpvId fRTHeightStructId = (SpvId) -1;
SpvId fRTHeightFieldIndex = (SpvId) -1;
friend class PointerLValue; friend class PointerLValue;
friend class SwizzleLValue; friend class SwizzleLValue;
typedef CodeGenerator INHERITED;
}; };
} }

View File

@ -72,6 +72,21 @@ public:
return result; return result;
} }
static sk_sp<GrShaderCaps> FragCoordsOld() {
sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions());
result->fVersionDeclString = "#version 110";
result->fGLSLGeneration = GrGLSLGeneration::k110_GrGLSLGeneration;
result->fFragCoordConventionsExtensionString = "GL_ARB_fragment_coord_conventions";
return result;
}
static sk_sp<GrShaderCaps> FragCoordsNew() {
sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions());
result->fVersionDeclString = "#version 400";
result->fFragCoordConventionsExtensionString = "GL_ARB_fragment_coord_conventions";
return result;
}
static sk_sp<GrShaderCaps> VariousCaps() { static sk_sp<GrShaderCaps> VariousCaps() {
sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions()); sk_sp<GrShaderCaps> result = sk_make_sp<GrShaderCaps>(GrContextOptions());
result->fVersionDeclString = "#version 400"; result->fVersionDeclString = "#version 400";

View File

@ -11,35 +11,66 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "SkSLContext.h"
#include "SkSLModifiers.h" #include "SkSLModifiers.h"
#include "SkSLProgramElement.h" #include "SkSLProgramElement.h"
#include "SkSLSymbolTable.h" #include "SkSLSymbolTable.h"
// name of the render target height uniform
#define SKSL_RTHEIGHT_NAME "u_skRTHeight"
namespace SkSL { namespace SkSL {
/** /**
* Represents a fully-digested program, ready for code generation. * Represents a fully-digested program, ready for code generation.
*/ */
struct Program { struct Program {
struct Settings {
const GrShaderCaps* fCaps = nullptr;
bool fFlipY = false;
};
struct Inputs {
// if true, this program requires the render target height uniform to be defined
bool fRTHeight;
void reset() {
fRTHeight = false;
}
bool isEmpty() {
return !fRTHeight;
}
};
enum Kind { enum Kind {
kFragment_Kind, kFragment_Kind,
kVertex_Kind kVertex_Kind
}; };
Program(Kind kind, Program(Kind kind,
Settings settings,
Modifiers::Flag defaultPrecision, Modifiers::Flag defaultPrecision,
Context* context,
std::vector<std::unique_ptr<ProgramElement>> elements, std::vector<std::unique_ptr<ProgramElement>> elements,
std::shared_ptr<SymbolTable> symbols) std::shared_ptr<SymbolTable> symbols,
Inputs inputs)
: fKind(kind) : fKind(kind)
, fSettings(settings)
, fDefaultPrecision(defaultPrecision) , fDefaultPrecision(defaultPrecision)
, fContext(context)
, fElements(std::move(elements)) , fElements(std::move(elements))
, fSymbols(symbols) {} , fSymbols(symbols)
, fInputs(inputs) {}
Kind fKind; Kind fKind;
Settings fSettings;
// FIXME handle different types; currently it assumes this is for floats // FIXME handle different types; currently it assumes this is for floats
Modifiers::Flag fDefaultPrecision; Modifiers::Flag fDefaultPrecision;
Context* fContext;
std::vector<std::unique_ptr<ProgramElement>> fElements; std::vector<std::unique_ptr<ProgramElement>> fElements;
std::shared_ptr<SymbolTable> fSymbols; std::shared_ptr<SymbolTable> fSymbols;
Inputs fInputs;
}; };
} // namespace } // namespace

View File

@ -2,7 +2,7 @@ STRINGIFY(
// defines built-in interfaces supported by SkiaSL fragment shaders // defines built-in interfaces supported by SkiaSL fragment shaders
layout(builtin=15) in vec4 gl_FragCoord; layout(builtin=15) in vec4 sk_FragCoord;
// 9999 is a temporary value that causes us to ignore these declarations beyond // 9999 is a temporary value that causes us to ignore these declarations beyond
// adding them to the symbol table. This works fine in GLSL (where they do not // adding them to the symbol table. This works fine in GLSL (where they do not

View File

@ -14,21 +14,34 @@
static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
SkSL::Compiler compiler; SkSL::Compiler compiler;
SkDynamicMemoryWStream out; SkDynamicMemoryWStream out;
bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, SkString(src), out); SkSL::Program::Settings settings;
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);
if (program) {
SkString ignored;
compiler.toSPIRV(*program, &ignored);
}
SkString skError(error); SkString skError(error);
if (compiler.errorText() != skError) { if (compiler.errorText() != skError) {
SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error,
compiler.errorText().c_str()); compiler.errorText().c_str());
} }
REPORTER_ASSERT(r, !result);
REPORTER_ASSERT(r, compiler.errorText() == skError); REPORTER_ASSERT(r, compiler.errorText() == skError);
} }
static void test_success(skiatest::Reporter* r, const char* src) { static void test_success(skiatest::Reporter* r, const char* src) {
SkSL::Compiler compiler; SkSL::Compiler compiler;
SkDynamicMemoryWStream out; SkDynamicMemoryWStream out;
bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, SkString(src), out); SkSL::Program::Settings settings;
REPORTER_ASSERT(r, result); 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);
REPORTER_ASSERT(r, program);
SkString ignored;
REPORTER_ASSERT(r, compiler.toSPIRV(*program, &ignored));
} }
DEF_TEST(SkSLUndefinedSymbol, r) { DEF_TEST(SkSLUndefinedSymbol, r) {

View File

@ -11,16 +11,20 @@
#if SK_SUPPORT_GPU #if SK_SUPPORT_GPU
static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings,
const char* expected) { const char* expected, SkSL::Program::Inputs* inputs) {
SkSL::Compiler compiler; SkSL::Compiler compiler;
SkString output; SkString output;
bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, SkString(src), caps, &output); std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
if (!result) { SkString(src),
settings);
if (!program) {
SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
} }
REPORTER_ASSERT(r, result); REPORTER_ASSERT(r, program);
if (result) { *inputs = program->fInputs;
REPORTER_ASSERT(r, compiler.toGLSL(*program, &output));
if (program) {
SkString skExpected(expected); SkString skExpected(expected);
if (output != skExpected) { if (output != skExpected) {
SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
@ -30,6 +34,14 @@ static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& cap
} }
} }
static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
const char* expected) {
SkSL::Program::Settings settings;
settings.fCaps = &caps;
SkSL::Program::Inputs inputs;
test(r, src, settings, expected, &inputs);
}
DEF_TEST(SkSLHelloWorld, r) { DEF_TEST(SkSLHelloWorld, r) {
test(r, test(r,
"void main() { sk_FragColor = vec4(0.75); }", "void main() { sk_FragColor = vec4(0.75); }",
@ -603,4 +615,66 @@ DEF_TEST(SkSLOffset, r) {
" int z;\n" " int z;\n"
"} test;\n"); "} test;\n");
} }
DEF_TEST(SkSLFragCoord, r) {
SkSL::Program::Settings settings;
settings.fFlipY = true;
sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
settings.fCaps = caps.get();
SkSL::Program::Inputs inputs;
test(r,
"void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
settings,
"#version 110\n"
"#extension GL_ARB_fragment_coord_conventions : require\n"
"layout(origin_upper_left) in vec4 gl_FragCoord;\n"
"void main() {\n"
" gl_FragColor.xy = gl_FragCoord.xy;\n"
"}\n",
&inputs);
REPORTER_ASSERT(r, !inputs.fRTHeight);
caps = SkSL::ShaderCapsFactory::FragCoordsNew();
settings.fCaps = caps.get();
test(r,
"void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
settings,
"#version 400\n"
"layout(origin_upper_left) in vec4 gl_FragCoord;\n"
"out vec4 sk_FragColor;\n"
"void main() {\n"
" sk_FragColor.xy = gl_FragCoord.xy;\n"
"}\n",
&inputs);
REPORTER_ASSERT(r, !inputs.fRTHeight);
caps = SkSL::ShaderCapsFactory::Default();
settings.fCaps = caps.get();
test(r,
"void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
settings,
"#version 400\n"
"uniform float u_skRTHeight;\n"
"highp vec2 _sktmpCoord = gl_FragCoord.xy;\n"
"highp vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n"
"out vec4 sk_FragColor;\n"
"void main() {\n"
" sk_FragColor.xy = sk_FragCoord.xy;\n"
"}\n",
&inputs);
REPORTER_ASSERT(r, inputs.fRTHeight);
settings.fFlipY = false;
test(r,
"void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
settings,
"#version 400\n"
"out vec4 sk_FragColor;\n"
"void main() {\n"
" sk_FragColor.xy = gl_FragCoord.xy;\n"
"}\n",
&inputs);
REPORTER_ASSERT(r, !inputs.fRTHeight);
}
#endif #endif