Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bufferobject.cc
Go to the documentation of this file.
1 
18 #include "ion/gfx/bufferobject.h"
19 
20 #include "ion/base/enumhelper.h"
21 #include "ion/base/invalid.h"
22 #include "ion/base/logging.h"
23 #include "ion/portgfx/glheaders.h"
24 
25 namespace ion {
26 namespace gfx {
27 
29  : specs_(*this),
30  data_(kDataChanged, BufferData(), this),
31  target_(kArrayBuffer),
32  sub_data_(*this),
33  sub_data_changed_(kSubDataChanged, false, this) {}
34 
36  : specs_(*this),
37  data_(kDataChanged, BufferData(), this),
38  target_(target),
39  sub_data_(*this),
40  sub_data_changed_(kSubDataChanged, false, this) {}
41 
43  if (base::DataContainer* data = GetData().Get()) data->RemoveReceiver(this);
44 }
45 
47  const size_t component_count,
48  const size_t byte_offset) {
49  if (component_count > 4U) {
50  LOG(ERROR) << "***ION: Elements must have no more than four components.";
51  return base::kInvalidIndex;
52  } else {
53  Spec spec(type, component_count, byte_offset);
54  size_t index = specs_.size();
56  for (size_t i = 0; i < index; ++i) {
57  if (spec == specs_[i]) {
58  index = i;
59  break;
60  }
61  }
63  if (index == specs_.size())
64  specs_.push_back(spec);
65  return index;
66  }
67 }
68 
70  const size_t element_index) const {
71  if (element_index >= specs_.size()) {
72  LOG(ERROR) << "***ION: Invalid element index " << element_index
73  << " passed to BufferObject with " << specs_.size()
74  << " elements.";
75  return base::InvalidReference<BufferObject::Spec>();
76  } else {
77  return specs_[element_index];
78  }
79 }
80 
81 void BufferObject::OnNotify(const base::Notifier* notifier) {
82  if (GetResourceCount()) {
83  if (notifier == GetData().Get()) {
85  Notify();
86  }
87  }
88 }
89 
96 
97 } // namespace gfx
98 
99 namespace base {
100 
101 using gfx::BufferObject;
102 
104 template <> ION_API const EnumHelper::EnumData<BufferObject::ComponentType>
105 EnumHelper::GetEnumData() {
106  static const GLenum kValues[] = {
107  GL_INVALID_ENUM, GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT,
108  GL_INT, GL_UNSIGNED_INT, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT
109  };
110  static const char* kStrings[] = {
111  "Invalid", "Byte", "Unsigned Byte", "Short", "Unsigned Short", "Int",
112  "Unsigned Int", "Float", "Float Matrix Column 2", "Float Matrix Column 3",
113  "Float Matrix Column 4"};
114  ION_STATIC_ASSERT(ARRAYSIZE(kValues) == ARRAYSIZE(kStrings),
115  "EnumHelper size mismatch");
116  return EnumData<BufferObject::ComponentType>(
118  ARRAYSIZE(kValues)),
119  kStrings);
120 }
121 
122 template <> ION_API const EnumHelper::EnumData<BufferObject::Target>
123 EnumHelper::GetEnumData() {
124  static const GLenum kValues[] = { GL_ARRAY_BUFFER,
125  GL_ELEMENT_ARRAY_BUFFER,
126  GL_COPY_READ_BUFFER,
127  GL_COPY_WRITE_BUFFER
128  };
129  static const char* kStrings[] = {
130  "ArrayBuffer",
131  "Elementbuffer",
132  "CopyReadBuffer",
133  "CopyWriteBuffer"
134  };
135  ION_STATIC_ASSERT(ARRAYSIZE(kValues) == ARRAYSIZE(kStrings),
136  "EnumHelper size mismatch");
137  return EnumData<BufferObject::Target>(
138  base::IndexMap<BufferObject::Target, GLenum>(kValues, ARRAYSIZE(kValues)),
139  kStrings);
140 }
141 
143 template <> ION_API const EnumHelper::EnumData<BufferObject::UsageMode>
144 EnumHelper::GetEnumData() {
145  static const GLenum kValues[] = {
146  GL_DYNAMIC_DRAW, GL_STATIC_DRAW, GL_STREAM_DRAW
147  };
148  static const char* kStrings[] = { "DynamicDraw", "StaticDraw", "StreamDraw" };
149  ION_STATIC_ASSERT(ARRAYSIZE(kValues) == ARRAYSIZE(kStrings),
150  "EnumHelper size mismatch");
151  return EnumData<BufferObject::UsageMode>(
153  ARRAYSIZE(kValues)),
154  kStrings);
155 }
156 
157 } // namespace base
158 
159 } // namespace ion
std::string type
Definition: printer.cc:353
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
size_t AddSpec(const ComponentType type, const size_t component_count, const size_t byte_offset)
Describes an element of an arbitrary datatype to the BufferObject.
Definition: bufferobject.cc:46
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
A Notifier both sends notifications to and receives notifications from other Notifiers.
Definition: notifier.h:35
~BufferSubData()
See comment in .cc file.
Definition: bufferobject.cc:95
void OnChanged(int bit) const
Forwards OnChanged to all resources.
void Notify() const
Notifies all contained Notifiers by calling their OnNotify().
Definition: notifier.cc:81
The DataContainer class encapsulates arbitrary user data passed to Ion.
Definition: datacontainer.h:74
This template class can be used to map between two kinds of indices when the following assumptions ap...
Definition: indexmap.h:40
A BufferObject describes a generic array of data used, for example, to describe the vertices in a Sha...
Definition: bufferobject.h:67
const base::DataContainerPtr & GetData() const
Gets the data container.
Definition: bufferobject.h:205
ComponentType
The type of the components of a spec.
Definition: bufferobject.h:77
Copyright 2016 Google Inc.
~BufferObject() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
Definition: bufferobject.cc:42
BufferObject()
Creates a BufferObject of type kArrayBuffer.
Definition: bufferobject.cc:28
int GetResourceCount() const
Returns the number of resources that this holder holds.
#define ION_STATIC_ASSERT(expr, message)
Copyright 2016 Google Inc.
Definition: static_assert.h:35
const Spec & GetSpec(const size_t spec_index) const
Gets the Spec at index spec_index.
Definition: bufferobject.cc:69