소스 검색

Light stage

Panagiotis Christopoulos Charitos 13 년 전
부모
커밋
b27c32bba5

+ 12 - 0
include/anki/gl/ShaderProgram.h

@@ -182,6 +182,7 @@ private:
 	GLuint index;
 	uint32_t size; ///< In bytes
 	std::string name;
+	GLuint bindingPoint = 0; ///< All blocks the default to 0
 };
 
 /// Shader program object
@@ -194,6 +195,8 @@ public:
 		UniformVariablesContainer;
 	typedef std::vector<ShaderProgramAttributeVariable*>
 		AttributeVariablesContainer;
+	typedef std::vector<std::shared_ptr<ShaderProgramUniformBlock>>
+		UniformBlocksContainer;
 
 	/// @name Constructors/Destructor
 	/// @{
@@ -314,6 +317,9 @@ private:
 	typedef ConstCharPtrHashMap<ShaderProgramAttributeVariable*>::Type
 		NameToAttribVarHashMap;
 
+	typedef ConstCharPtrHashMap<ShaderProgramUniformBlock*>::Type
+		NameToUniformBlockHashMap;
+
 	static thread_local const ShaderProgram* current;
 
 	/// Shader source that is used in ALL shader programs
@@ -335,12 +341,18 @@ private:
 	NameToVarHashMap nameToVar; ///< Variable searching
 	NameToUniVarHashMap nameToUniVar; ///< Uniform searching
 	NameToAttribVarHashMap nameToAttribVar; ///< Attribute searching
+
+	UniformBlocksContainer blocks;
+	NameToUniformBlockHashMap namtToBlock;
 	/// @}
 
 	/// Query the driver to get the vars. After the linking of the shader
 	/// prog is done gather all the vars in custom containers
 	void getUniAndAttribVars();
 
+	/// Get info about the uniform blocks
+	void initUniformBlocks();
+
 	/// Create and compile shader
 	/// @return The shader's OpenGL id
 	/// @exception Exception

+ 19 - 3
include/anki/renderer/Is.h

@@ -2,13 +2,11 @@
 #define ANKI_RENDERER_IS_H
 
 #include "anki/renderer/RenderingPass.h"
-#include "anki/gl/Fbo.h"
 #include "anki/resource/TextureResource.h"
 #include "anki/resource/Resource.h"
 #include "anki/resource/ShaderProgramResource.h"
+#include "anki/gl/Gl.h"
 #include "anki/math/Math.h"
-#include "anki/gl/Vbo.h"
-#include "anki/gl/Vao.h"
 #include "anki/renderer/Sm.h"
 #include "anki/renderer/Smo.h"
 
@@ -38,6 +36,24 @@ public:
 
 private:
 	Texture fai;
+	Fbo fbo;
+	/// Illumination stage ambient pass shader program
+	ShaderProgramResourcePointer ambientPassSProg;
+	/// Illumination stage point light shader program
+	ShaderProgramResourcePointer pointLightSProg;
+	/// Illumination stage spot light w/o shadow shader program
+	ShaderProgramResourcePointer spotLightNoShadowSProg;
+	/// Illumination stage spot light w/ shadow shader program
+	ShaderProgramResourcePointer spotLightShadowSProg;
+
+	/// The ambient pass
+	void ambientPass(const Vec3& color);
+
+	/// The point light pass
+	void pointLightPass(PointLight& plight);
+
+	/// The spot light pass
+	void spotLightPass(SpotLight& slight);
 };
 
 } // end namespace

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

@@ -64,7 +64,7 @@ public:
 	}
 
 protected:
-	Frustum* frustum;
+	Frustum* frustum = nullptr;
 };
 
 /// Perspective prustumable interface for scene nodes

+ 30 - 36
include/anki/scene/Light.h

@@ -114,7 +114,6 @@ public:
 	void frameUpdate(float prevUpdateTime, float crntTime, int frame)
 	{
 		SceneNode::frameUpdate(prevUpdateTime, crntTime, frame);
-		Movable::update();
 	}
 	/// @}
 
@@ -165,11 +164,11 @@ public:
 
 public:
 	Sphere sphereW;
-	Sphere sphereL;
+	Sphere sphereL = {Vec3(0.0), 1.0};
 };
 
 /// Spot light
-class SpotLight: public Light, public Frustumable
+class SpotLight: public Light, public PerspectiveFrustumable
 {
 public:
 	ANKI_HAS_SLOTS(SpotLight)
@@ -182,57 +181,52 @@ public:
 
 	/// @name Accessors
 	/// @{
-	float getFov() const
+	const Mat4& getViewMatrix() const
 	{
-		return frustum.getFovX();
+		return viewMat;
 	}
-	void setFov(float x)
+
+	const Mat4& getProjectionMatrix() const
 	{
-		fovProp->setValue(x);
+		return projectionMat;
 	}
 
-	float getDistance() const
+	Texture& getTexture()
 	{
-		return frustum.getFar();
+		return *tex;
 	}
-	void setDistance(float x)
+	const Texture& getTexture() const
 	{
-		distProp->setValue(x);
+		return *tex;
 	}
 
-	const Mat4& getViewMatrix() const
-	{
-		return viewMat;
-	}
-	Mat4& getViewMatrix()
+	float getFov() const
 	{
-		return viewMat;
+		return getFovX();
 	}
-	void setViewMatrix(const Mat4& x)
+	void setFov(float x)
 	{
-		viewMat = x;
+		setFovX(x);
+		setFovY(x);
 	}
 
-	const Mat4& getProjectionMatrix() const
-	{
-		return projectionMat;
-	}
-	Mat4& getProjectionMatrix()
+	float getDistance() const
 	{
-		return projectionMat;
+		return getFar();
 	}
-	void setProjectionMatrix(const Mat4& x)
+	void setDistance(float f)
 	{
-		projectionMat = x;
+		setFar(f);
 	}
+	/// @}
 
-	Texture& getTexture()
-	{
-		return *tex;
-	}
-	const Texture& getTexture() const
+	/// @name SceneNode virtuals
+	/// @{
+
+	/// Override SceneNode::getFrustumable()
+	Frustumable* getFrustumable()
 	{
-		return *tex;
+		return this;
 	}
 	/// @}
 
@@ -244,7 +238,7 @@ public:
 	void movableUpdate()
 	{
 		Movable::movableUpdate();
-		frustum.transform(getWorldTransform());
+		frustum.setTransform(getWorldTransform());
 		viewMat = Mat4(getWorldTransform().getInverse());
 	}
 	/// @}
@@ -273,11 +267,11 @@ private:
 	ReadWriteProperty<float>* fovProp; ///< Have it here for updates
 	ReadWriteProperty<float>* distProp; ///< Have it here for updates
 
-	void updateZFar(const float& f)
+	void updateFar(const float& f)
 	{
 		frustum.setFar(f);
 	}
-	ANKI_SLOT(updateZFar, const float&)
+	ANKI_SLOT(updateFar, const float&)
 
 	void updateFov(const float& f)
 	{

+ 37 - 59
shaders/IsLpGeneric.glsl

@@ -12,22 +12,27 @@
 
 /// @name Uniforms
 /// @{
-uniform vec2 planes; ///< for the calculation of frag pos in view space
-/// for the calculation of frag pos in view space
-uniform vec2 limitsOfNearPlane; 
-/// This is an optimization see PpsSsao.glsl and r403 for the clean one
-uniform vec2 limitsOfNearPlane2; 
-uniform float zNear; ///< for the calculation of frag pos in view space
-
-uniform sampler2D msNormalFai, msDiffuseFai, msSpecularFai, msDepthFai;
-uniform vec3 lightPos; ///< Light pos in eye space
-uniform float lightRadius;
-uniform vec3 lightDiffuseCol;
-uniform vec3 lightSpecularCol;
+
+/// Watch the placement
+layout(std140) uniform uniforms
+{
+	uniform vec2 planes; ///< for the calculation of frag pos in view space
+	/// for the calculation of frag pos in view space
+	uniform vec2 limitsOfNearPlane; 
+	/// This is an optimization see PpsSsao.glsl and r403 for the clean one
+	uniform vec2 limitsOfNearPlane2; 
+	uniform float zNear; ///< for the calculation of frag pos in view space
+	uniform float lightRadius;
+	uniform float shadowMapSize;
+	uniform vec3 lightPos; ///< Light pos in eye space
+	uniform vec3 lightDiffuseCol;
+	uniform vec3 lightSpecularCol;
+	uniform mat4 texProjectionMat;
+};
+
+uniform usampler2D msFai0, msDepthFai;
 uniform sampler2D lightTex;
-uniform mat4 texProjectionMat;
 uniform sampler2DShadow shadowMap;
-uniform float shadowMapSize;
 /// @}
 
 /// @name Varyings
@@ -40,12 +45,6 @@ in vec2 vTexCoords;
 out vec3 fColor;
 /// @}
 
-
-const float MAX_SHININESS = 128.0;
-
-
-//==============================================================================
-// getFragPosVSpace                                                            =
 //==============================================================================
 /// @return frag pos in view space
 vec3 getFragPosVSpace()
@@ -68,9 +67,6 @@ vec3 getFragPosVSpace()
 	return fragPosVspace;
 }
 
-
-//==============================================================================
-// getAttenuation                                                              =
 //==============================================================================
 /// @return The attenuation factor given the distance from the frag to the
 /// light source
@@ -80,12 +76,7 @@ float getAttenuation(in float fragLightDist)
 	//return 1.0 - fragLightDist * _inv_light_radius;
 }
 
