Преглед на файлове

Optimizing the renderer

Panagiotis Christopoulos Charitos преди 13 години
родител
ревизия
81ab490235

+ 9 - 7
include/anki/gl/ShaderProgram.h

@@ -149,7 +149,8 @@ public:
 
 private:
 	GLuint index;
-	GLsizei offset; ///< Offset inside the uniform block
+	GLint offset; ///< Offset inside the uniform block. -1 if it's inside the
+	              ///< default uniform block
 
 	/// Standard set uniform checks
 	/// - Check if initialized
@@ -173,16 +174,17 @@ public:
 /// Uniform shader block
 class ShaderProgramUniformBlock
 {
+public:
+	ShaderProgramUniformBlock(ShaderProgram& sprog);
+
 private:
-	std::vector<ShaderProgramUniformVariable> uniforms;
+	std::vector<ShaderProgramUniformVariable*> uniforms;
+	GLuint index;
+	uint32_t size; ///< In bytes
+	std::string name;
 };
 
 /// Shader program object
-///
-/// Shader program. Combines. Every shader program consist of one OpenGL ID, a 
-/// vector of uniform variables and a vector of attribute variables. Every 
-/// variable is a struct that contains the variable's name, location, OpenGL 
-/// data type and if it is a uniform or an attribute var.
 class ShaderProgram: public NonCopyable
 {
 public:

+ 3 - 15
include/anki/renderer/Ms.h

@@ -20,19 +20,9 @@ public:
 
 	/// @name Accessors
 	/// @{
-	const Texture& getNormalFai() const
+	const Texture& getFai0() const
 	{
-		return normalFai;
-	}
-
-	const Texture& getDiffuseFai() const
-	{
-		return diffuseFai;
-	}
-
-	const Texture& getSpecularFai() const
-	{
-		return specularFai;
+		return fai0;
 	}
 
 	const Texture& getDepthFai() const
@@ -47,9 +37,7 @@ public:
 private:
 	Ez ez; /// EarlyZ pass
 	Fbo fbo;
-	Texture normalFai; ///< The FAI for normals
-	Texture diffuseFai; ///< The FAI for diffuse color
-	Texture specularFai; ///< The FAI for specular color and shininess
+	Texture fai0; ///< The FAI for diffuse color, normals and specular
 	Texture depthFai; ///< The FAI for depth
 };
 

+ 1 - 1
include/anki/resource/Image.h

@@ -126,6 +126,6 @@ private:
 	void loadDds(const char* filename);
 };
 
-} // end namespace
+} // end namespace anki
 
 #endif

+ 2 - 2
src/renderer/Bl.cpp

@@ -56,7 +56,7 @@ void Bl::init(const Renderer::Initializer& initializer)
 	try
 	{
 		sideBlurFbo.create();
-		sideBlurFbo.setColorAttachments({&r->getMs().getNormalFai()});
+		sideBlurFbo.setColorAttachments({&r->getMs().getFai0()});
 	}
 	catch(std::exception& e)
 	{
@@ -98,7 +98,7 @@ void Bl::runBlur()
 	vBlurSProg->bind();
 	vBlurSProg->findUniformVariableByName("img")->set(blurFai);
 	vBlurSProg->findUniformVariableByName("msNormalFai")->set(
-		r->getMs().getNormalFai());
+		r->getMs().getFai0());
 	vBlurSProg->findUniformVariableByName("imgDimension")->set(
 		float(r->getHeight()));
 

+ 1 - 1
src/renderer/Dbg.cpp

@@ -19,7 +19,7 @@ void Dbg::init(const Renderer::Initializer& initializer)
 	{
 		fbo.create();
 		//fbo.setColorAttachments({&r->getPps().getPostPassFai()});
-		fbo.setColorAttachments({&r->getMs().getDiffuseFai()});
+		fbo.setColorAttachments({&r->getMs().getFai0()});
 		fbo.setOtherAttachment(GL_DEPTH_ATTACHMENT, r->getMs().getDepthFai());
 		ANKI_ASSERT(fbo.isComplete());
 	}

