Panagiotis Christopoulos Charitos преди 13 години
родител
ревизия
f27d67c0c2
променени са 6 файла, в които са добавени 177 реда и са изтрити 130 реда
  1. 7 0
      include/anki/gl/Texture.h
  2. 42 42
      include/anki/renderer/Renderer.h
  3. 23 28
      include/anki/renderer/Sm.h
  4. 1 1
      src/renderer/Renderer.cpp
  5. 104 58
      src/renderer/Sm.cpp
  6. 0 1
      testapp/Main.cpp

+ 7 - 0
include/anki/gl/Texture.h

@@ -243,6 +243,13 @@ public:
 		setFilteringNoBind(filterType);
 		setFilteringNoBind(filterType);
 	}
 	}
 
 
+	/// Set texture parameter
+	void setParameter(GLenum param, GLint value)
+	{
+		TextureUnitsSingleton::get().bindTextureAndActivateUnit(*this);
+		glTexParameteri(target, param, value);
+	}
+
 	/// Generate new mipmaps
 	/// Generate new mipmaps
 	void genMipmap();
 	void genMipmap();
 
 

+ 42 - 42
include/anki/renderer/Renderer.h

@@ -25,7 +25,7 @@ struct RendererInitializer
 	{
 	{
 		struct Ez
 		struct Ez
 		{
 		{
-			bool enabled = false;
+			Bool enabled = false;
 		} ez;
 		} ez;
 	} ms;
 	} ms;
 
 
@@ -35,11 +35,11 @@ struct RendererInitializer
 		// Sm
 		// Sm
 		struct Sm
 		struct Sm
 		{
 		{
-			bool enabled = true;
-			bool pcfEnabled = true;
-			bool bilinearEnabled = true;
-			int resolution = 1024;
-			float level0Distance = 3.0;
+			Bool enabled = true;
+			Bool pcfEnabled = true;
+			Bool bilinearEnabled = true;
+			U32 resolution = 512;
+			U32 maxLights = 4;
 		} sm;
 		} sm;
 	} is;
 	} is;
 
 
@@ -49,41 +49,41 @@ struct RendererInitializer
 		// Hdr
 		// Hdr
 		struct Hdr
 		struct Hdr
 		{
 		{
-			bool enabled = true;
-			float renderingQuality = 0.25;
-			float blurringDist = 1.0;
-			float blurringIterationsNum = 2;
-			float exposure = 4.0;
+			Bool enabled = true;
+			F32 renderingQuality = 0.25;
+			F32 blurringDist = 1.0;
+			F32 blurringIterationsNum = 2;
+			F32 exposure = 4.0;
 		} hdr;
 		} hdr;
 
 
 		// Ssao
 		// Ssao
 		struct Ssao
 		struct Ssao
 		{
 		{
-			bool enabled = true;
-			float renderingQuality = 0.3;
-			uint32_t blurringIterationsNum = 2;
+			Bool enabled = true;
+			F32 renderingQuality = 0.3;
+			U32 blurringIterationsNum = 2;
 		} ssao;
 		} ssao;
 
 
 		// Bl
 		// Bl
 		struct Bl
 		struct Bl
 		{
 		{
-			bool enabled = true;
-			uint blurringIterationsNum = 2;
-			float sideBlurFactor = 1.0;
+			Bool enabled = true;
+			U blurringIterationsNum = 2;
+			F32 sideBlurFactor = 1.0;
 		} bl;
 		} bl;
 	} pps;
 	} pps;
 
 
 	// Dbg
 	// Dbg
 	struct Dbg
 	struct Dbg
 	{
 	{
-		bool enabled = false;
+		Bool enabled = false;
 	} dbg;
 	} dbg;
 
 
 	// the globals
 	// the globals
-	int width; ///< Ignored by MainRenderer
-	int height; ///< Ignored by MainRenderer
-	float mainRendererQuality = 1.0; ///< Only for MainRenderer
-	float lodDistance; ///< Distance that used to calculate the LOD
+	U32 width; ///< Ignored by MainRenderer
+	U32 height; ///< Ignored by MainRenderer
+	F32 mainRendererQuality = 1.0; ///< Only for MainRenderer
+	F32 lodDistance; ///< Distance that used to calculate the LOD
 
 
 	// funcs
 	// funcs
 	RendererInitializer()
 	RendererInitializer()
