NAP
renderadvancedservice.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <nap/service.h>
9 #include <depthrendertarget.h>
10 #include <rendertexturecube.h>
11 #include <materialinstance.h>
12 #include <renderablemesh.h>
13 #include <scene.h>
14 
15 // Local includes
16 #include "cuberendertarget.h"
17 #include "cubedepthrendertarget.h"
18 #include "lightcomponent.h"
19 #include "lightflags.h"
20 #include "rendertag.h"
21 #include "emptymesh.h"
22 #include "cubemapfromfile.h"
23 
24 namespace nap
25 {
26  // Forward declares
27  class RenderService;
28  class RenderableComponentInstance;
29  class Material;
30 
31  // Light scene identifier
32  namespace scene
33  {
34  namespace light
35  {
36  inline constexpr const char* id = "lightscene";
37  }
38  }
39 
40 
42  // Render Advanced Service
44 
46  {
47  RTTI_ENABLE(ServiceConfiguration)
48  friend class PreRenderCubeMapsCommand;
49  public:
50  virtual rtti::TypeInfo getServiceType() const;
51 
52  DepthRenderTexture2D::EDepthFormat mDepthFormat = DepthRenderTexture2D::EDepthFormat::D16;
53  DepthRenderTextureCube::EDepthFormat mDepthFormatCube = DepthRenderTextureCube::EDepthFormat::D16;
54  bool mEnableShadowMapping = true;
55  };
56 
57 
99  class NAPAPI RenderAdvancedService : public Service
100  {
102  friend class CubeMapFromFile;
103  RTTI_ENABLE(Service)
104  public:
105  // Default Constructor
107 
112  bool isShadowMappingEnabled() const { return mShadowMappingEnabled; }
113 
118  const std::vector<LightComponentInstance*>& getLights() { return mLightComponents; }
119 
137  void renderShadows(const std::vector<RenderableComponentInstance*>& renderComps, bool updateMaterials = true, RenderMask renderMask = 0);
138 
144  void renderLocators(IRenderTarget& renderTarget, CameraComponentInstance& camera, bool drawFrustrum);
145 
160  void pushLights(const std::vector<RenderableComponentInstance*>& renderComps);
161 
165  static uint getMaximumLightCount() { return mMaxLightCount; }
166 
167  protected:
174  virtual void getDependentServices(std::vector<rtti::TypeInfo>& dependencies) override;
175 
181  virtual bool init(nap::utility::ErrorState& errorState) override;
182 
188  virtual void preShutdown() override;
189 
195  virtual void shutdown() override;
196 
201  virtual void preResourcesLoaded() override;
202 
206  virtual void postResourcesLoaded() override;
207 
215  SpawnedEntityInstance spawn(const nap::Entity& entity, nap::utility::ErrorState& error);
216 
222  void destroy(SpawnedEntityInstance& entityInstance);
223 
229  virtual void postUpdate(double deltaTime);
230 
231  private:
232  void pushLightsInternal(const std::vector<MaterialInstance*>& materials);
233  void registerLightComponent(LightComponentInstance& light);
234  void removeLightComponent(LightComponentInstance& light);
235  void registerCubeMap(CubeMapFromFile& cubemap);
236  void removeCubeMap(CubeMapFromFile& cubemap);
237  bool initServiceResources(utility::ErrorState& errorState);
238 
242  struct ShadowMapEntry
243  {
244  ShadowMapEntry(std::unique_ptr<DepthRenderTarget> target, std::unique_ptr<DepthRenderTexture2D> texture) :
245  mTarget(std::move(target)), mTexture(std::move(texture))
246  {
247  mTarget->mDepthTexture = mTexture.get();
248  }
249 
250  std::unique_ptr<DepthRenderTarget> mTarget;
251  std::unique_ptr<DepthRenderTexture2D> mTexture;
252  };
253 
257  struct CubeMapEntry
258  {
259  CubeMapEntry(std::unique_ptr<CubeDepthRenderTarget> target, std::unique_ptr<DepthRenderTextureCube> texture) :
260  mTarget(std::move(target)), mTexture(std::move(texture))
261  {
262  mTarget->mCubeDepthTexture = mTexture.get();
263  }
264 
265  std::unique_ptr<CubeDepthRenderTarget> mTarget;
266  std::unique_ptr<DepthRenderTextureCube> mTexture;
267  };
268 
269  // Reference to render service
270  RenderService* mRenderService = nullptr;
271 
272  // Registered light component instances
273  std::vector<LightComponentInstance*> mLightComponents;
274 
275  // Shadow mapping
276  std::unordered_map<LightComponentInstance*, std::unique_ptr<ShadowMapEntry>> mLightDepthMap;
277  std::unordered_map<LightComponentInstance*, std::unique_ptr<CubeMapEntry>> mLightCubeMap;
278  std::unordered_map<LightComponentInstance*, LightFlags> mLightFlagsMap;
279 
280  std::unique_ptr<Sampler2DArray> mSampler2DResource;
281  std::unique_ptr<SamplerCubeArray> mSamplerCubeResource;
282  std::unique_ptr<DepthRenderTexture2D> mShadowTextureDummy;
283  std::unique_ptr<nap::Scene> mLightScene = nullptr;
284 
285  bool mShadowMappingEnabled = true;
286  bool mShadowResourcesCreated = false;
287 
288  // Cube maps from file
289  std::unordered_map<CubeMapFromFile*, std::unique_ptr<CubeRenderTarget>> mCubeMapTargets;
290 
291  std::unique_ptr<EmptyMesh> mNoMesh;
292  std::unique_ptr<MaterialInstanceResource> mCubeMaterialInstanceResource;
293  std::unique_ptr<MaterialInstance> mCubeMaterialInstance;
294  Material* mCubeMapMaterial = nullptr;
295 
296  static constexpr const uint mRequiredVulkanVersionMajor = 1;
297  static constexpr const uint mRequiredVulkanVersionMinor = 0;
298  static constexpr const uint mMaxLightCount = 8;
299  };
300 }
nap::RenderAdvancedServiceConfiguration
Definition: renderadvancedservice.h:45
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::IRenderTarget
Definition: irendertarget.h:21
nap::DepthRenderTexture2D
Definition: rendertexture2d.h:69
nap::LightComponentInstance
Definition: lightcomponent.h:209
nap::CubeMapFromFile
Definition: cubemapfromfile.h:38
nap::CameraComponentInstance
Definition: cameracomponent.h:38
nap::RenderAdvancedService::isShadowMappingEnabled
bool isShadowMappingEnabled() const
Definition: renderadvancedservice.h:112
nap::RenderAdvancedService::getLights
const std::vector< LightComponentInstance * > & getLights()
Definition: renderadvancedservice.h:118
nap::RenderAdvancedService
Definition: renderadvancedservice.h:99
nap::SpawnedEntityInstance
Definition: entity.h:245
nap::utility::ErrorState
Definition: errorstate.h:19
nap::RenderMask
uint64 RenderMask
Definition: rendertag.h:23
nap::ServiceConfiguration
Definition: service.h:28
nap::sampler::texture
constexpr const char * texture
Definition: sampler.h:27
nap::Service
Definition: templateservice.h:8
nap
Definition: templateapp.h:17
nap::DepthRenderTextureCube
Definition: rendertexturecube.h:91
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::Entity
Definition: entity.h:339
nap::RenderAdvancedService::getMaximumLightCount
static uint getMaximumLightCount()
Definition: renderadvancedservice.h:165