GteResource.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.1 (2016/09/24)
7 
8 #pragma once
9 
11 #include <vector>
12 
13 namespace gte
14 {
15 
17 {
18 public:
19  // Abstract base class.
20  virtual ~Resource();
21 protected:
22  // The default usage is GPU_R.
23  Resource(unsigned int numElements, size_t elementSize, bool createStorage = true);
24 
25 public:
26  // Create or destroy the system-memory storage associated with the
27  // resource. A resource does not necessarily require system memory
28  // if it is intended only to provide information for GPU-resource
29  // creation.
30  void CreateStorage();
31  void DestroyStorage();
32 
33  // Basic member access.
34  inline unsigned int GetNumElements() const;
35  inline unsigned int GetElementSize() const;
36  inline unsigned int GetNumBytes() const;
37 
38  // The resource usage. These control how the GPU versions are created.
39  // You must set the usage type before binding the resource to an engine.
40  enum GTE_IMPEXP Usage
41  {
42  IMMUTABLE, // D3D11_USAGE_IMMUTABLE (default)
43  DYNAMIC_UPDATE, // D3D11_USAGE_DYNAMIC
44  SHADER_OUTPUT // D3D11_USAGE_DEFAULT
45  };
46 
47  inline void SetUsage(Usage usage);
48  inline Usage GetUsage() const;
49 
50  // Internal staging buffer generation. These control creation of staging
51  // buffers to support copies between CPU, staging buffers, and GPU. You
52  // must set the copy type before binding the resource to an engine.
53  enum GTE_IMPEXP CopyType
54  {
55  COPY_NONE, // 0 (default)
56  COPY_CPU_TO_STAGING, // D3D11_CPU_ACCESS_WRITE
57  COPY_STAGING_TO_CPU, // D3D11_CPU_ACCESS_READ
58  COPY_BIDIRECTIONAL // D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ
59  };
60 
61  inline void SetCopyType(CopyType copyType);
62  inline CopyType GetCopyType() const;
63 
64  // Member access to the raw data of the resource. The ResetData call sets
65  // mData to the storage (or to nullptr if storage does not exist.) The
66  // caller of SetData is responsible for ensuring 'data' has at least
67  // mNumBytes elements. The template member functions are a convenience for
68  // accessing the raw data as a specified type.
69  inline void ResetData();
70  inline void SetData(char* data);
71  inline char const* GetData() const;
72  inline char* GetData();
73  template <typename T> inline T const* Get() const;
74  template <typename T> inline T* Get();
75 
76  // Specify a contiguous block of active elements in the resource. An
77  // element occupies mElementSize bytes. Treating the data array as an
78  // array of elements, mOffset is the index of the first active element;
79  // that is, the first element has address mData + mOffset * mElementSize.
80  // The offset must satisfy
81  // mOffset < mNumElements
82  // The number of active elements is mNumActiveElements. It must satisfy
83  // mNumActiveElements <= mNumElements - mOffset
84  // If you plan on modifying both mOffset and mNumActiveElements for the
85  // same object, call SetOffset() before SetNumActiveElements() because
86  // of the dependency of the num-active constraint on offset.
87  void SetOffset(unsigned int offset);
88  inline unsigned int GetOffset() const;
89  void SetNumActiveElements(unsigned int numActiveElements);
90  inline unsigned int GetNumActiveElements() const;
91  inline unsigned int GetNumActiveBytes() const;
92 
93 protected:
94  unsigned int mNumElements;
95  unsigned int mElementSize;
96  unsigned int mNumBytes;
97  Usage mUsage;
98  CopyType mCopyType;
99  unsigned int mOffset;
100  unsigned int mNumActiveElements;
101  std::vector<char> mStorage;
102  char* mData;
103 };
104 
105 
106 inline unsigned int Resource::GetNumElements() const
107 {
108  return mNumElements;
109 }
110 
111 inline unsigned int Resource::GetElementSize() const
112 {
113  return mElementSize;
114 }
115 
116 inline unsigned int Resource::GetNumBytes() const
117 {
118  return mNumBytes;
119 }
120 
121 inline void Resource::SetUsage(Usage usage)
122 {
123  mUsage = usage;
124 }
125 
126 inline Resource::Usage Resource::GetUsage() const
127 {
128  return mUsage;
129 }
130 
131 inline void Resource::SetCopyType(CopyType copyType)
132 {
133  mCopyType = copyType;
134 }
135 
136 inline Resource::CopyType Resource::GetCopyType() const
137 {
138  return mCopyType;
139 }
140 
141 inline void Resource::ResetData()
142 {
143  mData = (mStorage.size() > 0 ? mStorage.data() : nullptr);
144 }
145 
146 inline void Resource::SetData(char* data)
147 {
148  mData = data;
149 }
150 
151 inline char const* Resource::GetData() const
152 {
153  return mData;
154 }
155 
156 inline char* Resource::GetData()
157 {
158  return mData;
159 }
160 
161 template <typename T>
162 inline T const* Resource::Get() const
163 {
164  return reinterpret_cast<T const*>(mData);
165 }
166 
167 template <typename T>
168 inline T* Resource::Get()
169 {
170  return reinterpret_cast<T*>(mData);
171 }
172 
173 inline unsigned int Resource::GetOffset() const
174 {
175  return mOffset;
176 }
177 
178 inline unsigned int Resource::GetNumActiveElements() const
179 {
180  return mNumActiveElements;
181 }
182 
183 inline unsigned int Resource::GetNumActiveBytes() const
184 {
185  return mNumActiveElements*mElementSize;
186 }
187 
188 
189 }
DYNAMIC_UPDATE
Definition: GteResource.h:42
COPY_NONE
Definition: GteResource.h:55
unsigned int GetNumElements() const
Definition: GteResource.h:106
unsigned int mNumElements
Definition: GteResource.h:94
COPY_STAGING_TO_CPU
Definition: GteResource.h:55
void ResetData()
Definition: GteResource.h:141
unsigned int GetOffset() const
Definition: GteResource.h:173
IMMUTABLE
Definition: GteResource.h:42
void SetUsage(Usage usage)
Definition: GteResource.h:121
Usage GetUsage() const
Definition: GteResource.h:126
T const * Get() const
Definition: GteResource.h:162
unsigned int mNumBytes
Definition: GteResource.h:96
CopyType mCopyType
Definition: GteResource.h:98
unsigned int GetElementSize() const
Definition: GteResource.h:111
unsigned int mElementSize
Definition: GteResource.h:95
GLboolean * data
Definition: glcorearb.h:126
unsigned int GetNumBytes() const
Definition: GteResource.h:116
char const * GetData() const
Definition: GteResource.h:151
unsigned int GetNumActiveElements() const
Definition: GteResource.h:178
COPY_CPU_TO_STAGING
Definition: GteResource.h:55
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:659
std::vector< char > mStorage
Definition: GteResource.h:101
CopyType GetCopyType() const
Definition: GteResource.h:136
void SetCopyType(CopyType copyType)
Definition: GteResource.h:131
GLintptr offset
Definition: glcorearb.h:660
unsigned int mOffset
Definition: GteResource.h:99
void SetData(char *data)
Definition: GteResource.h:146
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63
unsigned int GetNumActiveBytes() const
Definition: GteResource.h:183
unsigned int mNumActiveElements
Definition: GteResource.h:100


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01