Skip to content

Commit

Permalink
Merge pull request #55 from alandtse/new_llf
Browse files Browse the repository at this point in the history
feat: add Shader Defines advanced option
  • Loading branch information
doodlum authored Aug 27, 2023
2 parents 6375bcb + be4b478 commit 31ee90e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
22 changes: 12 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ find_package(imgui CONFIG REQUIRED)
find_package(EASTL CONFIG REQUIRED)
find_package(directxtk CONFIG REQUIRED)
find_path(CLIB_UTIL_INCLUDE_DIRS "ClibUtil/utils.hpp")
find_package(pystring CONFIG REQUIRED)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CLIB_UTIL_INCLUDE_DIRS}
${CLIB_UTIL_INCLUDE_DIRS}
)

target_link_libraries(
${PROJECT_NAME}
${PROJECT_NAME}
PRIVATE
debug ${CMAKE_CURRENT_SOURCE_DIR}/include/detours/Debug/detours.lib
optimized ${CMAKE_CURRENT_SOURCE_DIR}/include/detours/Release/detours.lib
magic_enum::magic_enum
xbyak::xbyak
nlohmann_json::nlohmann_json
imgui::imgui
EASTL
Microsoft::DirectXTK
debug ${CMAKE_CURRENT_SOURCE_DIR}/include/detours/Debug/detours.lib
optimized ${CMAKE_CURRENT_SOURCE_DIR}/include/detours/Release/detours.lib
magic_enum::magic_enum
xbyak::xbyak
nlohmann_json::nlohmann_json
imgui::imgui
EASTL
Microsoft::DirectXTK
pystring::pystring
)

# https://gitlab.kitware.com/cmake/cmake/-/issues/24922#note_1371990
Expand Down
13 changes: 13 additions & 0 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Menu.h"

#include <dinput.h>
#include <imgui_stdlib.h>
#include <magic_enum.hpp>

#include "ShaderCache.h"
Expand Down Expand Up @@ -395,6 +396,18 @@ void Menu::DrawSettings()
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}

auto& shaderDefines = State::GetSingleton()->shaderDefinesString;
if (ImGui::InputText("Shader Defines", &shaderDefines)) {
State::GetSingleton()->SetDefines(shaderDefines);
}
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::Text("Defines for Shader Compiler. Semicolon \";\" separated. Clear with space. Rebuild shaders after making change. Compute Shaders require a restart to recompile.");
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}

