Compare commits

...
5 changed files with 438 additions and 0 deletions

No files matched your search

+1
View File
@@ -28,6 +28,7 @@ done
python3 "$root/tools/dxvk/check_present_sync.py" "$work/source/src/dxvk/dxvk_presenter.cpp"
python3 "$root/tools/dxvk/check_hud_storage_format.py" "$work/source/src/dxvk/hud/shaders/hud_frame_time_eval.comp"
python3 "$root/tools/dxvk/check_d3d9_single_sample.py" "$work/source"
python3 "$root/tools/dxvk/check_keyed_mutex_fence.py" "$work/source/src/dxvk/dxvk_image.cpp"
for arch in 32 64; do
meson setup "$work/build$arch" "$work/source" --cross-file "$work/source/build-win$arch.txt" \
--buildtype release --prefix "$out" --bindir "x$arch" --libdir "x$arch" -Db_ndebug=if-release --strip
+159
View File
@@ -0,0 +1,159 @@
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import re
import subprocess
import tempfile
def function(text, signature):
start = text.index(signature)
opening = text.index('{', start)
stripped = re.sub(r'//[^\n]*|/\*.*?\*/|"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'',
lambda m: ' ' * len(m.group()), text, flags=re.S)
depth = 0
for index in range(opening, len(stripped)):
depth += (stripped[index] == '{') - (stripped[index] == '}')
if depth == 0:
return text[start:index + 1]
raise ValueError('Unterminated source function: ' + signature)
HARNESS = r'''
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <vector>
using UINT64 = uint64_t;
using DWORD = uint32_t;
using HRESULT = int32_t;
using NTSTATUS = int32_t;
using D3DKMT_HANDLE = uint32_t;
using VkSemaphore = uint64_t;
using VkResult = int;
constexpr HRESULT S_OK = 0;
constexpr HRESULT DXGI_ERROR_INVALID_CALL = 0x887A0001;
constexpr HRESULT WAIT_TIMEOUT = 258;
constexpr NTSTATUS STATUS_TIMEOUT = 0x102;
constexpr DWORD INFINITE = 0xFFFFFFFF;
constexpr int VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO = 1;
constexpr int VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO = 2;
struct LARGE_INTEGER { int64_t QuadPart; };
struct D3DKMT_ACQUIREKEYEDMUTEX { D3DKMT_HANDLE hKeyedMutex; UINT64 Key; LARGE_INTEGER* pTimeout; UINT64 FenceValue; };
struct D3DKMT_RELEASEKEYEDMUTEX { D3DKMT_HANDLE hKeyedMutex; UINT64 Key; UINT64 FenceValue; };
struct VkSemaphoreWaitInfo { int sType; uint32_t semaphoreCount; const VkSemaphore* pSemaphores; const uint64_t* pValues; };
struct VkSemaphoreSignalInfo { int sType; VkSemaphore semaphore; uint64_t value; };
struct Logger { template<typename... A> static void warn(const char*, A...) { std::puts("warn"); } };
struct Recorder {
NTSTATUS acquireStatus = 0;
uint64_t acquireFenceValue = 0;
bool timeoutPassed = false;
int64_t timeout = 0;
uint64_t releasedFenceValue = ~0ull;
std::vector<uint64_t> waited, signalled;
} rec;
NTSTATUS D3DKMTAcquireKeyedMutex(D3DKMT_ACQUIREKEYEDMUTEX* a) {
rec.timeoutPassed = a->pTimeout != nullptr;
rec.timeout = a->pTimeout ? a->pTimeout->QuadPart : 0;
a->FenceValue = rec.acquireFenceValue;
return rec.acquireStatus;
}
NTSTATUS D3DKMTReleaseKeyedMutex(D3DKMT_RELEASEKEYEDMUTEX* r) {
rec.releasedFenceValue = r->FenceValue;
return 0;
}
struct Fence { VkSemaphore handle() const { return 7; } };
struct FenceRef {
Fence* ptr = nullptr;
Fence* operator->() const { return ptr; }
explicit operator bool() const { return ptr != nullptr; }
};
struct Vkd {
int device() const { return 0; }
VkResult vkWaitSemaphores(int, const VkSemaphoreWaitInfo* i, int) { rec.waited.push_back(*i->pValues); return 0; }
VkResult vkSignalSemaphore(int, const VkSemaphoreSignalInfo* i) { rec.signalled.push_back(i->value); return 0; }
};
struct VkdRef { Vkd* operator->() const { static Vkd v; return &v; } };
struct DxvkKeyedMutex {
HRESULT AcquireSync(UINT64 key, DWORD milliseconds);
HRESULT ReleaseSync(UINT64 key);
VkdRef m_vkd;
FenceRef m_fence;
D3DKMT_HANDLE m_kmtLocal = 1;
uint64_t m_fenceValue = 0;
std::atomic<bool> m_owned = false;
};
@ACQUIRE_SYNC@
@RELEASE_SYNC@
'''
MAIN = r'''
#define CHECK(cond) do { if (!(cond)) { std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #cond); return 2; } } while (0)
int main() {
Fence fence;
{
rec = Recorder{}; rec.acquireFenceValue = 41;
DxvkKeyedMutex m;
CHECK(m.AcquireSync(0, 5) == S_OK);
CHECK(rec.timeoutPassed && rec.timeout == -50000);
CHECK(rec.waited.empty());
CHECK(m.ReleaseSync(0) == S_OK);
CHECK(rec.signalled.empty());
CHECK(rec.releasedFenceValue == 41);
}
{
rec = Recorder{}; rec.acquireFenceValue = 41;
DxvkKeyedMutex m; m.m_fence.ptr = &fence;
CHECK(m.AcquireSync(3, 5) == S_OK);
CHECK(rec.waited.size() == 1 && rec.waited[0] == 41);
CHECK(m.ReleaseSync(3) == S_OK);
CHECK(rec.signalled.size() == 1 && rec.signalled[0] == 42);
CHECK(rec.releasedFenceValue == 42);
}
{
rec = Recorder{};
DxvkKeyedMutex m;
CHECK(m.AcquireSync(0, INFINITE) == S_OK);
CHECK(!rec.timeoutPassed);
}
{
rec = Recorder{}; rec.acquireStatus = STATUS_TIMEOUT;
DxvkKeyedMutex m;
CHECK(m.AcquireSync(0, 5) == WAIT_TIMEOUT);
CHECK(m.ReleaseSync(0) == DXGI_ERROR_INVALID_CALL);
}
std::puts("PASS: keyed mutex acquire and release with and without a fence");
return 0;
}
'''
def main():
parser = argparse.ArgumentParser()
parser.add_argument('source', type=Path)
args = parser.parse_args()
text = args.source.read_text()
acquire = function(text, 'HRESULT DxvkKeyedMutex::AcquireSync(')
release = function(text, 'HRESULT DxvkKeyedMutex::ReleaseSync(')
code = HARNESS.replace('@ACQUIRE_SYNC@', acquire).replace('@RELEASE_SYNC@', release) + MAIN
with tempfile.TemporaryDirectory(prefix='dxvk-keyed-mutex-') as directory:
cpp = Path(directory) / 'keyed_mutex.cpp'
exe = Path(directory) / 'keyed_mutex'
cpp.write_text(code)
subprocess.run([os.environ.get('HOST_CXX', 'c++'), '-std=c++17', '-Wall', '-Wextra', '-Wno-missing-field-initializers', str(cpp), '-o', str(exe)], check=True)
return subprocess.run([str(exe)]).returncode
if __name__ == '__main__':
raise SystemExit(main())
@@ -0,0 +1,111 @@
diff --git a/src/dxvk/dxvk_image.cpp b/src/dxvk/dxvk_image.cpp
index 3b5b739..0b0421e 100644
--- a/src/dxvk/dxvk_image.cpp
+++ b/src/dxvk/dxvk_image.cpp
@@ -43,83 +43,89 @@ namespace dxvk {
}
DxvkKeyedMutex::~DxvkKeyedMutex() {
if (m_kmtLocal) {
D3DKMT_DESTROYKEYEDMUTEX destroy = { };
destroy.hKeyedMutex = m_kmtLocal;
D3DKMTDestroyKeyedMutex(&destroy);
}
}
HRESULT DxvkKeyedMutex::AcquireSync(UINT64 key, DWORD milliseconds) {
if (m_owned.load(std::memory_order_acquire))
return DXGI_ERROR_INVALID_CALL;
LARGE_INTEGER timeout = { };
D3DKMT_ACQUIREKEYEDMUTEX acquire = { };
acquire.hKeyedMutex = m_kmtLocal;
acquire.Key = key;
- acquire.pTimeout = &timeout;
- timeout.QuadPart = milliseconds * -10000;
+ acquire.pTimeout = milliseconds == INFINITE ? nullptr : &timeout;
+ timeout.QuadPart = int64_t(milliseconds) * -10000;
NTSTATUS status = D3DKMTAcquireKeyedMutex(&acquire);
if (status == STATUS_TIMEOUT)
return WAIT_TIMEOUT;
if (status)
return DXGI_ERROR_INVALID_CALL;
- VkSemaphore semaphore = m_fence->handle();
- VkSemaphoreWaitInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO };
- info.semaphoreCount = 1;
- info.pSemaphores = &semaphore;
- info.pValues = &acquire.FenceValue;
+ if (m_fence) {
+ VkSemaphore semaphore = m_fence->handle();
+ VkSemaphoreWaitInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO };
+ info.semaphoreCount = 1;
+ info.pSemaphores = &semaphore;
+ info.pValues = &acquire.FenceValue;
- if (m_vkd->vkWaitSemaphores(m_vkd->device(), &info, -1)) {
- Logger::warn("DxvkKeyedMutex::AcquireSync: Failed to wait semaphore");
- return DXGI_ERROR_INVALID_CALL;
+ if (m_vkd->vkWaitSemaphores(m_vkd->device(), &info, -1)) {
+ Logger::warn("DxvkKeyedMutex::AcquireSync: Failed to wait semaphore");
+ return DXGI_ERROR_INVALID_CALL;
+ }
}
m_fenceValue = acquire.FenceValue;
m_owned.store(true, std::memory_order_release);
return S_OK;
}
HRESULT DxvkKeyedMutex::ReleaseSync(UINT64 key) {
if (!m_owned.load(std::memory_order_acquire))
return DXGI_ERROR_INVALID_CALL;
- VkSemaphoreSignalInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO };
- info.semaphore = m_fence->handle();
- info.value = m_fenceValue + 1;
+ uint64_t fenceValue = m_fenceValue;
- if (m_vkd->vkSignalSemaphore(m_vkd->device(), &info)) {
- Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to signal semaphore");
- return DXGI_ERROR_INVALID_CALL;
+ if (m_fence) {
+ VkSemaphoreSignalInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO };
+ info.semaphore = m_fence->handle();
+ info.value = ++fenceValue;
+
+ if (m_vkd->vkSignalSemaphore(m_vkd->device(), &info)) {
+ Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to signal semaphore");
+ return DXGI_ERROR_INVALID_CALL;
+ }
}
D3DKMT_RELEASEKEYEDMUTEX release = { };
release.hKeyedMutex = m_kmtLocal;
release.Key = key;
- release.FenceValue = m_fenceValue + 1;
+ release.FenceValue = fenceValue;
if (D3DKMTReleaseKeyedMutex(&release)) {
Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to release mutex.");
return DXGI_ERROR_INVALID_CALL;
}
m_owned.store(false, std::memory_order_release);
return S_OK;
}
DxvkImage::DxvkImage(
DxvkDevice* device,
const DxvkImageCreateInfo& createInfo,
DxvkMemoryAllocator& allocator,
VkMemoryPropertyFlags memFlags)
: DxvkPagedResource(allocator),
m_vkd (device->vkd()),
m_properties (memFlags),
m_shaderStages (util::shaderStages(createInfo.stages)),
@@ -0,0 +1,101 @@
diff --git a/src/dxvk/dxvk_image.cpp b/src/dxvk/dxvk_image.cpp
index 5bcb22a..e98547d 100644
--- a/src/dxvk/dxvk_image.cpp
+++ b/src/dxvk/dxvk_image.cpp
@@ -54,75 +54,81 @@ namespace dxvk {
HRESULT DxvkKeyedMutex::AcquireSync(UINT64 key, DWORD milliseconds) {
if (m_owned.load(std::memory_order_acquire))
return DXGI_ERROR_INVALID_CALL;
LARGE_INTEGER timeout = { };
timeout.QuadPart = int64_t(milliseconds) * -10000;
D3DKMT_ACQUIREKEYEDMUTEX acquire = { };
acquire.hKeyedMutex = m_kmtLocal;
acquire.Key = key;
acquire.pTimeout = milliseconds == INFINITE ? nullptr : &timeout;
NTSTATUS status = D3DKMTAcquireKeyedMutex(&acquire);
if (status == STATUS_TIMEOUT)
return WAIT_TIMEOUT;
if (status)
return DXGI_ERROR_INVALID_CALL;
- VkSemaphore semaphore = m_fence->handle();
+ if (m_fence) {
+ VkSemaphore semaphore = m_fence->handle();
- VkSemaphoreWaitInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO };
- info.semaphoreCount = 1;
- info.pSemaphores = &semaphore;
- info.pValues = &acquire.FenceValue;
+ VkSemaphoreWaitInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO };
+ info.semaphoreCount = 1;
+ info.pSemaphores = &semaphore;
+ info.pValues = &acquire.FenceValue;
- if (m_vkd->vkWaitSemaphores(m_vkd->device(), &info, -1)) {
- Logger::warn("DxvkKeyedMutex::AcquireSync: Failed to wait semaphore");
- return DXGI_ERROR_INVALID_CALL;
+ if (m_vkd->vkWaitSemaphores(m_vkd->device(), &info, -1)) {
+ Logger::warn("DxvkKeyedMutex::AcquireSync: Failed to wait semaphore");
+ return DXGI_ERROR_INVALID_CALL;
+ }
}
m_fenceValue = acquire.FenceValue;
m_owned.store(true, std::memory_order_release);
return S_OK;
}
HRESULT DxvkKeyedMutex::ReleaseSync(UINT64 key) {
if (!m_owned.load(std::memory_order_acquire))
return DXGI_ERROR_INVALID_CALL;
- VkSemaphoreSignalInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO };
- info.semaphore = m_fence->handle();
- info.value = m_fenceValue + 1;
+ uint64_t fenceValue = m_fenceValue;
- if (m_vkd->vkSignalSemaphore(m_vkd->device(), &info)) {
- Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to signal semaphore");
- return DXGI_ERROR_INVALID_CALL;
+ if (m_fence) {
+ VkSemaphoreSignalInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO };
+ info.semaphore = m_fence->handle();
+ info.value = ++fenceValue;
+
+ if (m_vkd->vkSignalSemaphore(m_vkd->device(), &info)) {
+ Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to signal semaphore");
+ return DXGI_ERROR_INVALID_CALL;
+ }
}
D3DKMT_RELEASEKEYEDMUTEX release = { };
release.hKeyedMutex = m_kmtLocal;
release.Key = key;
- release.FenceValue = m_fenceValue + 1;
+ release.FenceValue = fenceValue;
if (D3DKMTReleaseKeyedMutex(&release)) {
Logger::warn("D3D11DXGIKeyedMutex::ReleaseSync: Failed to release mutex.");
return DXGI_ERROR_INVALID_CALL;
}
m_owned.store(false, std::memory_order_release);
return S_OK;
}
DxvkImage::DxvkImage(
DxvkDevice* device,
const DxvkImageCreateInfo& createInfo,
DxvkMemoryAllocator& allocator,
VkMemoryPropertyFlags memFlags)
: DxvkPagedResource(allocator),
m_vkd (device->vkd()),
m_properties (memFlags),
m_shaderStages (util::shaderStages(createInfo.stages)),
+66
View File
@@ -0,0 +1,66 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
PATCHES = {
'pinned component': ROOT / 'tools/dxvk/patches/0005-keyed-mutex-without-fence.patch',
'DroidDeck-Proton': ROOT / 'tools/dxvk/proton/keyed-mutex-without-fence.patch',
}
CHECK = ROOT / 'tools/dxvk/check_keyed_mutex_fence.py'
TARGET = 'src/dxvk/dxvk_image.cpp'
def pre_image(patch):
lines = []
inside = False
for line in patch.read_text().splitlines():
if line.startswith('@@'):
inside = True
lines.append('')
elif inside and line[:1] in (' ', '-') and not line.startswith('---'):
lines.append(line[1:])
return '\n'.join(lines) + '\n'
@unittest.skipUnless(shutil.which(os.environ.get('HOST_CXX', 'c++')), 'needs a native C++ compiler')
class KeyedMutexFenceTest(unittest.TestCase):
def setUp(self):
temporary = tempfile.TemporaryDirectory()
self.addCleanup(temporary.cleanup)
self.tree = Path(temporary.name)
def run_check(self, path):
return subprocess.run(['python3', '-I', str(CHECK), str(path)],
capture_output=True, text=True, timeout=120)
def test_patched_source_passes(self):
for name, patch in PATCHES.items():
with self.subTest(name):
tree = self.tree / patch.stem
(tree / 'src/dxvk').mkdir(parents=True)
target = tree / TARGET
target.write_text(pre_image(patch))
subprocess.run(['git', 'apply', '--unsafe-paths', '--directory', str(tree), str(patch)],
check=True, cwd=tree)
result = self.run_check(target)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn('PASS', result.stdout)
def test_unpatched_source_is_rejected(self):
for name, patch in PATCHES.items():
with self.subTest(name):
tree = self.tree / patch.stem
(tree / 'src/dxvk').mkdir(parents=True)
target = tree / TARGET
target.write_text(pre_image(patch))
result = self.run_check(target)
self.assertNotEqual(result.returncode, 0)
self.assertIn('FAIL', result.stderr)
if __name__ == '__main__':
unittest.main()