Просмотр исходного кода

Fixed the issue with OpenGL texture endianess
Fixed many issues with GL textures and pixel formats
Added GL builtin materials

Marko Pintera 13 лет назад
Родитель
Сommit
7a11f00bea

+ 121 - 2
BansheeEngine/Source/BsGLBuiltinMaterialFactory.cpp

@@ -5,6 +5,7 @@
 #include "CmPass.h"
 #include "CmMaterial.h"
 #include "CmBlendState.h"
+#include "CmRendererManager.h"
 
 using namespace CamelotFramework;
 
@@ -41,11 +42,129 @@ namespace BansheeEngine
 
 	void GLBuiltinMaterialFactory::initSpriteTextShader()
 	{
-		// TODO
+		String vsCode = "#version 400\n							\
+						\
+						uniform float halfViewportWidth;		\
+						uniform float halfViewportHeight;		\
+						uniform mat4 worldTransform;			\
+						\
+						in vec3 cm_position;					\
+						in vec2 cm_texcoord0;					\
+						out vec2 texcoord0;						\
+						void main()							\
+						{																	\
+						vec4 tfrmdPos = worldTransform * vec4(cm_position.xy, 0, 1);		\
+						\
+						float tfrmdX = (tfrmdPos.x / halfViewportWidth) - 1.0f;	\
+						float tfrmdY = (tfrmdPos.y / halfViewportHeight) + 1.0f;	\
+						\
+						gl_Position = vec4(tfrmdX, tfrmdY, 0, 1);							\
+						texcoord0 = cm_texcoord0;											\
+						}																	\
+						";
+
+		String psCode = "#version 400\n										\
+						\
+						uniform sampler2D mainTexture;						\
+						in vec2 texcoord0;									\
+						out vec4 fragColor;									\
+						\
+						void main()										\
+						{													\
+						vec4 color = vec4(1.0f, 1.0f, 1.0f, texture2D(mainTexture, texcoord0.st).r);	\
+						fragColor = color;									\
+						}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
+
+		vsProgram.waitUntilLoaded();
+		psProgram.waitUntilLoaded();
+
+		mSpriteTextShader = Shader::create("TextSpriteShader");
+
+		mSpriteTextShader->addParameter("worldTransform", "worldTransform", GPDT_MATRIX_4X4);
+		mSpriteTextShader->addParameter("halfViewportWidth", "halfViewportWidth", GPDT_FLOAT1);
+		mSpriteTextShader->addParameter("halfViewportHeight", "halfViewportHeight", GPDT_FLOAT1);
+		mSpriteTextShader->addParameter("mainTexSamp", "mainTexture", GPOT_SAMPLER2D);
+		mSpriteTextShader->addParameter("mainTexture", "mainTexture", GPOT_TEXTURE2D);
+
+		TechniquePtr newTechnique = mSpriteTextShader->addTechnique("GLRenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		BLEND_STATE_DESC desc;
+		desc.renderTargetDesc[0].blendEnable = true;
+		desc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].blendOp = BO_ADD;
+
+		HBlendState blendState = BlendState::create(desc);
+		newPass->setBlendState(blendState);
 	}
 
 	void GLBuiltinMaterialFactory::initSpriteImageShader()
 	{
-		// TODO
+		String vsCode = "#version 400\n							\
+						\
+						uniform float halfViewportWidth;		\
+						uniform float halfViewportHeight;		\
+						uniform mat4 worldTransform;			\
+						\
+						in vec3 cm_position;					\
+						in vec2 cm_texcoord0;					\
+						out vec2 texcoord0;						\
+						void main()							\
+						{																	\
+						vec4 tfrmdPos = worldTransform * vec4(cm_position.xy, 0, 1);		\
+						\
+						float tfrmdX = (tfrmdPos.x / halfViewportWidth) - 1.0f;	\
+						float tfrmdY = (tfrmdPos.y / halfViewportHeight) + 1.0f;	\
+						\
+						gl_Position = vec4(tfrmdX, tfrmdY, 0, 1);							\
+						texcoord0 = cm_texcoord0;											\
+						}																	\
+						";
+
+		String psCode = "#version 400\n										\
+																			\
+						uniform sampler2D mainTexture;						\
+						in vec2 texcoord0;									\
+						out vec4 fragColor;									\
+																			\
+						void main()										\
+						{													\
+						vec4 color = texture2D(mainTexture, texcoord0.st);	\
+						fragColor = color;									\
+						}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
+
+		vsProgram.waitUntilLoaded();
+		psProgram.waitUntilLoaded();
+
+		mSpriteImageShader = Shader::create("ImageSpriteShader");
+
+		mSpriteImageShader->addParameter("worldTransform", "worldTransform", GPDT_MATRIX_4X4);
+		mSpriteImageShader->addParameter("halfViewportWidth", "halfViewportWidth", GPDT_FLOAT1);
+		mSpriteImageShader->addParameter("halfViewportHeight", "halfViewportHeight", GPDT_FLOAT1);
+		mSpriteImageShader->addParameter("mainTexSamp", "mainTexture", GPOT_SAMPLER2D);
+		mSpriteImageShader->addParameter("mainTexture", "mainTexture", GPOT_TEXTURE2D);
+
+		TechniquePtr newTechnique = mSpriteImageShader->addTechnique("GLRenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		BLEND_STATE_DESC desc;
+		desc.renderTargetDesc[0].blendEnable = true;
+		desc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].blendOp = BO_ADD;
+
+		HBlendState blendState = BlendState::create(desc);
+		newPass->setBlendState(blendState);
 	}
 }