+ 1 - 1
src/renderer/MainRenderer.cpp

@@ -112,7 +112,7 @@ void MainRenderer::render(Scene& scene)
 	GlStateSingleton::get().disable(GL_DEPTH_TEST);
 	GlStateSingleton::get().disable(GL_BLEND);
 	sProg->bind();
-	const Texture& finalFai = ms.getDiffuseFai();
+	const Texture& finalFai = ms.getFai0();
 	sProg->findUniformVariableByName("rasterImage")->set(finalFai);
 	drawQuad();
 }

+ 3 - 7
src/renderer/Ms.cpp

@@ -17,18 +17,14 @@ void Ms::init(const RendererInitializer& initializer)
 {
 	try
 	{
-		Renderer::createFai(r->getWidth(), r->getHeight(), GL_RGB16F,
-			GL_RGB, GL_FLOAT, normalFai);
-		Renderer::createFai(r->getWidth(), r->getHeight(), GL_RGB8,
-			GL_RGB, GL_FLOAT, diffuseFai);
-		Renderer::createFai(r->getWidth(), r->getHeight(), GL_RGBA8,
-			GL_RGBA, GL_FLOAT, specularFai);
+		Renderer::createFai(r->getWidth(), r->getHeight(), GL_RGB32UI,
+			GL_RGB, GL_INT, fai0);
 		Renderer::createFai(r->getWidth(), r->getHeight(),
 			GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
 			GL_UNSIGNED_INT_24_8, depthFai);
 
 		fbo.create();
-		fbo.setColorAttachments({&normalFai, &diffuseFai, &specularFai});
+		fbo.setColorAttachments({&fai0});
 		fbo.setOtherAttachment(GL_DEPTH_STENCIL_ATTACHMENT, depthFai);
 		ANKI_ASSERT(fbo.isComplete());
 	}

+ 1 - 1
src/renderer/Ssao.cpp

@@ -117,7 +117,7 @@ void Ssao::run()
 
 	// msNormalFai
 	ssaoSProg.findUniformVariableByName("msNormalFai")->set(
-		r->getMs().getNormalFai());
+		r->getMs().getFai0());
 
 	r->drawQuad();
 

+ 61 - 51
src/resource/Image.cpp