-
 //==============================================================================
-// pcfLow                                                                      =
-//==============================================================================
-#if defined(SPOT_LIGHT_ENABLED) && defined(SHADOW_ENABLED)
-
 /// @return The blurred shadow
 float pcfLow(in vec3 shadowUv)
 {
@@ -114,11 +105,6 @@ float pcfLow(in vec3 shadowUv)
 	return shadowCol;
 }
 
-#endif
-
-
-//==============================================================================
-// doPhong                                                                     =
 //==============================================================================
 /// Performs phong lighting using the MS FAIs and a few other things
 /// @param fragPosVspace The fragment position in view space
@@ -134,8 +120,11 @@ vec3 doPhong(in vec3 fragPosVspace, out float fragLightDist)
 	fragLightDist = dot(frag2LightVec, frag2LightVec);
 	vec3 lightDir = frag2LightVec * inversesqrt(fragLightDist);
 
+	// Read diffuse, normal and the rest from MS
+	uvec2 msAll = texture(msFai0, vTexCoords).rg;
+
 	// Read the normal
-	vec3 normal = unpackNormal(texture(msNormalFai, vTexCoords).rg);
+	vec3 normal = unpackNormal(unpackHalf2x16(msAll[1]));
 
 	// Lambert term
 	float lambertTerm = dot(normal, lightDir);
@@ -146,29 +135,24 @@ vec3 doPhong(in vec3 fragPosVspace, out float fragLightDist)
 	}*/
 	lambertTerm = max(0.0, lambertTerm);
 
-	// Diffuce
-	vec3 diffuse = texture(msDiffuseFai, vTexCoords).rgb;
-	diffuse *= lightDiffuseCol;
-	vec3 color = diffuse * lambertTerm;
+	// Diffuse
+	vec4 diffuseAndSpec = unpackUnorm4x8(msAll[0]);
+	diffuseAndSpec.rgb *= lightDiffuseCol;
+	vec3 color = diffuseAndSpec.rgb * lambertTerm;
 
 	// Specular
-	vec4 specularMix = texture(msSpecularFai, vTexCoords); // the MS specular
-	                                      // FAI has the color and the shininess
-	vec3 specular = specularMix.xyz;
-	float shininess = specularMix.w * MAX_SHININESS;
+	vec2 specularAll = unpackSpecular(diffuseAndSpec.a);
 
 	vec3 _eyeVec_ = normalize(-fragPosVspace);
 	vec3 h = normalize(lightDir + _eyeVec_);
-	float specIntensity = pow(max(0.0, dot(normal, h)), shininess);
-	color += specular * lightSpecularCol * (specIntensity * lambertTerm);
+	float specIntensity = pow(max(0.0, dot(normal, h)), specularAll.g);
+	color += vec3(specularAll.r) * lightSpecularCol 
+		* (specIntensity * lambertTerm);
 	
 	// end
 	return color;
 }
 
-
-//==============================================================================
-// doPointLight                                                                =
 //==============================================================================
 vec3 doPointLight(in vec3 fragPosVspace)
 {
@@ -181,9 +165,6 @@ vec3 doPointLight(in vec3 fragPosVspace)
 	return ret;
 }
 
