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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ add_subdirectory(angelwrap)
add_subdirectory(cgame)
add_subdirectory(game)
add_subdirectory(steamshim)
add_subdirectory(gameshared/q_math)


if (NOT GAME_MODULES_ONLY)
add_subdirectory(cin)
Expand Down
21 changes: 21 additions & 0 deletions source/gameshared/q_math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
project(vectmath)


function(add_ref_math_test)
cmake_parse_arguments(
P_ARGS
""
"NAME"
"SRCS"
${ARGN}
)
add_executable(${P_ARGS_NAME} ${P_ARGS_SRCS} ${CMOCKA_SRC_FILES})
target_include_directories(${P_ARGS_NAME} PRIVATE ${CMOCKA_INCLUDE_DIR} ${STB_INCLUDE_DIR} ".")
target_link_libraries(${P_ARGS_NAME} PRIVATE "m")
qf_set_output_dir(${P_ARGS_NAME} test)
endfunction()

if(BUILD_UNIT_TEST )
add_ref_math_test(NAME vec_test SRCS "test/vector_test.c")
endif()

30 changes: 30 additions & 0 deletions source/gameshared/q_math/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Vector Math library for 3-D linear algebra (vector, matrix, quaternion)
SIMD support for SSE. Also includes generic multi-platform scalar version.

Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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 the Sony Computer Entertainment Inc 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 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 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.

53 changes: 53 additions & 0 deletions source/gameshared/q_math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

# Vectormath

Modified version of Sony's open sourced header-only vector and matrix math library.
I've uploaded a copy here so it can be easily submoduled on other projects.

The main differences from the original library released by Sony:

- Removed deprecated SPU/PPU implementations that only targeted PS3/PowerPC.
- Removed the C interfaces - the C++ interface is much nicer to use with operator overloads, return by val, etc.
- Massive namespace cleanup. Removed or replaced most macros with functions and constants.
- Better compliance with strict aliasing rules.
- Added portable macros for alignment annotations to remove some `#ifdefs`.
- Internal SSE helper code moved to a separate header - other files also renamed.
- Removed the Aos/Soa sub-namespaces, since the Soa implementations were only available for SPU.
- The library now includes only the generic scalar version and the x86/64 SSE intrinsics version.
- Added an unpadded `Vector2` and `Point2` to also support basic 2D vector maths. These are always scalar mode (size = 2 floats).
- All you need to do is include the public header file `vectormath.hpp`. It will expose the relevant parts of the library for you and try to select the SSE implementation if supported.

### Original copyright notice:

<pre>
Vector Math library for 3-D linear algebra (vector, matrix, quaternion)
SIMD support for SSE. Also includes generic multi-platform scalar version.

Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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 the Sony Computer Entertainment Inc 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 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 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.
</pre>

32 changes: 32 additions & 0 deletions source/gameshared/q_math/q_math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef Q_MATH_H_
#define Q_MATH_H_

#include "../q_arch.h"


#ifdef __cplusplus
#define Q_MATH_METHOD(METHOD_C, METHOD_CPP) METHOD_CPP
#else
#define Q_MATH_METHOD(METHOD_C, METHOD_CPP) METHOD_C
#endif


//#elif (VECTORMATH_CPU_HAS_SSE1_OR_BETTER && !VECTORMATH_FORCE_SCALAR_MODE) // SSE
// #include "sse/vectormath.hpp"
// using namespace Vectormath::SSE;
//#elif (VECTORMATH_CPU_HAS_NEON && !VECTORMATH_FORCE_SCALAR_MODE) // NEON
// #include "neon/vectormath.hpp"
// using namespace Vectormath::Neon;
//#else // !SSE
//#endif // Vectormath mode selection

#include "q_vector_math.h"
#include "q_math_common.h"

#ifdef __cplusplus
#else
#endif


#endif

64 changes: 64 additions & 0 deletions source/gameshared/q_math/q_math_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@


//----------------------------------------------------------------------------
// floatX
//----------------------------------------------------------------------------
// A simple structure containing 3 floating point values.
// float3 is always guaranteed to be 3 floats in size. Only use when a very
// specific size is required (like defining structures that need to be the
// same across platforms, or the same on CPU and GPU (like constant and
// structured buffers) In all other cases you should opt to use Vector3, since
// it uses SIMD optimizations whenever possible. float3 does not.
//----------------------------------------------------------------------------

#ifndef Q_MATH_COMMON_H_
#define Q_MATH_COMMON_H_

