mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-11 21:58:31 +09:00
[Refactor] (MG_State/Program): refractor uniform reflection using glslang reflection API
This commit is contained in:
@@ -47,13 +47,11 @@ namespace MobileGL {
|
|||||||
} else {
|
} else {
|
||||||
m_linkStatus = false;
|
m_linkStatus = false;
|
||||||
m_infoLog = result.error().log;
|
m_infoLog = result.error().log;
|
||||||
|
|
||||||
const std::string e = std::format("Shader link failed: \nerrc: {}\nmsg: {}\n", result.error().errc,
|
|
||||||
result.error().log);
|
|
||||||
THROW_EXCEPTION(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostLink();
|
// PostLink();
|
||||||
|
|
||||||
|
DoReflection();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgramObject::MarkAsDeleted() {
|
void ProgramObject::MarkAsDeleted() {
|
||||||
@@ -64,69 +62,96 @@ namespace MobileGL {
|
|||||||
return m_shaders;
|
return m_shaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgramObject::PreLink() {
|
void ProgramObject::DoReflection() {
|
||||||
m_uniforms.clear();
|
if (!m_program->buildReflection()) {
|
||||||
m_uniformOffsets.clear();
|
m_linkStatus = false;
|
||||||
|
m_infoLog = "Build reflection failed.";
|
||||||
for (const auto& shader : m_shaders) {
|
return;
|
||||||
for (const auto& [name, loc] : shader->GetUniformLocations()) {
|
|
||||||
// collect all the names to map
|
|
||||||
if (loc != 4095 || m_uniforms.find(name) == m_uniforms.end()) {
|
|
||||||
m_uniforms[name] = loc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set a flag for those who have an explicit location
|
|
||||||
if (loc != 4095) {
|
|
||||||
if (loc >= m_uniformOffsets.size()) {
|
|
||||||
m_uniformOffsets.reserve(std::bit_ceil(loc + 1));
|
|
||||||
m_uniformOffsets.resize(loc + 1, 0);
|
|
||||||
}
|
|
||||||
assert(m_uniformOffsets[loc] == 0);
|
|
||||||
m_uniformOffsets[loc] = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's find a location for those who doesn't have one yet
|
auto uniformCount = m_program->getNumUniformVariables();
|
||||||
Uint nextLocation = 0;
|
for (int i = 0; i < uniformCount; i++) {
|
||||||
|
auto& uniform = m_program->getUniform(i);
|
||||||
// Find first empty location
|
auto location = uniform.layoutLocation();
|
||||||
for (SizeT i = 0; i < m_uniformOffsets.size(); i++) {
|
m_maxUniformLocation = std::max(m_maxUniformLocation, location);
|
||||||
if (m_uniformOffsets[i] == 0) {
|
m_uniformLocations[uniform.name] = location;
|
||||||
nextLocation = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& [name, loc] : m_uniforms) {
|
m_uniformNames.resize(m_maxUniformLocation + 1);
|
||||||
if (loc == 4095) {
|
m_uniformTypes.resize(m_maxUniformLocation + 1);
|
||||||
// check if we drained all the holes already
|
m_uniformOffsets.resize(m_maxUniformLocation + 1);
|
||||||
if (nextLocation >= m_uniformOffsets.size()) {
|
|
||||||
loc = nextLocation;
|
|
||||||
m_uniformOffsets.push_back(1);
|
|
||||||
nextLocation++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign an empty location
|
for (int i = 0; i < uniformCount; i++) {
|
||||||
loc = nextLocation;
|
auto& uniform = m_program->getUniform(i);
|
||||||
m_uniformOffsets[loc] = 1;
|
auto location = uniform.layoutLocation();
|
||||||
|
m_uniformNames[location] = uniform.name;
|
||||||
// Find next empty location
|
m_uniformTypes[location] = uniform.glDefineType;
|
||||||
for (nextLocation++; nextLocation < m_uniformOffsets.size(); nextLocation++) {
|
|
||||||
if (m_uniformOffsets[nextLocation] == 0) break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_uniformNames.resize(m_uniformOffsets.size());
|
|
||||||
for (auto& [name, loc] : m_uniforms) {
|
|
||||||
m_uniformNames[loc] = name;
|
|
||||||
m_uniformNameMaxLength = std::max(m_uniformNameMaxLength, (Int)name.length());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProgramObject::PostLink() {
|
// void ProgramObject::PreLink() {
|
||||||
|
// m_uniforms.clear();
|
||||||
|
// m_uniformOffsets.clear();
|
||||||
|
//
|
||||||
|
// for (const auto& shader : m_shaders) {
|
||||||
|
// for (const auto& [name, loc] : shader->GetUniformLocations()) {
|
||||||
|
// // collect all the names to map
|
||||||
|
// if (loc != 4095 || m_uniforms.find(name) == m_uniforms.end()) {
|
||||||
|
// m_uniforms[name] = loc;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // set a flag for those who have an explicit location
|
||||||
|
// if (loc != 4095) {
|
||||||
|
// if (loc >= m_uniformOffsets.size()) {
|
||||||
|
// m_uniformOffsets.reserve(std::bit_ceil(loc + 1));
|
||||||
|
// m_uniformOffsets.resize(loc + 1, 0);
|
||||||
|
// }
|
||||||
|
// assert(m_uniformOffsets[loc] == 0);
|
||||||
|
// m_uniformOffsets[loc] = 1;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Let's find a location for those who doesn't have one yet
|
||||||
|
// Uint nextLocation = 0;
|
||||||
|
//
|
||||||
|
// // Find first empty location
|
||||||
|
// for (SizeT i = 0; i < m_uniformOffsets.size(); i++) {
|
||||||
|
// if (m_uniformOffsets[i] == 0) {
|
||||||
|
// nextLocation = i;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for (auto& [name, loc] : m_uniforms) {
|
||||||
|
// if (loc == 4095) {
|
||||||
|
// // check if we drained all the holes already
|
||||||
|
// if (nextLocation >= m_uniformOffsets.size()) {
|
||||||
|
// loc = nextLocation;
|
||||||
|
// m_uniformOffsets.push_back(1);
|
||||||
|
// nextLocation++;
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // assign an empty location
|
||||||
|
// loc = nextLocation;
|
||||||
|
// m_uniformOffsets[loc] = 1;
|
||||||
|
//
|
||||||
|
// // Find next empty location
|
||||||
|
// for (nextLocation++; nextLocation < m_uniformOffsets.size(); nextLocation++) {
|
||||||
|
// if (m_uniformOffsets[nextLocation] == 0) break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// m_uniformNames.resize(m_uniformOffsets.size());
|
||||||
|
// for (auto& [name, loc] : m_uniforms) {
|
||||||
|
// m_uniformNames[loc] = name;
|
||||||
|
// m_uniformNameMaxLength = std::max(m_uniformNameMaxLength, (Int)name.length());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// void ProgramObject::PostLink() {
|
||||||
// if (m_programBinary.empty()) {
|
// if (m_programBinary.empty()) {
|
||||||
// assert(false);
|
// assert(false);
|
||||||
// return;
|
// return;
|
||||||
@@ -156,7 +181,7 @@ namespace MobileGL {
|
|||||||
// auto location = m_uniforms[name];
|
// auto location = m_uniforms[name];
|
||||||
// m_uniformTypes[location] = gltype;
|
// m_uniformTypes[location] = gltype;
|
||||||
// }
|
// }
|
||||||
}
|
// }
|
||||||
} // namespace GLState
|
} // namespace GLState
|
||||||
} // namespace MG_State
|
} // namespace MG_State
|
||||||
} // namespace MobileGL
|
} // namespace MobileGL
|
||||||
@@ -19,8 +19,8 @@ namespace MobileGL {
|
|||||||
Int GetUniformMaxLength() const { return m_uniformNameMaxLength; }
|
Int GetUniformMaxLength() const { return m_uniformNameMaxLength; }
|
||||||
Uint GetUniformCount() { return m_uniformOffsets.size(); }
|
Uint GetUniformCount() { return m_uniformOffsets.size(); }
|
||||||
Int GetUniformLocation(const String& name) {
|
Int GetUniformLocation(const String& name) {
|
||||||
const auto it = m_uniforms.find(name);
|
const auto it = m_uniformLocations.find(name);
|
||||||
return (it == m_uniforms.end()) ? -1 : it->second;
|
return (it == m_uniformLocations.end()) ? -1 : it->second;
|
||||||
}
|
}
|
||||||
GLenum GetUniformType(Uint index) const {
|
GLenum GetUniformType(Uint index) const {
|
||||||
return m_uniformTypes[index];
|
return m_uniformTypes[index];
|
||||||
@@ -30,8 +30,9 @@ namespace MobileGL {
|
|||||||
return m_uniformNames[index];
|
return m_uniformNames[index];
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void PreLink();
|
void DoReflection();
|
||||||
void PostLink();
|
// void PreLink();
|
||||||
|
// void PostLink();
|
||||||
|
|
||||||
const Uint m_id = 0;
|
const Uint m_id = 0;
|
||||||
Vector<SharedPtr<ShaderObject>> m_shaders;
|
Vector<SharedPtr<ShaderObject>> m_shaders;
|
||||||
@@ -41,16 +42,16 @@ namespace MobileGL {
|
|||||||
// Uniforms
|
// Uniforms
|
||||||
MG_Util::ShaderTranspiler::SpvcMetadata m_metadata;
|
MG_Util::ShaderTranspiler::SpvcMetadata m_metadata;
|
||||||
|
|
||||||
UnorderedMap<String, Uint> m_uniforms;
|
UnorderedMap<String, Uint> m_uniformLocations;
|
||||||
// 0 or 1 for if the location is explicitly specified at PreLink stage,
|
|
||||||
// offsets into global ubo for PostLink
|
|
||||||
Vector<Uint> m_uniformOffsets;
|
|
||||||
Vector<String> m_uniformNames;
|
Vector<String> m_uniformNames;
|
||||||
Vector<GLenum> m_uniformTypes;
|
Vector<GLenum> m_uniformTypes;
|
||||||
|
|
||||||
|
// Need to be reflected after linking of SPIR-V binary
|
||||||
|
Vector<Uint> m_uniformOffsets;
|
||||||
Vector<Uint8> m_uboScratch;
|
Vector<Uint8> m_uboScratch;
|
||||||
|
|
||||||
Int m_uniformNameMaxLength = 0;
|
Int m_uniformNameMaxLength = 0;
|
||||||
|
Uint m_maxUniformLocation = 0;
|
||||||
|
|
||||||
String m_infoLog;
|
String m_infoLog;
|
||||||
Bool m_deleteStatus = false;
|
Bool m_deleteStatus = false;
|
||||||
|
|||||||
@@ -15,15 +15,19 @@ namespace MobileGL {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ShaderObject::Compile() {
|
void ShaderObject::Compile() {
|
||||||
if (!DoReflection()) {
|
// if (!DoReflection()) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
using namespace MG_Util::ShaderTranspiler;
|
using namespace MG_Util::ShaderTranspiler;
|
||||||
|
|
||||||
|
// Compile for OpenGL here, so that we can do validation and link
|
||||||
|
// like a real OpenGL driver at linking stage
|
||||||
|
// Will compile for other backends later.
|
||||||
ShaderAttrib attrib{
|
ShaderAttrib attrib{
|
||||||
.shaderType = GetGLShaderTypeByMGLShaderStage(m_stage),
|
.shaderType = GetGLShaderTypeByMGLShaderStage(m_stage),
|
||||||
.sourceStr = m_source,
|
.sourceStr = m_source,
|
||||||
.flags = 0
|
.flags = ShaderCompileBits::CompileForOpenGL
|
||||||
};
|
};
|
||||||
|
|
||||||
auto result = ShaderCompiler::CompileShader(attrib);
|
auto result = ShaderCompiler::CompileShader(attrib);
|
||||||
@@ -33,9 +37,6 @@ namespace MobileGL {
|
|||||||
} else {
|
} else {
|
||||||
m_compileStatus = false;
|
m_compileStatus = false;
|
||||||
m_infoLog = result.error().log;
|
m_infoLog = result.error().log;
|
||||||
|
|
||||||
const std::string e = std::format("Shader compilation failed: \nerrc: {}\nmsg: {}\n",
|
|
||||||
result.error().errc, result.error().log);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,35 +44,36 @@ namespace MobileGL {
|
|||||||
m_deleteStatus = true;
|
m_deleteStatus = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ShaderObject::DoReflection() {
|
// bool ShaderObject::DoReflection() {
|
||||||
using namespace MG_Util::ShaderTranspiler;
|
// using namespace MG_Util::ShaderTranspiler;
|
||||||
ShaderAttrib attrib{
|
// ShaderAttrib attrib{
|
||||||
.shaderType = GetGLShaderTypeByMGLShaderStage(m_stage),
|
// .shaderType = GetGLShaderTypeByMGLShaderStage(m_stage),
|
||||||
.sourceStr = m_source,
|
// .sourceStr = m_source,
|
||||||
.flags = ShaderCompileBits::CompileForOpenGL
|
// .flags = ShaderCompileBits::CompileForOpenGL
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
|
// auto result = ShaderCompiler::CompileShader(attrib);
|
||||||
|
// if (!result) {
|
||||||
|
// m_compileStatus = false;
|
||||||
|
// m_infoLog = result.error().log;
|
||||||
|
//
|
||||||
|
// const std::string e = std::format("Shader compilation failed: \nerrc: {}\nmsg: {}\n",
|
||||||
|
// result.error().errc, result.error().log);
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// auto pShader = result.value();
|
||||||
|
// auto root = pShader->getIntermediate()->getTreeRoot();
|
||||||
|
// UniformTraverser traverser;
|
||||||
|
// root->traverse(&traverser);
|
||||||
|
// auto& symbols = traverser.GetCollectedSymbols();
|
||||||
|
// for (const auto& symbol : symbols) {
|
||||||
|
// m_uniforms[symbol->getName().c_str()] = symbol->getQualifier().layoutLocation;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
auto result = ShaderCompiler::CompileShader(attrib);
|
|
||||||
if (!result) {
|
|
||||||
m_compileStatus = false;
|
|
||||||
m_infoLog = result.error().log;
|
|
||||||
|
|
||||||
const std::string e = std::format("Shader compilation failed: \nerrc: {}\nmsg: {}\n",
|
|
||||||
result.error().errc, result.error().log);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto pShader = result.value();
|
|
||||||
auto root = pShader->getIntermediate()->getTreeRoot();
|
|
||||||
UniformTraverser traverser;
|
|
||||||
root->traverse(&traverser);
|
|
||||||
auto& symbols = traverser.GetCollectedSymbols();
|
|
||||||
for (const auto& symbol : symbols) {
|
|
||||||
m_uniforms[symbol->getName().c_str()] = symbol->getQualifier().layoutLocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} // namespace GLState
|
} // namespace GLState
|
||||||
} // namespace MG_State
|
} // namespace MG_State
|
||||||
} // namespace MobileGL
|
} // namespace MobileGL
|
||||||
@@ -69,7 +69,7 @@ namespace MobileGL {
|
|||||||
const String& GetInfoLog() const { return m_infoLog; }
|
const String& GetInfoLog() const { return m_infoLog; }
|
||||||
const UnorderedMap<String, Uint>& GetUniformLocations() const { return m_uniforms; }
|
const UnorderedMap<String, Uint>& GetUniformLocations() const { return m_uniforms; }
|
||||||
private:
|
private:
|
||||||
bool DoReflection();
|
// bool DoReflection();
|
||||||
const Uint m_id = 0;
|
const Uint m_id = 0;
|
||||||
const ShaderStage m_stage;
|
const ShaderStage m_stage;
|
||||||
String m_source;
|
String m_source;
|
||||||
|
|||||||
@@ -113,9 +113,6 @@ TEST_F(ProgramTest, CompileAndLink) {
|
|||||||
LinkProgram(program);
|
LinkProgram(program);
|
||||||
printf("Program linked.\n");
|
printf("Program linked.\n");
|
||||||
|
|
||||||
// FIXME: fix these later, refactoring uniform location reflection stuff
|
|
||||||
FAIL() << "GetUniformLocation not implemented yet!";
|
|
||||||
|
|
||||||
EXPECT_EQ(GetUniformLocation(program, "ProjMat"), 0);
|
EXPECT_EQ(GetUniformLocation(program, "ProjMat"), 0);
|
||||||
EXPECT_EQ(GetUniformLocation(program, "Gray"), 1);
|
EXPECT_EQ(GetUniformLocation(program, "Gray"), 1);
|
||||||
EXPECT_EQ(GetUniformLocation(program, "Saturation"), 6);
|
EXPECT_EQ(GetUniformLocation(program, "Saturation"), 6);
|
||||||
|
|||||||
Reference in New Issue
Block a user