GteResource.cpp
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/11/28)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
10 #include <Graphics/GteResource.h>
11 using namespace gte;
12 
13 
15 {
17 }
18 
19 Resource::Resource(unsigned int numElements, size_t elementSize,
20  bool createStorage)
21  :
22  mNumElements(numElements),
23  mElementSize(static_cast<unsigned int>(elementSize)),
26  mOffset(0),
27  mData(nullptr)
28 {
30 
31  if (mNumElements > 0)
32  {
33  if (mElementSize > 0)
34  {
37  if (createStorage)
38  {
39  CreateStorage();
40  }
41  }
42  else
43  {
44  // The VertexBuffer constructor that takes only the number of
45  // vertices has been called. The vertex shader code is maintained
46  // completely in the HLSL.
47  mNumBytes = 0;
49  }
50  }
51  else
52  {
53  // No assertion may occur here. The VertexBuffer constructor with
54  // a VertexFormat of zero attributes (used for SV_VertexID-based
55  // drawing) and the IndexBuffer constructor for which no indices are
56  // provided will lead to this path.
57  mNumBytes = 0;
58  mElementSize = 0;
60  }
61 }
62 
64 {
65  if (mStorage.size() == 0)
66  {
67  mStorage.resize(mNumBytes);
68  if (!mData)
69  {
70  mData = mStorage.data();
71  }
72  }
73 }
74 
76 {
77  // The intent of DestroyStorage is to free up CPU memory that is not
78  // required when the resource GPU memory is all that is required.
79  // The 'clear' call sets the size to 0, but the capacity remains the
80  // same; that is, the memory is not freed. The 'shrink_to_fit' call
81  // is required to free the memory.
82  if (mStorage.size() > 0 && mData == mStorage.data())
83  {
84  mData = nullptr;
85  mStorage.clear();
86  mStorage.shrink_to_fit();
87  }
88 }
89 
90 void Resource::SetOffset(unsigned int offset)
91 {
92  if (offset < mNumElements)
93  {
94  mOffset = offset;
95  }
96  else
97  {
98  LogWarning("Invalid offset.");
99  mOffset = 0;
100  }
101 }
102 
103 void Resource::SetNumActiveElements(unsigned int numActiveElements)
104 {
105  if (numActiveElements + mOffset <= mNumElements)
106  {
107  mNumActiveElements = numActiveElements;
108  }
109  else
110  {
111  LogWarning("Invalid number of active elements.");
112  mNumActiveElements = static_cast<unsigned int>(mNumElements - mOffset);
113  }
114 }
115 
COPY_NONE
Definition: GteResource.h:55
unsigned int mNumElements
Definition: GteResource.h:94
Resource(unsigned int numElements, size_t elementSize, bool createStorage=true)
Definition: GteResource.cpp:19
IMMUTABLE
Definition: GteResource.h:42
GraphicsObjectType mType
GT_RESOURCE
void CreateStorage()
Definition: GteResource.cpp:63
unsigned int mNumBytes
Definition: GteResource.h:96
virtual ~Resource()
Definition: GteResource.cpp:14
CopyType mCopyType
Definition: GteResource.h:98
unsigned int mElementSize
Definition: GteResource.h:95
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void DestroyStorage()
Definition: GteResource.cpp:75
#define LogWarning(message)
Definition: GteLogger.h:95
void SetOffset(unsigned int offset)
Definition: GteResource.cpp:90
std::vector< char > mStorage
Definition: GteResource.h:101
void SetNumActiveElements(unsigned int numActiveElements)
GLintptr offset
Definition: glcorearb.h:660
unsigned int mOffset
Definition: GteResource.h:99
unsigned int mNumActiveElements
Definition: GteResource.h:100


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