Browse Source

Refactoring/fixing comparison sampler states
Don't assign default sampler state parameters if user didn't declare one in shader code

BearishSun 8 years ago
parent
commit
f7c66134dd

+ 33 - 34
Source/BansheeCore/Include/BsCommonTypes.h

@@ -9,8 +9,8 @@ namespace bs
 	 */
 
 	/**	Factors used when blending new pixels with existing pixels. */
-    enum BlendFactor
-    {
+	enum BlendFactor
+	{
 		BF_ONE, /**< Use a value of one for all pixel components. */
 		BF_ZERO, /**< Use a value of zero for all pixel components. */
 		BF_DEST_COLOR, /**< Use the existing pixel value. */
@@ -21,7 +21,7 @@ namespace bs
 		BF_SOURCE_ALPHA, /**< Use the newly generated alpha value. */
 		BF_INV_DEST_ALPHA, /**< Use the inverse of the existing alpha value. */
 		BF_INV_SOURCE_ALPHA /**< Use the inverse of the newly generated alpha value. */
-    };
+	};
 
 	/**	Operations that determines how are blending factors combined. */
 	enum BlendOperation
@@ -34,46 +34,45 @@ namespace bs
 	};
 
 	/**	Comparison functions used for the depth/stencil buffer. */
-    enum CompareFunction
-    {
+	enum CompareFunction
+	{
 		CMPF_ALWAYS_FAIL, /**< Operation will always fail. */
 		CMPF_ALWAYS_PASS, /**< Operation will always pass. */
 		CMPF_LESS, /**< Operation will pass if the new value is less than existing value. */
-        CMPF_LESS_EQUAL, /**< Operation will pass if the new value is less or equal than existing value. */
-        CMPF_EQUAL, /**< Operation will pass if the new value is equal to the existing value. */
-        CMPF_NOT_EQUAL, /**< Operation will pass if the new value is not equal to the existing value. */
-        CMPF_GREATER_EQUAL, /**< Operation will pass if the new value greater or equal than the existing value. */
-        CMPF_GREATER /**< Operation will pass if the new value greater than the existing value. */
-    };
+		CMPF_LESS_EQUAL, /**< Operation will pass if the new value is less or equal than existing value. */
+		CMPF_EQUAL, /**< Operation will pass if the new value is equal to the existing value. */
+		CMPF_NOT_EQUAL, /**< Operation will pass if the new value is not equal to the existing value. */
+		CMPF_GREATER_EQUAL, /**< Operation will pass if the new value greater or equal than the existing value. */
+		CMPF_GREATER /**< Operation will pass if the new value greater than the existing value. */
+	};
 
 	/**
 	 * Types of texture addressing modes that determine what happens when texture coordinates are outside of the valid range.
 	 */
-    enum TextureAddressingMode
-    {
+	enum TextureAddressingMode
+	{
 		TAM_WRAP, /**< Coordinates wrap back to the valid range. */
 		TAM_MIRROR, /**< Coordinates flip every time the size of the valid range is passed. */
 		TAM_CLAMP, /**< Coordinates are clamped within the valid range. */
 		TAM_BORDER /**< Coordinates outside of the valid range will return a separately set border color. */
-    };
+	};
 
 	/**	Types of available filtering situations. */
-    enum FilterType
-    {
+	enum FilterType
+	{
 		FT_MIN, /**< The filter used when shrinking a texture. */
-        FT_MAG, /**< The filter used when magnifying a texture. */
-        FT_MIP /**< The filter used when filtering between mipmaps. */
-    };
+		FT_MAG, /**< The filter used when magnifying a texture. */
+		FT_MIP /**< The filter used when filtering between mipmaps. */
+	};
 
 	/**	Filtering options for textures. */
-    enum FilterOptions
-    {
+	enum FilterOptions
+	{
 		FO_NONE = 0, /**< Use no filtering. Only relevant for mipmap filtering. */
 		FO_POINT = 1, /**< Filter using the nearest found pixel. Most basic filtering. */
 		FO_LINEAR = 2, /**< Average a 2x2 pixel area, signifies bilinear filtering for texture, trilinear for mipmaps. */
 		FO_ANISOTROPIC = 3, /**< More advanced filtering that improves quality when viewing textures at a steep angle */
-		FO_USE_COMPARISON = 4 /**< Specifies that the sampled values will be compared against existing sampled data. Should be OR-ed with other filtering options. */
-    };
+	};
 
 	/**	Types of frame buffers. */
 	enum FrameBufferType
