Overte C++ Documentation
Stream.h
1 //
2 // Stream.h
3 // interface/src/gpu
4 //
5 // Created by Sam Gateau on 10/29/2014.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_Stream_h
12 #define hifi_gpu_Stream_h
13 
14 #include <vector>
15 #include <map>
16 #include <array>
17 #include <string>
18 
19 #include <assert.h>
20 
21 #include "Resource.h"
22 #include "Format.h"
23 
24 namespace gpu {
25 
26 class Element;
27 
28 // Stream namespace class
29 class Stream {
30 public:
31 
33  enum InputSlot {
34  POSITION = 0,
35  NORMAL = 1,
36  COLOR = 2,
37  TEXCOORD0 = 3,
38  TEXCOORD = TEXCOORD0,
39  TANGENT = 4,
40  SKIN_CLUSTER_INDEX = 5,
41  SKIN_CLUSTER_WEIGHT = 6,
42  TEXCOORD1 = 7,
43  TEXCOORD2 = 8,
44  TEXCOORD3 = 9,
45  TEXCOORD4 = 10,
46  FADE1 = 7,
47  FADE2 = 8,
48  FADE3 = 9,
49  FADE4 = 10,
50  FADE5 = 11,
51  FADE6 = 12,
52  FADE7 = 13,
53 
54  NUM_INPUT_SLOTS,
55 
56  DRAW_CALL_INFO = 15, // Reserve last input slot for draw call infos
57  };
58 
59  typedef uint8 Slot;
60 
61  static const std::array<Element, InputSlot::NUM_INPUT_SLOTS>& getDefaultElements();
62 
64  enum Frequency {
65  PER_VERTEX = 0,
66  PER_INSTANCE = 1,
67  };
68 
71  class Attribute {
72  public:
73  Attribute() {}
74 
82  Attribute(Slot slot, Slot channel, Element element, Offset offset = 0, Frequency frequency = PER_VERTEX) :
83  _slot(slot),
84  _channel(channel),
85  _element(element),
86  _offset(offset),
87  _frequency(frequency)
88  {}
89 
90  Slot _slot{ POSITION }; // Logical slot assigned to the attribute
91  Slot _channel{ POSITION }; // index of the channel where to get the data from
92  Element _element{ Element::VEC3F_XYZ };
93  Offset _offset{ 0 };
94  uint32 _frequency{ PER_VERTEX };
95 
99  uint32 getSize() const { return _element.getSize(); }
100 
105  std::string getKey() const;
106  };
107 
109  class Format {
110  public:
111  typedef std::map< Slot, Attribute > AttributeMap;
112 
114  class ChannelInfo {
115  public:
116 
118  std::vector< Slot > _slots;
119  Offset _stride;
120 
122  uint32 _netSize;
123 
125  uint32 _frequency{ PER_VERTEX };
126 
127  ChannelInfo() : _stride(0), _netSize(0) {}
128  };
129  typedef std::map< Slot, ChannelInfo > ChannelMap;
130 
134  size_t getNumAttributes() const { return _attributes.size(); }
135 
139  const AttributeMap& getAttributes() const { return _attributes; }
140 
144  size_t getNumChannels() const { return _channels.size(); }
145 
149  const ChannelMap& getChannels() const { return _channels; }
150 
155  Offset getChannelStride(Slot channel) const { return _channels.at(channel)._stride; }
156 
160  size_t getElementTotalSize() const { return _elementTotalSize; }
161 
173  bool setAttribute(Slot slot, Slot channel, Element element, Offset offset = 0, Frequency frequency = PER_VERTEX);
174 
179  bool hasAttribute(Slot slot) const { return (_attributes.find(slot) != _attributes.end()); }
180 
186  Attribute getAttribute(Slot slot) const;
187 
191  const std::string& getKey() const { return _key; }
192 
195 
196  protected:
198  AttributeMap _attributes;
199 
201  ChannelMap _channels;
202 
204  uint32 _elementTotalSize { 0 };
205 
207  std::string _key;
208 
209  friend class Serializer;
210  friend class Deserializer;
211 
216  void evaluateCache();
217  };
218 
219  typedef std::shared_ptr<Format> FormatPointer;
220 };
221 
222 typedef std::vector< Offset > Offsets;
223 
227 public:
228  using Strides = Offsets;
229 
233  void clear() { _buffers.clear(); _offsets.clear(); _strides.clear(); }
234 
240  void addBuffer(const BufferPointer& buffer, Offset offset, Offset stride);
241 
245  const Buffers& getBuffers() const { return _buffers; }
246 
250  const Offsets& getOffsets() const { return _offsets; }
251 
255  const Strides& getStrides() const { return _strides; }
256 
260  size_t getNumBuffers() const { return _buffers.size(); }
261 
262  BufferStream& operator = (const BufferStream& src) = default;
263 
264 protected:
265  Buffers _buffers;
266  Offsets _offsets;
267  Strides _strides;
268 };
269 typedef std::shared_ptr<BufferStream> BufferStreamPointer;
270 
271 };
272 
273 
274 #endif
Definition: Stream.h:226
const Strides & getStrides() const
Definition: Stream.h:255
const Buffers & getBuffers() const
Definition: Stream.h:245
size_t getNumBuffers() const
Definition: Stream.h:260
const Offsets & getOffsets() const
Definition: Stream.h:250
void addBuffer(const BufferPointer &buffer, Offset offset, Offset stride)
Definition: Stream.cpp:97
void clear()
Definition: Stream.h:233
Definition: Format.h:295
uint32 getSize() const
Definition: Format.h:349
Definition: gpu/src/gpu/Forward.h:134
Definition: Stream.h:71
std::string getKey() const
Generate a string key describing the attribute uniquely.
Definition: Stream.cpp:50
uint32 getSize() const
Definition: Stream.h:99
Attribute(Slot slot, Slot channel, Element element, Offset offset=0, Frequency frequency=PER_VERTEX)
Definition: Stream.h:82
Contains information about single channel.
Definition: Stream.h:114
uint32 _frequency
How often current position is incremented.
Definition: Stream.h:125
uint32 _netSize
Size of all attributes in this channel.
Definition: Stream.h:122
std::vector< Slot > _slots
Which slots use this channel.
Definition: Stream.h:118
Stream Format is describing how to feed a list of attributes from a bunch of stream buffer channels.
Definition: Stream.h:109
ChannelMap _channels
Channels mapped to binding indices.
Definition: Stream.h:201
AttributeMap _attributes
Attributes mapped to slots.
Definition: Stream.h:198
size_t getNumAttributes() const
Definition: Stream.h:134
const GPUObjectPointer gpuObject
GPU backend-specific object representing format.
Definition: Stream.h:194
Attribute getAttribute(Slot slot) const
Definition: Stream.cpp:88
const std::string & getKey() const
Definition: Stream.h:191
uint32 _elementTotalSize
Sum of the size of all attributes in bytes.
Definition: Stream.h:204
Offset getChannelStride(Slot channel) const
Definition: Stream.h:155
size_t getElementTotalSize() const
Definition: Stream.h:160
void evaluateCache()
Definition: Stream.cpp:60
const AttributeMap & getAttributes() const
Definition: Stream.h:139
bool setAttribute(Slot slot, Slot channel, Element element, Offset offset=0, Frequency frequency=PER_VERTEX)
Sets attribute in a format.
Definition: Stream.cpp:82
const ChannelMap & getChannels() const
Definition: Stream.h:149
size_t getNumChannels() const
Definition: Stream.h:144
std::string _key
String uniquely describing a format. Automatically updated when adding attributes.
Definition: Stream.h:207
bool hasAttribute(Slot slot) const
Definition: Stream.h:179