Browse Source

Removed DX11 HardwarePixelBuffer & it now compiles

Marko Pintera 13 years ago
parent
commit
1fcc2b5929

+ 0 - 2
CamelotD3D11RenderSystem/CamelotD3D11RenderSystem.vcxproj

@@ -154,7 +154,6 @@
     <ClInclude Include="Include\CmD3D11GpuProgram.h" />
     <ClInclude Include="Include\CmD3D11GpuProgramManager.h" />
     <ClInclude Include="Include\CmD3D11HardwareBuffer.h" />
-    <ClInclude Include="Include\CmD3D11HardwarePixelBuffer.h" />
     <ClInclude Include="Include\CmD3D11HLSLProgram.h" />
     <ClInclude Include="Include\CmD3D11HardwareIndexBuffer.h" />
     <ClInclude Include="Include\CmD3D11Mappings.h" />
@@ -177,7 +176,6 @@
     <ClCompile Include="Source\CmD3D11HardwareBuffer.cpp" />
     <ClCompile Include="Source\CmD3D11HardwareBufferManager.cpp" />
     <ClCompile Include="Source\CmD3D11HardwareConstantBuffer.cpp" />
-    <ClCompile Include="Source\CmD3D11HardwarePixelBuffer.cpp" />
     <ClCompile Include="Source\CmD3D11HLSLProgram.cpp" />
     <ClCompile Include="Source\CmD3D11HardwareIndexBuffer.cpp" />
     <ClCompile Include="Source\CmD3D11Mappings.cpp" />

+ 0 - 6
CamelotD3D11RenderSystem/CamelotD3D11RenderSystem.vcxproj.filters

@@ -69,9 +69,6 @@
     <ClInclude Include="Include\CmD3D11RenderWindow.h">
       <Filter>Header Files</Filter>
     </ClInclude>
-    <ClInclude Include="Include\CmD3D11HardwarePixelBuffer.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="Include\CmD3D11Texture.h">
       <Filter>Header Files</Filter>
     </ClInclude>
@@ -131,9 +128,6 @@
     <ClCompile Include="Source\CmD3D11RenderWindow.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="Source\CmD3D11HardwarePixelBuffer.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="Source\CmD3D11Texture.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>

+ 0 - 86
CamelotD3D11RenderSystem/Include/CmD3D11HardwarePixelBuffer.h

@@ -1,86 +0,0 @@
-#pragma once
-
-#include "CmD3D11Prerequisites.h"
-#include "CmHardwarePixelBuffer.h"
-
-namespace CamelotEngine
-{
-	class CM_D3D11_EXPORT D3D11HardwarePixelBuffer : public HardwarePixelBuffer
-	{
-	public:
-		D3D11HardwarePixelBuffer(D3D11Texture* parentTexture, D3D11Device& device, UINT32 subresourceIndex,
-			UINT32 width, UINT32 height, UINT32 depth, UINT32 face, PixelFormat format, HardwareBuffer::Usage usage);
-
-		/// @copydoc HardwarePixelBuffer::blit
-		void blit(const HardwarePixelBufferPtr& src, const Box& srcBox, const Box& dstBox);
-
-		/// @copydoc HardwarePixelBuffer::blitFromMemory
-		void blitFromMemory(const PixelData& src, const Box& dstBox);
-
-		/// @copydoc HardwarePixelBuffer::blitToMemory
-		void blitToMemory(const Box& srcBox, const PixelData& dst);
-
-		/// Internal function to update mipmaps on update of level 0
-		void _genMipmaps();
-
-		~D3D11HardwarePixelBuffer();
-
-		/// Get rendertarget for z slice
-		RenderTexture *getRenderTarget(UINT32 zoffset);
-
-		/// Notify TextureBuffer of destruction of render target
-		virtual void _clearSliceRTT(UINT32 zoffset)
-		{
-			if (mSliceTRT.size() > zoffset)
-			{
-				mSliceTRT[zoffset] = 0;
-			}
-		}
-
-		D3D11Texture * getParentTexture() const;
-		UINT32 getSubresourceIndex() const;
-		UINT32 getFace() const;
-
-	protected:
-		/// Lock a box
-		PixelData lockImpl(const Box lockBox, LockOptions options);
-
-		/// Unlock a box
-		void unlockImpl(void);
-
-		/// D3DDevice pointer
-		D3D11Device& mDevice;
-
-		D3D11Texture* mParentTexture;
-		UINT32 mSubresourceIndex;
-
-		// if the usage is static - alloc at lock then use device UpdateSubresource when unlock and free memory
-		UINT8* mDataForStaticUsageLock; 
-
-		UINT32 mFace;
-
-		Box mLockBox;
-		PixelData mCurrentLock;
-		LockOptions mCurrentLockOptions;
-
-		D3D11_BOX convertBoxToDx11Box(const Box &inBox) const;
-
-		/// Util functions to convert a D3D locked box to a pixel box
-		void fromD3DLock(PixelData &rval, const DXGI_MAPPED_RECT &lrect);
-
-		/// Render targets
-		typedef vector<RenderTexture*>::type SliceTRT;
-		SliceTRT mSliceTRT;
-
-		void createStagingBuffer();
-		bool mUsingStagingBuffer;
-		ID3D11Resource *mStagingBuffer;
-
-		void* _map(ID3D11Resource *res, D3D11_MAP flags);
-		void* _mapstagingbuffer(D3D11_MAP flags);
-		void* _mapstaticbuffer(PixelData lock);
-		void _unmap(ID3D11Resource *res);
-		void _unmapstagingbuffer(bool copyback = true);
-		void _unmapstaticbuffer();
-	};
-}

+ 1 - 1
CamelotD3D11RenderSystem/Include/CmD3D11Mappings.h

@@ -45,7 +45,7 @@ namespace CamelotEngine
 		/// return the combination of D3DUSAGE values for Ogre buffer usage
 		static DWORD get(HardwareBuffer::Usage usage);
 		/// Get lock options
-		static D3D11_MAP get(HardwareBuffer::LockOptions options, HardwareBuffer::Usage usage);
+		static D3D11_MAP get(LockOptions options, HardwareBuffer::Usage usage);
 		static UINT getByteWidth(HardwareIndexBuffer::IndexType itype);
 		/// Get vertex data type
 		static DXGI_FORMAT get(VertexElementType vType);

+ 3 - 3
CamelotD3D11RenderSystem/Source/CmD3D11HardwareBuffer.cpp

@@ -66,7 +66,7 @@ namespace CamelotEngine
 			CM_EXCEPT(RenderingAPIException, "Provided length " + toString(length) + " larger than the buffer " + toString(mSizeInBytes) + ".");		
 
 		// Use direct (and faster) Map/Unmap if dynamic write, or a staging read/write