-
-//==============================================================================
-// doSpotLight                                                                 =
 //==============================================================================
 vec3 doSpotLight(in vec3 fragPosVspace)
 {
@@ -198,8 +179,8 @@ vec3 doSpotLight(in vec3 fragPosVspace)
 	   texCoords2.w < lightRadius)
 	{
 		// Get shadow
-#if defined(SHADOW_ENABLED)
-#	if defined(PCF_ENABLED)
+#if defined(SHADOW)
+#	if defined(PCF)
 		float shadowCol = pcfLow(texCoords3);
 #	else
 		float shadowCol = texture(shadowMap, texCoords3);
@@ -217,7 +198,7 @@ vec3 doSpotLight(in vec3 fragPosVspace)
 		vec3 lightTexCol = textureProj(lightTex, texCoords2.xyz).rgb;
 		float att = getAttenuation(fragLightDist);
 
-#if defined(SHADOW_ENABLED)
+#if defined(SHADOW)
 		return lightTexCol * color * (shadowCol * att);
 #else
 		return lightTexCol * color * att;
@@ -229,18 +210,15 @@ vec3 doSpotLight(in vec3 fragPosVspace)
 	}
 }
 
-
-//==============================================================================
-// main                                                                        =
 //==============================================================================
 void main()
 {
 	// get frag pos in view space
 	vec3 fragPosVspace = getFragPosVSpace();
 
-#if defined(POINT_LIGHT_ENABLED)
+#if defined(POINT_LIGHT)
 	fColor = doPointLight(fragPosVspace);
-#elif defined(SPOT_LIGHT_ENABLED)
+#elif defined(SPOT_LIGHT)
 	fColor = doSpotLight(fragPosVspace);
 #endif // spot light
 
@@ -248,7 +226,7 @@ void main()
 	//fColor = fColor - fColor + vec3(1, 0, 1);
 
 	//gl_FragData[0] = gl_FragData[0] - gl_FragData[0] + vec4(1, 0, 1, 1);
-	/*#if defined(SPOT_LIGHT_ENABLED)
+	/*#if defined(SPOT_LIGHT)
 	fColor = fColor - fColor + vec3(1, 0, 1);
 	//gl_FragData[0] = vec4(texture(msDepthFai, vTexCoords).rg), 1.0);
 	#endif*/

+ 1 - 0
shaders/MaterialFragmentVariables.glsl

@@ -27,6 +27,7 @@ flat in float vSpecularComponent;
 
 /// @name Fragment out
 /// @{
+#if defined(PASS_COLOR)
 layout(location = 0) out uvec2 fMsFai0;
 #	define fMsFai0_DEFINED
 #endif

+ 2 - 0
shaders/MaterialVertex.glsl

@@ -59,5 +59,7 @@ void doVertex()
 void doVertexPrepackSpecular(in vec2 specular)
 {
 	doVertex();
+#if defined(PASS_COLOR)
 	vSpecularComponent = packSpecular(specular);
+#endif
 }

+ 17 - 7
src/gl/ShaderProgram.cpp

@@ -45,7 +45,8 @@ ShaderProgramVariable::ShaderProgramVariable(
 //==============================================================================
 void ShaderProgramUniformVariable::doCommonSetCode() const
 {
-	ANKI_ASSERT(getLocation() != -1);
+	ANKI_ASSERT(getLocation() != -1
+		&& "You cannot set variable in uniform block");
 	ANKI_ASSERT(ShaderProgram::getCurrentProgramGlId() == 
 		getFatherShaderProgram().getGlId());
 
@@ -248,6 +249,7 @@ void ShaderProgram::create(const char* vertSource, const char* tcSource,
 	// init the rest
 	bind();
 	getUniAndAttribVars();
+	initUniformBlocks();
 }
 
 //==============================================================================
@@ -412,12 +414,8 @@ void ShaderProgram::getUniAndAttribVars()
 			&size, &type, &name_[0]);
 		name_[length] = '\0';
 
-		// check if its FFP location
-		int loc = glGetUniformLocation(glId, &name_[0]);
-		if(loc == -1) // if -1 it means that its an FFP var
-		{
-			throw ANKI_EXCEPTION("You are using FFP vertex uniforms");
-		}
+		// -1 means in uniform block
+		GLint loc = glGetUniformLocation(glId, &name_[0]);
 
 		ShaderProgramUniformVariable* var =
 			new ShaderProgramUniformVariable(loc, &name_[0], type, 
@@ -434,6 +432,18 @@ void ShaderProgram::getUniAndAttribVars()
 	attribs.shrink_to_fit();
 }
 
+//==============================================================================
+void ShaderProgram::initUniformBlocks()
+{
+	GLint blocksCount;
+	glGetProgramiv(glId, GL_ACTIVE_UNIFORM_BLOCKS, &blocksCount);
+
+	for(GLint i = 0; i < blocksCount; i++)
+	{
+
+	}
+}
+
 //==============================================================================
 const ShaderProgramVariable* ShaderProgram::findVariableByName(
 	const char* name) const

+ 27 - 1
src/renderer/Is.cpp

@@ -17,6 +17,32 @@ Is::~Is()
 //==============================================================================
 void Is::init(const RendererInitializer& /*initializer*/)
 {
+	// Load the passes
+	//
+	// XXX
+
+	// Load the programs
+	//
+
+	// Ambient pass
+	ambientPassSProg.load("shaders/IsAp.glsl");
+
+	// point light
+	pointLightSProg.load(ShaderProgramResource::createSrcCodeToCache(
+		"shaders/IsLpGeneric.glsl", "#define POINT_LIGHT 1\n").c_str());
+
+	// spot light no shadow
+	spotLightNoShadowSProg.load(ShaderProgramResource::createSrcCodeToCache(
+		"shaders/IsLpGeneric.glsl", "#define SPOT_LIGHT 1\n").c_str());
+
+	// spot light w/t shadow
+	std::string pps = std::string("#define SPOT_LIGHT 1\n#define SHADOW 1\n");
+	if(/*sm.isPcfEnabled()*/ 1) // XXX
+	{
+		pps += "#define PCF 1\n";
+	}
+	spotLightShadowSProg.load(ShaderProgramResource::createSrcCodeToCache(
+		"shaders/IsLpGeneric.glsl", pps.c_str()).c_str());
 }
 
 //==============================================================================
@@ -25,4 +51,4 @@ void Is::run()
 	/// TODO
 }
 
-} // end namespace
+} // end namespace anki

+ 3 - 3
src/scene/Light.cpp

@@ -52,7 +52,7 @@ PointLight::PointLight(const char* name, Scene* scene,
 SpotLight::SpotLight(const char* name, Scene* scene,
 	uint32_t movableFlags, Movable* movParent)
 	: Light(LT_SPOT, name, scene, movableFlags, movParent, &frustum),
-		Frustumable(&frustum)
+		PerspectiveFrustumable(&frustum)
 {
 	// Fov
 	//
@@ -66,11 +66,11 @@ SpotLight::SpotLight(const char* name, Scene* scene,
 	float dist = 10.0;
 	distProp = new ReadWriteProperty<float>("distance", dist);
 	addNewProperty(distProp);
-	ANKI_CONNECT(distProp, valueChanged, this, updateZFar);
+	ANKI_CONNECT(distProp, valueChanged, this, updateFar);
 
 	// Fix frustum
 	//
 	frustum.setAll(ang, ang, 0.1, dist);
 }
 
-} // end namespace
+} // end namespace anki

+ 7 - 2
testapp/Main.cpp

@@ -115,6 +115,10 @@ void mainLoopExtra()
 	{
 		mover = SceneSingleton::get().findSceneNode("ccam")->getMovable();
 	}
+	if(in.getKey(SDL_SCANCODE_3))
+	{
+		mover = SceneSingleton::get().findSceneNode("spot0")->getMovable();
+	}
 
 	if(in.getKey(SDL_SCANCODE_UP)) mover->rotateLocalX(ang);
 	if(in.getKey(SDL_SCANCODE_DOWN)) mover->rotateLocalX(-ang);
@@ -232,7 +236,7 @@ void main()
 
 		// Sleep
 		//
-#if 0
+#if 1
 		timer.stop();
 		if(timer.getElapsedTime() < AppSingleton::get().getTimerTick())
 		{
@@ -248,7 +252,8 @@ void main()
 
 	}
 
-	ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime() << " sec)");
+	ANKI_LOGI("Exiting main loop (" << mainLoopTimer.getElapsedTime()
+		<< " sec)");
 }