@@ -8,9 +8,48 @@
 
 namespace anki {
 
+//==============================================================================
+
 uint8_t Image::tgaHeaderUncompressed[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 uint8_t Image::tgaHeaderCompressed[12] = {0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
+//==============================================================================
+void Image::load(const char* filename)
+{
+	// get the extension
+	const char* ext = getFileExtension(filename);
+	ANKI_ASSERT(ext);
+
+	// load from this extension
+	try
+	{
+		if(strcmp(ext, "tga") == 0)
+		{
+			loadTga(filename);
+		}
+		else if(strcmp(ext, "png") == 0)
+		{
+			std::string err;
+			if(!loadPng(filename, err))
+			{
+				throw ANKI_EXCEPTION(err);
+			}
+		}
+		else if(strcmp(ext, "dds") == 0)
+		{
+			loadDds(filename);
+		}
+		else
+		{
+			throw ANKI_EXCEPTION("Unsupported extension");
+		}
+	}
+	catch(std::exception& e)
+	{
+		throw ANKI_EXCEPTION("File " + filename) << e;
+	}
+}
+
 //==============================================================================
 void Image::loadUncompressedTga(std::fstream& fs, uint32_t& bpp)
 {
@@ -403,51 +442,14 @@ cleanup:
 	return ok;
 }
 
-//==============================================================================
-void Image::load(const char* filename)
-{
-	// get the extension
-	const char* ext = getFileExtension(filename);
-	ANKI_ASSERT(ext);
-
-	// load from this extension
-	try
-	{
-		if(strcmp(ext, "tga") == 0)
-		{
-			loadTga(filename);
-		}
-		else if(strcmp(ext, "png") == 0)
-		{
-			std::string err;
-			if(!loadPng(filename, err))
-			{
-				throw ANKI_EXCEPTION(err);
-			}
-		}
-		else if(strcmp(ext, "dds") == 0)
-		{
-			loadDds(filename);
-		}
-		else
-		{
-			throw ANKI_EXCEPTION("Unsupported extension");
-		}
-	}
-	catch(std::exception& e)
-	{
-		throw ANKI_EXCEPTION("File " + filename) << e;
-	}
-}
-
 //==============================================================================
 // DDS                                                                         =
 //==============================================================================
 
-//  little-endian, of course
+// little-endian, of course
 #define DDS_MAGIC 0x20534444
 
-//  DDS_header.dwFlags
+// DdsHeader.dwFlags
 #define DDSD_CAPS                   0x00000001
 #define DDSD_HEIGHT                 0x00000002
 #define DDSD_WIDTH                  0x00000004
@@ -457,18 +459,18 @@ void Image::load(const char* filename)
 #define DDSD_LINEARSIZE             0x00080000
 #define DDSD_DEPTH                  0x00800000
 
-//  DDS_header.sPixelFormat.dwFlags
+// DdsHeader.sPixelFormat.dwFlags
 #define DDPF_ALPHAPIXELS            0x00000001
 #define DDPF_FOURCC                 0x00000004
 #define DDPF_INDEXED                0x00000020
 #define DDPF_RGB                    0x00000040
 
-//  DDS_header.sCaps.dwCaps1
+// DdsHeader.sCaps.dwCaps1
 #define DDSCAPS_COMPLEX             0x00000008
 #define DDSCAPS_TEXTURE             0x00001000
 #define DDSCAPS_MIPMAP              0x00400000
 
-//  DDS_header.sCaps.dwCaps2
+// DdsHeader.sCaps.dwCaps2
 #define DDSCAPS2_CUBEMAP            0x00000200
 #define DDSCAPS2_CUBEMAP_POSITIVEX  0x00000400
 #define DDSCAPS2_CUBEMAP_NEGATIVEX  0x00000800
@@ -539,7 +541,7 @@ static uint toInt(const char* x)
   ((pf.dwFlags & DDPF_INDEXED) && \
    (pf.dwRGBBitCount == 8))
 
-union DDS_header
+union DdsHeader
 {
 	struct
 	{
@@ -609,7 +611,7 @@ void Image::loadDds(const char* filename)
 
 	// Read header
 	//
-	DDS_header hdr;
+	DdsHeader hdr;
 	in.read((char*)&hdr, sizeof(hdr));
 
 	if(hdr.data.dwMagic != DDS_MAGIC || hdr.data.dwSize != 124
@@ -652,18 +654,26 @@ void Image::loadDds(const char* filename)
 		throw ANKI_EXCEPTION("Currently mipmaps are not supported in DDS");
 	}
 
-	uint x = hdr.data.dwWidth;
-	uint y = hdr.data.dwHeight;
-
 	if(li->compressed)
 	{
-		size_t size = std::max(li->divSize, x)
-			/ li->divSize * std::max(li->divSize, y)
+		size_t size = std::max(li->divSize, hdr.data.dwWidth)
+			/ li->divSize * std::max(li->divSize, hdr.data.dwHeight)
 			/ li->divSize * li->blockBytes;
-		//assert( size == hdr.dwPitchOrLinearSize );
+
+		if(size != hdr.dwPitchOrLinearSize)
+		{
+			throw ANKI_EXCEPTION("Size in header and the calculated are "
+				"not the same");
+		}
+
 		//assert( hdr.dwFlags & DDSD_LINEARSIZE );
 		data.resize(size);
 		in.read((char*)(&data[0]), size);
+
+		if(in.fail() || in.eof())
+		{
+			throw ANKI_EXCEPTION("Error reading file data");
+		}
 	}
 
 	type = CT_RGBA;
@@ -673,4 +683,4 @@ void Image::loadDds(const char* filename)
 	in.close();
 }
 
-} // end namespace
+} // end namespace anki