Pioneer
UniformBuffer.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #pragma once
5 
6 #include "graphics/Types.h"
9 
10 #include <memory>
11 
12 namespace Graphics {
13 
14  namespace OGL {
15 
17  public:
18  UniformBuffer(uint32_t size, BufferUsage usage);
19  ~UniformBuffer() override;
20 
21  void Unmap() override;
22 
23  // change the buffer data without mapping
24  void BufferData(const size_t, void *) override;
25 
26  void BindRange(uint32_t binding, uint32_t offset, uint32_t range);
27  void Release();
28 
29  protected:
30  void *MapInternal(BufferMapMode mode) override;
31  };
32 
33  /*
34  Implements a linear-allocator style uniform buffer binding.
35  Call Allocate to reserve and map a subrange of the buffer for code to write into once.
36  All allocations are reset at the start of the next frame.
37  TODO: there should be a better interface for attaching linear allocators to generic buffers.
38  */
40  public:
41  UniformLinearBuffer(uint32_t maxSize);
42  ~UniformLinearBuffer() override;
43 
44  // Don't copy a buffer
47 
48  // public for ScopedMapping
49  void Unmap() override;
50 
51  // Flushes all written data so far to the uniform buffer.
52  // Call this once before executing a command list to ensure data
53  // is made visible to the GPU
54  void Flush();
55 
56  // Resets all allocations to the buffer and orphans the previous data,
57  // making it ready for a new frame.
58  void Reset();
59 
60  uint32_t FreeSize() const { return m_capacity - m_size; }
61  uint32_t NumAllocs() const { return m_numAllocs; }
62 
63  template <typename T>
65  {
66  assert(m_mapMode == BUFFER_MAP_NONE);
67  return ScopedMapping<T>(AllocInternal(sizeof(T), outBinding), this);
68  }
69 
70  BufferBinding<UniformBuffer> Allocate(void *data, size_t size);
71 
72  protected:
73  // protect some methods we don't want to allow public access to
75  using UniformBuffer::Map;
76 
77  void *AllocInternal(size_t size, BufferBinding<UniformBuffer> &outBinding);
78 
79  // cache individual allocations into a single buffer and upload to
80  // the GPU in one large chunk.
81  std::unique_ptr<char[]> m_data;
82 
83  // This tracks the end of the last section of flushed data so
84  // we can use the same allocator with multiple command lists
85  uint32_t m_lastFlush;
86  uint32_t m_numAllocs;
87  };
88 
89  } // namespace OGL
90 
91 } // namespace Graphics
uint32_t m_capacity
Definition: BufferCommon.h:34
uint32_t m_size
Definition: BufferCommon.h:32
BufferMapMode m_mapMode
Definition: BufferCommon.h:29
Definition: GLBufferBase.h:12
Definition: UniformBuffer.h:16
void Unmap() override
Definition: UniformBuffer.cpp:30
void * MapInternal(BufferMapMode mode) override
Definition: UniformBuffer.cpp:56
UniformBuffer(uint32_t size, BufferUsage usage)
Definition: UniformBuffer.cpp:15
void Release()
Definition: UniformBuffer.cpp:51
void BufferData(const size_t, void *) override
Definition: UniformBuffer.cpp:38
void BindRange(uint32_t binding, uint32_t offset, uint32_t range)
Definition: UniformBuffer.cpp:46
~UniformBuffer() override
Definition: UniformBuffer.cpp:24
Definition: UniformBuffer.h:39
uint32_t NumAllocs() const
Definition: UniformBuffer.h:61
void Unmap() override
Definition: UniformBuffer.cpp:161
uint32_t m_lastFlush
Definition: UniformBuffer.h:85
void * AllocInternal(size_t size, BufferBinding< UniformBuffer > &outBinding)
Definition: UniformBuffer.cpp:143
void Flush()
Definition: UniformBuffer.cpp:99
UniformLinearBuffer(const UniformLinearBuffer &)=delete
void Reset()
Definition: UniformBuffer.cpp:89
std::unique_ptr< char[]> m_data
Definition: UniformBuffer.h:81
ScopedMapping< T > Allocate(BufferBinding< UniformBuffer > &outBinding)
Definition: UniformBuffer.h:64
uint32_t FreeSize() const
Definition: UniformBuffer.h:60
uint32_t m_numAllocs
Definition: UniformBuffer.h:86
~UniformLinearBuffer() override
Definition: UniformBuffer.cpp:82
UniformLinearBuffer & operator=(const UniformLinearBuffer &)=delete
UniformLinearBuffer(uint32_t maxSize)
Definition: UniformBuffer.cpp:69
Definition: UniformBuffer.h:11
ScopedMapping< T > Map(BufferMapMode mode)
Definition: UniformBuffer.h:20
Definition: Background.h:14
BufferUsage
Definition: Types.h:65
BufferMapMode
Definition: Types.h:70
@ BUFFER_MAP_NONE
Definition: Types.h:71
Definition: BufferCommon.h:63
Definition: BufferCommon.h:38