Skip to content

Commit 2a720c3

Browse files
committed
Revert "feat(graphics/rdr3): various graphics improvements"
This reverts commit c0bf21c.
1 parent e09833e commit 2a720c3

8 files changed

Lines changed: 96 additions & 568 deletions

File tree

code/components/glue/src/ConnectToNative.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ static InitFunction initFunction([] ()
10381038
{
10391039
static bool done = ([]
10401040
{
1041+
#ifdef GTA_FIVE
10411042
std::thread([]
10421043
{
10431044
UiDone();
@@ -1050,6 +1051,7 @@ static InitFunction initFunction([] ()
10501051
SetForegroundWindow(hWnd);
10511052
})
10521053
.detach();
1054+
#endif
10531055

10541056
MarkNuiLoaded();
10551057

code/components/glue/src/GtaNui.cpp

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,73 @@ fwRefContainer<GITexture> GtaNuiInterface::CreateTextureFromShareHandle(HANDLE s
643643

644644
if (texRef)
645645
{
646-
rage::sga::Driver_Destroy_Texture(texRef);
646+
rage::sga::Driver_Destroy_Texture(texRef);
647647

648+
// Vulkan API magic time (copy/pasted from samples on GH)
648649
VkDevice device = (VkDevice)GetGraphicsDriverHandle();
649-
VkPhysicalDevice physicalDevice = GetVulkanPhysicalHandle();
650-
650+
651+
VkExtent3D Extent = { width, height, 1 };
652+
653+
VkExternalMemoryImageCreateInfo ExternalMemoryImageCreateInfo = { VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO };
654+
ExternalMemoryImageCreateInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT;
655+
VkImageCreateInfo ImageCreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
656+
ImageCreateInfo.pNext = &ExternalMemoryImageCreateInfo;
657+
ImageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
658+
ImageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
659+
ImageCreateInfo.extent = Extent;
660+
ImageCreateInfo.mipLevels = 1;
661+
ImageCreateInfo.arrayLayers = 1;
662+
ImageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
663+
ImageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
664+
ImageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
665+
ImageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
666+
ImageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
667+
651668
VkImage Image;
652-
VkDeviceMemory DeviceMemory;
653-
CreateVKImageFromShareHandle(device, shareHandle, width, height, Image, DeviceMemory);
669+
VkResult result = vkCreateImage(device, &ImageCreateInfo, nullptr, &Image);
670+
671+
if (result != VK_SUCCESS)
672+
{
673+
FatalError("Failed to create a Vulkan image. VkResult: %s", ResultToString(result));
674+
}
675+
676+
VkMemoryRequirements MemoryRequirements;
677+
vkGetImageMemoryRequirements(device, Image, &MemoryRequirements);
678+
679+
VkMemoryDedicatedAllocateInfo MemoryDedicatedAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO };
680+
MemoryDedicatedAllocateInfo.image = Image;
681+
VkImportMemoryWin32HandleInfoKHR ImportMemoryWin32HandleInfo = { VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
682+
ImportMemoryWin32HandleInfo.pNext = &MemoryDedicatedAllocateInfo;
683+
ImportMemoryWin32HandleInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT;
684+
ImportMemoryWin32HandleInfo.handle = shareHandle;
685+
VkMemoryAllocateInfo MemoryAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
686+
MemoryAllocateInfo.pNext = &ImportMemoryWin32HandleInfo;
687+
MemoryAllocateInfo.allocationSize = MemoryRequirements.size;
688+
689+
unsigned long typeIndex;
690+
_BitScanForward(&typeIndex, MemoryRequirements.memoryTypeBits);
691+
MemoryAllocateInfo.memoryTypeIndex = typeIndex;
692+
693+
static auto _vkBindImageMemory2 = (PFN_vkBindImageMemory2)vkGetDeviceProcAddr(device, "vkBindImageMemory2");
694+
695+
VkDeviceMemory ImageMemory;
696+
result = vkAllocateMemory(device, &MemoryAllocateInfo, nullptr, &ImageMemory);
697+
698+
if (result != VK_SUCCESS)
699+
{
700+
FatalError("Failed to allocate memory for Vulkan. VkResult: %s", ResultToString(result));
701+
}
702+
703+
VkBindImageMemoryInfo BindImageMemoryInfo = { VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO };
704+
BindImageMemoryInfo.image = Image;
705+
BindImageMemoryInfo.memory = ImageMemory;
706+
707+
result = _vkBindImageMemory2(device, 1, &BindImageMemoryInfo);
708+
709+
if (result != VK_SUCCESS)
710+
{
711+
FatalError("Failed to bind Vulkan image memory. VkResult: %s", ResultToString(result));
712+
}
654713

655714
auto newImage = new rage::sga::TextureVK::ImageData;
656715
//memcpy(newImage, texRef->image, sizeof(*newImage));
@@ -660,9 +719,7 @@ fwRefContainer<GITexture> GtaNuiInterface::CreateTextureFromShareHandle(HANDLE s
660719
texRef->image = newImage;
661720

662721
texRef->image->image = Image;
663-
texRef->image->memory = DeviceMemory;
664-
texRef->width = width;
665-
texRef->height = height;
722+
texRef->image->memory = ImageMemory;
666723

667724
rage::sga::TextureViewDesc srvDesc;
668725
srvDesc.mipLevels = 1;
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
includedirs {
2-
'../../../vendor/vulkan-headers/include/',
3-
'../rage-graphics-five/include'
4-
}
5-
6-
return function()
7-
filter {}
8-
9-
files {
10-
'components/rage-graphics-five/include/dxerr.h',
11-
'components/rage-graphics-five/src/dxerr.cpp'
12-
}
13-
end
1+
includedirs { '../../../vendor/vulkan-headers/include/' }

code/components/rage-graphics-rdr3/include/DrawCommands.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ extern GFX_EXPORT GraphicsAPI GetCurrentGraphicsAPI();
7575
// VK context or D3D12 device
7676
extern GFX_EXPORT void* GetGraphicsDriverHandle();
7777

78-
// VK physicalDevice
79-
extern GFX_EXPORT VkPhysicalDevice GetVulkanPhysicalHandle();
80-
8178
namespace rage::sga
8279
{
8380
class GFX_EXPORT GraphicsContext

code/components/rage-graphics-rdr3/include/VulkanHelper.h

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
#if IS_RDR3
44
#include <string_view>
55
#include <vulkan/vulkan_core.h>
6-
#include <vulkan/vulkan_win32.h>
7-
8-
#include <DrawCommands.h>
96

107
inline std::string_view ResultToString(VkResult result)
118
{
@@ -74,77 +71,5 @@ inline std::string_view ResultToString(VkResult result)
7471
default:
7572
return std::to_string(static_cast<uint32_t>(result));
7673
}
77-
}
78-
79-
static uint32_t FindMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties)
80-
{
81-
VkPhysicalDeviceMemoryProperties memProperties;
82-
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
83-
84-
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
85-
{
86-
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
87-
{
88-
return i;
89-
}
90-
}
91-
92-
return INT32_MAX;
93-
}
94-
95-
static void CreateVKImageFromShareHandle(VkDevice& device, HANDLE handle, unsigned int width, unsigned int height, VkImage& outImage, VkDeviceMemory& outMemory)
96-
{
97-
VkExternalMemoryImageCreateInfo ExternalMemoryImageCreateInfo = { VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO };
98-
ExternalMemoryImageCreateInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT;
99-
VkImageCreateInfo ImageCreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
100-
ImageCreateInfo.pNext = &ExternalMemoryImageCreateInfo;
101-
ImageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
102-
ImageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
103-
ImageCreateInfo.extent = { width, height, 1 };
104-
ImageCreateInfo.mipLevels = 1;
105-
ImageCreateInfo.arrayLayers = 1;
106-
ImageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
107-
ImageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
108-
ImageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
109-
ImageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
110-
ImageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
111-
112-
VkResult result;
113-
if (result = vkCreateImage(device, &ImageCreateInfo, nullptr, &outImage); result != VK_SUCCESS)
114-
{
115-
FatalError("Failed to create a Vulkan image. VkResult: %s", ResultToString(result));
116-
}
117-
118-
VkMemoryRequirements MemoryRequirements;
119-
vkGetImageMemoryRequirements(device, outImage, &MemoryRequirements);
120-
121-
VkMemoryDedicatedAllocateInfo MemoryDedicatedAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO };
122-
MemoryDedicatedAllocateInfo.image = outImage;
123-
VkImportMemoryWin32HandleInfoKHR ImportMemoryWin32HandleInfo = { VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
124-
ImportMemoryWin32HandleInfo.pNext = &MemoryDedicatedAllocateInfo;
125-
ImportMemoryWin32HandleInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT;
126-
ImportMemoryWin32HandleInfo.handle = handle;
127-
VkMemoryAllocateInfo MemoryAllocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
128-
MemoryAllocateInfo.pNext = &ImportMemoryWin32HandleInfo;
129-
MemoryAllocateInfo.allocationSize = MemoryRequirements.size;
130-
131-
VkPhysicalDeviceMemoryProperties memProps;
132-
vkGetPhysicalDeviceMemoryProperties(GetVulkanPhysicalHandle(), &memProps);
133-
134-
MemoryAllocateInfo.memoryTypeIndex = FindMemoryType(GetVulkanPhysicalHandle(), MemoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
135-
if (MemoryAllocateInfo.memoryTypeIndex == INT32_MAX)
136-
{
137-
FatalError("Failed to compatible memory type for NUI. This system may not be equipped to run RedM under Vulkan.\n");
138-
}
139-
140-
if (result = vkAllocateMemory(device, &MemoryAllocateInfo, nullptr, &outMemory); result != VK_SUCCESS)
141-
{
142-
FatalError("Failed to allocate memory for Vulkan. VkResult: %s", ResultToString(result));
143-
}
144-
145-
if (result = vkBindImageMemory(device, outImage, outMemory, 0); result != VK_SUCCESS)
146-
{
147-
FatalError("Failed to bind Vulkan image memory. VkResult: %s", ResultToString(result));
148-
}
14974
}
15075
#endif

code/components/rage-graphics-rdr3/include/grcTexture.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ namespace rage
177177
uint32_t pad[24];
178178
};
179179

180-
char pad[0x10];
181-
uint16_t width;
182-
uint16_t height;
183-
char pad2[44];
180+
char pad[64];
184181
ImageData* image;
185182
};
186183

code/components/rage-graphics-rdr3/src/GfxSpec.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,19 @@ static void WrapEndDraw(void* cxt)
522522
origEndDraw(cxt);
523523
}
524524

525+
static LPWSTR GetCommandLineWHook()
526+
{
527+
static wchar_t str[8192];
528+
wcscpy(str, GetCommandLineW());
529+
530+
if (!wcsstr(str, L"sgadriver"))
531+
{
532+
wcscat(str, L" -sgadriver=d3d12");
533+
}
534+
535+
return str;
536+
}
537+
525538
static void* g_d3d12Driver;
526539
static void* g_vkDriver;
527540

@@ -542,8 +555,6 @@ GraphicsAPI GetCurrentGraphicsAPI()
542555
void** g_d3d12Device;
543556
VkDevice* g_vkHandle;
544557

545-
VkPhysicalDevice* g_vkPhysicalHandle;
546-
547558
void* GetGraphicsDriverHandle()
548559
{
549560
switch (GetCurrentGraphicsAPI())
@@ -557,14 +568,6 @@ void* GetGraphicsDriverHandle()
557568
}
558569
}
559570

560-
GFX_EXPORT VkPhysicalDevice GetVulkanPhysicalHandle()
561-
{
562-
if (GetCurrentGraphicsAPI() == GraphicsAPI::Vulkan)
563-
{
564-
return *g_vkPhysicalHandle;
565-
}
566-
}
567-
568571
namespace rage::sga
569572
{
570573
void Driver_Create_ShaderResourceView(rage::sga::Texture* texture, const rage::sga::TextureViewDesc& desc)
@@ -676,17 +679,9 @@ void DynamicTexture2::UnmapInternal(GraphicsContext* context, const MapData& map
676679
}
677680

678681
static bool ShouldUsePipelineCache(const char* pipelineCachePrefix)
679-
{
680-
// This file is created on any hooked graphics related crashes (currently ERR_GFX_STATE)
681-
FILE* f = _wfopen(MakeRelativeCitPath("data/cache/clearPipelineCache").c_str(), L"r");
682-
if (!f)
683-
{
684-
fclose(f);
685-
return true;
686-
}
687-
688-
_wunlink(MakeRelativeCitPath("data/cache/clearPipelineCache").c_str());
689-
return false;
682+
{
683+
// #TODO: check for ERR_GFX_STATE or similar failures last launch?
684+
return true;
690685
}
691686

692687
static HookFunction hookFunction([]()
@@ -727,13 +722,15 @@ static HookFunction hookFunction([]()
727722

728723
g_d3d12Device = hook::get_address<decltype(g_d3d12Device)>(hook::get_pattern("48 8B 01 FF 50 78 48 8B 0B 48 8D", -7));
729724
g_vkHandle = hook::get_address<decltype(g_vkHandle)>(hook::get_pattern("8D 50 41 8B CA 44 8B C2 F3 48 AB 48 8B 0D", 14));
730-
g_vkPhysicalHandle = hook::get_address<decltype(g_vkPhysicalHandle)>(hook::get_pattern("FF 15 ? ? ? ? 33 D2 48 8D 4C 24 ? 41 B8", 27));
731725

732726
{
733727
auto location = hook::get_pattern<char>("83 25 ? ? ? ? 00 83 25 ? ? ? ? 00 D1 F8 89 05", -0x26);
734728
rage::g_WindowWidth = hook::get_address<int*>(location + 6);
735729
rage::g_WindowHeight = hook::get_address<int*>(location + 0x3E);
736730
}
737731

732+
// #TODORDR: badly force d3d12 sga driver (vulkan crashes on older Windows 10?)
733+
hook::iat("kernel32.dll", GetCommandLineWHook, "GetCommandLineW");
734+
738735
MH_EnableHook(MH_ALL_HOOKS);
739736
});

0 commit comments

Comments
 (0)