Panagiotis Christopoulos Charitos 13 years ago
parent
commit
ae476e61e5

+ 26 - 10
include/anki/gl/ShaderProgram.h

@@ -89,8 +89,10 @@ private:
 
 /// Uniform shader variable
 class ShaderProgramUniformVariable: public ShaderProgramVariable, 
-	public Flags<uint32_t>
+	public Flags<U32>
 {
+	friend class ShaderProgramUniformBlock;
+
 public:
 	enum ShaderProgramUniformVariableFlag
 	{
@@ -99,20 +101,21 @@ public:
 	};
 
 	ShaderProgramUniformVariable(
-		int loc,
+		GLint loc,
 		const char* name,
 		GLenum glDataType,
 		size_t size,
-		const ShaderProgram* fatherSProg)
+		const ShaderProgram* fatherSProg,
+		GLuint index_)
 		: ShaderProgramVariable(loc, name, glDataType, size, SPVT_UNIFORM, 
-			fatherSProg)
+			fatherSProg), index(index_)
 	{
 		enableFlag(SPUVF_DIRTY);
 	}
 
 	/// @name Set the var
 	/// @{
-	void set(const float x) const;
+	void set(const F32 x) const;
 	void set(const Vec2& x) const;
 	void set(const Vec3& x) const
 	{
@@ -132,14 +135,14 @@ public:
 	}
 	void set(const Texture& tex) const;
 
-	void set(const float x[], uint size) const;
+	void set(const F32 x[], uint size) const;
 	void set(const Vec2 x[], uint size) const;
 	void set(const Vec3 x[], uint size) const;
 	void set(const Vec4 x[], uint size) const;
 	void set(const Mat3 x[], uint size) const;
 	void set(const Mat4 x[], uint size) const;
 
-	/// @tparam Container It could be something like array<float, X> or 
+	/// @tparam Container It could be something like array<F32, X> or 
 	///         vector<Vec2> etc
 	template<typename Container>
 	void setContainer(const Container& c) const
@@ -150,8 +153,21 @@ public:
 
 private:
 	GLuint index;
-	GLint offset; ///< Offset inside the uniform block. -1 if it's inside the
-	              ///< default uniform block
+
+	ShaderProgramUniformBlock* block = nullptr;
+
+	/// Offset inside the uniform block. -1 if it's inside the default uniform 
+	/// block
+	GLint offset; 
+
+	/// "An array identifying the stride between elements, in basic machine 
+	/// units, of each of the uniforms specified by the corresponding array of
+	/// uniformIndices is returned.  The stride of a uniform associated with 
+	/// the default uniform block is -1.  Note that this information only makes 
+	/// sense for uniforms that are arrays. For uniforms that are not arrays, 
+	/// but are declared in a named uniform block, an array stride of zero is 
+	/// returned"
+	GLint arrayStride;
 
 	/// Standard set uniform checks
 	/// - Check if initialized
@@ -192,7 +208,7 @@ public:
 		return index;
 	}
 
-	uint32_t getSize() const
+	U32 getSize() const
 	{
 		return size;
 	}

+ 2 - 0
include/anki/renderer/Sm.h

@@ -30,6 +30,7 @@ public:
 	void run();
 
 private:
