MeshLib
 
Loading...
Searching...
No Matches
MRRenderHelpers.h
Go to the documentation of this file.
1#pragma once
2#include "exports.h"
3#include "MRMesh/MRMeshFwd.h"
4#include "MRMesh/MRBuffer.h"
5#include "MRMesh/MRVector2.h"
6
7namespace MR
8{
9
10// calc texture resolution, to fit MAX_TEXTURE_SIZE, and have minimal empty pixels
11MRVIEWER_API Vector2i calcTextureRes( int bufferSize, int maxTextWidth );
12
13// provides access to shared buffer with type casting
14template <typename T>
16{
17public:
18 RenderBufferRef() = default;
19
20 RenderBufferRef( T* data, std::size_t glSize, bool dirty )
21 : data_( data )
22 , glSize_( glSize )
23 , dirty_( dirty )
24 {
25 if ( data_ == nullptr )
26 assert( !dirty_ );
27 }
28
29 // returns reference to i-th element
30 T& operator []( std::size_t i ) const noexcept
31 {
32 assert( dirty_ );
33 return data_[i];
34 }
35 // returns pointer to buffer data
36 T* data() const noexcept
37 {
38 return data_;
39 }
40 // returns actual buffer size
41 [[nodiscard]] std::size_t size() const noexcept
42 {
43 return dirty_ ? glSize_ : 0;
44 }
45 // returns number of elements that are about to be loaded or already loaded to GL memory
46 [[nodiscard]] std::size_t glSize() const noexcept
47 {
48 return glSize_;
49 }
50 // returns true if associated data were updated
51 [[nodiscard]] bool dirty() const noexcept
52 {
53 return dirty_;
54 }
55
56private:
57 T* data_{ nullptr };
58 std::size_t glSize_{ 0 };
59 bool dirty_{ false };
60};
61
62// provides shared buffer for loading different types of data to GL memory
63class MRVIEWER_CLASS RenderObjectBuffer
64{
65public:
66 template <typename T>
67 RenderBufferRef<T> prepareBuffer( std::size_t glSize, bool dirty = true )
68 {
69 if ( dirty )
70 {
71 auto memSize = sizeof( T ) * glSize;
72 if ( buffer_.size() < memSize )
73 buffer_.resize( memSize );
74 }
75 return { reinterpret_cast<T*>( buffer_.data() ), glSize, dirty };
76 }
77
78 size_t heapBytes() const { return buffer_.heapBytes(); }
79
80private:
81 Buffer<std::byte> buffer_;
82};
83
84}
std::vector<V>-like container that is 1) resized without initialization of its elements,...
Definition MRBuffer.h:54
Definition MRRenderHelpers.h:16
std::size_t size() const noexcept
Definition MRRenderHelpers.h:41
T * data() const noexcept
Definition MRRenderHelpers.h:36
RenderBufferRef()=default
RenderBufferRef(T *data, std::size_t glSize, bool dirty)
Definition MRRenderHelpers.h:20
std::size_t glSize() const noexcept
Definition MRRenderHelpers.h:46
bool dirty() const noexcept
Definition MRRenderHelpers.h:51
T & operator[](std::size_t i) const noexcept
Definition MRRenderHelpers.h:30
Definition MRRenderHelpers.h:64
size_t heapBytes() const
Definition MRRenderHelpers.h:78
RenderBufferRef< T > prepareBuffer(std::size_t glSize, bool dirty=true)
Definition MRRenderHelpers.h:67
Definition MRCameraOrientationPlugin.h:7
MRVIEWER_API Vector2i calcTextureRes(int bufferSize, int maxTextWidth)