-		if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != HardwareBuffer::HBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
+		if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != HBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
 		{
 			D3D11_MAP mapType;
 
@@ -229,7 +229,7 @@ namespace CamelotEngine
 	{
 		// There is no functional interface in D3D, just do via manual 
 		// lock, copy & unlock
-		void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
+		void* pSrc = this->lock(offset, length, HBL_READ_ONLY);
 		memcpy(pDest, pSrc, length);
 		this->unlock();
 	}
@@ -240,7 +240,7 @@ namespace CamelotEngine
 		if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
 		{
 			void* pDst = this->lock(offset, length, 
-				discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
+				discardWholeBuffer ? HBL_WRITE_ONLY_DISCARD : HBL_READ_WRITE);
 			memcpy(pDst, pSource, length);
 			this->unlock();
 		}

+ 0 - 765
CamelotD3D11RenderSystem/Source/CmD3D11HardwarePixelBuffer.cpp

@@ -1,765 +0,0 @@
-#include "CmD3D11HardwarePixelBuffer.h"
-#include "CmD3D11Device.h"
-#include "CmD3D11Mappings.h"
-#include "CmRenderSystem.h"
-#include "CmTexture.h"
-#include "CmDataStream.h"
-
-namespace CamelotEngine
-{
-	D3D11HardwarePixelBuffer::D3D11HardwarePixelBuffer(D3D11Texture* parentTexture, D3D11Device& device, UINT32 subresourceIndex,
-		UINT32 width, UINT32 height, UINT32 depth, UINT32 face, PixelFormat format, HardwareBuffer::Usage usage):
-	HardwarePixelBuffer(width, height, depth, format, usage, false),
-		mParentTexture(parentTexture),
-		mDevice(device),
-		mSubresourceIndex(subresourceIndex),
-		mFace(face),
-		mDataForStaticUsageLock(0),
-		mStagingBuffer(NULL)
-	{
-		if(mUsage & TU_RENDERTARGET)
-		{
-			// Create render target for each slice
-			mSliceTRT.reserve(mDepth);
-			for(UINT32 zoffset = 0; zoffset < mDepth; ++zoffset)
-			{
-				String name;
-				name = "rtt/" + toString((UINT32)mParentTexture) + "/" + toString(mSubresourceIndex);
-
-				RenderTexture *trt = new D3D11RenderTexture(name, this, mDevice);
-				mSliceTRT.push_back(trt);
-				
-				RenderSystem::instance().attachRenderTarget(*trt);
-			}
-		}
-	}
-
-	D3D11HardwarePixelBuffer::~D3D11HardwarePixelBuffer()
-	{
-		if(!mSliceTRT.empty())
-		{	
-			// Delete all render targets that are not yet deleted via _clearSliceRTT
-			for(UINT32 zoffset = 0; zoffset < mDepth; ++zoffset)
-			{
-				if(mSliceTRT[zoffset])
-					RenderSystem::instance().destroyRenderTarget(mSliceTRT[zoffset]);
-			}
-		}
-
-		SAFE_DELETE_ARRAY(mDataForStaticUsageLock);
-		SAFE_RELEASE(mStagingBuffer);
-	}
-  
-	// Util functions to convert a D3D locked box to a pixel box
-	void D3D11HardwarePixelBuffer::fromD3DLock(PixelData& rval, const DXGI_MAPPED_RECT& lrect)
-	{
-		rval.rowPitch = lrect.Pitch / PixelUtil::getNumElemBytes(rval.format);
-		rval.slicePitch = rval.rowPitch * rval.getHeight();
-		assert((lrect.Pitch % PixelUtil::getNumElemBytes(rval.format))==0);
-		rval.data = lrect.pBits;
-	}
-
-	void* D3D11HardwarePixelBuffer::_map(ID3D11Resource *res, D3D11_MAP flags)
-	{
-		D3D11_MAPPED_SUBRESOURCE pMappedResource;
-		pMappedResource.pData = NULL;
-
-		switch(mParentTexture->getTextureType()) 
-		{
-		case TEX_TYPE_1D:
-			{  
-				mDevice.getImmediateContext()->Map(res, static_cast<UINT>(mSubresourceIndex), flags, 0, &pMappedResource);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 1D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D:
-			{
-				mDevice.getImmediateContext()->Map(res, D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mFace, mParentTexture->getNumMipmaps()+1), 
-					flags, 0, &pMappedResource);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 2D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_2D_ARRAY:
-			{
-				mDevice.getImmediateContext()->Map(res, D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mLockBox.front, mParentTexture->getNumMipmaps()+1), 
-					flags, 0, &pMappedResource);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 2D texture array\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				mDevice.getImmediateContext()->Map(res, static_cast<UINT>(mSubresourceIndex), flags, 0, &pMappedResource);
-
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 3D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		}
-
-		return pMappedResource.pData;
-	}
-
-	void *D3D11HardwarePixelBuffer::_mapstaticbuffer(PixelData lock)
-	{
-		// for static usage just alloc
-		size_t sizeOfImage = lock.getConsecutiveSize();
-		
-		mDataForStaticUsageLock = new UINT8[sizeOfImage];
-		return mDataForStaticUsageLock;
-	}
-  
-	void *D3D11HardwarePixelBuffer::_mapstagingbuffer(D3D11_MAP flags)
-	{
-		if(!mStagingBuffer)
-			createStagingBuffer();
-
-		if(flags == D3D11_MAP_READ_WRITE || flags == D3D11_MAP_READ || flags == D3D11_MAP_WRITE)  
-		{
-			if(mLockBox.getHeight() == mParentTexture->getHeight() && mLockBox.getWidth() == mParentTexture->getWidth())
-				mDevice.getImmediateContext()->CopyResource(mStagingBuffer, mParentTexture->getTextureResource());
-			else
-			{
-				D3D11_BOX dstBoxDx11 = convertBoxToDx11Box(mLockBox);
-				dstBoxDx11.front = 0;
-				dstBoxDx11.back = mLockBox.getDepth();
-
-				unsigned int subresource = D3D11CalcSubresource(mSubresourceIndex, mLockBox.front, mParentTexture->getNumMipmaps()+1);
-				mDevice.getImmediateContext()->CopySubresourceRegion(mStagingBuffer, subresource, mLockBox.left, mLockBox.top, 
-					mSubresourceIndex, mParentTexture->getTextureResource(), subresource, &dstBoxDx11);
-			}
-		}
-		else if(flags == D3D11_MAP_WRITE_DISCARD)
-			flags = D3D11_MAP_WRITE; // stagingbuffer doesn't support discarding
-
-		return _map(mStagingBuffer, flags);
-	}
- 
-	PixelData D3D11HardwarePixelBuffer::lockImpl(const Box lockBox, LockOptions options)
-	{
-		// Check for misuse
-		if(mUsage & TU_RENDERTARGET)
-			CM_EXCEPT(RenderingAPIException, "DirectX does not allow locking of or directly writing to RenderTargets. Use blitFromMemory if you need the contents.");	
-
-		mLockBox = lockBox;
-
-		// Set extents and format
-		// Note that we do not carry over the left/top/front here, since the returned
-		// PixelBox will be re-based from the locking point onwards
-		PixelData rval(lockBox.getWidth(), lockBox.getHeight(), lockBox.getDepth(), mFormat);
-		// Set locking flags according to options
-		D3D11_MAP  flags = D3D11_MAP_WRITE_DISCARD ;
-		switch(options)
-		{
-		case HBL_WRITE_ONLY_NO_OVERWRITE:
-			flags = D3D11_MAP_WRITE_NO_OVERWRITE;
-			break;
-		case HBL_READ_WRITE:
-			flags = D3D11_MAP_READ_WRITE;
-			break;
-		case HBL_WRITE_ONLY_DISCARD:
-			flags = D3D11_MAP_WRITE_DISCARD;
-			break;
-		case HBL_READ_ONLY:
-			flags = D3D11_MAP_READ;
-			break;
-		case HBL_WRITE_ONLY:
-			flags = D3D11_MAP_WRITE;
-			break;
-		default: 
-			break;
-		};
-
-		size_t offset = 0;
-
-		if(mUsage == HBU_STATIC || mUsage & HBU_DYNAMIC)
-		{
-			if(mUsage == HBU_STATIC || options == HBL_READ_ONLY || options == HBL_READ_WRITE || options == HBL_WRITE_ONLY)
-				rval.data = _mapstagingbuffer(flags);
-			else
-				rval.data = _map(mParentTexture->getTextureResource(), flags);
-
-			// calculate the offset in bytes
-			offset = D3D11Mappings::_getSizeInBytes(rval.format, rval.left, rval.front);
-			// add the offset, so the right memory will be changed
-			//rval.data = static_cast<int*>(rval.data) + offset;
-		}
-		else
-		{
-			size_t sizeOfImage = rval.getConsecutiveSize();
-            mDataForStaticUsageLock = new UINT8[sizeOfImage];
-            rval.data = mDataForStaticUsageLock;
-		}
-		// save without offset
- 		mCurrentLock = rval;
-		mCurrentLockOptions = options;
-
-		// add the offset, so the right memory will be changed
-		rval.data = static_cast<int*>(rval.data) + offset;
-
-		return rval;
-	}
-
-	void D3D11HardwarePixelBuffer::_unmap(ID3D11Resource *res)
-	{
-		switch(mParentTexture->getTextureType()) {
-		case TEX_TYPE_1D:
-			{
-				mDevice.getImmediateContext()->Unmap(res, mSubresourceIndex);
-			}
-			break;
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D:
-			{							  
-				mDevice.getImmediateContext()->Unmap(res, D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mFace, mParentTexture->getNumMipmaps()+1));
-			}
-			break;
-		case TEX_TYPE_2D_ARRAY:
-			{
-				mDevice.getImmediateContext()->Unmap(res, D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mLockBox.front, mParentTexture->getNumMipmaps()+1));
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				mDevice.getImmediateContext()->Unmap(res, mSubresourceIndex);
-			}
-			break;
-		}
-
-		if (mDevice.hasError())
-		{
-			String errorDescription = mDevice.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
-		}
-	}
-
-	void D3D11HardwarePixelBuffer::_unmapstaticbuffer()
-	{
-		D3D11_BOX dstBoxDx11 = convertBoxToDx11Box(mLockBox);
-		dstBoxDx11.front = 0;
-		dstBoxDx11.back = mLockBox.getDepth();
-
-		size_t rowWidth = D3D11Mappings::_getSizeInBytes(mCurrentLock.format, mCurrentLock.getWidth());
-
-		switch(mParentTexture->getTextureType()) {
-		case TEX_TYPE_1D:
-			{
-
-				mDevice.getImmediateContext()->UpdateSubresource(mParentTexture->GetTex1D(), 
-					static_cast<UINT>(mSubresourceIndex), &dstBoxDx11, 
-					mDataForStaticUsageLock, rowWidth, 0);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 1D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D:
-			{
-				mDevice.getImmediateContext()->UpdateSubresource(mParentTexture->GetTex2D(), 
-					D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mFace, mParentTexture->getNumMipmaps()+1),
-					&dstBoxDx11, 
-					mDataForStaticUsageLock, rowWidth, 0);
-
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 2D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_2D_ARRAY:
-			{
-				mDevice.getImmediateContext()->UpdateSubresource(mParentTexture->GetTex2D(), 
-					D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mLockBox.front, mParentTexture->getNumMipmaps()+1),
-					&dstBoxDx11, mDataForStaticUsageLock, rowWidth, 0);
-
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 2D texture array\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				size_t sliceWidth = D3D11Mappings::_getSizeInBytes(mCurrentLock.format, mCurrentLock.getWidth(), mCurrentLock.getHeight());
-
-				mDevice.getImmediateContext()->UpdateSubresource(mParentTexture->GetTex3D(), static_cast<UINT>(mSubresourceIndex), 
-					&dstBoxDx11, mDataForStaticUsageLock, rowWidth, sliceWidth);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map 3D texture\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		}
-
-		SAFE_DELETE_ARRAY(mDataForStaticUsageLock) ;
-	}
-  
-	void D3D11HardwarePixelBuffer::_unmapstagingbuffer(bool copyback)
-	{
-		_unmap(mStagingBuffer);
-
-		if(copyback)
-		{
-			if(mLockBox.getHeight() == mParentTexture->getHeight() && mLockBox.getWidth() == mParentTexture->getWidth())
-				mDevice.getImmediateContext()->CopyResource(mParentTexture->getTextureResource(), mStagingBuffer);
-			else
-			{
-				D3D11_BOX dstBoxDx11 = convertBoxToDx11Box(mLockBox);
-				dstBoxDx11.front = 0;
-				dstBoxDx11.back = mLockBox.getDepth();
-
-				unsigned int subresource = D3D11CalcSubresource(mSubresourceIndex, mLockBox.front, mParentTexture->getNumMipmaps()+1);
-				mDevice.getImmediateContext()->CopySubresourceRegion(mParentTexture->getTextureResource(), subresource, mLockBox.left, mLockBox.top, mSubresourceIndex, mStagingBuffer, subresource, &dstBoxDx11);
-			}
-		}
-	}
- 
-	void D3D11HardwarePixelBuffer::unlockImpl(void)
-	{
-		if(mUsage == HBU_STATIC)
-			_unmapstagingbuffer();
-		else if(mUsage & HBU_DYNAMIC)
- 		{
-			if(mCurrentLockOptions == HBL_READ_ONLY || mCurrentLockOptions == HBL_READ_WRITE || mCurrentLockOptions == HBL_WRITE_ONLY)
-			{
-				size_t sizeinbytes = D3D11Mappings::_getSizeInBytes(mParentTexture->getFormat(), mParentTexture->getWidth(), mParentTexture->getHeight());
-
-				void *data = _map(mParentTexture->getTextureResource(), D3D11_MAP_WRITE_DISCARD);
-
-				memcpy(data, mCurrentLock.data, sizeinbytes);
-
-				// unmap the texture and the staging buffer
-				_unmap(mParentTexture->getTextureResource());
-
-				_unmapstagingbuffer(false);
- 			}
-			else
-				_unmap(mParentTexture->getTextureResource());
- 		}
- 		else
-			_unmapstaticbuffer();
-	}
-
-	D3D11_BOX D3D11HardwarePixelBuffer::convertBoxToDx11Box(const Box &inBox) const
-	{
-		D3D11_BOX res;
-		res.left	= static_cast<UINT>(inBox.left);
-		res.top		= static_cast<UINT>(inBox.top);
-		res.front	= static_cast<UINT>(inBox.front);
-		res.right	= static_cast<UINT>(inBox.right);
-		res.bottom	= static_cast<UINT>(inBox.bottom);
-		res.back	= static_cast<UINT>(inBox.back);
-
-		return res;
-	}
-
-	void D3D11HardwarePixelBuffer::blit(const HardwarePixelBufferPtr &rsrc, const Box &srcBox, const Box &dstBox)
-	{
-		if (
-			(srcBox.getWidth() != dstBox.getWidth())
-			|| (srcBox.getHeight() != dstBox.getHeight())
-			|| (srcBox.getDepth() != dstBox.getDepth())
-			)
-		{
-			CM_EXCEPT(RenderingAPIException, 
-				"D3D11 device cannot copy a subresource - source and dest size are not the same and they have to be the same in DX11.");
-		}
-
-		D3D11_BOX srcBoxDx11 = convertBoxToDx11Box(srcBox);
-		D3D11HardwarePixelBuffer* rsrcDx11 = static_cast<D3D11HardwarePixelBuffer*>(rsrc.get());
-
-		switch(mParentTexture->getTextureType()) {
-		case TEX_TYPE_1D:
-			{
-
-				mDevice.getImmediateContext()->CopySubresourceRegion(
-					mParentTexture->GetTex1D(), 
-					static_cast<UINT>(mSubresourceIndex),
-					static_cast<UINT>(dstBox.left),
-					0,
-					0,
-					rsrcDx11->mParentTexture->GetTex1D(),
-					static_cast<UINT>(rsrcDx11->mSubresourceIndex),
-					&srcBoxDx11);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy 1d subresource Region\nError Description:" + errorDescription);
-				}			
-			}
-			break;
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D:
-			{
-				mDevice.getImmediateContext()->CopySubresourceRegion(
-					mParentTexture->GetTex2D(), 
-					D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mFace, mParentTexture->getNumMipmaps()+1),
-					static_cast<UINT>(dstBox.left),
-					static_cast<UINT>(dstBox.top),
-					mFace,
-					rsrcDx11->mParentTexture->GetTex2D(),
-					static_cast<UINT>(rsrcDx11->mSubresourceIndex),
-					&srcBoxDx11);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy 2d subresource Region\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_2D_ARRAY:
-			{
-				mDevice.getImmediateContext()->CopySubresourceRegion(
-					mParentTexture->GetTex2D(), 
-					D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), srcBox.front, mParentTexture->getNumMipmaps()+1),
-					static_cast<UINT>(dstBox.left),
-					static_cast<UINT>(dstBox.top),
-					srcBox.front,
-					rsrcDx11->mParentTexture->GetTex2D(),
-					static_cast<UINT>(rsrcDx11->mSubresourceIndex),
-					&srcBoxDx11);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy 2d subresource Region\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				mDevice.getImmediateContext()->CopySubresourceRegion(
-					mParentTexture->GetTex3D(), 
-					static_cast<UINT>(mSubresourceIndex),
-					static_cast<UINT>(dstBox.left),
-					static_cast<UINT>(dstBox.top),
-					static_cast<UINT>(dstBox.front),
-					rsrcDx11->mParentTexture->GetTex3D(),
-					static_cast<UINT>(rsrcDx11->mSubresourceIndex),
-					&srcBoxDx11);
-				if (mDevice.hasError())
-				{
-					String errorDescription = mDevice.getErrorDescription();
-					CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy 3d subresource Region\nError Description:" + errorDescription);
-				}
-			}
-			break;
-		}
-
-		_genMipmaps();
-	}
- 
-	void D3D11HardwarePixelBuffer::blitFromMemory(const PixelData& src, const Box& dstBox)
-	{
-		bool isCompressed = false;
-		switch(mFormat)
-		{
-		case PF_DXT1:
-		case PF_DXT2:
-		case PF_DXT3:
-		case PF_DXT4:
-		case PF_DXT5:
-			isCompressed = true;
-			break;
-		default:
-
-			break;
-		}
-
-		if (isCompressed && (dstBox.getWidth() % 4 != 0 || dstBox.getHeight() % 4 != 0 ))
-		{
-			return;
-		}
-
-
-		// for scoped deletion of conversion buffer
-		PixelData converted = src;
-
-		D3D11_BOX dstBoxDx11 = convertBoxToDx11Box(dstBox);
-		dstBoxDx11.front = 0;
-		dstBoxDx11.back = converted.getDepth();
-
-		// convert to pixelbuffer's native format if necessary
-		if (src.format != mFormat)
-		{
-			MemoryDataStreamPtr buf(new MemoryDataStream(PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat)));
-			converted = PixelData(src.getWidth(), src.getHeight(), src.getDepth(), mFormat, buf->getPtr());
-			PixelUtil::bulkPixelConversion(src, converted);
-		}
-
-		if (mUsage & HBU_DYNAMIC)
-		{
-			size_t sizeinbytes;
-			if (PixelUtil::isCompressed(converted.format))
- 			{
-				// D3D wants the width of one row of cells in bytes
-				if (converted.format == PF_DXT1)
-				{
-					// 64 bits (8 bytes) per 4x4 block
-					sizeinbytes = std::max<size_t>(1, converted.getWidth() / 4) * std::max<size_t>(1, converted.getHeight() / 4) * 8;
-				}
-				else
-				{
-					// 128 bits (16 bytes) per 4x4 block
-					sizeinbytes = std::max<size_t>(1, converted.getWidth() / 4) * std::max<size_t>(1, converted.getHeight() / 4) * 16;
-				}
- 			}
- 			else
- 			{
-				sizeinbytes = converted.getHeight() * converted.getWidth() * PixelUtil::getNumElemBytes(converted.format);
- 			}
- 
-			const PixelData& locked = lock(dstBox, HBL_WRITE_ONLY_DISCARD);
-
-			memcpy(locked.data, converted.data, sizeinbytes);
-
-			unlock();
- 		}
- 		else
-		{
-			size_t rowWidth;
-			if (PixelUtil::isCompressed(converted.format))
- 			{
-				// D3D wants the width of one row of cells in bytes
-				if (converted.format == PF_DXT1)
- 				{
-					// 64 bits (8 bytes) per 4x4 block
-					rowWidth = (converted.rowPitch / 4) * 8;
-				}
-				else
-				{
-					// 128 bits (16 bytes) per 4x4 block
-					rowWidth = (converted.rowPitch / 4) * 16;
- 				}
- 			}
-			else
- 			{
-				rowWidth = converted.rowPitch * PixelUtil::getNumElemBytes(converted.format);
-			}
-
-			switch(mParentTexture->getTextureType()) {
-			case TEX_TYPE_1D:
- 				{
-
-					mDevice.getImmediateContext()->UpdateSubresource( 
-						mParentTexture->GetTex1D(), 
-						0,
-						&dstBoxDx11,
-						converted.data,
-						rowWidth,
-						0 );
-					if (mDevice.hasError())
-					{
-						String errorDescription = mDevice.getErrorDescription();
-						CM_EXCEPT(RenderingAPIException, "D3D11 device cannot update 1d subresource\nError Description:" + errorDescription);
-					}
- 				}
-				break;
-			case TEX_TYPE_CUBE_MAP:
-			case TEX_TYPE_2D:
- 				{
-					mDevice.getImmediateContext()->UpdateSubresource( 
-						mParentTexture->GetTex2D(), 
-						D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), mFace, mParentTexture->getNumMipmaps()+1),
-						&dstBoxDx11,
-						converted.data,
-						rowWidth,
-						0 );
-					if (mDevice.hasError())
-					{
-						String errorDescription = mDevice.getErrorDescription();
-						CM_EXCEPT(RenderingAPIException, "D3D11 device cannot update 2d subresource\nError Description:" + errorDescription);
-					}
- 				}
-				break;
-			case TEX_TYPE_2D_ARRAY:
- 				{
-					mDevice.getImmediateContext()->UpdateSubresource( 
-						mParentTexture->GetTex2D(), 
-						D3D11CalcSubresource(static_cast<UINT>(mSubresourceIndex), src.front, mParentTexture->getNumMipmaps()+1),
-						&dstBoxDx11,
-						converted.data,
-						rowWidth,
-						0 );
-					if (mDevice.hasError())
- 					{
-						String errorDescription = mDevice.getErrorDescription();
-						CM_EXCEPT(RenderingAPIException, "D3D11 device cannot update 2d array subresource\nError Description:" + errorDescription);
-				}
-				}
-				break;
-			case TEX_TYPE_3D:
-				{
-					// copied from dx9
-					size_t sliceWidth;
-					if (PixelUtil::isCompressed(converted.format))
-					{
-						// D3D wants the width of one slice of cells in bytes
-						if (converted.format == PF_DXT1)
-						{
-							// 64 bits (8 bytes) per 4x4 block
-							sliceWidth = (converted.slicePitch / 16) * 8;
-						}
-						else
-						{
-							// 128 bits (16 bytes) per 4x4 block
-							sliceWidth = (converted.slicePitch / 16) * 16;
-						}
-
- 					}
- 					else
- 					{
-						sliceWidth = converted.slicePitch * PixelUtil::getNumElemBytes(converted.format);
- 					}
- 
-					mDevice.getImmediateContext()->UpdateSubresource( 
-						mParentTexture->GetTex3D(), 
-						static_cast<UINT>(mSubresourceIndex),
-						&dstBoxDx11,
-						converted.data,
-						rowWidth,
-						sliceWidth
-						);
-					if (mDevice.hasError())
-					{
-						String errorDescription = mDevice.getErrorDescription();
-						CM_EXCEPT(RenderingAPIException, "D3D11 device cannot update 3d subresource\nError Description:" + errorDescription);
-					}
- 				}
-				break;
-			}
- 
-			if (!isCompressed)
-			{
-				_genMipmaps();
- 			}
-		}	
- 	}
-
-	void D3D11HardwarePixelBuffer::blitToMemory(const Box &srcBox, const PixelData &dst)
-	{
-		if(mParentTexture->getTextureType() != TEX_TYPE_2D)
-		{
-			CM_EXCEPT(NotImplementedException, "Only implemeted for 2D buffers.");
-		}
-
-		PixelData lockedData = lock(srcBox, HardwareBuffer::HBL_READ_ONLY);
-
-		if(dst.getConsecutiveSize() != lockedData.getConsecutiveSize())
-		{
-			unlock();
-
-			CM_EXCEPT(RenderingAPIException, "Size of the provided buffer (" + toString(dst.getConsecutiveSize()) + "), doesn't "
-				+ "match the size of the texture buffer (" + toString(lockedData.getConsecutiveSize()) + ").");
-		}
-
-		memcpy(dst.data, lockedData.data, lockedData.getConsecutiveSize());
-
-		unlock();
-	}
-  
-	void D3D11HardwarePixelBuffer::_genMipmaps()
-	{
-        if(mParentTexture->HasAutoMipMapGenerationEnabled())
- 		{
-            ID3D11ShaderResourceView *pShaderResourceView = mParentTexture->getTexture();
-			mDevice.getImmediateContext()->GenerateMips(pShaderResourceView);
-			if (mDevice.hasError())
-			{
-				String errorDescription = mDevice.getErrorDescription();
-				CM_EXCEPT(RenderingAPIException, "D3D11 device cannot generate mips\nError Description:" + errorDescription);
-			}	
-		}
-	}
-    
-	RenderTexture *D3D11HardwarePixelBuffer::getRenderTarget(UINT32 zoffset)
-	{
-		assert(mUsage & TU_RENDERTARGET);
-		assert(zoffset < mDepth);
-		return mSliceTRT[zoffset];
-	}
-
-	D3D11Texture * D3D11HardwarePixelBuffer::getParentTexture() const
-	{
-		return mParentTexture;
-	}
-    
-	UINT32 D3D11HardwarePixelBuffer::getSubresourceIndex() const
-	{
-		return mSubresourceIndex;
-	}
-   
-	UINT32 D3D11HardwarePixelBuffer::getFace() const
-	{
-		return mFace;
-	}
-   
-	void D3D11HardwarePixelBuffer::createStagingBuffer()
-	{
-		switch (mParentTexture->getTextureType())
-		{
-		case TEX_TYPE_1D:
-			{
-				D3D11_TEXTURE1D_DESC desc;
-				mParentTexture->GetTex1D()->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				mDevice.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
-			} 					
-			break;
-		case TEX_TYPE_2D:
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D_ARRAY:
-			{
-				D3D11_TEXTURE2D_DESC desc;
-				mParentTexture->GetTex2D()->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				mDevice.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				D3D11_TEXTURE3D_DESC desc;
-				mParentTexture->GetTex3D()->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				mDevice.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
-			}
-			break;
-		}
-	}
-}

