ソースを参照

shaderc source split into multiple files.

Branimir Karadžić 11 年 前
コミット
8cd9551f26

+ 9 - 988
tools/shaderc/shaderc.cpp

@@ -3,50 +3,10 @@
  * License: http://www.opensource.org/licenses/BSD-2-Clause
  */
 
-#define _BX_TRACE(_format, ...) \
-				BX_MACRO_BLOCK_BEGIN \
-					if (g_verbose) \
-					{ \
-						fprintf(stderr, BX_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__); \
-					} \
-				BX_MACRO_BLOCK_END
-
-#define _BX_WARN(_condition, _format, ...) \
-				BX_MACRO_BLOCK_BEGIN \
-					if (!(_condition) ) \
-					{ \
-						BX_TRACE("WARN " _format, ##__VA_ARGS__); \
-					} \
-				BX_MACRO_BLOCK_END
-
-#define _BX_CHECK(_condition, _format, ...) \
-				BX_MACRO_BLOCK_BEGIN \
-					if (!(_condition) ) \
-					{ \
-						BX_TRACE("CHECK " _format, ##__VA_ARGS__); \
-						bx::debugBreak(); \
-					} \
-				BX_MACRO_BLOCK_END
-
-#define BX_TRACE _BX_TRACE
-#define BX_WARN  _BX_WARN
-#define BX_CHECK _BX_CHECK
+#include "shaderc.h"
 
 bool g_verbose = false;
 
-#include <bx/bx.h>
-#include <bx/debug.h>
-
-#include <alloca.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <algorithm>
-#include <string>
-#include <vector>
-#include <unordered_map>
-
 #define MAX_TAGS 256
 extern "C"
 {
@@ -57,27 +17,6 @@ extern "C"
 #define BGFX_CHUNK_MAGIC_FSH BX_MAKEFOURCC('F', 'S', 'H', 0x3)
 #define BGFX_CHUNK_MAGIC_VSH BX_MAKEFOURCC('V', 'S', 'H', 0x3)
 
-#include <bx/commandline.h>
-#include <bx/endian.h>
-#include <bx/uint32_t.h>
-#include <bx/readerwriter.h>
-#include <bx/string.h>
-#include <bx/hash.h>
-
-#include "glsl_optimizer.h"
-
-#include "../../src/vertexdecl.h"
-
-#if BX_PLATFORM_WINDOWS
-#	include <sal.h>
-#	define __D3DX9MATH_INL__ // not used and MinGW complains about type-punning
-	BX_PRAGMA_DIAGNOSTIC_PUSH();
-	BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wundef");
-#	include <d3dx9.h>
-#	include <d3dcompiler.h>
-	BX_PRAGMA_DIAGNOSTIC_POP();
-#endif // BX_PLATFORM_WINDOWS
-
 long int fsize(FILE* _file)
 {
 	long int pos = ftell(_file);
@@ -128,71 +67,6 @@ static const char* s_OES_texture_3D[] =
 	NULL
 };
 
-struct RemapInputSemantic
-{
-	bgfx::Attrib::Enum m_attr;
-	const char* m_name;
-	uint8_t m_index;
-};
-
-static const RemapInputSemantic s_remapInputSemantic[bgfx::Attrib::Count+1] =
-{
-	{ bgfx::Attrib::Position,  "POSITION",     0 },
-	{ bgfx::Attrib::Normal,    "NORMAL",       0 },
-	{ bgfx::Attrib::Tangent,   "TANGENT",      0 },
-	{ bgfx::Attrib::Bitangent, "BITANGENT",    0 },
-	{ bgfx::Attrib::Color0,    "COLOR",        0 },
-	{ bgfx::Attrib::Color1,    "COLOR",        1 },
-	{ bgfx::Attrib::Indices,   "BLENDINDICES", 0 },
-	{ bgfx::Attrib::Weight,    "BLENDWEIGHT",  0 },
-	{ bgfx::Attrib::TexCoord0, "TEXCOORD",     0 },
-	{ bgfx::Attrib::TexCoord1, "TEXCOORD",     1 },
-	{ bgfx::Attrib::TexCoord2, "TEXCOORD",     2 },
-	{ bgfx::Attrib::TexCoord3, "TEXCOORD",     3 },
-	{ bgfx::Attrib::TexCoord4, "TEXCOORD",     4 },
-	{ bgfx::Attrib::TexCoord5, "TEXCOORD",     5 },
-	{ bgfx::Attrib::TexCoord6, "TEXCOORD",     6 },
-	{ bgfx::Attrib::TexCoord7, "TEXCOORD",     7 },
-	{ bgfx::Attrib::Count,     "",             0 },
-};
-
-const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
-{
-	for (uint32_t ii = 0; ii < bgfx::Attrib::Count; ++ii)
-	{
-		const RemapInputSemantic& ris = s_remapInputSemantic[ii];
-		if (0 == strcmp(ris.m_name, _name)
-		&&  ris.m_index == _index)
-		{
-			return ris;
-		}
-	}
-
-	return s_remapInputSemantic[bgfx::Attrib::Count];
-}
-
-struct UniformType
-{
-	enum Enum
-	{
-		Uniform1i,
-		Uniform1f,
-		End,
-
-		Uniform1iv,
-		Uniform1fv,
-		Uniform2fv,
-		Uniform3fv,
-		Uniform4fv,
-		Uniform3x3fv,
-		Uniform4x4fv,
-
-		Count
-	};
-};
-
-#define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10)
-
 const char* s_uniformTypeName[UniformType::Count] =
 {
 	"int",
@@ -207,30 +81,6 @@ const char* s_uniformTypeName[UniformType::Count] =
 	"mat4",
 };
 
-UniformType::Enum nameToUniformTypeEnum(const char* _name)
-{
-	for (uint32_t ii = 0; ii < UniformType::Count; ++ii)
-	{
-		if (NULL != s_uniformTypeName[ii]
-		&&  0 == strcmp(_name, s_uniformTypeName[ii]) )
-		{
-			return UniformType::Enum(ii);
-		}
-	}
-
-	return UniformType::Count;
-}
-
-struct Uniform
-{
-	std::string name;
-	UniformType::Enum type;
-	uint8_t num;
-	uint16_t regIndex;
-	uint16_t regCount;
-};
-typedef std::vector<Uniform> UniformArray;
-
 const char* interpolationDx11(const char* _glsl)
 {
 	if (0 == strcmp(_glsl, "smooth") )
@@ -245,114 +95,25 @@ const char* interpolationDx11(const char* _glsl)
 	return _glsl; // noperspective
 }
 
-#if BX_PLATFORM_WINDOWS
-struct UniformRemapDx9
-{
-	UniformType::Enum id;
-	D3DXPARAMETER_CLASS paramClass;
-	D3DXPARAMETER_TYPE paramType;
-	uint8_t columns;
-	uint8_t rows;
-};
-
-static const UniformRemapDx9 s_constRemapDx9[7] =
-{
-	{ UniformType::Uniform1iv,   D3DXPC_SCALAR,         D3DXPT_INT,   0, 0 },
-	{ UniformType::Uniform1fv,   D3DXPC_SCALAR,         D3DXPT_FLOAT, 0, 0 },
-	{ UniformType::Uniform2fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
-	{ UniformType::Uniform3fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
-	{ UniformType::Uniform4fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
-	{ UniformType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 3, 3 },
-	{ UniformType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4 },
-};
-
-UniformType::Enum findUniformTypeDx9(const D3DXCONSTANT_DESC& constDesc)
+const char* getUniformTypeName(UniformType::Enum _enum)
 {
-	for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx9); ++ii)
-	{
-		const UniformRemapDx9& remap = s_constRemapDx9[ii];
-
-		if (remap.paramClass == constDesc.Class
-		&&  remap.paramType  == constDesc.Type)
-		{
-			if (D3DXPC_MATRIX_COLUMNS != constDesc.Class)
-			{
-				return remap.id;
-			}
-
-			if (remap.columns == constDesc.Columns
-			&&  remap.rows    == constDesc.Rows)
-			{
-				return remap.id;
-			}
-		}
-	}
-
-	return UniformType::Count;
+	return s_uniformTypeName[_enum];
 }
 
-static uint32_t s_optimizationLevelDx9[4] =
-{
-	D3DXSHADER_OPTIMIZATION_LEVEL0,
-	D3DXSHADER_OPTIMIZATION_LEVEL1,
-	D3DXSHADER_OPTIMIZATION_LEVEL2,
-	D3DXSHADER_OPTIMIZATION_LEVEL3,
-};
-
-struct UniformRemapDx11
-{
-	UniformType::Enum id;
-	D3D_SHADER_VARIABLE_CLASS paramClass;
-	D3D_SHADER_VARIABLE_TYPE paramType;
-	uint8_t columns;
-	uint8_t rows;
-};
-
-static const UniformRemapDx11 s_constRemapDx11[7] =
-{
-	{ UniformType::Uniform1iv,   D3D_SVC_SCALAR,         D3D_SVT_INT,   0, 0 },
-	{ UniformType::Uniform1fv,   D3D_SVC_SCALAR,         D3D_SVT_FLOAT, 0, 0 },
-	{ UniformType::Uniform2fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
-	{ UniformType::Uniform3fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
-	{ UniformType::Uniform4fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
-	{ UniformType::Uniform3x3fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 3, 3 },
-	{ UniformType::Uniform4x4fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 4, 4 },
-};
-
-UniformType::Enum findUniformTypeDx11(const D3D11_SHADER_TYPE_DESC& constDesc)
+UniformType::Enum nameToUniformTypeEnum(const char* _name)
 {
-	for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx11); ++ii)
+	for (uint32_t ii = 0; ii < UniformType::Count; ++ii)
 	{
-		const UniformRemapDx11& remap = s_constRemapDx11[ii];
-
-		if (remap.paramClass == constDesc.Class
-		&&  remap.paramType == constDesc.Type)
+		if (NULL != s_uniformTypeName[ii]
+		&&  0 == strcmp(_name, s_uniformTypeName[ii]) )
 		{
-			if (D3D_SVC_MATRIX_COLUMNS != constDesc.Class)
-			{
-				return remap.id;
-			}
-
-			if (remap.columns == constDesc.Columns
-			&&  remap.rows    == constDesc.Rows)
-			{
-				return remap.id;
-			}
+			return UniformType::Enum(ii);
 		}
 	}
 
 	return UniformType::Count;
 }
 
-static uint32_t s_optimizationLevelDx11[4] =
-{
-	D3DCOMPILE_OPTIMIZATION_LEVEL0,
-	D3DCOMPILE_OPTIMIZATION_LEVEL1,
-	D3DCOMPILE_OPTIMIZATION_LEVEL2,
-	D3DCOMPILE_OPTIMIZATION_LEVEL3,
-};
-#endif // BX_PLATFORM_WINDOWS
-
 int32_t writef(bx::WriterI* _writer, const char* _format, ...)
 {
 	va_list argList;
@@ -591,7 +352,7 @@ public:
 	uint32_t m_size;
 };
 
-void printCode(const char* _code, int32_t _line = 0, int32_t _start = 0, int32_t _end = INT32_MAX)
+void printCode(const char* _code, int32_t _line, int32_t _start, int32_t _end)
 {
 	fprintf(stderr, "Code:\n---\n");
 
@@ -621,746 +382,6 @@ void writeFile(const char* _filePath, const void* _data, int32_t _size)
 	}
 }
 
-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
-		: (ch == 'c' ? kGlslOptShaderCompute : kGlslOptShaderVertex);
-
-	glslopt_target target = kGlslTargetOpenGL;
-	switch (_gles)
-	{
-		case 2:
-			target = kGlslTargetOpenGLES20;
-			break;
-
-		case 3:
-			target = kGlslTargetOpenGLES30;
-			break;
-
-		default:
-			target = kGlslTargetOpenGL;
-			break;
-	}
-
-	glslopt_ctx* ctx = glslopt_initialize(target);
-
-	glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
-
-	if (!glslopt_get_status(shader) )
-	{
-		const char* log = glslopt_get_log(shader);
-		int32_t source = 0;
-		int32_t line = 0;
-		int32_t column = 0;
-		int32_t start = 0;
-		int32_t end = INT32_MAX;
-
-		if (3 == sscanf(log, "%u:%u(%u):", &source, &line, &column)
-		&&  0 != line)
-		{
-			start = bx::uint32_imax(1, line-10);
-			end = start + 20;
-		}
-
-		printCode(_code.c_str(), line, start, end);
-		fprintf(stderr, "Error: %s\n", log);
-		glslopt_cleanup(ctx);
-		return false;
-	}
-
-	const char* optimizedShader = glslopt_get_output(shader);
-
-	// Trim all directives.
-	while ('#' == *optimizedShader)
-	{
-		optimizedShader = bx::strnl(optimizedShader);
-	}
-
-	if (0 != _gles)
-	{
-		char* shader = const_cast<char*>(optimizedShader);
-		strreplace(shader, "gl_FragDepthEXT", "gl_FragDepth");
-
-		strreplace(shader, "texture2DLodEXT", "texture2DLod");
-		strreplace(shader, "texture2DProjLodEXT", "texture2DProjLod");
-		strreplace(shader, "textureCubeLodEXT", "textureCubeLod");
-		strreplace(shader, "texture2DGradEXT", "texture2DGrad");
-		strreplace(shader, "texture2DProjGradEXT", "texture2DProjGrad");
-		strreplace(shader, "textureCubeGradEXT", "textureCubeGrad");
-
-		strreplace(shader, "shadow2DEXT", "shadow2D");
-		strreplace(shader, "shadow2DProjEXT", "shadow2DProj");
-	}
-
-	UniformArray uniforms;
-
-	{
-		const char* parse = optimizedShader;
-
-		while (NULL != parse
-		   &&  *parse != '\0')
-		{
-			parse = bx::strws(parse);
-			const char* eol = strchr(parse, ';');
-			if (NULL != eol)
-			{
-				const char* qualifier = parse;
-				parse = bx::strws(bx::strword(parse) );
-
-				if (0 == strncmp(qualifier, "attribute", 9)
-				||  0 == strncmp(qualifier, "varying", 7) )
-				{
-					// skip attributes and varyings.
-					parse = eol + 1;
-					continue;
-				}
-
-				if (0 != strncmp(qualifier, "uniform", 7) )
-				{
-					// end if there is no uniform keyword.
-					parse = NULL;
-					continue;
-				}
-
-				const char* precision = NULL;
-				const char* type = parse;
-
-				if (0 == strncmp(type, "lowp", 4)
-				||  0 == strncmp(type, "mediump", 7)
-				||  0 == strncmp(type, "highp", 5) )
-				{
-					precision = type;
-					type = parse = bx::strws(bx::strword(parse) );
-				}
-
-				BX_UNUSED(precision);
-
-				char uniformType[256];
-				parse = bx::strword(parse);
-
-				if (0 == strncmp(type, "sampler", 7) )
-				{
-					strcpy(uniformType, "int");
-				}
-				else
-				{
-					bx::strlcpy(uniformType, type, parse-type+1);
-				}
-
-				const char* name = parse = bx::strws(parse);
-
-				char uniformName[256];
-				uint8_t num = 1;
-				const char* array = bx::strnstr(name, "[", eol-parse);
-				if (NULL != array)
-				{
-					bx::strlcpy(uniformName, name, array-name+1);
-
-					char arraySize[32];
-					const char* end = bx::strnstr(array, "]", eol-array);
-					bx::strlcpy(arraySize, array+1, end-array);
-					num = atoi(arraySize);
-				}
-				else
-				{
-					bx::strlcpy(uniformName, name, eol-name+1);
-				}
-
-				Uniform un;
-				un.type = nameToUniformTypeEnum(uniformType);
-
-				if (UniformType::Count != un.type)
-				{
-					BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
-
-					un.name = uniformName;
-					un.num = num;
-					un.regIndex = 0;
-					un.regCount = num;
-					uniforms.push_back(un);
-				}
-
-				parse = eol + 1;
-			}
-		}
-	}
-
-	uint16_t count = (uint16_t)uniforms.size();
-	bx::write(_writer, count);
-
-	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
-	{
-		const Uniform& un = *it;
-		uint8_t nameSize = (uint8_t)un.name.size();
-		bx::write(_writer, nameSize);
-		bx::write(_writer, un.name.c_str(), nameSize);
-		uint8_t type = un.type;
-		bx::write(_writer, type);
-		bx::write(_writer, un.num);
-		bx::write(_writer, un.regIndex);
-		bx::write(_writer, un.regCount);
-
-		BX_TRACE("%s, %s, %d, %d, %d"
-			, un.name.c_str()
-			, s_uniformTypeName[un.type]
-			, un.num
-			, un.regIndex
-			, un.regCount
-			);
-	}
-
-	uint32_t shaderSize = (uint32_t)strlen(optimizedShader);
-	bx::write(_writer, shaderSize);
-	bx::write(_writer, optimizedShader, shaderSize);
-	uint8_t nul = 0;
-	bx::write(_writer, nul);
-
-	glslopt_cleanup(ctx);
-
-	return true;
-}
-
-bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
-{
-#if BX_PLATFORM_WINDOWS
-	BX_TRACE("DX9");
-
-	const char* profile = _cmdLine.findOption('p', "profile");
-	if (NULL == profile)
-	{
-		fprintf(stderr, "Shader profile must be specified.\n");
-		return false;
-	}
-
-	bool debug = _cmdLine.hasArg('\0', "debug");
-
-	uint32_t flags = 0;
-	flags |= debug ? D3DXSHADER_DEBUG : 0;
-	flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
-	flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
-	flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
-	flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
-	flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
-
-	bool werror = _cmdLine.hasArg('\0', "Werror");
-
-	uint32_t optimization = 3;
-	if (_cmdLine.hasArg(optimization, 'O') )
-	{
-		optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx9)-1);
-		flags |= s_optimizationLevelDx9[optimization];
-	}
-	else
-	{
-		flags |= D3DXSHADER_SKIPOPTIMIZATION;
-	}
-
-	BX_TRACE("Profile: %s", profile);
-	BX_TRACE("Flags: 0x%08x", flags);
-
-	LPD3DXBUFFER code;
-	LPD3DXBUFFER errorMsg;
-	LPD3DXCONSTANTTABLE constantTable;
-
-	HRESULT hr;
-
-	// Output preprocessed shader so that HLSL can be debugged via GPA
-	// or PIX. Compiling through memory won't embed preprocessed shader
-	// file path.
-	if (debug)
-	{
-		std::string hlslfp = _cmdLine.findOption('o');
-		hlslfp += ".hlsl";
-		writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
-
-		hr = D3DXCompileShaderFromFileA(hlslfp.c_str()
-				, NULL
-				, NULL
-				, "main"
-				, profile
-				, flags
-				, &code
-				, &errorMsg
-				, &constantTable
-				);
-	}
-	else
-	{
-		hr = D3DXCompileShader(_code.c_str()
-				, (uint32_t)_code.size()
-				, NULL
-				, NULL
-				, "main"
-				, profile
-				, flags
-				, &code
-				, &errorMsg
-				, &constantTable
-				);
-	}
-
-	if (FAILED(hr)
-	|| (werror && NULL != errorMsg) )
-	{
-		const char* log = (const char*)errorMsg->GetBufferPointer();
-
-		char source[1024];
-		int32_t line = 0;
-		int32_t column = 0;
-		int32_t start = 0;
-		int32_t end = INT32_MAX;
-
-		if (3 == sscanf(log, "%[^(](%u,%u):", source, &line, &column)
-		&&  0 != line)
-		{
-			start = bx::uint32_imax(1, line-10);
-			end = start + 20;
-		}
-
-		printCode(_code.c_str(), line, start, end);
-		fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
-		errorMsg->Release();
-		return false;
-	}
-
-	UniformArray uniforms;
-
-	if (NULL != constantTable)
-	{
-		D3DXCONSTANTTABLE_DESC desc;
-		hr = constantTable->GetDesc(&desc);
-		if (FAILED(hr) )
-		{
-			fprintf(stderr, "Error 0x%08x\n", (uint32_t)hr);
-			return false;
-		}
-
-		BX_TRACE("Creator: %s 0x%08x", desc.Creator, (uint32_t /*mingw warning*/)desc.Version);
-		BX_TRACE("Num constants: %d", desc.Constants);
-		BX_TRACE("#   cl ty RxC   S  By Name");
-
-		for (uint32_t ii = 0; ii < desc.Constants; ++ii)
-		{
-			D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
-			D3DXCONSTANT_DESC constDesc;
-			uint32_t count;
-			constantTable->GetConstantDesc(handle, &constDesc, &count);
-			BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
-				, ii
-				, constDesc.Class
-				, constDesc.Type
-				, constDesc.Rows
-				, constDesc.Columns
-				, constDesc.StructMembers
-				, constDesc.Bytes
-				, constDesc.Name
-				, constDesc.Elements
-				, constDesc.RegisterIndex
-				, constDesc.RegisterCount
-				);
-
-			UniformType::Enum type = findUniformTypeDx9(constDesc);
-			if (UniformType::Count != type)
-			{
-				Uniform un;
-				un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
-				un.type = type;
-				un.num = constDesc.Elements;
-				un.regIndex = constDesc.RegisterIndex;
-				un.regCount = constDesc.RegisterCount;
-				uniforms.push_back(un);
-			}
-		}
-	}
-
-	uint16_t count = (uint16_t)uniforms.size();
-	bx::write(_writer, count);
-
-	uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
-	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
-	{
-		const Uniform& un = *it;
-		uint8_t nameSize = (uint8_t)un.name.size();
-		bx::write(_writer, nameSize);
-		bx::write(_writer, un.name.c_str(), nameSize);
-		uint8_t type = un.type|fragmentBit;
-		bx::write(_writer, type);
-		bx::write(_writer, un.num);
-		bx::write(_writer, un.regIndex);
-		bx::write(_writer, un.regCount);
-
-		BX_TRACE("%s, %s, %d, %d, %d"
-			, un.name.c_str()
-			, s_uniformTypeName[un.type]
-			, un.num
-			, un.regIndex
-			, un.regCount
-			);
-	}
-
-	uint16_t shaderSize = (uint16_t)code->GetBufferSize();
-	bx::write(_writer, shaderSize);
-	bx::write(_writer, code->GetBufferPointer(), shaderSize);
-	uint8_t nul = 0;
-	bx::write(_writer, nul);
-
-	if (_cmdLine.hasArg('\0', "disasm") )
-	{
-		LPD3DXBUFFER disasm;
-		D3DXDisassembleShader( (const DWORD*)code->GetBufferPointer()
-			, false
-			, NULL
-			, &disasm
-			);
-
-		if (NULL != disasm)
-		{
-			std::string disasmfp = _cmdLine.findOption('o');
-			disasmfp += ".disasm";
-
-			writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), disasm->GetBufferSize() );
-			disasm->Release();
-		}
-	}
-
-	if (NULL != code)
-	{
-		code->Release();
-	}
-
-	if (NULL != errorMsg)
-	{
-		errorMsg->Release();
-	}
-
-	if (NULL != constantTable)
-	{
-		constantTable->Release();
-	}
-
-	return true;
-#else
-	BX_UNUSED(_cmdLine, _code, _writer);
-	fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
-	return false;
-#endif // BX_PLATFORM_WINDOWS
-}
-
-bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
-{
-#if BX_PLATFORM_WINDOWS
-	BX_TRACE("DX11");
-
-	const char* profile = _cmdLine.findOption('p', "profile");
-	if (NULL == profile)
-	{
-		fprintf(stderr, "Shader profile must be specified.\n");
-		return false;
-	}
-
-	bool debug = _cmdLine.hasArg('\0', "debug");
-
-	uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
-	flags |= debug ? D3DCOMPILE_DEBUG : 0;
-	flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
-	flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
-	flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
-	flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
-	flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
-
-	bool werror = _cmdLine.hasArg('\0', "Werror");
-
-	if (werror)
-	{
-		flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
-	}
-
-	uint32_t optimization = 3;
-	if (_cmdLine.hasArg(optimization, 'O') )
-	{
-		optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx11)-1);
-		flags |= s_optimizationLevelDx11[optimization];
-	}
-	else
-	{
-		flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
-	}
-
-	BX_TRACE("Profile: %s", profile);
-	BX_TRACE("Flags: 0x%08x", flags);
-
-	ID3DBlob* code;
-	ID3DBlob* errorMsg;
-
-	// Output preprocessed shader so that HLSL can be debugged via GPA
-	// or PIX. Compiling through memory won't embed preprocessed shader
-	// file path.
-	std::string hlslfp;
-
-	if (debug)
-	{
-		hlslfp = _cmdLine.findOption('o');
-		hlslfp += ".hlsl";
-		writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
-	}
-
-	HRESULT hr = D3DCompile(_code.c_str()
-					, _code.size()
-					, hlslfp.c_str()
-					, NULL
-					, NULL
-					, "main"
-					, profile
-					, flags
-					, 0
-					, &code
-					, &errorMsg
-					);
-	if (FAILED(hr)
-	|| (werror && NULL != errorMsg) )
-	{
-		const char* log = (char*)errorMsg->GetBufferPointer();
-
-		int32_t line = 0;
-		int32_t column = 0;
-		int32_t start = 0;
-		int32_t end = INT32_MAX;
-
-		if (2 == sscanf(log, "(%u,%u):", &line, &column)
-		&&  0 != line)
-		{
-			start = bx::uint32_imax(1, line-10);
-			end = start + 20;
-		}
-
-		printCode(_code.c_str(), line, start, end);
-		fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
-		errorMsg->Release();
-		return false;
-	}
-
-	UniformArray uniforms;
-
-	ID3D11ShaderReflection* reflect = NULL;
-	hr = D3DReflect(code->GetBufferPointer()
-		, code->GetBufferSize()
-		, IID_ID3D11ShaderReflection
-		, (void**)&reflect
-		);
-	if (FAILED(hr) )
-	{
-		fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
-		return false;
-	}
-
-	D3D11_SHADER_DESC desc;
-	hr = reflect->GetDesc(&desc);
-	if (FAILED(hr) )
-	{
-		fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", (uint32_t)hr);
-		return false;
-	}
-
-	BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
-	BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
-
-	BX_TRACE("Input:");
-	uint8_t numAttrs = 0;
-	uint16_t attrs[bgfx::Attrib::Count];
-
-	if (profile[0] == 'v') // Only care about input semantic on vertex shaders
-	{
-		for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
-		{
-			D3D11_SIGNATURE_PARAMETER_DESC spd;
-			reflect->GetInputParameterDesc(ii, &spd);
-			BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
-				, ii
-				, spd.SemanticName
-				, spd.SemanticIndex
-				, spd.SystemValueType
-				, spd.ComponentType
-				, spd.Mask
-				, spd.Register
-				);
-
-			const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
-			if (ris.m_attr != bgfx::Attrib::Count)
-			{
-				attrs[numAttrs] = bgfx::attribToId(ris.m_attr);
-				++numAttrs;
-			}
-		}
-	}
-
-	BX_TRACE("Output:");
-	for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
-	{
-		D3D11_SIGNATURE_PARAMETER_DESC spd;
-		reflect->GetOutputParameterDesc(ii, &spd);
-		BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
-	}
-
-	uint16_t size = 0;
-
-	for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
-	{
-		ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
-		D3D11_SHADER_BUFFER_DESC bufferDesc;
-		hr = cbuffer->GetDesc(&bufferDesc);
-
-		size = (uint16_t)bufferDesc.Size;
-
-		if (SUCCEEDED(hr) )
-		{
-			BX_TRACE("%s, %d, vars %d, size %d"
-				, bufferDesc.Name
-				, bufferDesc.Type
-				, bufferDesc.Variables
-				, bufferDesc.Size
-				);
-
-			for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
-			{
-				ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
-				ID3D11ShaderReflectionType* type = var->GetType();
-				D3D11_SHADER_VARIABLE_DESC varDesc;
-				hr = var->GetDesc(&varDesc);
-				if (SUCCEEDED(hr) )
-				{
-					D3D11_SHADER_TYPE_DESC constDesc;
-					hr = type->GetDesc(&constDesc);
-					if (SUCCEEDED(hr) )
-					{
-						UniformType::Enum type = findUniformTypeDx11(constDesc);
-
-						if (UniformType::Count != type
-						&&  0 != (varDesc.uFlags & D3D_SVF_USED) )
-						{
-							Uniform un;
-							un.name = varDesc.Name;
-							un.type = type;
-							un.num = constDesc.Elements;
-							un.regIndex = varDesc.StartOffset;
-							un.regCount = BX_ALIGN_16(varDesc.Size)/16;
-							uniforms.push_back(un);
-
-							BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
-								, varDesc.Name
-								, varDesc.StartOffset
-								, varDesc.Size
-								, varDesc.uFlags
-								, type
-								);
-						}
-						else
-						{
-							BX_TRACE("\t%s, unknown type", varDesc.Name);
-						}
-					}
-				}
-			}
-		}
-	}
-
-	BX_TRACE("Bound:");
-	for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
-	{
-		D3D11_SHADER_INPUT_BIND_DESC bindDesc;
-
-		hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
-		if (SUCCEEDED(hr) )
-		{
-//			if (bindDesc.Type == D3D_SIT_SAMPLER)
-			{
-				BX_TRACE("\t%s, %d, %d, %d"
-					, bindDesc.Name
-					, bindDesc.Type
-					, bindDesc.BindPoint
-					, bindDesc.BindCount
-					);
-			}
-		}
-	}
-
-	uint16_t count = (uint16_t)uniforms.size();
-	bx::write(_writer, count);
-
-	uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
-	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
-	{
-		const Uniform& un = *it;
-		uint8_t nameSize = (uint8_t)un.name.size();
-		bx::write(_writer, nameSize);
-		bx::write(_writer, un.name.c_str(), nameSize);
-		uint8_t type = un.type|fragmentBit;
-		bx::write(_writer, type);
-		bx::write(_writer, un.num);
-		bx::write(_writer, un.regIndex);
-		bx::write(_writer, un.regCount);
-
-		BX_TRACE("%s, %s, %d, %d, %d"
-			, un.name.c_str()
-			, s_uniformTypeName[un.type]
-			, un.num
-			, un.regIndex
-			, un.regCount
-			);
-	}
-
-	uint16_t shaderSize = (uint16_t)code->GetBufferSize();
-	bx::write(_writer, shaderSize);
-	bx::write(_writer, code->GetBufferPointer(), shaderSize);
-	uint8_t nul = 0;
-	bx::write(_writer, nul);
-
-	bx::write(_writer, numAttrs);
-	bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );
-
-	bx::write(_writer, size);
-
-	if (_cmdLine.hasArg('\0', "disasm") )
-	{
-		ID3DBlob* disasm;
-		D3DDisassemble(code->GetBufferPointer()
-			, code->GetBufferSize()
-			, 0
-			, NULL
-			, &disasm
-			);
-
-		if (NULL != disasm)
-		{
-			std::string disasmfp = _cmdLine.findOption('o');
-			disasmfp += ".disasm";
-
-			writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
-			disasm->Release();
-		}
-	}
-
-	if (NULL != reflect)
-	{
-		reflect->Release();
-	}
-
-	if (NULL != errorMsg)
-	{
-		errorMsg->Release();
-	}
-
-	code->Release();
-
-	return true;
-#else
-	BX_UNUSED(_cmdLine, _code, _writer);
-	fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
-	return false;
-#endif // BX_PLATFORM_WINDOWS
-}
-
 struct Preprocessor
 {
 	Preprocessor(const char* _filePath, bool _gles, const char* _includeDir = NULL)

+ 113 - 0
tools/shaderc/shaderc.h

@@ -0,0 +1,113 @@
+/*
+ * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#ifndef SHADERC_H_HEADER_GUARD
+#define SHADERC_H_HEADER_GUARD
+
+#define _BX_TRACE(_format, ...) \
+				BX_MACRO_BLOCK_BEGIN \
+					if (g_verbose) \
+					{ \
+						fprintf(stderr, BX_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__); \
+					} \
+				BX_MACRO_BLOCK_END
+
+#define _BX_WARN(_condition, _format, ...) \
+				BX_MACRO_BLOCK_BEGIN \
+					if (!(_condition) ) \
+					{ \
+						BX_TRACE("WARN " _format, ##__VA_ARGS__); \
+					} \
+				BX_MACRO_BLOCK_END
+
+#define _BX_CHECK(_condition, _format, ...) \
+				BX_MACRO_BLOCK_BEGIN \
+					if (!(_condition) ) \
+					{ \
+						BX_TRACE("CHECK " _format, ##__VA_ARGS__); \
+						bx::debugBreak(); \
+					} \
+				BX_MACRO_BLOCK_END
+
+#define BX_TRACE _BX_TRACE
+#define BX_WARN  _BX_WARN
+#define BX_CHECK _BX_CHECK
+
+#ifndef SHADERC_CONFIG_DIRECT3D9
+#	define SHADERC_CONFIG_DIRECT3D9 BX_PLATFORM_WINDOWS
+#endif // SHADERC_CONFIG_DIRECT3D9
+
+#ifndef SHADERC_CONFIG_DIRECT3D11
+#	define SHADERC_CONFIG_DIRECT3D11 BX_PLATFORM_WINDOWS
+#endif // SHADERC_CONFIG_DIRECT3D11
+
+extern bool g_verbose;
+
+#include <alloca.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <unordered_map>
+
+#include <bx/bx.h>
+#include <bx/debug.h>
+#include <bx/commandline.h>
+#include <bx/endian.h>
+#include <bx/uint32_t.h>
+#include <bx/readerwriter.h>
+#include <bx/string.h>
+#include <bx/hash.h>
+#include "../../src/vertexdecl.h"
+
+struct UniformType
+{
+	enum Enum
+	{
+		Uniform1i,
+		Uniform1f,
+		End,
+
+		Uniform1iv,
+		Uniform1fv,
+		Uniform2fv,
+		Uniform3fv,
+		Uniform4fv,
+		Uniform3x3fv,
+		Uniform4x4fv,
+
+		Count
+	};
+};
+
+#define BGFX_UNIFORM_FRAGMENTBIT UINT8_C(0x10)
+
+const char* getUniformTypeName(UniformType::Enum _enum);
+UniformType::Enum nameToUniformTypeEnum(const char* _name);
+
+struct Uniform
+{
+	std::string name;
+	UniformType::Enum type;
+	uint8_t num;
+	uint16_t regIndex;
+	uint16_t regCount;
+};
+
+typedef std::vector<Uniform> UniformArray;
+
+void printCode(const char* _code, int32_t _line = 0, int32_t _start = 0, int32_t _end = INT32_MAX);
+void strreplace(char* _str, const char* _find, const char* _replace);
+int32_t writef(bx::WriterI* _writer, const char* _format, ...);
+void writeFile(const char* _filePath, const void* _data, int32_t _size);
+
+bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer);
+bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer);
+bool compileGLSLShader(bx::CommandLine& _cmdLine, uint32_t _gles, const std::string& _code, bx::WriterI* _writer);
+
+#endif // SHADERC_H_HEADER_GUARD

+ 424 - 0
tools/shaderc/shaderc_dx11.cpp

@@ -0,0 +1,424 @@
+/*
+ * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "shaderc.h"
+
+#if SHADERC_CONFIG_DIRECT3D11
+
+#include <d3dcompiler.h>
+
+struct RemapInputSemantic
+{
+	bgfx::Attrib::Enum m_attr;
+	const char* m_name;
+	uint8_t m_index;
+};
+
+static const RemapInputSemantic s_remapInputSemantic[bgfx::Attrib::Count+1] =
+{
+	{ bgfx::Attrib::Position,  "POSITION",     0 },
+	{ bgfx::Attrib::Normal,    "NORMAL",       0 },
+	{ bgfx::Attrib::Tangent,   "TANGENT",      0 },
+	{ bgfx::Attrib::Bitangent, "BITANGENT",    0 },
+	{ bgfx::Attrib::Color0,    "COLOR",        0 },
+	{ bgfx::Attrib::Color1,    "COLOR",        1 },
+	{ bgfx::Attrib::Indices,   "BLENDINDICES", 0 },
+	{ bgfx::Attrib::Weight,    "BLENDWEIGHT",  0 },
+	{ bgfx::Attrib::TexCoord0, "TEXCOORD",     0 },
+	{ bgfx::Attrib::TexCoord1, "TEXCOORD",     1 },
+	{ bgfx::Attrib::TexCoord2, "TEXCOORD",     2 },
+	{ bgfx::Attrib::TexCoord3, "TEXCOORD",     3 },
+	{ bgfx::Attrib::TexCoord4, "TEXCOORD",     4 },
+	{ bgfx::Attrib::TexCoord5, "TEXCOORD",     5 },
+	{ bgfx::Attrib::TexCoord6, "TEXCOORD",     6 },
+	{ bgfx::Attrib::TexCoord7, "TEXCOORD",     7 },
+	{ bgfx::Attrib::Count,     "",             0 },
+};
+
+const RemapInputSemantic& findInputSemantic(const char* _name, uint8_t _index)
+{
+	for (uint32_t ii = 0; ii < bgfx::Attrib::Count; ++ii)
+	{
+		const RemapInputSemantic& ris = s_remapInputSemantic[ii];
+		if (0 == strcmp(ris.m_name, _name)
+			&&  ris.m_index == _index)
+		{
+			return ris;
+		}
+	}
+
+	return s_remapInputSemantic[bgfx::Attrib::Count];
+}
+
+struct UniformRemapDx11
+{
+	UniformType::Enum id;
+	D3D_SHADER_VARIABLE_CLASS paramClass;
+	D3D_SHADER_VARIABLE_TYPE paramType;
+	uint8_t columns;
+	uint8_t rows;
+};
+
+static const UniformRemapDx11 s_constRemapDx11[7] =
+{
+	{ UniformType::Uniform1iv,   D3D_SVC_SCALAR,         D3D_SVT_INT,   0, 0 },
+	{ UniformType::Uniform1fv,   D3D_SVC_SCALAR,         D3D_SVT_FLOAT, 0, 0 },
+	{ UniformType::Uniform2fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
+	{ UniformType::Uniform3fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
+	{ UniformType::Uniform4fv,   D3D_SVC_VECTOR,         D3D_SVT_FLOAT, 0, 0 },
+	{ UniformType::Uniform3x3fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 3, 3 },
+	{ UniformType::Uniform4x4fv, D3D_SVC_MATRIX_COLUMNS, D3D_SVT_FLOAT, 4, 4 },
+};
+
+UniformType::Enum findUniformTypeDx11(const D3D11_SHADER_TYPE_DESC& constDesc)
+{
+	for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx11); ++ii)
+	{
+		const UniformRemapDx11& remap = s_constRemapDx11[ii];
+
+		if (remap.paramClass == constDesc.Class
+			&&  remap.paramType == constDesc.Type)
+		{
+			if (D3D_SVC_MATRIX_COLUMNS != constDesc.Class)
+			{
+				return remap.id;
+			}
+
+			if (remap.columns == constDesc.Columns
+				&&  remap.rows    == constDesc.Rows)
+			{
+				return remap.id;
+			}
+		}
+	}
+
+	return UniformType::Count;
+}
+
+static uint32_t s_optimizationLevelDx11[4] =
+{
+	D3DCOMPILE_OPTIMIZATION_LEVEL0,
+	D3DCOMPILE_OPTIMIZATION_LEVEL1,
+	D3DCOMPILE_OPTIMIZATION_LEVEL2,
+	D3DCOMPILE_OPTIMIZATION_LEVEL3,
+};
+
+bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
+{
+	BX_TRACE("DX11");
+
+	const char* profile = _cmdLine.findOption('p', "profile");
+	if (NULL == profile)
+	{
+		fprintf(stderr, "Shader profile must be specified.\n");
+		return false;
+	}
+
+	bool debug = _cmdLine.hasArg('\0', "debug");
+
+	uint32_t flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
+	flags |= debug ? D3DCOMPILE_DEBUG : 0;
+	flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DCOMPILE_AVOID_FLOW_CONTROL : 0;
+	flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DCOMPILE_NO_PRESHADER : 0;
+	flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DCOMPILE_PARTIAL_PRECISION : 0;
+	flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DCOMPILE_PREFER_FLOW_CONTROL : 0;
+	flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY : 0;
+
+	bool werror = _cmdLine.hasArg('\0', "Werror");
+
+	if (werror)
+	{
+		flags |= D3DCOMPILE_WARNINGS_ARE_ERRORS;
+	}
+
+	uint32_t optimization = 3;
+	if (_cmdLine.hasArg(optimization, 'O') )
+	{
+		optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx11)-1);
+		flags |= s_optimizationLevelDx11[optimization];
+	}
+	else
+	{
+		flags |= D3DCOMPILE_SKIP_OPTIMIZATION;
+	}
+
+	BX_TRACE("Profile: %s", profile);
+	BX_TRACE("Flags: 0x%08x", flags);
+
+	ID3DBlob* code;
+	ID3DBlob* errorMsg;
+
+	// Output preprocessed shader so that HLSL can be debugged via GPA
+	// or PIX. Compiling through memory won't embed preprocessed shader
+	// file path.
+	std::string hlslfp;
+
+	if (debug)
+	{
+		hlslfp = _cmdLine.findOption('o');
+		hlslfp += ".hlsl";
+		writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
+	}
+
+	HRESULT hr = D3DCompile(_code.c_str()
+		, _code.size()
+		, hlslfp.c_str()
+		, NULL
+		, NULL
+		, "main"
+		, profile
+		, flags
+		, 0
+		, &code
+		, &errorMsg
+		);
+	if (FAILED(hr)
+		|| (werror && NULL != errorMsg) )
+	{
+		const char* log = (char*)errorMsg->GetBufferPointer();
+
+		int32_t line = 0;
+		int32_t column = 0;
+		int32_t start = 0;
+		int32_t end = INT32_MAX;
+
+		if (2 == sscanf(log, "(%u,%u):", &line, &column)
+			&&  0 != line)
+		{
+			start = bx::uint32_imax(1, line-10);
+			end = start + 20;
+		}
+
+		printCode(_code.c_str(), line, start, end);
+		fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
+		errorMsg->Release();
+		return false;
+	}
+
+	UniformArray uniforms;
+
+	ID3D11ShaderReflection* reflect = NULL;
+	hr = D3DReflect(code->GetBufferPointer()
+		, code->GetBufferSize()
+		, IID_ID3D11ShaderReflection
+		, (void**)&reflect
+		);
+	if (FAILED(hr) )
+	{
+		fprintf(stderr, "Error: 0x%08x\n", (uint32_t)hr);
+		return false;
+	}
+
+	D3D11_SHADER_DESC desc;
+	hr = reflect->GetDesc(&desc);
+	if (FAILED(hr) )
+	{
+		fprintf(stderr, BX_FILE_LINE_LITERAL "Error: 0x%08x\n", (uint32_t)hr);
+		return false;
+	}
+
+	BX_TRACE("Creator: %s 0x%08x", desc.Creator, desc.Version);
+	BX_TRACE("Num constant buffers: %d", desc.ConstantBuffers);
+
+	BX_TRACE("Input:");
+	uint8_t numAttrs = 0;
+	uint16_t attrs[bgfx::Attrib::Count];
+
+	if (profile[0] == 'v') // Only care about input semantic on vertex shaders
+	{
+		for (uint32_t ii = 0; ii < desc.InputParameters; ++ii)
+		{
+			D3D11_SIGNATURE_PARAMETER_DESC spd;
+			reflect->GetInputParameterDesc(ii, &spd);
+			BX_TRACE("\t%2d: %s%d, vt %d, ct %d, mask %x, reg %d"
+				, ii
+				, spd.SemanticName
+				, spd.SemanticIndex
+				, spd.SystemValueType
+				, spd.ComponentType
+				, spd.Mask
+				, spd.Register
+				);
+
+			const RemapInputSemantic& ris = findInputSemantic(spd.SemanticName, spd.SemanticIndex);
+			if (ris.m_attr != bgfx::Attrib::Count)
+			{
+				attrs[numAttrs] = bgfx::attribToId(ris.m_attr);
+				++numAttrs;
+			}
+		}
+	}
+
+	BX_TRACE("Output:");
+	for (uint32_t ii = 0; ii < desc.OutputParameters; ++ii)
+	{
+		D3D11_SIGNATURE_PARAMETER_DESC spd;
+		reflect->GetOutputParameterDesc(ii, &spd);
+		BX_TRACE("\t%2d: %s%d, %d, %d", ii, spd.SemanticName, spd.SemanticIndex, spd.SystemValueType, spd.ComponentType);
+	}
+
+	uint16_t size = 0;
+
+	for (uint32_t ii = 0; ii < bx::uint32_min(1, desc.ConstantBuffers); ++ii)
+	{
+		ID3D11ShaderReflectionConstantBuffer* cbuffer = reflect->GetConstantBufferByIndex(ii);
+		D3D11_SHADER_BUFFER_DESC bufferDesc;
+		hr = cbuffer->GetDesc(&bufferDesc);
+
+		size = (uint16_t)bufferDesc.Size;
+
+		if (SUCCEEDED(hr) )
+		{
+			BX_TRACE("%s, %d, vars %d, size %d"
+				, bufferDesc.Name
+				, bufferDesc.Type
+				, bufferDesc.Variables
+				, bufferDesc.Size
+				);
+
+			for (uint32_t jj = 0; jj < bufferDesc.Variables; ++jj)
+			{
+				ID3D11ShaderReflectionVariable* var = cbuffer->GetVariableByIndex(jj);
+				ID3D11ShaderReflectionType* type = var->GetType();
+				D3D11_SHADER_VARIABLE_DESC varDesc;
+				hr = var->GetDesc(&varDesc);
+				if (SUCCEEDED(hr) )
+				{
+					D3D11_SHADER_TYPE_DESC constDesc;
+					hr = type->GetDesc(&constDesc);
+					if (SUCCEEDED(hr) )
+					{
+						UniformType::Enum type = findUniformTypeDx11(constDesc);
+
+						if (UniformType::Count != type
+							&&  0 != (varDesc.uFlags & D3D_SVF_USED) )
+						{
+							Uniform un;
+							un.name = varDesc.Name;
+							un.type = type;
+							un.num = constDesc.Elements;
+							un.regIndex = varDesc.StartOffset;
+							un.regCount = BX_ALIGN_16(varDesc.Size)/16;
+							uniforms.push_back(un);
+
+							BX_TRACE("\t%s, %d, size %d, flags 0x%08x, %d"
+								, varDesc.Name
+								, varDesc.StartOffset
+								, varDesc.Size
+								, varDesc.uFlags
+								, type
+								);
+						}
+						else
+						{
+							BX_TRACE("\t%s, unknown type", varDesc.Name);
+						}
+					}
+				}
+			}
+		}
+	}
+
+	BX_TRACE("Bound:");
+	for (uint32_t ii = 0; ii < desc.BoundResources; ++ii)
+	{
+		D3D11_SHADER_INPUT_BIND_DESC bindDesc;
+
+		hr = reflect->GetResourceBindingDesc(ii, &bindDesc);
+		if (SUCCEEDED(hr) )
+		{
+			//			if (bindDesc.Type == D3D_SIT_SAMPLER)
+			{
+				BX_TRACE("\t%s, %d, %d, %d"
+					, bindDesc.Name
+					, bindDesc.Type
+					, bindDesc.BindPoint
+					, bindDesc.BindCount
+					);
+			}
+		}
+	}
+
+	uint16_t count = (uint16_t)uniforms.size();
+	bx::write(_writer, count);
+
+	uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
+	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
+	{
+		const Uniform& un = *it;
+		uint8_t nameSize = (uint8_t)un.name.size();
+		bx::write(_writer, nameSize);
+		bx::write(_writer, un.name.c_str(), nameSize);
+		uint8_t type = un.type|fragmentBit;
+		bx::write(_writer, type);
+		bx::write(_writer, un.num);
+		bx::write(_writer, un.regIndex);
+		bx::write(_writer, un.regCount);
+
+		BX_TRACE("%s, %s, %d, %d, %d"
+			, un.name.c_str()
+			, getUniformTypeName(un.type)
+			, un.num
+			, un.regIndex
+			, un.regCount
+			);
+	}
+
+	uint16_t shaderSize = (uint16_t)code->GetBufferSize();
+	bx::write(_writer, shaderSize);
+	bx::write(_writer, code->GetBufferPointer(), shaderSize);
+	uint8_t nul = 0;
+	bx::write(_writer, nul);
+
+	bx::write(_writer, numAttrs);
+	bx::write(_writer, attrs, numAttrs*sizeof(uint16_t) );
+
+	bx::write(_writer, size);
+
+	if (_cmdLine.hasArg('\0', "disasm") )
+	{
+		ID3DBlob* disasm;
+		D3DDisassemble(code->GetBufferPointer()
+			, code->GetBufferSize()
+			, 0
+			, NULL
+			, &disasm
+			);
+
+		if (NULL != disasm)
+		{
+			std::string disasmfp = _cmdLine.findOption('o');
+			disasmfp += ".disasm";
+
+			writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), (uint32_t)disasm->GetBufferSize() );
+			disasm->Release();
+		}
+	}
+
+	if (NULL != reflect)
+	{
+		reflect->Release();
+	}
+
+	if (NULL != errorMsg)
+	{
+		errorMsg->Release();
+	}
+
+	code->Release();
+
+	return true;
+}
+
+#else
+
+bool compileHLSLShaderDx11(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
+{
+	BX_UNUSED(_cmdLine, _code, _writer);
+	fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
+	return false;
+}
+
+#endif // SHADERC_CONFIG_DIRECT3D11

+ 299 - 0
tools/shaderc/shaderc_dx9.cpp

@@ -0,0 +1,299 @@
+/*
+ * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "shaderc.h"
+
+#if SHADERC_CONFIG_DIRECT3D9
+
+#include <sal.h>
+#define __D3DX9MATH_INL__ // not used and MinGW complains about type-punning
+BX_PRAGMA_DIAGNOSTIC_PUSH();
+BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wundef");
+#include <d3dx9.h>
+BX_PRAGMA_DIAGNOSTIC_POP();
+
+struct UniformRemapDx9
+{
+	UniformType::Enum id;
+	D3DXPARAMETER_CLASS paramClass;
+	D3DXPARAMETER_TYPE paramType;
+	uint8_t columns;
+	uint8_t rows;
+};
+
+static const UniformRemapDx9 s_constRemapDx9[7] =
+{
+	{ UniformType::Uniform1iv,   D3DXPC_SCALAR,         D3DXPT_INT,   0, 0 },
+	{ UniformType::Uniform1fv,   D3DXPC_SCALAR,         D3DXPT_FLOAT, 0, 0 },
+	{ UniformType::Uniform2fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
+	{ UniformType::Uniform3fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
+	{ UniformType::Uniform4fv,   D3DXPC_VECTOR,         D3DXPT_FLOAT, 0, 0 },
+	{ UniformType::Uniform3x3fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 3, 3 },
+	{ UniformType::Uniform4x4fv, D3DXPC_MATRIX_COLUMNS, D3DXPT_FLOAT, 4, 4 },
+};
+
+UniformType::Enum findUniformTypeDx9(const D3DXCONSTANT_DESC& constDesc)
+{
+	for (uint32_t ii = 0; ii < BX_COUNTOF(s_constRemapDx9); ++ii)
+	{
+		const UniformRemapDx9& remap = s_constRemapDx9[ii];
+
+		if (remap.paramClass == constDesc.Class
+			&&  remap.paramType  == constDesc.Type)
+		{
+			if (D3DXPC_MATRIX_COLUMNS != constDesc.Class)
+			{
+				return remap.id;
+			}
+
+			if (remap.columns == constDesc.Columns
+				&&  remap.rows    == constDesc.Rows)
+			{
+				return remap.id;
+			}
+		}
+	}
+
+	return UniformType::Count;
+}
+
+static uint32_t s_optimizationLevelDx9[4] =
+{
+	D3DXSHADER_OPTIMIZATION_LEVEL0,
+	D3DXSHADER_OPTIMIZATION_LEVEL1,
+	D3DXSHADER_OPTIMIZATION_LEVEL2,
+	D3DXSHADER_OPTIMIZATION_LEVEL3,
+};
+
+bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
+{
+	BX_TRACE("DX9");
+
+	const char* profile = _cmdLine.findOption('p', "profile");
+	if (NULL == profile)
+	{
+		fprintf(stderr, "Shader profile must be specified.\n");
+		return false;
+	}
+
+	bool debug = _cmdLine.hasArg('\0', "debug");
+
+	uint32_t flags = 0;
+	flags |= debug ? D3DXSHADER_DEBUG : 0;
+	flags |= _cmdLine.hasArg('\0', "avoid-flow-control") ? D3DXSHADER_AVOID_FLOW_CONTROL : 0;
+	flags |= _cmdLine.hasArg('\0', "no-preshader") ? D3DXSHADER_NO_PRESHADER : 0;
+	flags |= _cmdLine.hasArg('\0', "partial-precision") ? D3DXSHADER_PARTIALPRECISION : 0;
+	flags |= _cmdLine.hasArg('\0', "prefer-flow-control") ? D3DXSHADER_PREFER_FLOW_CONTROL : 0;
+	flags |= _cmdLine.hasArg('\0', "backwards-compatibility") ? D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY : 0;
+
+	bool werror = _cmdLine.hasArg('\0', "Werror");
+
+	uint32_t optimization = 3;
+	if (_cmdLine.hasArg(optimization, 'O') )
+	{
+		optimization = bx::uint32_min(optimization, BX_COUNTOF(s_optimizationLevelDx9)-1);
+		flags |= s_optimizationLevelDx9[optimization];
+	}
+	else
+	{
+		flags |= D3DXSHADER_SKIPOPTIMIZATION;
+	}
+
+	BX_TRACE("Profile: %s", profile);
+	BX_TRACE("Flags: 0x%08x", flags);
+
+	LPD3DXBUFFER code;
+	LPD3DXBUFFER errorMsg;
+	LPD3DXCONSTANTTABLE constantTable;
+
+	HRESULT hr;
+
+	// Output preprocessed shader so that HLSL can be debugged via GPA
+	// or PIX. Compiling through memory won't embed preprocessed shader
+	// file path.
+	if (debug)
+	{
+		std::string hlslfp = _cmdLine.findOption('o');
+		hlslfp += ".hlsl";
+		writeFile(hlslfp.c_str(), _code.c_str(), (int32_t)_code.size() );
+
+		hr = D3DXCompileShaderFromFileA(hlslfp.c_str()
+			, NULL
+			, NULL
+			, "main"
+			, profile
+			, flags
+			, &code
+			, &errorMsg
+			, &constantTable
+			);
+	}
+	else
+	{
+		hr = D3DXCompileShader(_code.c_str()
+			, (uint32_t)_code.size()
+			, NULL
+			, NULL
+			, "main"
+			, profile
+			, flags
+			, &code
+			, &errorMsg
+			, &constantTable
+			);
+	}
+
+	if (FAILED(hr)
+	|| (werror && NULL != errorMsg) )
+	{
+		const char* log = (const char*)errorMsg->GetBufferPointer();
+
+		char source[1024];
+		int32_t line = 0;
+		int32_t column = 0;
+		int32_t start = 0;
+		int32_t end = INT32_MAX;
+
+		if (3 == sscanf(log, "%[^(](%u,%u):", source, &line, &column)
+			&&  0 != line)
+		{
+			start = bx::uint32_imax(1, line-10);
+			end = start + 20;
+		}
+
+		printCode(_code.c_str(), line, start, end);
+		fprintf(stderr, "Error: 0x%08x %s\n", (uint32_t)hr, log);
+		errorMsg->Release();
+		return false;
+	}
+
+	UniformArray uniforms;
+
+	if (NULL != constantTable)
+	{
+		D3DXCONSTANTTABLE_DESC desc;
+		hr = constantTable->GetDesc(&desc);
+		if (FAILED(hr) )
+		{
+			fprintf(stderr, "Error 0x%08x\n", (uint32_t)hr);
+			return false;
+		}
+
+		BX_TRACE("Creator: %s 0x%08x", desc.Creator, (uint32_t /*mingw warning*/)desc.Version);
+		BX_TRACE("Num constants: %d", desc.Constants);
+		BX_TRACE("#   cl ty RxC   S  By Name");
+
+		for (uint32_t ii = 0; ii < desc.Constants; ++ii)
+		{
+			D3DXHANDLE handle = constantTable->GetConstant(NULL, ii);
+			D3DXCONSTANT_DESC constDesc;
+			uint32_t count;
+			constantTable->GetConstantDesc(handle, &constDesc, &count);
+			BX_TRACE("%3d %2d %2d [%dx%d] %d %3d %s[%d] c%d (%d)"
+				, ii
+				, constDesc.Class
+				, constDesc.Type
+				, constDesc.Rows
+				, constDesc.Columns
+				, constDesc.StructMembers
+				, constDesc.Bytes
+				, constDesc.Name
+				, constDesc.Elements
+				, constDesc.RegisterIndex
+				, constDesc.RegisterCount
+				);
+
+			UniformType::Enum type = findUniformTypeDx9(constDesc);
+			if (UniformType::Count != type)
+			{
+				Uniform un;
+				un.name = '$' == constDesc.Name[0] ? constDesc.Name+1 : constDesc.Name;
+				un.type = type;
+				un.num = constDesc.Elements;
+				un.regIndex = constDesc.RegisterIndex;
+				un.regCount = constDesc.RegisterCount;
+				uniforms.push_back(un);
+			}
+		}
+	}
+
+	uint16_t count = (uint16_t)uniforms.size();
+	bx::write(_writer, count);
+
+	uint32_t fragmentBit = profile[0] == 'p' ? BGFX_UNIFORM_FRAGMENTBIT : 0;
+	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
+	{
+		const Uniform& un = *it;
+		uint8_t nameSize = (uint8_t)un.name.size();
+		bx::write(_writer, nameSize);
+		bx::write(_writer, un.name.c_str(), nameSize);
+		uint8_t type = un.type|fragmentBit;
+		bx::write(_writer, type);
+		bx::write(_writer, un.num);
+		bx::write(_writer, un.regIndex);
+		bx::write(_writer, un.regCount);
+
+		BX_TRACE("%s, %s, %d, %d, %d"
+			, un.name.c_str()
+			, getUniformTypeName(un.type)
+			, un.num
+			, un.regIndex
+			, un.regCount
+			);
+	}
+
+	uint16_t shaderSize = (uint16_t)code->GetBufferSize();
+	bx::write(_writer, shaderSize);
+	bx::write(_writer, code->GetBufferPointer(), shaderSize);
+	uint8_t nul = 0;
+	bx::write(_writer, nul);
+
+	if (_cmdLine.hasArg('\0', "disasm") )
+	{
+		LPD3DXBUFFER disasm;
+		D3DXDisassembleShader( (const DWORD*)code->GetBufferPointer()
+			, false
+			, NULL
+			, &disasm
+			);
+
+		if (NULL != disasm)
+		{
+			std::string disasmfp = _cmdLine.findOption('o');
+			disasmfp += ".disasm";
+
+			writeFile(disasmfp.c_str(), disasm->GetBufferPointer(), disasm->GetBufferSize() );
+			disasm->Release();
+		}
+	}
+
+	if (NULL != code)
+	{
+		code->Release();
+	}
+
+	if (NULL != errorMsg)
+	{
+		errorMsg->Release();
+	}
+
+	if (NULL != constantTable)
+	{
+		constantTable->Release();
+	}
+
+	return true;
+}
+
+#else
+
+bool compileHLSLShaderDx9(bx::CommandLine& _cmdLine, const std::string& _code, bx::WriterI* _writer)
+{
+	BX_UNUSED(_cmdLine, _code, _writer);
+	fprintf(stderr, "HLSL compiler is not supported on this platform.\n");
+	return false;
+}
+
+#endif // SHADERC_CONFIG_DIRECT3D9

