Reemoved use of old LineReader.

This commit is contained in:
Бранимир Караџић 2020-06-20 15:14:54 -07:00
parent d5d3d29e9c
commit a95ddd1c0c
6 changed files with 190 additions and 213 deletions

View File

@ -6452,11 +6452,8 @@ namespace bgfx { namespace gl
for (int32_t line = 1; !lineReader.isDone(); ++line)
{
bx::StringView str = lineReader.next();
if (!lineReader.isDone() )
{
BX_TRACE("%3d %.*s", line, str.getLength(), str.getPtr() );
}
BX_TRACE("%3d %.*s", line, str.getLength(), str.getPtr() );
BX_UNUSED(str);
}
GLsizei len;

View File

@ -529,22 +529,17 @@ namespace bgfx
{
bx::printf("Code:\n---\n");
bx::Error err;
LineReader reader(_code);
for (int32_t line = 1; err.isOk() && line < _end; ++line)
bx::LineReader reader(_code);
for (int32_t line = 1; !reader.isDone() && line < _end; ++line)
{
char str[4096];
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
bx::StringView strLine = reader.next();
if (err.isOk()
&& line >= _start)
if (line >= _start)
{
bx::StringView strLine(str, len);
if (_line == line)
{
bx::printf("\n");
bx::printf(">>> %3d: %.*s", line, strLine.getLength(), strLine.getPtr() );
bx::printf(">>> %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
if (-1 != _column)
{
bx::printf(">>> %3d: %*s\n", _column, _column, "^");
@ -553,7 +548,7 @@ namespace bgfx
}
else
{
bx::printf(" %3d: %.*s", line, strLine.getLength(), strLine.getPtr() );
bx::printf(" %3d: %.*s\n", line, strLine.getLength(), strLine.getPtr() );
}
}
}
@ -1950,7 +1945,7 @@ namespace bgfx
!bx::strFind(preprocessedInput, "floatBitsToInt").isEmpty() ||
!bx::strFind(preprocessedInput, "intBitsToFloat").isEmpty() ||
!bx::strFind(preprocessedInput, "uintBitsToFloat").isEmpty()
) )
) )
)
{
glsl = 430;

View File

@ -11,33 +11,33 @@ namespace bgfx
extern bool g_verbose;
}
#define _BX_TRACE(_format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (bgfx::g_verbose) \
{ \
#define _BX_TRACE(_format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (bgfx::g_verbose) \
{ \
fprintf(stdout, BX_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__); \
} \
} \
BX_MACRO_BLOCK_END
#define _BX_WARN(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!(_condition) ) \
{ \
#define _BX_WARN(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!(_condition) ) \
{ \
BX_TRACE("WARN " _format, ##__VA_ARGS__); \
} \
} \
BX_MACRO_BLOCK_END
#define _BX_ASSERT(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!(_condition) ) \
{ \
#define _BX_ASSERT(_condition, _format, ...) \
BX_MACRO_BLOCK_BEGIN \
if (!(_condition) ) \
{ \
BX_TRACE("CHECK " _format, ##__VA_ARGS__); \
bx::debugBreak(); \
} \
bx::debugBreak(); \
} \
BX_MACRO_BLOCK_END
#define BX_TRACE _BX_TRACE
#define BX_WARN _BX_WARN
#define BX_TRACE _BX_TRACE
#define BX_WARN _BX_WARN
#define BX_ASSERT _BX_ASSERT
#ifndef SHADERC_CONFIG_HLSL
@ -66,45 +66,6 @@ namespace bgfx
{
extern bool g_verbose;
class LineReader : public bx::ReaderI
{
public:
LineReader(const char* _str)
: m_str(_str)
, m_pos(0)
, m_size(bx::strLen(_str) )
{
}
virtual int32_t read(void* _data, int32_t _size, bx::Error* _err) override
{
if (m_str[m_pos] == '\0'
|| m_pos == m_size)
{
BX_ERROR_SET(_err, BX_ERROR_READERWRITER_EOF, "LineReader: EOF.");
return 0;
}
uint32_t pos = m_pos;
const char* str = &m_str[pos];
const char* nl = bx::strFindNl(bx::StringView(str, str + (m_size - pos))).getPtr();
pos += (uint32_t)(nl - str);
const char* eol = &m_str[pos];
uint32_t size = bx::uint32_min(uint32_t(eol - str), _size);
bx::memCopy(_data, str, size);
m_pos += size;
return size;
}
const char* m_str;
uint32_t m_pos;
uint32_t m_size;
};
bx::StringView nextWord(bx::StringView& _parse);
constexpr uint8_t kUniformFragmentBit = 0x10;

View File

@ -680,38 +680,42 @@ namespace bgfx { namespace hlsl
// first time through, we just find unused uniforms and get rid of them
std::string output;
bx::Error err;
LineReader reader(_code.c_str() );
while (err.isOk() )
bx::LineReader reader(_code.c_str() );
while (!reader.isDone() )
{
char str[4096];
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
if (err.isOk() )
bx::StringView strLine = reader.next();
bool found = false;
for (UniformNameList::iterator it = unusedUniforms.begin(), itEnd = unusedUniforms.end(); it != itEnd; ++it)
{
std::string strLine(str, len);
for (UniformNameList::iterator it = unusedUniforms.begin(), itEnd = unusedUniforms.end(); it != itEnd; ++it)
bx::StringView str = strFind(strLine, "uniform ");
if (str.isEmpty() )
{
size_t index = strLine.find("uniform ");
if (index == std::string::npos)
{
continue;
}
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine.c_str(), it->c_str() ).isEmpty() )
{
strLine = strLine.replace(index, strLength, "static");
unusedUniforms.erase(it);
break;
}
continue;
}
output += strLine;
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine, it->c_str() ).isEmpty() )
{
output.append(strLine.getPtr(), str.getPtr() );
output += "static ";
output.append(str.getTerm(), strLine.getTerm() );
output += "\n";
found = true;
unusedUniforms.erase(it);
break;
}
}
if (!found)
{
output.append(strLine.getPtr(), strLine.getTerm() );
output += "\n";
}
}