File diff suppressed because it is too large
+ 739 - 739
CamelotD3D11RenderSystem/Source/CmD3D11Mappings.cpp


+ 495 - 494
CamelotD3D11RenderSystem/Source/CmD3D11Texture.cpp

@@ -13,22 +13,29 @@
 
 namespace CamelotEngine
 {
-	D3D11Texture::D3D11Texture()
-		: m1DTex(nullptr)
-		, m2DTex(nullptr)
-		, m3DTex(nullptr)
-		, mTex(nullptr)
-		, mShaderResourceView(nullptr)
-		, mStagingBuffer(nullptr)
-		, mLockedSubresourceIdx(-1)
-		, mLockedForReading(false)
-		, mStaticBuffer(nullptr)
-	{
-		ZeroMemory(&mSRVDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
-	}
-
-	void D3D11Texture::setRawPixels_internal(const PixelData& data, UINT32 face, UINT32 mip)
-	{
+	D3D11Texture::D3D11Texture()
+		: m1DTex(nullptr)
+		, m2DTex(nullptr)
+		, m3DTex(nullptr)
+		, mTex(nullptr)
+		, mShaderResourceView(nullptr)
+		, mStagingBuffer(nullptr)
+		, mLockedSubresourceIdx(-1)
+		, mLockedForReading(false)
+		, mStaticBuffer(nullptr)
+	{
+		ZeroMemory(&mSRVDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
+	}
+
+	D3D11Texture::~D3D11Texture()
+	{
+		THROW_IF_NOT_RENDER_THREAD;
+
+		freeInternalResources();			
+	}
+
+	void D3D11Texture::setRawPixels_internal(const PixelData& data, UINT32 face, UINT32 mip)
+	{
 		THROW_IF_NOT_RENDER_THREAD
 
 		if(mip < 0 || mip > mNumMipmaps)
@@ -46,15 +53,15 @@ namespace CamelotEngine
 				" Width: " + toString(mWidth) + "/" + toString(data.getWidth()) + 
 				" Height: " + toString(mHeight) + "/" + toString(data.getHeight()) + 
 				" Depth: " + toString(mDepth) + "/" + toString(data.getDepth()));
-		}
-
-		PixelData myData = lock(HBL_WRITE_ONLY_DISCARD, mip, face);
-		memcpy(myData.data, data.data, data.getConsecutiveSize());
-		unlock();
-	}
-
-	void D3D11Texture::getRawPixels_internal(UINT32 face, UINT32 mip, AsyncOp& op)
-	{
+		}
+
+		PixelData myData = lock(HBL_WRITE_ONLY_DISCARD, mip, face);
+		memcpy(myData.data, data.data, data.getConsecutiveSize());
+		unlock();
+	}
+
+	void D3D11Texture::getRawPixels_internal(UINT32 face, UINT32 mip, AsyncOp& op)
+	{
 		THROW_IF_NOT_RENDER_THREAD;
 
 		if(mip < 0 || mip > mNumMipmaps)
@@ -82,502 +89,496 @@ namespace CamelotEngine
 		UINT8* buffer = new UINT8[totalSize]; 
 		PixelDataPtr dst(new PixelData(width, height, depth, getFormat(), buffer, true));
 
-		PixelData myData = lock(HBL_READ_ONLY, mip, face);
-
-#if CM_DEBUG_MODE
-		if(dst->getConsecutiveSize() != myData.getConsecutiveSize())
-		{
-			unlock();
-			CM_EXCEPT(InternalErrorException, "Buffer sizes don't match");
-		}
-#endif
-
-		memcpy(dst->data, myData.data, dst->getConsecutiveSize());
+		PixelData myData = lock(HBL_READ_ONLY, mip, face);
+
+#if CM_DEBUG_MODE
+		if(dst->getConsecutiveSize() != myData.getConsecutiveSize())
+		{
+			unlock();
+			CM_EXCEPT(InternalErrorException, "Buffer sizes don't match");
+		}
+#endif
+
+		memcpy(dst->data, myData.data, dst->getConsecutiveSize());
 		unlock();
 
-		op.completeOperation(dst);
-	}
-
-	void D3D11Texture::copy_internal(TexturePtr& target)
-	{
-		if (target->getUsage() != this->getUsage() ||
-			target->getTextureType() != this->getTextureType())
-		{
-			CM_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
-		}
-
+		op.completeOperation(dst);
+	}
+
+	void D3D11Texture::copy_internal(TexturePtr& target)
+	{
+		if (target->getUsage() != this->getUsage() ||
+			target->getTextureType() != this->getTextureType())
+		{
+			CM_EXCEPT(InvalidParametersException, "Source and destination textures must be of same type and must have the same usage and type.");
+		}
+
 		if(getWidth() != target->getWidth() || getHeight() != target->getHeight() || getDepth() != target->getDepth())
 		{
 			CM_EXCEPT(InvalidParametersException, "Texture sizes don't match." \
 				" Width: " + toString(getWidth()) + "/" + toString(target->getWidth()) + 
 				" Height: " + toString(getHeight()) + "/" + toString(target->getHeight()) + 
 				" Depth: " + toString(getDepth()) + "/" + toString(target->getDepth()));
-		}
-
+		}
+
 		if(getNumFaces() != target->getNumFaces())
 		{
 			CM_EXCEPT(InvalidParametersException, "Number of texture faces doesn't match." \
 				" Num faces: " + toString(getNumFaces()) + "/" + toString(target->getNumFaces()));
-		}
-
+		}
+
 		if(getNumMipmaps() != target->getNumMipmaps())
 		{
 			CM_EXCEPT(InvalidParametersException, "Number of mipmaps doesn't match." \
 				" Num mipmaps: " + toString(getNumMipmaps()) + "/" + toString(target->getNumMipmaps()));
-		}
-
-		// get the target
-		D3D11Texture* other = static_cast<D3D11Texture*>(target.get());
-
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		device.getImmediateContext()->CopyResource(other->getDX11Resource(), mTex);
-
-		if (device.hasError())
-		{
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy resource\nError Description:" + errorDescription);
-		}
-	}
-
-	PixelData D3D11Texture::lock(LockOptions options, UINT32 mipLevel, UINT32 face)
-	{
-		UINT32 mipWidth = mipLevel >> mWidth;
-		UINT32 mipHeight = mipLevel >> mHeight;
-		UINT32 mipDepth = mipLevel >> mDepth;
-
-		PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
-
-		D3D11_MAP flags = D3D11Mappings::_getLockOptions(options);
-		switch(options)
-		{
-		case HBL_WRITE_ONLY_NO_OVERWRITE:
-			flags = D3D11_MAP_WRITE_NO_OVERWRITE;
-			break;
-		case HBL_READ_WRITE:
-			flags = D3D11_MAP_READ_WRITE;
-			break;
-		case HBL_WRITE_ONLY_DISCARD:
-			flags = D3D11_MAP_WRITE_DISCARD;
-			break;
-		case HBL_READ_ONLY:
-			flags = D3D11_MAP_READ;
-			break;
-		case HBL_WRITE_ONLY:
-			flags = D3D11_MAP_WRITE;
-			break;
-		default: 
-			break;
-		};
-
-		if(flags == D3D11_MAP_READ || flags == D3D11_MAP_READ_WRITE)
-		{
-			lockedArea.data = _mapstagingbuffer(flags, face, mipLevel);
-			mLockedForReading = true;
-		}
-		else
-		{
-			if(mUsage == TU_DYNAMIC)
-				lockedArea.data = _map(mTex, flags, face, mipLevel);
-			else
-				lockedArea.data = _mapstaticbuffer(lockedArea, mipLevel, face);
-
-			mLockedForReading = false;
-		}
-
-		return lockedArea;
-	}
-
-	void D3D11Texture::unlock()
-	{
-		if(mLockedForReading)
-			_unmapstagingbuffer();
-		else
-			_unmap(mTex);
-	}
-
-	void D3D11Texture::initialize_internal()
-	{
+		}
+
+		// get the target
+		D3D11Texture* other = static_cast<D3D11Texture*>(target.get());
+
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		device.getImmediateContext()->CopyResource(other->getDX11Resource(), mTex);
+
+		if (device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot copy resource\nError Description:" + errorDescription);
+		}
+	}
+
+	PixelData D3D11Texture::lock(LockOptions options, UINT32 mipLevel, UINT32 face)
+	{
+		UINT32 mipWidth = mipLevel >> mWidth;
+		UINT32 mipHeight = mipLevel >> mHeight;
+		UINT32 mipDepth = mipLevel >> mDepth;
+
+		PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
+
+		D3D11_MAP flags = D3D11Mappings::_getLockOptions(options);
+		switch(options)
+		{
+		case HBL_WRITE_ONLY_NO_OVERWRITE:
+			flags = D3D11_MAP_WRITE_NO_OVERWRITE;
+			break;
+		case HBL_READ_WRITE:
+			flags = D3D11_MAP_READ_WRITE;
+			break;
+		case HBL_WRITE_ONLY_DISCARD:
+			flags = D3D11_MAP_WRITE_DISCARD;
+			break;
+		case HBL_READ_ONLY:
+			flags = D3D11_MAP_READ;
+			break;
+		case HBL_WRITE_ONLY:
+			flags = D3D11_MAP_WRITE;
+			break;
+		default: 
+			break;
+		};
+
+		if(flags == D3D11_MAP_READ || flags == D3D11_MAP_READ_WRITE)
+		{
+			lockedArea.data = _mapstagingbuffer(flags, face, mipLevel);
+			mLockedForReading = true;
+		}
+		else
+		{
+			if(mUsage == TU_DYNAMIC)
+				lockedArea.data = _map(mTex, flags, face, mipLevel);
+			else
+				lockedArea.data = _mapstaticbuffer(lockedArea, mipLevel, face);
+
+			mLockedForReading = false;
+		}
+
+		return lockedArea;
+	}
+
+	void D3D11Texture::unlock()
+	{
+		if(mLockedForReading)
+			_unmapstagingbuffer();
+		else
+			_unmap(mTex);
+	}
+
+	void D3D11Texture::initialize_internal()
+	{
 		THROW_IF_NOT_RENDER_THREAD
 
 		createInternalResources();
 
-		Resource::initialize_internal();
-	}
-
-	void D3D11Texture::createInternalResourcesImpl()
-	{
-		// load based on tex.type
-		switch (getTextureType())
-		{
-		case TEX_TYPE_1D:
-			_create1DTex();
-			break;
-		case TEX_TYPE_2D:
-		case TEX_TYPE_CUBE_MAP:
-			_create2DTex();
-			break;
-		case TEX_TYPE_3D:
-			_create3DTex();
-			break;
-		default:
-			freeInternalResources();
-			CM_EXCEPT(RenderingAPIException, "Unknown texture type");
-		}
-	}
-
-	void D3D11Texture::freeInternalResourcesImpl()
-	{
-		SAFE_RELEASE(mTex);
-		SAFE_RELEASE(mShaderResourceView);
-		SAFE_RELEASE(m1DTex);
-		SAFE_RELEASE(m2DTex);
-		SAFE_RELEASE(m3DTex);
-		SAFE_RELEASE(mStagingBuffer);
-	}
-
-	void D3D11Texture::_create1DTex()
-	{
-		// We must have those defined here
-		assert(mWidth > 0 || mHeight > 0);
-
-		// Determine which D3D11 pixel format we'll use
-		HRESULT hr;
-		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
-
-		// Determine total number of mipmaps including main one (d3d11 convention)
-		UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
-
-		D3D11_TEXTURE1D_DESC desc;
-		desc.Width			= static_cast<UINT32>(mWidth);
-		desc.MipLevels		= numMips;
-		desc.ArraySize		= 1;
-		desc.Format			= d3dPF;
-		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
-		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
-		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
-		desc.MiscFlags		= 0;
-
+		Resource::initialize_internal();
+	}
+
+	void D3D11Texture::createInternalResourcesImpl()
+	{
+		// load based on tex.type
+		switch (getTextureType())
+		{
+		case TEX_TYPE_1D:
+			_create1DTex();
+			break;
+		case TEX_TYPE_2D:
+		case TEX_TYPE_CUBE_MAP:
+			_create2DTex();
+			break;
+		case TEX_TYPE_3D:
+			_create3DTex();
+			break;
+		default:
+			freeInternalResources();
+			CM_EXCEPT(RenderingAPIException, "Unknown texture type");
+		}
+	}
+
+	void D3D11Texture::freeInternalResourcesImpl()
+	{
+		SAFE_RELEASE(mTex);
+		SAFE_RELEASE(mShaderResourceView);
+		SAFE_RELEASE(m1DTex);
+		SAFE_RELEASE(m2DTex);
+		SAFE_RELEASE(m3DTex);
+		SAFE_RELEASE(mStagingBuffer);
+	}
+
+	void D3D11Texture::_create1DTex()
+	{
+		// We must have those defined here
+		assert(mWidth > 0 || mHeight > 0);
+
+		// Determine which D3D11 pixel format we'll use
+		HRESULT hr;
+		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
+
+		// Determine total number of mipmaps including main one (d3d11 convention)
+		UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
+
+		D3D11_TEXTURE1D_DESC desc;
+		desc.Width			= static_cast<UINT32>(mWidth);
+		desc.MipLevels		= numMips;
+		desc.ArraySize		= 1;
+		desc.Format			= d3dPF;
+		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
+		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
+		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
+		desc.MiscFlags		= 0;
+
 		// Create the texture
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		hr = device.getD3D11Device()->CreateTexture1D(&desc, nullptr, &m1DTex);
-
-		// Check result and except if failed
-		if (FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
-		}
-
-		HRESULT hr = m1DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
-
-		if(FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
-		}
-
-		// Create texture view
-		D3D11_TEXTURE1D_DESC desc;
-
-		m1DTex->GetDesc(&desc);
-		mNumMipmaps = desc.MipLevels - 1;
-
-		ZeroMemory( &mSRVDesc, sizeof(mSRVDesc) );
-		mSRVDesc.Format = desc.Format;
-		mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
-		mSRVDesc.Texture1D.MipLevels = desc.MipLevels;
-		hr = device.getD3D11Device()->CreateShaderResourceView(m1DTex, &mSRVDesc, &mShaderResourceView);
-
-		if (FAILED(hr) || device.hasError())
-		{
-			String errorDescription = device.getErrorDescription(hr);
-			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
-		}
-	}
-
-	void D3D11Texture::_create2DTex()
-	{
-		// We must have those defined here
-		assert(mWidth > 0 || mHeight > 0);
-
-		// Determine which D3D11 pixel format we'll use
-		HRESULT hr;
-		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
-
-		// Determine total number of mipmaps including main one (d3d11 convention)
-		UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
-
-		DXGI_SAMPLE_DESC sampleDesc;
-		sampleDesc.Count = 1;
-		sampleDesc.Quality = 0;
-
-		D3D11_TEXTURE2D_DESC desc;
-		desc.Width			= static_cast<UINT32>(mWidth);
-		desc.Height			= static_cast<UINT32>(mHeight);
-		desc.MipLevels		= numMips;
-		desc.ArraySize		= 1;
-		desc.Format			= d3dPF;
-		desc.SampleDesc		= sampleDesc;
-		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
-		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
-		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
-		desc.MiscFlags		= 0;
-
-        if (this->getTextureType() == TEX_TYPE_CUBE_MAP)
-        {
-                desc.MiscFlags          |= D3D11_RESOURCE_MISC_TEXTURECUBE;
-                desc.ArraySize          = 6;
-        }
-
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		hr = device.getD3D11Device()->CreateTexture1D(&desc, nullptr, &m1DTex);
+
+		// Check result and except if failed
+		if (FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
+		}
+
+		hr = m1DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
+
+		if(FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
+		}
+
+		// Create texture view
+		m1DTex->GetDesc(&desc);
+		mNumMipmaps = desc.MipLevels - 1;
+
+		ZeroMemory( &mSRVDesc, sizeof(mSRVDesc) );
+		mSRVDesc.Format = desc.Format;
+		mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
+		mSRVDesc.Texture1D.MipLevels = desc.MipLevels;
+		hr = device.getD3D11Device()->CreateShaderResourceView(m1DTex, &mSRVDesc, &mShaderResourceView);
+
+		if (FAILED(hr) || device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
+		}
+	}
+
+	void D3D11Texture::_create2DTex()
+	{
+		// We must have those defined here
+		assert(mWidth > 0 || mHeight > 0);
+
+		// Determine which D3D11 pixel format we'll use
+		HRESULT hr;
+		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
+
+		// Determine total number of mipmaps including main one (d3d11 convention)
+		UINT32 numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > mWidth) ? 0 : mNumMipmaps + 1;
+
+		DXGI_SAMPLE_DESC sampleDesc;
+		sampleDesc.Count = 1;
+		sampleDesc.Quality = 0;
+
+		D3D11_TEXTURE2D_DESC desc;
+		desc.Width			= static_cast<UINT32>(mWidth);
+		desc.Height			= static_cast<UINT32>(mHeight);
+		desc.MipLevels		= numMips;
+		desc.ArraySize		= 1;
+		desc.Format			= d3dPF;
+		desc.SampleDesc		= sampleDesc;
+		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
+		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
+		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
+		desc.MiscFlags		= 0;
+
+        if (this->getTextureType() == TEX_TYPE_CUBE_MAP)
+        {
+                desc.MiscFlags          |= D3D11_RESOURCE_MISC_TEXTURECUBE;
+                desc.ArraySize          = 6;
+        }
+
 		// Create the texture
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		hr = device.getD3D11Device()->CreateTexture2D(&desc, nullptr, &m2DTex);
-
-		// Check result and except if failed
-		if (FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
-		}
-
-		HRESULT hr = m2DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
-
-		if(FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
-		}
-
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		hr = device.getD3D11Device()->CreateTexture2D(&desc, nullptr, &m2DTex);
+
+		// Check result and except if failed
+		if (FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
+		}
+
+		hr = m2DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
+
+		if(FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
+		}
+
 		// Create texture view
