2019-09-11 04:01:19 +00:00
|
|
|
#pragma once
|
|
|
|
#include "common/bitfield.h"
|
2020-04-18 15:16:58 +00:00
|
|
|
#include "common/fifo_queue.h"
|
2019-11-05 09:19:49 +00:00
|
|
|
#include "common/rectangle.h"
|
2020-10-22 09:31:28 +00:00
|
|
|
#include "gpu_types.h"
|
2019-09-20 13:40:19 +00:00
|
|
|
#include "timers.h"
|
2019-09-11 04:01:19 +00:00
|
|
|
#include "types.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <algorithm>
|
2019-09-11 04:01:19 +00:00
|
|
|
#include <array>
|
2019-09-13 16:07:31 +00:00
|
|
|
#include <deque>
|
2019-10-22 13:07:51 +00:00
|
|
|
#include <memory>
|
2019-10-26 02:55:56 +00:00
|
|
|
#include <tuple>
|
2019-09-13 16:07:31 +00:00
|
|
|
#include <vector>
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2019-09-14 10:28:47 +00:00
|
|
|
class StateWrapper;
|
|
|
|
|
2019-11-03 14:39:48 +00:00
|
|
|
class HostDisplay;
|
2021-01-23 09:00:54 +00:00
|
|
|
class HostDisplayTexture;
|
2019-11-03 14:39:48 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
class TimingEvent;
|
2019-09-20 13:40:19 +00:00
|
|
|
class Timers;
|
2019-09-11 04:01:19 +00:00
|
|
|
|
|
|
|
class GPU
|
|
|
|
{
|
|
|
|
public:
|
2020-04-18 15:16:58 +00:00
|
|
|
enum class BlitterState : u8
|
2019-11-16 05:27:57 +00:00
|
|
|
{
|
|
|
|
Idle,
|
|
|
|
ReadingVRAM,
|
2020-04-18 15:16:58 +00:00
|
|
|
WritingVRAM,
|
|
|
|
DrawingPolyLine
|
2019-11-16 05:27:57 +00:00
|
|
|
};
|
|
|
|
|
2019-11-03 03:36:54 +00:00
|
|
|
enum class DMADirection : u32
|
|
|
|
{
|
|
|
|
Off = 0,
|
|
|
|
FIFO = 1,
|
|
|
|
CPUtoGP0 = 2,
|
|
|
|
GPUREADtoCPU = 3
|
|
|
|
};
|
|
|
|
|
2019-10-04 05:00:32 +00:00
|
|
|
enum : u32
|
|
|
|
{
|
2020-04-18 15:16:58 +00:00
|
|
|
MAX_FIFO_SIZE = 4096,
|
2019-10-04 05:00:32 +00:00
|
|
|
DOT_TIMER_INDEX = 0,
|
2020-02-28 06:59:55 +00:00
|
|
|
HBLANK_TIMER_INDEX = 1,
|
2021-01-03 17:08:58 +00:00
|
|
|
MAX_RESOLUTION_SCALE = 32,
|
2020-04-10 03:34:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum : u16
|
|
|
|
{
|
2020-03-19 15:12:41 +00:00
|
|
|
NTSC_TICKS_PER_LINE = 3413,
|
2020-08-16 15:21:52 +00:00
|
|
|
NTSC_HSYNC_TICKS = 200,
|
2020-03-19 15:12:41 +00:00
|
|
|
NTSC_TOTAL_LINES = 263,
|
|
|
|
PAL_TICKS_PER_LINE = 3406,
|
2020-08-16 15:21:52 +00:00
|
|
|
PAL_HSYNC_TICKS = 200, // actually one more on odd lines
|
2020-03-19 15:12:41 +00:00
|
|
|
PAL_TOTAL_LINES = 314,
|
2019-10-04 05:00:32 +00:00
|
|
|
};
|
|
|
|
|
2020-12-12 07:59:09 +00:00
|
|
|
enum : u16
|
|
|
|
{
|
|
|
|
NTSC_HORIZONTAL_ACTIVE_START = 488,
|
|
|
|
NTSC_HORIZONTAL_ACTIVE_END = 3288,
|
|
|
|
NTSC_VERTICAL_ACTIVE_START = 16,
|
|
|
|
NTSC_VERTICAL_ACTIVE_END = 256,
|
|
|
|
PAL_HORIZONTAL_ACTIVE_START = 487,
|
|
|
|
PAL_HORIZONTAL_ACTIVE_END = 3282,
|
|
|
|
PAL_VERTICAL_ACTIVE_START = 20,
|
|
|
|
PAL_VERTICAL_ACTIVE_END = 308,
|
|
|
|
};
|
|
|
|
|
2019-11-03 03:36:54 +00:00
|
|
|
// Base class constructor.
|
2019-09-11 04:01:19 +00:00
|
|
|
GPU();
|
2019-09-12 02:53:04 +00:00
|
|
|
virtual ~GPU();
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2020-01-24 04:51:53 +00:00
|
|
|
virtual bool IsHardwareRenderer() const = 0;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
virtual bool Initialize(HostDisplay* host_display);
|
2021-01-23 09:00:54 +00:00
|
|
|
virtual void Reset(bool clear_vram);
|
|
|
|
virtual bool DoState(StateWrapper& sw, HostDisplayTexture** save_to_texture, bool update_display);
|
2019-10-04 05:00:32 +00:00
|
|
|
|
2019-10-04 12:10:43 +00:00
|
|
|
// Graphics API state reset/restore - call when drawing the UI etc.
|
|
|
|
virtual void ResetGraphicsAPIState();
|
|
|
|
virtual void RestoreGraphicsAPIState();
|
|
|
|
|
2019-10-04 05:00:32 +00:00
|
|
|
// Render statistics debug window.
|
2019-10-26 02:55:56 +00:00
|
|
|
void DrawDebugStateWindow();
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
void CPUClockChanged();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
// MMIO access
|
2019-09-11 04:01:19 +00:00
|
|
|
u32 ReadRegister(u32 offset);
|
|
|
|
void WriteRegister(u32 offset, u32 value);
|
|
|
|
|
2019-09-11 04:59:41 +00:00
|
|
|
// DMA access
|
2019-10-13 06:48:11 +00:00
|
|
|
void DMARead(u32* words, u32 word_count);
|
2020-08-01 14:25:07 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE bool BeginDMAWrite() const { return (m_GPUSTAT.dma_direction == DMADirection::CPUtoGP0); }
|
|
|
|
ALWAYS_INLINE void DMAWrite(u32 address, u32 value)
|
|
|
|
{
|
|
|
|
m_fifo.Push((ZeroExtend64(address) << 32) | ZeroExtend64(value));
|
|
|
|
}
|
|
|
|
void EndDMAWrite();
|
2019-09-11 04:59:41 +00:00
|
|
|
|
2020-12-12 07:59:09 +00:00
|
|
|
/// Returns true if no data is being sent from VRAM to the DAC or that no portion of VRAM would be visible on screen.
|
2020-08-15 14:17:10 +00:00
|
|
|
ALWAYS_INLINE bool IsDisplayDisabled() const
|
|
|
|
{
|
|
|
|
return m_GPUSTAT.display_disable || m_crtc_state.display_vram_width == 0 || m_crtc_state.display_vram_height == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if scanout should be interlaced.
|
|
|
|
ALWAYS_INLINE bool IsInterlacedDisplayEnabled() const
|
|
|
|
{
|
2020-12-15 01:35:01 +00:00
|
|
|
return (!m_force_progressive_scan) && m_GPUSTAT.vertical_interlace;
|
2020-08-15 14:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if interlaced rendering is enabled and force progressive scan is disabled.
|
|
|
|
ALWAYS_INLINE bool IsInterlacedRenderingEnabled() const
|
|
|
|
{
|
2020-12-15 01:35:01 +00:00
|
|
|
return (!m_force_progressive_scan) && m_GPUSTAT.SkipDrawingToActiveField();
|
2020-08-15 14:17:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 14:13:20 +00:00
|
|
|
/// Returns the number of pending GPU ticks.
|
2020-06-12 15:28:49 +00:00
|
|
|
TickCount GetPendingCRTCTicks() const;
|
|
|
|
TickCount GetPendingCommandTicks() const;
|
2020-03-25 14:13:20 +00:00
|
|
|
|
|
|
|
/// Returns true if enough ticks have passed for the raster to be on the next line.
|
2020-06-12 15:28:49 +00:00
|
|
|
bool IsCRTCScanlinePending() const;
|
2020-03-25 14:13:20 +00:00
|
|
|
|
2020-04-18 15:16:58 +00:00
|
|
|
/// Returns true if a raster scanline or command execution is pending.
|
2020-06-12 15:28:49 +00:00
|
|
|
bool IsCommandCompletionPending() const;
|
2020-04-18 15:16:58 +00:00
|
|
|
|
2020-08-02 17:06:03 +00:00
|
|
|
/// Synchronizes the CRTC, updating the hblank timer.
|
2020-06-12 15:28:49 +00:00
|
|
|
void SynchronizeCRTC();
|
2020-01-24 04:53:40 +00:00
|
|
|
|
2020-08-02 17:06:03 +00:00
|
|
|
/// Recompile shaders/recreate framebuffers when needed.
|
2019-11-01 14:31:25 +00:00
|
|
|
virtual void UpdateSettings();
|
2019-09-12 02:53:04 +00:00
|
|
|
|
2020-08-02 17:06:03 +00:00
|
|
|
/// Updates the resolution scale when it's set to automatic.
|
|
|
|
virtual void UpdateResolutionScale();
|
|
|
|
|
2020-08-15 14:17:10 +00:00
|
|
|
/// Returns the effective display resolution of the GPU.
|
|
|
|
virtual std::tuple<u32, u32> GetEffectiveDisplayResolution();
|
|
|
|
|
2019-11-03 14:39:48 +00:00
|
|
|
// gpu_hw_d3d11.cpp
|
|
|
|
static std::unique_ptr<GPU> CreateHardwareD3D11Renderer();
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
// gpu_hw_opengl.cpp
|
|
|
|
static std::unique_ptr<GPU> CreateHardwareOpenGLRenderer();
|
|
|
|
|
2020-06-18 14:18:17 +00:00
|
|
|
// gpu_hw_vulkan.cpp
|
|
|
|
static std::unique_ptr<GPU> CreateHardwareVulkanRenderer();
|
|
|
|
|
2019-10-26 02:57:35 +00:00
|
|
|
// gpu_sw.cpp
|
|
|
|
static std::unique_ptr<GPU> CreateSoftwareRenderer();
|
|
|
|
|
2020-04-25 15:10:46 +00:00
|
|
|
// Converts window coordinates into horizontal ticks and scanlines. Returns false if out of range. Used for lightguns.
|
2020-12-19 04:22:59 +00:00
|
|
|
bool ConvertScreenCoordinatesToBeamTicksAndLines(s32 window_x, s32 window_y, float x_scale, u32* out_tick,
|
|
|
|
u32* out_line) const;
|
2020-04-25 15:10:46 +00:00
|
|
|
|
2020-07-01 16:51:22 +00:00
|
|
|
// Returns the video clock frequency.
|
|
|
|
TickCount GetCRTCFrequency() const;
|
|
|
|
|
2021-01-13 09:24:41 +00:00
|
|
|
// Dumps raw VRAM to a file.
|
|
|
|
bool DumpVRAMToFile(const char* filename);
|
|
|
|
|
2019-09-12 02:53:04 +00:00
|
|
|
protected:
|
2020-06-12 15:28:49 +00:00
|
|
|
TickCount CRTCTicksToSystemTicks(TickCount crtc_ticks, TickCount fractional_ticks) const;
|
|
|
|
TickCount SystemTicksToCRTCTicks(TickCount sysclk_ticks, TickCount* fractional_ticks) const;
|
|
|
|
|
|
|
|
// The GPU internally appears to run at 2x the system clock.
|
|
|
|
ALWAYS_INLINE static constexpr TickCount GPUTicksToSystemTicks(TickCount gpu_ticks)
|
|
|
|
{
|
2021-01-03 06:05:43 +00:00
|
|
|
return std::max<TickCount>((gpu_ticks + 1) >> 1, 1);
|
2020-06-12 15:28:49 +00:00
|
|
|
}
|
|
|
|
ALWAYS_INLINE static constexpr TickCount SystemTicksToGPUTicks(TickCount sysclk_ticks) { return sysclk_ticks << 1; }
|
2020-03-25 14:13:20 +00:00
|
|
|
|
2019-09-18 05:43:25 +00:00
|
|
|
// Helper/format conversion functions.
|
|
|
|
static constexpr u32 RGBA5551ToRGBA8888(u16 color)
|
|
|
|
{
|
|
|
|
u8 r = Truncate8(color & 31);
|
|
|
|
u8 g = Truncate8((color >> 5) & 31);
|
|
|
|
u8 b = Truncate8((color >> 10) & 31);
|
|
|
|
u8 a = Truncate8((color >> 15) & 1);
|
|
|
|
|
|
|
|
// 00012345 -> 1234545
|
|
|
|
b = (b << 3) | (b & 0b111);
|
|
|
|
g = (g << 3) | (g & 0b111);
|
|
|
|
r = (r << 3) | (r & 0b111);
|
|
|
|
a = a ? 255 : 0;
|
|
|
|
|
|
|
|
return ZeroExtend32(r) | (ZeroExtend32(g) << 8) | (ZeroExtend32(b) << 16) | (ZeroExtend32(a) << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr u16 RGBA8888ToRGBA5551(u32 color)
|
|
|
|
{
|
|
|
|
const u16 r = Truncate16((color >> 3) & 0x1Fu);
|
|
|
|
const u16 g = Truncate16((color >> 11) & 0x1Fu);
|
|
|
|
const u16 b = Truncate16((color >> 19) & 0x1Fu);
|
|
|
|
const u16 a = Truncate16((color >> 31) & 0x01u);
|
|
|
|
|
|
|
|
return r | (g << 5) | (b << 10) | (a << 15);
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:55:56 +00:00
|
|
|
static constexpr std::tuple<u8, u8> UnpackTexcoord(u16 texcoord)
|
|
|
|
{
|
|
|
|
return std::make_tuple(static_cast<u8>(texcoord), static_cast<u8>(texcoord >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr std::tuple<u8, u8, u8> UnpackColorRGB24(u32 rgb24)
|
|
|
|
{
|
|
|
|
return std::make_tuple(static_cast<u8>(rgb24), static_cast<u8>(rgb24 >> 8), static_cast<u8>(rgb24 >> 16));
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:43:25 +00:00
|
|
|
static bool DumpVRAMToFile(const char* filename, u32 width, u32 height, u32 stride, const void* buffer,
|
|
|
|
bool remove_alpha);
|
|
|
|
|
2019-09-11 04:01:19 +00:00
|
|
|
void SoftReset();
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2019-09-17 04:25:25 +00:00
|
|
|
// Sets dots per scanline
|
2020-06-07 07:36:45 +00:00
|
|
|
float ComputeHorizontalFrequency() const;
|
|
|
|
float ComputeVerticalFrequency() const;
|
2020-09-26 05:11:45 +00:00
|
|
|
float GetDisplayAspectRatio() const;
|
2019-09-17 04:25:25 +00:00
|
|
|
void UpdateCRTCConfig();
|
2020-03-28 15:14:37 +00:00
|
|
|
void UpdateCRTCDisplayParameters();
|
2019-09-17 04:25:25 +00:00
|
|
|
|
|
|
|
// Update ticks for this execution slice
|
2020-06-12 15:28:49 +00:00
|
|
|
void UpdateCRTCTickEvent();
|
|
|
|
void UpdateCommandTickEvent();
|
2019-09-17 04:25:25 +00:00
|
|
|
|
2019-09-13 16:07:31 +00:00
|
|
|
// Updates dynamic bits in GPUSTAT (ready to send VRAM/ready to receive DMA)
|
2020-03-01 07:06:38 +00:00
|
|
|
void UpdateDMARequest();
|
2020-06-12 15:28:49 +00:00
|
|
|
void UpdateGPUIdle();
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
// Ticks for hblank/vblank.
|
2020-06-12 15:28:49 +00:00
|
|
|
void CRTCTickEvent(TickCount ticks);
|
|
|
|
void CommandTickEvent(TickCount ticks);
|
2020-01-24 04:53:40 +00:00
|
|
|
|
2020-05-26 12:57:56 +00:00
|
|
|
/// Returns 0 if the currently-displayed field is on odd lines (1,3,5,...) or 1 if even (2,4,6,...).
|
|
|
|
ALWAYS_INLINE u32 GetInterlacedDisplayField() const { return ZeroExtend32(m_crtc_state.interlaced_field); }
|
|
|
|
|
|
|
|
/// Returns 0 if the currently-displayed field is on an even line in VRAM, otherwise 1.
|
|
|
|
ALWAYS_INLINE u32 GetActiveLineLSB() const { return ZeroExtend32(m_crtc_state.active_line_lsb); }
|
2019-12-10 12:52:46 +00:00
|
|
|
|
2019-12-11 06:35:14 +00:00
|
|
|
/// Sets/decodes GP0(E1h) (set draw mode).
|
|
|
|
void SetDrawMode(u16 bits);
|
|
|
|
|
|
|
|
/// Sets/decodes polygon/rectangle texture palette value.
|
|
|
|
void SetTexturePalette(u16 bits);
|
|
|
|
|
2020-04-03 14:10:55 +00:00
|
|
|
/// Sets/decodes texture window bits.
|
|
|
|
void SetTextureWindow(u32 value);
|
|
|
|
|
2019-09-11 04:59:41 +00:00
|
|
|
u32 ReadGPUREAD();
|
2020-04-18 15:16:58 +00:00
|
|
|
void FinishVRAMWrite();
|
|
|
|
|
|
|
|
/// Returns the number of vertices in the buffered poly-line.
|
|
|
|
ALWAYS_INLINE u32 GetPolyLineVertexCount() const
|
|
|
|
{
|
|
|
|
return (static_cast<u32>(m_blit_buffer.size()) + BoolToUInt32(m_render_command.shading_enable)) >>
|
|
|
|
BoolToUInt8(m_render_command.shading_enable);
|
|
|
|
}
|
|
|
|
|
2020-06-13 15:01:01 +00:00
|
|
|
/// Returns true if the drawing area is valid (i.e. left <= right, top <= bottom).
|
|
|
|
ALWAYS_INLINE bool IsDrawingAreaIsValid() const { return m_drawing_area.Valid(); }
|
|
|
|
|
2020-10-03 04:10:54 +00:00
|
|
|
/// Clamps the specified coordinates to the drawing area.
|
|
|
|
ALWAYS_INLINE void ClampCoordinatesToDrawingArea(s32* x, s32* y)
|
|
|
|
{
|
|
|
|
const s32 x_value = *x;
|
|
|
|
if (x_value < static_cast<s32>(m_drawing_area.left))
|
|
|
|
*x = m_drawing_area.left;
|
|
|
|
else if (x_value >= static_cast<s32>(m_drawing_area.right))
|
|
|
|
*x = m_drawing_area.right - 1;
|
|
|
|
|
|
|
|
const s32 y_value = *y;
|
|
|
|
if (y_value < static_cast<s32>(m_drawing_area.top))
|
|
|
|
*y = m_drawing_area.top;
|
|
|
|
else if (y_value >= static_cast<s32>(m_drawing_area.bottom))
|
|
|
|
*y = m_drawing_area.bottom - 1;
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:16:58 +00:00
|
|
|
void AddCommandTicks(TickCount ticks);
|
|
|
|
|
2019-09-11 04:01:19 +00:00
|
|
|
void WriteGP1(u32 value);
|
2019-11-14 12:17:09 +00:00
|
|
|
void EndCommand();
|
2020-04-18 15:16:58 +00:00
|
|
|
void ExecuteCommands();
|
2019-10-05 14:37:31 +00:00
|
|
|
void HandleGetGPUInfoCommand(u32 value);
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2019-09-12 02:53:04 +00:00
|
|
|
// Rendering in the backend
|
2019-11-14 07:16:59 +00:00
|
|
|
virtual void ReadVRAM(u32 x, u32 y, u32 width, u32 height);
|
2019-11-02 15:05:37 +00:00
|
|
|
virtual void FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color);
|
2020-12-14 16:19:28 +00:00
|
|
|
virtual void UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data, bool set_mask, bool check_mask);
|
2019-09-17 14:58:30 +00:00
|
|
|
virtual void CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 height);
|
2020-04-18 15:16:58 +00:00
|
|
|
virtual void DispatchRenderCommand();
|
2019-09-12 02:53:04 +00:00
|
|
|
virtual void FlushRender();
|
2020-08-02 17:26:11 +00:00
|
|
|
virtual void ClearDisplay();
|
2019-11-05 09:44:17 +00:00
|
|
|
virtual void UpdateDisplay();
|
|
|
|
virtual void DrawRendererStats(bool is_idle_frame);
|
2019-09-11 06:04:31 +00:00
|
|
|
|
2020-10-03 04:10:54 +00:00
|
|
|
ALWAYS_INLINE void AddDrawTriangleTicks(s32 x1, s32 y1, s32 x2, s32 y2, s32 x3, s32 y3, bool shaded, bool textured,
|
|
|
|
bool semitransparent)
|
2020-04-18 15:16:58 +00:00
|
|
|
{
|
2020-10-03 04:10:54 +00:00
|
|
|
// This will not produce the correct results for triangles which are partially outside the clip area.
|
|
|
|
// However, usually it'll undershoot not overshoot. If we wanted to make this more accurate, we'd need to intersect
|
|
|
|
// the edges with the clip rectangle.
|
|
|
|
ClampCoordinatesToDrawingArea(&x1, &y1);
|
|
|
|
ClampCoordinatesToDrawingArea(&x2, &y2);
|
|
|
|
ClampCoordinatesToDrawingArea(&x3, &y3);
|
|
|
|
|
|
|
|
TickCount pixels = std::abs((x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2);
|
2020-06-12 15:29:48 +00:00
|
|
|
if (textured)
|
2020-10-03 04:10:54 +00:00
|
|
|
pixels += pixels;
|
2020-06-12 15:29:48 +00:00
|
|
|
if (semitransparent || m_GPUSTAT.check_mask_before_draw)
|
2020-10-03 04:10:54 +00:00
|
|
|
pixels += (pixels + 1) / 2;
|
2020-09-19 11:15:53 +00:00
|
|
|
if (m_GPUSTAT.SkipDrawingToActiveField())
|
2020-10-03 04:10:54 +00:00
|
|
|
pixels /= 2;
|
2020-06-12 15:29:48 +00:00
|
|
|
|
2020-10-03 04:10:54 +00:00
|
|
|
AddCommandTicks(pixels);
|
2020-06-12 15:29:48 +00:00
|
|
|
}
|
|
|
|
ALWAYS_INLINE void AddDrawRectangleTicks(u32 width, u32 height, bool textured, bool semitransparent)
|
|
|
|
{
|
|
|
|
u32 ticks_per_row = width;
|
|
|
|
if (textured)
|
|
|
|
ticks_per_row += width;
|
|
|
|
if (semitransparent || m_GPUSTAT.check_mask_before_draw)
|
|
|
|
ticks_per_row += (width + 1u) / 2u;
|
2020-09-19 11:15:53 +00:00
|
|
|
if (m_GPUSTAT.SkipDrawingToActiveField())
|
2020-06-18 07:37:11 +00:00
|
|
|
height = std::max<u32>(height / 2, 1u);
|
2020-06-12 15:29:48 +00:00
|
|
|
|
|
|
|
AddCommandTicks(ticks_per_row * height);
|
2020-04-18 15:16:58 +00:00
|
|
|
}
|
2020-06-18 07:37:11 +00:00
|
|
|
ALWAYS_INLINE void AddDrawLineTicks(u32 width, u32 height, bool shaded)
|
|
|
|
{
|
2020-09-19 11:15:53 +00:00
|
|
|
if (m_GPUSTAT.SkipDrawingToActiveField())
|
2020-06-18 07:37:11 +00:00
|
|
|
height = std::max<u32>(height / 2, 1u);
|
|
|
|
|
|
|
|
AddCommandTicks(std::max(width, height));
|
|
|
|
}
|
2020-04-18 15:16:58 +00:00
|
|
|
|
2019-11-03 14:39:48 +00:00
|
|
|
HostDisplay* m_host_display = nullptr;
|
2019-09-11 04:01:19 +00:00
|
|
|
|
2020-06-12 15:28:49 +00:00
|
|
|
std::unique_ptr<TimingEvent> m_crtc_tick_event;
|
|
|
|
std::unique_ptr<TimingEvent> m_command_tick_event;
|
2020-01-24 04:53:40 +00:00
|
|
|
|
2019-11-14 07:16:59 +00:00
|
|
|
// Pointer to VRAM, used for reads/writes. In the hardware backends, this is the shadow buffer.
|
|
|
|
u16* m_vram_ptr = nullptr;
|
|
|
|
|
2019-09-11 04:01:19 +00:00
|
|
|
union GPUSTAT
|
|
|
|
{
|
|
|
|
u32 bits;
|
|
|
|
BitField<u32, u8, 0, 4> texture_page_x_base;
|
|
|
|
BitField<u32, u8, 4, 1> texture_page_y_base;
|
2020-10-22 09:31:28 +00:00
|
|
|
BitField<u32, GPUTransparencyMode, 5, 2> semi_transparency_mode;
|
|
|
|
BitField<u32, GPUTextureMode, 7, 2> texture_color_mode;
|
2019-09-11 04:01:19 +00:00
|
|
|
BitField<u32, bool, 9, 1> dither_enable;
|
2020-04-03 14:11:33 +00:00
|
|
|
BitField<u32, bool, 10, 1> draw_to_displayed_field;
|
2019-11-24 08:47:40 +00:00
|
|
|
BitField<u32, bool, 11, 1> set_mask_while_drawing;
|
|
|
|
BitField<u32, bool, 12, 1> check_mask_before_draw;
|
2020-08-18 14:02:35 +00:00
|
|
|
BitField<u32, u8, 13, 1> interlaced_field;
|
2019-09-11 04:01:19 +00:00
|
|
|
BitField<u32, bool, 14, 1> reverse_flag;
|
|
|
|
BitField<u32, bool, 15, 1> texture_disable;
|
|
|
|
BitField<u32, u8, 16, 1> horizontal_resolution_2;
|
|
|
|
BitField<u32, u8, 17, 2> horizontal_resolution_1;
|
2019-10-15 12:36:10 +00:00
|
|
|
BitField<u32, bool, 19, 1> vertical_resolution;
|
2019-09-11 04:01:19 +00:00
|
|
|
BitField<u32, bool, 20, 1> pal_mode;
|
|
|
|
BitField<u32, bool, 21, 1> display_area_color_depth_24;
|
|
|
|
BitField<u32, bool, 22, 1> vertical_interlace;
|
2019-10-28 07:43:34 +00:00
|
|
|
BitField<u32, bool, 23, 1> display_disable;
|
2019-09-11 04:01:19 +00:00
|
|
|
BitField<u32, bool, 24, 1> interrupt_request;
|
|
|
|
BitField<u32, bool, 25, 1> dma_data_request;
|
2020-04-18 15:16:58 +00:00
|
|
|
BitField<u32, bool, 26, 1> gpu_idle;
|
2019-09-11 04:01:19 +00:00
|
|
|
BitField<u32, bool, 27, 1> ready_to_send_vram;
|
|
|
|
BitField<u32, bool, 28, 1> ready_to_recieve_dma;
|
2019-09-11 04:59:41 +00:00
|
|
|
BitField<u32, DMADirection, 29, 2> dma_direction;
|
2020-05-26 12:57:56 +00:00
|
|
|
BitField<u32, bool, 31, 1> display_line_lsb;
|
2019-11-13 14:58:15 +00:00
|
|
|
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE bool IsMaskingEnabled() const
|
2020-02-28 14:18:50 +00:00
|
|
|
{
|
|
|
|
static constexpr u32 MASK = ((1 << 11) | (1 << 12));
|
|
|
|
return ((bits & MASK) != 0);
|
|
|
|
}
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE bool SkipDrawingToActiveField() const
|
2020-04-03 14:10:41 +00:00
|
|
|
{
|
|
|
|
static constexpr u32 MASK = (1 << 19) | (1 << 22) | (1 << 10);
|
|
|
|
static constexpr u32 ACTIVE = (1 << 19) | (1 << 22);
|
|
|
|
return ((bits & MASK) == ACTIVE);
|
|
|
|
}
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE bool InInterleaved480iMode() const
|
2020-08-15 04:56:20 +00:00
|
|
|
{
|
|
|
|
static constexpr u32 ACTIVE = (1 << 19) | (1 << 22);
|
|
|
|
return ((bits & ACTIVE) == ACTIVE);
|
|
|
|
}
|
2019-11-24 08:47:40 +00:00
|
|
|
|
2020-03-05 14:29:35 +00:00
|
|
|
// During transfer/render operations, if ((dst_pixel & mask_and) == 0) { pixel = src_pixel | mask_or }
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE u16 GetMaskAND() const
|
2020-03-05 14:29:35 +00:00
|
|
|
{
|
|
|
|
// return check_mask_before_draw ? 0x8000 : 0x0000;
|
|
|
|
return Truncate16((bits << 3) & 0x8000);
|
|
|
|
}
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE u16 GetMaskOR() const
|
2020-03-05 14:29:35 +00:00
|
|
|
{
|
|
|
|
// return set_mask_while_drawing ? 0x8000 : 0x0000;
|
|
|
|
return Truncate16((bits << 4) & 0x8000);
|
|
|
|
}
|
2019-09-11 04:01:19 +00:00
|
|
|
} m_GPUSTAT = {};
|
2019-09-11 06:04:31 +00:00
|
|
|
|
2019-12-11 06:35:14 +00:00
|
|
|
struct DrawMode
|
2019-09-11 06:04:31 +00:00
|
|
|
{
|
2019-12-11 06:35:14 +00:00
|
|
|
static constexpr u16 PALETTE_MASK = UINT16_C(0b0111111111111111);
|
2020-04-03 14:10:55 +00:00
|
|
|
static constexpr u32 TEXTURE_WINDOW_MASK = UINT32_C(0b11111111111111111111);
|
2019-09-13 16:07:31 +00:00
|
|
|
|
2019-12-11 10:28:14 +00:00
|
|
|
// original values
|
2020-10-22 09:31:28 +00:00
|
|
|
GPUDrawModeReg mode_reg;
|
2019-12-11 10:28:14 +00:00
|
|
|
u16 palette_reg; // from vertex
|
|
|
|
u32 texture_window_value;
|
|
|
|
|
2019-09-13 16:07:31 +00:00
|
|
|
// decoded values
|
2019-09-27 06:17:09 +00:00
|
|
|
u32 texture_page_x;
|
|
|
|
u32 texture_page_y;
|
|
|
|
u32 texture_palette_x;
|
|
|
|
u32 texture_palette_y;
|
2020-11-21 03:32:58 +00:00
|
|
|
GPUTextureWindow texture_window;
|
2019-09-25 14:15:21 +00:00
|
|
|
bool texture_x_flip;
|
|
|
|
bool texture_y_flip;
|
2019-12-11 06:35:14 +00:00
|
|
|
bool texture_page_changed;
|
|
|
|
bool texture_window_changed;
|
|
|
|
|
2020-11-22 03:22:20 +00:00
|
|
|
/// Returns a rectangle comprising the texture palette area.
|
|
|
|
ALWAYS_INLINE_RELEASE Common::Rectangle<u32> GetTexturePaletteRectangle() const
|
|
|
|
{
|
|
|
|
static constexpr std::array<u32, 4> palette_widths = {{16, 256, 0, 0}};
|
|
|
|
return Common::Rectangle<u32>::FromExtents(texture_palette_x, texture_palette_y,
|
|
|
|
palette_widths[static_cast<u8>(mode_reg.texture_mode.GetValue())], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsTexturePageChanged() const { return texture_page_changed; }
|
|
|
|
ALWAYS_INLINE void SetTexturePageChanged() { texture_page_changed = true; }
|
|
|
|
ALWAYS_INLINE void ClearTexturePageChangedFlag() { texture_page_changed = false; }
|
2019-09-27 11:20:35 +00:00
|
|
|
|
2020-11-22 03:22:20 +00:00
|
|
|
ALWAYS_INLINE bool IsTextureWindowChanged() const { return texture_window_changed; }
|
|
|
|
ALWAYS_INLINE void SetTextureWindowChanged() { texture_window_changed = true; }
|
|
|
|
ALWAYS_INLINE void ClearTextureWindowChangedFlag() { texture_window_changed = false; }
|
2019-12-11 06:35:14 +00:00
|
|
|
} m_draw_mode = {};
|
2019-09-11 06:04:31 +00:00
|
|
|
|
2020-06-18 14:18:17 +00:00
|
|
|
Common::Rectangle<u32> m_drawing_area{0, 0, VRAM_WIDTH, VRAM_HEIGHT};
|
2019-09-11 06:04:31 +00:00
|
|
|
|
|
|
|
struct DrawingOffset
|
|
|
|
{
|
|
|
|
s32 x;
|
|
|
|
s32 y;
|
|
|
|
} m_drawing_offset = {};
|
|
|
|
|
2020-06-07 07:36:45 +00:00
|
|
|
bool m_console_is_pal = false;
|
2019-12-11 06:47:49 +00:00
|
|
|
bool m_set_texture_disable_mask = false;
|
2019-11-05 09:19:49 +00:00
|
|
|
bool m_drawing_area_changed = false;
|
2019-12-10 12:52:46 +00:00
|
|
|
bool m_force_progressive_scan = false;
|
2020-04-10 03:34:12 +00:00
|
|
|
bool m_force_ntsc_timings = false;
|
2019-11-05 09:19:49 +00:00
|
|
|
|
2019-09-17 04:25:25 +00:00
|
|
|
struct CRTCState
|
|
|
|
{
|
|
|
|
struct Regs
|
|
|
|
{
|
2020-07-10 10:31:58 +00:00
|
|
|
static constexpr u32 DISPLAY_ADDRESS_START_MASK = 0b111'11111111'11111110;
|
2019-09-17 04:25:25 +00:00
|
|
|
static constexpr u32 HORIZONTAL_DISPLAY_RANGE_MASK = 0b11111111'11111111'11111111;
|
|
|
|
static constexpr u32 VERTICAL_DISPLAY_RANGE_MASK = 0b1111'11111111'11111111;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u32 display_address_start;
|
2020-07-10 10:31:58 +00:00
|
|
|
BitField<u32, u16, 0, 10> X;
|
2020-02-28 07:01:01 +00:00
|
|
|
BitField<u32, u16, 10, 9> Y;
|
2019-09-17 04:25:25 +00:00
|
|
|
};
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u32 horizontal_display_range;
|
2020-02-28 07:01:01 +00:00
|
|
|
BitField<u32, u16, 0, 12> X1;
|
|
|
|
BitField<u32, u16, 12, 12> X2;
|
2019-09-17 04:25:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u32 vertical_display_range;
|
2020-02-28 07:01:01 +00:00
|
|
|
BitField<u32, u16, 0, 10> Y1;
|
|
|
|
BitField<u32, u16, 10, 10> Y2;
|
2019-09-17 04:25:25 +00:00
|
|
|
};
|
|
|
|
} regs;
|
|
|
|
|
2020-02-28 07:01:01 +00:00
|
|
|
u16 dot_clock_divider;
|
2019-09-17 04:25:25 +00:00
|
|
|
|
2020-03-28 15:14:37 +00:00
|
|
|
// Size of the simulated screen in pixels. Depending on crop mode, this may include overscan area.
|
|
|
|
u16 display_width;
|
|
|
|
u16 display_height;
|
2019-09-17 04:25:25 +00:00
|
|
|
|
2020-12-12 07:59:09 +00:00
|
|
|
// Top-left corner in screen coordinates where the outputted portion of VRAM is first visible.
|
2020-03-28 15:14:37 +00:00
|
|
|
u16 display_origin_left;
|
|
|
|
u16 display_origin_top;
|
|
|
|
|
2020-12-12 07:59:09 +00:00
|
|
|
// Rectangle in VRAM coordinates describing the area of VRAM that is visible on screen.
|
2020-03-28 15:14:37 +00:00
|
|
|
u16 display_vram_left;
|
|
|
|
u16 display_vram_top;
|
|
|
|
u16 display_vram_width;
|
|
|
|
u16 display_vram_height;
|
|
|
|
|
2020-12-12 08:02:24 +00:00
|
|
|
// Visible range of the screen, in GPU ticks/lines. Clamped to lie within the active video region.
|
2020-12-12 07:59:09 +00:00
|
|
|
u16 horizontal_visible_start;
|
|
|
|
u16 horizontal_visible_end;
|
|
|
|
u16 vertical_visible_start;
|
|
|
|
u16 vertical_visible_end;
|
|
|
|
|
2020-03-28 15:14:37 +00:00
|
|
|
u16 horizontal_display_start;
|
|
|
|
u16 horizontal_display_end;
|
|
|
|
u16 vertical_display_start;
|
|
|
|
u16 vertical_display_end;
|
2019-09-17 04:25:25 +00:00
|
|
|
|
2020-12-12 07:59:09 +00:00
|
|
|
u16 horizontal_total;
|
|
|
|
u16 horizontal_sync_start; // <- not currently saved to state, so we don't have to bump the version
|
|
|
|
u16 vertical_total;
|
|
|
|
|
2019-09-17 04:25:25 +00:00
|
|
|
TickCount fractional_ticks;
|
|
|
|
TickCount current_tick_in_scanline;
|
|
|
|
u32 current_scanline;
|
|
|
|
|
2020-12-05 05:59:47 +00:00
|
|
|
TickCount fractional_dot_ticks; // only used when timer0 is enabled
|
|
|
|
|
2019-09-17 04:25:25 +00:00
|
|
|
bool in_hblank;
|
|
|
|
bool in_vblank;
|
2020-05-16 15:02:20 +00:00
|
|
|
|
2020-05-26 12:57:56 +00:00
|
|
|
u8 interlaced_field; // 0 = odd, 1 = even
|
2020-08-15 04:56:20 +00:00
|
|
|
u8 interlaced_display_field;
|
2020-05-26 12:57:56 +00:00
|
|
|
u8 active_line_lsb;
|
2019-09-17 04:25:25 +00:00
|
|
|
} m_crtc_state = {};
|
|
|
|
|
2020-04-18 15:16:58 +00:00
|
|
|
BlitterState m_blitter_state = BlitterState::Idle;
|
2019-11-14 12:17:09 +00:00
|
|
|
u32 m_command_total_words = 0;
|
2020-06-12 15:28:49 +00:00
|
|
|
TickCount m_pending_command_ticks = 0;
|
2020-03-01 07:06:38 +00:00
|
|
|
|
|
|
|
/// GPUREAD value for non-VRAM-reads.
|
|
|
|
u32 m_GPUREAD_latch = 0;
|
|
|
|
|
2020-04-18 15:16:58 +00:00
|
|
|
/// True if currently executing/syncing.
|
|
|
|
bool m_syncing = false;
|
|
|
|
bool m_fifo_pushed = false;
|
|
|
|
|
2019-11-14 12:17:09 +00:00
|
|
|
struct VRAMTransfer
|
|
|
|
{
|
|
|
|
u16 x;
|
|
|
|
u16 y;
|
|
|
|
u16 width;
|
|
|
|
u16 height;
|
|
|
|
u16 col;
|
|
|
|
u16 row;
|
|
|
|
} m_vram_transfer = {};
|
|
|
|
|
2020-08-01 14:25:07 +00:00
|
|
|
HeapFIFOQueue<u64, MAX_FIFO_SIZE> m_fifo;
|
2020-04-18 15:16:58 +00:00
|
|
|
std::vector<u32> m_blit_buffer;
|
|
|
|
u32 m_blit_remaining_words;
|
2020-10-22 09:31:28 +00:00
|
|
|
GPURenderCommand m_render_command{};
|
2020-04-18 15:16:58 +00:00
|
|
|
|
2020-08-01 14:25:07 +00:00
|
|
|
ALWAYS_INLINE u32 FifoPop() { return Truncate32(m_fifo.Pop()); }
|
|
|
|
ALWAYS_INLINE u32 FifoPeek() { return Truncate32(m_fifo.Peek()); }
|
|
|
|
ALWAYS_INLINE u32 FifoPeek(u32 i) { return Truncate32(m_fifo.Peek(i)); }
|
|
|
|
|
2020-04-18 15:16:58 +00:00
|
|
|
TickCount m_max_run_ahead = 128;
|
|
|
|
u32 m_fifo_size = 128;
|
2019-09-18 05:43:25 +00:00
|
|
|
|
2019-11-05 09:44:17 +00:00
|
|
|
struct Stats
|
|
|
|
{
|
|
|
|
u32 num_vram_reads;
|
|
|
|
u32 num_vram_fills;
|
|
|
|
u32 num_vram_writes;
|
|
|
|
u32 num_vram_copies;
|
|
|
|
u32 num_vertices;
|
|
|
|
u32 num_polygons;
|
|
|
|
};
|
|
|
|
Stats m_stats = {};
|
|
|
|
Stats m_last_stats = {};
|
|
|
|
|
2019-10-18 12:05:06 +00:00
|
|
|
private:
|
2020-04-18 15:16:58 +00:00
|
|
|
using GP0CommandHandler = bool (GPU::*)();
|
2019-10-18 12:05:06 +00:00
|
|
|
using GP0CommandHandlerTable = std::array<GP0CommandHandler, 256>;
|
2019-10-22 13:07:51 +00:00
|
|
|
static GP0CommandHandlerTable GenerateGP0CommandHandlerTable();
|
2019-10-18 12:05:06 +00:00
|
|
|
|
|
|
|
// Rendering commands, returns false if not enough data is provided
|
2020-04-18 15:16:58 +00:00
|
|
|
bool HandleUnknownGP0Command();
|
|
|
|
bool HandleNOPCommand();
|
|
|
|
bool HandleClearCacheCommand();
|
|
|
|
bool HandleInterruptRequestCommand();
|
|
|
|
bool HandleSetDrawModeCommand();
|
|
|
|
bool HandleSetTextureWindowCommand();
|
|
|
|
bool HandleSetDrawingAreaTopLeftCommand();
|
|
|
|
bool HandleSetDrawingAreaBottomRightCommand();
|
|
|
|
bool HandleSetDrawingOffsetCommand();
|
|
|
|
bool HandleSetMaskBitCommand();
|
|
|
|
bool HandleRenderPolygonCommand();
|
|
|
|
bool HandleRenderRectangleCommand();
|
|
|
|
bool HandleRenderLineCommand();
|
|
|
|
bool HandleRenderPolyLineCommand();
|
|
|
|
bool HandleFillRectangleCommand();
|
|
|
|
bool HandleCopyRectangleCPUToVRAMCommand();
|
|
|
|
bool HandleCopyRectangleVRAMToCPUCommand();
|
|
|
|
bool HandleCopyRectangleVRAMToVRAMCommand();
|
2019-10-18 12:05:06 +00:00
|
|
|
|
|
|
|
static const GP0CommandHandlerTable s_GP0_command_handler_table;
|
2019-09-11 04:01:19 +00:00
|
|
|
};
|
2019-11-03 03:36:54 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
extern std::unique_ptr<GPU> g_gpu;
|