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 
168  void pushLights(const std::vector<RenderableComponentInstance*>& renderComps);
169 
173  static uint getMaximumLightCount() { return mMaxLightCount; }
174 
175  protected:
182  virtual void getDependentServices(std::vector<rtti::TypeInfo>& dependencies) override;
183 
189  virtual bool init(nap::utility::ErrorState& errorState) override;
190 
196  virtual void preShutdown() override;
197 
203  virtual void shutdown() override;
204 
209  virtual void preResourcesLoaded() override;
210 
214  virtual void postResourcesLoaded() override;
215 
223  SpawnedEntityInstance spawn(const nap::Entity& entity, nap::utility::ErrorState& error);
224 
230  void destroy(SpawnedEntityInstance& entityInstance);
231 
237  virtual void postUpdate(double deltaTime);
238 
239  private:
240  void pushLightsInternal(const std::vector<MaterialInstance*>& materials);
241  void registerLightComponent(LightComponentInstance& light);
242  void removeLightComponent(LightComponentInstance& light);
243  void registerCubeMap(CubeMapFromFile& cubemap);
244  void removeCubeMap(CubeMapFromFile& cubemap);
245  bool initServiceResources(utility::ErrorState& errorState);
246 
250  struct ShadowMapEntry
251  {
252  ShadowMapEntry(std::unique_ptr<DepthRenderTarget> target, std::unique_ptr<DepthRenderTexture2D> texture) :
253  mTarget(std::move(target)), mTexture(std::move(texture))
254  {
255  mTarget->mDepthTexture = mTexture.get();
256  }
257 
258  std::unique_ptr<DepthRenderTarget> mTarget;
259  std::unique_ptr<DepthRenderTexture2D> mTexture;
260  };
261 
265  struct CubeMapEntry
266  {
267  CubeMapEntry(std::unique_ptr<CubeDepthRenderTarget> target, std::unique_ptr<DepthRenderTextureCube> texture) :
268  mTarget(std::move(target)), mTexture(std::move(texture))
269  {
270  mTarget->mCubeDepthTexture = mTexture.get();
271  }
272 
273  std::unique_ptr<CubeDepthRenderTarget> mTarget;
274  std::unique_ptr<DepthRenderTextureCube> mTexture;
275  };
276 
277  // Reference to render service
278  RenderService* mRenderService = nullptr;
279 
280  // Registered light component instances
281  std::vector<LightComponentInstance*> mLightComponents;
282 
283  // Shadow mapping
284  std::unordered_map<LightComponentInstance*, std::unique_ptr<ShadowMapEntry>> mLightDepthMap;
285  std::unordered_map<LightComponentInstance*, std::unique_ptr<CubeMapEntry>> mLightCubeMap;
286  std::unordered_map<LightComponentInstance*, LightFlags> mLightFlagsMap;
287 
288  std::unique_ptr<Sampler2DArray> mSampler2DResource;
289  std::unique_ptr<SamplerCubeArray> mSamplerCubeResource;
290  std::unique_ptr<DepthRenderTexture2D> mShadowTextureDummy;
291  std::unique_ptr<nap::Scene> mLightScene = nullptr;
292 
293  bool mShadowMappingEnabled = true;
294  bool mShadowResourcesCreated = false;
295 
296  // Cube maps from file
297  std::unordered_map<CubeMapFromFile*, std::unique_ptr<CubeRenderTarget>> mCubeMapTargets;
298 
299  std::unique_ptr<EmptyMesh> mNoMesh;
300  std::unique_ptr<MaterialInstanceResource> mCubeMaterialInstanceResource;
301  std::unique_ptr<MaterialInstance> mCubeMaterialInstance;
302  Material* mCubeMapMaterial = nullptr;
303 
304  static constexpr const uint mRequiredVulkanVersionMajor = 1;
305  static constexpr const uint mRequiredVulkanVersionMinor = 0;
306  static constexpr const uint mMaxLightCount = 8;
307  };
308 }
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:211
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:173