if (ImGui::CollapsingHeader("General", ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) {
Expand Down
11 changes: 10 additions & 1 deletion src/ShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,10 @@ namespace SIE
for (const auto& def : defines) {
if (def.Name != nullptr) {
result += def.Name;
if (def.Definition != nullptr && !std::string(def.Definition).empty()) {
result += "=";
result += def.Definition;
}
result += ' ';
} else {
break;
Expand Down Expand Up @@ -1054,7 +1058,7 @@ namespace SIE
const auto type = shader.shaderType.get();
const std::wstring path = GetShaderPath(shader.fxpFilename);

std::array<D3D_SHADER_MACRO, 64> defines;
std::array<D3D_SHADER_MACRO, 64> defines{};
auto lastIndex = 0;
if (shaderClass == ShaderClass::Vertex) {
defines[lastIndex++] = { "VSHADER", nullptr };
Expand All @@ -1067,6 +1071,11 @@ namespace SIE
}
if (REL::Module::IsVR())
defines[lastIndex++] = { "VR", nullptr };
auto shaderDefines = State::GetSingleton()->GetDefines();
if (!shaderDefines->empty()) {
for (unsigned int i = 0; i < shaderDefines->size(); i++)
defines[lastIndex++] = { shaderDefines->at(i).first.c_str(), shaderDefines->at(i).second.c_str() };
}
defines[lastIndex] = { nullptr, nullptr }; // do final entry
GetShaderDefines(type, descriptor, &defines[lastIndex]);

Expand Down
36 changes: 36 additions & 0 deletions src/State.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "State.h"

#include <magic_enum.hpp>
#include <pystring/pystring.h>

#include "Menu.h"
#include "ShaderCache.h"
Expand Down Expand Up @@ -79,6 +80,8 @@ void State::Load()
if (advanced["Log Level"].is_number_integer()) {
logLevel = static_cast<spdlog::level::level_enum>((int)advanced["Log Level"]);
//logLevel = static_cast<spdlog::level::level_enum>(max(spdlog::level::trace, min(spdlog::level::off, (int)advanced["Log Level"])));
if (advanced["Shader Defines"].is_string())
SetDefines(advanced["Shader Defines"]);
}
}

Expand Down Expand Up @@ -120,6 +123,7 @@ void State::Save()
json advanced;
advanced["Dump Shaders"] = shaderCache.IsDump();
advanced["Log Level"] = logLevel;
advanced["Shader Defines"] = shaderDefinesString;
settings["Advanced"] = advanced;

json general;
Expand Down Expand Up @@ -170,6 +174,38 @@ spdlog::level::level_enum State::GetLogLevel()
return logLevel;
}

void State::SetDefines(std::string a_defines)
{
shaderDefines.clear();
shaderDefinesString = "";
std::string name = "";
std::string definition = "";
auto defines = pystring::split(a_defines, ";");
for (const auto& define : defines) {
auto cleanedDefine = pystring::strip(define);
auto token = pystring::split(cleanedDefine, "=");
if (token.empty() || token[0].empty())
continue;
if (token.size() > 2) {
logger::warn("Define string has too many '='; ignoring {}", define);
continue;
}
name = pystring::strip(token[0]);
if (token.size() == 2) {
definition = pystring::strip(token[1]);
}
shaderDefinesString += pystring::strip(define) + ";";
shaderDefines.push_back(std::pair(name, definition));
}
shaderDefinesString = shaderDefinesString.substr(0, shaderDefinesString.size() - 1);
logger::debug("Shader Defines set to {}", shaderDefinesString);
}

std::vector<std::pair<std::string, std::string>>* State::GetDefines()
{
return &shaderDefines;
}

bool State::ShaderEnabled(const RE::BSShader::Type a_type)
{
auto index = static_cast<uint32_t>(a_type) + 1;
Expand Down
5 changes: 5 additions & 0 deletions src/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class State
uint32_t currentVertexDescriptor = 0;
uint32_t currentPixelDescriptor = 0;
spdlog::level::level_enum logLevel = spdlog::level::info;
std::string shaderDefinesString = "";
std::vector<std::pair<std::string, std::string>> shaderDefines{}; // data structure to parse string into; needed to avoid dangling pointers

void Draw();
void Reset();
Expand All @@ -32,6 +34,9 @@ class State
void SetLogLevel(spdlog::level::level_enum a_level = spdlog::level::info);
spdlog::level::level_enum GetLogLevel();

void SetDefines(std::string defines);
std::vector<std::pair<std::string, std::string>>* GetDefines();

/*
* Whether a_type is currently enabled in Community Shaders
*
Expand Down
13 changes: 13 additions & 0 deletions src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ namespace Util
macros.push_back({ "D3DCOMPILE_SKIP_OPTIMIZATION", "" });
macros.push_back({ "D3DCOMPILE_DEBUG", "" });
}
auto shaderDefines = State::GetSingleton()->GetDefines();
if (!shaderDefines->empty()) {
for (unsigned int i = 0; i < shaderDefines->size(); i++)
macros.push_back({ shaderDefines->at(i).first.c_str(), shaderDefines->at(i).second.c_str() });
}
if (!_stricmp(ProgramType, "ps_5_0"))
macros.push_back({ "PIXELSHADER", "" });
else if (!_stricmp(ProgramType, "vs_5_0"))
Expand Down Expand Up @@ -197,6 +202,10 @@ namespace Util
for (const auto& def : defines) {
if (def.first != nullptr) {
result += def.first;
if (def.second != nullptr && !std::string(def.second).empty()) {
result += "=";
result += def.second;
}
result += ' ';
} else {
break;
Expand All @@ -210,6 +219,10 @@ namespace Util
for (const auto& def : defines) {
if (def.Name != nullptr) {
result += def.Name;
if (def.Definition != nullptr && !std::string(def.Definition).empty()) {
result += "=";
result += def.Definition;
}
result += ' ';
} else {
break;
Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"catch2",
"magic-enum",
"nlohmann-json",
"pystring",
{
"name": "imgui",
"features": [
Expand Down

0 comments on commit 31ee90e

Please sign in to comment.