mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-09 04:38:30 +09:00
[Improvement] (...): Optimize code.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "ProgramObject.h"
|
||||
#include <MG_Util/ShaderTranspiler/ShaderCompiler.h>
|
||||
#include <MG_Util/Converters/MGToGL/ProgramEnumConverter.h>
|
||||
#include <MG_Util/Converters/SPIRVCrossToGL/SpvcTypeConverter.h>
|
||||
|
||||
namespace MobileGL {
|
||||
@@ -25,17 +26,14 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
void ProgramObject::Link() {
|
||||
// PreLink();
|
||||
|
||||
Vector<GLenum> shaderTypes(m_shaders.size());
|
||||
Vector<SharedPtr<glslang::TShader>> shaders(m_shaders.size());
|
||||
for (SizeT i = 0; i < m_shaders.size(); i++) {
|
||||
shaderTypes[i] = ConvertGLShaderTypeByMGLShaderStage(m_shaders[i]->GetShaderStage());
|
||||
shaderTypes[i] = MG_Util::ConvertShaderStageToGLEnum(m_shaders[i]->GetShaderStage());
|
||||
shaders[i] = m_shaders[i]->GetCompiledShader();
|
||||
}
|
||||
|
||||
MG_Util::ShaderTranspiler::ProgramAttrib attrib{
|
||||
// .shaderTypes = Move(shaderTypes),
|
||||
.shaders = Move(shaders),
|
||||
};
|
||||
|
||||
@@ -48,8 +46,6 @@ namespace MobileGL {
|
||||
m_infoLog = result.error().log;
|
||||
}
|
||||
|
||||
// PostLink();
|
||||
|
||||
DoReflection();
|
||||
GenerateBinary();
|
||||
}
|
||||
@@ -108,11 +104,10 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
SizeT locNeedle = 0;
|
||||
for (auto index: unallocatedUniformIndex) {
|
||||
for (auto index : unallocatedUniformIndex) {
|
||||
auto& uniform = m_program->getUniform(index);
|
||||
for (; locNeedle <= m_maxUniformLocation; locNeedle++) {
|
||||
if (m_uniformIndexInTProgram[locNeedle] != 4095)
|
||||
continue;
|
||||
if (m_uniformIndexInTProgram[locNeedle] != 4095) continue;
|
||||
// Found a vacant location at locNeedle
|
||||
m_uniformIndexInTProgram[locNeedle] = index;
|
||||
m_uniformLocations[uniform.name] = locNeedle;
|
||||
@@ -220,7 +215,7 @@ namespace MobileGL {
|
||||
Vector<SharedPtr<glslang::TShader>> shaders(m_shaders.size());
|
||||
Vector<GLenum> shaderTypes(m_shaders.size());
|
||||
for (SizeT i = 0; i < m_shaders.size(); i++) {
|
||||
auto shaderType = ConvertGLShaderTypeByMGLShaderStage(m_shaders[i]->GetShaderStage());
|
||||
auto shaderType = MG_Util::ConvertShaderStageToGLEnum(m_shaders[i]->GetShaderStage());
|
||||
shaderTypes[i] = shaderType;
|
||||
ShaderAttrib attrib{
|
||||
.shaderType = shaderType, .sourceStr = m_shaders[i]->GetShaderSource(), .flags = 0};
|
||||
@@ -244,7 +239,7 @@ namespace MobileGL {
|
||||
assert(binaryResult);
|
||||
m_generatedSpirv = Move(binaryResult.value());
|
||||
|
||||
for (auto spv: m_generatedSpirv) {
|
||||
for (auto spv : m_generatedSpirv) {
|
||||
SpvcSession session(spv);
|
||||
auto result = session.ParseMetaData();
|
||||
if (result < 0) {
|
||||
@@ -255,7 +250,6 @@ namespace MobileGL {
|
||||
// auto src = srcResult.value();
|
||||
// printf("decompiled src: \n%s\n", src.c_str());
|
||||
|
||||
|
||||
auto& meta = session.GetMetadata();
|
||||
auto size = meta.uboSize;
|
||||
m_uboScratch.resize(size);
|
||||
@@ -280,100 +274,6 @@ namespace MobileGL {
|
||||
void ProgramObject::SetExplicitAttribLocation(Uint index, const char* name) {
|
||||
m_explicitAttribLocations[name] = index;
|
||||
}
|
||||
|
||||
// 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()) {
|
||||
// assert(false);
|
||||
// return;
|
||||
// }
|
||||
// MG_Util::ShaderTranspiler::SpvcSession session(m_programBinary[0]);
|
||||
// const char* src = nullptr; // we don't care the source atm
|
||||
// auto result = session.Compile(&src);
|
||||
// if (result != SPVC_SUCCESS) {
|
||||
// assert(false);
|
||||
// return;
|
||||
// }
|
||||
// m_metadata = session.GetMetadata();
|
||||
// auto& uniformOffsets = m_metadata.plainUniformOffsetsInUBO;
|
||||
// for (const auto& [name, offset] : uniformOffsets) {
|
||||
// assert(m_uniforms.find(name) != m_uniforms.end());
|
||||
// assert(m_uniforms[name] < m_uniformOffsets.size());
|
||||
// m_uniformOffsets[m_uniforms[name]] = offset;
|
||||
// }
|
||||
// m_uboScratch.resize(m_metadata.uboSize, 0);
|
||||
//
|
||||
// auto& types = m_metadata.plainUniformMemberTypes;
|
||||
//
|
||||
// assert(types.size() == m_uniformOffsets.size());
|
||||
// m_uniformTypes.resize(m_uniformOffsets.size());
|
||||
// for (const auto& [name, type] : types) {
|
||||
// auto gltype = MG_Util::ConvertSpvcTypeToGLEnum(type);
|
||||
// auto location = m_uniforms[name];
|
||||
// m_uniformTypes[location] = gltype;
|
||||
// }
|
||||
// }
|
||||
} // namespace GLState
|
||||
} // namespace MG_State
|
||||
} // namespace MobileGL
|
||||
@@ -25,8 +25,7 @@ namespace MobileGL {
|
||||
Uint GetMaxUniformLocation() const { return m_maxUniformLocation; }
|
||||
Int GetUniformLocation(const String& name) {
|
||||
const auto it = m_uniformLocations.find(name);
|
||||
if (it == m_uniformLocations.end())
|
||||
return -1;
|
||||
if (it == m_uniformLocations.end()) return -1;
|
||||
return (Int)it->second;
|
||||
}
|
||||
|
||||
@@ -40,9 +39,7 @@ namespace MobileGL {
|
||||
return uniform.getType();
|
||||
}
|
||||
|
||||
Bool IsUniformOpaqueAtLocation(Uint location) const {
|
||||
return GetUniformTType(location)->isOpaque();
|
||||
}
|
||||
Bool IsUniformOpaqueAtLocation(Uint location) const { return GetUniformTType(location)->isOpaque(); }
|
||||
|
||||
const String& GetUniformName(Uint location) const {
|
||||
auto& uniform = m_program->getUniform(m_uniformIndexInTProgram[location]);
|
||||
@@ -85,9 +82,6 @@ namespace MobileGL {
|
||||
void DoReflection();
|
||||
void GenerateBinary();
|
||||
void WaitUntilGenerationCompleted();
|
||||
// void PreLink();
|
||||
// void PostLink();
|
||||
|
||||
const Uint m_id = 0;
|
||||
Vector<SharedPtr<ShaderObject>> m_shaders;
|
||||
|
||||
@@ -101,8 +95,6 @@ namespace MobileGL {
|
||||
Vector<GLenum> m_attribTypes;
|
||||
|
||||
// Uniforms
|
||||
// MG_Util::ShaderTranspiler::SpvcMetadata m_metadata;
|
||||
|
||||
UnorderedMap<String, Uint> m_uniformLocations;
|
||||
// Ordered by location,
|
||||
// aka. m_uniformIndexInTProgram[loc] == "uniform index of TProgram at location `loc`"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ShaderObject.h"
|
||||
#include <MG_Util/ShaderTranspiler/ShaderCompiler.h>
|
||||
#include <MG_Util/Converters/MGToGL/ProgramEnumConverter.h>
|
||||
#include <MG_Util/ShaderTranspiler/glslang/UniformTraverser.h>
|
||||
|
||||
namespace MobileGL {
|
||||
@@ -14,16 +15,12 @@ namespace MobileGL {
|
||||
}
|
||||
|
||||
void ShaderObject::Compile() {
|
||||
// if (!DoReflection()) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
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{.shaderType = ConvertGLShaderTypeByMGLShaderStage(m_stage),
|
||||
ShaderAttrib attrib{.shaderType = MG_Util::ConvertShaderStageToGLEnum(m_stage),
|
||||
.sourceStr = m_source,
|
||||
.flags = ShaderCompileBits::CompileForOpenGL};
|
||||
|
||||
@@ -40,37 +37,6 @@ namespace MobileGL {
|
||||
void ShaderObject::MarkAsDeleted() {
|
||||
m_deleteStatus = true;
|
||||
}
|
||||
|
||||
// bool ShaderObject::DoReflection() {
|
||||
// using namespace MG_Util::ShaderTranspiler;
|
||||
// ShaderAttrib attrib{
|
||||
// .shaderType = GetGLShaderTypeByMGLShaderStage(m_stage),
|
||||
// .sourceStr = m_source,
|
||||
// .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;
|
||||
// }
|
||||
|
||||
} // namespace GLState
|
||||
} // namespace MG_State
|
||||
} // namespace MobileGL
|
||||
@@ -2,58 +2,18 @@
|
||||
#include <Includes.h>
|
||||
|
||||
namespace MobileGL {
|
||||
enum class ShaderStage {
|
||||
Vertex,
|
||||
TessControl,
|
||||
TessEval,
|
||||
Geometry,
|
||||
Fragment,
|
||||
Compute,
|
||||
Unknown = -1
|
||||
};
|
||||
|
||||
namespace MG_State {
|
||||
namespace GLState {
|
||||
enum class ShaderStage {
|
||||
Vertex,
|
||||
TessControl,
|
||||
TessEval,
|
||||
Geometry,
|
||||
Fragment,
|
||||
Compute,
|
||||
Unknown = -1
|
||||
};
|
||||
|
||||
inline static GLenum ConvertGLShaderTypeByMGLShaderStage(ShaderStage stage) {
|
||||
switch (stage) {
|
||||
case ShaderStage::Vertex:
|
||||
return GL_VERTEX_SHADER;
|
||||
case ShaderStage::TessControl:
|
||||
return GL_TESS_CONTROL_SHADER;
|
||||
case ShaderStage::TessEval:
|
||||
return GL_TESS_EVALUATION_SHADER;
|
||||
case ShaderStage::Geometry:
|
||||
return GL_GEOMETRY_SHADER;
|
||||
case ShaderStage::Fragment:
|
||||
return GL_FRAGMENT_SHADER;
|
||||
case ShaderStage::Compute:
|
||||
return GL_COMPUTE_SHADER;
|
||||
default:
|
||||
assert(false);
|
||||
return GL_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
inline static ShaderStage ConvertMGLShaderStageByGLShaderType(GLenum type) {
|
||||
switch (type) {
|
||||
case GL_VERTEX_SHADER:
|
||||
return ShaderStage::Vertex;
|
||||
case GL_TESS_CONTROL_SHADER:
|
||||
return ShaderStage::TessControl;
|
||||
case GL_TESS_EVALUATION_SHADER:
|
||||
return ShaderStage::TessEval;
|
||||
case GL_GEOMETRY_SHADER:
|
||||
return ShaderStage::Geometry;
|
||||
case GL_FRAGMENT_SHADER:
|
||||
return ShaderStage::Fragment;
|
||||
case GL_COMPUTE_SHADER:
|
||||
return ShaderStage::Compute;
|
||||
default:
|
||||
assert(false);
|
||||
return ShaderStage::Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
class ShaderObject {
|
||||
public:
|
||||
ShaderObject(const ShaderStage stage, const Uint id) : m_stage(stage), m_id(id) {}
|
||||
@@ -72,7 +32,6 @@ namespace MobileGL {
|
||||
Bool GetDeleteStatus() const { return m_deleteStatus; }
|
||||
|
||||
private:
|
||||
// bool DoReflection();
|
||||
const Uint m_id = 0;
|
||||
const ShaderStage m_stage;
|
||||
String m_source;
|
||||
|
||||
Reference in New Issue
Block a user