Panagiotis Christopoulos Charitos hace 13 años
padre
commit
533546c02b

+ 22 - 9
include/anki/gl/BufferObject.h

@@ -3,6 +3,7 @@
 
 #include "anki/gl/Ogl.h"
 #include "anki/util/Assert.h"
+#include "anki/util/StdTypes.h"
 
 namespace anki {
 
@@ -19,8 +20,8 @@ public:
 	BufferObject()
 	{}
 
-	/// Default constructor @see create
-	BufferObject(GLenum target, uint32_t sizeInBytes,
+	/// @see create
+	BufferObject(GLenum target, U32 sizeInBytes,
 		const void* dataPtr, GLenum usage)
 	{
 		create(target, sizeInBytes, dataPtr, usage);
@@ -50,7 +51,7 @@ public:
 		return usage;
 	}
 
-	size_t getSizeInBytes() const
+	U32 getSizeInBytes() const
 	{
 		ANKI_ASSERT(isCreated());
 		return sizeInBytes;
@@ -85,7 +86,7 @@ public:
 	/// @param usage It should be: GL_STREAM_DRAW or GL_STATIC_DRAW or
 	///		   GL_DYNAMIC_DRAW only!!!!!!!!!
 	/// @exception Exception
-	void create(GLenum target, uint32_t sizeInBytes, const void* dataPtr,
+	void create(GLenum target, U32 sizeInBytes, const void* dataPtr,
 		GLenum usage);
 
 	/// Delete the BO
@@ -108,9 +109,21 @@ public:
 	/// @param[in] buff The buffer to copy to BO
 	/// @param[in] offset The offset
 	/// @param[in] size The size in bytes we want to write
-	void write(void* buff, size_t offset, size_t size);
+	void write(void* buff, U32 offset, U32 size);
 
-	/// If created is run successfully it returns true
+	/// Map buffer
+	void* map(U32 offset, U32 length, GLuint flags);
+
+	/// Map the entire buffer
+	void* map(GLuint flags)
+	{
+		return map(0, sizeInBytes, flags);
+	}
+
+	/// Unmap buffer
+	void unmap();
+
+	/// If created is run successfully this returns true
 	bool isCreated() const
 	{
 		return glId != 0;
@@ -118,8 +131,8 @@ public:
 
 	void setBinding(GLuint binding) const
 	{
-		ANKI_ASSERT(target == GL_TRANSFORM_FEEDBACK_BUFFER ||
-			target == GL_UNIFORM_BUFFER);
+		ANKI_ASSERT(target == GL_TRANSFORM_FEEDBACK_BUFFER 
+			|| target == GL_UNIFORM_BUFFER);
 		bind();
 		glBindBufferBase(target, binding, glId);
 	}
@@ -136,7 +149,7 @@ private:
 	GLenum target;
 
 	GLenum usage; ///< GL_STREAM_DRAW or GL_STATIC_DRAW or GL_DYNAMIC_DRAW
-	size_t sizeInBytes; ///< The size of the buffer
+	U32 sizeInBytes; ///< The size of the buffer
 };
 /// @}
 

+ 1 - 0
include/anki/math/Vec2.h

@@ -67,6 +67,7 @@ public:
 
 	/// @name Other
 	/// @{
+	F32 getLengthSquared() const;
 	F32 getLength() const;
 	Vec2 getNormalized() const;
 	void normalize();

+ 8 - 2
include/anki/math/Vec2.inl.h

@@ -187,7 +187,7 @@ inline Bool Vec2::operator<=(const Vec2& b) const
 }
 
 //==============================================================================
-// Operators with F32                                                        =
+// Operators with F32                                                          =
 //==============================================================================
 
 // vec2 + F32
@@ -246,10 +246,16 @@ inline Vec2& Vec2::operator/=(F32 f)
 // Misc methods                                                                =
 //==============================================================================
 
+// getLengthSquared
+inline F32 Vec2::getLengthSquared() const
+{
+	return x() * x() + y() * y();
+}
+
 // getLength
 inline F32 Vec2::getLength() const
 {
-	return Math::sqrt(x() * x() + y() * y());
+	return Math::sqrt(getLengthSquared());
 }
 
 // normalize

+ 20 - 3
src/gl/BufferObject.cpp

@@ -19,7 +19,7 @@ BufferObject::~BufferObject()
 }
 
 //==============================================================================
-void BufferObject::create(GLenum target_, uint32_t sizeInBytes_,
+void BufferObject::create(GLenum target_, U32 sizeInBytes_,
 	const void* dataPtr, GLenum usage_)
 {
 	ANKI_ASSERT(!isCreated());
@@ -52,6 +52,23 @@ void BufferObject::create(GLenum target_, uint32_t sizeInBytes_,
 	ANKI_CHECK_GL_ERROR();
 }
 
+//==============================================================================
+void* BufferObject::map(U32 offset, U32 length, GLuint flags)
+{
+	bind();
+	ANKI_ASSERT(offset + length <= sizeInBytes);
+	void* mappedMem = glMapBufferRange(target, offset, length, flags);
+	ANKI_ASSERT(mappedMem != nullptr);
+	return mappedMem;
+}
+
+//==============================================================================
+void BufferObject::unmap()
+{
+	bind();
+	glUnmapBuffer(target);
+}
+
 //==============================================================================
 void BufferObject::write(void* buff)
 {
@@ -68,7 +85,7 @@ void BufferObject::write(void* buff)
 }
 
 //==============================================================================
-void BufferObject::write(void* buff, size_t offset, size_t size)
+void BufferObject::write(void* buff, U32 offset, U32 size)
 {
 	ANKI_ASSERT(isCreated());
 	ANKI_ASSERT(usage != GL_STATIC_DRAW);
@@ -82,4 +99,4 @@ void BufferObject::write(void* buff, size_t offset, size_t size)
 	glUnmapBuffer(target);
 }
 
-} // end namespace
+} // end namespace anki

+ 1 - 1
src/math/F16.cpp

@@ -66,7 +66,7 @@ F16 F16::toF16(F32 f)
 
 		if (e > 30)
 		{
-			//overflow();
+			ANKI_ASSERT(0 && "Overflow");
 			out.data = s | 0x7c00;
 		}
 		else

+ 91 - 15
src/renderer/Is.cpp

@@ -39,12 +39,12 @@ struct ShaderSpotLights
 struct ShaderTile
 {
 	U32 lightsCount;
-	U32 lightIndices[Is::MAX_LIGHTS_PER_TILE];
+	Array<U32, Is::MAX_LIGHTS_PER_TILE> lightIndices;
 };
 
 struct ShaderTiles
 {
-	ShaderTile tile[Is::TILES_X_COUNT * Is::TILES_X_COUNT];
+	Array<Array<ShaderTile, Is::TILES_Y_COUNT>, Is::TILES_X_COUNT> tiles;
 };
 
 struct ShaderCommonUniforms
@@ -167,14 +167,18 @@ void Is::initInternal(const RendererInitializer& initializer)
 	//
 	// Init tiles
 	//
-	F32 tileWidth = 1.0 / TILES_X_COUNT;
-	F32 tileHeight = 1.0 / TILES_Y_COUNT;
+	F32 tileW = 1.0 / TILES_X_COUNT;
+	F32 tileH = 1.0 / TILES_Y_COUNT;
 
 	for(U i = 0; i < TILES_X_COUNT; i++)
 	{
 		for(U j = 0; j < TILES_Y_COUNT; j++)
 		{
-			// XXX
+			F32 x = i * tileW;
+			F32 y = j * tileH;
+
+			tiles[i][j].coords[0] = Vec2(x, y) * 2.0 - 1.0;
+			tiles[i][j].coords[1] = Vec2(x + tileW, y + tileH) * 2.0 - 1.0;
 		}
 	}
 }
@@ -233,14 +237,12 @@ Bool Is::circleIntersects(const Tile& tile, const Vec2& c, F32 r)
 }
 
 //==============================================================================
