Skip to content

Commit

Permalink
Merge branch 'clshortfuse:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
marat569 authored Jul 29, 2024
2 parents 989288a + f53840f commit 5b12741
Show file tree
Hide file tree
Showing 13 changed files with 3,394 additions and 2,945 deletions.
3,107 changes: 617 additions & 2,490 deletions src/devkit/addon.cpp

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/games/p5r/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,14 @@ bool OnDrawIndexed(

auto& shader_state = cmd_list->get_private_data<renodx::utils::shader::CommandListData>();

if (shader_state.pixel_shader_hash == 0xC6D14699) return false; // Video
if (shader_state.pixel_shader_hash == 0xB6E26AC7) {
auto pixel_shader_hash = shader_state.GetCurrentPixelShaderHash();
if (pixel_shader_hash == 0xC6D14699) return false; // Video
if (pixel_shader_hash == 0xB6E26AC7) {
g_completed_render = true;
return false;
}
if (!g_completed_render) return false;
if (!g_8bit_hashes.contains(shader_state.pixel_shader_hash)) return false;
if (!g_8bit_hashes.contains(pixel_shader_hash)) return false;

auto& swapchain_state = cmd_list->get_private_data<renodx::utils::swapchain::CommandListData>();

Expand Down Expand Up @@ -469,9 +470,9 @@ void OnInitPipeline(

void OnBindPipeline(
reshade::api::command_list* cmd_list,
reshade::api::pipeline_stage type,
reshade::api::pipeline_stage stages,
reshade::api::pipeline pipeline) {
if (type != reshade::api::pipeline_stage::output_merger) return;
if (stages != reshade::api::pipeline_stage::output_merger) return;
auto& data = cmd_list->get_private_data<CommandListData>();
data.last_output_merger = pipeline;
}
Expand Down
4 changes: 2 additions & 2 deletions src/games/p5r_archive/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ void OnBindRenderTargetsAndDepthStencil(reshade::api::command_list* cmd_list, ui

if (after_tonemapping) {
auto& shader_state = cmd_list->get_private_data<renodx::utils::shader::CommandListData>();
const uint32_t shader_hash = shader_state.pixel_shader_hash;
const uint32_t shader_hash = shader_state.GetCurrentPixelShaderHash();
if ((rtvs != nullptr) && rtvs->handle != 0) {
if (IsUiShader(shader_hash)) {
auto* device = cmd_list->get_device();
Expand Down Expand Up @@ -405,7 +405,7 @@ void OnBindPipeline(reshade::api::command_list* cmd_list, reshade::api::pipeline
state.pipelines[type] = pipeline;

auto& shader_state = cmd_list->get_private_data<renodx::utils::shader::CommandListData>();
const uint32_t shader_hash = shader_state.pixel_shader_hash;
const uint32_t shader_hash = shader_state.GetCurrentPixelShaderHash();

if (after_tonemapping && IsUiShader(shader_hash) && type == reshade::api::pipeline_stage::output_merger) {
ClampAlpha(cmd_list);
Expand Down
16 changes: 9 additions & 7 deletions src/games/starfield/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,17 @@ bool HandlePreDraw(reshade::api::command_list* cmd_list, bool is_dispatch = fals
// 0x0a152bb1 (tonemapper) (r11g11b10 => rgb8a_unorm tRender)
// 0x17FAB08F (sharpen?) (rgb8a_unorm tRender => rgb8a_unorm tComposite)
// 0xe9d9e225 (ui) (rgb8a_unorm tUI => rgb8a_unorm tComposite)

auto pixel_shader_hash = shader_state.GetCurrentPixelShaderHash();
if (
!is_dispatch
&& (shader_state.pixel_shader_hash == 0x0a152bb1 // tonemapper
|| shader_state.pixel_shader_hash == 0x054D0CB8 // tonemapper
|| shader_state.pixel_shader_hash == 0x3B344832 // tonemapper
|| shader_state.pixel_shader_hash == 0x17fab08f // sharpener
|| shader_state.pixel_shader_hash == 0x32580F53 // movie
|| shader_state.pixel_shader_hash == 0xe9d9e225 // ui
|| shader_state.pixel_shader_hash == 0x0d5add1f // copy
&& (pixel_shader_hash == 0x0a152bb1 // tonemapper
|| pixel_shader_hash == 0x054D0CB8 // tonemapper
|| pixel_shader_hash == 0x3B344832 // tonemapper
|| pixel_shader_hash == 0x17fab08f // sharpener
|| pixel_shader_hash == 0x32580F53 // movie
|| pixel_shader_hash == 0xe9d9e225 // ui
|| pixel_shader_hash == 0x0d5add1f // copy
)) {
auto& swapchain_state = cmd_list->get_private_data<renodx::utils::swapchain::CommandListData>();

Expand Down
49 changes: 15 additions & 34 deletions src/mods/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <d3d12.h>
#include <dxgi.h>
#include <dxgi1_6.h>
#include <cstdint>
#include <cstdio>

#include <sstream>
Expand All @@ -30,19 +31,18 @@
namespace renodx::mods::shader {
struct CustomShader {
uint32_t crc32;
const uint8_t* code;
uint32_t code_size;
std::vector<uint8_t> code;
bool swap_chain_only;
int32_t index = -1;
};

using CustomShaders = std::unordered_map<uint32_t, CustomShader>;

// clang-format off
#define CustomSwapchainShader(crc32) { crc32, { crc32, _##crc32, sizeof(_##crc32), true } }
#define CustomShaderEntry(crc32) { crc32, { crc32, _##crc32, sizeof(_##crc32) } }
#define BypassShaderEntry(crc32) { crc32, { crc32, nullptr, 0 } }
#define CustomCountedShader(crc32, index) { crc32, { crc32, _##crc32, sizeof(_##crc32), false, ##index} }
#define CustomSwapchainShader(crc32) { crc32, { crc32, std::vector<uint8_t>(_##crc32, _##crc32 + sizeof(_##crc32)), true } }
#define CustomShaderEntry(crc32) { crc32, { crc32, std::vector<uint8_t>(_##crc32, _##crc32 + sizeof(_##crc32)) } }
#define BypassShaderEntry(crc32) { crc32, { crc32, std::vector<uint8_t>(0) } }
#define CustomCountedShader(crc32, index) { crc32, { crc32, std::vector<uint8_t>(_##crc32, _##crc32 + sizeof(_##crc32)), false, ##index} }
// clang-format on

static thread_local std::vector<reshade::api::pipeline_layout_param*> created_params;
Expand Down Expand Up @@ -560,7 +560,7 @@ static bool HandlePreDraw(reshade::api::command_list* cmd_list, bool is_dispatch
auto& device_data = device->get_private_data<DeviceData>();
const std::unique_lock local_device_lock(device_data.mutex);

const auto& shader_state = cmd_list->get_private_data<renodx::utils::shader::CommandListData>();
auto& shader_state = renodx::utils::shader::GetCurrentState(cmd_list);

float resource_tag = -1;

Expand All @@ -572,7 +572,8 @@ static bool HandlePreDraw(reshade::api::command_list* cmd_list, bool is_dispatch
}
}

const uint32_t shader_hash = is_dispatch ? shader_state.compute_shader_hash : shader_state.pixel_shader_hash;
const uint32_t shader_hash = is_dispatch ? shader_state.GetCurrentComputeShaderHash()
: shader_state.GetCurrentPixelShaderHash();

auto custom_shader_info_pair = device_data.custom_shaders.find(shader_hash);
if (custom_shader_info_pair == device_data.custom_shaders.end()) {
Expand Down Expand Up @@ -687,26 +688,6 @@ static bool HandlePreDraw(reshade::api::command_list* cmd_list, bool is_dispatch
shader_injection);
}
}

// perform bind pipeline (replace shader)
if (
auto pair = shader_device_state.shader_to_pipeline_replacement.find(shader_hash);
pair != shader_device_state.shader_to_pipeline_replacement.end()) {
// has replacement that can be bound;
const auto& [pipeline_handle, pipeline_type] = pair->second;
#ifdef DEBUG_LEVEL_1
std::stringstream s;
s << "mods::shader::HandlePreDraw(binding pipeline: ";
s << PRINT_CRC32(shader_hash);
s << ", dispatch: " << (is_dispatch ? "true" : "false");
s << ", handle: " << reinterpret_cast<void*>(pipeline_handle);
s << ", stage: " << shader_state.pipeline_stage;
s << ")";
reshade::log_message(reshade::log_level::debug, s.str().c_str());
#endif

cmd_list->bind_pipeline(shader_state.pipeline_stage, {pipeline_handle});
}
return false;
}

Expand Down Expand Up @@ -779,7 +760,7 @@ static void Use(DWORD fdw_reason, CustomShaders new_custom_shaders, T* new_injec
if (!trace_unmodified_shaders) {
for (const auto& [hash, shader] : (new_custom_shaders)) {
if (shader.swap_chain_only) using_swap_chain_only = true;
if (shader.code_size == 0) using_bypass = true;
if (shader.code.empty()) using_bypass = true;
if (shader.index != -1) using_counted_shaders = true;
}
}
Expand All @@ -790,15 +771,15 @@ static void Use(DWORD fdw_reason, CustomShaders new_custom_shaders, T* new_injec

if (force_pipeline_cloning || use_pipeline_layout_cloning) {
for (const auto& [hash, shader] : (new_custom_shaders)) {
renodx::utils::shader::AddInitPipelineReplacement(hash, shader.code_size, shader.code);
renodx::utils::shader::AddRuntimeReplacement(hash, shader.code);
}
} else {
for (const auto& [hash, shader] : (new_custom_shaders)) {
if (!shader.swap_chain_only && shader.code_size && shader.index == -1) {
renodx::utils::shader::AddCreatePipelineReplacement(hash, shader.code_size, shader.code);
if (!shader.swap_chain_only && !shader.code.empty() && shader.index == -1) {
renodx::utils::shader::AddCompileTimeReplacement(hash, shader.code);
}
// Use Init as fallback
renodx::utils::shader::AddInitPipelineReplacement(hash, shader.code_size, shader.code);
// Use Runtime as fallback
renodx::utils::shader::AddRuntimeReplacement(hash, shader.code);
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/utils/path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2024 Carlos Lopez
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <Windows.h>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <span>

namespace renodx::utils::path {

static std::filesystem::path GetOutputPath() {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
wchar_t file_prefix[MAX_PATH] = L"";
GetModuleFileNameW(nullptr, file_prefix, ARRAYSIZE(file_prefix));

std::filesystem::path dump_path = file_prefix;
dump_path = dump_path.parent_path();
dump_path /= "renodx-dev";
return dump_path;
}

static std::vector<uint8_t> ReadBinaryFile(const std::filesystem::path& path) {
std::ifstream file(path, std::ios::binary);
file.seekg(0, std::ios::end);
const size_t file_size = file.tellg();
if (file_size == 0) return {};

auto result = std::vector<uint8_t>(file_size);
file.seekg(0, std::ios::beg).read(reinterpret_cast<char*>(result.data()), file_size);
return result;
}

static std::string ReadTextFile(const std::filesystem::path& path) {
auto data = ReadBinaryFile(path);
if (data.empty()) return "";
return {reinterpret_cast<const char*>(data.data()), data.size()};
}

static void WriteBinaryFile(const std::filesystem::path& path, std::span<uint8_t> data) {
std::ofstream file(path, std::ios::binary);
file.write(reinterpret_cast<const char*>(data.data()), data.size());
}

} // namespace renodx::utils::path
28 changes: 27 additions & 1 deletion src/utils/pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#pragma once

#include <include/reshade.hpp>
#include <span>
#include "./format.hpp"

namespace renodx::utils::pipeline {

static reshade::api::pipeline_subobject* ClonePipelineSubObjects(uint32_t subobject_count, const reshade::api::pipeline_subobject* subobjects) {
static reshade::api::pipeline_subobject* ClonePipelineSubObjects(const reshade::api::pipeline_subobject* subobjects, uint32_t subobject_count) {
auto* new_subobjects = new reshade::api::pipeline_subobject[subobject_count];
memcpy(new_subobjects, subobjects, sizeof(reshade::api::pipeline_subobject) * subobject_count);
for (uint32_t i = 0; i < subobject_count; ++i) {
Expand Down Expand Up @@ -126,4 +127,29 @@ static reshade::api::pipeline_subobject* ClonePipelineSubObjects(uint32_t subobj
return new_subobjects;
}

static void DestroyPipelineSubobjects(std::span<reshade::api::pipeline_subobject> subobjects) {
for (auto& subobject : subobjects) {
switch (subobject.type) {
case reshade::api::pipeline_subobject_type::vertex_shader:
case reshade::api::pipeline_subobject_type::compute_shader:
case reshade::api::pipeline_subobject_type::pixel_shader: {
auto* desc = static_cast<reshade::api::shader_desc*>(subobject.data);
free(const_cast<void*>(desc->code));
desc->code = nullptr;
break;
}
default:
break;
}

free(subobject.data);
subobject.data = nullptr;
}
}

static void DestroyPipelineSubobjects(reshade::api::pipeline_subobject* subobjects, uint32_t subobject_count) {
DestroyPipelineSubobjects({subobjects, subobjects + subobject_count});
delete[] subobjects;
}

} // namespace renodx::utils::pipeline
Loading

0 comments on commit 5b12741

Please sign in to comment.