diff options
author | rtk0c <[email protected]> | 2022-04-17 20:08:57 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-17 20:08:57 -0700 |
commit | 5424a1d5434e3ddd911a504719918c2df027e2fd (patch) | |
tree | 6275aab13140d81dcc46c8290e73ac9a8bbb5605 /source/Shader.hpp | |
parent | afcac59c7d04f4337d6b04ebed8cac7e871ccc50 (diff) |
Changeset: 8 Initial work on sprites and texture system
Diffstat (limited to 'source/Shader.hpp')
-rw-r--r-- | source/Shader.hpp | 67 |
1 files changed, 40 insertions, 27 deletions
diff --git a/source/Shader.hpp b/source/Shader.hpp index 79262a6..7ad2bbb 100644 --- a/source/Shader.hpp +++ b/source/Shader.hpp @@ -3,15 +3,17 @@ #include "EditorAttachment.hpp" #include "GraphicsTags.hpp" #include "RcPtr.hpp" +#include "Utils.hpp" -#include <absl/container/flat_hash_map.h> #include <glad/glad.h> +#include <robin_hood.h> #include <filesystem> #include <memory> #include <string_view> #include <vector> // TODO move to variable after pattern matching is in the language +// TODO migrate shader editor to Ires // Forward declarations class Shader; @@ -25,6 +27,9 @@ struct ShaderVariable { std::string name; Kind kind; GLuint location; + Tags::VertexElementSemantic semantic = Tags::VES_Generic; + + virtual void ShowInfo() const = 0; protected: ShaderVariable(Kind kind) @@ -39,6 +44,8 @@ struct ShaderMathVariable : public ShaderVariable { ShaderMathVariable() : ShaderVariable(KD_Math) {} + + virtual void ShowInfo() const override; }; struct ShaderSamplerVariable : public ShaderVariable { @@ -47,6 +54,8 @@ struct ShaderSamplerVariable : public ShaderVariable { ShaderSamplerVariable() : ShaderVariable(KD_Sampler) {} + + virtual void ShowInfo() const override; }; struct ShaderThingId { @@ -64,29 +73,27 @@ struct ShaderThingId { }; struct ShaderInfo { - struct InputOutputThing { - ShaderMathVariable variable; - Tags::VertexElementSemantic semantic = Tags::VES_Generic; - }; - - absl::flat_hash_map<std::string, ShaderThingId> things; - std::vector<InputOutputThing> inputs; - std::vector<InputOutputThing> outputs; + robin_hood::unordered_map<std::string, ShaderThingId, StringHash, StringEqual> things; + std::vector<ShaderMathVariable> inputs; + std::vector<ShaderMathVariable> outputs; std::vector<std::unique_ptr<ShaderVariable>> uniforms; + GLuint FindInputLocation(Tags::VertexElementSemantic semantic); + GLuint FindOutputLocation(Tags::VertexElementSemantic semantic); ShaderVariable* FindVariable(const ShaderThingId& thing); - - bool SaveToFile(const std::filesystem::path& filePath) const; - bool LoadFromFile(const std::filesystem::path& filePath); - void LoadLocations(const Shader& ownerShader); }; class Shader : public RefCounted { private: std::string mName; - std::unique_ptr<ShaderInfo> mInfo; + ShaderInfo mInfo; std::unique_ptr<EditorAttachment> mEditorAttachment; - GLuint mHandle = 0; + GLuint mProgram = 0; + +public: + GLuint autofillLoc_Transform = Tags::kInvalidLocation; + GLuint autofillLoc_Time = Tags::kInvalidLocation; + GLuint autofillLoc_DeltaTime = Tags::kInvalidLocation; public: Shader(std::string name = ""); @@ -97,14 +104,14 @@ public: Shader& operator=(Shader&&) = default; enum ErrorCode { - Success, + EC_Success, /// Generated when Init*() functions are called on an already initialized Shader object. - ShaderAlreadyCreated, + EC_AlreadyInitialized, /// Generated when the one-source-file text contains invalid or duplicate shader variants. - InvalidShaderVariant, - FileIOFailed, - CompilationFailed, - LinkingFailed, + EC_InvalidShaderVariant, + EC_FileIoFailed, + EC_CompilationFailed, + EC_LinkingFailed, }; struct ShaderSources { @@ -138,14 +145,20 @@ public: void GetDesignatedMetadataPath(char* buffer, int bufferSize); std::filesystem::path GetDesignatedMetadataPath(); - bool CreateEmptyInfoIfAbsent(); - bool GatherInfoIfAbsent(); - ShaderInfo* GetInfo() const; + /// Rebuild info object using OpenGL shader introspection API. Requires OpenGL 4.3 or above. Overrides existing info object. + bool GatherInfoShaderIntrospection(); + const ShaderInfo& GetInfo() const { return mInfo; } + ShaderInfo& GetInfo() { return mInfo; } /// If not empty, this name must not duplicate with any other shader object in the process. - const std::string& GetName() const; - GLuint GetProgram() const; + const std::string& GetName() const { return mName; } + GLuint GetProgram() const { return mProgram; } + bool IsAnnoymous() const; bool IsValid() const; + + bool SaveMetadataToFile(const std::filesystem::path& filePath) const; + /// Overrides existing info object. + bool LoadMetadataFromFile(const std::filesystem::path& filePath); }; class ShaderManager { @@ -153,7 +166,7 @@ public: static inline ShaderManager* instance = nullptr; private: - absl::flat_hash_map<std::string_view, RcPtr<Shader>> mShaders; + robin_hood::unordered_map<std::string_view, RcPtr<Shader>> mShaders; public: void DiscoverShaders(); |