diff --git a/shaders/frag.spv b/shaders/frag.spv index da37f7e..9495412 100644 Binary files a/shaders/frag.spv and b/shaders/frag.spv differ diff --git a/shaders/shader.frag b/shaders/shader.frag index 7c5b0e7..873f541 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,9 +1,12 @@ #version 450 +layout(binding = 1) uniform sampler2D texSampler; + layout(location = 0) in vec3 fragColor; +layout(location = 1) in vec2 fragTexCoord; layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(fragColor, 1.0); + outColor = texture(texSampler, fragTexCoord); } diff --git a/shaders/shader.vert b/shaders/shader.vert index 5ffbb2d..5510aa3 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -8,10 +8,13 @@ layout(binding = 0) uniform UniformBufferObject { layout(location = 0) in vec2 inPosition; layout(location = 1) in vec3 inColor; +layout(location = 2) in vec2 inTexCoord; layout(location = 0) out vec3 fragColor; +layout(location = 1) out vec2 fragTexCoord; void main() { gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); fragColor = inColor; + fragTexCoord = inTexCoord; } diff --git a/shaders/vert.spv b/shaders/vert.spv index 2b7afb0..9ded648 100644 Binary files a/shaders/vert.spv and b/shaders/vert.spv differ diff --git a/src/app.rs b/src/app.rs index 05a7f59..1c449c9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -32,10 +32,26 @@ pub const MAX_FRAMES_IN_FLIGHT: usize = 2; lazy_static! { pub static ref VERTICES: Vec = vec![ - Vertex::new(glm::vec2(-0.5, -0.5), glm::vec3(1.0, 0.0, 0.0)), - Vertex::new(glm::vec2(0.5, -0.5), glm::vec3(0.0, 1.0, 0.0)), - Vertex::new(glm::vec2(0.5, 0.5), glm::vec3(0.0, 0.0, 1.0)), - Vertex::new(glm::vec2(-0.5, 0.5), glm::vec3(1.0, 1.0, 1.0)), + Vertex::new( + glm::vec2(-0.5, -0.5), + glm::vec3(1.0, 0.0, 0.0), + glm::vec2(1.0, 0.0) + ), + Vertex::new( + glm::vec2(0.5, -0.5), + glm::vec3(0.0, 1.0, 0.0), + glm::vec2(0.0, 0.0) + ), + Vertex::new( + glm::vec2(0.5, 0.5), + glm::vec3(0.0, 0.0, 1.0), + glm::vec2(0.0, 1.0) + ), + Vertex::new( + glm::vec2(-0.5, 0.5), + glm::vec3(1.0, 1.0, 1.0), + glm::vec2(1.0, 1.0) + ), ]; } diff --git a/src/app/data.rs b/src/app/data.rs index 17136ed..7fa6bef 100644 --- a/src/app/data.rs +++ b/src/app/data.rs @@ -518,7 +518,13 @@ impl AppData { .descriptor_count(1) .stage_flags(vk::ShaderStageFlags::VERTEX); - let bindings = &[ubo_binding]; + let sampler_binding = vk::DescriptorSetLayoutBinding::builder() + .binding(1) + .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .descriptor_count(1) + .stage_flags(vk::ShaderStageFlags::FRAGMENT); + + let bindings = &[ubo_binding, sampler_binding]; let info = vk::DescriptorSetLayoutCreateInfo::builder().bindings(bindings); self.descriptor_set_layout = device.create_descriptor_set_layout(&info, None)?; @@ -884,7 +890,11 @@ impl AppData { .type_(vk::DescriptorType::UNIFORM_BUFFER) .descriptor_count(self.swapchain_images.len() as u32); - let pool_sizes = &[ubo_size]; + let sampler_size = vk::DescriptorPoolSize::builder() + .type_(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .descriptor_count(self.swapchain_images.len() as u32); + + let pool_sizes = &[ubo_size, sampler_size]; let info = vk::DescriptorPoolCreateInfo::builder() .pool_sizes(pool_sizes) .max_sets(self.swapchain_images.len() as u32); @@ -916,7 +926,23 @@ impl AppData { .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) .buffer_info(buffer_info); - device.update_descriptor_sets(&[ubo_write], &[] as &[vk::CopyDescriptorSet]); + let info = vk::DescriptorImageInfo::builder() + .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) + .image_view(self.texture_image_view) + .sampler(self.texture_sampler); + + let image_info = &[info]; + let sampler_write = vk::WriteDescriptorSet::builder() + .dst_set(self.descriptor_sets[i]) + .dst_binding(1) + .dst_array_element(0) + .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .image_info(image_info); + + device.update_descriptor_sets( + &[ubo_write, sampler_write], + &[] as &[vk::CopyDescriptorSet], + ); } Ok(()) @@ -1118,7 +1144,7 @@ impl AppData { &self, device: &Device, image: vk::Image, - format: vk::Format, + _format: vk::Format, old_layout: vk::ImageLayout, new_layout: vk::ImageLayout, ) -> Result<()> { diff --git a/src/app/structs.rs b/src/app/structs.rs index 37f5e62..3fb93ea 100644 --- a/src/app/structs.rs +++ b/src/app/structs.rs @@ -99,11 +99,16 @@ impl SwapchainSupport { pub struct Vertex { pos: glm::Vec2, color: glm::Vec3, + tex_coord: glm::Vec2, } impl Vertex { - pub fn new(pos: glm::Vec2, color: glm::Vec3) -> Self { - Self { pos, color } + pub fn new(pos: glm::Vec2, color: glm::Vec3, tex_coord: glm::Vec2) -> Self { + Self { + pos, + color, + tex_coord, + } } pub fn binding_description() -> vk::VertexInputBindingDescription { @@ -114,7 +119,7 @@ impl Vertex { .build() } - pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 2] { + pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] { let pos = vk::VertexInputAttributeDescription::builder() .binding(0) .location(0) @@ -129,7 +134,14 @@ impl Vertex { .offset(size_of::() as u32) .build(); - [pos, color] + let tex_coord = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(2) + .format(vk::Format::R32G32_SFLOAT) + .offset((size_of::() + size_of::()) as u32) + .build(); + + [pos, color, tex_coord] } }