@@ -146,22 +146,22 @@ public:
 		return pps;
 		return pps;
 	}
 	}
 
 
-	uint getWidth() const
+	U getWidth() const
 	{
 	{
 		return width;
 		return width;
 	}
 	}
 
 
-	uint getHeight() const
+	U getHeight() const
 	{
 	{
 		return height;
 		return height;
 	}
 	}
 
 
-	float getAspectRatio() const
+	F32 getAspectRatio() const
 	{
 	{
-		return float(width) / height;
+		return F32(width) / height;
 	}
 	}
 
 
-	uint32_t getFramesCount() const
+	U32 getFramesCount() const
 	{
 	{
 		return framesNum;
 		return framesNum;
 	}
 	}
@@ -206,35 +206,35 @@ public:
 		return planesUpdateTimestamp;
 		return planesUpdateTimestamp;
 	}
 	}
 
 
-	double getMsTime() const
+	F64 getMsTime() const
 	{
 	{
 		return msTime;
 		return msTime;
 	}
 	}
 
 
-	double getIsTime() const
+	F64 getIsTime() const
 	{
 	{
 		return isTime;
 		return isTime;
 	}
 	}
 
 
-	double getPpsTime() const
+	F64 getPpsTime() const
 	{
 	{
 		return ppsTime;
 		return ppsTime;
 	}
 	}
 
 
-	double getBsTime() const
+	F64 getBsTime() const
 	{
 	{
 		return bsTime;
 		return bsTime;
 	}
 	}
 
 
-	bool getStagesProfilingEnabled() const
+	Bool getStagesProfilingEnabled() const
 	{
 	{
 		return enableStagesProfilingFlag;
 		return enableStagesProfilingFlag;
 	}
 	}
-	bool& getStagesProfilingEnabled()
+	Bool& getStagesProfilingEnabled()
 	{
 	{
 		return enableStagesProfilingFlag;
 		return enableStagesProfilingFlag;
 	}
 	}
-	void setStagesProfilingEnabled(const bool x)
+	void setStagesProfilingEnabled(const Bool x)
 	{
 	{
 		enableStagesProfilingFlag = x;
 		enableStagesProfilingFlag = x;
 	}
 	}
@@ -264,7 +264,7 @@ public:
 	void drawQuadInstanced(U32 primitiveCount);
 	void drawQuadInstanced(U32 primitiveCount);
 
 
 	/// Create FAI texture
 	/// Create FAI texture
-	static void createFai(uint width, uint height, int internalFormat,
+	static void createFai(U32 width, U32 height, int internalFormat,
 		int format, int type, Texture& fai);
 		int format, int type, Texture& fai);
 
 
 	/// Calculate the planes needed for the calculation of the fragment
 	/// Calculate the planes needed for the calculation of the fragment
@@ -287,7 +287,7 @@ public:
 		Vec2& limitsOfNearPlane);
 		Vec2& limitsOfNearPlane);
 
 
 	/// Get the LOD given the distance of an object from the camera
 	/// Get the LOD given the distance of an object from the camera
-	uint calculateLod(float distance) const
+	U calculateLod(F32 distance) const
 	{
 	{
 		return distance / lodDistance;
 		return distance / lodDistance;
 	}
 	}
@@ -303,20 +303,20 @@ protected:
 
 
 	/// @name Profiling stuff
 	/// @name Profiling stuff
 	/// @{
 	/// @{
-	double msTime, isTime, ppsTime, bsTime;
+	F64 msTime, isTime, ppsTime, bsTime;
 	std::unique_ptr<Query> msTq, isTq, ppsTq, bsTq, pps2Tq;
 	std::unique_ptr<Query> msTq, isTq, ppsTq, bsTq, pps2Tq;
-	bool enableStagesProfilingFlag;
+	Bool enableStagesProfilingFlag;
 	/// @}
 	/// @}
 
 
 	/// Width of the rendering. Don't confuse with the window width
 	/// Width of the rendering. Don't confuse with the window width
-	uint width;
+	U width;
 	/// Height of the rendering. Don't confuse with the window width
 	/// Height of the rendering. Don't confuse with the window width
