Upload all my tests

- Add Count<> to AssetManager
- Initialize pCode in Shader
- Rename linearAllocator to LinearAllocator
- Add Construct and destroy to linear allocator
- Add Shader unloader (fixes atexit crash i guess)
-
This commit is contained in:
2025-11-30 21:57:01 +01:00
parent febf506a5e
commit 078af99ae5
16 changed files with 389 additions and 151 deletions

BIN
.clang-format Normal file

Binary file not shown.

BIN
example/romfs/white.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 B

View File

@@ -16,8 +16,7 @@
.alias in_uvc v1 .alias in_uvc v1
.alias in_col v2 .alias in_col v2
.entry vmain .proc main
.proc vmain
mov r0.xy, in_xy.xy mov r0.xy, in_xy.xy
mov r0.w, ones mov r0.w, ones

View File

@@ -1,118 +1,26 @@
#include <amethyst.hpp> #include <amethyst.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include "amethyst/iron.hpp" class Example : public Amy::App {
struct memory_metrics {
uint64_t t_TotalAllocated = 0; ///< Total Allocated Memory
uint64_t t_TotalFreed = 0; ///< Total Deleted Memory
/// @brief Gets the Currently Allocated Memory
uint32_t t_CurrentlyAllocated() { return t_TotalAllocated - t_TotalFreed; }
};
static memory_metrics metrics;
bool rd7i_enable_memtrack = true;
void* operator new(size_t size) {
void* ptr = malloc(size);
if (rd7i_enable_memtrack) metrics.t_TotalAllocated += size;
return ptr;
}
void operator delete(void* memory, size_t size) {
if (rd7i_enable_memtrack) metrics.t_TotalFreed += size;
free(memory);
}
size_t GetTotalAllocated() { return metrics.t_TotalAllocated; }
size_t GetTotalFreed() { return metrics.t_TotalFreed; }
size_t GetCurrent() { return metrics.t_CurrentlyAllocated(); }
std::string FormatBytes(unsigned long long bytes) {
static const std::vector<std::string> endings = {
"B", "KB", "MB", "GB", "TB", "Unk",
};
int i = 0;
double b = bytes;
while (b > 1024) {
i++;
b /= 1024;
}
if (i >= (int)endings.size()) {
i = (int)endings.size() - 1;
}
return std::format("{:.1f} {}", b, endings[i]);
}
class example : public Amy::App {
public: public:
example() { Example() {
Amy::Ctr::Init(); Amy::Ctr::Init();
consoleInit(GFX_BOTTOM, NULL);
Amy::C3D::Init(); Amy::C3D::Init();
m_top = Amy::C3D::CreateScreen(GFX_TOP); Top = Amy::C3D::CreateScreen(GFX_TOP);
Amy::Iron::Init(); }
dl = new Amy::Iron::Drawlist(); ~Example() { delete Top; }
dl->DrawSolid();
mgr.AutoLoad("shader", "romfs:/shaders/shader2d.shbin");
mgr.Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 3);
mgr.Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 3);
mgr.AutoLoad("icon", "romfs:/icon.png");
};
~example() {
Amy::C3D::DeleteScreen(m_top);
Amy::C3D::Deinit();
};
void Main() override {
std::cout << std::format("\x1b[1;1HDelta: {:.3f} -> {:.3} FPS\x1b[K", this->Delta(), 1000.0/this->Delta());
std::cout << std::format("\x1b[2;1HTime: {:.3f}\x1b[K", this->Time());
std::cout << std::format(
"\x1b[3;1HMem: {}\n +{}\n -{}\nLin: {}\x1b[K",
FormatBytes(GetCurrent()), FormatBytes(GetTotalAllocated()),
FormatBytes(GetTotalFreed()), FormatBytes(linearSpaceFree()));
void Main() {
Amy::C3D::StartFrame(); Amy::C3D::StartFrame();
m_top->Use(); Top->Clear();
m_top->Clear(); Top->Use();
dl->DrawTex(mgr.Get<Amy::Texture>("icon"));
dl->DrawRectFilled(100, 48, 0xffffffff);
dl->DrawSolid();
dl->DrawRectFilled(0, 50, 0xffffffff);
Amy::Iron::NewFrame();
Amy::Iron::DrawOn(m_top);
Amy::Iron::Draw(*dl);
dl->Clear();
mgr.Get<Amy::C3D::Shader>("shader")->Use();
mgr.Get<Amy::C3D::Shader>("shader")->SetMat4(0, mtx);
C3D_ImmDrawBegin(GPU_TRIANGLES);
C3D_ImmSendAttrib(200, 50, 0, 0);
C3D_ImmSendAttrib(1, 0, 0, 1);
C3D_ImmSendAttrib(300, 190, 0, 0);
C3D_ImmSendAttrib(0, 1, 0, 1);
C3D_ImmSendAttrib(100, 190, 0, 0);
C3D_ImmSendAttrib(0, 0, 1, 1);
C3D_ImmDrawEnd();
Amy::C3D::EndFrame(); Amy::C3D::EndFrame();
} }
private: Amy::C3D::Screen *Top;
Amy::C3D::Screen* m_top = nullptr;
Amy::Iron::Drawlist* dl = nullptr;
Amy::C3D::Shader* test = nullptr;
Amy::AssetMgr mgr;
Amy::mat4 mtx =
Amy::mat4::identity() * Amy::mat4::ortho(0, 400, 240, 0, 0, 1);
}; };
int main() { int main() {
Amy::RegisterCxxExceptionHandler(); Example App;
example app; App.Run();
app.Run();
return 0; return 0;
} }

