Skip to content

Commit 4866518

Browse files
Merge pull request #16477 from hrydgard/invalidation-refactor
Add a flags parameter to InvalidateCachedState and rename it Invalidate.
2 parents 60771d7 + 331d024 commit 4866518

24 files changed

+88
-75
lines changed

Common/GPU/D3D11/thin3d_d3d11.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class D3D11DrawContext : public DrawContext {
9999

100100
void GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) override;
101101

102-
void InvalidateCachedState() override;
102+
void Invalidate(InvalidationFlags flags) override;
103103

104104
void BindTextures(int start, int count, Texture **textures, TextureBindFlags flags) override;
105105
void BindNativeTexture(int index, void *nativeTexture) override;
@@ -1080,17 +1080,19 @@ void D3D11DrawContext::UpdateDynamicUniformBuffer(const void *ub, size_t size) {
10801080
context_->Unmap(curPipeline_->dynamicUniforms, 0);
10811081
}
10821082

1083-
void D3D11DrawContext::InvalidateCachedState() {
1084-
// This is a signal to forget all our state caching.
1085-
curBlend_ = nullptr;
1086-
curDepthStencil_ = nullptr;
1087-
curRaster_ = nullptr;
1088-
curPS_ = nullptr;
1089-
curVS_ = nullptr;
1090-
curGS_ = nullptr;
1091-
curInputLayout_ = nullptr;
1092-
curTopology_ = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
1093-
curPipeline_ = nullptr;
1083+
void D3D11DrawContext::Invalidate(InvalidationFlags flags) {
1084+
if (flags & InvalidationFlags::CACHED_RENDER_STATE) {
1085+
// This is a signal to forget all our state caching.
1086+
curBlend_ = nullptr;
1087+
curDepthStencil_ = nullptr;
1088+
curRaster_ = nullptr;
1089+
curPS_ = nullptr;
1090+
curVS_ = nullptr;
1091+
curGS_ = nullptr;
1092+
curInputLayout_ = nullptr;
1093+
curTopology_ = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
1094+
curPipeline_ = nullptr;
1095+
}
10941096
}
10951097

10961098
void D3D11DrawContext::BindPipeline(Pipeline *pipeline) {
@@ -1698,7 +1700,7 @@ void D3D11DrawContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const Ren
16981700
}
16991701

17001702
if (invalidationCallback_) {
1701-
invalidationCallback_(InvalidationFlags::RENDER_PASS_STATE);
1703+
invalidationCallback_(InvalidationCallbackFlags::RENDER_PASS_STATE);
17021704
}
17031705
}
17041706

