Ver código fonte

Fixed DX9 lack of non-normalized UINT16 vertex decl type.

bkaradzic 13 anos atrás
pai
commit
e0eca362ad
5 arquivos alterados com 59 adições e 51 exclusões
  1. 27 27
      examples/06-bump/bump.cpp
  2. 9 1
      include/bgfx.h
  3. 4 4
      src/renderer_d3d11.cpp
  4. 1 1
      src/renderer_gl.cpp
  5. 18 18
      src/vertexdecl.cpp

+ 27 - 27
examples/06-bump/bump.cpp

@@ -22,8 +22,8 @@ struct PosNormalTangentTexcoordVertex
 	float m_z;
 	float m_z;
 	uint32_t m_normal;
 	uint32_t m_normal;
 	uint32_t m_tangent;
 	uint32_t m_tangent;
-	float m_u;
-	float m_v;
+	uint16_t m_u;
+	uint16_t m_v;
 };
 };
 
 
 static bgfx::VertexDecl s_PosNormalTangentTexcoordDecl;
 static bgfx::VertexDecl s_PosNormalTangentTexcoordDecl;
@@ -55,30 +55,30 @@ uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
 
 
 static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
 static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
 {
 {
-	{-1.0f,  1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 0.0f, 0.0f },
-	{ 1.0f,  1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 1.0f, 0.0f },
-	{-1.0f, -1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 0.0f, 1.0f },
-	{ 1.0f, -1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 1.0f, 1.0f },
-	{-1.0f,  1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 0.0f, 0.0f },
-	{ 1.0f,  1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 1.0f, 0.0f },
-	{-1.0f, -1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 0.0f, 1.0f },
-	{ 1.0f, -1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 1.0f, 1.0f },
-	{-1.0f,  1.0f,  1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 0.0f, 0.0f },
-	{ 1.0f,  1.0f,  1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 1.0f, 0.0f },
-	{-1.0f,  1.0f, -1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 0.0f, 1.0f },
-	{ 1.0f,  1.0f, -1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 1.0f, 1.0f },
-	{-1.0f, -1.0f,  1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 0.0f, 0.0f },
-	{ 1.0f, -1.0f,  1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 1.0f, 0.0f },
-	{-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 0.0f, 1.0f },
-	{ 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 1.0f, 1.0f },
-	{ 1.0f, -1.0f,  1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 0.0f, 0.0f },
-	{ 1.0f,  1.0f,  1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 1.0f, 0.0f },
-	{ 1.0f, -1.0f, -1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 0.0f, 1.0f },
-	{ 1.0f,  1.0f, -1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 1.0f, 1.0f },
-	{-1.0f, -1.0f,  1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 0.0f, 0.0f },
-	{-1.0f,  1.0f,  1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 1.0f, 0.0f },
-	{-1.0f, -1.0f, -1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 0.0f, 1.0f },
-	{-1.0f,  1.0f, -1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 1.0f, 1.0f },
+	{-1.0f,  1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0,      0,      0 },
+	{ 1.0f,  1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 0x7fff,      0 },
+	{-1.0f, -1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0,      0, 0x7fff },
+	{ 1.0f, -1.0f,  1.0f, packF4u( 0.0f,  0.0f,  1.0f), 0, 0x7fff, 0x7fff },
+	{-1.0f,  1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0,      0,      0 },
+	{ 1.0f,  1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 0x7fff,      0 },
+	{-1.0f, -1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0,      0, 0x7fff },
+	{ 1.0f, -1.0f, -1.0f, packF4u( 0.0f,  0.0f, -1.0f), 0, 0x7fff, 0x7fff },
+	{-1.0f,  1.0f,  1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0,      0,      0 },
+	{ 1.0f,  1.0f,  1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 0x7fff,      0 },
+	{-1.0f,  1.0f, -1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0,      0, 0x7fff },
+	{ 1.0f,  1.0f, -1.0f, packF4u( 0.0f,  1.0f,  0.0f), 0, 0x7fff, 0x7fff },
+	{-1.0f, -1.0f,  1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0,      0,      0 },
+	{ 1.0f, -1.0f,  1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 0x7fff,      0 },
+	{-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0,      0, 0x7fff },
+	{ 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f,  0.0f), 0, 0x7fff, 0x7fff },
+	{ 1.0f, -1.0f,  1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0,      0,      0 },
+	{ 1.0f,  1.0f,  1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 0x7fff,      0 },
+	{ 1.0f, -1.0f, -1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0,      0, 0x7fff },
+	{ 1.0f,  1.0f, -1.0f, packF4u( 1.0f,  0.0f,  0.0f), 0, 0x7fff, 0x7fff },
+	{-1.0f, -1.0f,  1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0,      0,      0 },
+	{-1.0f,  1.0f,  1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 0x7fff,      0 },
+	{-1.0f, -1.0f, -1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0,      0, 0x7fff },
+	{-1.0f,  1.0f, -1.0f, packF4u(-1.0f,  0.0f,  0.0f), 0, 0x7fff, 0x7fff },
 };
 };
 
 
 static const uint16_t s_cubeIndices[36] =
 static const uint16_t s_cubeIndices[36] =
@@ -299,7 +299,7 @@ int _main_(int _argc, char** _argv)
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true);
 	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true);
-	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
+	s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true);
 	s_PosNormalTangentTexcoordDecl.end();
 	s_PosNormalTangentTexcoordDecl.end();
 
 
 	const bgfx::Memory* mem;
 	const bgfx::Memory* mem;

+ 9 - 1
include/bgfx.h

@@ -284,7 +284,7 @@ namespace bgfx
 		enum Enum
 		enum Enum
 		{
 		{
 			Uint8,
 			Uint8,
-			Uint16,
+			Int16,
 			Half,
 			Half,
 			Float,
 			Float,
 
 
@@ -450,6 +450,14 @@ namespace bgfx
 
 
 		/// Add attribute to VertexDecl.
 		/// Add attribute to VertexDecl.
 		///
 		///
+		/// @param _attrib Attribute semantics.
+		/// @param _num Number of elements 1, 2, 3 or 4.
+		/// @param _type Element type.
+		/// @param _normalized Value is normalized for vertex shader.
+		/// @param _asInt Packaging rule for vertexPack, vertexUnpack, and
+		///   vertexConvert for AttribType::Uint8 and AttribType::Int16.
+		///   Unpacking code must be implemented inside vertex shader.
+		///
 		/// NOTE:
 		/// NOTE:
 		///   Must be called between begin/end.
 		///   Must be called between begin/end.
 		void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);
 		void add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized = false, bool _asInt = false);

+ 4 - 4
src/renderer_d3d11.cpp

@@ -194,10 +194,10 @@ namespace bgfx
 			{ DXGI_FORMAT_R8G8B8A8_UINT,      DXGI_FORMAT_R8G8B8A8_UNORM     },
 			{ DXGI_FORMAT_R8G8B8A8_UINT,      DXGI_FORMAT_R8G8B8A8_UNORM     },
 		},
 		},
 		{
 		{
-			{ DXGI_FORMAT_R16_UINT,           DXGI_FORMAT_R16_UNORM          },
-			{ DXGI_FORMAT_R16G16_UINT,        DXGI_FORMAT_R16G16_UNORM       },
-			{ DXGI_FORMAT_R16G16B16A16_UINT,  DXGI_FORMAT_R16G16B16A16_UNORM },
-			{ DXGI_FORMAT_R16G16B16A16_UINT,  DXGI_FORMAT_R16G16B16A16_UNORM },
+			{ DXGI_FORMAT_R16_SINT,           DXGI_FORMAT_R16_SNORM          },
+			{ DXGI_FORMAT_R16G16_SINT,        DXGI_FORMAT_R16G16_SNORM       },
+			{ DXGI_FORMAT_R16G16B16A16_SINT,  DXGI_FORMAT_R16G16B16A16_SNORM },
+			{ DXGI_FORMAT_R16G16B16A16_SINT,  DXGI_FORMAT_R16G16B16A16_SNORM },
 		},
 		},
 		{
 		{
 			{ DXGI_FORMAT_R16_FLOAT,          DXGI_FORMAT_R16_FLOAT          },
 			{ DXGI_FORMAT_R16_FLOAT,          DXGI_FORMAT_R16_FLOAT          },

+ 1 - 1
src/renderer_gl.cpp

@@ -534,7 +534,7 @@ namespace bgfx
 	static const GLenum s_attribType[AttribType::Count] =
 	static const GLenum s_attribType[AttribType::Count] =
 	{
 	{
 		GL_UNSIGNED_BYTE,
 		GL_UNSIGNED_BYTE,
-		GL_UNSIGNED_SHORT,
+		GL_SHORT,
 		GL_HALF_FLOAT,
 		GL_HALF_FLOAT,
 		GL_FLOAT,
 		GL_FLOAT,
 	};
 	};

+ 18 - 18
src/vertexdecl.cpp

@@ -207,7 +207,7 @@ namespace bgfx
 			}
 			}
 			break;
 			break;
 
 
