melonDS/src/GPU2D.h

154 lines
3.2 KiB
C
Raw Normal View History

/*
2024-06-15 09:01:19 -06:00
Copyright 2016-2024 melonDS team
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef GPU2D_H
#define GPU2D_H
2020-12-06 09:40:16 -07:00
#include "types.h"
#include "Savestate.h"
namespace melonDS
{
class GPU;
2021-02-27 14:25:27 -07:00
namespace GPU2D
{
class Unit
{
2017-01-17 20:03:19 -07:00
public:
// take a reference to the GPU so we can access its state
// and ensure that it's not null
Unit(u32 num, melonDS::GPU& gpu);
Clean up the 3D renderer for enhanced flexibility (#1895) * Give `GPU2D::Unit` a virtual destructor - Undefined behavior avoided! * Add an `array2d` alias * Move various parts of `GPU2D::SoftRenderer` to `constexpr` - `SoftRenderer::MosaicTable` is now initialized at compile-time - Most of the `SoftRenderer::Color*` functions are now `constexpr` - The aforementioned functions are used with a constant value in at least one place, so they'll be at least partially computed at compile-time * Generalize `GLRenderer::PrepareCaptureFrame` - Declare it in the base `Renderer3D` class, but make it empty * Remove unneeded `virtual` specifiers * Store `Framebuffer`'s memory in `unique_ptr`s - Reduce the risk of leaks this way * Clean up how `GLCompositor` is initialized - Return it as an `std::optional` instead of a `std::unique_ptr` - Make `GLCompositor` movable - Replace `GLCompositor`'s plain arrays with `std::array` to simplify moving * Pass `GPU` to `GLCompositor`'s important functions instead of passing it to the constructor * Move `GLCompositor` to be a field within `GLRenderer` - Some methods were moved up and made `virtual` * Fix some linker errors * Set the renderer in the frontend * Remove unneeded `virtual` specifiers * Remove `RenderSettings` in favor of just exposing the relevant member variables * Update the frontend to accommodate the core changes * Add `constexpr` and `const` to places in the interpolator * Qualify references to `size_t` * Construct the `optional` directly instead of using `make_optional` - It makes the Linux build choke - I think it's because `GLCompositor`'s constructor is `private`
2023-11-29 07:23:11 -07:00
virtual ~Unit() = default;
2021-02-27 14:25:27 -07:00
Unit(const Unit&) = delete;
Unit& operator=(const Unit&) = delete;
Allow for a more modular renderer backends (#990) * Draft GPU3D renderer modularization * Update sources C++ standard to C++17 The top-level `CMakeLists.txt` is already using the C++17 standard. * Move GLCompositor into class type Some other misc fixes to push towards better modularity * Make renderer-implementation types move-only These types are going to be holding onto handles of GPU-side resources and shouldn't ever be copied around. * Fix OSX: Remove 'register' storage class specifier `register` has been removed in C++17... But this keyword hasn't done anything in years anyways. OSX builds consider this "warning" an error and it stops the whole build. * Add RestartFrame to Renderer3D interface * Move Accelerated property to Renderer3D interface There are points in the code base where we do: `renderer != 0` to know if we are feeding an openGL renderer. Rather than that we can instead just have this be a property of the renderer itself. With this pattern a renderer can just say how it wants its data to come in rather than have everyone know that they're talking to an OpenGL renderer. * Remove Accelerated flag from GPU * Move 2D_Soft interface in separate header Also make the current 2D engine an "owned" unique_ptr. * Update alignment attribute to standard alignas Uses standardized `alignas` rather than compiler-specific attributes. https://en.cppreference.com/w/cpp/language/alignas * Fix Clang: alignas specifier Alignment must be specified before the array to align the entire array. https://en.cppreference.com/w/cpp/language/alignas * Converted Renderer3D Accelerated to variable This flag is checked a lot during scanline rasterization. So rather than having an expensive vtable-lookup call during mainline rendering code, it is now a public constant bool type that is written to only once during Renderer3D initialization.
2021-02-09 15:38:51 -07:00
2017-01-17 20:03:19 -07:00
void Reset();
2018-10-17 18:31:01 -06:00
void DoSavestate(Savestate* file);
void SetEnabled(bool enable) { Enabled = enable; }
2017-01-17 20:03:19 -07:00
u8 Read8(u32 addr);
u16 Read16(u32 addr);
u32 Read32(u32 addr);
void Write8(u32 addr, u8 val);
void Write16(u32 addr, u16 val);
void Write32(u32 addr, u32 val);
bool UsesFIFO() const
2017-06-26 03:02:10 -06:00
{
if (((DispCnt >> 16) & 0x3) == 3)
return true;
if ((CaptureCnt & (1<<25)) && ((CaptureCnt >> 29) & 0x3) != 0)
return true;
return false;
}
void SampleFIFO(u32 offset, u32 num);
void VBlank();
2020-12-06 09:40:16 -07:00
virtual void VBlankEnd();
2016-12-06 09:32:51 -07:00
2017-04-08 19:35:32 -06:00
void CheckWindows(u32 line);
u16* GetBGExtPal(u32 slot, u32 pal);
u16* GetOBJExtPal();
void GetBGVRAM(u8*& data, u32& mask) const;
void GetOBJVRAM(u8*& data, u32& mask) const;
2020-12-06 09:40:16 -07:00
2021-02-27 14:25:27 -07:00
void UpdateMosaicCounters(u32 line);
void CalculateWindowMask(u32 line, u8* windowMask, const u8* objWindow);
2021-02-27 14:25:27 -07:00
2017-01-17 20:03:19 -07:00
u32 Num;
bool Enabled;
2017-06-26 03:02:10 -06:00
u16 DispFIFO[16];
u32 DispFIFOReadPtr;
u32 DispFIFOWritePtr;
2017-03-20 15:18:35 -06:00
u16 DispFIFOBuffer[256];
u32 DispCnt;
u16 BGCnt[4];
2017-01-20 07:27:56 -07:00
u16 BGXPos[4];
u16 BGYPos[4];
s32 BGXRef[2];
s32 BGYRef[2];
s32 BGXRefInternal[2];
s32 BGYRefInternal[2];
s16 BGRotA[2];
s16 BGRotB[2];
s16 BGRotC[2];
s16 BGRotD[2];
2017-04-08 19:35:32 -06:00
u8 Win0Coords[4];
u8 Win1Coords[4];
u8 WinCnt[4];
u32 Win0Active;
u32 Win1Active;
2017-04-08 19:35:32 -06:00
2017-07-23 07:31:09 -06:00
u8 BGMosaicSize[2];
u8 OBJMosaicSize[2];
u8 BGMosaicY, BGMosaicYMax;
u8 OBJMosaicYCount, OBJMosaicY, OBJMosaicYMax;
u16 BlendCnt;
u16 BlendAlpha;
u8 EVA, EVB;
u8 EVY;
bool CaptureLatch;
u32 CaptureCnt;
2017-03-01 12:23:41 -07:00
u16 MasterBrightness;
private:
melonDS::GPU& GPU;
2021-02-27 14:25:27 -07:00
};
2017-03-01 12:23:41 -07:00
2021-02-27 14:25:27 -07:00
class Renderer2D
{
public:
virtual ~Renderer2D() {}
2020-12-06 09:40:16 -07:00
2021-02-27 14:25:27 -07:00
virtual void DrawScanline(u32 line, Unit* unit) = 0;
virtual void DrawSprites(u32 line, Unit* unit) = 0;
virtual void VBlankEnd(Unit* unitA, Unit* unitB) = 0;
2020-12-06 09:40:16 -07:00
2021-02-27 14:25:27 -07:00
void SetFramebuffer(u32* unitA, u32* unitB)
{
Framebuffer[0] = unitA;
Framebuffer[1] = unitB;
}
protected:
u32* Framebuffer[2];
Unit* CurUnit;
2020-12-06 09:40:16 -07:00
};
2021-02-27 14:25:27 -07:00
}
}
#endif