From 17d5b091c9b2901ac96f5eee0da6af07228ae690 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 10 Apr 2022 23:00:36 -0700 Subject: Changeset: 5 Add shader and corresponding editor components --- source/Shader.hpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 13 deletions(-) (limited to 'source/Shader.hpp') 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 #include -#include +#include #include #include +#include -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> 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 things; + std::vector inputs; + std::vector outputs; + /// Possible values: KD_Math, KD_Sampler + std::vector> uniforms; + std::vector uniformBlocks; + + bool SaveToFile(const std::filesystem::path& filePath) const; + bool LoadFromFile(const std::filesystem::path& filePath); }; class Shader : public RefCounted { private: - std::unique_ptr mDetails; + std::string mName; + std::unique_ptr mInfo; std::unique_ptr 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> mShaders; + absl::flat_hash_map> mShaders; public: void DiscoverShaders(); - const tsl::array_map>& GetShaders() const { return mShaders; } + const auto& GetShaders() const { return mShaders; } Shader* FindShader(std::string_view name); }; -- cgit v1.2.3-70-g09d2