Skip to content

Commit

Permalink
Attempt to compile shaders asynchronously, #2701
Browse files Browse the repository at this point in the history
  • Loading branch information
gonetz committed Apr 22, 2023
1 parent 0fee30d commit e24c431
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
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

0 comments on commit e24c431

Please sign in to comment.