1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-19 04:52:01 +00:00

Avoid allocating unmanaged string per shader (#3730)

* Avoid reallocating same unmanaged string per shader

* Address PR feedback

* Rename to _disposed
This commit is contained in:
mageven 2022-10-02 14:29:34 +05:30 committed by GitHub
parent 33e673ceb8
commit 96bf7f8522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,17 +9,19 @@ using System.Threading.Tasks;
namespace Ryujinx.Graphics.Vulkan namespace Ryujinx.Graphics.Vulkan
{ {
class Shader class Shader : IDisposable
{ {
// The shaderc.net dependency's Options constructor and dispose are not thread safe. // The shaderc.net dependency's Options constructor and dispose are not thread safe.
// Take this lock when using them. // Take this lock when using them.
private static object _shaderOptionsLock = new object(); private static object _shaderOptionsLock = new object();
private static readonly IntPtr _ptrMainEntryPointName = Marshal.StringToHGlobalAnsi("main");
private readonly Vk _api; private readonly Vk _api;
private readonly Device _device; private readonly Device _device;
private readonly ShaderStageFlags _stage; private readonly ShaderStageFlags _stage;
private IntPtr _entryPointName; private bool _disposed;
private ShaderModule _module; private ShaderModule _module;
public ShaderStageFlags StageFlags => _stage; public ShaderStageFlags StageFlags => _stage;
@ -39,7 +41,6 @@ namespace Ryujinx.Graphics.Vulkan
CompileStatus = ProgramLinkStatus.Incomplete; CompileStatus = ProgramLinkStatus.Incomplete;
_stage = shaderSource.Stage.Convert(); _stage = shaderSource.Stage.Convert();
_entryPointName = Marshal.StringToHGlobalAnsi("main");
CompileTask = Task.Run(() => CompileTask = Task.Run(() =>
{ {
@ -145,7 +146,7 @@ namespace Ryujinx.Graphics.Vulkan
SType = StructureType.PipelineShaderStageCreateInfo, SType = StructureType.PipelineShaderStageCreateInfo,
Stage = _stage, Stage = _stage,
Module = _module, Module = _module,
PName = (byte*)_entryPointName PName = (byte*)_ptrMainEntryPointName
}; };
} }
@ -156,11 +157,10 @@ namespace Ryujinx.Graphics.Vulkan
public unsafe void Dispose() public unsafe void Dispose()
{ {
if (_entryPointName != IntPtr.Zero) if (!_disposed)
{ {
_api.DestroyShaderModule(_device, _module, null); _api.DestroyShaderModule(_device, _module, null);
Marshal.FreeHGlobal(_entryPointName); _disposed = true;
_entryPointName = IntPtr.Zero;
} }
} }
} }