View File

@ -8,6 +8,9 @@
BX_PRAGMA_DIAGNOSTIC_PUSH()
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: 'inclusionDepth' : unreferenced formal parameter
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4265) // error C4265: 'spv::spirvbin_t': class has virtual functions, but destructor is not virtual
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wattributes") // warning: attribute ignored
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wdeprecated-declarations") // warning: MSLVertexAttr is deprecated
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits") // warning: comparison of unsigned expression in < 0 is always false
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow") // warning: declaration of 'userData' shadows a member of 'glslang::TShader::Includer::IncludeResult'
#define ENABLE_OPT 1
#include <ShaderLang.h>
@ -585,7 +588,7 @@ namespace bgfx { namespace metal
{
uint16_t size = 0;
uint16_t count = static_cast<uint16_t>(uniforms.size());
uint16_t count = static_cast<uint16_t>(uniforms.size() );
bx::write(_writer, count);
uint32_t fragmentBit = isFragmentShader ? kUniformFragmentBit : 0;
@ -598,7 +601,7 @@ namespace bgfx { namespace metal
uint8_t nameSize = (uint8_t)un.name.size();
bx::write(_writer, nameSize);
bx::write(_writer, un.name.c_str(), nameSize);
bx::write(_writer, uint8_t(un.type | fragmentBit));
bx::write(_writer, uint8_t(un.type | fragmentBit) );
bx::write(_writer, un.num);
bx::write(_writer, un.regIndex);
bx::write(_writer, un.regCount);
@ -719,53 +722,59 @@ namespace bgfx { namespace metal
// first time through, we just find unused uniforms and get rid of them
std::string output;
bx::Error err;
LineReader reader(_code.c_str() );
while (err.isOk() )
bx::LineReader reader(_code.c_str() );
while (!reader.isDone() )
{
char str[4096];
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
if (err.isOk() )
bx::StringView strLine = reader.next();
bx::StringView str = strFind(strLine, "uniform ");
if (!str.isEmpty() )
{
std::string strLine(str, len);
bool found = false;
size_t index = strLine.find("uniform ");
if (index != std::string::npos)
for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
{
bool found = false;
for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
if (!bx::findIdentifierMatch(strLine, s_samplerTypes[ii]).isEmpty() )
{
if (!bx::findIdentifierMatch(strLine.c_str(), s_samplerTypes[ii]).isEmpty())
found = true;
break;
}
}
if (!found)
{
for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
{
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine, program->getUniformName(ii) ).isEmpty() )
{
found = true;
break;
}
}
if (!found)
{
for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
{
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine.c_str(), program->getUniformName(ii)).isEmpty())
{
found = true;
break;
}
}
}
if (!found)
{
strLine = strLine.replace(index, 7 /* uniform */, "static");
}
}
output += strLine;
if (!found)
{
output.append(strLine.getPtr(), str.getPtr() );
output += "static ";
output.append(str.getTerm(), strLine.getTerm() );
output += "\n";
}
else
{
output.append(strLine.getPtr(), strLine.getTerm() );
output += "\n";
}
}
else
{
output.append(strLine.getPtr(), strLine.getTerm() );
output += "\n";
}
}
@ -788,7 +797,7 @@ namespace bgfx { namespace metal
un.regIndex = uint16_t(offset);
un.regCount = un.num;
switch (program->getUniformType(ii))
switch (program->getUniformType(ii) )
{
case 0x1404: // GL_INT:
un.type = UniformType::Sampler;
@ -887,14 +896,14 @@ namespace bgfx { namespace metal
}
uint16_t size = writeUniformArray( _writer, uniforms, _options.shaderType == 'f');
if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0))
if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) )
{
if (g_verbose)
{
glslang::SpirvToolsDisassemble(std::cout, spirv);
}
spirv_cross::CompilerMSL msl(std::move(spirv));
spirv_cross::CompilerMSL msl(std::move(spirv) );
auto executionModel = msl.get_execution_model();
spirv_cross::MSLResourceBinding newBinding;
@ -903,7 +912,7 @@ namespace bgfx { namespace metal
spirv_cross::ShaderResources resources = msl.get_shader_resources();
spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
if (!entryPoints.empty())
if (!entryPoints.empty() )
msl.rename_entry_point(entryPoints[0].name, "xlatMtlMain", entryPoints[0].execution_model);
for (auto &resource : resources.uniform_buffers)
@ -943,7 +952,9 @@ namespace bgfx { namespace metal
{
std::string name = msl.get_name(resource.id);
if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
msl.set_name(resource.id, name.substr(0, name.length() - 7));
{
msl.set_name(resource.id, name.substr(0, name.length() - 7) );
}
unsigned set = msl.get_decoration( resource.id, spv::DecorationDescriptorSet );
unsigned binding = msl.get_decoration( resource.id, spv::DecorationBinding );
@ -958,7 +969,9 @@ namespace bgfx { namespace metal
{
std::string name = msl.get_name(resource.id);
if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
msl.set_name(resource.id, name.substr(0, name.length() - 7));
{
msl.set_name(resource.id, name.substr(0, name.length() - 7) );
}
unsigned set = msl.get_decoration( resource.id, spv::DecorationDescriptorSet );
unsigned binding = msl.get_decoration( resource.id, spv::DecorationBinding );

View File

@ -8,6 +8,9 @@
BX_PRAGMA_DIAGNOSTIC_PUSH()
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: 'inclusionDepth' : unreferenced formal parameter
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4265) // error C4265: 'spv::spirvbin_t': class has virtual functions, but destructor is not virtual
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wattributes") // warning: attribute ignored
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wdeprecated-declarations") // warning: MSLVertexAttr is deprecated
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits") // warning: comparison of unsigned expression in < 0 is always false
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow") // warning: declaration of 'userData' shadows a member of 'glslang::TShader::Includer::IncludeResult'
#define ENABLE_OPT 1
#include <ShaderLang.h>
@ -638,21 +641,24 @@ namespace bgfx { namespace spirv
{
uint16_t size = 0;
uint16_t count = static_cast<uint16_t>(uniforms.size());
uint16_t count = static_cast<uint16_t>(uniforms.size() );
bx::write(_writer, count);
uint32_t fragmentBit = isFragmentShader ? kUniformFragmentBit : 0;
for (uint16_t ii = 0; ii < count; ++ii)
{
const Uniform& un = uniforms[ii];
if ((un.type & ~kUniformMask) > UniformType::End)
size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16));
if ( (un.type & ~kUniformMask) > UniformType::End)
{
size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16) );
}
uint8_t nameSize = (uint8_t)un.name.size();
bx::write(_writer, nameSize);
bx::write(_writer, un.name.c_str(), nameSize);
bx::write(_writer, uint8_t(un.type | fragmentBit));
bx::write(_writer, uint8_t(un.type | fragmentBit) );
bx::write(_writer, un.num);
bx::write(_writer, un.regIndex);
bx::write(_writer, un.regCount);
@ -665,7 +671,7 @@ namespace bgfx { namespace spirv
, un.num
, un.regIndex
, un.regCount
);
);
}
return size;
}
@ -782,88 +788,89 @@ namespace bgfx { namespace spirv
};
std::vector<Uniform> uniforms;
bx::Error err;
LineReader reader(_code.c_str() );
while (err.isOk() )
bx::LineReader reader(_code.c_str() );
while (!reader.isDone() )
{
char str[4096];
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
if (err.isOk() )
bx::StringView strLine = reader.next();
bool moved = false;
bx::StringView str = strFind(strLine, "uniform ");
if (!str.isEmpty() )
{
std::string strLine(str, len);
bool found = false;
bool sampler = false;
std::string name = "";
bool moved = false;
// add to samplers
size_t index = strLine.find("uniform ");
if (index != std::string::npos)
for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
{
bool found = false;
bool sampler = false;
std::string name = "";
// add to samplers
for (uint32_t ii = 0; ii < BX_COUNTOF(s_samplerTypes); ++ii)
if (!bx::findIdentifierMatch(strLine, s_samplerTypes[ii]).isEmpty() )
{
if (!bx::findIdentifierMatch(strLine.c_str(), s_samplerTypes[ii]).isEmpty())
found = true;
sampler = true;
break;
}
}
if (!found)
{
for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
{
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine, program->getUniformName(ii) ).isEmpty() )
{
found = true;
sampler = true;
name = program->getUniformName(ii);
break;
}
}
if (!found)
{
for (int32_t ii = 0, num = program->getNumLiveUniformVariables(); ii < num; ++ii)
{
// matching lines like: uniform u_name;
// we want to replace "uniform" with "static" so that it's no longer
// included in the uniform blob that the application must upload
// we can't just remove them, because unused functions might still reference
// them and cause a compile error when they're gone
if (!bx::findIdentifierMatch(strLine.c_str(), program->getUniformName(ii)).isEmpty())
{
found = true;
name = program->getUniformName(ii);
break;
}
}
}
if (!found)
{
strLine.replace(index, 7 /* uniform */, "static");
}
else if (!sampler)
{
Uniform uniform;
uniform.name = name;
uniform.decl = strLine;
uniforms.push_back(uniform);
moved = true;
}
}
if (!moved)
output += strLine;
if (!found)
{
output.append(strLine.getPtr(), str.getPtr() );
output += "static ";
output.append(str.getTerm(), strLine.getTerm() );
output += "\n";
moved = true;
}
else if (!sampler)
{
Uniform uniform;
uniform.name = name;
uniform.decl = std::string(strLine.getPtr(), strLine.getTerm() );
uniforms.push_back(uniform);
moved = true;
}
}
if (!moved)
{
output.append(strLine.getPtr(), strLine.getTerm() );
output += "\n";
}
}
std::string uniformBlock;
uniformBlock += "cbuffer UniformBlock\n";
uniformBlock += "{\n";
for (const Uniform& uniform : uniforms)
{
uniformBlock += uniform.decl.substr(7 /* uniform */);
uniformBlock += "\n";
}
uniformBlock += "};\n";
output = uniformBlock + output;
//std::cout << "[debug] uniforms: " << std::endl << uniformBlock << std::endl;
// recompile with the unused uniforms converted to statics
return compile(_options, _version, output.c_str(), _writer, false);
}
@ -887,7 +894,7 @@ namespace bgfx { namespace spirv
un.regIndex = uint16_t(offset);
un.regCount = un.num;
switch (program->getUniformType(ii))
switch (program->getUniformType(ii) )
{
case 0x1404: // GL_INT:
un.type = UniformType::Sampler;
@ -991,7 +998,7 @@ namespace bgfx { namespace spirv
bool isCompareSampler = false;
for (auto& sampler : resourcesrefl.separate_samplers)
{
if (binding_index + 16 == refl.get_decoration(sampler.id, spv::Decoration::DecorationBinding))
if (binding_index + 16 == refl.get_decoration(sampler.id, spv::Decoration::DecorationBinding) )
{
std::string samplerName = refl.get_name(sampler.id);
isCompareSampler = refl.variable_is_depth_or_compare(sampler.id) || samplerName.find("Comparison") != std::string::npos;
@ -1006,8 +1013,8 @@ namespace bgfx { namespace spirv
| (isCompareSampler ? kUniformCompareBit : 0)
);
un.texComponent = uint8_t(SpirvCrossBaseTypeToFormatType(componentType));
un.texDimension = uint8_t(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed));
un.texComponent = uint8_t(SpirvCrossBaseTypeToFormatType(componentType) );
un.texDimension = uint8_t(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
un.regIndex = binding_index;
un.regCount = 0; // unused
@ -1039,8 +1046,8 @@ namespace bgfx { namespace spirv
un.name = uniform_name;
un.type = type;
un.texComponent = uint8_t(SpirvCrossBaseTypeToFormatType(componentType));
un.texDimension = uint8_t(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed));
un.texComponent = uint8_t(SpirvCrossBaseTypeToFormatType(componentType) );
un.texDimension = uint8_t(SpirvDimToTextureViewDimension(imageType.dim, imageType.arrayed) );
un.regIndex = binding_index;
un.regCount = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; // for descriptor type
@ -1056,7 +1063,7 @@ namespace bgfx { namespace spirv
for (auto& uniform : uniforms)
{
if (!bx::strFind(uniform.name.c_str(), name.c_str()).isEmpty())
if (!bx::strFind(uniform.name.c_str(), name.c_str() ).isEmpty() )
{
spirv_cross::Bitset flags = refl.get_buffer_block_flags(resource.id);
UniformType::Enum type = flags.get(spv::DecorationNonWritable)
@ -1077,7 +1084,7 @@ namespace bgfx { namespace spirv
if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) )
{
spirv_cross::CompilerMSL msl(std::move(spirv));
spirv_cross::CompilerMSL msl(std::move(spirv) );
spirv_cross::ShaderResources resources = msl.get_shader_resources();
@ -1108,7 +1115,7 @@ namespace bgfx { namespace spirv
if (name.size() > 7
&& 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
{
msl.set_name(resource.id, name.substr(0, name.length() - 7));
msl.set_name(resource.id, name.substr(0, name.length() - 7) );
}
}