118
example/source/main.cpp.2 Normal file
View File

@@ -0,0 +1,118 @@
#include <amethyst.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include "amethyst/iron.hpp"
struct memory_metrics {
uint64_t t_TotalAllocated = 0; ///< Total Allocated Memory
uint64_t t_TotalFreed = 0; ///< Total Deleted Memory
/// @brief Gets the Currently Allocated Memory
uint32_t t_CurrentlyAllocated() { return t_TotalAllocated - t_TotalFreed; }
};
static memory_metrics metrics;
bool rd7i_enable_memtrack = true;
void* operator new(size_t size) {
void* ptr = malloc(size);
if (rd7i_enable_memtrack) metrics.t_TotalAllocated += size;
return ptr;
}
void operator delete(void* memory, size_t size) {
if (rd7i_enable_memtrack) metrics.t_TotalFreed += size;
free(memory);
}
size_t GetTotalAllocated() { return metrics.t_TotalAllocated; }
size_t GetTotalFreed() { return metrics.t_TotalFreed; }
size_t GetCurrent() { return metrics.t_CurrentlyAllocated(); }
std::string FormatBytes(unsigned long long bytes) {
static const std::vector<std::string> endings = {
"B", "KB", "MB", "GB", "TB", "Unk",
};
int i = 0;
double b = bytes;
while (b > 1024) {
i++;
b /= 1024;
}
if (i >= (int)endings.size()) {
i = (int)endings.size() - 1;
}
return std::format("{:.1f} {}", b, endings[i]);
}
class example : public Amy::App {
public:
example() {
Amy::Ctr::Init();
consoleInit(GFX_BOTTOM, NULL);
Amy::C3D::Init();
m_top = Amy::C3D::CreateScreen(GFX_TOP);
Amy::Iron::Init();
dl = new Amy::Iron::Drawlist();
dl->DrawSolid();
mgr.AutoLoad("shader", "romfs:/shaders/shader2d.shbin");
mgr.Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 3);
mgr.Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 3);
mgr.AutoLoad("icon", "romfs:/icon.png");
};
~example() {
Amy::C3D::DeleteScreen(m_top);
Amy::C3D::Deinit();
};
void Main() override {
std::cout << std::format("\x1b[1;1HDelta: {:.3f} -> {:.3} FPS\x1b[K", this->Delta(), 1000.0/this->Delta());
std::cout << std::format("\x1b[2;1HTime: {:.3f}\x1b[K", this->Time());
std::cout << std::format(
"\x1b[3;1HMem: {}\n +{}\n -{}\nLin: {}\x1b[K",
FormatBytes(GetCurrent()), FormatBytes(GetTotalAllocated()),
FormatBytes(GetTotalFreed()), FormatBytes(linearSpaceFree()));
Amy::C3D::StartFrame();
m_top->Use();
m_top->Clear();
dl->DrawTex(mgr.Get<Amy::Texture>("icon"));
dl->DrawRectFilled(100, 48, 0xffffffff);
dl->DrawSolid();
dl->DrawRectFilled(0, 50, 0xffffffff);
Amy::Iron::NewFrame();
Amy::Iron::DrawOn(m_top);
Amy::Iron::Draw(*dl);
dl->Clear();
/*mgr.Get<Amy::C3D::Shader>("shader")->Use();
mgr.Get<Amy::C3D::Shader>("shader")->SetMat4(0, mtx);
C3D_ImmDrawBegin(GPU_TRIANGLES);
C3D_ImmSendAttrib(200, 50, 0, 0);
C3D_ImmSendAttrib(1, 0, 0, 1);
C3D_ImmSendAttrib(300, 190, 0, 0);
C3D_ImmSendAttrib(0, 1, 0, 1);
C3D_ImmSendAttrib(100, 190, 0, 0);
C3D_ImmSendAttrib(0, 0, 1, 1);
C3D_ImmDrawEnd();*/
Amy::C3D::EndFrame();
}
private:
Amy::C3D::Screen* m_top = nullptr;
Amy::Iron::Drawlist* dl = nullptr;
Amy::C3D::Shader* test = nullptr;
Amy::AssetMgr mgr;
Amy::mat4 mtx =
Amy::mat4::identity() * Amy::mat4::ortho(0, 400, 240, 0, 0, 1);
};
int main() {
Amy::RegisterCxxExceptionHandler();
example app;
app.Run();
return 0;
}

