Skip to content

Commit

Permalink
Working on textures, added asset registry
Browse files Browse the repository at this point in the history
  • Loading branch information
nchalkley2 committed Aug 25, 2024
1 parent e26d9ad commit a3a160d
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 120 deletions.
2 changes: 2 additions & 0 deletions dependencies/egg-library/include/egg/hashmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "egg/assert.hpp"

// Hashmap that doesn't allocate any heap memory
//
// Key is not actually stored, just the hashes of the keys
template <size_t size, typename keyT, typename valueT>
class HashMap
Expand Down
2 changes: 1 addition & 1 deletion dependencies/egg-ps2-graphics-lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ EE_OBJS_DIR = obj/
EE_INCS += -I./include -I$(PS2SDK)/ports/include -I$(EE_SRC_DIR)

EE_LIB = $(EE_LIB_DIR)libegg-ps2-graphics-lib.a
EE_OBJS = src/egg-ps2-graphics-lib.o src/mesh.o src/vu_programs.o src/types.o src/draw.o
EE_OBJS = src/egg-ps2-graphics-lib.o src/mesh.o src/vu_programs.o src/types.o src/draw.o src/texture.o
EE_OPTFLAGS = -std=gnu++20 -g
EE_WARNFLAGS = -Wall -Wno-unused-function -Wno-unused-variable -Wno-strict-aliasing -Wno-conversion-null -Wno-unused-but-set-variable -Werror=return-type

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "tamtypes.h"
#include "draw_primitives.h"

namespace egg::ps2::graphics
{
prim_t& get_empty_prim();
void set_fog_color(u8 r, u8 g, u8 b);
}
} // namespace egg::ps2::graphics
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <packet2.h>
#include <gs_psm.h>

#include <array>
#include "types.hpp"

Expand All @@ -9,7 +11,41 @@ struct Vector;

namespace egg::ps2::graphics
{
void init();
struct init_options
{
init_options();

// Defaults to 640
u32 framebuffer_width;

// Defaults to 448
u32 framebuffer_height;

// Framebuffer color depth, defaults to 32bpp
u32 framebuffer_psm;

// Screen mode, defaults to NTSC (256 x 224 to 640 x 448)
//
// If you change the framebuffer resolution, you should probably change this
// too
u32 graph_mode;

// Defaults to non-interlaced (0)
u32 interlaced;

// Frame or field mode. Honestly not sure what this does, I think it
// refers to interlacing.
//
// Defaults to frame (1)
u32 ffmd;

// Enable/disable flicker filter (for interlacing)
u32 flicker_filter;

bool double_buffer_vu;
};

void init(const init_options& init_options = init_options());

// Uploads a VU program, returns the address of the loaded program
u32 load_vu_program(void* program_start_address, void* program_end_address);
Expand All @@ -24,13 +60,6 @@ void end_draw();

using vif_packet_t = utils::inline_packet2<10000>;

// Returns the two vif packets in use
std::array<vif_packet_t, 2>& get_vif_packets();

packet2_t* get_current_vif_packet();

u8 get_vif_packet_context();

void flip_vip_packet_context();

} // namespace egg::ps2::graphics
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct mesh_descriptor
// Has to be at least 3
u32 num_verts;

bool enable_texture_mapping;

// Address of the VU program loaded in memory used to perform vertex
// processing on this mesh.
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "tamtypes.h"
#include "packet2_utils.h"

