3 #include <Engine/RaEngine.hpp>
7 #include <nlohmann/json.hpp>
10 #include <Core/Utils/Color.hpp>
11 #include <Core/Utils/EnumConverter.hpp>
12 #include <Core/Utils/Log.hpp>
13 #include <Core/Utils/StdOptional.hpp>
15 #include <Core/Containers/VariableSet.hpp>
17 #include <Engine/Data/ShaderProgram.hpp>
62 std::reference_wrapper<RenderParameters>,
63 std::reference_wrapper<const RenderParameters>>;
89 template <
typename EnumBaseType>
90 void addEnumConverter(
const std::string& name,
99 template <
typename EnumBaseType>
100 Core::Utils::optional<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>
101 getEnumConverter(
const std::string& name );
110 template <
typename Enum,
typename std::enable_if<std::is_enum<Enum> {},
bool>::type = true>
111 std::string getEnumString(
const std::string& name, Enum value );
121 template <
typename EnumBaseType>
123 getEnumString(
const std::string& name,
125 typename std::enable_if<!std::is_enum<EnumBaseType> {},
bool>::type = true );
137 template <
typename T>
138 void addParameter(
const std::string& name,
140 typename std::enable_if<!std::is_class<T> {},
bool>::type = true );
148 template <
typename T,
typename std::enable_if<std::is_
class<T> {},
bool>::type = true>
149 void addParameter(
const std::string& name,
const T& value );
161 template <
typename T,
162 typename std::enable_if<std::is_base_of<Data::Texture, T>::value,
bool>::type =
true>
163 void addParameter(
const std::string& name, T* tex,
int texUnit = -1 );
172 void addParameter(
const std::string& name,
const std::string& value );
173 void addParameter(
const std::string& name,
const char* value );
180 void addParameter(
const std::string& name, RenderParameters& value );
182 void addParameter(
const std::string& name,
const RenderParameters& value );
192 template <
typename T>
193 bool removeParameter(
const std::string& name );
201 void mergeKeepParameters(
const RenderParameters& params );
209 void mergeReplaceParameters(
const RenderParameters& params );
223 template <
typename T>
224 const UniformBindableSet<T>& getParameterSet()
const;
225 template <
typename T>
226 UniformBindableSet<T>& getParameterSet();
236 template <
typename T>
237 Core::Utils::optional<UniformBindableSet<T>*> hasParameterSet()
const;
245 template <
typename T>
246 Core::Utils::optional<UniformVariable<T>> containsParameter(
const std::string& name )
const;
256 template <
typename T>
257 const T& getParameter(
const std::string& name )
const;
258 template <
typename T>
259 T& getParameter(
const std::string& name );
264 template <
typename V>
265 void visit( V&& visitor )
const;
267 template <
typename V,
typename T>
268 void visit( V&& visitor, T& userParams )
const;
270 template <
typename V,
typename T>
271 void visit( V&& visitor, T&& userParams )
const;
285 class StaticParameterBinder
289 using types = BindableTypes;
294 void operator()(
const std::string& name,
297 shader->
setUniform( name.c_str(), Ra::Core::Utils::Color::VectorType( p ) );
303 void operator()(
const std::string& name,
306 auto [tex, texUnit] = p;
308 else { shader->
setUniform( name.c_str(), tex, texUnit ); }
315 template <
typename T>
316 void operator()(
const std::string& ,
317 const std::reference_wrapper<T>& p,
318 const Data::ShaderProgram* shader ) {
319 p.get().bind( shader );
325 template <
typename T>
326 void operator()(
const std::string& name,
const T& p,
const Data::ShaderProgram* shader ) {
327 shader->setUniform( name.c_str(), p );
334 static StaticParameterBinder s_binder;
339 Core::VariableSet m_parameterSets;
358 static void loadMetaData(
const std::string& basename, nlohmann::json& destination );
373 virtual const RenderParameters& getParameters()
const {
return m_renderParameters; }
407 template <
typename EnumBaseType>
409 const std::string& name,
412 std::function<void(
Core::VariableSet&,
const std::string&,
const std::string& )>
414 const std::string& nm,
415 const std::string& vl ) {
416 vs.insertOrAssignVariable( nm, converter->second->getEnumerator( vl ) );
421 template <
typename EnumBaseType>
422 Core::Utils::optional<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>
424 auto storedConverter =
425 m_parameterSets.
existsVariable<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>(
427 if ( storedConverter ) {
return ( *storedConverter )->second; }
431 template <
typename EnumBaseType>
433 const std::string& name,
435 typename std::enable_if<!std::is_enum<EnumBaseType> {},
bool>::type ) {
436 auto storedConverter =
437 m_parameterSets.existsVariable<std::shared_ptr<Core::Utils::EnumConverter<EnumBaseType>>>(
439 if ( storedConverter ) {
return ( *storedConverter )->second->getEnumerator( value ); }
440 LOG( Ra::Core::Utils::logWARNING ) << name +
" is not a registered Enum with underlying type " +
441 Ra::Core::Utils::demangleType<EnumBaseType>() +
".";
445 template <
typename Enum,
typename std::enable_if<std::is_enum<Enum> {},
bool>::type>
447 using EnumBaseType =
typename std::underlying_type_t<Enum>;
453 template <
typename T>
457 typename std::enable_if<!std::is_class<T> {},
bool>::type ) {
458 if constexpr ( std::is_enum<T>::value ) {
459 auto v =
static_cast<typename std::underlying_type<T>::type
>( value );
460 m_parameterSets.insertOrAssignVariable( name, v );
462 else { m_parameterSets.insertOrAssignVariable( name, value ); }
465 template <
typename T,
typename std::enable_if<std::is_
class<T> {},
bool>::type>
470 template <typename T, typename std::enable_if<std::is_base_of<Data::Texture, T>::value,
bool>::type>
484 template <
typename T>
489 template <
typename T>
494 template <
typename T>
499 template <
typename T>
500 inline Core::Utils::optional<RenderParameters::UniformBindableSet<T>*>
502 if constexpr ( std::is_enum<T>::value ) {
511 template <
typename T>
512 inline Core::Utils::optional<RenderParameters::UniformVariable<T>>
514 if constexpr ( std::is_enum<T>::value ) {
515 return m_parameterSets.
existsVariable<
typename std::underlying_type<T>::type>( name );
520 template <
typename T>
522 if constexpr ( std::is_enum<T>::value ) {
524 return reinterpret_cast<const T&
>(
525 m_parameterSets.
getVariable<
typename std::underlying_type<T>::type>( name ) );
527 else {
return m_parameterSets.
getVariable<T>( name ); }
530 template <
typename T>
532 return const_cast<T&
>(
const_cast<const RenderParameters*
>( this )->getParameter<T>( name ) );
535 template <
typename V>
537 m_parameterSets.
visit( visitor );
540 template <
typename V,
typename T>
542 m_parameterSets.
visit( visitor, std::forward<T&>( userParams ) );
545 template <
typename V,
typename T>
547 m_parameterSets.
visit( visitor, std::forward<T&&>( userParams ) );
This class manage the bijective association between string and integral representation of an enumerat...
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
auto getVariable(const std::string &name) const -> T &
get the value of the given variable
typename VariableContainer< T >::iterator VariableHandle
Handle of a variable A handle on a variable with type T is an iterator into the BaseContainer....
auto insertOrAssignVariable(const std::string &name, const T &value) -> std::pair< VariableHandle< T >, bool >
reset (or set if the variable does not exist yet) the value of the variable.
auto existsVariable(const std::string &name) const -> Utils::optional< VariableHandle< T >>
test the existence of the given variable
bool deleteVariable(const std::string &name)
Remove a variable, i.e. a name->value association.
auto getAllVariables() const -> VariableContainer< T > &
Get the whole container for variables of a given type.
std::map< std::string, T > VariableContainer
Container type for the mapping name->value of variables with type T.
auto existsVariableType() const -> Utils::optional< VariableContainer< T > * >
Test if the storage supports a given variable type.
void visit(F &&visitor) const
Visit the container using a user defined visitor.
virtual nlohmann::json getParametersMetadata() const =0
Get a json containing metadata about the parameters.
void addParameter(const std::string &name, T value, typename std::enable_if<!std::is_class< T > {}, bool >::type=true)
Add a parameter by value.
const T & getParameter(const std::string &name) const
Get a typed parameter.
void addEnumConverter(const std::string &name, std::shared_ptr< Core::Utils::EnumConverter< EnumBaseType >> converter)
Associate a converter for enumerated type to the given parameter name.
bool removeParameter(const std::string &name)
Remove the given parameter from the parameterSet.
std::pair< Data::Texture *, int > TextureInfo
Special type for Texture parameter.
const Core::VariableSet & getStorage() const
Get access to the parameter storage.
Core::Utils::TypeList< bool, Core::Utils::Color, int, uint, Scalar, TextureInfo, std::vector< int >, std::vector< uint >, std::vector< Scalar >, Core::Vector2, Core::Vector3, Core::Vector4, Core::Matrix2, Core::Matrix3, Core::Matrix4, std::reference_wrapper< RenderParameters >, std::reference_wrapper< const RenderParameters > > BindableTypes
Aliases for bindable parameter types.
Core::VariableSet::VariableContainer< T > UniformBindableSet
Core::Utils::optional< UniformVariable< T > > containsParameter(const std::string &name) const
Core::Utils::optional< std::shared_ptr< Core::Utils::EnumConverter< EnumBaseType > > > getEnumConverter(const std::string &name)
Search for a converter associated with an enumeration parameter.
const UniformBindableSet< T > & getParameterSet() const
Get a typed parameter set.
Core::Utils::optional< UniformBindableSet< T > * > hasParameterSet() const
Test if parameters of type T are stored.
std::string getEnumString(const std::string &name, Enum value)
Return the string associated to the actual value of a parameter.
Core::VariableSet::VariableHandle< T > UniformVariable
void visit(V &&visitor) const
virtual void updateFromParameters()
Update the attributes of the ShaderParameterProvider to their actual values stored in the renderParam...
virtual void updateGL()=0
Update the OpenGL states used by the ShaderParameterProvider. These state could be the ones from an a...
virtual std::list< std::string > getPropertyList() const
Get the list of properties the provider might use in a shader. Each property will be added to the sha...
void setUniform(const char *name, const T &value) const
Uniform setters.
void setUniformTexture(const char *name, Texture *tex) const