From 82faf448f4469206faa4ee377d4ec2bff6d26531 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 8 Jun 2022 02:10:58 +0200 Subject: [PATCH] working on some hacky randering --- .../src/mm/services/vulkan_renderer.cpp | 72 ++++++++++ .../src/mm/services/vulkan_renderer.hpp | 14 ++ .../src/mm/vulkan/res/tut1.frag | 10 ++ .../src/mm/vulkan/res/tut1.frag.spv | Bin 0 -> 500 bytes .../src/mm/vulkan/res/tut1.frag.spv.h | 45 +++++++ .../src/mm/vulkan/res/tut1.vert | 21 +++ .../src/mm/vulkan/res/tut1.vert.spv | Bin 0 -> 1432 bytes .../src/mm/vulkan/res/tut1.vert.spv.h | 123 ++++++++++++++++++ .../vulkan_renderer/test/vulkan_test.cpp | 2 +- 9 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv.h create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert.spv create mode 100644 framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert.spv.h diff --git a/framework/vulkan_renderer/src/mm/services/vulkan_renderer.cpp b/framework/vulkan_renderer/src/mm/services/vulkan_renderer.cpp index 21c0480..fe19066 100644 --- a/framework/vulkan_renderer/src/mm/services/vulkan_renderer.cpp +++ b/framework/vulkan_renderer/src/mm/services/vulkan_renderer.cpp @@ -14,8 +14,20 @@ #include +// HACK +namespace { // dont leak linkage +#include +#include + +// meh, we dont have the type, only the value +//static_assert(alignof(tut1_vert_spv) == alignof(uint32_t)); +//static_assert(alignof(tut1_frag_spv) == alignof(uint32_t)); +} + #include +// https://youtu.be/eaKeeRngZBo + // this needs to be defined only once VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE @@ -219,8 +231,17 @@ void VulkanRenderer::disable(Engine&) { } }; + auto device_destroy_each_kv = [&device](auto& map) { + for (const auto& it : map) { + device.destroy(it.second); + } + }; + device.waitIdle(); + // resouce cache + device_destroy_each_kv(_r_shader_module); + device_destroy_each(_swapchain_framebuffers); device_destroy_each(_swapchain_image_views); device.destroy(_swapchain); @@ -459,6 +480,57 @@ bool VulkanRenderer::createSwapchain(Engine& engine) { // TODO: move + // TODO: refactor, provide "primitive" wrapper like opengl + auto create_shader_module = [&](const uint8_t* data, const size_t size) -> vk::ShaderModule { + return device.createShaderModule(vk::ShaderModuleCreateInfo{ + {}, + size, + reinterpret_cast(data) + }); + }; + + using namespace entt::literals; + + auto vert_module = create_shader_module(tut1_vert_spv, tut1_vert_spv_len); + _r_shader_module["tut1_vert"_hs] = vert_module; + auto frag_module = create_shader_module(tut1_frag_spv, tut1_frag_spv_len); + _r_shader_module["tut1_frag"_hs] = frag_module; + + std::vector pl_shader_ci { + { + {}, + vk::ShaderStageFlagBits::eVertex, + vert_module, + "main", + }, + { + {}, + vk::ShaderStageFlagBits::eFragment, + frag_module, + "main", + } + }; + + std::vector pl_vertex_input_ci { + // hardcoded in shader, so no actual data here + {}, + {}, // do i need two? + }; + + vk::PipelineInputAssemblyStateCreateInfo pl_input_asm_ci { + {}, + vk::PrimitiveTopology::eTriangleList, + VK_FALSE + }; + + device.createGraphicsPipeline({}, { + {}, + static_cast(pl_shader_ci.size()), + pl_shader_ci.data(), + pl_vertex_input_ci.data(), + &pl_input_asm_ci, + }); + _swapchain_framebuffers.clear(); for (const auto& img_view : _swapchain_image_views) { vk::ImageView tmp_img_view = img_view; diff --git a/framework/vulkan_renderer/src/mm/services/vulkan_renderer.hpp b/framework/vulkan_renderer/src/mm/services/vulkan_renderer.hpp index de436d7..964e08d 100644 --- a/framework/vulkan_renderer/src/mm/services/vulkan_renderer.hpp +++ b/framework/vulkan_renderer/src/mm/services/vulkan_renderer.hpp @@ -1,7 +1,9 @@ #pragma once +#include "entt/core/hashed_string.hpp" #include #include +#include // fwd vk stuff @@ -25,6 +27,7 @@ MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSemaphore) MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkFence) MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkImageView) MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkFramebuffer) +MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkShaderModule) // extensions MM_VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkDebugUtilsMessengerEXT) @@ -57,6 +60,9 @@ class VulkanRenderer : public Service { std::vector _swapchain_sem_render_finished{}; std::vector _swapchain_fence_in_flight{}; + // resource cache + std::unordered_map _r_shader_module{}; + public: VulkanRenderer(void); ~VulkanRenderer(void); @@ -73,6 +79,14 @@ class VulkanRenderer : public Service { bool createDevice(Engine& engine); bool createSwapchain(Engine& engine); + + //VkShaderModule getR_shader_module(const entt::hashed_string::value_type key) { + //return _r_shader_module.at(key); + //} + + //VkShaderModule getR_shader_module(const char* key) { + //return getR_shader_module(entt::hashed_string::value(key)); + //} }; } // MM::Services diff --git a/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag new file mode 100644 index 0000000..76bdf57 --- /dev/null +++ b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag @@ -0,0 +1,10 @@ +#version 450 + +layout(location = 0) in vec3 fragColor; + +layout(location = 0) out vec4 outColor; + +void main() { + outColor = vec4(fragColor, 1.0); +} + diff --git a/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv new file mode 100644 index 0000000000000000000000000000000000000000..a94fd3903554fa866205c4e84b81431fe7592a5c GIT binary patch literal 500 zcmYk1y-EX75QQhZZZ;;G9|PJ+#8L<|pCBNLpf1GN`)odyjo|lPcg4G$ zotbmb%$b{Khh^lg=td{T@q0#*MT@i-?G)#WyT$ZveLp=pJyy|=JPFkdqKplqcv(KZ zda((%z!05$339%S_(@X%`(Izc*i*f)udC;3MG8}-84s)FV?)mAQn#;wW35k9k8#lB zAh|`#cgfpu6R?NgU99z=@b3_Pjr@6C>mjGUOH^;IzDMKx@3->|*=?*RMNd+#2XxcVONzS;5& F`~b~_7{>qr literal 0 HcmV?d00001 diff --git a/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv.h b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv.h new file mode 100644 index 0000000..800a5a5 --- /dev/null +++ b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.frag.spv.h @@ -0,0 +1,45 @@ +alignas(uint32_t) unsigned char tut1_frag_spv[] = { + 0x03, 0x02, 0x23, 0x07, 0x00, 0x03, 0x01, 0x00, 0x0a, 0x00, 0x08, 0x00, + 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x07, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x03, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, + 0x02, 0x00, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00, 0x6f, 0x75, 0x74, 0x43, + 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x66, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, + 0x72, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x03, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, + 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, + 0x05, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, + 0x0d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x07, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x3e, 0x00, 0x03, 0x00, 0x09, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, + 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 +}; +unsigned int tut1_frag_spv_len = 500; diff --git a/framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert new file mode 100644 index 0000000..f8a9a20 --- /dev/null +++ b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert @@ -0,0 +1,21 @@ +#version 450 + +layout(location = 0) out vec3 fragColor; + +vec2 positions[3] = vec2[]( + vec2(0.0, -0.5), + vec2(0.5, 0.5), + vec2(-0.5, 0.5) +); + +vec3 colors[3] = vec3[]( + vec3(1.0, 0.0, 0.0), + vec3(0.0, 1.0, 0.0), + vec3(0.0, 0.0, 1.0) +); + +void main() { + gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); + fragColor = colors[gl_VertexIndex]; +} + diff --git a/framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert.spv b/framework/vulkan_renderer/src/mm/vulkan/res/tut1.vert.spv new file mode 100644 index 0000000000000000000000000000000000000000..3f0f5fede87026dfff8db585ddc5e2c8762da125 GIT binary patch literal 1432 zcmYk5U29W85QaC2C(%}0Yi;XCt*0N>kJebVErO^>g(&o*Qt-BfHkJc1DMo^#HIY#3(Vci!2Z**&RM*BhZ$2~**6*bl{;3)N5|oDTKew+@dFo5Rsrb9Zk? z#Z0K>LN&A0xh`Sf+F8$d4co+?V>NzN{2#;#s!3e}bCujhf0&K3es4&uW(j}V@Ad}} z2|c9X^X|z}XYi#n7c`p?cQl; zoO3dB-@Dy$E@1{|a@miwp(&dA)H|~yXK~)IgZBA>y%T?gOYyGh!i_omlha3?eaYEF zDt=I7> zR>3?+yn(r_e}Fe9Z@qZ4+{C-Ly@+>8>|I+g_WM+0{w3ae^ZR@YeuK{J zQu7xk!M_FhP5HLQzK!?x2DQCC#O&)mYI}N&sh3w{fBrw@Kcn%?t&+oyx$Tm3<|C?{ y<0+=robT-0d5*I_!@N)L%)Z3xV_z?bV_(j?i`l!p8t2}_(); engine.disableService();