mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-23 13:18:16 +08:00
add and update samples for CUDA 11.5
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
294
Common/dynlink_d3d10.h
Normal file
294
Common/dynlink_d3d10.h
Normal file
@@ -0,0 +1,294 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// File: dynlink_d3d10.h
|
||||
//
|
||||
// Shortcut macros and functions for using DX objects
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
#ifndef _DYNLINK_D3D10_H_
|
||||
#define _DYNLINK_D3D10_H_
|
||||
|
||||
// Standard Windows includes
|
||||
#include <windows.h>
|
||||
#include <initguid.h>
|
||||
#include <assert.h>
|
||||
#include <wchar.h>
|
||||
#include <mmsystem.h>
|
||||
#include <commctrl.h> // for InitCommonControls()
|
||||
#include <shellapi.h> // for ExtractIcon()
|
||||
#include <new.h> // for placement new
|
||||
#include <shlobj.h>
|
||||
#include <math.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// CRT's memory leak detection
|
||||
#if defined(DEBUG) || defined(_DEBUG)
|
||||
#include <crtdbg.h>
|
||||
#endif
|
||||
|
||||
// Direct3D9 includes
|
||||
#include <d3d9.h>
|
||||
|
||||
// Direct3D10 includes
|
||||
#include <dxgi.h>
|
||||
#include <d3d10_1.h>
|
||||
#include <d3d10.h>
|
||||
|
||||
// XInput includes
|
||||
#include <xinput.h>
|
||||
|
||||
// strsafe.h deprecates old unsecure string functions. If you
|
||||
// really do not want to it to (not recommended), then uncomment the next line
|
||||
//#define STRSAFE_NO_DEPRECATE
|
||||
|
||||
#ifndef STRSAFE_NO_DEPRECATE
|
||||
#pragma deprecated("strncpy")
|
||||
#pragma deprecated("wcsncpy")
|
||||
#pragma deprecated("_tcsncpy")
|
||||
#pragma deprecated("wcsncat")
|
||||
#pragma deprecated("strncat")
|
||||
#pragma deprecated("_tcsncat")
|
||||
#endif
|
||||
|
||||
#pragma warning( disable : 4996 ) // disable deprecated warning
|
||||
#include <strsafe.h>
|
||||
#pragma warning( default : 4996 )
|
||||
|
||||
#include <DirectXMath.h>
|
||||
|
||||
using namespace DirectX;
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Structs
|
||||
//--------------------------------------------------------------------------------------
|
||||
struct DXUTD3D9DeviceSettings
|
||||
{
|
||||
UINT AdapterOrdinal;
|
||||
D3DDEVTYPE DeviceType;
|
||||
D3DFORMAT AdapterFormat;
|
||||
DWORD BehaviorFlags;
|
||||
D3DPRESENT_PARAMETERS pp;
|
||||
};
|
||||
|
||||
struct DXUTD3D10DeviceSettings
|
||||
{
|
||||
UINT AdapterOrdinal;
|
||||
D3D10_DRIVER_TYPE DriverType;
|
||||
UINT Output;
|
||||
DXGI_SWAP_CHAIN_DESC sd;
|
||||
UINT32 CreateFlags;
|
||||
UINT32 SyncInterval;
|
||||
DWORD PresentFlags;
|
||||
bool AutoCreateDepthStencil; // DXUT will create the a depth stencil resource and view if true
|
||||
DXGI_FORMAT AutoDepthStencilFormat;
|
||||
};
|
||||
|
||||
enum DXUTDeviceVersion { DXUT_D3D9_DEVICE, DXUT_D3D10_DEVICE };
|
||||
struct DXUTDeviceSettings
|
||||
{
|
||||
DXUTDeviceVersion ver;
|
||||
union
|
||||
{
|
||||
DXUTD3D9DeviceSettings d3d9; // only valid if ver == DXUT_D3D9_DEVICE
|
||||
DXUTD3D10DeviceSettings d3d10; // only valid if ver == DXUT_D3D10_DEVICE
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Error codes
|
||||
//--------------------------------------------------------------------------------------
|
||||
#define DXUTERR_NODIRECT3D MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0901)
|
||||
#define DXUTERR_NOCOMPATIBLEDEVICES MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0902)
|
||||
#define DXUTERR_MEDIANOTFOUND MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0903)
|
||||
#define DXUTERR_NONZEROREFCOUNT MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0904)
|
||||
#define DXUTERR_CREATINGDEVICE MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0905)
|
||||
#define DXUTERR_RESETTINGDEVICE MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0906)
|
||||
#define DXUTERR_CREATINGDEVICEOBJECTS MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0907)
|
||||
#define DXUTERR_RESETTINGDEVICEOBJECTS MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0908)
|
||||
#define DXUTERR_DEVICEREMOVED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x090A)
|
||||
|
||||
|
||||
typedef HRESULT(WINAPI *LPCREATEDXGIFACTORY)(REFIID, void **);
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEDEVICE)(IDXGIAdapter *, D3D10_DRIVER_TYPE, HMODULE, UINT, UINT32,
|
||||
ID3D10Device **);
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEDEVICE1)(IDXGIAdapter *, D3D10_DRIVER_TYPE, HMODULE, UINT,
|
||||
D3D10_FEATURE_LEVEL1, UINT, ID3D10Device1 **);
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATESTATEBLOCK)(ID3D10Device *pDevice, D3D10_STATE_BLOCK_MASK *pStateBlockMask,
|
||||
ID3D10StateBlock **ppStateBlock);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKUNION)(D3D10_STATE_BLOCK_MASK *pA, D3D10_STATE_BLOCK_MASK *pB,
|
||||
D3D10_STATE_BLOCK_MASK *pResult);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKINTERSECT)(D3D10_STATE_BLOCK_MASK *pA, D3D10_STATE_BLOCK_MASK *pB,
|
||||
D3D10_STATE_BLOCK_MASK *pResult);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKDIFFERENCE)(D3D10_STATE_BLOCK_MASK *pA, D3D10_STATE_BLOCK_MASK *pB,
|
||||
D3D10_STATE_BLOCK_MASK *pResult);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKENABLECAPTURE)(D3D10_STATE_BLOCK_MASK *pMask,
|
||||
D3D10_DEVICE_STATE_TYPES StateType, UINT RangeStart,
|
||||
UINT RangeLength);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKDISABLECAPTURE)(D3D10_STATE_BLOCK_MASK *pMask,
|
||||
D3D10_DEVICE_STATE_TYPES StateType, UINT RangeStart,
|
||||
UINT RangeLength);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKENABLEALL)(D3D10_STATE_BLOCK_MASK *pMask);
|
||||
typedef HRESULT(WINAPI *LPD3D10STATEBLOCKMASKDISABLEALL)(D3D10_STATE_BLOCK_MASK *pMask);
|
||||
typedef BOOL (WINAPI *LPD3D10STATEBLOCKMASKGETSETTING)(D3D10_STATE_BLOCK_MASK *pMask,
|
||||
D3D10_DEVICE_STATE_TYPES StateType, UINT Entry);
|
||||
|
||||
typedef HRESULT(WINAPI *LPD3D10COMPILEEFFECTFROMMEMORY)(void *pData, SIZE_T DataLength, LPCSTR pSrcFileName,
|
||||
CONST D3D10_SHADER_MACRO *pDefines,
|
||||
ID3D10Include *pInclude, UINT HLSLFlags, UINT FXFlags,
|
||||
ID3D10Blob **ppCompiledEffect, ID3D10Blob **ppErrors);
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEEFFECTFROMMEMORY)(void *pData, SIZE_T DataLength, UINT FXFlags,
|
||||
ID3D10Device *pDevice,
|
||||
ID3D10EffectPool *pEffectPool,
|
||||
ID3D10Effect **ppEffect);
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEEFFECTPOOLFROMMEMORY)(void *pData, SIZE_T DataLength, UINT FXFlags,
|
||||
ID3D10Device *pDevice, ID3D10EffectPool **ppEffectPool);
|
||||
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEDEVICEANDSWAPCHAIN)(IDXGIAdapter *pAdapter,
|
||||
D3D10_DRIVER_TYPE DriverType,
|
||||
HMODULE Software,
|
||||
UINT Flags,
|
||||
UINT SDKVersion,
|
||||
DXGI_SWAP_CHAIN_DESC *pSwapChainDesc,
|
||||
IDXGISwapChain **ppSwapChain,
|
||||
ID3D10Device **ppDevice);
|
||||
|
||||
typedef HRESULT(WINAPI *LPD3D10CREATEDEVICEANDSWAPCHAIN1)(IDXGIAdapter *pAdapter,
|
||||
D3D10_DRIVER_TYPE DriverType,
|
||||
HMODULE Software,
|
||||
UINT Flags,
|
||||
D3D10_FEATURE_LEVEL1 HardwareLevel,
|
||||
UINT SDKVersion,
|
||||
DXGI_SWAP_CHAIN_DESC *pSwapChainDesc,
|
||||
IDXGISwapChain **ppSwapChain,
|
||||
ID3D10Device1 **ppDevice);
|
||||
|
||||
// Module and function pointers
|
||||
static HMODULE g_hModDXGI = NULL;
|
||||
static HMODULE g_hModD3D10 = NULL;
|
||||
static HMODULE g_hModD3D101 = NULL;
|
||||
static LPCREATEDXGIFACTORY sFnPtr_CreateDXGIFactory = NULL;
|
||||
static LPD3D10CREATESTATEBLOCK sFnPtr_D3D10CreateStateBlock = NULL;
|
||||
static LPD3D10CREATEDEVICE sFnPtr_D3D10CreateDevice = NULL;
|
||||
static LPD3D10CREATEDEVICE1 sFnPtr_D3D10CreateDevice1 = NULL;
|
||||
static LPD3D10STATEBLOCKMASKUNION sFnPtr_D3D10StateBlockMaskUnion = NULL;
|
||||
static LPD3D10STATEBLOCKMASKINTERSECT sFnPtr_D3D10StateBlockMaskIntersect = NULL;
|
||||
static LPD3D10STATEBLOCKMASKDIFFERENCE sFnPtr_D3D10StateBlockMaskDifference = NULL;
|
||||
static LPD3D10STATEBLOCKMASKENABLECAPTURE sFnPtr_D3D10StateBlockMaskEnableCapture = NULL;
|
||||
static LPD3D10STATEBLOCKMASKDISABLECAPTURE sFnPtr_D3D10StateBlockMaskDisableCapture = NULL;
|
||||
static LPD3D10STATEBLOCKMASKENABLEALL sFnPtr_D3D10StateBlockMaskEnableAll = NULL;
|
||||
static LPD3D10STATEBLOCKMASKDISABLEALL sFnPtr_D3D10StateBlockMaskDisableAll = NULL;
|
||||
static LPD3D10STATEBLOCKMASKGETSETTING sFnPtr_D3D10StateBlockMaskGetSetting = NULL;
|
||||
static LPD3D10COMPILEEFFECTFROMMEMORY sFnPtr_D3D10CompileEffectFromMemory = NULL;
|
||||
static LPD3D10CREATEEFFECTFROMMEMORY sFnPtr_D3D10CreateEffectFromMemory = NULL;
|
||||
static LPD3D10CREATEEFFECTPOOLFROMMEMORY sFnPtr_D3D10CreateEffectPoolFromMemory = NULL;
|
||||
static LPD3D10CREATEDEVICEANDSWAPCHAIN sFnPtr_D3D10CreateDeviceAndSwapChain = NULL;
|
||||
static LPD3D10CREATEDEVICEANDSWAPCHAIN1 sFnPtr_D3D10CreateDeviceAndSwapChain1 = NULL;
|
||||
|
||||
// unload the D3D10 DLLs
|
||||
static bool dynlinkUnloadD3D10API(void)
|
||||
{
|
||||
if (g_hModD3D10)
|
||||
{
|
||||
FreeLibrary(g_hModD3D10);
|
||||
g_hModD3D10 = NULL;
|
||||
}
|
||||
|
||||
if (g_hModDXGI)
|
||||
{
|
||||
FreeLibrary(g_hModDXGI);
|
||||
g_hModDXGI = NULL;
|
||||
}
|
||||
|
||||
if (g_hModD3D101)
|
||||
{
|
||||
FreeLibrary(g_hModD3D101);
|
||||
g_hModD3D101 = NULL;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Dynamically load the D3D10 DLLs loaded and map the function pointers
|
||||
static bool dynlinkLoadD3D10API(void)
|
||||
{
|
||||
// First check to see if the D3D10 Library is present.
|
||||
// if it succeeds, then we can call GetProcAddress to grab all of the DX10 functions
|
||||
g_hModD3D10 = LoadLibrary("d3d10.dll");
|
||||
|
||||
if (g_hModD3D10 != NULL)
|
||||
{
|
||||
sFnPtr_D3D10CreateStateBlock = (LPD3D10CREATESTATEBLOCK) GetProcAddress(g_hModD3D10, "D3D10CreateStateBlock");
|
||||
sFnPtr_D3D10CreateDevice = (LPD3D10CREATEDEVICE) GetProcAddress(g_hModD3D10, "D3D10CreateDevice");
|
||||
|
||||
sFnPtr_D3D10StateBlockMaskUnion = (LPD3D10STATEBLOCKMASKUNION) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskUnion");
|
||||
sFnPtr_D3D10StateBlockMaskIntersect = (LPD3D10STATEBLOCKMASKINTERSECT) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskIntersect");
|
||||
sFnPtr_D3D10StateBlockMaskDifference = (LPD3D10STATEBLOCKMASKDIFFERENCE) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskDifference");
|
||||
sFnPtr_D3D10StateBlockMaskEnableCapture = (LPD3D10STATEBLOCKMASKENABLECAPTURE) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskEnableCapture");
|
||||
sFnPtr_D3D10StateBlockMaskDisableCapture = (LPD3D10STATEBLOCKMASKDISABLECAPTURE)GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskDisableCapture");
|
||||
|
||||
sFnPtr_D3D10StateBlockMaskEnableAll = (LPD3D10STATEBLOCKMASKENABLEALL) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskEnableAll");
|
||||
sFnPtr_D3D10StateBlockMaskDisableAll = (LPD3D10STATEBLOCKMASKDISABLEALL) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskDisableAll");
|
||||
sFnPtr_D3D10StateBlockMaskGetSetting = (LPD3D10STATEBLOCKMASKGETSETTING) GetProcAddress(g_hModD3D10, "D3D10StateBlockMaskGetSetting");
|
||||
|
||||
sFnPtr_D3D10CompileEffectFromMemory = (LPD3D10COMPILEEFFECTFROMMEMORY) GetProcAddress(g_hModD3D10, "D3D10CompileEffectFromMemory");
|
||||
sFnPtr_D3D10CreateEffectFromMemory = (LPD3D10CREATEEFFECTFROMMEMORY) GetProcAddress(g_hModD3D10, "D3D10CreateEffectFromMemory");
|
||||
sFnPtr_D3D10CreateEffectPoolFromMemory = (LPD3D10CREATEEFFECTPOOLFROMMEMORY) GetProcAddress(g_hModD3D10, "D3D10CreateEffectPoolFromMemory");
|
||||
|
||||
sFnPtr_D3D10CreateDeviceAndSwapChain = (LPD3D10CREATEDEVICEANDSWAPCHAIN) GetProcAddress(g_hModD3D10, "D3D10CreateDeviceAndSwapChain");
|
||||
}
|
||||
|
||||
g_hModDXGI = LoadLibrary("dxgi.dll");
|
||||
|
||||
if (g_hModDXGI)
|
||||
{
|
||||
sFnPtr_CreateDXGIFactory = (LPCREATEDXGIFACTORY) GetProcAddress(g_hModDXGI , "CreateDXGIFactory");
|
||||
}
|
||||
|
||||
// This may fail if this machine isn't Windows Vista SP1 or later
|
||||
g_hModD3D101 = LoadLibrary("d3d10_1.dll");
|
||||
|
||||
if (g_hModD3D101 != NULL)
|
||||
{
|
||||
sFnPtr_D3D10CreateDevice1 = (LPD3D10CREATEDEVICE1) GetProcAddress(g_hModD3D101, "D3D10CreateDevice1");
|
||||
sFnPtr_D3D10CreateDeviceAndSwapChain1 = (LPD3D10CREATEDEVICEANDSWAPCHAIN1) GetProcAddress(g_hModD3D101, "D3D10CreateDeviceAndSwapChain1");
|
||||
}
|
||||
|
||||
if (g_hModD3D10 == NULL || g_hModDXGI == NULL || g_hModD3D101 == NULL)
|
||||
{
|
||||
dynlinkUnloadD3D10API();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
78
Common/multithreading.cpp
Normal file
78
Common/multithreading.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <multithreading.h>
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
// Create thread
|
||||
CUTThread cutStartThread(CUT_THREADROUTINE func, void *data) {
|
||||
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, data, 0, NULL);
|
||||
}
|
||||
|
||||
// Wait for thread to finish
|
||||
void cutEndThread(CUTThread thread) {
|
||||
WaitForSingleObject(thread, INFINITE);
|
||||
CloseHandle(thread);
|
||||
}
|
||||
|
||||
// Destroy thread
|
||||
void cutDestroyThread(CUTThread thread) {
|
||||
TerminateThread(thread, 0);
|
||||
CloseHandle(thread);
|
||||
}
|
||||
|
||||
// Wait for multiple threads
|
||||
void cutWaitForThreads(const CUTThread *threads, int num) {
|
||||
WaitForMultipleObjects(num, threads, true, INFINITE);
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
CloseHandle(threads[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Create thread
|
||||
CUTThread cutStartThread(CUT_THREADROUTINE func, void *data) {
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, func, data);
|
||||
return thread;
|
||||
}
|
||||
|
||||
// Wait for thread to finish
|
||||
void cutEndThread(CUTThread thread) { pthread_join(thread, NULL); }
|
||||
|
||||
// Destroy thread
|
||||
void cutDestroyThread(CUTThread thread) { pthread_cancel(thread); }
|
||||
|
||||
// Wait for multiple threads
|
||||
void cutWaitForThreads(const CUTThread *threads, int num) {
|
||||
for (int i = 0; i < num; i++) {
|
||||
cutEndThread(threads[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
76
Common/multithreading.h
Normal file
76
Common/multithreading.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef MULTITHREADING_H
|
||||
#define MULTITHREADING_H
|
||||
|
||||
|
||||
//Simple portable thread library.
|
||||
|
||||
//Windows threads.
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
#include <windows.h>
|
||||
|
||||
typedef HANDLE CUTThread;
|
||||
typedef unsigned(WINAPI *CUT_THREADROUTINE)(void *);
|
||||
|
||||
#define CUT_THREADPROC unsigned WINAPI
|
||||
#define CUT_THREADEND return 0
|
||||
|
||||
#else
|
||||
//POSIX threads.
|
||||
#include <pthread.h>
|
||||
|
||||
typedef pthread_t CUTThread;
|
||||
typedef void *(*CUT_THREADROUTINE)(void *);
|
||||
|
||||
#define CUT_THREADPROC void
|
||||
#define CUT_THREADEND
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//Create thread.
|
||||
CUTThread cutStartThread(CUT_THREADROUTINE, void *data);
|
||||
|
||||
//Wait for thread to finish.
|
||||
void cutEndThread(CUTThread thread);
|
||||
|
||||
//Destroy thread.
|
||||
void cutDestroyThread(CUTThread thread);
|
||||
|
||||
//Wait for multiple threads.
|
||||
void cutWaitForThreads(const CUTThread *threads, int num);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} //extern "C"
|
||||
#endif
|
||||
|
||||
#endif //MULTITHREADING_H
|
||||
111
Common/nvMath.h
Normal file
111
Common/nvMath.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// Template math library for common 3D functionality
|
||||
//
|
||||
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||
// The copyright for glh follows this notice.
|
||||
//
|
||||
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
Copyright (c) 2000 Cass Everitt
|
||||
Copyright (c) 2000 NVIDIA Corporation
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or
|
||||
without modification, are permitted provided that the following
|
||||
conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials
|
||||
provided with the distribution.
|
||||
|
||||
* The names of contributors to this software may not be used
|
||||
to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
Cass Everitt - cass@r3.nu
|
||||
*/
|
||||
|
||||
#ifndef NV_MATH_H
|
||||
#define NV_MATH_H
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <nvVector.h>
|
||||
#include <nvMatrix.h>
|
||||
#include <nvQuaternion.h>
|
||||
|
||||
#define NV_PI float(3.1415926535897932384626433832795)
|
||||
|
||||
namespace nv
|
||||
{
|
||||
|
||||
typedef vec2<float> vec2f;
|
||||
typedef vec3<float> vec3f;
|
||||
typedef vec3<int> vec3i;
|
||||
typedef vec3<unsigned int> vec3ui;
|
||||
typedef vec4<float> vec4f;
|
||||
typedef matrix4<float> matrix4f;
|
||||
typedef quaternion<float> quaternionf;
|
||||
|
||||
|
||||
inline void applyRotation(const quaternionf &r)
|
||||
{
|
||||
float angle;
|
||||
vec3f axis;
|
||||
r.get_value(axis, angle);
|
||||
glRotatef(angle/3.1415926f * 180.0f, axis[0], axis[1], axis[2]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
540
Common/nvMatrix.h
Normal file
540
Common/nvMatrix.h
Normal file
@@ -0,0 +1,540 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// Template math library for common 3D functionality
|
||||
//
|
||||
// nvMatrix.h - template matrix code
|
||||
//
|
||||
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||
// The copyright for glh follows this notice.
|
||||
//
|
||||
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
Copyright (c) 2000 Cass Everitt
|
||||
Copyright (c) 2000 NVIDIA Corporation
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or
|
||||
without modification, are permitted provided that the following
|
||||
conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials
|
||||
provided with the distribution.
|
||||
|
||||
* The names of contributors to this software may not be used
|
||||
to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
Cass Everitt - cass@r3.nu
|
||||
*/
|
||||
|
||||
#ifndef NV_MATRIX_H
|
||||
#define NV_MATRIX_H
|
||||
|
||||
namespace nv
|
||||
{
|
||||
|
||||
template <class T> class vec2;
|
||||
template <class T> class vec3;
|
||||
template <class T> class vec4;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Matrix
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
template<class T>
|
||||
class matrix4
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
matrix4()
|
||||
{
|
||||
make_identity();
|
||||
}
|
||||
|
||||
matrix4(T t)
|
||||
{
|
||||
set_value(t);
|
||||
}
|
||||
|
||||
matrix4(const T *m)
|
||||
{
|
||||
set_value(m);
|
||||
}
|
||||
|
||||
matrix4(T a00, T a01, T a02, T a03,
|
||||
T a10, T a11, T a12, T a13,
|
||||
T a20, T a21, T a22, T a23,
|
||||
T a30, T a31, T a32, T a33) :
|
||||
_11(a00), _12(a01), _13(a02), _14(a03),
|
||||
_21(a10), _22(a11), _23(a12), _24(a13),
|
||||
_31(a20), _32(a21), _33(a22), _34(a23),
|
||||
_41(a30), _42(a31), _43(a32), _44(a33)
|
||||
{}
|
||||
|
||||
|
||||
void get_value(T *mp) const
|
||||
{
|
||||
int c = 0;
|
||||
|
||||
for (int j=0; j < 4; j++)
|
||||
for (int i=0; i < 4; i++)
|
||||
{
|
||||
mp[c++] = element(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
const T *get_value() const
|
||||
{
|
||||
return _array;
|
||||
}
|
||||
|
||||
void set_value(T *mp)
|
||||
{
|
||||
int c = 0;
|
||||
|
||||
for (int j=0; j < 4; j++)
|
||||
for (int i=0; i < 4; i++)
|
||||
{
|
||||
element(i,j) = mp[c++];
|
||||
}
|
||||
}
|
||||
|
||||
void set_value(T r)
|
||||
{
|
||||
for (int i=0; i < 4; i++)
|
||||
for (int j=0; j < 4; j++)
|
||||
{
|
||||
element(i,j) = r;
|
||||
}
|
||||
}
|
||||
|
||||
void make_identity()
|
||||
{
|
||||
element(0,0) = 1.0;
|
||||
element(0,1) = 0.0;
|
||||
element(0,2) = 0.0;
|
||||
element(0,3) = 0.0;
|
||||
|
||||
element(1,0) = 0.0;
|
||||
element(1,1) = 1.0;
|
||||
element(1,2) = 0.0;
|
||||
element(1,3) = 0.0;
|
||||
|
||||
element(2,0) = 0.0;
|
||||
element(2,1) = 0.0;
|
||||
element(2,2) = 1.0;
|
||||
element(2,3) = 0.0;
|
||||
|
||||
element(3,0) = 0.0;
|
||||
element(3,1) = 0.0;
|
||||
element(3,2) = 0.0;
|
||||
element(3,3) = 1.0;
|
||||
}
|
||||
|
||||
// set a uniform scale
|
||||
void set_scale(T s)
|
||||
{
|
||||
element(0,0) = s;
|
||||
element(1,1) = s;
|
||||
element(2,2) = s;
|
||||
}
|
||||
|
||||
void set_scale(const vec3<T> &s)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
element(i,i) = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void set_translate(const vec3<T> &t)
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
element(i,3) = t[i];
|
||||
}
|
||||
}
|
||||
|
||||
void set_row(int r, const vec4<T> &t)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
element(r,i) = t[i];
|
||||
}
|
||||
}
|
||||
|
||||
void set_column(int c, const vec4<T> &t)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
element(i,c) = t[i];
|
||||
}
|
||||
}
|
||||
|
||||
vec4<T> get_row(int r) const
|
||||
{
|
||||
vec4<T> v;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
v[i] = element(r,i);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
vec4<T> get_column(int c) const
|
||||
{
|
||||
vec4<T> v;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
v[i] = element(i,c);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
friend matrix4 inverse(const matrix4 &m)
|
||||
{
|
||||
matrix4 minv;
|
||||
|
||||
T r1[8], r2[8], r3[8], r4[8];
|
||||
T *s[4], *tmprow;
|
||||
|
||||
s[0] = &r1[0];
|
||||
s[1] = &r2[0];
|
||||
s[2] = &r3[0];
|
||||
s[3] = &r4[0];
|
||||
|
||||
register int i,j,p,jj;
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
for (j=0; j<4; j++)
|
||||
{
|
||||
s[i][j] = m.element(i,j);
|
||||
|
||||
if (i==j)
|
||||
{
|
||||
s[i][j+4] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
s[i][j+4] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
T scp[4];
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
scp[i] = T(fabs(s[i][0]));
|
||||
|
||||
for (j=1; j<4; j++)
|
||||
if (T(fabs(s[i][j])) > scp[i])
|
||||
{
|
||||
scp[i] = T(fabs(s[i][j]));
|
||||
}
|
||||
|
||||
if (scp[i] == 0.0)
|
||||
{
|
||||
return minv; // singular matrix!
|
||||
}
|
||||
}
|
||||
|
||||
int pivot_to;
|
||||
T scp_max;
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
// select pivot row
|
||||
pivot_to = i;
|
||||
scp_max = T(fabs(s[i][i]/scp[i]));
|
||||
|
||||
// find out which row should be on top
|
||||
for (p=i+1; p<4; p++)
|
||||
if (T(fabs(s[p][i]/scp[p])) > scp_max)
|
||||
{
|
||||
scp_max = T(fabs(s[p][i]/scp[p]));
|
||||
pivot_to = p;
|
||||
}
|
||||
|
||||
// Pivot if necessary
|
||||
if (pivot_to != i)
|
||||
{
|
||||
tmprow = s[i];
|
||||
s[i] = s[pivot_to];
|
||||
s[pivot_to] = tmprow;
|
||||
T tmpscp;
|
||||
tmpscp = scp[i];
|
||||
scp[i] = scp[pivot_to];
|
||||
scp[pivot_to] = tmpscp;
|
||||
}
|
||||
|
||||
T mji;
|
||||
|
||||
// perform gaussian elimination
|
||||
for (j=i+1; j<4; j++)
|
||||
{
|
||||
mji = s[j][i]/s[i][i];
|
||||
s[j][i] = 0.0;
|
||||
|
||||
for (jj=i+1; jj<8; jj++)
|
||||
{
|
||||
s[j][jj] -= mji*s[i][jj];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (s[3][3] == 0.0)
|
||||
{
|
||||
return minv; // singular matrix!
|
||||
}
|
||||
|
||||
//
|
||||
// Now we have an upper triangular matrix.
|
||||
//
|
||||
// x x x x | y y y y
|
||||
// 0 x x x | y y y y
|
||||
// 0 0 x x | y y y y
|
||||
// 0 0 0 x | y y y y
|
||||
//
|
||||
// we'll back substitute to get the inverse
|
||||
//
|
||||
// 1 0 0 0 | z z z z
|
||||
// 0 1 0 0 | z z z z
|
||||
// 0 0 1 0 | z z z z
|
||||
// 0 0 0 1 | z z z z
|
||||
//
|
||||
|
||||
T mij;
|
||||
|
||||
for (i=3; i>0; i--)
|
||||
{
|
||||
for (j=i-1; j > -1; j--)
|
||||
{
|
||||
mij = s[j][i]/s[i][i];
|
||||
|
||||
for (jj=j+1; jj<8; jj++)
|
||||
{
|
||||
s[j][jj] -= mij*s[i][jj];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
for (j=0; j<4; j++)
|
||||
{
|
||||
minv(i,j) = s[i][j+4] / s[i][i];
|
||||
}
|
||||
|
||||
return minv;
|
||||
}
|
||||
|
||||
|
||||
friend matrix4 transpose(const matrix4 &m)
|
||||
{
|
||||
matrix4 mtrans;
|
||||
|
||||
for (int i=0; i<4; i++)
|
||||
for (int j=0; j<4; j++)
|
||||
{
|
||||
mtrans(i,j) = m.element(j,i);
|
||||
}
|
||||
|
||||
return mtrans;
|
||||
}
|
||||
|
||||
matrix4 &operator *= (const matrix4 &rhs)
|
||||
{
|
||||
matrix4 mt(*this);
|
||||
set_value(T(0));
|
||||
|
||||
for (int i=0; i < 4; i++)
|
||||
for (int j=0; j < 4; j++)
|
||||
for (int c=0; c < 4; c++)
|
||||
{
|
||||
element(i,j) += mt(i,c) * rhs(c,j);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend matrix4 operator * (const matrix4 &lhs, const matrix4 &rhs)
|
||||
{
|
||||
matrix4 r(T(0));
|
||||
|
||||
for (int i=0; i < 4; i++)
|
||||
for (int j=0; j < 4; j++)
|
||||
for (int c=0; c < 4; c++)
|
||||
{
|
||||
r.element(i,j) += lhs(i,c) * rhs(c,j);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
// dst = M * src
|
||||
vec4<T> operator *(const vec4<T> &src) const
|
||||
{
|
||||
vec4<T> r;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
r[i] = (src[0] * element(i,0) + src[1] * element(i,1) +
|
||||
src[2] * element(i,2) + src[3] * element(i,3));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
// dst = src * M
|
||||
friend vec4<T> operator *(const vec4<T> &lhs, const matrix4 &rhs)
|
||||
{
|
||||
vec4<T> r;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
r[i] = (lhs[0] * rhs.element(0,i) + lhs[1] * rhs.element(1,i) +
|
||||
lhs[2] * rhs.element(2,i) + lhs[3] * rhs.element(3,i));
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
T &operator()(int row, int col)
|
||||
{
|
||||
return element(row,col);
|
||||
}
|
||||
|
||||
const T &operator()(int row, int col) const
|
||||
{
|
||||
return element(row,col);
|
||||
}
|
||||
|
||||
T &element(int row, int col)
|
||||
{
|
||||
return _array[row | (col<<2)];
|
||||
}
|
||||
|
||||
const T &element(int row, int col) const
|
||||
{
|
||||
return _array[row | (col<<2)];
|
||||
}
|
||||
|
||||
matrix4 &operator *= (const T &r)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
element(0,i) *= r;
|
||||
element(1,i) *= r;
|
||||
element(2,i) *= r;
|
||||
element(3,i) *= r;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
matrix4 &operator += (const matrix4 &mat)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
element(0,i) += mat.element(0,i);
|
||||
element(1,i) += mat.element(1,i);
|
||||
element(2,i) += mat.element(2,i);
|
||||
element(3,i) += mat.element(3,i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
friend bool operator == (const matrix4 &lhs, const matrix4 &rhs)
|
||||
{
|
||||
bool r = true;
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
r &= lhs._array[i] == rhs._array[i];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
friend bool operator != (const matrix4 &lhs, const matrix4 &rhs)
|
||||
{
|
||||
bool r = true;
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
r &= lhs._array[i] != rhs._array[i];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
T _11, _12, _13, _14; // standard names for components
|
||||
T _21, _22, _23, _24; // standard names for components
|
||||
T _31, _32, _33, _34; // standard names for components
|
||||
T _41, _42, _43, _44; // standard names for components
|
||||
};
|
||||
T _array[16]; // array access
|
||||
};
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
530
Common/nvQuaternion.h
Normal file
530
Common/nvQuaternion.h
Normal file
@@ -0,0 +1,530 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//
|
||||
// Template math library for common 3D functionality
|
||||
//
|
||||
// nvQuaterion.h - quaternion template and utility functions
|
||||
//
|
||||
// This code is in part deriver from glh, a cross platform glut helper library.
|
||||
// The copyright for glh follows this notice.
|
||||
//
|
||||
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
Copyright (c) 2000 Cass Everitt
|
||||
Copyright (c) 2000 NVIDIA Corporation
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or
|
||||
without modification, are permitted provided that the following
|
||||
conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials
|
||||
provided with the distribution.
|
||||
|
||||
* The names of contributors to this software may not be used
|
||||
to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
Cass Everitt - cass@r3.nu
|
||||
*/
|
||||
#ifndef NV_QUATERNION_H
|
||||
#define NV_QUATERNION_H
|
||||
|
||||
namespace nv
|
||||
{
|
||||
|
||||
template <class T> class vec2;
|
||||
template <class T> class vec3;
|
||||
template <class T> class vec4;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Quaternion
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template< class T>
|
||||
class quaternion
|
||||
{
|
||||
public:
|
||||
|
||||
quaternion() : x(0.0), y(0.0), z(0.0), w(0.0)
|
||||
{
|
||||
}
|
||||
|
||||
quaternion(const T v[4])
|
||||
{
|
||||
set_value(v);
|
||||
}
|
||||
|
||||
|
||||
quaternion(T q0, T q1, T q2, T q3)
|
||||
{
|
||||
set_value(q0, q1, q2, q3);
|
||||
}
|
||||
|
||||
|
||||
quaternion(const matrix4<T> &m)
|
||||
{
|
||||
set_value(m);
|
||||
}
|
||||
|
||||
|
||||
quaternion(const vec3<T> &axis, T radians)
|
||||
{
|
||||
set_value(axis, radians);
|
||||
}
|
||||
|
||||
|
||||
quaternion(const vec3<T> &rotateFrom, const vec3<T> &rotateTo)
|
||||
{
|
||||
set_value(rotateFrom, rotateTo);
|
||||
}
|
||||
|
||||
quaternion(const vec3<T> &from_look, const vec3<T> &from_up,
|
||||
const vec3<T> &to_look, const vec3<T> &to_up)
|
||||
{
|
||||
set_value(from_look, from_up, to_look, to_up);
|
||||
}
|
||||
|
||||
const T *get_value() const
|
||||
{
|
||||
return &_array[0];
|
||||
}
|
||||
|
||||
void get_value(T &q0, T &q1, T &q2, T &q3) const
|
||||
{
|
||||
q0 = _array[0];
|
||||
q1 = _array[1];
|
||||
q2 = _array[2];
|
||||
q3 = _array[3];
|
||||
}
|
||||
|
||||
quaternion &set_value(T q0, T q1, T q2, T q3)
|
||||
{
|
||||
_array[0] = q0;
|
||||
_array[1] = q1;
|
||||
_array[2] = q2;
|
||||
_array[3] = q3;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void get_value(vec3<T> &axis, T &radians) const
|
||||
{
|
||||
radians = T(acos(_array[3]) * T(2.0));
|
||||
|
||||
if (radians == T(0.0))
|
||||
{
|
||||
axis = vec3<T>(0.0, 0.0, 1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
axis[0] = _array[0];
|
||||
axis[1] = _array[1];
|
||||
axis[2] = _array[2];
|
||||
axis = normalize(axis);
|
||||
}
|
||||
}
|
||||
|
||||
void get_value(matrix4<T> &m) const
|
||||
{
|
||||
T s, xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;
|
||||
|
||||
T norm = _array[0] * _array[0] + _array[1] * _array[1] + _array[2] * _array[2] + _array[3] * _array[3];
|
||||
|
||||
s = (norm == T(0.0)) ? T(0.0) : (T(2.0) / norm);
|
||||
|
||||
xs = _array[0] * s;
|
||||
ys = _array[1] * s;
|
||||
zs = _array[2] * s;
|
||||
|
||||
wx = _array[3] * xs;
|
||||
wy = _array[3] * ys;
|
||||
wz = _array[3] * zs;
|
||||
|
||||
xx = _array[0] * xs;
|
||||
xy = _array[0] * ys;
|
||||
xz = _array[0] * zs;
|
||||
|
||||
yy = _array[1] * ys;
|
||||
yz = _array[1] * zs;
|
||||
zz = _array[2] * zs;
|
||||
|
||||
m(0,0) = T(T(1.0) - (yy + zz));
|
||||
m(1,0) = T(xy + wz);
|
||||
m(2,0) = T(xz - wy);
|
||||
|
||||
m(0,1) = T(xy - wz);
|
||||
m(1,1) = T(T(1.0) - (xx + zz));
|
||||
m(2,1) = T(yz + wx);
|
||||
|
||||
m(0,2) = T(xz + wy);
|
||||
m(1,2) = T(yz - wx);
|
||||
m(2,2) = T(T(1.0) - (xx + yy));
|
||||
|
||||
m(3,0) = m(3,1) = m(3,2) = m(0,3) = m(1,3) = m(2,3) = T(0.0);
|
||||
m(3,3) = T(1.0);
|
||||
}
|
||||
|
||||
quaternion &set_value(const T *qp)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
_array[i] = qp[i];
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
quaternion &set_value(const matrix4<T> &m)
|
||||
{
|
||||
T tr, s;
|
||||
int i, j, k;
|
||||
const int nxt[3] = { 1, 2, 0 };
|
||||
|
||||
tr = m(0,0) + m(1,1) + m(2,2);
|
||||
|
||||
if (tr > T(0))
|
||||
{
|
||||
s = T(sqrt(tr + m(3,3)));
|
||||
_array[3] = T(s * 0.5);
|
||||
s = T(0.5) / s;
|
||||
|
||||
_array[0] = T((m(1,2) - m(2,1)) * s);
|
||||
_array[1] = T((m(2,0) - m(0,2)) * s);
|
||||
_array[2] = T((m(0,1) - m(1,0)) * s);
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
|
||||
if (m(1,1) > m(0,0))
|
||||
{
|
||||
i = 1;
|
||||
}
|
||||
|
||||
if (m(2,2) > m(i,i))
|
||||
{
|
||||
i = 2;
|
||||
}
|
||||
|
||||
j = nxt[i];
|
||||
k = nxt[j];
|
||||
|
||||
s = T(sqrt((m(i,j) - (m(j,j) + m(k,k))) + T(1.0)));
|
||||
|
||||
_array[i] = T(s * 0.5);
|
||||
s = T(0.5 / s);
|
||||
|
||||
_array[3] = T((m(j,k) - m(k,j)) * s);
|
||||
_array[j] = T((m(i,j) + m(j,i)) * s);
|
||||
_array[k] = T((m(i,k) + m(k,i)) * s);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
quaternion &set_value(const vec3<T> &axis, T theta)
|
||||
{
|
||||
T sqnorm = square_norm(axis);
|
||||
|
||||
if (sqnorm == T(0.0))
|
||||
{
|
||||
// axis too small.
|
||||
x = y = z = T(0.0);
|
||||
w = T(1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
theta *= T(0.5);
|
||||
T sin_theta = T(sin(theta));
|
||||
|
||||
if (sqnorm != T(1))
|
||||
{
|
||||
sin_theta /= T(sqrt(sqnorm));
|
||||
}
|
||||
|
||||
x = sin_theta * axis[0];
|
||||
y = sin_theta * axis[1];
|
||||
z = sin_theta * axis[2];
|
||||
w = T(cos(theta));
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
quaternion &set_value(const vec3<T> &rotateFrom, const vec3<T> &rotateTo)
|
||||
{
|
||||
vec3<T> p1, p2;
|
||||
T alpha;
|
||||
|
||||
p1 = normalize(rotateFrom);
|
||||
p2 = normalize(rotateTo);
|
||||
|
||||
alpha = dot(p1, p2);
|
||||
|
||||
if (alpha == T(1.0))
|
||||
{
|
||||
*this = quaternion();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ensures that the anti-parallel case leads to a positive dot
|
||||
if (alpha == T(-1.0))
|
||||
{
|
||||
vec3<T> v;
|
||||
|
||||
if (p1[0] != p1[1] || p1[0] != p1[2])
|
||||
{
|
||||
v = vec3<T>(p1[1], p1[2], p1[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
v = vec3<T>(-p1[0], p1[1], p1[2]);
|
||||
}
|
||||
|
||||
v -= p1 * dot(p1, v);
|
||||
v = normalize(v);
|
||||
|
||||
set_value(v, T(3.1415926));
|
||||
return *this;
|
||||
}
|
||||
|
||||
p1 = normalize(cross(p1, p2));
|
||||
|
||||
set_value(p1,T(acos(alpha)));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
quaternion &set_value(const vec3<T> &from_look, const vec3<T> &from_up,
|
||||
const vec3<T> &to_look, const vec3<T> &to_up)
|
||||
{
|
||||
quaternion r_look = quaternion(from_look, to_look);
|
||||
|
||||
vec3<T> rotated_from_up(from_up);
|
||||
r_look.mult_vec(rotated_from_up);
|
||||
|
||||
quaternion r_twist = quaternion(rotated_from_up, to_up);
|
||||
|
||||
*this = r_twist;
|
||||
*this *= r_look;
|
||||
return *this;
|
||||
}
|
||||
|
||||
quaternion &operator *= (const quaternion<T> &qr)
|
||||
{
|
||||
quaternion ql(*this);
|
||||
|
||||
w = ql.w * qr.w - ql.x * qr.x - ql.y * qr.y - ql.z * qr.z;
|
||||
x = ql.w * qr.x + ql.x * qr.w + ql.y * qr.z - ql.z * qr.y;
|
||||
y = ql.w * qr.y + ql.y * qr.w + ql.z * qr.x - ql.x * qr.z;
|
||||
z = ql.w * qr.z + ql.z * qr.w + ql.x * qr.y - ql.y * qr.x;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend quaternion normalize(const quaternion<T> &q)
|
||||
{
|
||||
quaternion r(q);
|
||||
T rnorm = T(1.0) / T(sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z));
|
||||
|
||||
r.x *= rnorm;
|
||||
r.y *= rnorm;
|
||||
r.z *= rnorm;
|
||||
r.w *= rnorm;
|
||||
}
|
||||
|
||||
friend quaternion<T> conjugate(const quaternion<T> &q)
|
||||
{
|
||||
quaternion<T> r(q);
|
||||
r._array[0] *= T(-1.0);
|
||||
r._array[1] *= T(-1.0);
|
||||
r._array[2] *= T(-1.0);
|
||||
return r;
|
||||
}
|
||||
|
||||
friend quaternion<T> inverse(const quaternion<T> &q)
|
||||
{
|
||||
return conjugate(q);
|
||||
}
|
||||
|
||||
//
|
||||
// Quaternion multiplication with cartesian vector
|
||||
// v' = q*v*q(star)
|
||||
//
|
||||
void mult_vec(const vec3<T> &src, vec3<T> &dst) const
|
||||
{
|
||||
T v_coef = w * w - x * x - y * y - z * z;
|
||||
T u_coef = T(2.0) * (src[0] * x + src[1] * y + src[2] * z);
|
||||
T c_coef = T(2.0) * w;
|
||||
|
||||
dst.v[0] = v_coef * src.v[0] + u_coef * x + c_coef * (y * src.v[2] - z * src.v[1]);
|
||||
dst.v[1] = v_coef * src.v[1] + u_coef * y + c_coef * (z * src.v[0] - x * src.v[2]);
|
||||
dst.v[2] = v_coef * src.v[2] + u_coef * z + c_coef * (x * src.v[1] - y * src.v[0]);
|
||||
}
|
||||
|
||||
void mult_vec(vec3<T> &src_and_dst) const
|
||||
{
|
||||
mult_vec(vec3<T>(src_and_dst), src_and_dst);
|
||||
}
|
||||
|
||||
void scale_angle(T scaleFactor)
|
||||
{
|
||||
vec3<T> axis;
|
||||
T radians;
|
||||
|
||||
get_value(axis, radians);
|
||||
radians *= scaleFactor;
|
||||
set_value(axis, radians);
|
||||
}
|
||||
|
||||
friend quaternion<T> slerp(const quaternion<T> &p, const quaternion<T> &q, T alpha)
|
||||
{
|
||||
quaternion r;
|
||||
|
||||
T cos_omega = p.x * q.x + p.y * q.y + p.z * q.z + p.w * q.w;
|
||||
// if B is on opposite hemisphere from A, use -B instead
|
||||
|
||||
int bflip;
|
||||
|
||||
if ((bflip = (cos_omega < T(0))))
|
||||
{
|
||||
cos_omega = -cos_omega;
|
||||
}
|
||||
|
||||
// complementary interpolation parameter
|
||||
T beta = T(1) - alpha;
|
||||
|
||||
if (cos_omega >= T(1))
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
T omega = T(acos(cos_omega));
|
||||
T one_over_sin_omega = T(1.0) / T(sin(omega));
|
||||
|
||||
beta = T(sin(omega*beta) * one_over_sin_omega);
|
||||
alpha = T(sin(omega*alpha) * one_over_sin_omega);
|
||||
|
||||
if (bflip)
|
||||
{
|
||||
alpha = -alpha;
|
||||
}
|
||||
|
||||
r.x = beta * p._array[0]+ alpha * q._array[0];
|
||||
r.y = beta * p._array[1]+ alpha * q._array[1];
|
||||
r.z = beta * p._array[2]+ alpha * q._array[2];
|
||||
r.w = beta * p._array[3]+ alpha * q._array[3];
|
||||
return r;
|
||||
}
|
||||
|
||||
T &operator [](int i)
|
||||
{
|
||||
return _array[i];
|
||||
}
|
||||
|
||||
const T &operator [](int i) const
|
||||
{
|
||||
return _array[i];
|
||||
}
|
||||
|
||||
|
||||
friend bool operator == (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||
{
|
||||
bool r = true;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
r &= lhs._array[i] == rhs._array[i];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
friend bool operator != (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||
{
|
||||
bool r = true;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
r &= lhs._array[i] == rhs._array[i];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
friend quaternion<T> operator * (const quaternion<T> &lhs, const quaternion<T> &rhs)
|
||||
{
|
||||
quaternion r(lhs);
|
||||
r *= rhs;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
T x;
|
||||
T y;
|
||||
T z;
|
||||
T w;
|
||||
};
|
||||
T _array[4];
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
260
Common/nvShaderUtils.h
Normal file
260
Common/nvShaderUtils.h
Normal file
@@ -0,0 +1,260 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Utility functions for compiling shaders and programs
|
||||
*
|
||||
* Author: Evan Hart
|
||||
* Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef NV_SHADER_UTILS_H
|
||||
#define NV_SHADER_UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace nv {
|
||||
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
inline GLuint CompileGLSLShader(GLenum target, const char *shader) {
|
||||
GLuint object;
|
||||
|
||||
object = glCreateShader(target);
|
||||
|
||||
if (!object) {
|
||||
return object;
|
||||
}
|
||||
|
||||
glShaderSource(object, 1, &shader, NULL);
|
||||
|
||||
glCompileShader(object);
|
||||
|
||||
// check if shader compiled
|
||||
GLint compiled = 0;
|
||||
glGetShaderiv(object, GL_COMPILE_STATUS, &compiled);
|
||||
|
||||
if (!compiled) {
|
||||
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||
char temp[256] = "";
|
||||
glGetShaderInfoLog(object, 256, NULL, temp);
|
||||
fprintf(stderr, "Compile failed:\n%s\n", temp);
|
||||
#endif
|
||||
glDeleteShader(object);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
inline GLuint CompileGLSLShaderFromFile(GLenum target, const char *filename) {
|
||||
FILE *shaderFile;
|
||||
char *text;
|
||||
long size;
|
||||
size_t fsize = 0;
|
||||
|
||||
// read files as binary to prevent problems from newline translation
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
|
||||
if (fopen_s(&shaderFile, filename, "rb") != 0)
|
||||
#else
|
||||
if ((shaderFile = fopen(filename, "rb")) == 0)
|
||||
#endif
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get the length of the file
|
||||
fseek(shaderFile, 0, SEEK_END);
|
||||
size = ftell(shaderFile);
|
||||
|
||||
// Read the file contents from the start, then close file and add a null
|
||||
// terminator
|
||||
fseek(shaderFile, 0, SEEK_SET);
|
||||
text = new char[size + 1];
|
||||
fsize = fread(text, size, 1, shaderFile);
|
||||
fclose(shaderFile);
|
||||
|
||||
if (fsize == 0) {
|
||||
printf("CompileGLSLShaderFromFile(), error... fsize = 0\n");
|
||||
}
|
||||
|
||||
text[size] = '\0';
|
||||
|
||||
GLuint object = CompileGLSLShader(target, text);
|
||||
|
||||
delete[] text;
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
// Create a program composed of vertex and fragment shaders.
|
||||
inline GLuint LinkGLSLProgram(GLuint vertexShader, GLuint fragmentShader) {
|
||||
GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, fragmentShader);
|
||||
glLinkProgram(program);
|
||||
|
||||
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||
// Get error log.
|
||||
GLint charsWritten, infoLogLength;
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
char *infoLog = new char[infoLogLength];
|
||||
glGetProgramInfoLog(program, infoLogLength, &charsWritten, infoLog);
|
||||
printf(infoLog);
|
||||
delete[] infoLog;
|
||||
#endif
|
||||
|
||||
// Test linker result.
|
||||
GLint linkSucceed = GL_FALSE;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceed);
|
||||
|
||||
if (linkSucceed == GL_FALSE) {
|
||||
glDeleteProgram(program);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
// Create a program composed of vertex, geometry and fragment shaders.
|
||||
inline GLuint LinkGLSLProgram(GLuint vertexShader, GLuint geometryShader,
|
||||
GLint inputType, GLint vertexOut,
|
||||
GLint outputType, GLuint fragmentShader) {
|
||||
GLuint program = glCreateProgram();
|
||||
glAttachShader(program, vertexShader);
|
||||
glAttachShader(program, geometryShader);
|
||||
glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, inputType);
|
||||
glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, vertexOut);
|
||||
glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, outputType);
|
||||
glAttachShader(program, fragmentShader);
|
||||
glLinkProgram(program);
|
||||
|
||||
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||
// Get error log.
|
||||
GLint charsWritten, infoLogLength;
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
|
||||
|
||||
char *infoLog = new char[infoLogLength];
|
||||
glGetProgramInfoLog(program, infoLogLength, &charsWritten, infoLog);
|
||||
printf(infoLog);
|
||||
delete[] infoLog;
|
||||
#endif
|
||||
|
||||
// Test linker result.
|
||||
GLint linkSucceed = GL_FALSE;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceed);
|
||||
|
||||
if (linkSucceed == GL_FALSE) {
|
||||
glDeleteProgram(program);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
inline GLuint CompileASMShader(GLenum program_type, const char *code) {
|
||||
GLuint program_id;
|
||||
glGenProgramsARB(1, &program_id);
|
||||
glBindProgramARB(program_type, program_id);
|
||||
glProgramStringARB(program_type, GL_PROGRAM_FORMAT_ASCII_ARB,
|
||||
(GLsizei)strlen(code), (GLubyte *)code);
|
||||
|
||||
GLint error_pos;
|
||||
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
|
||||
|
||||
if (error_pos != -1) {
|
||||
#ifdef NV_REPORT_COMPILE_ERRORS
|
||||
const GLubyte *error_string;
|
||||
error_string = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
||||
fprintf(stderr, "Program error at position: %d\n%s\n", (int)error_pos,
|
||||
error_string);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
return program_id;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
////////////////////////////////////////////////////////////
|
||||
inline GLuint CompileASMShaderFromFile(GLenum target, const char *filename) {
|
||||
FILE *shaderFile;
|
||||
char *text;
|
||||
long size;
|
||||
size_t fsize = 0;
|
||||
|
||||
// read files as binary to prevent problems from newline translation
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
||||
|
||||
if (fopen_s(&shaderFile, filename, "rb") != 0)
|
||||
#else
|
||||
if ((shaderFile = fopen(filename, "rb")) == 0)
|
||||
#endif
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get the length of the file
|
||||
fseek(shaderFile, 0, SEEK_END);
|
||||
size = ftell(shaderFile);
|
||||
|
||||
// Read the file contents from the start, then close file and add a null
|
||||
// terminator
|
||||
fseek(shaderFile, 0, SEEK_SET);
|
||||
text = new char[size + 1];
|
||||
fsize = fread(text, size, 1, shaderFile);
|
||||
fclose(shaderFile);
|
||||
|
||||
if (fsize == 0) {
|
||||
printf("CompileGLSLShaderFromFile(), error... fsize = 0\n");
|
||||
}
|
||||
|
||||
text[size] = '\0';
|
||||
|
||||
GLuint program_id = CompileASMShader(target, text);
|
||||
|
||||
delete[] text;
|
||||
|
||||
return program_id;
|
||||
}
|
||||
|
||||
} // namespace nv
|
||||
#endif
|
||||
1074
Common/nvVector.h
Normal file
1074
Common/nvVector.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
236
Common/param.h
Normal file
236
Common/param.h
Normal file
@@ -0,0 +1,236 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
Simple parameter system
|
||||
sgreen@nvidia.com 4/2001
|
||||
*/
|
||||
|
||||
#ifndef PARAM_H
|
||||
#define PARAM_H
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// base class for named parameter
|
||||
class ParamBase {
|
||||
public:
|
||||
ParamBase(const char *name) : m_name(name) {}
|
||||
virtual ~ParamBase() {}
|
||||
|
||||
std::string &GetName() { return m_name; }
|
||||
|
||||
virtual float GetFloatValue() = 0;
|
||||
virtual int GetIntValue() = 0;
|
||||
virtual std::string GetValueString() = 0;
|
||||
|
||||
virtual void Reset() = 0;
|
||||
virtual void Increment() = 0;
|
||||
virtual void Decrement() = 0;
|
||||
|
||||
virtual float GetPercentage() = 0;
|
||||
virtual void SetPercentage(float p) = 0;
|
||||
|
||||
virtual void Write(std::ostream &stream) = 0;
|
||||
virtual void Read(std::istream &stream) = 0;
|
||||
|
||||
virtual bool IsList() = 0;
|
||||
|
||||
protected:
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
// derived class for single-valued parameter
|
||||
template <class T>
|
||||
class Param : public ParamBase {
|
||||
public:
|
||||
Param(const char *name, T value = 0, T min = 0, T max = 10000, T step = 1,
|
||||
T *ptr = 0)
|
||||
: ParamBase(name),
|
||||
m_default(value),
|
||||
m_min(min),
|
||||
m_max(max),
|
||||
m_step(step),
|
||||
m_precision(3) {
|
||||
if (ptr) {
|
||||
m_ptr = ptr;
|
||||
} else {
|
||||
m_ptr = &m_value;
|
||||
}
|
||||
|
||||
*m_ptr = value;
|
||||
}
|
||||
~Param() {}
|
||||
|
||||
T GetValue() const { return *m_ptr; }
|
||||
T SetValue(const T value) { *m_ptr = value; }
|
||||
|
||||
float GetFloatValue() { return (float)*m_ptr; }
|
||||
int GetIntValue() { return (int)*m_ptr; }
|
||||
|
||||
std::string GetValueString() {
|
||||
std::ostringstream ost;
|
||||
ost << std::setprecision(m_precision) << std::fixed;
|
||||
ost << *m_ptr;
|
||||
return ost.str();
|
||||
}
|
||||
|
||||
void SetPrecision(int x) { m_precision = x; }
|
||||
|
||||
float GetPercentage() { return (*m_ptr - m_min) / (float)(m_max - m_min); }
|
||||
|
||||
void SetPercentage(float p) { *m_ptr = (T)(m_min + p * (m_max - m_min)); }
|
||||
|
||||
void Reset() { *m_ptr = m_default; }
|
||||
|
||||
void Increment() {
|
||||
*m_ptr += m_step;
|
||||
|
||||
if (*m_ptr > m_max) {
|
||||
*m_ptr = m_max;
|
||||
}
|
||||
}
|
||||
|
||||
void Decrement() {
|
||||
*m_ptr -= m_step;
|
||||
|
||||
if (*m_ptr < m_min) {
|
||||
*m_ptr = m_min;
|
||||
}
|
||||
}
|
||||
|
||||
void Write(std::ostream &stream) {
|
||||
stream << m_name << " " << *m_ptr << '\n';
|
||||
}
|
||||
void Read(std::istream &stream) { stream >> m_name >> *m_ptr; }
|
||||
|
||||
bool IsList() { return false; }
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
T *m_ptr; // pointer to value declared elsewhere
|
||||
T m_default, m_min, m_max, m_step;
|
||||
int m_precision; // number of digits after decimal point in string output
|
||||
};
|
||||
|
||||
const Param<int> dummy("error");
|
||||
|
||||
// list of parameters
|
||||
class ParamList : public ParamBase {
|
||||
public:
|
||||
ParamList(const char *name = "") : ParamBase(name) { active = true; }
|
||||
~ParamList() {}
|
||||
|
||||
float GetFloatValue() { return 0.0f; }
|
||||
int GetIntValue() { return 0; }
|
||||
|
||||
void AddParam(ParamBase *param) {
|
||||
m_params.push_back(param);
|
||||
m_map[param->GetName()] = param;
|
||||
m_current = m_params.begin();
|
||||
}
|
||||
|
||||
// look-up parameter based on name
|
||||
ParamBase *GetParam(char *name) {
|
||||
ParamBase *p = m_map[name];
|
||||
|
||||
if (p) {
|
||||
return p;
|
||||
} else {
|
||||
return (ParamBase *)&dummy;
|
||||
}
|
||||
}
|
||||
|
||||
ParamBase *GetParam(int i) { return m_params[i]; }
|
||||
|
||||
ParamBase *GetCurrent() { return *m_current; }
|
||||
|
||||
int GetSize() { return (int)m_params.size(); }
|
||||
|
||||
std::string GetValueString() { return m_name; }
|
||||
|
||||
// functions to traverse list
|
||||
void Reset() { m_current = m_params.begin(); }
|
||||
|
||||
void Increment() {
|
||||
m_current++;
|
||||
|
||||
if (m_current == m_params.end()) {
|
||||
m_current = m_params.begin();
|
||||
}
|
||||
}
|
||||
|
||||
void Decrement() {
|
||||
if (m_current == m_params.begin()) {
|
||||
m_current = m_params.end() - 1;
|
||||
} else {
|
||||
m_current--;
|
||||
}
|
||||
}
|
||||
|
||||
float GetPercentage() { return 0.0f; }
|
||||
void SetPercentage(float /*p*/) {}
|
||||
|
||||
void Write(std::ostream &stream) {
|
||||
stream << m_name << '\n';
|
||||
|
||||
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||
p != m_params.end(); ++p) {
|
||||
(*p)->Write(stream);
|
||||
}
|
||||
}
|
||||
|
||||
void Read(std::istream &stream) {
|
||||
stream >> m_name;
|
||||
|
||||
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||
p != m_params.end(); ++p) {
|
||||
(*p)->Read(stream);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsList() { return true; }
|
||||
|
||||
void ResetAll() {
|
||||
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||
p != m_params.end(); ++p) {
|
||||
(*p)->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
bool active;
|
||||
std::vector<ParamBase *> m_params;
|
||||
std::map<std::string, ParamBase *> m_map;
|
||||
std::vector<ParamBase *>::const_iterator m_current;
|
||||
};
|
||||
|
||||
#endif
|
||||
307
Common/paramgl.h
Normal file
307
Common/paramgl.h
Normal file
@@ -0,0 +1,307 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
ParamListGL
|
||||
- class derived from ParamList to do simple OpenGL rendering of a parameter
|
||||
list sgg 8/2001
|
||||
*/
|
||||
|
||||
#ifndef PARAMGL_H
|
||||
#define PARAMGL_H
|
||||
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
#include <GLUT/glut.h>
|
||||
#else
|
||||
#include <GL/freeglut.h>
|
||||
#endif
|
||||
|
||||
#include <param.h>
|
||||
#include <string.h>
|
||||
|
||||
inline void beginWinCoords(void) {
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glTranslatef(0.0, (GLfloat)(glutGet(GLUT_WINDOW_HEIGHT) - 1.0), 0.0);
|
||||
glScalef(1.0, -1.0, 1.0);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT), -1, 1);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
}
|
||||
|
||||
inline void endWinCoords(void) {
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
inline void glPrint(int x, int y, const char *s, void *font) {
|
||||
glRasterPos2f((GLfloat)x, (GLfloat)y);
|
||||
int len = (int)strlen(s);
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
glutBitmapCharacter(font, s[i]);
|
||||
}
|
||||
}
|
||||
|
||||
inline void glPrintShadowed(int x, int y, const char *s, void *font,
|
||||
float *color) {
|
||||
glColor3f(0.0, 0.0, 0.0);
|
||||
glPrint(x - 1, y - 1, s, font);
|
||||
|
||||
glColor3fv((GLfloat *)color);
|
||||
glPrint(x, y, s, font);
|
||||
}
|
||||
|
||||
class ParamListGL : public ParamList {
|
||||
public:
|
||||
ParamListGL(const char *name = "")
|
||||
: ParamList(name),
|
||||
m_active(true),
|
||||
m_text_color_selected(1.0, 1.0, 1.0),
|
||||
m_text_color_unselected(0.75, 0.75, 0.75),
|
||||
m_text_color_shadow(0.0, 0.0, 0.0),
|
||||
m_bar_color_outer(0.25, 0.25, 0.25),
|
||||
m_bar_color_inner(1.0, 1.0, 1.0) {
|
||||
m_font = (void *)GLUT_BITMAP_9_BY_15; // GLUT_BITMAP_8_BY_13;
|
||||
m_font_h = 15;
|
||||
m_bar_x = 260;
|
||||
m_bar_w = 250;
|
||||
m_bar_h = 10;
|
||||
m_bar_offset = 5;
|
||||
m_text_x = 5;
|
||||
m_separation = 15;
|
||||
m_value_x = 200;
|
||||
m_start_x = 0;
|
||||
m_start_y = 0;
|
||||
}
|
||||
|
||||
void Render(int x, int y, bool shadow = false) {
|
||||
beginWinCoords();
|
||||
|
||||
m_start_x = x;
|
||||
m_start_y = y;
|
||||
|
||||
for (std::vector<ParamBase *>::const_iterator p = m_params.begin();
|
||||
p != m_params.end(); ++p) {
|
||||
if ((*p)->IsList()) {
|
||||
ParamListGL *list = (ParamListGL *)(*p);
|
||||
list->Render(x + 10, y);
|
||||
y += m_separation * list->GetSize();
|
||||
} else {
|
||||
if (p == m_current) {
|
||||
glColor3fv(&m_text_color_selected.r);
|
||||
} else {
|
||||
glColor3fv(&m_text_color_unselected.r);
|
||||
}
|
||||
|
||||
if (shadow) {
|
||||
glPrintShadowed(x + m_text_x, y + m_font_h, (*p)->GetName().c_str(),
|
||||
m_font,
|
||||
(p == m_current) ? &m_text_color_selected.r
|
||||
: &m_text_color_unselected.r);
|
||||
glPrintShadowed(x + m_value_x, y + m_font_h,
|
||||
(*p)->GetValueString().c_str(), m_font,
|
||||
(p == m_current) ? &m_text_color_selected.r
|
||||
: &m_text_color_unselected.r);
|
||||
} else {
|
||||
glPrint(x + m_text_x, y + m_font_h, (*p)->GetName().c_str(), m_font);
|
||||
glPrint(x + m_value_x, y + m_font_h, (*p)->GetValueString().c_str(),
|
||||
m_font);
|
||||
}
|
||||
|
||||
glColor3fv((GLfloat *)&m_bar_color_outer.r);
|
||||
glBegin(GL_LINE_LOOP);
|
||||
glVertex2f((GLfloat)(x + m_bar_x), (GLfloat)(y + m_bar_offset));
|
||||
glVertex2f((GLfloat)(x + m_bar_x + m_bar_w),
|
||||
(GLfloat)(y + m_bar_offset));
|
||||
glVertex2f((GLfloat)(x + m_bar_x + m_bar_w),
|
||||
(GLfloat)(y + m_bar_offset + m_bar_h));
|
||||
glVertex2f((GLfloat)(x + m_bar_x),
|
||||
(GLfloat)(y + m_bar_offset + m_bar_h));
|
||||
glEnd();
|
||||
|
||||
glColor3fv((GLfloat *)&m_bar_color_inner.r);
|
||||
glRectf(
|
||||
(GLfloat)(x + m_bar_x), (GLfloat)(y + m_bar_offset + m_bar_h),
|
||||
(GLfloat)(x + m_bar_x + ((m_bar_w - 1) * (*p)->GetPercentage())),
|
||||
(GLfloat)(y + m_bar_offset + 1));
|
||||
|
||||
y += m_separation;
|
||||
}
|
||||
}
|
||||
|
||||
endWinCoords();
|
||||
}
|
||||
|
||||
bool Mouse(int x, int y, int button = GLUT_LEFT_BUTTON,
|
||||
int state = GLUT_DOWN) {
|
||||
if ((y < m_start_y) ||
|
||||
(y > (int)(m_start_y + (m_separation * m_params.size()) - 1))) {
|
||||
m_active = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_active = true;
|
||||
|
||||
int i = (y - m_start_y) / m_separation;
|
||||
|
||||
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) {
|
||||
#if defined(__GNUC__) && (__GNUC__ < 3)
|
||||
m_current = &m_params[i];
|
||||
#else
|
||||
|
||||
// MJH: workaround since the version of vector::at used here is
|
||||
// non-standard
|
||||
for (m_current = m_params.begin(); m_current != m_params.end() && i > 0;
|
||||
m_current++, i--)
|
||||
;
|
||||
|
||||
// m_current = (std::vector<ParamBase
|
||||
// *>::const_iterator)&m_params.at(i);
|
||||
#endif
|
||||
|
||||
if ((x > m_bar_x) && (x < m_bar_x + m_bar_w)) {
|
||||
Motion(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Motion(int x, int y) {
|
||||
if ((y < m_start_y) ||
|
||||
(y > m_start_y + (m_separation * (int)m_params.size()) - 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x < m_bar_x) {
|
||||
(*m_current)->SetPercentage(0.0);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x > m_bar_x + m_bar_w) {
|
||||
(*m_current)->SetPercentage(1.0);
|
||||
return true;
|
||||
}
|
||||
|
||||
(*m_current)->SetPercentage((x - m_bar_x) / (float)m_bar_w);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Special(int key, int x, int y) {
|
||||
if (!m_active) return;
|
||||
|
||||
switch (key) {
|
||||
case GLUT_KEY_DOWN:
|
||||
Increment();
|
||||
break;
|
||||
|
||||
case GLUT_KEY_UP:
|
||||
Decrement();
|
||||
break;
|
||||
|
||||
case GLUT_KEY_RIGHT:
|
||||
GetCurrent()->Increment();
|
||||
break;
|
||||
|
||||
case GLUT_KEY_LEFT:
|
||||
GetCurrent()->Decrement();
|
||||
break;
|
||||
|
||||
case GLUT_KEY_HOME:
|
||||
GetCurrent()->Reset();
|
||||
break;
|
||||
|
||||
case GLUT_KEY_END:
|
||||
GetCurrent()->SetPercentage(1.0);
|
||||
break;
|
||||
}
|
||||
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void SetFont(void *font, int height) {
|
||||
m_font = font;
|
||||
m_font_h = height;
|
||||
}
|
||||
|
||||
void SetSelectedColor(float r, float g, float b) {
|
||||
m_text_color_selected = Color(r, g, b);
|
||||
}
|
||||
void SetUnSelectedColor(float r, float g, float b) {
|
||||
m_text_color_unselected = Color(r, g, b);
|
||||
}
|
||||
void SetBarColorInner(float r, float g, float b) {
|
||||
m_bar_color_inner = Color(r, g, b);
|
||||
}
|
||||
void SetBarColorOuter(float r, float g, float b) {
|
||||
m_bar_color_outer = Color(r, g, b);
|
||||
}
|
||||
|
||||
void SetActive(bool b) { m_active = b; }
|
||||
|
||||
private:
|
||||
void *m_font;
|
||||
int m_font_h; // font height
|
||||
|
||||
int m_bar_x; // bar start x position
|
||||
int m_bar_w; // bar width
|
||||
int m_bar_h; // bar height
|
||||
int m_text_x; // text start x position
|
||||
int m_separation; // bar separation in y
|
||||
int m_value_x; // value text x position
|
||||
int m_bar_offset; // bar offset in y
|
||||
|
||||
int m_start_x, m_start_y;
|
||||
|
||||
bool m_active;
|
||||
|
||||
struct Color {
|
||||
Color(float _r, float _g, float _b) {
|
||||
r = _r;
|
||||
g = _g;
|
||||
b = _b;
|
||||
}
|
||||
float r, g, b;
|
||||
};
|
||||
|
||||
Color m_text_color_selected;
|
||||
Color m_text_color_unselected;
|
||||
Color m_text_color_shadow;
|
||||
Color m_bar_color_outer;
|
||||
Color m_bar_color_inner;
|
||||
};
|
||||
|
||||
#endif
|
||||
128
Common/rendercheck_d3d10.cpp
Normal file
128
Common/rendercheck_d3d10.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Utility funcs to wrap up saving a surface or the back buffer as a PPM file
|
||||
// In addition, wraps up a threshold comparision of two PPMs.
|
||||
//
|
||||
// These functions are designed to be used to implement an automated QA testing
|
||||
// for SDK samples.
|
||||
//
|
||||
// Author: Bryan Dudash
|
||||
// Email: sdkfeedback@nvidia.com
|
||||
//
|
||||
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include <rendercheck_d3d10.h>
|
||||
|
||||
HRESULT CheckRenderD3D10::ActiveRenderTargetToPPM(ID3D10Device *pDevice,
|
||||
const char *zFileName) {
|
||||
ID3D10RenderTargetView *pRTV = NULL;
|
||||
pDevice->OMGetRenderTargets(1, &pRTV, NULL);
|
||||
|
||||
ID3D10Resource *pSourceResource = NULL;
|
||||
pRTV->GetResource(&pSourceResource);
|
||||
|
||||
return ResourceToPPM(pDevice, pSourceResource, zFileName);
|
||||
}
|
||||
|
||||
HRESULT CheckRenderD3D10::ResourceToPPM(ID3D10Device *pDevice,
|
||||
ID3D10Resource *pResource,
|
||||
const char *zFileName) {
|
||||
D3D10_RESOURCE_DIMENSION rType;
|
||||
pResource->GetType(&rType);
|
||||
|
||||
if (rType != D3D10_RESOURCE_DIMENSION_TEXTURE2D) {
|
||||
printf("SurfaceToPPM: pResource is not a 2D texture! Aborting...\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
ID3D10Texture2D *pSourceTexture = (ID3D10Texture2D *)pResource;
|
||||
ID3D10Texture2D *pTargetTexture = NULL;
|
||||
|
||||
D3D10_TEXTURE2D_DESC desc;
|
||||
pSourceTexture->GetDesc(&desc);
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ;
|
||||
desc.Usage = D3D10_USAGE_STAGING;
|
||||
|
||||
if (FAILED(pDevice->CreateTexture2D(&desc, NULL, &pTargetTexture))) {
|
||||
printf(
|
||||
"SurfaceToPPM: Unable to create target Texture resoruce! Aborting... "
|
||||
"\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
pDevice->CopyResource(pTargetTexture, pSourceTexture);
|
||||
|
||||
D3D10_MAPPED_TEXTURE2D mappedTex2D;
|
||||
pTargetTexture->Map(0, D3D10_MAP_READ, 0, &mappedTex2D);
|
||||
|
||||
// Need to convert from dx pitch to pitch=width
|
||||
unsigned char *pPPMData = new unsigned char[desc.Width * desc.Height * 4];
|
||||
|
||||
for (unsigned int iHeight = 0; iHeight < desc.Height; iHeight++) {
|
||||
memcpy(
|
||||
&(pPPMData[iHeight * desc.Width * 4]),
|
||||
(unsigned char *)(mappedTex2D.pData) + iHeight * mappedTex2D.RowPitch,
|
||||
desc.Width * 4);
|
||||
}
|
||||
|
||||
pTargetTexture->Unmap(0);
|
||||
|
||||
// Prepends the PPM header info and bumps byte data afterwards
|
||||
sdkSavePPM4ub(zFileName, pPPMData, desc.Width, desc.Height);
|
||||
|
||||
delete[] pPPMData;
|
||||
pTargetTexture->Release();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
bool CheckRenderD3D10::PPMvsPPM(const char *src_file, const char *ref_file,
|
||||
const char *exec_path, const float epsilon,
|
||||
const float threshold) {
|
||||
char *ref_file_path = sdkFindFilePath(ref_file, exec_path);
|
||||
|
||||
if (ref_file_path == NULL) {
|
||||
printf(
|
||||
"CheckRenderD3D10::PPMvsPPM unable to find <%s> in <%s> Aborting "
|
||||
"comparison!\n",
|
||||
ref_file, exec_path);
|
||||
printf(">>> Check info.xml and [project//data] folder <%s> <<<\n",
|
||||
ref_file);
|
||||
printf("Aborting comparison!\n");
|
||||
printf(" FAILURE!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return (sdkComparePPM(src_file, ref_file_path, epsilon, threshold, true) ==
|
||||
true);
|
||||
}
|
||||
53
Common/rendercheck_d3d10.h
Normal file
53
Common/rendercheck_d3d10.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef _RENDERCHECK_D3D10_H_
|
||||
#define _RENDERCHECK_D3D10_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <d3d10.h>
|
||||
|
||||
class CheckRenderD3D10 {
|
||||
public:
|
||||
CheckRenderD3D10() {}
|
||||
|
||||
static HRESULT ActiveRenderTargetToPPM(ID3D10Device *pDevice,
|
||||
const char *zFileName);
|
||||
static HRESULT ResourceToPPM(ID3D10Device *pDevice, ID3D10Resource *pResource,
|
||||
const char *zFileName);
|
||||
|
||||
static bool PPMvsPPM(const char *src_file, const char *ref_file,
|
||||
const char *exec_path, const float epsilon,
|
||||
const float threshold = 0.0f);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
||||
167
Common/rendercheck_d3d9.cpp
Normal file
167
Common/rendercheck_d3d9.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Utility funcs to wrap up savings a surface or the back buffer as a PPM file
|
||||
// In addition, wraps up a threshold comparision of two PPMs.
|
||||
//
|
||||
// These functions are designed to be used to implement an automated QA testing
|
||||
// for SDK samples.
|
||||
//
|
||||
// Author: Bryan Dudash
|
||||
// Email: sdkfeedback@nvidia.com
|
||||
//
|
||||
// Copyright (c) NVIDIA Corporation. All rights reserved.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include <rendercheck_d3d9.h>
|
||||
|
||||
// originally copied from checkrender_gl.cpp and slightly modified
|
||||
bool CheckRenderD3D9::PPMvsPPM(const char *src_file, const char *ref_file,
|
||||
const char *exec_path, const float epsilon,
|
||||
const float threshold) {
|
||||
char *ref_file_path = sdkFindFilePath(ref_file, exec_path);
|
||||
|
||||
if (ref_file_path == NULL) {
|
||||
printf(
|
||||
"CheckRenderD3D9::PPMvsPPM unable to find <%s> in <%s> Aborting "
|
||||
"comparison!\n",
|
||||
ref_file, exec_path);
|
||||
printf(">>> Check info.xml and [project//data] folder <%s> <<<\n",
|
||||
ref_file);
|
||||
printf("Aborting comparison!\n");
|
||||
printf(" FAILURE!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return (sdkComparePPM(src_file, ref_file_path, epsilon, threshold, true) ==
|
||||
true);
|
||||
};
|
||||
|
||||
HRESULT CheckRenderD3D9::BackbufferToPPM(IDirect3DDevice9 *pDevice,
|
||||
const char *zFileName) {
|
||||
IDirect3DSurface9 *pSurface = NULL;
|
||||
|
||||
if (FAILED(
|
||||
pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface))) {
|
||||
printf("Unable to get the back buffer. Aborting...\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
// D3DXSaveSurfaceToFile("C:\\bing.dds",D3DXIFF_DDS,pSurface,NULL,NULL);
|
||||
|
||||
HRESULT hr = S_OK;
|
||||
hr = SurfaceToPPM(pDevice, pSurface, zFileName);
|
||||
|
||||
pSurface->Release();
|
||||
|
||||
return hr;
|
||||
}
|
||||
|
||||
HRESULT CheckRenderD3D9::SurfaceToPPM(IDirect3DDevice9 *pDevice,
|
||||
IDirect3DSurface9 *pSurface,
|
||||
const char *zFileName) {
|
||||
D3DSURFACE_DESC pDesc;
|
||||
pSurface->GetDesc(&pDesc);
|
||||
|
||||
// $$ For now only support common 8bit formats. TODO: support for more
|
||||
// complex formats via conversion?
|
||||
if (!(pDesc.Format == D3DFMT_A8R8G8B8 || pDesc.Format == D3DFMT_X8R8G8B8)) {
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
IDirect3DTexture9 *pTargetTex = NULL;
|
||||
|
||||
if (FAILED(pDevice->CreateTexture(pDesc.Width, pDesc.Height, 1,
|
||||
D3DUSAGE_DYNAMIC, pDesc.Format,
|
||||
D3DPOOL_SYSTEMMEM, &pTargetTex, NULL))) {
|
||||
printf("Unable to create texture for surface transfer! Aborting...\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
IDirect3DSurface9 *pTargetSurface = NULL;
|
||||
|
||||
if (FAILED(pTargetTex->GetSurfaceLevel(0, &pTargetSurface))) {
|
||||
printf("Unable to get surface for surface transfer! Aborting...\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
// This is required because we cannot lock a D3DPOOL_DEAULT surface directly.
|
||||
// So, we copy to our sysmem surface.
|
||||
if (FAILED(pDevice->GetRenderTargetData(pSurface, pTargetSurface))) {
|
||||
printf(
|
||||
"Unable to GetRenderTargetData() for surface transfer! Aborting...\n");
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
D3DLOCKED_RECT lockedRect;
|
||||
HRESULT hr = pTargetSurface->LockRect(&lockedRect, NULL, 0);
|
||||
|
||||
// Need to convert from dx pitch to pitch=width
|
||||
//
|
||||
// $ PPM is BGR and not RGB it seems. Saved image looks "funny" in viewer(red
|
||||
// and blue swapped), but since ref will be dumped using same method, this is
|
||||
// ok.
|
||||
// however, if we want the saved image to be properly colored, then we
|
||||
// can swizzle the color bytes here.
|
||||
unsigned char *pPPMData = new unsigned char[pDesc.Width * pDesc.Height * 4];
|
||||
|
||||
for (unsigned int iHeight = 0; iHeight < pDesc.Height; iHeight++) {
|
||||
#if 1 // swizzle to implment RGB to BGR conversion.
|
||||
|
||||
for (unsigned int iWidth = 0; iWidth < pDesc.Width; iWidth++) {
|
||||
DWORD color = *(DWORD *)((unsigned char *)(lockedRect.pBits) +
|
||||
iHeight * lockedRect.Pitch + iWidth * 4);
|
||||
|
||||
// R<->B, [7:0] <-> [23:16], swizzle
|
||||
color = ((color & 0xFF) << 16) | (color & 0xFF00) |
|
||||
((color & 0xFF0000) >> 16) | (color & 0xFF000000);
|
||||
|
||||
memcpy(&(pPPMData[(iHeight * pDesc.Width + iWidth) * 4]),
|
||||
(unsigned char *)&color, 4);
|
||||
}
|
||||
|
||||
#else
|
||||
memcpy(&(pPPMData[iHeight * pDesc.Width * 4]),
|
||||
(unsigned char *)(lockedRect.pBits) + iHeight * lockedRect.Pitch,
|
||||
pDesc.Width * 4);
|
||||
#endif
|
||||
}
|
||||
|
||||
pTargetSurface->UnlockRect();
|
||||
|
||||
// Prepends the PPM header info and bumps byte data afterwards
|
||||
sdkSavePPM4ub(zFileName, pPPMData, pDesc.Width, pDesc.Height);
|
||||
|
||||
delete[] pPPMData;
|
||||
pTargetSurface->Release();
|
||||
pTargetTex->Release();
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
54
Common/rendercheck_d3d9.h
Normal file
54
Common/rendercheck_d3d9.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef _RENDERCHECK_D3D9_H_
|
||||
#define _RENDERCHECK_D3D9_H_
|
||||
|
||||
#include <assert.h>
|
||||
#include <d3d9.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
class CheckRenderD3D9 {
|
||||
public:
|
||||
CheckRenderD3D9() {}
|
||||
|
||||
static HRESULT BackbufferToPPM(IDirect3DDevice9 *pDevice,
|
||||
const char *zFileName);
|
||||
static HRESULT SurfaceToPPM(IDirect3DDevice9 *pDevice,
|
||||
IDirect3DSurface9 *pSurface,
|
||||
const char *zFileName);
|
||||
|
||||
static bool PPMvsPPM(const char *src_file, const char *ref_file,
|
||||
const char *exec_path, const float epsilon,
|
||||
const float threshold = 0.0f);
|
||||
};
|
||||
|
||||
#endif
|
||||
1347
Common/rendercheck_gl.h
Normal file
1347
Common/rendercheck_gl.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user