+ 2 - 2
CamelotClient/CamelotClient.cpp

@@ -28,9 +28,9 @@
 #include "CmTestTextSprite.h"
 #include "CmEditorWindow.h"
 
-#define DX11
+//#define DX11
 //#define DX9
-//#define GL
+#define GL
 
 using namespace CamelotFramework;
 using namespace CamelotEditor;

+ 18 - 22
CamelotGLRenderer/Source/CmGLPixelFormat.cpp

@@ -42,21 +42,19 @@ namespace CamelotFramework  {
 			case PF_R8G8:
 				return GL_RG;
             case PF_R8G8B8:
-                return GL_BGR;
-            case PF_B8G8R8:
                 return GL_RGB;
-			case PF_X8R8G8B8:
+            case PF_B8G8R8:
+                return GL_BGR;
+			case PF_R8G8B8X8:
+			case PF_R8G8B8A8:
 			case PF_A8R8G8B8:
-				return GL_BGRA;
-			case PF_X8B8G8R8:
-            case PF_A8B8G8R8:
+			case PF_X8R8G8B8:
                 return GL_RGBA;
             case PF_B8G8R8A8:
 			case PF_B8G8R8X8:
+			case PF_A8B8G8R8:
+			case PF_X8B8G8R8:
                 return GL_BGRA;
-			case PF_R8G8B8X8:
-			case PF_R8G8B8A8:
-				return GL_RGBA;
 			case PF_FLOAT16_R:
                 return GL_RED;
 			case PF_FLOAT16_RG:
@@ -92,19 +90,19 @@ namespace CamelotFramework  {
             case PF_R8G8B8:
             case PF_B8G8R8:
 			case PF_R8G8:
-                return GL_UNSIGNED_BYTE;
+                return GL_UNSIGNED_INT_8_8_8_8_REV;
 			case PF_X8B8G8R8:
 			case PF_A8B8G8R8:
-                return GL_UNSIGNED_BYTE;
+                return GL_UNSIGNED_INT_8_8_8_8;
 			case PF_X8R8G8B8:
             case PF_A8R8G8B8:
-				return GL_UNSIGNED_BYTE;
+				return GL_UNSIGNED_INT_8_8_8_8;
             case PF_B8G8R8A8:
 			case PF_B8G8R8X8:
-                return GL_UNSIGNED_INT_8_8_8_8;
+                return GL_UNSIGNED_INT_8_8_8_8_REV;
 			case PF_R8G8B8A8:
 			case PF_R8G8B8X8:
-				return GL_UNSIGNED_INT_8_8_8_8;
+				return GL_UNSIGNED_INT_8_8_8_8_REV;
 			case PF_FLOAT16_R:
 			case PF_FLOAT16_RG:
             case PF_FLOAT16_RGB:
@@ -154,24 +152,22 @@ namespace CamelotFramework  {
 				return GL_RG8;
             case PF_R8G8B8:
             case PF_B8G8R8:
-			case PF_X8B8G8R8:
-			case PF_X8R8G8B8:
 			case PF_B8G8R8X8:
 			case PF_R8G8B8X8:
 				if (hwGamma)
 					return GL_SRGB8;
 				else
 					return GL_RGB8;
-            case PF_A8R8G8B8:
             case PF_B8G8R8A8:
+			case PF_R8G8B8A8:
 				if (hwGamma)
 					return GL_SRGB8_ALPHA8;
 				else
 					return GL_RGBA8;
 			case PF_FLOAT16_R:
-				return GL_LUMINANCE16F_ARB;
+				return GL_R16F;
             case PF_FLOAT16_RGB:
-                return GL_RGB16F_ARB;
+                return GL_RGB16F;
 			case PF_FLOAT16_RG: 
 				return GL_RG16F;
             case PF_FLOAT16_RGBA:
@@ -254,10 +250,10 @@ namespace CamelotFramework  {
 			return PF_R8G8;
 		case GL_RGB8:
 		case GL_SRGB8:
-			return PF_X8R8G8B8;
+			return PF_R8G8B8X8;
 		case GL_RGBA8:
 		case GL_SRGB8_ALPHA8:
-			return PF_A8R8G8B8;
+			return PF_R8G8B8A8;
 		case GL_R16F:
 			return PF_FLOAT16_R;
 		case GL_RG16F:
@@ -286,7 +282,7 @@ namespace CamelotFramework  {
 		case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
 			return PF_DXT5;
 		default:
-			return PF_A8R8G8B8;
+			return PF_R8G8B8A8;
 		};
 	}
 	//----------------------------------------------------------------------------- 

+ 5 - 0
CamelotGLRenderer/Source/CmGLTexture.cpp

@@ -194,6 +194,11 @@ namespace CamelotFramework {
 		}
 
 		createSurfaceList();
+
+		PixelBufferPtr buffer = getBuffer(0, 0);
+
+		if(buffer != nullptr)
+			mFormat = buffer->getFormat();
 	}
 
 	void GLTexture::destroy_internal()

+ 9 - 0
Notes.txt

@@ -16,7 +16,16 @@ Reminders:
   - When I'll be doing SRGB write make sure GUI textures are handled properly. Right now they are read in gamma space, and displayed
     as normal, but if I switch to SRGB write then gamma would be applied twice to those textures.
   - Make editor background have a tileable pattern (see Mini paper app on my cell). Since tileable images wouldn't work with scale9grid, it should be an overlay over the solid background.
+    - Maybe use the background type I used for website in WebDIP?
+    - Use black with yellow-orange highlights (highlight = selected button, selected frame border, selected entry box, etc.)
 
+Potential optimizations:
+ - bulkPixelConversion is EXTREMELY poorly unoptimized. Each pixel it calls a separate method that does redudant operations every pixel.
+ - UI buffer updates
+   - https://developer.nvidia.com/sites/default/files/akamai/gamedev/files/gdc12/Efficient_Buffer_Management_McDonald.pdf
+   - See here how to avoid locks and stalls when updating just parts of a GUI buffer. In need to modify my mesh update code because it currently always updates 100% of the buffer.
+  - Maybe get rid of CPU UI buffers completely.
+ - UI shader resolution params for gui should be in a separate constant buffer
 ----------------------------------------------------------------------------------------------
 More detailed thought out system descriptions:
 

+ 3 - 0
TODO.txt

@@ -8,6 +8,9 @@ When loading WindowFrameTex manually, it doesn't get released properly
   - Although more than likely I am loading the model incorrectly since it works in Unity?
   - I probably want to determine front faces based on normals
 
+invalid operation in GL. Most likely created by enabling a certain vertex attrib without disabling it when using another shader. 
+  - try just using text shader without mesh and see if that helps
+
 Immediate TODO:
  - Issue with rendering same object from multiple cameras:
    - Material parameters should be copied after being submitted to the render context