Radium Engine  1.5.20
Loading...
Searching...
No Matches
RawShaderMaterial.cpp
1#include <Engine/Data/RawShaderMaterial.hpp>
2
3#include <Engine/Data/MaterialConverters.hpp>
4#include <Engine/Data/ShaderConfigFactory.hpp>
5#include <Engine/Rendering/RenderTechnique.hpp>
6
7namespace Ra {
8namespace Engine {
9namespace Data {
10
12 const std::string& instanceName,
15 Material( instanceName, instanceName, Material::MaterialAspect::MAT_OPAQUE ),
16 m_shaders { shaders },
17 m_paramProvider { std::move( paramProvider ) } {
18 m_materialKey = computeKey();
19 setMaterialName( m_materialKey );
21}
22
23RawShaderMaterial::~RawShaderMaterial() {
24 Rendering::EngineRenderTechniques::removeDefaultTechnique( m_materialKey );
25}
26
27std::string RawShaderMaterial::computeKey() {
28 // Hash the shader source to obtain identification keys
29 auto content = std::to_string( std::size_t( this ) );
30 for ( const auto& p : m_shaders ) {
31 content += p.second;
32 content += std::to_string( std::size_t( this ) );
33 }
34 return std::to_string( std::hash<std::string> {}( content ) );
35}
36
38 // Generate configuration using the given glsl source.
39 // The configuration key/name is the hash of shader sources
40 // The same configuration will be used as z-prepass config and opaque pass config.
41 Data::ShaderConfiguration myConfig { m_materialKey };
42 for ( const auto& p : m_shaders ) {
43 myConfig.addShaderSource( p.first, p.second );
44 }
46 // Register the technique builder for the custom material
47 // For now, as we can't change the material name, always use the key of the initial
48 // configuration
49 auto materialKey { m_materialKey };
50
51 Rendering::EngineRenderTechniques::registerDefaultTechnique(
52 materialKey, [materialKey]( Rendering::RenderTechnique& rt, bool ) {
53 // Configure the technique to render this object using forward Renderer or any
54 // compatible one Main pass (Mandatory) : BlinnPhong
57 // Z prepass use the same config
59 } );
60}
61
63 // Defining the material converter
64 EngineMaterialConverters::registerMaterialConverter( "Ra::Engine::Data::RawShaderMaterial",
66}
67
69 EngineMaterialConverters::removeMaterialConverter( "Ra::Engine::Data::RawShaderMaterial" );
70}
71
73 // update the state of the parameter provider
74 m_paramProvider->updateGL();
75}
76
81 Rendering::EngineRenderTechniques::removeDefaultTechnique( m_materialKey );
82 m_shaders = shaders;
83 if ( paramProvider ) { m_paramProvider = std::move( paramProvider ); }
84 m_materialKey = computeKey();
85 setMaterialName( m_materialKey );
87}
88} // namespace Data
89} // namespace Engine
90} // namespace Ra
Base class for materials.
Definition Material.hpp:24
void setMaterialName(std::string newName)
Change the material name.
Definition Material.hpp:115
MaterialAspect
Identifies the type of the material.
Definition Material.hpp:31
void updateShaders(const std::vector< std::pair< Data::ShaderType, std::string > > &shaders, std::shared_ptr< Data::ShaderParameterProvider > paramProvider=nullptr)
RawShaderMaterial(const std::string &instanceName, const std::vector< std::pair< Data::ShaderType, std::string > > &shaders, std::shared_ptr< Data::ShaderParameterProvider > paramProvider)
void setConfiguration(const Data::ShaderConfiguration &newConfig, Core::Utils::Index pass=DefaultRenderingPasses::LIGHTING_OPAQUE)
T move(T... args)
bool removeMaterialConverter(const std::string &name)
bool registerMaterialConverter(const std::string &name, ConverterFunction converter)
void addConfiguration(const ShaderConfiguration &config)
Core::Utils::optional< ShaderConfiguration > getConfiguration(const std::string &name)
bool removeConfiguration(const std::string &configName)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
STL namespace.
T to_string(T... args)