Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
VertexBuffer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_VERTEX_BUFFER_OBJECT_H
34#define DVD_VERTEX_BUFFER_OBJECT_H
35
36#include "VertexDataInterface.h"
38
39namespace Divide
40{
41
42class ByteBuffer;
43FWD_DECLARE_MANAGED_CLASS(GenericVertexData);
48{
49 public:
50 constexpr static U32 PRIMITIVE_RESTART_INDEX_L = 0xFFFFFFFF;
51 constexpr static U32 PRIMITIVE_RESTART_INDEX_S = 0xFFFF;
52 constexpr static U16 INVALID_PARTITION_ID = 0xFFFF;
53
54 struct Vertex
55 {
56 UColour4 _colour{0u, 0u, 0u, 1u};
63 };
64
66 {
68 bool _largeIndices{false};
69 bool _keepCPUData{false};
71 };
72
73 VertexBuffer(GFXDevice& context, const Descriptor& descriptor);
74
75 void reserveIndexCount(const size_t size);
76
77 void setVertexCount(const size_t size);
78
79 void resizeVertexCount( const size_t size, const Vertex& defaultValue);
80 inline void resizeVertexCount(const size_t size ) { resizeVertexCount( size, {} ); }
81
82 [[nodiscard]] size_t getVertexCount() const noexcept;
83
84 [[nodiscard]] const vector<Vertex>& getVertices() const noexcept;
85
86 [[nodiscard]] const vec3<F32>& getPosition(const U32 index) const;
87
88 [[nodiscard]] vec2<F32> getTexCoord(const U32 index) const;
89
90 [[nodiscard]] F32 getNormal(const U32 index) const;
91
92 F32 getNormal(const U32 index, vec3<F32>& normalOut) const;
93
94 [[nodiscard]] F32 getTangent(const U32 index) const;
95
96 [[nodiscard]] F32 getTangent(const U32 index, vec3<F32>& tangentOut) const;
97
98 [[nodiscard]] vec4<U8> getBoneIndices(const U32 index) const;
99
100 [[nodiscard]] vec4<U8> getBoneWeightsPacked(const U32 index) const;
101
102 [[nodiscard]] vec4<F32> getBoneWeights(const U32 index) const;
103
104 [[nodiscard]] size_t getIndexCount() const noexcept;
105
106 [[nodiscard]] U32 getIndex(const size_t index) const;
107
108 [[nodiscard]] const vector<U32>& getIndices() const noexcept;
109
110 void addIndex(const U32 index);
111
112 void addIndices(const vector<U16>& indices);
113
114 void addIndices(const vector<U32>& indices);
115
116 void addRestartIndex();
117
118 void modifyPositionValues(const U32 indexOffset, const vector<vec3<F32>>& newValues);
119
120 void modifyPositionValue(const U32 index, const vec3<F32>& newValue);
121
122 void modifyPositionValue(const U32 index, const F32 x, const F32 y, const F32 z);
123
124 void modifyColourValue(const U32 index, const UColour4& newValue);
125
126 void modifyColourValue(const U32 index, const U8 r, const U8 g, const U8 b, const U8 a);
127
128 void modifyNormalValue(const U32 index, const vec3<F32>& newValue);
129
130 void modifyNormalValue(const U32 index, const F32 x, const F32 y, const F32 z);
131
132 void modifyTangentValue(const U32 index, const vec3<F32>& newValue);
133
134 void modifyTangentValue(const U32 index, const F32 x, const F32 y, const F32 z);
135
136 void modifyTexCoordValue(const U32 index, vec2<F32> newValue);
137
138 void modifyTexCoordValue(const U32 index, const F32 s, const F32 t);
139
140 void modifyBoneIndices(const U32 index, const vec4<U8> indices);
141
142 void modifyBoneWeights(const U32 index, const FColour4& weights);
143
144 void modifyBoneWeights(const U32 index, const vec4<U8> packedWeights);
145
146 [[nodiscard]] size_t partitionCount() const noexcept;
147
149
150 size_t getPartitionIndexCount(const U16 partitionID);
151
152 [[nodiscard]] size_t getPartitionOffset(const U16 partitionID) const;
153
154 [[nodiscard]] size_t lastPartitionOffset() const;
155
156 [[nodiscard]] AttributeMap generateAttributeMap();
157
158 void reset();
159
160 void fromBuffer(const VertexBuffer& other);
161 bool deserialize(ByteBuffer& dataIn);
162 bool serialize(ByteBuffer& dataOut) const;
163
164 void computeNormals();
165 void computeTangents();
166
167 protected:
169 bool refresh(BufferLock& dataLockOut, BufferLock& indexLockOut);
170
171 bool getMinimalData(const vector<Vertex>& dataIn, Byte* dataOut, size_t dataOutBufferLength);
173 void draw(const GenericDrawCommand& command, VDIUserData* data) override;
174
175 [[nodiscard]] static size_t GetTotalDataSize(const AttributeFlags& usedAttributes);
176 [[nodiscard]] static AttributeOffsets GetAttributeOffsets(const AttributeFlags& usedAttributes, size_t& totalDataSizeOut);
177
178 protected:
180 // first: offset, second: count
181 vector<std::pair<size_t, size_t>> _partitions;
186 GenericVertexData_ptr _internalGVD = nullptr;
187 bool _refreshQueued = false;
188 bool _dataLayoutChanged = false;
189 bool _indicesChanged = true;
190};
191
193
194}; // namespace Divide
195
196#endif //DVD_VERTEX_BUFFER_OBJECT_H
#define FWD_DECLARE_MANAGED_CLASS(T)
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
GFXDevice & context() const noexcept
void addIndex(const U32 index)
void modifyTexCoordValue(const U32 index, vec2< F32 > newValue)
bool deserialize(ByteBuffer &dataIn)
static constexpr U32 PRIMITIVE_RESTART_INDEX_L
Definition: VertexBuffer.h:50
U32 getIndex(const size_t index) const
void modifyPositionValues(const U32 indexOffset, const vector< vec3< F32 > > &newValues)
size_t getPartitionOffset(const U16 partitionID) const
vec4< U8 > getBoneWeightsPacked(const U32 index) const
void draw(const GenericDrawCommand &command, VDIUserData *data) override
Calculates the appropriate attribute offsets and returns the total size of a vertex for this buffer.
size_t getIndexCount() const noexcept
bool refresh(BufferLock &dataLockOut, BufferLock &indexLockOut)
Returns true if data was updated.
vec4< F32 > getBoneWeights(const U32 index) const
size_t lastPartitionOffset() const
AttributeFlags _useAttribute
Definition: VertexBuffer.h:185
const vector< Vertex > & getVertices() const noexcept
static constexpr U16 INVALID_PARTITION_ID
Definition: VertexBuffer.h:52
GenericVertexData_ptr _internalGVD
Definition: VertexBuffer.h:186
size_t partitionCount() const noexcept
size_t getPartitionIndexCount(const U16 partitionID)
vec2< F32 > getTexCoord(const U32 index) const
void addIndices(const vector< U16 > &indices)
F32 getTangent(const U32 index) const
size_t getVertexCount() const noexcept
void resizeVertexCount(const size_t size, const Vertex &defaultValue)
void fromBuffer(const VertexBuffer &other)
static size_t GetTotalDataSize(const AttributeFlags &usedAttributes)
bool getMinimalData(const vector< Vertex > &dataIn, Byte *dataOut, size_t dataOutBufferLength)
Trim down the Vertex vector to only upload the minimal amount of data to the GPU.
const vec3< F32 > & getPosition(const U32 index) const
void modifyBoneWeights(const U32 index, const FColour4 &weights)
bool serialize(ByteBuffer &dataOut) const
void modifyColourValue(const U32 index, const UColour4 &newValue)
void modifyTangentValue(const U32 index, const vec3< F32 > &newValue)
vec4< U8 > getBoneIndices(const U32 index) const
void reserveIndexCount(const size_t size)
static AttributeOffsets GetAttributeOffsets(const AttributeFlags &usedAttributes, size_t &totalDataSizeOut)
vector< Vertex > _data
Definition: VertexBuffer.h:182
Descriptor _descriptor
Definition: VertexBuffer.h:179
void modifyNormalValue(const U32 index, const vec3< F32 > &newValue)
void setVertexCount(const size_t size)
F32 getNormal(const U32 index) const
const vector< U32 > & getIndices() const noexcept
void modifyBoneIndices(const U32 index, const vec4< U8 > indices)
static constexpr U32 PRIMITIVE_RESTART_INDEX_S
Definition: VertexBuffer.h:51
void modifyPositionValue(const U32 index, const vec3< F32 > &newValue)
void resizeVertexCount(const size_t size)
Definition: VertexBuffer.h:80
AttributeMap generateAttributeMap()
Activate and set all of the required vertex attributes.
vector< std::pair< size_t, size_t > > _partitions
Definition: VertexBuffer.h:181
vector< U32 > _indices
Used for creating an "IB". If it's empty, then an outside source should provide the indices.
Definition: VertexBuffer.h:184
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::array< bool, to_base(AttribLocation::COUNT)> AttributeFlags
std::byte Byte
uint8_t U8
eastl::vector< Type > vector
Definition: Vector.h:42
uint16_t U16
std::array< size_t, to_base(AttribLocation::COUNT)> AttributeOffsets
uint32_t U32