HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderBufferPool.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_RENDER_BUFFER_POOL_H
8 #define PXR_IMAGING_HD_ST_RENDER_BUFFER_POOL_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/gf/vec3i.h"
12 #include "pxr/imaging/hdSt/api.h"
14 #include "pxr/imaging/hgi/hgi.h"
15 #include "pxr/imaging/hgi/enums.h"
17 
18 #include <deque>
19 #include <map>
20 #include <unordered_map>
21 
23 
25 
26 /// \class HdStPooledRenderBuffer
27 ///
28 /// Represents a Render Buffer that may be re-used between render graphs
29 /// Client code has exclusive access to this buffer during render graph
30 /// execution, but there are no guarentees about contents before first usage
31 /// and after last usage.
32 ///
34 {
35 public:
37  std::shared_ptr<HdStRenderBuffer> buffer,
39  const SdfPath& graphPath,
40  bool isDepth,
41  uint16_t idx);
43 
44  HdStRenderBuffer* GetBuffer() const { return _buffer.get(); }
45 private:
46  std::shared_ptr<HdStRenderBuffer> _buffer;
47  HdStRenderBufferPool* _pool;
48  const SdfPath _graphPath;
49  bool _isDepth;
50  uint16_t _idx;
51 
52  HdStPooledRenderBuffer & operator=(const HdStPooledRenderBuffer&) = delete;
54 };
55 
56 using HdStPooledRenderBufferUniquePtr = std::unique_ptr<HdStPooledRenderBuffer>;
57 
58 /// \class HdStRenderBufferPool
59 ///
60 /// System for re-using HdStRenderBuffers between tasks in different graphs
61 /// that regenerate data per-frame. Ex: Shadow buffers
62 ///
64 {
65 public:
66  // Allocate a RenderBuffer for usage during the current render graph
67  [[nodiscard]]
68  HDST_API
70  HdStResourceRegistry* registry,
71  const SdfPath& graphPath,
72  HdFormat fmt,
73  GfVec2i dims,
74  bool multiSampled,
75  bool depth);
76 
77  HDST_API
78  void Free(
79  HdFormat fmt,
80  GfVec2i dims,
81  bool multiSampled,
82  bool depth,
83  const SdfPath& graphPath,
84  uint16_t idx);
85 
86  // Frees allocations that are no longer in use by any render graphs
87  HDST_API
88  void Commit();
89 private:
90  struct _PooledRenderBufferDesc
91  {
92  HdFormat fmt : 6;
93  bool multiSampled : 1;
94  bool depth : 1;
95  GfVec2i dims;
96  bool operator==(const _PooledRenderBufferDesc &other) const
97  {
98  return fmt == other.fmt &&
99  dims == other.dims &&
100  multiSampled == other.multiSampled &&
101  depth == other.depth;
102  }
103  struct HashFunctor
104  {
105  size_t operator()(_PooledRenderBufferDesc const& desc) const
106  {
107  return (((desc.multiSampled << 0) | (desc.depth << 2)
108  | (desc.fmt << 3))
109  ^ (desc.dims[0] & 0xFFFF))
110  | ((size_t)(desc.dims[1] & 0xFFFF) << 32);
111  }
112  };
113  };
114 
115  using _HdStRenderBufferVec =
116  std::vector<std::shared_ptr<HdStRenderBuffer>>;
117 
118  class _AllocationTracker
119  {
120  public:
121  uint16_t Allocate();
122  void Free(uint16_t index);
123  private:
124  uint16_t max = 0;
125  std::deque<uint16_t> freeList;
126  };
127 
128  struct _HdStRenderBufferPoolEntry
129  {
130  std::map<const SdfPath, _AllocationTracker> allocs;
131  _HdStRenderBufferVec buffers;
132  };
133 
134  std::unordered_map<_PooledRenderBufferDesc,
135  _HdStRenderBufferPoolEntry,
136  _PooledRenderBufferDesc::HashFunctor> _pooledRenderBuffers;
137 };
138 
140 
141 #endif
HdFormat
Definition: types.h:408
Definition: vec2i.h:43
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
size_t operator()(_PooledRenderBufferDesc const &desc) const
HdStRenderBuffer * GetBuffer() const
GLuint buffer
Definition: glcorearb.h:660
HDST_API HdStPooledRenderBufferUniquePtr Allocate(HdStResourceRegistry *registry, const SdfPath &graphPath, HdFormat fmt, GfVec2i dims, bool multiSampled, bool depth)
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
std::unique_ptr< HdStPooledRenderBuffer > HdStPooledRenderBufferUniquePtr
const GLuint * buffers
Definition: glcorearb.h:661
Definition: path.h:280
HdStPooledRenderBuffer(std::shared_ptr< HdStRenderBuffer > buffer, HdStRenderBufferPool *pool, const SdfPath &graphPath, bool isDepth, uint16_t idx)
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
HDST_API void Commit()
#define HDST_API
Definition: api.h:23
GLuint index
Definition: glcorearb.h:786
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
**Note that the tasks the is the thread number *for the pool
Definition: thread.h:646
HDST_API void Free(HdFormat fmt, GfVec2i dims, bool multiSampled, bool depth, const SdfPath &graphPath, uint16_t idx)