-		D3D11_TEXTURE2D_DESC desc;
-
-		m2DTex->GetDesc(&desc);
-		mNumMipmaps = desc.MipLevels - 1;
-
-		ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
-		mSRVDesc.Format = desc.Format;
-
-		switch(getTextureType())
-		{
-		case TEX_TYPE_CUBE_MAP:
-			mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
-			mSRVDesc.TextureCube.MipLevels = desc.MipLevels;
-			mSRVDesc.TextureCube.MostDetailedMip = 0;
-			break;
-
-		case TEX_TYPE_2D:
-			mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
-			mSRVDesc.Texture2D.MostDetailedMip = 0;
-			mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
-			break;
-		}
-
-		hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
-
-		if (FAILED(hr) || device.hasError())
-		{
-			String errorDescription = device.getErrorDescription(hr);
-			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
-		}
-	}
-
-	void D3D11Texture::_create3DTex()
-	{
-		// We must have those defined here
-		assert(mWidth > 0 && mHeight > 0 && mDepth > 0);
-
-		// Determine which D3D11 pixel format we'll use
-		HRESULT hr;
-		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
-
-		// Determine total number of mipmaps including main one (d3d11 convention)
-		UINT numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > std::max(std::max(mWidth, mHeight), mDepth)) ? 0 : mNumMipmaps + 1;
-
-		D3D11_TEXTURE3D_DESC desc;
-		desc.Width			= static_cast<UINT32>(mWidth);
-		desc.Height			= static_cast<UINT32>(mHeight);
-		desc.Depth			= static_cast<UINT32>(mDepth);
-		desc.MipLevels		= numMips;
-		desc.Format			= d3dPF;
-		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
-		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
-		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
-		desc.MiscFlags		= 0;
-
+		m2DTex->GetDesc(&desc);
+		mNumMipmaps = desc.MipLevels - 1;
+
+		ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
+		mSRVDesc.Format = desc.Format;
+
+		switch(getTextureType())
+		{
+		case TEX_TYPE_CUBE_MAP:
+			mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
+			mSRVDesc.TextureCube.MipLevels = desc.MipLevels;
+			mSRVDesc.TextureCube.MostDetailedMip = 0;
+			break;
+
+		case TEX_TYPE_2D:
+			mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+			mSRVDesc.Texture2D.MostDetailedMip = 0;
+			mSRVDesc.Texture2D.MipLevels = desc.MipLevels;
+			break;
+		}
+
+		hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
+
+		if (FAILED(hr) || device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
+		}
+	}
+
+	void D3D11Texture::_create3DTex()
+	{
+		// We must have those defined here
+		assert(mWidth > 0 && mHeight > 0 && mDepth > 0);
+
+		// Determine which D3D11 pixel format we'll use
+		HRESULT hr;
+		DXGI_FORMAT d3dPF = D3D11Mappings::_getPF(D3D11Mappings::_getClosestSupportedPF(mFormat));
+
+		// Determine total number of mipmaps including main one (d3d11 convention)
+		UINT numMips = (mNumMipmaps == MIP_UNLIMITED || (1U << mNumMipmaps) > std::max(std::max(mWidth, mHeight), mDepth)) ? 0 : mNumMipmaps + 1;
+
+		D3D11_TEXTURE3D_DESC desc;
+		desc.Width			= static_cast<UINT32>(mWidth);
+		desc.Height			= static_cast<UINT32>(mHeight);
+		desc.Depth			= static_cast<UINT32>(mDepth);
+		desc.MipLevels		= numMips;
+		desc.Format			= d3dPF;
+		desc.Usage			= D3D11Mappings::_getUsage(mUsage);
+		desc.BindFlags		= D3D11_BIND_SHADER_RESOURCE;
+		desc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
+		desc.MiscFlags		= 0;
+
 		// Create the texture
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		hr = device.getD3D11Device()->CreateTexture3D(&desc, nullptr, &m3DTex);
-
-		// Check result and except if failed
-		if (FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
-		}
-
-		HRESULT hr = m3DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
-
-		if(FAILED(hr) || device.hasError())
-		{
-			freeInternalResources();
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
-		}
-
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		hr = device.getD3D11Device()->CreateTexture3D(&desc, nullptr, &m3DTex);
+
+		// Check result and except if failed
+		if (FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Error creating texture\nError Description:" + errorDescription);
+		}
+
+		hr = m3DTex->QueryInterface(__uuidof(ID3D11Resource), (void **)mTex);
+
+		if(FAILED(hr) || device.hasError())
+		{
+			freeInternalResources();
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "Can't get base texture\nError Description:" + errorDescription);
+		}
+
 		// Create texture view