#if defined( _MSC_VER )
// Visual Studio (MS compiler)
#define Q_MATH_ALIGNED( type ) __declspec( align( 16 ) ) type
#define Q_MATH_ALIGNED_TYPE_PRE __declspec( align( 16 ) )
#define Q_MATH_ALIGNED_TYPE_POST /* nothing */
#elif defined( __GNUC__ )
// GCC or Clang
#define Q_MATH_ALIGNED( type ) type __attribute__( ( aligned( 16 ) ) )
#define Q_MATH_ALIGNED_TYPE_PRE /* nothing */
#define Q_MATH_ALIGNED_TYPE_POST __attribute__( ( aligned( 16 ) ) )
#else
// Unknown compiler
#error "Define Q_MATH_ALIGNED for your compiler or platform!"
#endif


#define q_abs(x) (( (x) > 0 ) ? (x) : -(x))
#define q_max( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
#define q_min( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )

struct float4 {
union {
struct {
float x, y, z, w;
};
float v[4];
};
};

struct float3 {
union {
struct {
float x, y, z;
};
float v[3];
};
};

struct float2 {
union {
struct {
float x, y;
};
float v[2];
};
};

#endif
78 changes: 78 additions & 0 deletions source/gameshared/q_math/q_math_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

// ================================================================================================
// -*- C++ -*-
// File: vectormath/vectormath_settings.hpp
// Author: Guilherme R. Lampert
// Created on: April 1st, 2021
// Brief: This header exposes the Sony Vectormath library settings.
// ================================================================================================

#ifndef VECTORMATH_SETTINGS_HPP
#define VECTORMATH_SETTINGS_HPP

#if (!defined(VECTORMATH_DEBUG) && (defined(DEBUG) || defined(_DEBUG)))
#define VECTORMATH_DEBUG 1
#endif // DEBUG || _DEBUG

// Detecting the availability of SSE at compile-time is a bit more involving with Visual Studio...
#if defined(_MSC_VER) && !defined(NN_NINTENDO_SDK)
#if (defined(__AVX__) || defined(__AVX2__) || defined(_M_AMD64) || defined(_M_X64) || (_M_IX86_FP == 1) || (_M_IX86_FP == 2))
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 1
#else // SSE support
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 0
#endif // SSE support
#else // !_MSC_VER
#if defined(__SSE__)
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 1
#elif defined(__ANDROID__)
#if defined(ANDROID_ARM_NEON)
#define VECTORMATH_CPU_HAS_NEON 1
#else
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 0
#define VECTORMATH_CPU_HAS_NEON 0
#endif
#elif defined(__arm64) || defined(__aarch64__) || defined(__arm__ )
#define VECTORMATH_CPU_HAS_NEON 1
#else // !__SSE__
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 0
#define VECTORMATH_CPU_HAS_NEON 0
#endif // __SSE__
#endif // _MSC_VER

#define VECTORMATH_FORCE_SCALAR_MODE 0

#if defined(ORBIS) || defined(PROSPERO)
#define VECTORMATH_MODE_SCE 1
#endif

#if defined(__APPLE__) && defined(__arm__)
#define VECTORMATH_CPU_HAS_SSE1_OR_BETTER 0
#define VECTORMATH_CPU_HAS_NEON 1
#endif

// Vectormath mode selection
#if VECTORMATH_MODE_SCE
#define VECTORMATH_MODE_SCALAR 0
#define VECTORMATH_MODE_SSE 1
#define VECTORMATH_MODE_NEON 0
#define VECTORMATH_MIN_ALIGN 16
#elif (VECTORMATH_CPU_HAS_SSE1_OR_BETTER && !VECTORMATH_FORCE_SCALAR_MODE) // SSE
#define VECTORMATH_MODE_SCALAR 0
#define VECTORMATH_MODE_SSE 1
#define VECTORMATH_MODE_NEON 0
#define VECTORMATH_MIN_ALIGN 16
#elif (VECTORMATH_CPU_HAS_NEON && !VECTORMATH_FORCE_SCALAR_MODE) // NEON
#define VECTORMATH_MODE_SCALAR 0
#define VECTORMATH_MODE_SSE 0
#define VECTORMATH_MODE_NEON 1
#define VECTORMATH_MIN_ALIGN 16
#else // !SSE
#define VECTORMATH_MODE_SCALAR 1
#define VECTORMATH_MODE_SSE 0
#define VECTORMATH_MODE_NEON 0
#define VECTORMATH_MIN_ALIGN 0
#endif // Vectormath mode selection



#endif // VECTORMATH_SETTINGS_HPP
Loading