Browse Source

Making readPixels from texture a GL desktop only, Replace the FP minmax attachment to UI to be GLES3 comformant, optimize minmax shader

Panagiotis Christopoulos Charitos 13 năm trước cách đây
mục cha
commit
c553a0f7cc

+ 1 - 1
include/anki/gl/Texture.h

@@ -253,7 +253,7 @@ public:
 	/// Generate new mipmaps
 	/// Generate new mipmaps
 	void genMipmap();
 	void genMipmap();
 
 
-	/// Read the data from the texture
+	/// Read the data from the texture. Available only in GL desktop
 	void readPixels(void* data, U level = 0);
 	void readPixels(void* data, U level = 0);
 
 
 private:
 private:

+ 8 - 21
shaders/IsMinMax.glsl

@@ -16,46 +16,33 @@ uniform sampler2D depthMap;
 
 
 in vec2 vTexCoords;
 in vec2 vTexCoords;
 
 
-out vec2 fColor;
+out uvec2 fColor;
 
 
 layout(pixel_center_integer) in vec4 gl_FragCoord;
 layout(pixel_center_integer) in vec4 gl_FragCoord;
 
 
+#define W (RENDERER_WIDTH / TILES_X_COUNT)
+#define H (RENDERER_HEIGHT / TILES_Y_COUNT)
+
 //==============================================================================
 //==============================================================================
 void main()
 void main()
 {
 {
-	const int W = RENDERER_WIDTH / TILES_X_COUNT;
-	const int H = RENDERER_HEIGHT / TILES_Y_COUNT;
+	const ivec2 coord = ivec2(gl_FragCoord.xy * vec2(W, H));
 
 
 	float maxDepth = -10000.0;
 	float maxDepth = -10000.0;
 	float minDepth = 100000.0;
 	float minDepth = 100000.0;
 
 
-	ivec2 coord = ivec2(gl_FragCoord.xy * vec2(W, H));
-
 	for(int i = 0; i < W; i++)
 	for(int i = 0; i < W; i++)
 	{
 	{
 		for(int j = 0; j < H; j++)
 		for(int j = 0; j < H; j++)
 		{
 		{
 			float depth = texelFetch(depthMap, coord + ivec2(i, j), 0).r;
 			float depth = texelFetch(depthMap, coord + ivec2(i, j), 0).r;
 
 
-			if(depth < minDepth)
-			{
-				minDepth = depth;
-			}
-			if(depth > maxDepth)
-			{
-				maxDepth = depth;
-			}
+			minDepth = min(depth, minDepth);
+			maxDepth = max(depth, maxDepth);
 		}
 		}
 	}
 	}
 
 
-#if 0
-	float linearMin = linearizeDepth(minDepth, near, far);
-	float linearMax = linearizeDepth(maxDepth, near, far);
-	fColor = vec2(linearMin,
-		linearMax);
-#else
-	fColor = vec2(minDepth, maxDepth);
-#endif
+	fColor = uvec2(floatBitsToUint(minDepth), floatBitsToUint(maxDepth));
 }
 }
 
 
 
 

+ 5 - 4
src/gl/ShaderProgram.cpp

@@ -434,7 +434,7 @@ void ShaderProgram::destroy()
 GLuint ShaderProgram::createAndCompileShader(const char* sourceCode,
 GLuint ShaderProgram::createAndCompileShader(const char* sourceCode,
 	const char* preproc, GLenum type)
 	const char* preproc, GLenum type)
 {
 {
-	uint glId = 0;
+	GLuint glId = 0;
 	const char* sourceStrs[1] = {nullptr};
 	const char* sourceStrs[1] = {nullptr};
 
 
 	// create the shader
 	// create the shader
@@ -449,14 +449,14 @@ GLuint ShaderProgram::createAndCompileShader(const char* sourceCode,
 	glShaderSource(glId, 1, sourceStrs, NULL);
 	glShaderSource(glId, 1, sourceStrs, NULL);
 	glCompileShader(glId);
 	glCompileShader(glId);
 
 
-	int success;
+	GLint success;
 	glGetShaderiv(glId, GL_COMPILE_STATUS, &success);
 	glGetShaderiv(glId, GL_COMPILE_STATUS, &success);
 
 
 	if(!success)
 	if(!success)
 	{
 	{
 		// Get info log
 		// Get info log
-		int infoLen = 0;
-		int charsWritten = 0;
+		GLint infoLen = 0;
+		GLint charsWritten = 0;
 		Vector<char> infoLog;
 		Vector<char> infoLog;
 
 
 		glGetShaderiv(glId, GL_INFO_LOG_LENGTH, &infoLen);
 		glGetShaderiv(glId, GL_INFO_LOG_LENGTH, &infoLen);
@@ -483,6 +483,7 @@ GLuint ShaderProgram::createAndCompileShader(const char* sourceCode,
 		// Throw
 		// Throw
 		throw ANKI_EXCEPTION(err.str());
 		throw ANKI_EXCEPTION(err.str());
 	}
 	}
+	ANKI_ASSERT(glId != 0);
 
 
 	return glId;
 	return glId;
 }
 }

+ 4 - 0
src/gl/Texture.cpp

@@ -273,8 +273,12 @@ void Texture::setFilteringNoBind(TextureFilteringType filterType) const
 //==============================================================================
 //==============================================================================
 void Texture::readPixels(void* pixels, U level)
 void Texture::readPixels(void* pixels, U level)
 {
 {
+#if ANKI_GL == ANKI_GL_DESKTOP
 	TextureUnitsSingleton::get().bindTextureAndActivateUnit(*this);
 	TextureUnitsSingleton::get().bindTextureAndActivateUnit(*this);
 	glGetTexImage(target, level, format, type, pixels);
 	glGetTexImage(target, level, format, type, pixels);
+#else
+	ANKI_ASSERT(0 && "Not supported on GLES");
+#endif
 }
 }
 
 
 } // end namespace anki
 } // end namespace anki

