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 <bx/tokenizecmd.h>
namespace bgfx
{
bool g_verbose = false;
#define MAX_TAGS 256
extern "C"
{
#include <fpp.h>
} // extern "C"
#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)
namespace bgfx
{
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);
fseek(_file, 0L, SEEK_END);
long int size = ftell(_file);
fseek(_file, pos, SEEK_SET);
return size;
}
}
static const char* s_ARB_shader_texture_lod[] =
{
static const char* s_ARB_shader_texture_lod[] =
{
"texture2DLod",
"texture2DProjLod",
"texture3DLod",
@ -44,35 +43,35 @@ static const char* s_ARB_shader_texture_lod[] =
// "texture1DProjLod",
// "shadow1DLod",
// "shadow1DProjLod",
};
};
static const char* s_EXT_shadow_samplers[] =
{
static const char* s_EXT_shadow_samplers[] =
{
"shadow2D",
"shadow2DProj",
"sampler2DShadow",
NULL
};
};
static const char* s_OES_standard_derivatives[] =
{
static const char* s_OES_standard_derivatives[] =
{
"dFdx",
"dFdy",
"fwidth",
NULL
};
};
static const char* s_OES_texture_3D[] =
{
static const char* s_OES_texture_3D[] =
{
"texture3D",
"texture3DProj",
"texture3DLod",
"texture3DProjLod",
NULL
};
};
static const char* s_130[] =
{
static const char* s_130[] =
{
"uint",
"uint2",
"uint3",
@ -80,19 +79,19 @@ static const char* s_130[] =
"isampler3D",
"usampler3D",
NULL
};
};
const char* s_uniformTypeName[UniformType::Count] =
{
const char* s_uniformTypeName[UniformType::Count] =
{
"int",
NULL,
"vec4",
"mat3",
"mat4",
};
};
const char* interpolationDx11(const char* _glsl)
{
const char* interpolationDx11(const char* _glsl)
{
if (0 == strcmp(_glsl, "smooth") )
{
return "linear";
@ -103,15 +102,15 @@ const char* interpolationDx11(const char* _glsl)
}
return _glsl; // noperspective
}
}
const char* getUniformTypeName(UniformType::Enum _enum)
{
const char* getUniformTypeName(UniformType::Enum _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)
{
if (NULL != s_uniformTypeName[ii]
@ -122,10 +121,10 @@ UniformType::Enum nameToUniformTypeEnum(const char* _name)
}
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_start(argList, _format);
@ -145,11 +144,11 @@ int32_t writef(bx::WriterI* _writer, const char* _format, ...)
va_end(argList);
return len;
}
}
class Bin2cWriter : public bx::CrtFileWriter
{
public:
class Bin2cWriter : public bx::CrtFileWriter
{
public:
Bin2cWriter(const char* _name)
: m_name(_name)
{
@ -172,12 +171,12 @@ public:
return _size;
}
private:
private:
void generate()
{
#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"
#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"
const uint8_t* data = &m_buffer[0];
uint32_t size = (uint32_t)m_buffer.size();
@ -215,9 +214,9 @@ private:
}
outf("};\n");
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
#undef HEX_DUMP_WIDTH
#undef HEX_DUMP_SPACE_WIDTH
#undef HEX_DUMP_FORMAT
}
int32_t outf(const char* _format, ...)
@ -246,23 +245,23 @@ private:
std::string m_name;
typedef std::vector<uint8_t> Buffer;
Buffer m_buffer;
};
};
struct Varying
{
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;
};
};
typedef std::unordered_map<std::string, Varying> VaryingMap;
typedef std::unordered_map<std::string, Varying> VaryingMap;
class File
{
public:
class File
{
public:
File(const char* _filePath)
: m_data(NULL)
{
@ -292,20 +291,20 @@ public:
return m_size;
}
private:
private:
char* m_data;
uint32_t m_size;
};
};
void strInsert(char* _str, const char* _insert)
{
void strInsert(char* _str, const char* _insert)
{
size_t len = strlen(_insert);
memmove(&_str[len], _str, strlen(_str) );
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);
char* replace = (char*)alloca(len+1);
@ -321,16 +320,16 @@ void strReplace(char* _str, const char* _find, const char* _replace)
{
memcpy(ptr, replace, len);
}
}
}
void strNormalizeEol(char* _str)
{
void strNormalizeEol(char* _str)
{
strReplace(_str, "\r\n", "\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");
LineReader lr(_code);
@ -347,20 +346,20 @@ void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end)
}
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;
if (0 == out.open(_filePath) )
{
out.write(_data, _size);
out.close();
}
}
}
struct Preprocessor
{
struct Preprocessor
{
Preprocessor(const char* _filePath, bool _gles, const char* _includeDir = NULL)
: m_tagptr(m_tags)
, m_scratchPos(0)
@ -444,7 +443,7 @@ struct Preprocessor
{
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';
m_tagptr->tag = FPPTAG_INCLUDE_DIR;
@ -549,12 +548,12 @@ struct Preprocessor
char m_scratch[16<<10];
uint32_t m_scratchPos;
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;
_str = bx::strws(_str);
@ -587,10 +586,10 @@ uint32_t parseInOut(InOut& _inout, const char* _str, const char* _eol)
}
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];
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
);
}
}
void voidFragData(char* _data, uint32_t _idx)
{
void voidFragData(char* _data, uint32_t _idx)
{
char find[32];
bx::snprintf(find, sizeof(find), "gl_FragData[%d]", _idx);
strReplace(_data, find, "bgfx_VoidFrag");
}
}
// 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
// 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)
{
void help(const char* _error = NULL)
{
if (NULL != _error)
{
fprintf(stderr, "Error:\n%s\n\n", _error);
@ -685,10 +684,10 @@ void help(const char* _error = NULL)
"\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);
if (cmdLine.hasArg('h', "help") )
@ -823,7 +822,7 @@ int compileShader(int _argc, const char* _argv[])
eol = defines + strlen(defines);
}
std::string define(defines, eol);
preprocessor.setDefine(define.c_str());
preprocessor.setDefine(define.c_str() );
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_WINDOWS");
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_HLSL");
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* 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* init = parse = bx::strws((*parse == '=' ? ++parse : parse));
const char* init = parse = bx::strws( (*parse == '=' ? ++parse : parse) );
if (typen < eol
&& name < eol
@ -1284,7 +1283,7 @@ int compileShader(int _argc, const char* _argv[])
}
code += preprocessor.m_preprocessed;
#if 1
#if 1
bx::write(writer, uint16_t(0) );
uint32_t shaderSize = (uint32_t)code.size();
@ -1293,9 +1292,9 @@ int compileShader(int _argc, const char* _argv[])
bx::write(writer, uint8_t(0) );
compiled = true;
#else
#else
compiled = compileGLSLShader(cmdLine, essl, code, writer);
#endif // 0
#endif // 0
}
else
{
@ -1747,9 +1746,9 @@ int compileShader(int _argc, const char* _argv[])
"#define texture2DLod texture2DLodEXT\n"
"#define texture2DProjLod texture2DProjLodEXT\n"
"#define textureCubeLod textureCubeLodEXT\n"
// "#define texture2DGrad texture2DGradEXT\n"
// "#define texture2DProjGrad texture2DProjGradEXT\n"
// "#define textureCubeGrad textureCubeGradEXT\n"
// "#define texture2DGrad texture2DGradEXT\n"
// "#define texture2DProjGrad texture2DProjGradEXT\n"
// "#define textureCubeGrad textureCubeGradEXT\n"
);
}
@ -1831,7 +1830,7 @@ int compileShader(int _argc, const char* _argv[])
fprintf(stderr, "Failed to build shader.\n");
return EXIT_FAILURE;
}
}
} // namespace bgfx

View file

@ -8,9 +8,8 @@
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]);
const glslopt_shader_type type = ch == 'f'
? kGlslOptShaderFragment
@ -212,6 +211,6 @@ bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::str
glslopt_cleanup(ctx);
return true;
}
}
} // namespace bgfx