# Additions

- libpicasso (shader compiling)
- linearAllocator template (for std::vector)
- palladium vec and mat api
- iron (2d renderer)
This commit is contained in:
2025-11-22 14:55:01 +01:00
parent 2ad6d49511
commit c3a0e936c8
15 changed files with 1200 additions and 19 deletions

View File

@@ -2,6 +2,7 @@
#include <amethyst/c3d.hpp>
#include <amethyst/utils.hpp>
#include <pica.hpp>
namespace amy {
@@ -40,13 +41,22 @@ void c3d::shader::load(const std::string& path) {
if (!code.size()) {
throw std::runtime_error("[amy] shader: unable to load " + path);
}
m_code = DVLB_ParseFile((u32*)&code[0], code.size());
load(code);
}
void c3d::shader::load(const std::vector<uc>& data) {
m_code = DVLB_ParseFile((u32*)&data[0], data.size());
shaderProgramInit(&m_program);
shaderProgramSetVsh(&m_program, &m_code->DVLE[0]);
C3D_BindProgram(&m_program);
AttrInfo_Init(&m_info);
}
void c3d::shader::compile(const std::string& code) {
auto ret = Pica::AssembleCode(code.c_str());
load(ret);
}
void c3d::shader::use() {
shaderProgramUse(&m_program);
C3D_SetAttrInfo(&m_info);
@@ -56,6 +66,14 @@ void c3d::shader::setMat4(int loc, C3D_Mtx* m) {
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, loc, m);
}
void c3d::shader::setMat4(int loc, const mat4& m) {
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, loc, (const C3D_Mtx*)&m);
}
int c3d::shader::loc(const std::string& name) {
return shaderInstanceGetUniformLocation(m_program.vertexShader, name.c_str());
}
void c3d::shader::input(int reg, GPU_FORMATS f, int num) {
AttrInfo_AddLoader(&m_info, reg, f, num);
}
@@ -71,4 +89,13 @@ void c3d::frag::edit(int id) {
m_env = C3D_GetTexEnv(id);
C3D_TexEnvInit(m_env);
}
void c3d::drawArrays(int start, int count, GPU_Primitive_t prim) {
C3D_DrawArrays(prim, start, count);
}
void c3d::drawElements(int count, const void* idx_ptr, int type,
GPU_Primitive_t prim) {
C3D_DrawElements(prim, count, type, idx_ptr);
}
} // namespace amy

77
source/iron.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include <amethyst/iron.hpp>
namespace amy {
const char* __ironshader__ = R"(; 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
.entry vmain
.proc vmain
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)";
std::vector<iron::vertex, linearAllocator<iron::vertex>> iron::m_vbuf;
std::vector<u16, linearAllocator<u16>> iron::m_ibuf;
int iron::uLocProj = 0;
c3d::shader* iron::m_shader = nullptr;
mat4 iron::m_mtx;
int iron::m_idx = 0, iron::m_vtx = 0;
void iron::init() {
setupShader();
m_vbuf.resize(4 * 4096);
m_ibuf.resize(6 * 4096);
}
void iron::newFrame() {
m_idx = 0;
m_vtx = 0;
}
void iron::drawOn(c3d::screen* screen) {
m_shader->use();
m_mtx = mat4::ortho(0.f, (float)screen->width(), (float)screen->height(), 0.f,
1.f, -1.f);
m_shader->setMat4(uLocProj, m_mtx);
}
void iron::setupShader() {
m_shader = new c3d::shader();
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");
}
void iron::fragConfig() {
c3d::frag::edit();
c3d::frag::src(C3D_Both, GPU_TEXTURE0);
c3d::frag::func(C3D_Both, GPU_MODULATE);
}
} // namespace amy

121
source/maths/mat.cpp Normal file
View File

@@ -0,0 +1,121 @@
/*
MIT License
Copyright (c) 2024 - 2025 tobid7
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.
*/
#include <amethyst/maths/mat.hpp>
namespace amy {
mat4 mat4::rotateX(float a) {
float c = std::cos(a);
float s = std::sin(a);
mat4 ret = identity();
ret(1, 1) = c;
ret(1, 2) = -s;
ret(2, 1) = s;
ret(2, 2) = c;
return ret;
}
mat4 mat4::rotateY(float a) {
float c = std::cos(a);
float s = std::sin(a);
mat4 ret = identity();
ret(0, 0) = c;
ret(0, 2) = s;
ret(2, 0) = -s;
ret(2, 2) = c;
return ret;
}
mat4 mat4::rotateZ(float a) {
float c = std::cos(a);
float s = std::sin(a);
mat4 ret = identity();
ret(0, 0) = c;
ret(0, 1) = -s;
ret(1, 0) = s;
ret(1, 1) = c;
return ret;
}
mat4 mat4::rotate(fvec3 axis, float a) {
float s = std::sin(a);
float c = std::cos(a);
float t = 1.f - c;
axis = axis.Normalize();
float x = axis.x;
float y = axis.y;
float z = axis.z;
mat4 ret = identity();
ret(0, 0) = t * x * x + c;
ret(0, 1) = t * x * y - z * s;
ret(0, 2) = t * x * z + y * s;
ret(1, 0) = t * x * y + z * s;
ret(1, 1) = t * y * y + c;
ret(1, 2) = t * y * z - x * s;
ret(2, 0) = t * x * z - y * s;
ret(2, 1) = t * y * z + x * s;
ret(2, 2) = t * z * z + c;
return ret;
}
mat4 mat4::perspective(float fov, float aspect, float n, float f) {
float _fov = std::tan(fov / 2.f);
mat4 ret;
ret(0, 0) = 1.f / (aspect * _fov);
ret(1, 1) = 1.f / _fov;
#ifdef __3DS__
ret(2, 3) = f * n / (n - f);
ret(2, 2) = -(-1.f) * n / (n - f);
#else
ret(2, 2) = -(f + n) / (f - n);
ret(2, 3) = -(2.f * f * n) / (f - n);
#endif
ret(3, 2) = -1.f;
ret(3, 3) = 0.0f;
return ret;
}
mat4 mat4::lookAt(const fvec3& pos, const fvec3& center, const fvec3& up) {
auto f = fvec3(center - pos).Normalize();
auto s = f.Cross(up).Normalize();
auto u = s.Cross(f);
mat4 ret = identity();
ret(0, 0) = s.x;
ret(0, 1) = s.y;
ret(0, 2) = s.z;
ret(1, 0) = u.x;
ret(1, 1) = u.y;
ret(1, 2) = u.z;
ret(2, 0) = -f.x;
ret(2, 1) = -f.y;
ret(2, 2) = -f.z;
ret(0, 3) = -s.Dot(pos);
ret(1, 3) = -u.Dot(pos);
ret(2, 3) = f.Dot(pos);
return ret;
}
} // namespace amy