aboutsummaryrefslogtreecommitdiff
path: root/source/Material.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Material.cpp')
-rw-r--r--source/Material.cpp72
1 files changed, 58 insertions, 14 deletions
diff --git a/source/Material.cpp b/source/Material.cpp
index db76b21..5e42b96 100644
--- a/source/Material.cpp
+++ b/source/Material.cpp
@@ -16,18 +16,18 @@
namespace fs = std::filesystem;
using namespace std::literals;
-Material::Material(Shader* shader, std::string name)
- : mShader(shader)
- , mName(std::move(name)) {
+Material::Material() {
+}
+
+Material::Material(std::string name)
+ : mName(std::move(name)) {
}
namespace ProjectBrussel_UNITY_ID {
bool TryFindShaderId(Shader* shader, std::string_view name, int& out) {
- auto info = shader->GetInfo();
- if (!info) return false;
-
- auto iter = info->things.find(name);
- if (iter == info->things.end()) return false;
+ auto& info = shader->GetInfo();
+ auto iter = info.things.find(name);
+ if (iter == info.things.end()) return false;
auto& id = iter->second;
if (id.kind != ShaderThingId::KD_Uniform) return false;
@@ -111,7 +111,7 @@ Material::MatrixUniform ReadMatrixFromjson(const rapidjson::Value& rv) {
assert(row.Size() == w);
for (int x = 0; x < w; ++x) {
auto& val = row[x];
- assert(val.IsFloat());
+ assert(val.IsNumber());
result.value[x * h + y] = val.GetFloat();
}
}
@@ -224,8 +224,48 @@ Shader* Material::GetShader() const {
}
void Material::SetShader(Shader* shader) {
- // TODO validate uniforms?
mShader.Attach(shader);
+ auto& info = shader->GetInfo();
+
+ mBoundScalars.clear();
+ mBoundVectors.clear();
+ mBoundMatrices.clear();
+ mBoundTextures.clear();
+ for (int i = 0; i < info.uniforms.size(); ++i) {
+ auto& decl = info.uniforms[i];
+ switch (decl->kind) {
+ case ShaderVariable::KD_Math: {
+ auto& mathDecl = static_cast<ShaderMathVariable&>(*decl);
+ if (mathDecl.width == 1) {
+ if (mathDecl.height == 1) {
+ // Scalar
+ auto& scalar = mBoundScalars.emplace_back();
+ scalar.location = decl->location;
+ scalar.infoUniformIndex = i;
+ } else {
+ // Vector
+ auto& vec = mBoundVectors.emplace_back();
+ vec.location = decl->location;
+ vec.infoUniformIndex = i;
+ vec.actualLength = mathDecl.height;
+ }
+ } else {
+ // Matrix
+ auto& mat = mBoundMatrices.emplace_back();
+ mat.location = decl->location;
+ mat.infoUniformIndex = i;
+ mat.actualWidth = mathDecl.width;
+ mat.actualHeight = mathDecl.height;
+ }
+ } break;
+
+ case ShaderVariable::KD_Sampler: {
+ auto& uniform = mBoundTextures.emplace_back();
+ uniform.location = decl->location;
+ uniform.infoUniformIndex = i;
+ } break;
+ }
+ }
}
const std::string& Material::GetName() const {
@@ -306,7 +346,7 @@ bool Material::SaveToFile(const fs::path& filePath) const {
if (IsAnnoymous()) return false;
if (!IsValid()) return false;
- auto info = mShader->GetInfo();
+ auto& info = mShader->GetInfo();
rapidjson::Document root(rapidjson::kObjectType);
@@ -316,7 +356,7 @@ bool Material::SaveToFile(const fs::path& filePath) const {
rapidjson::Value fields(rapidjson::kArrayType);
for (auto& scalar : mBoundScalars) {
rapidjson::Value rvField(rapidjson::kObjectType);
- if (info) rvField.AddMember("Name", info->uniforms[scalar.infoUniformIndex]->name, root.GetAllocator());
+ rvField.AddMember("Name", info.uniforms[scalar.infoUniformIndex]->name, root.GetAllocator());
rvField.AddMember("Type", "Scalar", root.GetAllocator());
switch (scalar.actualType) {
case GL_FLOAT: rvField.AddMember("Value", scalar.floatValue, root.GetAllocator()); break;
@@ -327,14 +367,14 @@ bool Material::SaveToFile(const fs::path& filePath) const {
}
for (auto& vector : mBoundVectors) {
rapidjson::Value rvField(rapidjson::kObjectType);
- if (info) rvField.AddMember("Name", info->uniforms[vector.infoUniformIndex]->name, root.GetAllocator());
+ rvField.AddMember("Name", info.uniforms[vector.infoUniformIndex]->name, root.GetAllocator());
rvField.AddMember("Type", "Vector", root.GetAllocator());
rvField.AddMember("Value", MakeVectorJson(vector, root).Move(), root.GetAllocator());
fields.PushBack(rvField, root.GetAllocator());
}
for (auto& matrix : mBoundMatrices) {
rapidjson::Value rvField(rapidjson::kObjectType);
- if (info) rvField.AddMember("Name", info->uniforms[matrix.infoUniformIndex]->name, root.GetAllocator());
+ rvField.AddMember("Name", info.uniforms[matrix.infoUniformIndex]->name, root.GetAllocator());
rvField.AddMember("Type", "Matrix", root.GetAllocator());
rvField.AddMember("Value", MakeMatrixJson(matrix, root).Move(), root.GetAllocator());
fields.PushBack(rvField, root.GetAllocator());
@@ -388,6 +428,7 @@ bool Material::LoadFromFile(const fs::path& filePath) {
mShader.Attach(shader);
}
+ auto& shaderInfo = mShader->GetInfo();
auto fields = rapidjson::GetProperty(root, rapidjson::kArrayType, "Fields"sv);
if (!fields) return false;
@@ -407,6 +448,7 @@ bool Material::LoadFromFile(const fs::path& filePath) {
ScalarUniform uniform;
if (rvName) {
TryFindShaderId(mShader.Get(), rapidjson::AsStringView(*rvName), uniform.infoUniformIndex);
+ uniform.location = shaderInfo.uniforms[uniform.infoUniformIndex]->location;
}
if (rvValue->IsFloat()) {
uniform.actualType = GL_FLOAT;
@@ -423,12 +465,14 @@ bool Material::LoadFromFile(const fs::path& filePath) {
auto uniform = ReadVectorFromJson(*rvValue);
if (rvName) {
TryFindShaderId(mShader.Get(), rapidjson::AsStringView(*rvName), uniform.infoUniformIndex);
+ uniform.location = shaderInfo.uniforms[uniform.infoUniformIndex]->location;
}
mBoundVectors.push_back(std::move(uniform));
} else if (type == "Matrix"sv) {
auto uniform = ReadMatrixFromjson(*rvValue);
if (rvName) {
TryFindShaderId(mShader.Get(), rapidjson::AsStringView(*rvName), uniform.infoUniformIndex);
+ uniform.location = shaderInfo.uniforms[uniform.infoUniformIndex]->location;
}
mBoundMatrices.push_back(uniform);
} else if (type == "Texture"sv) {