@@ -88,19 +87,19 @@ namespace bs
 	 * be used for determining front or back facing polygons by checking the order of its vertices from the render 
 	 * perspective.
 	 */
-    enum CullingMode
-    {
+	enum CullingMode
+	{
 		CULL_NONE = 0, /**< Hardware performs no culling and renders both sides. */
 		CULL_CLOCKWISE = 1, /**< Hardware culls faces that have a clockwise vertex ordering. */
-        CULL_COUNTERCLOCKWISE = 2 /**< Hardware culls faces that have a counter-clockwise vertex ordering. */
-    };
+		CULL_COUNTERCLOCKWISE = 2 /**< Hardware culls faces that have a counter-clockwise vertex ordering. */
+	};
 
 	/**	Polygon mode to use when rasterizing. */
-    enum PolygonMode
-    {
+	enum PolygonMode
+	{
 		PM_WIREFRAME = 1, /**< Render as wireframe showing only polygon outlines. */
-        PM_SOLID = 2 /**< Render as solid showing whole polygons. */
-    };
+		PM_SOLID = 2 /**< Render as solid showing whole polygons. */
+	};
 
 	/**	Types of action that can happen on the stencil buffer. */
 	enum StencilOperation
@@ -140,7 +139,7 @@ namespace bs
 		 * Allows you to write to the buffer. Can cause a CPU-GPU sync point so avoid using it often (every frame) as 
 		 * that might limit your performance significantly.
 		 */
-        GBL_READ_WRITE,
+		GBL_READ_WRITE,
 		/**
 		 * Allows you to write to the buffer. Tells the driver to completely discard the contents of the buffer you are 
 		 * writing to. The driver will (most likely) internally allocate another buffer with same specifications (which is
@@ -160,7 +159,7 @@ namespace bs
 		 * that is currently used. This will also avoid CPU-GPU stalls, without requiring you to discard the entire buffer.
 		 * However it is hard to guarantee when GPU has finished using a buffer.
 		 */
-        GBL_WRITE_ONLY_NO_OVERWRITE,
+		GBL_WRITE_ONLY_NO_OVERWRITE,
 		/** Allows you to both read and write to a buffer. */
 		GBL_WRITE_ONLY	
 	};
@@ -187,7 +186,7 @@ namespace bs
 		 * Signifies that you don't plan on modifying the buffer often (or at all) after creation. Modifying such buffer 
 		 * will involve a larger performance hit. Mutually exclusive with GBU_DYNAMIC.
 		 */
-        GBU_STATIC = 0x01,
+		GBU_STATIC = 0x01,
 		/** 
 		 * Signifies that you will modify this buffer fairly often (e.g. every frame). Mutually exclusive with GBU_STATIC. 
 		 */

+ 4 - 7
Source/BansheeD3D11RenderAPI/Source/BsD3D11SamplerState.cpp

@@ -37,13 +37,9 @@ namespace bs { namespace ct
 		samplerState.MinLOD = mProperties.getMinimumMip();
 		samplerState.MipLODBias = mProperties.getTextureMipmapBias();
 
-		bool isComparison = ((mProperties.getTextureFiltering(FT_MIN) & FO_USE_COMPARISON) & 
-			(mProperties.getTextureFiltering(FT_MAG) & FO_USE_COMPARISON) &
-			(mProperties.getTextureFiltering(FT_MIP) & FO_USE_COMPARISON)) != 0;
-
-		FilterOptions minFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MIN) & ~FO_USE_COMPARISON);
-		FilterOptions magFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MAG) & ~FO_USE_COMPARISON);
-		FilterOptions mipFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MIP) & ~FO_USE_COMPARISON);
+		FilterOptions minFilter = mProperties.getTextureFiltering(FT_MIN);
+		FilterOptions magFilter = mProperties.getTextureFiltering(FT_MAG);
+		FilterOptions mipFilter = mProperties.getTextureFiltering(FT_MIP);
 
 		if (minFilter == FO_ANISOTROPIC && magFilter == FO_ANISOTROPIC && mipFilter == FO_ANISOTROPIC)
 		{
@@ -87,6 +83,7 @@ namespace bs { namespace ct
 			}
 		}
 
