Browse Source

IS refactoring. There is a UBO bug still

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
abca952b8a

+ 0 - 1
include/anki/Collision.h

@@ -10,7 +10,6 @@
 #include "anki/collision/LineSegment.h"
 #include "anki/collision/LineSegment.h"
 #include "anki/collision/Frustum.h"
 #include "anki/collision/Frustum.h"
 #include "anki/collision/Aabb.h"
 #include "anki/collision/Aabb.h"
-#include "anki/collision/CollisionPoint.h"
 
 
 #include "anki/collision/Functions.h"
 #include "anki/collision/Functions.h"
 
 

+ 2 - 0
include/anki/gl/BufferObject.h

@@ -178,8 +178,10 @@ public:
 		(void)correctTarget;
 		(void)correctTarget;
 
 
 		ANKI_ASSERT(offset + size <= sizeInBytes);
 		ANKI_ASSERT(offset + size <= sizeInBytes);
+		ANKI_ASSERT(size > 0);
 
 
 		glBindBufferRange(target, binding, glId, offset, size);
 		glBindBufferRange(target, binding, glId, offset, size);
+		ANKI_CHECK_GL_ERROR();
 	}
 	}
 
 
 private:
 private:

+ 1 - 0
include/anki/gl/GlObject.h

@@ -2,6 +2,7 @@
 #define ANKI_GL_GL_OBJECT_H
 #define ANKI_GL_GL_OBJECT_H
 
 
 #include "anki/gl/Ogl.h"
 #include "anki/gl/Ogl.h"
+#include "anki/gl/GlException.h"
 #include "anki/util/NonCopyable.h"
 #include "anki/util/NonCopyable.h"
 #include "anki/util/Assert.h"
 #include "anki/util/Assert.h"
 #include "anki/util/StdTypes.h"
 #include "anki/util/StdTypes.h"

+ 4 - 4
include/anki/renderer/Is.h

@@ -23,10 +23,7 @@ class SpotLight;
 /// Illumination stage
 /// Illumination stage
 class Is: private RenderingPass
 class Is: private RenderingPass
 {
 {
-	friend struct WritePointLightsUbo;
-	friend struct WriteSpotLightsUbo;
-	friend struct WriteTilesUboJob;
-	friend struct UpdateTilesJob;
+	friend struct WriteLightsJob;
 
 
 public:
 public:
 	Is(Renderer* r);
 	Is(Renderer* r);
@@ -102,6 +99,9 @@ private:
 
 
 	// Do the actual pass
 	// Do the actual pass
 	void lightPass();
 	void lightPass();
+
+	// Prepare GL for rendering
+	void setState();
 };
 };
 
 
 } // end namespace anki
 } // end namespace anki

+ 2 - 1
include/anki/renderer/Tiler.h

@@ -23,7 +23,8 @@ class Tiler
 	friend struct UpdatePlanesPerspectiveCameraJob;
 	friend struct UpdatePlanesPerspectiveCameraJob;
 
 
 public:
 public:
-	typedef std::bitset<ANKI_RENDERER_TILES_COUNT> Bitset;
+	typedef std::bitset<
+		ANKI_RENDERER_TILES_X_COUNT * ANKI_RENDERER_TILES_Y_COUNT> Bitset;
 
 
 	Tiler();
 	Tiler();
 	~Tiler();
 	~Tiler();

+ 3 - 9
shaders/IsCommon.glsl

@@ -11,8 +11,8 @@ struct Tilegrid
 {
 {
 	Plane planesX[TILES_X_COUNT - 1];
 	Plane planesX[TILES_X_COUNT - 1];
 	Plane planesY[TILES_Y_COUNT - 1];
 	Plane planesY[TILES_Y_COUNT - 1];
-	Plane planesNear[TILES_Y_COUNT][TILES_X_COUNT];
-	Plane planesFar[TILES_Y_COUNT][TILES_X_COUNT];
+	Plane planesNear[TILES_COUNT];
+	Plane planesFar[TILES_COUNT];
 };
 };
 
 
 // Representation of a tile
 // Representation of a tile
@@ -30,12 +30,6 @@ struct Tile
 #endif
 #endif
 };
 };
 
 
