Skip to content

Commit

Permalink
Make the initial 3D renderer configurable via NDSArgs (melonDS-emu#…
Browse files Browse the repository at this point in the history
…1913)

* Allow 3D renderers to be created without passing `GPU` to the constructor

* Make the initial 3D renderer configurable via `NDSArgs`

* Fix a compiler error
  • Loading branch information
JesseTG authored and kvnp committed Dec 25, 2023
1 parent 5b6b6c7 commit 5a6eb67
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 247 deletions.
6 changes: 6 additions & 0 deletions src/Args.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "DSi_NAND.h"
#include "FATStorage.h"
#include "FreeBIOS.h"
#include "GPU3D_Soft.h"
#include "SPI_Firmware.h"
#include "SPU.h"

Expand Down Expand Up @@ -118,6 +119,11 @@ struct NDSArgs
/// Defaults to disabled.
/// Ignored in builds that don't have the GDB stub included.
std::optional<GDBArgs> GDB = std::nullopt;

/// The 3D renderer to initialize the DS with.
/// Defaults to the software renderer.
/// Can be changed later at any time.
std::unique_ptr<melonDS::Renderer3D> Renderer3D = std::make_unique<SoftRenderer>();
};

/// Arguments to pass into the DSi constructor.
Expand Down
28 changes: 14 additions & 14 deletions src/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GPU::GPU(melonDS::NDS& nds, std::unique_ptr<Renderer3D>&& renderer3d, std::uniqu
NDS(nds),
GPU2D_A(0, *this),
GPU2D_B(1, *this),
GPU3D(nds, renderer3d ? std::move(renderer3d) : std::make_unique<SoftRenderer>(*this)),
GPU3D(nds, renderer3d ? std::move(renderer3d) : std::make_unique<SoftRenderer>()),
GPU2D_Renderer(renderer2d ? std::move(renderer2d) : std::make_unique<GPU2D::SoftRenderer>(*this))
{
NDS.RegisterEventFunc(Event_LCD, LCD_StartHBlank, MemberEventFunc(GPU, StartHBlank));
Expand Down Expand Up @@ -209,7 +209,7 @@ void GPU::Stop() noexcept
memset(Framebuffer[1][0].get(), 0, fbsize*4);
memset(Framebuffer[1][1].get(), 0, fbsize*4);

GPU3D.Stop();
GPU3D.Stop(*this);
}

void GPU::DoSavestate(Savestate* file) noexcept
Expand Down Expand Up @@ -294,7 +294,7 @@ void GPU::AssignFramebuffers() noexcept
void GPU::SetRenderer3D(std::unique_ptr<Renderer3D>&& renderer) noexcept
{
if (renderer == nullptr)
GPU3D.SetCurrentRenderer(std::make_unique<SoftRenderer>(*this));
GPU3D.SetCurrentRenderer(std::make_unique<SoftRenderer>());
else
GPU3D.SetCurrentRenderer(std::move(renderer));

Expand Down Expand Up @@ -899,7 +899,7 @@ void GPU::StartHBlank(u32 line) noexcept
}
else if (VCount == 215)
{
GPU3D.VCount215();
GPU3D.VCount215(*this);
}
else if (VCount == 262)
{
Expand All @@ -925,7 +925,7 @@ void GPU::FinishFrame(u32 lines) noexcept

if (GPU3D.AbortFrame)
{
GPU3D.RestartFrame();
GPU3D.RestartFrame(*this);
GPU3D.AbortFrame = false;
}
}
Expand Down Expand Up @@ -1018,7 +1018,7 @@ void GPU::StartScanline(u32 line) noexcept
// texture memory anyway and only update it before the start
//of the next frame.
// So we can give the rasteriser a bit more headroom
GPU3D.VCount144();
GPU3D.VCount144(*this);

// VBlank
DispStat[0] |= (1<<0);
Expand All @@ -1038,7 +1038,7 @@ void GPU::StartScanline(u32 line) noexcept

// Need a better way to identify the openGL renderer in particular
if (GPU3D.IsRendererAccelerated())
GPU3D.Blit();
GPU3D.Blit(*this);
}
}

Expand Down Expand Up @@ -1068,7 +1068,7 @@ void GPU::SetVCount(u16 val) noexcept
}

template <u32 Size, u32 MappingGranularity>
NonStupidBitField<Size/VRAMDirtyGranularity> VRAMTrackingSet<Size, MappingGranularity>::DeriveState(u32* currentMappings, GPU& gpu)
NonStupidBitField<Size/VRAMDirtyGranularity> VRAMTrackingSet<Size, MappingGranularity>::DeriveState(const u32* currentMappings, GPU& gpu)
{
NonStupidBitField<Size/VRAMDirtyGranularity> result;
u16 banksToBeZeroed = 0;
Expand Down Expand Up @@ -1131,12 +1131,12 @@ NonStupidBitField<Size/VRAMDirtyGranularity> VRAMTrackingSet<Size, MappingGranul
return result;
}

template NonStupidBitField<32*1024/VRAMDirtyGranularity> VRAMTrackingSet<32*1024, 8*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<8*1024/VRAMDirtyGranularity> VRAMTrackingSet<8*1024, 8*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<512*1024/VRAMDirtyGranularity> VRAMTrackingSet<512*1024, 128*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<128*1024/VRAMDirtyGranularity> VRAMTrackingSet<128*1024, 16*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<256*1024/VRAMDirtyGranularity> VRAMTrackingSet<256*1024, 16*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<512*1024/VRAMDirtyGranularity> VRAMTrackingSet<512*1024, 16*1024>::DeriveState(u32*, GPU& gpu);
template NonStupidBitField<32*1024/VRAMDirtyGranularity> VRAMTrackingSet<32*1024, 8*1024>::DeriveState(const u32*, GPU& gpu);
template NonStupidBitField<8*1024/VRAMDirtyGranularity> VRAMTrackingSet<8*1024, 8*1024>::DeriveState(const u32*, GPU& gpu);
template NonStupidBitField<512*1024/VRAMDirtyGranularity> VRAMTrackingSet<512*1024, 128*1024>::DeriveState(const u32*, GPU& gpu);
template NonStupidBitField<128*1024/VRAMDirtyGranularity> VRAMTrackingSet<128*1024, 16*1024>::DeriveState(const u32*, GPU& gpu);
template NonStupidBitField<256*1024/VRAMDirtyGranularity> VRAMTrackingSet<256*1024, 16*1024>::DeriveState(const u32*, GPU& gpu);
template NonStupidBitField<512*1024/VRAMDirtyGranularity> VRAMTrackingSet<512*1024, 16*1024>::DeriveState(const u32*, GPU& gpu);



Expand Down
2 changes: 1 addition & 1 deletion src/GPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct VRAMTrackingSet
Mapping[i] = 0x8000;
}
}
NonStupidBitField<Size/VRAMDirtyGranularity> DeriveState(u32* currentMappings, GPU& gpu);
NonStupidBitField<Size/VRAMDirtyGranularity> DeriveState(const u32* currentMappings, GPU& gpu);
};

class GPU
Expand Down
22 changes: 11 additions & 11 deletions src/GPU3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void MatrixLoadIdentity(s32* m);

GPU3D::GPU3D(melonDS::NDS& nds, std::unique_ptr<Renderer3D>&& renderer) noexcept :
NDS(nds),
CurrentRenderer(renderer ? std::move(renderer) : std::make_unique<SoftRenderer>(nds.GPU))
CurrentRenderer(renderer ? std::move(renderer) : std::make_unique<SoftRenderer>())
{
}

Expand Down Expand Up @@ -2367,20 +2367,20 @@ void GPU3D::CheckFIFODMA() noexcept
NDS.CheckDMAs(0, 0x07);
}

void GPU3D::VCount144() noexcept
void GPU3D::VCount144(GPU& gpu) noexcept
{
CurrentRenderer->VCount144();
CurrentRenderer->VCount144(gpu);
}

void GPU3D::RestartFrame() noexcept
void GPU3D::RestartFrame(GPU& gpu) noexcept
{
CurrentRenderer->RestartFrame();
CurrentRenderer->RestartFrame(gpu);
}

void GPU3D::Stop() noexcept
void GPU3D::Stop(const GPU& gpu) noexcept
{
if (CurrentRenderer)
CurrentRenderer->Stop();
CurrentRenderer->Stop(gpu);
}


Expand Down Expand Up @@ -2473,9 +2473,9 @@ void GPU3D::VBlank() noexcept
}
}

void GPU3D::VCount215() noexcept
void GPU3D::VCount215(GPU& gpu) noexcept
{
CurrentRenderer->RenderFrame();
CurrentRenderer->RenderFrame(gpu);
}

void GPU3D::SetRenderXPos(u16 xpos) noexcept
Expand Down Expand Up @@ -2935,10 +2935,10 @@ void GPU3D::Write32(u32 addr, u32 val) noexcept
Log(LogLevel::Debug, "unknown GPU3D write32 %08X %08X\n", addr, val);
}

void GPU3D::Blit() noexcept
void GPU3D::Blit(const GPU& gpu) noexcept
{
if (CurrentRenderer)
CurrentRenderer->Blit();
CurrentRenderer->Blit(gpu);
}

Renderer3D::Renderer3D(bool Accelerated)
Expand Down
22 changes: 11 additions & 11 deletions src/GPU3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class GPU3D
void CheckFIFOIRQ() noexcept;
void CheckFIFODMA() noexcept;

void VCount144() noexcept;
void VCount144(GPU& gpu) noexcept;
void VBlank() noexcept;
void VCount215() noexcept;
void VCount215(GPU& gpu) noexcept;

void RestartFrame() noexcept;
void Stop() noexcept;
void RestartFrame(GPU& gpu) noexcept;
void Stop(const GPU& gpu) noexcept;

void SetRenderXPos(u16 xpos) noexcept;
[[nodiscard]] u16 GetRenderXPos() const noexcept { return RenderXPos; }
Expand All @@ -125,7 +125,7 @@ class GPU3D
void Write8(u32 addr, u8 val) noexcept;
void Write16(u32 addr, u16 val) noexcept;
void Write32(u32 addr, u32 val) noexcept;
void Blit() noexcept;
void Blit(const GPU& gpu) noexcept;
private:
melonDS::NDS& NDS;
typedef union
Expand Down Expand Up @@ -334,19 +334,19 @@ class Renderer3D
Renderer3D(const Renderer3D&) = delete;
Renderer3D& operator=(const Renderer3D&) = delete;

virtual void Reset() = 0;
virtual void Reset(GPU& gpu) = 0;

// This "Accelerated" flag currently communicates if the framebuffer should
// be allocated differently and other little misc handlers. Ideally there
// are more detailed "traits" that we can ask of the Renderer3D type
const bool Accelerated;

virtual void VCount144() {};
virtual void Stop() {}
virtual void RenderFrame() = 0;
virtual void RestartFrame() {};
virtual void VCount144(GPU& gpu) {};
virtual void Stop(const GPU& gpu) {}
virtual void RenderFrame(GPU& gpu) = 0;
virtual void RestartFrame(GPU& gpu) {};
virtual u32* GetLine(int line) = 0;
virtual void Blit() {};
virtual void Blit(const GPU& gpu) {};
virtual void PrepareCaptureFrame() {}
protected:
Renderer3D(bool Accelerated);
Expand Down
Loading

0 comments on commit 5a6eb67

Please sign in to comment.