diff --git a/swCommonLib/Common/Buffers/BufferRaw.h b/swCommonLib/Common/Buffers/BufferRaw.h index 8853b4e5..4d741c27 100644 --- a/swCommonLib/Common/Buffers/BufferRaw.h +++ b/swCommonLib/Common/Buffers/BufferRaw.h @@ -7,6 +7,7 @@ #include "swCommonLib/Common/TypesDefinitions.h" #include "swCommonLib/Common/RTTR.h" +#include "swCommonLib/Common/Buffers/BufferRange.h" #include @@ -52,6 +53,8 @@ class BufferRaw const uint8* GetData () const { return reinterpret_cast< const uint8* >( m_data ); } bool IsValid () const { return m_data != nullptr; } + BufferRange AsRange () const { return BufferRange( m_data, m_size ); } + public: template< typename ContentType, class Aloc > friend class BufferTyped; diff --git a/swGUI/Core/System/Rendering/Drawings/Drawing.cpp b/swGUI/Core/System/Rendering/Drawings/Drawing.cpp index 879ed595..dc9d9d26 100644 --- a/swGUI/Core/System/Rendering/Drawings/Drawing.cpp +++ b/swGUI/Core/System/Rendering/Drawings/Drawing.cpp @@ -215,15 +215,44 @@ ReturnResult Drawing::UpdateGeometry ( ResourceManagerAPI rm, Geometry* geo ReturnIfInvalid( result ); GeometryData& data = result.Get(); - // Create new buffers if they didn't existed. - if( !vertexBuffer || !indexBuffer ) + // Create new buffers if they didn't exist. + if( !vertexBuffer ) + { + auto vertexSize = data.VertexBuffer.GetType().get_sizeof(); + auto result = rm.CreateVertexBuffer( vbName, data.VertexBuffer, (uint32)vertexSize ); + ReturnIfInvalid( result ); + + vertexBuffer = result.Get(); + } + else if( data.VertexBuffer.GetSize() > vertexBuffer->GetBytesSize() ) + { + ReturnIfInvalid( vertexBuffer->Resize( data.VertexBuffer.AsRange() ) ); + } + else + { + // Buffer is large enough for the new data. + // Note that we never resize buffer to smaller size. Maybe we should? + ReturnIfInvalid( vertexBuffer->UpdateData( data.VertexBuffer.AsRange(), 0 ) ); + } + + if( !indexBuffer ) { auto indexSize = data.ExtendedIB ? sizeof( Index32 ) : sizeof( Index16 ); - auto vertexSize = data.VertexBuffer.GetType().get_sizeof(); + auto result = rm.CreateIndexBuffer( ibName, data.IndexBuffer, (uint32)indexSize ); + ReturnIfInvalid( result ); - vertexBuffer = rm.CreateVertexBuffer( vbName, data.VertexBuffer, (uint32)vertexSize ).Get(); /// @todo What in case of error? - indexBuffer = rm.CreateIndexBuffer( ibName, data.IndexBuffer, (uint32)indexSize ).Get(); /// @todo What in case of error? + indexBuffer = result.Get(); } + else if( data.IndexBuffer.GetSize() > indexBuffer->GetBytesSize() ) + { + ReturnIfInvalid( indexBuffer->Resize( data.IndexBuffer.AsRange() ) ); + } + else + { + // Buffer is large enough for the new data. + // Note that we never resize buffer to smaller size. Maybe we should? + ReturnIfInvalid( indexBuffer->UpdateData( data.IndexBuffer.AsRange(), 0 ) ); + } m_geometryData.VertexBuffer = vertexBuffer; m_geometryData.IndexBuffer = indexBuffer; diff --git a/swGUI/Prototypes/TextShowcase/Application.cpp b/swGUI/Prototypes/TextShowcase/Application.cpp index 4d4fe1c4..e7e8dcdd 100644 --- a/swGUI/Prototypes/TextShowcase/Application.cpp +++ b/swGUI/Prototypes/TextShowcase/Application.cpp @@ -91,7 +91,7 @@ void Application::AddControls ( HostWindow* host ) { auto pen = std::make_shared< SolidColorBrush >( Colors::BurlyWood ); auto background = std::make_shared< SolidColorBrush >( Colors::Transparent ); - auto textBlock = AddText( host, background, pen, 300, 100, Position( 350, 350 ), L"" ); + auto textBlock = AddText( host, background, pen, 400, 100, Position( 350, 350 ), L"" ); textBlock->SetTextAlignment( sw::TextAlignment::Justify ); textBlock->SetFontSize( 60 ); textBlock->SetDataContext( &m_viewModel ); diff --git a/swGraphicAPI/Resources/Buffers/Buffer.cpp b/swGraphicAPI/Resources/Buffers/Buffer.cpp index 1864d534..6b222526 100644 --- a/swGraphicAPI/Resources/Buffers/Buffer.cpp +++ b/swGraphicAPI/Resources/Buffers/Buffer.cpp @@ -39,7 +39,7 @@ namespace sw // ================================ // // -Buffer::Buffer( const AssetPath& assetPath, unsigned int elementSize, unsigned int elementCount ) +Buffer::Buffer( const AssetPath& assetPath, uint32 elementSize, uint32 elementCount ) : IBuffer( assetPath ) , m_elementSize( elementSize ) , m_elementCount( elementCount ) diff --git a/swGraphicAPI/Resources/Buffers/Buffer.h b/swGraphicAPI/Resources/Buffers/Buffer.h index 0282c561..c3366ea4 100644 --- a/swGraphicAPI/Resources/Buffers/Buffer.h +++ b/swGraphicAPI/Resources/Buffers/Buffer.h @@ -27,17 +27,18 @@ class Buffer : public IBuffer RTTR_ENABLE( IBuffer ) protected: - unsigned int m_elementSize; ///< Element size. - unsigned int m_elementCount; ///< Number of elements. + uint32 m_elementSize; ///< Element size. + uint32 m_elementCount; ///< Number of elements. virtual ~Buffer () = default; public: - explicit Buffer ( const AssetPath& assetPath, unsigned int elementSize, unsigned int elementCount ); + explicit Buffer ( const AssetPath& assetPath, uint32 elementSize, uint32 elementCount ); - inline unsigned int GetStride () const { return m_elementSize; } - inline unsigned int GetElementSize () const { return m_elementSize; } - inline unsigned int GetElementCount () const { return m_elementCount; } + inline uint32 GetStride () const { return m_elementSize; } + inline uint32 GetElementSize () const { return m_elementSize; } + inline uint32 GetElementCount () const { return m_elementCount; } + inline Size GetBytesSize () const { return m_elementCount * m_elementSize; } }; DEFINE_RESOURCE_PTR_TYPE( Buffer );