[Feat] (MG_State/Program): Get current program

This commit is contained in:
2025-08-21 22:48:20 +08:00
parent 2617903e71
commit 476de2b8f7
6 changed files with 75 additions and 9 deletions
+34 -2
View File
@@ -383,12 +383,43 @@ namespace MobileGL {
return programObject->GetUniformLocation(name);
}
void GetUniform_State(GLuint program, GLint location, void* params) {
auto programObject = TryToGetProgramObject(program);
if (!programObject)
return;
if (!programObject->GetLinkStatus()) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`program` has not been successfully linked."));
return;
}
if (location >= programObject->GetUniformCount() || programObject->GetUniformName(location).empty()) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidOperation,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`location` does not correspond to a valid uniform variable location for the specified program object."));
return;
}
auto isOpaque = programObject->IsUniformOpaqueAtLocation(location);
if (!isOpaque) {
// TODO: probably handle int/float differences
auto offset = programObject->GetUniformOffset(location);
auto size = programObject->GetUniformSizesInBytes(location);
char* pUBO = (char*)programObject->MapUBO();
memcpy(params, pUBO + offset, size);
}
// TODO: handle 1i variant as texture unit
}
void GetUniformfv_State(GLuint program, GLint location, GLfloat* params) {
THROW_UNIMPL_EXCEPTION;
GetUniform_State(program, location, params);
}
void GetUniformiv_State(GLuint program, GLint location, GLint* params) {
THROW_UNIMPL_EXCEPTION;
GetUniform_State(program, location, params);
}
GLboolean IsProgram_State(GLuint program) {
@@ -447,6 +478,7 @@ namespace MobileGL {
}
void Uniform1fv_State(GLint location, GLsizei count, const GLfloat* value) {
THROW_UNIMPL_EXCEPTION;
}
+4
View File
@@ -191,6 +191,10 @@ namespace MobileGL {
void GLContext::UseProgram(Uint program) {
return m_programState.UseProgram(program);
}
SharedPtr<ProgramObject> GLContext::GetCurrentProgram() {
return m_programState.GetCurrentProgram();
}
} // namespace GLState
GLState::GLContext* pGLContext;
+1 -1
View File
@@ -64,7 +64,7 @@ namespace MobileGL {
SharedPtr<ProgramObject> GetProgramObject(Uint index);
SharedPtr<ShaderObject> GetShaderObject(Uint index);
void UseProgram(Uint program);
SharedPtr<ProgramObject> GetCurrentProgram();
private:
// Error
ErrorState m_errorState;
@@ -81,13 +81,17 @@ namespace MobileGL {
m_uniformNames.resize(m_maxUniformLocation + 1);
m_uniformTypes.resize(m_maxUniformLocation + 1);
m_uniformIsOpaqueType.resize(m_maxUniformLocation + 1);
m_uniformOffsets.resize(m_maxUniformLocation + 1);
m_uniformArraySizes.resize(m_maxUniformLocation + 1);
for (int i = 0; i < uniformCount; i++) {
auto& uniform = m_program->getUniform(i);
auto location = uniform.layoutLocation();
m_uniformNames[location] = uniform.name;
m_uniformTypes[location] = uniform.glDefineType;
m_uniformIsOpaqueType[location] = uniform.getType()->isOpaque();
m_uniformArraySizes[location] = uniform.size;
}
// attributes (pipe in)
@@ -159,7 +163,12 @@ namespace MobileGL {
auto binaryResult = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib);
assert(binaryResult);
m_generatedSpirv = Move(binaryResult.value());
SpvcSession session(m_generatedSpirv[0]);
auto srcResult = ShaderCompiler::DecompileShader(session);
assert(srcResult);
auto src = srcResult.value();
auto& meta = session.GetMetadata();
auto size = meta.uboSize;
m_uboScratch.resize(size);
@@ -167,11 +176,12 @@ namespace MobileGL {
for (const auto& [name, offset] : meta.plainUniformOffsetsInUBO) {
m_uniformOffsets[m_uniformLocations[name]] = offset;
}
auto srcResult = ShaderCompiler::DecompileShader(session);
assert(srcResult);
auto src = srcResult.value();
printf("decompiled src: \n%s", src.c_str());
m_uniformSizesInBytes.resize(meta.plainUniformMemberSizesInBytes.size());
for (const auto& [name, size] : meta.plainUniformMemberSizesInBytes) {
m_uniformSizesInBytes[m_uniformLocations[name]] = size;
}
assert(m_uniformOffsets.size() == GetUniformCount());
assert(m_uniformSizesInBytes.size() == GetUniformCount());
}
void ProgramObject::WaitUntilGenerationCompleted() {
@@ -23,15 +23,25 @@ namespace MobileGL {
Uint GetUniformCount() { return m_uniformNames.size(); }
Int GetUniformLocation(const String& name) {
const auto it = m_uniformLocations.find(name);
return (it == m_uniformLocations.end()) ? -1 : it->second;
return (it == m_uniformLocations.end()) ? -1 : (Int)it->second;
}
GLenum GetUniformType(Uint index) const {
return m_uniformTypes[index];
}
Bool IsUniformOpaqueAtLocation(Uint location) const {
return m_uniformIsOpaqueType[location];
}
const String& GetUniformName(Uint index) const {
return m_uniformNames[index];
}
Uint GetUniformOffset(Uint location) const {
return m_uniformOffsets[location];
}
Uint GetUniformSizesInBytes(Uint location) const {
return m_uniformSizesInBytes[location];
}
Int GetAttributeLocation(const String& name) {
const auto it = std::find(m_attribs.begin(), m_attribs.end(), name);
@@ -43,6 +53,9 @@ namespace MobileGL {
const String& GetAttribName(Uint index) const {
return m_attribs[index];
}
void* MapUBO() {
return m_uboScratch.data();
}
Bool GetDeleteStatus() const { return m_deleteStatus; }
Bool GetLinkStatus() const { return m_linkStatus; }
@@ -80,9 +93,12 @@ namespace MobileGL {
Vector<String> m_uniformNames;
// ditto.
Vector<GLenum> m_uniformTypes;
Vector<Bool> m_uniformIsOpaqueType;
Vector<Int> m_uniformArraySizes;
// Need to be reflected after linking of SPIR-V binary
Vector<Uint> m_uniformOffsets;
Vector<Uint> m_uniformSizesInBytes;
Vector<Uint8> m_uboScratch;
Uint m_maxUniformLocation = 0;
@@ -21,6 +21,10 @@ namespace MobileGL {
SharedPtr<ShaderObject> GetShaderObject(Uint shader);
void MarkShaderObjectForDeletion(Uint shader);
Bool ValidateShaderObject(Uint shader) const;
SharedPtr<ProgramObject> GetCurrentProgram() const {
return m_currentProgram;
}
private:
template <typename T>
static Bool CheckIndexAvail(const SizeT idx, const Vector<T>& vec) {