diff --git a/data/shaders/gltfskinning/mesh.frag b/data/shaders/gltfskinning/mesh.frag new file mode 100644 index 00000000..6ff22d7b --- /dev/null +++ b/data/shaders/gltfskinning/mesh.frag @@ -0,0 +1,28 @@ +#version 450 + +layout (set = 1, binding = 0) uniform sampler2D samplerColorMap; + +layout (location = 0) in vec3 inNormal; +layout (location = 1) in vec3 inColor; +layout (location = 2) in vec2 inUV; +layout (location = 3) in vec3 inViewVec; +layout (location = 4) in vec3 inLightVec; + +layout (location = 5) in vec4 inColVis; + +layout (location = 0) out vec4 outFragColor; + +void main() +{ + vec4 color = texture(samplerColorMap, inUV) * vec4(inColor, 1.0); + + vec3 N = normalize(inNormal); + vec3 L = normalize(inLightVec); + vec3 V = normalize(inViewVec); + vec3 R = reflect(-L, N); + vec3 diffuse = max(dot(N, L), 0.15) * inColor; + vec3 specular = pow(max(dot(R, V), 0.0), 16.0) * vec3(0.75); + outFragColor = vec4(diffuse * color.rgb + specular, 1.0); + +// outFragColor = inColVis; +} \ No newline at end of file diff --git a/data/shaders/gltfskinning/mesh.vert b/data/shaders/gltfskinning/mesh.vert new file mode 100644 index 00000000..50115434 --- /dev/null +++ b/data/shaders/gltfskinning/mesh.vert @@ -0,0 +1,55 @@ +#version 450 + +layout (location = 0) in vec3 inPos; +layout (location = 1) in vec3 inNormal; +layout (location = 2) in vec2 inUV; +layout (location = 3) in vec3 inColor; +layout (location = 4) in vec4 inJointIndices; +layout (location = 5) in vec4 inJointWeights; + +layout (set = 0, binding = 0) uniform UBOScene +{ + mat4 projection; + mat4 view; + vec4 lightPos; +} uboScene; + +layout(push_constant) uniform PushConsts { + mat4 model; +} primitive; + +layout(std430, set = 2, binding = 0) readonly buffer JointMatrices { + mat4 jointMatrices[]; +}; + +layout (location = 0) out vec3 outNormal; +layout (location = 1) out vec3 outColor; +layout (location = 2) out vec2 outUV; +layout (location = 3) out vec3 outViewVec; +layout (location = 4) out vec3 outLightVec; + +layout (location = 5) out vec4 outColVis; + +void main() +{ + outNormal = inNormal; + outColor = inColor; + outUV = inUV; + + // Calculated skinned matrix from vertice's weights and joint indices + mat4 skinMat = + inJointWeights.x * jointMatrices[int(inJointIndices.x)] + + inJointWeights.y * jointMatrices[int(inJointIndices.y)] + + inJointWeights.z * jointMatrices[int(inJointIndices.z)] + + inJointWeights.w * jointMatrices[int(inJointIndices.w)]; + + gl_Position = uboScene.projection * uboScene.view * primitive.model * skinMat * vec4(inPos.xyz, 1.0); + + vec4 pos = uboScene.view * vec4(inPos, 1.0); + outNormal = mat3(uboScene.view) * inNormal; + vec3 lPos = mat3(uboScene.view) * uboScene.lightPos.xyz; + outLightVec = lPos - pos.xyz; + outViewVec = -pos.xyz; + + outColVis = inJointWeights; +} \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a53c834c..0f9135ee 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -65,6 +65,7 @@ set(EXAMPLES gears geometryshader gltfscene + gltfskinning hdr imgui indirectdraw diff --git a/examples/gltfskinning/gltfskinning.cpp b/examples/gltfskinning/gltfskinning.cpp new file mode 100644 index 00000000..6d9349e9 --- /dev/null +++ b/examples/gltfskinning/gltfskinning.cpp @@ -0,0 +1,1005 @@ +/* +* Vulkan Example - glTF skinned animation +* +* Copyright (C) 2020 by Sascha Willems - www.saschawillems.de +* +* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +*/ + +/* + * Shows how to load and display a simple scene from a glTF file + * Note that this isn't a complete glTF loader and only basic functions are shown here + * This means no complex materials, no animations, no skins, etc. + * For details on how glTF 2.0 works, see the official spec at https://github.com/KhronosGroup/glTF/tree/master/specification/2.0 + * + * Other samples will load models using a dedicated model loader with more features (see base/VulkanglTFModel.hpp) + * + * If you are looking for a complete glTF implementation, check out https://github.com/SaschaWillems/Vulkan-glTF-PBR/ + */ + + // @todo: add link to https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_020_Skins.md + +#include +#include +#include +#include +#include + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include +#include + +#define TINYGLTF_IMPLEMENTATION +#define STB_IMAGE_IMPLEMENTATION +#define TINYGLTF_NO_STB_IMAGE_WRITE +#ifdef VK_USE_PLATFORM_ANDROID_KHR +#define TINYGLTF_ANDROID_LOAD_FROM_ASSETS +#endif +#include "tiny_gltf.h" + +#include +#include "vulkanexamplebase.h" +#include "VulkanTexture.hpp" + +#define ENABLE_VALIDATION false + +// Contains everything required to render a glTF model in Vulkan +// This class is heavily simplified (compared to glTF's feature set) but retains the basic glTF structure +class VulkanglTFModel +{ +public: + // The class requires some Vulkan objects so it can create it's own resources + vks::VulkanDevice* vulkanDevice; + VkQueue copyQueue; + + // The vertex layout for the samples' model + struct Vertex { + glm::vec3 pos; + glm::vec3 normal; + glm::vec2 uv; + glm::vec3 color; + // Contains indices of the joints that effect this vertex + glm::vec4 jointIndices; + // Contains the weights that define how strongly this vertex is affected by above joints + glm::vec4 jointWeights; + }; + + // Single vertex buffer for all primitives + struct { + VkBuffer buffer; + VkDeviceMemory memory; + } vertices; + + // Single index buffer for all primitives + struct { + int count; + VkBuffer buffer; + VkDeviceMemory memory; + } indices; + + // The following structures roughly represent the glTF scene structure + // To keep things simple, they only contain those properties that are required for this sample + struct Node; + + // A primitive contains the data for a single draw call + struct Primitive { + uint32_t firstIndex; + uint32_t indexCount; + int32_t materialIndex; + }; + + // Contains the node's (optional) geometry and can be made up of an arbitrary number of primitives + struct Mesh { + std::vector primitives; + + // POI: @todo: document + struct ShaderData { + vks::Buffer buffer; + struct Values { + // @todo: make const + glm::mat4 jointMatrix[16]{}; + float jointcount{ 0 }; + } values; + } shaderData; + VkDescriptorSet descriptorSet; + }; + + // A skin contains the joints and matrices applied during vertex skinning + struct Skin { + std::string name; + Node* skeletonRoot = nullptr; + std::vector inverseBindMatrices; + std::vector joints; + // POI: Store joint matrices in an SSBO + // @todo: proper comment + std::vector jointMatrices; + vks::Buffer ssbo; + VkDescriptorSet descriptorSet; + }; + + // A node represents an object in the glTF scene graph + struct Node { + Node* parent; + uint32_t index; + std::vector children; + Mesh mesh; + // Store matrix components as they may be altered by animations + glm::vec3 translation{}; + glm::vec3 scale{ 1.0f }; + glm::quat rotation{}; + // glTF stores the index of the skin for a node + int32_t skin = -1; + glm::mat4 matrix; + // Get the current local matrix based on translation, rotation and scale, which can all be altered by animation + glm::mat4 getLocalMatrix() { + return glm::translate(glm::mat4(1.0f), translation) * glm::mat4(rotation) * glm::scale(glm::mat4(1.0f), scale) * matrix; + } + + }; + + // A glTF material stores information in e.g. the exture that is attached to it and colors + struct Material { + glm::vec4 baseColorFactor = glm::vec4(1.0f); + uint32_t baseColorTextureIndex; + }; + + // Contains the texture for a single glTF image + // Images may be reused by texture objects and are as such separted + struct Image { + vks::Texture2D texture; + // We also store (and create) a descriptor set that's used to access this texture from the fragment shader + VkDescriptorSet descriptorSet; + }; + + // A glTF texture stores a reference to the image and a sampler + // In this sample, we are only interested in the image + struct Texture { + int32_t imageIndex; + }; + + /* + Model data + */ + std::vector images; + std::vector textures; + std::vector materials; + std::vector nodes; + std::vector skins; + + // POI: @todo: document + struct MeshData { + // @todo: make const + glm::mat4 jointMatrix[16]{}; + float jointcount{ 0 }; + }; + struct ShaderData { + vks::Buffer buffer; + } shaderData; + VkDescriptorSet descriptorSet; + std::vector meshdata; + + ~VulkanglTFModel() + { + // Release all Vulkan resources allocated for the model + vkDestroyBuffer(vulkanDevice->logicalDevice, vertices.buffer, nullptr); + vkFreeMemory(vulkanDevice->logicalDevice, vertices.memory, nullptr); + vkDestroyBuffer(vulkanDevice->logicalDevice, indices.buffer, nullptr); + vkFreeMemory(vulkanDevice->logicalDevice, indices.memory, nullptr); + for (Image image : images) { + vkDestroyImageView(vulkanDevice->logicalDevice, image.texture.view, nullptr); + vkDestroyImage(vulkanDevice->logicalDevice, image.texture.image, nullptr); + vkDestroySampler(vulkanDevice->logicalDevice, image.texture.sampler, nullptr); + vkFreeMemory(vulkanDevice->logicalDevice, image.texture.deviceMemory, nullptr); + } + } + + /* + glTF loading functions + + The following functions take a glTF input model loaded via tinyglTF and convert all required data into our own structure + */ + + void loadImages(tinygltf::Model& input) + { + // Images can be stored inside the glTF (which is the case for the sample model), so instead of directly + // loading them from disk, we fetch them from the glTF loader and upload the buffers + images.resize(input.images.size()); + for (size_t i = 0; i < input.images.size(); i++) { + tinygltf::Image& glTFImage = input.images[i]; + // Get the image data from the glTF loader + unsigned char* buffer = nullptr; + VkDeviceSize bufferSize = 0; + bool deleteBuffer = false; + // We convert RGB-only images to RGBA, as most devices don't support RGB-formats in Vulkan + if (glTFImage.component == 3) { + bufferSize = glTFImage.width * glTFImage.height * 4; + buffer = new unsigned char[bufferSize]; + unsigned char* rgba = buffer; + unsigned char* rgb = &glTFImage.image[0]; + for (size_t i = 0; i < glTFImage.width * glTFImage.height; ++i) { + for (int32_t j = 0; j < 3; ++j) { + rgba[j] = rgb[j]; + } + rgba += 4; + rgb += 3; + } + deleteBuffer = true; + } + else { + buffer = &glTFImage.image[0]; + bufferSize = glTFImage.image.size(); + } + // Load texture from image buffer + images[i].texture.fromBuffer(buffer, bufferSize, VK_FORMAT_R8G8B8A8_UNORM, glTFImage.width, glTFImage.height, vulkanDevice, copyQueue); + } + } + + void loadTextures(tinygltf::Model& input) + { + textures.resize(input.textures.size()); + for (size_t i = 0; i < input.textures.size(); i++) { + textures[i].imageIndex = input.textures[i].source; + } + } + + void loadMaterials(tinygltf::Model& input) + { + materials.resize(input.materials.size()); + for (size_t i = 0; i < input.materials.size(); i++) { + // We only read the most basic properties required for our sample + tinygltf::Material glTFMaterial = input.materials[i]; + // Get the base color factor + if (glTFMaterial.values.find("baseColorFactor") != glTFMaterial.values.end()) { + materials[i].baseColorFactor = glm::make_vec4(glTFMaterial.values["baseColorFactor"].ColorFactor().data()); + } + // Get base color texture index + if (glTFMaterial.values.find("baseColorTexture") != glTFMaterial.values.end()) { + materials[i].baseColorTextureIndex = glTFMaterial.values["baseColorTexture"].TextureIndex(); + } + } + } + + // Helper functions for locating glTF nodes + + Node* findNode(Node* parent, uint32_t index) { + Node* nodeFound = nullptr; + if (parent->index == index) { + return parent; + } + for (auto& child : parent->children) { + nodeFound = findNode(child, index); + if (nodeFound) { + break; + } + } + return nodeFound; + } + + Node* nodeFromIndex(uint32_t index) { + Node* nodeFound = nullptr; + for (auto& node : nodes) { + nodeFound = findNode(node, index); + if (nodeFound) { + break; + } + } + return nodeFound; + } + + void loadSkins(tinygltf::Model& input) + { + skins.resize(input.skins.size()); + + for (size_t i = 0; i < input.skins.size(); i++) { + tinygltf::Skin glTFSkin = input.skins[i]; + + skins[i].name = glTFSkin.name; + // Find the root node of the skeleton + skins[i].skeletonRoot = nodeFromIndex(glTFSkin.skeleton); + + // Find joint nodes + // @todo: reference vs pointer + for (int jointIndex : glTFSkin.joints) { + Node* node = nodeFromIndex(jointIndex); + if (node) { + skins[i].joints.push_back(node); + } + } + + // Get the inverse bind matrices from the buffer associated to this skin + if (glTFSkin.inverseBindMatrices > -1) { + const tinygltf::Accessor& accessor = input.accessors[glTFSkin.inverseBindMatrices]; + const tinygltf::BufferView& bufferView = input.bufferViews[accessor.bufferView]; + const tinygltf::Buffer& buffer = input.buffers[bufferView.buffer]; + skins[i].inverseBindMatrices.resize(accessor.count); + memcpy(skins[i].inverseBindMatrices.data(), &buffer.data[accessor.byteOffset + bufferView.byteOffset], accessor.count * sizeof(glm::mat4)); + } + + // Store inverse bind matrices for this skin in a shader storage buffer object + // To keep this sample simple, we create a host visible shader storage buffer + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &skins[i].ssbo, + sizeof(glm::mat4) * skins[i].inverseBindMatrices.size(), + skins[i].inverseBindMatrices.data())); + VK_CHECK_RESULT(skins[i].ssbo.map()); + + // @todo: destroy; + } + } + + void loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector& indexBuffer, std::vector& vertexBuffer) + { + VulkanglTFModel::Node* node = new VulkanglTFModel::Node{}; + node->parent = parent; + node->matrix = glm::mat4(1.0f); + node->index = nodeIndex; + node->skin = inputNode.skin; + + // Get the local node matrix + // It's either made up from translation, rotation, scale or a 4x4 matrix + if (inputNode.translation.size() == 3) { +// node->matrix = glm::translate(node->matrix, glm::vec3(glm::make_vec3(inputNode.translation.data()))); + node->translation = glm::make_vec3(inputNode.translation.data()); + } + if (inputNode.rotation.size() == 4) { + glm::quat q = glm::make_quat(inputNode.rotation.data()); +// node->matrix *= glm::mat4(q); + node->rotation = glm::mat4(q); + } + if (inputNode.scale.size() == 3) { +// node->matrix = glm::scale(node->matrix, glm::vec3(glm::make_vec3(inputNode.scale.data()))); + node->scale = glm::make_vec3(inputNode.scale.data()); + } + if (inputNode.matrix.size() == 16) { + node->matrix = glm::make_mat4x4(inputNode.matrix.data()); + }; + + // Load node's children + if (inputNode.children.size() > 0) { + for (size_t i = 0; i < inputNode.children.size(); i++) { + loadNode(input.nodes[inputNode.children[i]], input , node, inputNode.children[i], indexBuffer, vertexBuffer); + } + } + + // If the node contains mesh data, we load vertices and indices from the the buffers + // In glTF this is done via accessors and buffer views + if (inputNode.mesh > -1) { + const tinygltf::Mesh mesh = input.meshes[inputNode.mesh]; + // Iterate through all primitives of this node's mesh + for (size_t i = 0; i < mesh.primitives.size(); i++) { + const tinygltf::Primitive& glTFPrimitive = mesh.primitives[i]; + uint32_t firstIndex = static_cast(indexBuffer.size()); + uint32_t vertexStart = static_cast(vertexBuffer.size()); + uint32_t indexCount = 0; + bool hasSkin = false; + // Vertices + { + const float* positionBuffer = nullptr; + const float* normalsBuffer = nullptr; + const float* texCoordsBuffer = nullptr; + const uint16_t* jointIndicesBuffer = nullptr; + const float* jointWeightsBuffer = nullptr; + size_t vertexCount = 0; + + // Get buffer data for vertex normals + if (glTFPrimitive.attributes.find("POSITION") != glTFPrimitive.attributes.end()) { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("POSITION")->second]; + const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView]; + positionBuffer = reinterpret_cast(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset])); + vertexCount = accessor.count; + } + // Get buffer data for vertex normals + if (glTFPrimitive.attributes.find("NORMAL") != glTFPrimitive.attributes.end()) { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("NORMAL")->second]; + const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView]; + normalsBuffer = reinterpret_cast(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset])); + } + // Get buffer data for vertex texture coordinates + // glTF supports multiple sets, we only load the first one + if (glTFPrimitive.attributes.find("TEXCOORD_0") != glTFPrimitive.attributes.end()) { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("TEXCOORD_0")->second]; + const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView]; + texCoordsBuffer = reinterpret_cast(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset])); + } + + // Get buffer data required for vertex skinning + // Get vertex joint indices + if (glTFPrimitive.attributes.find("JOINTS_0") != glTFPrimitive.attributes.end()) { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("JOINTS_0")->second]; + const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView]; + jointIndicesBuffer = reinterpret_cast(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset])); + } + // Get vertex joint weights + if (glTFPrimitive.attributes.find("WEIGHTS_0") != glTFPrimitive.attributes.end()) { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.attributes.find("WEIGHTS_0")->second]; + const tinygltf::BufferView& view = input.bufferViews[accessor.bufferView]; + jointWeightsBuffer = reinterpret_cast(&(input.buffers[view.buffer].data[accessor.byteOffset + view.byteOffset])); + } + + hasSkin = (jointIndicesBuffer && jointWeightsBuffer); + + // Append data to model's vertex buffer + for (size_t v = 0; v < vertexCount; v++) { + Vertex vert{}; + vert.pos = glm::vec4(glm::make_vec3(&positionBuffer[v * 3]), 1.0f); + vert.normal = glm::normalize(glm::vec3(normalsBuffer ? glm::make_vec3(&normalsBuffer[v * 3]) : glm::vec3(0.0f))); + vert.uv = texCoordsBuffer ? glm::make_vec2(&texCoordsBuffer[v * 2]) : glm::vec3(0.0f); + vert.color = glm::vec3(1.0f); + vert.jointIndices = hasSkin ? glm::vec4(glm::make_vec4(&jointIndicesBuffer[v * 4])) : glm::vec4(0.0f); + vert.jointWeights = hasSkin ? glm::make_vec4(&jointWeightsBuffer[v * 4]) : glm::vec4(0.0f); + vertexBuffer.push_back(vert); + } + } + // Indices + { + const tinygltf::Accessor& accessor = input.accessors[glTFPrimitive.indices]; + const tinygltf::BufferView& bufferView = input.bufferViews[accessor.bufferView]; + const tinygltf::Buffer& buffer = input.buffers[bufferView.buffer]; + + indexCount += static_cast(accessor.count); + + // glTF supports different component types of indices + switch (accessor.componentType) { + case TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT: { + uint32_t* buf = new uint32_t[accessor.count]; + memcpy(buf, &buffer.data[accessor.byteOffset + bufferView.byteOffset], accessor.count * sizeof(uint32_t)); + for (size_t index = 0; index < accessor.count; index++) { + indexBuffer.push_back(buf[index] + vertexStart); + } + break; + } + case TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT: { + uint16_t* buf = new uint16_t[accessor.count]; + memcpy(buf, &buffer.data[accessor.byteOffset + bufferView.byteOffset], accessor.count * sizeof(uint16_t)); + for (size_t index = 0; index < accessor.count; index++) { + indexBuffer.push_back(buf[index] + vertexStart); + } + break; + } + case TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE: { + uint8_t* buf = new uint8_t[accessor.count]; + memcpy(buf, &buffer.data[accessor.byteOffset + bufferView.byteOffset], accessor.count * sizeof(uint8_t)); + for (size_t index = 0; index < accessor.count; index++) { + indexBuffer.push_back(buf[index] + vertexStart); + } + break; + } + default: + std::cerr << "Index component type " << accessor.componentType << " not supported!" << std::endl; + return; + } + } + Primitive primitive{}; + primitive.firstIndex = firstIndex; + primitive.indexCount = indexCount; + primitive.materialIndex = glTFPrimitive.material; + node->mesh.primitives.push_back(primitive); + // @todo + //node->mesh.createUniformBuffer(vulkanDevice); + } + } + + if (parent) { + parent->children.push_back(node); + } + else { + nodes.push_back(node); + } + } + + /* + glTF vertex skinning functions + */ + glm::mat4 getNodeMatrix(VulkanglTFModel::Node* node) { + // Pass the node's matrix via push constanst + // Traverse the node hierarchy to the top-most parent to get the final matrix of the current node + glm::mat4 nodeMatrix = node->matrix; + VulkanglTFModel::Node* currentParent = node->parent; + while (currentParent) { + nodeMatrix = currentParent->matrix * nodeMatrix; + currentParent = currentParent->parent; + } + return nodeMatrix; + } + + glm::mat4 getNodeMatrix2(VulkanglTFModel::Node* node) { + glm::mat4 m = node->getLocalMatrix(); + VulkanglTFModel::Node* p = node->parent; + while (p) { + m = p->getLocalMatrix() * m; + p = p->parent; + } + return m; + } + + void updateJoints(VulkanglTFModel::Node* node) { + if (node->skin > -1) { + glm::mat4 m = getNodeMatrix2(node); + // Update joint matrices + glm::mat4 inverseTransform = glm::inverse(m); + Skin skin = skins[node->skin]; + size_t numJoints = (uint32_t)skin.joints.size(); + // @todo: linkt to skin spec gltf https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_020_Skins.md#the-joint-matrices + std::vector jointMatrices(numJoints); + // @todo: bail out if model has more joints than shader can handle + for (size_t i = 0; i < numJoints; i++) { + jointMatrices[i] = getNodeMatrix2(skin.joints[i]) * skin.inverseBindMatrices[i]; + jointMatrices[i] = inverseTransform * jointMatrices[i]; + } + // Update ssbo + skin.ssbo.copyTo(jointMatrices.data(), jointMatrices.size() * sizeof(glm::mat4)); + } + + for (auto& child : node->children) { + updateJoints(child); + } + } + + /* + glTF rendering functions + */ + + // Draw a single node including child nodes (if present) + void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node node) + { + if (node.mesh.primitives.size() > 0) { + // Pass the node's matrix via push constanst + // Traverse the node hierarchy to the top-most parent to get the final matrix of the current node + glm::mat4 nodeMatrix = node.matrix; + VulkanglTFModel::Node* currentParent = node.parent; + while (currentParent) { + nodeMatrix = currentParent->matrix * nodeMatrix; + currentParent = currentParent->parent; + } + // Pass the final matrix to the vertex shader using push constants + vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &nodeMatrix); + // @todo + if (node.skin > -1) { + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 2, 1, &skins[node.skin].descriptorSet, 0, nullptr); + } else { + //@todo... + } + for (VulkanglTFModel::Primitive& primitive : node.mesh.primitives) { + if (primitive.indexCount > 0) { + // Get the texture index for this primitive + VulkanglTFModel::Texture texture = textures[materials[primitive.materialIndex].baseColorTextureIndex]; + // Bind the descriptor for the current primitive's texture + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 1, 1, &images[texture.imageIndex].descriptorSet, 0, nullptr); + vkCmdDrawIndexed(commandBuffer, primitive.indexCount, 1, primitive.firstIndex, 0, 0); + } + } + } + for (auto& child : node.children) { + drawNode(commandBuffer, pipelineLayout, *child); + } + } + + // Draw the glTF scene starting at the top-level-nodes + void draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout) + { + // All vertices and indices are stored in single buffers, so we only need to bind once + VkDeviceSize offsets[1] = { 0 }; + vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertices.buffer, offsets); + vkCmdBindIndexBuffer(commandBuffer, indices.buffer, 0, VK_INDEX_TYPE_UINT32); + // Render all nodes at top-level + for (auto& node : nodes) { + drawNode(commandBuffer, pipelineLayout, *node); + } + } + +}; + +class VulkanExample : public VulkanExampleBase +{ +public: + bool wireframe = false; + + VulkanglTFModel glTFModel; + + struct ShaderData { + vks::Buffer buffer; + struct Values { + glm::mat4 projection; + glm::mat4 model; + glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, -5.0f, 1.0f); + } values; + } shaderData; + + struct Pipelines { + VkPipeline solid; + VkPipeline wireframe = VK_NULL_HANDLE; + } pipelines; + + VkPipelineLayout pipelineLayout; + VkDescriptorSet descriptorSet; + + struct DescriptorSetLayouts { + VkDescriptorSetLayout matrices; + VkDescriptorSetLayout textures; + VkDescriptorSetLayout jointMatrices; + } descriptorSetLayouts; + + VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) + { + title = "glTF vertex skinning"; + camera.type = Camera::CameraType::lookat; + camera.flipY = true; + camera.setPosition(glm::vec3(0.0f, -0.1f, -1.0f)); + camera.setRotation(glm::vec3(0.0f, -135.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); + settings.overlay = true; + } + + ~VulkanExample() + { + // Clean up used Vulkan resources + // Note : Inherited destructor cleans up resources stored in base class + vkDestroyPipeline(device, pipelines.solid, nullptr); + if (pipelines.wireframe != VK_NULL_HANDLE) { + vkDestroyPipeline(device, pipelines.wireframe, nullptr); + } + + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); + + shaderData.buffer.destroy(); + } + + virtual void getEnabledFeatures() + { + // Fill mode non solid is required for wireframe display + if (deviceFeatures.fillModeNonSolid) { + enabledFeatures.fillModeNonSolid = VK_TRUE; + }; + } + + void buildCommandBuffers() + { + VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo(); + + VkClearValue clearValues[2]; + clearValues[0].color = defaultClearColor; + clearValues[0].color = { { 0.25f, 0.25f, 0.25f, 1.0f } };; + clearValues[1].depthStencil = { 1.0f, 0 }; + + VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo(); + renderPassBeginInfo.renderPass = renderPass; + renderPassBeginInfo.renderArea.offset.x = 0; + renderPassBeginInfo.renderArea.offset.y = 0; + renderPassBeginInfo.renderArea.extent.width = width; + renderPassBeginInfo.renderArea.extent.height = height; + renderPassBeginInfo.clearValueCount = 2; + renderPassBeginInfo.pClearValues = clearValues; + + const VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f); + const VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0); + + for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) + { + renderPassBeginInfo.framebuffer = frameBuffers[i]; + VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); + vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); + vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); + vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); + // Bind scene matrices descriptor to set 0 + vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr); + vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, wireframe ? pipelines.wireframe : pipelines.solid); + glTFModel.draw(drawCmdBuffers[i], pipelineLayout); + drawUI(drawCmdBuffers[i]); + vkCmdEndRenderPass(drawCmdBuffers[i]); + VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); + } + } + + void loadglTFFile(std::string filename) + { + tinygltf::Model glTFInput; + tinygltf::TinyGLTF gltfContext; + std::string error, warning; + + this->device = device; + +#if defined(__ANDROID__) + // On Android all assets are packed with the apk in a compressed form, so we need to open them using the asset manager + // We let tinygltf handle this, by passing the asset manager of our app + tinygltf::asset_manager = androidApp->activity->assetManager; +#endif + bool fileLoaded = gltfContext.LoadASCIIFromFile(&glTFInput, &error, &warning, filename); + + // Pass some Vulkan resources required for setup and rendering to the glTF model loading class + glTFModel.vulkanDevice = vulkanDevice; + glTFModel.copyQueue = queue; + + std::vector indexBuffer; + std::vector vertexBuffer; + + if (fileLoaded) { + glTFModel.loadImages(glTFInput); + glTFModel.loadMaterials(glTFInput); + glTFModel.loadTextures(glTFInput); + const tinygltf::Scene& scene = glTFInput.scenes[0]; + for (size_t i = 0; i < scene.nodes.size(); i++) { + const tinygltf::Node node = glTFInput.nodes[scene.nodes[i]]; + glTFModel.loadNode(node, glTFInput, nullptr, scene.nodes[i], indexBuffer, vertexBuffer); + } + glTFModel.loadSkins(glTFInput); + // Calculate initial pose + // @todo: Ugly code + // @todo: Linear nodes? + for (auto node : glTFModel.nodes) { + glTFModel.updateJoints(node); + } + } + else { + vks::tools::exitFatal("Could not open the glTF file.\n\nThe file is part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.", -1); + return; + } + + // Create and upload vertex and index buffer + // We will be using one single vertex buffer and one single index buffer for the whole glTF scene + // Primitives (of the glTF model) will then index into these using index offsets + + size_t vertexBufferSize = vertexBuffer.size() * sizeof(VulkanglTFModel::Vertex); + size_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t); + glTFModel.indices.count = static_cast(indexBuffer.size()); + + struct StagingBuffer { + VkBuffer buffer; + VkDeviceMemory memory; + } vertexStaging, indexStaging; + + // Create host visible staging buffers (source) + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + vertexBufferSize, + &vertexStaging.buffer, + &vertexStaging.memory, + vertexBuffer.data())); + // Index data + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + indexBufferSize, + &indexStaging.buffer, + &indexStaging.memory, + indexBuffer.data())); + + // Create device local buffers (targat) + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + vertexBufferSize, + &glTFModel.vertices.buffer, + &glTFModel.vertices.memory)); + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + indexBufferSize, + &glTFModel.indices.buffer, + &glTFModel.indices.memory)); + + // Copy data from staging buffers (host) do device local buffer (gpu) + VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); + VkBufferCopy copyRegion = {}; + + copyRegion.size = vertexBufferSize; + vkCmdCopyBuffer( + copyCmd, + vertexStaging.buffer, + glTFModel.vertices.buffer, + 1, + ©Region); + + copyRegion.size = indexBufferSize; + vkCmdCopyBuffer( + copyCmd, + indexStaging.buffer, + glTFModel.indices.buffer, + 1, + ©Region); + + vulkanDevice->flushCommandBuffer(copyCmd, queue, true); + + // Free staging resources + vkDestroyBuffer(device, vertexStaging.buffer, nullptr); + vkFreeMemory(device, vertexStaging.memory, nullptr); + vkDestroyBuffer(device, indexStaging.buffer, nullptr); + vkFreeMemory(device, indexStaging.memory, nullptr); + } + + void loadAssets() + { + loadglTFFile(getAssetPath() + "models/CesiumMan/glTF/CesiumMan.gltf"); + } + + void setupDescriptors() + { + /* + This sample uses separate descriptor sets (and layouts) for the matrices and materials (textures) + */ + + std::vector poolSizes = { + vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), + // One combined image sampler per material image/texture + vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, static_cast(glTFModel.images.size())), + // One ssbo per skin + vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, static_cast(glTFModel.skins.size())), + }; + // Number of descriptor sets = One for the scene ubo + one per image + one per skin + const uint32_t maxSetCount = static_cast(glTFModel.images.size()) + static_cast(glTFModel.skins.size()) + 1; + VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, maxSetCount); + VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); + + // Descriptor set layouts + VkDescriptorSetLayoutBinding setLayoutBinding{}; + VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI = vks::initializers::descriptorSetLayoutCreateInfo(&setLayoutBinding, 1); + + // Descriptor set layout for passing matrices + setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0); + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.matrices)); + + // Descriptor set layout for passing material textures + setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0); + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.textures)); + + // Descriptor set layout for passing skin joint matrices + setLayoutBinding = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0); + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.jointMatrices)); + + // The pipeline layout uses three sets: + // Set 0 = Scene matrices + // Set 1 = Material texture + // Set 2 = Joint matrices + std::array setLayouts = { + descriptorSetLayouts.matrices, + descriptorSetLayouts.textures, + descriptorSetLayouts.jointMatrices + }; + VkPipelineLayoutCreateInfo pipelineLayoutCI= vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast(setLayouts.size())); + + // We will use push constants to push the local matrices of a primitive to the vertex shader + VkPushConstantRange pushConstantRange = vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::mat4), 0); + // Push constant ranges are part of the pipeline layout + pipelineLayoutCI.pushConstantRangeCount = 1; + pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); + + // Descriptor set for scene matrices + VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.matrices, 1); + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); + VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &shaderData.buffer.descriptor); + vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); + + // Descriptor sets for glTF model materials + for (auto& image : glTFModel.images) { + const VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.textures, 1); + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &image.descriptorSet)); + VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(image.descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &image.texture.descriptor); + vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); + } + + // Descriptor set for glTF model skin joint matrices + for (auto& skin : glTFModel.skins) { + const VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.jointMatrices, 1); + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &skin.descriptorSet)); + VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(skin.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, &skin.ssbo.descriptor); + vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); + } + } + + void preparePipelines() + { + VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); + VkPipelineRasterizationStateCreateInfo rasterizationStateCI = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); + VkPipelineColorBlendAttachmentState blendAttachmentStateCI = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); + VkPipelineColorBlendStateCreateInfo colorBlendStateCI = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentStateCI); + VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL); + VkPipelineViewportStateCreateInfo viewportStateCI = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0); + VkPipelineMultisampleStateCreateInfo multisampleStateCI = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0); + const std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; + VkPipelineDynamicStateCreateInfo dynamicStateCI = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), static_cast(dynamicStateEnables.size()), 0); + // Vertex input bindings and attributes + const std::vector vertexInputBindings = { + vks::initializers::vertexInputBindingDescription(0, sizeof(VulkanglTFModel::Vertex), VK_VERTEX_INPUT_RATE_VERTEX), + }; + const std::vector vertexInputAttributes = { + vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, pos)), + vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, normal)), + vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, uv)), + vks::initializers::vertexInputAttributeDescription(0, 3, VK_FORMAT_R32G32B32_SFLOAT, offsetof(VulkanglTFModel::Vertex, color)), + vks::initializers::vertexInputAttributeDescription(0, 4, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(VulkanglTFModel::Vertex, jointIndices)), + vks::initializers::vertexInputAttributeDescription(0, 5, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(VulkanglTFModel::Vertex, jointWeights)), + }; + VkPipelineVertexInputStateCreateInfo vertexInputStateCI = vks::initializers::pipelineVertexInputStateCreateInfo(); + vertexInputStateCI.vertexBindingDescriptionCount = static_cast(vertexInputBindings.size()); + vertexInputStateCI.pVertexBindingDescriptions = vertexInputBindings.data(); + vertexInputStateCI.vertexAttributeDescriptionCount = static_cast(vertexInputAttributes.size()); + vertexInputStateCI.pVertexAttributeDescriptions = vertexInputAttributes.data(); + + const std::array shaderStages = { + loadShader(getAssetPath() + "shaders/gltfskinning/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT), + loadShader(getAssetPath() + "shaders/gltfskinning/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT) + }; + + VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0); + pipelineCI.pVertexInputState = &vertexInputStateCI; + pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; + pipelineCI.pRasterizationState = &rasterizationStateCI; + pipelineCI.pColorBlendState = &colorBlendStateCI; + pipelineCI.pMultisampleState = &multisampleStateCI; + pipelineCI.pViewportState = &viewportStateCI; + pipelineCI.pDepthStencilState = &depthStencilStateCI; + pipelineCI.pDynamicState = &dynamicStateCI; + pipelineCI.stageCount = static_cast(shaderStages.size()); + pipelineCI.pStages = shaderStages.data(); + + // Solid rendering pipeline + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.solid)); + + // Wire frame rendering pipeline + if (deviceFeatures.fillModeNonSolid) { + rasterizationStateCI.polygonMode = VK_POLYGON_MODE_LINE; + rasterizationStateCI.lineWidth = 1.0f; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.wireframe)); + } + } + + // Prepare and initialize uniform buffer containing shader uniforms + void prepareUniformBuffers() + { + // Vertex shader uniform buffer block + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &shaderData.buffer, + sizeof(shaderData.values))); + + // Map persistent + VK_CHECK_RESULT(shaderData.buffer.map()); + + updateUniformBuffers(); + } + + void updateUniformBuffers() + { + shaderData.values.projection = camera.matrices.perspective; + shaderData.values.model = camera.matrices.view; + memcpy(shaderData.buffer.mapped, &shaderData.values, sizeof(shaderData.values)); + } + + void prepare() + { + VulkanExampleBase::prepare(); + loadAssets(); + prepareUniformBuffers(); + setupDescriptors(); + preparePipelines(); + buildCommandBuffers(); + prepared = true; + } + + virtual void render() + { + renderFrame(); + if (camera.updated) { + updateUniformBuffers(); + } + } + + virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay) + { + if (overlay->header("Settings")) { + if (overlay->checkBox("Wireframe", &wireframe)) { + buildCommandBuffers(); + } + } + } +}; + +VULKAN_EXAMPLE_MAIN() \ No newline at end of file