This commit is contained in:
Branimir Karadžić 2016-01-30 21:13:41 -08:00
parent 2cf7eaaefc
commit d53b27d353
2 changed files with 1774 additions and 1776 deletions

View file

@ -6,32 +6,31 @@
#include "shaderc.h" #include "shaderc.h"
#include <bx/tokenizecmd.h> #include <bx/tokenizecmd.h>
namespace bgfx
{
bool g_verbose = false;
#define MAX_TAGS 256 #define MAX_TAGS 256
extern "C" extern "C"
{ {
#include <fpp.h> #include <fpp.h>
} // extern "C" } // extern "C"
#define BGFX_CHUNK_MAGIC_CSH BX_MAKEFOURCC('C', 'S', 'H', 0x2) namespace bgfx
#define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x4)
#define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x4)
long int fsize(FILE* _file)
{ {
bool g_verbose = false;
#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)
long int fsize(FILE* _file)
{
long int pos = ftell(_file); long int pos = ftell(_file);
fseek(_file, 0L, SEEK_END); fseek(_file, 0L, SEEK_END);
long int size = ftell(_file); long int size = ftell(_file);
fseek(_file, pos, SEEK_SET); fseek(_file, pos, SEEK_SET);
return size; return size;
} }
static const char* s_ARB_shader_texture_lod[] = static const char* s_ARB_shader_texture_lod[] =
{ {
"texture2DLod", "texture2DLod",
"texture2DProjLod", "texture2DProjLod",
"texture3DLod", "texture3DLod",
@ -44,35 +43,35 @@ static const char* s_ARB_shader_texture_lod[] =
// "texture1DProjLod", // "texture1DProjLod",
// "shadow1DLod", // "shadow1DLod",
// "shadow1DProjLod", // "shadow1DProjLod",
}; };
static const char* s_EXT_shadow_samplers[] = static const char* s_EXT_shadow_samplers[] =
{ {
"shadow2D", "shadow2D",
"shadow2DProj", "shadow2DProj",
"sampler2DShadow", "sampler2DShadow",
NULL NULL
}; };
static const char* s_OES_standard_derivatives[] = static const char* s_OES_standard_derivatives[] =
{ {
"dFdx", "dFdx",
"dFdy", "dFdy",
"fwidth", "fwidth",
NULL NULL
}; };
static const char* s_OES_texture_3D[] = static const char* s_OES_texture_3D[] =
{ {
"texture3D", "texture3D",
"texture3DProj", "texture3DProj",
"texture3DLod", "texture3DLod",
"texture3DProjLod", "texture3DProjLod",
NULL NULL
}; };
static const char* s_130[] = static const char* s_130[] =
{ {
"uint", "uint",
"uint2", "uint2",
"uint3", "uint3",
@ -80,19 +79,19 @@ static const char* s_130[] =
"isampler3D", "isampler3D",
"usampler3D", "usampler3D",
NULL NULL
}; };
const char* s_uniformTypeName[UniformType::Count] = const char* s_uniformTypeName[UniformType::Count] =
{ {
"int", "int",
NULL, NULL,
"vec4", "vec4",
"mat3", "mat3",
"mat4", "mat4",
}; };
const char* interpolationDx11(const char* _glsl) const char* interpolationDx11(const char* _glsl)
{ {
if (0 == strcmp(_glsl, "smooth") ) if (0 == strcmp(_glsl, "smooth") )
{ {
return "linear"; return "linear";
@ -103,15 +102,15 @@ const char* interpolationDx11(const char* _glsl)
} }
return _glsl; // noperspective return _glsl; // noperspective
} }
const char* getUniformTypeName(UniformType::Enum _enum) const char* getUniformTypeName(UniformType::Enum _enum)
{ {
return s_uniformTypeName[_enum]; return s_uniformTypeName[_enum];
} }
UniformType::Enum nameToUniformTypeEnum(const char* _name) UniformType::Enum nameToUniformTypeEnum(const char* _name)
{ {
for (uint32_t ii = 0; ii < UniformType::Count; ++ii) for (uint32_t ii = 0; ii < UniformType::Count; ++ii)
{ {
if (NULL != s_uniformTypeName[ii] if (NULL != s_uniformTypeName[ii]
@ -122,10 +121,10 @@ UniformType::Enum nameToUniformTypeEnum(const char* _name)
} }
return UniformType::Count; return UniformType::Count;
} }
int32_t writef(bx::WriterI* _writer, const char* _format, ...) int32_t writef(bx::WriterI* _writer, const char* _format, ...)
{ {
va_list argList; va_list argList;
va_start(argList, _format); va_start(argList, _format);
@ -145,11 +144,11 @@ int32_t writef(bx::WriterI* _writer, const char* _format, ...)
va_end(argList); va_end(argList);
return len; return len;
} }
class Bin2cWriter : public bx::CrtFileWriter class Bin2cWriter : public bx::CrtFileWriter
{ {
public: public:
Bin2cWriter(const char* _name) Bin2cWriter(const char* _name)
: m_name(_name) : m_name(_name)
{ {
@ -172,12 +171,12 @@ public:
return _size; return _size;
} }
private: private:
void generate() void generate()
{ {
#define HEX_DUMP_WIDTH 16 #define HEX_DUMP_WIDTH 16
#define HEX_DUMP_SPACE_WIDTH 96 #define HEX_DUMP_SPACE_WIDTH 96
#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s" #define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
const uint8_t* data = &m_buffer[0]; const uint8_t* data = &m_buffer[0];
uint32_t size = (uint32_t)m_buffer.size(); uint32_t size = (uint32_t)m_buffer.size();
@ -215,9 +214,9 @@ private:
} }
outf("};\n"); outf("};\n");
#undef HEX_DUMP_WIDTH #undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH #undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT #undef HEX_DUMP_FORMAT
} }
int32_t outf(const char* _format, ...) int32_t outf(const char* _format, ...)
@ -246,23 +245,23 @@ private:
std::string m_name; std::string m_name;
typedef std::vector<uint8_t> Buffer; typedef std::vector<uint8_t> Buffer;
Buffer m_buffer; Buffer m_buffer;
}; };
struct Varying struct Varying
{ {
std::string m_precision; std::string m_precision;
std::string m_interpolation; std::string m_interpolation;
std::string m_name; std::string m_name;
std::string m_type; std::string m_type;
std::string m_init; std::string m_init;
std::string m_semantics; std::string m_semantics;
}; };
typedef std::unordered_map<std::string, Varying> VaryingMap; typedef std::unordered_map<std::string, Varying> VaryingMap;
class File class File
{ {
public: public:
File(const char* _filePath) File(const char* _filePath)
: m_data(NULL) : m_data(NULL)
{ {
@ -292,20 +291,20 @@ public:
return m_size; return m_size;
} }
private: private:
char* m_data; char* m_data;
uint32_t m_size; uint32_t m_size;
}; };
void strInsert(char* _str, const char* _insert) void strInsert(char* _str, const char* _insert)
{ {
size_t len = strlen(_insert); size_t len = strlen(_insert);
memmove(&_str[len], _str, strlen(_str) ); memmove(&_str[len], _str, strlen(_str) );
memcpy(_str, _insert, len); memcpy(_str, _insert, len);
} }
void strReplace(char* _str, const char* _find, const char* _replace) void strReplace(char* _str, const char* _find, const char* _replace)
{ {
const size_t len = strlen(_find); const size_t len = strlen(_find);
char* replace = (char*)alloca(len+1); char* replace = (char*)alloca(len+1);
@ -321,16 +320,16 @@ void strReplace(char* _str, const char* _find, const char* _replace)
{ {
memcpy(ptr, replace, len); memcpy(ptr, replace, len);
} }
} }
void strNormalizeEol(char* _str) void strNormalizeEol(char* _str)
{ {
strReplace(_str, "\r\n", "\n"); strReplace(_str, "\r\n", "\n");
strReplace(_str, "\r", "\n"); strReplace(_str, "\r", "\n");
} }
void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end) void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end)
{ {
fprintf(stderr, "Code:\n---\n"); fprintf(stderr, "Code:\n---\n");
LineReader lr(_code); LineReader lr(_code);
@ -347,20 +346,20 @@ void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end)
} }
fprintf(stderr, "---\n"); fprintf(stderr, "---\n");
} }
void writeFile(const char* _filePath, const void* _data, int32_t _size) void writeFile(const char* _filePath, const void* _data, int32_t _size)
{ {
bx::CrtFileWriter out; bx::CrtFileWriter out;
if (0 == out.open(_filePath) ) if (0 == out.open(_filePath) )
{ {
out.write(_data, _size); out.write(_data, _size);
out.close(); out.close();
} }
} }
struct Preprocessor struct Preprocessor
{ {
Preprocessor(const char* _filePath, bool _gles, const char* _includeDir = NULL) Preprocessor(const char* _filePath, bool _gles, const char* _includeDir = NULL)
: m_tagptr(m_tags) : m_tagptr(m_tags)
, m_scratchPos(0) , m_scratchPos(0)
@ -444,7 +443,7 @@ struct Preprocessor
{ {
char* start = scratch(_includeDir); char* start = scratch(_includeDir);
for (char* split = strchr(start, ';'); NULL != split; split = strchr(start, ';')) for (char* split = strchr(start, ';'); NULL != split; split = strchr(start, ';') )
{ {
*split = '\0'; *split = '\0';
m_tagptr->tag = FPPTAG_INCLUDE_DIR; m_tagptr->tag = FPPTAG_INCLUDE_DIR;
@ -549,12 +548,12 @@ struct Preprocessor
char m_scratch[16<<10]; char m_scratch[16<<10];
uint32_t m_scratchPos; uint32_t m_scratchPos;
uint32_t m_fgetsPos; uint32_t m_fgetsPos;
}; };
typedef std::vector<std::string> InOut; typedef std::vector<std::string> InOut;
uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol) uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol)
{ {
uint32_t hash = 0; uint32_t hash = 0;
_str = bx::strws(_str); _str = bx::strws(_str);
@ -587,10 +586,10 @@ uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol)
} }
return hash; return hash;
} }
void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma) void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _comma)
{ {
char find[32]; char find[32];
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx); bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
@ -605,37 +604,37 @@ void addFragData(Preprocessor& _preprocessor, char* _data, uint32_t _idx, bool _
, _idx , _idx
, _idx , _idx
); );
} }
void voidFragData(char* _data, uint32_t _idx) void voidFragData(char* _data, uint32_t _idx)
{ {
char find[32]; char find[32];
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx); bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
strReplace(_data, find, "bgfx_VoidFrag"); strReplace(_data, find, "bgfx_VoidFrag");
} }
// c - compute // c - compute
// d - domain // d - domain
// f - fragment // f - fragment
// g - geometry // g - geometry
// h - hull // h - hull
// v - vertex // v - vertex
// //
// OpenGL #version Features Direct3D Features Shader Model // OpenGL #version Features Direct3D Features Shader Model
// 2.1 120 vf 9.0 vf 2.0 // 2.1 120 vf 9.0 vf 2.0
// 3.0 130 // 3.0 130
// 3.1 140 // 3.1 140
// 3.2 150 vgf // 3.2 150 vgf
// 3.3 330 10.0 vgf 4.0 // 3.3 330 10.0 vgf 4.0
// 4.0 400 vhdgf // 4.0 400 vhdgf
// 4.1 410 // 4.1 410
// 4.2 420 11.0 vhdgf+c 5.0 // 4.2 420 11.0 vhdgf+c 5.0
// 4.3 430 vhdgf+c // 4.3 430 vhdgf+c
// 4.4 440 // 4.4 440
void help(const char* _error = NULL) void help(const char* _error = NULL)
{ {
if (NULL != _error) if (NULL != _error)
{ {
fprintf(stderr, "Error:\n%s\n\n", _error); fprintf(stderr, "Error:\n%s\n\n", _error);
@ -685,10 +684,10 @@ void help(const char* _error = NULL)
"\n" "\n"
"For additional information, see https://github.com/bkaradzic/bgfx\n" "For additional information, see https://github.com/bkaradzic/bgfx\n"
); );
} }
int compileShader(int _argc, const char* _argv[]) int compileShader(int _argc, const char* _argv[])
{ {
bx::CommandLine cmdLine(_argc, _argv); bx::CommandLine cmdLine(_argc, _argv);
if (cmdLine.hasArg('h', "help") ) if (cmdLine.hasArg('h', "help") )
@ -823,7 +822,7 @@ int compileShader(int _argc, const char* _argv[])
eol = defines + strlen(defines); eol = defines + strlen(defines);
} }
std::string define(defines, eol); std::string define(defines, eol);
preprocessor.setDefine(define.c_str()); preprocessor.setDefine(define.c_str() );
defines = ';' == *eol ? eol+1 : eol; defines = ';' == *eol ? eol+1 : eol;
} }
@ -835,7 +834,7 @@ int compileShader(int _argc, const char* _argv[])
preprocessor.setDefaultDefine("BX_PLATFORM_OSX"); preprocessor.setDefaultDefine("BX_PLATFORM_OSX");
preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS"); preprocessor.setDefaultDefine("BX_PLATFORM_WINDOWS");
preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360"); preprocessor.setDefaultDefine("BX_PLATFORM_XBOX360");
// preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_ESSL"); // preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_ESSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL"); preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_GLSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL"); preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_HLSL");
preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL"); preprocessor.setDefaultDefine("BGFX_SHADER_LANGUAGE_METAL");
@ -978,9 +977,9 @@ int compileShader(int _argc, const char* _argv[])
const char* name = parse = bx::strws(bx::strword(parse) ); const char* name = parse = bx::strws(bx::strword(parse) );
const char* column = 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* semantics = parse = bx::strws( (*parse == ':' ? ++parse : parse) );
const char* assign = parse = bx::strws(bx::strword(parse) ); const char* assign = parse = bx::strws(bx::strword(parse) );
const char* init = parse = bx::strws((*parse == '=' ? ++parse : parse)); const char* init = parse = bx::strws( (*parse == '=' ? ++parse : parse) );
if (typen < eol if (typen < eol
&& name < eol && name < eol
@ -1284,7 +1283,7 @@ int compileShader(int _argc, const char* _argv[])
} }
code += preprocessor.m_preprocessed; code += preprocessor.m_preprocessed;
#if 1 #if 1
bx::write(writer, uint16_t(0) ); bx::write(writer, uint16_t(0) );
uint32_t shaderSize = (uint32_t)code.size(); uint32_t shaderSize = (uint32_t)code.size();
@ -1293,9 +1292,9 @@ int compileShader(int _argc, const char* _argv[])
bx::write(writer, uint8_t(0) ); bx::write(writer, uint8_t(0) );
compiled = true; compiled = true;
#else #else
compiled = compileGLSLShader(cmdLine, essl, code, writer); compiled = compileGLSLShader(cmdLine, essl, code, writer);
#endif // 0 #endif // 0
} }
else else
{ {
@ -1747,9 +1746,9 @@ int compileShader(int _argc, const char* _argv[])
"#define texture2DLod texture2DLodEXT\n" "#define texture2DLod texture2DLodEXT\n"
"#define texture2DProjLod texture2DProjLodEXT\n" "#define texture2DProjLod texture2DProjLodEXT\n"
"#define textureCubeLod textureCubeLodEXT\n" "#define textureCubeLod textureCubeLodEXT\n"
// "#define texture2DGrad texture2DGradEXT\n" // "#define texture2DGrad texture2DGradEXT\n"
// "#define texture2DProjGrad texture2DProjGradEXT\n" // "#define texture2DProjGrad texture2DProjGradEXT\n"
// "#define textureCubeGrad textureCubeGradEXT\n" // "#define textureCubeGrad textureCubeGradEXT\n"
); );
} }
@ -1831,7 +1830,7 @@ int compileShader(int _argc, const char* _argv[])
fprintf(stderr, "Failed to build shader.\n"); fprintf(stderr, "Failed to build shader.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} // namespace bgfx } // namespace bgfx

View file

@ -8,9 +8,8 @@
namespace bgfx namespace bgfx
{ {
bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::string& _code, bx::WriterI* _writer)
bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::string& _code, bx::WriterI* _writer) {
{
char ch = tolower(_cmdLine.findOption('\0', "type")[0]); char ch = tolower(_cmdLine.findOption('\0', "type")[0]);
const glslopt_shader_type type = ch == 'f' const glslopt_shader_type type = ch == 'f'
? kGlslOptShaderFragment ? kGlslOptShaderFragment
@ -212,6 +211,6 @@ bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::str
glslopt_cleanup(ctx); glslopt_cleanup(ctx);
return true; return true;
} }
} // namespace bgfx } // namespace bgfx