Merge branch 'NDK' into NDK-ShadowMapping

Conflicts:
	include/Nazara/Math/Matrix4.inl

Former-commit-id: e4b7d178a7acba17c03de2b585af86324b8d75a6
This commit is contained in:
Lynix
2015-09-13 12:10:30 +02:00
100 changed files with 3553 additions and 1359 deletions

View File

@@ -110,7 +110,7 @@ namespace
// Nous devons gérer nous-même le flux car il doit rester ouvert après le passage du loader
// (les flux automatiquement ouverts par le ResourceLoader étant fermés après celui-ci)
std::unique_ptr<NzFile> file(new NzFile);
if (!file->Open(filePath, NzFile::ReadOnly))
if (!file->Open(filePath, nzOpenMode_ReadOnly))
{
NazaraError("Failed to open stream from file: " + NzError::GetLastError());
return false;

View File

@@ -4,495 +4,18 @@
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/AbstractHash.hpp>
#include <Nazara/Math/Algorithm.hpp>
#include <algorithm>
#include <cstring>
#include <limits>
#include <ostream>
#include <Nazara/Core/Debug.hpp>
// Cet algorithme est inspiré de la documentation de Qt
inline unsigned int nzGetNewSize(unsigned int newSize)
std::ostream& operator<<(std::ostream& out, const NzByteArray& byteArray)
{
if (newSize < 20)
return newSize+4;
else
{
if (newSize < (1 << 12)-12)
return NzGetNearestPowerOfTwo(newSize << 1)-12;
else
return newSize + (1 << 11);
}
}
NzByteArray::NzByteArray() :
m_sharedArray(&emptyArray)
{
}
NzByteArray::NzByteArray(unsigned int size)
{
if (size > 0)
{
m_sharedArray = new SharedArray;
m_sharedArray->buffer = new nzUInt8[size];
m_sharedArray->capacity = size;
m_sharedArray->size = size;
}
else
m_sharedArray = &emptyArray;
}
NzByteArray::NzByteArray(const void* buffer, unsigned int size)
{
if (size > 0)
{
m_sharedArray = new SharedArray;
m_sharedArray->buffer = new nzUInt8[size];
m_sharedArray->capacity = size;
m_sharedArray->size = size;
std::memcpy(m_sharedArray->buffer, buffer, size);
}
else
m_sharedArray = &emptyArray;
}
NzByteArray::NzByteArray(const NzByteArray& buffer) :
m_sharedArray(buffer.m_sharedArray)
{
if (m_sharedArray != &emptyArray)
m_sharedArray->refCount++;
}
NzByteArray::NzByteArray(NzByteArray&& buffer) noexcept :
m_sharedArray(buffer.m_sharedArray)
{
buffer.m_sharedArray = &emptyArray;
}
NzByteArray::NzByteArray(SharedArray* sharedArray) :
m_sharedArray(sharedArray)
{
}
NzByteArray::~NzByteArray()
{
ReleaseArray();
}
NzByteArray& NzByteArray::Append(const void* buffer, unsigned int size)
{
return Insert(m_sharedArray->size, buffer, size);
}
NzByteArray& NzByteArray::Append(const NzByteArray& array)
{
return Insert(m_sharedArray->size, array);
}
void NzByteArray::Clear(bool keepBuffer)
{
if (keepBuffer)
{
EnsureOwnership();
m_sharedArray->size = 0;
}
else
ReleaseArray();
}
nzUInt8* NzByteArray::GetBuffer()
{
EnsureOwnership();
return m_sharedArray->buffer;
}
unsigned int NzByteArray::GetCapacity() const
{
return m_sharedArray->capacity;
}
const nzUInt8* NzByteArray::GetConstBuffer() const
{
return m_sharedArray->buffer;
}
unsigned int NzByteArray::GetSize() const
{
return m_sharedArray->size;
}
NzByteArray& NzByteArray::Insert(int pos, const void* buffer, unsigned int size)
{
if (size == 0)
return *this;
if (pos < 0)
pos = std::max(static_cast<int>(m_sharedArray->size + pos), 0);
unsigned int start = std::min(static_cast<unsigned int>(pos), m_sharedArray->size);
// Si le buffer est déjà suffisamment grand
if (m_sharedArray->capacity >= m_sharedArray->size + size)
{
EnsureOwnership();
std::memmove(&m_sharedArray->buffer[start+size], &m_sharedArray->buffer[start], m_sharedArray->size - start);
std::memcpy(&m_sharedArray->buffer[start], buffer, size);
m_sharedArray->size += size;
}
else
{
unsigned int newSize = m_sharedArray->size + size;
nzUInt8* newBuffer = new nzUInt8[newSize];
nzUInt8* ptr = newBuffer;
if (start > 0)
{
std::memcpy(ptr, m_sharedArray->buffer, start*sizeof(nzUInt8));
ptr += start;
}
std::memcpy(ptr, buffer, size*sizeof(nzUInt8));
ptr += size;
if (m_sharedArray->size > 0)
std::memcpy(ptr, &m_sharedArray->buffer[start], m_sharedArray->size - start);
ReleaseArray();
m_sharedArray = new SharedArray;
m_sharedArray->buffer = newBuffer;
m_sharedArray->capacity = newSize;
m_sharedArray->size = newSize;
}
return *this;
}
NzByteArray& NzByteArray::Insert(int pos, const NzByteArray& array)
{
return Insert(pos, array.m_sharedArray->buffer, array.m_sharedArray->size);
}
bool NzByteArray::IsEmpty() const
{
return m_sharedArray->size == 0;
}
NzByteArray& NzByteArray::Prepend(const void* buffer, unsigned int size)
{
return Insert(0, buffer, size);
}
NzByteArray& NzByteArray::Prepend(const NzByteArray& array)
{
return Insert(0, array);
}
void NzByteArray::Reserve(unsigned int bufferSize)
{
if (m_sharedArray->capacity >= bufferSize)
return;
nzUInt8* newBuffer = new nzUInt8[bufferSize];
if (m_sharedArray->size > 0)
std::memcpy(newBuffer, m_sharedArray->buffer, m_sharedArray->size);
unsigned int size = m_sharedArray->size;
ReleaseArray();
m_sharedArray = new SharedArray;
m_sharedArray->buffer = newBuffer;
m_sharedArray->capacity = bufferSize;
m_sharedArray->size = size;
}
NzByteArray& NzByteArray::Resize(int size)
{
if (size == 0)
{
Clear(true);
return *this;
}
if (size < 0)
size = std::max(static_cast<int>(m_sharedArray->size + size), 0);
unsigned int newSize = static_cast<unsigned int>(size);
if (m_sharedArray->capacity >= newSize)
{
EnsureOwnership();
// Nous avons déjà la place requise
m_sharedArray->size = newSize;
}
else // On veut forcément agrandir la chaine
{
nzUInt8* newBuffer = new nzUInt8[newSize];
if (m_sharedArray->size != 0)
std::memcpy(newBuffer, m_sharedArray->buffer, newSize);
ReleaseArray();
m_sharedArray = new SharedArray;
m_sharedArray->buffer = newBuffer;
m_sharedArray->capacity = newSize;
m_sharedArray->size = newSize;
}
return *this;
}
NzByteArray& NzByteArray::Resize(int size, nzUInt8 byte)
{
if (size == 0)
{
Clear(true);
return *this;
}
if (size < 0)
size = std::max(static_cast<int>(m_sharedArray->size + size), 0);
unsigned int newSize = static_cast<unsigned int>(size);
if (m_sharedArray->capacity >= newSize)
{
EnsureOwnership();
// Nous avons déjà la place requise, contentons-nous de remplir le buffer
if (newSize > m_sharedArray->size)
std::memset(&m_sharedArray->buffer[m_sharedArray->size], byte, newSize-m_sharedArray->size);
m_sharedArray->size = newSize;
}
else // On veut forcément agrandir la chaine
{
nzUInt8* newBuffer = new nzUInt8[newSize];
if (m_sharedArray->size != 0)
std::memcpy(newBuffer, m_sharedArray->buffer, newSize);
std::memset(&newBuffer[m_sharedArray->size], byte, newSize-m_sharedArray->size);
ReleaseArray();
m_sharedArray = new SharedArray;
m_sharedArray->buffer = newBuffer;
m_sharedArray->capacity = newSize;
m_sharedArray->size = newSize;
}
return *this;
}
NzByteArray NzByteArray::Resized(int size) const
{
if (size < 0)
size = m_sharedArray->size + size;
if (size <= 0)
return NzByteArray();
unsigned int newSize = static_cast<unsigned int>(size);
if (newSize == m_sharedArray->size)
return *this;
nzUInt8* buffer = new nzUInt8[newSize];
std::memcpy(buffer, m_sharedArray->buffer, (newSize > m_sharedArray->size) ? m_sharedArray->size : newSize);
return NzByteArray(new SharedArray(1, newSize, newSize, buffer));
}
NzByteArray NzByteArray::Resized(int size, nzUInt8 byte) const
{
if (size < 0)
size = m_sharedArray->size + size;
if (size <= 0)
return NzByteArray();
unsigned int newSize = static_cast<unsigned int>(size);
if (newSize == m_sharedArray->size)
return *this;
nzUInt8* buffer = new nzUInt8[newSize];
if (newSize > m_sharedArray->size)
{
std::memcpy(buffer, m_sharedArray->buffer, m_sharedArray->size);
std::memset(&buffer[m_sharedArray->size], byte, newSize - m_sharedArray->size);
}
else
std::memcpy(buffer, m_sharedArray->buffer, newSize);
return NzByteArray(new SharedArray(1, newSize, newSize, buffer));
}
NzByteArray NzByteArray::SubArray(int startPos, int endPos) const
{
if (startPos < 0)
startPos = std::max(m_sharedArray->size+startPos, 0U);
unsigned int start = static_cast<unsigned int>(startPos);
if (endPos < 0)
{
endPos = m_sharedArray->size + endPos;
if (endPos < 0)
return NzByteArray();
}
unsigned int minEnd = std::min(static_cast<unsigned int>(endPos), m_sharedArray->size-1);
if (start > minEnd || start >= m_sharedArray->size)
return NzByteArray();
unsigned int size = minEnd - start + 1;
nzUInt8* buffer = new nzUInt8[size];
std::memcpy(buffer, &m_sharedArray->buffer[start], size);
return NzByteArray(new SharedArray(1, size, size, buffer));
}
void NzByteArray::Swap(NzByteArray& array)
{
std::swap(m_sharedArray, array.m_sharedArray);
}
nzUInt8* NzByteArray::begin()
{
return m_sharedArray->buffer;
}
const nzUInt8* NzByteArray::begin() const
{
return m_sharedArray->buffer;
}
nzUInt8* NzByteArray::end()
{
return &m_sharedArray->buffer[m_sharedArray->size];
}
const nzUInt8* NzByteArray::end() const
{
return &m_sharedArray->buffer[m_sharedArray->size];
}
nzUInt8& NzByteArray::operator[](unsigned int pos)
{
EnsureOwnership();
if (pos >= m_sharedArray->size)
Resize(pos+1);
return m_sharedArray->buffer[pos];
}
nzUInt8 NzByteArray::operator[](unsigned int pos) const
{
#if NAZARA_CORE_SAFE
if (pos >= m_sharedArray->size)
{
NazaraError("Index out of range (" + NzString::Number(pos) + " >= " + NzString::Number(m_sharedArray->size) + ')');
return 0;
}
#endif
return m_sharedArray->buffer[pos];
}
NzByteArray& NzByteArray::operator=(const NzByteArray& array)
{
if (this != &array)
{
ReleaseArray();
m_sharedArray = array.m_sharedArray;
if (m_sharedArray != &emptyArray)
m_sharedArray->refCount++;
}
return *this;
}
NzByteArray& NzByteArray::operator=(NzByteArray&& array) noexcept
{
std::swap(m_sharedArray, array.m_sharedArray);
return *this;
}
NzByteArray NzByteArray::operator+(const NzByteArray& array) const
{
if (array.m_sharedArray->size == 0)
return *this;
if (m_sharedArray->size == 0)
return array;
unsigned int totalSize = m_sharedArray->size + array.m_sharedArray->size;
nzUInt8* buffer = new nzUInt8[totalSize];
std::memcpy(buffer, m_sharedArray->buffer, m_sharedArray->size);
std::memcpy(&buffer[m_sharedArray->size], array.m_sharedArray->buffer, array.m_sharedArray->size);
return NzByteArray(new SharedArray(1, totalSize, totalSize, buffer));
}
NzByteArray& NzByteArray::operator+=(const NzByteArray& array)
{
return Append(array);
}
int NzByteArray::Compare(const NzByteArray& first, const NzByteArray& second)
{
return std::memcmp(first.m_sharedArray->buffer, second.m_sharedArray->buffer, std::min(first.m_sharedArray->size, second.m_sharedArray->size));
}
void NzByteArray::EnsureOwnership()
{
if (m_sharedArray == &emptyArray)
return;
if (m_sharedArray->refCount > 1)
{
m_sharedArray->refCount--;
nzUInt8* buffer = new nzUInt8[m_sharedArray->capacity];
std::memcpy(buffer, m_sharedArray->buffer, m_sharedArray->size);
m_sharedArray = new SharedArray(1, m_sharedArray->capacity, m_sharedArray->size, buffer);
}
out << byteArray.ToString();
return out;
}
bool NzByteArray::FillHash(NzAbstractHash* hash) const
{
hash->Append(m_sharedArray->buffer, m_sharedArray->size);
hash->Append(GetConstBuffer(), GetSize());
return true;
}
void NzByteArray::ReleaseArray()
{
if (m_sharedArray == &emptyArray)
return;
if (--m_sharedArray->refCount == 0)
{
delete[] m_sharedArray->buffer;
delete m_sharedArray;
}
m_sharedArray = &emptyArray;
}
NzByteArray::SharedArray NzByteArray::emptyArray(0, 0, 0, nullptr);
unsigned int NzByteArray::npos(std::numeric_limits<unsigned int>::max());
namespace std
{
void swap(NzByteArray& lhs, NzByteArray& rhs)
{
lhs.Swap(rhs);
}
}

View File

@@ -11,6 +11,7 @@
#include <windows.h>
#elif defined(NAZARA_PLATFORM_POSIX)
#include <cstring>
#include <errno.h>
#endif
#include <Nazara/Core/Debug.hpp>

View File

@@ -32,22 +32,22 @@
NzFile::NzFile() :
m_endianness(nzEndianness_Unknown),
m_impl(nullptr),
m_openMode(0)
m_openMode(nzOpenMode_Current)
{
}
NzFile::NzFile(const NzString& filePath) :
m_endianness(nzEndianness_Unknown),
m_impl(nullptr),
m_openMode(0)
m_openMode(nzOpenMode_Current)
{
SetFile(filePath);
}
NzFile::NzFile(const NzString& filePath, unsigned long openMode) :
NzFile::NzFile(const NzString& filePath, unsigned int openMode) :
m_endianness(nzEndianness_Unknown),
m_impl(nullptr),
m_openMode(0)
m_openMode(openMode)
{
Open(filePath, openMode);
}
@@ -133,7 +133,7 @@ void NzFile::Flush()
return;
}
if ((m_openMode & ReadWrite) == 0 && (m_openMode & WriteOnly) == 0)
if ((m_openMode & nzOpenMode_ReadWrite) == 0 && (m_openMode & nzOpenMode_WriteOnly) == 0)
{
NazaraError("Cannot flush file without write access");
return;
@@ -225,7 +225,7 @@ std::size_t NzFile::Read(void* buffer, std::size_t size)
return 0;
}
if ((m_openMode & ReadOnly) == 0 && (m_openMode & ReadWrite) == 0)
if ((m_openMode & nzOpenMode_ReadOnly) == 0 && (m_openMode & nzOpenMode_ReadWrite) == 0)
{
NazaraError("File not opened with read access");
return 0;
@@ -242,7 +242,7 @@ std::size_t NzFile::Read(void* buffer, std::size_t size)
// Si nous ne devons rien lire, nous avançons simplement
nzUInt64 currentPos = m_impl->GetCursorPos();
m_impl->SetCursorPos(NzFile::AtCurrent, size);
m_impl->SetCursorPos(nzCursorPosition_AtCurrent, size);
return static_cast<std::size_t>(m_impl->GetCursorPos()-currentPos);
}
@@ -281,7 +281,7 @@ bool NzFile::Rename(const NzString& newFilePath)
return success;
}
bool NzFile::Open(unsigned long openMode)
bool NzFile::Open(unsigned int openMode)
{
NazaraLock(m_mutex)
@@ -306,13 +306,13 @@ bool NzFile::Open(unsigned long openMode)
m_impl = impl.release();
if (m_openMode & Text)
if (m_openMode & nzOpenMode_Text)
m_streamOptions |= nzStreamOption_Text;
return true;
}
bool NzFile::Open(const NzString& filePath, unsigned long openMode)
bool NzFile::Open(const NzString& filePath, unsigned int openMode)
{
NazaraLock(m_mutex)
@@ -322,7 +322,7 @@ bool NzFile::Open(const NzString& filePath, unsigned long openMode)
return Open(openMode);
}
bool NzFile::SetCursorPos(CursorPosition pos, nzInt64 offset)
bool NzFile::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{
NazaraLock(m_mutex)
@@ -349,7 +349,7 @@ bool NzFile::SetCursorPos(nzUInt64 offset)
}
#endif
return m_impl->SetCursorPos(AtBegin, offset);
return m_impl->SetCursorPos(nzCursorPosition_AtBegin, offset);
}
void NzFile::SetEndianness(nzEndianness endianness)
@@ -389,7 +389,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
{
NazaraLock(m_mutex)
if (openMode == 0 || openMode == m_openMode)
if (openMode == nzOpenMode_Current || openMode == m_openMode)
return true;
if (IsOpen())
@@ -406,7 +406,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
m_impl = impl.release();
if (m_openMode & Text)
if (m_openMode & nzOpenMode_Text)
m_streamOptions |= nzStreamOption_Text;
}
@@ -417,7 +417,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
bool NzFile::Write(const NzByteArray& byteArray)
{
unsigned int size = byteArray.GetSize();
NzByteArray::size_type size = byteArray.GetSize();
return Write(byteArray.GetConstBuffer(), 1, size) == size;
}
@@ -455,7 +455,7 @@ std::size_t NzFile::Write(const void* buffer, std::size_t typeSize, unsigned int
return 0;
}
if ((m_openMode & ReadWrite) == 0 && (m_openMode & WriteOnly) == 0)
if ((m_openMode & nzOpenMode_ReadWrite) == 0 && (m_openMode & nzOpenMode_WriteOnly) == 0)
{
NazaraError("File not opened with write access");
return 0;
@@ -716,7 +716,7 @@ bool NzFile::Rename(const NzString& sourcePath, const NzString& targetPath)
bool NzFile::FillHash(NzAbstractHash* hash) const
{
NzFile file(m_filePath);
if (!file.Open(NzFile::ReadOnly))
if (!file.Open(nzOpenMode_ReadOnly))
{
NazaraError("Unable to open file");
return false;

View File

@@ -116,6 +116,13 @@ NzString NzHardwareInfo::GetProcessorVendorName()
return vendorNames[s_vendorEnum+1];
}
nzUInt64 NzHardwareInfo::GetTotalMemory()
{
///DOC: Ne nécessite pas l'initialisation de HardwareInfo pour fonctionner
static nzUInt64 totalMemory = NzHardwareInfoImpl::GetTotalMemory();
return totalMemory;
}
bool NzHardwareInfo::HasCapability(nzProcessorCap capability)
{
#ifdef NAZARA_DEBUG

View File

@@ -82,11 +82,12 @@ NzString NzHashDigest::ToHex() const
return NzString();
unsigned int length = m_digestLength*2;
char* hexOutput = new char[length+1];
for (unsigned int i = 0; i < m_digestLength; ++i)
std::sprintf(hexOutput + i*2, "%02x", m_digest[i]);
return NzString(new NzString::SharedString(1, length, length, hexOutput));
NzString hexOutput(length);
for (unsigned int i = 0; i < m_digestLength; ++i)
std::sprintf(&hexOutput[i*2], "%02x", m_digest[i]);
return hexOutput;
}
nzUInt8 NzHashDigest::operator[](unsigned int pos) const

View File

@@ -114,7 +114,7 @@ void NzLog::Write(const NzString& string)
if (m_enabled)
{
if (!m_file)
m_file = new NzFile(m_filePath, NzFile::Text | NzFile::WriteOnly | ((m_append) ? NzFile::Append : NzFile::Truncate));
m_file = new NzFile(m_filePath, nzOpenMode_Text | nzOpenMode_WriteOnly | ((m_append) ? nzOpenMode_Append : nzOpenMode_Truncate));
NzString line;

View File

@@ -6,6 +6,7 @@
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <new>
#include <stdexcept>
#if defined(NAZARA_PLATFORM_WINDOWS)

View File

@@ -6,6 +6,9 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Debug.hpp>
#include <errno.h>
#include <sys/param.h>
NzDirectoryImpl::NzDirectoryImpl(const NzDirectory* parent)
{
NazaraUnused(parent);
@@ -82,13 +85,10 @@ bool NzDirectoryImpl::Exists(const NzString& dirPath)
NzString NzDirectoryImpl::GetCurrent()
{
NzString currentPath;
char* path = getcwd(nullptr, 0);
if (path)
{
char path[MAXPATHLEN];
if (getcwd(path, MAXPATHLEN))
currentPath = path;
free(path);
}
else
NazaraError("Unable to get current directory: " + NzError::GetLastSystemError()); // Bug: initialisation -> if no path for log !

View File

@@ -4,9 +4,7 @@
#include <Nazara/Core/Posix/FileImpl.hpp>
#include <Nazara/Core/Error.hpp>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <cstdio>
#include <Nazara/Core/Debug.hpp>
NzFileImpl::NzFileImpl(const NzFile* parent) :
@@ -53,33 +51,33 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
int flags;
mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if (mode & NzFile::ReadOnly)
if (mode & nzOpenMode_ReadOnly)
flags = O_RDONLY;
else if (mode & NzFile::ReadWrite)
else if (mode & nzOpenMode_ReadWrite)
{
flags = O_CREAT | O_RDWR;
if (mode & NzFile::Append)
if (mode & nzOpenMode_Append)
flags |= O_APPEND;
if (mode & NzFile::Truncate)
if (mode & nzOpenMode_Truncate)
flags |= O_TRUNC;
}
else if (mode & NzFile::WriteOnly)
else if (mode & nzOpenMode_WriteOnly)
{
flags = O_CREAT | O_WRONLY;
if (mode & NzFile::Append)
if (mode & nzOpenMode_Append)
flags |= O_APPEND;
if (mode & NzFile::Truncate)
if (mode & nzOpenMode_Truncate)
flags |= O_TRUNC;
}
else
return false;
///TODO: lock
// if ((mode & NzFile::Lock) == 0)
// if ((mode & nzOpenMode_Lock) == 0)
// shareMode |= FILE_SHARE_WRITE;
m_fileDescriptor = open64(filePath.GetConstBuffer(), flags, permissions);
@@ -100,20 +98,20 @@ std::size_t NzFileImpl::Read(void* buffer, std::size_t size)
return 0;
}
bool NzFileImpl::SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset)
bool NzFileImpl::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{
int moveMethod;
switch (pos)
{
case NzFile::AtBegin:
case nzCursorPosition_AtBegin:
moveMethod = SEEK_SET;
break;
case NzFile::AtCurrent:
case nzCursorPosition_AtCurrent:
moveMethod = SEEK_CUR;
break;
case NzFile::AtEnd:
case nzCursorPosition_AtEnd:
moveMethod = SEEK_END;
break;

View File

@@ -35,7 +35,7 @@ class NzFileImpl : NzNonCopyable
nzUInt64 GetCursorPos() const;
bool Open(const NzString& filePath, unsigned int mode);
std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset);
bool SetCursorPos(nzCursorPosition pos, nzInt64 offset);
std::size_t Write(const void* buffer, std::size_t size);
static bool Copy(const NzString& sourcePath, const NzString& targetPath);

View File

@@ -6,16 +6,16 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Debug.hpp>
void NzHardwareInfoImpl::Cpuid(nzUInt32 code, nzUInt32 result[4])
void NzHardwareInfoImpl::Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4])
{
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
// Source: http://stackoverflow.com/questions/1666093/cpuid-implementations-in-c
asm volatile ("cpuid" // Besoin d'être volatile ?
: "=a" (result[0]), "=b" (result[1]), "=c" (result[2]), "=d" (result[3]) // output
: "a" (code), "c" (0)); // input
#else
: "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) // output
: "a" (functionId), "c" (subFunctionId)); // input
#else
NazaraInternalError("Cpuid has been called although it is not supported");
#endif
#endif
}
unsigned int NzHardwareInfoImpl::GetProcessorCount()
@@ -24,32 +24,40 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
return sysconf(_SC_NPROCESSORS_CONF);
}
nzUInt64 NzHardwareInfoImpl::GetTotalMemory()
{
nzUInt64 pages = sysconf(_SC_PHYS_PAGES);
nzUInt64 page_size = sysconf(_SC_PAGE_SIZE);
return pages * page_size;
}
bool NzHardwareInfoImpl::IsCpuidSupported()
{
#ifdef NAZARA_PLATFORM_x64
#ifdef NAZARA_PLATFORM_x64
return true; // Toujours supporté sur un processeur 64 bits
#else
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
int supported;
asm volatile (" pushfl\n"
" pop %%eax\n"
" mov %%eax, %%ecx\n"
" xor $0x200000, %%eax\n"
" push %%eax\n"
" popfl\n"
" pushfl\n"
" pop %%eax\n"
" xor %%ecx, %%eax\n"
" mov %%eax, %0\n"
" push %%ecx\n"
" popfl"
: "=m" (supported) // output
: // input
: "eax", "ecx", "memory"); // clobbered register
#else
#if defined(NAZARA_COMPILER_CLANG) || defined(NAZARA_COMPILER_GCC) || defined(NAZARA_COMPILER_INTEL)
int supported;
asm volatile (" pushfl\n"
" pop %%eax\n"
" mov %%eax, %%ecx\n"
" xor $0x200000, %%eax\n"
" push %%eax\n"
" popfl\n"
" pushfl\n"
" pop %%eax\n"
" xor %%ecx, %%eax\n"
" mov %%eax, %0\n"
" push %%ecx\n"
" popfl"
: "=m" (supported) // output
: // input
: "eax", "ecx", "memory"); // clobbered register
return supported != 0;
#else
return false;
#endif
return supported != 0;
#else
return false;
#endif
#endif
}

