GLProgramManager
: Use std::unique_ptr
for memory management
This commit is contained in:
parent
63782bb00a
commit
e947de6a7d
@ -37,9 +37,9 @@ namespace spades {
|
||||
device->DeleteProgram(handle);
|
||||
}
|
||||
|
||||
void GLProgram::Attach(spades::draw::GLShader *shader) {
|
||||
void GLProgram::Attach(GLShader &shader) {
|
||||
SPADES_MARK_FUNCTION();
|
||||
device->AttachShader(handle, shader->GetHandle());
|
||||
this->Attach(shader.GetHandle());
|
||||
}
|
||||
|
||||
void GLProgram::Attach(IGLDevice::UInteger shader) {
|
||||
|
@ -36,7 +36,7 @@ namespace spades {
|
||||
GLProgram(IGLDevice *, std::string name = "(unnamed)");
|
||||
~GLProgram();
|
||||
|
||||
void Attach(GLShader *);
|
||||
void Attach(GLShader &);
|
||||
void Attach(IGLDevice::UInteger shader);
|
||||
|
||||
void Link();
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include <Core/FileManager.h>
|
||||
#include <Core/Math.h>
|
||||
#include <Core/Stopwatch.h>
|
||||
#include <Core/TMPUtils.h>
|
||||
|
||||
namespace spades {
|
||||
namespace draw {
|
||||
@ -40,97 +41,90 @@ namespace spades {
|
||||
SPADES_MARK_FUNCTION();
|
||||
}
|
||||
|
||||
GLProgramManager::~GLProgramManager() {
|
||||
SPADES_MARK_FUNCTION();
|
||||
|
||||
for (std::map<std::string, GLProgram *>::iterator it = programs.begin();
|
||||
it != programs.end(); it++) {
|
||||
delete it->second;
|
||||
}
|
||||
for (std::map<std::string, GLShader *>::iterator it = shaders.begin();
|
||||
it != shaders.end(); it++) {
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
GLProgramManager::~GLProgramManager() { SPADES_MARK_FUNCTION(); }
|
||||
|
||||
GLProgram *GLProgramManager::RegisterProgram(const std::string &name) {
|
||||
SPADES_MARK_FUNCTION();
|
||||
|
||||
std::map<std::string, GLProgram *>::iterator it;
|
||||
it = programs.find(name);
|
||||
auto it = programs.find(name);
|
||||
if (it == programs.end()) {
|
||||
GLProgram *prog = CreateProgram(name);
|
||||
programs[name] = prog;
|
||||
return prog;
|
||||
auto program = CreateProgram(name);
|
||||
GLProgram *programPtr = program.get();
|
||||
|
||||
programs[name] = std::move(program);
|
||||
|
||||
return programPtr;
|
||||
} else {
|
||||
return it->second;
|
||||
return it->second.get();
|
||||
}
|
||||
}
|
||||
|
||||
GLShader *GLProgramManager::RegisterShader(const std::string &name) {
|
||||
SPADES_MARK_FUNCTION();
|
||||
|
||||
std::map<std::string, GLShader *>::iterator it;
|
||||
it = shaders.find(name);
|
||||
auto it = shaders.find(name);
|
||||
if (it == shaders.end()) {
|
||||
GLShader *prog = CreateShader(name);
|
||||
shaders[name] = prog;
|
||||
return prog;
|
||||
auto shader = CreateShader(name);
|
||||
GLShader *shaderPtr = shader.get();
|
||||
|
||||
shaders[name] = std::move(shader);
|
||||
|
||||
return shaderPtr;
|
||||
} else {
|
||||
return it->second;
|
||||
return it->second.get();
|
||||
}
|
||||
}
|
||||
|
||||
GLProgram *GLProgramManager::CreateProgram(const std::string &name) {
|
||||
std::unique_ptr<GLProgram> GLProgramManager::CreateProgram(const std::string &name) {
|
||||
SPADES_MARK_FUNCTION();
|
||||
|
||||
SPLog("Loading GLSL program '%s'", name.c_str());
|
||||
std::string text = FileManager::ReadAllBytes(name.c_str());
|
||||
std::vector<std::string> lines = SplitIntoLines(text);
|
||||
|
||||
GLProgram *p = new GLProgram(&device, name);
|
||||
auto p = stmp::make_unique<GLProgram>(&device, name);
|
||||
|
||||
for (size_t i = 0; i < lines.size(); i++) {
|
||||
std::string text = TrimSpaces(lines[i]);
|
||||
if (text.empty())
|
||||
break;
|
||||
break; // TODO: Probably wrong, but unconfirmed
|
||||
|
||||
if (text == "*shadow*") {
|
||||
std::vector<GLShader *> shaders =
|
||||
GLShadowShader::RegisterShader(this, settings, false);
|
||||
for (size_t i = 0; i < shaders.size(); i++)
|
||||
p->Attach(shaders[i]);
|
||||
continue;
|
||||
for (GLShader *shader : shaders) {
|
||||
p->Attach(*shader);
|
||||
}
|
||||
} else if (text == "*shadow-lite*") {
|
||||
std::vector<GLShader *> shaders =
|
||||
GLShadowShader::RegisterShader(this, settings, false, true);
|
||||
for (size_t i = 0; i < shaders.size(); i++)
|
||||
p->Attach(shaders[i]);
|
||||
continue;
|
||||
for (GLShader *shader : shaders) {
|
||||
p->Attach(*shader);
|
||||
}
|
||||
} else if (text == "*shadow-variance*") {
|
||||
std::vector<GLShader *> shaders =
|
||||
GLShadowShader::RegisterShader(this, settings, true);
|
||||
for (size_t i = 0; i < shaders.size(); i++)
|
||||
p->Attach(shaders[i]);
|
||||
continue;
|
||||
for (GLShader *shader : shaders) {
|
||||
p->Attach(*shader);
|
||||
}
|
||||
} else if (text == "*dlight*") {
|
||||
std::vector<GLShader *> shaders = GLDynamicLightShader::RegisterShader(this);
|
||||
for (size_t i = 0; i < shaders.size(); i++)
|
||||
p->Attach(shaders[i]);
|
||||
continue;
|
||||
for (GLShader *shader : shaders) {
|
||||
p->Attach(*shader);
|
||||
}
|
||||
} else if (text == "*shadowmap*") {
|
||||
std::vector<GLShader *> shaders = GLShadowMapShader::RegisterShader(this);
|
||||
for (size_t i = 0; i < shaders.size(); i++)
|
||||
p->Attach(shaders[i]);
|
||||
continue;
|
||||
for (GLShader *shader : shaders) {
|
||||
p->Attach(*shader);
|
||||
}
|
||||
} else if (text[0] == '*') {
|
||||
SPRaise("Unknown special shader: %s", text.c_str());
|
||||
} else if (text[0] == '#') {
|
||||
continue;
|
||||
// Comment line
|
||||
} else {
|
||||
auto shader = CreateShader(text);
|
||||
p->Attach(*shader);
|
||||
}
|
||||
GLShader *s = CreateShader(text);
|
||||
|
||||
p->Attach(s);
|
||||
}
|
||||
|
||||
Stopwatch sw;
|
||||
@ -141,7 +135,7 @@ namespace spades {
|
||||
return p;
|
||||
}
|
||||
|
||||
GLShader *GLProgramManager::CreateShader(const std::string &name) {
|
||||
std::unique_ptr<GLShader> GLProgramManager::CreateShader(const std::string &name) {
|
||||
SPADES_MARK_FUNCTION();
|
||||
|
||||
SPLog("Loading GLSL shader '%s'", name.c_str());
|
||||
@ -157,7 +151,7 @@ namespace spades {
|
||||
else
|
||||
SPRaise("Failed to determine the type of a shader: %s", name.c_str());
|
||||
|
||||
GLShader *s = new GLShader(device, type);
|
||||
auto s = stmp::make_unique<GLShader>(device, type);
|
||||
|
||||
std::string finalSource;
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace spades {
|
||||
@ -34,11 +35,11 @@ namespace spades {
|
||||
GLSettings &settings;
|
||||
IGLShadowMapRenderer *shadowMapRenderer;
|
||||
|
||||
std::map<std::string, GLProgram *> programs;
|
||||
std::map<std::string, GLShader *> shaders;
|
||||
std::map<std::string, std::unique_ptr<GLProgram>> programs;
|
||||
std::map<std::string, std::unique_ptr<GLShader>> shaders;
|
||||
|
||||
GLProgram *CreateProgram(const std::string &name);
|
||||
GLShader *CreateShader(const std::string &name);
|
||||
std::unique_ptr<GLProgram> CreateProgram(const std::string &name);
|
||||
std::unique_ptr<GLShader> CreateShader(const std::string &name);
|
||||
|
||||
public:
|
||||
GLProgramManager(IGLDevice &, IGLShadowMapRenderer *shadowMapRenderer,
|
||||
|
Loading…
x
Reference in New Issue
Block a user