From e54e045fcbd9022e9ea75786cb10b39d0c30da3b Mon Sep 17 00:00:00 2001 From: EPICGameGuy Date: Mon, 27 Nov 2023 14:09:07 -0500 Subject: [PATCH] Trying out a custom renderer --- .github/workflows/docker-image.yml | 12 +- .../ps2gl_renderers/vertex_color_renderer.hpp | 17 ++ src/Makefile | 27 +-- src/Makefile.vuprogs | 26 +++ src/renderer/gs.cc | 3 + src/renderer/mesh.cc | 24 +- .../ps2gl_renderers/vertex_color_renderer.cc | 40 ++++ vu1/vertex_color_renderer.vcl | 213 ++++++++++++++++++ vu1/vertex_color_renderer.vo | Bin 0 -> 2912 bytes 9 files changed, 324 insertions(+), 38 deletions(-) create mode 100644 include/renderer/ps2gl_renderers/vertex_color_renderer.hpp create mode 100644 src/Makefile.vuprogs create mode 100644 src/renderer/ps2gl_renderers/vertex_color_renderer.cc create mode 100644 vu1/vertex_color_renderer.vcl create mode 100644 vu1/vertex_color_renderer.vo diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index d75544a..9a9106f 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -15,7 +15,17 @@ jobs: - uses: actions/checkout@v3 - name: Get dependencies run: | - apk add build-base git cmake xorriso assimp-dev + apk add build-base binutils-dev git cmake xorriso assimp-dev musl-obstack-dev + + - name: Install openvcl/MASP + run: + git clone https://github.com/nicholas477/openvcl.git dependencies/openvcl + cd dependencies/openvcl/contrib/masp + chmod +x ./configure && ./configure LIBS="-lobstack" + make clean install -j$(getconf _NPROCESSORS_ONLN) + cd ../../ + make clean install -j$(getconf _NPROCESSORS_ONLN) + - name: Install ps2stuff run: | diff --git a/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp b/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp new file mode 100644 index 0000000..6d3b63f --- /dev/null +++ b/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "GL/gl.h" +#include "ps2gl/immgmanager.h" +#include "ps2gl/linear_renderer.h" +#include "ps2gl/renderer.h" + +#define kVCRPrimType (((tU32)1 << 31) | 1) +#define kVCRPrimTypeFlag ((tU64)1 << 32) + +class VertexColorRenderer: public CLinearRenderer +{ +public: + VertexColorRenderer(); + + static VertexColorRenderer* Register(); +}; \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index f67a120..ba5963a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,6 +20,9 @@ ISO_FOLDER_DIR = build/iso VU_OUTPUT_DIR = vu VU_PROGS = $(addprefix $(OUTPUT_DIR)/$(VU_OUTPUT_DIR)/, $(patsubst %.vcl, %.o, $(VCL_FILES))) +PS2GL_DIR = dependencies/ps2gl +EE_INCS += -I$(PS2GL_DIR)/vu1 + # # Vector Unit assembler: # @@ -37,12 +40,14 @@ PS2_VCLPP = vclpp # # VCL/VU microprograms: # -VCL_PATH = $(PREFIX)vu1progs -VCL_FILES = - -EE_OBJS := $(VU_PROGS) $(EE_OBJS) +VCL_PATH = $(PREFIX) +VCL_FILES = renderer/ps2gl_renderers/vuprogs/general_pv_diff.vcl include src/Makefile.iso +include src/Makefile.vuprogs + +VUPROGS: $(addsuffix .vo, $(addprefix vu1/, $(RENDERERS))) +EE_OBJS := $(VUPROGS) $(EE_OBJS) .PHONY: compile cleancompile compile: $(EE_BIN) @@ -53,18 +58,10 @@ cleancompile: clean: cleaniso cleancompile -all: $(ISO_TGT) - -# -# VU microprograms: -# -$(VU_PROGS): $(OUTPUT_DIR)/$(VU_OUTPUT_DIR)/%.o: $(OUTPUT_DIR)/$(VU_OUTPUT_DIR)/%.vsm - $(PS2_VU_DVP) $< -o $@ +realclean: clean + rm -f $(VSM_SOURCES) -$(OUTPUT_DIR)/$(VU_OUTPUT_DIR)/%.vsm: - $(MKDIR) -p $(dir $@) - $(PS2_VCLPP) $(VCL_PATH)/$*.vcl $(dir $@)$*.pp.vcl -j - $(PS2_VCL) -o$@ $(dir $@)$*.pp.vcl +all: $(ISO_TGT) run: $(EE_BIN) cd $(dir $(EE_BIN)) diff --git a/src/Makefile.vuprogs b/src/Makefile.vuprogs new file mode 100644 index 0000000..ada8064 --- /dev/null +++ b/src/Makefile.vuprogs @@ -0,0 +1,26 @@ +RENDERERS = \ + vertex_color_renderer + +EE_OBJS += $(addsuffix .vo, $(addprefix vu1/, $(RENDERERS))) +VSM_SOURCES = $(addsuffix _vcl.vsm, $(addprefix vu1/, $(RENDERERS))) + +%.vo: %_vcl.vsm + dvp-as -o $@ $< + +%_vcl.vsm: %_pp4.vcl + vcl -o$@ $< + +%indexed_pp4.vcl: %indexed_pp3.vcl + cat $< | cc -E -P -imacros $(PS2GL_DIR)/vu1/vu1_mem_indexed.h -o $@ - + +%_pp4.vcl: %_pp3.vcl + cat $< | cc -E -P -imacros $(PS2GL_DIR)/vu1/vu1_mem_linear.h -o $@ - + +%_pp3.vcl: %_pp2.vcl + cat $< | sed 's/\[\([0-9]\)\]/_\1/g ; s/\[\([w-zW-Z]\)\]/\1/g' - > $@ + +%_pp2.vcl: %_pp1.vcl + masp -c ';' -I$(PS2GL_DIR) -o $@ $< + +%_pp1.vcl: %.vcl + cat $< | sed 's/#include[ ]\+.\+// ; s/#define[ ]\+.\+// ; s|\(\.include[ ]\+\)"\([^/].\+\)"|\1"$( $@ \ No newline at end of file diff --git a/src/renderer/gs.cc b/src/renderer/gs.cc index 1c8aaed..6e0850e 100644 --- a/src/renderer/gs.cc +++ b/src/renderer/gs.cc @@ -1,6 +1,7 @@ #include "objects/camera.hpp" #include "renderer/gs.hpp" #include "renderer/renderable.hpp" +#include "renderer/ps2gl_renderers/vertex_color_renderer.hpp" #include "stats.hpp" #include "egg/assert.hpp" @@ -109,6 +110,8 @@ static void init_renderer() init_lights(); + VertexColorRenderer::Register(); + for (Renderable::TIterator Itr = Renderable::Itr(); Itr; ++Itr) { Itr->on_gs_init(); diff --git a/src/renderer/mesh.cc b/src/renderer/mesh.cc index 387073e..0e80341 100644 --- a/src/renderer/mesh.cc +++ b/src/renderer/mesh.cc @@ -1,5 +1,6 @@ #include "renderer/mesh.hpp" #include "renderer/renderable.hpp" +#include "renderer/ps2gl_renderers/vertex_color_renderer.hpp" #include "egg/filesystem.hpp" #include @@ -7,29 +8,8 @@ #include #include -//std::unordered_map Mesh::loaded_meshes; static GLint num_lists = 0; -// static class MeshLoader: public renderable -// { -// public: -// MeshLoader() -// : renderable(true) -// { -// } - -// virtual void on_gs_init() override -// { -// //Mesh::loaded_meshes["/assets/models/kettle.ps2_model"_p] = Mesh("/assets/models/kettle.ps2_model"_p); -// //Mesh::loaded_meshes["/assets/models/shopping_cart.ps2_model"_p] = Mesh("/assets/models/shopping_cart.ps2_model"_p); - -// for (auto& [path, mesh] : Mesh::loaded_meshes) -// { -// mesh.compile(); -// } -// } -// } mesh_loader; - Mesh::Mesh() { list = -1; @@ -96,7 +76,7 @@ void Mesh::compile() printf("Compiling mesh strip: %d\n", i); const auto start_index = strip.strip_start_index; const auto count = strip.strip_end_index - start_index; - glDrawArrays(GL_TRIANGLE_STRIP, start_index, count); + glDrawArrays(kVCRPrimType, start_index, count); ++i; } diff --git a/src/renderer/ps2gl_renderers/vertex_color_renderer.cc b/src/renderer/ps2gl_renderers/vertex_color_renderer.cc new file mode 100644 index 0000000..1c8322b --- /dev/null +++ b/src/renderer/ps2gl_renderers/vertex_color_renderer.cc @@ -0,0 +1,40 @@ +#include "renderer/ps2gl_renderers/vertex_color_renderer.hpp" +#include "vu1_mem_linear.h" + +#define VU_FUNCTIONS(name) \ + void vsm##name##_CodeStart(); \ + void vsm##name##_CodeEnd() + +#define mVsmAddr(name) ((void*)vsm##name##_CodeStart) +#define mVsmSize(name) ((u8*)vsm##name##_CodeEnd - (u8*)vsm##name##_CodeStart) + +extern "C" { +VU_FUNCTIONS(VertexColorRenderer); +} + +VertexColorRenderer::VertexColorRenderer() + : CLinearRenderer(mVsmAddr(VertexColorRenderer), mVsmSize(VertexColorRenderer), 4, 3, + kInputStart, + kInputBufSize - kInputStart, + "Vertex color renderer") +{ + Requirements = 0; // no requirements + Capabilities = kVCRPrimTypeFlag; // provides the "kVCRPrimType" capability +} + +VertexColorRenderer* VertexColorRenderer::Register() +{ + // create a renderer and register it + + VertexColorRenderer* renderer = new VertexColorRenderer; + pglRegisterRenderer(renderer); + + // register the prim type + + pglRegisterCustomPrimType(kVCRPrimType, // the prim type we will pass to ps2gl (glBegin...) + kVCRPrimTypeFlag, // the corresponding renderer requirement + ~(tU64)0xffffffff, // we only care about the custom stuff (upper 32 bits) + true); // ok to merge multiple calls when possible + + return renderer; +} \ No newline at end of file diff --git a/vu1/vertex_color_renderer.vcl b/vu1/vertex_color_renderer.vcl new file mode 100644 index 0000000..f7838f1 --- /dev/null +++ b/vu1/vertex_color_renderer.vcl @@ -0,0 +1,213 @@ +/* Copyright (C) 2000,2001,2002 Sony Computer Entertainment America + + This file is subject to the terms and conditions of the GNU Lesser + General Public License Version 2.1. See the file "COPYING" in the + main directory of this archive for more details. */ + + #include "vu1_mem_linear.h" + + .include "db_in_db_out.i" + .include "math.i" + .include "lighting.i" + .include "clip_cull.i" + .include "geometry.i" + .include "io.i" + .include "general.i" + +kInputQPerV .equ 4 +kOutputQPerV .equ 3 + + .init_vf_all + .init_vi_all + + .name vsmVertexColorRenderer + + --enter + --endenter + + ; ------------------------ initialization --------------------------------- + + load_vert_xfrm vert_xform + + --cont + + ; -------------------- transform & texture loop --------------------------- + +main_loop_lid: + + init_constants + init_clip_cnst + + init_io_loop + init_out_buf + + set_strip_adcs + + ; const_color = material emissive + global ambient + get_cnst_color const_color + + init_bfc_strip + +xform_loop_lid: --LoopCS 1,3 + + ; xform/clip vertex + + load_vert vert + + xform_vert xformed_vert, vert_xform, vert + vert_to_gs gs_vert, xformed_vert + + load_strip_adc strip_adc + bfc_strip_vert xformed_vert, strip_adc + clip_vert xformed_vert + fcand vi01, 0x003ffff + iand vi01, vi01, do_clipping + set_adc_fbs gs_vert, strip_adc + + store_xyzf gs_vert + + ; constant color + + ; const_color = material emissive + global ambient + load_pvcolor vert_color + loi 255.0 + addi.w vert_color, vf00, i + muli.xyz vert_color, vert_color, i + store_rgba vert_color + + ;store_rgb const_color + + ; texture coords + + load_stq tex_stq + xform_tex_stq tex_stq, tex_stq, q ; q is from normalize_3 + store_stq tex_stq + + next_io + loop_io xform_loop_lid + + ibeq vi00, vi00, done_lid + + ; -------------------- lighting ------------------------------- + +lighting_lid: + + load_mat_amb material_amb + load_mat_spec material_spec + + load_vert_eye vert_to_eye + + ; ---------- directional lights ----------------- + + init_dlt_loop + ibeq num_dir_lights, vi00, pt_lights_lid + get_ones_vec ones + +dir_light_loop_lid: + + init_io_loop + + load_lt_amb light_amb + load_lt_diff light_diff + load_lt_spec light_spec + mul.xyz local_spec, light_spec, material_spec + + ; transform light direction into object space + load_lt_pos vert_to_light + load_o2wt obj_to_world_transpose + mul_vec_mat_33 vert_to_light, obj_to_world_transpose, vert_to_light + + ; in non-local viewer mode for infinite lights, the half-angle vec is fixed + get_half_angle half_angle, vert_to_eye, vert_to_light + +dir_light_vert_loop_lid: --LoopCS 1,3 + + load_normal normal + load_pvcolor material_diff + + get_diff_term acc, vert_to_light, normal, light_diff, material_diff, dot3_to_z, z, ones + add_spec_term acc, half_angle, normal, local_spec, dot3_to_w_slow, w + add_amb_term vert_color, light_amb, material_amb + + ; add to previous lighting calculations (other lights, global amb + emission) + accum_rgb vert_color, vert_color + + store_rgb vert_color + + next_io + loop_io dir_light_vert_loop_lid + + ; loop over lights + + next_dir_light + loop_dir_lts dir_light_loop_lid + + ; ---------- point lights ----------------- + +pt_lights_lid: + + init_plt_loop + ibeq num_pt_lights, vi00, done_lid + get_ones_vec ones + +pt_light_loop_lid: + + init_io_loop + + load_lt_amb light_amb + load_lt_diff light_diff + load_lt_spec light_spec + mul.xyz local_spec, light_spec, material_spec + load_lt_atten atten_coeff + + ; transform light position to object space + load_lt_pos light_pos + load_w2o world_to_obj + mul_pt_mat_34 light_pos, world_to_obj, light_pos + +pt_light_vert_loop_lid: + + --LoopCS 1,3 + --LoopExtra 5 + + load_normal normal + load_vert vert + load_pvcolor material_diff + + get_v2l_atten vert_to_light, light_pos, vert, atten, atten_coeff, ones + get_diff_term acc, vert_to_light, normal, light_diff, material_diff, dot3_to_w, w + + get_half_angle half_angle, vert_to_eye, vert_to_light + add_spec_term acc, half_angle, normal, local_spec, dot3_to_w, w + + add_amb_term vert_color, light_amb, material_amb + + atten_color vert_color, vert_color, atten + + accum_rgb vert_color, vert_color + + store_rgb vert_color + + next_io + loop_io pt_light_vert_loop_lid + + next_pt_light + loop_pt_lts pt_light_loop_lid + + ; -------------------- done! ------------------------------- + +done_lid: + + ; clamp and convert to fixed-point + ; reads vert_color + finish_colors + + ; ---------------- kick packet to GS ----------------------- + + kick_to_gs + + --cont + + b main_loop_lid + +.END ; for gasp diff --git a/vu1/vertex_color_renderer.vo b/vu1/vertex_color_renderer.vo new file mode 100644 index 0000000000000000000000000000000000000000..8fad06da638e785c336a49b4be16729b6a525e2f GIT binary patch literal 2912 zcmbtV(N9}d82?UhOK)LY%AUH+ga%}xZWh>zgNd$PR~6~jrY;lFcvol_SWDZrrEUp? zHvR!7CcZ71zW87?!GuR#`e26+(I*oVQDZMKkke4 z^8KGv{p$Pv@l|t3+dtTr*g5x0-xTI8>qN3GlCP}m_=wJU@7UH02HW>!ulmJFy}$+9d0(Op%oB{JGmvR-{LyNRD(#!L@TsWc$6Z zrO#q_3D|xhW7kS-{o~h`3X<(RvNOEE<~w^A$>SD>Ja5?`J1zIn=LV5b?~ZMQfVqT7yg@VhTv^Skx)G4Xa{0lVDt362INh3 zml*GMUy{n#VD5mz&H+Z)Cxln~a3BVH!s=8E~Gz9CRjg@tce- zfI3I0J}n+a0zTxmue}@ZuVB++Sk;?0jFcy-_+t%+rf7|h<9Nw0% zgsc(k2k<(pYH4}m9!s@#n^hjo(eVii1^6xd6?16BI10WvP;UwY? zk;D454!=)pq8Y4zpu-;_z9Ra@`lmYlIpSH-2G&;*mo4~G4TuM+h5!Hn literal 0 HcmV?d00001