From 8c4603c73675958efc960fbd4bb599a2909d106a Mon Sep 17 00:00:00 2001 From: Konstantin Date: Mon, 16 Jan 2023 14:52:43 +0100 Subject: Source codes --- Whisper/ML/ConstantBuffer.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Whisper/ML/ConstantBuffer.cpp (limited to 'Whisper/ML/ConstantBuffer.cpp') 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 -- cgit v1.2.3