From 65e1f8ac31322d83686372c552c394070473093c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Wed, 13 Feb 2019 12:17:22 +0100 Subject: [PATCH] Add sample 11_InitShaders (#288) + add submodule glslang --- .gitmodules | 3 + CMakeLists.txt | 1 + glslang | 1 + samples/11_InitShaders/11_InitShaders.cpp | 321 ++++++++++++++++++++++ samples/11_InitShaders/CMakeLists.txt | 37 +++ 5 files changed, 363 insertions(+) create mode 160000 glslang create mode 100644 samples/11_InitShaders/11_InitShaders.cpp create mode 100644 samples/11_InitShaders/CMakeLists.txt diff --git a/.gitmodules b/.gitmodules index 9066fe1..b57457a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "glm"] path = glm url = https://github.com/g-truc/glm.git +[submodule "glslang"] + path = glslang + url = https://github.com/KhronosGroup/glslang.git [submodule "tinyxml2"] path = tinyxml2 url = https://github.com/leethomason/tinyxml2.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d9f4aa..8f11117 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ target_include_directories(VulkanHppGenerator PRIVATE "${CMAKE_SOURCE_DIR}/tinyx option (SAMPLES_BUILD OFF) if (SAMPLES_BUILD) + add_subdirectory(glslang) add_subdirectory(samples) endif (SAMPLES_BUILD) diff --git a/glslang b/glslang new file mode 160000 index 0000000..05d12a9 --- /dev/null +++ b/glslang @@ -0,0 +1 @@ +Subproject commit 05d12a9461dd0a76053bdd42f062a37a10d56afb diff --git a/samples/11_InitShaders/11_InitShaders.cpp b/samples/11_InitShaders/11_InitShaders.cpp new file mode 100644 index 0000000..41affcf --- /dev/null +++ b/samples/11_InitShaders/11_InitShaders.cpp @@ -0,0 +1,321 @@ +// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// VulkanHpp Samples : 11_InitShaders +// Initialize vertex and fragment shaders + +#include +#include "vulkan/vulkan.hpp" +#include "SPIRV/GlslangToSpv.h" + +static char const* AppName = "11_InitShaders"; +static char const* EngineName = "Vulkan.hpp"; + +static std::vector getDeviceExtensions() +{ + std::vector extensions; + + extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + + return extensions; +} + +static std::vector getInstanceExtensions() +{ + std::vector extensions; + + extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); +#if defined(VK_USE_PLATFORM_ANDROID_KHR) + extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_IOS_MVK) + extensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_MACOS_MVK) + extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_MIR_KHR) + extensions.push_back(VK_KHR_MIR_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_VI_NN) + extensions.push_back(VK_NN_VI_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) + extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_WIN32_KHR) + extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_XCB_KHR) + extensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_XLIB_KHR) + extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); +#elif defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT) + extensions.push_back(VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME); +#endif + + return extensions; +} + +EShLanguage translateShaderStage(vk::ShaderStageFlagBits stage) +{ + switch (stage) + { + case vk::ShaderStageFlagBits::eVertex: return EShLangVertex; + case vk::ShaderStageFlagBits::eTessellationControl: return EShLangTessControl; + case vk::ShaderStageFlagBits::eTessellationEvaluation: return EShLangTessEvaluation; + case vk::ShaderStageFlagBits::eGeometry: return EShLangGeometry; + case vk::ShaderStageFlagBits::eFragment: return EShLangFragment; + case vk::ShaderStageFlagBits::eCompute: return EShLangCompute; + default: + assert(false && "Unknown shader stage"); + return EShLangVertex; + } +} + +void init(TBuiltInResource & resource) +{ + resource.maxLights = 32; + resource.maxClipPlanes = 6; + resource.maxTextureUnits = 32; + resource.maxTextureCoords = 32; + resource.maxVertexAttribs = 64; + resource.maxVertexUniformComponents = 4096; + resource.maxVaryingFloats = 64; + resource.maxVertexTextureImageUnits = 32; + resource.maxCombinedTextureImageUnits = 80; + resource.maxTextureImageUnits = 32; + resource.maxFragmentUniformComponents = 4096; + resource.maxDrawBuffers = 32; + resource.maxVertexUniformVectors = 128; + resource.maxVaryingVectors = 8; + resource.maxFragmentUniformVectors = 16; + resource.maxVertexOutputVectors = 16; + resource.maxFragmentInputVectors = 15; + resource.minProgramTexelOffset = -8; + resource.maxProgramTexelOffset = 7; + resource.maxClipDistances = 8; + resource.maxComputeWorkGroupCountX = 65535; + resource.maxComputeWorkGroupCountY = 65535; + resource.maxComputeWorkGroupCountZ = 65535; + resource.maxComputeWorkGroupSizeX = 1024; + resource.maxComputeWorkGroupSizeY = 1024; + resource.maxComputeWorkGroupSizeZ = 64; + resource.maxComputeUniformComponents = 1024; + resource.maxComputeTextureImageUnits = 16; + resource.maxComputeImageUniforms = 8; + resource.maxComputeAtomicCounters = 8; + resource.maxComputeAtomicCounterBuffers = 1; + resource.maxVaryingComponents = 60; + resource.maxVertexOutputComponents = 64; + resource.maxGeometryInputComponents = 64; + resource.maxGeometryOutputComponents = 128; + resource.maxFragmentInputComponents = 128; + resource.maxImageUnits = 8; + resource.maxCombinedImageUnitsAndFragmentOutputs = 8; + resource.maxCombinedShaderOutputResources = 8; + resource.maxImageSamples = 0; + resource.maxVertexImageUniforms = 0; + resource.maxTessControlImageUniforms = 0; + resource.maxTessEvaluationImageUniforms = 0; + resource.maxGeometryImageUniforms = 0; + resource.maxFragmentImageUniforms = 8; + resource.maxCombinedImageUniforms = 8; + resource.maxGeometryTextureImageUnits = 16; + resource.maxGeometryOutputVertices = 256; + resource.maxGeometryTotalOutputComponents = 1024; + resource.maxGeometryUniformComponents = 1024; + resource.maxGeometryVaryingComponents = 64; + resource.maxTessControlInputComponents = 128; + resource.maxTessControlOutputComponents = 128; + resource.maxTessControlTextureImageUnits = 16; + resource.maxTessControlUniformComponents = 1024; + resource.maxTessControlTotalOutputComponents = 4096; + resource.maxTessEvaluationInputComponents = 128; + resource.maxTessEvaluationOutputComponents = 128; + resource.maxTessEvaluationTextureImageUnits = 16; + resource.maxTessEvaluationUniformComponents = 1024; + resource.maxTessPatchComponents = 120; + resource.maxPatchVertices = 32; + resource.maxTessGenLevel = 64; + resource.maxViewports = 16; + resource.maxVertexAtomicCounters = 0; + resource.maxTessControlAtomicCounters = 0; + resource.maxTessEvaluationAtomicCounters = 0; + resource.maxGeometryAtomicCounters = 0; + resource.maxFragmentAtomicCounters = 8; + resource.maxCombinedAtomicCounters = 8; + resource.maxAtomicCounterBindings = 1; + resource.maxVertexAtomicCounterBuffers = 0; + resource.maxTessControlAtomicCounterBuffers = 0; + resource.maxTessEvaluationAtomicCounterBuffers = 0; + resource.maxGeometryAtomicCounterBuffers = 0; + resource.maxFragmentAtomicCounterBuffers = 1; + resource.maxCombinedAtomicCounterBuffers = 1; + resource.maxAtomicCounterBufferSize = 16384; + resource.maxTransformFeedbackBuffers = 4; + resource.maxTransformFeedbackInterleavedComponents = 64; + resource.maxCullDistances = 8; + resource.maxCombinedClipAndCullDistances = 8; + resource.maxSamples = 4; + resource.limits.nonInductiveForLoops = 1; + resource.limits.whileLoops = 1; + resource.limits.doWhileLoops = 1; + resource.limits.generalUniformIndexing = 1; + resource.limits.generalAttributeMatrixVectorIndexing = 1; + resource.limits.generalVaryingIndexing = 1; + resource.limits.generalSamplerIndexing = 1; + resource.limits.generalVariableIndexing = 1; + resource.limits.generalConstantMatrixVectorIndexing = 1; +} + +bool GLSLtoSPV(const vk::ShaderStageFlagBits shaderType, std::string const& glslShader, std::vector &spvShader) +{ + EShLanguage stage = translateShaderStage(shaderType); + + const char *shaderStrings[1]; + shaderStrings[0] = glslShader.data(); + + glslang::TShader shader(stage); + shader.setStrings(shaderStrings, 1); + + TBuiltInResource resource; + init(resource); + + // Enable SPIR-V and Vulkan rules when parsing GLSL + EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules); + + if (!shader.parse(&resource, 100, false, messages)) + { + puts(shader.getInfoLog()); + puts(shader.getInfoDebugLog()); + return false; // something didn't work + } + + glslang::TProgram program; + program.addShader(&shader); + + // + // Program-level processing... + // + + if (!program.link(messages)) + { + puts(shader.getInfoLog()); + puts(shader.getInfoDebugLog()); + fflush(stdout); + return false; + } + + glslang::GlslangToSpv(*program.getIntermediate(stage), spvShader); + return true; +} + +int main(int /*argc*/, char * /*argv[]*/) +{ + try + { + // create an instance + vk::ApplicationInfo appInfo(AppName, 1, EngineName, 1, VK_API_VERSION_1_1); + std::vector instanceExtensions = getInstanceExtensions(); + vk::InstanceCreateInfo instanceCreateInfo({}, &appInfo, 0, nullptr, static_cast(instanceExtensions.size()), instanceExtensions.data()); + vk::UniqueInstance instance = vk::createInstanceUnique(instanceCreateInfo); + + // get the physical devices + std::vector physicalDevices = instance->enumeratePhysicalDevices(); + assert(!physicalDevices.empty()); + + // determine a queueFamilyIndex that supports graphics + std::vector queueFamilyProperties = physicalDevices[0].getQueueFamilyProperties(); + size_t graphicsQueueFamilyIndex = std::distance(queueFamilyProperties.begin(), + std::find_if(queueFamilyProperties.begin(), + queueFamilyProperties.end(), + [](vk::QueueFamilyProperties const& qfp) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; })); + + // create a device + float queuePriority = 0.0f; + vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, static_cast(graphicsQueueFamilyIndex), 1, &queuePriority); + std::vector deviceExtensionNames = getDeviceExtensions(); + vk::UniqueDevice device = physicalDevices[0].createDeviceUnique(vk::DeviceCreateInfo({}, 1, &deviceQueueCreateInfo, 0, nullptr, static_cast(deviceExtensionNames.size()), deviceExtensionNames.data())); + + /* VULKAN_HPP_KEY_START */ + + static const std::string vertexShaderText = R"( +#version 400 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (std140, binding = 0) uniform bufferVals +{ + mat4 mvp; +} myBufferVals; + +layout (location = 0) in vec4 pos; +layout (location = 1) in vec4 inColor; +layout (location = 0) out vec4 outColor; + +void main() +{ + outColor = inColor; + gl_Position = myBufferVals.mvp * pos; +} +)"; + + static const std::string fragmentShaderText = R"( +#version 400 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec4 color; +layout (location = 0) out vec4 outColor; + +void main() +{ + outColor = color; +} +)"; + + glslang::InitializeProcess(); + + std::vector vertexShaderSPV; + bool ok = GLSLtoSPV(vk::ShaderStageFlagBits::eVertex, vertexShaderText, vertexShaderSPV); + assert(ok); + + vk::ShaderModuleCreateInfo vertexShaderModuleCreateInfo(vk::ShaderModuleCreateFlags(), vertexShaderSPV.size() * sizeof(unsigned int), vertexShaderSPV.data()); + vk::UniqueShaderModule vertexShaderModule = device->createShaderModuleUnique(vertexShaderModuleCreateInfo); + + std::vector fragmentShaderSPV; + ok = GLSLtoSPV(vk::ShaderStageFlagBits::eFragment, fragmentShaderText, fragmentShaderSPV); + assert(ok); + + vk::ShaderModuleCreateInfo fragmentShaderModuleCreateInfo(vk::ShaderModuleCreateFlags(), fragmentShaderSPV.size() * sizeof(unsigned int), fragmentShaderSPV.data()); + vk::UniqueShaderModule fragmentShaderModule = device->createShaderModuleUnique(fragmentShaderModuleCreateInfo); + + glslang::FinalizeProcess(); + + // Note: No need to explicitly destroy the ShaderModules, as the corresponding destroy + // functions are called by the destructor of the UniqueShaderModule on leaving this scope. + + /* VULKAN_HPP_KEY_END */ + } + catch (vk::SystemError err) + { + std::cout << "vk::SystemError: " << err.what() << std::endl; + exit(-1); + } + catch (std::runtime_error err) + { + std::cout << "std::runtime_error: " << err.what() << std::endl; + exit(-1); + } + catch (...) + { + std::cout << "unknown error\n"; + exit(-1); + } + return 0; +} diff --git a/samples/11_InitShaders/CMakeLists.txt b/samples/11_InitShaders/CMakeLists.txt new file mode 100644 index 0000000..ea19349 --- /dev/null +++ b/samples/11_InitShaders/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.2) + +project(11_InitShaders) + +set(HEADERS +) + +set(SOURCES + 11_InitShaders.cpp +) + +source_group(headers FILES ${HEADERS}) +source_group(sources FILES ${SOURCES}) + +add_executable(11_InitShaders + ${HEADERS} + ${SOURCES} +) + +set_target_properties(11_InitShaders PROPERTIES FOLDER "Samples") +target_include_directories(11_InitShaders PUBLIC ${CMAKE_SOURCE_DIR}/glslang) +target_link_libraries(11_InitShaders PUBLIC glslang SPIRV "$ENV{VULKAN_SDK}/Lib/vulkan-1.lib" +)