2012-10-28 08:34:41 +04:00
|
|
|
/*
|
2017-01-01 11:18:41 +03:00
|
|
|
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
|
2016-01-01 11:11:04 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
2012-10-28 08:34:41 +04:00
|
|
|
*/
|
|
|
|
|
2014-12-04 07:16:20 +03:00
|
|
|
#include "shaderc.h"
|
2017-01-09 02:55:14 +03:00
|
|
|
#include <bx/commandline.h>
|
2014-04-16 06:10:56 +04:00
|
|
|
|
2012-10-28 08:34:41 +04:00
|
|
|
#define MAX_TAGS 256
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <fpp.h>
|
|
|
|
} // extern "C"
|
|
|
|
|
2017-05-11 06:55:31 +03:00
|
|
|
#define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', 0x2)
|
|
|
|
#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x4)
|
|
|
|
#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x4)
|
|
|
|
|
|
|
|
#define BGFX_SHADERC_VERSION_MAJOR 1
|
2017-06-07 03:25:17 +03:00
|
|
|
#define BGFX_SHADERC_VERSION_MINOR 3
|
2017-05-11 06:55:31 +03:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
namespace bgfx
|
2014-02-10 04:46:50 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
bool g_verbose = false;
|
2014-02-10 04:46:50 +04:00
|
|
|
|
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
|
|
|
|
};
|
2012-11-26 06:24:50 +04:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2017-04-24 01:53:15 +03:00
|
|
|
static const char* s_EXT_gpu_shader4[] =
|
|
|
|
{
|
|
|
|
"gl_VertexID",
|
|
|
|
"gl_InstanceID",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-03-18 21:32:04 +03:00
|
|
|
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",
|
|
|
|
"isampler3D",
|
|
|
|
"usampler3D",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-08-22 00:03:16 +03:00
|
|
|
static const char* s_textureArray[] =
|
|
|
|
{
|
|
|
|
"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
|
|
|
|
};
|
|
|
|
|
2016-11-01 06:33:14 +03:00
|
|
|
const char* s_uniformTypeName[] =
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
2016-11-01 06:33:14 +03:00
|
|
|
"int", "int",
|
|
|
|
NULL, NULL,
|
|
|
|
"vec4", "float4",
|
|
|
|
"mat3", "float3x3",
|
|
|
|
"mat4", "float4x4",
|
2016-01-31 08:13:41 +03:00
|
|
|
};
|
2016-11-01 06:33:14 +03:00
|
|
|
BX_STATIC_ASSERT(BX_COUNTOF(s_uniformTypeName) == UniformType::Count*2);
|
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
|
|
|
{
|
2016-09-29 02:42:15 +03:00
|
|
|
uint32_t idx = _enum & ~(BGFX_UNIFORM_FRAGMENTBIT|BGFX_UNIFORM_SAMPLERBIT);
|
|
|
|
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
|
|
|
{
|
2016-11-01 06:33:14 +03:00
|
|
|
for (uint32_t ii = 0; ii < UniformType::Count*2; ++ii)
|
2012-11-26 06:24:50 +04:00
|
|
|
{
|
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]) )
|
2012-11-26 06:24:50 +04:00
|
|
|
{
|
2016-11-01 06:33:14 +03:00
|
|
|
return UniformType::Enum(ii/2);
|
2012-11-26 06:24:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-11-26 06:24:50 +04: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
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
len = bx::write(_writer, out, len);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2012-11-26 06:24:50 +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:
|
|
|
|
Bin2cWriter(const char* _name)
|
|
|
|
: 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
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
virtual void close() BX_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
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
virtual int32_t write(const void* _data, int32_t _size, bx::Error*) BX_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
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
outf("static const uint8_t %s[%d] =\n{\n", m_name.c_str(), size);
|
2014-02-10 04:46:50 +04:00
|
|
|
|
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;
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
ascii[asciiPos] = isprint(data[asciiPos]) && data[asciiPos] != '\\' ? data[asciiPos] : '.';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 04:16:11 +03:00
|
|
|
|
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
|
2014-02-09 09:48:35 +04:00
|
|
|
}
|
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
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::Error err;
|
2017-06-12 07:01:38 +03:00
|
|
|
int32_t size = bx::FileWriter::write(out, len, &err);
|
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
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string m_filePath;
|
|
|
|
std::string m_name;
|
|
|
|
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:
|
|
|
|
File(const char* _filePath)
|
|
|
|
: m_data(NULL)
|
2013-04-02 09:52:06 +04:00
|
|
|
{
|
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
|
|
|
{
|
2016-06-29 12:39:57 +03:00
|
|
|
m_size = (uint32_t)bx::getSize(&reader);
|
2016-01-31 08:13:41 +03:00
|
|
|
m_data = new char[m_size+1];
|
2016-06-29 12:39:57 +03:00
|
|
|
m_size = (uint32_t)bx::read(&reader, m_data, m_size);
|
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';
|
|
|
|
}
|
2013-04-02 09:52:06 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
~File()
|
2014-02-02 09:15:14 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
delete [] m_data;
|
2014-02-02 09:15:14 +04:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
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
|
|
|
|
2016-07-02 06:38:12 +03: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);
|
2016-07-02 06:38:12 +03:00
|
|
|
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)
|
2013-05-21 09:20:11 +04:00
|
|
|
{
|
2017-04-17 07:56:17 +03:00
|
|
|
const int32_t len = bx::strLen(_find);
|
2013-05-21 09:20:11 +04:00
|
|
|
|
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)
|
2013-05-21 09:20:11 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
replace[ii] = ' ';
|
2013-05-21 09:20:11 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
replace[len] = '\0';
|
2013-05-21 09:20:11 +04:00
|
|
|
|
2017-04-17 07:56:17 +03:00
|
|
|
BX_CHECK(len >= bx::strLen(_replace), "");
|
2017-04-23 00:47:02 +03:00
|
|
|
for (const char* ptr = bx::strFind(_str, _find); NULL != ptr; ptr = bx::strFind(ptr + len, _find) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
2017-03-24 03:33:29 +03:00
|
|
|
bx::memCopy(const_cast<char*>(ptr), replace, len);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2013-05-21 09:20:11 +04: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
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "Code:\n---\n");
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
bx::Error err;
|
|
|
|
LineReader reader(_code);
|
|
|
|
for (int32_t line = 1; err.isOk() && line < _end; ++line)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2017-06-06 03:37:26 +03:00
|
|
|
char str[4096];
|
|
|
|
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
|
|
|
|
|
|
|
|
if (err.isOk()
|
|
|
|
&& line >= _start)
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
2017-06-06 03:37:26 +03:00
|
|
|
std::string strLine(str, len);
|
|
|
|
|
2016-10-01 22:38:41 +03:00
|
|
|
if (_line == line)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n");
|
2017-06-06 03:37:26 +03:00
|
|
|
fprintf(stderr, ">>> %3d: %s", line, strLine.c_str() );
|
2016-10-01 22:38:41 +03:00
|
|
|
if (-1 != _column)
|
|
|
|
{
|
|
|
|
fprintf(stderr, ">>> %3d: %*s\n", _column, _column, "^");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 03:37:26 +03:00
|
|
|
fprintf(stderr, " %3d: %s", line, strLine.c_str() );
|
2016-10-01 22:38:41 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "---\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
|
|
|
{
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::write(&out, _data, _size);
|
|
|
|
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
|
|
|
{
|
2016-10-20 04:11:47 +03:00
|
|
|
Preprocessor(const char* _filePath, bool _essl)
|
2016-01-31 08:13:41 +03:00
|
|
|
: m_tagptr(m_tags)
|
|
|
|
, m_scratchPos(0)
|
|
|
|
, m_fgetsPos(0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
m_tagptr->tag = FPPTAG_IGNOREVERSION;
|
|
|
|
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++;
|
2012-11-26 06:24:50 +04:00
|
|
|
|
2016-10-01 04:16:04 +03:00
|
|
|
if (!_essl)
|
2012-11-26 06:24:50 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
m_default = "#define lowp\n#define mediump\n#define highp\n";
|
2012-11-26 06:24:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
void setDefine(const char* _define)
|
2012-11-26 06:24:50 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
m_tagptr->tag = FPPTAG_DEFINE;
|
|
|
|
m_tagptr->data = scratch(_define);
|
|
|
|
m_tagptr++;
|
2012-11-26 06:24:50 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
for (char* split = const_cast<char*>(bx::strFind(start, ';') )
|
2017-04-04 06:43:57 +03:00
|
|
|
; NULL != split
|
2017-04-23 00:47:02 +03:00
|
|
|
; split = const_cast<char*>(bx::strFind(start, ';') )
|
2017-04-04 06:43:57 +03:00
|
|
|
)
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
*split = '\0';
|
|
|
|
m_tagptr->tag = FPPTAG_INCLUDE_DIR;
|
|
|
|
m_tagptr->data = start;
|
|
|
|
m_tagptr++;
|
|
|
|
start = split + 1;
|
|
|
|
}
|
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];
|
|
|
|
bx::eolLF(temp, len, _input);
|
|
|
|
m_input += temp;
|
|
|
|
delete [] temp;
|
2012-10-28 22:00:53 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
fppTag* tagptr = m_tagptr;
|
2014-04-16 06:10:56 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
return NULL;
|
2014-08-12 07:34:52 +04:00
|
|
|
}
|
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
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
vfprintf(stderr, _format, _vargs);
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<std::string> InOut;
|
|
|
|
|
|
|
|
uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
uint32_t hash = 0;
|
|
|
|
_str = bx::strws(_str);
|
|
|
|
|
|
|
|
if (_str < _eol)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* delim;
|
|
|
|
do
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
delim = strpbrk(_str, " ,");
|
|
|
|
if (NULL != delim)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
delim = delim > _eol ? _eol : delim;
|
|
|
|
std::string token;
|
|
|
|
token.assign(_str, delim-_str);
|
|
|
|
_inout.push_back(token);
|
|
|
|
_str = bx::strws(delim + 1);
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
while (delim < _eol && _str < _eol && NULL != delim);
|
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), "gl_FragData_%d_", _idx);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
strReplace(_data, find, replace);
|
2013-05-21 09:20:11 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
_preprocessor.writef(
|
|
|
|
" \\\n\t%sout vec4 gl_FragData_%d_ : SV_TARGET%d"
|
|
|
|
, _comma ? ", " : " "
|
|
|
|
, _idx
|
|
|
|
, _idx
|
|
|
|
);
|
2013-05-21 09:20:11 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
void voidFragData(char* _data, uint32_t _idx)
|
2016-01-23 07:27:03 +03:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
char find[32];
|
|
|
|
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
|
2016-01-23 07:27:03 +03:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
strReplace(_data, find, "bgfx_VoidFrag");
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
// 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
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:\n%s\n\n", _error);
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr
|
2017-05-11 06:55:31 +03:00
|
|
|
, "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
|
2017-01-01 11:18:41 +03:00
|
|
|
"Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
|
2016-01-31 08:13:41 +03:00
|
|
|
"License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\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
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr
|
|
|
|
, "Usage: shaderc -f <in> -o <out> --type <v/f> --platform <platform>\n"
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
2017-05-11 06:55:31 +03:00
|
|
|
" -h, --help Help.\n"
|
|
|
|
" -v, --version Version information only.\n"
|
2016-01-31 08:13:41 +03:00
|
|
|
" -f <file path> Input file path.\n"
|
2017-01-15 20:51:30 +03:00
|
|
|
" -i <include path> Include path (for multiple paths use use -i multiple times).\n"
|
2016-01-31 08:13:41 +03:00
|
|
|
" -o <file path> Output file path.\n"
|
|
|
|
" --bin2c <file path> Generate C header file.\n"
|
|
|
|
" --depends Generate makefile style depends file.\n"
|
|
|
|
" --platform <platform> Target platform.\n"
|
|
|
|
" android\n"
|
|
|
|
" asm.js\n"
|
|
|
|
" ios\n"
|
|
|
|
" linux\n"
|
|
|
|
" nacl\n"
|
|
|
|
" osx\n"
|
|
|
|
" windows\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"
|
|
|
|
" --type <type> Shader type (vertex, fragment)\n"
|
|
|
|
" --varyingdef <file path> Path to varying.def.sc file.\n"
|
|
|
|
" --verbose Verbose.\n"
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
"\n"
|
|
|
|
"Options (DX9 and DX11 only):\n"
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
"\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"
|
|
|
|
|
|
|
|
"\n"
|
|
|
|
"For additional information, see https://github.com/bkaradzic/bgfx\n"
|
|
|
|
);
|
2014-11-23 20:37:13 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
int compileShader(int _argc, const char* _argv[])
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
bx::CommandLine cmdLine(_argc, _argv);
|
|
|
|
|
2017-05-11 06:55:31 +03:00
|
|
|
if (cmdLine.hasArg('v', "version") )
|
|
|
|
{
|
|
|
|
fprintf(stderr
|
|
|
|
, "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n"
|
|
|
|
, BGFX_SHADERC_VERSION_MAJOR
|
|
|
|
, BGFX_SHADERC_VERSION_MINOR
|
|
|
|
, BGFX_API_VERSION
|
|
|
|
);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (cmdLine.hasArg('h', "help") )
|
2013-05-18 09:13:32 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
help();
|
|
|
|
return EXIT_FAILURE;
|
2013-05-18 09:13:32 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
g_verbose = cmdLine.hasArg("verbose");
|
|
|
|
|
|
|
|
const char* filePath = cmdLine.findOption('f');
|
|
|
|
if (NULL == filePath)
|
2015-07-30 21:33:28 +03:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
help("Shader file name must be specified.");
|
|
|
|
return EXIT_FAILURE;
|
2015-07-30 21:33:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* outFilePath = cmdLine.findOption('o');
|
|
|
|
if (NULL == outFilePath)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
help("Output file name must be specified.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-06-26 05:43:26 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* type = cmdLine.findOption('\0', "type");
|
|
|
|
if (NULL == type)
|
|
|
|
{
|
|
|
|
help("Must specify shader type.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-06-26 05:43:26 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* platform = cmdLine.findOption('\0', "platform");
|
|
|
|
if (NULL == platform)
|
|
|
|
{
|
2016-10-16 20:29:46 +03:00
|
|
|
platform = "";
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2014-06-26 05:43:26 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
bool raw = cmdLine.hasArg('\0', "raw");
|
2014-06-26 05:43:26 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
uint32_t glsl = 0;
|
|
|
|
uint32_t essl = 0;
|
2016-10-01 04:16:04 +03:00
|
|
|
uint32_t hlsl = 0;
|
2016-01-31 08:13:41 +03:00
|
|
|
uint32_t d3d = 11;
|
|
|
|
uint32_t metal = 0;
|
2016-10-01 04:16:04 +03:00
|
|
|
uint32_t pssl = 0;
|
2016-10-16 20:29:46 +03:00
|
|
|
uint32_t spirv = 0;
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* profile = cmdLine.findOption('p', "profile");
|
|
|
|
if (NULL != profile)
|
|
|
|
{
|
2017-04-23 00:47:02 +03:00
|
|
|
if (0 == bx::strCmp(&profile[1], "s_4_0_level", 11) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
hlsl = 2;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(&profile[1], "s_3", 3) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
hlsl = 3;
|
|
|
|
d3d = 9;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(&profile[1], "s_4", 3) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
hlsl = 4;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(&profile[1], "s_5", 3) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
hlsl = 5;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(profile, "metal") )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
metal = 1;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(profile, "pssl") )
|
2016-10-01 04:16:04 +03:00
|
|
|
{
|
|
|
|
pssl = 1;
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(profile, "spirv") )
|
2016-10-16 20:29:46 +03:00
|
|
|
{
|
|
|
|
spirv = 1;
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
glsl = atoi(profile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
essl = 2;
|
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* bin2c = NULL;
|
|
|
|
if (cmdLine.hasArg("bin2c") )
|
|
|
|
{
|
|
|
|
bin2c = cmdLine.findOption("bin2c");
|
|
|
|
if (NULL == bin2c)
|
|
|
|
{
|
|
|
|
bin2c = bx::baseName(outFilePath);
|
2017-04-17 07:56:17 +03:00
|
|
|
uint32_t len = (uint32_t)bx::strLen(bin2c);
|
2016-01-31 08:13:41 +03:00
|
|
|
char* temp = (char*)alloca(len+1);
|
|
|
|
for (char *out = temp; *bin2c != '\0';)
|
|
|
|
{
|
|
|
|
char ch = *bin2c++;
|
|
|
|
if (isalnum(ch) )
|
2015-01-16 04:32:43 +03:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
*out++ = ch;
|
2015-01-16 04:32:43 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
*out++ = '_';
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
temp[len] = '\0';
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
bin2c = temp;
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
bool depends = cmdLine.hasArg("depends");
|
|
|
|
bool preprocessOnly = cmdLine.hasArg("preprocess");
|
|
|
|
const char* includeDir = cmdLine.findOption('i');
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2017-01-19 22:31:14 +03:00
|
|
|
BX_TRACE("depends: %d", depends);
|
|
|
|
BX_TRACE("preprocessOnly: %d", preprocessOnly);
|
|
|
|
BX_TRACE("includeDir: %s", includeDir);
|
|
|
|
|
2016-10-20 04:11:47 +03:00
|
|
|
Preprocessor preprocessor(filePath, 0 != essl);
|
|
|
|
|
|
|
|
for (int ii = 1; NULL != includeDir; ++ii)
|
|
|
|
{
|
|
|
|
preprocessor.addInclude(includeDir);
|
|
|
|
includeDir = cmdLine.findOption(ii, 'i');
|
|
|
|
}
|
2013-02-15 05:27:10 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string dir;
|
|
|
|
{
|
|
|
|
const char* base = bx::baseName(filePath);
|
2014-11-01 07:54:35 +03:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (base != filePath)
|
|
|
|
{
|
|
|
|
dir.assign(filePath, base-filePath);
|
|
|
|
preprocessor.addInclude(dir.c_str() );
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* defines = cmdLine.findOption("define");
|
|
|
|
while (NULL != defines
|
|
|
|
&& '\0' != *defines)
|
|
|
|
{
|
|
|
|
defines = bx::strws(defines);
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* eol = bx::strFind(defines, ';');
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL == eol)
|
2013-02-15 05:27:10 +04:00
|
|
|
{
|
2017-04-17 07:56:17 +03:00
|
|
|
eol = defines + bx::strLen(defines);
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string define(defines, eol);
|
|
|
|
preprocessor.setDefine(define.c_str() );
|
|
|
|
defines = ';' == *eol ? eol+1 : eol;
|
2014-05-11 07:51:44 +04: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");
|
|
|
|
preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
|
2016-10-16 20:29:46 +03:00
|
|
|
preprocessor.setDefaultDefine("BX_PLATFORM_XBOXONE");
|
|
|
|
|
2016-10-01 04:16:04 +03:00
|
|
|
// preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_ESSL");
|
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");
|
2016-11-04 08:00:55 +03:00
|
|
|
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];
|
2016-10-21 05:52:56 +03:00
|
|
|
bx::snprintf(glslDefine, BX_COUNTOF(glslDefine)
|
|
|
|
, "BGFX_SHADER_LANGUAGE_GLSL=%d"
|
2016-11-04 08:00:55 +03:00
|
|
|
, essl ? 1 : glsl
|
2016-10-21 05:52:56 +03:00
|
|
|
);
|
2016-01-31 08:13:41 +03:00
|
|
|
|
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");
|
|
|
|
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
|
|
|
|
}
|
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("BGFX_SHADER_LANGUAGE_GLSL=1");
|
|
|
|
}
|
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");
|
|
|
|
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
|
|
|
|
}
|
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");
|
2016-11-04 08:00:55 +03:00
|
|
|
if (0 != 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");
|
|
|
|
preprocessor.setDefine(glslDefine);
|
|
|
|
char temp[256];
|
|
|
|
bx::snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_METAL=%d", metal);
|
|
|
|
preprocessor.setDefine(temp);
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmpI(platform, "windows") )
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.setDefine("BX_PLATFORM_WINDOWS=1");
|
|
|
|
char temp[256];
|
|
|
|
bx::snprintf(temp, sizeof(temp), "BGFX_SHADER_LANGUAGE_HLSL=%d", hlsl);
|
|
|
|
preprocessor.setDefine(temp);
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmpI(platform, "xbox360") )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
preprocessor.setDefine("BX_PLATFORM_XBOX360=1");
|
|
|
|
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_HLSL=3");
|
|
|
|
}
|
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
|
|
|
}
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.setDefine("M_PI=3.1415926535897932384626433832795");
|
2012-11-04 00:12:26 +04:00
|
|
|
|
2017-03-12 01:44:00 +03:00
|
|
|
char shaderType = bx::toLower(type[0]);
|
2016-01-31 08:13:41 +03:00
|
|
|
switch (shaderType)
|
|
|
|
{
|
|
|
|
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;
|
2013-02-15 05:27:10 +04:00
|
|
|
|
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:
|
|
|
|
fprintf(stderr, "Unknown type: %s?!", type);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
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-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
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open file '%s'.\n", filePath);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
VaryingMap varyingMap;
|
|
|
|
|
|
|
|
std::string defaultVarying = dir + "varying.def.sc";
|
|
|
|
const char* varyingdef = cmdLine.findOption("varyingdef", defaultVarying.c_str() );
|
|
|
|
File attribdef(varyingdef);
|
|
|
|
const char* parse = attribdef.getData();
|
|
|
|
if (NULL != parse
|
|
|
|
&& *parse != '\0')
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.addDependency(varyingdef);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (NULL != parse
|
|
|
|
&& *parse != '\0')
|
|
|
|
{
|
|
|
|
parse = bx::strws(parse);
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* eol = bx::strFind(parse, ';');
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL == eol)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
eol = bx::streol(parse);
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL != eol)
|
|
|
|
{
|
|
|
|
const char* precision = NULL;
|
|
|
|
const char* interpolation = NULL;
|
|
|
|
const char* typen = parse;
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
if (0 == bx::strCmp(typen, "lowp", 4)
|
|
|
|
|| 0 == bx::strCmp(typen, "mediump", 7)
|
|
|
|
|| 0 == bx::strCmp(typen, "highp", 5) )
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
precision = typen;
|
|
|
|
typen = parse = bx::strws(bx::strword(parse) );
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2017-04-23 00:47:02 +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) )
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
interpolation = typen;
|
|
|
|
typen = parse = bx::strws(bx::strword(parse) );
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* name = parse = bx::strws(bx::strword(parse) );
|
|
|
|
const char* column = parse = bx::strws(bx::strword(parse) );
|
|
|
|
const char* semantics = parse = bx::strws( (*parse == ':' ? ++parse : parse) );
|
|
|
|
const char* assign = parse = bx::strws(bx::strword(parse) );
|
|
|
|
const char* init = parse = bx::strws( (*parse == '=' ? ++parse : parse) );
|
|
|
|
|
|
|
|
if (typen < eol
|
|
|
|
&& name < eol
|
|
|
|
&& column < eol
|
|
|
|
&& ':' == *column
|
|
|
|
&& semantics < eol)
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
Varying var;
|
|
|
|
if (NULL != precision)
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
var.m_precision.assign(precision, bx::strword(precision)-precision);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL != interpolation)
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
var.m_interpolation.assign(interpolation, bx::strword(interpolation)-interpolation);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
var.m_type.assign(typen, bx::strword(typen)-typen);
|
|
|
|
var.m_name.assign(name, bx::strword(name)-name);
|
|
|
|
var.m_semantics.assign(semantics, bx::strword(semantics)-semantics);
|
|
|
|
|
|
|
|
if (d3d == 9
|
|
|
|
&& var.m_semantics == "BITANGENT")
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
var.m_semantics = "BINORMAL";
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (assign < eol
|
|
|
|
&& '=' == *assign
|
|
|
|
&& init < eol)
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
var.m_init.assign(init, eol-init);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
varyingMap.insert(std::make_pair(var.m_name, var) );
|
|
|
|
}
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
parse = bx::strws(bx::strnl(eol) );
|
|
|
|
}
|
|
|
|
}
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
InOut shaderInputs;
|
|
|
|
InOut shaderOutputs;
|
|
|
|
uint32_t inputHash = 0;
|
|
|
|
uint32_t outputHash = 0;
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
char* data;
|
|
|
|
char* input;
|
|
|
|
{
|
2016-07-02 06:38:12 +03:00
|
|
|
const size_t padding = 4096;
|
2016-06-22 06:56:39 +03:00
|
|
|
uint32_t size = (uint32_t)bx::getSize(&reader);
|
2016-01-31 08:13:41 +03:00
|
|
|
data = new char[size+padding+1];
|
2016-06-22 06:56:39 +03:00
|
|
|
size = (uint32_t)bx::read(&reader, data, size);
|
|
|
|
|
|
|
|
if (data[0] == '\xef'
|
|
|
|
&& data[1] == '\xbb'
|
|
|
|
&& data[2] == '\xbf')
|
|
|
|
{
|
2017-05-26 06:24:02 +03:00
|
|
|
bx::memMove(data, &data[3], size-3);
|
2016-06-22 06:56:39 +03:00
|
|
|
size -= 3;
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
// Compiler generates "error X3000: syntax error: unexpected end of file"
|
|
|
|
// if input doesn't have empty line at EOF.
|
|
|
|
data[size] = '\n';
|
2017-05-26 06:24:02 +03:00
|
|
|
bx::memSet(&data[size+1], 0, padding);
|
2016-06-22 06:56:39 +03:00
|
|
|
bx::close(&reader);
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-03-30 06:28:28 +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.
|
|
|
|
preprocessor.run(data);
|
|
|
|
delete [] data;
|
|
|
|
|
|
|
|
size = (uint32_t)preprocessor.m_preprocessed.size();
|
|
|
|
data = new char[size+padding+1];
|
2017-05-26 06:24:02 +03:00
|
|
|
bx::memCopy(data, preprocessor.m_preprocessed.c_str(), size);
|
|
|
|
bx::memSet(&data[size], 0, padding+1);
|
2016-03-30 06:28:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
strNormalizeEol(data);
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
input = const_cast<char*>(bx::strws(data) );
|
|
|
|
while (input[0] == '$')
|
|
|
|
{
|
|
|
|
const char* str = bx::strws(input+1);
|
|
|
|
const char* eol = bx::streol(str);
|
|
|
|
const char* nl = bx::strnl(eol);
|
|
|
|
input = const_cast<char*>(nl);
|
2014-07-21 07:27:13 +04:00
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
if (0 == bx::strCmp(str, "input", 5) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
str += 5;
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* comment = bx::strFind(str, "//");
|
2016-01-31 08:13:41 +03:00
|
|
|
eol = NULL != comment && comment < eol ? comment : eol;
|
|
|
|
inputHash = parseInOut(shaderInputs, str, eol);
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(str, "output", 6) )
|
2014-07-21 07:27:13 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
str += 6;
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* comment = bx::strFind(str, "//");
|
2016-01-31 08:13:41 +03:00
|
|
|
eol = NULL != comment && comment < eol ? comment : eol;
|
|
|
|
outputHash = parseInOut(shaderOutputs, str, eol);
|
|
|
|
}
|
2017-04-23 00:47:02 +03:00
|
|
|
else if (0 == bx::strCmp(str, "raw", 3) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
raw = true;
|
|
|
|
str += 3;
|
2014-07-21 07:27:13 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
input = const_cast<char*>(bx::strws(input) );
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
if (raw)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter* writer = NULL;
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
if (NULL != bin2c)
|
|
|
|
{
|
|
|
|
writer = new Bin2cWriter(bin2c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
writer = new bx::FileWriter;
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
if (!bx::open(writer, outFilePath) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('f' == shaderType)
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_FSH);
|
|
|
|
bx::write(writer, inputHash);
|
|
|
|
}
|
|
|
|
else if ('v' == shaderType)
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_VSH);
|
|
|
|
bx::write(writer, outputHash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
|
|
|
|
bx::write(writer, outputHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != glsl)
|
|
|
|
{
|
|
|
|
bx::write(writer, uint16_t(0) );
|
|
|
|
|
2017-04-17 07:56:17 +03:00
|
|
|
uint32_t shaderSize = (uint32_t)bx::strLen(input);
|
2016-01-31 08:13:41 +03:00
|
|
|
bx::write(writer, shaderSize);
|
|
|
|
bx::write(writer, input, shaderSize);
|
|
|
|
bx::write(writer, uint8_t(0) );
|
|
|
|
|
|
|
|
compiled = true;
|
|
|
|
}
|
2016-10-01 04:16:04 +03:00
|
|
|
else if (0 != pssl)
|
|
|
|
{
|
|
|
|
compiled = compilePSSLShader(cmdLine, 0, input, writer);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
compiled = compileHLSLShader(cmdLine, d3d, input, writer);
|
|
|
|
}
|
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::close(writer);
|
2016-01-31 08:13:41 +03:00
|
|
|
delete writer;
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else if ('c' == shaderType) // Compute
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-04-23 00:47:02 +03:00
|
|
|
char* entry = const_cast<char*>(bx::strFind(input, "void main()") );
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL == entry)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "Shader entry point 'void main()' is not found.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (0 != glsl
|
|
|
|
|| 0 != essl
|
2016-11-04 08:00:55 +03:00
|
|
|
|| 0 != metal)
|
2015-03-18 03:27:04 +03:00
|
|
|
{
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
2013-10-02 09:07:40 +04:00
|
|
|
{
|
|
|
|
preprocessor.writef(
|
2016-01-31 08:13:41 +03:00
|
|
|
"#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"
|
2013-10-02 09:07:40 +04:00
|
|
|
);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
entry[4] = '_';
|
2014-06-26 05:43:26 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef("#define void_main()");
|
|
|
|
preprocessor.writef(" \\\n\tvoid main(");
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
uint32_t arg = 0;
|
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
const bool hasLocalInvocationID = NULL != bx::strFind(input, "gl_LocalInvocationID");
|
|
|
|
const bool hasLocalInvocationIndex = NULL != bx::strFind(input, "gl_LocalInvocationIndex");
|
|
|
|
const bool hasGlobalInvocationID = NULL != bx::strFind(input, "gl_GlobalInvocationID");
|
|
|
|
const bool hasWorkGroupID = NULL != bx::strFind(input, "gl_WorkGroupID");
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
if (hasLocalInvocationID)
|
2012-10-28 08:34:41 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sint3 gl_LocalInvocationID : SV_GroupThreadID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
2014-06-26 05:43:26 +04:00
|
|
|
);
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
2013-02-15 05:27:10 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasLocalInvocationIndex)
|
2014-09-01 22:24:51 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sint gl_LocalInvocationIndex : SV_GroupIndex"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
2014-09-01 22:24:51 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasGlobalInvocationID)
|
2014-09-01 22:24:51 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sint3 gl_GlobalInvocationID : SV_DispatchThreadID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
2014-09-01 22:24:51 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasWorkGroupID)
|
2013-02-15 05:27:10 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sint3 gl_WorkGroupID : SV_GroupID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
2013-02-15 05:27:10 +04:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t)\n"
|
|
|
|
);
|
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (preprocessor.run(input) )
|
|
|
|
{
|
|
|
|
BX_TRACE("Input file: %s", filePath);
|
|
|
|
BX_TRACE("Output file: %s", outFilePath);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (preprocessOnly)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter writer;
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
if (!bx::open(&writer, outFilePath) )
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
|
|
|
|
return EXIT_FAILURE;
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
|
|
|
|
bx::close(&writer);
|
2012-11-26 06:24:50 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter* writer = NULL;
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
if (NULL != bin2c)
|
2014-09-01 22:24:51 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
writer = new Bin2cWriter(bin2c);
|
2014-09-01 22:24:51 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
writer = new bx::FileWriter;
|
2014-09-01 22:24:51 +04:00
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
if (!bx::open(writer, outFilePath) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
|
|
|
|
bx::write(writer, outputHash);
|
2014-02-25 09:44:12 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (0 != glsl
|
|
|
|
|| 0 != essl)
|
2015-01-13 06:37:42 +03:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string code;
|
|
|
|
|
|
|
|
if (essl)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#version 310 es\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#version %d\n", glsl == 0 ? 430 : glsl);
|
|
|
|
}
|
|
|
|
|
|
|
|
code += preprocessor.m_preprocessed;
|
|
|
|
#if 1
|
|
|
|
bx::write(writer, uint16_t(0) );
|
|
|
|
|
|
|
|
uint32_t shaderSize = (uint32_t)code.size();
|
|
|
|
bx::write(writer, shaderSize);
|
|
|
|
bx::write(writer, code.c_str(), shaderSize);
|
|
|
|
bx::write(writer, uint8_t(0) );
|
|
|
|
|
|
|
|
compiled = true;
|
|
|
|
#else
|
|
|
|
compiled = compileGLSLShader(cmdLine, essl, code, writer);
|
|
|
|
#endif // 0
|
2015-01-13 06:37:42 +03:00
|
|
|
}
|
2016-10-16 20:29:46 +03:00
|
|
|
else if (0 != spirv)
|
|
|
|
{
|
|
|
|
compiled = compileSPIRVShader(cmdLine, 0, preprocessor.m_preprocessed, writer);
|
|
|
|
}
|
2016-10-01 04:16:04 +03:00
|
|
|
else if (0 != pssl)
|
|
|
|
{
|
|
|
|
compiled = compilePSSLShader(cmdLine, 0, preprocessor.m_preprocessed, writer);
|
|
|
|
}
|
2015-01-13 06:37:42 +03:00
|
|
|
else
|
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
compiled = compileHLSLShader(cmdLine, d3d, preprocessor.m_preprocessed, writer);
|
2015-01-13 06:37:42 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::close(writer);
|
2016-01-31 08:13:41 +03:00
|
|
|
delete writer;
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (compiled)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
if (depends)
|
2014-08-12 07:34:52 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string ofp = outFilePath;
|
|
|
|
ofp += ".d";
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter writer;
|
2016-02-01 03:00:02 +03:00
|
|
|
if (bx::open(&writer, ofp.c_str() ) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::close(&writer);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2014-08-12 07:34:52 +04:00
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Vertex/Fragment
|
|
|
|
{
|
2017-04-23 00:47:02 +03:00
|
|
|
char* entry = const_cast<char*>(bx::strFind(input, "void main()") );
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL == entry)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Shader entry point 'void main()' is not found.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (0 != glsl
|
|
|
|
|| 0 != essl
|
2016-11-04 08:00:55 +03:00
|
|
|
|| 0 != metal)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
if (0 == essl)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +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"
|
|
|
|
);
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2014-05-11 07:51:44 +04: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() )
|
2014-02-10 04:46:50 +04:00
|
|
|
{
|
2014-05-11 07:51:44 +04:00
|
|
|
const Varying& var = varyingIt->second;
|
2016-01-31 08:13:41 +03:00
|
|
|
const char* name = var.m_name.c_str();
|
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
if (0 == bx::strCmp(name, "a_", 2)
|
|
|
|
|| 0 == bx::strCmp(name, "i_", 2) )
|
2016-01-31 08:13:41 +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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2014-05-11 07:51:44 +04: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() )
|
2014-02-10 04:46:50 +04:00
|
|
|
{
|
2014-05-11 07:51:44 +04:00
|
|
|
const Varying& var = varyingIt->second;
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef("%s varying %s %s;\n"
|
|
|
|
, var.m_interpolation.c_str()
|
|
|
|
, var.m_type.c_str()
|
|
|
|
, var.m_name.c_str()
|
|
|
|
);
|
2014-02-10 04:46:50 +04:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +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"
|
|
|
|
);
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-10-01 04:16:04 +03:00
|
|
|
if (hlsl != 0
|
|
|
|
&& hlsl < 4)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
2016-07-11 06:49:10 +03:00
|
|
|
"#define centroid\n"
|
2016-01-31 08:13:41 +03:00
|
|
|
"#define flat\n"
|
|
|
|
"#define noperspective\n"
|
2016-07-11 06:49:10 +03:00
|
|
|
"#define smooth\n"
|
2016-01-31 08:13:41 +03:00
|
|
|
);
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2014-02-10 04:46:50 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
entry[4] = '_';
|
|
|
|
|
|
|
|
if ('f' == shaderType)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* insert = bx::strFind(entry, "{");
|
2016-07-02 06:38:12 +03:00
|
|
|
if (NULL != insert)
|
2014-02-10 04:46:50 +04:00
|
|
|
{
|
2016-07-02 06:38:12 +03:00
|
|
|
insert = strInsert(const_cast<char*>(insert+1), "\nvec4 bgfx_VoidFrag = vec4_splat(0.0);\n");
|
2014-02-10 04:46:50 +04:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
const bool hasFragColor = NULL != bx::strFind(input, "gl_FragColor");
|
|
|
|
const bool hasFragCoord = NULL != bx::strFind(input, "gl_FragCoord") || hlsl > 3 || hlsl == 2;
|
|
|
|
const bool hasFragDepth = NULL != bx::strFind(input, "gl_FragDepth");
|
|
|
|
const bool hasFrontFacing = NULL != bx::strFind(input, "gl_FrontFacing");
|
|
|
|
const bool hasPrimitiveId = NULL != bx::strFind(input, "gl_PrimitiveID");
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
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);
|
2017-04-23 00:47:02 +03:00
|
|
|
hasFragData[ii] = NULL != bx::strFind(input, temp);
|
2016-01-31 08:13:41 +03:00
|
|
|
numFragData += hasFragData[ii];
|
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +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 gl_FragData_0_\n");
|
2016-07-02 06:38:12 +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 || d3d < 11;
|
|
|
|
|
|
|
|
if (NULL != insert
|
|
|
|
&& d3d < 11
|
|
|
|
&& !hasFragColor)
|
|
|
|
{
|
|
|
|
insert = strInsert(const_cast<char*>(insert+1), "\ngl_FragColor = bgfx_VoidFrag;\n");
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2013-02-15 05:27:10 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef("#define void_main()");
|
|
|
|
preprocessor.writef(" \\\n\tvoid main(");
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
uint32_t arg = 0;
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasFragCoord)
|
|
|
|
{
|
|
|
|
preprocessor.writef(" \\\n\tvec4 gl_FragCoord : SV_POSITION");
|
|
|
|
++arg;
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
|
|
|
|
if (varyingIt != varyingMap.end() )
|
|
|
|
{
|
|
|
|
const Varying& var = varyingIt->second;
|
|
|
|
preprocessor.writef(" \\\n\t%s%s %s %s : %s"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
, interpolationDx11(var.m_interpolation.c_str() )
|
|
|
|
, var.m_type.c_str()
|
|
|
|
, var.m_name.c_str()
|
|
|
|
, var.m_semantics.c_str()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t maxRT = d3d > 9 ? BX_COUNTOF(hasFragData) : 4;
|
|
|
|
|
2016-07-02 06:38:12 +03:00
|
|
|
for (uint32_t ii = 0; ii < BX_COUNTOF(hasFragData); ++ii)
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
if (ii < maxRT)
|
2015-09-02 02:44:11 +03:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasFragData[ii])
|
|
|
|
{
|
|
|
|
addFragData(preprocessor, input, ii, arg++ > 0);
|
|
|
|
}
|
2015-09-02 02:44:11 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
voidFragData(input, ii);
|
2015-09-02 02:44:11 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasFragDepth)
|
|
|
|
{
|
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sout float gl_FragDepth : SV_DEPTH"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasFrontFacing
|
|
|
|
&& hlsl >= 3)
|
|
|
|
{
|
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%sfloat __vface : VFACE"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
2014-05-11 07:51:44 +04:00
|
|
|
);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hasPrimitiveId)
|
|
|
|
{
|
|
|
|
if (d3d > 9)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%suint gl_PrimitiveID : SV_PrimitiveID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
2014-05-11 07:51:44 +04:00
|
|
|
);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
else
|
|
|
|
{
|
2017-04-24 01:53:15 +03:00
|
|
|
fprintf(stderr, "gl_PrimitiveID builtin is not supported by this D3D9 HLSL.\n");
|
2016-01-31 08:13:41 +03:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t)\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hasFrontFacing)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
if (hlsl >= 3)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
"#define gl_FrontFacing (__vface <= 0.0)\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preprocessor.writef(
|
|
|
|
"#define gl_FrontFacing false\n"
|
2014-05-11 07:51:44 +04:00
|
|
|
);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ('v' == shaderType)
|
|
|
|
{
|
2017-04-24 01:53:15 +03:00
|
|
|
const bool hasVertexId = NULL != bx::strFind(input, "gl_VertexID");
|
|
|
|
const bool hasInstanceId = NULL != bx::strFind(input, "gl_InstanceID");
|
|
|
|
|
2017-04-23 00:47:02 +03:00
|
|
|
const char* brace = bx::strFind(entry, "{");
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL != brace)
|
|
|
|
{
|
|
|
|
const char* end = bx::strmb(brace, '{', '}');
|
|
|
|
if (NULL != end)
|
|
|
|
{
|
|
|
|
strInsert(const_cast<char*>(end), "__RETURN__;\n");
|
|
|
|
}
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
"struct Output\n"
|
|
|
|
"{\n"
|
|
|
|
"\tvec4 gl_Position : SV_POSITION;\n"
|
|
|
|
"#define gl_Position _varying_.gl_Position\n"
|
|
|
|
);
|
|
|
|
for (InOut::const_iterator it = shaderOutputs.begin(), itEnd = shaderOutputs.end(); it != itEnd; ++it)
|
|
|
|
{
|
|
|
|
VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
|
|
|
|
if (varyingIt != varyingMap.end() )
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
const Varying& var = varyingIt->second;
|
|
|
|
preprocessor.writef("\t%s %s : %s;\n", 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() );
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
preprocessor.writef(
|
|
|
|
"};\n"
|
|
|
|
);
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef("#define void_main() \\\n");
|
|
|
|
preprocessor.writef("Output main(");
|
2017-04-24 01:53:15 +03:00
|
|
|
uint32_t arg = 0;
|
2016-01-31 08:13:41 +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() )
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
const Varying& var = varyingIt->second;
|
2017-04-24 01:53:15 +03:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasVertexId)
|
|
|
|
{
|
|
|
|
if (d3d > 9)
|
|
|
|
{
|
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%suint gl_VertexID : SV_VertexID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gl_VertexID builtin is not supported by this D3D9 HLSL.\n");
|
|
|
|
return EXIT_FAILURE;
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2017-04-24 01:53:15 +03:00
|
|
|
|
|
|
|
if (hasInstanceId)
|
|
|
|
{
|
|
|
|
if (d3d > 9)
|
|
|
|
{
|
|
|
|
preprocessor.writef(
|
|
|
|
" \\\n\t%suint gl_InstanceID : SV_InstanceID"
|
|
|
|
, arg++ > 0 ? ", " : " "
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "gl_InstanceID builtin is not supported by this D3D9 HLSL.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
preprocessor.writef(
|
|
|
|
") \\\n"
|
|
|
|
"{ \\\n"
|
|
|
|
"\tOutput _varying_;"
|
|
|
|
);
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +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() )
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
const Varying& var = varyingIt->second;
|
|
|
|
preprocessor.writef(" \\\n\t%s", var.m_name.c_str() );
|
|
|
|
if (!var.m_init.empty() )
|
|
|
|
{
|
|
|
|
preprocessor.writef(" = %s", var.m_init.c_str() );
|
|
|
|
}
|
|
|
|
preprocessor.writef(";");
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
preprocessor.writef(
|
|
|
|
"\n#define __RETURN__ \\\n"
|
|
|
|
"\t} \\\n"
|
2016-04-15 05:40:53 +03:00
|
|
|
);
|
|
|
|
|
2016-10-01 04:16:04 +03:00
|
|
|
if (hlsl != 0
|
|
|
|
&& hlsl <= 3)
|
2016-04-15 05:40:53 +03:00
|
|
|
{
|
|
|
|
// preprocessor.writef(
|
|
|
|
// "\tgl_Position.xy += u_viewTexel.xy * gl_Position.w; \\\n"
|
|
|
|
// );
|
|
|
|
}
|
|
|
|
|
|
|
|
preprocessor.writef(
|
2016-01-31 08:13:41 +03:00
|
|
|
"\treturn _varying_"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preprocessor.run(input) )
|
|
|
|
{
|
|
|
|
BX_TRACE("Input file: %s", filePath);
|
|
|
|
BX_TRACE("Output file: %s", outFilePath);
|
|
|
|
|
|
|
|
if (preprocessOnly)
|
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter writer;
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
if (!bx::open(&writer, outFilePath) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != glsl)
|
|
|
|
{
|
|
|
|
if (NULL == profile)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
writef(&writer
|
|
|
|
, "#ifdef GL_ES\n"
|
|
|
|
"precision highp float;\n"
|
|
|
|
"#endif // GL_ES\n\n"
|
2014-05-11 07:51:44 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::write(&writer, preprocessor.m_preprocessed.c_str(), (int32_t)preprocessor.m_preprocessed.size() );
|
|
|
|
bx::close(&writer);
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-13 01:08:01 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter* writer = NULL;
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (NULL != bin2c)
|
|
|
|
{
|
|
|
|
writer = new Bin2cWriter(bin2c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-12 07:01:38 +03:00
|
|
|
writer = new bx::FileWriter;
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
if (!bx::open(writer, outFilePath) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open output file '%s'.", outFilePath);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('f' == shaderType)
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_FSH);
|
|
|
|
bx::write(writer, inputHash);
|
|
|
|
}
|
|
|
|
else if ('v' == shaderType)
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_VSH);
|
|
|
|
bx::write(writer, outputHash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
|
|
|
|
bx::write(writer, outputHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != glsl
|
|
|
|
|| 0 != essl
|
2016-11-04 08:00:55 +03:00
|
|
|
|| 0 != metal)
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
std::string code;
|
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (glsl < 400)
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
2017-06-06 03:37:26 +03:00
|
|
|
const bool usesTextureLod = false
|
|
|
|
|| !!bx::findIdentifierMatch(input, s_ARB_shader_texture_lod)
|
|
|
|
|| !!bx::findIdentifierMatch(input, s_EXT_shader_texture_lod)
|
|
|
|
;
|
|
|
|
const bool usesInstanceID = !!bx::strFind(input, "gl_InstanceID");
|
|
|
|
const bool usesGpuShader4 = !!bx::findIdentifierMatch(input, s_EXT_gpu_shader4);
|
|
|
|
const bool usesGpuShader5 = !!bx::findIdentifierMatch(input, s_ARB_gpu_shader5);
|
|
|
|
const bool usesTexelFetch = !!bx::findIdentifierMatch(input, s_texelFetch);
|
|
|
|
const bool usesTextureMS = !!bx::findIdentifierMatch(input, s_ARB_texture_multisample);
|
|
|
|
const bool usesTextureArray = !!bx::findIdentifierMatch(input, s_textureArray);
|
|
|
|
const bool usesPacking = !!bx::findIdentifierMatch(input, s_ARB_shading_language_packing);
|
|
|
|
|
|
|
|
if (0 == essl)
|
2016-06-15 19:33:11 +03:00
|
|
|
{
|
2017-06-06 03:37:26 +03:00
|
|
|
const bool need130 = 120 == glsl && (false
|
|
|
|
|| bx::findIdentifierMatch(input, s_130)
|
|
|
|
|| usesTexelFetch
|
2017-04-24 01:53:15 +03:00
|
|
|
);
|
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (0 != metal)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#version 120\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#version %s\n", need130 ? "130" : profile);
|
|
|
|
glsl = 130;
|
|
|
|
}
|
2017-04-24 01:53:15 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesInstanceID)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_ARB_draw_instanced : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-03-18 21:32:04 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesGpuShader4)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_gpu_shader4 : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-03-18 21:32:04 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesGpuShader5)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_ARB_gpu_shader5 : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2017-03-24 03:33:29 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesPacking)
|
2017-03-24 03:33:29 +03:00
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
2017-06-06 03:37:26 +03:00
|
|
|
, "#extension GL_ARB_shading_language_packing : enable\n"
|
2017-03-24 03:33:29 +03:00
|
|
|
);
|
|
|
|
}
|
2017-06-06 03:37:26 +03:00
|
|
|
|
|
|
|
bool ARB_shader_texture_lod = false;
|
|
|
|
bool EXT_shader_texture_lod = false;
|
|
|
|
|
|
|
|
if (usesTextureLod)
|
|
|
|
{
|
|
|
|
if ('f' == shaderType)
|
|
|
|
{
|
|
|
|
ARB_shader_texture_lod = true;
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_ARB_shader_texture_lod : enable\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EXT_shader_texture_lod = true;
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_shader_texture_lod : enable\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usesTextureMS)
|
2017-03-24 03:33:29 +03:00
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
2017-06-06 03:37:26 +03:00
|
|
|
, "#extension GL_ARB_texture_multisample : enable\n"
|
2017-03-24 03:33:29 +03:00
|
|
|
);
|
|
|
|
}
|
2016-03-31 06:32:13 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesTextureArray)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_texture_array : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-08-22 00:03:16 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (130 > glsl)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code,
|
|
|
|
"#define ivec2 vec2\n"
|
|
|
|
"#define ivec3 vec3\n"
|
|
|
|
"#define ivec4 vec4\n"
|
|
|
|
);
|
|
|
|
}
|
2016-08-24 08:06:50 +03:00
|
|
|
|
2017-06-06 03:37:26 +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"
|
|
|
|
);
|
|
|
|
}
|
2017-03-24 03:33:29 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#define bgfxShadow2D shadow2D\n"
|
|
|
|
"#define bgfxShadow2DProj shadow2DProj\n"
|
2017-03-24 03:33:29 +03:00
|
|
|
);
|
|
|
|
}
|
2017-06-06 03:37:26 +03:00
|
|
|
else
|
2017-03-24 03:33:29 +03:00
|
|
|
{
|
2017-06-06 03:37:26 +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"
|
|
|
|
"#define texture2DLod texture2DLodEXT\n"
|
|
|
|
"#define texture2DGrad texture2DGradEXT\n"
|
|
|
|
"#define texture2DProjLod texture2DProjLodEXT\n"
|
|
|
|
"#define texture2DProjGrad texture2DProjGradEXT\n"
|
|
|
|
"#define textureCubeLod textureCubeLodEXT\n"
|
|
|
|
"#define textureCubeGrad textureCubeGradEXT\n"
|
|
|
|
);
|
|
|
|
}
|
2016-08-24 08:06:50 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (NULL != bx::findIdentifierMatch(input, s_OES_standard_derivatives) )
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#extension GL_OES_standard_derivatives : enable\n");
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (NULL != bx::findIdentifierMatch(input, s_OES_texture_3D) )
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#extension GL_OES_texture_3D : enable\n");
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (NULL != bx::findIdentifierMatch(input, s_EXT_shadow_samplers) )
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_shadow_samplers : enable\n"
|
|
|
|
"#define shadow2D shadow2DEXT\n"
|
|
|
|
"#define shadow2DProj shadow2DProjEXT\n"
|
|
|
|
);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesGpuShader5)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_ARB_gpu_shader5 : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesPacking)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_ARB_shading_language_packing : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-03-18 21:32:04 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (NULL != bx::findIdentifierMatch(input, "gl_FragDepth") )
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_frag_depth : enable\n"
|
|
|
|
"#define gl_FragDepth gl_FragDepthEXT\n"
|
|
|
|
);
|
|
|
|
}
|
2016-03-18 21:32:04 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (usesTextureArray)
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code
|
|
|
|
, "#extension GL_EXT_texture_array : enable\n"
|
|
|
|
);
|
|
|
|
}
|
2016-08-24 08:06:50 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
bx::stringPrintf(code,
|
|
|
|
"#define ivec2 vec2\n"
|
|
|
|
"#define ivec3 vec3\n"
|
|
|
|
"#define ivec4 vec4\n"
|
|
|
|
);
|
2016-08-24 08:06:50 +03:00
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2017-06-07 03:25:17 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bx::stringPrintf(code, "#version %d\n", glsl);
|
|
|
|
}
|
2016-01-31 08:13:41 +03:00
|
|
|
|
|
|
|
code += preprocessor.m_preprocessed;
|
2016-10-16 20:29:46 +03:00
|
|
|
|
2017-06-06 03:37:26 +03:00
|
|
|
if (glsl > 400)
|
|
|
|
{
|
|
|
|
bx::write(writer, uint16_t(0) );
|
|
|
|
|
|
|
|
uint32_t shaderSize = (uint32_t)code.size();
|
|
|
|
bx::write(writer, shaderSize);
|
|
|
|
bx::write(writer, code.c_str(), shaderSize);
|
|
|
|
bx::write(writer, uint8_t(0) );
|
|
|
|
|
|
|
|
compiled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
compiled = compileGLSLShader(cmdLine
|
|
|
|
, metal ? BX_MAKEFOURCC('M', 'T', 'L', 0) : essl
|
|
|
|
, code
|
|
|
|
, writer
|
|
|
|
);
|
|
|
|
}
|
2016-11-04 08:00:55 +03:00
|
|
|
}
|
|
|
|
else if (0 != spirv)
|
|
|
|
{
|
|
|
|
compiled = compileSPIRVShader(cmdLine
|
|
|
|
, 0
|
|
|
|
, preprocessor.m_preprocessed
|
|
|
|
, writer
|
|
|
|
);
|
2016-10-01 04:16:04 +03:00
|
|
|
}
|
|
|
|
else if (0 != pssl)
|
|
|
|
{
|
|
|
|
compiled = compilePSSLShader(cmdLine
|
|
|
|
, 0
|
|
|
|
, preprocessor.m_preprocessed
|
|
|
|
, writer
|
|
|
|
);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
compiled = compileHLSLShader(cmdLine
|
2016-10-01 04:16:04 +03:00
|
|
|
, d3d
|
|
|
|
, preprocessor.m_preprocessed
|
|
|
|
, writer
|
|
|
|
);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::close(writer);
|
2016-01-31 08:13:41 +03:00
|
|
|
delete writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compiled)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
if (depends)
|
2014-05-11 07:51:44 +04:00
|
|
|
{
|
2016-01-31 08:13:41 +03:00
|
|
|
std::string ofp = outFilePath;
|
|
|
|
ofp += ".d";
|
2017-06-12 07:01:38 +03:00
|
|
|
bx::FileWriter writer;
|
2016-02-01 03:00:02 +03:00
|
|
|
if (bx::open(&writer, ofp.c_str() ) )
|
2016-01-31 08:13:41 +03:00
|
|
|
{
|
|
|
|
writef(&writer, "%s : %s\n", outFilePath, preprocessor.m_depends.c_str() );
|
2016-02-01 03:00:02 +03:00
|
|
|
bx::close(&writer);
|
2016-01-31 08:13:41 +03:00
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 05:27:10 +04:00
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
delete [] data;
|
|
|
|
}
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
if (compiled)
|
|
|
|
{
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2014-05-11 07:51:44 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
remove(outFilePath);
|
2012-10-28 08:34:41 +04:00
|
|
|
|
2016-01-31 08:13:41 +03:00
|
|
|
fprintf(stderr, "Failed to build shader.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-01-31 03:15:25 +03:00
|
|
|
|
|
|
|
} // namespace bgfx
|
|
|
|
|
|
|
|
int main(int _argc, const char* _argv[])
|
|
|
|
{
|
|
|
|
return bgfx::compileShader(_argc, _argv);
|
|
|
|
}
|