-	uint height;
+	U height;
 	Scene* scene; ///< Current scene
 	Scene* scene; ///< Current scene
 	/// Max color attachments an FBO can accept
 	/// Max color attachments an FBO can accept
 	static int maxColorAtachments;
 	static int maxColorAtachments;
 	RenderableDrawer sceneDrawer;
 	RenderableDrawer sceneDrawer;
-	float lodDistance; ///< Distance that used to calculate the LOD
+	F32 lodDistance; ///< Distance that used to calculate the LOD
 
 
 	/// @name Optimization vars
 	/// @name Optimization vars
 	/// Used in other stages
 	/// Used in other stages
@@ -335,7 +335,7 @@ protected:
 	/// @}
 	/// @}
 
 
 private:
 private:
-	uint framesNum; ///< Frame number
+	U framesNum; ///< Frame number
 	Mat4 viewProjectionMat; ///< Precalculated in case anyone needs it
 	Mat4 viewProjectionMat; ///< Precalculated in case anyone needs it
 
 
 	/// @name For drawing a quad into the active framebuffer
 	/// @name For drawing a quad into the active framebuffer

+ 23 - 28
include/anki/renderer/Sm.h

@@ -4,7 +4,7 @@
 #include "anki/renderer/RenderingPass.h"
 #include "anki/renderer/RenderingPass.h"
 #include "anki/gl/Fbo.h"
 #include "anki/gl/Fbo.h"
 #include "anki/resource/TextureResource.h"
 #include "anki/resource/TextureResource.h"
-#include <array>
+#include "anki/util/Vector.h"
 
 
 namespace anki {
 namespace anki {
 
 
@@ -24,26 +24,10 @@ public:
 	{
 	{
 		return enabled;
 		return enabled;
 	}
 	}
-
-	bool getPcfEnabled() const
-	{
-		return pcfEnabled;
-	}
-
-	bool getBilinearEnabled() const
-	{
-		return bilinearEnabled;
-	}
 	/// @}
 	/// @}
 
 
 	void init(const RendererInitializer& initializer);
 	void init(const RendererInitializer& initializer);
-
-	/// Render the scene only with depth and store the result in the
-	/// shadowMap
-	/// @param[in] light The light
-	/// @param[in] distance The distance between the viewer's camera and the
-	///            light
-	Texture* run(Light& light, float distance); /// XXX order
+	void run();
 
 
 private:
 private:
 	struct Shadowmap
 	struct Shadowmap
@@ -51,26 +35,37 @@ private:
 		Texture tex;
 		Texture tex;
 		Fbo fbo;
 		Fbo fbo;
 		Light* light = nullptr;
 		Light* light = nullptr;
+		U32 timestamp = 0; ///< Timestamp of last render or light change
 	};
 	};
 
 
-	static const int MAX_SHADOWMAPS = 5;
+	Vector<Shadowmap> sms;
 
 
-	std::array<Shadowmap, MAX_SHADOWMAPS> sms;
+	/// If false then disable SM at all
+	Bool enabled; 
 
 
-	bool enabled; ///< If false then disable SM at all
 	/// Enable Percentage Closer Filtering for all the levels
 	/// Enable Percentage Closer Filtering for all the levels
-	bool pcfEnabled;
+	Bool pcfEnabled;
+
 	/// Shadowmap bilinear filtering for the first level. Better quality
 	/// Shadowmap bilinear filtering for the first level. Better quality
-	bool bilinearEnabled;
+	Bool bilinearEnabled;
+
 	/// Shadowmap resolution
 	/// Shadowmap resolution
-	int resolution;
+	U32 resolution;
 
 
-	/// XXX
-	Shadowmap& findBestCandidate(Light& light);
-};
+	U32 getMaxLightsCount()
+	{
+		return sms.size();
+	}
+
+	void prepareDraw();
+	void afterDraw();
 
 
+	/// Find the best shadowmap for that light
+	Shadowmap& bestCandidate(Light& light);
 
 
-} // end namespace
+	void doLight(Light& light);
+};
 
 
+} // end namespace anki
 
 
 #endif
 #endif

+ 1 - 1
src/renderer/Renderer.cpp

