From 97907478abf6d0b2c9322316612a9a8dd64f8133 Mon Sep 17 00:00:00 2001 From: Criss Date: Sat, 15 Jun 2019 22:47:12 +0200 Subject: [PATCH] BRDF explorer app: Done some work on shader generation --- .../13.BRDF_Explorer/BRDFExplorerApp.cpp | 208 ++++++++++++++++++ .../CBRDFBuiltinIncludeLoader.h | 25 +++ 2 files changed, 233 insertions(+) create mode 100644 examples_tests/13.BRDF_Explorer/CBRDFBuiltinIncludeLoader.h diff --git a/examples_tests/13.BRDF_Explorer/BRDFExplorerApp.cpp b/examples_tests/13.BRDF_Explorer/BRDFExplorerApp.cpp index 1e31ad7c3..b5a15f9fa 100644 --- a/examples_tests/13.BRDF_Explorer/BRDFExplorerApp.cpp +++ b/examples_tests/13.BRDF_Explorer/BRDFExplorerApp.cpp @@ -34,6 +34,214 @@ SOFTWARE. using namespace irr; +class CShaderManager +{ +public: + struct SParams + { + bool constantAlbedo; + bool isotropicRoughness; + bool constantRoughness; + bool roughnessIsZero; + bool constantRI; + bool constantMetallic; + bool metallicIsZero; + bool metallicIsOne; + bool AOEnabled; + }; + +private: + using Key_t = uint16_t; + core::unordered_map Shaders; + video::IGPUProgrammingServices* Services = nullptr; + asset::IIncludeHandler* IncludeHandler = nullptr; + + enum E_SHADER_FLAGS : Key_t + { + ESF_CONST_ALBEDO = 1<<0, + ESF_ISOTROPIC_ROUGHNESS = 1<<1, + ESF_CONST_ROUGHNESS = 1<<2, + ESF_ZERO_ROUGHNESS = 1<<3, + ESF_CONST_RI = 1<<4, + ESF_CONST_METALLIC = 1<<5, + ESF_ZERO_METALLIC = 1<<6, + ESF_ONE_METALLIC = 1<<7, + ESF_AO_ENABLED = 1<<8 + }; + + static constexpr uint32_t firstSetBit(Key_t _x) + { + uint32_t n{}; + while (!(_x & Key_t{1u})) ++n; + return n; + } + Key_t flagsToKey(const SParams& _p) { + assert(!(_p.metallicIsZero && _p.metallicIsOne)); //this would be weird + + Key_t key{}; + key |= Key_t{_p.constantAlbedo}<second; // TODO + } + +public: + CShaderManager(video::IGPUProgrammingServices* _services, asset::IIncludeHandler* _inclHandler) : Services{_services}, IncludeHandler{_inclHandler} + { + } + + video::E_MATERIAL_TYPE getShader(const SParams& _params) + { + decltype(Shaders)::const_iterator found; + if ((found = Shaders.find(flagsToKey(_params))) != Shaders.cend()) + return found->second; + + return addShader(_params); + } +}; + namespace { const char* vertex_shader_source = diff --git a/examples_tests/13.BRDF_Explorer/CBRDFBuiltinIncludeLoader.h b/examples_tests/13.BRDF_Explorer/CBRDFBuiltinIncludeLoader.h new file mode 100644 index 000000000..8509e8841 --- /dev/null +++ b/examples_tests/13.BRDF_Explorer/CBRDFBuiltinIncludeLoader.h @@ -0,0 +1,25 @@ +#ifndef C_BRDF_BUILTIN_INCLUDE_LOADER_H_INCLUDED +#define C_BRDF_BUILTIN_INCLUDE_LOADER_H_INCLUDED + +#include "irr/asset/IBuiltinIncludeLoader.h" + +class CBRDFBuiltinIncludeLoader : public irr::asset::IBuiltinIncludeLoader +{ +public: + const char* getVirtualDirectoryName() const override { return "glsl/brdf/"; } + +protected: + irr::core::vector> getBuiltinNamesToFunctionMapping() const override + { + /* TODO */ + return { + { std::regex{"diffuse/lambert\\.glsl"}, [this](const std::string&) { return nullptr; }}, + { std::regex{"diffuse/oren_nayar\\.glsl"}, [](const std::string&) { return nullptr; }}, + { std::regex{"specular/ndf/ggx_trowbridge_reitz\\.glsl"}, nullptr }, + { std::regex{"specular/geom/ggx_smith\\.glsl"}, nullptr }, + { std::regex{"specular/fresnel/fresnel_schlick\\.glsl"}, nullptr } + }; + } +}; + +#endif //C_BRDF_BUILTIN_INCLUDE_LOADER_H_INCLUDED \ No newline at end of file