-		D3D11_TEXTURE3D_DESC desc;
-
-		m3DTex->GetDesc(&desc);
-		mNumMipmaps = desc.MipLevels - 1;
-
-		ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
-		mSRVDesc.Format = desc.Format;
-		mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
-		mSRVDesc.Texture3D.MostDetailedMip = 0;
-		mSRVDesc.Texture3D.MipLevels = desc.MipLevels;
-
-		hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
-
-		if (FAILED(hr) || device.hasError())
-		{
-			String errorDescription = device.getErrorDescription(hr);
-			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
-		}
+		m3DTex->GetDesc(&desc);
+		mNumMipmaps = desc.MipLevels - 1;
+
+		ZeroMemory(&mSRVDesc, sizeof(mSRVDesc));
+		mSRVDesc.Format = desc.Format;
+		mSRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
+		mSRVDesc.Texture3D.MostDetailedMip = 0;
+		mSRVDesc.Texture3D.MipLevels = desc.MipLevels;
+
+		hr = device.getD3D11Device()->CreateShaderResourceView(m2DTex, &mSRVDesc, &mShaderResourceView);
+
+		if (FAILED(hr) || device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device can't create shader resource view.\nError Description:" + errorDescription);
+		}
+	}
+
+	void* D3D11Texture::_map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
+	{
+		D3D11_MAPPED_SUBRESOURCE pMappedResource;
+		pMappedResource.pData = nullptr;
+
+		mipLevel = Math::Clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
+		face = Math::Clamp(face, (UINT32)0, mDepth - 1);
+
+		if(getTextureType() == TEX_TYPE_3D)
+			face = 0;
+
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+
+		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
+		device.getImmediateContext()->Map(res, mLockedSubresourceIdx, flags, 0, &pMappedResource);
+
+		if (device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
+		}
+
+		return pMappedResource.pData;
+	}
+
+	void D3D11Texture::_unmap(ID3D11Resource* res)
+	{
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		device.getImmediateContext()->Unmap(res, mLockedSubresourceIdx);
+
+		if (device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
+		}
+	}
+
+	void* D3D11Texture::_mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
+	{
+		if(!mStagingBuffer)
+			_createStagingBuffer();
+
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		device.getImmediateContext()->CopyResource(mStagingBuffer, mTex);
+
+		return _map(mStagingBuffer, flags, face, mipLevel);
+	}
+
+	void D3D11Texture::_unmapstagingbuffer()
+	{
+		_unmap(mStagingBuffer);
 	}
 
