From d43508ba4843801cbbf1f42a27af260d4eef5701 Mon Sep 17 00:00:00 2001 From: hnOsmium0001 Date: Sun, 17 Apr 2022 20:08:57 -0700 Subject: Initial work on sprites and texture system --- source/Shader.hpp | 67 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 27 deletions(-) (limited to 'source/Shader.hpp') 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 #include +#include #include #include #include #include // 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 things; - std::vector inputs; - std::vector outputs; + robin_hood::unordered_map things; + std::vector inputs; + std::vector outputs; std::vector> 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 mInfo; + ShaderInfo mInfo; std::unique_ptr 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> mShaders; + robin_hood::unordered_map> mShaders; public: void DiscoverShaders(); -- cgit v1.2.3-70-g09d2