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