diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e8a04ae2..ea95fdeb 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,27 +11,6 @@ function(buildExample EXAMPLE_NAME) if(EXISTS ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h) SET(MAIN_HEADER ${EXAMPLE_FOLDER}/${EXAMPLE_NAME}.h) ENDIF() - if(APPLE) - # SRS - Use MacPorts paths as default since the same on x86 and Apple Silicon, can override for homebrew on cmake command line - if(NOT OpenMP_omp_LIBRARY AND EXISTS /opt/local/lib/libomp/libomp.dylib) - set(OpenMP_omp_LIBRARY /opt/local/lib/libomp/libomp.dylib) - endif() - if(CMAKE_C_COMPILER_ID MATCHES "Clang\$") - set(OpenMP_C_FLAGS "-Xclang -fopenmp") - set(OpenMP_C_LIB_NAMES "omp") - if(NOT OpenMP_C_INCLUDE_DIR AND EXISTS /opt/local/include/libomp) - set(OpenMP_C_INCLUDE_DIR /opt/local/include/libomp) - endif() - endif() - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$") - set(OpenMP_CXX_FLAGS "-Xclang -fopenmp") - set(OpenMP_CXX_LIB_NAMES "omp") - if(NOT OpenMP_CXX_INCLUDE_DIR AND EXISTS /opt/local/include/libomp) - set(OpenMP_CXX_INCLUDE_DIR /opt/local/include/libomp) - endif() - endif() - endif() - find_package(OpenMP) # imgui example requires additional source files IF(${EXAMPLE_NAME} STREQUAL "imgui") file(GLOB ADD_SOURCE "../external/imgui/*.cpp") @@ -60,6 +39,27 @@ function(buildExample EXAMPLE_NAME) set_target_properties(${EXAMPLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) if(${EXAMPLE_NAME} STREQUAL "texture3d") + if(APPLE) + # SRS - Use MacPorts paths as default since the same on x86 and Apple Silicon, can override for homebrew on cmake command line + if(NOT OpenMP_omp_LIBRARY AND EXISTS /opt/local/lib/libomp/libomp.dylib) + set(OpenMP_omp_LIBRARY /opt/local/lib/libomp/libomp.dylib) + endif() + if(CMAKE_C_COMPILER_ID MATCHES "Clang\$") + set(OpenMP_C_FLAGS "-Xclang -fopenmp") + set(OpenMP_C_LIB_NAMES "omp") + if(NOT OpenMP_C_INCLUDE_DIR AND EXISTS /opt/local/include/libomp) + set(OpenMP_C_INCLUDE_DIR /opt/local/include/libomp) + endif() + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$") + set(OpenMP_CXX_FLAGS "-Xclang -fopenmp") + set(OpenMP_CXX_LIB_NAMES "omp") + if(NOT OpenMP_CXX_INCLUDE_DIR AND EXISTS /opt/local/include/libomp) + set(OpenMP_CXX_INCLUDE_DIR /opt/local/include/libomp) + endif() + endif() + endif() + find_package(OpenMP) if(OpenMP_CXX_FOUND) target_link_libraries(${EXAMPLE_NAME} OpenMP::OpenMP_CXX) endif() diff --git a/examples/computeraytracing/computeraytracing.cpp b/examples/computeraytracing/computeraytracing.cpp index 6cb83490..f9703c1a 100644 --- a/examples/computeraytracing/computeraytracing.cpp +++ b/examples/computeraytracing/computeraytracing.cpp @@ -55,6 +55,7 @@ public: glm::vec3 lookat = glm::vec3(0.0f, 0.5f, 0.0f); float fov = 10.0f; } camera; + glm::mat4 _pad; } ubo; } compute; @@ -90,6 +91,11 @@ public: camera.setTranslation(glm::vec3(0.0f, 0.0f, -4.0f)); camera.rotationSpeed = 0.0f; camera.movementSpeed = 2.5f; + +#if defined(VK_USE_PLATFORM_MACOS_MVK) + // SRS - on macOS set environment variable to ensure MoltenVK disables Metal argument buffers for this example + setenv("MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS", "0", 1); +#endif } ~VulkanExample() diff --git a/examples/descriptorindexing/descriptorindexing.cpp b/examples/descriptorindexing/descriptorindexing.cpp index d0a016ac..b174ac30 100644 --- a/examples/descriptorindexing/descriptorindexing.cpp +++ b/examples/descriptorindexing/descriptorindexing.cpp @@ -65,6 +65,12 @@ public: physicalDeviceDescriptorIndexingFeatures.descriptorBindingVariableDescriptorCount = VK_TRUE; deviceCreatepNextChain = &physicalDeviceDescriptorIndexingFeatures; + +#if defined(VK_USE_PLATFORM_MACOS_MVK) + // SRS - on macOS set environment variable to configure MoltenVK for using Metal argument buffers (needed for descriptor indexing) + // - MoltenVK supports Metal argument buffers on macOS, iOS possible in future (see https://github.com/KhronosGroup/MoltenVK/issues/1651) + setenv("MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS", "1", 1); +#endif } ~VulkanExample() @@ -120,15 +126,27 @@ public: std::uniform_int_distribution rndDist(0, static_cast(textures.size()) - 1); // Generate cubes with random per-face texture indices - const uint32_t count = 6; + const uint32_t count = 5; for (uint32_t i = 0; i < count; i++) { + // Push indices to buffer + const std::vector cubeIndices = { + 0,1,2,0,2,3, + 4,5,6,4,6,7, + 8,9,10,8,10,11, + 12,13,14,12,14,15, + 16,17,18,16,18,19, + 20,21,22,20,22,23 + }; + for (auto& index : cubeIndices) { + indices.push_back(index + static_cast(vertices.size())); + } // Get random per-Face texture indices that the shader will sample from int32_t textureIndices[6]; for (uint32_t j = 0; j < 6; j++) { textureIndices[j] = rndDist(rndEngine); } // Push vertices to buffer - float pos = 2.5f * i - (count * 2.5f / 2.0f); + float pos = 2.5f * i - (count * 2.5f / 2.0f) + 1.25f; const std::vector cube = { { { -1.0f + pos, -1.0f, 1.0f }, { 0.0f, 0.0f }, textureIndices[0] }, { { 1.0f + pos, -1.0f, 1.0f }, { 1.0f, 0.0f }, textureIndices[0] }, @@ -163,18 +181,6 @@ public: for (auto& vertex : cube) { vertices.push_back(vertex); } - // Push indices to buffer - const std::vector cubeIndices = { - 0,1,2,0,2,3, - 4,5,6,4,6,7, - 8,9,10,8,10,11, - 12,13,14,12,14,15, - 16,17,18,16,18,19, - 20,21,22,20,22,23 - }; - for (auto& index : cubeIndices) { - indices.push_back(index + static_cast(vertices.size())); - } } indexCount = static_cast(indices.size()); @@ -203,6 +209,10 @@ public: vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast(textures.size())) }; VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2); +#if defined(VK_USE_PLATFORM_MACOS_MVK) + // SRS - increase the per-stage descriptor samplers limit on macOS (maxPerStageDescriptorUpdateAfterBindSamplers > maxPerStageDescriptorSamplers) + descriptorPoolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT; +#endif VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); // Descriptor set layout @@ -228,6 +238,10 @@ public: setLayoutBindingFlags.pBindingFlags = descriptorBindingFlags.data(); VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings); +#if defined(VK_USE_PLATFORM_MACOS_MVK) + // SRS - increase the per-stage descriptor samplers limit on macOS (maxPerStageDescriptorUpdateAfterBindSamplers > maxPerStageDescriptorSamplers) + descriptorSetLayoutCI.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT; +#endif descriptorSetLayoutCI.pNext = &setLayoutBindingFlags; VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayout)); @@ -416,4 +430,4 @@ public: }; -VULKAN_EXAMPLE_MAIN() \ No newline at end of file +VULKAN_EXAMPLE_MAIN() diff --git a/examples/oit/oit.cpp b/examples/oit/oit.cpp index 77bdd68e..c6ffddf3 100644 --- a/examples/oit/oit.cpp +++ b/examples/oit/oit.cpp @@ -234,7 +234,12 @@ private: imageInfo.mipLevels = 1; imageInfo.arrayLayers = 1; imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; +#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)) + // SRS - On macOS/iOS use linear tiling for atomic image access, see https://github.com/KhronosGroup/MoltenVK/issues/1027 + imageInfo.tiling = VK_IMAGE_TILING_LINEAR; +#else imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; +#endif imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT; VK_CHECK_RESULT(vkCreateImage(device, &imageInfo, nullptr, &geometryPass.headIndex.image)); diff --git a/examples/specializationconstants/specializationconstants.cpp b/examples/specializationconstants/specializationconstants.cpp index d0692d03..c32aef09 100644 --- a/examples/specializationconstants/specializationconstants.cpp +++ b/examples/specializationconstants/specializationconstants.cpp @@ -44,6 +44,11 @@ public: camera.setPerspective(60.0f, ((float)width / 3.0f) / (float)height, 0.1f, 512.0f); camera.setRotation(glm::vec3(-40.0f, -90.0f, 0.0f)); camera.setTranslation(glm::vec3(0.0f, 0.0f, -2.0f)); + +#if defined(VK_USE_PLATFORM_MACOS_MVK) + // SRS - on macOS set environment variable to ensure MoltenVK disables Metal argument buffers for this example + setenv("MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS", "0", 1); +#endif } ~VulkanExample() diff --git a/examples/ssao/ssao.cpp b/examples/ssao/ssao.cpp index cb87d6fa..3cda3ced 100644 --- a/examples/ssao/ssao.cpp +++ b/examples/ssao/ssao.cpp @@ -11,7 +11,7 @@ #define ENABLE_VALIDATION false -#define SSAO_KERNEL_SIZE 32 +#define SSAO_KERNEL_SIZE 64 #define SSAO_RADIUS 0.3f #if defined(__ANDROID__) diff --git a/examples/texturearray/texturearray.cpp b/examples/texturearray/texturearray.cpp index 921addea..de997fcc 100644 --- a/examples/texturearray/texturearray.cpp +++ b/examples/texturearray/texturearray.cpp @@ -12,6 +12,8 @@ #define ENABLE_VALIDATION false +#define MAX_LAYERS 8 + // Vertex layout for this example struct Vertex { float pos[3]; @@ -120,6 +122,7 @@ public: textureArray.width = ktxTexture->baseWidth; textureArray.height = ktxTexture->baseHeight; layerCount = ktxTexture->numLayers; + assert(layerCount <= MAX_LAYERS); ktx_uint8_t *ktxTextureData = ktxTexture_GetData(ktxTexture); ktx_size_t ktxTextureSize = ktxTexture_GetSize(ktxTexture); @@ -475,7 +478,7 @@ public: { uboVS.instance = new UboInstanceData[layerCount]; - uint32_t uboSize = sizeof(uboVS.matrices) + (layerCount * sizeof(UboInstanceData)); + uint32_t uboSize = sizeof(uboVS.matrices) + (MAX_LAYERS * sizeof(UboInstanceData)); // Vertex shader uniform buffer block VK_CHECK_RESULT(vulkanDevice->createBuffer( diff --git a/xcode/examples.h b/xcode/examples.h index c6107e4f..15787981 100644 --- a/xcode/examples.h +++ b/xcode/examples.h @@ -191,7 +191,7 @@ # include "../examples/vertexattributes/vertexattributes.cpp" #endif -// Does not run. MoltenVK/Metal does not support stores and atomic operations in the fragment stage. +// Runs, but some Apple GPUs may not support stores and atomic operations in the fragment stage. #ifdef MVK_oit # include "../examples/oit/oit.cpp" #endif @@ -222,7 +222,7 @@ # include "../examples/pushdescriptors/pushdescriptors.cpp" #endif -// Does not run. Shader compilation fails with MoltenVK. +// Runs on macOS 11.0 or later with Metal argument buffers enabled. Not yet supported on iOS. #ifdef MVK_descriptorindexing # include "../examples/descriptorindexing/descriptorindexing.cpp" #endif @@ -358,7 +358,7 @@ # include "../examples/graphicspipelinelibrary/graphicspipelinelibrary.cpp" #endif -// Does not run yet. Requires VK_KHR_dynamic_rendering (under development in MoltenVK) +// Runs on MoltenVK 1.1.10 (SDK 1.3.216) or later. Requires VK_KHR_dynamic_rendering and dependencies. #ifdef MVK_dynamicrendering # include "../examples/dynamicrendering/dynamicrendering.cpp" #endif