Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Common/GPU/Vulkan/VulkanRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,21 @@ class CreateMultiPipelinesTask : public Task {

// Use during shutdown to make sure there aren't any leftover tasks sitting queued.
// Could probably be done more elegantly. Like waiting for all tasks of a type, or saving pointers to them, or something...
static void WaitForAll();
// Returns the maximum value of tasks in flight seen during the wait.
static int WaitForAll();
static std::atomic<int> tasksInFlight_;
};

void CreateMultiPipelinesTask::WaitForAll() {
while (tasksInFlight_.load() > 0) {
int CreateMultiPipelinesTask::WaitForAll() {
int inFlight = 0;
int maxInFlight = 0;
while ((inFlight = tasksInFlight_.load()) > 0) {
if (inFlight > maxInFlight) {
maxInFlight = inFlight;
}
sleep_ms(2, "create-multi-pipelines-wait");
}
return maxInFlight;
}

std::atomic<int> CreateMultiPipelinesTask::tasksInFlight_;
Expand Down Expand Up @@ -410,7 +417,7 @@ void VulkanRenderManager::StopThreads() {
presentWaitThread_.join();
}

INFO_LOG(Log::G3D, "Vulkan compiler thread joined. Now wait for any straggling compile tasks.");
INFO_LOG(Log::G3D, "Vulkan compiler thread joined. Now wait for any straggling compile tasks. runCompileThread_ = %d", (int)runCompileThread_);
CreateMultiPipelinesTask::WaitForAll();

{
Expand Down Expand Up @@ -775,6 +782,10 @@ void VulkanRenderManager::ReportBadStateForDraw() {
ERROR_LOG_REPORT_ONCE(baddraw, Log::G3D, "Can't draw: %s%s. Step count: %d", cause1, cause2, (int)steps_.size());
}

int VulkanRenderManager::WaitForPipelines() {
return CreateMultiPipelinesTask::WaitForAll();
}

VKRGraphicsPipeline *VulkanRenderManager::CreateGraphicsPipeline(VKRGraphicsPipelineDesc *desc, PipelineFlags pipelineFlags, uint32_t variantBitmask, VkSampleCountFlagBits sampleCount, bool cacheLoad, const char *tag) {
if (!desc->vertexShader || !desc->fragmentShader) {
ERROR_LOG(Log::G3D, "Can't create graphics pipeline with missing vs/ps: %p %p", desc->vertexShader, desc->fragmentShader);
Expand Down
2 changes: 2 additions & 0 deletions Common/GPU/Vulkan/VulkanRenderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ class VulkanRenderManager {

void ReportBadStateForDraw();

int WaitForPipelines();

void NudgeCompilerThread() {
compileQueueMutex_.lock();
compileCond_.notify_one();
Expand Down
4 changes: 3 additions & 1 deletion Common/Render/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ bool Atlas::Load(const uint8_t *data, size_t data_size) {
return false;
}

images = reader.ReadMultipleAlloc<AtlasImage>(num_images, header.version >= 1);
delete[] images;
delete[] fonts;

images = reader.ReadMultipleAlloc<AtlasImage>(num_images, header.version >= 1);
fonts = new AtlasFont[num_fonts];
for (int i = 0; i < num_fonts; i++) {
AtlasFontHeader font_header = reader.Read<AtlasFontHeader>();
Expand Down
2 changes: 1 addition & 1 deletion Core/CoreParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "Core/Compatibility.h"
#include "Core/Loaders.h"

enum GPUCore {
enum GPUCore : int {
GPUCORE_GLES,
GPUCORE_SOFTWARE,
GPUCORE_DIRECTX9,
Expand Down
41 changes: 20 additions & 21 deletions Core/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,21 @@ bool PSP_InitStart(const CoreParameter &coreParam) {
return;
}

// Initialize the GPU as far as we can here (do things like load cache files).

if (!gpu) { // should be!
INFO_LOG(Log::Loader, "Starting graphics...");
Draw::DrawContext *draw = g_CoreParameter.graphicsContext ? g_CoreParameter.graphicsContext->GetDrawContext() : nullptr;
// This set the `gpu` global.
GPUCore gpuCore = PSP_CoreParameter().gpuCore;
bool success = GPU_Init(gpuCore, g_CoreParameter.graphicsContext, draw);
if (!success) {
*error_string = "Unable to initialize rendering engine.";
CPU_Shutdown(false);
g_bootState = BootState::Failed;
}
}

g_bootState = BootState::Complete;
});

Expand Down Expand Up @@ -604,27 +619,11 @@ BootState PSP_InitUpdate(std::string *error_string) {
return BootState::Failed;
}

// Ok, async boot completed, let's finish up things on the main thread.
if (!gpu) { // should be!
INFO_LOG(Log::Loader, "Starting graphics...");
Draw::DrawContext *draw = g_CoreParameter.graphicsContext ? g_CoreParameter.graphicsContext->GetDrawContext() : nullptr;
// This set the `gpu` global.
bool success = GPU_Init(g_CoreParameter.graphicsContext, draw);
if (!success) {
*error_string = "Unable to initialize rendering engine.";
PSP_Shutdown(false);
g_bootState = BootState::Off;
return BootState::Failed;
}
}

// TODO: This should all be checked during GPU_Init.
if (!GPU_IsStarted()) {
*error_string = "Unable to initialize rendering engine.";
PSP_Shutdown(false);
g_bootState = BootState::Off;
Core_NotifyLifecycle(CoreLifecycle::START_COMPLETE);
return BootState::Failed;
// Ok, async part of the boot completed, let's finish up things on the main thread.
if (gpu) {
gpu->FinishInitOnMainThread();
} else {
_dbg_assert_(gpu);
}

Core_NotifyLifecycle(CoreLifecycle::START_COMPLETE);
Expand Down
2 changes: 1 addition & 1 deletion GPU/Common/FramebufferManagerCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2783,7 +2783,7 @@ void FramebufferManagerCommon::NotifyBlockTransferAfter(u32 dstBasePtr, int dstS

// A few games use this INSTEAD of actually drawing the video image to the screen, they just blast it to
// the backbuffer. Detect this and have the framebuffermanager draw the pixels.
if (!useBufferedRendering_ && currentRenderVfb_ != dstRect.vfb) {
if ((!useBufferedRendering_ && currentRenderVfb_ != dstRect.vfb) || dstRect.vfb == nullptr) {
return;
}

Expand Down
7 changes: 5 additions & 2 deletions GPU/D3D11/GPU_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ GPU_D3D11::GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);
D3D_FEATURE_LEVEL featureLevel = (D3D_FEATURE_LEVEL)draw->GetNativeObject(Draw::NativeObject::FEATURE_LEVEL);

stockD3D11.Create(device_);

shaderManagerD3D11_ = new ShaderManagerD3D11(draw, device_, context_, featureLevel);
framebufferManagerD3D11_ = new FramebufferManagerD3D11(draw);
framebufferManager_ = framebufferManagerD3D11_;
Expand Down Expand Up @@ -77,6 +75,11 @@ GPU_D3D11::GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
textureCache_->NotifyConfigChanged();
}

void GPU_D3D11::FinishInitOnMainThread() {
textureCacheD3D11_->InitDeviceObjects();
stockD3D11.Create(device_);
}

GPU_D3D11::~GPU_D3D11() {
stockD3D11.Destroy();
}
Expand Down
2 changes: 2 additions & 0 deletions GPU/D3D11/GPU_D3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class GPU_D3D11 : public GPUCommonHW {
GPU_D3D11(GraphicsContext *gfxCtx, Draw::DrawContext *draw);
~GPU_D3D11();

void FinishInitOnMainThread() override;

u32 CheckGPUFeatures() const override;

void GetStats(char *buffer, size_t bufsize) override;
Expand Down
24 changes: 11 additions & 13 deletions GPU/D3D11/TextureCacheD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ struct DepthPushConstants {
float pad[2];
};

#define INVALID_TEX (ID3D11ShaderResourceView *)(-1LL)

static const D3D11_INPUT_ELEMENT_DESC g_QuadVertexElements[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12,},
Expand Down Expand Up @@ -135,21 +133,21 @@ TextureCacheD3D11::TextureCacheD3D11(Draw::DrawContext *draw, Draw2D *draw2D)
device_ = (ID3D11Device *)draw->GetNativeObject(Draw::NativeObject::DEVICE);
context_ = (ID3D11DeviceContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);

lastBoundTexture = INVALID_TEX;

D3D11_BUFFER_DESC desc{ sizeof(DepthPushConstants), D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE };
HRESULT hr = device_->CreateBuffer(&desc, nullptr, &depalConstants_);
_dbg_assert_(SUCCEEDED(hr));

HRESULT result = 0;
lastBoundTexture_ = D3D11_INVALID_TEX;

nextTexture_ = nullptr;
InitDeviceObjects();
}

TextureCacheD3D11::~TextureCacheD3D11() {
Clear(true);
}

void TextureCacheD3D11::InitDeviceObjects() {
D3D11_BUFFER_DESC desc{ sizeof(DepthPushConstants), D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE };
HRESULT hr = device_->CreateBuffer(&desc, nullptr, &depalConstants_);
_dbg_assert_(SUCCEEDED(hr));
}

void TextureCacheD3D11::SetFramebufferManager(FramebufferManagerD3D11 *fbManager) {
framebufferManager_ = fbManager;
}
Expand All @@ -168,7 +166,7 @@ void TextureCacheD3D11::ReleaseTexture(TexCacheEntry *entry, bool delete_them) {
}

void TextureCacheD3D11::ForgetLastTexture() {
lastBoundTexture = INVALID_TEX;
lastBoundTexture_ = D3D11_INVALID_TEX;

ID3D11ShaderResourceView *nullTex[4]{};
context_->PSSetShaderResources(0, 4, nullTex);
Expand Down Expand Up @@ -217,9 +215,9 @@ void TextureCacheD3D11::BindTexture(TexCacheEntry *entry) {
return;
}
ID3D11ShaderResourceView *textureView = DxView(entry);
if (textureView != lastBoundTexture) {
if (textureView != lastBoundTexture_) {
context_->PSSetShaderResources(0, 1, &textureView);
lastBoundTexture = textureView;
lastBoundTexture_ = textureView;
}
int maxLevel = (entry->status & TexCacheEntry::STATUS_NO_MIPS) ? 0 : entry->maxLevel;
SamplerCacheKey samplerKey = GetSamplingParams(maxLevel, entry);
Expand Down
6 changes: 5 additions & 1 deletion GPU/D3D11/TextureCacheD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SamplerCacheD3D11 {
std::map<SamplerCacheKey, Microsoft::WRL::ComPtr<ID3D11SamplerState>> cache_;
};

#define D3D11_INVALID_TEX (ID3D11ShaderResourceView *)(-1LL)

class TextureCacheD3D11 : public TextureCacheCommon {
public:
TextureCacheD3D11(Draw::DrawContext *draw, Draw2D *draw2D);
Expand All @@ -56,6 +58,8 @@ class TextureCacheD3D11 : public TextureCacheCommon {
void DeviceLost() override { draw_ = nullptr; }
void DeviceRestore(Draw::DrawContext *draw) override { draw_ = draw; }

void InitDeviceObjects();

protected:
void BindTexture(TexCacheEntry *entry) override;
void Unbind() override;
Expand All @@ -82,7 +86,7 @@ class TextureCacheD3D11 : public TextureCacheCommon {

SamplerCacheD3D11 samplerCache_;

ID3D11ShaderResourceView *lastBoundTexture;
ID3D11ShaderResourceView *lastBoundTexture_ = D3D11_INVALID_TEX;
Microsoft::WRL::ComPtr<ID3D11Buffer> depalConstants_;
};

Expand Down
6 changes: 5 additions & 1 deletion GPU/Directx9/GPU_DX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)

// Some of our defaults are different from hw defaults, let's assert them.
// We restore each frame anyway, but here is convenient for tests.
dxstate.Restore();
textureCache_->NotifyConfigChanged();

if (g_Config.bHardwareTessellation) {
Expand All @@ -87,6 +86,11 @@ GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
}
}

void GPU_DX9::FinishInitOnMainThread() {
textureCacheDX9_->InitDeviceObjects();
dxstate.Restore();
}

u32 GPU_DX9::CheckGPUFeatures() const {
u32 features = GPUCommonHW::CheckGPUFeatures();
features |= GPU_USE_16BIT_FORMATS;
Expand Down
1 change: 1 addition & 0 deletions GPU/Directx9/GPU_DX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FramebufferManagerDX9;
class GPU_DX9 : public GPUCommonHW {
public:
GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw);
void FinishInitOnMainThread() override;

u32 CheckGPUFeatures() const override;

Expand Down
11 changes: 7 additions & 4 deletions GPU/Directx9/TextureCacheDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw, Draw2D *draw2D)
lastBoundTexture = INVALID_TEX;
device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
}

TextureCacheDX9::~TextureCacheDX9() {
Clear(true);
}

void TextureCacheDX9::InitDeviceObjects() {
D3DCAPS9 pCaps;
ZeroMemory(&pCaps, sizeof(pCaps));
HRESULT result = 0;
Expand All @@ -92,10 +99,6 @@ TextureCacheDX9::TextureCacheDX9(Draw::DrawContext *draw, Draw2D *draw2D)
device_->CreateVertexDeclaration(g_FramebufferVertexElements, &pFramebufferVertexDecl);
}

TextureCacheDX9::~TextureCacheDX9() {
Clear(true);
}

void TextureCacheDX9::SetFramebufferManager(FramebufferManagerDX9 *fbManager) {
framebufferManager_ = fbManager;
}
Expand Down
3 changes: 2 additions & 1 deletion GPU/Directx9/TextureCacheDX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TextureCacheDX9 : public TextureCacheCommon {
void DeviceLost() override { draw_ = nullptr; }
void DeviceRestore(Draw::DrawContext *draw) override { draw_ = draw; }

void InitDeviceObjects();
protected:
void BindTexture(TexCacheEntry *entry) override;
void Unbind() override;
Expand Down Expand Up @@ -73,7 +74,7 @@ class TextureCacheDX9 : public TextureCacheCommon {
IDirect3DBaseTexture9 *lastBoundTexture = nullptr;
float maxAnisotropyLevel;

FramebufferManagerDX9 *framebufferManagerDX9_;
FramebufferManagerDX9 *framebufferManagerDX9_ = nullptr;
};

static D3DFORMAT getClutDestFormat(GEPaletteFormat format);
Loading
Loading