aboutsummaryrefslogtreecommitdiff
path: root/source/Shader.hpp
diff options
context:
space:
mode:
authorhnOsmium0001 <[email protected]>2022-04-10 22:42:14 -0700
committerhnOsmium0001 <[email protected]>2022-04-10 23:00:36 -0700
commit67affc75a4824dfd8675cc5455d9ee71b1583c1c (patch)
treee6624157831039ed3c1668b6a4b21337633f41e6 /source/Shader.hpp
parent906557f094e407ce21d429ef647bc75fe3179cf1 (diff)
Add shader and corresponding editor components
Diffstat (limited to 'source/Shader.hpp')
-rw-r--r--source/Shader.hpp101
1 files changed, 88 insertions, 13 deletions
diff --git a/source/Shader.hpp b/source/Shader.hpp
index e7a069b..8af5217 100644
--- a/source/Shader.hpp
+++ b/source/Shader.hpp
@@ -1,30 +1,102 @@
#pragma once
#include "EditorAttachment.hpp"
-#include "EditorInspector.hpp"
+#include "GraphicsTags.hpp"
#include "RcPtr.hpp"
+#include <absl/container/flat_hash_map.h>
#include <glad/glad.h>
-#include <tsl/array_map.h>
+#include <filesystem>
#include <memory>
#include <string_view>
+#include <vector>
-class ShaderDetails : public EditorAttachment, public IEditorInspectorTarget {
-public:
- std::string fileName;
+// TODO move to variable after pattern matching is in the language
-public:
- virtual void ShowInspector() override;
+struct ShaderVariable {
+ enum Kind {
+ KD_Math,
+ KD_Sampler,
+ KD_UniformBlock,
+ };
+
+ Kind kind;
+
+protected:
+ ShaderVariable(Kind kind)
+ : kind{ kind } {}
+};
+
+struct ShaderMathVariable : public ShaderVariable {
+ std::string name;
+ GLuint location;
+ GLenum scalarType;
+ int arrayLength;
+ int width;
+ int height;
+
+ ShaderMathVariable()
+ : ShaderVariable(KD_Math) {}
+};
+
+struct ShaderSamplerVariable : public ShaderVariable {
+ std::string name;
+ GLuint location;
+ GLenum type;
+ int arrayLength;
+
+ ShaderSamplerVariable()
+ : ShaderVariable(KD_Sampler) {}
+};
+
+struct ShaderUniformBlockVariable : public ShaderVariable {
+ std::string name;
+ /// Possible values: KD_Math
+ std::vector<std::unique_ptr<ShaderVariable>> items;
+ GLuint index;
+
+ ShaderUniformBlockVariable()
+ : ShaderVariable(KD_UniformBlock) {}
+};
+
+struct ShaderInfo {
+ enum ThingKind {
+ TKD_Input,
+ TKD_Output,
+ TKD_Uniform,
+ TKD_UniformBlock,
+ };
+
+ struct ThingId {
+ ThingKind kind;
+ int index;
+ };
+
+ struct InputOutputThing {
+ ShaderMathVariable variable;
+ Tags::VertexElementSemantic semantic = Tags::VES_Generic;
+ };
+
+ absl::flat_hash_map<std::string, ThingId> things;
+ std::vector<InputOutputThing> inputs;
+ std::vector<InputOutputThing> outputs;
+ /// Possible values: KD_Math, KD_Sampler
+ std::vector<std::unique_ptr<ShaderVariable>> uniforms;
+ std::vector<ShaderUniformBlockVariable> uniformBlocks;
+
+ bool SaveToFile(const std::filesystem::path& filePath) const;
+ bool LoadFromFile(const std::filesystem::path& filePath);
};
class Shader : public RefCounted {
private:
- std::unique_ptr<ShaderDetails> mDetails;
+ std::string mName;
+ std::unique_ptr<ShaderInfo> mInfo;
std::unique_ptr<EditorAttachment> mEditorAttachment;
GLuint mHandle = 0;
public:
- Shader() = default;
+ Shader(std::string name = "");
~Shader();
Shader(const Shader&) = delete;
Shader& operator=(const Shader&) = delete;
@@ -70,8 +142,11 @@ public:
EditorAttachment* GetEditorAttachment() const { return mEditorAttachment.get(); }
void SetEditorAttachment(EditorAttachment* attachment) { mEditorAttachment.reset(attachment); }
- void GatherDetailsIfAbsent();
- const ShaderDetails* GetDetails() const;
+ bool CreateEmptyInfoIfAbsent();
+ bool GatherInfoIfAbsent();
+ ShaderInfo* GetInfo() const;
+ /// If not empty, this name must not duplicate with any other shader object in the process.
+ const std::string& GetName() const;
GLuint GetProgram() const;
bool IsValid() const;
@@ -82,11 +157,11 @@ public:
static inline ShaderManager* instance = nullptr;
private:
- tsl::array_map<char, RcPtr<Shader>> mShaders;
+ absl::flat_hash_map<std::string_view, RcPtr<Shader>> mShaders;
public:
void DiscoverShaders();
- const tsl::array_map<char, RcPtr<Shader>>& GetShaders() const { return mShaders; }
+ const auto& GetShaders() const { return mShaders; }
Shader* FindShader(std::string_view name);
};