Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
MaterialPipeline.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Graphics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_MATERIALPIPELINE_HPP
8 #define NAZARA_MATERIALPIPELINE_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Graphics/Config.hpp>
12 #include <Nazara/Graphics/Enums.hpp>
13 #include <Nazara/Renderer/RenderPipeline.hpp>
14 #include <Nazara/Renderer/UberShader.hpp>
15 #include <array>
16 
17 namespace Nz
18 {
19  struct MaterialPipelineInfo : RenderStates
20  {
21  bool alphaTest = false;
22  bool depthSorting = false;
23  bool hasAlphaMap = false;
24  bool hasDiffuseMap = false;
25  bool hasEmissiveMap = false;
26  bool hasHeightMap = false;
27  bool hasNormalMap = false;
28  bool hasSpecularMap = false;
29  bool reflectionMapping = false;
30  bool shadowReceive = true;
31 
32  UberShaderConstRef uberShader;
33  };
34 
35  inline bool operator==(const MaterialPipelineInfo& lhs, const MaterialPipelineInfo& rhs);
36  inline bool operator!=(const MaterialPipelineInfo& lhs, const MaterialPipelineInfo& rhs);
37 
38  class MaterialPipeline;
39 
40  using MaterialPipelineConstRef = ObjectRef<const MaterialPipeline>;
41  using MaterialPipelineLibrary = ObjectLibrary<MaterialPipeline>;
42  using MaterialPipelineRef = ObjectRef<MaterialPipeline>;
43 
44  class NAZARA_GRAPHICS_API MaterialPipeline : public RefCounted
45  {
46  friend class Graphics;
47  friend MaterialPipelineLibrary;
48 
49  public:
50  struct Instance;
51 
52  MaterialPipeline(const MaterialPipeline&) = delete;
54  ~MaterialPipeline() = default;
55 
56  inline const Instance& Apply(UInt32 flags = ShaderFlags_None) const;
57 
58  MaterialPipeline& operator=(const MaterialPipeline&) = delete;
59  MaterialPipeline& operator=(MaterialPipeline&&) = delete;
60 
61  inline const MaterialPipelineInfo& GetInfo() const;
62  inline const Instance& GetInstance(UInt32 flags = ShaderFlags_None) const;
63 
64  static MaterialPipelineRef GetPipeline(const MaterialPipelineInfo& pipelineInfo);
65 
66  struct Instance
67  {
68  RenderPipeline renderPipeline;
69  UberShaderInstance* uberInstance = nullptr;
70  std::array<int, MaterialUniform_Max + 1> uniforms;
71  };
72 
73  private:
74  inline MaterialPipeline(const MaterialPipelineInfo& pipelineInfo);
75 
76  void GenerateRenderPipeline(UInt32 flags) const;
77 
78  static bool Initialize();
79  template<typename... Args> static MaterialPipelineRef New(Args&&... args);
80  static void Uninitialize();
81 
82  MaterialPipelineInfo m_pipelineInfo;
83  mutable std::array<Instance, ShaderFlags_Max + 1> m_instances;
84 
85  using PipelineCache = std::unordered_map<MaterialPipelineInfo, MaterialPipelineRef>;
86  static PipelineCache s_pipelineCache;
87 
88  static MaterialPipelineLibrary::LibraryMap s_library;
89  };
90 }
91 
92 #include <Nazara/Graphics/MaterialPipeline.inl>
93 
94 #endif // NAZARA_MATERIALPIPELINE_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Graphics class that represents the module initializer of Graphics.
Definition: Graphics.hpp:15
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
decltype(auto) Apply(F &&fn, Tuple &&t)
Applies the tuple to the function (e.g. calls the function using the tuple content as arguments) ...
Definition: Algorithm.inl:48
Core class that represents a reference with a counter.
Definition: RefCounted.hpp:21
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
Graphics class used to contains all rendering states that are not allowed to change individually on r...
Definition: MaterialPipeline.hpp:44
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469