-	void* D3D11Texture::_map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
-	{
-		D3D11_MAPPED_SUBRESOURCE pMappedResource;
-		pMappedResource.pData = nullptr;
-
-		mipLevel = Math::Clamp(mipLevel, (UINT32)mipLevel, getNumMipmaps());
-		face = Math::Clamp(face, (UINT32)0, mDepth - 1);
-
-		if(getTextureType() == TEX_TYPE_3D)
-			face = 0;
-
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-
-		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
-		device.getImmediateContext()->Map(res, mLockedSubresourceIdx, flags, 0, &pMappedResource);
-
-		if (device.hasError())
-		{
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
-		}
-
-		return pMappedResource.pData;
-	}
-
-	void D3D11Texture::_unmap(ID3D11Resource* res)
-	{
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		device.getImmediateContext()->Unmap(res, mLockedSubresourceIdx);
-
-		if (device.hasError())
-		{
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "D3D11 device unmap resource\nError Description:" + errorDescription);
-		}
-	}
-
-	void* D3D11Texture::_mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face)
-	{
-		if(!mStagingBuffer)
-			_createStagingBuffer();
-
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		device.getImmediateContext()->CopyResource(mStagingBuffer, mTex);
-
-		return _map(mStagingBuffer, flags, face, mipLevel);
-	}
-
-	void D3D11Texture::_unmapstagingbuffer()
-	{
-		_unmap(mStagingBuffer);
-	}
-
-	void* D3D11Texture::_mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face)
-	{
-		UINT32 sizeOfImage = lock.getConsecutiveSize();
-		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
-
-		UINT8* bufferData = new UINT8[sizeOfImage];
-		mStaticBuffer = new PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat(), bufferData, true);
-
-		return bufferData;
-	}
-
-	void D3D11Texture::_unmapstaticbuffer()
-	{
-		size_t rowWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth());
-		size_t sliceWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth(), mStaticBuffer->getHeight());
-
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		device.getImmediateContext()->UpdateSubresource(mTex, mLockedSubresourceIdx, nullptr, mStaticBuffer->data, rowWidth, sliceWidth);
-
-		if (device.hasError())
-		{
-			String errorDescription = device.getErrorDescription();
-			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
-		}
-
-		SAFE_DELETE(mStaticBuffer);
-	}
-
-	void D3D11Texture::_createStagingBuffer()
-	{
-		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
-		switch (getTextureType())
-		{
-		case TEX_TYPE_1D:
-			{
-				D3D11_TEXTURE1D_DESC desc;
-				m1DTex->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				device.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
-			} 					
-			break;
-		case TEX_TYPE_2D:
-		case TEX_TYPE_CUBE_MAP:
-		case TEX_TYPE_2D_ARRAY:
-			{
-				D3D11_TEXTURE2D_DESC desc;
-				m2DTex->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				device.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
-			}
-			break;
-		case TEX_TYPE_3D:
-			{
-				D3D11_TEXTURE3D_DESC desc;
-				m3DTex->GetDesc(&desc);
-
-				desc.BindFlags = 0;
-				desc.MiscFlags = 0;
-				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
-				desc.Usage = D3D11_USAGE_STAGING;
-
-				device.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
-			}
-			break;
-		}
+	void* D3D11Texture::_mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face)
+	{
+		UINT32 sizeOfImage = lock.getConsecutiveSize();
+		mLockedSubresourceIdx = D3D11CalcSubresource(mipLevel, face, getNumMipmaps()+1);
+
+		UINT8* bufferData = new UINT8[sizeOfImage];
+		mStaticBuffer = new PixelData(lock.getWidth(), lock.getHeight(), lock.getDepth(), lock.getFormat(), bufferData, true);
+
+		return bufferData;
+	}
+
+	void D3D11Texture::_unmapstaticbuffer()
+	{
+		UINT32 rowWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth());
+		UINT32 sliceWidth = D3D11Mappings::_getSizeInBytes(mStaticBuffer->getFormat(), mStaticBuffer->getWidth(), mStaticBuffer->getHeight());
+
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		device.getImmediateContext()->UpdateSubresource(mTex, mLockedSubresourceIdx, nullptr, mStaticBuffer->data, rowWidth, sliceWidth);
+
+		if (device.hasError())
+		{
+			String errorDescription = device.getErrorDescription();
+			CM_EXCEPT(RenderingAPIException, "D3D11 device cannot map texture\nError Description:" + errorDescription);
+		}
+
+		SAFE_DELETE(mStaticBuffer);
+	}
+
+	void D3D11Texture::_createStagingBuffer()
+	{
+		D3D11Device& device = D3D11RenderSystem::getPrimaryDevice();
+		switch (getTextureType())
+		{
+		case TEX_TYPE_1D:
+			{
+				D3D11_TEXTURE1D_DESC desc;
+				m1DTex->GetDesc(&desc);
+
+				desc.BindFlags = 0;
+				desc.MiscFlags = 0;
+				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
+				desc.Usage = D3D11_USAGE_STAGING;
+
+				device.getD3D11Device()->CreateTexture1D(&desc, NULL, (ID3D11Texture1D**)(&mStagingBuffer));
+			} 					
+			break;
+		case TEX_TYPE_2D:
+		case TEX_TYPE_CUBE_MAP:
+		case TEX_TYPE_2D_ARRAY:
+			{
+				D3D11_TEXTURE2D_DESC desc;
+				m2DTex->GetDesc(&desc);
+
+				desc.BindFlags = 0;
+				desc.MiscFlags = 0;
+				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
+				desc.Usage = D3D11_USAGE_STAGING;
+
+				device.getD3D11Device()->CreateTexture2D(&desc, NULL, (ID3D11Texture2D**)(&mStagingBuffer));
+			}
+			break;
+		case TEX_TYPE_3D:
+			{
+				D3D11_TEXTURE3D_DESC desc;
+				m3DTex->GetDesc(&desc);
+
+				desc.BindFlags = 0;
+				desc.MiscFlags = 0;
+				desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
+				desc.Usage = D3D11_USAGE_STAGING;
+
+				device.getD3D11Device()->CreateTexture3D(&desc, NULL, (ID3D11Texture3D**)(&mStagingBuffer));
+			}
+			break;
+		}
 	}
 }
 