+	/// Shadowmap
 	struct Shadowmap
 	{
 		Texture tex;
@@ -52,6 +53,7 @@ private:
 	/// Shadowmap resolution
 	U32 resolution;
 
+	/// Get max shadow casters
 	U32 getMaxLightsCount()
 	{
 		return sms.size();

+ 3 - 2
include/anki/resource/Material.h

@@ -72,8 +72,9 @@ public:
 	const ShaderProgramUniformVariable* findShaderProgramUniformVariable(
 		const PassLevelKey& key) const
 	{
-		const ShaderProgramUniformVariable* var = sProgVars.at(key);
-		return var;
+		PassLevelToShaderProgramUniformVariableHashMap::const_iterator it =
+			sProgVars.find(key);
+		return (it == sProgVars.end()) ? nullptr : it->second;
 	}
 
 	/// Get the GL data type of all the shader program variables

+ 1 - 1
include/anki/scene/Light.h

@@ -121,7 +121,7 @@ private:
 	LightType type;
 	Vec4 color = Vec4(1.0);
 	Vec4 specColor = Vec4(1.0);
-	bool shadow = true;
+	bool shadow = false;
 };
 
 /// Point light

+ 4 - 4
include/anki/scene/VisibilityTester.h

@@ -2,7 +2,7 @@
 #define ANKI_SCENE_VISIBILITY_TESTER_H
 
 #include "anki/util/Vector.h"
-#include <cstdint>
+#include "anki/util/StdTypes.h"
 
 namespace anki {
 
@@ -23,8 +23,8 @@ class VisibilityInfo
 	friend class Octree;
 
 public:
-	typedef Vector<Renderable*> Renderables;
-	typedef Vector<Light*> Lights;
+	typedef Vector<SceneNode*> Renderables;
+	typedef Vector<SceneNode*> Lights;
 
 	Renderables::iterator getRenderablesBegin()
 	{
@@ -34,7 +34,7 @@ public:
 	{
 		return renderables.end();
 	}
-	uint32_t getRenderablesCount() const
+	U32 getRenderablesCount() const
 	{
 		return renderables.size();
 	}

+ 8 - 4
src/gl/ShaderProgram.cpp

@@ -42,6 +42,10 @@ ShaderProgramVariable::ShaderProgramVariable(
 	}
 }
 
+//==============================================================================
+// ShaderProgramUniformVariable                                                =
+//==============================================================================
+
 //==============================================================================
 void ShaderProgramUniformVariable::doCommonSetCode() const
 {
@@ -54,7 +58,7 @@ void ShaderProgramUniformVariable::doCommonSetCode() const
 }
 
 //==============================================================================
-void ShaderProgramUniformVariable::set(const float x) const
+void ShaderProgramUniformVariable::set(const F32 x) const
 {
 	doCommonSetCode();
 	ANKI_ASSERT(getGlDataType() == GL_FLOAT);
@@ -74,7 +78,7 @@ void ShaderProgramUniformVariable::set(const Vec2& x) const
 }
 
 //==============================================================================
-void ShaderProgramUniformVariable::set(const float x[], uint size) const
+void ShaderProgramUniformVariable::set(const F32 x[], uint size) const
 {
 	doCommonSetCode();
 	ANKI_ASSERT(getGlDataType() == GL_FLOAT);
@@ -451,7 +455,7 @@ void ShaderProgram::getUniAndAttribVars()
 
 	// uni locations
 	glGetProgramiv(glId, GL_ACTIVE_UNIFORMS, &num);
-	for(int i = 0; i < num; i++) // loop all uniforms
+	for(U i = 0; i < (U)num; i++) // loop all uniforms
 	{
 		glGetActiveUniform(glId, i, sizeof(name_) / sizeof(char), &length,
 			&size, &type, &name_[0]);
@@ -462,7 +466,7 @@ void ShaderProgram::getUniAndAttribVars()
 
 		ShaderProgramUniformVariable* var =
 			new ShaderProgramUniformVariable(loc, &name_[0], type, 
-			size, this);
+			size, this, i);
 
 		vars.push_back(std::shared_ptr<ShaderProgramVariable>(var));
 		unis.push_back(var);

+ 4 - 1
src/renderer/Is.cpp

@@ -584,7 +584,7 @@ void Is::lightPass()
 
 	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
 	{
-		Light* light = (*it);
+		Light* light = (*it)->getLight();
 		switch(light->getLightType())
 		{
 		case Light::LT_POINT:
@@ -732,6 +732,9 @@ void Is::run()
 	// Update tiles
 	updateTiles();
 
+	// Shadows
+	sm.run();
+
 	// Do the light pass
 	fbo.bind();
 	GlStateSingleton::get().setViewport(0, 0, r->getWidth(), r->getHeight());

+ 3 - 3
src/renderer/Ms.cpp

@@ -74,10 +74,10 @@ void Ms::run()
 	// render all
 	VisibilityInfo& vi =
 		r->getScene().getActiveCamera().getFrustumable()->getVisibilityInfo();
-	for(auto it = vi.getRenderablesBegin();
-		it != vi.getRenderablesEnd(); ++it)
+	for(auto it = vi.getRenderablesBegin(); it != vi.getRenderablesEnd(); ++it)
 	{
-		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 0, *(*it));
+		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 0, 
+			*((*it)->getRenderable()));
 	}
 
 	// restore depth

+ 45 - 12
src/renderer/Sm.cpp

@@ -81,13 +81,13 @@ void Sm::run()
 	// Get the shadow casters
 	//
 	const U MAX_SHADOW_CASTERS = 256;
-	ANKI_ASSERT(getMaxLightsCount() > MAX_SHADOW_CASTERS);
+	ANKI_ASSERT(getMaxLightsCount() < MAX_SHADOW_CASTERS);
 	Array<Light*, MAX_SHADOW_CASTERS> casters;
 	U32 castersCount = 0;
 
 	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
 	{
-		Light* light = (*it);
+		Light* light = (*it)->getLight();
 
 		if(light->getShadowEnabled())
 		{
@@ -106,11 +106,10 @@ void Sm::run()
 	castersCount = castersCount % getMaxLightsCount();
 
 	// render all
-	/*for(auto it = fr->getVisibilityInfo().getRenderablesBegin();
-		it != fr->getVisibilityInfo().getRenderablesEnd(); ++it)
+	for(U32 i = 0; i < castersCount; i++)
 	{
-		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 1, *(*it));
-	}*/
+		doLight(*casters[i]);
+	}
 
 	afterDraw();
 }
@@ -138,7 +137,7 @@ Sm::Shadowmap& Sm::bestCandidate(Light& light)
 		}
 	}
 
-	// Find an old
+	// Find an old and replace it
 	Shadowmap* sm = &sms[0];
 	for(U i = 1; i < sms.size(); i++)
 	{
@@ -156,19 +155,53 @@ Sm::Shadowmap& Sm::bestCandidate(Light& light)
 //==============================================================================
 void Sm::doLight(Light& light)
 {
-	/// XXX Set FBO
 	Shadowmap& sm = bestCandidate(light);
 
 	Frustumable* fr = light.getFrustumable();
 	ANKI_ASSERT(fr != nullptr);
 	Movable* mov = &light;
+	VisibilityInfo& vi = fr->getVisibilityInfo();
+
+	//
+	// Find last update
+	//
+	U32 lastUpdate = light.getMovableTimestamp();
+	lastUpdate = std::max(lastUpdate, fr->getFrustumableTimestamp());
 
-	U32 lightLastUpdateTimestamp = light.getMovableTimestamp();
-	lightLastUpdateTimestamp = std::max(lightLastUpdateTimestamp, 
-		light.getFrustumable()->getFrustumableTimestamp());
+	for(auto it = vi.getRenderablesBegin(); it != vi.getRenderablesEnd(); ++it)
+	{
+		SceneNode* node = *it;
+		Frustumable* bfr = node->getFrustumable();
+		Movable* bmov = node->getMovable();
 
-	//for()
+		if(bfr)
+		{
+			lastUpdate = std::max(lastUpdate, bfr->getFrustumableTimestamp());
+		}
+
+		if(bmov)
+		{
+			lastUpdate = std::max(lastUpdate, bmov->getMovableTimestamp());
+		}
+	}
 
+	Bool shouldUpdate = lastUpdate >= sm.timestamp;
+
+	if(!shouldUpdate)
+	{
+		return;
+	}
+
+	sm.timestamp = Timestamp::getTimestamp();
+
+	//
+	// Render
+	//
+	for(auto it = vi.getRenderablesBegin(); it != vi.getRenderablesEnd(); ++it)
+	{
+		r->getSceneDrawer().render(r->getScene().getActiveCamera(), 1,
+			*((*it)->getRenderable()));
+	}
 }
 
 } // end namespace anki

+ 2 - 2
src/scene/Octree.cpp

@@ -182,13 +182,13 @@ void Octree::doVisibilityTestsRec(Frustumable& fr, OctreeNode& node)
 		Renderable* r = sn->getRenderable();
 		if(r)
 		{
-			vinfo.renderables.push_back(r);
+			vinfo.renderables.push_back(sn);
 		}
 
 		Light* l = sn->getLight();
 		if(l)
 		{
-			vinfo.lights.push_back(l);
+			vinfo.lights.push_back(sn);
 
 			if(l->getShadowEnabled() && sn->getFrustumable() != nullptr)
 			{

+ 3 - 3
src/scene/VisibilityTester.cpp

@@ -44,13 +44,13 @@ void VisibilityTester::test(Frustumable& ref, Scene& scene)
 		Renderable* r = node->getRenderable();
 		if(r)
 		{
-			vinfo.renderables.push_back(r);
+			vinfo.renderables.push_back(node);
 		}
 
 		Light* l = node->getLight();
 		if(l)
 		{
-			vinfo.lights.push_back(l);
+			vinfo.lights.push_back(node);
 
 			if(l->getShadowEnabled() && fr)
 			{
@@ -97,7 +97,7 @@ void VisibilityTester::testLight(Light& light, Scene& scene)
 		Renderable* r = node->getRenderable();
 		if(r)
 		{
-			vinfo.renderables.push_back(r);
+			vinfo.renderables.push_back(node);
 		}
 	}
 }

+ 1 - 2
testapp/Main.cpp

@@ -101,7 +101,6 @@ void init()
 		lpos.z() = -50;
 	}
 
-
 #if 1
 	SpotLight* spot = new SpotLight("spot0", &scene, Movable::MF_NONE, nullptr);
 	spot->setOuterAngle(Math::toRad(45.0));
@@ -311,7 +310,7 @@ void initSubsystems(int argc, char* argv[])
 	// Main renderer
 	RendererInitializer initializer;
 	initializer.ms.ez.enabled = true;
-	initializer.dbg.enabled = false;
+	initializer.dbg.enabled = true;
 	initializer.is.sm.bilinearEnabled = true;
 	initializer.is.sm.enabled = true;
 	initializer.is.sm.pcfEnabled = true;