refactors

This commit is contained in:
turanszkij
2019-07-03 19:08:04 +01:00
parent 08ab7ae9de
commit b08d3a1153
9 changed files with 81 additions and 113 deletions
+1 -4
View File
@@ -3,8 +3,6 @@
#include "wiSpinLock.h"
#include <mutex>
namespace wiContainers
{
// Fixed size very simple thread safe ring buffer
@@ -51,8 +49,7 @@ namespace wiContainers
T data[capacity];
size_t head = 0;
size_t tail = 0;
//wiSpinLock lock;
std::mutex lock; // this just works better than spinlock here (on windows)
wiSpinLock lock;
};
}
+1 -1
View File
@@ -44,7 +44,7 @@ void wiHairParticle::UpdateCPU(const TransformComponent& transform, const MeshCo
_min.z -= length;
aabb = AABB(_min, _max);
aabb = aabb.get(world);
aabb = aabb.transform(world);
if (dt > 0)
{
+27 -65
View File
@@ -7,7 +7,7 @@ void AABB::createFromHalfWidth(const XMFLOAT3& center, const XMFLOAT3& halfwidth
_min = XMFLOAT3(center.x - halfwidth.x, center.y - halfwidth.y, center.z - halfwidth.z);
_max = XMFLOAT3(center.x + halfwidth.x, center.y + halfwidth.y, center.z + halfwidth.z);
}
AABB AABB::get(const XMMATRIX& mat) const
AABB AABB::transform(const XMMATRIX& mat) const
{
XMFLOAT3 corners[8];
for (int i = 0; i < 8; ++i)
@@ -32,9 +32,9 @@ AABB AABB::get(const XMMATRIX& mat) const
return AABB(min, max);
}
AABB AABB::get(const XMFLOAT4X4& mat) const
AABB AABB::transform(const XMFLOAT4X4& mat) const
{
return get(XMLoadFloat4x4(&mat));
return transform(XMLoadFloat4x4(&mat));
}
XMFLOAT3 AABB::getCenter() const
{
@@ -213,62 +213,28 @@ bool RAY::intersects(const SPHERE& b) const {
void Frustum::Create(XMFLOAT4X4 projectionMatrix, const XMFLOAT4X4& viewMatrix, float farPlane)
void Frustum::Create(const XMMATRIX& viewProjection)
{
view = viewMatrix;
// We are interested in columns of the matrix, so transpose because we can access only rows:
const XMMATRIX mat = XMMatrixTranspose(viewProjection);
// Calculate the minimum Z distance in the frustum.
float zMinimum = -projectionMatrix._43 / projectionMatrix._33;
float r = farPlane / (farPlane - zMinimum);
projectionMatrix._33 = r;
projectionMatrix._43 = -r * zMinimum;
// Near plane:
XMStoreFloat4(&planes[0], XMPlaneNormalize(mat.r[2]));
// Create the frustum matrix from the view matrix and updated projection matrix.
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, XMMatrixMultiply(XMLoadFloat4x4(&viewMatrix), XMLoadFloat4x4(&projectionMatrix)));
// Far plane:
XMStoreFloat4(&planes[1], XMPlaneNormalize(mat.r[3] - mat.r[2]));
// Calculate near plane of frustum.
m_planes[0].x = matrix._14 + matrix._13;
m_planes[0].y = matrix._24 + matrix._23;
m_planes[0].z = matrix._34 + matrix._33;
m_planes[0].w = matrix._44 + matrix._43;
XMStoreFloat4(&m_planesNorm[0], XMPlaneNormalize(XMLoadFloat4(&m_planes[0])));
// Left plane:
XMStoreFloat4(&planes[2], XMPlaneNormalize(mat.r[3] + mat.r[0]));
// Calculate far plane of frustum.
m_planes[1].x = matrix._14 - matrix._13;
m_planes[1].y = matrix._24 - matrix._23;
m_planes[1].z = matrix._34 - matrix._33;
m_planes[1].w = matrix._44 - matrix._43;
XMStoreFloat4(&m_planesNorm[1], XMPlaneNormalize(XMLoadFloat4(&m_planes[1])));
// Right plane:
XMStoreFloat4(&planes[3], XMPlaneNormalize(mat.r[3] - mat.r[0]));
// Calculate left plane of frustum.
m_planes[2].x = matrix._14 + matrix._11;
m_planes[2].y = matrix._24 + matrix._21;
m_planes[2].z = matrix._34 + matrix._31;
m_planes[2].w = matrix._44 + matrix._41;
XMStoreFloat4(&m_planesNorm[2], XMPlaneNormalize(XMLoadFloat4(&m_planes[2])));
// Calculate right plane of frustum.
m_planes[3].x = matrix._14 - matrix._11;
m_planes[3].y = matrix._24 - matrix._21;
m_planes[3].z = matrix._34 - matrix._31;
m_planes[3].w = matrix._44 - matrix._41;
XMStoreFloat4(&m_planesNorm[3], XMPlaneNormalize(XMLoadFloat4(&m_planes[3])));
// Calculate top plane of frustum.
m_planes[4].x = matrix._14 - matrix._12;
m_planes[4].y = matrix._24 - matrix._22;
m_planes[4].z = matrix._34 - matrix._32;
m_planes[4].w = matrix._44 - matrix._42;
XMStoreFloat4(&m_planesNorm[4], XMPlaneNormalize(XMLoadFloat4(&m_planes[4])));
// Calculate bottom plane of frustum.
m_planes[5].x = matrix._14 + matrix._12;
m_planes[5].y = matrix._24 + matrix._22;
m_planes[5].z = matrix._34 + matrix._32;
m_planes[5].w = matrix._44 + matrix._42;
XMStoreFloat4(&m_planesNorm[5], XMPlaneNormalize(XMLoadFloat4(&m_planes[5])));
// Top plane:
XMStoreFloat4(&planes[4], XMPlaneNormalize(mat.r[3] - mat.r[1]));
// Bottom plane:
XMStoreFloat4(&planes[5], XMPlaneNormalize(mat.r[3] + mat.r[1]));
}
bool Frustum::CheckPoint(const XMFLOAT3& point) const
@@ -276,7 +242,7 @@ bool Frustum::CheckPoint(const XMFLOAT3& point) const
XMVECTOR p = XMLoadFloat3(&point);
for (short i = 0; i < 6; i++)
{
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&m_planesNorm[i]), p)) < 0.0f)
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&planes[i]), p)) < 0.0f)
{
return false;
}
@@ -290,7 +256,7 @@ bool Frustum::CheckSphere(const XMFLOAT3& center, float radius) const
XMVECTOR c = XMLoadFloat3(&center);
for (i = 0; i < 6; i++)
{
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&m_planesNorm[i]), c)) < -radius)
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&planes[i]), c)) < -radius)
{
return false;
}
@@ -309,7 +275,7 @@ Frustum::BoxFrustumIntersect Frustum::CheckBox(const AABB& box) const
for (int i = 0; i < 8; ++i)
{
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&m_planesNorm[p]), XMLoadFloat3(&box.corner(i)))) < 0.0f)
if (XMVectorGetX(XMPlaneDotCoord(XMLoadFloat4(&planes[p]), XMLoadFloat3(&box.corner(i)))) < 0.0f)
{
iPtIn = 0;
--iInCount;
@@ -324,16 +290,12 @@ Frustum::BoxFrustumIntersect Frustum::CheckBox(const AABB& box) const
return(BOX_FRUSTUM_INTERSECTS);
}
const XMFLOAT4& Frustum::getLeftPlane() const { return m_planesNorm[2]; }
const XMFLOAT4& Frustum::getRightPlane() const { return m_planesNorm[3]; }
const XMFLOAT4& Frustum::getTopPlane() const { return m_planesNorm[4]; }
const XMFLOAT4& Frustum::getBottomPlane() const { return m_planesNorm[5]; }
const XMFLOAT4& Frustum::getFarPlane() const { return m_planesNorm[1]; }
const XMFLOAT4& Frustum::getNearPlane() const { return m_planesNorm[0]; }
XMFLOAT3 Frustum::getCamPos() const
{
return XMFLOAT3(-view._41, -view._42, -view._43);
}
const XMFLOAT4& Frustum::getNearPlane() const { return planes[0]; }
const XMFLOAT4& Frustum::getFarPlane() const { return planes[1]; }
const XMFLOAT4& Frustum::getLeftPlane() const { return planes[2]; }
const XMFLOAT4& Frustum::getRightPlane() const { return planes[3]; }
const XMFLOAT4& Frustum::getTopPlane() const { return planes[4]; }
const XMFLOAT4& Frustum::getBottomPlane() const { return planes[5]; }
+6 -9
View File
@@ -23,8 +23,8 @@ struct AABB
AABB(const XMFLOAT3& _min = XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX), const XMFLOAT3& _max = XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX)) : _min(_min), _max(_max) {}
void createFromHalfWidth(const XMFLOAT3& center, const XMFLOAT3& halfwidth);
AABB get(const XMMATRIX& mat) const;
AABB get(const XMFLOAT4X4& mat) const;
AABB transform(const XMMATRIX& mat) const;
AABB transform(const XMFLOAT4X4& mat) const;
XMFLOAT3 getCenter() const;
XMFLOAT3 getHalfWidth() const;
XMMATRIX getAsBoxMatrix() const;
@@ -86,11 +86,9 @@ struct RAY
class Frustum
{
private:
XMFLOAT4 m_planesNorm[6];
XMFLOAT4 m_planes[6];
XMFLOAT4X4 view;
XMFLOAT4 planes[6];
public:
void Create(XMFLOAT4X4 projectionMatrix, const XMFLOAT4X4& viewMatrix, float farPlane);
void Create(const XMMATRIX& viewProjection);
bool CheckPoint(const XMFLOAT3&) const;
bool CheckSphere(const XMFLOAT3&, float) const;
@@ -103,13 +101,12 @@ public:
};
BoxFrustumIntersect CheckBox(const AABB& box) const;
const XMFLOAT4& getNearPlane() const;
const XMFLOAT4& getFarPlane() const;
const XMFLOAT4& getLeftPlane() const;
const XMFLOAT4& getRightPlane() const;
const XMFLOAT4& getTopPlane() const;
const XMFLOAT4& getBottomPlane() const;
const XMFLOAT4& getFarPlane() const;
const XMFLOAT4& getNearPlane() const;
XMFLOAT3 getCamPos() const;
};
+1 -1
View File
@@ -230,7 +230,7 @@ namespace wiIntersect_BindLua
Matrix_BindLua* _matrix = Luna<Matrix_BindLua>::lightcheck(L, 1);
if (_matrix)
{
Luna<AABB_BindLua>::push(L, new AABB_BindLua(aabb.get(_matrix->matrix)));
Luna<AABB_BindLua>::push(L, new AABB_BindLua(aabb.transform(_matrix->matrix)));
return 1;
}
else
+5 -5
View File
@@ -17,7 +17,7 @@ namespace wiJobSystem
};
uint32_t numThreads = 0;
wiContainers::ThreadSafeRingBuffer<Job, 256> jobPool;
wiContainers::ThreadSafeRingBuffer<Job, 256> jobQueue;
std::condition_variable wakeCondition;
std::mutex wakeMutex;
@@ -25,7 +25,7 @@ namespace wiJobSystem
inline bool work()
{
Job job;
if (jobPool.pop_front(job))
if (jobQueue.pop_front(job))
{
job.task(); // execute job
job.ctx->counter.fetch_sub(1);
@@ -64,7 +64,7 @@ namespace wiJobSystem
// Do Windows-specific thread setup:
HANDLE handle = (HANDLE)worker.native_handle();
// Put each thread on to dedicated core
// Put each thread on to dedicated core:
DWORD_PTR affinityMask = 1ull << threadID;
DWORD_PTR affinity_result = SetThreadAffinityMask(handle, affinityMask);
assert(affinity_result > 0);
@@ -99,7 +99,7 @@ namespace wiJobSystem
ctx.counter.fetch_add(1);
// Try to push a new job until it is pushed successfully:
while (!jobPool.push_back({ job, &ctx })) { wakeCondition.notify_all(); }
while (!jobQueue.push_back({ job, &ctx })) { wakeCondition.notify_all(); }
// Wake any one thread that might be sleeping:
wakeCondition.notify_one();
@@ -139,7 +139,7 @@ namespace wiJobSystem
};
// Try to push a new job until it is pushed successfully:
while (!jobPool.push_back({ jobGroup, &ctx })) { wakeCondition.notify_all(); }
while (!jobQueue.push_back({ jobGroup, &ctx })) { wakeCondition.notify_all(); }
}
// Wake any threads that might be sleeping:
+35 -23
View File
@@ -1175,9 +1175,12 @@ struct SHCAM
const XMMATRIX rot = XMMatrixRotationQuaternion(rotation);
const XMVECTOR to = XMVector3TransformNormal(XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f), rot);
const XMVECTOR up = XMVector3TransformNormal(XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f), rot);
XMStoreFloat4x4(&View, XMMatrixLookToLH(eyePos, to, up));
XMStoreFloat4x4(&Projection, XMMatrixPerspectiveFovLH(fov, 1, farPlane, nearPlane));
frustum.Create(Projection, View, farPlane);
const XMMATRIX V = XMMatrixLookToLH(eyePos, to, up);
const XMMATRIX P = XMMatrixPerspectiveFovLH(fov, 1, farPlane, nearPlane);
const XMMATRIX VP = XMMatrixMultiply(V, P);
XMStoreFloat4x4(&View, V);
XMStoreFloat4x4(&Projection, P);
frustum.Create(VP);
};
XMMATRIX getVP() const {
return XMMatrixTranspose(XMLoadFloat4x4(&View)*XMLoadFloat4x4(&Projection));
@@ -1271,7 +1274,7 @@ inline void CreateDirLightShadowCams(const LightComponent& light, const CameraCo
_max.z = std::max(_max.z, farPlane * 0.5f);
// Compute bounding box used for culling, conservatively transformed by the light transform:
shcams[cascade].boundingbox = AABB(_min, _max).get(XMMatrixInverse(nullptr, lightView));
shcams[cascade].boundingbox = AABB(_min, _max).transform(XMMatrixInverse(nullptr, lightView));
const XMMATRIX lightProjection = XMMatrixOrthographicOffCenterLH(_min.x, _max.x, _min.y, _max.y, _max.z, _min.z); // notice reversed Z!
@@ -3855,16 +3858,25 @@ void UpdatePerFrameData(float dt, uint32_t layerMask)
GetRefCamera() = GetCamera();
GetRefCamera().Reflect(waterPlane);
for (auto& x : waterRipples)
{
x->Update(dt * 60);
}
ManageDecalAtlas();
ManageLightmapAtlas();
ManageImpostors();
ManageEnvProbes();
ManageWaterRipples();
wiJobSystem::Execute(ctx, [&] {
ManageDecalAtlas();
});
wiJobSystem::Execute(ctx, [&] {
ManageLightmapAtlas();
});
wiJobSystem::Execute(ctx, [&] {
ManageImpostors();
});
wiJobSystem::Execute(ctx, [&] {
ManageEnvProbes();
});
wiJobSystem::Execute(ctx, [&] {
for (auto& x : waterRipples)
{
x->Update(dt * 60);
}
ManageWaterRipples();
});
wiJobSystem::Wait(ctx);
}
@@ -5243,14 +5255,14 @@ void DrawScene(const CameraComponent& camera, bool tessellation, CommandList cmd
BindConstantBuffers(PS, cmd);
device->BindResource(PS, GetGlobalLightmap(), TEXSLOT_GLOBALLIGHTMAP, cmd);
if (decalAtlas.IsValid())
{
device->BindResource(PS, &decalAtlas, TEXSLOT_DECALATLAS, cmd);
}
if (renderPass == RENDERPASS_TILEDFORWARD)
{
device->BindResource(PS, &resourceBuffers[RBTYPE_ENTITYTILES_OPAQUE], SBSLOT_ENTITYTILES, cmd);
if (decalAtlas.IsValid())
{
device->BindResource(PS, &decalAtlas, TEXSLOT_DECALATLAS, cmd);
}
}
if (grass)
@@ -5333,14 +5345,14 @@ void DrawScene_Transparent(const CameraComponent& camera, RENDERPASS renderPass,
BindConstantBuffers(PS, cmd);
device->BindResource(PS, GetGlobalLightmap(), TEXSLOT_GLOBALLIGHTMAP, cmd);
if (decalAtlas.IsValid())
{
device->BindResource(PS, &decalAtlas, TEXSLOT_DECALATLAS, cmd);
}
if (renderPass == RENDERPASS_TILEDFORWARD)
{
device->BindResource(PS, &resourceBuffers[RBTYPE_ENTITYTILES_TRANSPARENT], SBSLOT_ENTITYTILES, cmd);
if (decalAtlas.IsValid())
{
device->BindResource(PS, &decalAtlas, TEXSLOT_DECALATLAS, cmd);
}
}
if (ocean != nullptr)
@@ -8161,7 +8173,7 @@ void UpdateFrameCB(CommandList cmd)
XMStoreFloat4x4(&cb.g_xFrame_MainCamera_View, XMMatrixTranspose(camera.GetView()));
XMStoreFloat4x4(&cb.g_xFrame_MainCamera_Proj, XMMatrixTranspose(camera.GetProjection()));
cb.g_xFrame_MainCamera_CamPos = camera.Eye;
cb.g_xFrame_MainCamera_DistanceFromOrigin, XMVectorGetX(XMVector3Length(XMLoadFloat3(&cb.g_xFrame_MainCamera_CamPos)));
cb.g_xFrame_MainCamera_DistanceFromOrigin = XMVectorGetX(XMVector3Length(XMLoadFloat3(&cb.g_xFrame_MainCamera_CamPos)));
XMStoreFloat4x4(&cb.g_xFrame_MainCamera_PrevV, XMMatrixTranspose(prevCam.GetView()));
XMStoreFloat4x4(&cb.g_xFrame_MainCamera_PrevP, XMMatrixTranspose(prevCam.GetProjection()));
XMStoreFloat4x4(&cb.g_xFrame_MainCamera_PrevVP, XMMatrixTranspose(prevCam.GetViewProjection()));
+4 -4
View File
@@ -955,7 +955,7 @@ namespace wiSceneSystem
XMStoreFloat4x4(&Projection, _P);
XMStoreFloat4x4(&InvProjection, XMMatrixInverse(nullptr, _P));
frustum.Create(Projection, View, zFarP);
frustum.Create(_VP);
}
void CameraComponent::TransformCamera(const TransformComponent& transform)
{
@@ -1787,7 +1787,7 @@ namespace wiSceneSystem
if (mesh != nullptr)
{
XMMATRIX W = XMLoadFloat4x4(&transform.world);
aabb = mesh->aabb.get(W);
aabb = mesh->aabb.transform(W);
// This is instance bounding box matrix:
XMFLOAT4X4 meshMatrix;
@@ -1928,7 +1928,7 @@ namespace wiSceneSystem
AABB& aabb = aabb_decals[args.jobIndex];
aabb.createFromHalfWidth(XMFLOAT3(0, 0, 0), XMFLOAT3(1, 1, 1));
aabb = aabb.get(transform.world);
aabb = aabb.transform(transform.world);
const MaterialComponent& material = *materials.GetComponent(entity);
decal.color = material.baseColor;
@@ -1965,7 +1965,7 @@ namespace wiSceneSystem
AABB& aabb = aabb_probes[args.jobIndex];
aabb.createFromHalfWidth(XMFLOAT3(0, 0, 0), XMFLOAT3(1, 1, 1));
aabb = aabb.get(transform.world);
aabb = aabb.transform(transform.world);
});
}
void RunForceUpdateSystem(
+1 -1
View File
@@ -9,7 +9,7 @@ namespace wiVersion
// minor features, major updates
const int minor = 27;
// minor bug fixes, alterations, refactors, updates
const int revision = 0;
const int revision = 1;
long GetVersion()