From 92d5623b9e2c8abe4c493229767d1d8fb23d721b Mon Sep 17 00:00:00 2001 From: nchalkley2 Date: Wed, 28 Aug 2024 14:06:39 -0400 Subject: [PATCH] Got texture rendering working! --- dependencies/egg-library/compile.sh | 2 +- .../include/egg/texture_header.hpp | 18 +++++ .../include/egg-ps2-graphics-lib/texture.hpp | 1 + dependencies/egg-ps2-graphics-lib/src/mesh.cc | 62 ++++++++++-------- .../egg-ps2-graphics-lib/src/texture.cc | 24 +++++-- .../vu1/vertex_color_texture_renderer.vcl | 16 +++-- .../vu1/vertex_color_texture_renderer.vo | Bin 1268 -> 1316 bytes src/renderer/mesh.cc | 10 ++- src/renderer/texture.cc | 11 ++++ .../src/texture/texture.cpp | 17 ++++- 10 files changed, 122 insertions(+), 39 deletions(-) diff --git a/dependencies/egg-library/compile.sh b/dependencies/egg-library/compile.sh index 0e21910..4288eb4 100755 --- a/dependencies/egg-library/compile.sh +++ b/dependencies/egg-library/compile.sh @@ -1,3 +1,3 @@ #!/bin/bash -make install -j$(nproc) && mkdir -p build && pushd build && cmake .. && sudo make install -j$(nproc); popd +make clean && make install -j$(nproc) && mkdir -p build && pushd build && cmake .. && sudo make install -j$(nproc); popd diff --git a/dependencies/egg-library/include/egg/texture_header.hpp b/dependencies/egg-library/include/egg/texture_header.hpp index fcbe341..7dcf7e3 100644 --- a/dependencies/egg-library/include/egg/texture_header.hpp +++ b/dependencies/egg-library/include/egg/texture_header.hpp @@ -14,6 +14,15 @@ struct TextureFileHeader uint8_t function; uint8_t components; + // Wrapping settings + uint8_t horizontal; + uint8_t vertical; + + int32_t minu; + int32_t maxu; + int32_t minv; + int32_t maxv; + OffsetArray clut; OffsetArray data; }; @@ -27,6 +36,15 @@ static size_t serialize(Serializer& serializer, const TextureFileHeader& texture serialize(serializer, texture_header.psm); serialize(serializer, texture_header.function); serialize(serializer, texture_header.components); + + serialize(serializer, texture_header.horizontal); + serialize(serializer, texture_header.vertical); + + serialize(serializer, texture_header.minu); + serialize(serializer, texture_header.maxu); + serialize(serializer, texture_header.minv); + serialize(serializer, texture_header.maxv); + serialize(serializer, texture_header.clut, 1, 16); serialize(serializer, texture_header.data, 1, 16); return begin; diff --git a/dependencies/egg-ps2-graphics-lib/include/egg-ps2-graphics-lib/texture.hpp b/dependencies/egg-ps2-graphics-lib/include/egg-ps2-graphics-lib/texture.hpp index 1e7445a..e2575d7 100644 --- a/dependencies/egg-ps2-graphics-lib/include/egg-ps2-graphics-lib/texture.hpp +++ b/dependencies/egg-ps2-graphics-lib/include/egg-ps2-graphics-lib/texture.hpp @@ -15,6 +15,7 @@ struct texture_descriptor lod_t lod; clutbuffer_t clut; texbuffer_t t_texbuff; + texwrap_t wrap; bool is_uploaded; diff --git a/dependencies/egg-ps2-graphics-lib/src/mesh.cc b/dependencies/egg-ps2-graphics-lib/src/mesh.cc index 4862ade..329e0dc 100644 --- a/dependencies/egg-ps2-graphics-lib/src/mesh.cc +++ b/dependencies/egg-ps2-graphics-lib/src/mesh.cc @@ -1,6 +1,7 @@ #include "egg-ps2-graphics-lib/mesh.hpp" #include "egg-ps2-graphics-lib/egg-ps2-graphics-lib.hpp" #include "egg-ps2-graphics-lib/vu_programs.hpp" +#include "egg-ps2-graphics-lib/texture.hpp" #include #include @@ -119,9 +120,29 @@ void draw_textured_strip(const Matrix& mesh_to_screen_matrix, const mesh_descrip packet2_add_float(get_current_vif_packet(), mesh.screen_scale.z); // scale packet2_add_u32(get_current_vif_packet(), mesh.num_verts); // vert count + // 5 + u8 j = 0; // RGBA + for (j = 0; j < 4; j++) + packet2_add_u32(get_current_vif_packet(), 128); + + // 6 + packet2_add_float(get_current_vif_packet(), mesh.fog_offset); // Offset + packet2_add_float(get_current_vif_packet(), mesh.fog_scale); // Scale + packet2_add_float(get_current_vif_packet(), 0.f); // padding + packet2_add_float(get_current_vif_packet(), 0.f); // padding + + // 7 + packet2_utils_gif_add_set(get_current_vif_packet(), 1); + + // 8 + packet2_utils_gs_add_lod(get_current_vif_packet(), &mesh.texture->lod); + + // 9 + packet2_utils_gs_add_texbuff_clut(get_current_vif_packet(), &mesh.texture->t_texbuff, &mesh.texture->clut); + if (mesh.enable_fog) { - // 5 + // 10 // The F in XYZF2 stands for fog packet2_utils_gs_add_prim_giftag(get_current_vif_packet(), &prim, mesh.num_verts, ((u64)GIF_REG_ST) << 0 | ((u64)GIF_REG_RGBAQ) << 4 | ((u64)GIF_REG_XYZF2) << 8, @@ -129,41 +150,30 @@ void draw_textured_strip(const Matrix& mesh_to_screen_matrix, const mesh_descrip } else { - // 5 + // 10 packet2_utils_gs_add_prim_giftag(get_current_vif_packet(), &prim, mesh.num_verts, DRAW_STQ2_REGLIST, 3, 0); } - - // 6 - u8 j = 0; // RGBA - for (j = 0; j < 4; j++) - packet2_add_u32(get_current_vif_packet(), 128); - - // 7 - packet2_add_float(get_current_vif_packet(), mesh.fog_offset); // Offset - packet2_add_float(get_current_vif_packet(), mesh.fog_scale); // Scale - packet2_add_float(get_current_vif_packet(), 0.f); // padding - packet2_add_float(get_current_vif_packet(), 0.f); // padding } packet2_utils_vu_close_unpack(get_current_vif_packet()); // Position data - packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8, mesh.pos, mesh.num_verts, 1); + packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 11, mesh.pos, mesh.num_verts, 1); if (mesh.color) { // Color data - packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8 + mesh.num_verts, mesh.color, mesh.num_verts, 1); + packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 11 + mesh.num_verts, mesh.color, mesh.num_verts, 1); } if (mesh.uvs) { // UV data - packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 8 + (mesh.num_verts * 2), mesh.uvs, mesh.num_verts, 1); + packet2_utils_vu_add_unpack_data(get_current_vif_packet(), 11 + (mesh.num_verts * 2), mesh.uvs, mesh.num_verts, 1); } - assert((8 + (mesh.num_verts * 6)) < 496); + assert((11 + (mesh.num_verts * 6)) < 496); packet2_utils_vu_add_start_program(get_current_vif_packet(), vu1_programs::get_vertex_color_texture_program_addr()); } @@ -236,16 +246,16 @@ void draw_mesh_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& strip.num_verts = num_verts; - draw_untextured_strip(mesh_to_screen_matrix, strip); + if (strip.texture) + { + assert(strip.uvs != nullptr); - // if (strip.enable_texture_mapping && strip.texture) - // { - // draw_textured_strip(mesh_to_screen_matrix, strip); - // } - // else - // { - // draw_untextured_strip(mesh_to_screen_matrix, strip); - // } + draw_textured_strip(mesh_to_screen_matrix, strip); + } + else + { + draw_untextured_strip(mesh_to_screen_matrix, strip); + } } if (i_end >= mesh.num_verts) diff --git a/dependencies/egg-ps2-graphics-lib/src/texture.cc b/dependencies/egg-ps2-graphics-lib/src/texture.cc index 05c0b27..4c0929e 100644 --- a/dependencies/egg-ps2-graphics-lib/src/texture.cc +++ b/dependencies/egg-ps2-graphics-lib/src/texture.cc @@ -8,6 +8,7 @@ #include #include #include +#include namespace egg::ps2::graphics { @@ -37,18 +38,24 @@ texture_descriptor::texture_descriptor() t_texbuff.info.height = 0; t_texbuff.info.components = TEXTURE_COMPONENTS_RGB; t_texbuff.info.function = TEXTURE_FUNCTION_DECAL; + + wrap.horizontal = WRAP_REPEAT; + wrap.vertical = WRAP_REPEAT; + wrap.minu = 0; + wrap.maxu = 1; + wrap.minv = 0; + wrap.maxv = 1; } void texture_descriptor::set_width_height(u32 in_width, u32 in_height) { - // Check the dimensions are powers of 2 - // assert((in_width & (in_width - 1)) == 0); - // assert((in_height & (in_height - 1)) == 0); - t_texbuff.width = in_width; height = in_height; + wrap.maxu = in_width; + wrap.maxv = in_height; + t_texbuff.info.width = draw_log2(in_width); t_texbuff.info.height = draw_log2(in_height); } @@ -89,6 +96,15 @@ void upload_texture(texture_descriptor& texture, void* texture_data, void* clut_ texture.clut.address, 64)); } + + printf("Texture max: %d, %d\n", texture.wrap.maxu, texture.wrap.maxv); + packet2_chain_open_cnt(texture_packet, 0, 0, 0); + packet2_update(texture_packet, draw_texture_wrapping( + texture_packet->next, + 0, + &texture.wrap)); + packet2_chain_close_tag(texture_packet); + packet2_update(texture_packet, draw_texture_flush(texture_packet->next)); dma_channel_send_packet2(texture_packet, DMA_CHANNEL_GIF, 1); dma_wait_fast(); diff --git a/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vcl b/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vcl index 5618a66..92c2a7e 100644 --- a/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vcl +++ b/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vcl @@ -27,13 +27,17 @@ ; float : X, Y, Z - scale vector that we will use to scale the verts after projecting them. ; float : W - vert count. ilw.w vertCount, 4(iBase) - lq primTag, 5(iBase) ; GIF tag - tell GS how many data we will send - lq rgba, 6(iBase) ; RGBA + lq rgba, 5(iBase) ; RGBA ; u32 : R, G, B, A (0-128) - lq.xy fogSetting, 7(iBase) ; x = offset, y = scale + lq.xy fogSetting, 6(iBase) ; x = offset, y = scale - iaddiu vertexData, iBase, 8 ; pointer to vertex data + lq gifSetTag, 7(iBase) ; GIF tag - set + lq texGifTag1, 8(iBase) ; GIF tag - texture LOD + lq texGifTag2, 9(iBase) ; GIF tag - texture buffer & CLUT + lq primTag, 10(iBase) ; GIF tag - tell GS how many data we will send + + iaddiu vertexData, iBase, 11 ; pointer to vertex data iadd colorData, vertexData, vertCount ; pointer to color data iadd uvData, colorData, vertCount ; pointer to uv data iadd kickAddress, uvData, vertCount ; pointer for XGKICK @@ -41,6 +45,10 @@ ;//////////////////////////////////////////// ;/////////// --- Store tags --- ///////////// + sqi gifSetTag, (destAddress++) ; + sqi texGifTag1, (destAddress++) ; texture LOD tag + sqi gifSetTag, (destAddress++) ; + sqi texGifTag2, (destAddress++) ; texture buffer & CLUT tag sqi primTag, (destAddress++) ; prim + tell gs how many data will be ;//////////////////////////////////////////// diff --git a/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vo b/dependencies/egg-ps2-graphics-lib/vu1/vertex_color_texture_renderer.vo index 59717535b6dd4b7ef60f8e5c6e85b825c28a00b4..715d82e3ec1a9ec00e4f8785e576de2bb15911d9 100644 GIT binary patch delta 219 zcmeyuxrA$i0^^>EiuRg}0*@H~Gcho*3p@eS90Je4G^fB*FwG_K987ahEL1nFRes(G zQe0~crD1$+2%i~h2#df`uqL*RD}@;+|6%5se1R?iQvJQ&?kY&rF&ia6nfq`-IM`q#4H9)Z^lY4<;OMuQe1{OQR OBEXu(%)sCe;sF3G&p|Z+ delta 167 zcmZ3&^@VeS0%OlaMSDS3f#;0>nHU%t1s;KEj)|S>g3JO>z#JBVqhOkS<3nM_$rqS8 zChM?pO#Z_d!&opml1X2%g8>R&Gcp_iGCMZUWQu15O8#V4X527YmPH**icgMV5dgAs qS=3oKFaqtGtjHofc@0o(%jCU4u@a#D$G~ELSOi$Dm>C%SK|BDo@+$!V diff --git a/src/renderer/mesh.cc b/src/renderer/mesh.cc index 82bed08..8dfac59 100644 --- a/src/renderer/mesh.cc +++ b/src/renderer/mesh.cc @@ -65,10 +65,14 @@ void Mesh::draw(const GS::GSState& gs_state, const Matrix& render_matrix, bool f return; } - m.uvs = get_mesh()->uvs.get_ptr() + start_index; + m.uvs = get_mesh()->uvs.get_ptr() + start_index; + m.texture = &texture->texture_descriptor; - // printf("Uploading texture!\n"); - // check(texture->upload_texture()); + if (!texture->texture_descriptor.is_uploaded) + { + printf("Uploading texture!\n"); + check(texture->upload_texture()); + } // printf("Using texture!\n"); // check(texture->use_texture()); diff --git a/src/renderer/texture.cc b/src/renderer/texture.cc index a5096a5..af819cb 100644 --- a/src/renderer/texture.cc +++ b/src/renderer/texture.cc @@ -33,6 +33,17 @@ Texture::Texture(Asset::Reference texture_asset_ref, AssetRegistry::Asset* in_te texture_descriptor.clut.load_method = CLUT_LOAD; texture_descriptor.clut.storage_mode = CLUT_STORAGE_MODE1; + texture_descriptor.wrap.horizontal = texture->horizontal; + texture_descriptor.wrap.vertical = texture->vertical; + + texture_descriptor.wrap.minu = texture->minu; + texture_descriptor.wrap.maxu = texture->maxu; + + texture_descriptor.wrap.minv = texture->minv; + texture_descriptor.wrap.maxv = texture->maxv; + + printf("Texture size (bytes): %ld\n", texture->data.length); + texture_descriptor.set_width_height(texture->size_x, texture->size_y); } } diff --git a/tools/ps2-mesh-converter/src/texture/texture.cpp b/tools/ps2-mesh-converter/src/texture/texture.cpp index ed88de4..7845239 100644 --- a/tools/ps2-mesh-converter/src/texture/texture.cpp +++ b/tools/ps2-mesh-converter/src/texture/texture.cpp @@ -58,6 +58,12 @@ #define TEXTURE_FUNCTION_HIGHLIGHT 2 #define TEXTURE_FUNCTION_HIGHLIGHT2 3 +/** Texture wrapping */ +#define WRAP_REPEAT 0 +#define WRAP_CLAMP 1 +#define WRAP_REGION_CLAMP 2 +#define WRAP_REGION_REPEAT 3 + static std::filesystem::path get_file_path(std::string_view json_path, const Json::Value& obj) { std::string input_file_path = obj["file"].asString(); @@ -255,7 +261,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector