diff options
author | rtk0c <[email protected]> | 2022-04-15 20:30:39 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-15 20:30:39 -0700 |
commit | afcac59c7d04f4337d6b04ebed8cac7e871ccc50 (patch) | |
tree | 8c32b90b4a0ab762a68f228dc8cc4e7f52fc5bd7 /source/Shader.cpp | |
parent | f2a1481123ac23aeb4937df5f61c57e0e4f1ff52 (diff) |
Changeset: 7 Work on Material system
Diffstat (limited to 'source/Shader.cpp')
-rw-r--r-- | source/Shader.cpp | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/source/Shader.cpp b/source/Shader.cpp index 3ff0d10..4a576d0 100644 --- a/source/Shader.cpp +++ b/source/Shader.cpp @@ -10,6 +10,7 @@ #include <rapidjson/filereadstream.h> #include <rapidjson/filewritestream.h> #include <rapidjson/writer.h> +#include <cassert> #include <cstddef> #include <cstdlib> #include <utility> @@ -17,6 +18,20 @@ namespace fs = std::filesystem; using namespace std::literals; +bool ShaderThingId::IsValid() const { + return kind == KD_Invalid; +} + +ShaderVariable* ShaderInfo::FindVariable(const ShaderThingId& thing) { + switch (thing.kind) { + case ShaderThingId::KD_Input: return &inputs[thing.index].variable; + case ShaderThingId::KD_Output: return &outputs[thing.index].variable; + case ShaderThingId::KD_Uniform: return uniforms[thing.index].get(); + case ShaderThingId::KD_Invalid: break; + } + return nullptr; +} + bool ShaderInfo::SaveToFile(const fs::path& filePath) const { rapidjson::Document root(rapidjson::kObjectType); @@ -103,6 +118,16 @@ bool ShaderInfo::LoadFromFile(const fs::path& filePath) { return true; } +void ShaderInfo::LoadLocations(const Shader& ownerShader) { + GLuint program = ownerShader.GetProgram(); + + // TODO inputs + // TODO outputs + for (auto& uniform : uniforms) { + uniform->location = glGetUniformLocation(ownerShader.GetProgram(), uniform->name.c_str()); + } +} + Shader::Shader(std::string name) : mName{ name } { } @@ -337,6 +362,14 @@ Shader::ErrorCode Shader::InitFromSource(std::string_view source) { #undef CATCH_ERROR +void Shader::GetDesignatedMetadataPath(char* buffer, int bufferSize) { + snprintf(buffer, bufferSize, "%s/Shaders/%s.json", AppConfig::assetDir.c_str(), mName.c_str()); +} + +fs::path Shader::GetDesignatedMetadataPath() { + return AppConfig::assetDirPath / "Shaders" / fs::path(mName).replace_extension(".json"); +} + namespace ProjectBrussel_UNITY_ID { bool QueryMathInfo(GLenum type, GLenum& scalarType, int& width, int& height) { auto DoOutput = [&](GLenum scalarTypeIn, int widthIn, int heightIn) { @@ -476,7 +509,6 @@ bool Shader::CreateEmptyInfoIfAbsent() { bool Shader::GatherInfoIfAbsent() { using namespace ProjectBrussel_UNITY_ID; - using ThingId = ShaderInfo::ThingId; if (mInfo || !IsValid()) { return false; @@ -487,7 +519,8 @@ bool Shader::GatherInfoIfAbsent() { // TODO handle differnt types of variables with the same name // TODO work with OpenGL < 4.3, possibly with glslang -#if 0 + return true; + int inputCount; glGetProgramInterfaceiv(mHandle, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &inputCount); int outputCount; @@ -498,7 +531,7 @@ bool Shader::GatherInfoIfAbsent() { glGetProgramInterfaceiv(mHandle, GL_UNIFORM, GL_ACTIVE_RESOURCES, &uniformCount); // Gather inputs - auto GatherMathVars = [&](int count, GLenum resourceType, ShaderInfo::ThingKind resourceKind, std::vector<ShaderInfo::InputOutputThing>& list) { + auto GatherMathVars = [&](int count, GLenum resourceType, ShaderThingId::Kind resourceKind, std::vector<ShaderInfo::InputOutputThing>& list) { for (int i = 0; i < count; ++i) { const GLenum query[] = { GL_NAME_LENGTH, GL_TYPE, GL_LOCATION, GL_TOP_LEVEL_ARRAY_SIZE }; GLint props[std::size(query)]; @@ -511,7 +544,7 @@ bool Shader::GatherInfoIfAbsent() { std::string fieldName(nameLength - 1, '\0'); glGetProgramResourceName(mHandle, GL_UNIFORM, i, nameLength, nullptr, fieldName.data()); - mInfo->things.insert(fieldName, ThingId{ resourceKind, i }); + mInfo->things.try_emplace(fieldName, ShaderThingId{ resourceKind, i }); auto& thing = list.emplace_back(ShaderInfo::InputOutputThing{}); auto& var = thing.variable; @@ -520,8 +553,8 @@ bool Shader::GatherInfoIfAbsent() { QueryMathInfo(type, var.scalarType, var.width, var.height); } }; - GatherMathVars(inputCount, GL_PROGRAM_INPUT, ShaderInfo::TKD_Input, mInfo->inputs); - GatherMathVars(outputCount, GL_PROGRAM_OUTPUT, ShaderInfo::TKD_Output, mInfo->outputs); + GatherMathVars(inputCount, GL_PROGRAM_INPUT, ShaderThingId::KD_Input, mInfo->inputs); + GatherMathVars(outputCount, GL_PROGRAM_OUTPUT, ShaderThingId::KD_Output, mInfo->outputs); // Gather uniform variables for (int i = 0; i < uniformCount; ++i) { @@ -540,13 +573,10 @@ bool Shader::GatherInfoIfAbsent() { std::string fieldName(nameLength - 1, '\0'); glGetProgramResourceName(mHandle, GL_UNIFORM, i, nameLength, nullptr, fieldName.data()); - mInfo->things.insert(fieldName, ThingId{ ShaderInfo::TKD_Uniform, i }); + mInfo->things.try_emplace(fieldName, ShaderThingId{ ShaderThingId::KD_Uniform, i }); mInfo->uniforms.push_back(CreateVariable(type, loc)); } - mInfo->things.shrink_to_fit(); -#endif - return true; } |