Skip to content

Commit

Permalink
GS:MTL: Experimental small rt mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TellowKrinkle committed Mar 21, 2022
1 parent 8146dc3 commit e85823e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class GSDeviceMTL final : public GSDevice

// MARK: Configuration
simd::float3 m_shadeboost_constants;
int m_mipmap;
bool m_enable_small_rt;

// MARK: Permanent resources
std::shared_ptr<std::pair<std::mutex, GSDeviceMTL*>> m_backref;
Expand Down Expand Up @@ -261,6 +261,7 @@ class GSDeviceMTL final : public GSDevice
GSTexture* tex[8] = {};
void* vertex_buffer = nullptr;
void* name = nullptr;
GSVector2i size = GSVector2i(0, 0);
struct Has
{
bool cb_vs : 1;
Expand Down Expand Up @@ -327,7 +328,7 @@ class GSDeviceMTL final : public GSDevice
/// End current render pass without flushing
void EndRenderPass();
/// Begin a new render pass (may reuse existing)
void BeginRenderPass(NSString* name, GSTexture* color, MTLLoadAction color_load, GSTexture* depth, MTLLoadAction depth_load, GSTexture* stencil = nullptr, MTLLoadAction stencil_load = MTLLoadActionDontCare);
void BeginRenderPass(NSString* name, GSTexture* color, MTLLoadAction color_load, GSTexture* depth, MTLLoadAction depth_load, GSTexture* stencil = nullptr, MTLLoadAction stencil_load = MTLLoadActionDontCare, GSVector2i size = GSVector2i(0, 0));

GSTexture* CreateSurface(GSTexture::Type type, int width, int height, int levels, GSTexture::Format format) override;

Expand Down
29 changes: 26 additions & 3 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
, m_dev(nil)
{
m_backref->second = this;
m_mipmap = theApp.GetConfigI("mipmap");
}

GSDeviceMTL::~GSDeviceMTL()
Expand Down Expand Up @@ -309,7 +308,7 @@
}
}

void GSDeviceMTL::BeginRenderPass(NSString* name, GSTexture* color, MTLLoadAction color_load, GSTexture* depth, MTLLoadAction depth_load, GSTexture* stencil, MTLLoadAction stencil_load)
void GSDeviceMTL::BeginRenderPass(NSString* name, GSTexture* color, MTLLoadAction color_load, GSTexture* depth, MTLLoadAction depth_load, GSTexture* stencil, MTLLoadAction stencil_load, GSVector2i size)
{
GSTextureMTL* mc = static_cast<GSTextureMTL*>(color);
GSTextureMTL* md = static_cast<GSTextureMTL*>(depth);
Expand All @@ -332,6 +331,12 @@
needs_new |= mc && color_load == MTLLoadActionClear;
needs_new |= md && depth_load == MTLLoadActionClear;
needs_new |= ms && stencil_load == MTLLoadActionClear;
if (m_current_render.size != GSVector2i(0, 0))
{
needs_new |= size == GSVector2i(0, 0);
needs_new |= size.x > m_current_render.size.x;
needs_new |= size.y > m_current_render.size.y;
}

if (!needs_new)
{
Expand Down Expand Up @@ -380,6 +385,12 @@
desc.stencilAttachment.loadAction = stencil_load;
}

if (@available(macOS 10.15, iOS 11, *))
{
desc.renderTargetWidth = size.x;
desc.renderTargetHeight = size.y;
}

EndRenderPass();
m_current_render.encoder = MRCRetain([GetRenderCmdBuf() renderCommandEncoderWithDescriptor:desc]);
m_current_render.name = (__bridge void*)name;
Expand All @@ -390,6 +401,12 @@
m_current_render.color_target = color;
m_current_render.depth_target = depth;
m_current_render.stencil_target = stencil;
m_current_render.size = size;
if (size != GSVector2i(0, 0))
{
GSVector2i rtsize = color ? color->GetSize() : depth ? depth->GetSize() : stencil->GetSize();
[m_current_render.encoder setViewport: (MTLViewport){ .originX = 0, .originY = 0, .width = (double)rtsize.x, .height = (double)rtsize.y, .znear = 0, .zfar = 1 }];
}
pxAssertRel(m_current_render.encoder, "Failed to create render encoder!");
}

Expand Down Expand Up @@ -652,6 +669,11 @@ static void setFnConstantI(MTLFunctionConstantValues* fc, unsigned int value, GS
else
m_spin_enable = false;

if (const char* env = getenv("MTL_SMALL_RT"))
m_enable_small_rt = env[0] == '1' || env[0] == 'y' || env[0] == 'Y';
else
m_enable_small_rt = false;

m_features.geometry_shader = false;
m_features.image_load_store = m_dev.features.primid;
m_features.texture_barrier = true;
Expand Down Expand Up @@ -1590,7 +1612,8 @@ void bitcpy(Dst& dst, const Src& src)
if (!config.ds && m_current_render.color_target == rt && stencil == m_current_render.stencil_target && m_current_render.depth_target != config.tex)
config.ds = m_current_render.depth_target;

BeginRenderPass(@"RenderHW", rt, MTLLoadActionLoad, config.ds, MTLLoadActionLoad, stencil, MTLLoadActionLoad);
GSVector2i rtsize = m_enable_small_rt ? GSVector2i(config.scissor.z, config.scissor.w) : GSVector2i(0, 0);
BeginRenderPass(@"RenderHW", rt, MTLLoadActionLoad, config.ds, MTLLoadActionLoad, stencil, MTLLoadActionLoad, rtsize);
id<MTLRenderCommandEncoder> mtlenc = m_current_render.encoder;
FlushDebugEntries(mtlenc);
MREInitHWDraw(config, allocation);
Expand Down

0 comments on commit e85823e

Please sign in to comment.