91
example/source/main.cpp.3 Normal file
View File

@@ -0,0 +1,91 @@
#include <3ds.h>
#include <citro3d.h>
#include <string.h>
#include <amethyst.hpp>
struct vertex {
vertex(Amy::fvec2 p, Amy::fvec2 tc, Amy::ui c) {
pos = pos;
uv = tc;
color = c;
}
vertex() = default;
Amy::fvec2 pos;
Amy::fvec2 uv;
Amy::ui color;
};
std::vector<vertex, Amy::LinearAllocator<vertex>> vbuf;
static int uLoc_projection = 0;
static Amy::C3D::Shader* Shader = nullptr;
static Amy::AssetMgr Assets;
static void sceneInit(void) {
Shader = new Amy::C3D::Shader("romfs:/shaders/li2.shbin");
uLoc_projection = Shader->loc("projection");
Shader->Input(GPU_FLOAT, 2);
Shader->Input(GPU_FLOAT, 2);
Shader->Input(GPU_UNSIGNED_BYTE, 4);
vbuf.resize(6);
vbuf[0] = vertex(Amy::fvec2(300, 40), Amy::fvec2(1, 1), 0xffffffff);
vbuf[1] = vertex(Amy::fvec2(100, 40), Amy::fvec2(0, 1), 0xffffffff);
vbuf[2] = vertex(Amy::fvec2(100, 200), Amy::fvec2(0, 0), 0xffffffff);
vbuf[3] = vertex(Amy::fvec2(100, 200), Amy::fvec2(0, 0), 0xffffffff);
vbuf[4] = vertex(Amy::fvec2(300, 200), Amy::fvec2(1, 0), 0xffffffff);
vbuf[5] = vertex(Amy::fvec2(300, 40), Amy::fvec2(1, 1), 0xffffffff);
Amy::C3D::BufCfg<3>(vbuf.data(), sizeof(vertex));
Amy::C3D::Frag::Edit();
Amy::C3D::Frag::Src(C3D_Both, GPU_TEXTURE0);
Amy::C3D::Frag::Func(C3D_Both, GPU_MODULATE);
Assets.AutoLoad("white", "romfs:/white.png");
}
static void sceneRender(void) {
Amy::C3D::DepthTest(false);
Shader->Use();
Shader->SetMat4(uLoc_projection, Amy::mat4::ortho(0, 400, 240, 0, 1, -1));
Assets.Get<Amy::Texture>("white")->Bind();
Amy::C3D::DrawArrays(0, vbuf.size());
Amy::C3D::DepthTest(true);
}
static void sceneExit(void) {
vbuf.clear();
delete Shader;
}
int main() {
Amy::RegisterCxxExceptionHandler();
Amy::Ctr::Init();
Amy::C3D::Init();
auto Top = Amy::C3D::CreateScreen(GFX_TOP);
sceneInit();
while (aptMainLoop()) {
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START) break;
Amy::C3D::StartFrame();
Top->Clear();
Top->Use();
sceneRender();
Amy::C3D::EndFrame();
}
// Deinitialize the scene
sceneExit();
Amy::C3D::Deinit();
gfxExit();
romfsExit();
return 0;
}

