Shader Uniform Boilerplate reduzieren #37

Closed
opened 2026-04-17 04:50:48 +00:00 by sebastian · 0 comments
Owner

Problem: Jeder konkrete Shader hat dasselbe repetitive Muster: pro Uniform ein int location_*-Feld, ein Eintrag in getAllUniformLocations(), und eine loadXxx()-Methode die nur loadFloat/loadMatrix/loadVector aufruft. Bei 6 Shadern mit je 5–8 Uniforms ist das ~150 Zeilen reiner Boilerplate.
Lösung: Uniform-Location-Cache in der Basisklasse:

// ShaderProgram.h:
class ShaderProgram {
protected:
    // NEU: zentrale Location-Map
    int uniform(const std::string& name);

private:
    std::unordered_map<std::string, GLint> uniformLocations;
};

// ShaderProgram.cpp:
int ShaderProgram::uniform(const std::string& name) {
    auto it = uniformLocations.find(name);
    if (it != uniformLocations.end()) return it->second;
    GLint loc = glGetUniformLocation(programID, name.c_str());
    uniformLocations[name] = loc;
    return loc;
}

Konkrete Shader werden deutlich schlanker:

// StaticShader.cpp – vorher:
void StaticShader::getAllUniformLocations() {
    location_transformationMatrix = getUniformLocation("transformationMatrix");
    location_projectionMatrix     = getUniformLocation("projectionMatrix");
    // 6 weitere Zeilen...
}
void StaticShader::loadTransformationMatrix(glm::mat4 m) {
    loadMatrix(location_transformationMatrix, m);
}

// StaticShader.cpp – nachher:

void StaticShader::loadTransformationMatrix(glm::mat4 m) {
    loadMatrix(uniform("transformationMatrix"), m);
}
// getAllUniformLocations() entfällt komplett

Acceptance Criteria:

  • Alle location_* Felder in allen Shadern entfernt
  • getAllUniformLocations() als abstrakte Methode entfernt
  • Funktionales Verhalten identisch (Location wird beim ersten Aufruf gecacht)
  • Kein Performance-Verlust: unordered_map-Lookup ist O(1)
Problem: Jeder konkrete Shader hat dasselbe repetitive Muster: pro Uniform ein int location_*-Feld, ein Eintrag in getAllUniformLocations(), und eine loadXxx()-Methode die nur loadFloat/loadMatrix/loadVector aufruft. Bei 6 Shadern mit je 5–8 Uniforms ist das ~150 Zeilen reiner Boilerplate. Lösung: Uniform-Location-Cache in der Basisklasse: ``` // ShaderProgram.h: class ShaderProgram { protected: // NEU: zentrale Location-Map int uniform(const std::string& name); private: std::unordered_map<std::string, GLint> uniformLocations; }; // ShaderProgram.cpp: int ShaderProgram::uniform(const std::string& name) { auto it = uniformLocations.find(name); if (it != uniformLocations.end()) return it->second; GLint loc = glGetUniformLocation(programID, name.c_str()); uniformLocations[name] = loc; return loc; } ``` Konkrete Shader werden deutlich schlanker: ``` // StaticShader.cpp – vorher: void StaticShader::getAllUniformLocations() { location_transformationMatrix = getUniformLocation("transformationMatrix"); location_projectionMatrix = getUniformLocation("projectionMatrix"); // 6 weitere Zeilen... } void StaticShader::loadTransformationMatrix(glm::mat4 m) { loadMatrix(location_transformationMatrix, m); } ``` // StaticShader.cpp – nachher: ``` void StaticShader::loadTransformationMatrix(glm::mat4 m) { loadMatrix(uniform("transformationMatrix"), m); } // getAllUniformLocations() entfällt komplett ``` Acceptance Criteria: - Alle location_* Felder in allen Shadern entfernt - getAllUniformLocations() als abstrakte Methode entfernt - Funktionales Verhalten identisch (Location wird beim ersten Aufruf gecacht) - Kein Performance-Verlust: unordered_map-Lookup ist O(1)
sebastian added the
refactoring
engine
labels 2026-04-17 04:50:48 +00:00
sebastian added this to the RenderSystem Refactoring milestone 2026-04-17 04:50:52 +00:00
sebastian added this to the Dicewars project 2026-04-17 04:50:53 +00:00
Sign in to join this conversation.
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sebastian/Dicewars-Siedler#37
No description provided.