+		bool isComparison = mProperties.getComparisonFunction() != CMPF_ALWAYS_PASS;
 		if(isComparison)
 		{
 			// Adds COMPARISON flag to the filter

+ 14 - 8
Source/BansheeGLRenderAPI/Include/BsGLRenderAPI.h

@@ -15,11 +15,11 @@ namespace bs { namespace ct
 	 */
 
 	/** Implementation of a render system using OpenGL. Provides abstracted access to various low level OpenGL methods. */
-    class GLRenderAPI : public RenderAPI
-    {
-    public:
-        GLRenderAPI();
-        ~GLRenderAPI();
+	class GLRenderAPI : public RenderAPI
+	{
+	public:
+		GLRenderAPI();
+		~GLRenderAPI();
 
 		/** @copydoc RenderAPI::getName() */
 		const StringID& getName() const override;
@@ -179,13 +179,13 @@ namespace bs { namespace ct
 		 * Sets the texture addressing mode for a texture unit. This determines how are UV address values outside of [0, 1]
 		 * range handled when sampling from texture.
 		 */
-        void setTextureAddressingMode(UINT16 unit, const UVWAddressingMode& uvw);
+		void setTextureAddressingMode(UINT16 unit, const UVWAddressingMode& uvw);
 
 		/**
 		 * Sets the texture border color for a texture unit. Border color determines color returned by the texture sampler
 		 * when border addressing mode is used and texture address is outside of [0, 1] range.
 		 */
-        void setTextureBorderColor(UINT16 unit, const Color& color);
+		void setTextureBorderColor(UINT16 unit, const Color& color);
 
 		/**
 		 * Sets the mipmap bias value for a given texture unit. Bias allows	you to adjust the mipmap selection calculation.
@@ -198,10 +198,16 @@ namespace bs { namespace ct
 		 * Allows you to specify how is the texture bound to the specified texture unit filtered. Different filter types are
 		 * used for different situations like magnifying or minifying a texture.
 		 */
-        void setTextureFiltering(UINT16 unit, FilterType ftype, FilterOptions filter);
+		void setTextureFiltering(UINT16 unit, FilterType ftype, FilterOptions filter);
 
 		/**	Sets anisotropy value for the specified texture unit. */
 		void setTextureAnisotropy(UINT16 unit, UINT32 maxAnisotropy);
+		
+		/** 
+		 * Sets the compare mode to use when sampling the texture (anything but "always" implies the use of a shadow 
+		 * sampler. 
+		 */
+		void setTextureCompareMode(UINT16 unit, CompareFunction compare);
 
 		/**	Gets anisotropy value for the specified texture unit. */
 		GLfloat getCurrentAnisotropy(UINT16 unit);

+ 12 - 0
Source/BansheeGLRenderAPI/Source/BsGLRenderAPI.cpp

@@ -498,6 +498,7 @@ namespace bs { namespace ct
 							setTextureFiltering(unit, FT_MIP, stateProps.getTextureFiltering(FT_MIP));
 
 							setTextureAnisotropy(unit, stateProps.getTextureAnisotropy());
+							setTextureCompareMode(unit, stateProps.getComparisonFunction());
 							setTextureMipmapBias(unit, stateProps.getTextureMipmapBias());
 
 							const UVWAddressingMode& uvw = stateProps.getTextureAddressingMode();
@@ -1748,6 +1749,17 @@ namespace bs { namespace ct
 			glTexParameterf(mTextureInfos[unit].type, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
 	}
 
+	void GLRenderAPI::setTextureCompareMode(UINT16 unit, CompareFunction compare)
+	{
+		if (compare == CMPF_ALWAYS_PASS)
+			glTexParameteri(mTextureInfos[unit].type, GL_TEXTURE_COMPARE_MODE, GL_NONE);
+		else
+		{
+			glTexParameteri(mTextureInfos[unit].type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
+			glTexParameteri(mTextureInfos[unit].type, GL_TEXTURE_COMPARE_FUNC, convertCompareFunction(compare));
+		}
+	}
+
 	bool GLRenderAPI::activateGLTextureUnit(UINT16 unit)
 	{
 		if (mActiveTextureUnit != unit)

+ 10 - 46
Source/BansheeSL/Source/BsSLFXCompiler.cpp

@@ -342,95 +342,59 @@ namespace bs
 		switch (sampState.filter)
 		{
 		case Xsc::Reflection::Filter::MinMagMipPoint: 
+		case Xsc::Reflection::Filter::ComparisonMinMagMipPoint: 
 			desc.minFilter = FO_POINT;
 			desc.magFilter = FO_POINT;
 			desc.mipFilter = FO_POINT;
 			break;
 		case Xsc::Reflection::Filter::MinMagPointMipLinear: 
+		case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear: 
 			desc.minFilter = FO_POINT;
 			desc.magFilter = FO_POINT;
 			desc.mipFilter = FO_LINEAR;
 			break;
 		case Xsc::Reflection::Filter::MinPointMagLinearMipPoint: 
+		case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint: 
 			desc.minFilter = FO_POINT;
 			desc.magFilter = FO_LINEAR;
 			desc.mipFilter = FO_POINT;
 			break;
 		case Xsc::Reflection::Filter::MinPointMagMipLinear: 
+		case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear: 
 			desc.minFilter = FO_POINT;
 			desc.magFilter = FO_LINEAR;
 			desc.mipFilter = FO_LINEAR;
 			break;
 		case Xsc::Reflection::Filter::MinLinearMagMipPoint: 
+		case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint: 
 			desc.minFilter = FO_LINEAR;
 			desc.magFilter = FO_POINT;
 			desc.mipFilter = FO_POINT;
 			break;
 		case Xsc::Reflection::Filter::MinLinearMagPointMipLinear: 
+		case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear: 
 			desc.minFilter = FO_LINEAR;
 			desc.magFilter = FO_POINT;
 			desc.mipFilter = FO_LINEAR;
 			break;
 		case Xsc::Reflection::Filter::MinMagLinearMipPoint: 
+		case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint: 
 			desc.minFilter = FO_LINEAR;
 			desc.magFilter = FO_LINEAR;
 			desc.mipFilter = FO_POINT;
 			break;
 		case Xsc::Reflection::Filter::MinMagMipLinear: 
+		case Xsc::Reflection::Filter::ComparisonMinMagMipLinear: 
 			desc.minFilter = FO_LINEAR;
 			desc.magFilter = FO_LINEAR;
 			desc.mipFilter = FO_LINEAR;
 			break;
 		case Xsc::Reflection::Filter::Anisotropic: 
+		case Xsc::Reflection::Filter::ComparisonAnisotropic: 
 			desc.minFilter = FO_ANISOTROPIC;
 			desc.magFilter = FO_ANISOTROPIC;
 			desc.minFilter = FO_ANISOTROPIC;
 			break;
-		case Xsc::Reflection::Filter::ComparisonMinMagMipPoint: 
-			desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinMagPointMipLinear: 
-			desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinPointMagLinearMipPoint: 
-			desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinPointMagMipLinear: 
-			desc.minFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinLinearMagMipPoint: 
-			desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinLinearMagPointMipLinear: 
-			desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinMagLinearMipPoint: 
-			desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_POINT | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonMinMagMipLinear: 
-			desc.minFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			desc.mipFilter = (FilterOptions)(FO_LINEAR | FO_USE_COMPARISON);
-			break;
-		case Xsc::Reflection::Filter::ComparisonAnisotropic: 
-			desc.minFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
-			desc.magFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
-			desc.minFilter = (FilterOptions)(FO_ANISOTROPIC | FO_USE_COMPARISON);
-			break;
 		default: 
 			break;
 		}
@@ -471,7 +435,7 @@ namespace bs
 			case Xsc::Reflection::UniformType::Sampler: 
 			{
 				auto findIter = reflData.samplerStates.find(entry.ident);
-				if (findIter == reflData.samplerStates.end())
+				if (findIter == reflData.samplerStates.end() || !findIter->second.isNonDefault)
 					desc.addParameter(ident, ident, GPOT_SAMPLER2D);
 				else
 				{

+ 1 - 1
Source/CMakeLists.txt

@@ -6,7 +6,7 @@ set (BS_VERSION_MAJOR 0)
 set (BS_VERSION_MINOR 4)
 
 set (BS_PREBUILT_DEPENDENCIES_VERSION 2)
-set (BS_SRC_DEPENDENCIES_VERSION 10)
+set (BS_SRC_DEPENDENCIES_VERSION 11)
 
 # Configuration types
 if(CMAKE_CONFIGURATION_TYPES) # Multiconfig generator?

+ 1 - 1
Source/External/XShaderCompiler

@@ -1 +1 @@
-Subproject commit 2f2916eb35607d8063cfbe47922114c05476a7b0
+Subproject commit 64b6bea9e85abf6d81710b8d8e835c0db2efe125