diff --git a/.gitmodules b/.gitmodules index bad792b..ed62a37 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "src/VulkanRenderer"] - path = src/VulkanRenderer - url = https://git.sleepy.day/sleepy-day/VulkanRenderer.git [submodule "src/dlib"] path = src/dlib url = https://git.sleepy.day/sleepy-day/dlib.git diff --git a/assets/gui.frag.spv b/assets/gui.frag.spv index 4c7e572..4ddc450 100644 Binary files a/assets/gui.frag.spv and b/assets/gui.frag.spv differ diff --git a/assets/gui.vert.spv b/assets/gui.vert.spv index 45ce2e3..77ac490 100644 Binary files a/assets/gui.vert.spv and b/assets/gui.vert.spv differ diff --git a/build.sh b/build.sh index a6981d5..ff3b5d0 100755 --- a/build.sh +++ b/build.sh @@ -15,6 +15,4 @@ for shader in src/shaders/*.glsl; do $shader_compiler $shader_flags $shader_out${base_name}.spv $shader done -/bin/bash src/VulkanRenderer/build.sh build - /bin/bash src/dlib/build.sh build diff --git a/dub.json b/dub.json index 5556082..987ce02 100644 --- a/dub.json +++ b/dub.json @@ -9,16 +9,16 @@ "targetPath": "build", "sourceFiles-linux": ["build/libvma.a", "build/libstb.a", "build/libm3d.a"], "sourceFiles-windows": [], - "importPaths": ["src/editor", "src/dlib", "src/dlib/external/xxhash", "src/VulkanRenderer"], - "sourcePaths": ["src/editor", "src/dlib", "src/dlib/external/xxhash", "src/VulkanRenderer"], + "importPaths": ["src/editor", "src/dlib/dlib", "src/dlib/vulkan"], + "sourcePaths": ["src/editor", "src/dlib/dlib", "src/dlib/vulkan"], "libs-linux": ["X11", "vulkan", "stdc++", "xfixes", "freetype"], "libs-windows": [], "versions": ["VULKAN_DEBUG", "ENABLE_RENDERER", "DLIB"], "preGenerateCommands-linux": ["./build.sh"], "preGenerateCommands-windows": [], - "dflags": ["-P-I/usr/include/freetype2", "-Jbuild", "-Jassets", "-P-DDLIB_INCLUDE_VULKAN"], - "dflags-ldc2": ["-link-debuglib"], - "dflags-dmd": [] + "dflags": ["-P-I/usr/include/freetype2", "-Jbuild", "-Jassets", "-P-DBUILD_VULKAN"], + "dflags-ldc2": ["-link-debuglib", "--Xcc=-DBUILD_VULKAN"], + "dflags-dmd": ["-Xcc=-DBUILD_VULKAN"] }, { "name": "editor-test", @@ -27,16 +27,16 @@ "targetPath": "build", "sourceFiles-linux": ["build/libvma.a", "build/libstb.a", "build/libm3d.a", "build/libcglm.a"], "sourceFiles-windows": [], - "importPaths": ["src/editor", "src/dlib", "src/dlib/external/xxhash", "src/VulkanRenderer"], - "sourcePaths": ["src/editor", "src/dlib", "src/dlib/external/xxhash", "src/VulkanRenderer"], + "importPaths": ["src/editor", "src/dlib/dlib", "src/dlib/vulkan"], + "sourcePaths": ["src/editor", "src/dlib/dlib", "src/dlib/vulkan"], "libs-linux": ["X11", "vulkan", "stdc++", "xfixes", "freetype"], "libs-windows": [], "versions": ["VULKAN_DEBUG"], "preGenerateCommands-linux": ["./build.sh"], "preGenerateCommands-windows": [], - "dflags": ["-P-I/usr/include/freetype2", "-Jbuild", "-Jassets", "-unittest"], - "dflags-ldc2": ["-link-debuglib"], - "dflags-dmd": [] + "dflags": ["-P-I/usr/include/freetype2", "-Jbuild", "-Jassets", "-unittest", "-P-DBUILD_VULKAN"], + "dflags-ldc2": ["-link-debuglib", "--Xcc=-DBUILD_VULKAN"], + "dflags-dmd": ["-Xcc=-DBUILD_VULKAN"] } ] } diff --git a/src/VulkanRenderer b/src/VulkanRenderer deleted file mode 160000 index 245db6e..0000000 --- a/src/VulkanRenderer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 245db6ea6fe787e91839dd78f92dda007e234f7c diff --git a/src/dlib b/src/dlib index 6ae59da..53eb211 160000 --- a/src/dlib +++ b/src/dlib @@ -1 +1 @@ -Subproject commit 6ae59dab5bca9d44fdf4a7f517654b1c0f8545f1 +Subproject commit 53eb211a1e227d1cf3d8036a0e8ecfb79f514eed diff --git a/src/editor/editor.d b/src/editor/editor.d index ae84e39..b805c4e 100644 --- a/src/editor/editor.d +++ b/src/editor/editor.d @@ -24,7 +24,6 @@ struct RenderCtx Descriptor[FS] font_descs; Descriptor default_tex; Descriptor sampler; - Descriptor msdf_texture; Pipeline pipeline; Pipeline slug_pipeline; DescSetLayout desc_set_layout; diff --git a/src/editor/main.d b/src/editor/main.d index 747bb47..824a928 100644 --- a/src/editor/main.d +++ b/src/editor/main.d @@ -48,7 +48,6 @@ void main(string[] argv) SetExtent(window.w, window.h); g_ctx.pc.resolution = current_res; Ortho(&g_ctx.pc.projection, 0.0, 0.0, current_res.x, current_res.y, -10.0, 10.0); - UpdateMSDFShaderInfo(&g_ctx.pc, g_ctx.pc.resolution); } if(inputs.first == null) diff --git a/src/editor/parsing.d b/src/editor/parsing.d index f2845eb..5e2c993 100644 --- a/src/editor/parsing.d +++ b/src/editor/parsing.d @@ -661,7 +661,8 @@ TokenizeD(FlatBuffer* fb) } } - t.end = tk.pos; + t.end = tk.pos; + tk.pos += 1; } break; } } diff --git a/src/editor/ui.d b/src/editor/ui.d index abf34c0..4b13d92 100644 --- a/src/editor/ui.d +++ b/src/editor/ui.d @@ -293,19 +293,6 @@ struct UIPanel alias item this; } -void -UpdateMSDFShaderInfo(PushConst* pc, Vec2 resolution) -{ - const f32 AA_PIXEL_SIZE = 1.0; - - f32 aa_in_em = AA_PIXEL_SIZE * resolution.x / MSDF_INFO.pixels_per_em; - - pc.antialias_per_em = 1.0 / aa_in_em; - pc.aem_range = MSDF_INFO.aem_range; - pc.threshold_em = 0.0; - pc.distance_range = MSDF_INFO.distance_field_range; -} - struct Window { Window* next, prev; @@ -396,184 +383,6 @@ struct UIKey */ -enum MSDFType -{ - None, - HardMask, - SoftMask, - SDF, - PSDF, - MSDF, - MTSDF, -} - -enum MSDFYOrigin -{ - None, - Left, - Bottom, - Right, - Top, -} - -struct MSDFInfo -{ - MSDFGlyph[] glyphs; - MSDFType type; - Vec2 aem_range; - u32 distance_field_range; - u32 distance_field_range_middle; - f32 pixels_per_em; - u32 texture_width; - u32 texture_height; - MSDFYOrigin y_origin; - f32 line_height; // EM - f32 ascender; // EM - f32 descender; // EM - f32 underline_y; // EM - f32 underline_thickness; // EM -} - -struct MSDFBounds -{ - f32 left = 0.0, bottom = 0.0, right = 0.0, top = 0.0; -} - -struct MSDFGlyph -{ - dchar glyph = 0; - f32 advance = 0.0; - MSDFBounds plane; // EM - MSDFBounds atlas; // Pixels -} - -const u8[] MSDF_ATLAS = cast(u8[])import("msdf-atlas.rgba"); - -const MSDFInfo MSDF_INFO = GenerateMSDF(); - -static MSDFInfo -GenerateMSDF() -{ - import std.json; - - string msdf_json = import("msdf.json"); - - JSONValue jsonv = parseJSON(msdf_json); - - static T GetJSONValue(T)(JSONValue jsonv, string name) - { - static if(is(T == string)) - { - T result = ""; - } - else - { - T result = cast(T)0; - } - - if(const(JSONValue)* value = name in jsonv) - { - switch(value.type()) - { - static if(is(T == u32) || is(T == u64) || is(T == f32)) - { - case JSONType.float_: result = cast(T)value.floating; break; - case JSONType.uinteger: result = cast(T)value.uinteger; break; - case JSONType.integer: result = cast(T)value.integer; break; - } - static if(is(T == string)) - { - case JSONType.string: result = cast(T)value.str; break; - } - default: assert(false, "Unable to retrieve value"); - } - } - - return result; - } - - MSDFInfo result = { - distance_field_range: GetJSONValue!(u32)(jsonv["atlas"], "distanceRange"), - distance_field_range_middle: GetJSONValue!(u32)(jsonv["atlas"], "distanceRangeMiddle"), - pixels_per_em: GetJSONValue!(f32)(jsonv["atlas"], "size"), - texture_width: GetJSONValue!(u32)(jsonv["atlas"], "width"), - texture_height: GetJSONValue!(u32)(jsonv["atlas"], "height"), - line_height: GetJSONValue!(f32)(jsonv["metrics"], "lineHeight"), - ascender: GetJSONValue!(f32)(jsonv["metrics"], "ascender"), - descender: GetJSONValue!(f32)(jsonv["metrics"], "descender"), - underline_y: GetJSONValue!(f32)(jsonv["metrics"], "underlineY"), - underline_thickness: GetJSONValue!(f32)(jsonv["metrics"], "underlineThickness"), - }; - - result.aem_range = Vec2( - (result.distance_field_range_middle - result.distance_field_range/2) / result.pixels_per_em, - (result.distance_field_range_middle + result.distance_field_range/2) / result.pixels_per_em - ); - - string type_string = GetJSONValue!(string)(jsonv["atlas"], "type"); - string y_origin_string = GetJSONValue!(string)(jsonv["atlas"], "yOrigin"); - - assert(type_string.length); - assert(y_origin_string.length); - - final switch(type_string) with(MSDFType) - { - case "hardmask": result.type = HardMask; break; - case "softmask": result.type = SoftMask; break; - case "sdf": result.type = SDF; break; - case "psdf": result.type = PSDF; break; - case "msdf": result.type = MSDF; break; - case "mtsdf": result.type = MTSDF; break; - } - - final switch(y_origin_string) with(MSDFYOrigin) - { - case "top": result.y_origin = Top; break; - case "left": result.y_origin = Left; break; - case "right": result.y_origin = Right; break; - case "bottom": result.y_origin = Bottom; break; - } - - MSDFGlyph[] glyphs = new MSDFGlyph[128]; - - JSONValue[] glyph_json = jsonv["glyphs"].array; - - foreach(i; 0 .. glyph_json.length) - { - u64 ch = cast(u64)glyph_json[i]["unicode"].integer; - MSDFGlyph glyph = { glyph: cast(dchar)ch }; - glyph.advance = GetJSONValue!(f32)(glyph_json[i], "advance"); - if("planeBounds" in glyph_json[i]) - { - glyph.plane.left = GetJSONValue!(f32)(glyph_json[i]["planeBounds"], "left"); - glyph.plane.right = GetJSONValue!(f32)(glyph_json[i]["planeBounds"], "right"); - glyph.plane.bottom = GetJSONValue!(f32)(glyph_json[i]["planeBounds"], "bottom"); - glyph.plane.top = GetJSONValue!(f32)(glyph_json[i]["planeBounds"], "top"); - - glyph.atlas.left = GetJSONValue!(f32)(glyph_json[i]["atlasBounds"], "left"); - glyph.atlas.right = GetJSONValue!(f32)(glyph_json[i]["atlasBounds"], "right"); - glyph.atlas.bottom = GetJSONValue!(f32)(glyph_json[i]["atlasBounds"], "bottom"); - glyph.atlas.top = GetJSONValue!(f32)(glyph_json[i]["atlasBounds"], "top"); - } - - glyphs[ch] = glyph; - } - - foreach(i; 0 .. glyphs.length) - { - if(glyphs[i].glyph == 0xFFFF) - { - glyphs[i].glyph = 0; - glyphs[i].plane = MSDFBounds(left: 0.0, right: 0.0, top: 0.0, bottom: 0.0); - glyphs[i].atlas = MSDFBounds(left: 0.0, right: 0.0, top: 0.0, bottom: 0.0); - } - } - - result.glyphs = glyphs; - - return result; -} - enum bool KeyType(T) = (StringType!(T) || is(T == UIKey) || is(T == const(UIKey))); enum bool ItemAndKeyType(T) = (KeyType!(T) || is(T == UIItem*)); @@ -611,12 +420,17 @@ InitUI(Ctx* ctx) version(ENABLE_RENDERER) { - InitRenderer(handles, MB(16), MB(8)); + VulkanBuildInfo build_info = { + platform_handles: handles, + application_name: "ScratchPad", + engine_name: "ScratchPad", + }; - DescLayoutBinding[3] layout_bindings = [ + InitRenderer(build_info, MB(16), MB(8)); + + DescLayoutBinding[2] layout_bindings = [ { binding: 0, descriptorType: DT.Image, descriptorCount: FONT_SIZES, stageFlags: SS.All }, { binding: 1, descriptorType: DT.Sampler, descriptorCount: 1, stageFlags: SS.All }, - { binding: 2, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All }, ]; ctx.desc_set_layout = CreateDescSetLayout(layout_bindings); @@ -655,20 +469,6 @@ InitUI(Ctx* ctx) CreateGraphicsPipeline(&ctx.pipeline, &ui_info); - CreateDescriptor( - &ctx.msdf_texture, - DescInfo( - type: DT.Image, - format: FMT.RGBA_UNORM, - usage: IU.Texture, - w: MSDF_INFO.texture_width, - h: MSDF_INFO.texture_height, - ch: 4, - binding: 2, - ), - cast(u8[])MSDF_ATLAS[12 .. $] - ); - for(u32 i = 0; i < ctx.font_descs.length; i += 1) { CreateDescriptor(&ctx.font_descs[i], DescInfo( @@ -704,7 +504,6 @@ InitUI(Ctx* ctx) { Write(ctx.desc_sets[i], ctx.font_descs); Write(ctx.desc_sets[i], &ctx.sampler); - Write(ctx.desc_sets[i], &ctx.msdf_texture); } SetClearColors([0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0]); @@ -2204,6 +2003,8 @@ DrawText(T, U, V)(Ctx* ctx, T text_param, U col_param, V text_size_param, Rect r AnimateReady(ready, &col); DrawGlyph(rect, g, &x, rect.p0.y, fs.line_height, fs.size, col); + + if(x > rect.p1.x) break; } } else static if(is(U == Vec4)) @@ -2216,6 +2017,8 @@ DrawText(T, U, V)(Ctx* ctx, T text_param, U col_param, V text_size_param, Rect r u8 ch = text[j]; Glyph* g = ch < fs.glyphs.length ? fs.glyphs.ptr + ch : null; DrawGlyph(rect, g, &x, rect.p0.y, fs.line_height, fs.size, hl_char == j ? InvertCol(col) : col); + + if(x > rect.p1.x) break; } } else static assert(false, "invalid input parameter, U must be TokenStyle[] or Vec4"); @@ -2546,8 +2349,6 @@ DrawGlyph(Rect rect, Glyph* glyph, f32* x_pos, f32 y, f32 line_height, u32 size, { if(glyph) { - const(MSDFGlyph)* msdf_glyph = &MSDF_INFO.glyphs[glyph.ch]; - Ctx* ctx = GetCtx(); Vertex* v = null; f32 advance = glyph.advance; @@ -2562,19 +2363,20 @@ DrawGlyph(Rect rect, Glyph* glyph, f32* x_pos, f32 y, f32 line_height, u32 size, } else { - f32 w = glyph.plane_right-glyph.plane_left; - f32 h = glyph.plane_bottom-glyph.plane_top; + f32 w = glyph.plane.right-glyph.plane.left; + f32 h = glyph.plane.bottom-glyph.plane.top; + v = GetVertex(ctx); - f32 y_pos = y + glyph.plane_top; + f32 y_pos = y + glyph.plane.top; // Need to change how the quads are calculated, i had changed it so they were all same height and same top/bottom placement as it was easier to draw rasterized glyphs that way but doesn't work when the SDFs are not generated in a similar way - v.dst_start = Vec2(*x_pos+glyph.plane_left, y_pos); - v.dst_end = Vec2(*x_pos+glyph.plane_left+w, y_pos+h); + v.dst_start = Vec2(*x_pos+glyph.plane.left, y_pos); + v.dst_end = Vec2(*x_pos+glyph.plane.left+w, y_pos+h); v.cols = col; - v.src_start = Vec2(glyph.atlas_left, glyph.atlas_top); // Vec2(msdf_glyph.atlas.left, msdf_glyph.atlas.top); - v.src_end = Vec2(glyph.atlas_right, glyph.atlas_bottom); // Vec2(msdf_glyph.atlas.right, msdf_glyph.atlas.bottom); + v.src_start = Vec2(glyph.atlas.left, glyph.atlas.top); + v.src_end = Vec2(glyph.atlas.right, glyph.atlas.bottom); v.has_texture = true; f32 end_x = *x_pos + advance; diff --git a/src/shaders/gui.frag.glsl b/src/shaders/gui.frag.glsl index 5d6249a..248f36c 100644 --- a/src/shaders/gui.frag.glsl +++ b/src/shaders/gui.frag.glsl @@ -65,20 +65,6 @@ Median(vec3 v) return max(min(v.r, v.g), min(max(v.r, v.g), v.b)); } -float -GetMSDFTexel() -{ - vec2 dx = 0.25 * dFdx(FD.uv); - vec2 dy = 0.25 * dFdy(FD.uv); - float center = Median(texture(MSDFAtlas, FD.uv).rgb); - float s0 = Median(texture(MSDFAtlas, FD.uv - dx - dy).rgb); - float s1 = Median(texture(MSDFAtlas, FD.uv + dx - dy).rgb); - float s2 = Median(texture(MSDFAtlas, FD.uv - dx + dy).rgb); - float s3 = Median(texture(MSDFAtlas, FD.uv + dx + dy).rgb); - - return mix(center, 0.25 * (s0+s1+s2+s3), 0.0); -} - vec2 Square(vec2 x) { @@ -126,17 +112,7 @@ void main() vec4 tex_color = vec4(1.0); // ToLinear(FD.color); if(FDF.has_texture != 0) { - // old: tex_color = texture(SpriteAtlas, FD.uv); - - /* - float sd = Median(texture(MSDFAtlas, FD.uv).rgb); - float px_range = ScreenPxRange(); - float screen_px_dist = ScreenPxRange() * (sd-0.5); - float opacity = clamp(0.5 + screen_px_dist/fwidth(screen_px_dist), 0.0, 1.0); - - color.a *= opacity; - */ } vec4 color = FD.color * tex_color; diff --git a/src/shaders/gui.layout b/src/shaders/gui.layout index aaa3fbf..7fe78ff 100644 --- a/src/shaders/gui.layout +++ b/src/shaders/gui.layout @@ -4,7 +4,6 @@ layout(rgba16f, set = 0, binding = 0) uniform image2D DrawImage; layout(set = 1, binding = 0) uniform texture2D SpriteAtlasArray[24]; layout(set = 1, binding = 1) uniform sampler SamplerNearest; -layout(set = 1, binding = 2) uniform texture2D MSDFAtlasTexture; layout (push_constant) uniform Constants { mat4 projection; @@ -17,7 +16,6 @@ layout (push_constant) uniform Constants { } PC; #define SpriteAtlas sampler2D(SpriteAtlasArray[PC.atlas_index], SamplerNearest) -#define MSDFAtlas sampler2D(MSDFAtlasTexture, SamplerNearest) #ifdef VERT_SHADER # define FragData out struct FragDataOut