@@ -162,7 +162,7 @@ Vec3 Renderer::unproject(const Vec3& windowCoords, const Mat4& modelViewMat,
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-void Renderer::createFai(uint width, uint height, int internalFormat,
+void Renderer::createFai(U32 width, U32 height, int internalFormat,
 	int format, int type, Texture& fai)
 	int format, int type, Texture& fai)
 {
 {
 	Texture::Initializer init;
 	Texture::Initializer init;

+ 104 - 58
src/renderer/Sm.cpp

@@ -22,6 +22,7 @@ void Sm::init(const RendererInitializer& initializer)
 	resolution = initializer.is.sm.resolution;
 	resolution = initializer.is.sm.resolution;
 
 
 	// Init the shadowmaps
 	// Init the shadowmaps
+	sms.resize(initializer.is.sm.maxLights);
 	for(Shadowmap& sm : sms)
 	for(Shadowmap& sm : sms)
 	{
 	{
 		Renderer::createFai(resolution, resolution,
 		Renderer::createFai(resolution, resolution,
@@ -35,9 +36,9 @@ void Sm::init(const RendererInitializer& initializer)
 		{
 		{
 			sm.tex.setFiltering(Texture::TFT_NEAREST);
 			sm.tex.setFiltering(Texture::TFT_NEAREST);
 		}
 		}
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
-			GL_COMPARE_R_TO_TEXTURE);
-		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
+
+		sm.tex.setParameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
+		sm.tex.setParameter(GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
 
 
 		sm.fbo.create();
 		sm.fbo.create();
 		sm.fbo.setOtherAttachment(GL_DEPTH_ATTACHMENT, sm.tex);
 		sm.fbo.setOtherAttachment(GL_DEPTH_ATTACHMENT, sm.tex);
@@ -45,84 +46,129 @@ void Sm::init(const RendererInitializer& initializer)
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-Sm::Shadowmap& Sm::findBestCandidate(Light& light)
+void Sm::prepareDraw()
 {
 {
-	uint32_t crntFrame = r->getFramesCount();
-	uint32_t minFrame = crntFrame;
-	Shadowmap* best = nullptr;
-	Shadowmap* secondBest = nullptr;
-
-	// Check if already in list
-	for(Shadowmap& sm : sms)
-	{
-		uint32_t fr;
+	// set GL
+	GlStateSingleton::get().setViewport(0, 0, resolution, resolution);
 
 
-		if(sm.light == &light)
-		{
-			return sm;
-		}
-		else if(sm.light == nullptr)
-		{
-			best = &sm;
-		}
-		else if((fr = sm.light->getLastUpdateFrame()) < minFrame)
-		{
-			secondBest = &sm;
-			minFrame = fr;
-		}
-	}
+	// disable color & blend & enable depth test
+	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	GlStateSingleton::get().enable(GL_DEPTH_TEST);
+	GlStateSingleton::get().disable(GL_BLEND);
 
 
-	ANKI_ASSERT(best != nullptr || secondBest != nullptr);
+	// for artifacts
+	glPolygonOffset(2.0, 2.0); // keep the values as low as possible!!!!
+	GlStateSingleton::get().enable(GL_POLYGON_OFFSET_FILL);
+}
 
 
-	return (best) ? *best : *secondBest;
+//==============================================================================
+void Sm::afterDraw()
+{
+	GlStateSingleton::get().disable(GL_POLYGON_OFFSET_FILL);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 }
 }
 
 
 //==============================================================================
 //==============================================================================
-Texture* Sm::run(Light& light, float distance)
+void Sm::run()
 {
 {
-	if(!enabled || !light.getShadowEnabled()
-		|| light.getLightType() == Light::LT_POINT)
-	{
-		return nullptr;
-	}
+	ANKI_ASSERT(enabled);
 
 
-	Shadowmap& sm = findBestCandidate(light);
+	Camera& cam = r->getScene().getActiveCamera();
+	VisibilityInfo& vi = cam.getFrustumable()->getVisibilityInfo();
 
 
-	// Render
-	//
+	prepareDraw();
 
 
-	// FBO
-	sm.fbo.bind();
+	// Get the shadow casters
+	//
+	const U MAX_SHADOW_CASTERS = 256;
+	ANKI_ASSERT(getMaxLightsCount() > MAX_SHADOW_CASTERS);
+	Array<Light*, MAX_SHADOW_CASTERS> casters;
+	U32 castersCount = 0;
 
 
-	// set GL
-	GlStateSingleton::get().setViewport(0, 0, resolution, resolution);
-	glClear(GL_DEPTH_BUFFER_BIT);
+	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
+	{
+		Light* light = (*it);
 
 
-	// disable color & blend & enable depth test
-	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-	GlStateSingleton::get().enable(GL_DEPTH_TEST);
-	GlStateSingleton::get().disable(GL_BLEND);
+		if(light->getShadowEnabled())
+		{
+			casters[castersCount % getMaxLightsCount()] = light;
+			++castersCount;
+		}
+	}
 
 
-	// for artifacts
-	glPolygonOffset(2.0, 2.0); // keep the values as low as possible!!!!
-	GlStateSingleton::get().enable(GL_POLYGON_OFFSET_FILL);
+#if 1
+	if(castersCount > getMaxLightsCount())
+	{
+		ANKI_LOGW("Too many shadow casters: " << castersCount);
+	}
+#endif
 
 
-	Frustumable* fr = light.getFrustumable();
-	ANKI_ASSERT(fr);
+	castersCount = castersCount % getMaxLightsCount();
 
 
 	// render all
 	// render all
-	for(auto it = fr->getVisibilityInfo().getRenderablesBegin();
+	/*for(auto it = fr->getVisibilityInfo().getRenderablesBegin();
 		it != fr->getVisibilityInfo().getRenderablesEnd(); ++it)
 		it != fr->getVisibilityInfo().getRenderablesEnd(); ++it)
 	{
 	{
 		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 1, *(*it));
 		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 1, *(*it));
+	}*/
+
+	afterDraw();
+}
+
+//==============================================================================
+Sm::Shadowmap& Sm::bestCandidate(Light& light)
+{
+	// Allready there
+	for(Shadowmap& sm : sms)
+	{
+		if(&light == sm.light)
+		{
+			return sm;
+		}
 	}
 	}
 
 
-	// restore GL
-	GlStateSingleton::get().disable(GL_POLYGON_OFFSET_FILL);
-	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	// Find a null
+	for(Shadowmap& sm : sms)
+	{
+		if(sm.light == nullptr)
+		{
+			sm.light = &light;
+			sm.timestamp = 0;
+			return sm;
+		}
+	}
 
 
-	return &sm.tex;
+	// Find an old
+	Shadowmap* sm = &sms[0];
+	for(U i = 1; i < sms.size(); i++)
+	{
+		if(sms[i].timestamp < sm->timestamp)
+		{
+			sm = &sms[i];
+		}
+	}
+
+	sm->light = &light;
+	sm->timestamp = 0;
+	return *sm;
 }
 }
 
 
+//==============================================================================
+void Sm::doLight(Light& light)
+{
+	/// XXX Set FBO
+	Shadowmap& sm = bestCandidate(light);
+
+	Frustumable* fr = light.getFrustumable();
+	ANKI_ASSERT(fr != nullptr);
+	Movable* mov = &light;
+
+	U32 lightLastUpdateTimestamp = light.getMovableTimestamp();
+	lightLastUpdateTimestamp = std::max(lightLastUpdateTimestamp, 
+		light.getFrustumable()->getFrustumableTimestamp());
+
+	//for()
+
+}
 
 
-} // end namespace
+} // end namespace anki

+ 0 - 1
testapp/Main.cpp

@@ -316,7 +316,6 @@ void initSubsystems(int argc, char* argv[])
 	initializer.is.sm.enabled = true;
 	initializer.is.sm.enabled = true;
 	initializer.is.sm.pcfEnabled = true;
 	initializer.is.sm.pcfEnabled = true;
 	initializer.is.sm.resolution = 1024;
 	initializer.is.sm.resolution = 1024;
-	initializer.is.sm.level0Distance = 3.0;
 	initializer.pps.hdr.enabled = true;
 	initializer.pps.hdr.enabled = true;
 	initializer.pps.hdr.renderingQuality = 0.25;
 	initializer.pps.hdr.renderingQuality = 0.25;
 	initializer.pps.hdr.blurringDist = 1.0;
 	initializer.pps.hdr.blurringDist = 1.0;