-// A container of tiles
-struct Tiles
-{
-	Tile tiles[TILES_Y_COUNT][TILES_X_COUNT];
-};
-
 // The base of all lights
 // The base of all lights
 struct Light
 struct Light
 {
 {
@@ -59,7 +53,7 @@ struct SpotLight
 // Spot light with texture
 // Spot light with texture
 struct SpotTexLight
 struct SpotTexLight
 {
 {
-	SpotLight spotLightbase;
+	SpotLight spotLightBase;
 	mat4 texProjectionMat;
 	mat4 texProjectionMat;
 };
 };
 
 

+ 49 - 48
shaders/IsLp.glsl

@@ -39,6 +39,7 @@ void main()
 #pragma anki include "shaders/CommonFrag.glsl"
 #pragma anki include "shaders/CommonFrag.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/Pack.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
 #pragma anki include "shaders/LinearDepth.glsl"
+#pragma anki include "shaders/IsCommon.glsl"
 
 
 #define ATTENUATION_FINE 0
 #define ATTENUATION_FINE 0
 
 
@@ -61,41 +62,29 @@ layout(std140, row_major) uniform commonBlock
 
 
 #define planes planes_.xy
 #define planes planes_.xy
 
 
-struct Light
+layout(std140) uniform pointLightsBlock
 {
 {
-	vec4 posRadius; ///< xyz: Light pos in eye space. w: The radius
-	vec4 diffuseColorShadowmapId;
-	vec4 specularColorTexId;
-};
-
-struct SpotLight
-{
-	Light light;
-	vec4 lightDirection;
-	vec4 outerCosInnerCos;
-	mat4 texProjectionMat;
-	vec4 extendPoints[4];
-};
-
-layout(std140, row_major) uniform pointLightsBlock
-{
-	Light plights[MAX_POINT_LIGHTS];
+	Light pointLights[MAX_POINT_LIGHTS];
 };
 };
 
 
 layout(std140) uniform spotLightsBlock
 layout(std140) uniform spotLightsBlock
 {
 {
-	SpotLight slights[MAX_SPOT_LIGHTS];
+	SpotLight spotLights[MAX_SPOT_LIGHTS];
 };
 };
 
 
-struct Tile
+layout(std140) uniform spotTexLightsBlock
 {
 {
-	uvec4 lightsCount;
-	uvec4 lightIndices[MAX_LIGHTS_PER_TILE / 4];
+	SpotTexLight spotTexLights[MAX_SPOT_TEX_LIGHTS];
 };
 };
 
 
-layout(std140, row_major) uniform tilesBlock
+#if __VERSION__ > 430
+layout(std430) 
+#else
+layout(std140) 
+#endif
+	uniform tilesBlock
 {
 {
-	Tile tiles[TILES_X_COUNT * TILES_Y_COUNT];
+	Tile tiles[TILES_COUNT];
 };
 };
 
 
 uniform highp usampler2D msFai0;
 uniform highp usampler2D msFai0;
@@ -180,9 +169,9 @@ vec3 calcPhong(in vec3 fragPosVspace, in vec3 diffuse,
 //==============================================================================
 //==============================================================================
 float calcSpotFactor(in SpotLight light, in vec3 fragPosVspace)
 float calcSpotFactor(in SpotLight light, in vec3 fragPosVspace)
 {
 {
-	vec3 l = normalize(fragPosVspace - light.light.posRadius.xyz);
+	vec3 l = normalize(fragPosVspace - light.lightBase.posRadius.xyz);
 
 
-	float costheta = dot(l, light.lightDirection.xyz);
+	float costheta = dot(l, light.lightDir.xyz);
 	float spotFactor = smoothstep(
 	float spotFactor = smoothstep(
 		light.outerCosInnerCos.x, 
 		light.outerCosInnerCos.x, 
 		light.outerCosInnerCos.y, 
 		light.outerCosInnerCos.y, 
@@ -211,7 +200,7 @@ float pcfLow(in sampler2DArrayShadow shadowMap, in vec3 shadowUv)
 #endif
 #endif
 
 
 //==============================================================================
 //==============================================================================
-float calcShadowFactor(in SpotLight light, in vec3 fragPosVspace, 
+float calcShadowFactor(in SpotTexLight light, in vec3 fragPosVspace, 
 	in mediump sampler2DArrayShadow shadowMapArr, in float layer)
 	in mediump sampler2DArrayShadow shadowMapArr, in float layer)
 {
 {
 	vec4 texCoords4 = light.texProjectionMat * vec4(fragPosVspace, 1.0);
 	vec4 texCoords4 = light.texProjectionMat * vec4(fragPosVspace, 1.0);
@@ -248,69 +237,81 @@ void main()
 	uint pointLightsCount = tiles[vInstanceId].lightsCount[0];
 	uint pointLightsCount = tiles[vInstanceId].lightsCount[0];
 	for(uint i = 0U; i < pointLightsCount; ++i)
 	for(uint i = 0U; i < pointLightsCount; ++i)
 	{
 	{
-		uint lightId = tiles[vInstanceId].lightIndices[i / 4U][i % 4U];
+#if __VERSION__ > 430
+		uint lightId = tiles[vInstanceId].pointLightIndices[i];
+#else
+		uint lightId = tiles[vInstanceId].pointLightIndices[i / 4U][i % 4U];
+#endif
+		PointLight light = pointLights[lightId];
 
 
 		vec3 ray;
 		vec3 ray;
-		float att = calcAttenuationFactor(fragPosVspace, 
-			plights[lightId], ray);
+		float att = calcAttenuationFactor(fragPosVspace, light, ray);
 
 
 		float lambert = calcLambertTerm(normal, ray);
 		float lambert = calcLambertTerm(normal, ray);
 
 
 		fColor += calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
 		fColor += calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-			specularAll, normal, plights[lightId], ray) * (att * lambert);
+			specularAll, normal, light, ray) * (att * lambert);
 	}
 	}
 
 
 	// Spot lights
 	// Spot lights
-	uint spotLightsCount = tiles[vInstanceId].lightsCount[1];
+	uint spotLightsCount = tiles[vInstanceId].lightsCount[2];
 
 
-	uint opt = pointLightsCount + spotLightsCount;
-	for(uint i = pointLightsCount; i < opt; ++i)
+	for(uint i = 0U; i < spotLightsCount; ++i)
 	{
 	{
-		uint lightId = tiles[vInstanceId].lightIndices[i / 4U][i % 4U];
+#if __VERSION__ > 430
+		uint lightId = tiles[vInstanceId].spotLightIndices[i];
+#else
+		uint lightId = tiles[vInstanceId].spotLightIndices[i / 4U][i % 4U];
+#endif
+		SpotLight light = spotLights[lightId];
 
 
 		vec3 ray;
 		vec3 ray;
 		float att = calcAttenuationFactor(fragPosVspace, 
 		float att = calcAttenuationFactor(fragPosVspace, 
-			slights[lightId].light, ray);
+			light.lightBase, ray);
 
 
 		float lambert = calcLambertTerm(normal, ray);
 		float lambert = calcLambertTerm(normal, ray);
 
 
-		float spot = calcSpotFactor(slights[lightId], fragPosVspace);
+		float spot = calcSpotFactor(light, fragPosVspace);
 
 
 		vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
 		vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-			specularAll, normal, slights[lightId].light, ray);
+			specularAll, normal, light.lightBase, ray);
 
 
 		fColor += col * (att * lambert * spot);
 		fColor += col * (att * lambert * spot);
 	}
 	}
 
 
 	// Spot lights with shadow
 	// Spot lights with shadow
-	uint spotLightsShadowCount = tiles[vInstanceId].lightsCount[2];
-	opt = pointLightsCount + spotLightsCount;
+	uint spotTexLightsCount = tiles[vInstanceId].lightsCount[3];
 
 
-	for(uint i = 0U; i < spotLightsShadowCount; ++i)
+	for(uint i = 0U; i < spotTexLightsCount; ++i)
 	{
 	{
-		uint id = i + opt;
-		uint lightId = tiles[vInstanceId].lightIndices[id / 4U][id % 4U];
+#if __VERSION__ > 430
+		uint lightId = tiles[vInstanceId].spotTexLightIndices[i];
+#else
+		uint lightId = tiles[vInstanceId].spotTexLightIndices[i / 4U][i % 4U];
+#endif
+		SpotTexLight light = spotTexLights[lightId];
 
 
 		vec3 ray;
 		vec3 ray;
 		float att = calcAttenuationFactor(fragPosVspace, 
 		float att = calcAttenuationFactor(fragPosVspace, 
-			slights[lightId].light, ray);
+			light.spotLightBase.lightBase, ray);
 
 
 		float lambert = calcLambertTerm(normal, ray);
 		float lambert = calcLambertTerm(normal, ray);
 
 
-		float spot = calcSpotFactor(slights[lightId], fragPosVspace);
+		float spot = 
+			calcSpotFactor(light.spotLightBase, fragPosVspace);
 
 
 		float midFactor = att * lambert * spot;
 		float midFactor = att * lambert * spot;
 
 
 		//if(midFactor > 0.0)
 		//if(midFactor > 0.0)
 		{
 		{
 			float shadowmapLayerId = 
 			float shadowmapLayerId = 
-				slights[lightId].light.diffuseColorShadowmapId.w;
+				light.spotLightBase.lightBase.diffuseColorShadowmapId.w;
 
 
-			float shadow = calcShadowFactor(slights[lightId], 
+			float shadow = calcShadowFactor(light, 
 				fragPosVspace, shadowMapArr, shadowmapLayerId);
 				fragPosVspace, shadowMapArr, shadowmapLayerId);
 
 
 			vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
 			vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-				specularAll, normal, slights[lightId].light, ray);
+				specularAll, normal, light.spotLightBase.lightBase, ray);
 
 
 			fColor += col * (midFactor * shadow);
 			fColor += col * (midFactor * shadow);
 		}
 		}

+ 1 - 0
src/core/App.cpp

@@ -90,6 +90,7 @@ void App::init(int argc, char* argv[])
 	// Install signal handlers
 	// Install signal handlers
 	signal(SIGSEGV, handler);
 	signal(SIGSEGV, handler);
 	signal(SIGBUS, handler);
 	signal(SIGBUS, handler);
+	signal(SIGFPE, handler);
 
 
 	// send output to handleMessageHanlderMsgs
 	// send output to handleMessageHanlderMsgs
 	ANKI_CONNECT(&LoggerSingleton::get(), messageRecieved, 
 	ANKI_CONNECT(&LoggerSingleton::get(), messageRecieved, 

+ 1 - 0
src/gl/BufferObject.cpp

@@ -2,6 +2,7 @@
 #include "anki/gl/BufferObject.h"
 #include "anki/gl/BufferObject.h"
 #include "anki/gl/GlException.h"
 #include "anki/gl/GlException.h"
 #include "anki/util/Exception.h"
 #include "anki/util/Exception.h"
+#include "anki/core/Logger.h"
 
 
 /// Instead of map/unmap use glBufferSubData() when writing to the whole buffer
 /// Instead of map/unmap use glBufferSubData() when writing to the whole buffer
 #define USE_BUFFER_DATA_ON_WRITE 1
 #define USE_BUFFER_DATA_ON_WRITE 1

+ 183 - 126
src/renderer/Is.cpp

@@ -54,23 +54,23 @@ struct Plane
 
 
 struct Tilegrid
 struct Tilegrid
 {
 {
-	Plane planesX[Is::TILES_X_COUNT - 1];
-	Plane planesY[Is::TILES_Y_COUNT - 1];
-	Plane planesNear[Is::TILES_Y_COUNT][Is::TILES_X_COUNT];
-	Plane planesFar[Is::TILES_Y_COUNT][Is::TILES_X_COUNT];
+	Plane planesX[TILES_X_COUNT - 1];
+	Plane planesY[TILES_Y_COUNT - 1];
+	Plane planesNear[TILES_Y_COUNT][TILES_X_COUNT];
+	Plane planesFar[TILES_Y_COUNT][TILES_X_COUNT];
 };
 };
 
 
 struct Tile
 struct Tile
 {
 {
-	U32 lightsCount[4]; 
-	U32 pointLightIndices[Is::MAX_POINT_LIGHTS_PER_TILE];
-	U32 spotLightIndices[Is::MAX_SPOT_LIGHTS_PER_TILE];
-	U32 spotTexLightndices[Is::MAX_SPOT_TEX_LIGHTS_PER_TILE];
+	Array<U32, 4> lightsCount; 
+	Array<U32, MAX_POINT_LIGHTS_PER_TILE> pointLightIndices;
+	Array<U32, MAX_SPOT_LIGHTS_PER_TILE> spotLightIndices;
+	Array<U32, MAX_SPOT_TEX_LIGHTS_PER_TILE> spotTexLightIndices;
 };
 };
 
 
 struct Tiles
 struct Tiles
 {
 {
-	Tile tiles[Is::TILES_Y_COUNT][Is::TILES_X_COUNT];
+	Tile tiles[TILES_Y_COUNT][TILES_X_COUNT];
 };
 };
 
 
 struct Light
 struct Light
@@ -87,7 +87,7 @@ struct SpotLight: Light
 {
 {
 	Vec4 lightDir;
 	Vec4 lightDir;
 	Vec4 outerCosInnerCos;
 	Vec4 outerCosInnerCos;
-	Vec4 extendPoints[4]; 
+	Array<Vec4, 4> extendPoints; 
 };
 };
 
 
 struct SpotTexLight: SpotLight
 struct SpotTexLight: SpotLight
@@ -105,9 +105,9 @@ struct CommonUniforms
 } // end namespace shader
 } // end namespace shader
 
 
 static const PtrSize LIGHTS_UBO_SIZE = 
 static const PtrSize LIGHTS_UBO_SIZE = 
-		MAX_POINT_LIGHTS * sizeof(shader::PointLight)
-		+ MAX_SPOT_LIGHTS * sizeof(shader::SpotLight)
-		+ MAX_SPOT_TEX_LIGHTS * sizeof(shader::SpotTexLight);
+	MAX_POINT_LIGHTS * sizeof(shader::PointLight)
+	+ MAX_SPOT_LIGHTS * sizeof(shader::SpotLight)
+	+ MAX_SPOT_TEX_LIGHTS * sizeof(shader::SpotTexLight);
 
 
 //==============================================================================
 //==============================================================================
 
 
@@ -119,21 +119,22 @@ struct WriteLightsJob: ThreadJob
 	shader::PointLight* pointLights = nullptr;
 	shader::PointLight* pointLights = nullptr;
 	shader::SpotLight* spotLights = nullptr;
 	shader::SpotLight* spotLights = nullptr;
 	shader::SpotTexLight* spotTexLights = nullptr;
 	shader::SpotTexLight* spotTexLights = nullptr;
+
 	shader::Tiles* tiles = nullptr;
 	shader::Tiles* tiles = nullptr;
 
 
-	SceneVector<Light*>::iterator lightsBegin; // XXX Make them const and ShaderNode
-	SceneVector<Light*>::iterator lightsEnd;
+	VisibilityTestResults::Container::const_iterator lightsBegin;
+	VisibilityTestResults::Container::const_iterator lightsEnd;
 
 
 	std::atomic<U32>* pointLightsCount = nullptr;
 	std::atomic<U32>* pointLightsCount = nullptr;
 	std::atomic<U32>* spotLightsCount = nullptr;
 	std::atomic<U32>* spotLightsCount = nullptr;
 	std::atomic<U32>* spotTexLightsCount = nullptr;
 	std::atomic<U32>* spotTexLightsCount = nullptr;
 		
 		
 	std::atomic<U32> 
 	std::atomic<U32> 
-		(*tilePointLightsCount)[TILES_Y_COUNT][TILES_X_COUNT];
+		(*tilePointLightsCount)[TILES_Y_COUNT][TILES_X_COUNT] = nullptr;
 	std::atomic<U32> 
 	std::atomic<U32> 
-		(*tileSpotLightsCount)[TILES_Y_COUNT][TILES_X_COUNT];
+		(*tileSpotLightsCount)[TILES_Y_COUNT][TILES_X_COUNT] = nullptr;
 	std::atomic<U32> 
 	std::atomic<U32> 
-		(*tileSpotTexLightsCount)[TILES_Y_COUNT][TILES_X_COUNT];
+		(*tileSpotTexLightsCount)[TILES_Y_COUNT][TILES_X_COUNT] = nullptr;
 
 
 	Tiler* tiler = nullptr;
 	Tiler* tiler = nullptr;
 	Is* is = nullptr;
 	Is* is = nullptr;
@@ -149,28 +150,52 @@ struct WriteLightsJob: ThreadJob
 		// Run all lights
 		// Run all lights
 		for(U64 i = start; i < end; i++)
 		for(U64 i = start; i < end; i++)
 		{
 		{
-			Light* light = lightsBegin + i;
+			const SceneNode* snode = *(lightsBegin + i);
+			const Light* light = snode->getLight();
+			ANKI_ASSERT(light);
 
 
 			switch(light->getLightType())
 			switch(light->getLightType())
 			{
 			{
 			case Light::LT_POINT:
 			case Light::LT_POINT:
-				doLight(*static_cast<PointLight*>(light));
+				{
+					const PointLight& l = 
+						*static_cast<const PointLight*>(light);
+					I pos = doLight(l);
+					if(pos != -1)
+					{
+						binLight(l, pos);
+					}
+				}
 				break;
 				break;
 			case Light::LT_SPOT:
 			case Light::LT_SPOT:
-				doLight(*static_cast<SpotLight*>(light));
+				{
+					const SpotLight& l = 
+						*static_cast<const SpotLight*>(light);
+					I pos = doLight(l);
+					if(pos != -1)
+					{
+						binLight(l, pos);
+					}
+				}
+				break;
+			default:
+				ANKI_ASSERT(0);
 				break;
 				break;
 			}
 			}
 		}
 		}
 	}
 	}
 
 
 	/// Copy CPU light to GPU buffer
 	/// Copy CPU light to GPU buffer
-	U doLight(PointLight& light)
+	I doLight(const PointLight& light)
 	{
 	{
 		// Get GPU light
 		// Get GPU light
-		U32 pos = pointLightsCount->fetch_add(1);
-		ANKI_ASSERT(pos < MAX_POINT_LIGHTS);
+		I i = pointLightsCount->fetch_add(1);
+		if(i >= (I)MAX_POINT_LIGHTS)
+		{
+			return -1;
+		}
 
 
-		shader::PointLight& slight = pointLights[pos];
+		shader::PointLight& slight = pointLights[i];
 
 
 		const Camera* cam = is->cam;
 		const Camera* cam = is->cam;
 		ANKI_ASSERT(cam);
 		ANKI_ASSERT(cam);
@@ -182,24 +207,28 @@ struct WriteLightsJob: ThreadJob
 		slight.diffuseColorShadowmapId = light.getDiffuseColor();
 		slight.diffuseColorShadowmapId = light.getDiffuseColor();
 		slight.specularColorTexId = light.getSpecularColor();
 		slight.specularColorTexId = light.getSpecularColor();
 
 
-		return pos;
+		return i;
 	}
 	}
 
 
 	/// Copy CPU spot light to GPU buffer
 	/// Copy CPU spot light to GPU buffer
-	U doLight(SpotLight& light)
+	I doLight(const SpotLight& light)
 	{
 	{
+		const Camera* cam = is->cam;
 		Bool isTexLight = light.getShadowEnabled();
 		Bool isTexLight = light.getShadowEnabled();
-
-		U32 pos;
+		I i;
 		shader::SpotLight* baseslight = nullptr;
 		shader::SpotLight* baseslight = nullptr;
+
 		if(isTexLight)
 		if(isTexLight)
 		{
 		{
 			// Spot tex light
 			// Spot tex light
 
 
-			pos = spotTexLightsCount->fetch_add(1);
-			ANKI_ASSERT(pos < MAX_SPOT_TEX_LIGHTS);
+			i = spotTexLightsCount->fetch_add(1);
+			if(i >= (I)MAX_SPOT_TEX_LIGHTS)
+			{
+				return -1;
+			}
 
 
-			shader::SpotTexLight& slight = spotTexLights[pos];
+			shader::SpotTexLight& slight = spotTexLights[i];
 			baseslight = &slight;
 			baseslight = &slight;
 
 
 			// Write matrix
 			// Write matrix
@@ -215,18 +244,18 @@ struct WriteLightsJob: ThreadJob
 
 
 			// Transpose because of driver bug
 			// Transpose because of driver bug
 			slight.texProjectionMat.transpose();
 			slight.texProjectionMat.transpose();
-
-			// Write tex IDs
-			slight.diffuseColorShadowmapId.w() = pos;
 		}
 		}
 		else
 		else
 		{
 		{
 			// Spot light without texture
 			// Spot light without texture
 
 
-			pos = spotLightsCount->fetch_add(1);
-			ANKI_ASSERT(pos < MAX_SPOT_LIGHTS);
+			i = spotLightsCount->fetch_add(1);
+			if(i >= (I)MAX_SPOT_LIGHTS)
+			{
+				return -1;
+			}
 
 
-			shader::SpotLight& slight = spotLights[pos];
+			shader::SpotLight& slight = spotLights[i];
 			baseslight = &slight;
 			baseslight = &slight;
 		}
 		}
 
 
@@ -238,13 +267,11 @@ struct WriteLightsJob: ThreadJob
 				cam->getViewMatrix());
 				cam->getViewMatrix());
 		baseslight->posRadius = Vec4(pos, light.getDistance());
 		baseslight->posRadius = Vec4(pos, light.getDistance());
 
 
-		// Diff color
-		baseslight->diffuseColorShadowmapId = Vec4(
-			light.getDiffuseColor().xyz(), 
-			slight.diffuseColorShadowmapId.w());
+		// Diff color. don't set the shadowmap ID now
+		baseslight->diffuseColorShadowmapId = light.getDiffuseColor();
 
 
 		// Spec color
 		// Spec color
-		slight.specularColorTexId = light.getSpecularColor();
+		baseslight->specularColorTexId = light.getSpecularColor();
 
 
 		// Light dir
 		// Light dir
 		Vec3 lightDir = -light.getWorldTransform().getRotation().getZAxis();
 		Vec3 lightDir = -light.getWorldTransform().getRotation().getZAxis();
@@ -252,21 +279,25 @@ struct WriteLightsJob: ThreadJob
 		baseslight->lightDir = Vec4(lightDir, 0.0);
 		baseslight->lightDir = Vec4(lightDir, 0.0);
 		
 		
 		// Angles
 		// Angles
-		slight.outerCosInnerCos = Vec4(light.getOuterAngleCos(),
-			light.getInnerAngleCos(), 1.0, 1.0);
+		baseslight->outerCosInnerCos = Vec4(
+			light.getOuterAngleCos(),
+			light.getInnerAngleCos(), 
+			1.0, 
+			1.0);
 
 
 		// extend points
 		// extend points
 		const PerspectiveFrustum& frustum = light.getFrustum();
 		const PerspectiveFrustum& frustum = light.getFrustum();
 
 
 		for(U i = 0; i < 4; i++)
 		for(U i = 0; i < 4; i++)
 		{
 		{
-			Vec3 dir = light.getWorldTransform().getOrigin() 
+			Vec3 extendPoint = light.getWorldTransform().getOrigin() 
 				+ frustum.getDirections()[i];
 				+ frustum.getDirections()[i];
-			dir.transform(cam->getViewMatrix());
-			baseslight->extendPoints[i] = Vec4(dir, 1.0);
+
+			extendPoint.transform(cam->getViewMatrix());
+			baseslight->extendPoints[i] = Vec4(extendPoint, 1.0);
 		}
 		}
 
 
-		return pos;
+		return i;
 	}
 	}
 
 
 	// Bin point light
 	// Bin point light
@@ -274,7 +305,7 @@ struct WriteLightsJob: ThreadJob
 	{
 	{
 		// Do the tests
 		// Do the tests
 		Tiler::Bitset bitset;
 		Tiler::Bitset bitset;
-		tiler->test(light->getSpatialCollisonShape(), true, &bitset);
+		tiler->test(light.getSpatialCollisionShape(), true, &bitset);
 
 
 		// Bin to the correct tiles
 		// Bin to the correct tiles
 		for(U t = 0; t < TILES_COUNT; t++)
 		for(U t = 0; t < TILES_COUNT; t++)
@@ -302,7 +333,7 @@ struct WriteLightsJob: ThreadJob
 	{
 	{
 		// Do the tests
 		// Do the tests
 		Tiler::Bitset bitset;
 		Tiler::Bitset bitset;
-		tiler->test(light->getSpatialCollisonShape(), true, &bitset);
+		tiler->test(light.getSpatialCollisionShape(), true, &bitset);
 
 
 		// Bin to the correct tiles
 		// Bin to the correct tiles
 		for(U t = 0; t < TILES_COUNT; t++)
 		for(U t = 0; t < TILES_COUNT; t++)
@@ -319,12 +350,20 @@ struct WriteLightsJob: ThreadJob
 			if(light.getShadowEnabled())
 			if(light.getShadowEnabled())
 			{
 			{
 				U tilePos = (*tileSpotTexLightsCount)[y][x].fetch_add(1);
 				U tilePos = (*tileSpotTexLightsCount)[y][x].fetch_add(1);
-				tiles->tiles[y][x].spotTexLightIndices[tilePos] = pos;
+
+				if(tilePos < MAX_SPOT_TEX_LIGHTS_PER_TILE)
+				{
+					tiles->tiles[y][x].spotTexLightIndices[tilePos] = pos;
+				}
 			}
 			}
 			else
 			else
 			{
 			{
 				U tilePos = (*tileSpotLightsCount)[y][x].fetch_add(1);
 				U tilePos = (*tileSpotLightsCount)[y][x].fetch_add(1);
-				tiles->tiles[y][x].spotLightIndices[tilePos] = pos;
+
+				if(tilePos < MAX_SPOT_LIGHTS_PER_TILE)
+				{
+					tiles->tiles[y][x].spotLightIndices[tilePos] = pos;
+				}
 			}
 			}
 		}
 		}
 	}
 	}
@@ -366,14 +405,21 @@ void Is::initInternal(const RendererInitializer& initializer)
 	// Load the programs
 	// Load the programs
 	//
 	//
 	std::string pps =
 	std::string pps =
-		"#define TILES_X_COUNT " + std::to_string(Tiler::TILES_X_COUNT) + "\n"
-		"#define TILES_Y_COUNT " + std::to_string(Tiler::TILES_Y_COUNT) + "\n"
+		"#define TILES_X_COUNT " + std::to_string(TILES_X_COUNT) + "\n"
+		"#define TILES_Y_COUNT " + std::to_string(TILES_Y_COUNT) + "\n"
+		"#define TILES_COUNT " + std::to_string(TILES_COUNT) + "\n"
 		"#define RENDERER_WIDTH " + std::to_string(r->getWidth()) + "\n"
 		"#define RENDERER_WIDTH " + std::to_string(r->getWidth()) + "\n"
 		"#define RENDERER_HEIGHT " + std::to_string(r->getHeight()) + "\n"
 		"#define RENDERER_HEIGHT " + std::to_string(r->getHeight()) + "\n"
-		"#define MAX_LIGHTS_PER_TILE " + std::to_string(MAX_LIGHTS_PER_TILE)
-		+ "\n"
+		"#define MAX_POINT_LIGHTS_PER_TILE " 
+		+ std::to_string(MAX_POINT_LIGHTS_PER_TILE) + "\n"
+		"#define MAX_SPOT_LIGHTS_PER_TILE " 
+		+ std::to_string(MAX_SPOT_LIGHTS_PER_TILE) + "\n"
+		"#define MAX_SPOT_TEX_LIGHTS_PER_TILE " 
+		+ std::to_string(MAX_SPOT_TEX_LIGHTS_PER_TILE) + "\n"
 		"#define MAX_POINT_LIGHTS " + std::to_string(MAX_POINT_LIGHTS) + "\n"
 		"#define MAX_POINT_LIGHTS " + std::to_string(MAX_POINT_LIGHTS) + "\n"
 		"#define MAX_SPOT_LIGHTS " + std::to_string(MAX_SPOT_LIGHTS) + "\n"
 		"#define MAX_SPOT_LIGHTS " + std::to_string(MAX_SPOT_LIGHTS) + "\n"
+		"#define MAX_SPOT_TEX_LIGHTS " 
+		+ std::to_string(MAX_SPOT_TEX_LIGHTS) + "\n"
 		"#define GROUND_LIGHT " + std::to_string(groundLightEnabled) + "\n";
 		"#define GROUND_LIGHT " + std::to_string(groundLightEnabled) + "\n";
 
 
 	if(sm.getPcfEnabled())
 	if(sm.getPcfEnabled())
@@ -389,9 +435,6 @@ void Is::initInternal(const RendererInitializer& initializer)
 	lightPassProg.load(ShaderProgramResource::createSrcCodeToCache(
 	lightPassProg.load(ShaderProgramResource::createSrcCodeToCache(
 		"shaders/IsLp.glsl", pps.c_str()).c_str());
 		"shaders/IsLp.glsl", pps.c_str()).c_str());
 
 
-	tilerProg.load(ShaderProgramResource::createSrcCodeToCache(
-		"shaders/IsUpdateTiles.glsl", pps.c_str()).c_str());
-
 	//
 	//
 	// Create FBOs
 	// Create FBOs
 	//
 	//
@@ -434,17 +477,18 @@ void Is::initInternal(const RendererInitializer& initializer)
 	commonUbo.create(sizeof(shader::CommonUniforms), nullptr);
 	commonUbo.create(sizeof(shader::CommonUniforms), nullptr);
 
 
 	// lights UBO
 	// lights UBO
-	lightsUbo.create()
-
-	pointLightsUbo.create(LIGHTS_UBO_SIZE, nullptr);
+	lightsUbo.create(LIGHTS_UBO_SIZE, nullptr);
 
 
 	// tiles UBO
 	// tiles UBO
 	tilesUbo.create(sizeof(shader::Tiles), nullptr);
 	tilesUbo.create(sizeof(shader::Tiles), nullptr);
 
 
 	// tilegrid
 	// tilegrid
-	tilegridBuffer.create(sizeof(shader::Tilegrid), GL_SHADER_STORAGE_BUFFER,
+	tilegridBuffer.create(GL_SHADER_STORAGE_BUFFER, sizeof(shader::Tilegrid),
 		nullptr, GL_STATIC_DRAW);
 		nullptr, GL_STATIC_DRAW);
 
 
+	ANKI_LOGI("Creating BOs: lights: " << LIGHTS_UBO_SIZE << "B tiles: "
+		<< sizeof(shader::Tiles) << "B");
+
 	// Sanity checks
 	// Sanity checks
 	const ShaderProgramUniformBlock* ublock;
 	const ShaderProgramUniformBlock* ublock;
 
 
@@ -507,6 +551,7 @@ void Is::lightPass()
 	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
 	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
 	{
 	{
 		Light* light = (*it)->getLight();
 		Light* light = (*it)->getLight();
+		ANKI_ASSERT(light);
 		switch(light->getLightType())
 		switch(light->getLightType())
 		{
 		{
 		case Light::LT_POINT:
 		case Light::LT_POINT:
@@ -534,32 +579,23 @@ void Is::lightPass()
 	}
 	}
 
 
 	// Sanitize the counts
 	// Sanitize the counts
-	visiblePointLightsCount = MAX_POINT_LIGHTS % visiblePointLightsCount;
-	visibleSpotLightsCount = MAX_SPOT_LIGHTS % visibleSpotLightsCount;
-	visibleSpotTexLightsCount = MAX_SPOT_TEX_LIGHTS % visibleSpotTexLightsCount;
+	visiblePointLightsCount = (visiblePointLightsCount > 0)
+		? visiblePointLightsCount % MAX_POINT_LIGHTS : visiblePointLightsCount;
+	visibleSpotLightsCount = (visibleSpotLightsCount > 0) 
+		? visibleSpotLightsCount % MAX_SPOT_LIGHTS : visibleSpotLightsCount;
+	visibleSpotTexLightsCount = (visibleSpotTexLightsCount > 0) 
+		? visibleSpotTexLightsCount % MAX_SPOT_TEX_LIGHTS 
+		: visibleSpotTexLightsCount;
 
 
 	//
 	//
 	// Do shadows pass
 	// Do shadows pass
 	//
 	//
 	Array<U32, Sm::MAX_SHADOW_CASTERS> shadowmapLayers;
 	Array<U32, Sm::MAX_SHADOW_CASTERS> shadowmapLayers;
-	sm.run(&shadowCasters[0], spotsShadowCount, shadowmapLayers);
+	sm.run(&shadowCasters[0], visibleSpotTexLightsCount, shadowmapLayers);
 
 
 	//
 	//
-	// Prepare state
+	// Write the ligths and tiles UBOs
 	//
 	//
-	fbo.bind();
-	r->clearAfterBindingFbo(
-		GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
-	GlStateSingleton::get().setViewport(
-		0, 0, r->getWidth(), r->getHeight());
-	GlStateSingleton::get().disable(GL_DEPTH_TEST);
-	GlStateSingleton::get().disable(GL_BLEND);
-
-	//
-	// Write the lights UBO
-	//
-
-	U8 clientBuffer[LIGHTS_UBO_SIZE];
 
 
 	// Get the offsets and sizes of each uniform block
 	// Get the offsets and sizes of each uniform block
 	PtrSize pointLightsOffset = 0;
 	PtrSize pointLightsOffset = 0;
@@ -579,7 +615,8 @@ void Is::lightPass()
 	// Fire the super jobs
 	// Fire the super jobs
 	WriteLightsJob jobs[ThreadPool::MAX_THREADS];
 	WriteLightsJob jobs[ThreadPool::MAX_THREADS];
 
 
-	shader::Tiles tilesClient;
+	U8 clientBuffer[LIGHTS_UBO_SIZE];
+	shader::Tiles clientTiles;
 
 
 	std::atomic<U32> pointLightsAtomicCount;
 	std::atomic<U32> pointLightsAtomicCount;
 	std::atomic<U32> spotLightsAtomicCount;
 	std::atomic<U32> spotLightsAtomicCount;
@@ -596,16 +633,17 @@ void Is::lightPass()
 	{
 	{
 		WriteLightsJob& job = jobs[i];
 		WriteLightsJob& job = jobs[i];
 
 
-		job.pointLigths = 
+		job.pointLights = 
 			(shader::PointLight*)(&clientBuffer[0] + pointLightsOffset);
 			(shader::PointLight*)(&clientBuffer[0] + pointLightsOffset);
-		job.spotLigths = 
+		job.spotLights = 
 			(shader::SpotLight*)(&clientBuffer[0] + spotLightsOffset);
 			(shader::SpotLight*)(&clientBuffer[0] + spotLightsOffset);
-		job.spotTexLigths = 
+		job.spotTexLights = 
 			(shader::SpotTexLight*)(&clientBuffer[0] + spotTexLightsOffset);
 			(shader::SpotTexLight*)(&clientBuffer[0] + spotTexLightsOffset);
 
 
-		job.tiles = &tilesClient;
+		job.tiles = &clientTiles;
 
 
-		// XXX add more
+		job.lightsBegin = vi.getLightsBegin();
+		job.lightsEnd = vi.getLightsEnd();
 
 
 		job.pointLightsCount = &pointLightsAtomicCount;
 		job.pointLightsCount = &pointLightsAtomicCount;
 		job.spotLightsCount = &spotLightsAtomicCount;
 		job.spotLightsCount = &spotLightsAtomicCount;
@@ -615,57 +653,50 @@ void Is::lightPass()
 		job.tileSpotLightsCount = &tileSpotLightsCount;
 		job.tileSpotLightsCount = &tileSpotLightsCount;
 		job.tileSpotTexLightsCount = &tileSpotTexLightsCount;
 		job.tileSpotTexLightsCount = &tileSpotTexLightsCount;
 
 
-		job.tiler = r->getTiler();
+		job.tiler = &r->getTiler();
 		job.is = this;
 		job.is = this;
+
+		threadPool.assignNewJob(i, &job);
 	}
 	}
 
 
+	// In the meantime set the state
+	setState();
+
 	// Sync
 	// Sync
 	threadPool.waitForAllJobsToFinish();
 	threadPool.waitForAllJobsToFinish();
 
 
-	// Write BO
-	lightsUbo.write(&clientBuffer[0], spotTexLightsOffset + spotTexLightsSize);
-
-	//
-	// Update the tiles UBO
-	//
-
-	shader::Tiles clientBuffer;
-
-	WriteTilesUboJob tjobs[ThreadPool::MAX_THREADS];
-	for(U i = 0; i < threadPool.getThreadsCount(); i++)
+	// Write the shadomap IDs
+	for(U i = 0; i < visibleSpotTexLightsCount; i++)
 	{
 	{
-		tjobs[i].visiblePointLights = &visiblePointLights[0];
-		tjobs[i].visiblePointLightsCount = visiblePointLightsCount;
-
-		tjobs[i].visibleSpotLights = &visibleSpotLights[0];
-		tjobs[i].visibleSpotLightsCount = visibleSpotLightsCount;
-
-		tjobs[i].shaderTiles = &clientBuffer.tiles[0];
-		tjobs[i].is = this;
-
-		threadPool.assignNewJob(i, &tjobs[i]);
+		jobs[0].spotTexLights[i].diffuseColorShadowmapId.w() = 
+			(F32)shadowmapLayers[i];
 	}
 	}
 
 
-	threadPool.waitForAllJobsToFinish();
-
-	tilesUbo.write(&clientBuffer);
-
-	// XXX
-	/*{
-		tilesUbo.bind(GL_SHADER_STORAGE_BUFFER);
-		tilesUbo.setBinding(0);
+	// Write the light count for each tile
+	for(U y = 0; y < TILES_Y_COUNT; y++)
+	{
+		for(U x = 0; x < TILES_X_COUNT; x++)
+		{
+			clientTiles.tiles[y][x].lightsCount[0] = 
+				tilePointLightsCount[y][x].load();
 
 
-		tilerProg->bind();
+			clientTiles.tiles[y][x].lightsCount[2] = 
+				tileSpotLightsCount[y][x].load();
 
 
-		glDispatchCompute(Tiler::TILES_X_COUNT, Tiler::TILES_Y_COUNT, 1);
+			clientTiles.tiles[y][x].lightsCount[3] = 
+				tileSpotTexLightsCount[y][x].load();
+		}
+	}
 
 
-		tilesUbo.bind(GL_UNIFORM_BUFFER);
-	}*/
+	// Write BOs
+	lightsUbo.write(
+		&clientBuffer[0], 0, spotTexLightsOffset + spotTexLightsSize);
+	tilesUbo.write(&clientTiles);
 
 
 	//
 	//
 	// Setup shader and draw
 	// Setup shader and draw
 	//
 	//
-	
+
 	// shader prog
 	// shader prog
 	lightPassProg->bind();
 	lightPassProg->bind();
 
 
@@ -673,8 +704,22 @@ void Is::lightPass()
 		Vec4(r->getLimitsOfNearPlane(), r->getLimitsOfNearPlane2()));
 		Vec4(r->getLimitsOfNearPlane(), r->getLimitsOfNearPlane2()));
 
 
 	commonUbo.setBinding(COMMON_UNIFORMS_BLOCK_BINDING);
 	commonUbo.setBinding(COMMON_UNIFORMS_BLOCK_BINDING);
-	pointLightsUbo.setBinding(POINT_LIGHTS_BLOCK_BINDING);
-	spotLightsUbo.setBinding(SPOT_LIGHTS_BLOCK_BINDING);
+
+	if(pointLightsSize > 0)
+	{
+		lightsUbo.setBindingRange(POINT_LIGHTS_BLOCK_BINDING, pointLightsOffset,
+			pointLightsSize);
+	}
+	if(spotLightsSize > 0)
+	{
+		lightsUbo.setBindingRange(SPOT_LIGHTS_BLOCK_BINDING, spotLightsOffset,
+			spotLightsSize);
+	}
+	if(spotTexLightsSize > 0)
+	{
+		lightsUbo.setBindingRange(SPOT_TEX_LIGHTS_BLOCK_BINDING, 
+			spotTexLightsOffset, spotTexLightsSize);
+	}
 	tilesUbo.setBinding(TILES_BLOCK_BINDING);
 	tilesUbo.setBinding(TILES_BLOCK_BINDING);
 
 
 	lightPassProg->findUniformVariable("msFai0").set(r->getMs().getFai0());
 	lightPassProg->findUniformVariable("msFai0").set(r->getMs().getFai0());
@@ -684,7 +729,19 @@ void Is::lightPass()
 
 
 	quadVao.bind();
 	quadVao.bind();
 	glDrawElementsInstanced(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0,
 	glDrawElementsInstanced(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0,
-		Tiler::TILES_Y_COUNT * Tiler::TILES_X_COUNT);
+		TILES_COUNT);
+}
+
+//==============================================================================
+void Is::setState()
+{
+	fbo.bind();
+	r->clearAfterBindingFbo(
+		GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+	GlStateSingleton::get().setViewport(
+		0, 0, r->getWidth(), r->getHeight());
+	GlStateSingleton::get().disable(GL_DEPTH_TEST);
+	GlStateSingleton::get().disable(GL_BLEND);
 }
 }
 
 
 //==============================================================================
 //==============================================================================

+ 2 - 16
src/scene/Visibility.cpp

@@ -59,10 +59,7 @@ struct VisibilityTestJob: ThreadJob
 			{
 			{
 				Spatial* subsp = *it;
 				Spatial* subsp = *it;
 	
 	
-				if(frustumable->insideFrustum(*subsp)
-					&& tiler->test2(
-					subsp->getSpatialCollisionShape(),
-					subsp->getAabb(), false, nullptr))
+				if(frustumable->insideFrustum(*subsp))
 				{
 				{
 					subSpatialsMask |= 1 << i;
 					subSpatialsMask |= 1 << i;
 					subsp->enableBits(Spatial::SF_VISIBLE_CAMERA);
 					subsp->enableBits(Spatial::SF_VISIBLE_CAMERA);
@@ -79,12 +76,6 @@ struct VisibilityTestJob: ThreadJob
 			Renderable* r = node->getRenderable();
 			Renderable* r = node->getRenderable();
 			if(r)
 			if(r)
 			{
 			{
-				if(!tiler->test2(
-					sp->getSpatialCollisionShape(),
-					sp->getAabb(), false, nullptr))
-				{
-					continue;
-				}
 				visible->renderables.push_back(node);
 				visible->renderables.push_back(node);
 
 
 				// Inform the renderable for the mask
 				// Inform the renderable for the mask
@@ -97,15 +88,10 @@ struct VisibilityTestJob: ThreadJob
 			else
 			else
 			{
 			{
 				Light* l = node->getLight();
 				Light* l = node->getLight();
-				Tiler::Bitset tilerBitset;
-				if(l
-					&& tiler->test2(sp->getSpatialCollisionShape(),
-					sp->getAabb(), true, &tilerBitset))
+				if(l)
 				{
 				{
 					visible->lights.push_back(node);
 					visible->lights.push_back(node);
 
 
-					sp->setTilerBitset(tilerBitset);
-
 					if(l->getShadowEnabled() && fr)
 					if(l->getShadowEnabled() && fr)
 					{
 					{
 						testLight(*node);
 						testLight(*node);

+ 1 - 1
testapp/Main.cpp

@@ -3,7 +3,7 @@
 #include <fstream>
 #include <fstream>
 
 
 #include "anki/input/Input.h"
 #include "anki/input/Input.h"
-#include "anki/math/Math.h"
+#include "anki/Math.h"
 #include "anki/renderer/Renderer.h"
 #include "anki/renderer/Renderer.h"
 #include "anki/core/App.h"
 #include "anki/core/App.h"
 #include "anki/resource/Mesh.h"
 #include "anki/resource/Mesh.h"

+ 1 - 1
tests/script/LuaBinder.cpp

@@ -1,6 +1,6 @@
 #include "tests/framework/Framework.h"
 #include "tests/framework/Framework.h"
 #include "anki/script/ScriptManager.h"
 #include "anki/script/ScriptManager.h"
-#include "anki/math/Math.h"
+#include "anki/Math.h"
 
 
 ANKI_TEST(Script, LuaBinder)
 ANKI_TEST(Script, LuaBinder)
 {
 {