namespace egg::ps2::graphics
{

struct texture_descriptor
{
texture_descriptor();

lod_t lod;
clutbuffer_t clut;
texbuffer_t t_texbuff;

bool is_uploaded;

void set_width_height(u32 width, u32 height);
};

// Uploads the texture to vram
void upload_texture(texture_descriptor& texture, void* texture_data);

// Sets the texture currently being drawn
void set_texture(texture_descriptor& texture);
} // namespace egg::ps2::graphics
11 changes: 8 additions & 3 deletions dependencies/egg-ps2-graphics-lib/src/draw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace egg::ps2::graphics
{
void set_fog_color(u8 r, u8 g, u8 b)
prim_t& get_empty_prim()
{
prim_t prim;
static prim_t prim;
prim.type = PRIM_TRIANGLE;
prim.shading = PRIM_SHADE_GOURAUD;
prim.mapping = 1;
Expand All @@ -21,11 +21,16 @@ void set_fog_color(u8 r, u8 g, u8 b)
prim.mapping_type = PRIM_MAP_ST;
prim.colorfix = PRIM_UNFIXED;

return prim;
}

void set_fog_color(u8 r, u8 g, u8 b)
{
packet2_utils_vu_open_unpack(get_current_vif_packet(), 0, 1);
{
packet2_utils_gif_add_set(get_current_vif_packet(), 1);
packet2_add_2x_s64(get_current_vif_packet(), GS_SET_FOGCOL(r, g, b), GS_REG_FOGCOL);
packet2_utils_gs_add_prim_giftag(get_current_vif_packet(), &prim, 0,
packet2_utils_gs_add_prim_giftag(get_current_vif_packet(), &get_empty_prim(), 0,
((u64)GIF_REG_RGBAQ) << 0, 1, 0);
}
packet2_utils_vu_close_unpack(get_current_vif_packet());
Expand Down
107 changes: 37 additions & 70 deletions dependencies/egg-ps2-graphics-lib/src/egg-ps2-graphics-lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ framebuffer_t* current_frame;
zbuffer_t z;

/** Some initialization of GS and VRAM allocation */
void init_gs(framebuffer_t* t_frame, zbuffer_t* t_z)
void init_gs(const init_options& init_options, framebuffer_t* t_frame, zbuffer_t* t_z)
{
printf("egg-ps2-graphics-lib: init gs\n");
// Define a 32-bit 640x512 framebuffer.
t_frame->width = 640;
t_frame->height = 512;

// Allocate the two framebuffers
t_frame->width = init_options.framebuffer_width;
t_frame->height = init_options.framebuffer_height;
t_frame->mask = 0;
t_frame->psm = GS_PSM_32;
t_frame->psm = init_options.framebuffer_psm;

// Allocate some vram for our framebuffer.
t_frame->address = graph_vram_allocate(t_frame->width, t_frame->height, t_frame->psm, GRAPH_ALIGN_PAGE);

t_frame++;

t_frame->width = 640;
t_frame->height = 512;
t_frame->width = init_options.framebuffer_width;
t_frame->height = init_options.framebuffer_height;
t_frame->mask = 0;
t_frame->psm = GS_PSM_32;
t_frame->psm = init_options.framebuffer_psm;

// Allocate some vram for our framebuffer.
t_frame->address = graph_vram_allocate(t_frame->width, t_frame->height, t_frame->psm, GRAPH_ALIGN_PAGE);
Expand All @@ -58,7 +59,11 @@ void init_gs(framebuffer_t* t_frame, zbuffer_t* t_z)
t_z->address = graph_vram_allocate(t_frame->width, t_frame->height, t_z->zsm, GRAPH_ALIGN_PAGE);

// Initialize the screen and tie the first framebuffer to the read circuits.
graph_initialize(t_frame->address, t_frame->width, t_frame->height, t_frame->psm, 0, 0);
graph_set_mode(init_options.interlaced, init_options.graph_mode, init_options.ffmd, init_options.flicker_filter);

graph_set_screen(0, 0, t_frame->width, t_frame->height);
graph_set_framebuffer_filtered(t_frame->address, t_frame->width, t_frame->psm, 0, 0);
graph_enable_output();
}

/** Some initialization of GS 2 */
Expand Down Expand Up @@ -105,52 +110,31 @@ void flip_buffers(framebuffer_t* t_frame)
draw_wait_finish();
}

static vif_packet_t vif_packet;

//static utils::inline_packet2<10> draw_finish_packet;

// void init_draw_finish()
// {
// // Load the draw finish program
// const auto [draw_finish_start, draw_finish_end] = vu1_programs::get_draw_finish_program_mem_address();
// vu1_programs::get_draw_finish_program_addr() = load_vu_program(draw_finish_start, draw_finish_end);

// // Set up the draw_finish packet
// draw_finish_packet.initialize(P2_TYPE_NORMAL, P2_MODE_NORMAL, 1);

// prim_t prim;
// prim.type = PRIM_TRIANGLE;
// prim.shading = PRIM_SHADE_GOURAUD;
// prim.mapping = 1;
// prim.fogging = 0;
// prim.blending = 1;
// prim.antialiasing = 0;
// prim.mapping_type = PRIM_MAP_ST;
// prim.colorfix = PRIM_UNFIXED;
} // namespace

// packet2_utils_vu_open_unpack(draw_finish_packet, 10, false);
// {
// packet2_utils_gif_add_set(draw_finish_packet, 1);
// packet2_utils_gs_add_draw_finish_giftag(draw_finish_packet);
// packet2_utils_gs_add_prim_giftag(draw_finish_packet, &prim, 0,
// ((u64)GIF_REG_RGBAQ) << 0, 1, 0);
// }
// packet2_utils_vu_close_unpack(draw_finish_packet);
namespace egg::ps2::graphics
{

// packet2_utils_vu_add_start_program(draw_finish_packet, vu1_programs::get_draw_finish_program_addr());
// packet2_utils_vu_add_end_tag(draw_finish_packet);
// }
init_options::init_options()
{
framebuffer_width = 640;
framebuffer_height = 448;

static u8 vif_packets_context = 0;
static std::array<vif_packet_t, 2> vif_packets;
framebuffer_psm = GS_PSM_32;

} // namespace
graph_mode = GRAPH_MODE_NTSC;
interlaced = GRAPH_MODE_NONINTERLACED;
ffmd = GRAPH_MODE_FRAME;
flicker_filter = GRAPH_DISABLE;

namespace egg::ps2::graphics
{
double_buffer_vu = true;
}

static u32 current_program_addr = 0;

void init()
void init(const init_options& init_options)
{
// Init DMA channels.
dma_channel_initialize(DMA_CHANNEL_GIF, NULL, 0);
Expand All @@ -160,24 +144,23 @@ void init()

current_program_addr = 0;

vu1_set_double_buffer_settings();
if (init_options.double_buffer_vu)
{
vu1_set_double_buffer_settings();
}

// Init the GS, framebuffer, zbuffer
init_gs(frame, &z);
init_gs(init_options, frame, &z);

init_drawing_environment(frame, &z);

//init_draw_finish();

// Load the kick program
const auto [kick_start, kick_end] = vu1_programs::get_kick_program_mem_address();
vu1_programs::get_kick_program_addr() = load_vu_program(kick_start, kick_end);

current_frame = frame;

vif_packets[0].initialize(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
vif_packets[1].initialize(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
vif_packets_context = 0;
vif_packet.initialize(P2_TYPE_NORMAL, P2_MODE_CHAIN, 1);
}

u32 load_vu_program(void* program_start_address, void* program_end_address)
Expand Down Expand Up @@ -236,7 +219,6 @@ void start_draw()
{
//printf("start_draw.........\n");

//flip_vip_packet_context();
packet2_reset(get_current_vif_packet(), 0);
}

Expand All @@ -263,24 +245,9 @@ void end_draw()
// *GS_REG_CSR |= 2;
}

std::array<vif_packet_t, 2>& get_vif_packets()
{
return vif_packets;
}

packet2_t* get_current_vif_packet()
{
return vif_packets[vif_packets_context];
}

u8 get_vif_packet_context()
{
return vif_packets_context;
}

void flip_vip_packet_context()
{
vif_packets_context ^= 1;
return vif_packet;
}

} // namespace egg::ps2::graphics
6 changes: 4 additions & 2 deletions dependencies/egg-ps2-graphics-lib/src/mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void draw_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh
// Define the triangle primitive we want to use.
prim.type = PRIM_TRIANGLE_STRIP;
prim.shading = PRIM_SHADE_GOURAUD;
prim.mapping = PRIM_MAP_ST;
prim.mapping = mesh.enable_texture_mapping ? DRAW_ENABLE : DRAW_DISABLE;
prim.fogging = mesh.enable_fog ? DRAW_ENABLE : DRAW_DISABLE;
prim.blending = DRAW_DISABLE;
prim.antialiasing = DRAW_DISABLE;
Expand Down Expand Up @@ -100,6 +100,8 @@ mesh_descriptor::mesh_descriptor()
// Has to be at least 3
num_verts = 0;

enable_texture_mapping = false;

// Address of the VU program loaded in memory used to perform vertex
// processing on this mesh.
//
Expand All @@ -122,7 +124,7 @@ mesh_descriptor::mesh_descriptor()
void mesh_descriptor::set_fog_start_and_end(float fog_start, float fog_end)
{
fog_offset = fog_start;
fog_scale = -256.f / std::max(0.01f, (fog_end - fog_start));
fog_scale = -256.f / (fog_end - fog_start);
}

void draw_mesh_strip(const Matrix& mesh_to_screen_matrix, const mesh_descriptor& mesh)
Expand Down
Loading

0 comments on commit a3a160d

Please sign in to comment.