Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to compile shaders asynchronously #2775

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions src/Combiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <functional>
#include <cstring>
#include <stdio.h>
#include <thread>
#include <osal_files.h>

#include "Combiner.h"
Expand Down Expand Up @@ -108,6 +109,13 @@ void CombinerInfo::init()
setCombine(EncodeCombineMode(0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0));
gDP.otherMode.cycleType = G_CYC_FILL;
setCombine(EncodeCombineMode(0, 0, 0, SHADE, 0, 0, 0, SHADE, 0, 0, 0, SHADE, 0, 0, 0, SHADE));

gDP.otherMode.cycleType = G_CYC_1CYCLE;
setCombine(EncodeCombineMode(0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0, 0, 0, 0, TEXEL0));
m_defaultKeys[0] = m_pCurrent->getKey();
gDP.otherMode.cycleType = G_CYC_2CYCLE;
setCombine(EncodeCombineMode(0, 0, 0, TEXEL0, 0, 0, 0, TEXEL1, 0, 0, 0, TEXEL0, 0, 0, 0, TEXEL1));
m_defaultKeys[1] = m_pCurrent->getKey();
}

m_shadowmapProgram.reset(gfxContext.createDepthFogShader());
Expand Down Expand Up @@ -299,13 +307,58 @@ void CombinerInfo::setCombine(u64 _mux )
if (iter != m_combiners.end()) {
m_pCurrent = iter->second;
} else {
if (gDP.otherMode.cycleType <= G_CYC_2CYCLE &&
config.generalEmulation.enableAsyncShadersCompilation != 0 &&
config.video.threadedVideo != 0)
{
iter = m_combiners.find(m_defaultKeys[gDP.otherMode.cycleType]);
if (iter != m_combiners.end()) {
m_pCurrent = iter->second;
if (m_unknownCombiners.insert(key).second)
m_combinersToCompile.push_back(key);
m_bChanged = true;
return;
}
}

m_pCurrent = Combiner_Compile(key);
m_pCurrent->update(true);
m_combiners[m_pCurrent->getKey()] = m_pCurrent;
}
m_bChanged = true;
}

void CombinerInfo::compileUnknownShaders()
{
#if 1
if (m_combinersToCompile.empty())
return;

std::thread task([this] {
std::unique_lock<std::mutex> lock(m_compileCombinersMutex);

for (auto& key : m_combinersToCompile)
{
m_pCurrent = Combiner_Compile(key);
m_pCurrent->update(true);
m_combiners[m_pCurrent->getKey()] = m_pCurrent;
}
m_combinersToCompile.clear();
});
task.detach();
#else
if (m_combinersToCompile.empty())
return;
for (auto& key : m_combinersToCompile)
{
m_pCurrent = Combiner_Compile(key);
m_pCurrent->update(true);
m_combiners[m_pCurrent->getKey()] = m_pCurrent;
}
m_combinersToCompile.clear();
#endif
}

void CombinerInfo::updateParameters()
{
m_pCurrent->update(false);
Expand Down
9 changes: 9 additions & 0 deletions src/Combiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <map>
#include <memory>
#include <mutex>
#include <set>

#include "GLideN64.h"
#include "GraphicsDrawer.h"
Expand Down Expand Up @@ -128,6 +130,8 @@ class CombinerInfo
void setCombine(u64 _mux);
void updateParameters();

void compileUnknownShaders();

void setDepthFogCombiner();
graphics::ShaderProgram * getTexrectUpscaleCopyProgram();
graphics::ShaderProgram * getTexrectColorAndDepthUpscaleCopyProgram();
Expand Down Expand Up @@ -163,6 +167,11 @@ class CombinerInfo
graphics::CombinerProgram * m_pCurrent;
graphics::Combiners m_combiners;

std::mutex m_compileCombinersMutex;
std::set<CombinerKey> m_unknownCombiners;
std::vector<CombinerKey> m_combinersToCompile;
std::array<CombinerKey, 2> m_defaultKeys;

std::unique_ptr<graphics::ShaderProgram> m_shadowmapProgram;
std::unique_ptr<graphics::ShaderProgram> m_texrectUpscaleCopyProgram;
std::unique_ptr<graphics::ShaderProgram> m_texrectColorAndDepthUpscaleCopyProgram;
Expand Down
1 change: 1 addition & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void Config::resetToDefaults()
generalEmulation.enableClipping = 1;
generalEmulation.enableCustomSettings = 1;
generalEmulation.enableShadersStorage = 1;
generalEmulation.enableAsyncShadersCompilation = 1;
generalEmulation.enableLegacyBlending = 0;
generalEmulation.enableHybridFilter = 1;
generalEmulation.enableInaccurateTextureCoordinates = 0;
Expand Down
1 change: 1 addition & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct Config
u32 enableClipping;
u32 enableCustomSettings;
u32 enableShadersStorage;
u32 enableAsyncShadersCompilation;
u32 enableLegacyBlending;
u32 enableHybridFilter;
u32 enableInaccurateTextureCoordinates;
Expand Down
1 change: 1 addition & 0 deletions src/gDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ void gDPFullSync()

CheckInterrupts();

CombinerInfo::get().compileUnknownShaders();
DebugMsg( DEBUG_NORMAL, "gDPFullSync();\n" );
}

Expand Down