bgfx/tools/shaderc/shaderc.cpp

2872 lines
73 KiB
C++
Raw Normal View History

2012-10-28 08:34:41 +04:00
/*
2023-01-14 21:05:12 +03:00
* Copyright 2011-2023 Branimir Karadzic. All rights reserved.
2022-01-15 22:59:06 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
2012-10-28 08:34:41 +04:00
*/
#include "shaderc.h"
2017-01-09 02:55:14 +03:00
#include <bx/commandline.h>
2017-07-14 10:10:33 +03:00
#include <bx/filepath.h>
2012-10-28 08:34:41 +04:00
#define MAX_TAGS 256
extern "C"
{
#include <fpp.h>
} // extern "C"
#define BGFX_SHADER_BIN_VERSION 11
#define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', BGFX_SHADER_BIN_VERSION)
#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', BGFX_SHADER_BIN_VERSION)
#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', BGFX_SHADER_BIN_VERSION)
2017-05-11 06:55:31 +03:00
#define BGFX_SHADERC_VERSION_MAJOR 1
#define BGFX_SHADERC_VERSION_MINOR 18
2017-05-11 06:55:31 +03:00
2016-01-31 08:13:41 +03:00
namespace bgfx
{
2016-01-31 08:13:41 +03:00
bool g_verbose = false;
2020-12-29 04:27:50 +03:00
struct ShadingLang
{
enum Enum
{
ESSL,
GLSL,
HLSL,
Metal,
PSSL,
SpirV,
Count
};
};
static const char* s_shadingLangName[] =
{
"OpenGL ES Shading Language / WebGL (ESSL)",
"OpenGL Shading Language (GLSL)",
"High-Level Shading Language (HLSL)",
"Metal Shading Language (MSL)",
"PlayStation Shader Language (PSSL)",
"Standard Portable Intermediate Representation - V (SPIR-V)",
"Unknown?!"
};
2020-12-29 04:27:50 +03:00
BX_STATIC_ASSERT(BX_COUNTOF(s_shadingLangName) == ShadingLang::Count+1, "ShadingLang::Enum and s_shadingLangName mismatch");
const char* getName(ShadingLang::Enum _lang)
{
return s_shadingLangName[_lang];
}
// c - compute
// d - domain
// f - fragment
// g - geometry
// h - hull
// v - vertex
//
// OpenGL #version Features Direct3D Features Shader Model
// 2.1 120 vf 9.0 vf 2.0
// 3.0 130
// 3.1 140
// 3.2 150 vgf
// 3.3 330 10.0 vgf 4.0
// 4.0 400 vhdgf
// 4.1 410
// 4.2 420 11.0 vhdgf+c 5.0
// 4.3 430 vhdgf+c
// 4.4 440
//
// SPIR-V profile naming convention:
// spirv<SPIR-V version>-<Vulkan version>
//
// SPIR-V version | Vulkan version | shaderc encoding
// 1.0 | 1.0 | 1010
// 1.3 | 1.1 | 1311
// 1.4 | 1.1 | 1411
// 1.5 | 1.2 | 1512
// 1.6 | 1.3 | 1613
2020-12-29 04:27:50 +03:00
struct Profile
{
ShadingLang::Enum lang;
2023-10-01 02:53:30 +03:00
uint32_t id;
const bx::StringLiteral name;
2020-12-29 04:27:50 +03:00
};
static const Profile s_profiles[] =
{
{ ShadingLang::ESSL, 100, "100_es" },
{ ShadingLang::ESSL, 300, "300_es" },
{ ShadingLang::ESSL, 310, "310_es" },
{ ShadingLang::ESSL, 320, "320_es" },
{ ShadingLang::HLSL, 400, "s_4_0" },
{ ShadingLang::HLSL, 500, "s_5_0" },
{ ShadingLang::Metal, 1000, "metal" },
{ ShadingLang::PSSL, 1000, "pssl" },
{ ShadingLang::SpirV, 1010, "spirv" },
{ ShadingLang::SpirV, 1010, "spirv10-10" },
{ ShadingLang::SpirV, 1311, "spirv13-11" },
2020-12-29 04:27:50 +03:00
{ ShadingLang::SpirV, 1411, "spirv14-11" },
{ ShadingLang::SpirV, 1512, "spirv15-12" },
{ ShadingLang::SpirV, 1613, "spirv16-13" },
2020-12-29 04:27:50 +03:00
{ ShadingLang::GLSL, 120, "120" },
{ ShadingLang::GLSL, 130, "130" },
{ ShadingLang::GLSL, 140, "140" },
{ ShadingLang::GLSL, 150, "150" },
{ ShadingLang::GLSL, 330, "330" },
{ ShadingLang::GLSL, 400, "400" },
{ ShadingLang::GLSL, 410, "410" },
{ ShadingLang::GLSL, 420, "420" },
{ ShadingLang::GLSL, 430, "430" },
{ ShadingLang::GLSL, 440, "440" },
};
2016-01-31 08:13:41 +03:00
static const char* s_ARB_shader_texture_lod[] =
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
"texture2DLod",
2016-08-24 08:06:50 +03:00
"texture2DArrayLod", // BK - interacts with ARB_texture_array.
2016-01-31 08:13:41 +03:00
"texture2DProjLod",
2017-03-24 03:33:29 +03:00
"texture2DGrad",
"texture2DProjGrad",
2016-01-31 08:13:41 +03:00
"texture3DLod",
"texture3DProjLod",
2017-03-24 03:33:29 +03:00
"texture3DGrad",
"texture3DProjGrad",
2016-01-31 08:13:41 +03:00
"textureCubeLod",
2017-03-24 03:33:29 +03:00
"textureCubeGrad",
2016-01-31 08:13:41 +03:00
"shadow2DLod",
"shadow2DProjLod",
NULL
// "texture1DLod",
// "texture1DProjLod",
// "shadow1DLod",
// "shadow1DProjLod",
};
2017-03-24 03:33:29 +03:00
static const char* s_EXT_shader_texture_lod[] =
{
"texture2DLod",
"texture2DProjLod",
"textureCubeLod",
"texture2DGrad",
"texture2DProjGrad",
"textureCubeGrad",
NULL
};
2016-01-31 08:13:41 +03:00
static const char* s_EXT_shadow_samplers[] =
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
"shadow2D",
"shadow2DProj",
"sampler2DShadow",
NULL
};
2016-01-31 08:13:41 +03:00
static const char* s_OES_standard_derivatives[] =
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
"dFdx",
"dFdy",
"fwidth",
NULL
};
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
static const char* s_OES_texture_3D[] =
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
"texture3D",
"texture3DProj",
"texture3DLod",
"texture3DProjLod",
NULL
};
static const char* s_EXT_gpu_shader4[] =
{
"gl_VertexID",
"gl_InstanceID",
"texture2DLodOffset",
NULL
};
// To be use from vertex program require:
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_viewport_layer_array.txt
// DX11 11_1 feature level
static const char* s_ARB_shader_viewport_layer_array[] =
{
"gl_ViewportIndex",
"gl_Layer",
NULL
};
static const char* s_ARB_gpu_shader5[] =
{
"bitfieldReverse",
"floatBitsToInt",
"floatBitsToUint",
"intBitsToFloat",
"uintBitsToFloat",
NULL
};
static const char* s_ARB_shading_language_packing[] =
{
"packHalf2x16",
"unpackHalf2x16",
NULL
};
2016-01-31 08:13:41 +03:00
static const char* s_130[] =
{
"uint",
"uint2",
"uint3",
"uint4",
2018-08-28 06:25:54 +03:00
"isampler2D",
"usampler2D",
2016-01-31 08:13:41 +03:00
"isampler3D",
"usampler3D",
2018-08-28 06:25:54 +03:00
"isamplerCube",
"usamplerCube",
"textureSize",
2016-01-31 08:13:41 +03:00
NULL
};
2016-08-22 00:03:16 +03:00
static const char* s_textureArray[] =
{
"sampler2DArray",
2016-08-22 00:03:16 +03:00
"texture2DArray",
"texture2DArrayLod",
"shadow2DArray",
NULL
};
2016-03-31 06:32:13 +03:00
static const char* s_ARB_texture_multisample[] =
{
"sampler2DMS",
"isampler2DMS",
"usampler2DMS",
NULL
};
2016-06-14 00:47:37 +03:00
static const char* s_texelFetch[] =
{
"texelFetch",
"texelFetchOffset",
NULL
};
static const char* s_bitsToEncoders[] =
{
"floatBitsToUint",
"floatBitsToInt",
"intBitsToFloat",
"uintBitsToFloat",
NULL
};
static const char* s_unsignedVecs[] =
{
"uvec2",
"uvec3",
"uvec4",
NULL
};
const char* s_uniformTypeName[] =
2016-01-31 08:13:41 +03:00
{
"int", "int",
NULL, NULL,
"vec4", "float4",
"mat3", "float3x3",
"mat4", "float4x4",
2016-01-31 08:13:41 +03:00
};
BX_STATIC_ASSERT(BX_COUNTOF(s_uniformTypeName) == UniformType::Count*2);
2016-01-31 08:13:41 +03:00
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
};
2017-11-24 21:01:13 +03:00
2021-10-26 05:26:38 +03:00
void fatal(const char* _filePath, uint16_t _line, Fatal::Enum _code, const char* _format, ...)
{
BX_UNUSED(_filePath, _line, _code);
va_list argList;
va_start(argList, _format);
bx::vprintf(_format, argList);
va_end(argList);
abort();
}
void trace(const char* _filePath, uint16_t _line, const char* _format, ...)
{
BX_UNUSED(_filePath, _line);
va_list argList;
va_start(argList, _format);
bx::vprintf(_format, argList);
va_end(argList);
}
2017-11-28 02:57:31 +03:00
Options::Options()
2017-11-24 21:01:13 +03:00
: shaderType(' ')
, disasm(false)
, raw(false)
, preprocessOnly(false)
, depends(false)
, debugInformation(false)
, avoidFlowControl(false)
, noPreshader(false)
, partialPrecision(false)
, preferFlowControl(false)
, backwardsCompatibility(false)
, warningsAreErrors(false)
, keepIntermediate(false)
2017-11-24 21:01:13 +03:00
, optimize(false)
, optimizationLevel(3)
{
}
void Options::dump()
{
BX_TRACE("Options:\n"
"\t shaderType: %c\n"
"\t platform: %s\n"
"\t profile: %s\n"
"\t inputFile: %s\n"
"\t outputFile: %s\n"
"\t disasm: %s\n"
"\t raw: %s\n"
"\t preprocessOnly: %s\n"
"\t depends: %s\n"
"\t debugInformation: %s\n"
"\t avoidFlowControl: %s\n"
"\t noPreshader: %s\n"
"\t partialPrecision: %s\n"
"\t preferFlowControl: %s\n"
"\t backwardsCompatibility: %s\n"
"\t warningsAreErrors: %s\n"
"\t keepIntermediate: %s\n"
2017-11-24 21:01:13 +03:00
"\t optimize: %s\n"
"\t optimizationLevel: %d\n"
, shaderType
, platform.c_str()
, profile.c_str()
, inputFilePath.c_str()
, outputFilePath.c_str()
, disasm ? "true" : "false"
, raw ? "true" : "false"
, preprocessOnly ? "true" : "false"
, depends ? "true" : "false"
, debugInformation ? "true" : "false"
, avoidFlowControl ? "true" : "false"
, noPreshader ? "true" : "false"
, partialPrecision ? "true" : "false"
, preferFlowControl ? "true" : "false"
, backwardsCompatibility ? "true" : "false"
, warningsAreErrors ? "true" : "false"
, keepIntermediate ? "true" : "false"
2017-11-24 21:01:13 +03:00
, optimize ? "true" : "false"
, optimizationLevel
);
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < includeDirs.size(); ++ii)
{
BX_TRACE("\t include :%s\n", includeDirs[ii].c_str());
}
2017-11-24 21:01:13 +03:00
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < defines.size(); ++ii)
{
BX_TRACE("\t define :%s\n", defines[ii].c_str());
}
2017-11-24 21:01:13 +03:00
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < dependencies.size(); ++ii)
{
BX_TRACE("\t dependency :%s\n", dependencies[ii].c_str());
}
2017-11-24 21:01:13 +03:00
}
2016-01-31 08:13:41 +03:00
const char* interpolationDx11(const char* _glsl)
2012-10-28 08:34:41 +04:00
{
2017-04-23 00:47:02 +03:00
if (0 == bx::strCmp(_glsl, "smooth") )
2016-01-31 08:13:41 +03:00
{
return "linear";
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmp(_glsl, "flat") )
2016-01-31 08:13:41 +03:00
{
return "nointerpolation";
}
2016-07-11 06:49:10 +03:00
return _glsl; // centroid, noperspective
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
const char* getUniformTypeName(UniformType::Enum _enum)
2012-10-28 08:34:41 +04:00
{
2020-06-19 07:20:01 +03:00
uint32_t idx = _enum & ~(kUniformFragmentBit|kUniformSamplerBit);
2016-09-29 02:42:15 +03:00
if (idx < UniformType::Count)
{
return s_uniformTypeName[idx];
}
return "Unknown uniform type?!";
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
UniformType::Enum nameToUniformTypeEnum(const char* _name)
2012-10-28 08:34:41 +04:00
{
for (uint32_t ii = 0; ii < UniformType::Count*2; ++ii)
{
2016-01-31 08:13:41 +03:00
if (NULL != s_uniformTypeName[ii]
2017-04-23 00:47:02 +03:00
&& 0 == bx::strCmp(_name, s_uniformTypeName[ii]) )
{
return UniformType::Enum(ii/2);
}
}
2016-01-31 08:13:41 +03:00
return UniformType::Count;
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
int32_t writef(bx::WriterI* _writer, const char* _format, ...)
2012-10-28 08:34:41 +04:00
{
va_list argList;
va_start(argList, _format);
char temp[2048];
2016-01-31 08:13:41 +03:00
char* out = temp;
int32_t max = sizeof(temp);
int32_t len = bx::vsnprintf(out, max, _format, argList);
if (len > max)
{
out = (char*)alloca(len);
len = bx::vsnprintf(out, len, _format, argList);
}
2012-10-28 08:34:41 +04:00
len = bx::write(_writer, out, len, bx::ErrorAssert{});
2012-10-28 08:34:41 +04:00
va_end(argList);
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
return len;
2012-10-28 08:34:41 +04:00
}
2017-06-12 07:01:38 +03:00
class Bin2cWriter : public bx::FileWriter
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
public:
2018-11-03 03:18:22 +03:00
Bin2cWriter(const bx::StringView& _name)
2016-01-31 08:13:41 +03:00
: m_name(_name)
2012-10-28 08:34:41 +04:00
{
}
2016-01-31 08:13:41 +03:00
virtual ~Bin2cWriter()
{
}
2012-10-28 08:34:41 +04:00
2017-07-15 10:17:29 +03:00
virtual void close() override
2016-01-31 08:13:41 +03:00
{
generate();
2017-06-12 07:01:38 +03:00
return bx::FileWriter::close();
2016-01-31 08:13:41 +03:00
}
2012-10-28 08:34:41 +04:00
2017-07-15 10:17:29 +03:00
virtual int32_t write(const void* _data, int32_t _size, bx::Error*) override
2016-01-31 08:13:41 +03:00
{
const char* data = (const char*)_data;
m_buffer.insert(m_buffer.end(), data, data+_size);
return _size;
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
private:
void generate()
{
2016-02-01 03:00:02 +03:00
#define HEX_DUMP_WIDTH 16
#define HEX_DUMP_SPACE_WIDTH 96
#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
2016-01-31 08:13:41 +03:00
const uint8_t* data = &m_buffer[0];
uint32_t size = (uint32_t)m_buffer.size();
2012-10-28 08:34:41 +04:00
2018-11-03 03:18:22 +03:00
outf("static const uint8_t %.*s[%d] =\n{\n", m_name.getLength(), m_name.getPtr(), size);
2016-01-31 08:13:41 +03:00
if (NULL != data)
{
char hex[HEX_DUMP_SPACE_WIDTH+1];
char ascii[HEX_DUMP_WIDTH+1];
uint32_t hexPos = 0;
uint32_t asciiPos = 0;
for (uint32_t ii = 0; ii < size; ++ii)
{
bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "0x%02x, ", data[asciiPos]);
hexPos += 6;
ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' && data[asciiPos] != '\t' ? data[asciiPos] : '.';
2016-01-31 08:13:41 +03:00
asciiPos++;
2014-02-06 11:07:11 +04:00
2016-01-31 08:13:41 +03:00
if (HEX_DUMP_WIDTH == asciiPos)
{
ascii[asciiPos] = '\0';
outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
data += asciiPos;
hexPos = 0;
asciiPos = 0;
}
}
2016-01-31 08:13:41 +03:00
if (0 != asciiPos)
{
ascii[asciiPos] = '\0';
outf("\t" HEX_DUMP_FORMAT "// %s\n", hex, ascii);
}
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
outf("};\n");
2016-02-01 03:00:02 +03:00
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
int32_t outf(const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
char temp[2048];
char* out = temp;
int32_t max = sizeof(temp);
int32_t len = bx::vsnprintf(out, max, _format, argList);
if (len > max)
{
out = (char*)alloca(len);
len = bx::vsnprintf(out, len, _format, argList);
}
2012-10-28 08:34:41 +04:00
int32_t size = bx::FileWriter::write(out, len, bx::ErrorAssert{});
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
va_end(argList);
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
return size;
}
2012-10-28 08:34:41 +04:00
2018-11-03 03:18:22 +03:00
bx::StringView m_name;
2016-01-31 08:13:41 +03:00
typedef std::vector<uint8_t> Buffer;
Buffer m_buffer;
};
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
struct Varying
{
std::string m_precision;
std::string m_interpolation;
std::string m_name;
std::string m_type;
std::string m_init;
std::string m_semantics;
};
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
typedef std::unordered_map<std::string, Varying> VaryingMap;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
class File
{
public:
2018-11-29 04:28:37 +03:00
File()
2016-01-31 08:13:41 +03:00
: m_data(NULL)
2018-11-29 04:28:37 +03:00
, m_size(0)
{
}
~File()
{
delete [] m_data;
}
void load(const bx::FilePath& _filePath)
{
2017-06-12 07:01:38 +03:00
bx::FileReader reader;
2016-06-22 06:56:39 +03:00
if (bx::open(&reader, _filePath) )
2016-01-31 08:13:41 +03:00
{
m_size = (uint32_t)bx::getSize(&reader);
2016-01-31 08:13:41 +03:00
m_data = new char[m_size+1];
m_size = (uint32_t)bx::read(&reader, m_data, m_size, bx::ErrorAssert{});
2016-06-22 06:56:39 +03:00
bx::close(&reader);
if (m_data[0] == '\xef'
&& m_data[1] == '\xbb'
&& m_data[2] == '\xbf')
{
2017-05-26 06:24:02 +03:00
bx::memMove(m_data, &m_data[3], m_size-3);
2016-06-22 06:56:39 +03:00
m_size -= 3;
}
2016-01-31 08:13:41 +03:00
m_data[m_size] = '\0';
}
}
2016-01-31 08:13:41 +03:00
const char* getData() const
{
return m_data;
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
uint32_t getSize() const
{
return m_size;
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
private:
char* m_data;
uint32_t m_size;
};
2012-10-28 08:34:41 +04:00
char* strInsert(char* _str, const char* _insert)
2012-10-28 08:34:41 +04:00
{
2017-04-17 07:56:17 +03:00
uint32_t len = bx::strLen(_insert);
bx::memMove(&_str[len], _str, bx::strLen(_str) );
2017-03-24 03:33:29 +03:00
bx::memCopy(_str, _insert, len);
return _str + len;
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
void strReplace(char* _str, const char* _find, const char* _replace)
{
2017-04-17 07:56:17 +03:00
const int32_t len = bx::strLen(_find);
2016-01-31 08:13:41 +03:00
char* replace = (char*)alloca(len+1);
2017-04-17 07:56:17 +03:00
bx::strCopy(replace, len+1, _replace);
for (int32_t ii = bx::strLen(replace); ii < len; ++ii)
{
2016-01-31 08:13:41 +03:00
replace[ii] = ' ';
}
2016-01-31 08:13:41 +03:00
replace[len] = '\0';
2020-06-16 20:06:18 +03:00
BX_ASSERT(len >= bx::strLen(_replace), "");
2018-10-21 09:33:31 +03:00
for (bx::StringView ptr = bx::strFind(_str, _find)
; !ptr.isEmpty()
; ptr = bx::strFind(ptr.getPtr() + len, _find)
2017-10-01 02:25:11 +03:00
)
2016-01-31 08:13:41 +03:00
{
2018-10-21 09:33:31 +03:00
bx::memCopy(const_cast<char*>(ptr.getPtr() ), replace, len);
2016-01-31 08:13:41 +03:00
}
}
2016-01-31 08:13:41 +03:00
void strNormalizeEol(char* _str)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
strReplace(_str, "\r\n", "\n");
strReplace(_str, "\r", "\n");
2012-10-28 08:34:41 +04:00
}
2016-10-01 22:38:41 +03:00
void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end, int32_t _column)
2012-10-28 08:34:41 +04:00
{
2019-08-14 05:35:10 +03:00
bx::printf("Code:\n---\n");
2012-10-28 08:34:41 +04:00
2020-06-21 01:14:54 +03:00
bx::LineReader reader(_code);
for (int32_t line = 1; !reader.isDone() && line < _end; ++line)
2012-10-28 08:34:41 +04:00
{
2020-06-21 01:14:54 +03:00
bx::StringView strLine = reader.next();
2020-06-21 01:14:54 +03:00
if (line >= _start)
2016-01-31 08:13:41 +03:00
{
2016-10-01 22:38:41 +03:00
if (_line == line)
{
2019-08-14 05:35:10 +03:00
bx::printf("\n");
2020-06-21 01:14:54 +03:00
bx::printf(">>> %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
2016-10-01 22:38:41 +03:00
if (-1 != _column)
{
2019-08-14 05:35:10 +03:00
bx::printf(">>> %3d: %*s\n", _column, _column, "^");
2016-10-01 22:38:41 +03:00
}
2019-08-14 05:35:10 +03:00
bx::printf("\n");
2016-10-01 22:38:41 +03:00
}
else
{
2020-06-21 01:14:54 +03:00
bx::printf(" %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
2016-10-01 22:38:41 +03:00
}
2016-01-31 08:13:41 +03:00
}
2012-10-28 08:34:41 +04:00
}
2019-08-14 05:35:10 +03:00
bx::printf("---\n");
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
void writeFile(const char* _filePath, const void* _data, int32_t _size)
2012-10-28 08:34:41 +04:00
{
2017-06-12 07:01:38 +03:00
bx::FileWriter out;
2016-02-01 03:00:02 +03:00
if (bx::open(&out, _filePath) )
2016-01-31 08:13:41 +03:00
{
bx::write(&out, _data, _size, bx::ErrorAssert{});
2016-02-01 03:00:02 +03:00
bx::close(&out);
2016-01-31 08:13:41 +03:00
}
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
struct Preprocessor
2012-10-28 08:34:41 +04:00
{
Preprocessor(const char* _filePath, bool _essl, bx::WriterI* _messageWriter)
2016-01-31 08:13:41 +03:00
: m_tagptr(m_tags)
, m_scratchPos(0)
, m_fgetsPos(0)
, m_messageWriter(_messageWriter)
2016-01-31 08:13:41 +03:00
{
m_tagptr->tag = FPPTAG_USERDATA;
m_tagptr->data = this;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_DEPENDS;
m_tagptr->data = (void*)fppDepends;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_INPUT;
m_tagptr->data = (void*)fppInput;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_OUTPUT;
m_tagptr->data = (void*)fppOutput;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_ERROR;
m_tagptr->data = (void*)fppError;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2018-04-25 06:07:38 +03:00
m_tagptr->tag = FPPTAG_SHOWVERSION;
2016-01-31 08:13:41 +03:00
m_tagptr->data = (void*)0;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_LINE;
m_tagptr->data = (void*)0;
m_tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_INPUT_NAME;
m_tagptr->data = scratch(_filePath);
m_tagptr++;
2016-10-01 04:16:04 +03:00
if (!_essl)
{
2016-01-31 08:13:41 +03:00
m_default = "#define lowp\n#define mediump\n#define highp\n";
}
}
2016-01-31 08:13:41 +03:00
void setDefine(const char* _define)
{
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_DEFINE;
m_tagptr->data = scratch(_define);
m_tagptr++;
}
2016-01-31 08:13:41 +03:00
void setDefaultDefine(const char* _name)
{
char temp[1024];
bx::snprintf(temp, BX_COUNTOF(temp)
, "#ifndef %s\n"
"# define %s 0\n"
"#endif // %s\n"
"\n"
, _name
, _name
, _name
);
m_default += temp;
}
2014-02-06 11:07:11 +04:00
2016-01-31 08:13:41 +03:00
void writef(const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
bx::stringPrintfVargs(m_default, _format, argList);
va_end(argList);
}
2014-02-06 11:07:11 +04:00
2016-01-31 08:13:41 +03:00
void addInclude(const char* _includeDir)
{
char* start = scratch(_includeDir);
2014-09-01 22:24:51 +04:00
2018-10-21 09:33:31 +03:00
for (bx::StringView split = bx::strFind(start, ';')
; !split.isEmpty()
; split = bx::strFind(start, ';')
2017-04-04 06:43:57 +03:00
)
2016-01-31 08:13:41 +03:00
{
2018-10-21 09:33:31 +03:00
*const_cast<char*>(split.getPtr() ) = '\0';
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_INCLUDE_DIR;
m_tagptr->data = start;
m_tagptr++;
2018-10-21 09:33:31 +03:00
start = const_cast<char*>(split.getPtr() ) + 1;
2016-01-31 08:13:41 +03:00
}
2014-02-06 11:07:11 +04:00
2016-01-31 08:13:41 +03:00
m_tagptr->tag = FPPTAG_INCLUDE_DIR;
m_tagptr->data = start;
m_tagptr++;
}
2014-02-06 11:07:11 +04:00
2016-01-31 08:13:41 +03:00
void addDependency(const char* _fileName)
{
m_depends += " \\\n ";
m_depends += _fileName;
}
2012-10-28 22:00:53 +04:00
2016-01-31 08:13:41 +03:00
bool run(const char* _input)
{
m_fgetsPos = 0;
2012-10-28 22:00:53 +04:00
2016-01-31 08:13:41 +03:00
m_preprocessed.clear();
m_input = m_default;
m_input += "\n\n";
2012-10-28 08:34:41 +04:00
2017-04-17 07:56:17 +03:00
int32_t len = bx::strLen(_input)+1;
2016-01-31 08:13:41 +03:00
char* temp = new char[len];
2018-10-29 06:48:34 +03:00
bx::StringView normalized = bx::normalizeEolLf(temp, len, _input);
std::string str;
str.assign(normalized.getPtr(), normalized.getTerm() );
m_input += str;
2016-01-31 08:13:41 +03:00
delete [] temp;
2012-10-28 22:00:53 +04:00
2016-01-31 08:13:41 +03:00
fppTag* tagptr = m_tagptr;
2016-01-31 08:13:41 +03:00
tagptr->tag = FPPTAG_END;
tagptr->data = 0;
tagptr++;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
int result = fppPreProcess(m_tags);
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
return 0 == result;
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
char* fgets(char* _buffer, int _size)
{
int ii = 0;
for (char ch = m_input[m_fgetsPos]; m_fgetsPos < m_input.size() && ii < _size-1; ch = m_input[++m_fgetsPos])
{
_buffer[ii++] = ch;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
if (ch == '\n' || ii == _size)
{
_buffer[ii] = '\0';
m_fgetsPos++;
return _buffer;
}
}
2016-01-31 08:13:41 +03:00
return NULL;
}
2016-01-31 08:13:41 +03:00
static void fppDepends(char* _fileName, void* _userData)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
Preprocessor* thisClass = (Preprocessor*)_userData;
thisClass->addDependency(_fileName);
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
static char* fppInput(char* _buffer, int _size, void* _userData)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
Preprocessor* thisClass = (Preprocessor*)_userData;
return thisClass->fgets(_buffer, _size);
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
static void fppOutput(int _ch, void* _userData)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
Preprocessor* thisClass = (Preprocessor*)_userData;
2017-03-12 01:44:00 +03:00
thisClass->m_preprocessed += char(_ch);
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
static void fppError(void* _userData, char* _format, va_list _vargs)
2015-06-13 01:08:01 +03:00
{
bx::ErrorAssert err;
Preprocessor* thisClass = (Preprocessor*)_userData;
bx::write(thisClass->m_messageWriter, _format, _vargs, &err);
2015-06-13 01:08:01 +03:00
}
2016-01-31 08:13:41 +03:00
char* scratch(const char* _str)
2015-03-18 03:27:04 +03:00
{
2016-01-31 08:13:41 +03:00
char* result = &m_scratch[m_scratchPos];
2017-05-26 06:24:02 +03:00
bx::strCopy(result, uint32_t(sizeof(m_scratch)-m_scratchPos), _str);
2017-04-17 07:56:17 +03:00
m_scratchPos += (uint32_t)bx::strLen(_str)+1;
2016-01-31 08:13:41 +03:00
return result;
2015-03-18 03:27:04 +03:00
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
fppTag m_tags[MAX_TAGS];
fppTag* m_tagptr;
std::string m_depends;
std::string m_default;
std::string m_input;
std::string m_preprocessed;
char m_scratch[16<<10];
uint32_t m_scratchPos;
uint32_t m_fgetsPos;
bx::WriterI* m_messageWriter;
2016-01-31 08:13:41 +03:00
};
typedef std::vector<std::string> InOut;
uint32_t parseInOut(InOut& _inout, const bx::StringView& _str)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
uint32_t hash = 0;
bx::StringView str = bx::strLTrimSpace(_str);
2016-01-31 08:13:41 +03:00
if (!str.isEmpty() )
2012-10-28 08:34:41 +04:00
{
bx::StringView delim;
2016-01-31 08:13:41 +03:00
do
2012-10-28 08:34:41 +04:00
{
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() )
2012-10-28 08:34:41 +04:00
{
_inout.push_back(std::string(token.getPtr(), token.getTerm() ) );
str = bx::strLTrimSpace(bx::StringView(delim.getPtr() + 1, str.getTerm() ) );
2012-10-28 08:34:41 +04:00
}
}
while (!delim.isEmpty() );
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
std::sort(_inout.begin(), _inout.end() );
bx::HashMurmur2A murmur;
murmur.begin();
for (InOut::const_iterator it = _inout.begin(), itEnd = _inout.end(); it != itEnd; ++it)
{
murmur.add(it->c_str(), (uint32_t)it->size() );
}
hash = murmur.end();
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
return hash;
2012-10-28 08:34:41 +04:00
}
2016-01-31 08:13:41 +03:00
void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma)
{
char find[32];
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
char replace[32];
bx::snprintf(replace, sizeof(replace), "bgfx_FragData%d", _idx);
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
strReplace(_data, find, replace);
2016-01-31 08:13:41 +03:00
_preprocessor.writef(
" \\\n\t%sout vec4 bgfx_FragData%d : SV_TARGET%d"
2016-01-31 08:13:41 +03:00
, _comma ? ", " : " "
, _idx
, _idx
);
}
2016-01-31 08:13:41 +03:00
void voidFragData(char* _data, uint32_t _idx)
{
2016-01-31 08:13:41 +03:00
char find[32];
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
2016-01-31 08:13:41 +03:00
strReplace(_data, find, "bgfx_VoidFrag");
2012-10-28 08:34:41 +04:00
}
2018-11-03 03:18:22 +03:00
bx::StringView baseName(const bx::StringView& _filePath)
2017-07-14 10:10:33 +03:00
{
bx::FilePath fp(_filePath);
2018-11-03 03:18:22 +03:00
return bx::strFind(_filePath, fp.getBaseName() );
2017-07-14 10:10:33 +03:00
}
2016-01-31 08:13:41 +03:00
void help(const char* _error = NULL)
2012-10-28 08:34:41 +04:00
{
2016-01-31 08:13:41 +03:00
if (NULL != _error)
{
2019-08-14 05:35:10 +03:00
bx::printf("Error:\n%s\n\n", _error);
2016-01-31 08:13:41 +03:00
}
2019-08-14 05:35:10 +03:00
bx::printf(
"shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
2023-01-14 21:05:12 +03:00
"Copyright 2011-2023 Branimir Karadzic. All rights reserved.\n"
2022-01-15 22:59:06 +03:00
"License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE\n\n"
2017-05-11 06:55:31 +03:00
, BGFX_SHADERC_VERSION_MAJOR
, BGFX_SHADERC_VERSION_MINOR
, BGFX_API_VERSION
2016-01-31 08:13:41 +03:00
);
2012-10-28 08:34:41 +04:00
2019-08-14 05:35:10 +03:00
bx::printf(
"Usage: shaderc -f <in> -o <out> --type <v/f/c> --platform <platform>\n"
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
"\n"
"Options:\n"
" -h, --help Display this help and exit.\n"
" -v, --version Output version information and exit.\n"
" -f <file path> Input's file path.\n"
" -i <include path> Include path. (for multiple paths use -i multiple times)\n"
" -o <file path> Output's file path.\n"
" --stdout Output to console.\n"
2018-11-03 03:18:22 +03:00
" --bin2c [array name] Generate C header file. If array name is not specified base file name will be used as name.\n"
2016-01-31 08:13:41 +03:00
" --depends Generate makefile style depends file.\n"
" --platform <platform> Target platform.\n"
" android\n"
" asm.js\n"
" ios\n"
" linux\n"
2019-01-09 01:14:47 +03:00
" orbis\n"
2016-01-31 08:13:41 +03:00
" osx\n"
" windows\n"
" -p, --profile <profile> Shader model. Defaults to GLSL.\n"
);
2020-12-29 04:27:50 +03:00
{
ShadingLang::Enum lang = ShadingLang::Count;
for (uint32_t ii = 0; ii < BX_COUNTOF(s_profiles); ++ii)
{
const Profile& profile = s_profiles[ii];
if (lang != profile.lang)
{
lang = profile.lang;
bx::printf("\n");
bx::printf(" %-20S %s\n", &profile.name, getName(profile.lang) );
2020-12-29 04:27:50 +03:00
}
else
{
bx::printf(" %S\n", &profile.name);
2020-12-29 04:27:50 +03:00
}
}
}
bx::printf(
" --preprocess Only pre-process.\n"
" --define <defines> Add defines to preprocessor. (Semicolon-separated)\n"
" --raw Do not process shader. No preprocessor, and no glsl-optimizer. (GLSL only)\n"
" --type <type> Shader type. Can be 'vertex', 'fragment, or 'compute'.\n"
" --varyingdef <file path> varying.def.sc's file path.\n"
" --verbose Be verbose.\n"
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
"\n"
"(DX9 and DX11 only):\n"
2016-01-31 08:13:41 +03:00
"\n"
" --debug Debug information.\n"
" --disasm Disassemble compiled shader.\n"
" -O <level> Set optimization level. Can be 0 to 3.\n"
2016-01-31 08:13:41 +03:00
" --Werror Treat warnings as errors.\n"
"\n"
"For additional information, see https://github.com/bkaradzic/bgfx\n"
);
}
2016-01-31 08:13:41 +03:00
2018-10-26 08:16:10 +03:00
bx::StringView nextWord(bx::StringView& _parse)
{
bx::StringView word = bx::strWord(bx::strLTrimSpace(_parse) );
_parse = bx::strLTrimSpace(bx::StringView(word.getTerm(), _parse.getTerm() ) );
return word;
}
2023-10-01 02:53:30 +03:00
bool compileShader(const char* _varying, const char* _comment, char* _shader, uint32_t _shaderLen, const Options& _options, bx::WriterI* _shaderWriter, bx::WriterI* _messageWriter)
2017-11-24 21:01:13 +03:00
{
bx::ErrorAssert messageErr;
2023-10-01 02:53:30 +03:00
uint32_t profileId = 0;
2023-10-01 02:53:30 +03:00
const bx::StringView profileOpt(_options.profile.c_str() );
if (!profileOpt.isEmpty() )
2016-01-31 08:13:41 +03:00
{
const uint32_t count = BX_COUNTOF(s_profiles);
2023-10-01 02:53:30 +03:00
for (profileId = 0; profileId < count; ++profileId)
2020-12-29 04:27:50 +03:00
{
2023-10-01 02:53:30 +03:00
if (0 == bx::strCmp(profileOpt, s_profiles[profileId].name) )
2020-12-29 04:27:50 +03:00
{
break;
}
2016-10-16 20:29:46 +03:00
}
2020-12-29 04:27:50 +03:00
2023-10-01 02:53:30 +03:00
if (profileId == count)
2020-12-29 04:27:50 +03:00
{
2023-10-01 02:53:30 +03:00
bx::write(_messageWriter, &messageErr, "Unknown profile: %S\n", &profileOpt);
return false;
2016-01-31 08:13:41 +03:00
}
}
2023-10-01 02:53:30 +03:00
else
{
bx::write(_messageWriter, &messageErr, "Shader profile must be specified.\n");
return false;
}
2020-12-29 04:27:50 +03:00
2023-10-01 02:53:30 +03:00
const Profile* profile = &s_profiles[profileId];
2012-10-28 08:34:41 +04:00
Preprocessor preprocessor(_options.inputFilePath.c_str(), profile->lang == ShadingLang::ESSL, _messageWriter);
2016-10-20 04:11:47 +03:00
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < _options.includeDirs.size(); ++ii)
2018-03-10 06:36:36 +03:00
{
2018-04-20 05:02:00 +03:00
preprocessor.addInclude(_options.includeDirs[ii].c_str() );
2018-03-10 06:36:36 +03:00
}
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < _options.defines.size(); ++ii)
2018-03-10 06:36:36 +03:00
{
2018-04-20 05:02:00 +03:00
preprocessor.setDefine(_options.defines[ii].c_str() );
2018-03-10 06:36:36 +03:00
}
2012-10-28 08:34:41 +04:00
2018-04-20 05:02:00 +03:00
for (size_t ii = 0; ii < _options.dependencies.size(); ++ii)
2018-03-10 06:36:36 +03:00
{
2018-04-20 05:02:00 +03:00
preprocessor.addDependency(_options.dependencies[ii].c_str() );
2018-03-10 06:36:36 +03:00
}
2016-01-31 08:13:41 +03:00
preprocessor.setDefaultDefine("BX_PLATFORM_ANDROID");
preprocessor.setDefaultDefine("BX_PLATFORM_EMSCRIPTEN");
preprocessor.setDefaultDefine("BX_PLATFORM_IOS");
preprocessor.setDefaultDefine("BX_PLATFORM_LINUX");
preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
2016-10-01 04:16:04 +03:00
preprocessor.setDefaultDefine("BX_PLATFORM_PS4");
2016-01-31 08:13:41 +03:00
preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
2016-10-16 20:29:46 +03:00
preprocessor.setDefaultDefine("BX_PLATFORM_XBOXONE");
2016-01-31 08:13:41 +03:00
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL");
2016-10-01 04:16:04 +03:00
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_PSSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_SPIRV");
2016-10-16 20:29:46 +03:00
2016-01-31 08:13:41 +03:00
preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_COMPUTE");
preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");
char glslDefine[128];
if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
{
bx::snprintf(glslDefine, BX_COUNTOF(glslDefine)
2023-10-01 02:53:30 +03:00
, "BGFX_SHADER_LANGUAGE_GLSL=%d"
, profile->id
);
}
2016-01-31 08:13:41 +03:00
char hlslDefine[128];
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::HLSL)
{
bx::snprintf(hlslDefine, BX_COUNTOF(hlslDefine)
2023-10-01 02:53:30 +03:00
, "BGFX_SHADER_LANGUAGE_HLSL=%d"
, profile->id);
}
2017-11-24 21:01:13 +03:00
const char* platform = _options.platform.c_str();
2017-04-23 00:47:02 +03:00
if (0 == bx::strCmpI(platform, "android") )
2016-01-31 08:13:41 +03:00
{
preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
if (profile->lang == ShadingLang::SpirV)
{
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
}
else
{
preprocessor.setDefine(glslDefine);
}
2016-01-31 08:13:41 +03:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "asm.js") )
2016-01-31 08:13:41 +03:00
{
preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
preprocessor.setDefine(glslDefine);
2016-01-31 08:13:41 +03:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "ios") )
2016-01-31 08:13:41 +03:00
{
preprocessor.setDefine("BX_PLATFORM_IOS=1");
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::Metal)
2019-01-23 00:31:41 +03:00
{
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_METAL=1");
}
else
{
preprocessor.setDefine(glslDefine);
2019-01-23 00:31:41 +03:00
}
2016-01-31 08:13:41 +03:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "linux") )
2016-01-31 08:13:41 +03:00
{
preprocessor.setDefine("BX_PLATFORM_LINUX=1");
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::SpirV)
{
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
}
else
{
preprocessor.setDefine(glslDefine);
}
2016-01-31 08:13:41 +03:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "osx") )
2016-01-31 08:13:41 +03:00
{
preprocessor.setDefine("BX_PLATFORM_OSX=1");
2020-12-29 04:27:50 +03:00
if (profile->lang != ShadingLang::Metal)
2019-01-14 11:24:06 +03:00
{
preprocessor.setDefine(glslDefine);
}
2016-01-31 08:13:41 +03:00
char temp[256];
2020-12-29 04:27:50 +03:00
bx::snprintf(
temp
, sizeof(temp)
, "BGFX_SHADER_LANGUAGE_METAL=%d"
, (profile->lang == ShadingLang::Metal) ? profile->id : 0
);
2016-01-31 08:13:41 +03:00
preprocessor.setDefine(temp);
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "windows") )
{
2016-01-31 08:13:41 +03:00
preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::HLSL)
{
preprocessor.setDefine(hlslDefine);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
{
preprocessor.setDefine(glslDefine);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::SpirV)
{
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
}
2016-01-31 08:13:41 +03:00
}
2017-04-23 00:47:02 +03:00
else if (0 == bx::strCmpI(platform, "orbis") )
2016-10-01 04:16:04 +03:00
{
preprocessor.setDefine("BX_PLATFORM_PS4=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_PSSL=1");
2016-10-06 06:41:25 +03:00
preprocessor.setDefine("lit=lit_reserved");
2016-10-01 04:16:04 +03:00
}
else
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::HLSL)
{
preprocessor.setDefine(hlslDefine);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
{
preprocessor.setDefine(glslDefine);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::SpirV)
{
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_SPIRV=1");
}
}
2014-07-21 07:27:13 +04:00
2016-01-31 08:13:41 +03:00
preprocessor.setDefine("M_PI=3.1415926535897932384626433832795");
2017-11-24 21:01:13 +03:00
switch (_options.shaderType)
2016-01-31 08:13:41 +03:00
{
case 'c':
preprocessor.setDefine("BGFX_SHADER_TYPE_COMPUTE=1");
break;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
case 'f':
preprocessor.setDefine("BGFX_SHADER_TYPE_FRAGMENT=1");
break;
2016-01-31 08:13:41 +03:00
case 'v':
preprocessor.setDefine("BGFX_SHADER_TYPE_VERTEX=1");
break;
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
default:
bx::write(_messageWriter, &messageErr, "Unknown type: %c?!", _options.shaderType);
2017-11-24 21:01:13 +03:00
return false;
2016-01-31 08:13:41 +03:00
}
2014-07-21 07:27:13 +04:00
2016-01-31 08:13:41 +03:00
bool compiled = false;
2014-02-06 11:07:11 +04:00
2017-11-24 21:01:13 +03:00
VaryingMap varyingMap;
2018-10-26 08:16:10 +03:00
bx::StringView parse(_varying);
2018-10-21 09:33:31 +03:00
bx::StringView term(parse);
2016-01-31 08:13:41 +03:00
2018-02-17 21:04:42 +03:00
bool usesInterpolationQualifiers = false;
2018-10-26 08:16:10 +03:00
while (!parse.isEmpty() )
2017-11-24 21:01:13 +03:00
{
2018-10-26 08:16:10 +03:00
parse = bx::strLTrimSpace(parse);
2018-10-21 09:33:31 +03:00
bx::StringView eol = bx::strFind(parse, ';');
if (eol.isEmpty() )
2014-07-21 07:27:13 +04:00
{
2018-10-26 08:16:10 +03:00
eol = bx::strFindEol(parse);
2016-01-31 08:13:41 +03:00
}
2018-10-21 09:33:31 +03:00
if (!eol.isEmpty() )
2016-01-31 08:13:41 +03:00
{
2018-10-26 08:16:10 +03:00
eol.set(eol.getPtr() + 1, parse.getTerm() );
bx::StringView precision;
bx::StringView interpolation;
bx::StringView typen = nextWord(parse);
2017-11-24 21:01:13 +03:00
if (0 == bx::strCmp(typen, "lowp", 4)
|| 0 == bx::strCmp(typen, "mediump", 7)
|| 0 == bx::strCmp(typen, "highp", 5) )
{
2017-11-24 21:01:13 +03:00
precision = typen;
2018-10-26 08:16:10 +03:00
typen = nextWord(parse);
}
2014-07-21 07:27:13 +04:00
2017-11-24 21:01:13 +03:00
if (0 == bx::strCmp(typen, "flat", 4)
|| 0 == bx::strCmp(typen, "smooth", 6)
|| 0 == bx::strCmp(typen, "noperspective", 13)
|| 0 == bx::strCmp(typen, "centroid", 8) )
2016-01-31 08:13:41 +03:00
{
if ('f' == _options.shaderType
2020-12-29 04:27:50 +03:00
|| profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
{
interpolation = typen;
usesInterpolationQualifiers = true;
}
2018-10-26 08:16:10 +03:00
typen = nextWord(parse);
2017-11-24 21:01:13 +03:00
}
2014-07-21 07:27:13 +04:00
2018-10-26 08:16:10 +03:00
bx::StringView name = nextWord(parse);
bx::StringView column = bx::strSubstr(parse, 0, 1);
bx::StringView semantics;
if (0 == bx::strCmp(column, ":", 1) )
{
parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
semantics = nextWord(parse);
}
bx::StringView assign = bx::strSubstr(parse, 0, 1);
bx::StringView init;
if (0 == bx::strCmp(assign, "=", 1))
{
parse = bx::strLTrimSpace(bx::StringView(parse.getPtr() + 1, parse.getTerm() ) );
init.set(parse.getPtr(), eol.getPtr() );
}
if (!typen.isEmpty()
&& !name.isEmpty()
&& !semantics.isEmpty() )
2017-11-24 21:01:13 +03:00
{
Varying var;
2018-10-26 08:16:10 +03:00
if (!precision.isEmpty() )
2012-10-28 08:34:41 +04:00
{
2018-10-26 08:16:10 +03:00
var.m_precision.assign(precision.getPtr(), precision.getTerm() );
2012-10-28 08:34:41 +04:00
}
2014-07-21 07:27:13 +04:00
2018-10-26 08:16:10 +03:00
if (!interpolation.isEmpty() )
2014-07-21 07:27:13 +04:00
{
2018-10-26 08:16:10 +03:00
var.m_interpolation.assign(interpolation.getPtr(), interpolation.getTerm() );
2014-07-21 07:27:13 +04:00
}
2018-10-26 08:16:10 +03:00
var.m_type.assign(typen.getPtr(), typen.getTerm() );
var.m_name.assign(name.getPtr(), name.getTerm() );
var.m_semantics.assign(semantics.getPtr(), semantics.getTerm() );
2016-01-31 08:13:41 +03:00
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::HLSL
&& profile->id < 400
2017-11-24 21:01:13 +03:00
&& var.m_semantics == "BITANGENT")
{
var.m_semantics = "BINORMAL";
}
2014-07-21 07:27:13 +04:00
2018-10-26 08:16:10 +03:00
if (!init.isEmpty() )
2017-11-24 21:01:13 +03:00
{
2018-10-26 08:16:10 +03:00
var.m_init.assign(init.getPtr(), init.getTerm() );
2016-01-31 08:13:41 +03:00
}
2014-07-21 07:27:13 +04:00
2017-11-24 21:01:13 +03:00
varyingMap.insert(std::make_pair(var.m_name, var) );
2016-01-31 08:13:41 +03:00
}
2016-06-22 06:56:39 +03:00
2018-10-26 08:16:10 +03:00
parse = bx::strLTrimSpace(bx::strFindNl(bx::StringView(eol.getPtr(), term.getTerm() ) ) );
2017-11-24 21:01:13 +03:00
}
}
2016-06-22 06:56:39 +03:00
2017-11-24 21:01:13 +03:00
bool raw = _options.raw;
2014-07-21 07:27:13 +04:00
2017-11-24 21:01:13 +03:00
InOut shaderInputs;
InOut shaderOutputs;
uint32_t inputHash = 0;
uint32_t outputHash = 0;
bx::ErrorAssert err;
2017-11-24 21:01:13 +03:00
char* data;
char* input;
{
data = _shader;
uint32_t size = _shaderLen;
2014-07-21 07:27:13 +04:00
const size_t padding = 16384;
2014-07-21 07:27:13 +04:00
2017-11-24 21:01:13 +03:00
if (!raw)
{
// To avoid commented code being recognized as used feature,
// first preprocess pass is used to strip all comments before
// substituting code.
2018-09-03 23:56:11 +03:00
bool ok = preprocessor.run(data);
2017-11-24 21:01:13 +03:00
delete [] data;
2016-01-31 08:13:41 +03:00
2018-09-03 23:56:11 +03:00
if (!ok)
{
return false;
}
2017-11-24 21:01:13 +03:00
size = (uint32_t)preprocessor.m_preprocessed.size();
data = new char[size+padding+1];
bx::memCopy(data, preprocessor.m_preprocessed.c_str(), size);
bx::memSet(&data[size], 0, padding+1);
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
strNormalizeEol(data);
2018-10-21 09:33:31 +03:00
input = const_cast<char*>(bx::strLTrimSpace(data).getPtr() );
2017-11-24 21:01:13 +03:00
while (input[0] == '$')
{
bx::StringView str = bx::strLTrimSpace(input+1);
2018-10-21 09:33:31 +03:00
bx::StringView eol = bx::strFindEol(str);
bx::StringView nl = bx::strFindNl(eol);
input = const_cast<char*>(nl.getPtr() );
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (0 == bx::strCmp(str, "input", 5) )
2016-01-31 08:13:41 +03:00
{
str = bx::StringView(str.getPtr() + 5, str.getTerm() );
2018-10-21 09:33:31 +03:00
bx::StringView comment = bx::strFind(str, "//");
eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol;
inputHash = parseInOut(shaderInputs, bx::StringView(str.getPtr(), eol.getPtr() ) );
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
else if (0 == bx::strCmp(str, "output", 6) )
2016-01-31 08:13:41 +03:00
{
str = bx::StringView(str.getPtr() + 6, str.getTerm() );
2018-10-21 09:33:31 +03:00
bx::StringView comment = bx::strFind(str, "//");
eol = !comment.isEmpty() && comment.getPtr() < eol.getPtr() ? comment.getPtr() : eol;
outputHash = parseInOut(shaderOutputs, bx::StringView(str.getPtr(), eol.getPtr() ) );
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
else if (0 == bx::strCmp(str, "raw", 3) )
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
raw = true;
str = bx::StringView(str.getPtr() + 3, str.getTerm() );
2016-01-31 08:13:41 +03:00
}
2018-10-21 09:33:31 +03:00
input = const_cast<char*>(bx::strLTrimSpace(input).getPtr() );
2017-11-24 21:01:13 +03:00
}
}
2016-01-31 08:13:41 +03:00
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::write(_messageWriter, &messageErr,
2019-08-14 05:35:10 +03:00
"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)
2017-11-24 21:01:13 +03:00
{
if ('f' == _options.shaderType)
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_FSH, &err);
2017-11-24 21:01:13 +03:00
}
else if ('v' == _options.shaderType)
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_VSH, &err);
2017-11-24 21:01:13 +03:00
}
else
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
2017-11-24 21:01:13 +03:00
}
2016-01-31 08:13:41 +03:00
bx::write(_shaderWriter, inputHash, &err);
bx::write(_shaderWriter, outputHash, &err);
}
if (raw)
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL)
2017-11-24 21:01:13 +03:00
{
bx::write(_shaderWriter, uint16_t(0), &err);
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
uint32_t shaderSize = (uint32_t)bx::strLen(input);
bx::write(_shaderWriter, shaderSize, &err);
bx::write(_shaderWriter, input, shaderSize, &err);
bx::write(_shaderWriter, uint8_t(0), &err);
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
compiled = true;
}
else if (profile->lang == ShadingLang::Metal)
{
compiled = compileMetalShader(_options, BX_MAKEFOURCC('M', 'T', 'L', 0), input, _shaderWriter, _messageWriter);
}
else if (profile->lang == ShadingLang::SpirV)
{
compiled = compileSPIRVShader(_options, profile->id, input, _shaderWriter, _messageWriter);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::PSSL)
2017-11-24 21:01:13 +03:00
{
compiled = compilePSSLShader(_options, 0, input, _shaderWriter, _messageWriter);
2017-11-24 21:01:13 +03:00
}
else
{
compiled = compileHLSLShader(_options, profile->id, input, _shaderWriter, _messageWriter);
}
2017-11-24 21:01:13 +03:00
}
else if ('c' == _options.shaderType) // Compute
{
2018-10-21 09:33:31 +03:00
bx::StringView entry = bx::strFind(input, "void main()");
if (entry.isEmpty() )
2017-11-24 21:01:13 +03:00
{
bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n");
2017-11-24 21:01:13 +03:00
}
else
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
{
2016-01-31 08:13:41 +03:00
}
else
{
2020-12-29 04:27:50 +03:00
if (profile->lang != ShadingLang::PSSL)
{
preprocessor.writef(getPsslPreamble() );
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
"#define lowp\n"
"#define mediump\n"
"#define highp\n"
"#define ivec2 int2\n"
"#define ivec3 int3\n"
"#define ivec4 int4\n"
"#define uvec2 uint2\n"
"#define uvec3 uint3\n"
"#define uvec4 uint4\n"
"#define vec2 float2\n"
"#define vec3 float3\n"
"#define vec4 float4\n"
"#define mat2 float2x2\n"
"#define mat3 float3x3\n"
"#define mat4 float4x4\n"
);
2018-10-21 09:33:31 +03:00
*const_cast<char*>(entry.getPtr() + 4) = '_';
2017-11-24 21:01:13 +03:00
preprocessor.writef("#define void_main()");
preprocessor.writef(" \\\n\tvoid main(");
uint32_t arg = 0;
2018-10-21 09:33:31 +03:00
const bool hasLocalInvocationID = !bx::strFind(input, "gl_LocalInvocationID").isEmpty();
const bool hasLocalInvocationIndex = !bx::strFind(input, "gl_LocalInvocationIndex").isEmpty();
const bool hasGlobalInvocationID = !bx::strFind(input, "gl_GlobalInvocationID").isEmpty();
const bool hasWorkGroupID = !bx::strFind(input, "gl_WorkGroupID").isEmpty();
2017-11-24 21:01:13 +03:00
if (hasLocalInvocationID)
2015-03-18 03:27:04 +03:00
{
2017-11-24 21:01:13 +03:00
preprocessor.writef(
" \\\n\t%sint3 gl_LocalInvocationID : SV_GroupThreadID"
, arg++ > 0 ? ", " : " "
);
2015-03-18 03:27:04 +03:00
}
2017-11-24 21:01:13 +03:00
if (hasLocalInvocationIndex)
2013-10-02 09:07:40 +04:00
{
preprocessor.writef(
2017-11-24 21:01:13 +03:00
" \\\n\t%sint gl_LocalInvocationIndex : SV_GroupIndex"
, arg++ > 0 ? ", " : " "
2013-10-02 09:07:40 +04:00
);
2017-11-24 21:01:13 +03:00
}
2012-10-28 08:34:41 +04:00
2017-11-24 21:01:13 +03:00
if (hasGlobalInvocationID)
{
preprocessor.writef(
" \\\n\t%sint3 gl_GlobalInvocationID : SV_DispatchThreadID"
, arg++ > 0 ? ", " : " "
);
}
2014-06-26 05:43:26 +04:00
2017-11-24 21:01:13 +03:00
if (hasWorkGroupID)
{
preprocessor.writef(
" \\\n\t%sint3 gl_WorkGroupID : SV_GroupID"
, arg++ > 0 ? ", " : " "
);
}
2012-10-28 08:34:41 +04:00
2017-11-24 21:01:13 +03:00
preprocessor.writef(
" \\\n\t)\n"
);
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (preprocessor.run(input) )
{
if (_options.preprocessOnly)
{
bx::write(
_shaderWriter
, preprocessor.m_preprocessed.c_str()
, (int32_t)preprocessor.m_preprocessed.size()
, &err
);
2017-11-24 21:01:13 +03:00
return true;
}
{
std::string code;
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
bx::write(_shaderWriter, uint32_t(0), &err);
bx::write(_shaderWriter, outputHash, &err);
2014-09-01 22:24:51 +04:00
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
2014-09-01 22:24:51 +04:00
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::ESSL)
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code, "#version 310 es\n");
}
else
{
2020-12-29 04:27:50 +03:00
bx::stringPrintf(
code
, "#version %d\n"
, (profile->lang != ShadingLang::GLSL) ? 430 : profile->id
);
2017-11-24 21:01:13 +03:00
}
code += preprocessor.m_preprocessed;
bx::write(_shaderWriter, uint16_t(0), &err);
2014-09-01 22:24:51 +04:00
2017-11-24 21:01:13 +03:00
uint32_t shaderSize = (uint32_t)code.size();
bx::write(_shaderWriter, shaderSize, &err);
bx::write(_shaderWriter, code.c_str(), shaderSize, &err);
bx::write(_shaderWriter, uint8_t(0), &err);
2017-11-24 21:01:13 +03:00
compiled = true;
}
else
{
code += _comment;
code += preprocessor.m_preprocessed;
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::Metal)
{
compiled = compileMetalShader(_options, BX_MAKEFOURCC('M', 'T', 'L', 0), code, _shaderWriter, _messageWriter);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::SpirV)
{
compiled = compileSPIRVShader(_options, profile->id, code, _shaderWriter, _messageWriter);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::PSSL)
{
compiled = compilePSSLShader(_options, 0, code, _shaderWriter, _messageWriter);
}
else
{
compiled = compileHLSLShader(_options, profile->id, code, _shaderWriter, _messageWriter);
}
}
2016-01-31 08:13:41 +03:00
}
2012-10-28 08:34:41 +04:00
2017-11-24 21:01:13 +03:00
if (compiled)
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
if (_options.depends)
{
2017-11-24 21:01:13 +03:00
std::string ofp = _options.outputFilePath;
ofp += ".d";
2017-06-12 07:01:38 +03:00
bx::FileWriter writer;
2017-11-24 21:01:13 +03:00
if (bx::open(&writer, ofp.c_str() ) )
{
2017-11-24 21:01:13 +03:00
writef(&writer, "%s : %s\n", _options.outputFilePath.c_str(), preprocessor.m_depends.c_str() );
bx::close(&writer);
}
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
}
}
}
else // Vertex/Fragment
{
2018-10-21 09:33:31 +03:00
bx::StringView shader(input);
bx::StringView entry = bx::strFind(shader, "void main()");
if (entry.isEmpty() )
2017-11-24 21:01:13 +03:00
{
bx::write(_messageWriter, &messageErr, "Shader entry point 'void main()' is not found.\n");
2017-11-24 21:01:13 +03:00
}
else
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
2017-11-24 21:01:13 +03:00
{
2020-12-29 04:27:50 +03:00
if (profile->lang != ShadingLang::ESSL)
{
2017-11-24 21:01:13 +03:00
// bgfx shadow2D/Proj behave like EXT_shadow_samplers
// not as GLSL language 1.2 specs shadow2D/Proj.
preprocessor.writef(
"#define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord).x\n"
"#define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord).x\n"
);
}
// gl_FragColor and gl_FragData are deprecated for essl > 300
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::ESSL
&& profile->id >= 300)
{
const bool hasFragColor = !bx::strFind(input, "gl_FragColor").isEmpty();
bool hasFragData[8] = {};
uint32_t numFragData = 0;
for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
{
char temp[32];
bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
hasFragData[ii] = !bx::strFind(input, temp).isEmpty();
numFragData += hasFragData[ii];
}
2020-12-29 04:27:50 +03:00
if (hasFragColor)
{
preprocessor.writef("#define gl_FragColor bgfx_FragColor\n");
preprocessor.writef("out mediump vec4 bgfx_FragColor;\n");
}
2020-12-29 04:27:50 +03:00
else if (numFragData)
{
preprocessor.writef("#define gl_FragData bgfx_FragData\n");
preprocessor.writef("out mediump vec4 bgfx_FragData[gl_MaxDrawBuffers];\n");
}
}
2017-11-24 21:01:13 +03:00
for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
{
2017-11-24 21:01:13 +03:00
const Varying& var = varyingIt->second;
const char* name = var.m_name.c_str();
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (0 == bx::strCmp(name, "a_", 2)
|| 0 == bx::strCmp(name, "i_", 2) )
2016-10-01 04:16:04 +03:00
{
2019-08-16 07:55:09 +03:00
preprocessor.writef(
"attribute %s %s %s %s;\n"
, var.m_precision.c_str()
, var.m_interpolation.c_str()
, var.m_type.c_str()
, name
);
2016-10-01 04:16:04 +03:00
}
else
{
2019-08-16 07:55:09 +03:00
preprocessor.writef(
"%s varying %s %s %s;\n"
, var.m_interpolation.c_str()
, var.m_precision.c_str()
, var.m_type.c_str()
, name
);
}
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
2017-11-24 21:01:13 +03:00
for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
{
2017-11-24 21:01:13 +03:00
const Varying& var = varyingIt->second;
preprocessor.writef("%s varying %s %s;\n"
, var.m_interpolation.c_str()
, var.m_type.c_str()
, var.m_name.c_str()
);
}
}
2016-01-31 08:13:41 +03:00
}
else
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::PSSL)
{
preprocessor.writef(getPsslPreamble() );
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
"#define lowp\n"
"#define mediump\n"
"#define highp\n"
"#define ivec2 int2\n"
"#define ivec3 int3\n"
"#define ivec4 int4\n"
"#define uvec2 uint2\n"
"#define uvec3 uint3\n"
"#define uvec4 uint4\n"
"#define vec2 float2\n"
"#define vec3 float3\n"
"#define vec4 float4\n"
"#define mat2 float2x2\n"
"#define mat3 float3x3\n"
"#define mat4 float4x4\n"
);
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::HLSL
&& profile->id < 400)
2017-11-24 21:01:13 +03:00
{
preprocessor.writef(
"#define centroid\n"
"#define flat\n"
"#define noperspective\n"
"#define smooth\n"
);
}
2018-10-21 09:33:31 +03:00
*const_cast<char*>(entry.getPtr() + 4) = '_';
2017-11-24 21:01:13 +03:00
if ('f' == _options.shaderType)
{
2018-10-21 09:33:31 +03:00
bx::StringView insert = bx::strFind(bx::StringView(entry.getPtr(), shader.getTerm() ), "{");
if (!insert.isEmpty() )
{
2018-10-21 09:33:31 +03:00
insert = strInsert(const_cast<char*>(insert.getPtr()+1), "\nvec4 bgfx_VoidFrag = vec4_splat(0.0);\n");
}
2018-10-21 09:33:31 +03:00
const bool hasFragColor = !bx::strFind(input, "gl_FragColor").isEmpty();
const bool hasFragCoord = !bx::strFind(input, "gl_FragCoord").isEmpty() || profile->id >= 400;
2018-10-21 09:33:31 +03:00
const bool hasFragDepth = !bx::strFind(input, "gl_FragDepth").isEmpty();
const bool hasFrontFacing = !bx::strFind(input, "gl_FrontFacing").isEmpty();
const bool hasPrimitiveId = !bx::strFind(input, "gl_PrimitiveID").isEmpty() && BGFX_CAPS_PRIMITIVE_ID;
if (!hasPrimitiveId)
{
preprocessor.writef("#define gl_PrimitiveID 0\n");
}
2017-11-24 21:01:13 +03:00
bool hasFragData[8] = {};
uint32_t numFragData = 0;
for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
{
2017-11-24 21:01:13 +03:00
char temp[32];
bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
2018-10-21 09:33:31 +03:00
hasFragData[ii] = !bx::strFind(input, temp).isEmpty();
2017-11-24 21:01:13 +03:00
numFragData += hasFragData[ii];
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (0 == numFragData)
{
// GL errors when both gl_FragColor and gl_FragData is used.
// This will trigger the same error with HLSL compiler too.
preprocessor.writef("#define gl_FragColor bgfx_FragData0\n");
2017-11-24 21:01:13 +03:00
// If it has gl_FragData or gl_FragColor, color target at
// index 0 exists, otherwise shader is not modifying color
// targets.
hasFragData[0] |= hasFragColor || profile->id < 400;
2017-11-24 21:01:13 +03:00
2018-10-21 09:33:31 +03:00
if (!insert.isEmpty()
&& profile->id < 400
2017-11-24 21:01:13 +03:00
&& !hasFragColor)
{
2018-10-21 09:33:31 +03:00
insert = strInsert(const_cast<char*>(insert.getPtr()+1), "\ngl_FragColor = bgfx_VoidFrag;\n");
}
}
2017-11-24 21:01:13 +03:00
preprocessor.writef("#define void_main()");
preprocessor.writef(" \\\n\tvoid main(");
uint32_t arg = 0;
if (hasFragCoord)
{
preprocessor.writef(" \\\n\tvec4 gl_FragCoord : SV_POSITION");
++arg;
}
for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
{
const Varying& var = varyingIt->second;
2017-11-24 21:01:13 +03:00
preprocessor.writef(" \\\n\t%s%s %s %s : %s"
, arg++ > 0 ? ", " : " "
, interpolationDx11(var.m_interpolation.c_str() )
2016-01-31 08:13:41 +03:00
, var.m_type.c_str()
, var.m_name.c_str()
2017-11-24 21:01:13 +03:00
, var.m_semantics.c_str()
2016-01-31 08:13:41 +03:00
);
}
}
const uint32_t maxRT = profile->id >= 400 ? BX_COUNTOF(hasFragData) : 4;
2017-11-24 21:01:13 +03:00
for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
{
if (ii < maxRT)
{
if (hasFragData[ii])
{
addFragData(preprocessor, input, ii, arg++ > 0);
}
}
else
{
voidFragData(input, ii);
}
}
if (hasFragDepth)
{
2016-01-31 08:13:41 +03:00
preprocessor.writef(
2017-11-24 21:01:13 +03:00
" \\\n\t%sout float gl_FragDepth : SV_DEPTH"
, arg++ > 0 ? ", " : " "
2016-01-31 08:13:41 +03:00
);
}
if (hasFrontFacing)
2017-11-24 21:01:13 +03:00
{
if (profile->id < 400)
{
preprocessor.writef(
" \\\n\t%sfloat __vface : VFACE"
, arg++ > 0 ? ", " : " "
);
}
else
{
preprocessor.writef(
" \\\n\t%sbool gl_FrontFacing : SV_IsFrontFace"
, arg++ > 0 ? ", " : " "
);
}
2017-11-24 21:01:13 +03:00
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (hasPrimitiveId)
{
if (profile->id >= 400)
2017-11-24 21:01:13 +03:00
{
preprocessor.writef(
" \\\n\t%suint gl_PrimitiveID : SV_PrimitiveID"
, arg++ > 0 ? ", " : " "
);
}
else
{
bx::write(_messageWriter, &messageErr, "gl_PrimitiveID builtin is not supported by D3D9 HLSL.\n");
2017-11-24 21:01:13 +03:00
return false;
}
2017-11-24 21:01:13 +03:00
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
" \\\n\t)\n"
);
2017-11-24 21:01:13 +03:00
if (hasFrontFacing)
{
if (profile->id < 400)
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
preprocessor.writef(
2018-06-09 02:52:09 +03:00
"#define gl_FrontFacing (__vface >= 0.0)\n"
2017-11-24 21:01:13 +03:00
);
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
}
else if ('v' == _options.shaderType)
{
const bool hasVertexId = !bx::strFind(input, "gl_VertexID").isEmpty();
const bool hasInstanceId = !bx::strFind(input, "gl_InstanceID").isEmpty();
const bool hasViewportId = !bx::strFind(input, "gl_ViewportIndex").isEmpty();
const bool hasLayerId = !bx::strFind(input, "gl_Layer").isEmpty();
2018-10-21 09:33:31 +03:00
bx::StringView brace = bx::strFind(bx::StringView(entry.getPtr(), shader.getTerm() ), "{");
if (!brace.isEmpty() )
2017-11-24 21:01:13 +03:00
{
2018-10-29 02:58:46 +03:00
bx::StringView block = bx::strFindBlock(bx::StringView(brace.getPtr(), shader.getTerm() ), '{', '}');
if (!block.isEmpty() )
2016-01-31 08:13:41 +03:00
{
2018-10-31 01:04:17 +03:00
strInsert(const_cast<char*>(block.getTerm()-1), "__RETURN__;\n");
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
"struct Output\n"
"{\n"
"\tvec4 gl_Position : SV_POSITION;\n"
"#define gl_Position _varying_.gl_Position\n"
);
2017-11-24 21:01:13 +03:00
for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
{
2017-11-24 21:01:13 +03:00
const Varying& var = varyingIt->second;
preprocessor.writef(
"\t%s %s %s : %s;\n"
, interpolationDx11(var.m_interpolation.c_str() )
, var.m_type.c_str()
, var.m_name.c_str()
, var.m_semantics.c_str()
);
preprocessor.writef(
"#define %s _varying_.%s\n"
, var.m_name.c_str()
, var.m_name.c_str()
);
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
if (hasViewportId)
{
if (profile->id >= 400)
{
preprocessor.writef(
"\tuint gl_ViewportIndex : SV_ViewportArrayIndex;\n"
"#define gl_ViewportIndex _varying_.gl_ViewportIndex\n"
);
}
else
{
bx::write(_messageWriter, &messageErr, "gl_ViewportIndex builtin is not supported by D3D9 HLSL.\n");
return false;
}
}
if (hasLayerId)
{
if (profile->id >= 400)
{
preprocessor.writef(
"\tuint gl_Layer : SV_RenderTargetArrayIndex;\n"
"#define gl_Layer _varying_.gl_Layer\n"
);
}
else
{
bx::write(_messageWriter, &messageErr, "gl_Layer builtin is not supported by D3D9 HLSL.\n");
return false;
}
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
"};\n"
);
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
preprocessor.writef("#define void_main() \\\n");
preprocessor.writef("Output main(");
uint32_t arg = 0;
for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
const Varying& var = varyingIt->second;
preprocessor.writef(
" \\\n\t%s%s %s : %s"
, arg++ > 0 ? ", " : ""
, var.m_type.c_str()
, var.m_name.c_str()
, var.m_semantics.c_str()
);
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (hasVertexId)
{
if (profile->id >= 400)
2016-01-31 08:13:41 +03:00
{
preprocessor.writef(
2017-11-24 21:01:13 +03:00
" \\\n\t%suint gl_VertexID : SV_VertexID"
2016-01-31 08:13:41 +03:00
, arg++ > 0 ? ", " : " "
);
}
2017-11-24 21:01:13 +03:00
else
{
bx::write(_messageWriter, &messageErr, "gl_VertexID builtin is not supported by D3D9 HLSL.\n");
2017-11-24 21:01:13 +03:00
return false;
}
}
2017-11-24 21:01:13 +03:00
if (hasInstanceId)
{
if (profile->id >= 400)
2016-01-31 08:13:41 +03:00
{
preprocessor.writef(
2017-11-24 21:01:13 +03:00
" \\\n\t%suint gl_InstanceID : SV_InstanceID"
2016-01-31 08:13:41 +03:00
, arg++ > 0 ? ", " : " "
);
2016-01-31 08:13:41 +03:00
}
2017-11-24 21:01:13 +03:00
else
2016-01-31 08:13:41 +03:00
{
bx::write(_messageWriter, &messageErr, "gl_InstanceID builtin is not supported by D3D9 HLSL.\n");
2017-11-24 21:01:13 +03:00
return false;
}
2017-11-24 21:01:13 +03:00
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
preprocessor.writef(
") \\\n"
"{ \\\n"
"\tOutput _varying_;"
);
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
{
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
if (varyingIt != varyingMap.end() )
{
2017-11-24 21:01:13 +03:00
const Varying& var = varyingIt->second;
preprocessor.writef(" \\\n\t%s", var.m_name.c_str() );
if (!var.m_init.empty() )
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
preprocessor.writef(" = %s", var.m_init.c_str() );
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(";");
2016-01-31 08:13:41 +03:00
}
}
2017-11-24 21:01:13 +03:00
preprocessor.writef(
"\n#define __RETURN__ \\\n"
"\t} \\\n"
);
preprocessor.writef(
"\treturn _varying_"
);
}
}
2017-11-24 21:01:13 +03:00
if (preprocessor.run(input) )
{
if (_options.preprocessOnly)
{
bx::write(
_shaderWriter
, preprocessor.m_preprocessed.c_str()
, (int32_t)preprocessor.m_preprocessed.size()
, &err
);
2017-11-24 21:01:13 +03:00
return true;
2016-01-31 08:13:41 +03:00
}
{
std::string code;
2017-11-24 21:01:13 +03:00
if ('f' == _options.shaderType)
2016-01-31 08:13:41 +03:00
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_FSH, &err);
bx::write(_shaderWriter, inputHash, &err);
bx::write(_shaderWriter, uint32_t(0), &err);
2015-06-13 01:08:01 +03:00
}
2017-11-24 21:01:13 +03:00
else if ('v' == _options.shaderType)
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_VSH, &err);
bx::write(_shaderWriter, uint32_t(0), &err);
bx::write(_shaderWriter, outputHash, &err);
2017-11-24 21:01:13 +03:00
}
else
{
bx::write(_shaderWriter, BGFX_CHUNK_MAGIC_CSH, &err);
bx::write(_shaderWriter, uint32_t(0), &err);
bx::write(_shaderWriter, outputHash, &err);
2017-11-24 21:01:13 +03:00
}
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL
|| profile->lang == ShadingLang::ESSL)
2017-11-24 21:01:13 +03:00
{
2019-02-19 00:40:17 +03:00
const bx::StringView preprocessedInput(preprocessor.m_preprocessed.c_str() );
uint32_t glsl_profile = profile->id;
2019-02-19 00:40:17 +03:00
const bool usesBitsToEncoders = true
&& _options.shaderType == 'f'
&& !bx::findIdentifierMatch(preprocessedInput, s_bitsToEncoders).isEmpty()
;
2019-02-19 00:40:17 +03:00
if (!bx::strFind(preprocessedInput, "layout(std430").isEmpty()
|| !bx::strFind(preprocessedInput, "image2D").isEmpty()
|| usesBitsToEncoders)
{
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::GLSL
&& glsl_profile < 430)
{
glsl_profile = 430;
}
2020-12-29 04:27:50 +03:00
else if (glsl_profile < 310)
{
glsl_profile = 310;
}
}
if (glsl_profile < 400)
2016-01-31 08:13:41 +03:00
{
2017-11-24 21:01:13 +03:00
const bool usesTextureLod = false
2018-10-21 09:33:31 +03:00
|| !bx::findIdentifierMatch(input, s_ARB_shader_texture_lod).isEmpty()
|| !bx::findIdentifierMatch(input, s_EXT_shader_texture_lod).isEmpty()
2017-11-24 21:01:13 +03:00
;
const bool usesGpuShader5 = true
&& _options.shaderType != 'f'
&& !bx::findIdentifierMatch(input, s_ARB_gpu_shader5).isEmpty()
;
const bool usesInstanceID = !bx::findIdentifierMatch(input, "gl_InstanceID").isEmpty();
const bool usesGpuShader4 = !bx::findIdentifierMatch(input, s_EXT_gpu_shader4).isEmpty();
const bool usesTexelFetch = !bx::findIdentifierMatch(input, s_texelFetch).isEmpty();
const bool usesTextureMS = !bx::findIdentifierMatch(input, s_ARB_texture_multisample).isEmpty();
const bool usesTextureArray = !bx::findIdentifierMatch(input, s_textureArray).isEmpty();
const bool usesPacking = !bx::findIdentifierMatch(input, s_ARB_shading_language_packing).isEmpty();
const bool usesViewportLayerArray = !bx::findIdentifierMatch(input, s_ARB_shader_viewport_layer_array).isEmpty();
2023-10-01 02:34:19 +03:00
const bool usesUnsignedVecs = !bx::findIdentifierMatch(preprocessedInput, s_unsignedVecs).isEmpty();
2017-11-24 21:01:13 +03:00
2020-12-29 04:27:50 +03:00
if (profile->lang != ShadingLang::ESSL)
2017-11-24 21:01:13 +03:00
{
const bool need130 = (120 == glsl_profile && (false
2018-10-21 09:33:31 +03:00
|| !bx::findIdentifierMatch(input, s_130).isEmpty()
2018-02-17 21:04:42 +03:00
|| usesInterpolationQualifiers
2017-11-24 21:01:13 +03:00
|| usesTexelFetch
|| usesUnsignedVecs
) );
2016-01-31 08:13:41 +03:00
bx::stringPrintf(code, "#version %d\n", need130 ? 130 : glsl_profile);
2016-01-31 08:13:41 +03:00
2018-02-17 21:04:42 +03:00
if (need130)
{
bx::stringPrintf(code, "#define varying %s\n"
, 'f' == _options.shaderType ? "in" : "out"
);
}
2017-11-24 21:01:13 +03:00
if (usesInstanceID)
{
bx::stringPrintf(code
, "#extension GL_ARB_draw_instanced : enable\n"
);
}
2016-01-31 08:13:41 +03:00
if (usesViewportLayerArray)
{
bx::stringPrintf(code
, "#extension GL_ARB_shader_viewport_layer_array : enable\n"
);
}
2017-11-24 21:01:13 +03:00
if (usesGpuShader4)
2016-06-15 19:33:11 +03:00
{
2017-11-24 21:01:13 +03:00
bx::stringPrintf(code
, "#extension GL_EXT_gpu_shader4 : enable\n"
);
2017-11-24 21:01:13 +03:00
}
2017-11-24 21:01:13 +03:00
if (usesGpuShader5)
{
bx::stringPrintf(code
, "#extension GL_ARB_gpu_shader5 : enable\n"
);
}
2017-11-24 21:01:13 +03:00
if (usesPacking)
{
bx::stringPrintf(code
, "#extension GL_ARB_shading_language_packing : enable\n"
);
}
2017-11-24 21:01:13 +03:00
bool ARB_shader_texture_lod = false;
bool EXT_shader_texture_lod = false;
2017-11-24 21:01:13 +03:00
if (usesTextureLod)
{
if ('f' == _options.shaderType)
{
2017-11-24 21:01:13 +03:00
ARB_shader_texture_lod = true;
bx::stringPrintf(code
2017-11-24 21:01:13 +03:00
, "#extension GL_ARB_shader_texture_lod : enable\n"
);
}
2017-11-24 21:01:13 +03:00
else
2017-03-24 03:33:29 +03:00
{
2017-11-24 21:01:13 +03:00
EXT_shader_texture_lod = true;
2017-03-24 03:33:29 +03:00
bx::stringPrintf(code
2017-11-24 21:01:13 +03:00
, "#extension GL_EXT_shader_texture_lod : enable\n"
2017-03-24 03:33:29 +03:00
);
}
2017-11-24 21:01:13 +03:00
}
2017-11-24 21:01:13 +03:00
if (usesTextureMS)
{
bx::stringPrintf(code
, "#extension GL_ARB_texture_multisample : enable\n"
);
}
2017-11-24 21:01:13 +03:00
if (usesTextureArray)
{
bx::stringPrintf(code
, "#extension GL_EXT_texture_array : enable\n"
);
}
2016-03-31 06:32:13 +03:00
if (130 > glsl_profile)
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code,
"#define ivec2 vec2\n"
"#define ivec3 vec3\n"
"#define ivec4 vec4\n"
);
}
2016-08-22 00:03:16 +03:00
2017-11-24 21:01:13 +03:00
if (ARB_shader_texture_lod)
{
bx::stringPrintf(code,
"#define texture2DProjLod texture2DProjLodARB\n"
"#define texture2DGrad texture2DGradARB\n"
"#define texture2DProjGrad texture2DProjGradARB\n"
"#define textureCubeGrad textureCubeGradARB\n"
);
}
else if (EXT_shader_texture_lod)
{
bx::stringPrintf(code,
"#define texture2DProjLod texture2DProjLodEXT\n"
"#define texture2DGrad texture2DGradEXT\n"
"#define texture2DProjGrad texture2DProjGradEXT\n"
"#define textureCubeGrad textureCubeGradEXT\n"
);
}
2016-08-24 08:06:50 +03:00
if (need130 || (glsl_profile >= 130))
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code
, "#define bgfxShadow2D(_sampler, _coord) vec4_splat(texture(_sampler, _coord))\n"
2018-02-17 21:04:42 +03:00
"#define bgfxShadow2DProj(_sampler, _coord) vec4_splat(textureProj(_sampler, _coord))\n"
2017-11-24 21:01:13 +03:00
);
}
else
{
bx::stringPrintf(code
, "#define bgfxShadow2D shadow2D\n"
"#define bgfxShadow2DProj shader2DProj\n"
2017-11-24 21:01:13 +03:00
);
}
}
else
{
2023-10-01 02:34:19 +03:00
if (glsl_profile < 300
&& usesUnsignedVecs)
{
glsl_profile = 300;
}
if (glsl_profile > 100)
2018-02-17 21:04:42 +03:00
{
bx::stringPrintf(code, "#version %d es\n", glsl_profile);
2018-02-17 21:04:42 +03:00
bx::stringPrintf(code, "#define attribute in\n");
bx::stringPrintf(code, "#define varying %s\n"
, 'f' == _options.shaderType ? "in" : "out"
);
bx::stringPrintf(code, "precision highp float;\n");
bx::stringPrintf(code, "precision highp int;\n");
2018-02-17 21:04:42 +03:00
}
if (glsl_profile >= 300 && usesTextureArray)
{
bx::stringPrintf(code, "precision highp sampler2DArray;\n");
}
2017-11-24 21:01:13 +03:00
// Pretend that all extensions are available.
// This will be stripped later.
if (usesTextureLod)
{
bx::stringPrintf(code
, "#extension GL_EXT_shader_texture_lod : enable\n"
2018-02-17 21:04:42 +03:00
"#define texture2DLod texture2DLodEXT\n"
"#define texture2DGrad texture2DGradEXT\n"
"#define texture2DProjLod texture2DProjLodEXT\n"
"#define texture2DProjGrad texture2DProjGradEXT\n"
"#define textureCubeLod textureCubeLodEXT\n"
"#define textureCubeGrad textureCubeGradEXT\n"
2017-11-24 21:01:13 +03:00
);
2017-03-24 03:33:29 +03:00
}
2016-01-31 08:13:41 +03:00
2018-10-21 09:33:31 +03:00
if (!bx::findIdentifierMatch(input, s_OES_standard_derivatives).isEmpty() )
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code, "#extension GL_OES_standard_derivatives : enable\n");
}
2018-10-21 09:33:31 +03:00
if (!bx::findIdentifierMatch(input, s_OES_texture_3D).isEmpty() )
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code, "#extension GL_OES_texture_3D : enable\n");
}
if ((glsl_profile < 300) && (!bx::findIdentifierMatch(input, s_EXT_shadow_samplers).isEmpty()))
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code
, "#extension GL_EXT_shadow_samplers : enable\n"
2018-02-17 21:04:42 +03:00
"#define shadow2D shadow2DEXT\n"
"#define shadow2DProj shadow2DProjEXT\n"
2017-11-24 21:01:13 +03:00
);
}
else
{
bx::stringPrintf(code
, "#define shadow2D(_sampler, _coord) texture(_sampler, _coord)\n"
"#define shadow2DProj(_sampler, _coord) textureProj(_sampler, _coord)\n"
);
}
2016-08-24 08:06:50 +03:00
2017-11-24 21:01:13 +03:00
if (usesGpuShader5)
{
bx::stringPrintf(code
, "#extension GL_ARB_gpu_shader5 : enable\n"
);
2016-08-24 08:06:50 +03:00
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (usesPacking)
{
bx::stringPrintf(code
, "#extension GL_ARB_shading_language_packing : enable\n"
);
}
2016-10-16 20:29:46 +03:00
if ((glsl_profile < 300) && (!bx::findIdentifierMatch(input, "gl_FragDepth").isEmpty() ))
2017-11-24 21:01:13 +03:00
{
bx::stringPrintf(code
, "#extension GL_EXT_frag_depth : enable\n"
2018-02-17 21:04:42 +03:00
"#define gl_FragDepth gl_FragDepthEXT\n"
2017-11-24 21:01:13 +03:00
);
}
2017-11-24 21:01:13 +03:00
if (usesTextureArray)
{
bx::stringPrintf(code
, "#extension GL_EXT_texture_array : enable\n"
);
}
if (glsl_profile == 100)
{
code +=
"mat2 transpose(mat2 _mtx)\n"
"{\n"
" vec2 v0 = _mtx[0];\n"
" vec2 v1 = _mtx[1];\n"
"\n"
" return mat2(\n"
" vec2(v0.x, v1.x)\n"
" , vec2(v0.y, v1.y)\n"
" );\n"
"}\n"
"\n"
"mat3 transpose(mat3 _mtx)\n"
"{\n"
" vec3 v0 = _mtx[0];\n"
" vec3 v1 = _mtx[1];\n"
" vec3 v2 = _mtx[2];\n"
"\n"
" return mat3(\n"
" vec3(v0.x, v1.x, v2.x)\n"
" , vec3(v0.y, v1.y, v2.y)\n"
" , vec3(v0.z, v1.z, v2.z)\n"
" );\n"
"}\n"
"\n"
"mat4 transpose(mat4 _mtx)\n"
"{\n"
" vec4 v0 = _mtx[0];\n"
" vec4 v1 = _mtx[1];\n"
" vec4 v2 = _mtx[2];\n"
" vec4 v3 = _mtx[3];\n"
"\n"
" return mat4(\n"
" vec4(v0.x, v1.x, v2.x, v3.x)\n"
" , vec4(v0.y, v1.y, v2.y, v3.y)\n"
" , vec4(v0.z, v1.z, v2.z, v3.z)\n"
" , vec4(v0.w, v1.w, v2.w, v3.w)\n"
" );\n"
"}\n"
;
}
}
}
2017-11-24 21:01:13 +03:00
else
{
bx::stringPrintf(code, "#version %d\n", glsl_profile);
if (120 < glsl_profile)
{
if (!bx::findIdentifierMatch(input, "gl_FragColor").isEmpty() )
{
bx::stringPrintf(code
, "out vec4 bgfx_FragColor;\n"
"#define gl_FragColor bgfx_FragColor\n"
);
}
}
bx::stringPrintf(code
2022-03-26 23:45:48 +03:00
, "#define texture2D texture\n"
"#define texture2DLod textureLod\n"
"#define texture2DGrad textureGrad\n"
"#define texture2DProjLod textureProjLod\n"
"#define texture2DProjGrad textureProjGrad\n"
"#define textureCubeLod textureLod\n"
"#define textureCubeGrad textureGrad\n"
"#define texture3D texture\n"
2022-03-26 23:45:48 +03:00
"#define texture2DLodOffset textureLodOffset\n"
2018-04-17 04:00:24 +03:00
);
bx::stringPrintf(code, "#define attribute in\n");
bx::stringPrintf(code, "#define varying %s\n"
, 'f' == _options.shaderType ? "in" : "out"
);
bx::stringPrintf(code
, "#define bgfxShadow2D(_sampler, _coord) vec4_splat(texture(_sampler, _coord))\n"
"#define bgfxShadow2DProj(_sampler, _coord) vec4_splat(textureProj(_sampler, _coord))\n"
);
2016-10-01 04:16:04 +03:00
}
2017-11-24 21:01:13 +03:00
2020-12-29 04:27:50 +03:00
if ( (profile->lang == ShadingLang::GLSL && glsl_profile > 400)
|| (profile->lang == ShadingLang::ESSL && glsl_profile > 300) )
2016-10-01 04:16:04 +03:00
{
code += preprocessor.m_preprocessed;
bx::write(_shaderWriter, uint16_t(0), &err);
2017-11-24 21:01:13 +03:00
uint32_t shaderSize = (uint32_t)code.size();
bx::write(_shaderWriter, shaderSize, &err);
bx::write(_shaderWriter, code.c_str(), shaderSize, &err);
bx::write(_shaderWriter, uint8_t(0), &err);
2017-11-24 21:01:13 +03:00
compiled = true;
2016-01-31 08:13:41 +03:00
}
else
{
code += _comment;
code += preprocessor.m_preprocessed;
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::ESSL)
{
glsl_profile |= 0x80000000;
}
2020-12-29 04:27:50 +03:00
compiled = compileGLSLShader(_options, glsl_profile, code, _shaderWriter, _messageWriter);
2016-01-31 08:13:41 +03:00
}
}
2017-11-24 21:01:13 +03:00
else
{
code += _comment;
code += preprocessor.m_preprocessed;
2020-12-29 04:27:50 +03:00
if (profile->lang == ShadingLang::Metal)
{
compiled = compileMetalShader(_options, BX_MAKEFOURCC('M', 'T', 'L', 0), code, _shaderWriter, _messageWriter);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::SpirV)
{
compiled = compileSPIRVShader(_options, profile->id, code, _shaderWriter, _messageWriter);
}
2020-12-29 04:27:50 +03:00
else if (profile->lang == ShadingLang::PSSL)
{
compiled = compilePSSLShader(_options, 0, code, _shaderWriter, _messageWriter);
}
else
{
compiled = compileHLSLShader(_options, profile->id, code, _shaderWriter, _messageWriter);
}
2017-11-24 21:01:13 +03:00
}
}
2016-01-31 08:13:41 +03:00
2017-11-24 21:01:13 +03:00
if (compiled)
{
if (_options.depends)
{
2017-11-24 21:01:13 +03:00
std::string ofp = _options.outputFilePath + ".d";
bx::FileWriter writer;
if (bx::open(&writer, ofp.c_str() ) )
{
2017-11-24 21:01:13 +03:00
writef(&writer, "%s : %s\n", _options.outputFilePath.c_str(), preprocessor.m_depends.c_str() );
bx::close(&writer);
}
}
}
}
2012-10-28 08:34:41 +04:00
}
2017-11-24 21:01:13 +03:00
}
delete [] data;
return compiled;
}
int compileShader(int _argc, const char* _argv[])
{
bx::CommandLine cmdLine(_argc, _argv);
if (cmdLine.hasArg('v', "version") )
{
2019-08-14 05:35:10 +03:00
bx::printf(
"shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
2017-11-24 21:01:13 +03:00
, BGFX_SHADERC_VERSION_MAJOR
, BGFX_SHADERC_VERSION_MINOR
, BGFX_API_VERSION
);
return bx::kExitSuccess;
}
if (cmdLine.hasArg('h', "help") )
{
help();
return bx::kExitFailure;
}
g_verbose = cmdLine.hasArg("verbose");
const char* filePath = cmdLine.findOption('f');
if (NULL == filePath)
{
help("Shader file name must be specified.");
return bx::kExitFailure;
}
bool consoleOut = cmdLine.hasArg("stdout");
2017-11-24 21:01:13 +03:00
const char* outFilePath = cmdLine.findOption('o');
if (NULL == outFilePath && !consoleOut)
2017-11-24 21:01:13 +03:00
{
help("Output file name must be specified or use \"--stdout\" to output to stdout.");
2017-11-24 21:01:13 +03:00
return bx::kExitFailure;
}
const char* type = cmdLine.findOption('\0', "type");
if (NULL == type)
{
help("Must specify shader type.");
return bx::kExitFailure;
}
Options options;
options.inputFilePath = filePath;
options.outputFilePath = consoleOut ? "" : outFilePath;
2017-11-24 21:01:13 +03:00
options.shaderType = bx::toLower(type[0]);
options.disasm = cmdLine.hasArg('\0', "disasm");
const char* platform = cmdLine.findOption('\0', "platform");
if (NULL == platform)
{
platform = "";
}
options.platform = platform;
options.raw = cmdLine.hasArg('\0', "raw");
const char* profile = cmdLine.findOption('p', "profile");
if ( NULL != profile)
{
options.profile = profile;
}
{
options.debugInformation = cmdLine.hasArg('\0', "debug");
options.avoidFlowControl = cmdLine.hasArg('\0', "avoid-flow-control");
options.noPreshader = cmdLine.hasArg('\0', "no-preshader");
options.partialPrecision = cmdLine.hasArg('\0', "partial-precision");
options.preferFlowControl = cmdLine.hasArg('\0', "prefer-flow-control");
2017-11-24 21:01:13 +03:00
options.backwardsCompatibility = cmdLine.hasArg('\0', "backwards-compatibility");
options.warningsAreErrors = cmdLine.hasArg('\0', "Werror");
options.keepIntermediate = cmdLine.hasArg('\0', "keep-intermediate");
2017-11-24 21:01:13 +03:00
uint32_t optimization = 3;
if (cmdLine.hasArg(optimization, 'O') )
{
options.optimize = true;
options.optimizationLevel = optimization;
}
}
2018-11-03 03:18:22 +03:00
bx::StringView bin2c;
2017-11-24 21:01:13 +03:00
if (cmdLine.hasArg("bin2c") )
{
2018-11-03 03:18:22 +03:00
const char* bin2cArg = cmdLine.findOption("bin2c");
if (NULL != bin2cArg)
{
bin2c.set(bin2cArg);
}
else
2017-11-24 21:01:13 +03:00
{
bin2c = baseName(outFilePath);
2018-11-03 03:18:22 +03:00
if (!bin2c.isEmpty() )
2017-11-24 21:01:13 +03:00
{
2018-11-03 03:18:22 +03:00
char* temp = (char*)alloca(bin2c.getLength()+1);
for (uint32_t ii = 0, num = bin2c.getLength(); ii < num; ++ii)
2017-11-24 21:01:13 +03:00
{
2018-11-03 03:18:22 +03:00
char ch = bin2c.getPtr()[ii];
if (bx::isAlphaNum(ch) )
{
temp[ii] = ch;
}
else
{
temp[ii] = '_';
}
2017-11-24 21:01:13 +03:00
}
2018-11-03 03:18:22 +03:00
temp[bin2c.getLength()] = '\0';
bin2c = temp;
}
2017-11-24 21:01:13 +03:00
}
}
2018-01-11 03:47:43 +03:00
options.depends = cmdLine.hasArg("depends");
2017-11-24 21:01:13 +03:00
options.preprocessOnly = cmdLine.hasArg("preprocess");
const char* includeDir = cmdLine.findOption('i');
2018-01-11 03:47:43 +03:00
BX_TRACE("depends: %d", options.depends);
2017-11-24 21:01:13 +03:00
BX_TRACE("preprocessOnly: %d", options.preprocessOnly);
BX_TRACE("includeDir: %s", includeDir);
for (int ii = 1; NULL != includeDir; ++ii)
{
options.includeDirs.push_back(includeDir);
includeDir = cmdLine.findOption(ii, 'i');
}
std::string dir;
{
2018-11-03 03:18:22 +03:00
bx::FilePath fp(filePath);
bx::StringView path(fp.getPath() );
2017-11-24 21:01:13 +03:00
2018-11-03 03:18:22 +03:00
dir.assign(path.getPtr(), path.getTerm() );
options.includeDirs.push_back(dir);
2017-11-24 21:01:13 +03:00
}
const char* defines = cmdLine.findOption("define");
while (NULL != defines
&& '\0' != *defines)
{
2018-10-21 09:33:31 +03:00
defines = bx::strLTrimSpace(defines).getPtr();
bx::StringView eol = bx::strFind(defines, ';');
std::string define(defines, eol.getPtr() );
2017-11-24 21:01:13 +03:00
options.defines.push_back(define.c_str() );
2018-10-21 09:33:31 +03:00
defines = ';' == *eol.getPtr() ? eol.getPtr()+1 : eol.getPtr();
2017-11-24 21:01:13 +03:00
}
std::string commandLineComment = "// shaderc command line:\n//";
for (int32_t ii = 0, num = cmdLine.getNum(); ii < num; ++ii)
{
commandLineComment += " ";
commandLineComment += cmdLine.get(ii);
}
commandLineComment += "\n\n";
2017-11-24 21:01:13 +03:00
bool compiled = false;
bx::FileReader reader;
if (!bx::open(&reader, filePath) )
{
2019-08-14 05:35:10 +03:00
bx::printf("Unable to open file '%s'.\n", filePath);
2017-11-24 21:01:13 +03:00
}
else
{
2018-11-28 05:14:29 +03:00
const char* varying = NULL;
2018-11-29 04:28:37 +03:00
File attribdef;
2018-11-28 05:14:29 +03:00
if ('c' != options.shaderType)
2017-11-24 21:01:13 +03:00
{
2018-11-28 05:14:29 +03:00
std::string defaultVarying = dir + "varying.def.sc";
const char* varyingdef = cmdLine.findOption("varyingdef", defaultVarying.c_str() );
2018-11-29 04:28:37 +03:00
attribdef.load(varyingdef);
2018-11-28 05:14:29 +03:00
varying = attribdef.getData();
if (NULL != varying
&& *varying != '\0')
{
options.dependencies.push_back(varyingdef);
}
else
{
2019-08-14 05:35:10 +03:00
bx::printf("ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef);
2018-11-28 05:14:29 +03:00
}
2017-11-24 21:01:13 +03:00
}
const size_t padding = 16384;
2017-11-24 21:01:13 +03:00
uint32_t size = (uint32_t)bx::getSize(&reader);
char* data = new char[size+padding+1];
size = (uint32_t)bx::read(&reader, data, size, bx::ErrorAssert{});
2017-11-24 21:01:13 +03:00
if (data[0] == '\xef'
&& data[1] == '\xbb'
&& data[2] == '\xbf')
{
bx::memMove(data, &data[3], size-3);
size -= 3;
}
// Compiler generates "error X3000: syntax error: unexpected end of file"
// if input doesn't have empty line at EOF.
data[size] = '\n';
bx::memSet(&data[size+1], 0, padding);
bx::close(&reader);
bx::FileWriter* writer = NULL;
if (!consoleOut)
2017-11-24 21:01:13 +03:00
{
if (!bin2c.isEmpty())
{
writer = new Bin2cWriter(bin2c);
}
else
{
writer = new bx::FileWriter;
}
2017-11-24 21:01:13 +03:00
if (!bx::open(writer, outFilePath))
{
bx::printf("Unable to open output file '%s'.\n", outFilePath);
return bx::kExitFailure;
}
2017-11-24 21:01:13 +03:00
}
compiled = compileShader(varying, commandLineComment.c_str(), data, size, options, consoleOut ? bx::getStdOut() : writer, bx::getStdOut());
2012-10-28 08:34:41 +04:00
if (!consoleOut)
{
bx::close(writer);
delete writer;
}
2016-01-31 08:13:41 +03:00
}
2012-10-28 08:34:41 +04:00
2016-01-31 08:13:41 +03:00
if (compiled)
{
2017-06-21 07:42:23 +03:00
return bx::kExitSuccess;
2016-01-31 08:13:41 +03:00
}
2019-01-19 05:07:29 +03:00
bx::remove(outFilePath);
2012-10-28 08:34:41 +04:00
2019-08-14 05:35:10 +03:00
bx::printf("Failed to build shader.\n");
2017-06-21 07:42:23 +03:00
return bx::kExitFailure;
2016-01-31 08:13:41 +03:00
}
2016-01-31 03:15:25 +03:00
} // namespace bgfx
int main(int _argc, const char* _argv[])
{
return bgfx::compileShader(_argc, _argv);
}