Skip to content

Commit

Permalink
GPU/HW: Use GSVector instead of Rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 3, 2024
1 parent ef152c4 commit 9ef7e8c
Show file tree
Hide file tree
Showing 28 changed files with 557 additions and 509 deletions.
78 changes: 44 additions & 34 deletions src/core/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "common/align.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/gsvector_formatter.h"
#include "common/heap_array.h"
#include "common/log.h"
#include "common/path.h"
Expand Down Expand Up @@ -351,6 +352,7 @@ bool GPU::DoState(StateWrapper& sw, GPUTexture** host_texture, bool update_displ
m_draw_mode.texture_page_changed = true;
m_draw_mode.texture_window_changed = true;
m_drawing_area_changed = true;
SetClampedDrawingArea();
UpdateDMARequest();
}

Expand Down Expand Up @@ -1089,12 +1091,11 @@ void GPU::UpdateCommandTickEvent()
void GPU::ConvertScreenCoordinatesToDisplayCoordinates(float window_x, float window_y, float* display_x,
float* display_y) const
{
const Common::Rectangle<s32> draw_rc =
CalculateDrawRect(g_gpu_device->GetWindowWidth(), g_gpu_device->GetWindowHeight());
const GSVector4i draw_rc = CalculateDrawRect(g_gpu_device->GetWindowWidth(), g_gpu_device->GetWindowHeight(), true);

// convert coordinates to active display region, then to full display region
const float scaled_display_x = (window_x - static_cast<float>(draw_rc.left)) / static_cast<float>(draw_rc.GetWidth());
const float scaled_display_y = (window_y - static_cast<float>(draw_rc.top)) / static_cast<float>(draw_rc.GetHeight());
const float scaled_display_x = (window_x - static_cast<float>(draw_rc.left)) / static_cast<float>(draw_rc.width());
const float scaled_display_y = (window_y - static_cast<float>(draw_rc.top)) / static_cast<float>(draw_rc.height());

// scale back to internal resolution
*display_x = scaled_display_x * static_cast<float>(m_crtc_state.display_width);
Expand Down Expand Up @@ -1632,6 +1633,21 @@ void GPU::CopyVRAM(u32 src_x, u32 src_y, u32 dst_x, u32 dst_y, u32 width, u32 he
}
}

void GPU::SetClampedDrawingArea()
{
if (!IsDrawingAreaIsValid()) [[unlikely]]
{
m_clamped_drawing_area = GSVector4i::zero();
return;
}

const u32 right = std::min(m_drawing_area.right + 1, static_cast<u32>(VRAM_WIDTH));
const u32 left = std::min(m_drawing_area.left, std::min(m_drawing_area.right, VRAM_WIDTH - 1));
const u32 bottom = std::min(m_drawing_area.bottom + 1, static_cast<u32>(VRAM_HEIGHT));
const u32 top = std::min(m_drawing_area.top, std::min(m_drawing_area.bottom, VRAM_HEIGHT - 1));
m_clamped_drawing_area = GSVector4i(left, top, right, bottom);
}

void GPU::SetDrawMode(u16 value)
{
GPUDrawModeReg new_mode_reg{static_cast<u16>(value & GPUDrawModeReg::MASK)};
Expand Down Expand Up @@ -1917,16 +1933,15 @@ bool GPU::PresentDisplay()
{
FlushRender();

const Common::Rectangle<s32> draw_rect =
m_display_texture ? CalculateDrawRect(g_gpu_device->GetWindowWidth(), g_gpu_device->GetWindowHeight()) :
Common::Rectangle<s32>();
const GSVector4i draw_rect = m_display_texture ?
CalculateDrawRect(g_gpu_device->GetWindowWidth(), g_gpu_device->GetWindowHeight()) :
GSVector4i::zero();
return RenderDisplay(nullptr, draw_rect, !g_settings.debugging.show_vram);
}

bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_rect, bool postfx)
bool GPU::RenderDisplay(GPUTexture* target, const GSVector4i draw_rect, bool postfx)
{
GL_SCOPE_FMT("RenderDisplay: {}x{} at {},{}", draw_rect.GetWidth(), draw_rect.GetHeight(), draw_rect.left,
draw_rect.top);
GL_SCOPE_FMT("RenderDisplay: {}", draw_rect);

if (m_display_texture)
m_display_texture->MakeReadyForSampling();
Expand All @@ -1947,8 +1962,8 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r

// Now we can apply the post chain.
GPUTexture* post_output_texture = PostProcessing::InternalChain.GetOutputTexture();
if (PostProcessing::InternalChain.Apply(display_texture, m_display_depth_buffer, post_output_texture, 0, 0,
display_texture_view_width, display_texture_view_height,
if (PostProcessing::InternalChain.Apply(display_texture, m_display_depth_buffer, post_output_texture,
GSVector4i(0, 0, display_texture_view_width, display_texture_view_height),
display_texture_view_width, display_texture_view_height,
m_crtc_state.display_width, m_crtc_state.display_height))
{
Expand All @@ -1966,7 +1981,7 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r
(postfx && PostProcessing::DisplayChain.IsActive() && !g_gpu_device->GetWindowInfo().IsSurfaceless() &&
hdformat != GPUTexture::Format::Unknown && target_width > 0 && target_height > 0 &&
PostProcessing::DisplayChain.CheckTargets(hdformat, target_width, target_height));
const Common::Rectangle<s32> real_draw_rect =
const GSVector4i real_draw_rect =
g_gpu_device->UsesLowerLeftOrigin() ? GPUDevice::FlipToLowerLeft(draw_rect, target_height) : draw_rect;
if (really_postfx)
{
Expand Down Expand Up @@ -2009,11 +2024,9 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r
{
texture_filter_linear = true;
uniforms.params[0] = std::max(
std::floor(static_cast<float>(draw_rect.GetWidth()) / static_cast<float>(m_display_texture_view_width)),
1.0f);
std::floor(static_cast<float>(draw_rect.width()) / static_cast<float>(m_display_texture_view_width)), 1.0f);
uniforms.params[1] = std::max(
std::floor(static_cast<float>(draw_rect.GetHeight()) / static_cast<float>(m_display_texture_view_height)),
1.0f);
std::floor(static_cast<float>(draw_rect.height()) / static_cast<float>(m_display_texture_view_height)), 1.0f);
uniforms.params[2] = 0.5f - 0.5f / uniforms.params[0];
uniforms.params[3] = 0.5f - 0.5f / uniforms.params[1];
}
Expand Down Expand Up @@ -2048,8 +2061,7 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r
uniforms.src_size[3] = rcp_height;
g_gpu_device->PushUniformBuffer(&uniforms, sizeof(uniforms));

g_gpu_device->SetViewportAndScissor(real_draw_rect.left, real_draw_rect.top, real_draw_rect.GetWidth(),
real_draw_rect.GetHeight());
g_gpu_device->SetViewportAndScissor(real_draw_rect);
g_gpu_device->Draw(3, 0);
}

Expand All @@ -2066,14 +2078,11 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r
const s32 orig_height = static_cast<s32>(std::ceil(static_cast<float>(m_crtc_state.display_height) * upscale_y));

return PostProcessing::DisplayChain.Apply(PostProcessing::DisplayChain.GetInputTexture(), nullptr, target,
real_draw_rect.left, real_draw_rect.top, real_draw_rect.GetWidth(),
real_draw_rect.GetHeight(), orig_width, orig_height,
m_crtc_state.display_width, m_crtc_state.display_height);
real_draw_rect, orig_width, orig_height, m_crtc_state.display_width,
m_crtc_state.display_height);
}
else
{
return true;
}
}

void GPU::DestroyDeinterlaceTextures()
Expand Down Expand Up @@ -2303,8 +2312,7 @@ bool GPU::ApplyChromaSmoothing()
return true;
}