Common/GPU/D3D9/thin3d_d3d9.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ class D3D9Context : public DrawContext {
610610

611611
void HandleEvent(Event ev, int width, int height, void *param1, void *param2) override;
612612

613-
void InvalidateCachedState() override;
613+
void Invalidate(InvalidationFlags flags) override;
614614

615615
void SetInvalidationCallback(InvalidationCallback callback) override {
616616
invalidationCallback_ = callback;
@@ -650,8 +650,10 @@ class D3D9Context : public DrawContext {
650650
InvalidationCallback invalidationCallback_;
651651
};
652652

653-
void D3D9Context::InvalidateCachedState() {
654-
curPipeline_ = nullptr;
653+
void D3D9Context::Invalidate(InvalidationFlags flags) {
654+
if (flags & InvalidationFlags::CACHED_RENDER_STATE) {
655+
curPipeline_ = nullptr;
656+
}
655657
}
656658

657659
// TODO: Move this detection elsewhere when it's needed elsewhere, not before. It's ugly.
@@ -1323,7 +1325,7 @@ void D3D9Context::BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPa
13231325
dxstate.viewport.restore();
13241326

13251327
if (invalidationCallback_) {
1326-
invalidationCallback_(InvalidationFlags::RENDER_PASS_STATE);
1328+
invalidationCallback_(InvalidationCallbackFlags::RENDER_PASS_STATE);
13271329
}
13281330
}
13291331

Common/GPU/MiscTypes.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
#include "Common/Common.h"
44

55
enum class InvalidationFlags {
6+
CACHED_RENDER_STATE = 1,
7+
};
8+
ENUM_CLASS_BITOPS(InvalidationFlags);
9+
10+
enum class InvalidationCallbackFlags {
611
RENDER_PASS_STATE = 1,
712
COMMAND_BUFFER_STATE = 2,
813
};
9-
ENUM_CLASS_BITOPS(InvalidationFlags);
14+
ENUM_CLASS_BITOPS(InvalidationCallbackFlags);
1015

11-
typedef std::function<void(InvalidationFlags)> InvalidationCallback;
16+
typedef std::function<void(InvalidationCallbackFlags)> InvalidationCallback;

Common/GPU/OpenGL/GLRenderManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void GLRenderManager::BindFramebufferAsRenderTarget(GLRFramebuffer *fb, GLRRende
356356
}
357357

358358
if (invalidationCallback_) {
359-
invalidationCallback_(InvalidationFlags::RENDER_PASS_STATE);
359+
invalidationCallback_(InvalidationCallbackFlags::RENDER_PASS_STATE);
360360
}
361361
}
362362

Common/GPU/OpenGL/thin3d_gl.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ class OpenGLContext : public DrawContext {
467467

468468
void HandleEvent(Event ev, int width, int height, void *param1, void *param2) override {}
469469

470-
void InvalidateCachedState() override;
470+
void Invalidate(InvalidationFlags flags) override;
471471

472472
void SetInvalidationCallback(InvalidationCallback callback) override {
473473
renderManager_.SetInvalidationCallback(callback);
@@ -761,18 +761,20 @@ void OpenGLContext::EndFrame() {
761761
renderManager_.EndPushBuffer(frameData.push); // upload the data!
762762
renderManager_.Finish();
763763

764-
InvalidateCachedState();
764+
Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
765765
}
766766

767-
void OpenGLContext::InvalidateCachedState() {
768-
// Unbind stuff.
769-
for (auto &texture : boundTextures_) {
770-
texture = nullptr;
771-
}
772-
for (auto &sampler : boundSamplers_) {
773-
sampler = nullptr;
767+
void OpenGLContext::Invalidate(InvalidationFlags flags) {
768+
if (flags & InvalidationFlags::CACHED_RENDER_STATE) {
769+
// Unbind stuff.
770+
for (auto &texture : boundTextures_) {
771+
texture = nullptr;
772+
}
773+
for (auto &sampler : boundSamplers_) {
774+
sampler = nullptr;
775+
}
776+
curPipeline_ = nullptr;
774777
}
775-
curPipeline_ = nullptr;
776778
}
777779

778780
InputLayout *OpenGLContext::CreateInputLayout(const InputLayoutDesc &desc) {

Common/GPU/Vulkan/VulkanRenderManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRR
752752
}
753753

754754
if (invalidationCallback_) {
755-
invalidationCallback_(InvalidationFlags::RENDER_PASS_STATE);
755+
invalidationCallback_(InvalidationCallbackFlags::RENDER_PASS_STATE);
756756
}
757757
}
758758

@@ -1213,7 +1213,7 @@ void VulkanRenderManager::Run(VKRRenderThreadTask &task) {
12131213
// Called from main thread.
12141214
void VulkanRenderManager::FlushSync() {
12151215
if (invalidationCallback_) {
1216-
invalidationCallback_(InvalidationFlags::COMMAND_BUFFER_STATE);
1216+
invalidationCallback_(InvalidationCallbackFlags::COMMAND_BUFFER_STATE);
12171217
}
12181218

12191219
int curFrame = vulkan_->GetCurFrame();

Common/GPU/Vulkan/VulkanRenderManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,5 +526,5 @@ class VulkanRenderManager {
526526
SimpleStat totalGPUTimeMs_;
527527
SimpleStat renderCPUTimeMs_;
528528

529-
std::function<void(InvalidationFlags)> invalidationCallback_;
529+
std::function<void(InvalidationCallbackFlags)> invalidationCallback_;
530530
};

Common/GPU/Vulkan/thin3d_vulkan.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class VKContext : public DrawContext {
498498

499499
void HandleEvent(Event ev, int width, int height, void *param1, void *param2) override;
500500

501-
void InvalidateCachedState() override;
501+
void Invalidate(InvalidationFlags flags) override;
502502

503503
void InvalidateFramebuffer(FBInvalidationStage stage, uint32_t channels) override;
504504

@@ -1017,20 +1017,22 @@ void VKContext::EndFrame() {
10171017
push_ = nullptr;
10181018

10191019
// Unbind stuff, to avoid accidentally relying on it across frames (and provide some protection against forgotten unbinds of deleted things).
1020-
InvalidateCachedState();
1020+
Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
10211021
}
10221022

1023-
void VKContext::InvalidateCachedState() {
1024-
curPipeline_ = nullptr;
1023+
void VKContext::Invalidate(InvalidationFlags flags) {
1024+
if (flags & InvalidationFlags::CACHED_RENDER_STATE) {
1025+
curPipeline_ = nullptr;
10251026

1026-
for (auto &view : boundImageView_) {
1027-
view = VK_NULL_HANDLE;
1028-
}
1029-
for (auto &sampler : boundSamplers_) {
1030-
sampler = nullptr;
1031-
}
1032-
for (auto &texture : boundTextures_) {
1033-
texture = nullptr;
1027+
for (auto &view : boundImageView_) {
1028+
view = VK_NULL_HANDLE;
1029+
}
1030+
for (auto &sampler : boundSamplers_) {
1031+
sampler = nullptr;
1032+
}
1033+
for (auto &texture : boundTextures_) {
1034+
texture = nullptr;
1035+
}
10341036
}
10351037
}
10361038

Common/GPU/thin3d.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ class DrawContext {
737737
// Clear state cached within thin3d. Must be called after directly calling API functions.
738738
// Note that framebuffer state (which framebuffer is bounds) may not be cached.
739739
// Must not actually perform any API calls itself since this can be called when no framebuffer is bound for rendering.
740-
virtual void InvalidateCachedState() = 0;
740+
virtual void Invalidate(InvalidationFlags flags) = 0;
741741

742742
virtual void BindPipeline(Pipeline *pipeline) = 0;
743743

GPU/Common/Draw2D.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void Draw2D::DrawStrip2D(Draw::Texture *tex, Draw2DVertex *verts, int vertexCoun
330330
draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, linearFilter ? &draw2DSamplerLinear_ : &draw2DSamplerNearest_);
331331
draw_->DrawUP(verts, vertexCount);
332332

333-
draw_->InvalidateCachedState();
333+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
334334

335335
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE | DIRTY_VERTEXSHADER_STATE);
336336
}

GPU/Common/FramebufferManagerCommon.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ void FramebufferManagerCommon::CopyToDepthFromOverlappingFramebuffers(VirtualFra
664664

665665
// for (auto &source : sources) {
666666
if (!sources.empty()) {
667-
draw_->InvalidateCachedState();
667+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
668668

669669
auto &source = sources.back();
670670
if (source.channel == RASTER_DEPTH) {
@@ -804,7 +804,7 @@ void FramebufferManagerCommon::CopyToColorFromOverlappingFramebuffers(VirtualFra
804804

805805
std::sort(sources.begin(), sources.end());
806806

807-
draw_->InvalidateCachedState();
807+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
808808

809809
bool tookActions = false;
810810

@@ -961,7 +961,7 @@ void FramebufferManagerCommon::BlitFramebufferDepth(VirtualFramebuffer *src, Vir
961961
RebindFramebuffer("After BlitFramebufferDepth");
962962
}
963963

964-
draw_->InvalidateCachedState();
964+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
965965
}
966966

967967
void FramebufferManagerCommon::NotifyRenderFramebufferCreated(VirtualFramebuffer *vfb) {
@@ -1137,7 +1137,7 @@ void FramebufferManagerCommon::DrawPixels(VirtualFramebuffer *vfb, int dstX, int
11371137

11381138
gpuStats.numUploads++;
11391139
pixelsTex->Release();
1140-
draw_->InvalidateCachedState();
1140+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
11411141

11421142
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
11431143
}
@@ -2777,7 +2777,7 @@ void FramebufferManagerCommon::ReadFramebufferToMemory(VirtualFramebuffer *vfb,
27772777
}
27782778
}
27792779

2780-
draw_->InvalidateCachedState();
2780+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
27812781
textureCache_->ForgetLastTexture();
27822782
RebindFramebuffer("RebindFramebuffer - ReadFramebufferToMemory");
27832783
}
@@ -2840,7 +2840,7 @@ void FramebufferManagerCommon::DownloadFramebufferForClut(u32 fb_address, u32 lo
28402840
}
28412841

28422842
void FramebufferManagerCommon::RebindFramebuffer(const char *tag) {
2843-
draw_->InvalidateCachedState();
2843+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
28442844
shaderManager_->DirtyLastShader();
28452845
if (currentRenderVfb_ && currentRenderVfb_->fbo) {
28462846
draw_->BindFramebufferAsRenderTarget(currentRenderVfb_->fbo, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::KEEP }, tag);
@@ -3064,7 +3064,7 @@ void FramebufferManagerCommon::BlitFramebuffer(VirtualFramebuffer *dst, int dstX
30643064
BlitUsingRaster(srcFBO, srcX1, srcY1, srcX2, srcY2, dst->fbo, dstX1, dstY1, dstX2, dstY2, false, dst->renderScaleFactor, pipeline, tag);
30653065
}
30663066

3067-
draw_->InvalidateCachedState();
3067+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
30683068

30693069
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
30703070
}

GPU/Common/PresentationCommon.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ void PresentationCommon::UpdateUniforms(bool hasVideo) {
605605
}
606606

607607
void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u0, float v0, float u1, float v1) {
608-
draw_->InvalidateCachedState();
608+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
609609

610610
// TODO: If shader objects have been created by now, we might have received errors.
611611
// GLES can have the shader fail later, shader->failed / shader->error.
@@ -869,7 +869,7 @@ void PresentationCommon::CopyToOutput(OutputFlags flags, int uvRotation, float u
869869
DoRelease(srcTexture_);
870870

871871
// Unbinds all textures and samplers too, needed since sometimes a MakePixelTexture is deleted etc.
872-
draw_->InvalidateCachedState();
872+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
873873

874874
previousUniforms_ = uniforms;
875875
}

GPU/Common/StencilCommon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ bool FramebufferManagerCommon::PerformWriteStencilFromMemory(u32 addr, int size,
357357
}
358358
tex->Release();
359359

360-
draw_->InvalidateCachedState();
360+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
361361
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
362362
return true;
363363
}

GPU/Common/TextureCacheCommon.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
21872187
CheckAlphaResult alphaStatus = CheckCLUTAlpha((const uint8_t *)clutBufRaw_, clutFormat, clutTotalColors);
21882188
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
21892189

2190-
draw_->InvalidateCachedState();
2190+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
21912191
InvalidateLastTexture();
21922192
return;
21932193
}
@@ -2262,7 +2262,7 @@ void TextureCacheCommon::ApplyTextureFramebuffer(VirtualFramebuffer *framebuffer
22622262
CheckAlphaResult alphaStatus = CheckCLUTAlpha((const uint8_t *)clutBufRaw_, clutFormat, clutTotalColors);
22632263
gstate_c.SetTextureFullAlpha(alphaStatus == CHECKALPHA_FULL);
22642264

2265-
draw_->InvalidateCachedState();
2265+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
22662266
shaderManager_->DirtyLastShader();
22672267
} else {
22682268
framebufferManager_->RebindFramebuffer("ApplyTextureFramebuffer");
@@ -2363,7 +2363,7 @@ void TextureCacheCommon::ApplyTextureDepal(TexCacheEntry *entry) {
23632363
// We don't know about alpha at all.
23642364
gstate_c.SetTextureFullAlpha(false);
23652365

2366-
draw_->InvalidateCachedState();
2366+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
23672367
shaderManager_->DirtyLastShader();
23682368

23692369
SamplerCacheKey samplerKey = GetFramebufferSamplingParams(texWidth, texHeight);

GPU/D3D11/DrawEngineD3D11.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ VertexArrayInfoD3D11::~VertexArrayInfoD3D11() {
327327
}
328328

329329
// In D3D, we're synchronous and state carries over so all we reset here on a new step is the viewport/scissor.
330-
void DrawEngineD3D11::Invalidate(InvalidationFlags flags) {
331-
if (flags & InvalidationFlags::RENDER_PASS_STATE) {
330+
void DrawEngineD3D11::Invalidate(InvalidationCallbackFlags flags) {
331+
if (flags & InvalidationCallbackFlags::RENDER_PASS_STATE) {
332332
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
333333
}
334334
}

GPU/D3D11/DrawEngineD3D11.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class DrawEngineD3D11 : public DrawEngineCommon {
155155
void ClearInputLayoutMap();
156156

157157
private:
158-
void Invalidate(InvalidationFlags flags);
158+
void Invalidate(InvalidationCallbackFlags flags);
159159

160160
void DoFlush();
161161

GPU/D3D11/GPU_D3D11.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void GPU_D3D11::BuildReportingInfo() {
131131
}
132132

133133
void GPU_D3D11::DeviceLost() {
134-
draw_->InvalidateCachedState();
134+
draw_->Invalidate(InvalidationFlags::CACHED_RENDER_STATE);
135135
// Simply drop all caches and textures.
136136
// FBOs appear to survive? Or no?
137137
shaderManagerD3D11_->ClearShaders();

GPU/Directx9/DrawEngineDX9.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ void DrawEngineDX9::BeginFrame() {
312312
}
313313

314314
// In D3D, we're synchronous and state carries over so all we reset here on a new step is the viewport/scissor.
315-
void DrawEngineDX9::Invalidate(InvalidationFlags flags) {
316-
if (flags & InvalidationFlags::RENDER_PASS_STATE) {
315+
void DrawEngineDX9::Invalidate(InvalidationCallbackFlags flags) {
316+
if (flags & InvalidationCallbackFlags::RENDER_PASS_STATE) {
317317
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
318318
}
319319
}

GPU/Directx9/DrawEngineDX9.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class DrawEngineDX9 : public DrawEngineCommon {
144144
void DecimateTrackedVertexArrays();
145145

146146
private:
147-
void Invalidate(InvalidationFlags flags);
147+
void Invalidate(InvalidationCallbackFlags flags);
148148
void DoFlush();
149149

150150
void ApplyDrawState(int prim);

0 commit comments

Comments
 (0)