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