Browse Source

Feature: Added a clear() method to Texture

BearishSun 8 years ago
parent
commit
00e417e305

+ 32 - 0
Source/BansheeCore/Image/BsPixelData.cpp

@@ -235,6 +235,38 @@ namespace bs
 		setColorsInternal(colors, numElements);
 		setColorsInternal(colors, numElements);
 	}
 	}
 
 
+	void PixelData::setColors(const Color& color)
+	{
+		UINT32 depth = mExtents.getDepth();
+		UINT32 height = mExtents.getHeight();
+		UINT32 width = mExtents.getWidth();
+
+		UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
+		UINT32 packedColor[4];
+		assert(pixelSize <= sizeof(packedColor));
+
+		PixelUtil::packColor(color, mFormat, packedColor);
+
+		UINT8* data = getData();
+		for (UINT32 z = 0; z < depth; z++)
+		{
+			UINT32 zDataIdx = z * mSlicePitch * pixelSize;
+
+			for (UINT32 y = 0; y < height; y++)
+			{
+				UINT32 yDataIdx = y * mRowPitch * pixelSize;
+
+				for (UINT32 x = 0; x < width; x++)
+				{
+					UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
+
+					UINT8* dest = data + dataIdx;
+					memcpy(dest, packedColor, pixelSize);
+				}
+			}
+		}
+	}
+
 	float PixelData::getDepthAt(UINT32 x, UINT32 y, UINT32 z) const
 	float PixelData::getDepthAt(UINT32 x, UINT32 y, UINT32 z) const
 	{
 	{
 		UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
 		UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);

+ 3 - 0
Source/BansheeCore/Image/BsPixelData.h

@@ -373,6 +373,9 @@ namespace bs
 		 */
 		 */
 		void setColors(Color* colors, UINT32 numElements);
 		void setColors(Color* colors, UINT32 numElements);
 
 
+		/** Initializes all the pixels with a single color. */
+		void setColors(const Color& color);
+
 		/** 
 		/** 
 		 * Interprets pixel data as depth information as retrieved from the GPU's depth buffer. Converts the device specific
 		 * Interprets pixel data as depth information as retrieved from the GPU's depth buffer. Converts the device specific
 		 * depth value to range [0, 1] and returns it.
 		 * depth value to range [0, 1] and returns it.

+ 28 - 1
Source/BansheeCore/Image/BsTexture.cpp

@@ -434,7 +434,7 @@ namespace bs
 
 
 		if (desc.srcFace >= target->mProperties.getNumFaces())
 		if (desc.srcFace >= target->mProperties.getNumFaces())
 		{
 		{
-			LOGERR("Invalid destination face index.");
+			LOGERR("Invalid source face index.");
 			return;
 			return;
 		}
 		}
 
 
@@ -515,6 +515,33 @@ namespace bs
 		copyImpl(target, desc, commandBuffer);
 		copyImpl(target, desc, commandBuffer);
 	}
 	}
 
 
+	void Texture::clear(const Color& value, UINT32 mipLevel, UINT32 face, UINT32 queueIdx)
+	{
+		THROW_IF_NOT_CORE_THREAD;
+
+		if (face >= mProperties.getNumFaces())
+		{
+			LOGERR("Invalid face index.");
+			return;
+		}
+
+		if (mipLevel > mProperties.getNumMipmaps())
+		{
+			LOGERR("Mip level out of range. Valid range is [0, " + toString(mProperties.getNumMipmaps()) + "].");
+			return;
+		}
+
+		clearImpl(value, mipLevel, face, queueIdx);
+	}
+
+	void Texture::clearImpl(const Color& value, UINT32 mipLevel, UINT32 face, UINT32 queueIdx)
+	{
+		SPtr<PixelData> data = mProperties.allocBuffer(face, mipLevel);
+		data->setColors(value);
+		
+		writeData(*data, mipLevel, face, true, queueIdx);
+	}
+
 	/************************************************************************/
 	/************************************************************************/
 	/* 								TEXTURE VIEW                      		*/
 	/* 								TEXTURE VIEW                      		*/
 	/************************************************************************/
 	/************************************************************************/

+ 13 - 0
Source/BansheeCore/Image/BsTexture.h

@@ -400,6 +400,16 @@ namespace bs
 		void copy(const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc = TEXTURE_COPY_DESC::DEFAULT,
 		void copy(const SPtr<Texture>& target, const TEXTURE_COPY_DESC& desc = TEXTURE_COPY_DESC::DEFAULT,
 			const SPtr<CommandBuffer>& commandBuffer = nullptr);
 			const SPtr<CommandBuffer>& commandBuffer = nullptr);
 
 
+		/** 
+		 * Sets all the pixels of the specified face and mip level to the provided value. 
+		 * 
+		 * @param[in]	value			Color to clear the pixels to.
+		 * @param[in]	mipLevel		Mip level to clear.
+		 * @param[in]	face			Face (array index or cubemap face) to clear.
+		 * @param[in]	queueIdx		Device queue to perform the write operation on. See @ref queuesDoc.
+		 */
+		void clear(const Color& value, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 queueIdx = 0);
+
 		/**
 		/**
 		 * Reads data from the texture buffer into the provided buffer.
 		 * Reads data from the texture buffer into the provided buffer.
 		 * 		  
 		 * 		  
@@ -487,6 +497,9 @@ namespace bs
 		virtual void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, 
 		virtual void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, 
 			bool discardWholeBuffer = false, UINT32 queueIdx = 0) = 0;
 			bool discardWholeBuffer = false, UINT32 queueIdx = 0) = 0;
 
 
+		/** @copydoc clear */
+		virtual void clearImpl(const Color& value, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 queueIdx = 0);
+
 		/************************************************************************/
 		/************************************************************************/
 		/* 								TEXTURE VIEW                      		*/
 		/* 								TEXTURE VIEW                      		*/
 		/************************************************************************/
 		/************************************************************************/

+ 1 - 1
Source/RenderBeast/BsShadowRendering.cpp

@@ -1438,7 +1438,7 @@ namespace bs { namespace ct
 	}
 	}
 
 
 	void ShadowRendering::calcShadowMapProperties(const RendererLight& light, const RendererViewGroup& viewGroup, 
 	void ShadowRendering::calcShadowMapProperties(const RendererLight& light, const RendererViewGroup& viewGroup, 
-		UINT32 border, UINT32& size, SmallVector<float, 4>& fadePercents, float& maxFadePercent) const
+		UINT32 border, UINT32& size, SmallVector<float, 6>& fadePercents, float& maxFadePercent) const
 	{
 	{
 		const static float SHADOW_TEXELS_PER_PIXEL = 1.0f;
 		const static float SHADOW_TEXELS_PER_PIXEL = 1.0f;
 
 

+ 2 - 2
Source/RenderBeast/BsShadowRendering.h

@@ -292,7 +292,7 @@ namespace bs { namespace ct
 		Sphere subjectBounds;
 		Sphere subjectBounds;
 
 
 		/** Determines the fade amount of the shadow, for each view in the scene. */
 		/** Determines the fade amount of the shadow, for each view in the scene. */
-		SmallVector<float, 4> fadePerView;
+		SmallVector<float, 6> fadePerView;
 	};
 	};
 
 
 	/** 
 	/** 
@@ -467,7 +467,7 @@ namespace bs { namespace ct
 		 * @param[out]	maxFadePercent	Maximum value in the @p fadePercents array.
 		 * @param[out]	maxFadePercent	Maximum value in the @p fadePercents array.
 		 */
 		 */
 		void calcShadowMapProperties(const RendererLight& light, const RendererViewGroup& viewGroup, UINT32 border, 
 		void calcShadowMapProperties(const RendererLight& light, const RendererViewGroup& viewGroup, UINT32 border, 
-			UINT32& size, SmallVector<float, 4>& fadePercents, float& maxFadePercent) const;
+			UINT32& size, SmallVector<float, 6>& fadePercents, float& maxFadePercent) const;
 
 
 		/**
 		/**
 		 * Draws a mesh representing near and far planes at the provided coordinates. The mesh is constructed using
 		 * Draws a mesh representing near and far planes at the provided coordinates. The mesh is constructed using