HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_HotSwapPoolEntry.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  * Base class for things that can be tracked by the CE_HotSwapPool
8  */
9 
10 #pragma once
11 
12 #include "CE_API.h"
13 
14 #include <UT/UT_SharedPtr.h>
15 
16 /// Base class for objects that can be managed by the
17 /// CE_HotSwapPool
18 /// This handles the registration / unloading /etc of the GPU buffers.
19 
21 {
22 public:
23  /// To avoid the need for dynamic casts:
24  enum class EntryType
25  {
26  BUFFER = 0,
27  VDB,
28  ATTRIBUTE
29  };
30 
31  /// Creates a new buffer. You must call setSize to make this buffer usable.
33  virtual ~CE_HotSwapPoolEntry() {}
34  CE_HotSwapPoolEntry(const CE_HotSwapPoolEntry &) = delete;
36 
37  virtual EntryType poolTypeID() const = 0;
38 
39  /// Returns whether the GPU data for this image is currently being used by
40  /// someone.
41  bool isInUseGPU() const
42  {
43  return myGPUInUseCount > 0;
44  }
45  /// Increments the user count of our GPU data. If there are no active users
46  /// of the GPU data, the memory pool will be able to unload the buffer.
47  /// When using one of the getGPUBuffer() methods, the in-use GPU flag must
48  /// be set to prevent the GPU data from getting unloaded:
49  /// // Set the in-use flag before grabbing the GPU buffer.
50  /// buffer.setInUseGPUFlag();
51  /// CE_Image& img = buffer.getGPUBufferW();
52  /// // Do stuff with the CE_Image (i.e. queue up an OpenCL command with
53  /// // it, can be asynchronous).
54  /// ...
55  /// // Flip the flag that we set earlier.
56  /// buffer.clearInUseGPUFlag();
57  /// Alternatively, you can use the RAII wrapper for this:
58  /// {
59  /// // Set the in-use flag before grabbing the GPU buffer.
60  /// CE_ScopedBufferInUseGPU scoped_gpu_use(buffer);
61  /// CE_Image& img = buffer.getGPUBufferW();
62  /// // Do stuff with the CE_Image (i.e. queue up an OpenCL command
63  /// // with it, can be asynchronous).
64  /// ...
65  /// // Destructor automatically clears the flag that we set earlier.
66  /// }
67  void setInUseGPUFlag() const;
68  /// Decrements the user count of our GPU data. If there are no active users
69  /// of the GPU data, the memory pool will be able to unload this buffer.
70  /// See setInUseGPUFlag() documentation for how these methods must be used.
71  /// This method also refreshes the buffer in the eyes of the memory pool,
72  /// making it less likely to get evicted.
73  void clearInUseGPUFlag() const;
74 
75 protected:
76  /// Queues up commands that transfer this buffer's GPU storage to main
77  /// memory. TODO: this is non-blocking, make sure that's okay...
78  virtual void unloadFromGPU() = 0;
79 
80  /// Resets the GPU Buffer pointer.
81  virtual void poolResetGPUBuffer() = 0;
82  /// CHnages the OnGPU status.
83  virtual void poolSetOnGPU(bool ongpu) = 0;
84  /// True if the gpu buffer is null.
85  virtual bool poolIsGPUBufferEmpty() const = 0;
86  /// True if the gpu buffer is not null and valid.
87  virtual bool poolIsGPUBufferValid() const = 0;
88  /// Swap two GPU buffers. Returns true if it did something.
89  virtual bool poolSwapGPUBuffer(CE_HotSwapPoolEntry *other) = 0;
90  /// Copies GPU buffer from source, shallow copy so it shares.
91  virtual void poolShallowCopyGPUBuffer(const CE_HotSwapPoolEntry *src) = 0;
92 
93 private:
94  /// Number of current users of the GPU memory from this buffer. All changes
95  /// to this member must be done by the memory pool!!!
96  mutable int myGPUInUseCount = 0;
97 
98  friend class CE_HotSwapPool;
99 };
100 
102 {
103 public:
105  : myBuffer(buffer)
106  { myBuffer.setInUseGPUFlag(); }
107 
109  { myBuffer.clearInUseGPUFlag(); }
110 
111 private:
112  const CE_HotSwapPoolEntry &myBuffer;
113 };
114 
#define CE_API
Definition: CE_API.h:13
void clearInUseGPUFlag() const
~CE_ScopedPoolEntryInUseGPU()
CE_ScopedPoolEntryInUseGPU(const CE_HotSwapPoolEntry &buffer)
CE_HotSwapPoolEntry()
Creates a new buffer. You must call setSize to make this buffer usable.
GLuint buffer
Definition: glcorearb.h:660
void setInUseGPUFlag() const
EntryType
To avoid the need for dynamic casts:
LeafData & operator=(const LeafData &)=delete
virtual ~CE_HotSwapPoolEntry()
bool isInUseGPU() const
GLenum src
Definition: glcorearb.h:1793