diff options
| author | Konstantin <const@const.me> | 2023-01-16 14:52:43 +0100 |
|---|---|---|
| committer | Konstantin <const@const.me> | 2023-01-16 14:52:43 +0100 |
| commit | 8c4603c73675958efc960fbd4bb599a2909d106a (patch) | |
| tree | 714dc6fc9a1672d5fd7f89676b97e10959662abc /Whisper/ML/ConstantBuffer.cpp | |
| parent | 990a8d0dbaefc996244097397259e92758b15cce (diff) | |
Source codes
Diffstat (limited to 'Whisper/ML/ConstantBuffer.cpp')
| -rw-r--r-- | Whisper/ML/ConstantBuffer.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Whisper/ML/ConstantBuffer.cpp b/Whisper/ML/ConstantBuffer.cpp new file mode 100644 index 0000000..5f3bfbe --- /dev/null +++ b/Whisper/ML/ConstantBuffer.cpp @@ -0,0 +1,63 @@ +#include "stdafx.h" +#include "ConstantBuffer.h" +#include "../D3D/MappedResource.h" +using namespace DirectCompute; + +HRESULT ConstantBuffer::create() +{ + if( nullptr == buffer ) + { + CD3D11_BUFFER_DESC desc{ 16 * 3 * 2, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE }; + return device()->CreateBuffer( &desc, nullptr, &buffer ); + } + return HRESULT_FROM_WIN32( ERROR_ALREADY_INITIALIZED ); +} + +namespace +{ + __forceinline void copy32( __m128i* rdi, const TensorShape& ts ) + { + _mm_storeu_si128( rdi, ts.sizeVec() ); + _mm_storeu_si128( rdi + 1, ts.stridesVec() ); + } +} + +HRESULT ConstantBuffer::update( const TensorShape& t0 ) +{ + MappedResource mapped; + CHECK( mapped.map( buffer, false ) ); + + __m128i* const rdi = ( __m128i* )mapped.data(); + copy32( rdi, t0 ); + return S_OK; +} + +HRESULT ConstantBuffer::update( const TensorShape& t0, const TensorShape& t1 ) +{ + MappedResource mapped; + CHECK( mapped.map( buffer, false ) ); + + __m128i* const rdi = ( __m128i* )mapped.data(); + copy32( rdi, t0 ); + copy32( rdi + 2, t1 ); + return S_OK; +} + +HRESULT ConstantBuffer::update( const TensorShape& t0, const TensorShape& t1, const TensorShape& t2 ) +{ + MappedResource mapped; + CHECK( mapped.map( buffer, false ) ); + + __m128i* const rdi = ( __m128i* )mapped.data(); + copy32( rdi, t0 ); + copy32( rdi + 2, t1 ); + copy32( rdi + 4, t2 ); + return S_OK; +} + +void ConstantBuffer::bind() const +{ + ID3D11Buffer* p = buffer; + assert( nullptr != p ); + context()->CSSetConstantBuffers( 0, 1, &p ); +}
\ No newline at end of file |