+ 1 - 1
CamelotD3D9Renderer/Include/CmD3D9Mappings.h

@@ -86,7 +86,7 @@ namespace CamelotEngine
         /// return the combination of D3DUSAGE values for Ogre buffer usage
         static DWORD get(HardwareBuffer::Usage usage);
         /// Get lock options
-        static DWORD get(HardwareBuffer::LockOptions options, HardwareBuffer::Usage usage);
+        static DWORD get(LockOptions options, HardwareBuffer::Usage usage);
         /// Get index type
         static D3DFORMAT get(HardwareIndexBuffer::IndexType itype);
 		/// Get vertex data type

+ 2 - 2
CamelotD3D9Renderer/Source/CmD3D9HardwareIndexBuffer.cpp

@@ -156,7 +156,7 @@ namespace CamelotEngine {
     {
        // There is no functional interface in D3D, just do via manual 
         // lock, copy & unlock
-        void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
+        void* pSrc = this->lock(offset, length, HBL_READ_ONLY);
         memcpy(pDest, pSrc, length);
         this->unlock();
 
@@ -169,7 +169,7 @@ namespace CamelotEngine {
         // There is no functional interface in D3D, just do via manual 
         // lock, copy & unlock
         void* pDst = this->lock(offset, length, 
-            discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
+            discardWholeBuffer ? HBL_WRITE_ONLY_DISCARD : HBL_READ_WRITE);
         memcpy(pDst, pSource, length);
         this->unlock();    
 	}

+ 2 - 2
CamelotD3D9Renderer/Source/CmD3D9HardwareVertexBuffer.cpp

@@ -156,7 +156,7 @@ namespace CamelotEngine {
     {
         // There is no functional interface in D3D, just do via manual 
         // lock, copy & unlock
-        void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
+        void* pSrc = this->lock(offset, length, HBL_READ_ONLY);
         memcpy(pDest, pSrc, length);
         this->unlock();
 
@@ -169,7 +169,7 @@ namespace CamelotEngine {
 		// There is no functional interface in D3D, just do via manual 
 		// lock, copy & unlock
 		void* pDst = this->lock(offset, length, 
-			discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
+			discardWholeBuffer ? HBL_WRITE_ONLY_DISCARD : HBL_READ_WRITE);
 		memcpy(pDst, pSource, length);
 		this->unlock();
 	}

+ 4 - 4
CamelotD3D9Renderer/Source/CmD3D9Mappings.cpp

@@ -329,10 +329,10 @@ namespace CamelotEngine
         return ret;
     }
 	//---------------------------------------------------------------------
-    DWORD D3D9Mappings::get(HardwareBuffer::LockOptions options, HardwareBuffer::Usage usage)
+    DWORD D3D9Mappings::get(LockOptions options, HardwareBuffer::Usage usage)
     {
         DWORD ret = 0;
-        if (options == HardwareBuffer::HBL_DISCARD)
+        if (options == HBL_WRITE_ONLY_DISCARD)
         {
 #if CM_D3D_MANAGE_BUFFERS
             // Only add the discard flag for dynamic usgae and default pool
@@ -345,7 +345,7 @@ namespace CamelotEngine
                 ret |= D3DLOCK_DISCARD;
 #endif
         }
-        if (options == HardwareBuffer::HBL_READ_ONLY)
+        if (options == HBL_READ_ONLY)
         {
 			// D3D debug runtime doesn't like you locking managed buffers readonly
 			// when they were created with write-only (even though you CAN read
@@ -354,7 +354,7 @@ namespace CamelotEngine
 				ret |= D3DLOCK_READONLY;
 
         }
-        if (options == HardwareBuffer::HBL_NO_OVERWRITE)
+        if (options == HBL_WRITE_ONLY_NO_OVERWRITE)
         {
 #if CM_D3D_MANAGE_BUFFERS
             // Only add the nooverwrite flag for dynamic usgae and default pool

+ 2 - 2
CamelotGLRenderer/Source/CmGLHardwarePixelBuffer.cpp

@@ -74,7 +74,7 @@ void GLHardwarePixelBuffer::freeBuffer()
 PixelData GLHardwarePixelBuffer::lockImpl(const Box lockBox,  LockOptions options)
 {
 	allocateBuffer();
-	if(options != HardwareBuffer::HBL_DISCARD) 
+	if(options != HBL_WRITE_ONLY_DISCARD) 
 	{
 		// Download the old contents of the texture
 		download(mBuffer);
@@ -86,7 +86,7 @@ PixelData GLHardwarePixelBuffer::lockImpl(const Box lockBox,  LockOptions option
 //-----------------------------------------------------------------------------  
 void GLHardwarePixelBuffer::unlockImpl(void)
 {
-	if (mCurrentLockOptions != HardwareBuffer::HBL_READ_ONLY)
+	if (mCurrentLockOptions != HBL_READ_ONLY)
 	{
 		// From buffer to card, only upload if was locked for writing
 		upload(mCurrentLock, mLockedBox);

+ 4 - 4
CamelotRenderer/Source/CmMesh.cpp

@@ -60,7 +60,7 @@ namespace CamelotEngine
 			mIndexData->indexCount, 
 			HardwareBuffer::HBU_STATIC_WRITE_ONLY);
 
-		UINT32* idxData = static_cast<UINT32*>(mIndexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL));
+		UINT32* idxData = static_cast<UINT32*>(mIndexData->indexBuffer->lock(HBL_READ_WRITE));
 
 		for(UINT32 i = 0; i < mIndexData->indexCount; i++)
 		{
@@ -89,7 +89,7 @@ namespace CamelotEngine
 			mVertexData->vertexBufferBinding->setBinding(streamIdx, vertexBuffer);
 
 			UINT32 vertexSize = vertexBuffer->getVertexSize();
-			UINT8* vertBufferData = static_cast<UINT8*>(vertexBuffer->lock(HardwareBuffer::HBL_NORMAL));
+			UINT8* vertBufferData = static_cast<UINT8*>(vertexBuffer->lock(HBL_READ_WRITE));
 
 			UINT32 numElements = mVertexData->vertexDeclaration->getElementCount();
 
@@ -192,7 +192,7 @@ namespace CamelotEngine
 			meshData->indexCount = mIndexData->indexCount - mIndexData->indexStart;
 			meshData->index = new int[meshData->indexCount];
 
-			UINT16* idxData = static_cast<UINT16*>(mIndexData->indexBuffer->lock(HardwareBuffer::HBL_READ_ONLY));
+			UINT16* idxData = static_cast<UINT16*>(mIndexData->indexBuffer->lock(HBL_READ_ONLY));
 
 			for(UINT32 i = 0; i < mIndexData->indexCount; i++)
 				meshData->index[i] = (UINT32)idxData[i];
@@ -212,7 +212,7 @@ namespace CamelotEngine
 
 				HardwareVertexBufferPtr vertexBuffer = mVertexData->vertexBufferBinding->getBuffer(i);
 				UINT32 vertexSize = vertexBuffer->getVertexSize();
-				UINT8* vertDataIter = static_cast<UINT8*>(vertexBuffer->lock(HardwareBuffer::HBL_READ_ONLY));
+				UINT8* vertDataIter = static_cast<UINT8*>(vertexBuffer->lock(HBL_READ_ONLY));
 
 				std::shared_ptr<MeshData::VertexData> vertexData(new MeshData::VertexData(meshData->vertexCount, i));
 				meshData->vertexBuffers[i] = vertexData;

+ 5 - 6
CamelotRenderer/Source/CmVertexIndexData.cpp

@@ -159,7 +159,7 @@ namespace CamelotEngine {
                 itBinding->second->getVertexSize();
             oldBufferLocks[itBinding->first] =
                 itBinding->second->lock(
-                    HardwareBuffer::HBL_READ_ONLY);
+                    HBL_READ_ONLY);
         }
 		
 		// Create new buffers and lock all for writing
@@ -177,7 +177,7 @@ namespace CamelotEngine {
 
             newBufferVertexSizes.push_back(vertexSize);
 			newBufferLocks.push_back(
-				vbuf->lock(HardwareBuffer::HBL_DISCARD));
+				vbuf->lock(HBL_WRITE_ONLY_DISCARD));
 			buf++;
 		}
 
@@ -413,7 +413,7 @@ namespace CamelotEngine {
 
 			if (conversionNeeded)
 			{
-				void* pBase = bindi->second->lock(HardwareBuffer::HBL_NORMAL);
+				void* pBase = bindi->second->lock(HBL_READ_WRITE);
 
 				for (UINT32 v = 0; v < bindi->second->getNumVertices(); ++v)
 				{
@@ -586,13 +586,12 @@ namespace CamelotEngine {
 			c = t;
 		}
 	};
-    //-----------------------------------------------------------------------
     //-----------------------------------------------------------------------
 	void IndexData::optimiseVertexCacheTriList(void)
 	{
 		if (indexBuffer->isLocked()) return;
 
-		void *buffer = indexBuffer->lock(HardwareBuffer::HBL_NORMAL);
+		void *buffer = indexBuffer->lock(HBL_READ_WRITE);
 
 		Triangle* triangles;
 		UINT32 *dest;
@@ -699,7 +698,7 @@ namespace CamelotEngine {
     {
 		if (indexBuffer->isLocked()) return;
 
-		UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(HardwareBuffer::HBL_READ_ONLY);
+		UINT16 *shortbuffer = (UINT16 *)indexBuffer->lock(HBL_READ_ONLY);
 
 		if (indexBuffer->getType() == HardwareIndexBuffer::IT_16BIT)
 			for (unsigned int i = 0; i < indexBuffer->getNumIndexes(); ++i)

Some files were not shown because too many files changed in this diff