diff --git a/include/engine.hpp b/include/engine.hpp index 0ec5934..f98bfab 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -5,7 +5,7 @@ class Tickable; namespace Engine { -void init(); +void init(int argc, char* argv[]); void run(); /// @brief Returns the current frame number. This is incremented after ticking. The first frame is frame 0. diff --git a/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp b/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp index 6d3b63f..141dd6f 100644 --- a/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp +++ b/include/renderer/ps2gl_renderers/vertex_color_renderer.hpp @@ -5,13 +5,15 @@ #include "ps2gl/linear_renderer.h" #include "ps2gl/renderer.h" -#define kVCRPrimType (((tU32)1 << 31) | 1) -#define kVCRPrimTypeFlag ((tU64)1 << 32) +static constexpr u32 kVCRPrimType = (((tU32)1 << 31) | GL_TRIANGLE_STRIP); +static constexpr u64 kVCRPrimTypeFlag((tU64)1 << 32); -class VertexColorRenderer: public CLinearRenderer +class CVertexColorRenderer: public CLinearRenderer { public: - VertexColorRenderer(); + CVertexColorRenderer(); - static VertexColorRenderer* Register(); + static CVertexColorRenderer* Register(); + //virtual void InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged) override; + virtual void DrawLinearArrays(CGeometryBlock& block) override; }; \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index ba5963a..00466e6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -65,7 +65,7 @@ all: $(ISO_TGT) run: $(EE_BIN) cd $(dir $(EE_BIN)) - ps2client -h $(PS2HOST) execee host:$(notdir $(EE_BIN)) + ps2client -h $(PS2HOST) execee host:$(notdir $(EE_BIN)) --filesystem=host reset: ps2client -h $(PS2HOST) reset \ No newline at end of file diff --git a/src/engine.cc b/src/engine.cc index 1e64327..7a3b207 100644 --- a/src/engine.cc +++ b/src/engine.cc @@ -33,9 +33,19 @@ static float game_time = 0.f; static float tickrate = 1.f / 59.93f; // ntsc default static u32 frameCounter = 0; -void init() +void init(int argc, char* argv[]) { - Filesystem::set_filesystem_type(Filesystem::Type::cdrom); + // TODO: figure out why this don't work + // if (argc == 2 && strcmp(argv[1], "--filesystem=host") == 0) + // { + // printf("Using host filesystem type\n"); + // Filesystem::set_filesystem_type(Filesystem::Type::host); + // } + // else + { + printf("Using cdrom filesystem type\n"); + Filesystem::set_filesystem_type(Filesystem::Type::cdrom); + } if (Filesystem::get_filesystem_type() != Filesystem::Type::host) { diff --git a/src/main.cc b/src/main.cc index ed2b4ed..cdabcbf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -5,7 +5,7 @@ int main(int argc, char* argv[]) { - Engine::init(); + Engine::init(argc, argv); Engine::run(); diff --git a/src/renderer/gs.cc b/src/renderer/gs.cc index 6e0850e..2dcb2f6 100644 --- a/src/renderer/gs.cc +++ b/src/renderer/gs.cc @@ -110,7 +110,7 @@ static void init_renderer() init_lights(); - VertexColorRenderer::Register(); + CVertexColorRenderer::Register(); for (Renderable::TIterator Itr = Renderable::Itr(); Itr; ++Itr) { diff --git a/src/renderer/mesh.cc b/src/renderer/mesh.cc index 0e80341..656dc46 100644 --- a/src/renderer/mesh.cc +++ b/src/renderer/mesh.cc @@ -76,6 +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(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 index 1c8322b..0e7b5c4 100644 --- a/src/renderer/ps2gl_renderers/vertex_color_renderer.cc +++ b/src/renderer/ps2gl_renderers/vertex_color_renderer.cc @@ -1,6 +1,17 @@ #include "renderer/ps2gl_renderers/vertex_color_renderer.hpp" #include "vu1_mem_linear.h" +#include "ps2gl/drawcontext.h" +#include "ps2gl/glcontext.h" +#include "ps2gl/immgmanager.h" +#include "ps2gl/lighting.h" +#include "ps2gl/material.h" +#include "ps2gl/matrix.h" +#include "ps2gl/metrics.h" +#include "ps2gl/texture.h" + +#include "ps2gl/renderer.h" + #define VU_FUNCTIONS(name) \ void vsm##name##_CodeStart(); \ void vsm##name##_CodeEnd() @@ -12,21 +23,40 @@ extern "C" { VU_FUNCTIONS(VertexColorRenderer); } -VertexColorRenderer::VertexColorRenderer() +using namespace RendererProps; + +static constexpr CRendererProps capabilities = { + PrimType : kPtsLinesStripsFans, + Lighting : 1, + NumDirLights : k3DirLights | k8DirLights, + NumPtLights : k1PtLight | k2PtLights | k8PtLights, + Texture : 1, + Specular : 1, + PerVtxMaterial : kDiffuse, + Clipping : kNonClipped | kClipped, + CullFace : 1, + TwoSidedLighting : 0, + ArrayAccess : kLinear +}; + +static constexpr CRendererProps no_reqs = static_cast(0LL); +static_assert(sizeof(0LL) == sizeof(CRendererProps)); + +CVertexColorRenderer::CVertexColorRenderer() : CLinearRenderer(mVsmAddr(VertexColorRenderer), mVsmSize(VertexColorRenderer), 4, 3, kInputStart, kInputBufSize - kInputStart, "Vertex color renderer") { - Requirements = 0; // no requirements - Capabilities = kVCRPrimTypeFlag; // provides the "kVCRPrimType" capability + Requirements = 0; + Capabilities = kVCRPrimTypeFlag | capabilities; } -VertexColorRenderer* VertexColorRenderer::Register() +CVertexColorRenderer* CVertexColorRenderer::Register() { // create a renderer and register it - VertexColorRenderer* renderer = new VertexColorRenderer; + CVertexColorRenderer* renderer = new CVertexColorRenderer; pglRegisterRenderer(renderer); // register the prim type @@ -34,7 +64,246 @@ VertexColorRenderer* VertexColorRenderer::Register() 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 + false); // ok to merge multiple calls when possible return renderer; +} + +// void CVertexColorRenderer::InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged) +// { +// //printf("CVertexColorRenderer::InitContext\n\n"); +// CGLContext& glContext = *pGLContext; +// CVifSCDmaPacket& packet = glContext.GetVif1Packet(); + +// packet.Cnt(); +// { +// //AddVu1RendererContext(packet, primType, kContextStart); +// CGLContext& glContext = *pGLContext; + +// packet.Stcycl(1, 1); +// packet.Flush(); +// packet.Pad96(); +// packet.OpenUnpack(Vifs::UnpackModes::v4_32, kContextStart, Packet::kSingleBuff); +// { +// // find light pointers +// CImmLighting& lighting = glContext.GetImmLighting(); +// tLightPtrs lightPtrs[8]; +// tLightPtrs *nextDir, *nextPt, *nextSpot; +// nextDir = nextPt = nextSpot = &lightPtrs[0]; +// int numDirs, numPts, numSpots; +// numDirs = numPts = numSpots = 0; +// for (int i = 0; i < 8; i++) +// { +// CImmLight& light = lighting.GetImmLight(i); +// if (light.IsEnabled()) +// { +// int lightBase = kLight0Base + kContextStart; +// if (light.IsDirectional()) +// { +// nextDir->dir = lightBase + i * kLightStructSize; +// nextDir++; +// numDirs++; +// } +// else if (light.IsPoint()) +// { +// nextPt->point = lightBase + i * kLightStructSize; +// nextPt++; +// numPts++; +// } +// else if (light.IsSpot()) +// { +// nextSpot->spot = lightBase + i * kLightStructSize; +// nextSpot++; +// numSpots++; +// } +// } +// } + +// bool doLighting = glContext.GetImmLighting().GetLightingEnabled(); + +// // transpose of object to world space xfrm (for light directions) +// cpu_mat_44 objToWorldXfrmTrans = glContext.GetModelViewStack().GetTop(); +// // clear any translations.. should be doing a 3x3 transpose.. +// objToWorldXfrmTrans.set_col3(cpu_vec_xyzw(0, 0, 0, 1)); +// objToWorldXfrmTrans = objToWorldXfrmTrans.transpose(); +// // do we need to rescale normals? +// cpu_mat_44 normalRescale; +// normalRescale.set_identity(); +// float normalScale = 1.0f; +// CImmDrawContext& drawContext = glContext.GetImmDrawContext(); +// if (drawContext.GetRescaleNormals()) +// { +// cpu_vec_xyzw fake_normal(1, 0, 0, 0); +// fake_normal = objToWorldXfrmTrans * fake_normal; +// normalScale = 1.0f / fake_normal.length(); +// normalRescale.set_scale(cpu_vec_xyz(normalScale, normalScale, normalScale)); +// } +// objToWorldXfrmTrans = normalRescale * objToWorldXfrmTrans; + +// // num lights +// if (doLighting) +// { +// packet += numDirs; +// packet += numPts; +// packet += numSpots; +// } +// else +// { +// packet += (tU64)0; +// packet += 0; +// } + +// // backface culling multiplier -- this is 1.0f or -1.0f, the 6th bit +// // also turns on/off culling +// float bfc_mult = (float)drawContext.GetCullFaceDir(); +// unsigned int bfc_word; +// asm(" ## nop ## " +// : "=r"(bfc_word) +// : "0"(bfc_mult)); +// bool do_culling = drawContext.GetDoCullFace() && (primType > GL_LINE_STRIP); +// packet += bfc_word | (unsigned int)do_culling << 5; + +// // light pointers +// packet.Add(&lightPtrs[0], 8); + +// float maxColorValue = GetMaxColorValue(glContext.GetTexManager().GetTexEnabled()); + +// // add light info +// for (int i = 0; i < 8; i++) +// { +// CImmLight& light = lighting.GetImmLight(i); +// packet += light.GetAmbient() * maxColorValue; +// packet += light.GetDiffuse() * maxColorValue; +// packet += light.GetSpecular() * maxColorValue; + +// if (light.IsDirectional()) +// packet += light.GetPosition(); +// else +// { +// packet += light.GetPosition(); +// } + +// packet += light.GetSpotDir(); + +// // attenuation coeffs for positional light sources +// // because we're doing lighting calculations in object space, +// // we need to adjust the attenuation of positional light sources +// // and all lighting directions to take into account scaling +// packet += light.GetConstantAtten(); +// packet += light.GetLinearAtten() * 1.0f / normalScale; +// packet += light.GetQuadAtten() * 1.0f / normalScale; +// packet += 0; // padding +// } + +// // global ambient +// cpu_vec_4 globalAmb; +// if (doLighting) +// globalAmb = lighting.GetGlobalAmbient() * maxColorValue; +// else +// globalAmb = cpu_vec_4(0, 0, 0, 0); +// packet.Add((tU32*)&globalAmb, 3); + +// // stick in the offset to convert clip space depth value to GS +// float depthClipToGs = (float)((1 << drawContext.GetDepthBits()) - 1) / 2.0f; +// packet += depthClipToGs; + +// // cur material + +// CImmMaterial& material = glContext.GetMaterialManager().GetImmMaterial(); + +// // add emissive component +// cpu_vec_4 emission; +// if (doLighting) +// emission = material.GetEmission() * maxColorValue; +// else +// emission = glContext.GetMaterialManager().GetCurColor() * maxColorValue; +// packet += emission; + +// // ambient +// packet += material.GetAmbient(); + +// // diffuse +// cpu_vec_4 matDiffuse = material.GetDiffuse(); +// // the alpha value is set to the alpha of the diffuse in the renderers; +// // this should be the current color alpha if lighting is disabled +// if (!doLighting) +// matDiffuse[3] = glContext.GetMaterialManager().GetCurColor()[3]; +// packet += matDiffuse; + +// // specular +// packet += material.GetSpecular(); + +// // vertex xform +// packet += drawContext.GetVertexXform(); + +// // fixed vertToEye vector for non-local specular +// cpu_vec_xyzw vertToEye(0.0f, 0.0f, 1.0f, 0.0f); +// packet += objToWorldXfrmTrans * vertToEye; + +// // transpose of object to world space transform +// packet += objToWorldXfrmTrans; + +// // world to object space xfrm (for light positions) +// cpu_mat_44 worldToObjXfrm = glContext.GetModelViewStack().GetInvTop(); +// packet += worldToObjXfrm; + +// // giftag - this is down at the bottom to make sure that when switching +// // primitives the last buffer will have a chance to copy the giftag before +// // it is overwritten with the new one +// GLenum newPrimType = drawContext.GetPolygonMode(); +// if (newPrimType == GL_FILL) +// newPrimType = primType; +// newPrimType &= 0xff; +// tGifTag giftag = BuildGiftag(GL_TRIANGLE_STRIP); +// packet += giftag; + +// // add info used by clipping code +// // first the dimensions of the framebuffer +// float xClip = (float)2048.0f / (drawContext.GetFBWidth() * 0.5f * 2.0f); +// packet += std::max(xClip, 1.0f); +// float yClip = (float)2048.0f / (drawContext.GetFBHeight() * 0.5f * 2.0f); +// packet += std::max(yClip, 1.0f); +// float depthClip = 2048.0f / depthClipToGs; +// // FIXME: maybe these 2048's should be 2047.5s... +// depthClip *= 1.003f; // round up a bit for fp error (????) +// packet += depthClip; +// // enable/disable clipping +// packet += (drawContext.GetDoClipping()) ? 1 : 0; +// } +// packet.CloseUnpack(); + + +// packet.Mscal(0); +// packet.Flushe(); + +// packet.Base(kDoubleBufBase); +// packet.Offset(kDoubleBufOffset); +// } +// packet.CloseTag(); + +// CacheRendererState(); +// } + +void CVertexColorRenderer::DrawLinearArrays(CGeometryBlock& block) +{ + printf("Drawing linear arrays!\n"); + + int wordsPerVert = block.GetWordsPerVertex(); + int wordsPerNormal = (block.GetNormalsAreValid()) ? block.GetWordsPerNormal() : 0; + int wordsPerTex = (block.GetTexCoordsAreValid()) ? block.GetWordsPerTexCoord() : 0; + int wordsPerColor = (block.GetColorsAreValid()) ? block.GetWordsPerColor() : 0; + + printf("wordsPerVert: %d\n", wordsPerVert); + printf("wordsPerNormal: %d\n", wordsPerNormal); + printf("wordsPerTex: %d\n", wordsPerTex); + printf("wordsPerColor: %d\n", wordsPerColor); + + int numStrips = block.GetNumStrips(); + + printf("numStrips: %d\n", numStrips); + + printf("InputQuadsPerVert: %d\n", InputQuadsPerVert); + printf("OutputQuadsPerVert: %d\n", OutputQuadsPerVert); + + CLinearRenderer::DrawLinearArrays(block); } \ No newline at end of file diff --git a/vu1/vertex_color_renderer.vcl b/vu1/vertex_color_renderer.vcl index f7838f1..5bb3bfc 100644 --- a/vu1/vertex_color_renderer.vcl +++ b/vu1/vertex_color_renderer.vcl @@ -86,113 +86,6 @@ xform_loop_lid: --LoopCS 1,3 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! -------------------------------