diff --git a/src/Resource.aps b/src/Resource.aps index a526e13..b273667 100644 Binary files a/src/Resource.aps and b/src/Resource.aps differ diff --git a/src/Resource.rc b/src/Resource.rc index 230b8c0..cee37ab 100644 --- a/src/Resource.rc +++ b/src/Resource.rc @@ -51,8 +51,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,2,0,0 - PRODUCTVERSION 1,2,0,0 + FILEVERSION 1,3,0,0 + PRODUCTVERSION 1,3,0,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -69,12 +69,12 @@ BEGIN BEGIN VALUE "CompanyName", "advancedfx.org" VALUE "FileDescription", "ReShade advancedfx addon" - VALUE "FileVersion", "1.2.0.0" + VALUE "FileVersion", "1.3.0.0" VALUE "InternalName", "ReShade_advancedfx.addon" VALUE "LegalCopyright", "Copyright (C) 2022" VALUE "OriginalFilename", "ReShade_advancedfx.addon" VALUE "ProductName", "ReShade_advancedfx" - VALUE "ProductVersion", "1.2.0.0" + VALUE "ProductVersion", "1.3.0.0" END END BLOCK "VarFileInfo" diff --git a/src/main.cpp b/src/main.cpp index 5cb13e2..017477d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,10 +7,8 @@ using namespace reshade::api; -struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data +struct __declspec(uuid("9A609C4B-75C6-47C5-AFB5-4C65E0807F69")) runtime_data { - effect_runtime *main_runtime = nullptr; - bool block_effects = true; bool _hasBackBuffer = false; @@ -27,11 +25,8 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data resource depth_texture = { 0 }; resource_view depth_texture_view = { 0 }; - void free_depth_resources() { - if (main_runtime == 0) - return; - - const auto& device = main_runtime->get_device(); + void free_depth_resources(effect_runtime * runtime) { + const auto& device = runtime->get_device(); if (depth_texture_view != 0) { device->destroy_resource_view(depth_texture_view); depth_texture_view = { 0 }; @@ -41,24 +36,22 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data depth_texture = { 0 }; } - update_effect_runtime(); + update_effect_runtime(runtime); } - void update_effect_runtime() const { - if (main_runtime == 0) - return; + void update_effect_runtime(effect_runtime* runtime) const { - main_runtime->update_texture_bindings("DEPTH", depth_texture_view, depth_texture_view); + runtime->update_texture_bindings("DEPTH", depth_texture_view, depth_texture_view); - main_runtime->enumerate_uniform_variables(nullptr, [this](effect_runtime* runtime, auto variable) { + runtime->enumerate_uniform_variables(nullptr, [this](effect_runtime* runtime, auto variable) { char source[32] = ""; if (runtime->get_annotation_string_from_uniform_variable(variable, "source", source) && std::strcmp(source, "bufready_depth") == 0) runtime->set_uniform_value_bool(variable, depth_texture_view != 0); }); } - void disable_techniques() { - main_runtime->enumerate_techniques(nullptr, [this](effect_runtime* runtime, auto technique) { + void disable_techniques(effect_runtime* runtime) { + runtime->enumerate_techniques(nullptr, [this](effect_runtime* runtime, auto technique) { if (runtime->get_technique_state(technique)) { _disabled_techniques.push(technique); runtime->set_technique_state(technique, false); @@ -66,18 +59,15 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data }); } - void reenable_techniques() { + void reenable_techniques(effect_runtime* runtime) { while (!_disabled_techniques.empty()) { - main_runtime->set_technique_state(_disabled_techniques.front(), true); + runtime->set_technique_state(_disabled_techniques.front(), true); _disabled_techniques.pop(); } } - void free_buffer_resources() { - if (main_runtime == 0) - return; - - const auto& device = main_runtime->get_device(); + void free_buffer_resources(effect_runtime* runtime) { + const auto& device = runtime->get_device(); device->destroy_resource(_back_buffer_resolved); _back_buffer_resolved = {}; @@ -91,11 +81,9 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data _hasBackBuffer = false; } - bool ensure_buffers(resource back_buffer_resource) { - if (main_runtime == 0) - return false; + bool ensure_buffers(effect_runtime* runtime, resource back_buffer_resource) { - const auto& device = main_runtime->get_device(); + const auto& device = runtime->get_device(); const resource_desc back_buffer_desc = device->get_resource_desc(back_buffer_resource); if (!_hasBackBuffer @@ -111,7 +99,7 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data || back_buffer_desc.usage != _back_buffer_desc.usage ) { if (_hasBackBuffer) { - free_buffer_resources(); + free_buffer_resources(runtime); } _back_buffer_desc = back_buffer_desc; @@ -156,7 +144,7 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data resource_view_desc(format_to_default_typed(_back_buffer_format, 1)), &_back_buffer_targets.emplace_back())) { - free_buffer_resources(); + free_buffer_resources(runtime); return false; } } @@ -176,7 +164,7 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data format_to_default_typed(back_buffer_desc.texture.format, 1), 0, 1, 0, 1), &_back_buffer_targets.emplace_back())) { - free_buffer_resources(); + free_buffer_resources(runtime); return false; } @@ -187,68 +175,68 @@ struct __declspec(uuid("745350f4-bd58-479b-8ad0-81e872a9952b")) device_data } }; -command_list* g_MainCommandList = 0; +effect_runtime* g_MainRuntime = 0; static void update_effect_runtime(effect_runtime* runtime) { - const auto &dev_data = runtime->get_device()->get_private_data(); - dev_data.update_effect_runtime(); + auto &data = runtime->get_private_data(); + data.update_effect_runtime(runtime); } -bool supply_depth(void* pDepthTextureResource) { - if (g_MainCommandList == 0) - return false; - - device* const device = g_MainCommandList->get_device(); - auto& dev_data = device->get_private_data(); +bool supply_depth(effect_runtime* runtime, void* pDepthTextureResource) { + auto& data = runtime->get_private_data(); if (pDepthTextureResource == 0) { - dev_data.free_depth_resources(); + data.free_depth_resources(runtime); return false; } - if (dev_data.main_runtime == 0) - return false; + device* const device = runtime->get_device(); resource rs_depth{ (uint64_t)pDepthTextureResource }; const resource_desc rs_depth_desc(device->get_resource_desc(rs_depth)); - const resource_desc rs_depth_target_desc(dev_data.depth_texture != 0 ? device->get_resource_desc(dev_data.depth_texture) : resource_desc()); + const resource_desc rs_depth_target_desc(data.depth_texture != 0 ? device->get_resource_desc(data.depth_texture) : resource_desc()); - if (dev_data.depth_texture != 0 && (rs_depth_desc.texture.width != rs_depth_target_desc.texture.width || rs_depth_desc.texture.height != rs_depth_desc.texture.height)) { - dev_data.free_depth_resources(); + if (data.depth_texture != 0 && (rs_depth_desc.texture.width != rs_depth_target_desc.texture.width || rs_depth_desc.texture.height != rs_depth_desc.texture.height)) { + data.free_depth_resources(runtime); } - if (dev_data.depth_texture == 0) { + if (data.depth_texture == 0) { resource_desc desc(rs_depth_desc); desc.type = resource_type::texture_2d; desc.heap = memory_heap::gpu_only; desc.usage = resource_usage::shader_resource | resource_usage::copy_dest; //desc.texture.format = format::r32_float; - if (device->create_resource(desc, nullptr, resource_usage::copy_dest, &dev_data.depth_texture)) - device->set_resource_name(dev_data.depth_texture, "ReShade advancedfx depth texture"); + if (device->create_resource(desc, nullptr, resource_usage::copy_dest, &data.depth_texture)) + device->set_resource_name(data.depth_texture, "ReShade advancedfx depth texture"); else return false; resource_view_desc view_desc(format_to_default_typed(desc.texture.format)); - if (!device->create_resource_view(dev_data.depth_texture, resource_usage::shader_resource, view_desc, &dev_data.depth_texture_view)) + if (!device->create_resource_view(data.depth_texture, resource_usage::shader_resource, view_desc, &data.depth_texture_view)) return false; - update_effect_runtime(dev_data.main_runtime); + update_effect_runtime(runtime); } - if (dev_data.depth_texture == 0 - || dev_data.depth_texture_view == 0) + if (data.depth_texture == 0 + || data.depth_texture_view == 0) return false; - g_MainCommandList->barrier(dev_data.depth_texture, resource_usage::shader_resource, resource_usage::copy_dest); - g_MainCommandList->barrier(rs_depth, resource_usage::render_target, resource_usage::copy_source); + if (auto command_queue = runtime->get_command_queue()) { + if (auto command_list = command_queue->get_immediate_command_list()) { + command_list->barrier(data.depth_texture, resource_usage::shader_resource, resource_usage::copy_dest); + command_list->barrier(rs_depth, resource_usage::render_target, resource_usage::copy_source); - g_MainCommandList->copy_resource(rs_depth, dev_data.depth_texture); + command_list->copy_resource(rs_depth, data.depth_texture); - g_MainCommandList->barrier(rs_depth, resource_usage::copy_source, resource_usage::render_target); - g_MainCommandList->barrier(dev_data.depth_texture, resource_usage::copy_dest, resource_usage::shader_resource); + command_list->barrier(rs_depth, resource_usage::copy_source, resource_usage::render_target); + command_list->barrier(data.depth_texture, resource_usage::copy_dest, resource_usage::shader_resource); + return true; + } + } - return true; + return false; } extern "C" bool __declspec(dllexport) AdvancedfxRenderEffects(void* pRenderTargetView, void * pDepthTextureResource) { @@ -258,127 +246,104 @@ extern "C" bool __declspec(dllexport) AdvancedfxRenderEffects(void* pRenderTarge return true; } - if (g_MainCommandList == 0) + if (g_MainRuntime == 0) return false; - device* const device = g_MainCommandList->get_device(); - auto& dev_data = device->get_private_data(); - - if (dev_data.main_runtime == 0) - return false; + auto& data = g_MainRuntime->get_private_data(); resource back_buffer_resource{ (uint64_t)pRenderTargetView }; - if (!dev_data.ensure_buffers(back_buffer_resource)) + if (!data.ensure_buffers(g_MainRuntime, back_buffer_resource)) return false; - // Resolve MSAA back buffer if MSAA is active or copy when format conversion is required - if (dev_data._back_buffer_resolved != 0) - { - if (dev_data._back_buffer_samples == 1) - { - g_MainCommandList->barrier(back_buffer_resource, resource_usage::present, resource_usage::copy_source); - g_MainCommandList->copy_texture_region(back_buffer_resource, 0, nullptr, dev_data._back_buffer_resolved, 0, nullptr); - g_MainCommandList->barrier(dev_data._back_buffer_resolved, resource_usage::copy_dest, resource_usage::render_target); - } - else - { - g_MainCommandList->barrier(back_buffer_resource, resource_usage::present, resource_usage::resolve_source); - g_MainCommandList->resolve_texture_region(back_buffer_resource, 0, nullptr, dev_data._back_buffer_resolved, 0, 0, 0, 0, dev_data._back_buffer_format); - g_MainCommandList->barrier(dev_data._back_buffer_resolved, resource_usage::resolve_dest, resource_usage::render_target); - } - } + if (auto command_queue = g_MainRuntime->get_command_queue()) { + if (auto command_list = command_queue->get_immediate_command_list()) { - supply_depth(pDepthTextureResource); + // Resolve MSAA back buffer if MSAA is active or copy when format conversion is required + if (data._back_buffer_resolved != 0) + { + if (data._back_buffer_samples == 1) + { + command_list->barrier(back_buffer_resource, resource_usage::present, resource_usage::copy_source); + command_list->copy_texture_region(back_buffer_resource, 0, nullptr, data._back_buffer_resolved, 0, nullptr); + command_list->barrier(data._back_buffer_resolved, resource_usage::copy_dest, resource_usage::render_target); + } + else + { + command_list->barrier(back_buffer_resource, resource_usage::present, resource_usage::resolve_source); + command_list->resolve_texture_region(back_buffer_resource, 0, nullptr, data._back_buffer_resolved, 0, 0, 0, 0, data._back_buffer_format); + command_list->barrier(data._back_buffer_resolved, resource_usage::resolve_dest, resource_usage::render_target); + } + } - if (dev_data._back_buffer_resolved != 0) - { - dev_data.block_effects = false; - dev_data.main_runtime->render_effects(g_MainCommandList, dev_data._back_buffer_targets[0], dev_data._back_buffer_targets[1]); - dev_data.block_effects = true; - } - else - { - g_MainCommandList->barrier(back_buffer_resource, resource_usage::present, resource_usage::render_target); - dev_data.block_effects = false; - dev_data.main_runtime->render_effects(g_MainCommandList, dev_data._back_buffer_targets[0], dev_data._back_buffer_targets[1]); - dev_data.block_effects = true; - g_MainCommandList->barrier(back_buffer_resource, resource_usage::render_target, resource_usage::present); - } + supply_depth(g_MainRuntime, pDepthTextureResource); - // Stretch main render target back into MSAA back buffer if MSAA is active or copy when format conversion is required - if (dev_data._back_buffer_resolved != 0) - { - const resource resources[2] = { back_buffer_resource, dev_data._back_buffer_resolved }; - const resource_usage state_old[2] = { resource_usage::copy_source | resource_usage::resolve_source, resource_usage::render_target }; - const resource_usage state_final[2] = { resource_usage::present, resource_usage::resolve_dest }; + if (data._back_buffer_resolved != 0) + { + data.block_effects = false; + g_MainRuntime->render_effects(command_list, data._back_buffer_targets[0], data._back_buffer_targets[1]); + data.block_effects = true; + } + else + { + command_list->barrier(back_buffer_resource, resource_usage::present, resource_usage::render_target); + data.block_effects = false; + g_MainRuntime->render_effects(command_list, data._back_buffer_targets[0], data._back_buffer_targets[1]); + data.block_effects = true; + command_list->barrier(back_buffer_resource, resource_usage::render_target, resource_usage::present); + } - const resource_usage state_new[2] = { resource_usage::copy_dest, resource_usage::copy_source }; + // Stretch main render target back into MSAA back buffer if MSAA is active or copy when format conversion is required + if (data._back_buffer_resolved != 0) + { + const resource resources[2] = { back_buffer_resource, data._back_buffer_resolved }; + const resource_usage state_old[2] = { resource_usage::copy_source | resource_usage::resolve_source, resource_usage::render_target }; + const resource_usage state_final[2] = { resource_usage::present, resource_usage::resolve_dest }; - g_MainCommandList->barrier(2, resources, state_old, state_new); - g_MainCommandList->copy_texture_region(dev_data._back_buffer_resolved, 0, nullptr, back_buffer_resource, 0, nullptr); - g_MainCommandList->barrier(2, resources, state_new, state_final); - } + const resource_usage state_new[2] = { resource_usage::copy_dest, resource_usage::copy_source }; - return true; -} + command_list->barrier(2, resources, state_old, state_new); + command_list->copy_texture_region(data._back_buffer_resolved, 0, nullptr, back_buffer_resource, 0, nullptr); + command_list->barrier(2, resources, state_new, state_final); + } -static void on_init_device(device *device) -{ - device->create_private_data(); -} -static void on_destroy_device(device *device) -{ - device->destroy_private_data(); -} + return true; + } + } -static void on_init_command_list(command_list *cmd_list) -{ - if (g_MainCommandList == 0) g_MainCommandList = cmd_list; -} -static void on_destroy_command_list(command_list *cmd_list) -{ - if (g_MainCommandList == cmd_list) g_MainCommandList = 0; + return false; } static void on_init_effect_runtime(effect_runtime *runtime) { - auto &dev_data = runtime->get_device()->get_private_data(); - // Assume last created effect runtime is the main one - dev_data.main_runtime = runtime; + runtime->create_private_data(); } static void on_destroy_effect_runtime(effect_runtime *runtime) { - device* const device = runtime->get_device(); + auto & data = runtime->get_private_data(); + data.free_depth_resources(runtime); + data.free_buffer_resources(runtime); - auto &dev_data = device->get_private_data(); - if (runtime == dev_data.main_runtime) { - dev_data.free_depth_resources(); - dev_data.free_buffer_resources(); - dev_data.main_runtime = nullptr; + if (runtime == g_MainRuntime) { + g_MainRuntime = nullptr; } + + runtime->destroy_private_data(); } -static void on_begin_render_effects(effect_runtime* runtime, command_list* cmd_list, resource_view, resource_view) +static void on_begin_render_effects(effect_runtime* runtime, command_list* /*cmd_list*/, resource_view, resource_view) { - if (g_MainCommandList == 0) - return; - - device* const device = g_MainCommandList->get_device(); - auto& dev_data = device->get_private_data(); + if (nullptr == g_MainRuntime) g_MainRuntime = runtime; - if (dev_data.block_effects) dev_data.disable_techniques(); + auto& data = runtime->get_private_data(); + if (data.block_effects) data.disable_techniques(runtime); } -static void on_finish_render_effects(effect_runtime* runtime, command_list* cmd_list, resource_view, resource_view) +static void on_finish_render_effects(effect_runtime* runtime, command_list* /*cmd_list*/, resource_view, resource_view) { - if (g_MainCommandList == 0) - return; - - device* const device = g_MainCommandList->get_device(); - auto& dev_data = device->get_private_data(); + auto& data = runtime->get_private_data(); - if (dev_data.block_effects) dev_data.reenable_techniques(); + if (data.block_effects) data.reenable_techniques(runtime); } BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID) @@ -388,10 +353,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID) case DLL_PROCESS_ATTACH: if (!reshade::register_addon(hModule)) return FALSE; - reshade::register_event(on_init_device); - reshade::register_event(on_destroy_device); - reshade::register_event(on_init_command_list); - reshade::register_event(on_destroy_command_list); reshade::register_event(on_init_effect_runtime); reshade::register_event(on_destroy_effect_runtime); reshade::register_event(on_begin_render_effects);