Skip to content

Commit a2d60bf

Browse files
committed
lock overkill
1 parent 3243f17 commit a2d60bf

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

WickedEngine/wiScene_Components.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace wi::scene
3636

3737
XMFLOAT3 TransformComponent::GetPosition() const
3838
{
39+
std::shared_lock lock(_mutex);
3940
return wi::math::GetPosition(world);
4041
}
4142
XMFLOAT4 TransformComponent::GetRotation() const
@@ -52,22 +53,29 @@ namespace wi::scene
5253
}
5354
XMVECTOR TransformComponent::GetPositionV() const
5455
{
56+
std::shared_lock lock(_mutex);
5557
return XMLoadFloat3((XMFLOAT3*)&world._41);
5658
}
5759
XMVECTOR TransformComponent::GetRotationV() const
5860
{
61+
std::shared_lock lock(_mutex);
5962
XMVECTOR S, R, T;
6063
XMMatrixDecompose(&S, &R, &T, XMLoadFloat4x4(&world));
6164
return R;
6265
}
6366
XMVECTOR TransformComponent::GetScaleV() const
6467
{
68+
std::shared_lock lock(_mutex);
6569
XMVECTOR S, R, T;
6670
XMMatrixDecompose(&S, &R, &T, XMLoadFloat4x4(&world));
6771
return S;
6872
}
6973
XMMATRIX TransformComponent::GetLocalMatrix() const
7074
{
75+
std::shared_lock lock(_mutex);
76+
return GetLocalMatrixNoLock();
77+
}
78+
XMMATRIX TransformComponent::GetLocalMatrixNoLock() const {
7179
XMVECTOR S_local = XMLoadFloat3(&scale_local);
7280
XMVECTOR R_local = XMLoadFloat4(&rotation_local);
7381
XMVECTOR T_local = XMLoadFloat3(&translation_local);
@@ -78,33 +86,40 @@ namespace wi::scene
7886
}
7987
XMFLOAT3 TransformComponent::GetForward() const
8088
{
89+
std::shared_lock lock(_mutex);
8190
return wi::math::GetForward(world);
8291
}
8392
XMFLOAT3 TransformComponent::GetUp() const
8493
{
94+
std::shared_lock lock(_mutex);
8595
return wi::math::GetUp(world);
8696
}
8797
XMFLOAT3 TransformComponent::GetRight() const
8898
{
99+
std::shared_lock lock(_mutex);
89100
return wi::math::GetRight(world);
90101
}
91102
XMVECTOR TransformComponent::GetForwardV() const
92103
{
104+
std::shared_lock lock(_mutex);
93105
XMFLOAT3 v = wi::math::GetForward(world);
94106
return XMLoadFloat3(&v);
95107
}
96108
XMVECTOR TransformComponent::GetUpV() const
97109
{
110+
std::shared_lock lock(_mutex);
98111
XMFLOAT3 v = wi::math::GetUp(world);
99112
return XMLoadFloat3(&v);
100113
}
101114
XMVECTOR TransformComponent::GetRightV() const
102115
{
116+
std::shared_lock lock(_mutex);
103117
XMFLOAT3 v = wi::math::GetRight(world);
104118
return XMLoadFloat3(&v);
105119
}
106120
void TransformComponent::GetPositionRotationScale(XMFLOAT3& position, XMFLOAT4& rotation, XMFLOAT3& scale) const
107121
{
122+
std::shared_lock lock(_mutex);
108123
XMVECTOR S, R, T;
109124
XMMatrixDecompose(&S, &R, &T, XMLoadFloat4x4(&world));
110125
XMStoreFloat3(&position, T);
@@ -115,21 +130,24 @@ namespace wi::scene
115130
{
116131
if (IsDirty())
117132
{
133+
std::unique_lock lock(_mutex);
118134
SetDirty(false);
119135

120-
XMStoreFloat4x4(&world, GetLocalMatrix());
136+
XMStoreFloat4x4(&world, GetLocalMatrixNoLock());
121137
}
122138
}
123139
void TransformComponent::UpdateTransform_Parented(const TransformComponent& parent)
124140
{
125-
XMMATRIX W = GetLocalMatrix();
141+
std::unique_lock lock(_mutex);
142+
XMMATRIX W = GetLocalMatrixNoLock();
126143
XMMATRIX W_parent = XMLoadFloat4x4(&parent.world);
127144
W = W * W_parent;
128145

129146
XMStoreFloat4x4(&world, W);
130147
}
131148
void TransformComponent::ApplyTransform()
132149
{
150+
std::unique_lock lock(_mutex);
133151
SetDirty();
134152

135153
XMVECTOR S, R, T;
@@ -140,20 +158,23 @@ namespace wi::scene
140158
}
141159
void TransformComponent::ClearTransform()
142160
{
161+
std::unique_lock lock(_mutex);
143162
SetDirty();
144163
scale_local = XMFLOAT3(1, 1, 1);
145164
rotation_local = XMFLOAT4(0, 0, 0, 1);
146165
translation_local = XMFLOAT3(0, 0, 0);
147166
}
148167
void TransformComponent::Translate(const XMFLOAT3& value)
149168
{
169+
std::unique_lock lock(_mutex);
150170
SetDirty();
151171
translation_local.x += value.x;
152172
translation_local.y += value.y;
153173
translation_local.z += value.z;
154174
}
155175
void TransformComponent::Translate(const XMVECTOR& value)
156176
{
177+
std::unique_lock lock(_mutex);
157178
XMFLOAT3 translation;
158179
XMStoreFloat3(&translation, value);
159180
Translate(translation);
@@ -765,7 +786,7 @@ namespace wi::scene
765786
{
766787
const XMFLOAT3& pos = vertex_positions[i];
767788
const uint8_t wind = vertex_windweights.empty() ? 0xFF : vertex_windweights[i];
768-
789+
769790
Vertex_POS16 v;
770791
v.FromFULL(aabb, pos, wind);
771792
XMFLOAT3 p = v.GetPOS(aabb);
@@ -1977,7 +1998,7 @@ namespace wi::scene
19771998
}
19781999
size_t MeshComponent::GetMemoryUsageCPU() const
19792000
{
1980-
size_t size =
2001+
size_t size =
19812002
vertex_positions.size() * sizeof(XMFLOAT3) +
19822003
vertex_normals.size() * sizeof(XMFLOAT3) +
19832004
vertex_tangents.size() * sizeof(XMFLOAT4) +

WickedEngine/wiScene_Components.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,39 @@ namespace wi::scene
4949
EMPTY = 0,
5050
DIRTY = 1 << 0,
5151
};
52-
52+
//private:
5353
XMFLOAT3 scale_local = XMFLOAT3(1, 1, 1);
5454
wi::copyable_atomic<uint32_t> _flags = DIRTY;
5555
XMFLOAT4 rotation_local = XMFLOAT4(0, 0, 0, 1); // this is a quaternion
5656
XMFLOAT3 translation_local = XMFLOAT3(0, 0, 0);
5757

5858
// Non-serialized attributes:
5959
float _padding = 0;
60-
60+
mutable std::shared_mutex _mutex;
6161
// The world matrix can be computed from local scale, rotation, translation
6262
// - by calling UpdateTransform()
6363
// - or by calling SetDirty() and letting the TransformUpdateSystem handle the updating
6464
XMFLOAT4X4 world = wi::math::IDENTITY_MATRIX;
65-
65+
public:
66+
TransformComponent() = default;
67+
TransformComponent(const TransformComponent& other) : _mutex() {
68+
std::shared_lock lock(other._mutex);
69+
_flags = other._flags;
70+
scale_local = other.scale_local;
71+
rotation_local = other.rotation_local;
72+
translation_local = other.translation_local;
73+
world = other.world;
74+
}
75+
TransformComponent& operator=(const TransformComponent& other) {
76+
std::shared_lock lock(other._mutex);
77+
std::unique_lock lock2(_mutex);
78+
_flags = other._flags;
79+
scale_local = other.scale_local;
80+
rotation_local = other.rotation_local;
81+
translation_local = other.translation_local;
82+
world = other.world;
83+
return *this;
84+
}
6685
constexpr void SetDirty(bool value = true) { if (value) { _flags |= DIRTY; } else { _flags &= ~DIRTY; } }
6786
WI_ATOMIC_CONSTEXPR bool IsDirty() const { return _flags & DIRTY; }
6887

@@ -82,7 +101,7 @@ namespace wi::scene
82101
// Computes the local space matrix from scale, rotation, translation and returns it
83102
XMMATRIX GetLocalMatrix() const;
84103
// Returns the stored world matrix that was computed the last time UpdateTransform() was called
85-
XMMATRIX GetWorldMatrix() const { return XMLoadFloat4x4(&world); };
104+
XMMATRIX GetWorldMatrix() const { std::shared_lock lock(_mutex); return XMLoadFloat4x4(&world); };
86105
// Applies the local space to the world space matrix. This overwrites world matrix
87106
void UpdateTransform();
88107
// Apply a parent transform relative to the local space. This overwrites world matrix
@@ -105,7 +124,10 @@ namespace wi::scene
105124
void CatmullRom(const TransformComponent& a, const TransformComponent& b, const TransformComponent& c, const TransformComponent& d, float t);
106125

107126
void Serialize(wi::Archive& archive, wi::ecs::EntitySerializer& seri);
127+
private:
128+
XMMATRIX GetLocalMatrixNoLock() const;
108129
};
130+
static_assert(sizeof(TransformComponent) % 16 == 0);
109131

110132
struct alignas(16) HierarchyComponent
111133
{

0 commit comments

Comments
 (0)