diff --git a/Cargo.toml b/Cargo.toml index 306a809..080f097 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ keywords = ["vulkan", "graphic"] [dependencies] shared_library = "0.1.5" +sharedlib = "6.0.0" diff --git a/examples/texture/Cargo.lock b/examples/texture/Cargo.lock index a6fe93d..ac1e8c9 100644 --- a/examples/texture/Cargo.lock +++ b/examples/texture/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "ash 0.1.0", "glfw 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "image 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -11,6 +12,30 @@ name = "ash" version = "0.1.0" dependencies = [ "shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -18,6 +43,16 @@ name = "bitflags" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byteorder" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cmake" version = "0.1.17" @@ -26,6 +61,28 @@ dependencies = [ "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "color_quant" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "dbghelp-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "deque" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "enum_primitive" version = "0.1.0" @@ -34,11 +91,37 @@ dependencies = [ "num 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "error-chain" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "flate2" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", + "miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "gif" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "color_quant 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glfw" version = "0.9.1" @@ -61,6 +144,51 @@ dependencies = [ "cmake 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "image" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "jpeg-decoder 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "png 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "scoped_threadpool 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "inflate" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "jpeg-decoder" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "0.2.1" @@ -76,6 +204,20 @@ name = "log" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lzw" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "miniz-sys" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "nom" version = "1.2.4" @@ -147,6 +289,25 @@ name = "num-traits" version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "num_cpus" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "png" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand" version = "0.3.14" @@ -155,11 +316,31 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rayon" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-serialize" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "scoped_threadpool" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "semver" version = "0.2.3" @@ -177,16 +358,54 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sharedlib" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "error-chain 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] +"checksum backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "346d7644f0b5f9bc73082d3b2236b69a05fd35cce0cfa3724e184e6a5c9e2a2f" +"checksum backtrace-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3602e8d8c43336088a8505fa55cae2b3884a9be29440863a11528a42f46f6bb7" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" +"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" +"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" "checksum cmake 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "dfcf5bcece56ef953b8ea042509e9dcbdfe97820b7e20d86beb53df30ed94978" +"checksum color_quant 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a475fc4af42d83d28adf72968d9bcfaf035a1a9381642d8e85d8a04957767b0d" +"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" +"checksum deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1614659040e711785ed8ea24219140654da1729f3ec8a47a9719d041112fe7bf" "checksum enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f79eff5be92a4d7d5bddf7daa7d650717ea71628634efe6ca7bcda85b2183c23" +"checksum error-chain 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5c82c815138e278b8dcdeffc49f27ea6ffb528403e9dea4194f2e3dd40b143" +"checksum flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "3eeb481e957304178d2e782f2da1257f1434dfecbae883bafb61ada2a9fea3bb" "checksum gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "dcb000abd6df9df4c637f75190297ebe56c1d7e66b56bbf3b4aa7aece15f61a2" +"checksum gif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "01c7c19a035de94bd7afbaa62c241aadfbdf1a70f560b348d2312eafa566ca16" "checksum glfw 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b14e68c4ccefdf293ecb65390a5761971b83fcfc54d153a5b73d438327633965" "checksum glfw-sys 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eaff144079cb22d6f17009e29e87c02f5fd6c4669093ce12b0b2faa6027f0d23" +"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum image 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)" = "76df2dce95fef56fd35dbc41c36e37b19aede703c6be7739e8b65d5788ffc728" +"checksum inflate 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e7e0062d2dc2f17d2f13750d95316ae8a2ff909af0fda957084f5defd87c43bb" +"checksum jpeg-decoder 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f1a2c12387f1adb21a9a2a096b5bc5f424a21729ea8e535fdf58681d396b6bbe" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f" "checksum libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "23e3757828fa702a20072c37ff47938e9dd331b92fac6e223d26d4b7a55f7ee2" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" +"checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" +"checksum miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d1f4d337a01c32e1f2122510fed46393d53ca35a7f429cb0450abaedfa3ed54" "checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce" "checksum num 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "d2ee34a0338c16ae67afb55824aaf8852700eb0f77ccd977807ccb7606b295f6" "checksum num-bigint 0.1.33 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc450723a2fe91d332a29edd8660e099b937d29e1a3ebe914e0da3f77ac1ad3" @@ -195,7 +414,15 @@ dependencies = [ "checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c" "checksum num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "48cdcc9ff4ae2a8296805ac15af88b3d88ce62128ded0cb74ffb63a587502a84" "checksum num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "95e58eac34596aac30ab134c8a8da9aa2dc99caa4b4b4838e6fc6e298016278f" +"checksum num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "cee7e88156f3f9e19bdd598f8d6c9db7bf4078f99f8381f43a55b09648d1a6e3" +"checksum png 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "06208e2ee243e3118a55dda9318f821f206d8563fb8d4df258767f8e62bb0997" "checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" +"checksum rayon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "655df67c314c30fa3055a365eae276eb88aa4f3413a352a1ab32c1320eda41ea" +"checksum rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1430d286cadb237c17c885e25447c982c97113926bb579f4379c0eca8d9586dc" "checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b" +"checksum scoped_threadpool 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "3ef399c8893e8cb7aa9696e895427fab3a6bf265977bb96e126f24ddd2cda85a" "checksum semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d5b7638a1f03815d94e88cb3b3c08e87f0db4d683ef499d1836aaf70a45623f" "checksum shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fb04126b6fcfd2710fb5b6d18f4207b6c535f2850a7e1a43bcd526d44f30a79a" +"checksum sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5182b4b49a0ff319bc69d23ca9e9f8e40dfb3b88abb895805a54e69325a092c7" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/examples/texture/Cargo.toml b/examples/texture/Cargo.toml index bfd98e7..5d38a51 100644 --- a/examples/texture/Cargo.toml +++ b/examples/texture/Cargo.toml @@ -5,4 +5,5 @@ authors = ["maik klein "] [dependencies] glfw = "0.9.1" +image = "*" ash = { version = "0.1.0", path = "../../"} diff --git a/examples/texture/rust.png b/examples/texture/rust.png new file mode 100644 index 0000000..ec71bd0 Binary files /dev/null and b/examples/texture/rust.png differ diff --git a/examples/texture/shader/frag.spv b/examples/texture/shader/frag.spv index afc80bb..3935b8c 100644 Binary files a/examples/texture/shader/frag.spv and b/examples/texture/shader/frag.spv differ diff --git a/examples/texture/shader/triangle.frag b/examples/texture/shader/triangle.frag index a8b8e92..62c02d6 100644 --- a/examples/texture/shader/triangle.frag +++ b/examples/texture/shader/triangle.frag @@ -2,12 +2,18 @@ #extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_shading_language_420pack : enable +layout (binding = 1) uniform sampler2D samplerColor; + layout (binding = 0) uniform UBO{ vec3 color; } ubo; -layout (location = 0) in vec4 o_color; + + +layout (location = 0) in vec2 o_uv; layout (location = 0) out vec4 uFragColor; void main() { - uFragColor = vec4(ubo.color, 1); + //uFragColor = o_color; + vec4 color = texture(samplerColor, o_uv); + uFragColor = color; } diff --git a/examples/texture/shader/triangle.vert b/examples/texture/shader/triangle.vert index 36b93f4..0c07f3d 100644 --- a/examples/texture/shader/triangle.vert +++ b/examples/texture/shader/triangle.vert @@ -3,11 +3,11 @@ #extension GL_ARB_shading_language_420pack : enable layout (location = 0) in vec4 pos; -layout (location = 1) in vec4 color; +layout (location = 1) in vec2 uv; -layout (location = 0) out vec4 o_color; +layout (location = 0) out vec2 o_uv; void main() { - o_color = color; + o_uv = uv; gl_Position = pos; } diff --git a/examples/texture/shader/vert.spv b/examples/texture/shader/vert.spv index 12576d6..516de65 100644 Binary files a/examples/texture/shader/vert.spv and b/examples/texture/shader/vert.spv differ diff --git a/examples/texture/src/main.rs b/examples/texture/src/main.rs index 76f9834..3d596d2 100644 --- a/examples/texture/src/main.rs +++ b/examples/texture/src/main.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] #[macro_use] extern crate ash; +extern crate image; extern crate glfw; @@ -92,7 +93,7 @@ pub fn find_memorytype_index(memory_req: &vk::MemoryRequirements, #[derive(Clone, Debug, Copy)] struct Vertex { pos: [f32; 4], - color: [f32; 4], + uv: [f32; 2], } fn main() { let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); @@ -280,13 +281,14 @@ fn main() { let command_buffer_allocate_info = vk::CommandBufferAllocateInfo { s_type: vk::StructureType::CommandBufferAllocateInfo, p_next: ptr::null(), - command_buffer_count: 2, + command_buffer_count: 3, command_pool: pool, level: vk::CommandBufferLevel::Primary, }; let command_buffers = device.allocate_command_buffers(&command_buffer_allocate_info).unwrap(); let setup_command_buffer = command_buffers[0]; let draw_command_buffer = command_buffers[1]; + let texture_command_buffer = command_buffers[2]; let present_images = device.get_swapchain_images_khr(swapchain).unwrap(); let present_image_views: Vec = present_images.iter() @@ -513,7 +515,7 @@ fn main() { device.create_framebuffer(&frame_buffer_create_info).unwrap() }) .collect(); - let index_buffer_data = [0u32, 1, 2]; + let index_buffer_data = [0u32, 1, 2, 2, 3, 0]; let index_buffer_info = vk::BufferCreateInfo { s_type: vk::StructureType::BufferCreateInfo, p_next: ptr::null(), @@ -556,7 +558,7 @@ fn main() { 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_data = [0.0f32, 1.0, 1.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); @@ -582,11 +584,27 @@ fn main() { 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 vertices = [Vertex { + pos: [-1.0, -1.0, 0.0, 1.0], + uv: [0.0, 1.0], + }, + Vertex { + pos: [-1.0, 1.0, 0.0, 1.0], + uv: [0.0, 0.0], + }, + Vertex { + pos: [1.0, 1.0, 0.0, 1.0], + uv: [1.0, 0.0], + }, + Vertex { + pos: [1.0, -1.0, 0.0, 1.0], + uv: [1.0, 1.0], + }]; let vertex_input_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, + size: std::mem::size_of_val(&vertices) as u64, usage: vk::BUFFER_USAGE_VERTEX_BUFFER_BIT, sharing_mode: vk::SharingMode::Exclusive, queue_family_index_count: 0, @@ -607,18 +625,6 @@ fn main() { memory_type_index: vertex_input_buffer_memory_index, }; let vertex_input_buffer_memory = device.allocate_memory(&vertex_buffer_allocate_info).unwrap(); - let vertices = [Vertex { - pos: [-1.0, 1.0, 0.0, 1.0], - color: [0.0, 1.0, 0.0, 1.0], - }, - Vertex { - pos: [1.0, 1.0, 0.0, 1.0], - color: [0.0, 0.0, 1.0, 1.0], - }, - Vertex { - pos: [0.0, -1.0, 0.0, 1.0], - color: [1.0, 0.0, 0.0, 1.0], - }]; let slice = device.map_memory::(vertex_input_buffer_memory, 0, vertex_input_buffer_info.size, @@ -629,9 +635,130 @@ fn main() { device.bind_buffer_memory(vertex_input_buffer, vertex_input_buffer_memory, 0).unwrap(); + let image = image::open("rust.png").unwrap().to_rgba(); + let image_dimensions = image.dimensions(); + let image_data = image.into_raw(); + let image_buffer_info = vk::BufferCreateInfo { + s_type: vk::StructureType::BufferCreateInfo, + p_next: ptr::null(), + flags: vk::BufferCreateFlags::empty(), + size: (std::mem::size_of::() * image_data.len()) as u64, + usage: vk::BUFFER_USAGE_TRANSFER_SRC_BIT, + sharing_mode: vk::SharingMode::Exclusive, + queue_family_index_count: 0, + p_queue_family_indices: ptr::null(), + }; + let image_buffer = device.create_buffer(&image_buffer_info).unwrap(); + let image_buffer_memory_req = device.get_buffer_memory_requirements(image_buffer); + let image_buffer_memory_index = find_memorytype_index(&image_buffer_memory_req, + &device_memory_properties, + vk::MEMORY_PROPERTY_HOST_VISIBLE_BIT) + .expect("Unable to find suitable memorytype for the vertex buffer."); + + let vertex_buffer_allocate_info = vk::MemoryAllocateInfo { + s_type: vk::StructureType::MemoryAllocateInfo, + p_next: ptr::null(), + allocation_size: image_buffer_memory_req.size, + memory_type_index: image_buffer_memory_index, + }; + let image_buffer_memory = device.allocate_memory(&vertex_buffer_allocate_info).unwrap(); + let image_buffer_slice = device.map_memory::(image_buffer_memory, + 0, + image_buffer_info.size, + vk::MemoryMapFlags::empty()) + .unwrap(); + image_buffer_slice.copy_from_slice(&image_data); + device.unmap_memory(image_buffer_memory); + device.bind_buffer_memory(image_buffer, image_buffer_memory, 0).unwrap(); + + let texture_create_info = vk::ImageCreateInfo { + s_type: vk::StructureType::ImageCreateInfo, + p_next: ptr::null(), + flags: Default::default(), + image_type: vk::ImageType::Type2d, + format: vk::Format::R8g8b8a8Unorm, + extent: vk::Extent3D { + width: image_dimensions.0, + height: image_dimensions.1, + depth: 1, + }, + mip_levels: 1, + array_layers: 1, + samples: vk::SAMPLE_COUNT_1_BIT, + tiling: vk::ImageTiling::Optimal, + usage: vk::IMAGE_USAGE_TRANSFER_DST_BIT | vk::IMAGE_USAGE_SAMPLED_BIT, + sharing_mode: vk::SharingMode::Exclusive, + queue_family_index_count: 0, + p_queue_family_indices: ptr::null(), + initial_layout: vk::ImageLayout::Undefined, + }; + let texture_image = device.create_image(&texture_create_info).unwrap(); + let texture_memory_req = device.get_image_memory_requirements(texture_image); + let texture_memory_index = find_memorytype_index(&texture_memory_req, + &device_memory_properties, + vk::MEMORY_PROPERTY_DEVICE_LOCAL_BIT) + .expect("Unable to find suitable memory index for depth image."); + + let texture_allocate_info = vk::MemoryAllocateInfo { + s_type: vk::StructureType::MemoryAllocateInfo, + p_next: ptr::null(), + allocation_size: texture_memory_req.size, + memory_type_index: texture_memory_index, + }; + let texture_memory = device.allocate_memory(&texture_allocate_info).unwrap(); + device.bind_image_memory(texture_image, texture_memory, 0) + .expect("Unable to bind depth image memory"); + + device.begin_command_buffer(texture_command_buffer, &command_buffer_begin_info).unwrap(); + let texture_barrier = vk::ImageMemoryBarrier { + s_type: vk::StructureType::ImageMemoryBarrier, + p_next: ptr::null(), + src_access_mask: Default::default(), + dst_access_mask: vk::ACCESS_TRANSFER_WRITE_BIT, + old_layout: vk::ImageLayout::Undefined, + new_layout: vk::ImageLayout::TransferDstOptimal, + src_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED, + dst_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED, + image: texture_image, + subresource_range: vk::ImageSubresourceRange { + aspect_mask: vk::IMAGE_ASPECT_COLOR_BIT, + base_mip_level: 0, + level_count: 1, + base_array_layer: 0, + layer_count: 1, + }, + }; + device.cmd_pipeline_barrier(texture_command_buffer, + vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT, + vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT, + vk::DependencyFlags::empty(), + &[], + &[], + &[texture_barrier]); + let wait_stage_mask = [vk::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT]; + let texture_cb_fence = device.create_fence(&fence_create_info).unwrap(); + let tex_submit_info = vk::SubmitInfo { + s_type: vk::StructureType::SubmitInfo, + p_next: ptr::null(), + wait_semaphore_count: 0, + p_wait_semaphores: ptr::null(), + signal_semaphore_count: 0, + p_signal_semaphores: ptr::null(), + p_wait_dst_stage_mask: wait_stage_mask.as_ptr(), + command_buffer_count: 1, + p_command_buffers: &texture_command_buffer, + }; + device.end_command_buffer(texture_command_buffer).unwrap(); + device.queue_submit(present_queue, &[tex_submit_info], texture_cb_fence).unwrap(); + device.wait_for_fences(&[texture_cb_fence], true, std::u64::MAX).unwrap(); + let descriptor_sizes = [vk::DescriptorPoolSize { typ: vk::DescriptorType::UniformBuffer, descriptor_count: 1, + }, + vk::DescriptorPoolSize { + typ: vk::DescriptorType::CombinedImageSampler, + descriptor_count: 1, }]; let descriptor_pool_info = vk::DescriptorPoolCreateInfo { s_type: vk::StructureType::DescriptorPoolCreateInfo, @@ -648,6 +775,13 @@ fn main() { descriptor_count: 1, stage_flags: vk::SHADER_STAGE_FRAGMENT_BIT, p_immutable_samplers: ptr::null(), + }, + vk::DescriptorSetLayoutBinding { + binding: 1, + descriptor_type: vk::DescriptorType::CombinedImageSampler, + descriptor_count: 1, + stage_flags: vk::SHADER_STAGE_FRAGMENT_BIT, + p_immutable_samplers: ptr::null(), }]; let descriptor_info = vk::DescriptorSetLayoutCreateInfo { s_type: vk::StructureType::DescriptorSetLayoutCreateInfo, @@ -758,8 +892,8 @@ fn main() { vk::VertexInputAttributeDescription { location: 1, binding: 0, - format: vk::Format::R32g32b32a32Sfloat, - offset: offset_of!(Vertex, color) as u32, + format: vk::Format::R32g32Sfloat, + offset: offset_of!(Vertex, uv) as u32, }]; let vertex_input_state_info = vk::PipelineVertexInputStateCreateInfo { s_type: vk::StructureType::PipelineVertexInputStateCreateInfo, @@ -912,7 +1046,7 @@ fn main() { let rendering_complete_semaphore = device.create_semaphore(&semaphore_create_info).unwrap(); let draw_fence = device.create_fence(&fence_create_info).unwrap(); - while !window.should_close() { + while window.should_close() { glfw.poll_events(); for (_, event) in glfw::flush_messages(&events) { handle_window_event(&mut window, event); @@ -946,15 +1080,15 @@ fn main() { device.cmd_begin_render_pass(draw_command_buffer, &render_pass_begin_info, vk::SubpassContents::Inline); + device.cmd_bind_pipeline(draw_command_buffer, + vk::PipelineBindPoint::Graphics, + graphic_pipeline); 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); device.cmd_set_viewport(draw_command_buffer, &viewports); device.cmd_set_scissor(draw_command_buffer, &scissors); device.cmd_bind_vertex_buffers(draw_command_buffer, &[vertex_input_buffer], &0); @@ -1022,8 +1156,13 @@ fn main() { device.destroy_image_view(depth_image_view); device.destroy_fence(submit_fence); device.destroy_fence(draw_fence); + device.destroy_fence(texture_cb_fence); device.free_memory(depth_image_memory); + device.free_memory(texture_memory); + device.free_memory(image_buffer_memory); device.destroy_image(depth_image); + device.destroy_image(texture_image); + device.destroy_buffer(image_buffer); for image_view in present_image_views { device.destroy_image_view(image_view); } diff --git a/examples/triangle/Cargo.lock b/examples/triangle/Cargo.lock index a6fe93d..b18e585 100644 --- a/examples/triangle/Cargo.lock +++ b/examples/triangle/Cargo.lock @@ -11,6 +11,30 @@ name = "ash" version = "0.1.0" dependencies = [ "shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -18,6 +42,11 @@ name = "bitflags" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cfg-if" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cmake" version = "0.1.17" @@ -26,6 +55,15 @@ dependencies = [ "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dbghelp-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "enum_primitive" version = "0.1.0" @@ -34,6 +72,14 @@ dependencies = [ "num 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "error-chain" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gcc" version = "0.3.32" @@ -61,6 +107,15 @@ dependencies = [ "cmake 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "0.2.1" @@ -155,6 +210,11 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rustc-demangle" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-serialize" version = "0.3.19" @@ -177,13 +237,40 @@ dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sharedlib" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "error-chain 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] +"checksum backtrace 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "346d7644f0b5f9bc73082d3b2236b69a05fd35cce0cfa3724e184e6a5c9e2a2f" +"checksum backtrace-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3602e8d8c43336088a8505fa55cae2b3884a9be29440863a11528a42f46f6bb7" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" +"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" "checksum cmake 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "dfcf5bcece56ef953b8ea042509e9dcbdfe97820b7e20d86beb53df30ed94978" +"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850" "checksum enum_primitive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f79eff5be92a4d7d5bddf7daa7d650717ea71628634efe6ca7bcda85b2183c23" +"checksum error-chain 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd5c82c815138e278b8dcdeffc49f27ea6ffb528403e9dea4194f2e3dd40b143" "checksum gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "dcb000abd6df9df4c637f75190297ebe56c1d7e66b56bbf3b4aa7aece15f61a2" "checksum glfw 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b14e68c4ccefdf293ecb65390a5761971b83fcfc54d153a5b73d438327633965" "checksum glfw-sys 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eaff144079cb22d6f17009e29e87c02f5fd6c4669093ce12b0b2faa6027f0d23" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f" "checksum libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "23e3757828fa702a20072c37ff47938e9dd331b92fac6e223d26d4b7a55f7ee2" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" @@ -196,6 +283,10 @@ dependencies = [ "checksum num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "48cdcc9ff4ae2a8296805ac15af88b3d88ce62128ded0cb74ffb63a587502a84" "checksum num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "95e58eac34596aac30ab134c8a8da9aa2dc99caa4b4b4838e6fc6e298016278f" "checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" +"checksum rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1430d286cadb237c17c885e25447c982c97113926bb579f4379c0eca8d9586dc" "checksum rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "6159e4e6e559c81bd706afe9c8fd68f547d3e851ce12e76b1de7914bab61691b" "checksum semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d5b7638a1f03815d94e88cb3b3c08e87f0db4d683ef499d1836aaf70a45623f" "checksum shared_library 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fb04126b6fcfd2710fb5b6d18f4207b6c535f2850a7e1a43bcd526d44f30a79a" +"checksum sharedlib 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5182b4b49a0ff319bc69d23ca9e9f8e40dfb3b88abb895805a54e69325a092c7" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/src/instance.rs b/src/instance.rs index ed92a2a..bf8bd61 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -77,6 +77,17 @@ impl<'r> Instance<'r> { } } + pub fn get_physical_device_format_properties(&self, + physical_device: vk::PhysicalDevice, + format: vk::Format) + -> vk::FormatProperties { + unsafe { + let mut format_prop = mem::uninitialized(); + self.instance_fn + .get_physical_device_format_properties(physical_device, format, &mut format_prop); + format_prop + } + } pub fn get_physical_device_memory_properties(&self, physical_device: vk::PhysicalDevice) -> vk::PhysicalDeviceMemoryProperties { diff --git a/src/vk.rs b/src/vk.rs index 4ed99fa..b9b59b3 100644 --- a/src/vk.rs +++ b/src/vk.rs @@ -3651,6 +3651,16 @@ macro_rules! vk_functions { )+ } + impl Clone for $struct_name { + fn clone(&self) -> Self{ + $struct_name { + $( + $name: self.$name, + )+ + } + } + } + unsafe impl Send for $struct_name {} unsafe impl Sync for $struct_name {}