diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2017-11-13 02:54:50 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2017-11-13 02:54:50 +0000 |
| commit | 8e24a0919d30f939140e776bc5e64bd77767e99c (patch) | |
| tree | bfa1c37385cafe7d6c192ed0c8eb936aa828cb5a /Graphics/GraphicsEngineD3D11 | |
| parent | Merge from dev branch (diff) | |
| download | DiligentCore-8e24a0919d30f939140e776bc5e64bd77767e99c.tar.gz DiligentCore-8e24a0919d30f939140e776bc5e64bd77767e99c.zip | |
Updated to Diligent Engine 2.1
Diffstat (limited to 'Graphics/GraphicsEngineD3D11')
41 files changed, 1061 insertions, 357 deletions
diff --git a/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems b/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems index 9e8c8b8f..f850aa42 100644 --- a/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems +++ b/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems @@ -86,4 +86,7 @@ <DeploymentContent>false</DeploymentContent> </Text> </ItemGroup> + <ItemGroup> + <None Include="$(MSBuildThisFileDirectory)..\..\readme.md" /> + </ItemGroup> </Project>
\ No newline at end of file diff --git a/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems.filters b/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems.filters index e23371f0..d3e94a3c 100644 --- a/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems.filters +++ b/Graphics/GraphicsEngineD3D11/build/Windows.Shared/GraphicsEngineD3D11.Shared.vcxitems.filters @@ -195,4 +195,7 @@ <ItemGroup> <Text Include="$(MSBuildThisFileDirectory)..\..\CMakeLists.txt" /> </ItemGroup> + <ItemGroup> + <None Include="$(MSBuildThisFileDirectory)..\..\readme.md" /> + </ItemGroup> </Project>
\ No newline at end of file diff --git a/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h index ae9e5297..57d27d2a 100644 --- a/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/BufferD3D11Impl.h @@ -48,15 +48,21 @@ enum class D3D11BufferState }; /// Implementation of the Diligent::IBufferD3D11 interface -class BufferD3D11Impl : public BufferBase<IBufferD3D11, BufferViewD3D11Impl, FixedBlockMemoryAllocator, FixedBlockMemoryAllocator> +class BufferD3D11Impl : public BufferBase<IBufferD3D11, BufferViewD3D11Impl, FixedBlockMemoryAllocator> { public: - typedef BufferBase<IBufferD3D11, BufferViewD3D11Impl, FixedBlockMemoryAllocator, FixedBlockMemoryAllocator> TBufferBase; - BufferD3D11Impl(FixedBlockMemoryAllocator &BufferObjMemAllocator, + typedef BufferBase<IBufferD3D11, BufferViewD3D11Impl, FixedBlockMemoryAllocator> TBufferBase; + BufferD3D11Impl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &BuffViewObjMemAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const BufferDesc& BuffDesc, const BufferData &BuffData = BufferData()); + BufferD3D11Impl(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &BuffViewObjMemAllocator, + class RenderDeviceD3D11Impl *pDeviceD3D11, + const BufferDesc& BuffDesc, + ID3D11Buffer *pd3d11Buffer); + ~BufferD3D11Impl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; @@ -64,9 +70,11 @@ public: virtual void UpdateData( IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData )override final; virtual void CopyData( IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override final; virtual void Map( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData )override final; - virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType )override final; + virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags )override final; virtual ID3D11Buffer *GetD3D11Buffer()override final{ return m_pd3d11Buffer; } + virtual void* GetNativeHandle()override final { return GetD3D11Buffer(); } + void ResetState(D3D11BufferState State){m_State = static_cast<Uint32>(State);} void AddState(D3D11BufferState State){m_State |= static_cast<Uint32>(State);} void ClearState(D3D11BufferState State){m_State &= ~static_cast<Uint32>(State);} diff --git a/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h index ffc65719..47a6a6b0 100644 --- a/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/BufferViewD3D11Impl.h @@ -35,12 +35,12 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::IBufferViewD3D11 interface -class BufferViewD3D11Impl : public BufferViewBase<IBufferViewD3D11, FixedBlockMemoryAllocator> +class BufferViewD3D11Impl : public BufferViewBase<IBufferViewD3D11> { public: - typedef BufferViewBase<IBufferViewD3D11, FixedBlockMemoryAllocator> TBufferViewBase; + typedef BufferViewBase<IBufferViewD3D11> TBufferViewBase; - BufferViewD3D11Impl( FixedBlockMemoryAllocator &BuffViewObjAllocator, + BufferViewD3D11Impl( IReferenceCounters *pRefCounters, IRenderDevice *pDevice, const BufferViewDesc& ViewDesc, class IBuffer *pBuffer, diff --git a/Graphics/GraphicsEngineD3D11/include/CommandListD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/CommandListD3D11Impl.h index 631db936..6f95fdfc 100644 --- a/Graphics/GraphicsEngineD3D11/include/CommandListD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/CommandListD3D11Impl.h @@ -35,11 +35,11 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::ICommandListD3D11 interface -class CommandListD3D11Impl : public CommandListBase<ICommandList, FixedBlockMemoryAllocator> +class CommandListD3D11Impl : public CommandListBase<ICommandList> { public: - typedef CommandListBase<ICommandList, FixedBlockMemoryAllocator> TCommandListBase; - CommandListD3D11Impl(FixedBlockMemoryAllocator &CommandListObjMemAllocator, + typedef CommandListBase<ICommandList> TCommandListBase; + CommandListD3D11Impl(IReferenceCounters *pRefCounters, IRenderDevice *pDevice, ID3D11CommandList *pd3d11CommandList); ~CommandListD3D11Impl(); diff --git a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h index ca494999..60d43770 100644 --- a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h +++ b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.h @@ -46,48 +46,101 @@ inline UINT BindFlagsToD3D11BindFlags(Uint32 BindFlags) return D3D11BindFlags; } +inline Uint32 D3D11BindFlagsToBindFlags(UINT D3D11BindFlags) +{ + Uint32 BindFlags = 0; + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_VERTEX_BUFFER) ? BIND_VERTEX_BUFFER : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_INDEX_BUFFER) ? BIND_INDEX_BUFFER : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_CONSTANT_BUFFER) ? BIND_UNIFORM_BUFFER : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_SHADER_RESOURCE) ? BIND_SHADER_RESOURCE : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_STREAM_OUTPUT) ? BIND_STREAM_OUTPUT : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_RENDER_TARGET) ? BIND_RENDER_TARGET : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_DEPTH_STENCIL) ? BIND_DEPTH_STENCIL : 0); + BindFlags = BindFlags | ((D3D11BindFlags & D3D11_BIND_UNORDERED_ACCESS) ? BIND_UNORDERED_ACCESS : 0); + VERIFY_EXPR( (D3D11BindFlags & + (D3D11_BIND_VERTEX_BUFFER|D3D11_BIND_INDEX_BUFFER|D3D11_BIND_CONSTANT_BUFFER| + D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_STREAM_OUTPUT|D3D11_BIND_RENDER_TARGET| + D3D11_BIND_DEPTH_STENCIL|D3D11_BIND_UNORDERED_ACCESS)) == BindFlagsToD3D11BindFlags(BindFlags)); + return BindFlags; +} + D3D11_PRIMITIVE_TOPOLOGY TopologyToD3D11Topology(PRIMITIVE_TOPOLOGY Topology); inline D3D11_USAGE UsageToD3D11Usage(USAGE Usage) { switch(Usage) { - case USAGE_STATIC: return D3D11_USAGE_IMMUTABLE; - case USAGE_DEFAULT: return D3D11_USAGE_DEFAULT; - case USAGE_DYNAMIC: return D3D11_USAGE_DYNAMIC; + case USAGE_STATIC: return D3D11_USAGE_IMMUTABLE; + case USAGE_DEFAULT: return D3D11_USAGE_DEFAULT; + case USAGE_DYNAMIC: return D3D11_USAGE_DYNAMIC; case USAGE_CPU_ACCESSIBLE: return D3D11_USAGE_STAGING; default: UNEXPECTED("Unknow usage" ); return D3D11_USAGE_DEFAULT; } } -inline D3D11_MAP MapTypeToD3D11MapType(MAP_TYPE MapType) +inline USAGE D3D11UsageToUsage(D3D11_USAGE D3D11Usage) +{ + switch(D3D11Usage) + { + case D3D11_USAGE_IMMUTABLE: return USAGE_STATIC; + case D3D11_USAGE_DEFAULT: return USAGE_DEFAULT; + case D3D11_USAGE_DYNAMIC: return USAGE_DYNAMIC; + case D3D11_USAGE_STAGING: return USAGE_CPU_ACCESSIBLE; + default: UNEXPECTED("Unknow D3D11 usage" ); return USAGE_DEFAULT; + } +} + +inline void MapParamsToD3D11MapParams(MAP_TYPE MapType, Uint32 MapFlags, D3D11_MAP &d3d11MapType, UINT d3d11MapFlags) { + d3d11MapType = static_cast<D3D11_MAP>(0); switch(MapType) { - case MAP_READ: return D3D11_MAP_READ; - case MAP_WRITE: return D3D11_MAP_WRITE; - case MAP_READ_WRITE: return D3D11_MAP_READ_WRITE; - case MAP_WRITE_DISCARD: return D3D11_MAP_WRITE_DISCARD; - case MAP_WRITE_NO_OVERWRITE:return D3D11_MAP_WRITE_NO_OVERWRITE; - default: UNEXPECTED( "Unknown map type" ); return D3D11_MAP_READ; + case MAP_READ: + d3d11MapType = D3D11_MAP_READ; + break; + + case MAP_WRITE: + if(MapFlags & MAP_FLAG_DISCARD) + d3d11MapType = D3D11_MAP_WRITE_DISCARD; + else if(MapFlags & MAP_FLAG_DO_NOT_SYNCHRONIZE) + d3d11MapType = D3D11_MAP_WRITE_NO_OVERWRITE; + else + d3d11MapType = D3D11_MAP_WRITE; + break; + + case MAP_READ_WRITE: + d3d11MapType = D3D11_MAP_READ_WRITE; + break; + + default: + UNEXPECTED( "Unknown map type" ); } + + d3d11MapFlags = 0; + d3d11MapFlags |= (MapFlags & MAP_FLAG_DO_NOT_WAIT) ? D3D11_MAP_FLAG_DO_NOT_WAIT : 0; } inline UINT MapFlagsToD3D11MapFlags(Uint32 MapFlags) { - UINT D3D11MapFlags = 0; - D3D11MapFlags |= (MapFlags & MAP_FLAG_DO_NOT_WAIT) ? D3D11_MAP_FLAG_DO_NOT_WAIT : 0; - return D3D11MapFlags; } inline UINT CPUAccessFlagsToD3D11CPUAccessFlags(Uint32 Flags) { UINT D3D11CPUAccessFlags = 0; - D3D11CPUAccessFlags |= (Flags & CPU_ACCESS_READ) ? D3D11_CPU_ACCESS_READ : 0; + D3D11CPUAccessFlags |= (Flags & CPU_ACCESS_READ) ? D3D11_CPU_ACCESS_READ : 0; D3D11CPUAccessFlags |= (Flags & CPU_ACCESS_WRITE) ? D3D11_CPU_ACCESS_WRITE : 0; return D3D11CPUAccessFlags; } +inline Uint32 D3D11CPUAccessFlagsToCPUAccessFlags(UINT D3D11CPUAccessFlags) +{ + Uint32 CPUAccessFlags = 0; + CPUAccessFlags |= (D3D11CPUAccessFlags & D3D11_CPU_ACCESS_READ) ? CPU_ACCESS_READ : 0; + CPUAccessFlags |= (D3D11CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) ? CPU_ACCESS_WRITE : 0; + VERIFY_EXPR(D3D11CPUAccessFlags == CPUAccessFlagsToD3D11CPUAccessFlags(CPUAccessFlags)); + return CPUAccessFlags; +} + inline UINT MiscTextureFlagsToD3D11Flags(Uint32 Flags) { UINT D3D11MiscFlags = 0; @@ -95,6 +148,13 @@ inline UINT MiscTextureFlagsToD3D11Flags(Uint32 Flags) return D3D11MiscFlags; } +inline Uint32 D3D11MiscFlagsToMiscTextureFlags(UINT D3D11MiscFlags) +{ + Uint32 MiscFlags = 0; + MiscFlags |= (D3D11MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS) ? MISC_TEXTURE_FLAG_GENERATE_MIPS : 0; + return MiscFlags; +} + D3D11_FILTER FilterTypeToD3D11Filter(FILTER_TYPE MinFilter, FILTER_TYPE MagFilter, FILTER_TYPE MipFilter); D3D11_TEXTURE_ADDRESS_MODE TexAddressModeToD3D11AddressMode(TEXTURE_ADDRESS_MODE Mode); diff --git a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h index 9eab566e..a48b5ef6 100644 --- a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.h @@ -43,7 +43,7 @@ class DeviceContextD3D11Impl : public DeviceContextBase<IDeviceContextD3D11> public: typedef DeviceContextBase<IDeviceContextD3D11> TDeviceContextBase; - DeviceContextD3D11Impl(IMemoryAllocator &Allocator, IRenderDevice *pDevice, ID3D11DeviceContext *pd3d11DeviceContext, const struct EngineD3D11Attribs &EngineAttribs, bool bIsDeferred); + DeviceContextD3D11Impl(IReferenceCounters *pRefCounters, IMemoryAllocator &Allocator, IRenderDevice *pDevice, ID3D11DeviceContext *pd3d11DeviceContext, const struct EngineD3D11Attribs &EngineAttribs, bool bIsDeferred); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; virtual void SetPipelineState(IPipelineState *pPipelineState)override final; @@ -57,7 +57,8 @@ public: virtual void SetBlendFactors(const float* pBlendFactors = nullptr)override final; virtual void SetVertexBuffers( Uint32 StartSlot, Uint32 NumBuffersSet, IBuffer **ppBuffers, Uint32 *pStrides, Uint32 *pOffsets, Uint32 Flags )override final; - virtual void ClearState()override final; + + virtual void InvalidateState()override final; virtual void SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset )override final; @@ -83,12 +84,15 @@ public: ID3D11DeviceContext* GetD3D11DeviceContext(){ return m_pd3d11DeviceContext; } - void RebindRenderTargets(); - - /// Clears the state caches. This function is called once per frame - /// (before present) to release all outstanding objects - /// that are only kept alive by references in the cache - void ClearShaderStateCache(); + void CommitRenderTargets(); + + /// Clears the committed shader resource cache. This function + /// is called once per frame (before present) to release all + /// outstanding objects that are only kept alive by references + /// in the cache. The function does not release cached vertex and + /// index buffers, input layout, depth-stencil, rasterizer, and blend + /// states. + void ReleaseCommittedShaderResources(); /// Number of different shader types (Vertex, Pixel, Geometry, Domain, Hull, Compute) static const int NumShaderTypes = 6; diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index e5a89d21..ff38e664 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -37,12 +37,12 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::IPipelineStateD3D11 interface -class PipelineStateD3D11Impl : public PipelineStateBase<IPipelineStateD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator> +class PipelineStateD3D11Impl : public PipelineStateBase<IPipelineStateD3D11, IRenderDeviceD3D11> { public: - typedef PipelineStateBase<IPipelineStateD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator> TPipelineStateBase; + typedef PipelineStateBase<IPipelineStateD3D11, IRenderDeviceD3D11> TPipelineStateBase; - PipelineStateD3D11Impl(FixedBlockMemoryAllocator &PSOAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const PipelineStateDesc& PipelineDesc); + PipelineStateD3D11Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D11Impl *pDeviceD3D11, const PipelineStateDesc& PipelineDesc); ~PipelineStateD3D11Impl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; @@ -70,15 +70,17 @@ public: virtual void CreateShaderResourceBinding( IShaderResourceBinding **ppShaderResourceBinding )override final; class ShaderResourceBindingD3D11Impl* GetDefaultResourceBinding(){return m_pDefaultShaderResBinding.get();} - IMemoryAllocator &GetResourceCacheDataAllocator(Uint32 Ind) + IMemoryAllocator &GetResourceCacheDataAllocator(Uint32 ActiveShaderInd) { - VERIFY_EXPR(Ind < _countof(m_ResourceCacheDataAllocators)); - return m_ResourceCacheDataAllocators[Ind]; + VERIFY_EXPR(ActiveShaderInd < m_NumShaders); + auto *pAllocator = m_Allocators.GetResourceCacheDataAllocator(ActiveShaderInd); + return pAllocator != nullptr ? *pAllocator : GetRawAllocator(); } - IMemoryAllocator &GetShaderResLayoutDataAllocators(Uint32 Ind) + IMemoryAllocator &GetShaderResLayoutDataAllocators(Uint32 ActiveShaderInd) { - VERIFY_EXPR(Ind < _countof(m_ShaderResLayoutDataAllocators)); - return m_ShaderResLayoutDataAllocators[Ind]; + VERIFY_EXPR(ActiveShaderInd < m_NumShaders); + auto *pAllocator = m_Allocators.GetShaderResLayoutDataAllocator(ActiveShaderInd); + return pAllocator != nullptr ? *pAllocator : GetRawAllocator(); } IShaderVariable* GetDummyShaderVariable(){return &m_DummyShaderVar;} @@ -88,10 +90,28 @@ private: CComPtr<ID3D11DepthStencilState> m_pd3d11DepthStencilState; CComPtr<ID3D11InputLayout> m_pd3d11InputLayout; - AdaptiveFixedBlockAllocator m_ShaderResLayoutDataAllocators[5]; // Use separate allocator for every shader stage - AdaptiveFixedBlockAllocator m_ResourceCacheDataAllocators[5]; // Use separate allocator for every shader stage + class DataAllocators + { + public: + ~DataAllocators(); + void Init(size_t NumActiveShaders, Uint32 SRBAllocationGranularity); + AdaptiveFixedBlockAllocator* GetShaderResLayoutDataAllocator(Uint32 Ind) + { + VERIFY_EXPR(Ind < _countof(m_pShaderResLayoutDataAllocators)); + return m_pShaderResLayoutDataAllocators[Ind]; + } + AdaptiveFixedBlockAllocator* GetResourceCacheDataAllocator(Uint32 Ind) + { + VERIFY_EXPR(Ind < _countof(m_pResourceCacheDataAllocators)); + return m_pResourceCacheDataAllocators[Ind]; + } + private: + AdaptiveFixedBlockAllocator* m_pShaderResLayoutDataAllocators[5] = {}; // Use separate allocator for every shader stage + AdaptiveFixedBlockAllocator* m_pResourceCacheDataAllocators[5] = {}; // Use separate allocator for every shader stage + }m_Allocators; // Allocators must be defined before the default shader res binding // Do not use strong reference to avoid cyclic references + // Must be declared after the data allocators std::unique_ptr<class ShaderResourceBindingD3D11Impl, STDDeleter<ShaderResourceBindingD3D11Impl, FixedBlockMemoryAllocator> > m_pDefaultShaderResBinding; DummyShaderVariable m_DummyShaderVar; ///< Dummy shader variable diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h index 4b1dc1e8..2255b211 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.h @@ -40,7 +40,7 @@ class RenderDeviceD3D11Impl : public RenderDeviceD3DBase<IRenderDeviceD3D11> public: typedef RenderDeviceD3DBase<IRenderDeviceD3D11> TRenderDeviceBase; - RenderDeviceD3D11Impl( IMemoryAllocator &RawMemAllocator, const EngineD3D11Attribs& EngineAttribs, ID3D11Device *pd3d11Device, Uint32 NumDeferredContexts ); + RenderDeviceD3D11Impl( IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator, const EngineD3D11Attribs& EngineAttribs, ID3D11Device *pd3d11Device, Uint32 NumDeferredContexts ); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; virtual void CreateBuffer(const BufferDesc& BuffDesc, const BufferData &BuffData, IBuffer **ppBuffer)override final; @@ -55,6 +55,11 @@ public: ID3D11Device* GetD3D11Device()override final{return m_pd3d11Device;} + virtual void CreateBufferFromD3DResource(ID3D11Buffer *pd3d11Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer)override final; + virtual void CreateTextureFromD3DResource(ID3D11Texture1D *pd3d11Texture, ITexture **ppTexture)override final; + virtual void CreateTextureFromD3DResource(ID3D11Texture2D *pd3d11Texture, ITexture **ppTexture)override final; + virtual void CreateTextureFromD3DResource(ID3D11Texture3D *pd3d11Texture, ITexture **ppTexture)override final; + private: virtual void TestTextureFormat( TEXTURE_FORMAT TexFormat ); diff --git a/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h index 6474f939..eaa81827 100644 --- a/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SamplerD3D11Impl.h @@ -35,12 +35,12 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::ISamplerD3D11 interface -class SamplerD3D11Impl : public SamplerBase<ISamplerD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator> +class SamplerD3D11Impl : public SamplerBase<ISamplerD3D11, IRenderDeviceD3D11> { public: - typedef SamplerBase<ISamplerD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator> TSamplerBase; + typedef SamplerBase<ISamplerD3D11, IRenderDeviceD3D11> TSamplerBase; - SamplerD3D11Impl(FixedBlockMemoryAllocator &SamplerObjAllocator, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const SamplerDesc& SamplerDesc); + SamplerD3D11Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const SamplerDesc& SamplerDesc); ~SamplerD3D11Impl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface ) final; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h index 59d30da7..9f6d8b97 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderD3D11Impl.h @@ -42,12 +42,12 @@ class FixedBlockMemoryAllocator; class ResourceMapping; /// Implementation of the Diligent::IShaderD3D11 interface -class ShaderD3D11Impl : public ShaderBase<IShaderD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator>, public ShaderD3DBase +class ShaderD3D11Impl : public ShaderBase<IShaderD3D11, IRenderDeviceD3D11>, public ShaderD3DBase { public: - typedef ShaderBase<IShaderD3D11, IRenderDeviceD3D11, FixedBlockMemoryAllocator> TShaderBase; + typedef ShaderBase<IShaderD3D11, IRenderDeviceD3D11> TShaderBase; - ShaderD3D11Impl(FixedBlockMemoryAllocator &ShaderObjAllocator, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const ShaderCreationAttribs &CreationAttribs); + ShaderD3D11Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const ShaderCreationAttribs &CreationAttribs); ~ShaderD3D11Impl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h index 3028b158..a48ee3dd 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.h @@ -38,11 +38,11 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::IShaderResourceBindingD3D11 interface -class ShaderResourceBindingD3D11Impl : public ShaderResourceBindingBase<IShaderResourceBindingD3D11, FixedBlockMemoryAllocator> +class ShaderResourceBindingD3D11Impl : public ShaderResourceBindingBase<IShaderResourceBindingD3D11> { public: - typedef ShaderResourceBindingBase<IShaderResourceBindingD3D11, FixedBlockMemoryAllocator> TBase; - ShaderResourceBindingD3D11Impl(FixedBlockMemoryAllocator &SRBAllocator, class PipelineStateD3D11Impl *pPSO, bool IsInternal); + typedef ShaderResourceBindingBase<IShaderResourceBindingD3D11> TBase; + ShaderResourceBindingD3D11Impl(IReferenceCounters *pRefCounters, class PipelineStateD3D11Impl *pPSO, bool IsInternal); ~ShaderResourceBindingD3D11Impl(); virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override final; diff --git a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h index e0a08683..8f1a2074 100644 --- a/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/SwapChainD3D11Impl.h @@ -34,11 +34,11 @@ namespace Diligent class IMemoryAllocator; /// Implementation of the Diligent::ISwapChainD3D11 interface -class SwapChainD3D11Impl : public SwapChainBase<ISwapChainD3D11, IMemoryAllocator> +class SwapChainD3D11Impl : public SwapChainBase<ISwapChainD3D11> { public: - typedef SwapChainBase<ISwapChainD3D11, IMemoryAllocator> TSwapChainBase; - SwapChainD3D11Impl(IMemoryAllocator &Allocator, + typedef SwapChainBase<ISwapChainD3D11> TSwapChainBase; + SwapChainD3D11Impl(IReferenceCounters *pRefCounters, const SwapChainDesc& SwapChainDesc, class RenderDeviceD3D11Impl* pRenderDeviceD3D11, class DeviceContextD3D11Impl* pDeviceContextD3D11, @@ -50,10 +50,10 @@ public: virtual void Present()override final; virtual void Resize( Uint32 NewWidth, Uint32 NewHeight )override final; - virtual IDXGISwapChain *GetDXGISwapChain(){ return m_pSwapChain; } + virtual IDXGISwapChain *GetDXGISwapChain()override final{ return m_pSwapChain; } - ID3D11RenderTargetView* GetRTV(){ return m_pRenderTargetView; } - ID3D11DepthStencilView* GetDSV(){ return m_pDepthStencilView; } + virtual ID3D11RenderTargetView* GetRTV()override final{ return m_pRenderTargetView; } + virtual ID3D11DepthStencilView* GetDSV()override final{ return m_pDepthStencilView; } private: void CreateRTVandDSV(); diff --git a/Graphics/GraphicsEngineD3D11/include/Texture1D_D3D11.h b/Graphics/GraphicsEngineD3D11/include/Texture1D_D3D11.h index 614badb1..9aca480b 100644 --- a/Graphics/GraphicsEngineD3D11/include/Texture1D_D3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/Texture1D_D3D11.h @@ -35,11 +35,15 @@ namespace Diligent class Texture1D_D3D11 : public TextureBaseD3D11 { public: - Texture1D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, + Texture1D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData = TextureData()); + Texture1D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + class RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture1D *pd3d11Texture); ~Texture1D_D3D11(); protected: diff --git a/Graphics/GraphicsEngineD3D11/include/Texture2D_D3D11.h b/Graphics/GraphicsEngineD3D11/include/Texture2D_D3D11.h index 0e94198c..3d9435ef 100644 --- a/Graphics/GraphicsEngineD3D11/include/Texture2D_D3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/Texture2D_D3D11.h @@ -35,11 +35,15 @@ namespace Diligent class Texture2D_D3D11 : public TextureBaseD3D11 { public: - Texture2D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, + Texture2D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData = TextureData()); + Texture2D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + class RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture2D *pd3d11Texture); ~Texture2D_D3D11(); protected: diff --git a/Graphics/GraphicsEngineD3D11/include/Texture3D_D3D11.h b/Graphics/GraphicsEngineD3D11/include/Texture3D_D3D11.h index 223aed13..8512f93c 100644 --- a/Graphics/GraphicsEngineD3D11/include/Texture3D_D3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/Texture3D_D3D11.h @@ -35,11 +35,15 @@ namespace Diligent class Texture3D_D3D11 : public TextureBaseD3D11 { public: - Texture3D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, + Texture3D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData = TextureData()); + Texture3D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + class RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture3D *pd3d11Texture); ~Texture3D_D3D11(); protected: diff --git a/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h b/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h index 4312ad14..4c1e42c0 100644 --- a/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/TextureBaseD3D11.h @@ -47,12 +47,12 @@ enum class D3D11TextureState }; /// Base implementation of the Diligent::ITextureD3D11 interface -class TextureBaseD3D11 : public TextureBase<ITextureD3D11, TextureViewD3D11Impl, FixedBlockMemoryAllocator, FixedBlockMemoryAllocator> +class TextureBaseD3D11 : public TextureBase<ITextureD3D11, TextureViewD3D11Impl, FixedBlockMemoryAllocator> { public: - typedef TextureBase<ITextureD3D11, TextureViewD3D11Impl, FixedBlockMemoryAllocator, FixedBlockMemoryAllocator> TTextureBase; + typedef TextureBase<ITextureD3D11, TextureViewD3D11Impl, FixedBlockMemoryAllocator> TTextureBase; - TextureBaseD3D11(FixedBlockMemoryAllocator &TexObjAllocator, + TextureBaseD3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, class RenderDeviceD3D11Impl *pDeviceD3D11, const TextureDesc& TexDesc, @@ -64,11 +64,13 @@ public: virtual void UpdateData( IDeviceContext *pContext, Uint32 MipLevel, Uint32 Slice, const Box &DstBox, const TextureSubResData &SubresData )override final; //virtual void CopyData(CTexture *pSrcTexture, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size); - virtual void Map( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData )override final; - virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType )override final; + virtual void Map( IDeviceContext *pContext, Uint32 Subresource, MAP_TYPE MapType, Uint32 MapFlags, MappedTextureSubresource &MappedData )override final; + virtual void Unmap( IDeviceContext *pContext, Uint32 Subresource, MAP_TYPE MapType, Uint32 MapFlags )override final; virtual ID3D11Resource* GetD3D11Texture()override final{ return m_pd3d11Texture; } + virtual void* GetNativeHandle()override final { return GetD3D11Texture(); } + void CopyData(IDeviceContext *pContext, ITexture *pSrcTexture, Uint32 SrcMipLevel, diff --git a/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h index caef1190..ce46bbbc 100644 --- a/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/TextureViewD3D11Impl.h @@ -36,12 +36,12 @@ namespace Diligent class FixedBlockMemoryAllocator; /// Implementation of the Diligent::ITextureViewD3D11 interface -class TextureViewD3D11Impl : public TextureViewBase<ITextureViewD3D11, FixedBlockMemoryAllocator> +class TextureViewD3D11Impl : public TextureViewBase<ITextureViewD3D11> { public: - typedef TextureViewBase<ITextureViewD3D11, FixedBlockMemoryAllocator> TTextureViewBase; + typedef TextureViewBase<ITextureViewD3D11> TTextureViewBase; - TextureViewD3D11Impl( FixedBlockMemoryAllocator &TexViewAllocator, + TextureViewD3D11Impl( IReferenceCounters *pRefCounters, IRenderDevice *pDevice, const TextureViewDesc& ViewDesc, class ITexture *pTexture, diff --git a/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h b/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h index d5c466b6..fe25f89a 100644 --- a/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h +++ b/Graphics/GraphicsEngineD3D11/interface/EngineD3D11Attribs.h @@ -27,6 +27,7 @@ /// Definition of the Engine D3D11 attribs #include "BasicTypes.h" +#include "GraphicsTypes.h" namespace Diligent { diff --git a/Graphics/GraphicsEngineD3D11/interface/PipelineStateD3D11.h b/Graphics/GraphicsEngineD3D11/interface/PipelineStateD3D11.h index a909d72d..d21bea39 100644 --- a/Graphics/GraphicsEngineD3D11/interface/PipelineStateD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/PipelineStateD3D11.h @@ -72,7 +72,7 @@ public: /// so Release() must not be called. virtual ID3D11VertexShader* GetD3D11VertexShader() = 0; - /// Returns a pointer to the interface of the internal pixel shader object. + /// Returns a pointer to the ID3D11PixelShader interface of the internal pixel shader object. /// The method does *NOT* call AddRef() on the returned interface, /// so Release() must not be called. diff --git a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceD3D11.h b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceD3D11.h index 82d15c16..62f56d30 100644 --- a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceD3D11.h @@ -45,6 +45,47 @@ public: /// The method does *NOT* call AddRef() on the returned interface, /// so Release() must not be called. virtual ID3D11Device* GetD3D11Device() = 0; + + /// Creates a buffer object from native d3d11 buffer + + /// \param [in] pd3d11Buffer - Pointer to the native buffer + /// \param [in] BuffDesc - Buffer description. Most of the fields will be + /// populated automatically if left in default values. + /// The only field that must be populated is BufferDesc::Format, + /// when initializing a formatted buffer. + /// \param [out] ppBuffer - Address of the memory location where the pointer to the + /// buffer interface will be stored. + /// The function calls AddRef(), so that the new object will contain + /// one reference. + virtual void CreateBufferFromD3DResource(ID3D11Buffer *pd3d11Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer) = 0; + + /// Creates a texture object from native d3d11 1D texture + + /// \param [in] pd3d11Texture - pointer to the native 1D texture + /// \param [out] ppTexture - Address of the memory location where the pointer to the + /// texture interface will be stored. + /// The function calls AddRef(), so that the new object will contain + /// one reference. + virtual void CreateTextureFromD3DResource(ID3D11Texture1D *pd3d11Texture, ITexture **ppTexture) = 0; + + /// Creates a texture object from native d3d11 2D texture + + /// \param [in] pd3d11Texture - pointer to the native 2D texture + /// \param [out] ppTexture - Address of the memory location where the pointer to the + /// texture interface will be stored. + /// The function calls AddRef(), so that the new object will contain + /// one reference. + virtual void CreateTextureFromD3DResource(ID3D11Texture2D *pd3d11Texture, ITexture **ppTexture) = 0; + + /// Creates a texture object from native d3d11 3D texture + + /// \param [in] pd3d11Texture - pointer to the native 3D texture + /// \param [out] ppTexture - Address of the memory location where the pointer to the + /// texture interface will be stored. + /// The function calls AddRef(), so that the new object will contain + /// one reference. + virtual void CreateTextureFromD3DResource(ID3D11Texture3D *pd3d11Texture, ITexture **ppTexture) = 0; + }; } diff --git a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h index aadef684..0f0a4e28 100644 --- a/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/RenderDeviceFactoryD3D11.h @@ -28,28 +28,46 @@ #include "Errors.h" #include "EngineD3D11Attribs.h" +#include "RenderDevice.h" +#include "DeviceContext.h" +#include "SwapChain.h" + +namespace Diligent +{ + +class IEngineFactoryD3D11 +{ +public: + virtual void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, + IRenderDevice **ppDevice, + IDeviceContext **ppContexts, + Uint32 NumDeferredContexts ) = 0; + + virtual void CreateSwapChainD3D11( IRenderDevice *pDevice, + IDeviceContext *pImmediateContext, + const SwapChainDesc& SCDesc, + void* pNativeWndHandle, + ISwapChain **ppSwapChain ) = 0; + + virtual void AttachToD3D11Device(void *pd3d11NativeDevice, + void *pd3d11ImmediateContext, + const EngineD3D11Attribs& EngineAttribs, + IRenderDevice **ppDevice, + IDeviceContext **ppContexts, + Uint32 NumDeferredContexts) = 0; +}; + +} extern "C" { #ifdef ENGINE_DLL - typedef void (*CreateDeviceAndContextsD3D11Type)( - const Diligent::EngineD3D11Attribs& EngineAttribs, - Diligent::IRenderDevice **ppDevice, - Diligent::IDeviceContext **ppContexts, - Diligent::Uint32 NumDeferredContexts ); + typedef Diligent::IEngineFactoryD3D11* (*GetEngineFactoryD3D11Type)(); - typedef void (*CreateSwapChainD3D11Type)( - Diligent::IRenderDevice *pDevice, - Diligent::IDeviceContext *pImmediateContext, - const Diligent::SwapChainDesc& SCDesc, - void* pNativeWndHandle, - Diligent::ISwapChain **ppSwapChain ); - - static void LoadGraphicsEngineD3D11(CreateDeviceAndContextsD3D11Type &CreateDeviceFunc, CreateSwapChainD3D11Type &CreateSwapChainFunc) + static bool LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) { - CreateDeviceFunc = nullptr; - CreateSwapChainFunc = nullptr; + GetFactoryFunc = nullptr; std::string LibName = "GraphicsEngineD3D11_"; #if _WIN64 @@ -69,35 +87,22 @@ extern "C" if( hModule == NULL ) { LOG_ERROR_MESSAGE( "Failed to load ", LibName, " library." ); - return; + return false; } - CreateDeviceFunc = reinterpret_cast<CreateDeviceAndContextsD3D11Type>( GetProcAddress(hModule, "CreateDeviceAndContextsD3D11") ); - if( CreateDeviceFunc == NULL ) + GetFactoryFunc = reinterpret_cast<GetEngineFactoryD3D11Type>( GetProcAddress(hModule, "GetEngineFactoryD3D11") ); + if( GetFactoryFunc == NULL ) { - LOG_ERROR_MESSAGE( "Failed to load CreateDeviceAndContextsD3D11() from ", LibName, " library." ); + LOG_ERROR_MESSAGE( "Failed to load GetEngineFactoryD3D11() from ", LibName, " library." ); FreeLibrary( hModule ); - return; + return false; } - CreateSwapChainFunc = reinterpret_cast<CreateSwapChainD3D11Type>( GetProcAddress(hModule, "CreateSwapChainD3D11") ); - if( CreateSwapChainFunc == NULL ) - { - LOG_ERROR_MESSAGE( "Failed to load CreateSwapChainD3D11() from ", LibName, " library." ); - FreeLibrary( hModule ); - return; - } + return true; } #else - void CreateDeviceAndContextsD3D11( const Diligent::EngineD3D11Attribs& EngineAttribs, - Diligent::IRenderDevice **ppDevice, - Diligent::IDeviceContext **ppContexts, - Diligent::Uint32 NumDeferredContexts ); - - void CreateSwapChainD3D11( Diligent::IRenderDevice *pDevice, - Diligent::IDeviceContext *pImmediateContext, - const Diligent::SwapChainDesc& SCDesc, - void* pNativeWndHandle, - Diligent::ISwapChain **ppSwapChain ); + + Diligent::IEngineFactoryD3D11* GetEngineFactoryD3D11(); + #endif }
\ No newline at end of file diff --git a/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h b/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h index 616da4a7..c516e8ab 100644 --- a/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/SwapChainD3D11.h @@ -36,7 +36,7 @@ static const Diligent::INTERFACE_ID IID_SwapChainD3D11 = { 0x4daf2e76, 0x9204, 0x4dc4, { 0xa5, 0x3a, 0xb0, 0x0, 0x97, 0x41, 0x2d, 0x3a } }; /// Interface to the swap chain object implemented in D3D11 -class ISwapChainD3D11 : public Diligent::ISwapChain +class ISwapChainD3D11 : public ISwapChain { public: @@ -45,6 +45,18 @@ public: /// The method does *NOT* call AddRef() on the returned interface, /// so Release() must not be called. virtual IDXGISwapChain *GetDXGISwapChain() = 0; + + /// Returns d3d11 render target view of the swap chain's back buffer + + /// The method does *NOT* call AddRef() on the returned interface, + /// so Release() must not be called. + virtual ID3D11RenderTargetView *GetRTV() = 0; + + /// Returns d3d11 depth stencil view of the internal depth buffer object + + /// The method does *NOT* call AddRef() on the returned interface, + /// so Release() must not be called. + virtual ID3D11DepthStencilView *GetDSV() = 0; }; } diff --git a/Graphics/GraphicsEngineD3D11/readme.md b/Graphics/GraphicsEngineD3D11/readme.md new file mode 100644 index 00000000..016d3d09 --- /dev/null +++ b/Graphics/GraphicsEngineD3D11/readme.md @@ -0,0 +1,128 @@ + +# GraphicsEngineD3D11 + +Implementation of Diligent Engine API using Direct3D11 + +# Initialization + +The following code snippet shows how to initialize diligent engine in D3D11 mode. + +```cpp +#include "RenderDeviceFactoryD3D11.h" +using namespace Diligent; + +// ... + +EngineD3D11Attribs DeviceAttribs; +DeviceAttribs.DebugFlags = + (Uint32)EngineD3D11DebugFlags::VerifyCommittedShaderResources | + (Uint32)EngineD3D11DebugFlags::VerifyCommittedResourceRelevance; + +// Get pointer to the function that returns the factory +#ifdef ENGINE_DLL + GetEngineFactoryD3D11Type GetEngineFactoryD3D11 = nullptr; + // Load the dll and import GetEngineFactoryD3D11() function + LoadGraphicsEngineD3D11(GetEngineFactoryD3D11); +#endif +auto *pFactoryD3D11 = GetEngineFactoryD3D11(); + +RefCntAutoPtr<IRenderDevice> pRenderDevice; +RefCntAutoPtr<IDeviceContext> pImmediateContext; +SwapChainDesc SwapChainDesc; +RefCntAutoPtr<ISwapChain> pSwapChain; +pFactoryD3D11->CreateDeviceAndContextsD3D11( DeviceAttribs, &pRenderDevice, &pImmediateContext, 0 ); +pFactoryD3D11->CreateSwapChainD3D11( pRenderDevice, pImmediateContext, SwapChainDesc, hWnd, &pSwapChain ); +``` + +Alternatively, the engine can be initialized by attaching to existing D3D11 device and immediate context (see below). + +# Interoperability with Direct3D11 + +Diligent Engine exposes methods to access internal D3D11 objects, is able to create diligent engine buffers +and textures from existing Direct3D11 buffers and textures, and can be initialized by attaching to existing D3D11 +device and immediate context. + +## Accessing Native D3D11 objects + +Below are some of the methods that provide access to internal D3D11 objects: + +| Function | Description | +|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------| +| `ID3D11Buffer *IBufferD3D11::GetD3D11Buffer()` | returns a pointer to the ID3D11Buffer interface of the internal Direct3D11 buffer object | +| `ID3D11Resource* ITextureD3D11::GetD3D11Texture()` | returns a pointer to the ID3D11Resource interface of the internal Direct3D11 texture object | +| `ID3D11View* IBufferViewD3D11()::GetD3D11View()` | returns a pointer to the ID3D11View interface of the internal d3d11 object representing the buffer view | +| `ID3D11View* ITextureViewD3D11::GetD3D11View()` | returns a pointer to the ID3D11View interface of the internal d3d11 object representing the texture view | +| `ID3D11Device* IRenderDeviceD3D11::GetD3D11Device()` | returns a pointer to the native D3D11 device object | +| `ID3D11DeviceContext* IDeviceContextD3D11::GetD3D11DeviceContext()` | returns a pointer to the native ID3D11DeviceContext object | + +## Creating Diligent Engine Objects from D3D11 Resources + +* `void IRenderDeviceD3D11::CreateBufferFromD3DResource(ID3D11Buffer *pd3d11Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer)` - + creates a diligent engine buffer object from the native d3d11 buffer +* `void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture1D *pd3d11Texture, ITexture **ppTexture)` - + create a diligent engine texture object from the native D3D11 1D texture +* `void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture2D *pd3d11Texture, ITexture **ppTexture)` - + create a diligent engine texture object from the native D3D11 2D texture +* `void IRenderDeviceD3D11::CreateTextureFromD3DResource(ID3D11Texture3D *pd3d11Texture, ITexture **ppTexture)` - + create a diligent engine texture object from the native D3D11 3D texture + +## Initializing the Engine by Attaching to Existing D3D11 Device and Immediate Context + +The code snippet below shows how diligent engine can be attached to D3D11 device returned by Unity + +```cpp +IUnityGraphicsD3D11* d3d = interfaces->Get<IUnityGraphicsD3D11>(); +ID3D11Device* d3d11NativeDevice = d3d->GetDevice(); +CComPtr<ID3D11DeviceContext> d3d11ImmediateContext; +d3d11NativeDevice->GetImmediateContext(&d3d11ImmediateContext); +auto *pFactoryD3d11 = GetEngineFactoryD3D11(); +EngineD3D11Attribs Attribs; +pFactoryD3d11->AttachToD3D11Device(d3d11NativeDevice, d3d11ImmediateContext, Attribs, &m_Device, &m_Context, 0); +``` + +For more information about interoperability with D3D11, please visit [Diligent Engine web site](http://diligentgraphics.com/diligent-engine/native-api-interoperability/direct3d11-interoperability/) + +# References + +[Diligent Engine](http://diligentgraphics.com/diligent-engine) + +[Interoperability with Direct3D11](http://diligentgraphics.com/diligent-engine/native-api-interoperability/direct3d11-interoperability/) + +[Architecture of D3D11-based implementation](http://diligentgraphics.com/diligent-engine/architecture/d3d11) + +# Release Notes + +## 2.1 + +### New features + +* Interoperability with Direct3D11 + - Accessing internal D3D11 objects and handles + - Createing diligent engine buffers/textures from D3D11 resources + - Attaching to existing D3D11 device +* Integraion with Unity +* Geometry shader support +* Tessellation support +* Support for structured buffers in HSLS shaders + +### API Changes + +* Updated map interface: removed MAP_WRITE_DISCARD and MAP_WRITE_NO_OVERWRITE map types and added MAP_FLAG_DISCARD and MAP_FLAG_DO_NOT_SYNCHRONIZE flags instead + +### Bug fixes + +* Fixed issue with unaligned uniform buffers + +## 2.0 + +Reworked the API to follow D3D12 style + +## 1.0 + +Initial release + + + +**Copyright 2015-2017 Egor Yusov** + +[Diligent Graphics](http://diligentgraphics.com)
\ No newline at end of file diff --git a/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp index 6c3f9cbd..45aa9c58 100644 --- a/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp @@ -35,41 +35,48 @@ namespace Diligent { -BufferD3D11Impl :: BufferD3D11Impl(FixedBlockMemoryAllocator &BufferObjMemAllocator, +BufferD3D11Impl :: BufferD3D11Impl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &BuffViewObjMemAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const BufferDesc& BuffDesc, const BufferData &BuffData /*= BufferData()*/) : - TBufferBase(BufferObjMemAllocator, BuffViewObjMemAllocator, pRenderDeviceD3D11, BuffDesc, false) + TBufferBase(pRefCounters, BuffViewObjMemAllocator, pRenderDeviceD3D11, BuffDesc, false) { -#define LOG_BUFFER_ERROR_AND_THROW(...) LOG_ERROR_AND_THROW("Buffer \"", BuffDesc.Name ? BuffDesc.Name : "", "\": ", ##__VA_ARGS__); +#define LOG_BUFFER_ERROR_AND_THROW(...) LOG_ERROR_AND_THROW("Buffer \"", m_Desc.Name ? m_Desc.Name : "", "\": ", ##__VA_ARGS__); - if( BuffDesc.Usage == USAGE_STATIC && BuffData.pData == nullptr ) + if( m_Desc.Usage == USAGE_STATIC && BuffData.pData == nullptr ) LOG_BUFFER_ERROR_AND_THROW("Static buffer must be initialized with data at creation time"); + + if (m_Desc.BindFlags & BIND_UNIFORM_BUFFER) + { + Uint32 AlignmentMask = 15; + m_Desc.uiSizeInBytes = (m_Desc.uiSizeInBytes + AlignmentMask) & (~AlignmentMask); + } + D3D11_BUFFER_DESC D3D11BuffDesc; - D3D11BuffDesc.BindFlags = BindFlagsToD3D11BindFlags(BuffDesc.BindFlags); - D3D11BuffDesc.ByteWidth = BuffDesc.uiSizeInBytes; + D3D11BuffDesc.BindFlags = BindFlagsToD3D11BindFlags(m_Desc.BindFlags); + D3D11BuffDesc.ByteWidth = m_Desc.uiSizeInBytes; D3D11BuffDesc.MiscFlags = 0; - if( BuffDesc.BindFlags & BIND_INDIRECT_DRAW_ARGS ) + if( m_Desc.BindFlags & BIND_INDIRECT_DRAW_ARGS ) { D3D11BuffDesc.MiscFlags |= D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS; } - D3D11BuffDesc.Usage = UsageToD3D11Usage(BuffDesc.Usage); + D3D11BuffDesc.Usage = UsageToD3D11Usage(m_Desc.Usage); D3D11BuffDesc.StructureByteStride = 0; - if( (BuffDesc.BindFlags & BIND_UNORDERED_ACCESS) ) + if( (m_Desc.BindFlags & BIND_UNORDERED_ACCESS) || (m_Desc.BindFlags & BIND_SHADER_RESOURCE) ) { - if( BuffDesc.Mode == BUFFER_MODE_STRUCTURED ) + if( m_Desc.Mode == BUFFER_MODE_STRUCTURED ) { D3D11BuffDesc.MiscFlags |= D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; - D3D11BuffDesc.StructureByteStride = BuffDesc.ElementByteStride; + D3D11BuffDesc.StructureByteStride = m_Desc.ElementByteStride; } - else if( BuffDesc.Mode == BUFFER_MODE_FORMATED ) + else if( m_Desc.Mode == BUFFER_MODE_FORMATTED ) { - auto ElementStride = GetValueSize( BuffDesc.Format.ValueType ) * BuffDesc.Format.NumComponents; + auto ElementStride = GetValueSize( m_Desc.Format.ValueType ) * m_Desc.Format.NumComponents; VERIFY( m_Desc.ElementByteStride == 0 || m_Desc.ElementByteStride == ElementStride, "Element byte stride does not match buffer format" ); m_Desc.ElementByteStride = ElementStride; - if( BuffDesc.Format.ValueType == VT_FLOAT32 || BuffDesc.Format.ValueType == VT_FLOAT16 ) + if( m_Desc.Format.ValueType == VT_FLOAT32 || m_Desc.Format.ValueType == VT_FLOAT16 ) m_Desc.Format.IsNormalized = false; } else @@ -78,7 +85,7 @@ BufferD3D11Impl :: BufferD3D11Impl(FixedBlockMemoryAllocator &BufferObjMemAlloca } } - D3D11BuffDesc.CPUAccessFlags = CPUAccessFlagsToD3D11CPUAccessFlags( BuffDesc.CPUAccessFlags ); + D3D11BuffDesc.CPUAccessFlags = CPUAccessFlagsToD3D11CPUAccessFlags( m_Desc.CPUAccessFlags ); D3D11_SUBRESOURCE_DATA InitData; InitData.pSysMem = BuffData.pData; @@ -90,6 +97,58 @@ BufferD3D11Impl :: BufferD3D11Impl(FixedBlockMemoryAllocator &BufferObjMemAlloca "Failed to create the Direct3D11 buffer" ); } +static BufferDesc BuffDescFromD3D11Buffer(ID3D11Buffer *pd3d11Buffer, BufferDesc BuffDesc) +{ + D3D11_BUFFER_DESC D3D11BuffDesc; + pd3d11Buffer->GetDesc(&D3D11BuffDesc); + + VERIFY(BuffDesc.uiSizeInBytes == 0 || BuffDesc.uiSizeInBytes == D3D11BuffDesc.ByteWidth, "Buffer size specified by the BufferDesc (",BuffDesc.uiSizeInBytes,") does not match d3d11 buffer size (", D3D11BuffDesc.ByteWidth, ")" ) + BuffDesc.uiSizeInBytes = Uint32{ D3D11BuffDesc.ByteWidth }; + + auto BindFlags = D3D11BindFlagsToBindFlags(D3D11BuffDesc.BindFlags); + if (D3D11BuffDesc.MiscFlags & D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS) + BindFlags |= BIND_INDIRECT_DRAW_ARGS; + VERIFY(BuffDesc.BindFlags == 0 || BuffDesc.BindFlags == BindFlags, "Bind flags specified by the BufferDesc (", BuffDesc.BindFlags,") do not match bind flags recovered from d3d11 buffer desc (", BindFlags, ")" ) + BuffDesc.BindFlags = BindFlags; + + auto Usage = D3D11UsageToUsage(D3D11BuffDesc.Usage); + VERIFY(BuffDesc.Usage == 0 || BuffDesc.Usage == Usage, "Usage specified by the BufferDesc (", BuffDesc.Usage,") do not match buffer usage recovered from d3d11 buffer desc (", Usage, ")" ) + BuffDesc.Usage = Usage; + + auto CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11BuffDesc.CPUAccessFlags); + VERIFY(BuffDesc.CPUAccessFlags == 0 || BuffDesc.CPUAccessFlags == CPUAccessFlags, "CPU access flags specified by the BufferDesc (", BuffDesc.CPUAccessFlags, ") do not match CPU access flags recovered from d3d11 buffer desc (", CPUAccessFlags, ")") + BuffDesc.CPUAccessFlags = CPUAccessFlags; + + if( (BuffDesc.BindFlags & BIND_UNORDERED_ACCESS) || (BuffDesc.BindFlags & BIND_SHADER_RESOURCE) ) + { + if(D3D11BuffDesc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED) + { + VERIFY(BuffDesc.Mode == BUFFER_MODE_UNDEFINED || BuffDesc.Mode == BUFFER_MODE_STRUCTURED, "Unexpected buffer mode") + BuffDesc.Mode = BUFFER_MODE_STRUCTURED; + VERIFY(BuffDesc.ElementByteStride == 0 || BuffDesc.ElementByteStride == D3D11BuffDesc.StructureByteStride, "Element byte stride specified by the BufferDesc (", BuffDesc.ElementByteStride, ") does not match structured byte stride recovered from d3d11 buffer desc (", D3D11BuffDesc.StructureByteStride, ")") + BuffDesc.ElementByteStride = Uint32{ D3D11BuffDesc.StructureByteStride }; + } + else + { + VERIFY(BuffDesc.Mode == BUFFER_MODE_UNDEFINED || BuffDesc.Mode == BUFFER_MODE_FORMATTED, "Unexpected buffer mode") + BuffDesc.Mode = BUFFER_MODE_FORMATTED; + VERIFY( BuffDesc.Format.ValueType != VT_UNDEFINED, "Value type is not specified for a formatted buffer" ); + VERIFY( BuffDesc.Format.NumComponents != 0, "Num components cannot be zero in a formated buffer" ); + } + } + + return BuffDesc; +} +BufferD3D11Impl :: BufferD3D11Impl(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &BuffViewObjMemAllocator, + class RenderDeviceD3D11Impl *pDeviceD3D11, + const BufferDesc& BuffDesc, + ID3D11Buffer *pd3d11Buffer) : + TBufferBase(pRefCounters, BuffViewObjMemAllocator, pDeviceD3D11, BuffDescFromD3D11Buffer(pd3d11Buffer, BuffDesc), false) +{ + m_pd3d11Buffer = pd3d11Buffer; +} + BufferD3D11Impl :: ~BufferD3D11Impl() { } @@ -135,8 +194,9 @@ void BufferD3D11Impl :: Map(IDeviceContext *pContext, MAP_TYPE MapType, Uint32 M TBufferBase::Map( pContext, MapType, MapFlags, pMappedData ); auto *pd3d11DeviceContext = static_cast<DeviceContextD3D11Impl*>(pContext)->GetD3D11DeviceContext(); - auto d3d11MapType = MapTypeToD3D11MapType(MapType); - auto d3d11MapFlags = MapFlagsToD3D11MapFlags(MapFlags); + D3D11_MAP d3d11MapType = static_cast<D3D11_MAP>(0); + UINT d3d11MapFlags = 0; + MapParamsToD3D11MapParams(MapType, MapFlags, d3d11MapType, d3d11MapFlags); D3D11_MAPPED_SUBRESOURCE MappedBuff; HRESULT hr = pd3d11DeviceContext->Map(m_pd3d11Buffer, 0, d3d11MapType, d3d11MapFlags, &MappedBuff); @@ -146,9 +206,9 @@ void BufferD3D11Impl :: Map(IDeviceContext *pContext, MAP_TYPE MapType, Uint32 M VERIFY( pMappedData || (MapFlags & MAP_FLAG_DO_NOT_WAIT) && (hr == DXGI_ERROR_WAS_STILL_DRAWING), "Map failed" ); } -void BufferD3D11Impl::Unmap( IDeviceContext *pContext, MAP_TYPE MapType ) +void BufferD3D11Impl::Unmap( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags ) { - TBufferBase::Unmap( pContext, MapType ); + TBufferBase::Unmap( pContext, MapType, MapFlags ); auto *pd3d11DeviceContext = static_cast<DeviceContextD3D11Impl*>(pContext)->GetD3D11DeviceContext(); pd3d11DeviceContext->Unmap(m_pd3d11Buffer, 0); @@ -173,13 +233,15 @@ void BufferD3D11Impl::CreateViewInternal( const BufferViewDesc &OrigViewDesc, IB { CComPtr<ID3D11UnorderedAccessView> pUAV; CreateUAV( ViewDesc, &pUAV ); - *ppView = NEW(BuffViewAllocator, "BufferViewD3D11Impl instance", BufferViewD3D11Impl, pDeviceD3D11Impl, ViewDesc, this, pUAV, bIsDefaultView ); + *ppView = NEW_RC_OBJ(BuffViewAllocator, "BufferViewD3D11Impl instance", BufferViewD3D11Impl, bIsDefaultView ? this : nullptr) + ( pDeviceD3D11Impl, ViewDesc, this, pUAV, bIsDefaultView ); } else if( ViewDesc.ViewType == BUFFER_VIEW_SHADER_RESOURCE ) { CComPtr<ID3D11ShaderResourceView> pSRV; CreateSRV( ViewDesc, &pSRV ); - *ppView = NEW(BuffViewAllocator, "BufferViewD3D11Impl instance", BufferViewD3D11Impl, pDeviceD3D11Impl, ViewDesc, this, pSRV, bIsDefaultView ); + *ppView = NEW_RC_OBJ(BuffViewAllocator, "BufferViewD3D11Impl instance", BufferViewD3D11Impl, bIsDefaultView ? this : nullptr) + (pDeviceD3D11Impl, ViewDesc, this, pSRV, bIsDefaultView ); } if( !bIsDefaultView && *ppView ) diff --git a/Graphics/GraphicsEngineD3D11/src/BufferViewD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/BufferViewD3D11Impl.cpp index dd8f53d4..f1031de2 100644 --- a/Graphics/GraphicsEngineD3D11/src/BufferViewD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/BufferViewD3D11Impl.cpp @@ -27,13 +27,13 @@ namespace Diligent { -BufferViewD3D11Impl::BufferViewD3D11Impl( FixedBlockMemoryAllocator &BuffViewObjAllocator, +BufferViewD3D11Impl::BufferViewD3D11Impl( IReferenceCounters *pRefCounters, IRenderDevice *pDevice, const BufferViewDesc& ViewDesc, IBuffer *pBuffer, ID3D11View* pD3D11View, bool bIsDefaultView ) : - TBufferViewBase( BuffViewObjAllocator, pDevice, ViewDesc, pBuffer, bIsDefaultView ), + TBufferViewBase( pRefCounters, pDevice, ViewDesc, pBuffer, bIsDefaultView ), m_pD3D11View( pD3D11View ) { } diff --git a/Graphics/GraphicsEngineD3D11/src/CommandListD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/CommandListD3D11Impl.cpp index 3ed841e9..ecd27fdd 100644 --- a/Graphics/GraphicsEngineD3D11/src/CommandListD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/CommandListD3D11Impl.cpp @@ -30,10 +30,10 @@ namespace Diligent { -CommandListD3D11Impl :: CommandListD3D11Impl(FixedBlockMemoryAllocator &CommandListObjMemAllocator, +CommandListD3D11Impl :: CommandListD3D11Impl(IReferenceCounters *pRefCounters, IRenderDevice *pDevice, ID3D11CommandList *pd3d11CommandList) : - TCommandListBase(CommandListObjMemAllocator, pDevice), + TCommandListBase(pRefCounters, pDevice), m_pd3d11CommandList(pd3d11CommandList) { } diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index a270e3d2..a64f943d 100644 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -32,7 +32,7 @@ #include "D3D11TypeConversions.h" #include "TextureViewD3D11Impl.h" #include "PipelineStateD3D11Impl.h" -#include "SwapChainD3D11Impl.h" +#include "SwapChainD3D11.h" #include "D3D11DebugUtilities.h" #include "ShaderResourceBindingD3D11Impl.h" #include "EngineD3D11Attribs.h" @@ -43,8 +43,8 @@ using namespace Diligent; namespace Diligent { - DeviceContextD3D11Impl::DeviceContextD3D11Impl( IMemoryAllocator &Allocator, IRenderDevice *pDevice, ID3D11DeviceContext *pd3d11DeviceContext, const struct EngineD3D11Attribs &EngineAttribs, bool bIsDeferred ) : - TDeviceContextBase(Allocator, pDevice, bIsDeferred), + DeviceContextD3D11Impl::DeviceContextD3D11Impl( IReferenceCounters *pRefCounters, IMemoryAllocator &Allocator, IRenderDevice *pDevice, ID3D11DeviceContext *pd3d11DeviceContext, const struct EngineD3D11Attribs &EngineAttribs, bool bIsDeferred ) : + TDeviceContextBase(pRefCounters, pDevice, bIsDeferred), m_pd3d11DeviceContext( pd3d11DeviceContext ), m_CommittedIBFormat(VT_UNDEFINED), m_CommittedD3D11IndexDataStartOffset(0), @@ -87,9 +87,9 @@ namespace Diligent #define COMMIT_SHADER(SN, ShaderName)\ { \ auto *pd3d11Shader = pPipelineStateD3D11->GetD3D11##ShaderName(); \ - if (m_CommittedD3DShaders[SN##Ind] != pd3d11Shader) \ + if (m_CommittedD3DShaders[SN##Ind] != pd3d11Shader) \ { \ - m_CommittedD3DShaders[SN##Ind] = pd3d11Shader; \ + m_CommittedD3DShaders[SN##Ind] = pd3d11Shader; \ m_pd3d11DeviceContext->SN##SetShader(pd3d11Shader, nullptr, 0); \ } \ } @@ -109,7 +109,14 @@ namespace Diligent m_pd3d11DeviceContext->RSSetState( pPipelineStateD3D11->GetD3D11RasterizerState() ); m_pd3d11DeviceContext->OMSetDepthStencilState( pPipelineStateD3D11->GetD3D11DepthStencilState(), m_StencilRef ); - m_pd3d11DeviceContext->IASetInputLayout(pPipelineStateD3D11->GetD3D11InputLayout()); + auto *pd3d11InputLayout = pPipelineStateD3D11->GetD3D11InputLayout(); + // It is safe to perform raw pointer comparison as the device context + // keeps bound input layout alive + if( m_CommittedD3D11InputLayout != pd3d11InputLayout ) + { + m_pd3d11DeviceContext->IASetInputLayout( pd3d11InputLayout ); + m_CommittedD3D11InputLayout = pd3d11InputLayout; + } } } @@ -670,14 +677,6 @@ namespace Diligent #endif auto *pd3d11InputLayout = pPipelineStateD3D11->GetD3D11InputLayout(); - // It is safe to perform raw pointer comparison as the device context - // keeps bound input layout alive - if( m_CommittedD3D11InputLayout != pd3d11InputLayout ) - { - m_pd3d11DeviceContext->IASetInputLayout( pd3d11InputLayout ); - m_CommittedD3D11InputLayout = pd3d11InputLayout; - } - if( pd3d11InputLayout != nullptr && !m_bCommittedD3D11VBsUpToDate ) { CommitD3D11VertexBuffers(pPipelineStateD3D11); @@ -714,7 +713,7 @@ namespace Diligent m_CommittedD3D11PrimTopology = TopologyToD3D11Topology( DrawAttribs.Topology ); m_pd3d11DeviceContext->IASetPrimitiveTopology( m_CommittedD3D11PrimTopology ); } - + if( DrawAttribs.IsIndirect ) { VERIFY( DrawAttribs.pIndirectDrawAttribs, "Indirect draw command attributes buffer is not set" ); @@ -791,7 +790,16 @@ namespace Diligent } else { - pd3d11DSV = ValidatedCast<SwapChainD3D11Impl>(m_pSwapChain.RawPtr())->GetDSV(); + if (m_pSwapChain) + { + pd3d11DSV = ValidatedCast<ISwapChainD3D11>(m_pSwapChain.RawPtr())->GetDSV(); + VERIFY_EXPR(pd3d11DSV != nullptr); + } + else + { + LOG_ERROR("Failed to clear default depth stencil buffer: swap chain is not initialized in the device context"); + return; + } } UINT32 d3d11ClearFlags = 0; if( ClearFlags & CLEAR_DEPTH_FLAG ) d3d11ClearFlags |= D3D11_CLEAR_DEPTH; @@ -815,7 +823,16 @@ namespace Diligent } else { - pd3d11RTV = ValidatedCast<SwapChainD3D11Impl>(m_pSwapChain.RawPtr())->GetRTV(); + if (m_pSwapChain) + { + pd3d11RTV = ValidatedCast<ISwapChainD3D11>(m_pSwapChain.RawPtr())->GetRTV(); + VERIFY_EXPR(pd3d11RTV != nullptr); + } + else + { + LOG_ERROR("Failed to clear default render target: swap chain is not initialized in the device context"); + return; + } } static const float Zero[4] = { 0.f, 0.f, 0.f, 0.f }; @@ -838,12 +855,6 @@ namespace Diligent m_bCommittedD3D11VBsUpToDate = false; } - void DeviceContextD3D11Impl::ClearState() - { - TDeviceContextBase::ClearState(); - - } - void DeviceContextD3D11Impl::SetIndexBuffer( IBuffer *pIndexBuffer, Uint32 ByteOffset ) { TDeviceContextBase::SetIndexBuffer( pIndexBuffer, ByteOffset ); @@ -897,23 +908,32 @@ namespace Diligent m_pd3d11DeviceContext->RSSetScissorRects( NumRects, d3d11ScissorRects ); } - void DeviceContextD3D11Impl::RebindRenderTargets() + void DeviceContextD3D11Impl::CommitRenderTargets() { const Uint32 MaxD3D11RTs = D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; Uint32 NumRenderTargets = m_NumBoundRenderTargets; VERIFY( NumRenderTargets <= MaxD3D11RTs, "D3D11 only allows 8 simultaneous render targets" ); NumRenderTargets = std::min( MaxD3D11RTs, NumRenderTargets ); - ID3D11RenderTargetView *pd3d11RTs[MaxD3D11RTs] = {}; + // Do not waste time setting RTVs to null + ID3D11RenderTargetView *pd3d11RTs[MaxD3D11RTs]; ID3D11DepthStencilView *pd3d11DSV = nullptr; - auto *pDepthStencil = m_pBoundDepthStencil.RawPtr(); - if( NumRenderTargets == 0 && pDepthStencil == nullptr ) + if( m_IsDefaultFramebufferBound ) { - NumRenderTargets = 1; - auto *pSwapChainD3D11 = ValidatedCast<SwapChainD3D11Impl>( m_pSwapChain.RawPtr() ); - pd3d11RTs[0] = pSwapChainD3D11->GetRTV(); - pd3d11DSV = pSwapChainD3D11->GetDSV(); + if (m_pSwapChain) + { + NumRenderTargets = 1; + auto *pSwapChainD3D11 = ValidatedCast<ISwapChainD3D11>(m_pSwapChain.RawPtr()); + pd3d11RTs[0] = pSwapChainD3D11->GetRTV(); + pd3d11DSV = pSwapChainD3D11->GetDSV(); + VERIFY_EXPR(pd3d11RTs[0] != nullptr && pd3d11DSV != nullptr); + } + else + { + LOG_ERROR("Failed to commit default render target and depth stencil: swap chain is not initialized in the device context"); + return; + } } else { @@ -929,6 +949,7 @@ namespace Diligent pd3d11RTs[rt] = nullptr; } + auto *pDepthStencil = m_pBoundDepthStencil.RawPtr(); if( pDepthStencil != nullptr ) { auto *pViewD3D11 = static_cast<TextureViewD3D11Impl*>(pDepthStencil); @@ -1102,7 +1123,7 @@ namespace Diligent VERIFY( pTexture, "Null resource provided" ) if( !pTexture )return; - bool bRebindRenderTargets = false; + bool bCommitRenderTargets = false; for( Uint32 rt = 0; rt < m_NumBoundRenderTargets; ++rt ) { if( auto pTexView = m_pBoundRenderTargets[rt] ) @@ -1110,13 +1131,13 @@ namespace Diligent if( pTexView->GetTexture() == pTexture ) { m_pBoundRenderTargets[rt].Release(); - bRebindRenderTargets = true; + bCommitRenderTargets = true; } } } - if( bRebindRenderTargets ) - RebindRenderTargets(); + if( bCommitRenderTargets ) + CommitRenderTargets(); pTexture->ClearState(D3D11TextureState::RenderTarget); } @@ -1129,7 +1150,7 @@ namespace Diligent if( m_pBoundDepthStencil && m_pBoundDepthStencil->GetTexture() == pTexD3D11 ) { m_pBoundDepthStencil.Release(); - RebindRenderTargets(); + CommitRenderTargets(); } pTexD3D11->ClearState(D3D11TextureState::DepthStencil); } @@ -1152,7 +1173,7 @@ namespace Diligent pTex->ResetState(D3D11TextureState::DepthStencil); } - RebindRenderTargets(); + CommitRenderTargets(); // Set the viewport to match the render target size SetViewports(1, nullptr, 0, 0); @@ -1178,10 +1199,10 @@ namespace Diligent } template<typename TD3D11ResourceType, typename TSetD3D11ResMethodType> - void ClearStateCacheInternal(TD3D11ResourceType CommittedD3D11Res[], - Uint8 &NumCommittedResources, - TSetD3D11ResMethodType SetD3D11ResMethod, - ID3D11DeviceContext *pDeviceCtx) + void ReleaseCommittedShaderResourcesHelper(TD3D11ResourceType CommittedD3D11Res[], + Uint8 NumCommittedResources, + TSetD3D11ResMethodType SetD3D11ResMethod, + ID3D11DeviceContext *pDeviceCtx) { if( NumCommittedResources > 0) { @@ -1190,14 +1211,14 @@ namespace Diligent } } - void DeviceContextD3D11Impl::ClearShaderStateCache() + void DeviceContextD3D11Impl::ReleaseCommittedShaderResources() { for( int ShaderType = 0; ShaderType < NumShaderTypes; ++ShaderType ) { - ClearStateCacheInternal( m_CommittedD3D11CBs [ShaderType], m_NumCommittedCBs [ShaderType], SetCBMethods[ShaderType], m_pd3d11DeviceContext ); - ClearStateCacheInternal( m_CommittedD3D11SRVs [ShaderType], m_NumCommittedSRVs [ShaderType], SetSRVMethods[ShaderType], m_pd3d11DeviceContext ); - ClearStateCacheInternal( m_CommittedD3D11Samplers[ShaderType], m_NumCommittedSamplers[ShaderType], SetSamplerMethods[ShaderType], m_pd3d11DeviceContext ); - ClearStateCacheInternal( m_CommittedD3D11UAVs [ShaderType], m_NumCommittedUAVs [ShaderType], SetUAVMethods[ShaderType], m_pd3d11DeviceContext ); + ReleaseCommittedShaderResourcesHelper( m_CommittedD3D11CBs [ShaderType], m_NumCommittedCBs [ShaderType], SetCBMethods[ShaderType], m_pd3d11DeviceContext); + ReleaseCommittedShaderResourcesHelper( m_CommittedD3D11SRVs [ShaderType], m_NumCommittedSRVs [ShaderType], SetSRVMethods[ShaderType], m_pd3d11DeviceContext); + ReleaseCommittedShaderResourcesHelper( m_CommittedD3D11Samplers[ShaderType], m_NumCommittedSamplers[ShaderType], SetSamplerMethods[ShaderType], m_pd3d11DeviceContext); + ReleaseCommittedShaderResourcesHelper( m_CommittedD3D11UAVs [ShaderType], m_NumCommittedUAVs [ShaderType], SetUAVMethods[ShaderType], m_pd3d11DeviceContext); memset(m_CommittedD3D11SRVResources[ShaderType], 0, sizeof(m_CommittedD3D11SRVResources[ShaderType][0])*m_NumCommittedSRVs[ShaderType] ); memset(m_CommittedD3D11UAVResources[ShaderType], 0, sizeof(m_CommittedD3D11UAVResources[ShaderType][0])*m_NumCommittedUAVs[ShaderType] ); m_NumCommittedCBs[ShaderType] = 0; @@ -1232,11 +1253,11 @@ namespace Diligent // ID3D11DeviceContext::ClearState() was called. &pd3d11CmdList); - CommandListD3D11Impl *pCmdListD3D11( NEW(m_CmdListAllocator, "CommandListD3D11Impl instance", CommandListD3D11Impl, m_pDevice, pd3d11CmdList) ); + CommandListD3D11Impl *pCmdListD3D11( NEW_RC_OBJ(m_CmdListAllocator, "CommandListD3D11Impl instance", CommandListD3D11Impl)(m_pDevice, pd3d11CmdList) ); pCmdListD3D11->QueryInterface( IID_CommandList, reinterpret_cast<IObject**>(ppCommandList) ); // Device context is now in default state - ClearStateCache(); + InvalidateState(); if(m_DebugFlags & (Uint32)EngineD3D11DebugFlags::VerifyCommittedResourceRelevance) { @@ -1271,7 +1292,7 @@ namespace Diligent ); // Device context is now in default state - ClearStateCache(); + InvalidateState(); if(m_DebugFlags & (Uint32)EngineD3D11DebugFlags::VerifyCommittedResourceRelevance) { @@ -1327,6 +1348,56 @@ namespace Diligent m_CommittedPrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED; } + void DeviceContextD3D11Impl::InvalidateState() + { + TDeviceContextBase::InvalidateState(); + + ReleaseCommittedShaderResources(); + for (int ShaderType = 0; ShaderType < NumShaderTypes; ++ShaderType) + m_CommittedD3DShaders[ShaderType].Release(); + m_pd3d11DeviceContext->VSSetShader(nullptr, nullptr, 0); + m_pd3d11DeviceContext->GSSetShader(nullptr, nullptr, 0); + m_pd3d11DeviceContext->PSSetShader(nullptr, nullptr, 0); + m_pd3d11DeviceContext->HSSetShader(nullptr, nullptr, 0); + m_pd3d11DeviceContext->DSSetShader(nullptr, nullptr, 0); + m_pd3d11DeviceContext->CSSetShader(nullptr, nullptr, 0); + ID3D11RenderTargetView *d3d11NullRTV[] = {nullptr}; + m_pd3d11DeviceContext->OMSetRenderTargets(1, d3d11NullRTV, nullptr); + + if (m_NumCommittedD3D11VBs > 0) + { + for (Uint32 vb = 0; vb < m_NumCommittedD3D11VBs; ++vb) + { + m_CommittedD3D11VertexBuffers[vb] = nullptr; + m_CommittedD3D11VBStrides[vb] = 0; + m_CommittedD3D11VBOffsets[vb] = 0; + } + m_pd3d11DeviceContext->IASetVertexBuffers(0, m_NumCommittedD3D11VBs, m_CommittedD3D11VertexBuffers, m_CommittedD3D11VBStrides, m_CommittedD3D11VBOffsets); + m_NumCommittedD3D11VBs = 0; + } + + m_bCommittedD3D11VBsUpToDate = false; + + if (m_CommittedD3D11InputLayout != nullptr) + { + m_pd3d11DeviceContext->IASetInputLayout(nullptr); + m_CommittedD3D11InputLayout = nullptr; + } + + if (m_CommittedD3D11IndexBuffer) + { + m_pd3d11DeviceContext->IASetIndexBuffer(nullptr, DXGI_FORMAT_R32_UINT, 0); + m_CommittedD3D11IndexBuffer.Release(); + } + + m_CommittedIBFormat = VT_UNDEFINED; + m_CommittedD3D11IndexDataStartOffset = 0; + m_bCommittedD3D11IBUpToDate = false; + + m_CommittedD3D11PrimTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; + m_CommittedPrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED; + } + #ifdef VERIFY_CONTEXT_BINDINGS void DeviceContextD3D11Impl::dbgVerifyRenderTargetFormats() { @@ -1339,11 +1410,19 @@ namespace Diligent TEXTURE_FORMAT BoundRTVFormats[8] = {TEX_FORMAT_UNKNOWN}; TEXTURE_FORMAT BoundDSVFormat = TEX_FORMAT_UNKNOWN; Uint32 NumBoundRTVs = 0; - if (m_NumBoundRenderTargets == 0 && m_pBoundDepthStencil == nullptr) + if (m_IsDefaultFramebufferBound) { - BoundRTVFormats[0] = m_pSwapChain->GetDesc().ColorBufferFormat; - BoundDSVFormat = m_pSwapChain->GetDesc().DepthBufferFormat; - NumBoundRTVs = 1; + if (m_pSwapChain) + { + BoundRTVFormats[0] = m_pSwapChain->GetDesc().ColorBufferFormat; + BoundDSVFormat = m_pSwapChain->GetDesc().DepthBufferFormat; + NumBoundRTVs = 1; + } + else + { + LOG_WARNING_MESSAGE("Failed to get bound render targets and depth-stencil buffer: swap chain is not initialized in the device context"); + return; + } } else { diff --git a/Graphics/GraphicsEngineD3D11/src/GraphicsEngineD3D11.def b/Graphics/GraphicsEngineD3D11/src/GraphicsEngineD3D11.def index 11ba3cd0..96b3626e 100644 --- a/Graphics/GraphicsEngineD3D11/src/GraphicsEngineD3D11.def +++ b/Graphics/GraphicsEngineD3D11/src/GraphicsEngineD3D11.def @@ -1,3 +1,2 @@ EXPORTS - CreateDeviceAndContextsD3D11 - CreateSwapChainD3D11
\ No newline at end of file + GetEngineFactoryD3D11
\ No newline at end of file diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 2084efe2..c5f6859b 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -30,25 +30,9 @@ namespace Diligent { -PipelineStateD3D11Impl::PipelineStateD3D11Impl(FixedBlockMemoryAllocator &PSOAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const PipelineStateDesc& PipelineDesc) : - TPipelineStateBase(PSOAllocator, pRenderDeviceD3D11, PipelineDesc), +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters *pRefCounters, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const PipelineStateDesc& PipelineDesc) : + TPipelineStateBase(pRefCounters, pRenderDeviceD3D11, PipelineDesc), m_pDefaultShaderResBinding( nullptr, STDDeleter<ShaderResourceBindingD3D11Impl, FixedBlockMemoryAllocator>(pRenderDeviceD3D11->GetSRBAllocator()) ), - m_ResourceCacheDataAllocators - { - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity} - }, - m_ShaderResLayoutDataAllocators - { - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity}, - {GetRawAllocator(), PipelineDesc.SRBAllocationGranularity} - }, m_DummyShaderVar(*this) { if (PipelineDesc.IsComputePipeline) @@ -62,7 +46,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(FixedBlockMemoryAllocator &PSOAll if (m_pCS && m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) { LOG_ERROR_AND_THROW( GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType)," provided" ); - } + } } else { @@ -120,13 +104,39 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(FixedBlockMemoryAllocator &PSOAll } } + if(PipelineDesc.SRBAllocationGranularity > 1) + m_Allocators.Init(m_NumShaders, PipelineDesc.SRBAllocationGranularity); + auto &SRBAllocator = pRenderDeviceD3D11->GetSRBAllocator(); - m_pDefaultShaderResBinding.reset( NEW(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl, this, true) ); + m_pDefaultShaderResBinding.reset( NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl, this)(this, true) ); } -PipelineStateD3D11Impl::~PipelineStateD3D11Impl() +void PipelineStateD3D11Impl::DataAllocators::Init(size_t NumActiveShaders, Uint32 SRBAllocationGranularity) +{ + VERIFY_EXPR(NumActiveShaders <= _countof(m_pShaderResLayoutDataAllocators) ); + // Since this is not constructor, there is no need to + // handle exceptions. If exception is thrown, ~DataAllocators() + // will destroy all allocators that have been initialized + for(size_t stage = 0; stage < NumActiveShaders; ++stage) + { + m_pShaderResLayoutDataAllocators[stage] = NEW_POOL_OBJECT(AdaptiveFixedBlockAllocator, "Shader resource layout data allocator", GetRawAllocator(), SRBAllocationGranularity); + m_pResourceCacheDataAllocators[stage] = NEW_POOL_OBJECT(AdaptiveFixedBlockAllocator, "Shader resource cache data allocator", GetRawAllocator(), SRBAllocationGranularity); + } +} + +PipelineStateD3D11Impl::DataAllocators::~DataAllocators() { + for(size_t i=0; i < _countof(m_pShaderResLayoutDataAllocators); ++i) + if(m_pShaderResLayoutDataAllocators[i] != nullptr) + DESTROY_POOL_OBJECT(m_pShaderResLayoutDataAllocators[i]); + for(size_t i=0; i < _countof(m_pResourceCacheDataAllocators); ++i) + if(m_pResourceCacheDataAllocators[i] != nullptr) + DESTROY_POOL_OBJECT(m_pResourceCacheDataAllocators[i]); +} + +PipelineStateD3D11Impl::~PipelineStateD3D11Impl() +{ } IMPLEMENT_QUERY_INTERFACE( PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase ) @@ -172,7 +182,7 @@ void PipelineStateD3D11Impl::CreateShaderResourceBinding(IShaderResourceBinding { auto *pRenderDeviceD3D11 = ValidatedCast<RenderDeviceD3D11Impl>( GetDevice() ); auto &SRBAllocator = pRenderDeviceD3D11->GetSRBAllocator(); - auto pShaderResBinding = NEW(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl, this, false); + auto pShaderResBinding = NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl)(this, false); pShaderResBinding->QueryInterface(IID_ShaderResourceBinding, reinterpret_cast<IObject**>(static_cast<IShaderResourceBinding**>(ppShaderResourceBinding))); } diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 2e533a89..5f8e9c9b 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -39,8 +39,8 @@ namespace Diligent { -RenderDeviceD3D11Impl :: RenderDeviceD3D11Impl(IMemoryAllocator &RawMemAllocator, const EngineD3D11Attribs& EngineAttribs, ID3D11Device *pd3d11Device, Uint32 NumDeferredContexts) : - TRenderDeviceBase(RawMemAllocator, NumDeferredContexts, sizeof(TextureBaseD3D11), sizeof(TextureViewD3D11Impl), sizeof(BufferD3D11Impl), sizeof(BufferViewD3D11Impl), sizeof(ShaderD3D11Impl), sizeof(SamplerD3D11Impl), sizeof(PipelineStateD3D11Impl), sizeof(ShaderResourceBindingD3D11Impl)), +RenderDeviceD3D11Impl :: RenderDeviceD3D11Impl(IReferenceCounters *pRefCounters, IMemoryAllocator &RawMemAllocator, const EngineD3D11Attribs& EngineAttribs, ID3D11Device *pd3d11Device, Uint32 NumDeferredContexts) : + TRenderDeviceBase(pRefCounters, RawMemAllocator, NumDeferredContexts, sizeof(TextureBaseD3D11), sizeof(TextureViewD3D11Impl), sizeof(BufferD3D11Impl), sizeof(BufferViewD3D11Impl), sizeof(ShaderD3D11Impl), sizeof(SamplerD3D11Impl), sizeof(PipelineStateD3D11Impl), sizeof(ShaderResourceBindingD3D11Impl)), m_EngineAttribs(EngineAttribs), m_pd3d11Device(pd3d11Device) { @@ -194,12 +194,27 @@ void RenderDeviceD3D11Impl::TestTextureFormat( TEXTURE_FORMAT TexFormat ) IMPLEMENT_QUERY_INTERFACE( RenderDeviceD3D11Impl, IID_RenderDeviceD3D11, TRenderDeviceBase ) +void RenderDeviceD3D11Impl :: CreateBufferFromD3DResource(ID3D11Buffer *pd3d11Buffer, const BufferDesc& BuffDesc, IBuffer **ppBuffer) +{ + CreateDeviceObject("buffer", BuffDesc, ppBuffer, + [&]() + { + BufferD3D11Impl *pBufferD3D11( NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl) + (m_BuffViewObjAllocator, this, BuffDesc, pd3d11Buffer ) ); + pBufferD3D11->QueryInterface( IID_Buffer, reinterpret_cast<IObject**>(ppBuffer) ); + pBufferD3D11->CreateDefaultViews(); + OnCreateDeviceObject( pBufferD3D11 ); + } + ); +} + void RenderDeviceD3D11Impl :: CreateBuffer(const BufferDesc& BuffDesc, const BufferData &BuffData, IBuffer **ppBuffer) { CreateDeviceObject("buffer", BuffDesc, ppBuffer, [&]() { - BufferD3D11Impl *pBufferD3D11( NEW(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl, m_BuffViewObjAllocator, this, BuffDesc, BuffData ) ); + BufferD3D11Impl *pBufferD3D11( NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl) + (m_BuffViewObjAllocator, this, BuffDesc, BuffData ) ); pBufferD3D11->QueryInterface( IID_Buffer, reinterpret_cast<IObject**>(ppBuffer) ); pBufferD3D11->CreateDefaultViews(); OnCreateDeviceObject( pBufferD3D11 ); @@ -212,7 +227,8 @@ void RenderDeviceD3D11Impl :: CreateShader(const ShaderCreationAttribs &ShaderCr CreateDeviceObject( "shader", ShaderCreationAttribs.Desc, ppShader, [&]() { - ShaderD3D11Impl *pShaderD3D11( NEW(m_ShaderObjAllocator, "ShaderD3D11Impl instance", ShaderD3D11Impl, this, ShaderCreationAttribs ) ); + ShaderD3D11Impl *pShaderD3D11( NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D11Impl instance", ShaderD3D11Impl) + (this, ShaderCreationAttribs ) ); pShaderD3D11->QueryInterface( IID_Shader, reinterpret_cast<IObject**>(ppShader) ); OnCreateDeviceObject( pShaderD3D11 ); @@ -220,6 +236,64 @@ void RenderDeviceD3D11Impl :: CreateShader(const ShaderCreationAttribs &ShaderCr ); } +void RenderDeviceD3D11Impl::CreateTextureFromD3DResource(ID3D11Texture1D *pd3d11Texture, ITexture **ppTexture) +{ + if (pd3d11Texture == nullptr) + return; + + TextureDesc TexDesc; + TexDesc.Name = "Texture1D from native d3d11 texture"; + CreateDeviceObject( "texture", TexDesc, ppTexture, + [&]() + { + TextureBaseD3D11 *pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_D3D11 instance", Texture1D_D3D11) + (m_TexViewObjAllocator, this, pd3d11Texture); + pTextureD3D11->QueryInterface( IID_Texture, reinterpret_cast<IObject**>(ppTexture) ); + pTextureD3D11->CreateDefaultViews(); + OnCreateDeviceObject( pTextureD3D11 ); + } + ); +} + +void RenderDeviceD3D11Impl::CreateTextureFromD3DResource(ID3D11Texture2D *pd3d11Texture, ITexture **ppTexture) +{ + if (pd3d11Texture == nullptr) + return; + + TextureDesc TexDesc; + TexDesc.Name = "Texture2D from native d3d11 texture"; + CreateDeviceObject( "texture", TexDesc, ppTexture, + [&]() + { + TextureBaseD3D11 *pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_D3D11 instance", Texture2D_D3D11) + (m_TexViewObjAllocator, this, pd3d11Texture); + pTextureD3D11->QueryInterface( IID_Texture, reinterpret_cast<IObject**>(ppTexture) ); + pTextureD3D11->CreateDefaultViews(); + OnCreateDeviceObject( pTextureD3D11 ); + } + ); +} + +void RenderDeviceD3D11Impl::CreateTextureFromD3DResource(ID3D11Texture3D *pd3d11Texture, ITexture **ppTexture) +{ + if (pd3d11Texture == nullptr) + return; + + TextureDesc TexDesc; + TexDesc.Name = "Texture3D from native d3d11 texture"; + CreateDeviceObject( "texture", TexDesc, ppTexture, + [&]() + { + TextureBaseD3D11 *pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_D3D11 instance", Texture3D_D3D11) + (m_TexViewObjAllocator, this, pd3d11Texture); + pTextureD3D11->QueryInterface( IID_Texture, reinterpret_cast<IObject**>(ppTexture) ); + pTextureD3D11->CreateDefaultViews(); + OnCreateDeviceObject( pTextureD3D11 ); + } + ); +} + + void RenderDeviceD3D11Impl :: CreateTexture(const TextureDesc& TexDesc, const TextureData &Data, ITexture **ppTexture) { CreateDeviceObject( "texture", TexDesc, ppTexture, @@ -230,18 +304,21 @@ void RenderDeviceD3D11Impl :: CreateTexture(const TextureDesc& TexDesc, const Te { case RESOURCE_DIM_TEX_1D: case RESOURCE_DIM_TEX_1D_ARRAY: - pTextureD3D11 = NEW(m_TexObjAllocator, "Texture1D_D3D11 instance", Texture1D_D3D11, m_TexViewObjAllocator, this, TexDesc, Data ); + pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_D3D11 instance", Texture1D_D3D11) + (m_TexViewObjAllocator, this, TexDesc, Data ); break; case RESOURCE_DIM_TEX_2D: case RESOURCE_DIM_TEX_2D_ARRAY: case RESOURCE_DIM_TEX_CUBE: case RESOURCE_DIM_TEX_CUBE_ARRAY: - pTextureD3D11 = NEW(m_TexObjAllocator, "Texture2D_D3D11 instance", Texture2D_D3D11, m_TexViewObjAllocator, this, TexDesc, Data ); + pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_D3D11 instance", Texture2D_D3D11) + (m_TexViewObjAllocator, this, TexDesc, Data ); break; case RESOURCE_DIM_TEX_3D: - pTextureD3D11 = NEW(m_TexObjAllocator, "Texture3D_D3D11 instance", Texture3D_D3D11, m_TexViewObjAllocator, this, TexDesc, Data ); + pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_D3D11 instance", Texture3D_D3D11) + (m_TexViewObjAllocator, this, TexDesc, Data ); break; default: LOG_ERROR_AND_THROW( "Unknown texture type. (Did you forget to initialize the Type member of TextureDesc structure?)" ); @@ -261,7 +338,8 @@ void RenderDeviceD3D11Impl :: CreateSampler(const SamplerDesc& SamplerDesc, ISam m_SamplersRegistry.Find( SamplerDesc, reinterpret_cast<IDeviceObject**>(ppSampler) ); if( *ppSampler == nullptr ) { - SamplerD3D11Impl *pSamplerD3D11( NEW(m_SamplerObjAllocator, "SamplerD3D11Impl instance", SamplerD3D11Impl, this, SamplerDesc ) ); + SamplerD3D11Impl *pSamplerD3D11( NEW_RC_OBJ(m_SamplerObjAllocator, "SamplerD3D11Impl instance", SamplerD3D11Impl) + (this, SamplerDesc ) ); pSamplerD3D11->QueryInterface( IID_Sampler, reinterpret_cast<IObject**>(ppSampler) ); OnCreateDeviceObject( pSamplerD3D11 ); m_SamplersRegistry.Add( SamplerDesc, *ppSampler ); @@ -275,7 +353,8 @@ void RenderDeviceD3D11Impl::CreatePipelineState(const PipelineStateDesc &Pipelin CreateDeviceObject( "Pipeline state", PipelineDesc, ppPipelineState, [&]() { - PipelineStateD3D11Impl *pPipelineStateD3D11( NEW(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl, this, PipelineDesc ) ); + PipelineStateD3D11Impl *pPipelineStateD3D11( NEW_RC_OBJ(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl) + (this, PipelineDesc ) ); pPipelineStateD3D11->QueryInterface( IID_PipelineState, reinterpret_cast<IObject**>(ppPipelineState) ); OnCreateDeviceObject( pPipelineStateD3D11 ); } diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp index fdd9d551..5128c16d 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceFactoryD3D11.cpp @@ -36,6 +36,36 @@ namespace Diligent { +/// Engine factory for D3D11 implementation +class EngineFactoryD3D11Impl : public IEngineFactoryD3D11 +{ +public: + static EngineFactoryD3D11Impl* GetInstance() + { + static EngineFactoryD3D11Impl TheFactory; + return &TheFactory; + } + + void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, + IRenderDevice **ppDevice, + IDeviceContext **ppContexts, + Uint32 NumDeferredContexts )override final; + + void CreateSwapChainD3D11( IRenderDevice *pDevice, + IDeviceContext *pImmediateContext, + const SwapChainDesc& SCDesc, + void* pNativeWndHandle, + ISwapChain **ppSwapChain )override final; + + void AttachToD3D11Device(void *pd3d11NativeDevice, + void *pd3d11ImmediateContext, + const EngineD3D11Attribs& EngineAttribs, + IRenderDevice **ppDevice, + IDeviceContext **ppContexts, + Uint32 NumDeferredContexts)override final; +}; + + #if defined(_DEBUG) // Check for SDK Layer support. inline bool SdkLayersAvailable() @@ -70,107 +100,143 @@ inline bool SdkLayersAvailable() /// of deferred contexts is requested, pointers to the /// contexts are written to ppContexts array starting /// at position 1 -void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, IRenderDevice **ppDevice, IDeviceContext **ppContexts, Uint32 NumDeferredContexts ) +void EngineFactoryD3D11Impl::CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, IRenderDevice **ppDevice, IDeviceContext **ppContexts, Uint32 NumDeferredContexts ) { - VERIFY( ppDevice && ppContexts, "Null pointer is provided" ); + VERIFY( ppDevice && ppContexts, "Null pointer provided" ); if( !ppDevice || !ppContexts ) return; - SetRawAllocator(EngineAttribs.pRawMemAllocator); - *ppDevice = nullptr; memset(ppContexts, 0, sizeof(*ppContexts) * (1+NumDeferredContexts)); - try - { - // This flag adds support for surfaces with a different color channel ordering - // than the API default. It is required for compatibility with Direct2D. - // D3D11_CREATE_DEVICE_BGRA_SUPPORT; - UINT creationFlags = 0; - - #if defined(_DEBUG) - if (SdkLayersAvailable()) - { - // If the project is in a debug build, enable debugging via SDK Layers with this flag. - creationFlags |= D3D11_CREATE_DEVICE_DEBUG; - } - #endif - - // This array defines the set of DirectX hardware feature levels this app will support. - // Note the ordering should be preserved. - // Don't forget to declare your application's minimum required feature level in its - // description. All applications are assumed to support 9.1 unless otherwise stated. - D3D_FEATURE_LEVEL featureLevels[] = - { + // This flag adds support for surfaces with a different color channel ordering + // than the API default. It is required for compatibility with Direct2D. + // D3D11_CREATE_DEVICE_BGRA_SUPPORT; + UINT creationFlags = 0; + +#if defined(_DEBUG) + if (SdkLayersAvailable()) + { + // If the project is in a debug build, enable debugging via SDK Layers with this flag. + creationFlags |= D3D11_CREATE_DEVICE_DEBUG; + } +#endif + + // This array defines the set of DirectX hardware feature levels this app will support. + // Note the ordering should be preserved. + // Don't forget to declare your application's minimum required feature level in its + // description. All applications are assumed to support 9.1 unless otherwise stated. + D3D_FEATURE_LEVEL featureLevels[] = + { #ifdef PLATFORM_UNIVERSAL_WINDOWS - D3D_FEATURE_LEVEL_11_1, + D3D_FEATURE_LEVEL_11_1, #endif - D3D_FEATURE_LEVEL_11_0, - D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, - D3D_FEATURE_LEVEL_9_3, - D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1 - }; - - // Create the Direct3D 11 API device object and a corresponding context. - CComPtr<ID3D11Device> pd3d11Device; - CComPtr<ID3D11DeviceContext> pd3d11Context; - - D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_11_0; - HRESULT hr = D3D11CreateDevice( - nullptr, // Specify nullptr to use the default adapter. - D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver. - 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. - creationFlags, // Set debug and Direct2D compatibility flags. - featureLevels, // List of feature levels this app can support. - ARRAYSIZE(featureLevels), // Size of the list above. - D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. - &pd3d11Device, // Returns the Direct3D device created. - &d3dFeatureLevel, // Returns feature level of device created. - &pd3d11Context // Returns the device immediate context. - ); - - if (FAILED(hr)) - { - // If the initialization fails, fall back to the WARP device. - // For more information on WARP, see: - // http://go.microsoft.com/fwlink/?LinkId=286690 - hr = D3D11CreateDevice( - nullptr, - D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device. - 0, - creationFlags, - featureLevels, - ARRAYSIZE(featureLevels), - D3D11_SDK_VERSION, - &pd3d11Device, - &d3dFeatureLevel, - &pd3d11Context - ); - CHECK_D3D_RESULT_THROW( hr, "Failed to create D3D11 Device and swap chain" ); - } + D3D_FEATURE_LEVEL_11_0, + D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, + D3D_FEATURE_LEVEL_9_3, + D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1 + }; + + // Create the Direct3D 11 API device object and a corresponding context. + CComPtr<ID3D11Device> pd3d11Device; + CComPtr<ID3D11DeviceContext> pd3d11Context; + + D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_11_0; + HRESULT hr = D3D11CreateDevice( + nullptr, // Specify nullptr to use the default adapter. + D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver. + 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. + creationFlags, // Set debug and Direct2D compatibility flags. + featureLevels, // List of feature levels this app can support. + ARRAYSIZE(featureLevels), // Size of the list above. + D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps. + &pd3d11Device, // Returns the Direct3D device created. + &d3dFeatureLevel, // Returns feature level of device created. + &pd3d11Context // Returns the device immediate context. + ); + + if (FAILED(hr)) + { + // If the initialization fails, fall back to the WARP device. + // For more information on WARP, see: + // http://go.microsoft.com/fwlink/?LinkId=286690 + hr = D3D11CreateDevice( + nullptr, + D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device. + 0, + creationFlags, + featureLevels, + ARRAYSIZE(featureLevels), + D3D11_SDK_VERSION, + &pd3d11Device, + &d3dFeatureLevel, + &pd3d11Context + ); + LOG_ERROR("Failed to create D3D11 native device and immediate context"); + return; + } + + AttachToD3D11Device(pd3d11Device, pd3d11Context, EngineAttribs, ppDevice, ppContexts, NumDeferredContexts); +} + + +/// Attaches to existing D3D11 render device and immediate context +/// \param [in] pd3d11NativeDevice - pointer to native D3D11 device +/// \param [in] pd3d11ImmediateContext - pointer to native D3D11 immediate context +/// \param [in] EngineAttribs - Engine creation attributes. +/// \param [out] ppDevice - Address of the memory location where pointer to +/// the created device will be written +/// \param [out] ppContexts - Address of the memory location where pointers to +/// the contexts will be written. Pointer to the immediate +/// context goes at position 0. If NumDeferredContexts > 0, +/// pointers to deferred contexts go afterwards. +/// \param [in] NumDeferredContexts - Number of deferred contexts. If non-zero number +/// of deferred contexts is requested, pointers to the +/// contexts are written to ppContexts array starting +/// at position 1 +void EngineFactoryD3D11Impl::AttachToD3D11Device(void *pd3d11NativeDevice, + void *pd3d11ImmediateContext, + const EngineD3D11Attribs& EngineAttribs, + IRenderDevice **ppDevice, + IDeviceContext **ppContexts, + Uint32 NumDeferredContexts) +{ + VERIFY( ppDevice && ppContexts, "Null pointer provided" ); + if( !ppDevice || !ppContexts ) + return; + + try + { + ID3D11Device *pd3d11Device = reinterpret_cast<ID3D11Device *>(pd3d11NativeDevice); + ID3D11DeviceContext *pd3d11ImmediateCtx = reinterpret_cast<ID3D11DeviceContext *>(pd3d11ImmediateContext); + + SetRawAllocator(EngineAttribs.pRawMemAllocator); auto &RawAlloctor = GetRawAllocator(); - RenderDeviceD3D11Impl *pRenderDeviceD3D11( NEW(RawAlloctor, "RenderDeviceD3D11Impl instance", RenderDeviceD3D11Impl, EngineAttribs, pd3d11Device, NumDeferredContexts ) ); - pRenderDeviceD3D11->QueryInterface(IID_RenderDevice, reinterpret_cast<IObject**>(ppDevice) ); + RenderDeviceD3D11Impl *pRenderDeviceD3D11(NEW_RC_OBJ(RawAlloctor, "RenderDeviceD3D11Impl instance", RenderDeviceD3D11Impl) + (RawAlloctor, EngineAttribs, pd3d11Device, NumDeferredContexts)); + pRenderDeviceD3D11->QueryInterface(IID_RenderDevice, reinterpret_cast<IObject**>(ppDevice)); - RefCntAutoPtr<DeviceContextD3D11Impl> pDeviceContextD3D11( NEW(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl, pRenderDeviceD3D11, pd3d11Context, EngineAttribs, false) ); + RefCntAutoPtr<DeviceContextD3D11Impl> pDeviceContextD3D11(NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) + (RawAlloctor, pRenderDeviceD3D11, pd3d11ImmediateCtx, EngineAttribs, false)); // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D11 will // keep a weak reference to the context - pDeviceContextD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppContexts) ); + pDeviceContextD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppContexts)); pRenderDeviceD3D11->SetImmediateContext(pDeviceContextD3D11); for (Uint32 DeferredCtx = 0; DeferredCtx < NumDeferredContexts; ++DeferredCtx) { CComPtr<ID3D11DeviceContext> pd3d11DeferredCtx; - hr = pd3d11Device->CreateDeferredContext(0, &pd3d11DeferredCtx); + HRESULT hr = pd3d11Device->CreateDeferredContext(0, &pd3d11DeferredCtx); CHECK_D3D_RESULT_THROW(hr, "Failed to create D3D11 deferred context") - - RefCntAutoPtr<DeviceContextD3D11Impl> pDeferredCtxD3D11( NEW(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl, pRenderDeviceD3D11, pd3d11DeferredCtx, EngineAttribs, true) ); + RefCntAutoPtr<DeviceContextD3D11Impl> pDeferredCtxD3D11( + NEW_RC_OBJ(RawAlloctor, "DeviceContextD3D11Impl instance", DeviceContextD3D11Impl) + (RawAlloctor, pRenderDeviceD3D11, pd3d11DeferredCtx, EngineAttribs, true)); // We must call AddRef() (implicitly through QueryInterface()) because pRenderDeviceD3D12 will // keep a weak reference to the context - pDeferredCtxD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppContexts + 1 + DeferredCtx) ); + pDeferredCtxD3D11->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppContexts + 1 + DeferredCtx)); pRenderDeviceD3D11->SetDeferredContext(DeferredCtx, pDeferredCtxD3D11); } } @@ -190,7 +256,7 @@ void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, IRen } } - LOG_ERROR( "Failed to create device and immediate context" ); + LOG_ERROR( "Failed to initialize D3D11 device and contexts" ); } } @@ -208,9 +274,9 @@ void CreateDeviceAndContextsD3D11( const EngineD3D11Attribs& EngineAttribs, IRen /// /// \param [out] ppSwapChain - Address of the memory location where pointer to the new /// swap chain will be written -void CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pImmediateContext, const SwapChainDesc& SCDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain ) +void EngineFactoryD3D11Impl::CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pImmediateContext, const SwapChainDesc& SCDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain ) { - VERIFY( ppSwapChain, "Null pointer is provided" ); + VERIFY( ppSwapChain, "Null pointer provided" ); if( !ppSwapChain ) return; @@ -222,7 +288,8 @@ void CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pIm auto *pDeviceContextD3D11 = ValidatedCast<DeviceContextD3D11Impl>(pImmediateContext); auto &RawMemAllocator = GetRawAllocator(); - auto *pSwapChainD3D11 = NEW(RawMemAllocator, "SwapChainD3D11Impl instance", SwapChainD3D11Impl, SCDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); + auto *pSwapChainD3D11 = NEW_RC_OBJ(RawMemAllocator, "SwapChainD3D11Impl instance", SwapChainD3D11Impl) + (SCDesc, pDeviceD3D11, pDeviceContextD3D11, pNativeWndHandle); pSwapChainD3D11->QueryInterface( IID_SwapChain, reinterpret_cast<IObject**>(ppSwapChain) ); pDeviceContextD3D11->SetSwapChain(pSwapChainD3D11); @@ -238,10 +305,12 @@ void CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pIm { auto *pDeferredCtxD3D11 = ValidatedCast<DeviceContextD3D11Impl>(pDeferredCtx.RawPtr()); pDeferredCtxD3D11->SetSwapChain(pSwapChainD3D11); - // Bind default render target - pDeferredCtxD3D11->SetRenderTargets( 0, nullptr, nullptr ); - // Set default viewport - pDeferredCtxD3D11->SetViewports( 1, nullptr, 0, 0 ); + // Do not bind default render target and viewport to be + // consistent with D3D12 + //// Bind default render target + //pDeferredCtxD3D11->SetRenderTargets( 0, nullptr, nullptr ); + //// Set default viewport + //pDeferredCtxD3D11->SetViewports( 1, nullptr, 0, 0 ); } } } @@ -259,17 +328,15 @@ void CreateSwapChainD3D11( IRenderDevice *pDevice, Diligent::IDeviceContext *pIm #ifdef DOXYGEN /// Loads Direct3D11-based engine implementation and exports factory functions -/// \param [out] CreateDeviceFunc - Pointer to the function that creates render device and device contexts. -/// See CreateDeviceAndContextsD3D11(). -/// \param [out] CreateSwapChainFunc - Pointer to the function that creates swap chain. -/// See CreateSwapChainD3D11(). +/// \param [out] GetFactoryFunc - Pointer to the function that returns factory for D3D11 engine implementation +/// See EngineFactoryD3D11Impl. /// \remarks Depending on the configuration and platform, the function loads different dll: /// Platform\\Configuration | Debug | Release /// --------------------------|-------------------------------|---------------------------- /// x86 | GraphicsEngineD3D11_32d.dll | GraphicsEngineD3D11_32r.dll /// x64 | GraphicsEngineD3D11_64d.dll | GraphicsEngineD3D11_64r.dll /// -void LoadGraphicsEngineD3D11(CreateDeviceAndContextsD3D11Type &CreateDeviceFunc, CreateSwapChainD3D11Type &CreateSwapChainFunc) +void LoadGraphicsEngineD3D11(GetEngineFactoryD3D11Type &GetFactoryFunc) { // This function is only required because DoxyGen refuses to generate documentation for a static function when SHOW_FILES==NO #error This function must never be compiled; @@ -278,18 +345,11 @@ void LoadGraphicsEngineD3D11(CreateDeviceAndContextsD3D11Type &CreateDeviceFunc, } - using namespace Diligent; extern "C" { - void CreateDeviceAndContextsD3D11(const EngineD3D11Attribs& EngineAttribs, IRenderDevice **ppDevice, IDeviceContext **ppContexts, Uint32 NumDeferredContexts) - { - Diligent::CreateDeviceAndContextsD3D11(EngineAttribs, ppDevice, ppContexts, NumDeferredContexts); - } - - - void CreateSwapChainD3D11(IRenderDevice *pDevice, IDeviceContext *pImmediateContext, const SwapChainDesc& SwapChainDesc, void* pNativeWndHandle, ISwapChain **ppSwapChain) + IEngineFactoryD3D11* GetEngineFactoryD3D11() { - Diligent::CreateSwapChainD3D11(pDevice, pImmediateContext, SwapChainDesc, pNativeWndHandle, ppSwapChain); + return EngineFactoryD3D11Impl::GetInstance(); } } diff --git a/Graphics/GraphicsEngineD3D11/src/SamplerD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/SamplerD3D11Impl.cpp index fbb48400..bf40b931 100644 --- a/Graphics/GraphicsEngineD3D11/src/SamplerD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/SamplerD3D11Impl.cpp @@ -29,8 +29,8 @@ namespace Diligent { -SamplerD3D11Impl::SamplerD3D11Impl(FixedBlockMemoryAllocator &SamplerObjAllocator, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const SamplerDesc& SamplerDesc) : - TSamplerBase(SamplerObjAllocator, pRenderDeviceD3D11, SamplerDesc) +SamplerD3D11Impl::SamplerD3D11Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const SamplerDesc& SamplerDesc) : + TSamplerBase(pRefCounters, pRenderDeviceD3D11, SamplerDesc) { auto *pd3d11Device = pRenderDeviceD3D11->GetD3D11Device(); D3D11_SAMPLER_DESC D3D11SamplerDesc = diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp index 2cf70547..2e8d6857 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderD3D11Impl.cpp @@ -30,8 +30,8 @@ namespace Diligent { -ShaderD3D11Impl::ShaderD3D11Impl(FixedBlockMemoryAllocator &ShaderObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const ShaderCreationAttribs &CreationAttribs) : - TShaderBase(ShaderObjAllocator, pRenderDeviceD3D11, CreationAttribs.Desc), +ShaderD3D11Impl::ShaderD3D11Impl(IReferenceCounters *pRefCounters, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const ShaderCreationAttribs &CreationAttribs) : + TShaderBase(pRefCounters, pRenderDeviceD3D11, CreationAttribs.Desc), ShaderD3DBase(CreationAttribs), m_StaticResLayout(*this, GetRawAllocator()), m_ShaderTypeIndex(Diligent::GetShaderTypeIndex(CreationAttribs.Desc.ShaderType)), diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 01caac80..ccb8e9b8 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -31,8 +31,8 @@ namespace Diligent { -ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( FixedBlockMemoryAllocator &SRBAllocator, PipelineStateD3D11Impl *pPSO, bool IsInternal) : - TBase( SRBAllocator, pPSO, IsInternal ), +ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounters *pRefCounters, PipelineStateD3D11Impl *pPSO, bool IsInternal) : + TBase( pRefCounters, pPSO, IsInternal ), m_bIsStaticResourcesBound(false) { for(size_t s=0; s < _countof(m_ResourceLayoutIndex); ++s) diff --git a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp index f729bef6..79d84b1c 100644 --- a/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/SwapChainD3D11Impl.cpp @@ -32,12 +32,12 @@ using namespace Diligent; namespace Diligent { -SwapChainD3D11Impl::SwapChainD3D11Impl(IMemoryAllocator &Allocator, +SwapChainD3D11Impl::SwapChainD3D11Impl(IReferenceCounters *pRefCounters, const SwapChainDesc& SCDesc, RenderDeviceD3D11Impl* pRenderDeviceD3D11, DeviceContextD3D11Impl* pDeviceContextD3D11, void* pNativeWndHandle) : - TSwapChainBase(Allocator, pRenderDeviceD3D11, pDeviceContextD3D11, SCDesc) + TSwapChainBase(pRefCounters, pRenderDeviceD3D11, pDeviceContextD3D11, SCDesc) { #ifdef PLATFORM_WIN32 @@ -190,7 +190,10 @@ void SwapChainD3D11Impl::Present() // that are only kept alive by references in the cache // It is better to do this before calling Present() as D3D11 // also releases resources during present. - pImmediateCtxD3D11->ClearShaderStateCache(); + pImmediateCtxD3D11->ReleaseCommittedShaderResources(); + // ReleaseCommittedShaderResources() does not unbind vertex and index buffers + // as this can explicitly be done by the user + m_pSwapChain->Present( SyncInterval, 0 ); @@ -198,7 +201,7 @@ void SwapChainD3D11Impl::Present() // backbuffer 0 from all GPU writeable bind points. // We need to rebind all render targets to make sure that // the back buffer is not unbound - pImmediateCtxD3D11->RebindRenderTargets(); + pImmediateCtxD3D11->CommitRenderTargets(); } void SwapChainD3D11Impl::Resize( Uint32 NewWidth, Uint32 NewHeight ) diff --git a/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp index 8c54fb69..8e1f3bd5 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture1D_D3D11.cpp @@ -29,12 +29,12 @@ namespace Diligent { -Texture1D_D3D11 :: Texture1D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, +Texture1D_D3D11 :: Texture1D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : - TextureBaseD3D11(TexObjAllocator, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) { auto D3D11TexFormat = TexFormatToDXGI_Format(m_Desc.Format, m_Desc.BindFlags); auto D3D11BindFlags = BindFlagsToD3D11BindFlags(m_Desc.BindFlags); @@ -64,6 +64,37 @@ Texture1D_D3D11 :: Texture1D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, CHECK_D3D_RESULT_THROW( hr, "Failed to create the Direct3D11 Texture1D" ); } +static TextureDesc TexDescFromD3D11Texture1D(ID3D11Texture1D *pd3d11Texture) +{ + D3D11_TEXTURE1D_DESC D3D11TexDesc; + pd3d11Texture->GetDesc(&D3D11TexDesc); + + TextureDesc TexDesc; + TexDesc.Name = "Texture1D_D3D11 from native d3d11 texture"; + TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_1D_ARRAY : RESOURCE_DIM_TEX_1D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = 1; + TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = 1; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + + return TexDesc; +} + +Texture1D_D3D11 :: Texture1D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture1D *pd3d11Texture) : + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pDeviceD3D11, TexDescFromD3D11Texture1D(pd3d11Texture), TextureData()) +{ + m_pd3d11Texture = pd3d11Texture; +} + Texture1D_D3D11 :: ~Texture1D_D3D11() { } diff --git a/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp index 1e749926..82372121 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture2D_D3D11.cpp @@ -29,12 +29,12 @@ namespace Diligent { -Texture2D_D3D11 :: Texture2D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, +Texture2D_D3D11 :: Texture2D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : - TextureBaseD3D11(TexObjAllocator, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) { auto D3D11TexFormat = TexFormatToDXGI_Format(m_Desc.Format, m_Desc.BindFlags); auto D3D11BindFlags = BindFlagsToD3D11BindFlags(m_Desc.BindFlags); @@ -69,6 +69,37 @@ Texture2D_D3D11 :: Texture2D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, CHECK_D3D_RESULT_THROW( hr, "Failed to create the Direct3D11 Texture2D" ); } +static TextureDesc TexDescFromD3D11Texture2D(ID3D11Texture2D *pd3d11Texture) +{ + D3D11_TEXTURE2D_DESC D3D11TexDesc; + pd3d11Texture->GetDesc(&D3D11TexDesc); + + TextureDesc TexDesc; + TexDesc.Name = "Texture2D_D3D11 from native d3d11 texture"; + TexDesc.Type = D3D11TexDesc.ArraySize > 1 ? RESOURCE_DIM_TEX_2D_ARRAY : RESOURCE_DIM_TEX_2D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = Uint32{D3D11TexDesc.Height}; + TexDesc.ArraySize = Uint32{D3D11TexDesc.ArraySize}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = Uint32{ D3D11TexDesc.SampleDesc.Count }; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + + return TexDesc; +} + +Texture2D_D3D11 :: Texture2D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture2D *pd3d11Texture) : + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pDeviceD3D11, TexDescFromD3D11Texture2D(pd3d11Texture), TextureData()) +{ + m_pd3d11Texture = pd3d11Texture; +} + Texture2D_D3D11 :: ~Texture2D_D3D11() { } diff --git a/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp b/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp index 0cbe3b7f..87b09876 100644 --- a/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/Texture3D_D3D11.cpp @@ -29,12 +29,12 @@ namespace Diligent { -Texture3D_D3D11 :: Texture3D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, +Texture3D_D3D11 :: Texture3D_D3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : - TextureBaseD3D11(TexObjAllocator, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc, InitData) { auto D3D11TexFormat = TexFormatToDXGI_Format(m_Desc.Format, m_Desc.BindFlags); auto D3D11BindFlags = BindFlagsToD3D11BindFlags(m_Desc.BindFlags); @@ -65,6 +65,37 @@ Texture3D_D3D11 :: Texture3D_D3D11(FixedBlockMemoryAllocator &TexObjAllocator, CHECK_D3D_RESULT_THROW( hr, "Failed to create the Direct3D11 Texture3D" ); } +static TextureDesc TexDescFromD3D11Texture3D(ID3D11Texture3D *pd3d11Texture) +{ + D3D11_TEXTURE3D_DESC D3D11TexDesc; + pd3d11Texture->GetDesc(&D3D11TexDesc); + + TextureDesc TexDesc; + TexDesc.Name = "Texture3D_D3D11 from native d3d11 texture"; + TexDesc.Type = RESOURCE_DIM_TEX_3D; + TexDesc.Width = Uint32{D3D11TexDesc.Width}; + TexDesc.Height = Uint32{D3D11TexDesc.Height}; + TexDesc.Depth = Uint32{D3D11TexDesc.Depth}; + TexDesc.Format = DXGI_FormatToTexFormat(D3D11TexDesc.Format); + TexDesc.MipLevels = Uint32{D3D11TexDesc.MipLevels}; + TexDesc.SampleCount = 1; + TexDesc.Usage = D3D11UsageToUsage(D3D11TexDesc.Usage); + TexDesc.BindFlags = D3D11BindFlagsToBindFlags(D3D11TexDesc.BindFlags); + TexDesc.CPUAccessFlags = D3D11CPUAccessFlagsToCPUAccessFlags(D3D11TexDesc.CPUAccessFlags); + TexDesc.MiscFlags = D3D11MiscFlagsToMiscTextureFlags(D3D11TexDesc.MiscFlags); + + return TexDesc; +} + +Texture3D_D3D11 :: Texture3D_D3D11(IReferenceCounters *pRefCounters, + FixedBlockMemoryAllocator &TexViewObjAllocator, + RenderDeviceD3D11Impl *pDeviceD3D11, + ID3D11Texture3D *pd3d11Texture) : + TextureBaseD3D11(pRefCounters, TexViewObjAllocator, pDeviceD3D11, TexDescFromD3D11Texture3D(pd3d11Texture), TextureData()) +{ + m_pd3d11Texture = pd3d11Texture; +} + void Texture3D_D3D11::CreateSRV( TextureViewDesc &SRVDesc, ID3D11ShaderResourceView **ppD3D11SRV ) { VERIFY( ppD3D11SRV && *ppD3D11SRV == nullptr, "SRV pointer address is null or contains non-null pointer to an existing object" ); diff --git a/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp index 12726e4b..79706138 100644 --- a/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp @@ -32,8 +32,8 @@ namespace Diligent { -TextureBaseD3D11 :: TextureBaseD3D11(FixedBlockMemoryAllocator &TexObjAllocator, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : - TTextureBase(TexObjAllocator, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc) +TextureBaseD3D11 :: TextureBaseD3D11(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &TexViewObjAllocator, RenderDeviceD3D11Impl *pRenderDeviceD3D11, const TextureDesc& TexDesc, const TextureData &InitData /*= TextureData()*/) : + TTextureBase(pRefCounters, TexViewObjAllocator, pRenderDeviceD3D11, TexDesc) { if( TexDesc.Usage == USAGE_STATIC && InitData.pSubResources == nullptr ) LOG_ERROR_AND_THROW("Static Texture must be initialized with data at creation time"); @@ -100,7 +100,8 @@ void TextureBaseD3D11::CreateViewInternal( const struct TextureViewDesc &ViewDes auto &TexViewAllocator = pDeviceD3D11Impl->GetTexViewObjAllocator(); VERIFY( &TexViewAllocator == &m_dbgTexViewObjAllocator, "Texture view allocator does not match allocator provided during texture initialization" ); - auto pViewD3D11 = NEW(TexViewAllocator, "TextureViewD3D11Impl instance", TextureViewD3D11Impl, pDeviceD3D11Impl, UpdatedViewDesc, this, pD3D11View, bIsDefaultView ); + auto pViewD3D11 = NEW_RC_OBJ(TexViewAllocator, "TextureViewD3D11Impl instance", TextureViewD3D11Impl, bIsDefaultView ? this : nullptr) + (pDeviceD3D11Impl, UpdatedViewDesc, this, pD3D11View, bIsDefaultView ); VERIFY( pViewD3D11->GetDesc().ViewType == ViewDesc.ViewType, "Incorrect view type" ); if( bIsDefaultView ) @@ -144,6 +145,12 @@ TextureBaseD3D11 :: ~TextureBaseD3D11() void TextureBaseD3D11::UpdateData( IDeviceContext *pContext, Uint32 MipLevel, Uint32 Slice, const Box &DstBox, const TextureSubResData &SubresData ) { TTextureBase::UpdateData( pContext, MipLevel, Slice, DstBox, SubresData ); + if (SubresData.pSrcBuffer != nullptr) + { + LOG_ERROR("D3D11 does not support updating texture subresource from a GPU buffer"); + return; + } + VERIFY( m_Desc.Usage == USAGE_DEFAULT, "Only default usage resiurces can be updated with UpdateData()" ); auto *pd3d11DeviceContext = static_cast<DeviceContextD3D11Impl*>(pContext)->GetD3D11DeviceContext(); @@ -192,28 +199,36 @@ void TextureBaseD3D11 :: CopyData(IDeviceContext *pContext, pd3d11DeviceContext->CopySubresourceRegion(m_pd3d11Texture, DstSubRes, DstX, DstY, DstZ, pSrTextureBaseD3D11->GetD3D11Texture(), SrcSubRes, pD3D11SrcBox); } -void TextureBaseD3D11 :: Map(IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData) +void TextureBaseD3D11 :: Map( IDeviceContext *pContext, Uint32 Subresource, MAP_TYPE MapType, Uint32 MapFlags, MappedTextureSubresource &MappedData ) { - TTextureBase::Map( pContext, MapType, MapFlags, pMappedData ); + TTextureBase::Map( pContext, Subresource, MapType, MapFlags, MappedData ); - //auto *pd3d11DeviceContext = static_cast<RenderDeviceD3D11Impl*>( static_cast<CRenderDevice*>(m_pDevice) )->GetD3D11DeviceContext(); - //auto d3d11MapType = MapTypeToD3D11MapType(MapType); - //auto d3d11MapFlags = MapFlagsToD3D11MapFlags(MapFlags); - - //D3D11_MAPPED_SUBRESOURCE MappedBuff; - //pd3d11DeviceContext->Map(m_pd3d11Texture, 0, d3d11MapType, d3d11MapFlags, &MappedBuff); - - //pMappedData = MappedBuff.pData; + auto *pd3d11DeviceContext = static_cast<DeviceContextD3D11Impl*>(pContext)->GetD3D11DeviceContext(); + D3D11_MAP d3d11MapType = static_cast<D3D11_MAP>(0); + UINT d3d11MapFlags = 0; + MapParamsToD3D11MapParams(MapType, MapFlags, d3d11MapType, d3d11MapFlags); - //VERIFY( pMappedData, "Map failed" ); + D3D11_MAPPED_SUBRESOURCE MappedTex; + auto hr = pd3d11DeviceContext->Map(m_pd3d11Texture, Subresource, d3d11MapType, d3d11MapFlags, &MappedTex); + if( FAILED(hr) ) + { + VERIFY_EXPR( hr == DXGI_ERROR_WAS_STILL_DRAWING ); + MappedData = MappedTextureSubresource(); + } + else + { + MappedData.pData = MappedTex.pData; + MappedData.Stride = MappedTex.RowPitch; + MappedData.DepthStride = MappedTex.DepthPitch; + } } -void TextureBaseD3D11::Unmap( IDeviceContext *pContext, MAP_TYPE MapType ) +void TextureBaseD3D11::Unmap( IDeviceContext *pContext, Uint32 Subresource, MAP_TYPE MapType, Uint32 MapFlags ) { - TTextureBase::Unmap( pContext, MapType ); + TTextureBase::Unmap( pContext, Subresource, MapType, MapFlags ); - //auto *pd3d11DeviceContext = static_cast<RenderDeviceD3D11Impl*>( static_cast<CRenderDevice*>(m_pDevice) )->GetD3D11DeviceContext(); - //pd3d11DeviceContext->Unmap(m_pd3d11Texture, 0); + auto *pd3d11DeviceContext = static_cast<DeviceContextD3D11Impl*>(pContext)->GetD3D11DeviceContext(); + pd3d11DeviceContext->Unmap(m_pd3d11Texture, Subresource); } } diff --git a/Graphics/GraphicsEngineD3D11/src/TextureViewD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/TextureViewD3D11Impl.cpp index af0ca4ef..5876ed60 100644 --- a/Graphics/GraphicsEngineD3D11/src/TextureViewD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/TextureViewD3D11Impl.cpp @@ -28,13 +28,13 @@ namespace Diligent { -TextureViewD3D11Impl::TextureViewD3D11Impl( FixedBlockMemoryAllocator &TexViewAllocator, +TextureViewD3D11Impl::TextureViewD3D11Impl( IReferenceCounters *pRefCounters, IRenderDevice *pDevice, const TextureViewDesc& ViewDesc, ITexture *pTexture, ID3D11View* pD3D11View, bool bIsDefaultView ) : - TTextureViewBase( TexViewAllocator, pDevice, ViewDesc, pTexture, bIsDefaultView ), + TTextureViewBase( pRefCounters, pDevice, ViewDesc, pTexture, bIsDefaultView ), m_pD3D11View( pD3D11View ) { } |
