From 5df2d1b5f75e0ee2d3fce7131fcee5dc8e1d7cc4 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 24 May 2015 12:16:22 -0700 Subject: Move video_core/math.h to common/vector_math.h The file only contained vector manipulation code, and such widely-useable code doesn't belong in video_core. --- src/citra_qt/debugger/graphics_cmdlists.cpp | 4 +- src/common/CMakeLists.txt | 1 + src/common/vector_math.h | 640 +++++++++++++++++++++ src/video_core/CMakeLists.txt | 1 - src/video_core/color.h | 3 +- src/video_core/debug_utils/debug_utils.cpp | 2 +- src/video_core/debug_utils/debug_utils.h | 3 +- src/video_core/math.h | 640 --------------------- src/video_core/pica.h | 3 +- .../renderer_opengl/gl_rasterizer_cache.cpp | 2 +- 10 files changed, 649 insertions(+), 650 deletions(-) create mode 100644 src/common/vector_math.h delete mode 100644 src/video_core/math.h diff --git a/src/citra_qt/debugger/graphics_cmdlists.cpp b/src/citra_qt/debugger/graphics_cmdlists.cpp index 804c735a3..cabf5fe07 100644 --- a/src/citra_qt/debugger/graphics_cmdlists.cpp +++ b/src/citra_qt/debugger/graphics_cmdlists.cpp @@ -11,10 +11,10 @@ #include #include -#include "video_core/pica.h" -#include "video_core/math.h" +#include "common/vector_math.h" #include "video_core/debug_utils/debug_utils.h" +#include "video_core/pica.h" #include "graphics_cmdlists.h" diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index dbaaac77b..d1c306d3a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -54,6 +54,7 @@ set(HEADERS thread_queue_list.h thunk.h timer.h + vector_math.h ) create_directory_groups(${SRCS} ${HEADERS}) diff --git a/src/common/vector_math.h b/src/common/vector_math.h new file mode 100644 index 000000000..4928c9bf2 --- /dev/null +++ b/src/common/vector_math.h @@ -0,0 +1,640 @@ +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + + +// Copyright 2014 Tony Wasserka +// 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 owner 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. + +#pragma once + +#include + +namespace Math { + +template class Vec2; +template class Vec3; +template class Vec4; + +template +static inline Vec2 MakeVec(const T& x, const T& y); +template +static inline Vec3 MakeVec(const T& x, const T& y, const T& z); +template +static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w); + + +template +class Vec2 { +public: + T x; + T y; + + T* AsArray() { return &x; } + + Vec2() = default; + Vec2(const T a[2]) : x(a[0]), y(a[1]) {} + Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} + + template + Vec2 Cast() const { + return Vec2((T2)x, (T2)y); + } + + static Vec2 AssignToAll(const T& f) + { + return Vec2(f, f); + } + + void Write(T a[2]) + { + a[0] = x; a[1] = y; + } + + Vec2 operator +(const Vec2& other) const + { + return MakeVec(x+other.x, y+other.y); + } + void operator += (const Vec2 &other) + { + x+=other.x; y+=other.y; + } + Vec2 operator -(const Vec2& other) const + { + return MakeVec(x-other.x, y-other.y); + } + void operator -= (const Vec2& other) + { + x-=other.x; y-=other.y; + } + Vec2 operator -() const + { + return MakeVec(-x,-y); + } + Vec2 operator * (const Vec2& other) const + { + return MakeVec(x*other.x, y*other.y); + } + template + Vec2 operator * (const V& f) const + { + return MakeVec(x*f,y*f); + } + template + void operator *= (const V& f) + { + x*=f; y*=f; + } + template + Vec2 operator / (const V& f) const + { + return MakeVec(x/f,y/f); + } + template + void operator /= (const V& f) + { + *this = *this / f; + } + + T Length2() const + { + return x*x + y*y; + } + + // Only implemented for T=float + float Length() const; + void SetLength(const float l); + Vec2 WithLength(const float l) const; + float Distance2To(Vec2 &other); + Vec2 Normalized() const; + float Normalize(); // returns the previous length, which is often useful + + T& operator [] (int i) //allow vector[1] = 3 (vector.y=3) + { + return *((&x) + i); + } + T operator [] (const int i) const + { + return *((&x) + i); + } + + void SetZero() + { + x=0; y=0; + } + + // Common aliases: UV (texel coordinates), ST (texture coordinates) + T& u() { return x; } + T& v() { return y; } + T& s() { return x; } + T& t() { return y; } + + const T& u() const { return x; } + const T& v() const { return y; } + const T& s() const { return x; } + const T& t() const { return y; } + + // swizzlers - create a subvector of specific components + const Vec2 yx() const { return Vec2(y, x); } + const Vec2 vu() const { return Vec2(y, x); } + const Vec2 ts() const { return Vec2(y, x); } +}; + +template +Vec2 operator * (const V& f, const Vec2& vec) +{ + return Vec2(f*vec.x,f*vec.y); +} + +typedef Vec2 Vec2f; + +template +class Vec3 +{ +public: + T x; + T y; + T z; + + T* AsArray() { return &x; } + + Vec3() = default; + Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {} + Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} + + template + Vec3 Cast() const { + return MakeVec((T2)x, (T2)y, (T2)z); + } + + // Only implemented for T=int and T=float + static Vec3 FromRGB(unsigned int rgb); + unsigned int ToRGB() const; // alpha bits set to zero + + static Vec3 AssignToAll(const T& f) + { + return MakeVec(f, f, f); + } + + void Write(T a[3]) + { + a[0] = x; a[1] = y; a[2] = z; + } + + Vec3 operator +(const Vec3 &other) const + { + return MakeVec(x+other.x, y+other.y, z+other.z); + } + void operator += (const Vec3 &other) + { + x+=other.x; y+=other.y; z+=other.z; + } + Vec3 operator -(const Vec3 &other) const + { + return MakeVec(x-other.x, y-other.y, z-other.z); + } + void operator -= (const Vec3 &other) + { + x-=other.x; y-=other.y; z-=other.z; + } + Vec3 operator -() const + { + return MakeVec(-x,-y,-z); + } + Vec3 operator * (const Vec3 &other) const + { + return MakeVec(x*other.x, y*other.y, z*other.z); + } + template + Vec3 operator * (const V& f) const + { + return MakeVec(x*f,y*f,z*f); + } + template + void operator *= (const V& f) + { + x*=f; y*=f; z*=f; + } + template + Vec3 operator / (const V& f) const + { + return MakeVec(x/f,y/f,z/f); + } + template + void operator /= (const V& f) + { + *this = *this / f; + } + + T Length2() const + { + return x*x + y*y + z*z; + } + + // Only implemented for T=float + float Length() const; + void SetLength(const float l); + Vec3 WithLength(const float l) const; + float Distance2To(Vec3 &other); + Vec3 Normalized() const; + float Normalize(); // returns the previous length, which is often useful + + T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) + { + return *((&x) + i); + } + T operator [] (const int i) const + { + return *((&x) + i); + } + + void SetZero() + { + x=0; y=0; z=0; + } + + // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) + T& u() { return x; } + T& v() { return y; } + T& w() { return z; } + + T& r() { return x; } + T& g() { return y; } + T& b() { return z; } + + T& s() { return x; } + T& t() { return y; } + T& q() { return z; } + + const T& u() const { return x; } + const T& v() const { return y; } + const T& w() const { return z; } + + const T& r() const { return x; } + const T& g() const { return y; } + const T& b() const { return z; } + + const T& s() const { return x; } + const T& t() const { return y; } + const T& q() const { return z; } + + // swizzlers - create a subvector of specific components + // e.g. Vec2 uv() { return Vec2(x,y); } + // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx) +#define _DEFINE_SWIZZLER2(a, b, name) const Vec2 name() const { return Vec2(a, b); } +#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ + _DEFINE_SWIZZLER2(a, b, a##b); \ + _DEFINE_SWIZZLER2(a, b, a2##b2); \ + _DEFINE_SWIZZLER2(a, b, a3##b3); \ + _DEFINE_SWIZZLER2(a, b, a4##b4); \ + _DEFINE_SWIZZLER2(b, a, b##a); \ + _DEFINE_SWIZZLER2(b, a, b2##a2); \ + _DEFINE_SWIZZLER2(b, a, b3##a3); \ + _DEFINE_SWIZZLER2(b, a, b4##a4) + + DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t); + DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q); + DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q); +#undef DEFINE_SWIZZLER2 +#undef _DEFINE_SWIZZLER2 +}; + +template +Vec3 operator * (const V& f, const Vec3& vec) +{ + return Vec3(f*vec.x,f*vec.y,f*vec.z); +} + +template<> +inline float Vec3::Length() const { + return std::sqrt(x * x + y * y + z * z); +} + +template<> +inline Vec3 Vec3::Normalized() const { + return *this / Length(); +} + + +typedef Vec3 Vec3f; + +template +class Vec4 +{ +public: + T x; + T y; + T z; + T w; + + T* AsArray() { return &x; } + + Vec4() = default; + Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {} + Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} + + template + Vec4 Cast() const { + return Vec4((T2)x, (T2)y, (T2)z, (T2)w); + } + + // Only implemented for T=int and T=float + static Vec4 FromRGBA(unsigned int rgba); + unsigned int ToRGBA() const; + + static Vec4 AssignToAll(const T& f) { + return Vec4(f, f, f, f); + } + + void Write(T a[4]) + { + a[0] = x; a[1] = y; a[2] = z; a[3] = w; + } + + Vec4 operator +(const Vec4& other) const + { + return MakeVec(x+other.x, y+other.y, z+other.z, w+other.w); + } + void operator += (const Vec4& other) + { + x+=other.x; y+=other.y; z+=other.z; w+=other.w; + } + Vec4 operator -(const Vec4 &other) const + { + return MakeVec(x-other.x, y-other.y, z-other.z, w-other.w); + } + void operator -= (const Vec4 &other) + { + x-=other.x; y-=other.y; z-=other.z; w-=other.w; + } + Vec4 operator -() const + { + return MakeVec(-x,-y,-z,-w); + } + Vec4 operator * (const Vec4 &other) const + { + return MakeVec(x*other.x, y*other.y, z*other.z, w*other.w); + } + template + Vec4 operator * (const V& f) const + { + return MakeVec(x*f,y*f,z*f,w*f); + } + template + void operator *= (const V& f) + { + x*=f; y*=f; z*=f; w*=f; + } + template + Vec4 operator / (const V& f) const + { + return MakeVec(x/f,y/f,z/f,w/f); + } + template + void operator /= (const V& f) + { + *this = *this / f; + } + + T Length2() const + { + return x*x + y*y + z*z + w*w; + } + + // Only implemented for T=float + float Length() const; + void SetLength(const float l); + Vec4 WithLength(const float l) const; + float Distance2To(Vec4 &other); + Vec4 Normalized() const; + float Normalize(); // returns the previous length, which is often useful + + T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) + { + return *((&x) + i); + } + T operator [] (const int i) const + { + return *((&x) + i); + } + + void SetZero() + { + x=0; y=0; z=0; + } + + // Common alias: RGBA (colors) + T& r() { return x; } + T& g() { return y; } + T& b() { return z; } + T& a() { return w; } + + const T& r() const { return x; } + const T& g() const { return y; } + const T& b() const { return z; } + const T& a() const { return w; } + + // Swizzlers - Create a subvector of specific components + // e.g. Vec2 uv() { return Vec2(x,y); } + + // _DEFINE_SWIZZLER2 defines a single such function + // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r) + // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and permutations (xy<->yx) +#define _DEFINE_SWIZZLER2(a, b, name) const Vec2 name() const { return Vec2(a, b); } +#define DEFINE_SWIZZLER2_COMP1(a, a2) \ + _DEFINE_SWIZZLER2(a, a, a##a); \ + _DEFINE_SWIZZLER2(a, a, a2##a2) +#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \ + _DEFINE_SWIZZLER2(a, b, a##b); \ + _DEFINE_SWIZZLER2(a, b, a2##b2); \ + _DEFINE_SWIZZLER2(b, a, b##a); \ + _DEFINE_SWIZZLER2(b, a, b2##a2) + + DEFINE_SWIZZLER2_COMP2(x, y, r, g); + DEFINE_SWIZZLER2_COMP2(x, z, r, b); + DEFINE_SWIZZLER2_COMP2(x, w, r, a); + DEFINE_SWIZZLER2_COMP2(y, z, g, b); + DEFINE_SWIZZLER2_COMP2(y, w, g, a); + DEFINE_SWIZZLER2_COMP2(z, w, b, a); + DEFINE_SWIZZLER2_COMP1(x, r); + DEFINE_SWIZZLER2_COMP1(y, g); + DEFINE_SWIZZLER2_COMP1(z, b); + DEFINE_SWIZZLER2_COMP1(w, a); +#undef DEFINE_SWIZZLER2_COMP1 +#undef DEFINE_SWIZZLER2_COMP2 +#undef _DEFINE_SWIZZLER2 + +#define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3 name() const { return Vec3(a, b, c); } +#define DEFINE_SWIZZLER3_COMP1(a, a2) \ + _DEFINE_SWIZZLER3(a, a, a, a##a##a); \ + _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2) +#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \ + _DEFINE_SWIZZLER3(a, b, c, a##b##c); \ + _DEFINE_SWIZZLER3(a, c, b, a##c##b); \ + _DEFINE_SWIZZLER3(b, a, c, b##a##c); \ + _DEFINE_SWIZZLER3(b, c, a, b##c##a); \ + _DEFINE_SWIZZLER3(c, a, b, c##a##b); \ + _DEFINE_SWIZZLER3(c, b, a, c##b##a); \ + _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \ + _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \ + _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \ + _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \ + _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \ + _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2) + + DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b); + DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a); + DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a); + DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a); + DEFINE_SWIZZLER3_COMP1(x, r); + DEFINE_SWIZZLER3_COMP1(y, g); + DEFINE_SWIZZLER3_COMP1(z, b); + DEFINE_SWIZZLER3_COMP1(w, a); +#undef DEFINE_SWIZZLER3_COMP1 +#undef DEFINE_SWIZZLER3_COMP3 +#undef _DEFINE_SWIZZLER3 +}; + + +template +Vec4 operator * (const V& f, const Vec4& vec) +{ + return MakeVec(f*vec.x,f*vec.y,f*vec.z,f*vec.w); +} + +typedef Vec4 Vec4f; + + +template +static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec2& a, const Vec2& b) +{ + return a.x*b.x + a.y*b.y; +} + +template +static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec3& a, const Vec3& b) +{ + return a.x*b.x + a.y*b.y + a.z*b.z; +} + +template +static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec4& a, const Vec4& b) +{ + return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; +} + +template +static inline Vec3 Cross(const Vec3& a, const Vec3& b) +{ + return MakeVec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); +} + +// linear interpolation via float: 0.0=begin, 1.0=end +template +static inline decltype(X{}*float{}+X{}*float{}) Lerp(const X& begin, const X& end, const float t) +{ + return begin*(1.f-t) + end*t; +} + +// linear interpolation via int: 0=begin, base=end +template +static inline decltype((X{}*int{}+X{}*int{}) / base) LerpInt(const X& begin, const X& end, const int t) +{ + return (begin*(base-t) + end*t) / base; +} + +// Utility vector factories +template +static inline Vec2 MakeVec(const T& x, const T& y) +{ + return Vec2{x, y}; +} + +template +static inline Vec3 MakeVec(const T& x, const T& y, const T& z) +{ + return Vec3{x, y, z}; +} + +template +static inline Vec4 MakeVec(const T& x, const T& y, const Vec2& zw) +{ + return MakeVec(x, y, zw[0], zw[1]); +} + +template +static inline Vec3 MakeVec(const Vec2& xy, const T& z) +{ + return MakeVec(xy[0], xy[1], z); +} + +template +static inline Vec3 MakeVec(const T& x, const Vec2& yz) +{ + return MakeVec(x, yz[0], yz[1]); +} + +template +static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w) +{ + return Vec4{x, y, z, w}; +} + +template +static inline Vec4 MakeVec(const Vec2& xy, const T& z, const T& w) +{ + return MakeVec(xy[0], xy[1], z, w); +} + +template +static inline Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) +{ + return MakeVec(x, yz[0], yz[1], w); +} + +// NOTE: This has priority over "Vec2> MakeVec(const Vec2& x, const Vec2& y)". +// Even if someone wanted to use an odd object like Vec2>, the compiler would error +// out soon enough due to misuse of the returned structure. +template +static inline Vec4 MakeVec(const Vec2& xy, const Vec2& zw) +{ + return MakeVec(xy[0], xy[1], zw[0], zw[1]); +} + +template +static inline Vec4 MakeVec(const Vec3& xyz, const T& w) +{ + return MakeVec(xyz[0], xyz[1], xyz[2], w); +} + +template +static inline Vec4 MakeVec(const T& x, const Vec3& yzw) +{ + return MakeVec(x, yzw[0], yzw[1], yzw[2]); +} + + +} // namespace diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 0258a3255..f50f73b52 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -33,7 +33,6 @@ set(HEADERS command_processor.h gpu_debugger.h hwrasterizer_base.h - math.h pica.h primitive_assembly.h rasterizer.h diff --git a/src/video_core/color.h b/src/video_core/color.h index 4d2026eb0..422fdc8af 100644 --- a/src/video_core/color.h +++ b/src/video_core/color.h @@ -6,8 +6,7 @@ #include "common/common_types.h" #include "common/swap.h" - -#include "video_core/math.h" +#include "common/vector_math.h" namespace Color { diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp index b92cd1a7e..5392dae21 100644 --- a/src/video_core/debug_utils/debug_utils.cpp +++ b/src/video_core/debug_utils/debug_utils.cpp @@ -19,9 +19,9 @@ #include "common/assert.h" #include "common/file_util.h" #include "common/math_util.h" +#include "common/vector_math.h" #include "video_core/color.h" -#include "video_core/math.h" #include "video_core/pica.h" #include "video_core/utils.h" #include "video_core/video_core.h" diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h index f361a5385..7926d64ec 100644 --- a/src/video_core/debug_utils/debug_utils.h +++ b/src/video_core/debug_utils/debug_utils.h @@ -12,7 +12,8 @@ #include #include -#include "video_core/math.h" +#include "common/vector_math.h" + #include "video_core/pica.h" namespace Pica { diff --git a/src/video_core/math.h b/src/video_core/math.h deleted file mode 100644 index 4928c9bf2..000000000 --- a/src/video_core/math.h +++ /dev/null @@ -1,640 +0,0 @@ -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - - -// Copyright 2014 Tony Wasserka -// 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 owner 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. - -#pragma once - -#include - -namespace Math { - -template class Vec2; -template class Vec3; -template class Vec4; - -template -static inline Vec2 MakeVec(const T& x, const T& y); -template -static inline Vec3 MakeVec(const T& x, const T& y, const T& z); -template -static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w); - - -template -class Vec2 { -public: - T x; - T y; - - T* AsArray() { return &x; } - - Vec2() = default; - Vec2(const T a[2]) : x(a[0]), y(a[1]) {} - Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} - - template - Vec2 Cast() const { - return Vec2((T2)x, (T2)y); - } - - static Vec2 AssignToAll(const T& f) - { - return Vec2(f, f); - } - - void Write(T a[2]) - { - a[0] = x; a[1] = y; - } - - Vec2 operator +(const Vec2& other) const - { - return MakeVec(x+other.x, y+other.y); - } - void operator += (const Vec2 &other) - { - x+=other.x; y+=other.y; - } - Vec2 operator -(const Vec2& other) const - { - return MakeVec(x-other.x, y-other.y); - } - void operator -= (const Vec2& other) - { - x-=other.x; y-=other.y; - } - Vec2 operator -() const - { - return MakeVec(-x,-y); - } - Vec2 operator * (const Vec2& other) const - { - return MakeVec(x*other.x, y*other.y); - } - template - Vec2 operator * (const V& f) const - { - return MakeVec(x*f,y*f); - } - template - void operator *= (const V& f) - { - x*=f; y*=f; - } - template - Vec2 operator / (const V& f) const - { - return MakeVec(x/f,y/f); - } - template - void operator /= (const V& f) - { - *this = *this / f; - } - - T Length2() const - { - return x*x + y*y; - } - - // Only implemented for T=float - float Length() const; - void SetLength(const float l); - Vec2 WithLength(const float l) const; - float Distance2To(Vec2 &other); - Vec2 Normalized() const; - float Normalize(); // returns the previous length, which is often useful - - T& operator [] (int i) //allow vector[1] = 3 (vector.y=3) - { - return *((&x) + i); - } - T operator [] (const int i) const - { - return *((&x) + i); - } - - void SetZero() - { - x=0; y=0; - } - - // Common aliases: UV (texel coordinates), ST (texture coordinates) - T& u() { return x; } - T& v() { return y; } - T& s() { return x; } - T& t() { return y; } - - const T& u() const { return x; } - const T& v() const { return y; } - const T& s() const { return x; } - const T& t() const { return y; } - - // swizzlers - create a subvector of specific components - const Vec2 yx() const { return Vec2(y, x); } - const Vec2 vu() const { return Vec2(y, x); } - const Vec2 ts() const { return Vec2(y, x); } -}; - -template -Vec2 operator * (const V& f, const Vec2& vec) -{ - return Vec2(f*vec.x,f*vec.y); -} - -typedef Vec2 Vec2f; - -template -class Vec3 -{ -public: - T x; - T y; - T z; - - T* AsArray() { return &x; } - - Vec3() = default; - Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {} - Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} - - template - Vec3 Cast() const { - return MakeVec((T2)x, (T2)y, (T2)z); - } - - // Only implemented for T=int and T=float - static Vec3 FromRGB(unsigned int rgb); - unsigned int ToRGB() const; // alpha bits set to zero - - static Vec3 AssignToAll(const T& f) - { - return MakeVec(f, f, f); - } - - void Write(T a[3]) - { - a[0] = x; a[1] = y; a[2] = z; - } - - Vec3 operator +(const Vec3 &other) const - { - return MakeVec(x+other.x, y+other.y, z+other.z); - } - void operator += (const Vec3 &other) - { - x+=other.x; y+=other.y; z+=other.z; - } - Vec3 operator -(const Vec3 &other) const - { - return MakeVec(x-other.x, y-other.y, z-other.z); - } - void operator -= (const Vec3 &other) - { - x-=other.x; y-=other.y; z-=other.z; - } - Vec3 operator -() const - { - return MakeVec(-x,-y,-z); - } - Vec3 operator * (const Vec3 &other) const - { - return MakeVec(x*other.x, y*other.y, z*other.z); - } - template - Vec3 operator * (const V& f) const - { - return MakeVec(x*f,y*f,z*f); - } - template - void operator *= (const V& f) - { - x*=f; y*=f; z*=f; - } - template - Vec3 operator / (const V& f) const - { - return MakeVec(x/f,y/f,z/f); - } - template - void operator /= (const V& f) - { - *this = *this / f; - } - - T Length2() const - { - return x*x + y*y + z*z; - } - - // Only implemented for T=float - float Length() const; - void SetLength(const float l); - Vec3 WithLength(const float l) const; - float Distance2To(Vec3 &other); - Vec3 Normalized() const; - float Normalize(); // returns the previous length, which is often useful - - T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) - { - return *((&x) + i); - } - T operator [] (const int i) const - { - return *((&x) + i); - } - - void SetZero() - { - x=0; y=0; z=0; - } - - // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) - T& u() { return x; } - T& v() { return y; } - T& w() { return z; } - - T& r() { return x; } - T& g() { return y; } - T& b() { return z; } - - T& s() { return x; } - T& t() { return y; } - T& q() { return z; } - - const T& u() const { return x; } - const T& v() const { return y; } - const T& w() const { return z; } - - const T& r() const { return x; } - const T& g() const { return y; } - const T& b() const { return z; } - - const T& s() const { return x; } - const T& t() const { return y; } - const T& q() const { return z; } - - // swizzlers - create a subvector of specific components - // e.g. Vec2 uv() { return Vec2(x,y); } - // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx) -#define _DEFINE_SWIZZLER2(a, b, name) const Vec2 name() const { return Vec2(a, b); } -#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ - _DEFINE_SWIZZLER2(a, b, a##b); \ - _DEFINE_SWIZZLER2(a, b, a2##b2); \ - _DEFINE_SWIZZLER2(a, b, a3##b3); \ - _DEFINE_SWIZZLER2(a, b, a4##b4); \ - _DEFINE_SWIZZLER2(b, a, b##a); \ - _DEFINE_SWIZZLER2(b, a, b2##a2); \ - _DEFINE_SWIZZLER2(b, a, b3##a3); \ - _DEFINE_SWIZZLER2(b, a, b4##a4) - - DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t); - DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q); - DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q); -#undef DEFINE_SWIZZLER2 -#undef _DEFINE_SWIZZLER2 -}; - -template -Vec3 operator * (const V& f, const Vec3& vec) -{ - return Vec3(f*vec.x,f*vec.y,f*vec.z); -} - -template<> -inline float Vec3::Length() const { - return std::sqrt(x * x + y * y + z * z); -} - -template<> -inline Vec3 Vec3::Normalized() const { - return *this / Length(); -} - - -typedef Vec3 Vec3f; - -template -class Vec4 -{ -public: - T x; - T y; - T z; - T w; - - T* AsArray() { return &x; } - - Vec4() = default; - Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {} - Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} - - template - Vec4 Cast() const { - return Vec4((T2)x, (T2)y, (T2)z, (T2)w); - } - - // Only implemented for T=int and T=float - static Vec4 FromRGBA(unsigned int rgba); - unsigned int ToRGBA() const; - - static Vec4 AssignToAll(const T& f) { - return Vec4(f, f, f, f); - } - - void Write(T a[4]) - { - a[0] = x; a[1] = y; a[2] = z; a[3] = w; - } - - Vec4 operator +(const Vec4& other) const - { - return MakeVec(x+other.x, y+other.y, z+other.z, w+other.w); - } - void operator += (const Vec4& other) - { - x+=other.x; y+=other.y; z+=other.z; w+=other.w; - } - Vec4 operator -(const Vec4 &other) const - { - return MakeVec(x-other.x, y-other.y, z-other.z, w-other.w); - } - void operator -= (const Vec4 &other) - { - x-=other.x; y-=other.y; z-=other.z; w-=other.w; - } - Vec4 operator -() const - { - return MakeVec(-x,-y,-z,-w); - } - Vec4 operator * (const Vec4 &other) const - { - return MakeVec(x*other.x, y*other.y, z*other.z, w*other.w); - } - template - Vec4 operator * (const V& f) const - { - return MakeVec(x*f,y*f,z*f,w*f); - } - template - void operator *= (const V& f) - { - x*=f; y*=f; z*=f; w*=f; - } - template - Vec4 operator / (const V& f) const - { - return MakeVec(x/f,y/f,z/f,w/f); - } - template - void operator /= (const V& f) - { - *this = *this / f; - } - - T Length2() const - { - return x*x + y*y + z*z + w*w; - } - - // Only implemented for T=float - float Length() const; - void SetLength(const float l); - Vec4 WithLength(const float l) const; - float Distance2To(Vec4 &other); - Vec4 Normalized() const; - float Normalize(); // returns the previous length, which is often useful - - T& operator [] (int i) //allow vector[2] = 3 (vector.z=3) - { - return *((&x) + i); - } - T operator [] (const int i) const - { - return *((&x) + i); - } - - void SetZero() - { - x=0; y=0; z=0; - } - - // Common alias: RGBA (colors) - T& r() { return x; } - T& g() { return y; } - T& b() { return z; } - T& a() { return w; } - - const T& r() const { return x; } - const T& g() const { return y; } - const T& b() const { return z; } - const T& a() const { return w; } - - // Swizzlers - Create a subvector of specific components - // e.g. Vec2 uv() { return Vec2(x,y); } - - // _DEFINE_SWIZZLER2 defines a single such function - // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r) - // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and permutations (xy<->yx) -#define _DEFINE_SWIZZLER2(a, b, name) const Vec2 name() const { return Vec2(a, b); } -#define DEFINE_SWIZZLER2_COMP1(a, a2) \ - _DEFINE_SWIZZLER2(a, a, a##a); \ - _DEFINE_SWIZZLER2(a, a, a2##a2) -#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \ - _DEFINE_SWIZZLER2(a, b, a##b); \ - _DEFINE_SWIZZLER2(a, b, a2##b2); \ - _DEFINE_SWIZZLER2(b, a, b##a); \ - _DEFINE_SWIZZLER2(b, a, b2##a2) - - DEFINE_SWIZZLER2_COMP2(x, y, r, g); - DEFINE_SWIZZLER2_COMP2(x, z, r, b); - DEFINE_SWIZZLER2_COMP2(x, w, r, a); - DEFINE_SWIZZLER2_COMP2(y, z, g, b); - DEFINE_SWIZZLER2_COMP2(y, w, g, a); - DEFINE_SWIZZLER2_COMP2(z, w, b, a); - DEFINE_SWIZZLER2_COMP1(x, r); - DEFINE_SWIZZLER2_COMP1(y, g); - DEFINE_SWIZZLER2_COMP1(z, b); - DEFINE_SWIZZLER2_COMP1(w, a); -#undef DEFINE_SWIZZLER2_COMP1 -#undef DEFINE_SWIZZLER2_COMP2 -#undef _DEFINE_SWIZZLER2 - -#define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3 name() const { return Vec3(a, b, c); } -#define DEFINE_SWIZZLER3_COMP1(a, a2) \ - _DEFINE_SWIZZLER3(a, a, a, a##a##a); \ - _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2) -#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \ - _DEFINE_SWIZZLER3(a, b, c, a##b##c); \ - _DEFINE_SWIZZLER3(a, c, b, a##c##b); \ - _DEFINE_SWIZZLER3(b, a, c, b##a##c); \ - _DEFINE_SWIZZLER3(b, c, a, b##c##a); \ - _DEFINE_SWIZZLER3(c, a, b, c##a##b); \ - _DEFINE_SWIZZLER3(c, b, a, c##b##a); \ - _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \ - _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \ - _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \ - _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \ - _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \ - _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2) - - DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b); - DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a); - DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a); - DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a); - DEFINE_SWIZZLER3_COMP1(x, r); - DEFINE_SWIZZLER3_COMP1(y, g); - DEFINE_SWIZZLER3_COMP1(z, b); - DEFINE_SWIZZLER3_COMP1(w, a); -#undef DEFINE_SWIZZLER3_COMP1 -#undef DEFINE_SWIZZLER3_COMP3 -#undef _DEFINE_SWIZZLER3 -}; - - -template -Vec4 operator * (const V& f, const Vec4& vec) -{ - return MakeVec(f*vec.x,f*vec.y,f*vec.z,f*vec.w); -} - -typedef Vec4 Vec4f; - - -template -static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec2& a, const Vec2& b) -{ - return a.x*b.x + a.y*b.y; -} - -template -static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec3& a, const Vec3& b) -{ - return a.x*b.x + a.y*b.y + a.z*b.z; -} - -template -static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec4& a, const Vec4& b) -{ - return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; -} - -template -static inline Vec3 Cross(const Vec3& a, const Vec3& b) -{ - return MakeVec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); -} - -// linear interpolation via float: 0.0=begin, 1.0=end -template -static inline decltype(X{}*float{}+X{}*float{}) Lerp(const X& begin, const X& end, const float t) -{ - return begin*(1.f-t) + end*t; -} - -// linear interpolation via int: 0=begin, base=end -template -static inline decltype((X{}*int{}+X{}*int{}) / base) LerpInt(const X& begin, const X& end, const int t) -{ - return (begin*(base-t) + end*t) / base; -} - -// Utility vector factories -template -static inline Vec2 MakeVec(const T& x, const T& y) -{ - return Vec2{x, y}; -} - -template -static inline Vec3 MakeVec(const T& x, const T& y, const T& z) -{ - return Vec3{x, y, z}; -} - -template -static inline Vec4 MakeVec(const T& x, const T& y, const Vec2& zw) -{ - return MakeVec(x, y, zw[0], zw[1]); -} - -template -static inline Vec3 MakeVec(const Vec2& xy, const T& z) -{ - return MakeVec(xy[0], xy[1], z); -} - -template -static inline Vec3 MakeVec(const T& x, const Vec2& yz) -{ - return MakeVec(x, yz[0], yz[1]); -} - -template -static inline Vec4 MakeVec(const T& x, const T& y, const T& z, const T& w) -{ - return Vec4{x, y, z, w}; -} - -template -static inline Vec4 MakeVec(const Vec2& xy, const T& z, const T& w) -{ - return MakeVec(xy[0], xy[1], z, w); -} - -template -static inline Vec4 MakeVec(const T& x, const Vec2& yz, const T& w) -{ - return MakeVec(x, yz[0], yz[1], w); -} - -// NOTE: This has priority over "Vec2> MakeVec(const Vec2& x, const Vec2& y)". -// Even if someone wanted to use an odd object like Vec2>, the compiler would error -// out soon enough due to misuse of the returned structure. -template -static inline Vec4 MakeVec(const Vec2& xy, const Vec2& zw) -{ - return MakeVec(xy[0], xy[1], zw[0], zw[1]); -} - -template -static inline Vec4 MakeVec(const Vec3& xyz, const T& w) -{ - return MakeVec(xyz[0], xyz[1], xyz[2], w); -} - -template -static inline Vec4 MakeVec(const T& x, const Vec3& yzw) -{ - return MakeVec(x, yzw[0], yzw[1], yzw[2]); -} - - -} // namespace diff --git a/src/video_core/pica.h b/src/video_core/pica.h index 6ebeb08f7..c556def03 100644 --- a/src/video_core/pica.h +++ b/src/video_core/pica.h @@ -15,8 +15,7 @@ #include "common/common_funcs.h" #include "common/common_types.h" #include "common/logging/log.h" - -#include "math.h" +#include "common/vector_math.h" namespace Pica { diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp index 6f88a8b21..2e4110a88 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp @@ -4,13 +4,13 @@ #include "common/make_unique.h" #include "common/math_util.h" +#include "common/vector_math.h" #include "core/memory.h" #include "video_core/renderer_opengl/gl_rasterizer_cache.h" #include "video_core/renderer_opengl/pica_to_gl.h" #include "video_core/debug_utils/debug_utils.h" -#include "video_core/math.h" RasterizerCacheOpenGL::~RasterizerCacheOpenGL() { FullFlush(); -- cgit v1.2.3 From 76690392bf8923d7172936836d15e3caebb26cf0 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 24 May 2015 12:20:31 -0700 Subject: Move video_core/color.h to common/color.h --- src/citra_qt/debugger/graphics_framebuffer.cpp | 3 +- src/common/CMakeLists.txt | 1 + src/common/color.h | 214 +++++++++++++++++++++++ src/core/hw/gpu.cpp | 2 +- src/video_core/CMakeLists.txt | 1 - src/video_core/color.h | 214 ----------------------- src/video_core/debug_utils/debug_utils.cpp | 2 +- src/video_core/rasterizer.cpp | 2 +- src/video_core/renderer_opengl/gl_rasterizer.cpp | 3 +- 9 files changed, 222 insertions(+), 220 deletions(-) create mode 100644 src/common/color.h delete mode 100644 src/video_core/color.h diff --git a/src/citra_qt/debugger/graphics_framebuffer.cpp b/src/citra_qt/debugger/graphics_framebuffer.cpp index e07344591..6bbe7572c 100644 --- a/src/citra_qt/debugger/graphics_framebuffer.cpp +++ b/src/citra_qt/debugger/graphics_framebuffer.cpp @@ -9,10 +9,11 @@ #include #include +#include "common/color.h" + #include "core/hw/gpu.h" #include "core/memory.h" -#include "video_core/color.h" #include "video_core/pica.h" #include "video_core/utils.h" diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d1c306d3a..e78f4f144 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -24,6 +24,7 @@ set(HEADERS bit_field.h break_points.h chunk_file.h + color.h common_funcs.h common_paths.h common_types.h diff --git a/src/common/color.h b/src/common/color.h new file mode 100644 index 000000000..422fdc8af --- /dev/null +++ b/src/common/color.h @@ -0,0 +1,214 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" +#include "common/swap.h" +#include "common/vector_math.h" + +namespace Color { + +/// Convert a 1-bit color component to 8 bit +inline u8 Convert1To8(u8 value) { + return value * 255; +} + +/// Convert a 4-bit color component to 8 bit +inline u8 Convert4To8(u8 value) { + return (value << 4) | value; +} + +/// Convert a 5-bit color component to 8 bit +inline u8 Convert5To8(u8 value) { + return (value << 3) | (value >> 2); +} + +/// Convert a 6-bit color component to 8 bit +inline u8 Convert6To8(u8 value) { + return (value << 2) | (value >> 4); +} + +/// Convert a 8-bit color component to 1 bit +inline u8 Convert8To1(u8 value) { + return value >> 7; +} + +/// Convert a 8-bit color component to 4 bit +inline u8 Convert8To4(u8 value) { + return value >> 4; +} + +/// Convert a 8-bit color component to 5 bit +inline u8 Convert8To5(u8 value) { + return value >> 3; +} + +/// Convert a 8-bit color component to 6 bit +inline u8 Convert8To6(u8 value) { + return value >> 2; +} + +/** + * Decode a color stored in RGBA8 format + * @param bytes Pointer to encoded source color + * @return Result color decoded as Math::Vec4 + */ +inline const Math::Vec4 DecodeRGBA8(const u8* bytes) { + return { bytes[3], bytes[2], bytes[1], bytes[0] }; +} + +/** + * Decode a color stored in RGB8 format + * @param bytes Pointer to encoded source color + * @return Result color decoded as Math::Vec4 + */ +inline const Math::Vec4 DecodeRGB8(const u8* bytes) { + return { bytes[2], bytes[1], bytes[0], 255 }; +} + +/** + * Decode a color stored in RGB565 format + * @param bytes Pointer to encoded source color + * @return Result color decoded as Math::Vec4 + */ +inline const Math::Vec4 DecodeRGB565(const u8* bytes) { + const u16_le pixel = *reinterpret_cast(bytes); + return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), + Convert5To8(pixel & 0x1F), 255 }; +} + +/** + * Decode a color stored in RGB5A1 format + * @param bytes Pointer to encoded source color + * @return Result color decoded as Math::Vec4 + */ +inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { + const u16_le pixel = *reinterpret_cast(bytes); + return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), + Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) }; +} + +/** + * Decode a color stored in RGBA4 format + * @param bytes Pointer to encoded source color + * @return Result color decoded as Math::Vec4 + */ +inline const Math::Vec4 DecodeRGBA4(const u8* bytes) { + const u16_le pixel = *reinterpret_cast(bytes); + return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), + Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) }; +} + +/** + * Decode a depth value stored in D16 format + * @param bytes Pointer to encoded source value + * @return Depth value as an u32 + */ +inline u32 DecodeD16(const u8* bytes) { + return *reinterpret_cast(bytes); +} + +/** + * Decode a depth value stored in D24 format + * @param bytes Pointer to encoded source value + * @return Depth value as an u32 + */ +inline u32 DecodeD24(const u8* bytes) { + return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; +} + +/** + * Decode a depth value and a stencil value stored in D24S8 format + * @param bytes Pointer to encoded source values + * @return Resulting values stored as a Math::Vec2 + */ +inline const Math::Vec2 DecodeD24S8(const u8* bytes) { + return { static_cast((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3] }; +} + +/** + * Encode a color as RGBA8 format + * @param color Source color to encode + * @param bytes Destination pointer to store encoded color + */ +inline void EncodeRGBA8(const Math::Vec4& color, u8* bytes) { + bytes[3] = color.r(); + bytes[2] = color.g(); + bytes[1] = color.b(); + bytes[0] = color.a(); +} + +/** + * Encode a color as RGB8 format + * @param color Source color to encode + * @param bytes Destination pointer to store encoded color + */ +inline void EncodeRGB8(const Math::Vec4& color, u8* bytes) { + bytes[2] = color.r(); + bytes[1] = color.g(); + bytes[0] = color.b(); +} + +/** + * Encode a color as RGB565 format + * @param color Source color to encode + * @param bytes Destination pointer to store encoded color + */ +inline void EncodeRGB565(const Math::Vec4& color, u8* bytes) { + *reinterpret_cast(bytes) = (Convert8To5(color.r()) << 11) | + (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); +} + +/** + * Encode a color as RGB5A1 format + * @param color Source color to encode + * @param bytes Destination pointer to store encoded color + */ +inline void EncodeRGB5A1(const Math::Vec4& color, u8* bytes) { + *reinterpret_cast(bytes) = (Convert8To5(color.r()) << 11) | + (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); +} + +/** + * Encode a color as RGBA4 format + * @param color Source color to encode + * @param bytes Destination pointer to store encoded color + */ +inline void EncodeRGBA4(const Math::Vec4& color, u8* bytes) { + *reinterpret_cast(bytes) = (Convert8To4(color.r()) << 12) | + (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); +} + +/** + * Encode a 16 bit depth value as D16 format + * @param value 16 bit source depth value to encode + * @param bytes Pointer where to store the encoded value + */ +inline void EncodeD16(u32 value, u8* bytes) { + *reinterpret_cast(bytes) = value & 0xFFFF; +} + +/** + * Encode a 24 bit depth value as D24 format + * @param value 24 bit source depth value to encode + * @param bytes Pointer where to store the encoded value + */ +inline void EncodeD24(u32 value, u8* bytes) { + bytes[0] = value & 0xFF; + bytes[1] = (value >> 8) & 0xFF; + bytes[2] = (value >> 16) & 0xFF; +} + +/** + * Encode a 24 bit depth and 8 bit stencil values as D24S8 format + * @param depth 24 bit source depth value to encode + * @param stencil 8 bit source stencil value to encode + * @param bytes Pointer where to store the encoded value + */ +inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { + *reinterpret_cast(bytes) = (stencil << 24) | depth; +} + +} // namespace diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp index ed607646e..fe93b369e 100644 --- a/src/core/hw/gpu.cpp +++ b/src/core/hw/gpu.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/color.h" #include "common/common_types.h" #include "core/arm/arm_interface.h" @@ -22,7 +23,6 @@ #include "video_core/command_processor.h" #include "video_core/utils.h" #include "video_core/video_core.h" -#include "video_core/color.h" namespace GPU { diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index f50f73b52..5c7f4ae18 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -29,7 +29,6 @@ set(HEADERS renderer_opengl/pica_to_gl.h renderer_opengl/renderer_opengl.h clipper.h - color.h command_processor.h gpu_debugger.h hwrasterizer_base.h diff --git a/src/video_core/color.h b/src/video_core/color.h deleted file mode 100644 index 422fdc8af..000000000 --- a/src/video_core/color.h +++ /dev/null @@ -1,214 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "common/common_types.h" -#include "common/swap.h" -#include "common/vector_math.h" - -namespace Color { - -/// Convert a 1-bit color component to 8 bit -inline u8 Convert1To8(u8 value) { - return value * 255; -} - -/// Convert a 4-bit color component to 8 bit -inline u8 Convert4To8(u8 value) { - return (value << 4) | value; -} - -/// Convert a 5-bit color component to 8 bit -inline u8 Convert5To8(u8 value) { - return (value << 3) | (value >> 2); -} - -/// Convert a 6-bit color component to 8 bit -inline u8 Convert6To8(u8 value) { - return (value << 2) | (value >> 4); -} - -/// Convert a 8-bit color component to 1 bit -inline u8 Convert8To1(u8 value) { - return value >> 7; -} - -/// Convert a 8-bit color component to 4 bit -inline u8 Convert8To4(u8 value) { - return value >> 4; -} - -/// Convert a 8-bit color component to 5 bit -inline u8 Convert8To5(u8 value) { - return value >> 3; -} - -/// Convert a 8-bit color component to 6 bit -inline u8 Convert8To6(u8 value) { - return value >> 2; -} - -/** - * Decode a color stored in RGBA8 format - * @param bytes Pointer to encoded source color - * @return Result color decoded as Math::Vec4 - */ -inline const Math::Vec4 DecodeRGBA8(const u8* bytes) { - return { bytes[3], bytes[2], bytes[1], bytes[0] }; -} - -/** - * Decode a color stored in RGB8 format - * @param bytes Pointer to encoded source color - * @return Result color decoded as Math::Vec4 - */ -inline const Math::Vec4 DecodeRGB8(const u8* bytes) { - return { bytes[2], bytes[1], bytes[0], 255 }; -} - -/** - * Decode a color stored in RGB565 format - * @param bytes Pointer to encoded source color - * @return Result color decoded as Math::Vec4 - */ -inline const Math::Vec4 DecodeRGB565(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); - return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F), - Convert5To8(pixel & 0x1F), 255 }; -} - -/** - * Decode a color stored in RGB5A1 format - * @param bytes Pointer to encoded source color - * @return Result color decoded as Math::Vec4 - */ -inline const Math::Vec4 DecodeRGB5A1(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); - return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F), - Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) }; -} - -/** - * Decode a color stored in RGBA4 format - * @param bytes Pointer to encoded source color - * @return Result color decoded as Math::Vec4 - */ -inline const Math::Vec4 DecodeRGBA4(const u8* bytes) { - const u16_le pixel = *reinterpret_cast(bytes); - return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF), - Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) }; -} - -/** - * Decode a depth value stored in D16 format - * @param bytes Pointer to encoded source value - * @return Depth value as an u32 - */ -inline u32 DecodeD16(const u8* bytes) { - return *reinterpret_cast(bytes); -} - -/** - * Decode a depth value stored in D24 format - * @param bytes Pointer to encoded source value - * @return Depth value as an u32 - */ -inline u32 DecodeD24(const u8* bytes) { - return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; -} - -/** - * Decode a depth value and a stencil value stored in D24S8 format - * @param bytes Pointer to encoded source values - * @return Resulting values stored as a Math::Vec2 - */ -inline const Math::Vec2 DecodeD24S8(const u8* bytes) { - return { static_cast((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3] }; -} - -/** - * Encode a color as RGBA8 format - * @param color Source color to encode - * @param bytes Destination pointer to store encoded color - */ -inline void EncodeRGBA8(const Math::Vec4& color, u8* bytes) { - bytes[3] = color.r(); - bytes[2] = color.g(); - bytes[1] = color.b(); - bytes[0] = color.a(); -} - -/** - * Encode a color as RGB8 format - * @param color Source color to encode - * @param bytes Destination pointer to store encoded color - */ -inline void EncodeRGB8(const Math::Vec4& color, u8* bytes) { - bytes[2] = color.r(); - bytes[1] = color.g(); - bytes[0] = color.b(); -} - -/** - * Encode a color as RGB565 format - * @param color Source color to encode - * @param bytes Destination pointer to store encoded color - */ -inline void EncodeRGB565(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = (Convert8To5(color.r()) << 11) | - (Convert8To6(color.g()) << 5) | Convert8To5(color.b()); -} - -/** - * Encode a color as RGB5A1 format - * @param color Source color to encode - * @param bytes Destination pointer to store encoded color - */ -inline void EncodeRGB5A1(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = (Convert8To5(color.r()) << 11) | - (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a()); -} - -/** - * Encode a color as RGBA4 format - * @param color Source color to encode - * @param bytes Destination pointer to store encoded color - */ -inline void EncodeRGBA4(const Math::Vec4& color, u8* bytes) { - *reinterpret_cast(bytes) = (Convert8To4(color.r()) << 12) | - (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a()); -} - -/** - * Encode a 16 bit depth value as D16 format - * @param value 16 bit source depth value to encode - * @param bytes Pointer where to store the encoded value - */ -inline void EncodeD16(u32 value, u8* bytes) { - *reinterpret_cast(bytes) = value & 0xFFFF; -} - -/** - * Encode a 24 bit depth value as D24 format - * @param value 24 bit source depth value to encode - * @param bytes Pointer where to store the encoded value - */ -inline void EncodeD24(u32 value, u8* bytes) { - bytes[0] = value & 0xFF; - bytes[1] = (value >> 8) & 0xFF; - bytes[2] = (value >> 16) & 0xFF; -} - -/** - * Encode a 24 bit depth and 8 bit stencil values as D24S8 format - * @param depth 24 bit source depth value to encode - * @param stencil 8 bit source stencil value to encode - * @param bytes Pointer where to store the encoded value - */ -inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) { - *reinterpret_cast(bytes) = (stencil << 24) | depth; -} - -} // namespace diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp index 5392dae21..7b8ab72b6 100644 --- a/src/video_core/debug_utils/debug_utils.cpp +++ b/src/video_core/debug_utils/debug_utils.cpp @@ -17,11 +17,11 @@ #include #include "common/assert.h" +#include "common/color.h" #include "common/file_util.h" #include "common/math_util.h" #include "common/vector_math.h" -#include "video_core/color.h" #include "video_core/pica.h" #include "video_core/utils.h" #include "video_core/video_core.h" diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp index 6df3a74f2..93288be2c 100644 --- a/src/video_core/rasterizer.cpp +++ b/src/video_core/rasterizer.cpp @@ -4,6 +4,7 @@ #include +#include "common/color.h" #include "common/common_types.h" #include "common/math_util.h" #include "common/profiler.h" @@ -13,7 +14,6 @@ #include "debug_utils/debug_utils.h" #include "math.h" -#include "color.h" #include "pica.h" #include "rasterizer.h" #include "vertex_shader.h" diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index bacdb7172..e33a712c6 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -2,10 +2,11 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/color.h" + #include "core/settings.h" #include "core/hw/gpu.h" -#include "video_core/color.h" #include "video_core/pica.h" #include "video_core/utils.h" #include "video_core/renderer_opengl/gl_rasterizer.h" -- cgit v1.2.3