Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
#define THREAD_ENABLED
#endif

#define GRAPHICS_API_GL
enum eGraphicsAPI
{
GL = 0,
};
2 changes: 1 addition & 1 deletion Core/IO/MaterialIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct MaterialIR
std::string NameStr;

// Legacy / Phong Attributes
Color128 Ambient{0.f, 0.f, 0.f, 1.f};
Color128 Ambient{0.001f, 0.001f, 0.001f, 1.f};
Color128 Diffuse{1.f, 1.f, 1.f, 1.f};
Color128 Specular{1.f, 1.f, 1.f, 1.f};
float Shininess = 32.0f;
Expand Down
33 changes: 23 additions & 10 deletions Core/IO/ResourceParseFuncs.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ResourceParseFuncs.h"

#include <assimp/GltfMaterial.h>
#include <assimp/Importer.hpp>
#include <assimp/pbrmaterial.h>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <fstream>
Expand Down Expand Up @@ -507,12 +507,15 @@ std::unique_ptr<const MaterialIR> parseMaterial(const Path& path,
aiColor4D color4D;
float fVal = 0.0f;
int iVal = 0;
aiString aiStr;

// Legacy / Phong Attributes
if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_COLOR_AMBIENT, color3D))
{
pMaterialIR->Ambient = Color128(color3D.r, color3D.g, color3D.b);
if (pMaterialIR->Ambient == Color128(0.0f, 0.0f, 0.0f))
{
pMaterialIR->Ambient = Color128(0.001f, 0.001f, 0.001f);
}
}
if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_COLOR_DIFFUSE, color3D))
{
Expand All @@ -525,6 +528,10 @@ std::unique_ptr<const MaterialIR> parseMaterial(const Path& path,
if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_SHININESS, fVal))
{
pMaterialIR->Shininess = fVal;
if (pMaterialIR->Shininess <= 0.01f)
{
pMaterialIR->Shininess = 32.0f;
}
}

// PBR Core Attributes
Expand All @@ -551,22 +558,28 @@ std::unique_ptr<const MaterialIR> parseMaterial(const Path& path,
pMaterialIR->Opacity = fVal;
}

pMaterialIR->AlphaMode =
pMaterialIR->Opacity < 1.f ? MaterialIR::eAlphaMode::Blend : MaterialIR::eAlphaMode::Opaque;
pMaterialIR->AlphaThreshold = 0.5f;

if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_GLTF_ALPHAMODE, aiStr))
aiString alphaModeStr;
if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_GLTF_ALPHAMODE, alphaModeStr))
{
const std::string mode(aiStr.C_Str());
if (mode == "MASK")
if (alphaModeStr == aiString("MASK"))
{
pMaterialIR->AlphaMode = MaterialIR::eAlphaMode::Mask;
}
else if (mode == "BLEND")
else if (alphaModeStr == aiString("BLEND"))
{
pMaterialIR->AlphaMode = MaterialIR::eAlphaMode::Blend;
}
else
{
pMaterialIR->AlphaMode = MaterialIR::eAlphaMode::Opaque;
}
}
else
{
pMaterialIR->AlphaMode =
pMaterialIR->Opacity < 1.f ? MaterialIR::eAlphaMode::Blend : MaterialIR::eAlphaMode::Opaque;
}

if (AI_SUCCESS == assimpMaterial.Get(AI_MATKEY_GLTF_ALPHACUTOFF, fVal))
{
pMaterialIR->AlphaThreshold = fVal;
Expand Down
14 changes: 5 additions & 9 deletions Core/Templates/FixedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ template <typename T>
class FixedArray
{
public:
FixedArray()
: mpArray(nullptr)
, mSize(0)
{
}
FixedArray() = default;

FixedArray(int32_t size)
: mSize(size)
Expand All @@ -29,13 +25,13 @@ class FixedArray
FixedArray(const FixedArray&) = delete;
FixedArray& operator=(const FixedArray&) = delete;

FixedArray(FixedArray&& other)
FixedArray(FixedArray&& other) noexcept
: mpArray(std::move(other.mpArray))
, mSize(other.mSize)
{
}

FixedArray& operator=(FixedArray&& rhs)
FixedArray& operator=(FixedArray&& rhs) noexcept
{
if (this == &rhs)
{
Expand Down Expand Up @@ -122,7 +118,7 @@ class FixedArray
}

private:
std::unique_ptr<T[]> mpArray;
int32_t mSize;
std::unique_ptr<T[]> mpArray = nullptr;
int32_t mSize = 0;
};
} // namespace ho
143 changes: 143 additions & 0 deletions Core/Templates/FixedQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#pragma once

#include <memory>

#include "Macros.h"

namespace ho
{
template <typename T>
class FixedQueue
{
public:
FixedQueue() = default;

FixedQueue(int32_t size)
: mSize(size)
, mCapacity(size + 1)
{
HO_ASSERT(size >= 0, "Size cannot be negative.");
if (mSize > 0)
{
mpQueue = std::make_unique<T[]>(mCapacity);
}
}

FixedQueue(const FixedQueue&) = delete;
FixedQueue& operator=(const FixedQueue&) = delete;

FixedQueue(FixedQueue&& other) noexcept
: mpQueue(std::move(other.mpQueue))
, mHeadIdx(other.mHeadIdx)
, mTailIdx(other.mTailIdx)
, mSize(other.mSize)
, mCapacity(other.mCapacity)
{
}

FixedQueue& operator=(FixedQueue&& rhs) noexcept
{
if (this == &rhs)
{
return *this;
}

mpQueue = std::move(rhs.mpQueue);
mHeadIdx = rhs.mHeadIdx;
mTailIdx = rhs.mTailIdx;
mSize = rhs.mSize;
mCapacity = rhs.mCapacity;

return *this;
}

template <typename U = T>
void Push(U&& v)
{
if ((mTailIdx + 1) % mCapacity == mHeadIdx)
{
HO_ASSERT(false, "Queue is full.");
return;
}

mpQueue[mTailIdx] = std::forward<U>(v);
mTailIdx = (mTailIdx + 1) % mCapacity;
}

template <typename... Args>
void Emplace(Args&&... args)
{
if ((mTailIdx + 1) % mCapacity == mHeadIdx)
{
HO_ASSERT(false, "Queue is full.");
return;
}

mpQueue[mTailIdx] = T(std::forward<Args>(args)...);
mTailIdx = (mTailIdx + 1) % mCapacity;
}

void Pop()
{
if (IsEmpty())
{
return;
}

mHeadIdx = (mHeadIdx + 1) % mCapacity;
}

T& Front()
{
HO_ASSERT(mHeadIdx != mTailIdx, "Queue is empty.");
return mpQueue[mHeadIdx];
}

const T& Front() const
{
HO_ASSERT(mHeadIdx != mTailIdx, "Queue is empty.");
return mpQueue[mHeadIdx];
}

int32_t GetCount() const
{
return (mTailIdx + mCapacity - mHeadIdx) % mCapacity;
}

int32_t GetSize() const
{
return mSize;
}

bool IsEmpty() const
{
return mHeadIdx == mTailIdx;
}

bool IsFull() const
{
return (mTailIdx + 1) % mCapacity == mHeadIdx;
}

void Release()
{
mpQueue.reset();
mHeadIdx = 0;
mTailIdx = 0;
mSize = 0;
mCapacity = 0;
}

bool IsReleased()
{
return mpQueue == nullptr;
}

private:
std::unique_ptr<T[]> mpQueue = nullptr;
int32_t mHeadIdx = 0;
int32_t mTailIdx = 0;
int32_t mSize = 0;
int32_t mCapacity = 0;
};
} // namespace ho
57 changes: 0 additions & 57 deletions Core/Thread/SpinLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,13 @@

#include <atomic>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#include "Config.h"
#include "Macros.h"
#include "Thread.h"

namespace ho
{

/*
* This function is adapted from Godot Engine's spin_lock.h.
*
* The original work is licensed under the MIT License:
*
* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.
* Copyright (c) 2014-present Godot Engine contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Local modifications: function name normalized to CPU_PAUSE and minor
* formatting changes. No functional changes to the original logic.
*/

FORCE_INLINE static void CPU_PAUSE()
{
#if defined(_MSC_VER)
// ----- MSVC.
#if defined(_M_ARM) || defined(_M_ARM64) // ARM.
__yield();
#elif defined(_M_IX86) || defined(_M_X64) // x86.
_mm_pause();
#endif
#elif defined(__GNUC__) || defined(__clang__)
// ----- GCC/Clang.
#if defined(__i386__) || defined(__x86_64__) // x86.
__builtin_ia32_pause();
#elif defined(__arm__) || defined(__aarch64__) // ARM.
asm volatile("yield");
#elif defined(__powerpc__) // PowerPC.
asm volatile("or 27,27,27");
#elif defined(__riscv) // RISC-V.
asm volatile(".insn i 0x0F, 0, x0, x0, 0x010");
#endif
#endif
}

#if defined(THREAD_ENABLED)

static_assert(std::atomic_bool::is_always_lock_free, "std::atomic<bool> must be lock-free for this SpinLock.");
Expand Down
Loading
Loading