diff options
Diffstat (limited to '3rdparty/bgfx/tools/shaderc/shaderc.cpp')
-rw-r--r-- | 3rdparty/bgfx/tools/shaderc/shaderc.cpp | 263 |
1 files changed, 174 insertions, 89 deletions
diff --git a/3rdparty/bgfx/tools/shaderc/shaderc.cpp b/3rdparty/bgfx/tools/shaderc/shaderc.cpp index bc92f90570d..4d87d511dc9 100644 --- a/3rdparty/bgfx/tools/shaderc/shaderc.cpp +++ b/3rdparty/bgfx/tools/shaderc/shaderc.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ @@ -124,6 +124,7 @@ namespace bgfx static const char* s_textureArray[] = { + "sampler2DArray", "texture2DArray", "texture2DArrayLod", "shadow2DArray", @@ -155,6 +156,33 @@ namespace bgfx }; BX_STATIC_ASSERT(BX_COUNTOF(s_uniformTypeName) == UniformType::Count*2); + static const char* s_allowedVertexShaderInputs[] = + { + "a_position", + "a_normal", + "a_tangent", + "a_bitangent", + "a_color0", + "a_color1", + "a_color2", + "a_color3", + "a_indices", + "a_weight", + "a_texcoord0", + "a_texcoord1", + "a_texcoord2", + "a_texcoord3", + "a_texcoord4", + "a_texcoord5", + "a_texcoord6", + "a_texcoord7", + "i_data0", + "i_data1", + "i_data2", + "i_data3", + "i_data4", + NULL + }; Options::Options() : shaderType(' ') @@ -393,7 +421,6 @@ namespace bgfx return size; } - std::string m_filePath; bx::StringView m_name; typedef std::vector<uint8_t> Buffer; Buffer m_buffer; @@ -500,7 +527,7 @@ namespace bgfx void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end, int32_t _column) { - fprintf(stderr, "Code:\n---\n"); + bx::printf("Code:\n---\n"); bx::Error err; LineReader reader(_code); @@ -512,26 +539,26 @@ namespace bgfx if (err.isOk() && line >= _start) { - std::string strLine(str, len); + bx::StringView strLine(str, len); if (_line == line) { - fprintf(stderr, "\n"); - fprintf(stderr, ">>> %3d: %s", line, strLine.c_str() ); + bx::printf("\n"); + bx::printf(">>> %3d: %.*s", line, strLine.getLength(), strLine.getPtr() ); if (-1 != _column) { - fprintf(stderr, ">>> %3d: %*s\n", _column, _column, "^"); + bx::printf(">>> %3d: %*s\n", _column, _column, "^"); } - fprintf(stderr, "\n"); + bx::printf("\n"); } else { - fprintf(stderr, " %3d: %s", line, strLine.c_str() ); + bx::printf(" %3d: %.*s", line, strLine.getLength(), strLine.getPtr() ); } } } - fprintf(stderr, "---\n"); + bx::printf("---\n"); } void writeFile(const char* _filePath, const void* _data, int32_t _size) @@ -712,7 +739,7 @@ namespace bgfx static void fppError(void* /*_userData*/, char* _format, va_list _vargs) { - vfprintf(stderr, _format, _vargs); + bx::vprintf(_format, _vargs); } char* scratch(const char* _str) @@ -738,27 +765,31 @@ namespace bgfx typedef std::vector<std::string> InOut; - uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol) + uint32_t parseInOut(InOut& _inout, const bx::StringView& _str) { uint32_t hash = 0; - _str = bx::strLTrimSpace(_str).getPtr(); + bx::StringView str = bx::strLTrimSpace(_str); - if (_str < _eol) + if (!str.isEmpty() ) { - const char* delim; + bx::StringView delim; do { - delim = strpbrk(_str, " ,"); - if (NULL != delim) + delim = bx::strFind(str, ','); + if (delim.isEmpty() ) + { + delim = bx::strFind(str, ' '); + } + + const bx::StringView token(bx::strRTrim(bx::StringView(str.getPtr(), delim.getPtr() ), " ") ); + + if (!token.isEmpty() ) { - delim = delim > _eol ? _eol : delim; - std::string token; - token.assign(_str, delim-_str); - _inout.push_back(token); - _str = bx::strLTrimSpace(delim + 1).getPtr(); + _inout.push_back(std::string(token.getPtr(), token.getTerm() ) ); + str = bx::strLTrimSpace(bx::StringView(delim.getPtr() + 1, str.getTerm() ) ); } } - while (delim < _eol && _str < _eol && NULL != delim); + while (!delim.isEmpty() ); std::sort(_inout.begin(), _inout.end() ); @@ -829,20 +860,20 @@ namespace bgfx { if (NULL != _error) { - fprintf(stderr, "Error:\n%s\n\n", _error); + bx::printf("Error:\n%s\n\n", _error); } - fprintf(stderr - , "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n" - "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n" + bx::printf( + "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n" + "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n" "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n" , BGFX_SHADERC_VERSION_MAJOR , BGFX_SHADERC_VERSION_MINOR , BGFX_API_VERSION ); - fprintf(stderr - , "Usage: shaderc -f <in> -o <out> --type <v/f> --platform <platform>\n" + bx::printf( + "Usage: shaderc -f <in> -o <out> --type <v/f> --platform <platform>\n" "\n" "Options:\n" @@ -858,9 +889,17 @@ namespace bgfx " asm.js\n" " ios\n" " linux\n" - " nacl\n" + " orbis\n" " osx\n" " windows\n" + " -p, --profile <profile> Shader model (default GLSL).\n" + " s_3\n" + " s_4\n" + " s_4_0_level\n" + " s_5\n" + " metal\n" + " pssl\n" + " spirv\n" " --preprocess Preprocess only.\n" " --define <defines> Add defines to preprocessor (semicolon separated).\n" " --raw Do not process shader. No preprocessor, and no glsl-optimizer (GLSL only).\n" @@ -874,7 +913,6 @@ namespace bgfx "\n" " --debug Debug information.\n" " --disasm Disassemble compiled shader.\n" - " -p, --profile <profile> Shader model (f.e. ps_3_0).\n" " -O <level> Optimization level (0, 1, 2, 3).\n" " --Werror Treat warnings as errors.\n" @@ -999,7 +1037,14 @@ namespace bgfx else if (0 == bx::strCmpI(platform, "ios") ) { preprocessor.setDefine("BX_PLATFORM_IOS=1"); - preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1"); + if (metal) + { + preprocessor.setDefine("BGFX_SHADER_LANGUAGE_METAL=1"); + } + else + { + preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1"); + } } else if (0 == bx::strCmpI(platform, "linux") ) { @@ -1016,7 +1061,10 @@ namespace bgfx else if (0 == bx::strCmpI(platform, "osx") ) { preprocessor.setDefine("BX_PLATFORM_OSX=1"); - preprocessor.setDefine(glslDefine); + if (!metal) + { + preprocessor.setDefine(glslDefine); + } char temp[256]; bx::snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_METAL=%d", metal); preprocessor.setDefine(temp); @@ -1052,7 +1100,7 @@ namespace bgfx break; default: - fprintf(stderr, "Unknown type: %c?!", _options.shaderType); + bx::printf("Unknown type: %c?!", _options.shaderType); return false; } @@ -1094,9 +1142,15 @@ namespace bgfx || 0 == bx::strCmp(typen, "noperspective", 13) || 0 == bx::strCmp(typen, "centroid", 8) ) { - interpolation = typen; + if ('f' == _options.shaderType + || 0 != glsl + || 0 != essl) + { + interpolation = typen; + usesInterpolationQualifiers = true; + } + typen = nextWord(parse); - usesInterpolationQualifiers = true; } bx::StringView name = nextWord(parse); @@ -1192,36 +1246,61 @@ namespace bgfx input = const_cast<char*>(bx::strLTrimSpace(data).getPtr() ); while (input[0] == '$') { - const char* str = bx::strLTrimSpace(input+1).getPtr(); + bx::StringView str = bx::strLTrimSpace(input+1); bx::StringView eol = bx::strFindEol(str); bx::StringView nl = bx::strFindNl(eol); input = const_cast<char*>(nl.getPtr() ); if (0 == bx::strCmp(str, "input", 5) ) { - str += 5; + str = bx::StringView(str.getPtr() + 5, str.getTerm() ); bx::StringView comment = bx::strFind(str, "//"); eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol; - inputHash = parseInOut(shaderInputs, str, eol.getPtr() ); + inputHash = parseInOut(shaderInputs, bx::StringView(str.getPtr(), eol.getPtr() ) ); } else if (0 == bx::strCmp(str, "output", 6) ) { - str += 6; + str = bx::StringView(str.getPtr() + 6, str.getTerm() ); bx::StringView comment = bx::strFind(str, "//"); eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol; - outputHash = parseInOut(shaderOutputs, str, eol.getPtr() ); + outputHash = parseInOut(shaderOutputs, bx::StringView(str.getPtr(), eol.getPtr() ) ); } else if (0 == bx::strCmp(str, "raw", 3) ) { raw = true; - str += 3; + str = bx::StringView(str.getPtr() + 3, str.getTerm() ); } input = const_cast<char*>(bx::strLTrimSpace(input).getPtr() ); } } - if (raw) + bool invalidShaderAttribute = false; + if ('v' == _options.shaderType) + { + for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it) + { + if (bx::findIdentifierMatch(it->c_str(), s_allowedVertexShaderInputs).isEmpty() ) + { + invalidShaderAttribute = true; + bx::printf( + "Invalid vertex shader input attribute '%s'.\n" + "\n" + "Valid input attributes:\n" + " a_position, a_normal, a_tangent, a_bitangent, a_color0, a_color1, a_color2, a_color3, a_indices, a_weight,\n" + " a_texcoord0, a_texcoord1, a_texcoord2, a_texcoord3, a_texcoord4, a_texcoord5, a_texcoord6, a_texcoord7,\n" + " i_data0, i_data1, i_data2, i_data3, i_data4.\n" + "\n" + , it->c_str() ); + break; + } + } + } + + if (invalidShaderAttribute) + { + } + else if (raw) { if ('f' == _options.shaderType) { @@ -1267,13 +1346,12 @@ namespace bgfx bx::StringView entry = bx::strFind(input, "void main()"); if (entry.isEmpty() ) { - fprintf(stderr, "Shader entry point 'void main()' is not found.\n"); + bx::printf("Shader entry point 'void main()' is not found.\n"); } else { if (0 != glsl - || 0 != essl - || 0 != metal) + || 0 != essl) { } else @@ -1401,7 +1479,11 @@ namespace bgfx code += _comment; code += preprocessor.m_preprocessed; - if (0 != spirv) + if (0 != metal) + { + compiled = compileMetalShader(_options, BX_MAKEFOURCC('M', 'T', 'L', 0), code, _writer); + } + else if (0 != spirv) { compiled = compileSPIRVShader(_options, 0, code, _writer); } @@ -1439,13 +1521,12 @@ namespace bgfx bx::StringView entry = bx::strFind(shader, "void main()"); if (entry.isEmpty() ) { - fprintf(stderr, "Shader entry point 'void main()' is not found.\n"); + bx::printf("Shader entry point 'void main()' is not found.\n"); } else { if (0 != glsl - || 0 != essl - || 0 != metal) + || 0 != essl) { if (0 == essl) { @@ -1468,21 +1549,23 @@ namespace bgfx if (0 == bx::strCmp(name, "a_", 2) || 0 == bx::strCmp(name, "i_", 2) ) { - preprocessor.writef("attribute %s %s %s %s;\n" - , var.m_precision.c_str() - , var.m_interpolation.c_str() - , var.m_type.c_str() - , name - ); + preprocessor.writef( + "attribute %s %s %s %s;\n" + , var.m_precision.c_str() + , var.m_interpolation.c_str() + , var.m_type.c_str() + , name + ); } else { - preprocessor.writef("%s varying %s %s %s;\n" - , var.m_interpolation.c_str() - , var.m_precision.c_str() - , var.m_type.c_str() - , name - ); + preprocessor.writef( + "%s varying %s %s %s;\n" + , var.m_interpolation.c_str() + , var.m_precision.c_str() + , var.m_type.c_str() + , name + ); } } } @@ -1663,7 +1746,7 @@ namespace bgfx } else { - fprintf(stderr, "gl_PrimitiveID builtin is not supported by this D3D9 HLSL.\n"); + bx::printf("gl_PrimitiveID builtin is not supported by this D3D9 HLSL.\n"); return false; } } @@ -1757,7 +1840,7 @@ namespace bgfx } else { - fprintf(stderr, "gl_VertexID builtin is not supported by this D3D9 HLSL.\n"); + bx::printf("gl_VertexID builtin is not supported by this D3D9 HLSL.\n"); return false; } } @@ -1773,7 +1856,7 @@ namespace bgfx } else { - fprintf(stderr, "gl_InstanceID builtin is not supported by this D3D9 HLSL.\n"); + bx::printf("gl_InstanceID builtin is not supported by this D3D9 HLSL.\n"); return false; } } @@ -1871,10 +1954,12 @@ namespace bgfx } if (0 != glsl - || 0 != essl - || 0 != metal) + || 0 != essl) { - if (!bx::strFind(preprocessor.m_preprocessed.c_str(), "layout(std430").isEmpty() ) + const bx::StringView preprocessedInput(preprocessor.m_preprocessed.c_str() ); + + if (!bx::strFind(preprocessedInput, "layout(std430").isEmpty() + || !bx::strFind(preprocessedInput, "image2D").isEmpty() ) { glsl = 430; } @@ -1895,21 +1980,14 @@ namespace bgfx if (0 == essl) { - const bool need130 = 120 == glsl && (false + const bool need130 = (120 == glsl && (false || !bx::findIdentifierMatch(input, s_130).isEmpty() || usesInterpolationQualifiers || usesTexelFetch - ); + ) ); - if (0 != metal) - { - bx::stringPrintf(code, "#version 120\n"); - } - else - { - bx::stringPrintf(code, "#version %s\n", need130 ? "130" : _options.profile.c_str()); - glsl = 130; - } + bx::stringPrintf(code, "#version %s\n", need130 ? "130" : _options.profile.c_str()); + glsl = 130; if (need130) { @@ -2020,7 +2098,7 @@ namespace bgfx { bx::stringPrintf(code , "#define bgfxShadow2D shadow2D\n" - "#define bgfxShadow2DProj shadow2DProj\n" + "#define bgfxShadow2DProj shadow2DProj\n" ); } } @@ -2145,7 +2223,10 @@ namespace bgfx , "#define ivec2 vec2\n" "#define ivec3 vec3\n" "#define ivec4 vec4\n" - ); + "#define uvec2 vec2\n" + "#define uvec3 vec3\n" + "#define uvec4 vec4\n" + ); } } else @@ -2199,7 +2280,11 @@ namespace bgfx code += _comment; code += preprocessor.m_preprocessed; - if (0 != spirv) + if (0 != metal) + { + compiled = compileMetalShader(_options, BX_MAKEFOURCC('M', 'T', 'L', 0), code, _writer); + } + else if (0 != spirv) { compiled = compileSPIRVShader(_options, 0, code, _writer); } @@ -2242,8 +2327,8 @@ namespace bgfx if (cmdLine.hasArg('v', "version") ) { - fprintf(stderr - , "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n" + bx::printf( + "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n" , BGFX_SHADERC_VERSION_MAJOR , BGFX_SHADERC_VERSION_MINOR , BGFX_API_VERSION @@ -2403,7 +2488,7 @@ namespace bgfx bx::FileReader reader; if (!bx::open(&reader, filePath) ) { - fprintf(stderr, "Unable to open file '%s'.\n", filePath); + bx::printf("Unable to open file '%s'.\n", filePath); } else { @@ -2423,7 +2508,7 @@ namespace bgfx } else { - fprintf(stderr, "ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef); + bx::printf("ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef); } } @@ -2459,7 +2544,7 @@ namespace bgfx if (!bx::open(writer, outFilePath) ) { - fprintf(stderr, "Unable to open output file '%s'.", outFilePath); + bx::printf("Unable to open output file '%s'.", outFilePath); return bx::kExitFailure; } @@ -2474,9 +2559,9 @@ namespace bgfx return bx::kExitSuccess; } - remove(outFilePath); + bx::remove(outFilePath); - fprintf(stderr, "Failed to build shader.\n"); + bx::printf("Failed to build shader.\n"); return bx::kExitFailure; } |