vulkan renderer draft implementation

This commit is contained in:
Rinthel Kwon
2019-07-16 17:42:25 +09:00
committed by Бранимир Караџић
parent 6c6f43cff0
commit 6927a9179b
25 changed files with 2679 additions and 1606 deletions

View File

@@ -591,7 +591,8 @@ namespace bgfx { namespace spirv
{
const Uniform& un = uniforms[ii];
size += un.regCount*16;
if (un.type != UniformType::Sampler)
size = bx::max(size, (uint16_t)(un.regIndex + un.regCount*16));
uint8_t nameSize = (uint8_t)un.name.size();
bx::write(_writer, nameSize);
@@ -636,6 +637,11 @@ namespace bgfx { namespace spirv
);
shader->setEntryPoint("main");
shader->setAutoMapBindings(true);
uint32_t bindingOffset = (stage == EShLanguage::EShLangFragment ? 48 : 0);
shader->setShiftBinding(glslang::EResUbo, bindingOffset);
shader->setShiftBinding(glslang::EResTexture, bindingOffset + 16);
shader->setShiftBinding(glslang::EResSampler, bindingOffset + 32);
const char* shaderStrings[] = { _code.c_str() };
shader->setStrings(
@@ -705,6 +711,7 @@ namespace bgfx { namespace spirv
{
program->buildReflection();
std::map<std::string, uint32_t> samplerStageMap;
if (_firstPass)
{
// first time through, we just find unused uniforms and get rid of them
@@ -759,6 +766,75 @@ namespace bgfx { namespace spirv
// recompile with the unused uniforms converted to statics
return compile(_options, _version, output.c_str(), _writer, false);
}
else
{
// second time, find sampler state and get its stage index
bx::Error err;
LineReader reader(_code.c_str());
while (err.isOk())
{
char str[4096];
int32_t len = bx::read(&reader, str, BX_COUNTOF(str), &err);
if (err.isOk())
{
std::string strLine(str, len);
size_t index = strLine.find("uniform ");
if (index != std::string::npos)
{
if (!bx::findIdentifierMatch(strLine.c_str(), "SamplerState").isEmpty() ||
!bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty())
{
bx::StringView found = bx::findIdentifierMatch(strLine.c_str(), "register");
const char* ptr = found.getPtr() + found.getLength();
const char* start = NULL;
const char* end = NULL;
while (*ptr != ')' && ptr < strLine.c_str() + strLine.size())
{
if (*ptr >= '0' && *ptr <= '9')
{
if (start == NULL)
start = ptr;
end = ptr;
}
ptr++;
}
BX_CHECK(start != NULL && end != NULL, "SamplerState should have register number");
bx::StringView numberString(start, end - start + 1);
int32_t regNumber = -1;
bx::fromString(&regNumber, numberString);
BX_CHECK(regNumber >= 0, "register number should be semi-positive integer");
found = bx::findIdentifierMatch(strLine.c_str(), "SamplerState");
if (found.isEmpty())
found = bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState");
ptr = found.getPtr() + found.getLength();
start = NULL;
end = NULL;
while (ptr < strLine.c_str() + strLine.size())
{
if (*ptr != ' ')
{
if (start == NULL)
start = ptr;
end = ptr;
}
else if (start != NULL)
{
break;
}
ptr++;
}
BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found");
std::string samplerName(start, end - start + 1);
samplerStageMap[samplerName] = regNumber;
}
}
}
}
}
UniformArray uniforms;
@@ -855,9 +931,24 @@ namespace bgfx { namespace spirv
un.name = uniform_name;
un.type = UniformType::Sampler;
un.num = 0; // needed?
un.regIndex = 0; // needed?
un.regCount = 0; // needed?
uint32_t texture_binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
uint32_t sampler_binding_index = 0;
std::string sampler_name;
for (auto& sampler_resource : resourcesrefl.separate_samplers)
{
sampler_name = refl.get_name(sampler_resource.id);
if (sampler_name.size() > 7 &&
!bx::strFind(sampler_name.c_str(), uniform_name.c_str()).isEmpty() &&
0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "Sampler"))
{
sampler_binding_index = refl.get_decoration(sampler_resource.id, spv::Decoration::DecorationBinding);
break;
}
}
un.num = samplerStageMap[sampler_name]; // want to write stage index
un.regIndex = texture_binding_index; // for sampled image binding index
un.regCount = sampler_binding_index; // for sampler binding index
uniforms.push_back(un);
}