+ 208 - 0
tools/shaderc/shaderc_glsl.cpp

@@ -0,0 +1,208 @@
+/*
+ * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "shaderc.h"
+#include "glsl_optimizer.h"
+
+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
+		: (ch == 'c' ? kGlslOptShaderCompute : kGlslOptShaderVertex);
+
+	glslopt_target target = kGlslTargetOpenGL;
+	switch (_gles)
+	{
+	case 2:
+		target = kGlslTargetOpenGLES20;
+		break;
+
+	case 3:
+		target = kGlslTargetOpenGLES30;
+		break;
+
+	default:
+		target = kGlslTargetOpenGL;
+		break;
+	}
+
+	glslopt_ctx* ctx = glslopt_initialize(target);
+
+	glslopt_shader* shader = glslopt_optimize(ctx, type, _code.c_str(), 0);
+
+	if (!glslopt_get_status(shader) )
+	{
+		const char* log = glslopt_get_log(shader);
+		int32_t source = 0;
+		int32_t line = 0;
+		int32_t column = 0;
+		int32_t start = 0;
+		int32_t end = INT32_MAX;
+
+		if (3 == sscanf(log, "%u:%u(%u):", &source, &line, &column)
+			&&  0 != line)
+		{
+			start = bx::uint32_imax(1, line-10);
+			end = start + 20;
+		}
+
+		printCode(_code.c_str(), line, start, end);
+		fprintf(stderr, "Error: %s\n", log);
+		glslopt_cleanup(ctx);
+		return false;
+	}
+
+	const char* optimizedShader = glslopt_get_output(shader);
+
+	// Trim all directives.
+	while ('#' == *optimizedShader)
+	{
+		optimizedShader = bx::strnl(optimizedShader);
+	}
+
+	if (0 != _gles)
+	{
+		char* shader = const_cast<char*>(optimizedShader);
+		strreplace(shader, "gl_FragDepthEXT", "gl_FragDepth");
+
+		strreplace(shader, "texture2DLodEXT", "texture2DLod");
+		strreplace(shader, "texture2DProjLodEXT", "texture2DProjLod");
+		strreplace(shader, "textureCubeLodEXT", "textureCubeLod");
+		strreplace(shader, "texture2DGradEXT", "texture2DGrad");
+		strreplace(shader, "texture2DProjGradEXT", "texture2DProjGrad");
+		strreplace(shader, "textureCubeGradEXT", "textureCubeGrad");
+
+		strreplace(shader, "shadow2DEXT", "shadow2D");
+		strreplace(shader, "shadow2DProjEXT", "shadow2DProj");
+	}
+
+	UniformArray uniforms;
+
+	{
+		const char* parse = optimizedShader;
+
+		while (NULL != parse
+			&&  *parse != '\0')
+		{
+			parse = bx::strws(parse);
+			const char* eol = strchr(parse, ';');
+			if (NULL != eol)
+			{
+				const char* qualifier = parse;
+				parse = bx::strws(bx::strword(parse) );
+
+				if (0 == strncmp(qualifier, "attribute", 9)
+					||  0 == strncmp(qualifier, "varying", 7) )
+				{
+					// skip attributes and varyings.
+					parse = eol + 1;
+					continue;
+				}
+
+				if (0 != strncmp(qualifier, "uniform", 7) )
+				{
+					// end if there is no uniform keyword.
+					parse = NULL;
+					continue;
+				}
+
+				const char* precision = NULL;
+				const char* type = parse;
+
+				if (0 == strncmp(type, "lowp", 4)
+					||  0 == strncmp(type, "mediump", 7)
+					||  0 == strncmp(type, "highp", 5) )
+				{
+					precision = type;
+					type = parse = bx::strws(bx::strword(parse) );
+				}
+
+				BX_UNUSED(precision);
+
+				char uniformType[256];
+				parse = bx::strword(parse);
+
+				if (0 == strncmp(type, "sampler", 7) )
+				{
+					strcpy(uniformType, "int");
+				}
+				else
+				{
+					bx::strlcpy(uniformType, type, parse-type+1);
+				}
+
+				const char* name = parse = bx::strws(parse);
+
+				char uniformName[256];
+				uint8_t num = 1;
+				const char* array = bx::strnstr(name, "[", eol-parse);
+				if (NULL != array)
+				{
+					bx::strlcpy(uniformName, name, array-name+1);
+
+					char arraySize[32];
+					const char* end = bx::strnstr(array, "]", eol-array);
+					bx::strlcpy(arraySize, array+1, end-array);
+					num = atoi(arraySize);
+				}
+				else
+				{
+					bx::strlcpy(uniformName, name, eol-name+1);
+				}
+
+				Uniform un;
+				un.type = nameToUniformTypeEnum(uniformType);
+
+				if (UniformType::Count != un.type)
+				{
+					BX_TRACE("name: %s (type %d, num %d)", uniformName, un.type, num);
+
+					un.name = uniformName;
+					un.num = num;
+					un.regIndex = 0;
+					un.regCount = num;
+					uniforms.push_back(un);
+				}
+
+				parse = eol + 1;
+			}
+		}
+	}
+
+	uint16_t count = (uint16_t)uniforms.size();
+	bx::write(_writer, count);
+
+	for (UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it)
+	{
+		const Uniform& un = *it;
+		uint8_t nameSize = (uint8_t)un.name.size();
+		bx::write(_writer, nameSize);
+		bx::write(_writer, un.name.c_str(), nameSize);
+		uint8_t type = un.type;
+		bx::write(_writer, type);
+		bx::write(_writer, un.num);
+		bx::write(_writer, un.regIndex);
+		bx::write(_writer, un.regCount);
+
+		BX_TRACE("%s, %s, %d, %d, %d"
+			, un.name.c_str()
+			, getUniformTypeName(un.type)
+			, un.num
+			, un.regIndex
+			, un.regCount
+			);
+	}
+
+	uint32_t shaderSize = (uint32_t)strlen(optimizedShader);
+	bx::write(_writer, shaderSize);
+	bx::write(_writer, optimizedShader, shaderSize);
+	uint8_t nul = 0;
+	bx::write(_writer, nul);
+
+	glslopt_cleanup(ctx);
+
+	return true;
+}