89
example/source/main.cpp44 Normal file
View File

@@ -0,0 +1,89 @@
#include <amethyst.hpp>
typedef struct {
float pos[3];
float color[4];
} Vertex;
static const Vertex vertex_list[] = {
{{200.0f, 200.0f, 0.5f}, {1.0f, 0.0f, 0.0f, 1.0f}},
{{100.0f, 40.0f, 0.5f}, {0.0f, 1.0f, 0.0f, 1.0f}},
{{300.0f, 40.0f, 0.5f}, {0.0f, 0.0f, 1.0f, 1.0f}},
};
#define vertex_list_count (sizeof(vertex_list) / sizeof(vertex_list[0]))
static int uLoc_projection;
Amy::AssetMgr* Assets = nullptr;
static void* vbo_data;
extern "C" {
static void sceneInit(void) {
Assets->AutoLoad("shader", "romfs:/shaders/shader2d.shbin");
Assets->Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 3);
Assets->Get<Amy::C3D::Shader>("shader")->Input(GPU_FLOAT, 4);
uLoc_projection = Assets->Get<Amy::C3D::Shader>("shader")->loc("projecttion");
vbo_data = linearAlloc(sizeof(vertex_list));
memcpy(vbo_data, vertex_list, sizeof(vertex_list));
// Configure buffers
C3D_BufInfo* bufInfo = C3D_GetBufInfo();
BufInfo_Init(bufInfo);
BufInfo_Add(bufInfo, vbo_data, sizeof(Vertex), 2, 0x10);
// Configure the first fragment shading substage to just pass through the
// vertex color See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml
// for more insight
C3D_TexEnv* env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR);
C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
}
}
static void sceneRender(void) {
Assets->Get<Amy::C3D::Shader>("shader")->Use();
Assets->Get<Amy::C3D::Shader>("shader")->SetMat4(
uLoc_projection, Amy::mat4::ortho(0, 400, 240, 0, 1, -1));
Amy::C3D::DrawArrays(0, vertex_list_count);
}
static void sceneExit(void) {
// Free the VBO
linearFree(vbo_data);
Assets->Remove("shader");
}
int main() {
Amy::RegisterCxxExceptionHandler();
Amy::Ctr::Init();
Amy::C3D::Init();
auto Top = Amy::C3D::CreateScreen(GFX_TOP);
Assets = new Amy::AssetMgr();
sceneInit();
while (aptMainLoop()) {
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START) break;
Amy::C3D::StartFrame();
Top->Clear();
Top->Use();
sceneRender();
Amy::C3D::EndFrame();
}
sceneExit();
C3D_Fini();
gfxExit();
romfsExit();
return 0;
}

View File