-Bool Is::cullLight(const PointLight& light, const Tile& tile)
+Bool Is::cullLight(const PointLight& plight, const Tile& tile)
 {
-	const Camera& cam = r->getScene().getActiveCamera();
 	Vec2 cc;
-	F32 r;
-	const PointLight& plight = static_cast<const PointLight&>(light);
-	projectShape(cam, plight.getSphere(), cc, r);
-	return circleIntersects(tile, cc, r);
+	F32 rad;
+	projectShape(r->getScene().getActiveCamera(), plight.getSphere(), cc, rad);
+	return circleIntersects(tile, cc, rad);
 }
 
 //==============================================================================
@@ -284,13 +286,64 @@ void Is::pointLightsPass()
 	Camera& cam = r->getScene().getActiveCamera();
 	VisibilityInfo& vi = cam.getFrustumable()->getVisibilityInfo();
 
+	//
+	// Write the lightsUbo
+	//
+
+	// Quickly count the point lights
+	U pointLightsCount = 0;
+	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
+	{
+		const Light& light = *(*it);
+		if(light.getLightType() == Light::LT_POINT)
+		{
+			++pointLightsCount;		
+		}
+	}
+
+	// Map
+	ShaderPointLight* lightsMappedBuff = (ShaderPointLight*)lightsUbo.map(
+		0, 
+		sizeof(ShaderPointLight) * pointLightsCount,
+		GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
+
+	// Write the buff
+	pointLightsCount = 0;
+	for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
+	{
+		const Light& light = *(*it);
+		if(light.getLightType() == Light::LT_POINT)
+		{
+			ShaderPointLight& pl = lightsMappedBuff[pointLightsCount];
+			const PointLight& plight = static_cast<const PointLight&>(light);
+
+			Vec3 pos = light.getWorldTransform().getOrigin().getTransformed(
+				cam.getViewMatrix());
+
+			pl.posAndRadius = Vec4(pos, plight.getRadius());
+			pl.diffuseColor = light.getDiffuseColor();
+			pl.specularColor = light.getSpecularColor();
+
+			++pointLightsCount;		
+		}
+	}
+
+	// Done
+	lightsUbo.unmap();
+
+	//
+	// Update the tiles
+	//
+
+	// For all tiles write their indices 
+	// OPT Parallelize that
 	for(U i = 0; i < TILES_X_COUNT; i++)
 	{
 		for(U j = 0; j < TILES_Y_COUNT; j++)
 		{
 			Tile& tile = tiles[i][j];
 
-			U lightsCount = 0;
+			U lightsInTileCount = 0;
 			U ids = 0;
 
 			for(auto it = vi.getLightsBegin(); it != vi.getLightsEnd(); ++it)
@@ -306,16 +359,39 @@ void Is::pointLightsPass()
 
 				if(cullLight(plight, tile))
 				{
-					tile.lightIndices[ids] = lightsCount;
+					tile.lightIndices[ids] = lightsInTileCount;
 					++ids;
 				}
 
-				++lightsCount;
+				++lightsInTileCount;
 			}
 
-			tile.lightsCount = lightsCount;
+			tile.lightsCount = lightsInTileCount;
 		}
 	}
+
+	//
+	// Write the lightIndicesUbo
+	//
+
+	ShaderTiles* stiles = (ShaderTiles*)lightIndicesUbo.map(
+		GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
+
+	for(U i = 0; i < TILES_X_COUNT; i++)
+	{
+		for(U j = 0; j < TILES_Y_COUNT; j++)
+		{
+			const Tile& tile = tiles[i][j];
+			stiles->tiles[i][j].lightsCount = tile.lightsCount;
+
+			for(U k = 0; k < tile.lightsCount; k++)
+			{
+				stiles->tiles[i][j].lightIndices[k] = tile.lightIndices[k];
+			}
+		}
+	}
+
+	lightIndicesUbo.unmap();
 }
 
 //==============================================================================