[Feat] (MG_State/Program): glShaderSource, glUseProgram

This commit is contained in:
2025-08-11 16:47:18 +08:00
parent 7517711308
commit 21a46ee151
7 changed files with 49 additions and 2 deletions
+27 -2
View File
@@ -203,11 +203,36 @@ namespace MobileGL {
}
void ShaderSource_State(GLuint shader, GLsizei count, const GLchar* const* string, const GLint* length) {
THROW_UNIMPL_EXCEPTION;
if (count < 0) {
MG_State::pGLContext->RecordError(
ErrorCode::InvalidValue,
MakeShared<GenericErrorInfo>("MG_Impl/GLImpl", __func__,
"`count` is less than 0."));
}
auto programObject = TryToGetShaderObject(shader);
if (!programObject)
return;
std::string src;
for (GLsizei i = 0; i < count; i++) {
src +=
(length[i] <= 0) ? string[i] : std::string(string[i], length[i]);
}
programObject->SetShaderSource(Move(src));
}
void UseProgram_State(GLuint program) {
THROW_UNIMPL_EXCEPTION;
if (program == 0) {
MG_State::pGLContext->UseProgram(0);
return;
}
auto programObject = TryToGetProgramObject(program);
if (!programObject)
return;
MG_State::pGLContext->UseProgram(program);
}
void Uniform1f_State(GLint location, GLfloat v0) {
+4
View File
@@ -187,6 +187,10 @@ namespace MobileGL {
SharedPtr<ShaderObject> GLContext::GetShaderObject(const Uint index) {
return m_programState.GetShaderObject(index);
}
void GLContext::UseProgram(Uint program) {
return m_programState.UseProgram(program);
}
} // namespace GLState
GLState::GLContext* pGLContext;
+1
View File
@@ -63,6 +63,7 @@ namespace MobileGL {
Bool ValidateShaderName(Uint index) const;
SharedPtr<ProgramObject> GetProgramObject(Uint index);
SharedPtr<ShaderObject> GetShaderObject(Uint index);
void UseProgram(Uint program);
private:
// Error
ErrorState m_errorState;
@@ -33,6 +33,14 @@ namespace MobileGL {
return CheckIndexAvail(program, m_programObjects) && m_programObjects[program] != nullptr;
}
void ProgramState::UseProgram(Uint program) {
if (program == 0)
m_currentProgram.reset();
if (!CheckIndexAvail(program, m_programObjects)) return;
m_currentProgram = m_programObjects[program];
}
Uint ProgramState::CreateShader(ShaderStage stage) {
Uint shaderId = 0;
m_shaderIndexGenerator.Generate(1, &shaderId);
@@ -15,6 +15,8 @@ namespace MobileGL {
void MarkProgramObjectForDeletion(Uint program);
Bool ValidateProgramObject(Uint program) const;
void UseProgram(Uint program);
Uint CreateShader(ShaderStage stage);
SharedPtr<ShaderObject> GetShaderObject(Uint shader);
void MarkShaderObjectForDeletion(Uint shader);
@@ -38,6 +40,8 @@ namespace MobileGL {
IndexGenerator<Uint> m_shaderIndexGenerator;
Vector<SharedPtr<ShaderObject>> m_shaderObjects;
SharedPtr<ProgramObject> m_currentProgram;
};
} // namespace GLState
} // namespace MG_State
@@ -8,6 +8,10 @@ namespace MobileGL {
m_source = source;
}
void ShaderObject::SetShaderSource(std::string &&source) {
m_source = Move(source);
}
void ShaderObject::Compile() {
using namespace MG_Util::ShaderTranspiler;
ShaderAttrib attrib{
@@ -58,6 +58,7 @@ namespace MobileGL {
public:
ShaderObject(const ShaderStage stage) : m_stage(stage) {}
void SetShaderSource(const std::string& source);
void SetShaderSource(std::string&& source);
void Compile();
void MarkAsDeleted();