diff --git a/shader/shader.frag.glsl b/shader/shader.frag.glsl index 9517576..4b942ed 100644 --- a/shader/shader.frag.glsl +++ b/shader/shader.frag.glsl @@ -21,7 +21,7 @@ layout(location = 0) in vec3 frag_color; layout(location = 1) in vec2 frag_texture_coordinate; -layout(binding = 1) uniform sampler2D texture_sampler; +layout(binding = 0) uniform sampler2D texture_sampler; layout(location = 0) out vec4 out_color; diff --git a/shader/shader.second.vertex.glsl b/shader/shader.second.vertex.glsl new file mode 100644 index 0000000..216466e --- /dev/null +++ b/shader/shader.second.vertex.glsl @@ -0,0 +1,33 @@ + +/* + * VulkZample + * Copyright (C) 2024 Rebekah Rowe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#version 450 + +layout(location = 0) in vec2 in_position; +layout(location = 2) in vec3 in_color; +layout(location = 4) in vec2 in_texture_coordinate; + +layout(location = 0) out vec3 frag_color; +layout(location = 1) out vec2 frag_texture_coordinate; + +void main() { + gl_Position = vec4(in_position, 0.5, 1.0); + frag_color = in_color; + frag_texture_coordinate = in_texture_coordinate; +} diff --git a/shader/shader.vertex.glsl b/shader/shader.third.vertex.glsl similarity index 95% rename from shader/shader.vertex.glsl rename to shader/shader.third.vertex.glsl index 7701c17..c361502 100644 --- a/shader/shader.vertex.glsl +++ b/shader/shader.third.vertex.glsl @@ -19,7 +19,7 @@ #version 450 -layout(binding = 0) uniform UniformBufferObject { +layout(binding = 1) uniform UniformBufferObject { mat4 model; mat4 view; mat4 proj; diff --git a/src/main.cpp b/src/main.cpp index db3c78a..6cf7077 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -141,7 +141,7 @@ private: vk::VertexInputAttributeDescription { // https://docs.vulkan.org/tutorial/latest/04_Vertex_buffers/00_Vertex_input_description.html .location = 0, .binding = 0, - .format = vk::Format::eR32G32B32Sfloat, + .format = vk::Format::eR32G32Sfloat, .offset = offsetof(Vertex2, pos) }, vk::VertexInputAttributeDescription { .location = 2, @@ -219,7 +219,7 @@ private: : parent(_parent) { assert(_parent != nullptr); } public: - std::optional vk_descriptor_set_layout_ubo; + std::optional vk_descriptor_set_layout; std::optional vk_descriptor_pool; std::vector vk_descriptor_sets; @@ -249,25 +249,24 @@ private: this->vk_shader_frag.emplace(gpu, gfx_shaders.second); } { - constexpr vk::DescriptorSetLayoutBinding descriptor_set_layout_binding_ubo { - .binding = 0, - .descriptorType = vk::DescriptorType::eUniformBuffer, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eVertex, - .pImmutableSamplers = nullptr // Optional - }; - constexpr vk::DescriptorSetLayoutBinding descriptor_set_layout_binding_sampler { - .binding = UsesProjection() ? 1 : 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment, - .pImmutableSamplers = nullptr // Optional - }; - constexpr auto descriptor_set_layout_bindings = [&]() { - if constexpr (UsesProjection()) - return std::array { descriptor_set_layout_binding_ubo, descriptor_set_layout_binding_sampler }; - else + constexpr vk::DescriptorSetLayoutBinding descriptor_set_layout_binding_sampler { + .binding = 0, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + .pImmutableSamplers = nullptr // Optional + }; + if constexpr (UsesProjection()) { + constexpr vk::DescriptorSetLayoutBinding descriptor_set_layout_binding_ubo { + .binding = 1, + .descriptorType = vk::DescriptorType::eUniformBuffer, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eVertex, + .pImmutableSamplers = nullptr // Optional + }; + return std::array { descriptor_set_layout_binding_sampler, descriptor_set_layout_binding_ubo }; + } else return std::array { descriptor_set_layout_binding_sampler }; }(); @@ -275,7 +274,7 @@ private: .bindingCount = static_cast(descriptor_set_layout_bindings.size()), .pBindings = descriptor_set_layout_bindings.data() }; - this->vk_descriptor_set_layout_ubo.emplace(gpu, descriptor_set_layout_create_info); + this->vk_descriptor_set_layout.emplace(gpu, descriptor_set_layout_create_info); } { // Init graphics pipeline/renderpass const vk::PipelineShaderStageCreateInfo shader_stage_create_info_vertex { @@ -374,7 +373,7 @@ private: const vk::PipelineLayoutCreateInfo pipeline_layout_create_info { .setLayoutCount = 1, // Optional - .pSetLayouts = &**this->vk_descriptor_set_layout_ubo, // Optional + .pSetLayouts = &**this->vk_descriptor_set_layout, // Optional //.pushConstantRangeCount = 0, // Optional //.pPushConstantRanges = nullptr // Optional }; @@ -415,17 +414,17 @@ private: } void UpdateDescriptors(const vk::ImageView& texture_view) { { // update descriptors - const vk::DescriptorPoolSize descriptor_pool_size_sampler { - .type = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = static_cast(this->parent->vk_max_frames_in_flight) - }; const vk::DescriptorPoolSize descriptor_pool_size_ubo { .type = vk::DescriptorType::eUniformBuffer, .descriptorCount = static_cast(this->parent->vk_max_frames_in_flight) }; + const vk::DescriptorPoolSize descriptor_pool_size_sampler { + .type = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = static_cast(this->parent->vk_max_frames_in_flight) + }; const auto descriptor_pool_sizes = [&]() { if constexpr (UsesProjection()) - return std::array { descriptor_pool_size_ubo, descriptor_pool_size_sampler }; + return std::array { descriptor_pool_size_sampler, descriptor_pool_size_ubo }; else return std::array { descriptor_pool_size_sampler }; }(); @@ -439,15 +438,10 @@ private: assert(descriptor_pool_create_info.flags & vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet); // requirement, to soothe validation layer complaints this->vk_descriptor_pool.emplace(*this->parent->vk_gpu, descriptor_pool_create_info); - const std::vector descriptor_set_layouts = [&]() { - if (this->vk_descriptor_set_layout_ubo) { - return std::vector(this->parent->vk_max_frames_in_flight, *this->vk_descriptor_set_layout_ubo); - } - return std::vector(); - }(); + const std::vector descriptor_set_layouts = std::vector(this->parent->vk_max_frames_in_flight, *this->vk_descriptor_set_layout); const vk::DescriptorSetAllocateInfo descriptor_set_allocate_info { .descriptorPool = **this->vk_descriptor_pool, - .descriptorSetCount = static_cast(this->parent->vk_max_frames_in_flight), + .descriptorSetCount = static_cast(descriptor_set_layouts.size()), .pSetLayouts = descriptor_set_layouts.data() }; this->vk_descriptor_sets = vk::raii::DescriptorSets(*this->parent->vk_gpu, descriptor_set_allocate_info); @@ -455,29 +449,6 @@ private: std::vector write_descriptor_sets; write_descriptor_sets.reserve(this->parent->vk_max_frames_in_flight); - std::vector buffer_infos; - buffer_infos.reserve(this->parent->vk_max_frames_in_flight); - for (std::size_t i = 0; i < this->parent->vk_max_frames_in_flight; i++) { - const vk::DescriptorBufferInfo descriptor_buffer_info_ubo { - .buffer = *this->vk_buffers_uniform.at(i)->first, - .offset = 0, - .range = sizeof(UniformBufferObject_Projection), - }; - const vk::DescriptorBufferInfo& descriptor_buffer_info_handle = buffer_infos.emplace_back(descriptor_buffer_info_ubo); - - const vk::WriteDescriptorSet descriptor_write_ubo { - .dstSet = *this->vk_descriptor_sets[i], - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = vk::DescriptorType::eUniformBuffer, - .pImageInfo = nullptr, // Optional - .pBufferInfo = &descriptor_buffer_info_handle, - .pTexelBufferView = nullptr, // Optional - }; - write_descriptor_sets.emplace_back(descriptor_write_ubo); - } - std::vector image_infos; image_infos.reserve(this->parent->vk_max_frames_in_flight); for (std::size_t i = 0; i < this->parent->vk_max_frames_in_flight; i++) { @@ -489,7 +460,7 @@ private: const vk::DescriptorImageInfo& descriptor_image_info_handle = image_infos.emplace_back(descriptor_image_info_texture); const vk::WriteDescriptorSet descriptor_write_texture { .dstSet = *this->vk_descriptor_sets[i], - .dstBinding = 1, + .dstBinding = 0, .dstArrayElement = 0, .descriptorCount = 1, .descriptorType = vk::DescriptorType::eCombinedImageSampler, @@ -497,6 +468,32 @@ private: }; write_descriptor_sets.emplace_back(descriptor_write_texture); } + + std::vector buffer_infos; + if constexpr (UsesProjection()) { + buffer_infos.reserve(this->parent->vk_max_frames_in_flight); + for (std::size_t i = 0; i < this->parent->vk_max_frames_in_flight; i++) { + const vk::DescriptorBufferInfo descriptor_buffer_info_ubo { + .buffer = *this->vk_buffers_uniform.at(i)->first, + .offset = 0, + .range = sizeof(UniformBufferObject_Projection), + }; + const vk::DescriptorBufferInfo& descriptor_buffer_info_handle = buffer_infos.emplace_back(descriptor_buffer_info_ubo); + + const vk::WriteDescriptorSet descriptor_write_ubo { + .dstSet = *this->vk_descriptor_sets[i], + .dstBinding = 1, + .dstArrayElement = 0, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eUniformBuffer, + .pImageInfo = nullptr, // Optional + .pBufferInfo = &descriptor_buffer_info_handle, + .pTexelBufferView = nullptr, // Optional + }; + write_descriptor_sets.emplace_back(descriptor_write_ubo); + } + } + this->parent->vk_gpu->updateDescriptorSets(write_descriptor_sets, nullptr); } } @@ -549,9 +546,17 @@ private: } }; + VulkanRenderPipeline vk_pipeline_second; VulkanRenderPipeline vk_pipeline_third; - static inline const std::vector vertices_quad_sample = { + static inline const std::vector vertices_rect_sample = { + { { -0.5f, -0.5f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } }, + { { 0.5f, -0.5f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } }, + { { 0.5f, 0.5f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } }, + { { -0.5f, 0.5f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } } + }; + + static inline const std::vector vertices_cube_sample = { { { -0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } }, { { 0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } }, { { 0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } }, @@ -582,7 +587,8 @@ private: public: VulkanExampleApplication() - : vk_pipeline_third(this) { + : vk_pipeline_second(this) + , vk_pipeline_third(this) { try { // try me // all the code is in init :O boo hoo, ill shove all ur ram out of scope ASAP i can, dont seperate them, just scope them instead dumb dumb. { // Window init this->libsdl.emplace(SDL_INIT_VIDEO); @@ -976,9 +982,12 @@ public: this->vk_render_pass.emplace(*this->vk_gpu, render_pass_create_info); } { - const auto shader_info_vertex = vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_vertex_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_vertex_glsl_spv.begin) }; const auto shader_info_frag = vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_frag_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_frag_glsl_spv.begin) }; - this->vk_pipeline_third.CreatePipeline(*this->vk_gpu, *this->vk_render_pass, { shader_info_vertex, shader_info_frag }, this->vk_msaa_samples); + const auto shader_second_info_vertex = vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_second_vertex_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_second_vertex_glsl_spv.begin) }; + this->vk_pipeline_second.CreatePipeline(*this->vk_gpu, *this->vk_render_pass, { shader_second_info_vertex, shader_info_frag }, this->vk_msaa_samples); + + const auto shader_third_info_vertex = vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_third_vertex_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_third_vertex_glsl_spv.begin) }; + this->vk_pipeline_third.CreatePipeline(*this->vk_gpu, *this->vk_render_pass, { shader_third_info_vertex, shader_info_frag }, this->vk_msaa_samples); } { this->CreateBufferFrame(); @@ -1001,7 +1010,8 @@ public: this->TransitionImageLayout(*this->vk_depth_image->first, this->vk_depth_format, vk::ImageLayout::eUndefined, vk::ImageLayout::eDepthStencilAttachmentOptimal); } { // fill sample vertex data - this->vk_pipeline_third.ReplaceModelInfo(vertices_quad_sample, Vertex3::IndexMap::rectangle); + this->vk_pipeline_second.ReplaceModelInfo(vertices_rect_sample, Vertex2::IndexMap::rectangle); + this->vk_pipeline_third.ReplaceModelInfo(vertices_cube_sample, Vertex3::IndexMap::rectangle); } { // syncronizing vars assert(this->vk_semephores_image_available.empty()); @@ -1131,6 +1141,7 @@ public: this->vk_texture_sampler.emplace(*this->vk_gpu, sampler_create_info); } { + this->vk_pipeline_second.UpdateDescriptors(*this->vk_texture_view); this->vk_pipeline_third.UpdateDescriptors(*this->vk_texture_view); } } catch (const vk::SystemError& error) { @@ -1638,11 +1649,12 @@ public: .pClearValues = clear_colors.data() }; command_buffer.beginRenderPass(render_pass_info, vk::SubpassContents::eInline); - this->vk_pipeline_third.Bind(command_buffer, frame_index); this->RecordDynamic(command_buffer); - + this->vk_pipeline_third.Bind(command_buffer, frame_index); this->vk_pipeline_third.Draw(command_buffer); + this->vk_pipeline_second.Bind(command_buffer, frame_index); + this->vk_pipeline_second.Draw(command_buffer); command_buffer.endRenderPass(); command_buffer.end(); }; @@ -1663,7 +1675,6 @@ public: command_buffer.setScissor(0, scissor); } -private: public: ~VulkanExampleApplication() { this->vk_gpu->waitIdle();