-		case AttribType::Uint16:
+		case AttribType::Int16:
 			{
 			{
 				uint16_t* packed = (uint16_t*)data;
 				uint16_t* packed = (uint16_t*)data;
 				if (_inputNormalized)
 				if (_inputNormalized)
@@ -216,20 +216,20 @@ namespace bgfx
 					{
 					{
 						switch (num)
 						switch (num)
 						{
 						{
-						default: *packed++ = uint16_t(*_input++ * 32767.0f + 32768.0f);
-						case 3:  *packed++ = uint16_t(*_input++ * 32767.0f + 32768.0f);
-						case 2:  *packed++ = uint16_t(*_input++ * 32767.0f + 32768.0f);
-						case 1:  *packed++ = uint16_t(*_input++ * 32767.0f + 32768.0f);
+						default: *packed++ = uint16_t(*_input++ * 32767.0f);
+						case 3:  *packed++ = uint16_t(*_input++ * 32767.0f);
+						case 2:  *packed++ = uint16_t(*_input++ * 32767.0f);
+						case 1:  *packed++ = uint16_t(*_input++ * 32767.0f);
 						}
 						}
 					}
 					}
 					else
 					else
 					{
 					{
 						switch (num)
 						switch (num)
 						{
 						{
-						default: *packed++ = uint16_t(*_input++ * 65535.0f);
-						case 3:  *packed++ = uint16_t(*_input++ * 65535.0f);
-						case 2:  *packed++ = uint16_t(*_input++ * 65535.0f);
-						case 1:  *packed++ = uint16_t(*_input++ * 65535.0f);
+						default: *packed++ = uint16_t(*_input++ * 65535.0f - 32768.0f);
+						case 3:  *packed++ = uint16_t(*_input++ * 65535.0f - 32768.0f);
+						case 2:  *packed++ = uint16_t(*_input++ * 65535.0f - 32768.0f);
+						case 1:  *packed++ = uint16_t(*_input++ * 65535.0f - 32768.0f);
 						}
 						}
 					}
 					}
 				}
 				}
@@ -311,27 +311,27 @@ namespace bgfx
 			}
 			}
 			break;
 			break;
 
 
-		case AttribType::Uint16:
+		case AttribType::Int16:
 			{
 			{
 				uint16_t* packed = (uint16_t*)data;
 				uint16_t* packed = (uint16_t*)data;
 				if (asInt)
 				if (asInt)
 				{
 				{
 					switch (num)
 					switch (num)
 					{
 					{
-					default: *_output++ = (float(*packed++) - 32768.0f)*1.0f/32767.0f;
-					case 3:  *_output++ = (float(*packed++) - 32768.0f)*1.0f/32767.0f;
-					case 2:  *_output++ = (float(*packed++) - 32768.0f)*1.0f/32767.0f;
-					case 1:  *_output++ = (float(*packed++) - 32768.0f)*1.0f/32767.0f;
+					default: *_output++ = float(*packed++)*1.0f/32767.0f;
+					case 3:  *_output++ = float(*packed++)*1.0f/32767.0f;
+					case 2:  *_output++ = float(*packed++)*1.0f/32767.0f;
+					case 1:  *_output++ = float(*packed++)*1.0f/32767.0f;
 					}
 					}
 				}
 				}
 				else
 				else
 				{
 				{
 					switch (num)
 					switch (num)
 					{
 					{
-					default: *_output++ = float(*packed++)*1.0f/65535.0f;
-					case 3:  *_output++ = float(*packed++)*1.0f/65535.0f;
-					case 2:  *_output++ = float(*packed++)*1.0f/65535.0f;
-					case 1:  *_output++ = float(*packed++)*1.0f/65535.0f;
+					default: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
+					case 3:  *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
+					case 2:  *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
+					case 1:  *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
 					}
 					}
 				}
 				}
 			}
 			}