diff --git a/CMakeLists.txt b/CMakeLists.txt index f0cb9690..a58b0227 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ cmake_minimum_required(VERSION 3.4 FATAL_ERROR) -cmake_policy(VERSION 2.8) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") set(NAME vulkanExamples) diff --git a/README.md b/README.md index 421d1963..b25d671a 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ Note that some examples require specific device features, and if you are on a mu ## Shaders -Vulkan consumes shaders in an intermediate representation called SPIR-V. This makes it possible to use different shader languages by compiling them to that bytecode format. The primary shader language used here is [GLSL](data/shaders/glsl) but thanks to an external contribution you'll also find [HLSL](data/shaders/hlsl) shader sources. +Vulkan consumes shaders in an intermediate representation called SPIR-V. This makes it possible to use different shader languages by compiling them to that bytecode format. The primary shader language used here is [GLSL](data/shaders/glsl) but most samples also come with [HLSL](data/shaders/hlsl) shader sources. ## A note on synchronization diff --git a/examples/descriptorsets/descriptorsets.cpp b/examples/descriptorsets/descriptorsets.cpp index a5389937..de55c75d 100644 --- a/examples/descriptorsets/descriptorsets.cpp +++ b/examples/descriptorsets/descriptorsets.cpp @@ -3,7 +3,7 @@ * * Relevant code parts are marked with [POI] * -* Copyright (C) 2018 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2018-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -191,11 +191,11 @@ public: std::array descriptorPoolSizes{}; - // Uniform buffers : 1 for scene and 1 per object (scene and local matrices) + // Uniform buffers : 1 per object descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptorPoolSizes[0].descriptorCount = 1 + static_cast(cubes.size()); + descriptorPoolSizes[0].descriptorCount = static_cast(cubes.size()); - // Combined image samples : 1 per mesh texture + // Combined image samples : 1 per object texture descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; descriptorPoolSizes[1].descriptorCount = static_cast(cubes.size()); diff --git a/examples/dynamicrendering/dynamicrendering.cpp b/examples/dynamicrendering/dynamicrendering.cpp index acbb9561..e7845217 100644 --- a/examples/dynamicrendering/dynamicrendering.cpp +++ b/examples/dynamicrendering/dynamicrendering.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - Using VK_KHR_dynamic_rendering for rendering without framebuffers and render passes (wip) * - * Copyright (C) 2022 by Sascha Willems - www.saschawillems.de + * Copyright (C) 2022-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -119,7 +119,7 @@ public: 0, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VkImageSubresourceRange{ VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, 0, 1, 0, 1 }); @@ -128,7 +128,7 @@ public: VkRenderingAttachmentInfoKHR colorAttachment{}; colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR; colorAttachment.imageView = swapChain.buffers[i].view; - colorAttachment.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR; + colorAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.clearValue.color = { 0.0f,0.0f,0.0f,0.0f }; diff --git a/examples/shaderobjects/shaderobjects.cpp b/examples/shaderobjects/shaderobjects.cpp index 2e973100..3de2d2d9 100644 --- a/examples/shaderobjects/shaderobjects.cpp +++ b/examples/shaderobjects/shaderobjects.cpp @@ -77,6 +77,8 @@ public: enabledDeviceExtensions.push_back(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME); enabledDeviceExtensions.push_back(VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME); + enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); + enabledShaderObjectFeaturesEXT.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_OBJECT_FEATURES_EXT; enabledShaderObjectFeaturesEXT.shaderObject = VK_TRUE; @@ -281,7 +283,7 @@ public: VkRenderingAttachmentInfoKHR colorAttachment{}; colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR; colorAttachment.imageView = swapChain.buffers[i].view; - colorAttachment.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR; + colorAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.clearValue.color = { 0.0f,0.0f,0.0f,0.0f }; @@ -327,6 +329,7 @@ public: vertexInputBinding.binding = 0; vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; vertexInputBinding.stride = sizeof(vkglTF::Vertex); + vertexInputBinding.divisor = 1; std::vector vertexAttributes = { { VK_STRUCTURE_TYPE_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION_2_EXT, nullptr, 0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(vkglTF::Vertex, pos) }, @@ -344,7 +347,8 @@ public: vkCmdBindShadersEXT(drawCmdBuffers[i], 2, stages, shaders); scene.draw(drawCmdBuffers[i]); - drawUI(drawCmdBuffers[i]); + // @todo: Currently disabled, the UI needs to be adopated to work with shader objects + // drawUI(drawCmdBuffers[i]); // End dynamic rendering vkCmdEndRenderingKHR(drawCmdBuffers[i]); diff --git a/examples/terraintessellation/terraintessellation.cpp b/examples/terraintessellation/terraintessellation.cpp index 30bcbd1e..0eab2db3 100644 --- a/examples/terraintessellation/terraintessellation.cpp +++ b/examples/terraintessellation/terraintessellation.cpp @@ -1,7 +1,7 @@ /* * Vulkan Example - Dynamic terrain tessellation * -* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de +* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ @@ -105,7 +105,7 @@ public: camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f); camera.setRotation(glm::vec3(-12.0f, 159.0f, 0.0f)); camera.setTranslation(glm::vec3(18.0f, 22.5f, 57.5f)); - camera.movementSpeed = 7.5f; + camera.movementSpeed = 10.0f; } ~VulkanExample() @@ -420,7 +420,7 @@ public: vertices[index].pos[0] = x * wx + wx / 2.0f - (float)PATCH_SIZE * wx / 2.0f; vertices[index].pos[1] = 0.0f; vertices[index].pos[2] = y * wy + wy / 2.0f - (float)PATCH_SIZE * wy / 2.0f; - vertices[index].uv = glm::vec2((float)x / PATCH_SIZE, (float)y / PATCH_SIZE) * UV_SCALE; + vertices[index].uv = glm::vec2((float)x / (PATCH_SIZE - 1), (float)y / (PATCH_SIZE - 1)) * UV_SCALE; } } diff --git a/shaders/hlsl/meshshader/meshshader.frag b/shaders/hlsl/meshshader/meshshader.frag new file mode 100644 index 00000000..d4e1e8de --- /dev/null +++ b/shaders/hlsl/meshshader/meshshader.frag @@ -0,0 +1,15 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSOutput +{ +[[vk::location(0)]] float4 color : COLOR0; +}; + +float4 main(VSOutput input) : SV_TARGET +{ + return float4(input.color); +} \ No newline at end of file diff --git a/shaders/hlsl/meshshader/meshshader.frag.spv b/shaders/hlsl/meshshader/meshshader.frag.spv new file mode 100644 index 00000000..93ce5e69 Binary files /dev/null and b/shaders/hlsl/meshshader/meshshader.frag.spv differ diff --git a/shaders/hlsl/meshshader/meshshader.mesh b/shaders/hlsl/meshshader/meshshader.mesh new file mode 100644 index 00000000..840e009b --- /dev/null +++ b/shaders/hlsl/meshshader/meshshader.mesh @@ -0,0 +1,50 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct UBO +{ + float4x4 projection; + float4x4 model; + float4x4 view; +}; + +cbuffer ubo : register(b0) { UBO ubo; } + +struct VertexOutput +{ + float4 position: SV_Position; + float4 color: COLOR0; +}; + +static const float4 positions[3] = { + float4( 0.0, -1.0, 0.0, 1.0), + float4(-1.0, 1.0, 0.0, 1.0), + float4( 1.0, 1.0, 0.0, 1.0) +}; + +static const float4 colors[3] = { + float4(0.0, 1.0, 0.0, 1.0), + float4(0.0, 0.0, 1.0, 1.0), + float4(1.0, 0.0, 0.0, 1.0) +}; + +[outputtopology("triangle")] +[numthreads(1, 1, 1)] +void main(out indices uint3 triangles[1], out vertices VertexOutput vertices[3], uint3 DispatchThreadID : SV_DispatchThreadID) +{ + float4x4 mvp = mul(ubo.projection, mul(ubo.view, ubo.model)); + + float4 offset = float4(0.0, 0.0, (float)DispatchThreadID, 0.0); + + SetMeshOutputCounts(3, 1); + for (uint i = 0; i < 3; i++) { + vertices[i].position = mul(mvp, positions[i] + offset); + vertices[i].color = colors[i]; + } + + SetMeshOutputCounts(3, 1); + triangles[0] = uint3(0, 1, 2); +} diff --git a/shaders/hlsl/meshshader/meshshader.mesh.spv b/shaders/hlsl/meshshader/meshshader.mesh.spv new file mode 100644 index 00000000..7e7eb60d Binary files /dev/null and b/shaders/hlsl/meshshader/meshshader.mesh.spv differ diff --git a/shaders/hlsl/meshshader/meshshader.task b/shaders/hlsl/meshshader/meshshader.task new file mode 100644 index 00000000..3b3b524e --- /dev/null +++ b/shaders/hlsl/meshshader/meshshader.task @@ -0,0 +1,19 @@ +/* Copyright (c) 2023, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct DummyPayLoad +{ + uint dummyData; +}; + +// We don't use pay loads in this sample, but the fn call requires one +groupshared DummyPayLoad dummyPayLoad; + +[numthreads(1,1,1)] +void main() +{ + DispatchMesh(3, 1, 1, dummyPayLoad); +} diff --git a/shaders/hlsl/meshshader/meshshader.task.spv b/shaders/hlsl/meshshader/meshshader.task.spv new file mode 100644 index 00000000..97278132 Binary files /dev/null and b/shaders/hlsl/meshshader/meshshader.task.spv differ