View File

@@ -13,8 +13,9 @@
class NzHardwareInfoImpl
{
public:
static void Cpuid(nzUInt32 code, nzUInt32 result[4]);
static void Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4]);
static unsigned int GetProcessorCount();
static nzUInt64 GetTotalMemory();
static bool IsCpuidSupported();
};

View File

@@ -0,0 +1,192 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Posix/TaskSchedulerImpl.hpp>
#include <Nazara/Core/Config.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Debug.hpp>
bool NzTaskSchedulerImpl::Initialize(unsigned int workerCount)
{
if (IsInitialized())
return true; // Déjà initialisé
#if NAZARA_CORE_SAFE
if (workerCount == 0)
{
NazaraError("Invalid worker count ! (0)");
return false;
}
#endif
s_workerCount = workerCount;
s_isDone = false;
s_isWaiting = false;
s_shouldFinish = false;
s_threads.reset(new pthread_t[workerCount]);
// On initialise les conditions variables, mutex et barrière.
pthread_cond_init(&s_cvEmpty, nullptr);
pthread_cond_init(&s_cvNotEmpty, nullptr);
pthread_mutex_init(&s_mutexQueue, nullptr);
pthread_barrier_init(&s_barrier, nullptr, workerCount + 1);
for (unsigned int i = 0; i < s_workerCount; ++i)
{
// Le thread va se lancer, attendre que tous se créent et attendre d'être réveillé.
pthread_create(&s_threads[i], nullptr, WorkerProc, nullptr);
}
pthread_barrier_wait(&s_barrier); // On attend que les enfants soient bien créés.
return true;
}
bool NzTaskSchedulerImpl::IsInitialized()
{
return s_workerCount > 0;
}
void NzTaskSchedulerImpl::Run(NzFunctor** tasks, unsigned int count)
{
// On s'assure que des tâches ne sont pas déjà en cours
Wait();
pthread_mutex_lock(&s_mutexQueue);
s_isDone = false;
while (count--)
s_tasks.push(*tasks++);
pthread_cond_signal(&s_cvNotEmpty);
pthread_mutex_unlock(&s_mutexQueue);
}
void NzTaskSchedulerImpl::Uninitialize()
{
#ifdef NAZARA_CORE_SAFE
if (s_workerCount == 0)
{
NazaraError("Task scheduler is not initialized");
return;
}
#endif
// On réveille les threads pour qu'ils sortent de la boucle et terminent.
pthread_mutex_lock(&s_mutexQueue);
// On commence par vider la queue et demander qu'ils s'arrêtent.
std::queue<NzFunctor*> emptyQueue;
std::swap(s_tasks, emptyQueue);
s_shouldFinish = true;
pthread_cond_broadcast(&s_cvNotEmpty);
pthread_mutex_unlock(&s_mutexQueue);
// On attend que chaque thread se termine
for (unsigned int i = 0; i < s_workerCount; ++i)
pthread_join(s_threads[i], nullptr);
// Et on libère les ressources
pthread_barrier_destroy(&s_barrier);
pthread_cond_destroy(&s_cvEmpty);
pthread_cond_destroy(&s_cvNotEmpty);
pthread_mutex_destroy(&s_mutexQueue);
s_workerCount = 0;
}
void NzTaskSchedulerImpl::WaitForTasks()
{
#ifdef NAZARA_CORE_SAFE
if (s_workerCount == 0)
{
NazaraError("Task scheduler is not initialized");
return;
}
#endif
Wait();
}
NzFunctor* NzTaskSchedulerImpl::PopQueue()
{
NzFunctor* task = nullptr;
pthread_mutex_lock(&s_mutexQueue);
if (!s_tasks.empty())
{
task = s_tasks.front();
s_tasks.pop();
}
pthread_mutex_unlock(&s_mutexQueue);
return task;
}
void NzTaskSchedulerImpl::Wait()
{
if (s_isDone)
return;
pthread_mutex_lock(&s_mutexQueue);
s_isWaiting = true;
pthread_cond_broadcast(&s_cvNotEmpty);
pthread_cond_wait(&s_cvEmpty, &s_mutexQueue);
pthread_mutex_unlock(&s_mutexQueue);
s_isDone = true;
}
void* NzTaskSchedulerImpl::WorkerProc(void* /*userdata*/)
{
// On s'assure que tous les threads soient correctement lancés.
pthread_barrier_wait(&s_barrier);
// On quitte s'il doit terminer.
while (!s_shouldFinish)
{
NzFunctor* task = PopQueue();
if (task)
{
// On exécute la tâche avant de la supprimer
task->Run();
delete task;
}
else
{
pthread_mutex_lock(&s_mutexQueue);
if (s_tasks.empty())
s_isDone = true;
while (!(!s_tasks.empty() || s_isWaiting || s_shouldFinish))
pthread_cond_wait(&s_cvNotEmpty, &s_mutexQueue);
if (s_tasks.empty() && s_isWaiting)
{
// On prévient le thread qui attend que les tâches soient effectuées.
s_isWaiting = false;
pthread_cond_signal(&s_cvEmpty);
}
pthread_mutex_unlock(&s_mutexQueue);
}
}
return nullptr;
}
std::queue<NzFunctor*> NzTaskSchedulerImpl::s_tasks;
std::unique_ptr<pthread_t[]> NzTaskSchedulerImpl::s_threads;
std::atomic<bool> NzTaskSchedulerImpl::s_isDone;
std::atomic<bool> NzTaskSchedulerImpl::s_isWaiting;
std::atomic<bool> NzTaskSchedulerImpl::s_shouldFinish;
unsigned int NzTaskSchedulerImpl::s_workerCount;
pthread_mutex_t NzTaskSchedulerImpl::s_mutexQueue;
pthread_cond_t NzTaskSchedulerImpl::s_cvEmpty;
pthread_cond_t NzTaskSchedulerImpl::s_cvNotEmpty;
pthread_barrier_t NzTaskSchedulerImpl::s_barrier;

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TASKSCHEDULERIMPL_HPP
#define NAZARA_TASKSCHEDULERIMPL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Functor.hpp>
#include <atomic>
#include <memory>
#include <pthread.h>
#include <queue>
class NzTaskSchedulerImpl
{
public:
NzTaskSchedulerImpl() = delete;
~NzTaskSchedulerImpl() = delete;
static bool Initialize(unsigned int workerCount);
static bool IsInitialized();
static void Run(NzFunctor** tasks, unsigned int count);
static void Uninitialize();
static void WaitForTasks();
private:
static NzFunctor* PopQueue();
static void Wait();
static void* WorkerProc(void* userdata);
static std::queue<NzFunctor*> s_tasks;
static std::unique_ptr<pthread_t[]> s_threads;
static std::atomic<bool> s_isDone;
static std::atomic<bool> s_isWaiting;
static std::atomic<bool> s_shouldFinish;
static unsigned int s_workerCount;
static pthread_mutex_t s_mutexQueue;
static pthread_cond_t s_cvEmpty;
static pthread_cond_t s_cvNotEmpty;
static pthread_barrier_t s_barrier;
};
#endif // NAZARA_TASKSCHEDULERIMPL_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -57,31 +57,31 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
DWORD access;
DWORD shareMode = FILE_SHARE_READ;
DWORD openMode;
if (mode & NzFile::ReadOnly)
if (mode & nzOpenMode_ReadOnly)
{
access = GENERIC_READ;
openMode = OPEN_EXISTING;
}
else if (mode & NzFile::ReadWrite)
else if (mode & nzOpenMode_ReadWrite)
{
if (mode & NzFile::Append)
if (mode & nzOpenMode_Append)
access = FILE_APPEND_DATA;
else
access = GENERIC_READ | GENERIC_WRITE;
if (mode & NzFile::Truncate)
if (mode & nzOpenMode_Truncate)
openMode = CREATE_ALWAYS;
else
openMode = OPEN_ALWAYS;
}
else if (mode & NzFile::WriteOnly)
else if (mode & nzOpenMode_WriteOnly)
{
if (mode & NzFile::Append)
if (mode & nzOpenMode_Append)
access = FILE_APPEND_DATA;
else
access = GENERIC_WRITE;
if (mode & NzFile::Truncate)
if (mode & nzOpenMode_Truncate)
openMode = CREATE_ALWAYS;
else
openMode = OPEN_ALWAYS;
@@ -89,7 +89,7 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
else
return false;
if ((mode & NzFile::Lock) == 0)
if ((mode & nzOpenMode_Lock) == 0)
shareMode |= FILE_SHARE_WRITE;
m_handle = CreateFileW(filePath.GetWideString().data(), access, shareMode, nullptr, openMode, 0, nullptr);
@@ -134,20 +134,20 @@ std::size_t NzFileImpl::Read(void* buffer, std::size_t size)
return 0;
}
bool NzFileImpl::SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset)
bool NzFileImpl::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{
DWORD moveMethod;
switch (pos)
{
case NzFile::AtBegin:
case nzCursorPosition_AtBegin:
moveMethod = FILE_BEGIN;
break;
case NzFile::AtCurrent:
case nzCursorPosition_AtCurrent:
moveMethod = FILE_CURRENT;
break;
case NzFile::AtEnd:
case nzCursorPosition_AtEnd:
moveMethod = FILE_END;
break;

View File

@@ -28,7 +28,7 @@ class NzFileImpl : NzNonCopyable
nzUInt64 GetCursorPos() const;
bool Open(const NzString& filePath, unsigned int mode);
std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset);
bool SetCursorPos(nzCursorPosition pos, nzInt64 offset);
std::size_t Write(const void* buffer, std::size_t size);
static bool Copy(const NzString& sourcePath, const NzString& targetPath);

View File

@@ -33,11 +33,20 @@ unsigned int NzHardwareInfoImpl::GetProcessorCount()
{
// Plus simple (et plus portable) que de passer par le CPUID
SYSTEM_INFO infos;
GetSystemInfo(&infos);
GetNativeSystemInfo(&infos);
return infos.dwNumberOfProcessors;
}
nzUInt64 NzHardwareInfoImpl::GetTotalMemory()
{
MEMORYSTATUSEX memStatus;
memStatus.dwLength = sizeof(memStatus);
GlobalMemoryStatusEx(&memStatus);
return memStatus.ullTotalPhys;
}
bool NzHardwareInfoImpl::IsCpuidSupported()
{
#ifdef NAZARA_PLATFORM_x64

View File

@@ -14,6 +14,7 @@ class NzHardwareInfoImpl
public:
static void Cpuid(nzUInt32 functionId, nzUInt32 subFunctionId, nzUInt32 registers[4]);
static unsigned int GetProcessorCount();
static nzUInt64 GetTotalMemory();
static bool IsCpuidSupported();
};

View File

@@ -38,7 +38,7 @@ namespace
bool LoadMaterials(NzModel* model, const NzString& filePath, const NzMaterialParams& parameters, const NzString* materials, const NzOBJParser::Mesh* meshes, unsigned int meshCount)
{
NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text))
if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{
NazaraError("Failed to open MTL file (" + file.GetPath() + ')');
return false;

View File

@@ -103,7 +103,8 @@ bool NzSkyboxBackground::Initialize()
"void main()\n"
"{\n"
" gl_Position = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n"
" vec4 WVPVertex = WorldViewProjMatrix * vec4(VertexPosition, 1.0);\n"
" gl_Position = WVPVertex.xyww;\n"
" vTexCoord = vec3(VertexPosition.x, VertexPosition.y, -VertexPosition.z);\n"
"}\n";

View File

@@ -393,7 +393,7 @@ bool NzLuaInstance::Execute(const NzString& code)
bool NzLuaInstance::ExecuteFromFile(const NzString& filePath)
{
NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text))
if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{
NazaraError("Failed to open file");
return false;
@@ -624,7 +624,7 @@ void NzLuaInstance::PushBoolean(bool value)
lua_pushboolean(m_state, (value) ? 1 : 0);
}
void NzLuaInstance::PushCFunction(NzLuaCFunction func, int upvalueCount)
void NzLuaInstance::PushCFunction(NzLuaCFunction func, unsigned int upvalueCount)
{
lua_pushcclosure(m_state, func, upvalueCount);
}
@@ -677,6 +677,11 @@ void NzLuaInstance::PushString(const char* str)
lua_pushstring(m_state, str);
}
void NzLuaInstance::PushString(const char* str, unsigned int size)
{
lua_pushlstring(m_state, str, size);
}
void NzLuaInstance::PushString(const NzString& str)
{
lua_pushlstring(m_state, str.GetConstBuffer(), str.GetSize());

View File

@@ -367,6 +367,25 @@ void NzDebugDrawer::Draw(const NzVector3f& position, float size)
Draw(NzBoxf(position.x - size*0.5f, position.y - size*0.5f, position.z - size*0.5f, size, size, size));
}
void NzDebugDrawer::DrawAxes(const NzVector3f& position, float size)
{
NzColor oldPrimaryColor = s_primaryColor;
s_primaryColor = NzColor::Red;
DrawLine(position, position + NzVector3f::UnitX() * 3.f * size / 4.f);
s_primaryColor = NzColor::Green;
DrawLine(position, position + NzVector3f::UnitY() * 3.f * size / 4.f);
s_primaryColor = NzColor::Blue;
DrawLine(position, position + NzVector3f::UnitZ() * 3.f * size / 4.f);
s_primaryColor = NzColor::Red;
DrawCone(position + NzVector3f::UnitX() * size, NzEulerAnglesf(0.f, 90.f, 0.f), 15, size / 4.f);
s_primaryColor = NzColor::Green;
DrawCone(position + NzVector3f::UnitY() * size, NzEulerAnglesf(-90.f, 0.f, 0.f), 15, size / 4.f);
s_primaryColor = NzColor::Blue;
DrawCone(position + NzVector3f::UnitZ() * size, NzEulerAnglesf(0.f, 0.f, 0.f), 15, size / 4.f);
s_primaryColor = oldPrimaryColor;
}
void NzDebugDrawer::DrawBinormals(const NzStaticMesh* subMesh)
{
if (!Initialize())

View File

@@ -2002,7 +2002,8 @@ void NzRenderer::UpdateMatrix(nzMatrixType type)
// Matrices combinées
case nzMatrixType_ViewProj:
s_matrices[nzMatrixType_ViewProj].matrix = s_matrices[nzMatrixType_View].matrix * s_matrices[nzMatrixType_Projection].matrix;
s_matrices[nzMatrixType_ViewProj].matrix = s_matrices[nzMatrixType_View].matrix;
s_matrices[nzMatrixType_ViewProj].matrix.Concatenate(s_matrices[nzMatrixType_Projection].matrix);
s_matrices[nzMatrixType_ViewProj].updated = true;
break;

View File

@@ -199,7 +199,7 @@ NzByteArray NzShader::GetBinary() const
if (binaryLength > 0)
{
byteArray.Resize(sizeof(nzUInt64) + binaryLength);
byteArray.Reserve(sizeof(nzUInt64) + binaryLength);
nzUInt8* buffer = byteArray.GetBuffer();

View File

@@ -174,7 +174,7 @@ bool NzShaderStage::SetSourceFromFile(const NzString& filePath)
#endif
NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text))
if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{
NazaraError("Failed to open \"" + filePath + '"');
return false;

View File

@@ -156,7 +156,7 @@ void NzUberShaderPreprocessor::SetShader(nzShaderStage stage, const NzString& so
bool NzUberShaderPreprocessor::SetShaderFromFile(nzShaderStage stage, const NzString& filePath, const NzString& shaderFlags, const NzString& requiredFlags)
{
NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text))
if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{
NazaraError("Failed to open \"" + filePath + '"');
return false;

View File

@@ -279,7 +279,7 @@ namespace
bool SetFile(const NzString& filePath)
{
std::unique_ptr<NzFile> file(new NzFile);
if (!file->Open(filePath, NzFile::ReadOnly))
if (!file->Open(filePath, nzOpenMode_ReadOnly))
{
NazaraError("Failed to open stream from file: " + NzError::GetLastError());
return false;

View File

@@ -53,7 +53,7 @@ bool NzVertexBuffer::FillRaw(const void* data, unsigned int offset, unsigned int
if (!m_buffer)
{
NazaraError("No buffer");
return nullptr;
return false;
}
if (m_startOffset + offset + size > m_endOffset)