From 4cf3685832ffb9008f09e425880fd900e04d0e1e Mon Sep 17 00:00:00 2001 From: tobid7 Date: Wed, 17 Dec 2025 08:53:59 +0100 Subject: [PATCH] Fix the shader loader - fixed cmake file as well? - remove useless files from example - reenable runtime shader compiler for some reason the raw data of a shader must stay in ram while the shader is loaded. It can be unloaded when the shader gets unloaded --- CMakeLists.txt | 6 +++--- example/CMakeLists.txt | 18 ---------------- example/romfs/shaders/.gitkeep | 0 example/shaders/lithium.v.pica | 33 ---------------------------- example/shaders/shader2d.v.pica | 38 --------------------------------- include/amethyst/c3d.hpp | 1 + source/c3d.cpp | 15 +++++++------ source/iron/iron.cpp | 19 ++--------------- 8 files changed, 14 insertions(+), 116 deletions(-) delete mode 100755 example/romfs/shaders/.gitkeep delete mode 100755 example/shaders/lithium.v.pica delete mode 100755 example/shaders/shader2d.v.pica diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f27833..ef3e2aa 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED true) option(AMY_GOD_DEV "Turn this on if you think you are god" OFF) # THis option should be disabled if you use STB_IMAGE in you main project -set(AMY_BUILD_STB_IMAGE 0) +set(AMY_BUILD_STB_IMAGE CACHE BOOL 0) set(AMY_BUILD_STB_TRUETYPE 1) set(AMY_WITH_MPG123 "Include MP3 Support" CACHE BOOL 1) -#add_subdirectory(vendor/libpicasso) +add_subdirectory(vendor/libpicasso) add_library(${PROJECT_NAME} STATIC source/app.cpp @@ -36,7 +36,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/source/internal ) -#target_link_libraries(${PROJECT_NAME} PUBLIC pica::pica) +target_link_libraries(${PROJECT_NAME} PUBLIC pica::pica) target_link_libraries(${PROJECT_NAME} PUBLIC m z ctru citro3d mpg123) target_compile_definitions(${PROJECT_NAME} PUBLIC AMY_3DS diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 79d98e7..ec91514 100755 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,27 +1,9 @@ cmake_minimum_required(VERSION 3.22) -find_program(PICASSO NAMES picasso REQUIRED) - -# Function origanally Created in Re-Craft-3DS -function(__amy_make_shader arg1 arg2) - # These only exist cause i was stupid - set(__FILE ${arg1}) - set(__NAME ${arg2}) - - # Need to build shaders during config stage :( - execute_process( - COMMAND ${PICASSO} -o "${CMAKE_CURRENT_SOURCE_DIR}/romfs/shaders/${__NAME}.shbin" "${__FILE}" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) -endfunction() - project(amethyst-example) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED true) -__amy_make_shader(${CMAKE_CURRENT_SOURCE_DIR}/shaders/shader2d.v.pica shader2d) -__amy_make_shader(${CMAKE_CURRENT_SOURCE_DIR}/shaders/lithium.v.pica lithium) - add_executable(${PROJECT_NAME} source/main.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE amethyst) target_compile_options(${PROJECT_NAME} PRIVATE -O2) diff --git a/example/romfs/shaders/.gitkeep b/example/romfs/shaders/.gitkeep deleted file mode 100755 index e69de29..0000000 diff --git a/example/shaders/lithium.v.pica b/example/shaders/lithium.v.pica deleted file mode 100755 index 392f29d..0000000 --- a/example/shaders/lithium.v.pica +++ /dev/null @@ -1,33 +0,0 @@ -; LI7 Shader -; Constants -.constf myconst(0.0, 1.0, 0.00392156862745, 0.0) -.alias ones myconst.yyyy ; Vector full of ones - -; Uniforms -.fvec projection[4] - -; Outputs -.out out_position position -.out out_color color -.out out_uv texcoord0 - -; Inputs -.alias in_xy v0 -.alias in_uvc v1 -.alias in_col v2 - -.proc main - mov r0.xy, in_xy.xy - mov r0.w, ones - - dp4 out_position.x, projection[0], r0 - dp4 out_position.y, projection[1], r0 - dp4 out_position.z, projection[2], r0 - dp4 out_position.w, projection[3], r0 - - mov out_uv, in_uvc.xy - - mul r1, myconst.zzzz, in_col - mov out_color, r1 - end -.end \ No newline at end of file diff --git a/example/shaders/shader2d.v.pica b/example/shaders/shader2d.v.pica deleted file mode 100755 index f9cd1cb..0000000 --- a/example/shaders/shader2d.v.pica +++ /dev/null @@ -1,38 +0,0 @@ -; Example PICA200 vertex shader - -; Uniforms -.fvec projection[4] - -; Constants -.constf myconst(0.0, 1.0, -1.0, 0.1) -.constf myconst2(0.3, 0.0, 0.0, 0.0) -.alias zeros myconst.xxxx ; Vector full of zeros -.alias ones myconst.yyyy ; Vector full of ones - -; Outputs -.out outpos position -.out outclr color - -; Inputs (defined as aliases for convenience) -.alias inpos v0 -.alias inclr v1 - -.bool test - -.proc main - ; Force the w component of inpos to be 1.0 - mov r0.xyz, inpos - mov r0.w, ones - - ; outpos = projectionMatrix * inpos - dp4 outpos.x, projection[0], r0 - dp4 outpos.y, projection[1], r0 - dp4 outpos.z, projection[2], r0 - dp4 outpos.w, projection[3], r0 - - ; outclr = inclr - mov outclr, inclr - - ; We're finished - end -.end \ No newline at end of file diff --git a/include/amethyst/c3d.hpp b/include/amethyst/c3d.hpp index ab95364..dc42ccf 100755 --- a/include/amethyst/c3d.hpp +++ b/include/amethyst/c3d.hpp @@ -60,6 +60,7 @@ class C3D { shaderProgram_s pProgram; DVLB_s* pCode = nullptr; int pReg = 0; + std::vector pRawData; }; class Frag { diff --git a/source/c3d.cpp b/source/c3d.cpp index 971eb76..39782d2 100755 --- a/source/c3d.cpp +++ b/source/c3d.cpp @@ -2,7 +2,7 @@ #include #include -// #include +#include namespace Amy { @@ -58,22 +58,23 @@ void C3D::Shader::Load(const std::string& path) { } void C3D::Shader::Load(const std::vector& data) { - pCode = DVLB_ParseFile((u32*)&data[0], data.size()); + if (!data.size()) { + throw std::runtime_error("[amy] unable to load shader from data!"); + } + pRawData = data; + pCode = DVLB_ParseFile((u32*)&pRawData[0], pRawData.size()); shaderProgramInit(&pProgram); shaderProgramSetVsh(&pProgram, &pCode->DVLE[0]); - C3D_BindProgram(&pProgram); AttrInfo_Init(&pInfo); } void C3D::Shader::Compile(const std::string& code) { - throw std::runtime_error("[amy]: unable to compile shader (not allowed)"); - /*auto ret = Pica::AssembleCode(code.c_str()); - Load(ret);*/ + auto ret = Pica::AssembleCode(code.c_str()); + Load(ret); } void C3D::Shader::Use() { // C3D_BindProgram(&pProgram); - // for some reason i need both ??? // code works perfectly without C3D_BindProgram // but nor withour shaderProgramUse ... shaderProgramUse(&pProgram); diff --git a/source/iron/iron.cpp b/source/iron/iron.cpp index a863b95..f9faf62 100644 --- a/source/iron/iron.cpp +++ b/source/iron/iron.cpp @@ -39,12 +39,6 @@ const char* __ironshader__ = R"(; LI7 Shader end .end)"; -// clang-format off -unsigned char li_shader[] = { - 0x44, 0x56, 0x4c, 0x42, 0x1, 0x0, 0x0, 0x0, 0xa4, 0x0, 0x0, 0x0, 0x44, 0x56, 0x4c, 0x50, 0x0, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50, 0x0, 0x0, 0x0, 0x9, 0x0, 0x0, 0x0, 0x98, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4e, 0x1, 0xf0, 0x7, 0x4e, 0x2, 0x8, 0x2, 0x8, 0x3, 0x18, 0x2, 0x8, 0x4, 0x28, 0x2, 0x8, 0x5, 0x38, 0x2, 0x8, 0x6, 0x10, 0x40, 0x4c, 0x7, 0xf1, 0x27, 0x22, 0x8, 0x10, 0x21, 0x4c, 0x0, 0x0, 0x0, 0x88, 0xac, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa1, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x68, 0xc3, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0xc3, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x62, 0xc3, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0xc3, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0xaf, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4f, 0xd5, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6f, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x44, 0x56, 0x4c, 0x45, 0x2, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x54, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x54, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x6c, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x74, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x2, 0x0, 0x5f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3f, 0x0, 0x1, 0x1, 0x37, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1, 0x0, 0xf, 0x0, 0x0, 0x0, 0x3, 0x0, 0x2, 0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x13, 0x0, 0x70, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x0, 0x0, - }; -// clang-format on -size_t li_shader_size = 0x124; std::vector> Iron::m_vbuf; std::vector> Iron::m_ibuf; int Iron::uLocProj = 0; @@ -132,20 +126,11 @@ bool Iron::pCheckSize(size_t idx, size_t vtx) { void Iron::pSetupShader() { m_shader = new C3D::Shader(); - m_shader->pCode = DVLB_ParseFile((u32*)li_shader, li_shader_size); - shaderProgramInit(&m_shader->pProgram); - shaderProgramSetVsh(&m_shader->pProgram, &m_shader->pCode->DVLE[0]); - AttrInfo_Init(&m_shader->pInfo); - AttrInfo_AddLoader(&m_shader->pInfo, 0, GPU_FLOAT, 2); - AttrInfo_AddLoader(&m_shader->pInfo, 1, GPU_FLOAT, 2); - AttrInfo_AddLoader(&m_shader->pInfo, 2, GPU_UNSIGNED_BYTE, 4); - // TODO: FUNKTIONIRT NICHT AHHHHHHHHHH - /*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); // uv m_shader->Input(GPU_UNSIGNED_BYTE, 4); // color - uLocProj = m_shader->loc("projection");*/ + uLocProj = m_shader->loc("projection"); } void Iron::pFragConfig() {