+ 8 - 2
src/renderer/Is.cpp

@@ -334,8 +334,8 @@ void Is::initInternal(const RendererInitializer& initializer)
 	}
 	}
 
 
 	// min max FBO
 	// min max FBO
-	Renderer::createFai(TILES_X_COUNT, TILES_Y_COUNT, GL_RG32F, GL_RG,
-		GL_FLOAT, minMaxFai);
+	Renderer::createFai(TILES_X_COUNT, TILES_Y_COUNT, GL_RG32UI,
+		GL_RG_INTEGER, GL_UNSIGNED_INT, minMaxFai);
 	minMaxFai.setFiltering(Texture::TFT_NEAREST);
 	minMaxFai.setFiltering(Texture::TFT_NEAREST);
 	minMaxTilerFbo.create();
 	minMaxTilerFbo.create();
 	minMaxTilerFbo.setColorAttachments({&minMaxFai});
 	minMaxTilerFbo.setColorAttachments({&minMaxFai});
@@ -444,7 +444,13 @@ void Is::updateTiles()
 	r->drawQuad();
 	r->drawQuad();
 
 
 	F32 pixels[TILES_Y_COUNT][TILES_X_COUNT][2];
 	F32 pixels[TILES_Y_COUNT][TILES_X_COUNT][2];
+#if ANKI_GL == ANKI_GL_DESKTOP
+	// It seems read from texture is a bit faster than readpixels on nVidia
 	minMaxFai.readPixels(pixels);
 	minMaxFai.readPixels(pixels);
+#else
+	glReadPixels(0, 0, TILES_X_COUNT, TILES_Y_COUNT, GL_RG_INTEGER,
+		GL_UNSIGNED_INT, &pixels[0][0][0]);
+#endif
 
 
 	// Update the rest of the tile stuff in parallel
 	// Update the rest of the tile stuff in parallel
 	// 
 	// 

+ 1 - 1
src/renderer/Renderer.cpp

@@ -134,7 +134,7 @@ void Renderer::createFai(U32 width, U32 height, int internalFormat,
 	init.type = type;
 	init.type = type;
 	init.data = NULL;
 	init.data = NULL;
 	init.mipmapping = false;
 	init.mipmapping = false;
-	init.filteringType = Texture::TFT_LINEAR;
+	init.filteringType = Texture::TFT_NEAREST;
 	init.repeat = false;
 	init.repeat = false;
 	init.anisotropyLevel = 0;
 	init.anisotropyLevel = 0;
 
 

+ 2 - 2
src/resource/MaterialShaderProgramCreator.cpp

@@ -34,7 +34,7 @@ void MaterialShaderProgramCreator::parseShaderProgramTag(
 
 
 	// Create block
 	// Create block
 	StringList block;
 	StringList block;
-	block.push_back("layout(std140, row_major, binding = 0) "
+	block.push_back("/*layout(std140, row_major, binding = 0) "
 		"uniform commonBlock\n{");
 		"uniform commonBlock\n{");
 	for(Input* in : inputs)
 	for(Input* in : inputs)
 	{
 	{
@@ -46,7 +46,7 @@ void MaterialShaderProgramCreator::parseShaderProgramTag(
 
 
 		block.push_back("\tuniform " + in->type + " " + in->name + "_;");
 		block.push_back("\tuniform " + in->type + " " + in->name + "_;");
 	}
 	}
-	block.push_back("};\n");
+	block.push_back("};*/\n");
 
 
 	if(block.size() > 2)
 	if(block.size() > 2)
 	{
 	{