Common::Rectangle<s32> GPU::CalculateDrawRect(s32 window_width, s32 window_height,
bool apply_aspect_ratio /* = true */) const
GSVector4i GPU::CalculateDrawRect(s32 window_width, s32 window_height, bool apply_aspect_ratio /* = true */) const
{
const bool integer_scale = (g_settings.display_scaling == DisplayScalingMode::NearestInteger ||
g_settings.display_scaling == DisplayScalingMode::BlinearInteger);
Expand Down Expand Up @@ -2401,9 +2409,11 @@ Common::Rectangle<s32> GPU::CalculateDrawRect(s32 window_width, s32 window_heigh
}

// TODO: This should be a float rectangle. But because GL is lame, it only has integer viewports...
return Common::Rectangle<s32>::FromExtents(
static_cast<s32>(active_left * scale + left_padding), static_cast<s32>(active_top * scale + top_padding),
static_cast<s32>(active_width * scale), static_cast<s32>(active_height * scale));
const s32 left = static_cast<s32>(active_left * scale + left_padding);
const s32 top = static_cast<s32>(active_top * scale + top_padding);
const s32 right = left + static_cast<s32>(active_width * scale);
const s32 bottom = top + static_cast<s32>(active_height * scale);
return GSVector4i(left, top, right, bottom);
}

bool CompressAndWriteTextureToFile(u32 width, u32 height, std::string filename, FileSystem::ManagedCFilePtr fp,
Expand Down Expand Up @@ -2575,7 +2585,7 @@ bool GPU::WriteDisplayTextureToFile(std::string filename, bool compress_on_threa
flip_y, std::move(texture_data), texture_data_stride, m_display_texture->GetFormat(), false, compress_on_thread);
}

