Skip to content

Commit

Permalink
Got texture rendering working!
Browse files Browse the repository at this point in the history
  • Loading branch information
nchalkley2 committed Aug 28, 2024
1 parent eda92d9 commit 92d5623
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dependencies/egg-library/compile.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions dependencies/egg-library/include/egg/texture_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> clut;
OffsetArray<std::byte> data;
};
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct texture_descriptor
lod_t lod;
clutbuffer_t clut;
texbuffer_t t_texbuff;
texwrap_t wrap;

bool is_uploaded;

Expand Down
62 changes: 36 additions & 26 deletions dependencies/egg-ps2-graphics-lib/src/mesh.cc
Original file line number Diff line number Diff line change
@@ -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 <kernel.h>
#include <malloc.h>
Expand Down Expand Up @@ -119,51 +120,60 @@ 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,
3, 0);
}
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());
}
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions dependencies/egg-ps2-graphics-lib/src/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <dma.h>
#include <draw.h>
#include <gs_psm.h>
#include <stdio.h>

namespace egg::ps2::graphics
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@
; 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
iadd destAddress, uvData, vertCount ; helper pointer for data inserting
;////////////////////////////////////////////
;/////////// --- 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
;////////////////////////////////////////////
Expand Down
Binary file not shown.
10 changes: 7 additions & 3 deletions src/renderer/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 11 additions & 0 deletions src/renderer/texture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
17 changes: 16 additions & 1 deletion tools/ps2-mesh-converter/src/texture/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -255,7 +261,7 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
using namespace Magick;
print("Opening texture!");

std::filesystem::path texture_file_path = get_file_path(path, obj);
const std::filesystem::path texture_file_path = get_file_path(path, obj);

print("Texture file path: %s", texture_file_path.c_str());

Expand All @@ -268,6 +274,15 @@ bool parseTexture(std::string_view path, const Json::Value& obj, std::vector<std
texture_header.function = TEXTURE_FUNCTION_DECAL;
texture_header.components = obj["alpha"].asBool() ? TEXTURE_COMPONENTS_RGBA : TEXTURE_COMPONENTS_RGB;

print("Setting texture to repeat mapping");
texture_header.horizontal = WRAP_REPEAT;
texture_header.vertical = WRAP_REPEAT;

texture_header.minu = 0;
texture_header.maxu = texture_header.size_x;
texture_header.minv = 0;
texture_header.maxv = texture_header.size_y;

const std::string color_type = obj["psm"].asString();

print("Texture size: x: %u y: %u", texture_header.size_x, texture_header.size_y);
Expand Down

0 comments on commit 92d5623

Please sign in to comment.