From 673dbb51c30e1c6b1843efbdc96586dda3557020 Mon Sep 17 00:00:00 2001 From: maik klein Date: Sat, 10 Dec 2016 21:30:51 +0100 Subject: [PATCH] Send uniform data to frag shader --- examples/texture/shader/frag.spv | Bin 448 -> 788 bytes examples/texture/shader/triangle.frag | 7 ++- examples/texture/shader/triangle.vert | 2 +- examples/texture/shader/vert.spv | Bin 852 -> 904 bytes examples/texture/src/main.rs | 77 +++++++++++++++++++++----- src/device.rs | 11 ++++ 6 files changed, 80 insertions(+), 17 deletions(-) diff --git a/examples/texture/shader/frag.spv b/examples/texture/shader/frag.spv index e1a850d82440c1021c6ff732fae6dd6eea16e628..afc80bb0e2db524b5659a431c2a47bcc87881099 100644 GIT binary patch literal 788 zcmY*V%TB^j5FIE5eDM$gbwNOuCWII?CPodISQjL|;`-(SO+k^87WRIdU**Qcb8ZX4 z+e~NX%$cW^Y-U6f__A^=(JM+4zQ~TGjduF4{Z_abwL0f*GIC-WA~|`^~_sp z&sCkX_FNA?#73pfzBUl{Ufu`VyYr`hAV4haA@bIH#5T6E;mR-yoA@4c20rYRncG)U z)G?Ea)R=(xu#w-4AaUaQ+>LzJI$6gezp>%-E~|PaT!eWIa~&~u1$G)(9a2R--i|)^ ztmB`B=JknR#Xff?M*cQ5`Sfe}b9Z9Q-Gyd9V$7w_d5M+b=b-D51mraK^RW4tUw|f; z^%!TL5=7+dmhnSa%w5e!pZgNK!0njJJ@`1^4-gx=i$7}@A)JRjufb%WeF*VqaM@?LAO;z12Q-Wm%Kr=0V*$hf DM?ece diff --git a/examples/texture/shader/triangle.frag b/examples/texture/shader/triangle.frag index 299ad8f..a8b8e92 100644 --- a/examples/texture/shader/triangle.frag +++ b/examples/texture/shader/triangle.frag @@ -1,10 +1,13 @@ -#version 400 +#version 450 #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable +layout (binding = 0) uniform UBO{ + vec3 color; +} ubo; layout (location = 0) in vec4 o_color; layout (location = 0) out vec4 uFragColor; void main() { - uFragColor = o_color; + uFragColor = vec4(ubo.color, 1); } diff --git a/examples/texture/shader/triangle.vert b/examples/texture/shader/triangle.vert index 836fc7a..36b93f4 100644 --- a/examples/texture/shader/triangle.vert +++ b/examples/texture/shader/triangle.vert @@ -1,4 +1,4 @@ -#version 400 +#version 450 #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable diff --git a/examples/texture/shader/vert.spv b/examples/texture/shader/vert.spv index 8d8a6f1d53465143793f9490531950e43059cbf1..12576d6882f88f01f2be77445a3dec7cfd510143 100644 GIT binary patch delta 77 zcmcb@*1i;e#{q>l4i^9b delta 35 qcmeBRzrr>lhjGHh+{cr17)vI9Vcf^f${@hNz`zc~Y?BW%9R~p68VcM1 diff --git a/examples/texture/src/main.rs b/examples/texture/src/main.rs index 7dbd846..76f9834 100644 --- a/examples/texture/src/main.rs +++ b/examples/texture/src/main.rs @@ -546,6 +546,42 @@ fn main() { device.unmap_memory(index_buffer_memory); device.bind_buffer_memory(index_buffer, index_buffer_memory, 0).unwrap(); + let uniform_color_buffer_info = vk::BufferCreateInfo { + s_type: vk::StructureType::BufferCreateInfo, + p_next: ptr::null(), + flags: vk::BufferCreateFlags::empty(), + size: 3 * std::mem::size_of::() as u64, + usage: vk::BUFFER_USAGE_UNIFORM_BUFFER_BIT, + sharing_mode: vk::SharingMode::Exclusive, + queue_family_index_count: 0, + p_queue_family_indices: ptr::null(), + }; + let uniform_color_buffer_data = [0.0f32, 1.0, 0.0]; + let uniform_color_buffer = device.create_buffer(&uniform_color_buffer_info).unwrap(); + let uniform_color_buffer_memory_req = + device.get_buffer_memory_requirements(uniform_color_buffer); + let uniform_color_buffer_memory_index = + find_memorytype_index(&uniform_color_buffer_memory_req, + &device_memory_properties, + vk::MEMORY_PROPERTY_HOST_VISIBLE_BIT) + .expect("Unable to find suitable memorytype for the vertex buffer."); + + let uniform_color_buffer_allocate_info = vk::MemoryAllocateInfo { + s_type: vk::StructureType::MemoryAllocateInfo, + p_next: ptr::null(), + allocation_size: uniform_color_buffer_memory_req.size, + memory_type_index: uniform_color_buffer_memory_index, + }; + let uniform_color_buffer_memory = device.allocate_memory(&uniform_color_buffer_allocate_info) + .unwrap(); + let uniform_slice = device.map_memory::(uniform_color_buffer_memory, + 0, + uniform_color_buffer_info.size, + vk::MemoryMapFlags::empty()) + .unwrap(); + uniform_slice.copy_from_slice(&uniform_color_buffer_data[..]); + device.unmap_memory(uniform_color_buffer_memory); + device.bind_buffer_memory(uniform_color_buffer, uniform_color_buffer_memory, 0).unwrap(); let vertex_input_buffer_info = vk::BufferCreateInfo { s_type: vk::StructureType::BufferCreateInfo, p_next: ptr::null(), @@ -632,20 +668,25 @@ fn main() { }; let descriptor_sets = device.allocate_descriptor_sets(&desc_alloc_info).unwrap(); - let write_desc_sets = [ - vk::WriteDescriptorSet{ - s_type: vk::StructureType::WriteDescriptorSet, - p_next: ptr::null(), - dst_set: descriptor_sets[0], - dst_binding: 0, - dst_array_element: 0, - descriptor_count: 1, - descriptor_type: vk::DescriptorType::UniformBuffer, - p_image_info: ptr::null(), - p_buffer_info: ptr::null(), - p_texel_buffer_view: ptr::null(), - } - ]; + let uniform_color_buffer_descriptor = vk::DescriptorBufferInfo { + buffer: uniform_color_buffer, + offset: 0, + range: mem::size_of_val(&uniform_color_buffer_data) as u64, + }; + + let write_desc_sets = [vk::WriteDescriptorSet { + s_type: vk::StructureType::WriteDescriptorSet, + p_next: ptr::null(), + dst_set: descriptor_sets[0], + dst_binding: 0, + dst_array_element: 0, + descriptor_count: 1, + descriptor_type: vk::DescriptorType::UniformBuffer, + p_image_info: ptr::null(), + p_buffer_info: &uniform_color_buffer_descriptor, + p_texel_buffer_view: ptr::null(), + }]; + device.update_descriptor_sets(&write_desc_sets[..], &[]); let vertex_spv_file = File::open(Path::new("shader/vert.spv")) .expect("Could not find vert.spv."); let frag_spv_file = File::open(Path::new("shader/frag.spv")).expect("Could not find frag.spv."); @@ -905,6 +946,12 @@ fn main() { device.cmd_begin_render_pass(draw_command_buffer, &render_pass_begin_info, vk::SubpassContents::Inline); + device.cmd_bind_descriptor_sets(draw_command_buffer, + vk::PipelineBindPoint::Graphics, + pipeline_layout, + 0, + &descriptor_sets[..], + &[]); device.cmd_bind_pipeline(draw_command_buffer, vk::PipelineBindPoint::Graphics, graphic_pipeline); @@ -965,6 +1012,8 @@ fn main() { device.free_memory(index_buffer_memory); device.destroy_buffer(index_buffer); device.free_memory(vertex_input_buffer_memory); + device.destroy_buffer(uniform_color_buffer); + device.free_memory(uniform_color_buffer_memory); device.destroy_buffer(vertex_input_buffer); for framebuffer in framebuffers { device.destroy_framebuffer(framebuffer); diff --git a/src/device.rs b/src/device.rs index 71e1d02..a816591 100644 --- a/src/device.rs +++ b/src/device.rs @@ -123,6 +123,17 @@ impl<'r> Device<'r> { descriptor_sets.as_ptr()); } } + pub fn update_descriptor_sets(&self, + descriptor_writes: &[vk::WriteDescriptorSet], + descriptor_copies: &[vk::CopyDescriptorSet]) { + unsafe { + self.device_fn.update_descriptor_sets(self.handle, + descriptor_writes.len() as u32, + descriptor_writes.as_ptr(), + descriptor_copies.len() as u32, + descriptor_copies.as_ptr()); + } + } pub fn allocate_descriptor_sets(&self, create_info: &vk::DescriptorSetAllocateInfo)