Skip to content

Commit

Permalink
Restructure function hooking for contant buffer write intercepts, gen…
Browse files Browse the repository at this point in the history
…eral optimizations, and some refactoring
  • Loading branch information
4lex4nder committed Feb 12, 2023
1 parent c9609c3 commit 4bb9730
Show file tree
Hide file tree
Showing 41 changed files with 1,368 additions and 1,002 deletions.
26 changes: 13 additions & 13 deletions src/AddonUIConstants.h
Expand Up @@ -32,7 +32,7 @@ static const unordered_set<string> varExclusionSet({

static void DisplayConstantViewer(AddonImGui::AddonUIData& instance, ToggleGroup* group, device* dev, command_queue* queue)
{
if (group == nullptr)
if (group == nullptr || instance.GetConstantHandler() == nullptr)
{
return;
}
Expand Down Expand Up @@ -158,15 +158,15 @@ static void DisplayConstantViewer(AddonImGui::AddonUIData& instance, ToggleGroup
{
if (ImGui::Button("Add Variable Binding"))
{
ImGui::OpenPopup("Add");
ImGui::OpenPopup("Add###const_variables");
}

ImGui::Separator();

if (ImGui::BeginPopupModal("Add", nullptr, ImGuiWindowFlags_AlwaysAutoResize) && instance.GetRESTVariables()->size() > 0)
if (ImGui::BeginPopupModal("Add###const_variables", nullptr, ImGuiWindowFlags_AlwaysAutoResize) && instance.GetRESTVariables()->size() > 0)
{
ImGui::Text("Add constant buffer offset to variable binding:");

static int varSelectionIndex = 0;
vector<string> varNames;
std::transform(instance.GetRESTVariables()->begin(), instance.GetRESTVariables()->end(), std::back_inserter(varNames),
Expand All @@ -176,9 +176,9 @@ static void DisplayConstantViewer(AddonImGui::AddonUIData& instance, ToggleGroup
});
vector<string> filteredVars;
std::copy_if(varNames.begin(), varNames.end(), std::back_inserter(filteredVars), [](const string& s) { return !varExclusionSet.contains(s); });

static string varSelectedItem = filteredVars.size() > 0 ? filteredVars[0] : "";

if (ImGui::BeginCombo("Variable", varSelectedItem.c_str(), ImGuiComboFlags_None))
{
for (auto& v : filteredVars)
Expand All @@ -193,17 +193,17 @@ static void DisplayConstantViewer(AddonImGui::AddonUIData& instance, ToggleGroup
}
ImGui::EndCombo();
}

ImGui::Text("0x");
ImGui::SameLine();
ImGui::InputText("Offset", offsetInputBuf, offsetInputBufSize, ImGuiInputTextFlags_CharsHexadecimal);

static bool prevValue = false;

ImGui::Checkbox("Use previous value", &prevValue);

ImGui::Separator();

ImGui::SetCursorPosX(ImGui::GetWindowWidth() / 2 - 120 - ImGui::GetStyle().ItemSpacing.x / 2 - ImGui::GetStyle().FramePadding.x / 2);
if (ImGui::Button("OK", ImVec2(120, 0)))
{
Expand All @@ -219,7 +219,7 @@ static void DisplayConstantViewer(AddonImGui::AddonUIData& instance, ToggleGroup
{
ImGui::CloseCurrentPopup();
}

ImGui::EndPopup();
}

Expand Down
77 changes: 71 additions & 6 deletions src/AddonUIData.cpp
Expand Up @@ -35,9 +35,9 @@
using namespace AddonImGui;

AddonUIData::AddonUIData(ShaderManager* pixelShaderManager, ShaderManager* vertexShaderManager, ConstantHandlerBase* cHandler, atomic_uint32_t* activeCollectorFrameCounter,
vector<string>* techniques, unordered_map<string, tuple<constant_type, vector<effect_uniform_variable>>>* constants) :
vector<string>* techniques):
_pixelShaderManager(pixelShaderManager), _vertexShaderManager(vertexShaderManager), _activeCollectorFrameCounter(activeCollectorFrameCounter),
_allTechniques(techniques), _constantHandler(cHandler), _constants(constants)
_allTechniques(techniques), _constantHandler(cHandler)
{
_toggleGroupIdShaderEditing = -1;
_overlayOpacity = 0.2f;
Expand All @@ -62,6 +62,50 @@ std::unordered_map<int, ToggleGroup>& AddonUIData::GetToggleGroups()
return _toggleGroups;
}


const std::vector<ToggleGroup*>* AddonUIData::GetToggleGroupsForPixelShaderHash(uint32_t hash)
{
const auto& it = _pixelShaderHashToToggleGroups.find(hash);

if (it != _pixelShaderHashToToggleGroups.end())
{
return &it->second;
}

return nullptr;
}

const std::vector<ToggleGroup*>* AddonUIData::GetToggleGroupsForVertexShaderHash(uint32_t hash)
{
const auto& it = _vertexShaderHashToToggleGroups.find(hash);

if (it != _vertexShaderHashToToggleGroups.end())
{
return &it->second;
}

return nullptr;
}

void AddonUIData::UpdateToggleGroupsForShaderHashes()
{
_pixelShaderHashToToggleGroups.clear();
_vertexShaderHashToToggleGroups.clear();

for (auto& group : _toggleGroups)
{
for (const auto& h : group.second.getPixelShaderHashes())
{
_pixelShaderHashToToggleGroups[h].push_back(&group.second);
}

for (const auto& h : group.second.getVertexShaderHashes())
{
_vertexShaderHashToToggleGroups[h].push_back(&group.second);
}
}
}

const vector<string>* AddonUIData::GetAllTechniques() const
{
return _allTechniques;
Expand Down Expand Up @@ -105,8 +149,17 @@ void AddonUIData::LoadShaderTogglerIniFile()
return;
}

_memcpyHookAttempt = iniFile.GetBool("AttemptMemcpyHook", "General");
_memcpyAssumeUnnested = iniFile.GetBool("MemcpyAssumeUnnestedMap", "General");
_constHookType = iniFile.GetValue("ConstantBufferHookType", "General");
if (_constHookType.size() <= 0)
{
_constHookType = "none";
}

_constHookCopyType = iniFile.GetValue("ConstantBufferHookCopyType", "General");
if (_constHookCopyType.size() <= 0)
{
_constHookCopyType = "singular";
}

for (uint32_t i = 0; i < ARRAYSIZE(KeybindNames); i++)
{
Expand Down Expand Up @@ -137,6 +190,16 @@ void AddonUIData::LoadShaderTogglerIniFile()
{
group.second.loadState(iniFile, groupCounter); // groupCounter is normally 0 or greater. For when the old format is detected, it's -1 (and there's 1 group).
groupCounter++;

for (const auto& h : group.second.getPixelShaderHashes())
{
_pixelShaderHashToToggleGroups[h].push_back(&group.second);
}

for (const auto& h : group.second.getVertexShaderHashes())
{
_vertexShaderHashToToggleGroups[h].push_back(&group.second);
}
}
}

Expand All @@ -150,8 +213,8 @@ void AddonUIData::SaveShaderTogglerIniFile()
// groups are stored with "Group" + group counter, starting with 0.
CDataFile iniFile;

iniFile.SetBool("AttemptMemcpyHook", _memcpyHookAttempt, "", "General");
iniFile.SetBool("MemcpyAssumeUnnestedMap", _memcpyAssumeUnnested, "", "General");
iniFile.SetValue("ConstantBufferHookType", _constHookType, "", "General");
iniFile.SetValue("ConstantBufferHookCopyType", _constHookCopyType, "", "General");

for (uint32_t i = 0; i < ARRAYSIZE(KeybindNames); i++)
{
Expand Down Expand Up @@ -185,6 +248,8 @@ void AddonUIData::EndShaderEditing(bool acceptCollectedShaderHashes, ToggleGroup
_vertexShaderManager->stopHuntingMode();
}
_toggleGroupIdShaderEditing = -1;

UpdateToggleGroupsForShaderHashes();
}


Expand Down
20 changes: 12 additions & 8 deletions src/AddonUIData.h
Expand Up @@ -36,7 +36,7 @@
#include "ShaderManager.h"
#include "CDataFile.h"
#include "ToggleGroup.h"
#include "ConstantHandler.h"
#include "ConstantHandlerBase.h"

#define FRAMECOUNT_COLLECTION_PHASE_DEFAULT 10;
#define HASH_FILE_NAME "ReshadeEffectShaderToggler.ini"
Expand Down Expand Up @@ -87,21 +87,25 @@ namespace AddonImGui
ConstantHandlerBase* _constantHandler;
atomic_uint32_t* _activeCollectorFrameCounter;
vector<string>* _allTechniques;
unordered_map<string, tuple<constant_type, vector<effect_uniform_variable>>>* _constants;
atomic_int _historyIndexSelection = 0;
atomic_int _toggleGroupIdShaderEditing = -1;
atomic_int _toggleGroupIdEffectEditing = -1;
atomic_int _toggleGroupIdConstantEditing = -1;
std::unordered_map<int, ToggleGroup> _toggleGroups;
std::unordered_map<uint32_t, vector<ToggleGroup*>> _pixelShaderHashToToggleGroups;
std::unordered_map<uint32_t, vector<ToggleGroup*>> _vertexShaderHashToToggleGroups;
int _startValueFramecountCollectionPhase = FRAMECOUNT_COLLECTION_PHASE_DEFAULT;
float _overlayOpacity = 0.2f;
uint32_t _keyBindings[ARRAYSIZE(KeybindNames)];
bool _memcpyHookAttempt = false;
bool _memcpyAssumeUnnested = false;
string _constHookType = "none";
string _constHookCopyType = "singular";
public:
AddonUIData(ShaderManager* pixelShaderManager, ShaderManager* vertexShaderManager, ConstantHandlerBase* constants, atomic_uint32_t* activeCollectorFrameCounter,
vector<string>* techniques, unordered_map<string, tuple<constant_type, vector<effect_uniform_variable>>>*);
vector<string>* techniques);
std::unordered_map<int, ToggleGroup>& GetToggleGroups();
const std::vector<ToggleGroup*>* GetToggleGroupsForPixelShaderHash(uint32_t hash);
const std::vector<ToggleGroup*>* GetToggleGroupsForVertexShaderHash(uint32_t hash);
void UpdateToggleGroupsForShaderHashes();
void AddDefaultGroup();
const atomic_int& GetToggleGroupIdShaderEditing() const;
void EndShaderEditing(bool acceptCollectedShaderHashes, ToggleGroup& groupEditing);
Expand All @@ -127,10 +131,10 @@ namespace AddonImGui
void SetConstantHandler(ConstantHandlerBase* handler) { _constantHandler = handler; }
ConstantHandlerBase* GetConstantHandler() { return _constantHandler; }
uint32_t GetKeybinding(Keybind keybind);
bool GetAttemptMemcpyHook() { return _memcpyHookAttempt; }
bool GetMemcpyAssumeUnnested() { return _memcpyAssumeUnnested; }
const string& GetConstHookType() { return _constHookType; }
const string& GetConstHookCopyType() { return _constHookCopyType; }
void SetKeybinding(Keybind keybind, uint32_t keys);
const unordered_map<string, tuple<constant_type, vector<effect_uniform_variable>>>* GetRESTVariables() { return _constants; };
const unordered_map<string, tuple<constant_type, vector<effect_uniform_variable>>>* GetRESTVariables() { return _constantHandler->GetRESTVariables(); };
reshade::api::format cFormat;
};
}
70 changes: 5 additions & 65 deletions src/AddonUIDisplay.h
Expand Up @@ -172,71 +172,6 @@ static void DisplayTechniqueSelection(AddonImGui::AddonUIData& instance, ToggleG
}
}


//static void DisplayShaderSelection(AddonImGui::AddonUIData& instance, effect_runtime* runtime, ToggleGroup* group)
//{
// if (*instance.ActiveCollectorFrameCounter() > 0)
// {
// const uint32_t counterValue = *instance.ActiveCollectorFrameCounter();
// ImGui::Text("Collecting active shaders... frames to go: %d", counterValue);
// }
// else
// {
// float height = ImGui::GetWindowHeight();
//
// ShaderManager* shaderManagers[] = { instance.GetPixelShaderManager(), instance.GetVertexShaderManager() };
// const char* shaderManagerIds[] = { "Pixel Shader", "Vertex Shader" };
//
// for (uint32_t i = 0; i < IM_ARRAYSIZE(shaderManagers); i++)
// {
// if(!shaderManagers[i]->isInHuntingMode())
// {
// continue;
// }
//
// ImGui::SameLine();
//
// if (!ImGui::BeginTable(std::format("{}##table", shaderManagerIds[i]).c_str(), 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
// {
// ImGui::EndTable();
// continue;
// }
//
// vector<uint32_t> pShader = shaderManagers[i]->getHuntedShaders();
// bool noneSelected = true;
// for (int i = 0; i < pShader.size(); i++)
// {
// ImGui::TableNextColumn();
// ImGui::TableHeader(std::format("{:#02d}", i).c_str());
//
// bool enabled = shaderManagers[i]->isMarkedShader(pShader[i]);
// ImGui::TableNextColumn();
// ImGui::Checkbox(std::format("{}", pShader[i]).c_str(), &enabled);
// if (ImGui::IsItemHovered())
// {
// noneSelected = false;
// shaderManagers[i]->setActiveHuntedShader(pShader[i]);
// }
//
// if (enabled)
// shaderManagers[i]->markHuntedShader(pShader[i]);
// else
// shaderManagers[i]->unmarkHuntedShader(pShader[i]);
// }
//
// if (noneSelected)
// {
// shaderManagers[i]->setActiveHuntedShader(0);
// }
//
// ImGui::EndTable();
// }
//
// ImGui::EndChild();
// }
//}


static void DisplayOverlay(AddonImGui::AddonUIData& instance, effect_runtime* runtime)
{
if (instance.GetToggleGroupIdConstantEditing() >= 0)
Expand Down Expand Up @@ -332,6 +267,11 @@ static void CheckHotkeys(AddonImGui::AddonUIData& instance, effect_runtime* runt
--(*instance.ActiveCollectorFrameCounter());
}

if (instance.GetToggleGroupIdShaderEditing() == -1)
{
return;
}

ToggleGroup* editGroup = nullptr;

for (auto& group : instance.GetToggleGroups())
Expand Down
63 changes: 63 additions & 0 deletions src/ConstantCopyBase.cpp
@@ -0,0 +1,63 @@
#include "ConstantCopyBase.h"

using namespace ConstantFeedback;

ConstantHandlerBase* ConstantCopyBase::_constHandler = nullptr;

ConstantCopyBase::ConstantCopyBase()
{

}

ConstantCopyBase::~ConstantCopyBase()
{

}

void ConstantCopyBase::SetConstantHandler(ConstantHandlerBase* constantHandler)
{
_constHandler = constantHandler;
}

std::string ConstantCopyBase::GetExecutableName()
{
char fileName[MAX_PATH + 1];
DWORD charsWritten = GetModuleFileNameA(NULL, fileName, MAX_PATH + 1);
if (charsWritten != 0)
{
std::string ret(fileName);
std::size_t found = ret.find_last_of("/\\");
return ret.substr(found + 1);
}

return std::string();
}

template<typename T>
T* ConstantCopyBase::InstallHook(void* target, T* callback)
{
void* original_function = nullptr;

if (MH_CreateHook(target, reinterpret_cast<void*>(callback), &original_function) != MH_OK)
return nullptr;

return reinterpret_cast<T*>(original_function);
}

template<typename T>
T* ConstantCopyBase::InstallApiHook(LPCWSTR pszModule, LPCSTR pszProcName, T* callback)
{
void* original_function = nullptr;

if (MH_CreateHookApi(pszModule, pszProcName, reinterpret_cast<void*>(callback), &original_function) != MH_OK)
return nullptr;

return reinterpret_cast<T*>(original_function);
}

// Template instances
template sig_memcpy* ConstantCopyBase::InstallHook(void* target, sig_memcpy* callback);
template sig_memcpy* ConstantCopyBase::InstallApiHook(LPCWSTR pszModule, LPCSTR pszProcName, sig_memcpy* callback);

template sig_ffxiv_cbload* ConstantCopyBase::InstallHook(void* target, sig_ffxiv_cbload* callback);
template sig_ffxiv_cbload* ConstantCopyBase::InstallApiHook(LPCWSTR pszModule, LPCSTR pszProcName, sig_ffxiv_cbload* callback);

0 comments on commit 4bb9730

Please sign in to comment.