|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | + |
| 5 | +#include "camera.h" |
| 6 | +#include "log.h" |
| 7 | + |
| 8 | +// Captures the main camera when the game binds it for a draw, straight from the CPU memory the game uploaded it to. Its constant buffer |
| 9 | +// is renamed (discarded) mid frame, so by the resolve the camera can be gone, and some views draw nothing that binds it after the scene. |
| 10 | +class CameraCapture |
| 11 | +{ |
| 12 | +public: |
| 13 | + // The large dynamic constant buffers the game uploads its per frame constants to |
| 14 | + static bool MayHoldCamera(const D3D11_BUFFER_DESC& desc); |
| 15 | + // The camera's constant buffer, if this binding update has it (null otherwise) |
| 16 | + static const reshade::api::buffer_range* FindBinding(reshade::api::shader_stage stages, const reshade::api::descriptor_table_update& update); |
| 17 | + |
| 18 | + // From any thread |
| 19 | + void OnBufferMapped(uint64_t buffer, const void* data, uint64_t size); |
| 20 | + void OnBufferDestroyed(uint64_t buffer); |
| 21 | + // From the immediate context, so the game's render thread, where the camera is also used (the resolve and present) |
| 22 | + void OnCameraBound(const reshade::api::buffer_range& binding, RenderSize render_size); |
| 23 | + |
| 24 | + // The main camera, as last bound this frame (null if none was) |
| 25 | + const Camera* GetCamera() const |
| 26 | + { |
| 27 | + if (!has_camera) |
| 28 | + return nullptr; |
| 29 | + return &camera; |
| 30 | + } |
| 31 | + void EndFrame(); |
| 32 | + |
| 33 | +private: |
| 34 | + struct MappedBuffer |
| 35 | + { |
| 36 | + uint64_t handle; |
| 37 | + const uint8_t* data; |
| 38 | + uint64_t size; |
| 39 | + }; |
| 40 | + static constexpr uint32_t kCameraSlot = 2; // In the scene's vertex shaders |
| 41 | + static constexpr size_t kMaxMappedBuffers = 64; |
| 42 | + |
| 43 | + // The caller holds the mutex |
| 44 | + MappedBuffer* FindMappedBuffer(uint64_t handle); |
| 45 | + |
| 46 | + // The mutex guards the mapped buffers. The camera is only used on the render thread. |
| 47 | + std::mutex mutex; |
| 48 | + MappedBuffer mapped_buffers[kMaxMappedBuffers] = {}; |
| 49 | + size_t mapped_count = 0; |
| 50 | + bool logged_overflow = false; |
| 51 | + Camera camera = {}; |
| 52 | + bool has_camera = false; |
| 53 | +}; |
| 54 | + |
| 55 | +// The game suballocates its per frame constants from a few big dynamic buffers (64 KB and up), so small ones are skipped before any lock |
| 56 | +inline bool CameraCapture::MayHoldCamera(const D3D11_BUFFER_DESC& desc) |
| 57 | +{ |
| 58 | + return (desc.BindFlags & D3D11_BIND_CONSTANT_BUFFER) && desc.Usage == D3D11_USAGE_DYNAMIC && desc.ByteWidth >= 65536; |
| 59 | +} |
| 60 | + |
| 61 | +// Runs for every binding the game makes, so integer checks only. An update can cover a range of slots. |
| 62 | +inline const reshade::api::buffer_range* CameraCapture::FindBinding(reshade::api::shader_stage stages, const reshade::api::descriptor_table_update& update) |
| 63 | +{ |
| 64 | + if (update.type != reshade::api::descriptor_type::constant_buffer || !(uint32_t(stages) & uint32_t(reshade::api::shader_stage::vertex))) |
| 65 | + return nullptr; |
| 66 | + if (kCameraSlot < update.binding || kCameraSlot >= update.binding + update.count) |
| 67 | + return nullptr; |
| 68 | + return &static_cast<const reshade::api::buffer_range*>(update.descriptors)[kCameraSlot - update.binding]; |
| 69 | +} |
| 70 | + |
| 71 | +// The latest map wins: a discard returns the buffer's new memory, which the later uploads go to |
| 72 | +inline void CameraCapture::OnBufferMapped(uint64_t buffer, const void* data, uint64_t size) |
| 73 | +{ |
| 74 | + const MappedBuffer map = {buffer, static_cast<const uint8_t*>(data), size}; |
| 75 | + const std::lock_guard lock(mutex); |
| 76 | + if (MappedBuffer* known = FindMappedBuffer(buffer)) |
| 77 | + { |
| 78 | + *known = map; |
| 79 | + } |
| 80 | + else if (mapped_count < kMaxMappedBuffers) |
| 81 | + { |
| 82 | + mapped_buffers[mapped_count++] = map; |
| 83 | + } |
| 84 | + else if (!logged_overflow) |
| 85 | + { |
| 86 | + logged_overflow = true; |
| 87 | + LogDiagnostic(reshade::log::level::warning, "over {} large constant buffers were mapped in a frame, so the camera can be missed", kMaxMappedBuffers); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// A destroyed buffer's handle can be reused |
| 92 | +inline void CameraCapture::OnBufferDestroyed(uint64_t buffer) |
| 93 | +{ |
| 94 | + const std::lock_guard lock(mutex); |
| 95 | + if (MappedBuffer* known = FindMappedBuffer(buffer)) |
| 96 | + *known = mapped_buffers[--mapped_count]; |
| 97 | +} |
| 98 | + |
| 99 | +// Keeps the bound block if it's the main camera, read from where its buffer was last mapped (the game wrote it there before binding it) |
| 100 | +inline void CameraCapture::OnCameraBound(const reshade::api::buffer_range& binding, RenderSize render_size) |
| 101 | +{ |
| 102 | + const std::lock_guard lock(mutex); |
| 103 | + const MappedBuffer* map = FindMappedBuffer(binding.buffer.handle); |
| 104 | + // The binding's offset is into the buffer, so the block has to be within what was mapped |
| 105 | + if (!map || binding.offset + sizeof(Camera) > map->size) |
| 106 | + return; |
| 107 | + const auto& bound = *reinterpret_cast<const Camera*>(map->data + binding.offset); |
| 108 | + if (!bound.IsMain(render_size)) |
| 109 | + return; |
| 110 | + camera = bound; |
| 111 | + has_camera = true; |
| 112 | +} |
| 113 | + |
| 114 | +// At present: the mapped memory is only valid until the buffers are mapped again, and each frame binds its own camera |
| 115 | +inline void CameraCapture::EndFrame() |
| 116 | +{ |
| 117 | + const std::lock_guard lock(mutex); |
| 118 | + mapped_count = 0; |
| 119 | + has_camera = false; |
| 120 | +} |
| 121 | + |
| 122 | +// A linear search: only a handful of large buffers are mapped in a frame |
| 123 | +inline CameraCapture::MappedBuffer* CameraCapture::FindMappedBuffer(uint64_t handle) |
| 124 | +{ |
| 125 | + for (size_t i = 0; i < mapped_count; i++) |
| 126 | + if (mapped_buffers[i].handle == handle) |
| 127 | + return &mapped_buffers[i]; |
| 128 | + return nullptr; |
| 129 | +} |
0 commit comments