Rendering an image as a texture

This commit is contained in:
Timothy Warren 2023-04-19 10:17:19 -04:00
parent 29095b589d
commit 0a3cd3e809
7 changed files with 73 additions and 13 deletions

Binary file not shown.

View File

@ -1,9 +1,12 @@
#version 450 #version 450
layout(binding = 1) uniform sampler2D texSampler;
layout(location = 0) in vec3 fragColor; layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor; layout(location = 0) out vec4 outColor;
void main() { void main() {
outColor = vec4(fragColor, 1.0); outColor = texture(texSampler, fragTexCoord);
} }

View File

@ -8,10 +8,13 @@ layout(binding = 0) uniform UniformBufferObject {
layout(location = 0) in vec2 inPosition; layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor; layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 0) out vec3 fragColor; layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
void main() { void main() {
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
fragColor = inColor; fragColor = inColor;
fragTexCoord = inTexCoord;
} }

Binary file not shown.

View File

@ -32,10 +32,26 @@ pub const MAX_FRAMES_IN_FLIGHT: usize = 2;
lazy_static! { lazy_static! {
pub static ref VERTICES: Vec<Vertex> = vec![ pub static ref VERTICES: Vec<Vertex> = vec![
Vertex::new(glm::vec2(-0.5, -0.5), glm::vec3(1.0, 0.0, 0.0)), Vertex::new(
Vertex::new(glm::vec2(0.5, -0.5), glm::vec3(0.0, 1.0, 0.0)), glm::vec2(-0.5, -0.5),
Vertex::new(glm::vec2(0.5, 0.5), glm::vec3(0.0, 0.0, 1.0)), glm::vec3(1.0, 0.0, 0.0),
Vertex::new(glm::vec2(-0.5, 0.5), glm::vec3(1.0, 1.0, 1.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)
),
]; ];
} }

View File

@ -518,7 +518,13 @@ impl AppData {
.descriptor_count(1) .descriptor_count(1)
.stage_flags(vk::ShaderStageFlags::VERTEX); .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); let info = vk::DescriptorSetLayoutCreateInfo::builder().bindings(bindings);
self.descriptor_set_layout = device.create_descriptor_set_layout(&info, None)?; self.descriptor_set_layout = device.create_descriptor_set_layout(&info, None)?;
@ -884,7 +890,11 @@ impl AppData {
.type_(vk::DescriptorType::UNIFORM_BUFFER) .type_(vk::DescriptorType::UNIFORM_BUFFER)
.descriptor_count(self.swapchain_images.len() as u32); .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() let info = vk::DescriptorPoolCreateInfo::builder()
.pool_sizes(pool_sizes) .pool_sizes(pool_sizes)
.max_sets(self.swapchain_images.len() as u32); .max_sets(self.swapchain_images.len() as u32);
@ -916,7 +926,23 @@ impl AppData {
.descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)
.buffer_info(buffer_info); .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(()) Ok(())
@ -1118,7 +1144,7 @@ impl AppData {
&self, &self,
device: &Device, device: &Device,
image: vk::Image, image: vk::Image,
format: vk::Format, _format: vk::Format,
old_layout: vk::ImageLayout, old_layout: vk::ImageLayout,
new_layout: vk::ImageLayout, new_layout: vk::ImageLayout,
) -> Result<()> { ) -> Result<()> {

View File

@ -99,11 +99,16 @@ impl SwapchainSupport {
pub struct Vertex { pub struct Vertex {
pos: glm::Vec2, pos: glm::Vec2,
color: glm::Vec3, color: glm::Vec3,
tex_coord: glm::Vec2,
} }
impl Vertex { impl Vertex {
pub fn new(pos: glm::Vec2, color: glm::Vec3) -> Self { pub fn new(pos: glm::Vec2, color: glm::Vec3, tex_coord: glm::Vec2) -> Self {
Self { pos, color } Self {
pos,
color,
tex_coord,
}
} }
pub fn binding_description() -> vk::VertexInputBindingDescription { pub fn binding_description() -> vk::VertexInputBindingDescription {
@ -114,7 +119,7 @@ impl Vertex {
.build() .build()
} }
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 2] { pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
let pos = vk::VertexInputAttributeDescription::builder() let pos = vk::VertexInputAttributeDescription::builder()
.binding(0) .binding(0)
.location(0) .location(0)
@ -129,7 +134,14 @@ impl Vertex {
.offset(size_of::<glm::Vec2>() as u32) .offset(size_of::<glm::Vec2>() as u32)
.build(); .build();
[pos, color] let tex_coord = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(2)
.format(vk::Format::R32G32_SFLOAT)
.offset((size_of::<glm::Vec2>() + size_of::<glm::Vec3>()) as u32)
.build();
[pos, color, tex_coord]
} }
} }