From 4366d6d6fbbcab597dd41bab1eee2aa732e90b45 Mon Sep 17 00:00:00 2001 From: h4570 Date: Thu, 28 Jul 2022 20:44:21 +0200 Subject: [PATCH] some bugfixes --- .../programs/mcpip_programs_manager.hpp | 2 +- .../inc/renderer/core/paths/path1/path1.hpp | 9 +++ .../inc/renderer/core/paths/path3/path3.hpp | 2 +- .../inc/renderer/core/renderer_core_sync.hpp | 24 ++++--- .../pipeline/dynamic/core/dynpip_renderer.cpp | 4 +- .../programs/mcpip_programs_manager.cpp | 3 +- .../static/core/stapip_qbuffer_renderer.cpp | 4 +- .../core/paths/path1/draw_finish.vclpp | 30 +++++++++ .../src/renderer/core/paths/path1/path1.cpp | 62 ++++++++++++++++++- .../src/renderer/core/paths/path3/path3.cpp | 2 +- engine/src/renderer/core/renderer_core.cpp | 2 +- .../src/renderer/core/renderer_core_sync.cpp | 23 +++++-- .../core/texture/renderer_core_texture.cpp | 24 ++++--- tutorials/01-hello/src/tutorial_01.cpp | 34 +++++----- 14 files changed, 177 insertions(+), 48 deletions(-) create mode 100644 engine/src/renderer/core/paths/path1/draw_finish.vclpp diff --git a/engine/inc/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.hpp b/engine/inc/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.hpp index 6dac5c4..af19713 100644 --- a/engine/inc/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.hpp +++ b/engine/inc/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.hpp @@ -63,7 +63,7 @@ class BlockizerProgramsManager { private: McpipProgramsRepository repo; - Renderer* renderer; + RendererCore* rendererCore; McpipSingleTexBlockData singleTexBlockData; McpipMultiTexBlockData multiTexBlockData; McpipProgramName lastProgramName; diff --git a/engine/inc/renderer/core/paths/path1/path1.hpp b/engine/inc/renderer/core/paths/path1/path1.hpp index 0b74635..bcbfc3e 100644 --- a/engine/inc/renderer/core/paths/path1/path1.hpp +++ b/engine/inc/renderer/core/paths/path1/path1.hpp @@ -24,13 +24,22 @@ class Path1 { u32 uploadProgram(VU1Program* program, const u32& address); + void sendDrawFinishTag(); + + void addDrawFinishTag(packet2_t* packet); + packet2_t* createProgramsCache(VU1Program** programs, const u32& count, const u32& address); void setDoubleBuffer(const u16& startingAddress, const u16& bufferSize); private: + void uploadDrawFinishProgram(); + void prepareDrawFinishPacket(); + packet2_t* doubleBufferPacket; + packet2_t* drawFinishPacket; + u32 drawFinishAddr; }; } // namespace Tyra diff --git a/engine/inc/renderer/core/paths/path3/path3.hpp b/engine/inc/renderer/core/paths/path3/path3.hpp index aebfa05..c10a19e 100644 --- a/engine/inc/renderer/core/paths/path3/path3.hpp +++ b/engine/inc/renderer/core/paths/path3/path3.hpp @@ -27,7 +27,7 @@ class Path3 { void init(RendererSettings* settings); - void addDrawFinishTag(); + void sendDrawFinishTag(); void clearScreen(zbuffer_t* z, const Color& color); void sendTexture(Texture* texture, const RendererCoreTextureBuffers& texBuffers); diff --git a/engine/inc/renderer/core/renderer_core_sync.hpp b/engine/inc/renderer/core/renderer_core_sync.hpp index 529d332..9ef7e21 100644 --- a/engine/inc/renderer/core/renderer_core_sync.hpp +++ b/engine/inc/renderer/core/renderer_core_sync.hpp @@ -13,6 +13,7 @@ #include #include #include "./paths/path3/path3.hpp" +#include "./paths/path1/path1.hpp" namespace Tyra { @@ -29,22 +30,29 @@ class RendererCoreSync { RendererCoreSync(); ~RendererCoreSync(); - void init(Path3* path3); + void init(Path3* path3, Path1* path1); + + // --- Auto + + /** clear() -> sendPath1Req() -> waitAndClear() */ + void align3D(); + + /** clear() -> sendPath3Req() -> waitAndClear() */ + void align2D(); + + // --- Manual - /** Send draw finish tag via VU1 to GS. */ - void add(); - /** Check if finish flag was set by GS. */ u8 check(); - /** Clear finish tag flag. */ void clear(); - /** Wait for finish flag and clear it. */ void waitAndClear(); + void sendPath1Req(); + void sendPath3Req(); - /** Clear() -> Add() -> waitAndClear() */ - void align(); + void addPath1Req(packet2_t* packet); private: Path3* path3; + Path1* path1; }; } // namespace Tyra diff --git a/engine/src/renderer/3d/pipeline/dynamic/core/dynpip_renderer.cpp b/engine/src/renderer/3d/pipeline/dynamic/core/dynpip_renderer.cpp index 760290f..e841592 100644 --- a/engine/src/renderer/3d/pipeline/dynamic/core/dynpip_renderer.cpp +++ b/engine/src/renderer/3d/pipeline/dynamic/core/dynpip_renderer.cpp @@ -125,10 +125,10 @@ void DynPipRenderer::sendObjectData( packet2_utils_gs_add_lod(objectDataPacket, &rendererCore->gs.lod); if (texBuffers != nullptr) { + rendererCore->texture.updateClutBuffer(texBuffers->clut); + packet2_utils_gs_add_texbuff_clut(objectDataPacket, texBuffers->core, &rendererCore->texture.clut); - - rendererCore->texture.updateClutBuffer(texBuffers->clut); } } packet2_utils_vu_close_unpack(objectDataPacket); diff --git a/engine/src/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.cpp b/engine/src/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.cpp index 69e5632..fe2e874 100644 --- a/engine/src/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.cpp +++ b/engine/src/renderer/3d/pipeline/minecraft/programs/mcpip_programs_manager.cpp @@ -37,6 +37,7 @@ BlockizerProgramsManager::~BlockizerProgramsManager() { } void BlockizerProgramsManager::init(RendererCore* core) { + rendererCore = core; culler.init(core, &singleTexBlockData); clipper.init(core, &singleTexBlockData, &multiTexBlockData); } @@ -46,7 +47,7 @@ void BlockizerProgramsManager::setProgramsCache() { programs[0] = repo.getProgram(McpipProgramName::McPipCull); programs[1] = repo.getProgram(McpipProgramName::McPipAsIs); programsPacket = - renderer->core.getPath1()->createProgramsCache(programs, 2, 0); + rendererCore->getPath1()->createProgramsCache(programs, 2, 0); delete[] programs; } diff --git a/engine/src/renderer/3d/pipeline/static/core/stapip_qbuffer_renderer.cpp b/engine/src/renderer/3d/pipeline/static/core/stapip_qbuffer_renderer.cpp index 4c9eb45..376f369 100644 --- a/engine/src/renderer/3d/pipeline/static/core/stapip_qbuffer_renderer.cpp +++ b/engine/src/renderer/3d/pipeline/static/core/stapip_qbuffer_renderer.cpp @@ -139,10 +139,10 @@ void StaPipQBufferRenderer::sendObjectData( packet2_utils_gs_add_lod(objectDataPacket, &rendererCore->gs.lod); if (texBuffers != nullptr) { + rendererCore->texture.updateClutBuffer(texBuffers->clut); + packet2_utils_gs_add_texbuff_clut(objectDataPacket, texBuffers->core, &rendererCore->texture.clut); - - rendererCore->texture.updateClutBuffer(texBuffers->clut); } } packet2_utils_vu_close_unpack(objectDataPacket); diff --git a/engine/src/renderer/core/paths/path1/draw_finish.vclpp b/engine/src/renderer/core/paths/path1/draw_finish.vclpp new file mode 100644 index 0000000..95535a2 --- /dev/null +++ b/engine/src/renderer/core/paths/path1/draw_finish.vclpp @@ -0,0 +1,30 @@ + +; ______ ____ ___ +; | \/ ____| |___| +; | | | \ | | +;--------------------------------------------------------------- +; Copyright 2022, tyra - https://github.com/h4570/tyra +; Licenced under Apache License 2.0 +; Sandro Sobczyński +; +;--------------------------------------------------------------- +; Needed for synchronization with draw_wait_finish() +;--------------------------------------------------------------- + +.syntax new +.name VU1DrawFinish +.vu +.init_vf_all +.init_vi_all + +--enter +--endenter + +#vuprog VU1DrawFinish + xtop buffer + iaddiu kickAddress, buffer, 10 + xgkick kickAddress +#endvuprog + +--exit +--endexit \ No newline at end of file diff --git a/engine/src/renderer/core/paths/path1/path1.cpp b/engine/src/renderer/core/paths/path1/path1.cpp index 072c322..a09263b 100644 --- a/engine/src/renderer/core/paths/path1/path1.cpp +++ b/engine/src/renderer/core/paths/path1/path1.cpp @@ -10,12 +10,70 @@ #include "renderer/core/paths/path1/path1.hpp" +extern u32 VU1DrawFinish_CodeStart __attribute__((section(".vudata"))); +extern u32 VU1DrawFinish_CodeEnd __attribute__((section(".vudata"))); + namespace Tyra { Path1::Path1() { doubleBufferPacket = packet2_create(2, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + drawFinishPacket = packet2_create(10, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + uploadDrawFinishProgram(); + prepareDrawFinishPacket(); +} + +void Path1::uploadDrawFinishProgram() { + int count = (&VU1DrawFinish_CodeEnd - &VU1DrawFinish_CodeStart) / 2; + if (count & 1) count++; + + drawFinishAddr = 1000 - count; + + packet2_t* packet2 = packet2_create(10, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); + packet2_vif_add_micro_program(packet2, drawFinishAddr, + &VU1DrawFinish_CodeStart, + &VU1DrawFinish_CodeEnd); + dma_channel_wait(DMA_CHANNEL_VIF1, 0); + dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, true); + packet2_free(packet2); +} + +void Path1::addDrawFinishTag(packet2_t* packet) { + 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; + + packet2_utils_vu_open_unpack(packet, 10, true); + { + packet2_utils_gif_add_set(packet, 1); + packet2_utils_gs_add_draw_finish_giftag(packet); + packet2_utils_gs_add_prim_giftag(packet, &prim, 0, + ((u64)GIF_REG_RGBAQ) << 0, 1, 0); + } + packet2_utils_vu_close_unpack(packet); + + packet2_utils_vu_add_start_program(packet, drawFinishAddr); +} + +void Path1::sendDrawFinishTag() { + dma_channel_wait(DMA_CHANNEL_VIF1, 0); + dma_channel_send_packet2(drawFinishPacket, DMA_CHANNEL_VIF1, true); +} + +Path1::~Path1() { + packet2_free(doubleBufferPacket); + packet2_free(drawFinishPacket); +} + +void Path1::prepareDrawFinishPacket() { + addDrawFinishTag(drawFinishPacket); + packet2_utils_vu_add_end_tag(drawFinishPacket); } -Path1::~Path1() { packet2_free(doubleBufferPacket); } u32 Path1::uploadProgram(VU1Program* program, const u32& address) { // TYRA_LOG("Uploading VU1 program. Size: ", program->getProgramSize(), @@ -24,7 +82,7 @@ u32 Path1::uploadProgram(VU1Program* program, const u32& address) { auto packetSize = program->getPacketSize() + 1; // + end tag packet2_t* packet2 = - packet2_create(packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, 1); + packet2_create(packetSize, P2_TYPE_NORMAL, P2_MODE_CHAIN, true); packet2_vif_add_micro_program(packet2, address, program->getStart(), program->getEnd()); diff --git a/engine/src/renderer/core/paths/path3/path3.cpp b/engine/src/renderer/core/paths/path3/path3.cpp index 304fd86..7b3a775 100644 --- a/engine/src/renderer/core/paths/path3/path3.cpp +++ b/engine/src/renderer/core/paths/path3/path3.cpp @@ -36,7 +36,7 @@ void Path3::init(RendererSettings* t_settings) { TYRA_LOG("Path3 initialized"); } -void Path3::addDrawFinishTag() { +void Path3::sendDrawFinishTag() { dma_channel_wait(DMA_CHANNEL_GIF, 0); dma_channel_send_packet2(drawFinishPacket, DMA_CHANNEL_GIF, true); } diff --git a/engine/src/renderer/core/renderer_core.cpp b/engine/src/renderer/core/renderer_core.cpp index 4511c84..92a73cb 100644 --- a/engine/src/renderer/core/renderer_core.cpp +++ b/engine/src/renderer/core/renderer_core.cpp @@ -22,7 +22,7 @@ void RendererCore::init() { path3.init(&settings); renderer3D.init(&settings, &path1); renderer2D.init(&settings, &texture.clut); - sync.init(&path3); + sync.init(&path3, &path1); } void RendererCore::setClearScreenColor(const Color& color) { bgColor = color; } diff --git a/engine/src/renderer/core/renderer_core_sync.cpp b/engine/src/renderer/core/renderer_core_sync.cpp index b862976..640a848 100644 --- a/engine/src/renderer/core/renderer_core_sync.cpp +++ b/engine/src/renderer/core/renderer_core_sync.cpp @@ -15,15 +15,30 @@ namespace Tyra { RendererCoreSync::RendererCoreSync() {} RendererCoreSync::~RendererCoreSync() {} -void RendererCoreSync::init(Path3* t_path3) { path3 = t_path3; } +void RendererCoreSync::init(Path3* t_path3, Path1* t_path1) { + path3 = t_path3; + path1 = t_path1; +} -void RendererCoreSync::align() { +void RendererCoreSync::align3D() { clear(); - add(); + sendPath1Req(); waitAndClear(); } -void RendererCoreSync::add() { path3->addDrawFinishTag(); } +void RendererCoreSync::align2D() { + clear(); + sendPath3Req(); + waitAndClear(); +} + +void RendererCoreSync::sendPath1Req() { path1->sendDrawFinishTag(); } + +void RendererCoreSync::sendPath3Req() { path3->sendDrawFinishTag(); } + +void RendererCoreSync::addPath1Req(packet2_t* packet) { + path1->addDrawFinishTag(packet); +} u8 RendererCoreSync::check() { return *GS_REG_CSR & 2; } diff --git a/engine/src/renderer/core/texture/renderer_core_texture.cpp b/engine/src/renderer/core/texture/renderer_core_texture.cpp index 4cb7359..3d37c72 100644 --- a/engine/src/renderer/core/texture/renderer_core_texture.cpp +++ b/engine/src/renderer/core/texture/renderer_core_texture.cpp @@ -25,7 +25,7 @@ void RendererCoreTexture::init(RendererCoreGS* t_gs, Path3* t_path3) { } void RendererCoreTexture::updateClutBuffer(texbuffer_t* clutBuffer) { - if (clutBuffer == nullptr) { + if (clutBuffer == nullptr || clutBuffer->width == 0) { clut.psm = 0; clut.load_method = CLUT_NO_LOAD; clut.address = 0; @@ -44,15 +44,23 @@ RendererCoreTextureBuffers RendererCoreTexture::useTexture(Texture* t_tex) { auto allocated = getAllocatedBuffersByTextureId(t_tex->getId()); if (allocated.id != 0) return allocated; - while ((t_tex->getSizeInMB()) > sender.getFreeVRamInMB()) { - // auto idToDealloc = - // cacheManager.getTextureIdToDealloc(currentAllocations); - auto idToDealloc = currentAllocations.back().id; - auto buffToDealloc = getAllocatedBuffersByTextureId(idToDealloc); - sender.deallocate(buffToDealloc); - unregisterAllocation(idToDealloc); + // TODO: Dont like this..., but it works for now. + if (t_tex->getSizeInMB() > sender.getFreeVRamInMB()) { + for (int i = currentAllocations.size() - 1; i >= 0; i--) { + sender.deallocate(currentAllocations[i]); + } + currentAllocations.clear(); } + // while (t_tex->getSizeInMB() > sender.getFreeVRamInMB()) { + // auto idToDealloc = + // cacheManager.getTextureIdToDealloc(currentAllocations); + // auto buffToDealloc = getAllocatedBuffersByTextureId(idToDealloc); + // TYRA_LOG("Deallocate(", buffToDealloc.id, ")"); + // sender.deallocate(buffToDealloc); + // unregisterAllocation(idToDealloc); + // } + auto newTexBuffer = sender.allocate(t_tex); path3->sendTexture(t_tex, newTexBuffer); registerAllocation(newTexBuffer); diff --git a/tutorials/01-hello/src/tutorial_01.cpp b/tutorials/01-hello/src/tutorial_01.cpp index bddafa8..43f2066 100644 --- a/tutorials/01-hello/src/tutorial_01.cpp +++ b/tutorials/01-hello/src/tutorial_01.cpp @@ -92,7 +92,7 @@ void Tutorial01 ::init() { dynpip.setRenderer(&engine->renderer.core); stapip.setRenderer(&engine->renderer.core); - picturesCount = 1; + picturesCount = 3; pictures = new Sprite*[picturesCount]; for (u32 i = 0; i < picturesCount; i++) pictures[i] = get2DPicture(&engine->renderer); @@ -159,23 +159,23 @@ void Tutorial01 ::loop() { for (u32 i = 0; i < picturesCount; i++) engine->renderer.renderer2D.render(pictures[i]); - // engine->renderer.renderer3D.usePipeline(&stapip); - // { - // stapip.render(staticMesh, staOptions); - // stapip.render(skybox, skyboxOptions); - // } + engine->renderer.renderer3D.usePipeline(&stapip); + { + stapip.render(staticMesh, staOptions); + stapip.render(skybox, skyboxOptions); + } - // engine->renderer.renderer3D.usePipeline(&dynpip); - // { - // dynpip.render(cube); - // Threading::switchThread(); - // for (u8 i = 0; i < warriorsCount; i++) { - // dynpip.render(warriors[i], dynOptions); - // if (i == 5) Threading::switchThread(); - // if (i == 10) Threading::switchThread(); - // if (i == 15) Threading::switchThread(); - // } - // } + engine->renderer.renderer3D.usePipeline(&dynpip); + { + dynpip.render(cube); + Threading::switchThread(); + for (u8 i = 0; i < warriorsCount; i++) { + dynpip.render(warriors[i], dynOptions); + if (i == 5) Threading::switchThread(); + if (i == 10) Threading::switchThread(); + if (i == 15) Threading::switchThread(); + } + } engine->renderer.renderer3D.usePipeline(&mcPip); { mcPip.render(blocks, blocksCount, blocksTex2, true); }