Merge changes from The Witness.
This commit is contained in:
parent
fea97461c5
commit
3b4fcd0369
|
@ -30,6 +30,7 @@ namespace nv
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class NVCORE_CLASS Array {
|
class NVCORE_CLASS Array {
|
||||||
public:
|
public:
|
||||||
|
typedef uint size_type;
|
||||||
|
|
||||||
// Default constructor.
|
// Default constructor.
|
||||||
NV_FORCEINLINE Array() : m_buffer(NULL), m_capacity(0), m_size(0) {}
|
NV_FORCEINLINE Array() : m_buffer(NULL), m_capacity(0), m_size(0) {}
|
||||||
|
@ -86,12 +87,18 @@ namespace nv
|
||||||
/// Get vector size.
|
/// Get vector size.
|
||||||
NV_FORCEINLINE uint count() const { return m_size; }
|
NV_FORCEINLINE uint count() const { return m_size; }
|
||||||
|
|
||||||
|
/// Get vector capacity.
|
||||||
|
NV_FORCEINLINE uint capacity() const { return m_capacity; }
|
||||||
|
|
||||||
/// Get const vector pointer.
|
/// Get const vector pointer.
|
||||||
NV_FORCEINLINE const T * buffer() const { return m_buffer; }
|
NV_FORCEINLINE const T * buffer() const { return m_buffer; }
|
||||||
|
|
||||||
/// Get vector pointer.
|
/// Get vector pointer.
|
||||||
NV_FORCEINLINE T * buffer() { return m_buffer; }
|
NV_FORCEINLINE T * buffer() { return m_buffer; }
|
||||||
|
|
||||||
|
NV_FORCEINLINE T * begin() { return m_buffer; }
|
||||||
|
NV_FORCEINLINE T * end() { return m_buffer + m_size; }
|
||||||
|
|
||||||
/// Is vector empty.
|
/// Is vector empty.
|
||||||
NV_FORCEINLINE bool isEmpty() const { return m_size == 0; }
|
NV_FORCEINLINE bool isEmpty() const { return m_size == 0; }
|
||||||
|
|
||||||
|
@ -120,6 +127,7 @@ namespace nv
|
||||||
void replaceWithLast(uint index);
|
void replaceWithLast(uint index);
|
||||||
void resize(uint new_size);
|
void resize(uint new_size);
|
||||||
void resize(uint new_size, const T & elem);
|
void resize(uint new_size, const T & elem);
|
||||||
|
void fill(const T & elem);
|
||||||
void clear();
|
void clear();
|
||||||
void shrink();
|
void shrink();
|
||||||
void reserve(uint desired_size);
|
void reserve(uint desired_size);
|
||||||
|
|
|
@ -249,10 +249,19 @@ namespace nv
|
||||||
construct_range(m_buffer, new_size, old_size, elem);
|
construct_range(m_buffer, new_size, old_size, elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fill array with the given value.
|
||||||
|
template <typename T>
|
||||||
|
void Array<T>::fill(const T & elem)
|
||||||
|
{
|
||||||
|
fill(m_buffer, m_size, elem)
|
||||||
|
}
|
||||||
|
|
||||||
// Clear the buffer.
|
// Clear the buffer.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
NV_FORCEINLINE void Array<T>::clear()
|
NV_FORCEINLINE void Array<T>::clear()
|
||||||
{
|
{
|
||||||
|
nvDebugCheck(isValidPtr(m_buffer));
|
||||||
|
|
||||||
// Destruct old elements
|
// Destruct old elements
|
||||||
destroy_range(m_buffer, 0, m_size);
|
destroy_range(m_buffer, 0, m_size);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
|
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
|
||||||
|
|
||||||
#include "Debug.h"
|
#include "Debug.h"
|
||||||
|
#include "Array.inl"
|
||||||
#include "StrLib.h" // StringBuilder
|
#include "StrLib.h" // StringBuilder
|
||||||
|
|
||||||
|
#include "StdStream.h" // fileOpen
|
||||||
|
|
||||||
// Extern
|
// Extern
|
||||||
#if NV_OS_WIN32 //&& NV_CC_MSVC
|
#if NV_OS_WIN32 //&& NV_CC_MSVC
|
||||||
# define WIN32_LEAN_AND_MEAN
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
@ -58,6 +61,9 @@
|
||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define USE_SEPARATE_THREAD 1
|
||||||
|
|
||||||
|
|
||||||
using namespace nv;
|
using namespace nv;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
@ -67,6 +73,7 @@ namespace
|
||||||
static AssertHandler * s_assert_handler = NULL;
|
static AssertHandler * s_assert_handler = NULL;
|
||||||
|
|
||||||
static bool s_sig_handler_enabled = false;
|
static bool s_sig_handler_enabled = false;
|
||||||
|
static bool s_interactive = true;
|
||||||
|
|
||||||
#if NV_OS_WIN32 && NV_CC_MSVC
|
#if NV_OS_WIN32 && NV_CC_MSVC
|
||||||
|
|
||||||
|
@ -86,12 +93,82 @@ namespace
|
||||||
|
|
||||||
#if NV_OS_WIN32 && NV_CC_MSVC
|
#if NV_OS_WIN32 && NV_CC_MSVC
|
||||||
|
|
||||||
|
// We should try to simplify the top level filter as much as possible.
|
||||||
|
// http://www.nynaeve.net/?p=128
|
||||||
|
|
||||||
|
#if USE_SEPARATE_THREAD
|
||||||
|
|
||||||
|
// The critical section enforcing the requirement that only one exception be
|
||||||
|
// handled by a handler at a time.
|
||||||
|
static CRITICAL_SECTION s_handler_critical_section;
|
||||||
|
|
||||||
|
// Semaphores used to move exception handling between the exception thread
|
||||||
|
// and the handler thread. handler_start_semaphore_ is signalled by the
|
||||||
|
// exception thread to wake up the handler thread when an exception occurs.
|
||||||
|
// handler_finish_semaphore_ is signalled by the handler thread to wake up
|
||||||
|
// the exception thread when handling is complete.
|
||||||
|
static HANDLE s_handler_start_semaphore = NULL;
|
||||||
|
static HANDLE s_handler_finish_semaphore = NULL;
|
||||||
|
|
||||||
|
// The exception handler thread.
|
||||||
|
static HANDLE s_handler_thread = NULL;
|
||||||
|
|
||||||
|
static DWORD s_requesting_thread_id = 0;
|
||||||
|
static EXCEPTION_POINTERS * s_exception_info = NULL;
|
||||||
|
|
||||||
|
#endif // USE_SEPARATE_THREAD
|
||||||
|
|
||||||
|
|
||||||
|
struct MinidumpCallbackContext {
|
||||||
|
ULONG64 memory_base;
|
||||||
|
ULONG memory_size;
|
||||||
|
bool finished;
|
||||||
|
};
|
||||||
|
|
||||||
|
// static
|
||||||
|
static BOOL CALLBACK miniDumpWriteDumpCallback(PVOID context, const PMINIDUMP_CALLBACK_INPUT callback_input, PMINIDUMP_CALLBACK_OUTPUT callback_output)
|
||||||
|
{
|
||||||
|
switch (callback_input->CallbackType)
|
||||||
|
{
|
||||||
|
case MemoryCallback: {
|
||||||
|
MinidumpCallbackContext* callback_context = reinterpret_cast<MinidumpCallbackContext*>(context);
|
||||||
|
if (callback_context->finished)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
// Include the specified memory region.
|
||||||
|
callback_output->MemoryBase = callback_context->memory_base;
|
||||||
|
callback_output->MemorySize = callback_context->memory_size;
|
||||||
|
callback_context->finished = true;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include all modules.
|
||||||
|
case IncludeModuleCallback:
|
||||||
|
case ModuleCallback:
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
// Include all threads.
|
||||||
|
case IncludeThreadCallback:
|
||||||
|
case ThreadCallback:
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
// Stop receiving cancel callbacks.
|
||||||
|
case CancelCallback:
|
||||||
|
callback_output->CheckCancel = FALSE;
|
||||||
|
callback_output->Cancel = FALSE;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore other callback types.
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
static bool writeMiniDump(EXCEPTION_POINTERS * pExceptionInfo)
|
static bool writeMiniDump(EXCEPTION_POINTERS * pExceptionInfo)
|
||||||
{
|
{
|
||||||
// create the file
|
// create the file
|
||||||
HANDLE hFile = CreateFileA("crash.dmp", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
HANDLE hFile = CreateFileA("crash.dmp", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
if (hFile == INVALID_HANDLE_VALUE) {
|
if (hFile == INVALID_HANDLE_VALUE) {
|
||||||
nvDebug("*** Failed to create dump file.\n");
|
//nvDebug("*** Failed to create dump file.\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,20 +177,77 @@ namespace
|
||||||
ExInfo.ExceptionPointers = pExceptionInfo;
|
ExInfo.ExceptionPointers = pExceptionInfo;
|
||||||
ExInfo.ClientPointers = NULL;
|
ExInfo.ClientPointers = NULL;
|
||||||
|
|
||||||
|
MINIDUMP_CALLBACK_INFORMATION callback;
|
||||||
|
MINIDUMP_CALLBACK_INFORMATION * callback_pointer = NULL;
|
||||||
|
MinidumpCallbackContext context;
|
||||||
|
|
||||||
|
// Find a memory region of 256 bytes centered on the
|
||||||
|
// faulting instruction pointer.
|
||||||
|
const ULONG64 instruction_pointer =
|
||||||
|
#if defined(_M_IX86)
|
||||||
|
pExceptionInfo->ContextRecord->Eip;
|
||||||
|
#elif defined(_M_AMD64)
|
||||||
|
pExceptionInfo->ContextRecord->Rip;
|
||||||
|
#else
|
||||||
|
#error Unsupported platform
|
||||||
|
#endif
|
||||||
|
|
||||||
|
MEMORY_BASIC_INFORMATION info;
|
||||||
|
|
||||||
|
if (VirtualQuery(reinterpret_cast<LPCVOID>(instruction_pointer), &info, sizeof(MEMORY_BASIC_INFORMATION)) != 0 && info.State == MEM_COMMIT)
|
||||||
|
{
|
||||||
|
// Attempt to get 128 bytes before and after the instruction
|
||||||
|
// pointer, but settle for whatever's available up to the
|
||||||
|
// boundaries of the memory region.
|
||||||
|
const ULONG64 kIPMemorySize = 256;
|
||||||
|
context.memory_base = max(reinterpret_cast<ULONG64>(info.BaseAddress), instruction_pointer - (kIPMemorySize / 2));
|
||||||
|
ULONG64 end_of_range = min(instruction_pointer + (kIPMemorySize / 2), reinterpret_cast<ULONG64>(info.BaseAddress) + info.RegionSize);
|
||||||
|
context.memory_size = static_cast<ULONG>(end_of_range - context.memory_base);
|
||||||
|
context.finished = false;
|
||||||
|
|
||||||
|
callback.CallbackRoutine = miniDumpWriteDumpCallback;
|
||||||
|
callback.CallbackParam = reinterpret_cast<void*>(&context);
|
||||||
|
callback_pointer = &callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
MINIDUMP_TYPE miniDumpType = (MINIDUMP_TYPE)(MiniDumpNormal|MiniDumpWithHandleData|MiniDumpWithThreadInfo);
|
||||||
|
|
||||||
// write the dump
|
// write the dump
|
||||||
BOOL ok = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL) != 0;
|
BOOL ok = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, miniDumpType, &ExInfo, NULL, callback_pointer) != 0;
|
||||||
CloseHandle(hFile);
|
CloseHandle(hFile);
|
||||||
|
|
||||||
if (ok == FALSE) {
|
if (ok == FALSE) {
|
||||||
nvDebug("*** Failed to save dump file.\n");
|
//nvDebug("*** Failed to save dump file.\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
nvDebug("\nDump file saved.\n");
|
//nvDebug("\nDump file saved.\n");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if USE_SEPARATE_THREAD
|
||||||
|
|
||||||
|
static DWORD WINAPI ExceptionHandlerThreadMain(void* lpParameter) {
|
||||||
|
nvDebugCheck(s_handler_start_semaphore != NULL);
|
||||||
|
nvDebugCheck(s_handler_finish_semaphore != NULL);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (WaitForSingleObject(s_handler_start_semaphore, INFINITE) == WAIT_OBJECT_0) {
|
||||||
|
writeMiniDump(s_exception_info);
|
||||||
|
|
||||||
|
// Allow the requesting thread to proceed.
|
||||||
|
ReleaseSemaphore(s_handler_finish_semaphore, 1, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This statement is not reached when the thread is unconditionally
|
||||||
|
// terminated by the ExceptionHandler destructor.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // USE_SEPARATE_THREAD
|
||||||
|
|
||||||
static bool hasStackTrace() {
|
static bool hasStackTrace() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -195,11 +329,11 @@ namespace
|
||||||
}
|
}
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
|
|
||||||
static NV_NOINLINE void printStackTrace(void * trace[], int size, int start=0)
|
static NV_NOINLINE void writeStackTrace(void * trace[], int size, int start, Array<const char *> & lines)
|
||||||
{
|
{
|
||||||
HANDLE hProcess = GetCurrentProcess();
|
StringBuilder builder(512);
|
||||||
|
|
||||||
nvDebug( "\nDumping stacktrace:\n" );
|
HANDLE hProcess = GetCurrentProcess();
|
||||||
|
|
||||||
// Resolve PC to function names
|
// Resolve PC to function names
|
||||||
for (int i = start; i < size; i++)
|
for (int i = start; i < size; i++)
|
||||||
|
@ -243,7 +377,7 @@ namespace
|
||||||
DWORD dwDisplacement;
|
DWORD dwDisplacement;
|
||||||
if (!SymGetLineFromAddr64(hProcess, ip, &dwDisplacement, &theLine))
|
if (!SymGetLineFromAddr64(hProcess, ip, &dwDisplacement, &theLine))
|
||||||
{
|
{
|
||||||
nvDebug("unknown(%08X) : %s\n", (uint32)ip, pFunc);
|
builder.format("unknown(%08X) : %s\n", (uint32)ip, pFunc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -256,26 +390,128 @@ namespace
|
||||||
|
|
||||||
int line = theLine.LineNumber;
|
int line = theLine.LineNumber;
|
||||||
|
|
||||||
nvDebug("%s(%d) : %s\n", pFile, line, pFunc);
|
builder.format("%s(%d) : %s\n", pFile, line, pFunc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lines.append(builder.release());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Write mini dump and print stack trace.
|
// Write mini dump and print stack trace.
|
||||||
static LONG WINAPI topLevelFilter(EXCEPTION_POINTERS * pExceptionInfo)
|
static LONG WINAPI handleException(EXCEPTION_POINTERS * pExceptionInfo)
|
||||||
{
|
{
|
||||||
|
#if USE_SEPARATE_THREAD
|
||||||
|
EnterCriticalSection(&s_handler_critical_section);
|
||||||
|
|
||||||
|
s_requesting_thread_id = GetCurrentThreadId();
|
||||||
|
s_exception_info = pExceptionInfo;
|
||||||
|
|
||||||
|
// This causes the handler thread to call writeMiniDump.
|
||||||
|
ReleaseSemaphore(s_handler_start_semaphore, 1, NULL);
|
||||||
|
|
||||||
|
// Wait until WriteMinidumpWithException is done and collect its return value.
|
||||||
|
WaitForSingleObject(s_handler_finish_semaphore, INFINITE);
|
||||||
|
//bool status = s_handler_return_value;
|
||||||
|
|
||||||
|
// Clean up.
|
||||||
|
s_requesting_thread_id = 0;
|
||||||
|
s_exception_info = NULL;
|
||||||
|
|
||||||
|
LeaveCriticalSection(&s_handler_critical_section);
|
||||||
|
#else
|
||||||
|
// First of all, write mini dump.
|
||||||
|
writeMiniDump(pExceptionInfo);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
nvDebug("\nDump file saved.\n");
|
||||||
|
|
||||||
|
// Try to attach to debugger.
|
||||||
|
if (s_interactive && debug::attachToDebugger()) {
|
||||||
|
nvDebugBreak();
|
||||||
|
return EXCEPTION_CONTINUE_EXECUTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If that fails, then try to pretty print a stack trace and terminate.
|
||||||
void * trace[64];
|
void * trace[64];
|
||||||
|
|
||||||
int size = backtraceWithSymbols(pExceptionInfo->ContextRecord, trace, 64);
|
int size = backtraceWithSymbols(pExceptionInfo->ContextRecord, trace, 64);
|
||||||
printStackTrace(trace, size, 0);
|
|
||||||
|
|
||||||
writeMiniDump(pExceptionInfo);
|
// @@ Use win32's CreateFile?
|
||||||
|
FILE * fp = fileOpen("crash.txt", "wb");
|
||||||
|
if (fp != NULL) {
|
||||||
|
Array<const char *> lines;
|
||||||
|
writeStackTrace(trace, size, 0, lines);
|
||||||
|
|
||||||
return EXCEPTION_CONTINUE_SEARCH;
|
for (uint i = 0; i < lines.count(); i++) {
|
||||||
|
fputs(lines[i], fp);
|
||||||
|
delete lines[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @@ Add more info to crash.txt?
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXCEPTION_EXECUTE_HANDLER; // Terminate app.
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static void handlePureVirtualCall() {
|
||||||
|
// This is an pure virtual function call, not an exception. It's safe to
|
||||||
|
// play with sprintf here.
|
||||||
|
AutoExceptionHandler auto_exception_handler;
|
||||||
|
ExceptionHandler* current_handler = auto_exception_handler.get_handler();
|
||||||
|
|
||||||
|
MDRawAssertionInfo assertion;
|
||||||
|
memset(&assertion, 0, sizeof(assertion));
|
||||||
|
assertion.type = MD_ASSERTION_INFO_TYPE_PURE_VIRTUAL_CALL;
|
||||||
|
|
||||||
|
// Make up an exception record for the current thread and CPU context
|
||||||
|
// to make it possible for the crash processor to classify these
|
||||||
|
// as do regular crashes, and to make it humane for developers to
|
||||||
|
// analyze them.
|
||||||
|
EXCEPTION_RECORD exception_record = {};
|
||||||
|
CONTEXT exception_context = {};
|
||||||
|
EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };
|
||||||
|
|
||||||
|
::RtlCaptureContext(&exception_context);
|
||||||
|
|
||||||
|
exception_record.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
|
||||||
|
|
||||||
|
// We store pointers to the the expression and function strings,
|
||||||
|
// and the line as exception parameters to make them easy to
|
||||||
|
// access by the developer on the far side.
|
||||||
|
exception_record.NumberParameters = 3;
|
||||||
|
exception_record.ExceptionInformation[0] = reinterpret_cast<ULONG_PTR>(&assertion.expression);
|
||||||
|
exception_record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(&assertion.file);
|
||||||
|
exception_record.ExceptionInformation[2] = assertion.line;
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
// In case of out-of-process dump generation, directly call
|
||||||
|
// WriteMinidumpWithException since there is no separate thread running.
|
||||||
|
|
||||||
|
success = current_handler->WriteMinidumpOnHandlerThread(&exception_ptrs, &assertion);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
if (current_handler->previous_pch_) {
|
||||||
|
// The handler didn't fully handle the exception. Give it to the
|
||||||
|
// previous purecall handler.
|
||||||
|
current_handler->previous_pch_();
|
||||||
|
else {
|
||||||
|
// If there's no previous handler, return and let _purecall handle it.
|
||||||
|
// This will just put up an assertion dialog.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The handler either took care of the invalid parameter problem itself,
|
||||||
|
// or passed it on to another handler. "Swallow" it by exiting, paralleling
|
||||||
|
// the behavior of "swallowing" exceptions.
|
||||||
|
exit(0);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
#elif !NV_OS_WIN32 && defined(HAVE_SIGNAL_H) // NV_OS_LINUX || NV_OS_DARWIN
|
#elif !NV_OS_WIN32 && defined(HAVE_SIGNAL_H) // NV_OS_LINUX || NV_OS_DARWIN
|
||||||
|
|
||||||
#if defined(HAVE_EXECINFO_H)
|
#if defined(HAVE_EXECINFO_H)
|
||||||
|
@ -491,10 +727,14 @@ namespace
|
||||||
}
|
}
|
||||||
nvDebug( error_string.str() );
|
nvDebug( error_string.str() );
|
||||||
|
|
||||||
|
// Print stack trace:
|
||||||
|
debug::dumpInfo();
|
||||||
|
|
||||||
if (debug::isDebuggerPresent()) {
|
if (debug::isDebuggerPresent()) {
|
||||||
return NV_ABORT_DEBUG;
|
return NV_ABORT_DEBUG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s_interactive) {
|
||||||
flushMessageQueue();
|
flushMessageQueue();
|
||||||
int action = MessageBoxA(NULL, error_string.str(), "Assertion failed", MB_ABORTRETRYIGNORE|MB_ICONERROR);
|
int action = MessageBoxA(NULL, error_string.str(), "Assertion failed", MB_ABORTRETRYIGNORE|MB_ICONERROR);
|
||||||
switch( action ) {
|
switch( action ) {
|
||||||
|
@ -512,8 +752,9 @@ namespace
|
||||||
/*if( _CrtDbgReport( _CRT_ASSERT, file, line, module, exp ) == 1 ) {
|
/*if( _CrtDbgReport( _CRT_ASSERT, file, line, module, exp ) == 1 ) {
|
||||||
return NV_ABORT_DEBUG;
|
return NV_ABORT_DEBUG;
|
||||||
}*/
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
if( ret == NV_ABORT_EXIT ) {
|
if (ret == NV_ABORT_EXIT) {
|
||||||
// Exit cleanly.
|
// Exit cleanly.
|
||||||
throw "Assertion failed";
|
throw "Assertion failed";
|
||||||
}
|
}
|
||||||
|
@ -633,7 +874,16 @@ void debug::dumpInfo()
|
||||||
{
|
{
|
||||||
void * trace[64];
|
void * trace[64];
|
||||||
int size = backtrace(trace, 64);
|
int size = backtrace(trace, 64);
|
||||||
printStackTrace(trace, size, 1);
|
|
||||||
|
nvDebug( "\nDumping stacktrace:\n" );
|
||||||
|
|
||||||
|
Array<const char *> lines;
|
||||||
|
writeStackTrace(trace, size, 1, lines);
|
||||||
|
|
||||||
|
for (uint i = 0; i < lines.count(); i++) {
|
||||||
|
nvDebug(lines[i]);
|
||||||
|
delete lines[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -664,15 +914,87 @@ void debug::resetAssertHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Enable signal handler.
|
#if USE_SEPARATE_THREAD
|
||||||
void debug::enableSigHandler()
|
|
||||||
|
static void initHandlerThread()
|
||||||
|
{
|
||||||
|
static const int kExceptionHandlerThreadInitialStackSize = 64 * 1024;
|
||||||
|
|
||||||
|
// Set synchronization primitives and the handler thread. Each
|
||||||
|
// ExceptionHandler object gets its own handler thread because that's the
|
||||||
|
// only way to reliably guarantee sufficient stack space in an exception,
|
||||||
|
// and it allows an easy way to get a snapshot of the requesting thread's
|
||||||
|
// context outside of an exception.
|
||||||
|
InitializeCriticalSection(&s_handler_critical_section);
|
||||||
|
|
||||||
|
s_handler_start_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
|
||||||
|
nvDebugCheck(s_handler_start_semaphore != NULL);
|
||||||
|
|
||||||
|
s_handler_finish_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
|
||||||
|
nvDebugCheck(s_handler_finish_semaphore != NULL);
|
||||||
|
|
||||||
|
// Don't attempt to create the thread if we could not create the semaphores.
|
||||||
|
if (s_handler_finish_semaphore != NULL && s_handler_start_semaphore != NULL) {
|
||||||
|
DWORD thread_id;
|
||||||
|
s_handler_thread = CreateThread(NULL, // lpThreadAttributes
|
||||||
|
kExceptionHandlerThreadInitialStackSize,
|
||||||
|
ExceptionHandlerThreadMain,
|
||||||
|
NULL, // lpParameter
|
||||||
|
0, // dwCreationFlags
|
||||||
|
&thread_id);
|
||||||
|
nvDebugCheck(s_handler_thread != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* @@ We should avoid loading modules in the exception handler!
|
||||||
|
dbghelp_module_ = LoadLibrary(L"dbghelp.dll");
|
||||||
|
if (dbghelp_module_) {
|
||||||
|
minidump_write_dump_ = reinterpret_cast<MiniDumpWriteDump_type>(GetProcAddress(dbghelp_module_, "MiniDumpWriteDump"));
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
static void shutHandlerThread() {
|
||||||
|
// @@ Free stuff. Terminate thread.
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Enable signal handler.
|
||||||
|
void debug::enableSigHandler(bool interactive)
|
||||||
{
|
{
|
||||||
nvCheck(s_sig_handler_enabled != true);
|
nvCheck(s_sig_handler_enabled != true);
|
||||||
s_sig_handler_enabled = true;
|
s_sig_handler_enabled = true;
|
||||||
|
s_interactive = interactive;
|
||||||
|
|
||||||
#if NV_OS_WIN32 && NV_CC_MSVC
|
#if NV_OS_WIN32 && NV_CC_MSVC
|
||||||
|
if (interactive) {
|
||||||
|
// Do not display message boxes on error.
|
||||||
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx
|
||||||
|
SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
|
||||||
|
|
||||||
|
// CRT reports errors to debug output only.
|
||||||
|
// http://msdn.microsoft.com/en-us/library/1y71x448(v=vs.80).aspx
|
||||||
|
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
|
||||||
|
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
|
||||||
|
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if USE_SEPARATE_THREAD
|
||||||
|
initHandlerThread();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
s_old_exception_filter = ::SetUnhandledExceptionFilter( handleException );
|
||||||
|
|
||||||
|
/*
|
||||||
|
#if _MSC_VER >= 1400 // MSVC 2005/8
|
||||||
|
_set_invalid_parameter_handler(handleInvalidParameter);
|
||||||
|
#endif // _MSC_VER >= 1400
|
||||||
|
|
||||||
|
_set_purecall_handler(handlePureVirtualCall);
|
||||||
|
*/
|
||||||
|
|
||||||
s_old_exception_filter = ::SetUnhandledExceptionFilter( topLevelFilter );
|
|
||||||
|
|
||||||
// SYMOPT_DEFERRED_LOADS make us not take a ton of time unless we actual log traces
|
// SYMOPT_DEFERRED_LOADS make us not take a ton of time unless we actual log traces
|
||||||
SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_FAIL_CRITICAL_ERRORS|SYMOPT_LOAD_LINES|SYMOPT_UNDNAME);
|
SymSetOptions(SYMOPT_DEFERRED_LOADS|SYMOPT_FAIL_CRITICAL_ERRORS|SYMOPT_LOAD_LINES|SYMOPT_UNDNAME);
|
||||||
|
@ -797,8 +1119,6 @@ bool debug::attachToDebugger()
|
||||||
::Sleep(200);
|
::Sleep(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nvDebugBreak();
|
|
||||||
#endif // NV_OS_WIN32
|
#endif // NV_OS_WIN32
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -76,6 +76,23 @@
|
||||||
} \
|
} \
|
||||||
NV_MULTI_LINE_MACRO_END
|
NV_MULTI_LINE_MACRO_END
|
||||||
|
|
||||||
|
// Interesting assert macro from Insomniac:
|
||||||
|
// http://www.gdcvault.com/play/1015319/Developing-Imperfect-Software-How-to
|
||||||
|
// Used as follows:
|
||||||
|
// if (nvCheck(i < count)) {
|
||||||
|
// normal path
|
||||||
|
// } else {
|
||||||
|
// fixup code.
|
||||||
|
// }
|
||||||
|
// This style of macro could be combined with __builtin_expect to let the compiler know failure is unlikely.
|
||||||
|
#define nvCheckMacro(exp) \
|
||||||
|
(\
|
||||||
|
(exp) ? true : ( \
|
||||||
|
(nvAbort(#exp, __FILE__, __LINE__, __FUNC__) == NV_ABORT_DEBUG) ? (nvDebugBreak(), true) : ( false ) \
|
||||||
|
) \
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
#define nvAssert(exp) nvAssertMacro(exp)
|
#define nvAssert(exp) nvAssertMacro(exp)
|
||||||
#define nvCheck(exp) nvAssertMacro(exp)
|
#define nvCheck(exp) nvAssertMacro(exp)
|
||||||
|
|
||||||
|
@ -90,7 +107,7 @@
|
||||||
#endif // NV_NO_ASSERT
|
#endif // NV_NO_ASSERT
|
||||||
|
|
||||||
// Use nvAssume for very simple expresions only: nvAssume(0), nvAssume(value == true), etc.
|
// Use nvAssume for very simple expresions only: nvAssume(0), nvAssume(value == true), etc.
|
||||||
#if !defined(_DEBUG)
|
/*#if !defined(_DEBUG)
|
||||||
# if NV_CC_MSVC
|
# if NV_CC_MSVC
|
||||||
# define nvAssume(exp) __assume(exp)
|
# define nvAssume(exp) __assume(exp)
|
||||||
# else
|
# else
|
||||||
|
@ -98,6 +115,20 @@
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# define nvAssume(exp) nvCheck(exp)
|
# define nvAssume(exp) nvCheck(exp)
|
||||||
|
#endif*/
|
||||||
|
|
||||||
|
#if defined(_DEBUG)
|
||||||
|
# if NV_CC_MSVC
|
||||||
|
# define nvUnreachable() nvAssert(0 && "unreachable"); __assume(0)
|
||||||
|
# else
|
||||||
|
# define nvUnreachable() nvAssert(0 && "unreachable"); __builtin_unreachable()
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
# if NV_CC_MSVC
|
||||||
|
# define nvUnreachable() __assume(0)
|
||||||
|
# else
|
||||||
|
# define nvUnreachable() __builtin_unreachable()
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -138,13 +169,13 @@ namespace nv
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Message handler interface.
|
// Message handler interface.
|
||||||
struct MessageHandler {
|
struct MessageHandler {
|
||||||
virtual void log(const char * str, va_list arg) = 0;
|
virtual void log(const char * str, va_list arg) = 0;
|
||||||
virtual ~MessageHandler() {}
|
virtual ~MessageHandler() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Assert handler interface.
|
// Assert handler interface.
|
||||||
struct AssertHandler {
|
struct AssertHandler {
|
||||||
virtual int assertion(const char *exp, const char *file, int line, const char *func = NULL) = 0;
|
virtual int assertion(const char *exp, const char *file, int line, const char *func = NULL) = 0;
|
||||||
virtual ~AssertHandler() {}
|
virtual ~AssertHandler() {}
|
||||||
|
@ -161,7 +192,7 @@ namespace nv
|
||||||
NVCORE_API void setAssertHandler( AssertHandler * assertHanlder );
|
NVCORE_API void setAssertHandler( AssertHandler * assertHanlder );
|
||||||
NVCORE_API void resetAssertHandler();
|
NVCORE_API void resetAssertHandler();
|
||||||
|
|
||||||
NVCORE_API void enableSigHandler();
|
NVCORE_API void enableSigHandler(bool interactive);
|
||||||
NVCORE_API void disableSigHandler();
|
NVCORE_API void disableSigHandler();
|
||||||
|
|
||||||
NVCORE_API bool isDebuggerPresent();
|
NVCORE_API bool isDebuggerPresent();
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define NV_FASTCALL __attribute__((fastcall))
|
#define NV_FASTCALL __attribute__((fastcall))
|
||||||
#define NV_FORCEINLINE inline __attribute__((always_inline))
|
#define NV_FORCEINLINE __attribute__((always_inline))
|
||||||
#define NV_DEPRECATED __attribute__((deprecated))
|
#define NV_DEPRECATED __attribute__((deprecated))
|
||||||
|
|
||||||
#if __GNUC__ > 2
|
#if __GNUC__ > 2
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define NV_FASTCALL __attribute__((fastcall))
|
#define NV_FASTCALL __attribute__((fastcall))
|
||||||
#define NV_FORCEINLINE inline __attribute__((always_inline))
|
#define NV_FORCEINLINE __attribute__((always_inline))
|
||||||
#define NV_DEPRECATED __attribute__((deprecated))
|
#define NV_DEPRECATED __attribute__((deprecated))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,8 @@
|
||||||
#define NV_NOINLINE __declspec(noinline)
|
#define NV_NOINLINE __declspec(noinline)
|
||||||
#define NV_FORCEINLINE __forceinline
|
#define NV_FORCEINLINE __forceinline
|
||||||
|
|
||||||
|
#define NV_THREAD_LOCAL __declspec(thread)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Type definitions
|
// Type definitions
|
||||||
typedef unsigned char uint8;
|
typedef unsigned char uint8;
|
||||||
|
|
|
@ -28,7 +28,7 @@ bool FileSystem::exists(const char * path)
|
||||||
#elif NV_OS_WIN32 || NV_OS_XBOX
|
#elif NV_OS_WIN32 || NV_OS_XBOX
|
||||||
// PathFileExists requires linking to shlwapi.lib
|
// PathFileExists requires linking to shlwapi.lib
|
||||||
//return PathFileExists(path) != 0;
|
//return PathFileExists(path) != 0;
|
||||||
return GetFileAttributesA(path) != 0xFFFFFFFF;
|
return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
|
||||||
#else
|
#else
|
||||||
if (FILE * fp = fopen(path, "r"))
|
if (FILE * fp = fopen(path, "r"))
|
||||||
{
|
{
|
||||||
|
|
|
@ -31,25 +31,24 @@ namespace nv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Some hash functors:
|
template <typename T>
|
||||||
|
inline uint hash(const T & t, uint h = 5381)
|
||||||
|
{
|
||||||
|
return sdbmHash(&t, sizeof(T), h);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline uint hash(const float & f, uint h)
|
||||||
|
{
|
||||||
|
return sdbmFloatHash(&f, 1, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Functors for hash table:
|
||||||
template <typename Key> struct Hash
|
template <typename Key> struct Hash
|
||||||
{
|
{
|
||||||
uint operator()(const Key & k) const {
|
uint operator()(const Key & k) const {
|
||||||
return sdbmHash(&k, sizeof(Key));
|
return hash(k);
|
||||||
}
|
|
||||||
};
|
|
||||||
/*template <> struct Hash<int>
|
|
||||||
{
|
|
||||||
uint operator()(int x) const { return x; }
|
|
||||||
};
|
|
||||||
template <> struct Hash<uint>
|
|
||||||
{
|
|
||||||
uint operator()(uint x) const { return x; }
|
|
||||||
};*/
|
|
||||||
template <> struct Hash<float>
|
|
||||||
{
|
|
||||||
uint operator()(float f) const {
|
|
||||||
return sdbmFloatHash(&f, 1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -60,6 +59,25 @@ namespace nv
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// @@ Move to Utils.h?
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
struct Pair {
|
||||||
|
T1 first;
|
||||||
|
T2 second;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
bool operator==(const Pair<T1,T2> & p0, const Pair<T1,T2> & p1) {
|
||||||
|
return p0.first == p1.first && p0.second == p1.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
uint hash(const Pair<T1,T2> & p, uint h = 5381) {
|
||||||
|
return hash(p.second, hash(p.first));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
||||||
#endif // NV_CORE_HASH_H
|
#endif // NV_CORE_HASH_H
|
||||||
|
|
|
@ -101,17 +101,18 @@ bool nv::strEqual(const char * s1, const char * s2)
|
||||||
return strCmp(s1, s2) == 0;
|
return strCmp(s1, s2) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool nv::strBeginsWith(const char * dst, const char * prefix)
|
bool nv::strBeginsWith(const char * str, const char * prefix)
|
||||||
{
|
{
|
||||||
//return strstr(dst, prefix) == dst;
|
//return strstr(str, prefix) == dst;
|
||||||
return strncmp(dst, prefix, strlen(prefix)) == 0;
|
return strncmp(str, prefix, strlen(prefix)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @@ Not tested.
|
bool nv::strEndsWith(const char * str, const char * suffix)
|
||||||
bool nv::strEndsWith(const char * dst, const char * suffix)
|
|
||||||
{
|
{
|
||||||
const size_t len = strlen(suffix);
|
uint ml = strLen(str);
|
||||||
return strncmp(dst + strlen(dst) - len, suffix, len) == 0;
|
uint sl = strLen(suffix);
|
||||||
|
if (ml < sl) return false;
|
||||||
|
return strncmp(str + ml - sl, suffix, sl) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -379,6 +380,28 @@ StringBuilder & StringBuilder::appendFormatList( const char * fmt, va_list arg )
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Append n spaces.
|
||||||
|
StringBuilder & StringBuilder::appendSpace(uint n)
|
||||||
|
{
|
||||||
|
if (m_str == NULL) {
|
||||||
|
m_size = n + 1;
|
||||||
|
m_str = strAlloc(m_size);
|
||||||
|
memset(m_str, ' ', m_size);
|
||||||
|
m_str[n] = '\0';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const uint len = strLen(m_str);
|
||||||
|
if (m_size < len + n + 1) {
|
||||||
|
m_size = len + n + 1;
|
||||||
|
m_str = strReAlloc(m_str, m_size);
|
||||||
|
}
|
||||||
|
memset(m_str + len, ' ', n);
|
||||||
|
m_str[len+n] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Convert number to string in the given base. */
|
/** Convert number to string in the given base. */
|
||||||
StringBuilder & StringBuilder::number( int i, int base )
|
StringBuilder & StringBuilder::number( int i, int base )
|
||||||
|
|
|
@ -77,6 +77,8 @@ namespace nv
|
||||||
StringBuilder & appendFormat( const char * format, ... ) __attribute__((format (printf, 2, 3)));
|
StringBuilder & appendFormat( const char * format, ... ) __attribute__((format (printf, 2, 3)));
|
||||||
StringBuilder & appendFormatList( const char * format, va_list arg );
|
StringBuilder & appendFormatList( const char * format, va_list arg );
|
||||||
|
|
||||||
|
StringBuilder & appendSpace(uint n);
|
||||||
|
|
||||||
StringBuilder & number( int i, int base = 10 );
|
StringBuilder & number( int i, int base = 10 );
|
||||||
StringBuilder & number( uint i, int base = 10 );
|
StringBuilder & number( uint i, int base = 10 );
|
||||||
|
|
||||||
|
|
|
@ -119,7 +119,7 @@ namespace nv
|
||||||
|
|
||||||
/// Return the maximum of the three arguments.
|
/// Return the maximum of the three arguments.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T & max(const T & a, const T & b, const T & c)
|
inline const T & max3(const T & a, const T & b, const T & c)
|
||||||
{
|
{
|
||||||
return max(a, max(b, c));
|
return max(a, max(b, c));
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,7 @@ namespace nv
|
||||||
|
|
||||||
/// Return the maximum of the three arguments.
|
/// Return the maximum of the three arguments.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T & min(const T & a, const T & b, const T & c)
|
inline const T & min3(const T & a, const T & b, const T & c)
|
||||||
{
|
{
|
||||||
return min(a, min(b, c));
|
return min(a, min(b, c));
|
||||||
}
|
}
|
||||||
|
@ -210,6 +210,7 @@ namespace nv
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void destroy_range(T * restrict ptr, uint new_size, uint old_size) {
|
void destroy_range(T * restrict ptr, uint new_size, uint old_size) {
|
||||||
for (uint i = new_size; i < old_size; i++) {
|
for (uint i = new_size; i < old_size; i++) {
|
||||||
|
nvDebugCheck(ptr != NULL && isValidPtr(ptr));
|
||||||
(ptr+i)->~T(); // Explicit call to the destructor
|
(ptr+i)->~T(); // Explicit call to the destructor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,58 @@ using namespace nv;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Clip the given segment against this box.
|
||||||
|
bool Box::clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const {
|
||||||
|
|
||||||
|
// Avoid aliasing.
|
||||||
|
float tnear = *t_near;
|
||||||
|
float tfar = *t_far;
|
||||||
|
|
||||||
|
// clip ray segment to box
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
const float pos = origin.component[i] + tfar * dir.component[i];
|
||||||
|
const float dt = tfar - tnear;
|
||||||
|
|
||||||
|
if (dir.component[i] < 0) {
|
||||||
|
|
||||||
|
// clip end point
|
||||||
|
if (pos < minCorner.component[i]) {
|
||||||
|
tfar = tnear + dt * (origin.component[i] - minCorner.component[i]) / (origin.component[i] - pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clip start point
|
||||||
|
if (origin.component[i] > maxCorner.component[i]) {
|
||||||
|
tnear = tnear + dt * (origin.component[i] - maxCorner.component[i]) / (tfar * dir.component[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
// clip end point
|
||||||
|
if (pos > maxCorner.component[i]) {
|
||||||
|
tfar = tnear + dt * (maxCorner.component[i] - origin.component[i]) / (pos - origin.component[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clip start point
|
||||||
|
if (origin.component[i] < minCorner.component[i]) {
|
||||||
|
tnear = tnear + dt * (minCorner.component[i] - origin.component[i]) / (tfar * dir.component[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tnear > tfar) {
|
||||||
|
// Clipped away.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return result.
|
||||||
|
*t_near = tnear;
|
||||||
|
*t_far = tfar;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
float nv::distanceSquared(const Box &box, const Vector3 &point) {
|
float nv::distanceSquared(const Box &box, const Vector3 &point) {
|
||||||
Vector3 closest;
|
Vector3 closest;
|
||||||
|
|
||||||
|
@ -64,3 +116,4 @@ bool nv::intersect(const Box & box, const Vector3 & p, const Vector3 & id, float
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,9 @@ namespace nv
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Box();
|
inline Box() {}
|
||||||
Box(const Box & b);
|
inline Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) {}
|
||||||
Box(const Vector3 & mins, const Vector3 & maxs);
|
inline Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) {}
|
||||||
|
|
||||||
Box & operator=(const Box & b);
|
Box & operator=(const Box & b);
|
||||||
|
|
||||||
|
@ -30,6 +30,9 @@ namespace nv
|
||||||
// Clear the bounds.
|
// Clear the bounds.
|
||||||
void clearBounds();
|
void clearBounds();
|
||||||
|
|
||||||
|
// min < max
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
// Build a cube centered on center and with edge = 2*dist
|
// Build a cube centered on center and with edge = 2*dist
|
||||||
void cube(const Vector3 & center, float dist);
|
void cube(const Vector3 & center, float dist);
|
||||||
|
|
||||||
|
@ -51,6 +54,9 @@ namespace nv
|
||||||
// Add a box to this box.
|
// Add a box to this box.
|
||||||
void addBoxToBounds(const Box & b);
|
void addBoxToBounds(const Box & b);
|
||||||
|
|
||||||
|
// Add sphere to this box.
|
||||||
|
void addSphereToBounds(const Vector3 & p, float r);
|
||||||
|
|
||||||
// Translate box.
|
// Translate box.
|
||||||
void translate(const Vector3 & v);
|
void translate(const Vector3 & v);
|
||||||
|
|
||||||
|
@ -72,6 +78,11 @@ namespace nv
|
||||||
// Split the given box in 8 octants and assign the ith one to this box.
|
// Split the given box in 8 octants and assign the ith one to this box.
|
||||||
void setOctant(const Box & box, const Vector3 & center, int i);
|
void setOctant(const Box & box, const Vector3 & center, int i);
|
||||||
|
|
||||||
|
|
||||||
|
// Clip the given segment against this box.
|
||||||
|
bool clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const;
|
||||||
|
|
||||||
|
|
||||||
friend Stream & operator<< (Stream & s, Box & box);
|
friend Stream & operator<< (Stream & s, Box & box);
|
||||||
|
|
||||||
const Vector3 & corner(int i) const { return (&minCorner)[i]; }
|
const Vector3 & corner(int i) const { return (&minCorner)[i]; }
|
||||||
|
|
|
@ -12,13 +12,13 @@
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
// Default ctor.
|
// Default ctor.
|
||||||
inline Box::Box() { };
|
//inline Box::Box() { };
|
||||||
|
|
||||||
// Copy ctor.
|
// Copy ctor.
|
||||||
inline Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
|
//inline Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }
|
||||||
|
|
||||||
// Init ctor.
|
// Init ctor.
|
||||||
inline Box::Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) { }
|
//inline Box::Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) { }
|
||||||
|
|
||||||
// Assignment operator.
|
// Assignment operator.
|
||||||
inline Box & Box::operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }
|
inline Box & Box::operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }
|
||||||
|
@ -30,6 +30,12 @@ namespace nv
|
||||||
maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// min < max
|
||||||
|
inline bool Box::isValid() const
|
||||||
|
{
|
||||||
|
return minCorner.x <= maxCorner.x && minCorner.y <= maxCorner.y && minCorner.z <= maxCorner.z;
|
||||||
|
}
|
||||||
|
|
||||||
// Build a cube centered on center and with edge = 2*dist
|
// Build a cube centered on center and with edge = 2*dist
|
||||||
inline void Box::cube(const Vector3 & center, float dist)
|
inline void Box::cube(const Vector3 & center, float dist)
|
||||||
{
|
{
|
||||||
|
@ -62,7 +68,7 @@ namespace nv
|
||||||
if (axis == 0) return (maxCorner.x - minCorner.x) * 0.5f;
|
if (axis == 0) return (maxCorner.x - minCorner.x) * 0.5f;
|
||||||
if (axis == 1) return (maxCorner.y - minCorner.y) * 0.5f;
|
if (axis == 1) return (maxCorner.y - minCorner.y) * 0.5f;
|
||||||
if (axis == 2) return (maxCorner.z - minCorner.z) * 0.5f;
|
if (axis == 2) return (maxCorner.z - minCorner.z) * 0.5f;
|
||||||
nvAssume(false);
|
nvUnreachable();
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,6 +86,12 @@ namespace nv
|
||||||
maxCorner = max(maxCorner, b.maxCorner);
|
maxCorner = max(maxCorner, b.maxCorner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add sphere to this box.
|
||||||
|
inline void Box::addSphereToBounds(const Vector3 & p, float r) {
|
||||||
|
minCorner = min(minCorner, p - Vector3(r));
|
||||||
|
maxCorner = min(maxCorner, p + Vector3(r));
|
||||||
|
}
|
||||||
|
|
||||||
// Translate box.
|
// Translate box.
|
||||||
inline void Box::translate(const Vector3 & v)
|
inline void Box::translate(const Vector3 & v)
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace nv
|
||||||
// Clamp color components.
|
// Clamp color components.
|
||||||
inline Vector3 colorClamp(Vector3::Arg c)
|
inline Vector3 colorClamp(Vector3::Arg c)
|
||||||
{
|
{
|
||||||
return Vector3(clamp(c.x, 0.0f, 1.0f), clamp(c.y, 0.0f, 1.0f), clamp(c.z, 0.0f, 1.0f));
|
return Vector3(saturate(c.x), saturate(c.y), saturate(c.z));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clamp without allowing the hue to change.
|
// Clamp without allowing the hue to change.
|
||||||
|
@ -63,11 +63,10 @@ namespace nv
|
||||||
inline Color32 toColor32(Vector4::Arg v)
|
inline Color32 toColor32(Vector4::Arg v)
|
||||||
{
|
{
|
||||||
Color32 color;
|
Color32 color;
|
||||||
color.r = uint8(clamp(v.x, 0.0f, 1.0f) * 255);
|
color.r = uint8(saturate(v.x) * 255);
|
||||||
color.g = uint8(clamp(v.y, 0.0f, 1.0f) * 255);
|
color.g = uint8(saturate(v.y) * 255);
|
||||||
color.b = uint8(clamp(v.z, 0.0f, 1.0f) * 255);
|
color.b = uint8(saturate(v.z) * 255);
|
||||||
color.a = uint8(clamp(v.w, 0.0f, 1.0f) * 255);
|
color.a = uint8(saturate(v.w) * 255);
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +86,13 @@ namespace nv
|
||||||
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
|
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline float hue(float r, float g, float b) {
|
||||||
|
float h = atan2f(sqrtf(3.0f)*(g-b), 2*r-g-b) * (1.0f / (2 * PI)) + 0.5f;
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
||||||
#endif // NV_MATH_COLOR_INL
|
#endif // NV_MATH_COLOR_INL
|
||||||
|
|
|
@ -1,4 +1,310 @@
|
||||||
// This code is in the public domain -- castanyo@yahoo.es
|
// This code is in the public domain -- castanyo@yahoo.es
|
||||||
|
|
||||||
#include "Matrix.h"
|
|
||||||
#include "Matrix.inl"
|
#include "Matrix.inl"
|
||||||
|
#include "Vector.inl"
|
||||||
|
|
||||||
|
#include "nvcore/Array.inl"
|
||||||
|
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
|
using namespace nv;
|
||||||
|
|
||||||
|
|
||||||
|
// Given a matrix a[1..n][1..n], this routine replaces it by the LU decomposition of a rowwise
|
||||||
|
// permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above;
|
||||||
|
// indx[1..n] is an output vector that records the row permutation effected by the partial
|
||||||
|
// pivoting; d is output as -1 depending on whether the number of row interchanges was even
|
||||||
|
// or odd, respectively. This routine is used in combination with lubksb to solve linear equations
|
||||||
|
// or invert a matrix.
|
||||||
|
static bool ludcmp(float **a, int n, int *indx, float *d)
|
||||||
|
{
|
||||||
|
const float TINY = 1.0e-20f;
|
||||||
|
|
||||||
|
Array<float> vv; // vv stores the implicit scaling of each row.
|
||||||
|
vv.resize(n);
|
||||||
|
|
||||||
|
*d = 1.0; // No row interchanges yet.
|
||||||
|
for (int i = 0; i < n; i++) { // Loop over rows to get the implicit scaling information.
|
||||||
|
|
||||||
|
float big = 0.0;
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
big = max(big, fabsf(a[i][j]));
|
||||||
|
}
|
||||||
|
if (big == 0) {
|
||||||
|
return false; // Singular matrix
|
||||||
|
}
|
||||||
|
|
||||||
|
// No nonzero largest element.
|
||||||
|
vv[i] = 1.0f / big; // Save the scaling.
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < n; j++) { // This is the loop over columns of Crout's method.
|
||||||
|
for (int i = 0; i < j; i++) { // This is equation (2.3.12) except for i = j.
|
||||||
|
float sum = a[i][j];
|
||||||
|
for (int k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
|
||||||
|
a[i][j] = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
int imax = -1;
|
||||||
|
float big = 0.0; // Initialize for the search for largest pivot element.
|
||||||
|
for (int i = j; i < n; i++) { // This is i = j of equation (2.3.12) and i = j+ 1 : : : N
|
||||||
|
float sum = a[i][j]; // of equation (2.3.13).
|
||||||
|
for (int k = 0; k < j; k++) {
|
||||||
|
sum -= a[i][k]*a[k][j];
|
||||||
|
}
|
||||||
|
a[i][j]=sum;
|
||||||
|
|
||||||
|
float dum = vv[i]*fabs(sum);
|
||||||
|
if (dum >= big) {
|
||||||
|
// Is the figure of merit for the pivot better than the best so far?
|
||||||
|
big = dum;
|
||||||
|
imax = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nvDebugCheck(imax != -1);
|
||||||
|
|
||||||
|
if (j != imax) { // Do we need to interchange rows?
|
||||||
|
for (int k = 0; k < n; k++) { // Yes, do so...
|
||||||
|
swap(a[imax][k], a[j][k]);
|
||||||
|
}
|
||||||
|
*d = -(*d); // ...and change the parity of d.
|
||||||
|
vv[imax]=vv[j]; // Also interchange the scale factor.
|
||||||
|
}
|
||||||
|
|
||||||
|
indx[j]=imax;
|
||||||
|
if (a[j][j] == 0.0) a[j][j] = TINY;
|
||||||
|
|
||||||
|
// If the pivot element is zero the matrix is singular (at least to the precision of the
|
||||||
|
// algorithm). For some applications on singular matrices, it is desirable to substitute
|
||||||
|
// TINY for zero.
|
||||||
|
if (j != n-1) { // Now, finally, divide by the pivot element.
|
||||||
|
float dum = 1.0f / a[j][j];
|
||||||
|
for (int i = j+1; i < n; i++) a[i][j] *= dum;
|
||||||
|
}
|
||||||
|
} // Go back for the next column in the reduction.
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Solves the set of n linear equations Ax = b. Here a[1..n][1..n] is input, not as the matrix
|
||||||
|
// A but rather as its LU decomposition, determined by the routine ludcmp. indx[1..n] is input
|
||||||
|
// as the permutation vector returned by ludcmp. b[1..n] is input as the right-hand side vector
|
||||||
|
// B, and returns with the solution vector X. a, n, and indx are not modified by this routine
|
||||||
|
// and can be left in place for successive calls with different right-hand sides b. This routine takes
|
||||||
|
// into account the possibility that b will begin with many zero elements, so it is efficient for use
|
||||||
|
// in matrix inversion.
|
||||||
|
static void lubksb(float **a, int n, int *indx, float b[])
|
||||||
|
{
|
||||||
|
int ii = 0;
|
||||||
|
for (int i=0; i<n; i++) { // When ii is set to a positive value, it will become
|
||||||
|
int ip = indx[i]; // the index of the first nonvanishing element of b. We now
|
||||||
|
float sum = b[ip]; // do the forward substitution, equation (2.3.6). The
|
||||||
|
b[ip] = b[i]; // only new wrinkle is to unscramble the permutation as we go.
|
||||||
|
if (ii != 0) {
|
||||||
|
for (int j = ii-1; j < i; j++) sum -= a[i][j]*b[j];
|
||||||
|
}
|
||||||
|
else if (sum != 0.0f) {
|
||||||
|
ii = i+1; // A nonzero element was encountered, so from now on we
|
||||||
|
}
|
||||||
|
b[i] = sum; // will have to do the sums in the loop above.
|
||||||
|
}
|
||||||
|
for (int i=n-1; i>=0; i--) { // Now we do the backsubstitution, equation (2.3.7).
|
||||||
|
float sum = b[i];
|
||||||
|
for (int j = i+1; j < n; j++) {
|
||||||
|
sum -= a[i][j]*b[j];
|
||||||
|
}
|
||||||
|
b[i] = sum/a[i][i]; // Store a component of the solution vector X.
|
||||||
|
} // All done!
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool nv::solveLU(const Matrix & A, const Vector4 & b, Vector4 * x)
|
||||||
|
{
|
||||||
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
|
float m[4][4];
|
||||||
|
float *a[4] = {m[0], m[1], m[2], m[3]};
|
||||||
|
int idx[4];
|
||||||
|
float d;
|
||||||
|
|
||||||
|
for (int y = 0; y < 4; y++) {
|
||||||
|
for (int x = 0; x < 4; x++) {
|
||||||
|
a[x][y] = A(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create LU decomposition.
|
||||||
|
if (!ludcmp(a, 4, idx, &d)) {
|
||||||
|
// Singular matrix.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init solution.
|
||||||
|
*x = b;
|
||||||
|
|
||||||
|
// Do back substitution.
|
||||||
|
lubksb(a, 4, idx, x->component);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nv::solveLU(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
||||||
|
{
|
||||||
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
|
float m[3][3];
|
||||||
|
float *a[3] = {m[0], m[1], m[2]};
|
||||||
|
int idx[3];
|
||||||
|
float d;
|
||||||
|
|
||||||
|
for (int y = 0; y < 3; y++) {
|
||||||
|
for (int x = 0; x < 3; x++) {
|
||||||
|
a[x][y] = A(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create LU decomposition.
|
||||||
|
if (!ludcmp(a, 3, idx, &d)) {
|
||||||
|
// Singular matrix.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init solution.
|
||||||
|
*x = b;
|
||||||
|
|
||||||
|
// Do back substitution.
|
||||||
|
lubksb(a, 3, idx, x->component);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
|
||||||
|
{
|
||||||
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
|
*x = transform(inverse(A), b);
|
||||||
|
|
||||||
|
return true; // @@ Return false if determinant(A) == 0 !
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nv::solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x)
|
||||||
|
{
|
||||||
|
nvDebugCheck(x != NULL);
|
||||||
|
|
||||||
|
const float det = A.determinant();
|
||||||
|
if (equal(det, 0.0f)) { // @@ Use input epsilon.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix3 Ai = inverse(A);
|
||||||
|
|
||||||
|
*x = transform(Ai, b);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
// Copyright (C) 1999-2004 Michael Garland.
|
||||||
|
//
|
||||||
|
// 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, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, provided that the above
|
||||||
|
// copyright notice(s) and this permission notice appear in all copies of
|
||||||
|
// the Software and that both the above copyright notice(s) and this
|
||||||
|
// permission notice appear in supporting documentation.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||||
|
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
|
||||||
|
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
|
||||||
|
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||||
|
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
//
|
||||||
|
// Except as contained in this notice, the name of a copyright holder
|
||||||
|
// shall not be used in advertising or otherwise to promote the sale, use
|
||||||
|
// or other dealings in this Software without prior written authorization
|
||||||
|
// of the copyright holder.
|
||||||
|
|
||||||
|
|
||||||
|
// Matrix inversion code for 4x4 matrices using Gaussian elimination
|
||||||
|
// with partial pivoting. This is a specialized version of a
|
||||||
|
// procedure originally due to Paul Heckbert <ph@cs.cmu.edu>.
|
||||||
|
//
|
||||||
|
// Returns determinant of A, and B=inverse(A)
|
||||||
|
// If matrix A is singular, returns 0 and leaves trash in B.
|
||||||
|
//
|
||||||
|
#define SWAP(a, b, t) {t = a; a = b; b = t;}
|
||||||
|
double invert(Mat4& B, const Mat4& m)
|
||||||
|
{
|
||||||
|
Mat4 A = m;
|
||||||
|
int i, j, k;
|
||||||
|
double max, t, det, pivot;
|
||||||
|
|
||||||
|
/*---------- forward elimination ----------*/
|
||||||
|
|
||||||
|
for (i=0; i<4; i++) /* put identity matrix in B */
|
||||||
|
for (j=0; j<4; j++)
|
||||||
|
B(i, j) = (double)(i==j);
|
||||||
|
|
||||||
|
det = 1.0;
|
||||||
|
for (i=0; i<4; i++) { /* eliminate in column i, below diag */
|
||||||
|
max = -1.;
|
||||||
|
for (k=i; k<4; k++) /* find pivot for column i */
|
||||||
|
if (fabs(A(k, i)) > max) {
|
||||||
|
max = fabs(A(k, i));
|
||||||
|
j = k;
|
||||||
|
}
|
||||||
|
if (max<=0.) return 0.; /* if no nonzero pivot, PUNT */
|
||||||
|
if (j!=i) { /* swap rows i and j */
|
||||||
|
for (k=i; k<4; k++)
|
||||||
|
SWAP(A(i, k), A(j, k), t);
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
SWAP(B(i, k), B(j, k), t);
|
||||||
|
det = -det;
|
||||||
|
}
|
||||||
|
pivot = A(i, i);
|
||||||
|
det *= pivot;
|
||||||
|
for (k=i+1; k<4; k++) /* only do elems to right of pivot */
|
||||||
|
A(i, k) /= pivot;
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
B(i, k) /= pivot;
|
||||||
|
/* we know that A(i, i) will be set to 1, so don't bother to do it */
|
||||||
|
|
||||||
|
for (j=i+1; j<4; j++) { /* eliminate in rows below i */
|
||||||
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
|
for (k=i+1; k<4; k++) /* subtract scaled row i from row j */
|
||||||
|
A(j, k) -= A(i, k)*t; /* (ignore k<=i, we know they're 0) */
|
||||||
|
for (k=0; k<4; k++)
|
||||||
|
B(j, k) -= B(i, k)*t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*---------- backward elimination ----------*/
|
||||||
|
|
||||||
|
for (i=4-1; i>0; i--) { /* eliminate in column i, above diag */
|
||||||
|
for (j=0; j<i; j++) { /* eliminate in rows above i */
|
||||||
|
t = A(j, i); /* we're gonna zero this guy */
|
||||||
|
for (k=0; k<4; k++) /* subtract scaled row i from row j */
|
||||||
|
B(j, k) -= B(i, k)*t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return det;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,15 @@
|
||||||
|
|
||||||
#include "Vector.h"
|
#include "Vector.h"
|
||||||
|
|
||||||
|
// - Matrices are stored in memory in *column major* order.
|
||||||
|
// - Points are to be though of as column vectors.
|
||||||
|
// - Transformation of a point p by a matrix M is: p' = M * p
|
||||||
|
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
enum identity_t { identity };
|
enum identity_t { identity };
|
||||||
|
|
||||||
|
// 3x3 matrix.
|
||||||
class NVMATH_CLASS Matrix3
|
class NVMATH_CLASS Matrix3
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -19,6 +24,8 @@ namespace nv
|
||||||
Matrix3(const Matrix3 & m);
|
Matrix3(const Matrix3 & m);
|
||||||
Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
|
Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
|
||||||
|
|
||||||
|
float data(uint idx) const;
|
||||||
|
float & data(uint idx);
|
||||||
float get(uint row, uint col) const;
|
float get(uint row, uint col) const;
|
||||||
float operator()(uint row, uint col) const;
|
float operator()(uint row, uint col) const;
|
||||||
float & operator()(uint row, uint col);
|
float & operator()(uint row, uint col);
|
||||||
|
@ -31,17 +38,22 @@ namespace nv
|
||||||
void operator+=(const Matrix3 & m);
|
void operator+=(const Matrix3 & m);
|
||||||
void operator-=(const Matrix3 & m);
|
void operator-=(const Matrix3 & m);
|
||||||
|
|
||||||
|
void scale(float s);
|
||||||
|
void scale(Vector3::Arg s);
|
||||||
float determinant() const;
|
float determinant() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float m_data[9];
|
float m_data[9];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Solve equation system using LU decomposition and back-substitution.
|
||||||
|
extern bool solveLU(const Matrix3 & m, const Vector3 & b, Vector3 * x);
|
||||||
|
|
||||||
// 4x4 transformation matrix.
|
// Solve equation system using Cramer's inverse.
|
||||||
// -# Matrices are stored in memory in column major order.
|
extern bool solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x);
|
||||||
// -# Points are to be though of as column vectors.
|
|
||||||
// -# Transformation of a point p by a matrix M is: p' = M * p
|
|
||||||
|
// 4x4 matrix.
|
||||||
class NVMATH_CLASS Matrix
|
class NVMATH_CLASS Matrix
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -76,6 +88,12 @@ namespace nv
|
||||||
float m_data[16];
|
float m_data[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Solve equation system using LU decomposition and back-substitution.
|
||||||
|
extern bool solveLU(const Matrix & m, const Vector4 & b, Vector4 * x);
|
||||||
|
|
||||||
|
// Solve equation system using Cramer's inverse.
|
||||||
|
extern bool solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x);
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
||||||
#endif // NV_MATH_MATRIX_H
|
#endif // NV_MATH_MATRIX_H
|
||||||
|
|
|
@ -40,6 +40,16 @@ namespace nv
|
||||||
m_data[6] = v2.x; m_data[7] = v2.y; m_data[8] = v2.z;
|
m_data[6] = v2.x; m_data[7] = v2.y; m_data[8] = v2.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline float Matrix3::data(uint idx) const
|
||||||
|
{
|
||||||
|
nvDebugCheck(idx < 9);
|
||||||
|
return m_data[idx];
|
||||||
|
}
|
||||||
|
inline float & Matrix3::data(uint idx)
|
||||||
|
{
|
||||||
|
nvDebugCheck(idx < 9);
|
||||||
|
return m_data[idx];
|
||||||
|
}
|
||||||
inline float Matrix3::get(uint row, uint col) const
|
inline float Matrix3::get(uint row, uint col) const
|
||||||
{
|
{
|
||||||
nvDebugCheck(row < 3 && col < 3);
|
nvDebugCheck(row < 3 && col < 3);
|
||||||
|
@ -150,6 +160,29 @@ namespace nv
|
||||||
return mul(a, b);
|
return mul(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Transform the given 3d vector with the given matrix.
|
||||||
|
inline Vector3 transform(const Matrix3 & m, const Vector3 & p)
|
||||||
|
{
|
||||||
|
return Vector3(
|
||||||
|
p.x * m(0,0) + p.y * m(0,1) + p.z * m(0,2),
|
||||||
|
p.x * m(1,0) + p.y * m(1,1) + p.z * m(1,2),
|
||||||
|
p.x * m(2,0) + p.y * m(2,1) + p.z * m(2,2));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Matrix3::scale(float s)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
m_data[i] *= s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Matrix3::scale(Vector3::Arg s)
|
||||||
|
{
|
||||||
|
m_data[0] *= s.x; m_data[1] *= s.x; m_data[2] *= s.x;
|
||||||
|
m_data[3] *= s.y; m_data[4] *= s.y; m_data[5] *= s.y;
|
||||||
|
m_data[6] *= s.z; m_data[7] *= s.z; m_data[8] *= s.z;
|
||||||
|
}
|
||||||
|
|
||||||
inline float Matrix3::determinant() const
|
inline float Matrix3::determinant() const
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
|
@ -161,6 +194,33 @@ namespace nv
|
||||||
get(0,0) * get(1,2) * get(2,1);
|
get(0,0) * get(1,2) * get(2,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inverse using Cramer's rule.
|
||||||
|
inline Matrix3 inverse(const Matrix3 & m)
|
||||||
|
{
|
||||||
|
const float det = m.determinant();
|
||||||
|
if (equal(det, 0.0f, 0.0f)) {
|
||||||
|
return Matrix3(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix3 r;
|
||||||
|
|
||||||
|
r.data(0) = - m.data(5) * m.data(7) + m.data(4) * m.data(8);
|
||||||
|
r.data(1) = + m.data(5) * m.data(6) - m.data(3) * m.data(8);
|
||||||
|
r.data(2) = - m.data(4) * m.data(6) + m.data(3) * m.data(7);
|
||||||
|
|
||||||
|
r.data(3) = + m.data(2) * m.data(7) - m.data(1) * m.data(8);
|
||||||
|
r.data(4) = - m.data(2) * m.data(6) + m.data(0) * m.data(8);
|
||||||
|
r.data(5) = + m.data(1) * m.data(6) - m.data(0) * m.data(7);
|
||||||
|
|
||||||
|
r.data(6) = - m.data(2) * m.data(4) + m.data(1) * m.data(5);
|
||||||
|
r.data(7) = + m.data(2) * m.data(3) - m.data(0) * m.data(5);
|
||||||
|
r.data(8) = - m.data(1) * m.data(3) + m.data(0) * m.data(4);
|
||||||
|
|
||||||
|
r.scale(1.0f / det);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline Matrix::Matrix()
|
inline Matrix::Matrix()
|
||||||
|
@ -470,6 +530,7 @@ namespace nv
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inverse using Cramer's rule.
|
||||||
inline Matrix inverse(Matrix::Arg m)
|
inline Matrix inverse(Matrix::Arg m)
|
||||||
{
|
{
|
||||||
Matrix r;
|
Matrix r;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
Plane transformPlane(const Matrix& m, Plane::Arg p)
|
Plane transformPlane(const Matrix & m, const Plane & p)
|
||||||
{
|
{
|
||||||
Vector3 newVec = transformVector(m, p.vector());
|
Vector3 newVec = transformVector(m, p.vector());
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ namespace nv
|
||||||
return Plane(newVec, ptInPlane);
|
return Plane(newVec, ptInPlane);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 planeIntersection(Plane::Arg a, Plane::Arg b, Plane::Arg c)
|
Vector3 planeIntersection(const Plane & a, const Plane & b, const Plane & c)
|
||||||
{
|
{
|
||||||
return dot(a.vector(), cross(b.vector(), c.vector())) * (
|
return dot(a.vector(), cross(b.vector(), c.vector())) * (
|
||||||
a.offset() * cross(b.vector(), c.vector()) +
|
a.offset() * cross(b.vector(), c.vector()) +
|
||||||
|
|
|
@ -14,31 +14,26 @@ namespace nv
|
||||||
class NVMATH_CLASS Plane
|
class NVMATH_CLASS Plane
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef Plane const & Arg;
|
|
||||||
|
|
||||||
Plane();
|
Plane();
|
||||||
Plane(float x, float y, float z, float w);
|
Plane(float x, float y, float z, float w);
|
||||||
Plane(Vector4::Arg v);
|
Plane(const Vector4 & v);
|
||||||
Plane(Vector3::Arg v, float d);
|
Plane(const Vector3 & v, float d);
|
||||||
Plane(Vector3::Arg normal, Vector3::Arg point);
|
Plane(const Vector3 & normal, const Vector3 & point);
|
||||||
|
Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2);
|
||||||
|
|
||||||
const Plane & operator=(Plane::Arg v);
|
const Plane & operator=(const Plane & v);
|
||||||
|
|
||||||
Vector3 vector() const;
|
Vector3 vector() const;
|
||||||
float offset() const;
|
float offset() const;
|
||||||
|
|
||||||
const Vector4 & asVector() const;
|
|
||||||
Vector4 & asVector();
|
|
||||||
|
|
||||||
void operator*=(float s);
|
void operator*=(float s);
|
||||||
|
|
||||||
private:
|
Vector4 v;
|
||||||
Vector4 p;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Plane transformPlane(const Matrix&, Plane::Arg);
|
Plane transformPlane(const Matrix &, const Plane &);
|
||||||
|
|
||||||
Vector3 planeIntersection(Plane::Arg a, Plane::Arg b, Plane::Arg c);
|
Vector3 planeIntersection(const Plane & a, const Plane & b, const Plane & c);
|
||||||
|
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
|
@ -10,37 +10,38 @@
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
inline Plane::Plane() {}
|
inline Plane::Plane() {}
|
||||||
inline Plane::Plane(float x, float y, float z, float w) : p(x, y, z, w) {}
|
inline Plane::Plane(float x, float y, float z, float w) : v(x, y, z, w) {}
|
||||||
inline Plane::Plane(Vector4::Arg v) : p(v) {}
|
inline Plane::Plane(const Vector4 & v) : v(v) {}
|
||||||
inline Plane::Plane(Vector3::Arg v, float d) : p(v, d) {}
|
inline Plane::Plane(const Vector3 & v, float d) : v(v, d) {}
|
||||||
inline Plane::Plane(Vector3::Arg normal, Vector3::Arg point) : p(normal, dot(normal, point)) {}
|
inline Plane::Plane(const Vector3 & normal, const Vector3 & point) : v(normal, -dot(normal, point)) {}
|
||||||
|
inline Plane::Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2) {
|
||||||
|
Vector3 n = cross(v1-v0, v2-v0);
|
||||||
|
float d = -dot(n, v0);
|
||||||
|
v = Vector4(n, d);
|
||||||
|
}
|
||||||
|
|
||||||
inline const Plane & Plane::operator=(Plane::Arg v) { p = v.p; return *this; }
|
inline const Plane & Plane::operator=(const Plane & p) { v = p.v; return *this; }
|
||||||
|
|
||||||
inline Vector3 Plane::vector() const { return p.xyz(); }
|
inline Vector3 Plane::vector() const { return v.xyz(); }
|
||||||
inline float Plane::offset() const { return p.w; }
|
inline float Plane::offset() const { return v.w; }
|
||||||
|
|
||||||
inline const Vector4 & Plane::asVector() const { return p; }
|
|
||||||
inline Vector4 & Plane::asVector() { return p; }
|
|
||||||
|
|
||||||
// Normalize plane.
|
// Normalize plane.
|
||||||
inline Plane normalize(Plane::Arg plane, float epsilon = NV_EPSILON)
|
inline Plane normalize(const Plane & plane, float epsilon = NV_EPSILON)
|
||||||
{
|
{
|
||||||
const float len = length(plane.vector());
|
const float len = length(plane.vector());
|
||||||
nvDebugCheck(!isZero(len, epsilon));
|
const float inv = isZero(len, epsilon) ? 0 : 1.0f / len;
|
||||||
const float inv = 1.0f / len;
|
return Plane(plane.v * inv);
|
||||||
return Plane(plane.asVector() * inv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the signed distance from the given point to this plane.
|
// Get the signed distance from the given point to this plane.
|
||||||
inline float distance(Plane::Arg plane, Vector3::Arg point)
|
inline float distance(const Plane & plane, const Vector3 & point)
|
||||||
{
|
{
|
||||||
return dot(plane.vector(), point) - plane.offset();
|
return dot(plane.vector(), point) + plane.offset();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Plane::operator*=(float s)
|
inline void Plane::operator*=(float s)
|
||||||
{
|
{
|
||||||
scale(p, s);
|
v *= s;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
|
@ -18,6 +18,8 @@ namespace nv
|
||||||
Vector2(float x, float y);
|
Vector2(float x, float y);
|
||||||
Vector2(Vector2::Arg v);
|
Vector2(Vector2::Arg v);
|
||||||
|
|
||||||
|
template <typename T> operator T() const { return T(x, y); }
|
||||||
|
|
||||||
const Vector2 & operator=(Vector2::Arg v);
|
const Vector2 & operator=(Vector2::Arg v);
|
||||||
|
|
||||||
const float * ptr() const;
|
const float * ptr() const;
|
||||||
|
@ -41,10 +43,6 @@ namespace nv
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helpers to convert vector types. Assume T has x,y members and 2 argument constructor.
|
|
||||||
template <typename T> T to(Vector2::Arg v) { return T(v.x, v.y); }
|
|
||||||
|
|
||||||
|
|
||||||
class NVMATH_CLASS Vector3
|
class NVMATH_CLASS Vector3
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -56,6 +54,8 @@ namespace nv
|
||||||
Vector3(Vector2::Arg v, float z);
|
Vector3(Vector2::Arg v, float z);
|
||||||
Vector3(Vector3::Arg v);
|
Vector3(Vector3::Arg v);
|
||||||
|
|
||||||
|
template <typename T> operator T() const { return T(x, y, z); }
|
||||||
|
|
||||||
const Vector3 & operator=(Vector3::Arg v);
|
const Vector3 & operator=(Vector3::Arg v);
|
||||||
|
|
||||||
Vector2 xy() const;
|
Vector2 xy() const;
|
||||||
|
@ -82,10 +82,6 @@ namespace nv
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
|
|
||||||
template <typename T> T to(Vector3::Arg v) { return T(v.x, v.y, v.z); }
|
|
||||||
|
|
||||||
|
|
||||||
class NVMATH_CLASS Vector4
|
class NVMATH_CLASS Vector4
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -100,6 +96,8 @@ namespace nv
|
||||||
Vector4(Vector4::Arg v);
|
Vector4(Vector4::Arg v);
|
||||||
// Vector4(const Quaternion & v);
|
// Vector4(const Quaternion & v);
|
||||||
|
|
||||||
|
template <typename T> operator T() const { return T(x, y, z, w); }
|
||||||
|
|
||||||
const Vector4 & operator=(Vector4::Arg v);
|
const Vector4 & operator=(Vector4::Arg v);
|
||||||
|
|
||||||
Vector2 xy() const;
|
Vector2 xy() const;
|
||||||
|
@ -127,10 +125,6 @@ namespace nv
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helpers to convert vector types. Assume T has x,y,z members and 3 argument constructor.
|
|
||||||
template <typename T> T to(Vector4::Arg v) { return T(v.x, v.y, v.z, v.w); }
|
|
||||||
|
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
||||||
#endif // NV_MATH_VECTOR_H
|
#endif // NV_MATH_VECTOR_H
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "Vector.h"
|
#include "Vector.h"
|
||||||
#include "nvcore/Utils.h" // min, max
|
#include "nvcore/Utils.h" // min, max
|
||||||
|
#include "nvcore/Hash.h" // hash
|
||||||
|
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
|
@ -419,6 +420,13 @@ namespace nv
|
||||||
return (v0.x * v1.y - v0.y * v1.x);
|
return (v0.x * v1.y - v0.y * v1.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline uint hash(const Vector2 & v, uint h)
|
||||||
|
{
|
||||||
|
return sdbmFloatHash(v.component, 2, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Vector3
|
// Vector3
|
||||||
|
|
||||||
|
@ -617,6 +625,11 @@ namespace nv
|
||||||
return v - (2 * dot(v, n)) * n;
|
return v - (2 * dot(v, n)) * n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline uint hash(const Vector3 & v, uint h)
|
||||||
|
{
|
||||||
|
return sdbmFloatHash(v.component, 3, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Vector4
|
// Vector4
|
||||||
|
@ -765,6 +778,12 @@ namespace nv
|
||||||
return vf;
|
return vf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline uint hash(const Vector4 & v, uint h)
|
||||||
|
{
|
||||||
|
return sdbmFloatHash(v.component, 4, h);
|
||||||
|
}
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
||||||
#endif // NV_MATH_VECTOR_INL
|
#endif // NV_MATH_VECTOR_INL
|
||||||
|
|
|
@ -54,7 +54,7 @@
|
||||||
#define IS_NEGATIVE_FLOAT(x) (IR(x)&SIGN_BITMASK)
|
#define IS_NEGATIVE_FLOAT(x) (IR(x)&SIGN_BITMASK)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline double sqrt_assert(const double f)
|
extern "C" inline double sqrt_assert(const double f)
|
||||||
{
|
{
|
||||||
nvDebugCheck(f >= 0.0f);
|
nvDebugCheck(f >= 0.0f);
|
||||||
return sqrt(f);
|
return sqrt(f);
|
||||||
|
@ -66,7 +66,7 @@ inline float sqrtf_assert(const float f)
|
||||||
return sqrtf(f);
|
return sqrtf(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double acos_assert(const double f)
|
extern "C" inline double acos_assert(const double f)
|
||||||
{
|
{
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
return acos(f);
|
return acos(f);
|
||||||
|
@ -78,7 +78,7 @@ inline float acosf_assert(const float f)
|
||||||
return acosf(f);
|
return acosf(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double asin_assert(const double f)
|
extern "C" inline double asin_assert(const double f)
|
||||||
{
|
{
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
return asin(f);
|
return asin(f);
|
||||||
|
@ -98,6 +98,17 @@ inline float asinf_assert(const float f)
|
||||||
#define asin asin_assert
|
#define asin asin_assert
|
||||||
#define asinf asinf_assert
|
#define asinf asinf_assert
|
||||||
|
|
||||||
|
#if NV_CC_MSVC
|
||||||
|
NV_FORCEINLINE float log2f(float x)
|
||||||
|
{
|
||||||
|
nvCheck(x >= 0);
|
||||||
|
return logf(x) / logf(2.0f);
|
||||||
|
}
|
||||||
|
NV_FORCEINLINE float exp2f(float x)
|
||||||
|
{
|
||||||
|
return powf(2.0f, x);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
|
@ -109,7 +120,7 @@ namespace nv
|
||||||
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
|
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
|
||||||
{
|
{
|
||||||
//return fabs(f0-f1) <= epsilon;
|
//return fabs(f0-f1) <= epsilon;
|
||||||
return fabs(f0-f1) <= epsilon * max(1.0f, fabsf(f0), fabsf(f1));
|
return fabs(f0-f1) <= epsilon * max3(1.0f, fabsf(f0), fabsf(f1));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool isZero(const float f, const float epsilon = NV_EPSILON)
|
inline bool isZero(const float f, const float epsilon = NV_EPSILON)
|
||||||
|
@ -154,18 +165,6 @@ namespace nv
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if NV_CC_MSVC
|
|
||||||
NV_FORCEINLINE float log2f(float x)
|
|
||||||
{
|
|
||||||
nvCheck(x >= 0);
|
|
||||||
return logf(x) / logf(2.0f);
|
|
||||||
}
|
|
||||||
NV_FORCEINLINE float exp2f(float x)
|
|
||||||
{
|
|
||||||
return powf(2.0f, x);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
inline float lerp(float f0, float f1, float t)
|
inline float lerp(float f0, float f1, float t)
|
||||||
{
|
{
|
||||||
const float s = 1.0f - t;
|
const float s = 1.0f - t;
|
||||||
|
@ -211,8 +210,8 @@ namespace nv
|
||||||
// Eliminates negative zeros from a float array.
|
// Eliminates negative zeros from a float array.
|
||||||
inline void floatCleanup(float * fp, int n)
|
inline void floatCleanup(float * fp, int n)
|
||||||
{
|
{
|
||||||
nvDebugCheck(isFinite(*fp));
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
|
//nvDebugCheck(isFinite(fp[i]));
|
||||||
union { float f; uint32 i; } x = { fp[i] };
|
union { float f; uint32 i; } x = { fp[i] };
|
||||||
if (x.i == 0x80000000) fp[i] = 0.0f;
|
if (x.i == 0x80000000) fp[i] = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,25 +34,33 @@ void Event::wait() {
|
||||||
|
|
||||||
#elif NV_OS_UNIX
|
#elif NV_OS_UNIX
|
||||||
|
|
||||||
// @@ TODO
|
|
||||||
#pragma NV_MESSAGE("Implement event using pthreads!")
|
#pragma NV_MESSAGE("Implement event using pthreads!")
|
||||||
|
|
||||||
struct Event::Private {
|
struct Event::Private {
|
||||||
|
pthread_cond_t pt_cond;
|
||||||
|
pthread_mutex_t pt_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
Event::Event() : m(new Private) {
|
Event::Event() : m(new Private) {
|
||||||
|
// pthread equivalent of auto-reset event
|
||||||
|
pthread_cond_init(&m->pt_cond, NULL);
|
||||||
|
pthread_mutex_init(&m->pt_mutex, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Event::~Event() {
|
Event::~Event() {
|
||||||
|
pthread_cond_destroy(&m->pt_cond);
|
||||||
|
pthread_mutex_destroy(&m->pt_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Event::post() {
|
void Event::post() {
|
||||||
|
pthread_cond_signal(&m->pt_cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Event::wait() {
|
void Event::wait() {
|
||||||
|
pthread_cond_wait(&m->pt_cond, &m->pt_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // NV_OS_UNIX
|
||||||
|
|
||||||
|
|
||||||
/*static*/ void Event::post(Event * events, uint count) {
|
/*static*/ void Event::post(Event * events, uint count) {
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct Thread::Private
|
||||||
#if NV_OS_WIN32
|
#if NV_OS_WIN32
|
||||||
|
|
||||||
unsigned long __stdcall threadFunc(void * arg) {
|
unsigned long __stdcall threadFunc(void * arg) {
|
||||||
Thread * thread = (Thread *)arg;
|
Thread::Private * thread = (Thread::Private *)arg;
|
||||||
thread->func(thread->arg);
|
thread->func(thread->arg);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ unsigned long __stdcall threadFunc(void * arg) {
|
||||||
#elif NV_OS_UNIX
|
#elif NV_OS_UNIX
|
||||||
|
|
||||||
extern "C" void * threadFunc(void * arg) {
|
extern "C" void * threadFunc(void * arg) {
|
||||||
Thread * thread = (Thread *)arg;
|
Thread::Private * thread = (Thread::Private *)arg;
|
||||||
thread->func(thread->arg);
|
thread->func(thread->arg);
|
||||||
pthread_exit(0);
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
@ -55,15 +55,15 @@ Thread::~Thread()
|
||||||
|
|
||||||
void Thread::start(ThreadFunc * func, void * arg)
|
void Thread::start(ThreadFunc * func, void * arg)
|
||||||
{
|
{
|
||||||
this->func = func;
|
p->func = func;
|
||||||
this->arg = arg;
|
p->arg = arg;
|
||||||
|
|
||||||
#if NV_OS_WIN32
|
#if NV_OS_WIN32
|
||||||
p->thread = CreateThread(NULL, 0, threadFunc, this, 0, NULL);
|
p->thread = CreateThread(NULL, 0, threadFunc, p.ptr(), 0, NULL);
|
||||||
//p->thread = (HANDLE)_beginthreadex (0, 0, threadFunc, this, 0, NULL); // @@ So that we can call CRT functions...
|
//p->thread = (HANDLE)_beginthreadex (0, 0, threadFunc, p.ptr(), 0, NULL); // @@ So that we can call CRT functions...
|
||||||
nvDebugCheck(p->thread != NULL);
|
nvDebugCheck(p->thread != NULL);
|
||||||
#elif NV_OS_UNIX
|
#elif NV_OS_UNIX
|
||||||
int result = pthread_create(&p->thread, NULL, threadFunc, this);
|
int result = pthread_create(&p->thread, NULL, threadFunc, p.ptr());
|
||||||
nvDebugCheck(result == 0);
|
nvDebugCheck(result == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
#include "nvthread.h"
|
#include "nvthread.h"
|
||||||
|
|
||||||
#include "nvcore/Ptr.h"
|
#include "nvcore/Ptr.h" // AutoPtr
|
||||||
|
|
||||||
namespace nv
|
namespace nv
|
||||||
{
|
{
|
||||||
|
@ -30,15 +30,8 @@ namespace nv
|
||||||
|
|
||||||
static void wait(Thread * threads, uint count);
|
static void wait(Thread * threads, uint count);
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
struct Private;
|
struct Private;
|
||||||
AutoPtr<Private> p;
|
AutoPtr<Private> p;
|
||||||
|
|
||||||
public: // @@ Why public? Also in private?!
|
|
||||||
ThreadFunc * func;
|
|
||||||
void * arg;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // nv namespace
|
} // nv namespace
|
||||||
|
|
|
@ -79,6 +79,8 @@ ThreadPool::ThreadPool()
|
||||||
startEvents = new Event[workerCount];
|
startEvents = new Event[workerCount];
|
||||||
finishEvents = new Event[workerCount];
|
finishEvents = new Event[workerCount];
|
||||||
|
|
||||||
|
nvCompilerWriteBarrier(); // @@ Use a memory fence?
|
||||||
|
|
||||||
for (uint i = 0; i < workerCount; i++) {
|
for (uint i = 0; i < workerCount; i++) {
|
||||||
workers[i].start(workerFunc, (void *)i);
|
workers[i].start(workerFunc, (void *)i);
|
||||||
}
|
}
|
||||||
|
@ -110,8 +112,6 @@ void ThreadPool::start(ThreadFunc * func, void * arg)
|
||||||
|
|
||||||
allIdle = false;
|
allIdle = false;
|
||||||
|
|
||||||
nvCompilerWriteBarrier();
|
|
||||||
|
|
||||||
// Resume threads.
|
// Resume threads.
|
||||||
Event::post(startEvents, workerCount);
|
Event::post(startEvents, workerCount);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,12 @@
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include "Thread.h"
|
#include "Thread.h"
|
||||||
|
|
||||||
|
// The thread pool creates one worker thread for each physical core.
|
||||||
|
// The threads are idle waiting for their start events so that they do not consume any resources while inactive.
|
||||||
|
// The thread pool runs the same function in all worker threads, the idea is to use this as the foundation of a custom task scheduler.
|
||||||
|
// When the thread pool starts, the main thread continues running, but the common use case is to inmmediately wait of the termination events of the worker threads.
|
||||||
|
// @@ The start and wait methods could probably be merged.
|
||||||
|
|
||||||
namespace nv {
|
namespace nv {
|
||||||
|
|
||||||
class Thread;
|
class Thread;
|
||||||
|
|
|
@ -9,14 +9,13 @@
|
||||||
#elif NV_OS_UNIX
|
#elif NV_OS_UNIX
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <unistd.h>
|
|
||||||
#elif NV_OS_DARWIN
|
#elif NV_OS_DARWIN
|
||||||
#import <stdio.h>
|
#import <stdio.h>
|
||||||
#import <string.h>
|
#import <string.h>
|
||||||
#import <mach/mach_host.h>
|
#import <mach/mach_host.h>
|
||||||
#import <sys/sysctl.h>
|
#import <sys/sysctl.h>
|
||||||
|
|
||||||
#include <CoreFoundation/CoreFoundation.h>
|
//#include <CoreFoundation/CoreFoundation.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
|
@ -1582,7 +1582,7 @@ void Surface::toRGBE(int mantissaBits, int exponentBits)
|
||||||
float B = ::clamp(b[i], 0.0f, maxValue);
|
float B = ::clamp(b[i], 0.0f, maxValue);
|
||||||
|
|
||||||
// Compute max:
|
// Compute max:
|
||||||
float M = max(R, G, B);
|
float M = max3(R, G, B);
|
||||||
|
|
||||||
// Preliminary exponent:
|
// Preliminary exponent:
|
||||||
int E = max(- exponentBias - 1, floatExponent(M)) + 1 + exponentBias;
|
int E = max(- exponentBias - 1, floatExponent(M)) + 1 + exponentBias;
|
||||||
|
@ -1879,7 +1879,7 @@ void Surface::toneMap(ToneMapper tm, float * parameters)
|
||||||
if (tm == ToneMapper_Linear) {
|
if (tm == ToneMapper_Linear) {
|
||||||
// Clamp preserving the hue.
|
// Clamp preserving the hue.
|
||||||
for (uint i = 0; i < count; i++) {
|
for (uint i = 0; i < count; i++) {
|
||||||
float m = max(r[i], g[i], b[i]);
|
float m = max3(r[i], g[i], b[i]);
|
||||||
if (m > 1.0f) {
|
if (m > 1.0f) {
|
||||||
r[i] *= 1.0f / m;
|
r[i] *= 1.0f / m;
|
||||||
g[i] *= 1.0f / m;
|
g[i] *= 1.0f / m;
|
||||||
|
@ -1907,7 +1907,7 @@ void Surface::toneMap(ToneMapper tm, float * parameters)
|
||||||
// Avoid clamping abrubtly.
|
// Avoid clamping abrubtly.
|
||||||
// Minimize color difference along most of the color range. [0, alpha)
|
// Minimize color difference along most of the color range. [0, alpha)
|
||||||
for (uint i = 0; i < count; i++) {
|
for (uint i = 0; i < count; i++) {
|
||||||
float m = max(r[i], g[i], b[i]);
|
float m = max3(r[i], g[i], b[i]);
|
||||||
if (m > 1.0f) {
|
if (m > 1.0f) {
|
||||||
r[i] *= 1.0f / m;
|
r[i] *= 1.0f / m;
|
||||||
g[i] *= 1.0f / m;
|
g[i] *= 1.0f / m;
|
||||||
|
|
|
@ -53,7 +53,7 @@ Vector3 Utils::lerp(const Vector3& a, const Vector3 &b, int i, int denom)
|
||||||
case 3: denom *= 5; i *= 5; // fall through to case 15
|
case 3: denom *= 5; i *= 5; // fall through to case 15
|
||||||
case 15: weights = denom15_weights_64; break;
|
case 15: weights = denom15_weights_64; break;
|
||||||
case 7: weights = denom7_weights_64; break;
|
case 7: weights = denom7_weights_64; break;
|
||||||
default: nvAssume(0);
|
default: nvUnreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// no need to round these as this is an exact division
|
// no need to round these as this is an exact division
|
||||||
|
@ -96,7 +96,7 @@ void Utils::clamp(Vector3 &v)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
nvAssume (0);
|
nvUnreachable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -223,7 +223,7 @@ static void write_header(const ComprEndpts endpts[NREGIONS_ONE], const Pattern &
|
||||||
case FIELD_GZ:
|
case FIELD_GZ:
|
||||||
case FIELD_BY:
|
case FIELD_BY:
|
||||||
case FIELD_BZ:
|
case FIELD_BZ:
|
||||||
default: nvAssume(0);
|
default: nvUnreachable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -278,7 +278,7 @@ static void read_header(Bits &in, ComprEndpts endpts[NREGIONS_ONE], Pattern &p)
|
||||||
case FIELD_GZ:
|
case FIELD_GZ:
|
||||||
case FIELD_BY:
|
case FIELD_BY:
|
||||||
case FIELD_BZ:
|
case FIELD_BZ:
|
||||||
default: nvAssume(0);
|
default: nvUnreachable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -281,7 +281,7 @@ static void write_header(const ComprEndpts endpts[NREGIONS_TWO], int shapeindex,
|
||||||
case FIELD_BX: out.write(bx >> endbit, len); break;
|
case FIELD_BX: out.write(bx >> endbit, len); break;
|
||||||
case FIELD_BY: out.write(by >> endbit, len); break;
|
case FIELD_BY: out.write(by >> endbit, len); break;
|
||||||
case FIELD_BZ: out.write(bz >> endbit, len); break;
|
case FIELD_BZ: out.write(bz >> endbit, len); break;
|
||||||
default: nvAssume(0);
|
default: nvUnreachable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -338,7 +338,7 @@ static bool read_header(Bits &in, ComprEndpts endpts[NREGIONS_TWO], int &shapein
|
||||||
case FIELD_BX: bx |= in.read(len) << endbit; break;
|
case FIELD_BX: bx |= in.read(len) << endbit; break;
|
||||||
case FIELD_BY: by |= in.read(len) << endbit; break;
|
case FIELD_BY: by |= in.read(len) << endbit; break;
|
||||||
case FIELD_BZ: bz |= in.read(len) << endbit; break;
|
case FIELD_BZ: bz |= in.read(len) << endbit; break;
|
||||||
default: nvAssume(0);
|
default: nvUnreachable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ struct MyMessageHandler : public nv::MessageHandler {
|
||||||
struct MyAssertHandler : public nv::AssertHandler {
|
struct MyAssertHandler : public nv::AssertHandler {
|
||||||
MyAssertHandler() {
|
MyAssertHandler() {
|
||||||
nv::debug::setAssertHandler( this );
|
nv::debug::setAssertHandler( this );
|
||||||
nv::debug::enableSigHandler();
|
nv::debug::enableSigHandler(/*interactive=*/true);
|
||||||
}
|
}
|
||||||
~MyAssertHandler() {
|
~MyAssertHandler() {
|
||||||
nv::debug::resetAssertHandler();
|
nv::debug::resetAssertHandler();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user