Skip to content

Commit

Permalink
Adding concepts to psyqo's Fragments and Primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Dec 13, 2023
1 parent 0b29cd8 commit b9c2576
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 27 deletions.
49 changes: 49 additions & 0 deletions src/mips/psyqo/fragment-concept.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
MIT License
Copyright (c) 2023 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

#include <concepts>
#include <cstddef>

namespace psyqo {

/**
* @brief The Fragment concept.
* @details This concept can be used as a template type constraint
* to ensure that a type is a valid fragment.
*/

template<typename Frag>
concept Fragment = requires(Frag frag) {
{ new int[(alignof(Frag) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(Frag) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(frag.head)) == 4 ? 1 : -1] };
{ new int[((offsetof(Frag, head)) & 3) == 0 ? 1 : -1] };
{ frag.getActualFragmentSize() } -> std::convertible_to<size_t>;
};

} // namespace psyqo
32 changes: 17 additions & 15 deletions src/mips/psyqo/fragments.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ SOFTWARE.
#include <EASTL/array.h>
#include <stdint.h>

#include "psyqo/primitive-concept.hh"

namespace psyqo {

/**
Expand Down Expand Up @@ -60,17 +62,17 @@ namespace Fragments {
* @tparam T The primitive type.
*/

template <typename T>
template <Primitive Prim>
struct SimpleFragment {
constexpr size_t maxSize() const { return 1; }
SimpleFragment() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(T)),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(Prim)),
"Spurious padding in simple fragment");
}
typedef T FragmentBaseType;
constexpr size_t getActualFragmentSize() const { return sizeof(T) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
constexpr size_t getActualFragmentSize() const { return sizeof(Prim) / sizeof(uint32_t); }
uint32_t head;
T primitive;
Prim primitive;
};

/**
Expand All @@ -84,18 +86,18 @@ struct SimpleFragment {
* @tparam N The maximum number of primitives in the payload.
*/

template <typename T, size_t N>
template <Primitive Prim, size_t N>
struct FixedFragment {
constexpr size_t maxSize() const { return N; }
FixedFragment() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(T) * N),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
typedef T FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(T) * count) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
uint32_t head;
eastl::array<T, N> primitives;
eastl::array<Prim, N> primitives;
};

/**
Expand All @@ -112,19 +114,19 @@ struct FixedFragment {
* @tparam N The maximum number of primitives in the payload.
*/

template <typename P, typename T, size_t N>
template <Primitive P, Primitive Prim, size_t N>
struct FixedFragmentWithPrologue {
constexpr size_t maxSize() const { return N; }
FixedFragmentWithPrologue() {
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(P) + sizeof(T) * N),
static_assert(sizeof(*this) == (sizeof(unsigned) + sizeof(uint32_t) + sizeof(P) + sizeof(Prim) * N),
"Spurious padding in fixed fragment");
}
typedef T FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(P) + sizeof(T) * count) / sizeof(uint32_t); }
typedef Prim FragmentBaseType;
size_t getActualFragmentSize() const { return (sizeof(P) + sizeof(Prim) * count) / sizeof(uint32_t); }
unsigned count = N;
uint32_t head;
P prologue;
eastl::array<T, N> primitives;
eastl::array<Prim, N> primitives;
};

} // namespace Fragments
Expand Down
27 changes: 15 additions & 12 deletions src/mips/psyqo/gpu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ SOFTWARE.
#include <EASTL/utility.h>
#include <stdint.h>

#include "psyqo/fragment-concept.hh"
#include "psyqo/hardware/gpu.hh"
#include "psyqo/primitives.hh"
#include "psyqo/primitive-concept.hh"
#include "psyqo/primitives/common.hh"
#include "psyqo/primitives/control.hh"
#include "psyqo/primitives/misc.hh"

namespace psyqo {

Expand Down Expand Up @@ -179,8 +183,8 @@ class GPU {
*
* @param fragment The fragment to send to the GPU.
*/
template <typename Fragment>
void sendFragment(const Fragment &fragment) {
template <Fragment Frag>
void sendFragment(const Frag &fragment) {
sendFragment(&fragment.head + 1, fragment.getActualFragmentSize());
}

Expand All @@ -193,8 +197,8 @@ class GPU {
* @param callback The callback to call upon completion.
* @param dmaCallback `DMA::FROM_MAIN_LOOP` or `DMA::FROM_ISR`.
*/
template <typename Fragment>
void sendFragment(const Fragment &fragment, eastl::function<void()> &&callback,
template <Fragment Frag>
void sendFragment(const Frag &fragment, eastl::function<void()> &&callback,
DMA::DmaCallback dmaCallback = DMA::FROM_MAIN_LOOP) {
sendFragment(&fragment.head + 1, fragment.getActualFragmentSize(), eastl::move(callback), dmaCallback);
}
Expand Down Expand Up @@ -252,14 +256,13 @@ class GPU {
* @details This method will immediately send the specified primitive to the GPU.
* @param primitive The primitive to send to the GPU.
*/
template <typename Primitive>
static void sendPrimitive(const Primitive &primitive) {
static_assert((sizeof(Primitive) % 4) == 0, "Primitive's size must be a multiple of 4");
template <Primitive Prim>
static void sendPrimitive(const Prim &primitive) {
waitReady();
const uint32_t *ptr = reinterpret_cast<const uint32_t *>(&primitive);
size_t size = sizeof(Primitive) / sizeof(uint32_t);
constexpr size_t size = sizeof(Prim) / sizeof(uint32_t);
for (int i = 0; i < size; i++) {
if constexpr (sizeof(Primitive) > 56) waitFifo();
if constexpr (sizeof(Prim) > 56) waitFifo();
sendRaw(*ptr++);
}
}
Expand All @@ -277,8 +280,8 @@ class GPU {
* if applicable.
* @param fragment The fragment to chain.
*/
template <typename Fragment>
void chain(Fragment &fragment) {
template <Fragment Frag>
void chain(Frag &fragment) {
chain(&fragment.head, fragment.getActualFragmentSize());
}

Expand Down
46 changes: 46 additions & 0 deletions src/mips/psyqo/primitive-concept.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
MIT License
Copyright (c) 2023 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

#include <concepts>
#include <cstddef>

namespace psyqo {

/**
* @brief The Primitive concept.
* @details This concept can be used as a template type constraint
* to ensure that a type is a valid primitive.
*/

template <typename Prim>
concept Primitive = requires {
{ new int[(alignof(Prim) & 3) == 0 ? 1 : -1] };
{ new int[(sizeof(Prim) & 3) == 0 ? 1 : -1] };
};

} // namespace psyqo
6 changes: 6 additions & 0 deletions src/mips/psyqo/primitives.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ SOFTWARE.

#pragma once

/**
* @file primitives.hh
* @brief This file is a convenience header that includes all the primitive
* headers.
*/

#include "psyqo/primitives/control.hh"
#include "psyqo/primitives/lines.hh"
#include "psyqo/primitives/misc.hh"
Expand Down

0 comments on commit b9c2576

Please sign in to comment.