From 619f4a26416418e09a8682c4dd6d24a95c6b1564 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 24 Apr 2025 04:13:58 +0200 Subject: [PATCH] vectors are now passed to the gpu instead of being embedded to the shader --- main.cpp | 93 ++++++++++++++++++++++++++++++++++++++---- shaders/fragment.frag | 4 +- shaders/fragment.spv | Bin 572 -> 572 bytes shaders/vertex.spv | Bin 1504 -> 1084 bytes shaders/vertex.vert | 19 +++------ 5 files changed, 91 insertions(+), 25 deletions(-) diff --git a/main.cpp b/main.cpp index f81d160..d09a919 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,13 @@ #include #include #include +#include + +struct vertex +{ + glm::vec2 position; + glm::vec3 color; +}; std::vector read_file(const char *filename) { @@ -57,7 +64,9 @@ int main() extensions.push_back("VK_KHR_portability_enumeration"); extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); //Apple silicon extensions #endif - //layers.push_back("VK_LAYER_KHRONOS_validation"); + #ifndef NDEBUG + layers.push_back("VK_LAYER_KHRONOS_validation"); + #endif vk::InstanceCreateInfo createinfo = vk::InstanceCreateInfo( vk::InstanceCreateFlags(VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR), @@ -106,13 +115,15 @@ int main() float queue_priority = 1.0f; vk::DeviceQueueCreateInfo queue_info = vk::DeviceQueueCreateInfo(vk::DeviceQueueCreateFlags(), graphics_queue_index, 1, &queue_priority); + std::vector device_extensions; - uint32_t device_extension_count = 1; - char *device_extension_names[device_extension_count]; - device_extension_names[0] = (char *)"VK_KHR_swapchain"; + device_extensions.push_back("VK_KHR_swapchain"); + #ifdef __APPLE__ + device_extensions.push_back("VK_KHR_portability_subset"); + #endif vk::PhysicalDeviceFeatures device_features = vk::PhysicalDeviceFeatures(); - vk::DeviceCreateInfo device_info = vk::DeviceCreateInfo(vk::DeviceCreateFlags(), 1, &queue_info, 0, nullptr, 1, device_extension_names, &device_features); + vk::DeviceCreateInfo device_info = vk::DeviceCreateInfo(vk::DeviceCreateFlags(), 1, &queue_info, 0, nullptr, device_extensions.size(), device_extensions.data(), &device_features); vk::Device device = selected_physical_device.createDevice(device_info); @@ -225,7 +236,31 @@ int main() std::vector pipeline_shaders = {vertex_stage_info, fragment_stage_info}; + vk::VertexInputBindingDescription binding_description = {}; + binding_description.binding = 0; + binding_description.stride = sizeof(vertex); + binding_description.inputRate = vk::VertexInputRate::eVertex; + + vk::VertexInputAttributeDescription att_description_pos = {}; + att_description_pos.binding = 0; + att_description_pos.location = 0; + att_description_pos.format = vk::Format::eR32G32Sfloat; + att_description_pos.offset = offsetof(vertex, position); + + vk::VertexInputAttributeDescription att_description_color = {}; + att_description_color.binding = 0; + att_description_color.location = 1; + att_description_color.format = vk::Format::eR32G32B32Sfloat; + att_description_color.offset = offsetof(vertex, color); + + std::vector att_descriptions = {att_description_pos, att_description_color}; vk::PipelineVertexInputStateCreateInfo vertex_input_info = {}; + vertex_input_info.vertexAttributeDescriptionCount = 2; + vertex_input_info.vertexBindingDescriptionCount = 1; + vertex_input_info.pVertexBindingDescriptions = &binding_description; + vertex_input_info.pVertexAttributeDescriptions = att_descriptions.data(); + + vk::PipelineInputAssemblyStateCreateInfo input_assembly_info = vk::PipelineInputAssemblyStateCreateInfo(vk::PipelineInputAssemblyStateCreateFlags(), vk::PrimitiveTopology::eTriangleList, VK_FALSE); vk::Viewport viewport = vk::Viewport(0.0f, 0.0f, @@ -274,6 +309,7 @@ int main() vk::SubpassDependency subpass_dependency = {}; subpass_dependency.srcSubpass = VK_SUBPASS_EXTERNAL; subpass_dependency.dstSubpass = 0; + subpass_dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput; subpass_dependency.dstStageMask = vk::PipelineStageFlags(vk::PipelineStageFlagBits::eColorAttachmentOutput); subpass_dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; @@ -323,6 +359,41 @@ int main() framebuffers.push_back(device.createFramebuffer(framebuffer_info)); } + std::vector vertices = { + {{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}}, + {{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}}, + {{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}} + }; + + + vk::BufferCreateInfo buffer_info = vk::BufferCreateInfo(vk::BufferCreateFlags(), sizeof(vertices[0]) * vertices.size(), vk::BufferUsageFlagBits::eVertexBuffer, vk::SharingMode::eExclusive); + vk::Buffer vertex_buffer = device.createBuffer(buffer_info); + VkMemoryRequirements memory_requirements; + vkGetBufferMemoryRequirements(device, vertex_buffer, &memory_requirements); + vk::PhysicalDeviceMemoryProperties memory_propierties = selected_physical_device.getMemoryProperties(); + + int propierty_index = -1; + for (int i = 0; i < memory_propierties.memoryTypeCount; i++) + { + if (memory_propierties.memoryTypes[i].propertyFlags & vk::MemoryPropertyFlags(vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent)) + { + propierty_index = i; + break; + } + } + if (propierty_index == -1) + { + throw std::runtime_error("Didnt find a suitable memory"); + } + + vk::MemoryAllocateInfo alloc_info = vk::MemoryAllocateInfo(memory_requirements.size, propierty_index); + + vk::DeviceMemory vertex_buffer_memory = device.allocateMemory(alloc_info); + device.bindBufferMemory(vertex_buffer, vertex_buffer_memory, 0); + + char *data = (char *)device.mapMemory(vertex_buffer_memory, 0, buffer_info.size); + memcpy(data, vertices.data(), buffer_info.size); + device.unmapMemory(vertex_buffer_memory); vk::CommandPoolCreateInfo command_pool_info = {}; command_pool_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer; @@ -361,10 +432,13 @@ int main() vk::Rect2D render_area = {{0, 0}, framebuffer_extension}; vk::RenderPassBeginInfo render_pass_begin = vk::RenderPassBeginInfo(render_pass, framebuffers[image_index], render_area, 1, &clear_color); + vk::DeviceSize offset = 0; command_buffers[0].beginRenderPass(render_pass_begin, vk::SubpassContents::eInline); command_buffers[0].bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); - command_buffers[0].setViewport(0, viewport); - command_buffers[0].setScissor(0, scissor); + //command_buffers[0].setViewport(0, viewport); + //command_buffers[0].setScissor(0, scissor); + + command_buffers[0].bindVertexBuffers(0, 1, &vertex_buffer, &offset); command_buffers[0].draw(3, 1, 0, 0); command_buffers[0].endRenderPass(); if (vkEndCommandBuffer(command_buffers[0]) != VK_SUCCESS) @@ -393,12 +467,13 @@ int main() graphics_queue.presentKHR(present_info); } - + device.waitIdle(); + device.destroyBuffer(vertex_buffer); + device.freeMemory(vertex_buffer_memory); for (auto &framebuffer: framebuffers) { device.destroyFramebuffer(framebuffer); } - device.destroyFence(next_frame_fence); device.destroySemaphore(render_semaphore); device.destroySemaphore(image_semaphore); diff --git a/shaders/fragment.frag b/shaders/fragment.frag index 02a2b45..5584d49 100644 --- a/shaders/fragment.frag +++ b/shaders/fragment.frag @@ -1,9 +1,9 @@ #version 450 -layout(location = 0) in vec3 in_color; +layout(location = 0) in vec3 frag_color; layout(location = 0) out vec4 out_color; void main() { - out_color = vec4(in_color, 1.0); + out_color = vec4(frag_color, 1.0); } \ No newline at end of file diff --git a/shaders/fragment.spv b/shaders/fragment.spv index 782c404c1bb50d695218f562a042a094c51bcc77..692e6c1eab87c30dbfd16d8f1994b55f4ec762cb 100644 GIT binary patch delta 21 ccmdnPvWI2D3$C=H#Ps;&{G9xvji0<20b{BO4*&oF delta 21 ccmdnPvWI2D3$Dz(_~iVY{2~U1ji0<20bipCMgRZ+ diff --git a/shaders/vertex.spv b/shaders/vertex.spv index ef3cf1165c3986597902e82b48d6d6786ee59feb..be23c4621dc4729d2ef956d831ec5380354baea0 100644 GIT binary patch literal 1084 zcmYk4OH0F05QVR;@vZg!epFlv;zC6b6;X+cf{Nfalr+kPn3gn(y7IUAtK0~lFUhUF zVKOskUgu6yt?UdNGhoKeq}evbnK2b1M%YlU{l-IMubZ{@4i5Lpm@?Hok(`Dy) z6frm}b{X5wmF;HDB#e47HbCq(j(agr_vG#zPY*k=NzidK!Pv=D+I$YecHB;jJa(kK z5;4YCAMV2Z8`WTQoOW38jre;1dDM^xKbMCM#=d;L;k7B@;KQlkhYvn|=fr}Z+v-Rk z>_u5a(sjv_>h%NVKK&Quk4V=goZZsrFQMPM{4r_j!8T>V&GjIL)Ri|O?YN(mKKlh@ z&QzXP^koiyO~QfKv@~g60nK=B1!QGu8kHAYD_*#ry;UmMbG%p|qD*%qUI@}HUb ztNdc(dD@vxhuppQoO^EP-kG7@+MY6I!pxdEb7bnZWLjd3xK8Dx-dV3(l>P3(;XaCa z)2@VQ7R+=NPwMyUh0Ot5vK`qoSzCWC`9Bg*ni&)I;^^cg>K(_)a2OZmk1UNdo2PN# zj?$#G-%@nb4V}%C?6Th!1X48j3rt|HYLDTdu%#X3g<>>o^2s0@j6?*QB^3UW#i!}$ zJROzkcR{f=PfX5zLhMwfXU2-Rd3k1ksyk}x9CIo6KC{CQwkR+1Bpv5CA##^lHqHe) z3(kf`Kb(;?bu*vxJ{>U&=VtS`Y8`E)x=^0|J}OVlnyuZKqgOb**HHsUKb|9o;|wGH z(g*ml!|cByV=Ze}zOktN+tRAF)}!n}x3#@coGiel>5g=gfcfeVG% zoA1HDa+v#sqaHseV|hGZ!{2*455Bb^Lj&(Mi;A`0dd-pm9Ok)eim9o&PSwM*FyBK@ zo*A#p(}$m7MKPS8Vb$f~nE}lEYtC>(*qC4Hr&HD56oR8~%=5P#_BqerZfMEDv){Um zZ&8Pae(%V$H+SssfIH=Fa0hqg$-`|Z3gvwbIgGp6lxOBp#$4bn860=LEg#AzWb6aJ zCu326U!EL1_23=Hk$if diff --git a/shaders/vertex.vert b/shaders/vertex.vert index c2f3b56..71b74b4 100644 --- a/shaders/vertex.vert +++ b/shaders/vertex.vert @@ -3,22 +3,13 @@ // x -> -1 (left) 1(right) // y -> -1 (top) 1(bottom) -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); +layout(location = 0) in vec2 in_position; +layout(location = 1) in vec3 in_color; -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) -); - -layout(location = 0) out vec3 in_color; +layout(location = 0) out vec3 frag_color; void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - in_color = colors[gl_VertexIndex]; + gl_Position = vec4(in_position, 0.0, 1.0); + frag_color = in_color; } \ No newline at end of file