bool GPU::RenderScreenshotToBuffer(u32 width, u32 height, const Common::Rectangle<s32>& draw_rect, bool postfx,
bool GPU::RenderScreenshotToBuffer(u32 width, u32 height, const GSVector4i draw_rect, bool postfx,
std::vector<u32>* out_pixels, u32* out_stride, GPUTexture::Format* out_format)
{
const GPUTexture::Format hdformat =
Expand Down Expand Up @@ -2627,15 +2637,15 @@ bool GPU::RenderScreenshotToFile(std::string filename, DisplayScreenshotMode mod
{
u32 width = g_gpu_device->GetWindowWidth();
u32 height = g_gpu_device->GetWindowHeight();
Common::Rectangle<s32> draw_rect = CalculateDrawRect(width, height);
GSVector4i draw_rect = CalculateDrawRect(width, height, true);

const bool internal_resolution = (mode != DisplayScreenshotMode::ScreenResolution || g_settings.debugging.show_vram);
if (internal_resolution && m_display_texture_view_width != 0 && m_display_texture_view_height != 0)
{
if (mode == DisplayScreenshotMode::InternalResolution)
{
const u32 draw_width = static_cast<u32>(draw_rect.GetWidth());
const u32 draw_height = static_cast<u32>(draw_rect.GetHeight());
const u32 draw_width = static_cast<u32>(draw_rect.width());
const u32 draw_height = static_cast<u32>(draw_rect.height());

// If internal res, scale the computed draw rectangle to the internal res.
// We re-use the draw rect because it's already been AR corrected.
Expand Down Expand Up @@ -2679,7 +2689,7 @@ bool GPU::RenderScreenshotToFile(std::string filename, DisplayScreenshotMode mod
}

// Remove padding, it's not part of the framebuffer.
draw_rect.Set(0, 0, static_cast<s32>(width), static_cast<s32>(height));
draw_rect = GSVector4i(0, 0, static_cast<s32>(width), static_cast<s32>(height));
}
if (width == 0 || height == 0)
return false;
Expand Down
31 changes: 16 additions & 15 deletions src/core/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "common/bitfield.h"
#include "common/fifo_queue.h"
#include "common/rectangle.h"
#include "common/types.h"

#include <algorithm>
Expand Down Expand Up @@ -208,13 +207,13 @@ class GPU
virtual void FlushRender() = 0;

/// Helper function for computing the draw rectangle in a larger window.
Common::Rectangle<s32> CalculateDrawRect(s32 window_width, s32 window_height, bool apply_aspect_ratio = true) const;
GSVector4i CalculateDrawRect(s32 window_width, s32 window_height, bool apply_aspect_ratio = true) const;

/// Helper function to save current display texture to PNG.
bool WriteDisplayTextureToFile(std::string filename, bool compress_on_thread = false);

/// Renders the display, optionally with postprocessing to the specified image.
bool RenderScreenshotToBuffer(u32 width, u32 height, const Common::Rectangle<s32>& draw_rect, bool postfx,
bool RenderScreenshotToBuffer(u32 width, u32 height, const GSVector4i draw_rect, bool postfx,
std::vector<u32>* out_pixels, u32* out_stride, GPUTexture::Format* out_format);

/// Helper function to save screenshot to PNG.
Expand Down Expand Up @@ -276,6 +275,9 @@ class GPU
/// 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); }

/// Updates drawing area that's suitablef or clamping.
void SetClampedDrawingArea();

/// Sets/decodes GP0(E1h) (set draw mode).
void SetDrawMode(u16 bits);

Expand Down Expand Up @@ -323,23 +325,21 @@ class GPU
virtual void DrawRendererStats();
virtual void OnBufferSwapped();

ALWAYS_INLINE_RELEASE void AddDrawTriangleTicks(s32 x1, s32 y1, s32 x2, s32 y2, s32 x3, s32 y3, bool shaded,
ALWAYS_INLINE_RELEASE void AddDrawTriangleTicks(GSVector4i v1, GSVector4i v2, GSVector4i v3, bool shaded,
bool textured, bool semitransparent)
{
// 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.
// TODO: Coordinates are exclusive, so off by one here...
const s32 clip_right = static_cast<s32>(m_drawing_area.right) + 1;
const s32 clip_bottom = static_cast<s32>(m_drawing_area.bottom) + 1;
x1 = std::clamp(x1, static_cast<s32>(m_drawing_area.left), clip_right);
x2 = std::clamp(x2, static_cast<s32>(m_drawing_area.left), clip_right);
x3 = std::clamp(x3, static_cast<s32>(m_drawing_area.left), clip_right);
y1 = std::clamp(y1, static_cast<s32>(m_drawing_area.top), clip_bottom);
y2 = std::clamp(y2, static_cast<s32>(m_drawing_area.top), clip_bottom);
y3 = std::clamp(y3, static_cast<s32>(m_drawing_area.top), clip_bottom);

TickCount pixels = std::abs((x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2);
const GSVector4i clamp_min = m_clamped_drawing_area; // would be xyxy(), but zw isn't used.
const GSVector4i clamp_max = m_clamped_drawing_area.zwzw();
v1 = v1.sat_i32(clamp_min, clamp_max);
v2 = v2.sat_i32(clamp_min, clamp_max);
v3 = v3.sat_i32(clamp_min, clamp_max);

TickCount pixels =
std::abs((v1.x * v2.y + v2.x * v3.y + v3.x * v1.y - v1.x * v3.y - v2.x * v1.y - v3.x * v2.y) / 2);
if (textured)
pixels += pixels;
if (semitransparent || m_GPUSTAT.check_mask_before_draw)
Expand Down Expand Up @@ -479,6 +479,7 @@ class GPU

GPUDrawingArea m_drawing_area = {};
GPUDrawingOffset m_drawing_offset = {};
GSVector4i m_clamped_drawing_area = {};

bool m_console_is_pal = false;
bool m_set_texture_disable_mask = false;
Expand Down Expand Up @@ -609,7 +610,7 @@ class GPU
void SetDisplayTexture(GPUTexture* texture, GPUTexture* depth_texture, s32 view_x, s32 view_y, s32 view_width,
s32 view_height);

bool RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_rect, bool postfx);
bool RenderDisplay(GPUTexture* target, const GSVector4i draw_rect, bool postfx);

bool Deinterlace(u32 field, u32 line_skip);
bool DeinterlaceExtractField(u32 dst_bufidx, GPUTexture* src, u32 x, u32 y, u32 width, u32 height, u32 line_skip);
Expand Down
2 changes: 2 additions & 0 deletions src/core/gpu_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ bool GPU::HandleSetDrawingAreaTopLeftCommand()
m_drawing_area.left = left;
m_drawing_area.top = top;
m_drawing_area_changed = true;
SetClampedDrawingArea();
}

AddCommandTicks(1);
Expand All @@ -271,6 +272,7 @@ bool GPU::HandleSetDrawingAreaBottomRightCommand()
m_drawing_area.right = right;
m_drawing_area.bottom = bottom;
m_drawing_area_changed = true;
SetClampedDrawingArea();
}

AddCommandTicks(1);
Expand Down
Loading

0 comments on commit 9ef7e8c

Please sign in to comment.