From 92b3afdf82b35cc0cf22653f20e247a402b0e8b9 Mon Sep 17 00:00:00 2001 From: Kaydax Date: Thu, 24 Dec 2020 10:36:32 -0500 Subject: [PATCH 01/10] Fix ImGui.NET.SampleProgram to work with Vulkan on the latest Vildrid releases --- .../ImGuiController.cs | 4 +-- .../Shaders/SPIR-V/imgui-frag.glsl | 2 +- .../Shaders/SPIR-V/imgui-frag.spv | Bin 868 -> 868 bytes .../Shaders/SPIR-V/imgui-vertex.glsl | 23 +++++++++--------- .../Shaders/SPIR-V/imgui-vertex.spv | Bin 1444 -> 1308 bytes 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/ImGui.NET.SampleProgram/ImGuiController.cs b/src/ImGui.NET.SampleProgram/ImGuiController.cs index 3c600ad..b636e28 100644 --- a/src/ImGui.NET.SampleProgram/ImGuiController.cs +++ b/src/ImGui.NET.SampleProgram/ImGuiController.cs @@ -99,8 +99,8 @@ namespace ImGuiNET byte[] vertexShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex); byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment); - _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "VS")); - _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "FS")); + _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "main")); + _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "main")); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { diff --git a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.glsl b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.glsl index 2d1c1d7..f94fa48 100644 --- a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.glsl +++ b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.glsl @@ -13,4 +13,4 @@ layout (location = 0) out vec4 outputColor; void main() { outputColor = color * texture(sampler2D(FontTexture, FontSampler), texCoord); -} +} \ No newline at end of file diff --git a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.spv b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-frag.spv index c8d71cc6f5d75cf680b533a3ce31c8fea24c82f6..5d3d96f32e08822f4621e0f813cf814e668b2a67 100644 GIT binary patch delta 18 ZcmaFD_Joa-nMs+Qfq{{MYa?egGXN!T126yp delta 18 ZcmaFD_Joa-nMs+Qfq{{MX(MMeGXNz=11JCh diff --git a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.glsl b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.glsl index 6fdab3f..6249a36 100644 --- a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.glsl +++ b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.glsl @@ -3,27 +3,26 @@ #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable -layout (location = 0) in vec2 vsin_position; -layout (location = 1) in vec2 vsin_texCoord; -layout (location = 2) in vec4 vsin_color; +layout (location = 0) in vec2 in_position; +layout (location = 1) in vec2 in_texCoord; +layout (location = 2) in vec4 in_color; -layout (binding = 0) uniform Projection +layout (binding = 0) uniform ProjectionMatrixBuffer { - mat4 projection; + mat4 projection_matrix; }; -layout (location = 0) out vec4 vsout_color; -layout (location = 1) out vec2 vsout_texCoord; +layout (location = 0) out vec4 color; +layout (location = 1) out vec2 texCoord; -out gl_PerVertex +out gl_PerVertex { vec4 gl_Position; }; void main() { - gl_Position = projection * vec4(vsin_position, 0, 1); - vsout_color = vsin_color; - vsout_texCoord = vsin_texCoord; - gl_Position.y = -gl_Position.y; + gl_Position = projection_matrix * vec4(in_position, 0, 1); + color = in_color; + texCoord = in_texCoord; } diff --git a/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.spv b/src/ImGui.NET.SampleProgram/Shaders/SPIR-V/imgui-vertex.spv index 795cd0e728725a28a1c8d616cba0cdb494434aff..b40ec8ca53630b09e586eda3ada5c2196a047e3e 100644 GIT binary patch delta 211 zcmZ3&J%@{znMs+Qfq{{Mi-ChdV=Hxy&Ezb>^7c8CXVX8z&VTdj>z137d#bSQ~I z+V@y1%3 Date: Thu, 24 Dec 2020 12:47:23 -0800 Subject: [PATCH 02/10] Pass additional GraphicsDeviceOptions. --- src/ImGui.NET.SampleProgram/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImGui.NET.SampleProgram/Program.cs b/src/ImGui.NET.SampleProgram/Program.cs index bc93d83..8b2c381 100644 --- a/src/ImGui.NET.SampleProgram/Program.cs +++ b/src/ImGui.NET.SampleProgram/Program.cs @@ -36,7 +36,7 @@ namespace ImGuiNET // Create window, GraphicsDevice, and all resources necessary for the demo. VeldridStartup.CreateWindowAndGraphicsDevice( new WindowCreateInfo(50, 50, 1280, 720, WindowState.Normal, "ImGui.NET Sample Program"), - new GraphicsDeviceOptions(true, null, true), + new GraphicsDeviceOptions(true, null, true, ResourceBindingModel.Improved, true, true), out _window, out _gd); _window.Resized += () => From 351a3de4ca89a3a544042e91c3fad80c9856a320 Mon Sep 17 00:00:00 2001 From: Zaafar Date: Sat, 5 Dec 2020 13:08:25 -0500 Subject: [PATCH 03/10] Stop generation of igCalcTextSize and igInputTextWithHint func. They are manually created in ImGui.Manual.cs file. --- src/CodeGenerator/TypeInfo.cs | 4 +- src/ImGui.NET/Generated/ImGui.gen.cs | 322 --------------------------- 2 files changed, 3 insertions(+), 323 deletions(-) diff --git a/src/CodeGenerator/TypeInfo.cs b/src/CodeGenerator/TypeInfo.cs index b4f94b4..07539c4 100644 --- a/src/CodeGenerator/TypeInfo.cs +++ b/src/CodeGenerator/TypeInfo.cs @@ -99,7 +99,9 @@ namespace CodeGenerator public static readonly HashSet SkippedFunctions = new HashSet() { "igInputText", - "igInputTextMultiline" + "igInputTextMultiline", + "igCalcTextSize", + "igInputTextWithHint" }; } } \ No newline at end of file diff --git a/src/ImGui.NET/Generated/ImGui.gen.cs b/src/ImGui.NET/Generated/ImGui.gen.cs index 8058e97..ea427fd 100644 --- a/src/ImGui.NET/Generated/ImGui.gen.cs +++ b/src/ImGui.NET/Generated/ImGui.gen.cs @@ -1093,37 +1093,6 @@ namespace ImGuiNET float ret = ImGuiNative.igCalcItemWidth(); return ret; } - public static Vector2 CalcTextSize(string text) - { - Vector2 __retval; - byte* native_text; - int text_byteCount = 0; - if (text != null) - { - text_byteCount = Encoding.UTF8.GetByteCount(text); - if (text_byteCount > Util.StackAllocationSizeLimit) - { - native_text = Util.Allocate(text_byteCount + 1); - } - else - { - byte* native_text_stackBytes = stackalloc byte[text_byteCount + 1]; - native_text = native_text_stackBytes; - } - int native_text_offset = Util.GetUtf8(text, native_text, text_byteCount); - native_text[native_text_offset] = 0; - } - else { native_text = null; } - byte* native_text_end = null; - byte hide_text_after_double_hash = 0; - float wrap_width = -1.0f; - ImGuiNative.igCalcTextSize(&__retval, native_text, native_text_end, hide_text_after_double_hash, wrap_width); - if (text_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_text); - } - return __retval; - } public static void CaptureKeyboardFromApp() { byte want_capture_keyboard_value = 1; @@ -8085,297 +8054,6 @@ namespace ImGuiNET } return ret != 0; } - public static bool InputTextWithHint(string label, string hint, string buf, uint buf_size) - { - byte* native_label; - int label_byteCount = 0; - if (label != null) - { - label_byteCount = Encoding.UTF8.GetByteCount(label); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - native_label = Util.Allocate(label_byteCount + 1); - } - else - { - byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1]; - native_label = native_label_stackBytes; - } - int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - else { native_label = null; } - byte* native_hint; - int hint_byteCount = 0; - if (hint != null) - { - hint_byteCount = Encoding.UTF8.GetByteCount(hint); - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - native_hint = Util.Allocate(hint_byteCount + 1); - } - else - { - byte* native_hint_stackBytes = stackalloc byte[hint_byteCount + 1]; - native_hint = native_hint_stackBytes; - } - int native_hint_offset = Util.GetUtf8(hint, native_hint, hint_byteCount); - native_hint[native_hint_offset] = 0; - } - else { native_hint = null; } - byte* native_buf; - int buf_byteCount = 0; - if (buf != null) - { - buf_byteCount = Encoding.UTF8.GetByteCount(buf); - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - native_buf = Util.Allocate(buf_byteCount + 1); - } - else - { - byte* native_buf_stackBytes = stackalloc byte[buf_byteCount + 1]; - native_buf = native_buf_stackBytes; - } - int native_buf_offset = Util.GetUtf8(buf, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - else { native_buf = null; } - ImGuiInputTextFlags flags = (ImGuiInputTextFlags)0; - ImGuiInputTextCallback callback = null; - void* user_data = null; - byte ret = ImGuiNative.igInputTextWithHint(native_label, native_hint, native_buf, buf_size, flags, callback, user_data); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_label); - } - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_hint); - } - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_buf); - } - return ret != 0; - } - public static bool InputTextWithHint(string label, string hint, string buf, uint buf_size, ImGuiInputTextFlags flags) - { - byte* native_label; - int label_byteCount = 0; - if (label != null) - { - label_byteCount = Encoding.UTF8.GetByteCount(label); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - native_label = Util.Allocate(label_byteCount + 1); - } - else - { - byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1]; - native_label = native_label_stackBytes; - } - int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - else { native_label = null; } - byte* native_hint; - int hint_byteCount = 0; - if (hint != null) - { - hint_byteCount = Encoding.UTF8.GetByteCount(hint); - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - native_hint = Util.Allocate(hint_byteCount + 1); - } - else - { - byte* native_hint_stackBytes = stackalloc byte[hint_byteCount + 1]; - native_hint = native_hint_stackBytes; - } - int native_hint_offset = Util.GetUtf8(hint, native_hint, hint_byteCount); - native_hint[native_hint_offset] = 0; - } - else { native_hint = null; } - byte* native_buf; - int buf_byteCount = 0; - if (buf != null) - { - buf_byteCount = Encoding.UTF8.GetByteCount(buf); - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - native_buf = Util.Allocate(buf_byteCount + 1); - } - else - { - byte* native_buf_stackBytes = stackalloc byte[buf_byteCount + 1]; - native_buf = native_buf_stackBytes; - } - int native_buf_offset = Util.GetUtf8(buf, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - else { native_buf = null; } - ImGuiInputTextCallback callback = null; - void* user_data = null; - byte ret = ImGuiNative.igInputTextWithHint(native_label, native_hint, native_buf, buf_size, flags, callback, user_data); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_label); - } - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_hint); - } - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_buf); - } - return ret != 0; - } - public static bool InputTextWithHint(string label, string hint, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback) - { - byte* native_label; - int label_byteCount = 0; - if (label != null) - { - label_byteCount = Encoding.UTF8.GetByteCount(label); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - native_label = Util.Allocate(label_byteCount + 1); - } - else - { - byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1]; - native_label = native_label_stackBytes; - } - int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - else { native_label = null; } - byte* native_hint; - int hint_byteCount = 0; - if (hint != null) - { - hint_byteCount = Encoding.UTF8.GetByteCount(hint); - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - native_hint = Util.Allocate(hint_byteCount + 1); - } - else - { - byte* native_hint_stackBytes = stackalloc byte[hint_byteCount + 1]; - native_hint = native_hint_stackBytes; - } - int native_hint_offset = Util.GetUtf8(hint, native_hint, hint_byteCount); - native_hint[native_hint_offset] = 0; - } - else { native_hint = null; } - byte* native_buf; - int buf_byteCount = 0; - if (buf != null) - { - buf_byteCount = Encoding.UTF8.GetByteCount(buf); - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - native_buf = Util.Allocate(buf_byteCount + 1); - } - else - { - byte* native_buf_stackBytes = stackalloc byte[buf_byteCount + 1]; - native_buf = native_buf_stackBytes; - } - int native_buf_offset = Util.GetUtf8(buf, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - else { native_buf = null; } - void* user_data = null; - byte ret = ImGuiNative.igInputTextWithHint(native_label, native_hint, native_buf, buf_size, flags, callback, user_data); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_label); - } - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_hint); - } - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_buf); - } - return ret != 0; - } - public static bool InputTextWithHint(string label, string hint, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, IntPtr user_data) - { - byte* native_label; - int label_byteCount = 0; - if (label != null) - { - label_byteCount = Encoding.UTF8.GetByteCount(label); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - native_label = Util.Allocate(label_byteCount + 1); - } - else - { - byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1]; - native_label = native_label_stackBytes; - } - int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - else { native_label = null; } - byte* native_hint; - int hint_byteCount = 0; - if (hint != null) - { - hint_byteCount = Encoding.UTF8.GetByteCount(hint); - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - native_hint = Util.Allocate(hint_byteCount + 1); - } - else - { - byte* native_hint_stackBytes = stackalloc byte[hint_byteCount + 1]; - native_hint = native_hint_stackBytes; - } - int native_hint_offset = Util.GetUtf8(hint, native_hint, hint_byteCount); - native_hint[native_hint_offset] = 0; - } - else { native_hint = null; } - byte* native_buf; - int buf_byteCount = 0; - if (buf != null) - { - buf_byteCount = Encoding.UTF8.GetByteCount(buf); - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - native_buf = Util.Allocate(buf_byteCount + 1); - } - else - { - byte* native_buf_stackBytes = stackalloc byte[buf_byteCount + 1]; - native_buf = native_buf_stackBytes; - } - int native_buf_offset = Util.GetUtf8(buf, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - else { native_buf = null; } - void* native_user_data = (void*)user_data.ToPointer(); - byte ret = ImGuiNative.igInputTextWithHint(native_label, native_hint, native_buf, buf_size, flags, callback, native_user_data); - if (label_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_label); - } - if (hint_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_hint); - } - if (buf_byteCount > Util.StackAllocationSizeLimit) - { - Util.Free(native_buf); - } - return ret != 0; - } public static bool InvisibleButton(string str_id, Vector2 size) { byte* native_str_id; From 0e6ed64e5c6ec28fed07101848f8022b017441a4 Mon Sep 17 00:00:00 2001 From: Zaafar Date: Sat, 5 Dec 2020 13:10:11 -0500 Subject: [PATCH 04/10] CalcTextSize now supports all arguments --- src/ImGui.NET/ImGui.Manual.cs | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index 8fac3de..8b7ba52 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -355,6 +355,47 @@ namespace ImGuiNET return result != 0; } + public static Vector2 CalcTextSize( + string text, + int? start = null, + int? length = null, + bool hideTextAfterDoubleHash = false, + float wrapWidth = -1.0f) + { + Vector2 ret; + byte* nativeText = null; + byte* nativeTextStart = null; + byte* nativeTextEnd = null; + int textByteCount = 0; + int textByteSize = 0; + if (text != null) + { + textByteCount = Encoding.UTF8.GetByteCount(text); + textByteSize = Encoding.UTF8.GetByteCount("X"); + if (textByteCount > Util.StackAllocationSizeLimit) + { + nativeText = Util.Allocate(textByteCount + 1); + } + else + { + byte* nativeTextStackBytes = stackalloc byte[textByteCount + 1]; + nativeText = nativeTextStackBytes; + } + int nativeTextOffset = Util.GetUtf8(text, nativeText, textByteCount); + nativeText[nativeTextOffset] = 0; + nativeTextStart = nativeText + (start.HasValue ? (start.Value * textByteSize) : 0); + nativeTextEnd = length.HasValue ? nativeTextStart + (length.Value + textByteSize) : null; + } + + ImGuiNative.igCalcTextSize(&ret, nativeTextStart, nativeTextEnd, *((byte*)(&hideTextAfterDoubleHash)), wrapWidth); + if (textByteCount > Util.StackAllocationSizeLimit) + { + Util.Free(nativeText); + } + + return ret; + } + public static bool InputText( string label, IntPtr buf, From 33aefc005c27f49fedbf66faceef8b718d046bc7 Mon Sep 17 00:00:00 2001 From: Zaafar Date: Sat, 5 Dec 2020 13:51:02 -0500 Subject: [PATCH 05/10] bug fix: correctly calculating String start/end. --- src/ImGui.NET/ImGui.Manual.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index 8b7ba52..ac6dd8b 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -384,7 +384,7 @@ namespace ImGuiNET int nativeTextOffset = Util.GetUtf8(text, nativeText, textByteCount); nativeText[nativeTextOffset] = 0; nativeTextStart = nativeText + (start.HasValue ? (start.Value * textByteSize) : 0); - nativeTextEnd = length.HasValue ? nativeTextStart + (length.Value + textByteSize) : null; + nativeTextEnd = length.HasValue ? nativeTextStart + (length.Value * textByteSize) : null; } ImGuiNative.igCalcTextSize(&ret, nativeTextStart, nativeTextEnd, *((byte*)(&hideTextAfterDoubleHash)), wrapWidth); From 93d375af5a059076cb1b14cf71be9842146e3184 Mon Sep 17 00:00:00 2001 From: Zaafar Date: Fri, 25 Dec 2020 03:57:19 -0500 Subject: [PATCH 06/10] handling comments - Adding overloaded functions for CalcTextSize - Making CalcTextSizeImpl private - Removing the use of textByteSize --- src/ImGui.NET/ImGui.Manual.cs | 58 +++++++++++++++++++++++++++-------- src/ImGui.NET/Util.cs | 21 +++++++++++++ 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index ac6dd8b..84926a6 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -355,42 +355,74 @@ namespace ImGuiNET return result != 0; } - public static Vector2 CalcTextSize( + public static Vector2 CalcTextSize(string text) + => CalcTextSizeImpl(text); + + public static Vector2 CalcTextSize(string text, int start) + => CalcTextSizeImpl(text, start); + + public static Vector2 CalcTextSize(string text, float wrapWidth) + => CalcTextSizeImpl(text, wrapWidth: wrapWidth); + + public static Vector2 CalcTextSize(string text, bool hideTextAfterDoubleHash) + => CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash); + + public static Vector2 CalcTextSize(string text, int start, int length) + => CalcTextSizeImpl(text, start, length); + + public static Vector2 CalcTextSize(string text, int start, bool hideTextAfterDoubleHash) + => CalcTextSizeImpl(text, start, hideTextAfterDoubleHash: hideTextAfterDoubleHash); + + public static Vector2 CalcTextSize(string text, int start, float wrapWidth) + => CalcTextSizeImpl(text, start, wrapWidth: wrapWidth); + + public static Vector2 CalcTextSize(string text, bool hideTextAfterDoubleHash, float wrapWidth) + => CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash, wrapWidth: wrapWidth); + + public static Vector2 CalcTextSize(string text, int start, int length, bool hideTextAfterDoubleHash) + => CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash); + + public static Vector2 CalcTextSize(string text, int start, int length, float wrapWidth) + => CalcTextSizeImpl(text, start, length, wrapWidth: wrapWidth); + + public static Vector2 CalcTextSize(string text, int start, int length, bool hideTextAfterDoubleHash, float wrapWidth) + => CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash, wrapWidth); + + private static Vector2 CalcTextSizeImpl( string text, - int? start = null, + int start = 0, int? length = null, bool hideTextAfterDoubleHash = false, float wrapWidth = -1.0f) { Vector2 ret; - byte* nativeText = null; byte* nativeTextStart = null; byte* nativeTextEnd = null; int textByteCount = 0; - int textByteSize = 0; if (text != null) { - textByteCount = Encoding.UTF8.GetByteCount(text); - textByteSize = Encoding.UTF8.GetByteCount("X"); + + int textToCopyLen = length.HasValue ? length.Value : text.Length; + textByteCount = Util.CalcUtf8(text, start, textToCopyLen); if (textByteCount > Util.StackAllocationSizeLimit) { - nativeText = Util.Allocate(textByteCount + 1); + nativeTextStart = Util.Allocate(textByteCount + 1); } else { byte* nativeTextStackBytes = stackalloc byte[textByteCount + 1]; - nativeText = nativeTextStackBytes; + nativeTextStart = nativeTextStackBytes; } - int nativeTextOffset = Util.GetUtf8(text, nativeText, textByteCount); - nativeText[nativeTextOffset] = 0; - nativeTextStart = nativeText + (start.HasValue ? (start.Value * textByteSize) : 0); - nativeTextEnd = length.HasValue ? nativeTextStart + (length.Value * textByteSize) : null; + + int nativeTextOffset = Util.GetUtf8(text, nativeTextStart, textByteCount, start, textToCopyLen); + nativeTextStart[nativeTextOffset] = 0; + nativeTextEnd = nativeTextStart + nativeTextOffset; } ImGuiNative.igCalcTextSize(&ret, nativeTextStart, nativeTextEnd, *((byte*)(&hideTextAfterDoubleHash)), wrapWidth); if (textByteCount > Util.StackAllocationSizeLimit) { - Util.Free(nativeText); + Util.Free(nativeTextStart); } return ret; diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index 34280a0..0df92f0 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -33,6 +33,19 @@ namespace ImGuiNET internal static byte* Allocate(int byteCount) => (byte*)Marshal.AllocHGlobal(byteCount); internal static void Free(byte* ptr) => Marshal.FreeHGlobal((IntPtr)ptr); + internal static int CalcUtf8(string s, int start, int length) + { + if (start > s.Length - 1 || length > s.Length || start + length > s.Length) + { + throw new ArgumentOutOfRangeException(); + } + + fixed (char* utf16Ptr = s) + { + return Encoding.UTF8.GetByteCount(utf16Ptr + start, length); + } + } + internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount) { fixed (char* utf16Ptr = s) @@ -40,5 +53,13 @@ namespace ImGuiNET return Encoding.UTF8.GetBytes(utf16Ptr, s.Length, utf8Bytes, utf8ByteCount); } } + + internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount, int start = 0, int length = 0) + { + fixed (char* utf16Ptr = s) + { + return Encoding.UTF8.GetBytes(utf16Ptr + start, length, utf8Bytes, utf8ByteCount); + } + } } } From 85d01be394217de854f2154a03df2c8b2d63587b Mon Sep 17 00:00:00 2001 From: Zaafar Date: Fri, 25 Dec 2020 04:19:17 -0500 Subject: [PATCH 07/10] minor fixup: updated util function signature. --- src/ImGui.NET/ImGui.Manual.cs | 4 ++-- src/ImGui.NET/Util.cs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index 84926a6..cec21cf 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -403,7 +403,7 @@ namespace ImGuiNET { int textToCopyLen = length.HasValue ? length.Value : text.Length; - textByteCount = Util.CalcUtf8(text, start, textToCopyLen); + textByteCount = Util.CalcSizeInUtf8(text, start, textToCopyLen); if (textByteCount > Util.StackAllocationSizeLimit) { nativeTextStart = Util.Allocate(textByteCount + 1); @@ -414,7 +414,7 @@ namespace ImGuiNET nativeTextStart = nativeTextStackBytes; } - int nativeTextOffset = Util.GetUtf8(text, nativeTextStart, textByteCount, start, textToCopyLen); + int nativeTextOffset = Util.GetUtf8(text, start, textToCopyLen, nativeTextStart, textByteCount); nativeTextStart[nativeTextOffset] = 0; nativeTextEnd = nativeTextStart + nativeTextOffset; } diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index 0df92f0..1753d30 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -32,8 +32,10 @@ namespace ImGuiNET } internal static byte* Allocate(int byteCount) => (byte*)Marshal.AllocHGlobal(byteCount); + internal static void Free(byte* ptr) => Marshal.FreeHGlobal((IntPtr)ptr); - internal static int CalcUtf8(string s, int start, int length) + + internal static int CalcSizeInUtf8(string s, int start, int length) { if (start > s.Length - 1 || length > s.Length || start + length > s.Length) { @@ -54,7 +56,7 @@ namespace ImGuiNET } } - internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount, int start = 0, int length = 0) + internal static int GetUtf8(string s, int start, int length, byte* utf8Bytes, int utf8ByteCount) { fixed (char* utf16Ptr = s) { From 5aae99ed9ad3215e2cb6c969f82b7039eddec803 Mon Sep 17 00:00:00 2001 From: Zaafar Date: Fri, 25 Dec 2020 04:54:25 -0500 Subject: [PATCH 08/10] added checks for ArgumentOutOfRangeException --- src/ImGui.NET/Util.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index 1753d30..f0f71d1 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -58,6 +58,11 @@ namespace ImGuiNET internal static int GetUtf8(string s, int start, int length, byte* utf8Bytes, int utf8ByteCount) { + if (start > s.Length - 1 || length > s.Length || start + length > s.Length) + { + throw new ArgumentOutOfRangeException(); + } + fixed (char* utf16Ptr = s) { return Encoding.UTF8.GetBytes(utf16Ptr + start, length, utf8Bytes, utf8ByteCount); From f7d49bcc388c670644d3c0c19cc68c494a4f484f Mon Sep 17 00:00:00 2001 From: Zaafar Date: Fri, 25 Dec 2020 10:48:17 -0500 Subject: [PATCH 09/10] updated checks for ArgumentOutOfRangeException --- src/ImGui.NET/Util.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index f0f71d1..52785b6 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -37,7 +37,7 @@ namespace ImGuiNET internal static int CalcSizeInUtf8(string s, int start, int length) { - if (start > s.Length - 1 || length > s.Length || start + length > s.Length) + if (start < 0 || length < 0 || start + length > s.Length) { throw new ArgumentOutOfRangeException(); } @@ -58,7 +58,7 @@ namespace ImGuiNET internal static int GetUtf8(string s, int start, int length, byte* utf8Bytes, int utf8ByteCount) { - if (start > s.Length - 1 || length > s.Length || start + length > s.Length) + if (start < 0 || length < 0 || start + length > s.Length) { throw new ArgumentOutOfRangeException(); } From 817d35a42506ebc2edaf0b0d2cf701a7ddc1030e Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Sun, 21 Mar 2021 01:31:25 -0700 Subject: [PATCH 10/10] Fix Metal rendering. --- NuGet.Config | 6 ------ src/ImGui.NET.SampleProgram/ImGuiController.cs | 7 ++++--- 2 files changed, 4 insertions(+), 9 deletions(-) delete mode 100644 NuGet.Config diff --git a/NuGet.Config b/NuGet.Config deleted file mode 100644 index eba6f65..0000000 --- a/NuGet.Config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/src/ImGui.NET.SampleProgram/ImGuiController.cs b/src/ImGui.NET.SampleProgram/ImGuiController.cs index b636e28..4cbe615 100644 --- a/src/ImGui.NET.SampleProgram/ImGuiController.cs +++ b/src/ImGui.NET.SampleProgram/ImGuiController.cs @@ -99,8 +99,8 @@ namespace ImGuiNET byte[] vertexShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex); byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment); - _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, "main")); - _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, "main")); + _vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "VS" : "main")); + _fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "FS" : "main")); VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[] { @@ -123,7 +123,8 @@ namespace ImGuiNET PrimitiveTopology.TriangleList, new ShaderSetDescription(vertexLayouts, new[] { _vertexShader, _fragmentShader }), new ResourceLayout[] { _layout, _textureLayout }, - outputDescription); + outputDescription, + ResourceBindingModel.Default); _pipeline = factory.CreateGraphicsPipeline(ref pd); _mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout,