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 
32  // Possible input slots identifiers
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 
47  NUM_INPUT_SLOTS,
48 
49  DRAW_CALL_INFO = 15, // Reserve last input slot for draw call infos
50  };
51 
52  typedef uint8 Slot;
53 
54  static const std::array<Element, InputSlot::NUM_INPUT_SLOTS>& getDefaultElements();
55 
56  // Frequency describer
57  enum Frequency {
58  PER_VERTEX = 0,
59  PER_INSTANCE = 1,
60  };
61 
62  // The attribute description
63  // Every thing that is needed to detail a stream attribute and how to interpret it
64  class Attribute {
65  public:
66  Attribute() {}
67 
68  Attribute(Slot slot, Slot channel, Element element, Offset offset = 0, Frequency frequency = PER_VERTEX) :
69  _slot(slot),
70  _channel(channel),
71  _element(element),
72  _offset(offset),
73  _frequency(frequency)
74  {}
75 
76  Slot _slot{ POSITION }; // Logical slot assigned to the attribute
77  Slot _channel{ POSITION }; // index of the channel where to get the data from
78  Element _element{ Element::VEC3F_XYZ };
79  Offset _offset{ 0 };
80  uint32 _frequency{ PER_VERTEX };
81 
82  // Size of the
83  uint32 getSize() const { return _element.getSize(); }
84 
85  // Generate a string key describing the attribute uniquely
86  std::string getKey() const;
87  };
88 
89  // Stream Format is describing how to feed a list of attributes from a bunch of stream buffer channels
90  class Format {
91  public:
92  typedef std::map< Slot, Attribute > AttributeMap;
93 
94  class ChannelInfo {
95  public:
96  std::vector< Slot > _slots;
97  std::vector< Offset > _offsets;
98  Offset _stride;
99  uint32 _netSize;
100 
101  ChannelInfo() : _stride(0), _netSize(0) {}
102  };
103  typedef std::map< Slot, ChannelInfo > ChannelMap;
104 
105  size_t getNumAttributes() const { return _attributes.size(); }
106  const AttributeMap& getAttributes() const { return _attributes; }
107 
108  size_t getNumChannels() const { return _channels.size(); }
109  const ChannelMap& getChannels() const { return _channels; }
110  Offset getChannelStride(Slot channel) const { return _channels.at(channel)._stride; }
111 
112  size_t getElementTotalSize() const { return _elementTotalSize; }
113 
114  bool setAttribute(Slot slot, Slot channel, Element element, Offset offset = 0, Frequency frequency = PER_VERTEX);
115  bool setAttribute(Slot slot, Frequency frequency = PER_VERTEX);
116  bool setAttribute(Slot slot, Slot channel, Frequency frequency = PER_VERTEX);
117 
118  bool hasAttribute(Slot slot) const { return (_attributes.find(slot) != _attributes.end()); }
119  Attribute getAttribute(Slot slot) const;
120 
121  const std::string& getKey() const { return _key; }
122 
123  const GPUObjectPointer gpuObject{};
124 
125  protected:
126  AttributeMap _attributes;
127  ChannelMap _channels;
128  uint32 _elementTotalSize { 0 };
129  std::string _key;
130 
131  friend class Serializer;
132  friend class Deserializer;
133  void evaluateCache();
134  };
135 
136  typedef std::shared_ptr<Format> FormatPointer;
137 };
138 
139 typedef std::vector< Offset > Offsets;
140 
141 // Buffer Stream is a container of N Buffers and their respective Offsets and Srides representing N consecutive channels.
142 // A Buffer Stream can be assigned to the Batch to set several stream channels in one call
143 class BufferStream {
144 public:
145  using Strides = Offsets;
146 
147  void clear() { _buffers.clear(); _offsets.clear(); _strides.clear(); }
148  void addBuffer(const BufferPointer& buffer, Offset offset, Offset stride);
149 
150  const Buffers& getBuffers() const { return _buffers; }
151  const Offsets& getOffsets() const { return _offsets; }
152  const Strides& getStrides() const { return _strides; }
153  size_t getNumBuffers() const { return _buffers.size(); }
154 
155  BufferStream makeRangedStream(uint32 offset, uint32 count = -1) const;
156 
157  BufferStream& operator = (const BufferStream& src) = default;
158 
159 protected:
160  Buffers _buffers;
161  Offsets _offsets;
162  Strides _strides;
163 };
164 typedef std::shared_ptr<BufferStream> BufferStreamPointer;
165 
166 };
167 
168 
169 #endif