2020-04-13 11:24:53 +00:00
|
|
|
#include "shader.h"
|
|
|
|
|
|
|
|
#include "../shader_source.h"
|
|
|
|
#include "renderer.h"
|
|
|
|
|
|
|
|
using namespace base;
|
|
|
|
|
|
|
|
namespace eng {
|
|
|
|
|
|
|
|
Shader::Shader(unsigned resource_id,
|
|
|
|
std::shared_ptr<void> impl_data,
|
|
|
|
Renderer* renderer)
|
|
|
|
: RenderResource(resource_id, impl_data, renderer) {}
|
|
|
|
|
|
|
|
Shader::~Shader() {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::Create(std::unique_ptr<ShaderSource> source,
|
2020-09-17 22:03:21 +00:00
|
|
|
const VertexDescripton& vd,
|
|
|
|
Primitive primitive) {
|
2020-04-13 11:24:53 +00:00
|
|
|
Destroy();
|
|
|
|
valid_ = true;
|
2020-09-17 22:03:21 +00:00
|
|
|
renderer_->CreateShader(impl_data_, std::move(source), vd, primitive);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::Destroy() {
|
|
|
|
if (valid_) {
|
2020-09-17 22:03:21 +00:00
|
|
|
renderer_->DestroyShader(impl_data_);
|
2020-04-13 11:24:53 +00:00
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::Activate() {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->ActivateShader(impl_data_);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, const Vector2& v) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, v);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, const Vector3& v) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, v);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, const Vector4& v) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, v);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, const Matrix4x4& m) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, m);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, float f) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, f);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetUniform(const std::string& name, int i) {
|
2020-09-17 22:03:21 +00:00
|
|
|
if (valid_)
|
|
|
|
renderer_->SetUniform(impl_data_, name, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::UploadUniforms() {
|
|
|
|
if (valid_)
|
|
|
|
renderer_->UploadUniforms(impl_data_);
|
2020-04-13 11:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace eng
|