diff --git a/assets/shaders/gui.vert.spv b/assets/shaders/gui.vert.spv index df1c78b..1c92e68 100644 Binary files a/assets/shaders/gui.vert.spv and b/assets/shaders/gui.vert.spv differ diff --git a/dub.json b/dub.json index 593d0a8..467e59a 100644 --- a/dub.json +++ b/dub.json @@ -11,7 +11,7 @@ "sourceFiles-windows": [], "importPaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"], "sourcePaths": ["src/gears", "src/shared", "src/generated", "external/xxhash", "external/inteli"], - "libs-linux": ["xcb", "X11", "X11-xcb", "vulkan", "stdc++", "xcb-xfixes"], + "libs-linux": ["xcb", "X11", "X11-xcb", "vulkan", "stdc++", "xcb-xfixes", "freetype2"], "libs-windows": [], "preGenerateCommands-linux": ["./build.sh", "build/Packer"], "preGenerateCommands-windows": [], @@ -31,18 +31,5 @@ "preGenerateCommands-windows": [], "dflags-dmd": ["-P=-DSTBI_NO_SIMD"], }, - { - "name": "codegen", - "targetType": "executable", - "targetPath": "build", - "targetName": "Codegen", - "importPaths": ["src/codegen", "src/shared", "external/xxhash", "external/inteli"], - "sourcePaths": ["src/codegen", "src/shared", "external/xxhash", "external/inteli"], - "sourceFiles-linux": ["build/libstb_image.a"], - "preGenerateCommands-linux": ["./build.sh"], - "preGenerateCommands-windows": [], - "versions": ["codegen"], - "dflags-dmd": ["-P=-DSTBI_NO_SIMD"], - } ] } diff --git a/src/codegen/codegen.d b/src/codegen/codegen.d deleted file mode 100644 index 95e26aa..0000000 --- a/src/codegen/codegen.d +++ /dev/null @@ -1,107 +0,0 @@ -import aliases; -import util; -import std.stdio; -import std.file; -import std.string; -import std.path; -import std.uni; -import std.conv; -import std.array; - -void -main() -{ - CreateAssetLookups(); -} - -void -CreateAssetLookups() -{ - try - { - mkdir("src/generated"); - } - catch(Exception e) {} - - string[] dirs; - string[][string] file_names; - string[][string] base_names; - u64[][string] file_hashes; - - foreach(string dir; dirEntries("assets", SpanMode.shallow)) - { - dirs ~= baseName(dir); - } - - foreach(dir; dirs) - { - string[] files; - string[] names; - foreach(string file; dirEntries("assets/" ~ dir, SpanMode.shallow)) - { - files ~= dir ~ "/" ~ baseName(file); - names ~= dir ~ "/" ~ baseName(stripExtension(file)); - } - - file_names[dir] = files; - base_names[dir] = names; - } - - foreach(dir, names; base_names) - { - u64[] hashes; - foreach(name; names) - { - hashes ~= Hash(name); - } - - file_hashes[dir] = hashes; - } - - auto f = File("src/generated/assets_codegen.d", "w"); - scope(exit) f.close(); - - string asset_str = "import aliases;\n\n"; - - asset_str ~= "enum AssetType : u32\n{\n\tNone,\n"; - foreach(i, dir; dirs) - { - auto graphemes = dir.byGrapheme.array; - string upper = [graphemes[0]].byCodePoint.text.toUpper; - graphemes[0] = upper.decodeGrapheme; - - string asset_type = graphemes.byCodePoint.text.stripRight("s"); - asset_str ~= format("\t%s,\n", asset_type); - } - asset_str ~= "}\n\nalias AT = AssetType;\n\n"; - - foreach(dir; dirs) - { - string asset_type = stripRight(dir, "s").toUpper(); - - asset_str ~= format("static immutable string[] %s_FILES = [\n", asset_type); - foreach(file; file_names[dir]) - { - asset_str ~= format("\t\"%s\",\n", file); - } - asset_str ~= "];\n\n"; - - - asset_str ~= format("static immutable string[] %s_NAMES = [\n", asset_type); - foreach(name; base_names[dir]) - { - asset_str ~= format("\t\"%s\",\n", name); - } - asset_str ~= "];\n\n"; - - asset_str ~= format("static immutable u64[] %s_HASHES = [\n", asset_type); - foreach(hash; file_hashes[dir]) - { - asset_str ~= format("\t%d,\n", hash); - } - asset_str ~= "];\n\n"; - - } - - f.write(asset_str); -} diff --git a/src/gears/game.d b/src/gears/game.d index 9dfd801..adfaf40 100644 --- a/src/gears/game.d +++ b/src/gears/game.d @@ -76,9 +76,9 @@ struct Game u32[] ui_index_buf; u32 ui_count; - ImageView draw_image, depth_image; + ImageView default_tex; - GlobalUniforms globals; + UIPushConst ui_pc; Timer timer; } @@ -92,11 +92,6 @@ InitGame(PlatformWindow* window) frame_arena: CreateArena(MB(16)), window: window, timer: CreateTimer(), - globals: { - light_direction: Vec3(-0.57735), - light_color: Vec4(0.8, 0.8, 0.8, 1.0), - ambient_color: Vec4(0.7, 0.7, 0.7, 1.0), - }, }; UVec2 ext = GetExtent(&g.rd); @@ -109,6 +104,12 @@ InitGame(PlatformWindow* window) g.ui_desc_set = AllocDescSet(&g.rd, g.ui_desc_layout); g.ui_layout = CreatePipelineLayout(&g.rd, g.ui_desc_layout, UIPushConst.sizeof); + u8[16*16*4] white_tex; + white_tex[] = u8.max; + + CreateImageView(&g.rd, &g.default_tex, 16, 16, 4, white_tex); + WriteGUI(&g.rd, g.ui_desc_set, &g.default_tex); + GfxPipelineInfo ui_info = { vertex_shader: "shaders/gui.vert.spv", frag_shader: "shaders/gui.frag.spv", @@ -129,6 +130,11 @@ InitGame(PlatformWindow* window) PrintShaderDisassembly(&g.rd, g.ui_pipeline, VK_SHADER_STAGE_VERTEX_BIT); PrintShaderDisassembly(&g.rd, g.ui_pipeline, VK_SHADER_STAGE_FRAGMENT_BIT); + g.ui_vertex_buf = GetUIVertexBuffer(&g.rd); + g.ui_index_buf = GetUIIndexBuffer(&g.rd); + + WaitForTransfers(&g.rd); + Reset(&g.frame_arena); return g; @@ -155,37 +161,30 @@ Cycle(Game* g) { g_DELTA = DeltaTime(&g.timer); + g.ui_count = 0; + Reset(&g.frame_arena); - //ProcessInputs(g, &g.camera); - - //Sort(g, g.camera.pos, &g.model); - - //Update(g, &g.camera); - - //Update(&g.camera); + DrawRect(g, 200.0, 200.0, 800.0, 400.0, Vec4(0.2, 0.3, 0.7, 1.0)); BeginFrame(&g.rd); UVec2 ext = GetExtent(&g.rd); - f32 aspect = (cast(f32)ext.x) / (cast(f32)ext.y); - Mat4 projection = Perspective(90.0, aspect, 10000.0, 0.1); - + g.ui_pc.res.x = cast(f32)ext.x; + g.ui_pc.res.y = cast(f32)ext.y; + BeginRendering(&g.rd); + PushConstants(&g.rd, g.ui_pipeline, &g.ui_pc); + Bind(&g.rd, g.ui_pipeline, g.ui_desc_set); BindUIBuffers(&g.rd); + DrawIndexed(&g.rd, 6, g.ui_count, 0); + FinishRendering(&g.rd); - //ImageBarrier(&g.rd); - - //BeginRendering(&g.rd, &g.draw_image, &g.depth_image); - - - //FinishRendering(&g.rd); - SubmitAndPresent(&g.rd); } @@ -322,8 +321,8 @@ DrawRect(Game* g, f32 p0_x, f32 p0_y, f32 p1_x, f32 p1_y, Vec4 col) g.ui_index_buf[index_count+0] = index_count+0; g.ui_index_buf[index_count+1] = index_count+1; g.ui_index_buf[index_count+2] = index_count+2; - g.ui_index_buf[index_count+3] = index_count+1; - g.ui_index_buf[index_count+4] = index_count+2; + g.ui_index_buf[index_count+3] = index_count+2; + g.ui_index_buf[index_count+4] = index_count+1; g.ui_index_buf[index_count+5] = index_count+3; g.ui_count += 1; diff --git a/src/gears/vulkan.d b/src/gears/vulkan.d index 1735128..a55ee67 100644 --- a/src/gears/vulkan.d +++ b/src/gears/vulkan.d @@ -25,6 +25,7 @@ alias DescSet = VkDescriptorSet; alias DescSetLayout = VkDescriptorSetLayout; alias PipelineLayout = VkPipelineLayout; alias DescLayoutBinding = VkDescriptorSetLayoutBinding; +alias DescWrite = VkWriteDescriptorSet; bool g_VLAYER_SUPPORT = false; bool g_DEBUG_PRINTF = false; @@ -955,6 +956,12 @@ TransferAssets(Vulkan* vk) return true; } +void +WaitForTransfers(Vulkan* vk) +{ + vkWaitForFences(vk.device, 1, &vk.imm_fence, VK_TRUE, u64.max); +} + pragma(inline): void CreateImageView(Vulkan* vk, ImageView* view, u32 w, u32 h, u32 ch, u8[] data) { @@ -1142,12 +1149,18 @@ CreateImageView(Vulkan* vk, ImageView* view, u32 w, u32 h, Format format, ImageU } void -PushConstants(T)(Vulkan* vk, PipelineLayout layout, T* pc) +PushConstants(T)(Vulkan* vk, Pipeline pipeline_id, T* pc) { + assert(pipeline_id > 0, "PushConstants pipeline_id == 0"); + PipelineHandles* pipeline = vk.pipeline_handles.ptr + pipeline_id; + VkShaderStageFlags stage = (pipeline.type == VK_PIPELINE_BIND_POINT_GRAPHICS ? + VK_SHADER_STAGE_VERTEX_BIT|VK_SHADER_STAGE_FRAGMENT_BIT : + VK_SHADER_STAGE_COMPUTE_BIT); + vkCmdPushConstants( vk.cmds[vk.frame_index], - layout, - VK_SHADER_STAGE_VERTEX_BIT|VK_SHADER_STAGE_FRAGMENT_BIT|VK_SHADER_STAGE_COMPUTE_BIT, + pipeline.layout, + stage, 0, T.sizeof, pc @@ -2079,6 +2092,28 @@ InitGlobalDescSet(Vulkan* vk) return success; } +void +WriteGUI(Vulkan* vk, DescSet set, ImageView* atlas) +{ + VkDescriptorImageInfo image_info = { + imageView: atlas.view, + imageLayout: atlas.layout, + }; + + VkWriteDescriptorSet[] writes = [ + { + sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + dstSet: set, + dstBinding: 0, + descriptorCount: 1, + descriptorType: VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + pImageInfo: &image_info, + } + ]; + + vkUpdateDescriptorSets(vk.device, cast(u32)writes.length, writes.ptr, 0, null); +} + void WriteDrawImageDesc(Vulkan* vk) { diff --git a/src/generated/assets_codegen.d b/src/generated/assets_codegen.d deleted file mode 100644 index 6dc7c71..0000000 --- a/src/generated/assets_codegen.d +++ /dev/null @@ -1,101 +0,0 @@ -import aliases; - -static immutable string[] MODEL_FILES = [ - "models/cube.m3d", - "models/test_char.m3d", - "models/yoda.m3d", -]; - -static immutable string[] MODEL_NAMES = [ - "models/cube", - "models/test_char", - "models/yoda", -]; - -static immutable u64[] MODEL_HASHES = [ - 16487010975852376380, - 13826959199295087925, - 4559395153940738542, -]; - -static immutable string[] STYLIZEDNATURE_FILES = [ - "StylizedNature/Textures", - "StylizedNature/Nature", - "StylizedNature/Ruins", -]; - -static immutable string[] STYLIZEDNATURE_NAMES = [ - "StylizedNature/Textures", - "StylizedNature/Nature", - "StylizedNature/Ruins", -]; - -static immutable u64[] STYLIZEDNATURE_HASHES = [ - 13989528115033644773, - 6938651194392675090, - 4760645683512501027, -]; - -static immutable string[] SHADER_FILES = [ - "shaders/gui.frag.spv", - "shaders/triangle.vert.spv", - "shaders/pbr.frag.spv", - "shaders/gui.vert.spv", - "shaders/gradient.comp.spv", - "shaders/convert.comp.spv", - "shaders/triangle.frag.spv", - "shaders/pbr.vert.spv", -]; - -static immutable string[] SHADER_NAMES = [ - "shaders/gui.frag", - "shaders/triangle.vert", - "shaders/pbr.frag", - "shaders/gui.vert", - "shaders/gradient.comp", - "shaders/convert.comp", - "shaders/triangle.frag", - "shaders/pbr.vert", -]; - -static immutable u64[] SHADER_HASHES = [ - 15780387719315455808, - 8769314645675479020, - 2230071466542309169, - 14797956403837654625, - 16130483095684998267, - 3780699516113804562, - 6282520872716708711, - 8518761701216801634, -]; - -static immutable string[] TEXTURE_FILES = [ - "textures/ham_smoke.png", - "textures/cheesoid.png", - "textures/hog.jpg", - "textures/patamon.png", - "textures/pattermon.png", - "textures/hamster.png", - "textures/purplemon.png", -]; - -static immutable string[] TEXTURE_NAMES = [ - "textures/ham_smoke", - "textures/cheesoid", - "textures/hog", - "textures/patamon", - "textures/pattermon", - "textures/hamster", - "textures/purplemon", -]; - -static immutable u64[] TEXTURE_HASHES = [ - 15457434128510259736, - 12443444479937967236, - 5538438794723924882, - 16650761267170532297, - 11718504567089932798, - 7627422980398294448, - 14316598952102237724, -]; - diff --git a/src/shaders/gui.vert.glsl b/src/shaders/gui.vert.glsl index 5fe8e20..eac1cc1 100644 --- a/src/shaders/gui.vert.glsl +++ b/src/shaders/gui.vert.glsl @@ -43,7 +43,7 @@ void main() FragData.color = in_col; FragData.uv = vec2( in_src_start.x / tex_size.x, - in_src_start + in_src_start.y / tex_size.y ); gl_Position = vec4(2 * dst_pos.x / PC.res.x - 1, diff --git a/src/shared/includes.c b/src/shared/includes.c index 5350278..b4662be 100644 --- a/src/shared/includes.c +++ b/src/shared/includes.c @@ -6,6 +6,7 @@ # include # include # include +# include #endif #include