@@ -5,6 +5,7 @@
#include <amethyst/c3d.hpp> #include <amethyst/c3d.hpp>
#include <amethyst/ctru.hpp> #include <amethyst/ctru.hpp>
#include <amethyst/image.hpp> #include <amethyst/image.hpp>
#include <amethyst/iron.hpp>
#include <amethyst/renderer.hpp> #include <amethyst/renderer.hpp>
#include <amethyst/texture.hpp> #include <amethyst/texture.hpp>

View File

@@ -49,6 +49,17 @@ class AssetMgr {
return dynamic_cast<T*>(r->second) != nullptr; return dynamic_cast<T*>(r->second) != nullptr;
} }
template <typename T>
size_t Count() const {
size_t ret = 0;
for (auto& it : pAssets) {
if (dynamic_cast<T*>(it.second)) {
ret++;
}
}
return ret;
}
private: private:
std::map<ID, Asset*> pAssets; std::map<ID, Asset*> pAssets;
}; };

View File

@@ -56,7 +56,7 @@ class C3D {
// private: // private:
C3D_AttrInfo pInfo; C3D_AttrInfo pInfo;
shaderProgram_s pProgram; shaderProgram_s pProgram;
DVLB_s* pCode; DVLB_s* pCode = nullptr;
int pReg = 0; int pReg = 0;
}; };

View File

@@ -158,8 +158,8 @@ class Iron {
static void pInitSolidTex(); static void pInitSolidTex();
static bool pCheckSize(size_t idx, size_t vtx); static bool pCheckSize(size_t idx, size_t vtx);
static std::vector<Vertex, linearAllocator<Vertex>> m_vbuf; static std::vector<Vertex, LinearAllocator<Vertex>> m_vbuf;
static std::vector<u16, linearAllocator<u16>> m_ibuf; static std::vector<u16, LinearAllocator<u16>> m_ibuf;
static int uLocProj; static int uLocProj;
static C3D::Shader* m_shader; static C3D::Shader* m_shader;
static mat4 m_mtx; static mat4 m_mtx;

View File

@@ -11,25 +11,35 @@
namespace Amy { namespace Amy {
template <typename T> template <typename T>
class linearAllocator { class LinearAllocator {
public: public:
using value_type = T; using value_type = T;
linearAllocator() = default; LinearAllocator() noexcept = default;
template <typename U> template <typename U>
constexpr linearAllocator(const linearAllocator<U>&) noexcept {} constexpr LinearAllocator(const LinearAllocator<U>&) noexcept {}
T* allocate(std::size_t n) { T* allocate(std::size_t n) {
if (n > std::allocator_traits<linearAllocator>::max_size(*this)) { if (n > max_size()) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
return static_cast<T*>(linearAlloc(n * sizeof(T))); return static_cast<T*>(linearAlloc(n * sizeof(T)));
} }
void deallocate(T* p, std::size_t) noexcept { linearFree(p); } void deallocate(T* p, std::size_t) noexcept { linearFree(p); }
friend bool operator==(const linearAllocator, const linearAllocator) { template <class U, class... Args>
void construct(U* p, Args&&... args) {
::new ((void*)p) U(std::forward<Args>(args)...);
}
template <class U>
void destroy(U* p) {
p->~U();
}
friend bool operator==(const LinearAllocator, const LinearAllocator) {
return true; return true;
} }
friend bool operator!=(const linearAllocator, const linearAllocator) { friend bool operator!=(const LinearAllocator, const LinearAllocator) {
return false; return false;
} }

View File

@@ -40,7 +40,13 @@ void C3D::DeleteScreen(C3D::Screen* screen) { delete screen; }
C3D::Shader::Shader(const std::string& path) { Load(path); } C3D::Shader::Shader(const std::string& path) { Load(path); }
C3D::Shader::~Shader() {} C3D::Shader::~Shader() {
if (pCode) {
shaderProgramFree(&pProgram);
DVLB_Free(pCode);
pCode = nullptr;
}
}
void C3D::Shader::Load(const std::string& path) { void C3D::Shader::Load(const std::string& path) {
auto code = Utils::LoadFile2Mem(path); auto code = Utils::LoadFile2Mem(path);
@@ -66,7 +72,7 @@ void C3D::Shader::Compile(const std::string& code) {
} }
void C3D::Shader::Use() { void C3D::Shader::Use() {
C3D_BindProgram(&pProgram); // C3D_BindProgram(&pProgram);
// for some reason i need both ??? // for some reason i need both ???
// code works perfectly without C3D_BindProgram // code works perfectly without C3D_BindProgram
// but nor withour shaderProgramUse ... // but nor withour shaderProgramUse ...

View File

@@ -57,13 +57,14 @@ unsigned char li_shader[] = {
}; };
// clang-format on // clang-format on
size_t li_shader_size = 0x124; size_t li_shader_size = 0x124;
std::vector<Iron::Vertex, linearAllocator<Iron::Vertex>> Iron::m_vbuf; std::vector<Iron::Vertex, LinearAllocator<Iron::Vertex>> Iron::m_vbuf;
std::vector<u16, linearAllocator<u16>> Iron::m_ibuf; std::vector<u16, LinearAllocator<u16>> Iron::m_ibuf;
int Iron::uLocProj = 0; int Iron::uLocProj = 0;
C3D::Shader* Iron::m_shader = nullptr; C3D::Shader* Iron::m_shader = nullptr;
mat4 Iron::m_mtx; mat4 Iron::m_mtx;
int Iron::m_idx = 0, Iron::m_vtx = 0; int Iron::m_idx = 0, Iron::m_vtx = 0;
Texture* Iron::m_solid = nullptr; Texture* Iron::m_solid = nullptr;
C3D_Mtx __m;
void Iron::Init() { void Iron::Init() {
pSetupShader(); pSetupShader();
@@ -79,9 +80,13 @@ void Iron::NewFrame() {
void Iron::DrawOn(C3D::Screen* screen) { void Iron::DrawOn(C3D::Screen* screen) {
m_shader->Use(); m_shader->Use();
m_mtx = mat4::ortho(0.f, (float)screen->Width(), (float)screen->Height(), 0.f, Mtx_Identity(&__m);
1.f, -1.f); Mtx_OrthoTilt(&__m, 0, (float)screen->Width(), (float)screen->Height(), 0.f,
m_shader->SetMat4(uLocProj, m_mtx); 1.f, -1.f, true);
// m_mtx = mat4::ortho(0.f, (float)screen->Width(), (float)screen->Height(),
// 0.f,
// 1.f, -1.f);
m_shader->SetMat4(uLocProj, &__m);
} }
void Iron::Draw(const std::vector<Iron::Command::ref>& data) { void Iron::Draw(const std::vector<Iron::Command::ref>& data) {
@@ -148,11 +153,11 @@ void Iron::pSetupShader() {
AttrInfo_AddLoader(&m_shader->pInfo, 0, GPU_FLOAT, 2); AttrInfo_AddLoader(&m_shader->pInfo, 0, GPU_FLOAT, 2);
AttrInfo_AddLoader(&m_shader->pInfo, 1, GPU_FLOAT, 2); AttrInfo_AddLoader(&m_shader->pInfo, 1, GPU_FLOAT, 2);
AttrInfo_AddLoader(&m_shader->pInfo, 2, GPU_UNSIGNED_BYTE, 4); AttrInfo_AddLoader(&m_shader->pInfo, 2, GPU_UNSIGNED_BYTE, 4);
// m_shader->Load("romfs:/shaders/lithium.shbin"); /* m_shader->Load("romfs:/shaders/lithium.shbin");
// m_shader->Compile(__ironshader__); // m_shader->Compile(__ironshader__);
// m_shader->Input(GPU_FLOAT, 2); // pos m_shader->Input(GPU_FLOAT, 2); // pos
// m_shader->Input(GPU_FLOAT, 2); // uv m_shader->Input(GPU_FLOAT, 2); // uv
// m_shader->Input(GPU_UNSIGNED_BYTE, 4); // color m_shader->Input(GPU_UNSIGNED_BYTE, 4); // color*/
uLocProj = m_shader->loc("projection"); uLocProj = m_shader->loc("projection");
} }