Split sample into header and source files due to complexity

This commit is contained in:
Sascha Willems 2020-05-15 07:13:51 +02:00
parent 940aef5b86
commit 149ff8f94e
6 changed files with 1122 additions and 1077 deletions

View file

@ -1,6 +1,6 @@
#version 450
layout (set = 1, binding = 0) uniform sampler2D samplerColorMap;
layout (set = 2, binding = 0) uniform sampler2D samplerColorMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;

View file

@ -18,7 +18,7 @@ layout(push_constant) uniform PushConsts {
mat4 model;
} primitive;
layout(std430, set = 2, binding = 0) readonly buffer JointMatrices {
layout(std430, set = 1, binding = 0) readonly buffer JointMatrices {
mat4 jointMatrices[];
};
@ -43,8 +43,9 @@ void main()
gl_Position = uboScene.projection * uboScene.view * primitive.model * skinMat * vec4(inPos.xyz, 1.0);
outNormal = normalize(transpose(inverse(mat3(uboScene.view * primitive.model * skinMat))) * inNormal);
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;

View file

@ -19,203 +19,31 @@
// @todo: add link to https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_020_Skins.md
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <vector>
#include "gltfskinning.h"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
/*
#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"
glTF model class
#include <vulkan/vulkan.h>
#include "vulkanexamplebase.h"
#include "VulkanTexture.hpp"
Contains everything required to render a skinned glTF model in Vulkan
This class is simplified compared to glTF's feature set but retains the basic glTF structure required for this sample
#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<Primitive> 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<glm::mat4> inverseBindMatrices;
std::vector<Node*> joints;
// POI: Store joint matrices in an SSBO
// @todo: proper comment
std::vector<glm::mat4> 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<Node*> 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() {
/*
Get a node's local matrix from the current translation, rotation and scale values
These are calculated from the current animation an need to be calculated dynamically
*/
glm::mat4 VulkanglTFModel::Node::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;
};
/*
glTF animation channel
// @todo: Comment
*/
struct AnimationChannel {
std::string path;
Node* node;
uint32_t samplerIndex;
};
/*
glTF animation sampler
// @todo: Comment
*/
struct AnimationSampler {
std::string interpolation;
std::vector<float> inputs;
std::vector<glm::vec4> outputsVec4;
};
/*
glTF animation
// @todo: Comment
*/
struct Animation {
std::string name;
std::vector<AnimationSampler> samplers;
std::vector<AnimationChannel> channels;
float start = std::numeric_limits<float>::max();
float end = std::numeric_limits<float>::min();
};
/*
Model data
*/
std::vector<Image> images;
std::vector<Texture> textures;
std::vector<Material> materials;
std::vector<Node*> nodes;
std::vector<Skin> skins;
std::vector<Animation> animations;
// 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> meshdata;
~VulkanglTFModel()
{
// Release all Vulkan resources allocated for the model
/*
Release all Vulkan resources acquired for the model
*/
VulkanglTFModel::~VulkanglTFModel()
{
vkDestroyBuffer(vulkanDevice->logicalDevice, vertices.buffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, vertices.memory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, indices.buffer, nullptr);
@ -226,16 +54,16 @@ public:
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)
{
void VulkanglTFModel::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());
@ -252,9 +80,7 @@ public:
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];
}
memcpy(rgba, rgb, sizeof(unsigned char) * 3);
rgba += 4;
rgb += 3;
}
@ -266,19 +92,22 @@ public:
}
// Load texture from image buffer
images[i].texture.fromBuffer(buffer, bufferSize, VK_FORMAT_R8G8B8A8_UNORM, glTFImage.width, glTFImage.height, vulkanDevice, copyQueue);
if (deleteBuffer) {
delete buffer;
}
}
}
void loadTextures(tinygltf::Model& input)
{
void VulkanglTFModel::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)
{
void VulkanglTFModel::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
@ -292,11 +121,11 @@ public:
materials[i].baseColorTextureIndex = glTFMaterial.values["baseColorTexture"].TextureIndex();
}
}
}
}
// Helper functions for locating glTF nodes
// Helper functions for locating glTF nodes
Node* findNode(Node* parent, uint32_t index) {
VulkanglTFModel::Node* VulkanglTFModel::findNode(Node* parent, uint32_t index) {
Node* nodeFound = nullptr;
if (parent->index == index) {
return parent;
@ -308,9 +137,9 @@ public:
}
}
return nodeFound;
}
}
Node* nodeFromIndex(uint32_t index) {
VulkanglTFModel::Node* VulkanglTFModel::nodeFromIndex(uint32_t index) {
Node* nodeFound = nullptr;
for (auto& node : nodes) {
nodeFound = findNode(node, index);
@ -319,10 +148,12 @@ public:
}
}
return nodeFound;
}
}
void loadSkins(tinygltf::Model& input)
{
// @todo: comment
// @todo: Add link to spec
void VulkanglTFModel::loadSkins(tinygltf::Model& input)
{
skins.resize(input.skins.size());
for (size_t i = 0; i < input.skins.size(); i++) {
@ -362,13 +193,13 @@ public:
// @todo: destroy;
}
}
}
// @todo: Helper for getting buffer
// @todo: Helper for getting buffer
// @todo: comment
void loadAnimations(tinygltf::Model& input)
{
// @todo: comment
void VulkanglTFModel::loadAnimations(tinygltf::Model& input)
{
animations.resize(input.animations.size());
for (size_t i = 0; i < input.animations.size(); i++) {
@ -445,10 +276,10 @@ public:
dstChannel.node = nodeFromIndex(srcChannel.target_node);
}
}
}
}
void loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer)
{
void VulkanglTFModel::loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer)
{
VulkanglTFModel::Node* node = new VulkanglTFModel::Node{};
node->parent = parent;
node->matrix = glm::mat4(1.0f);
@ -458,16 +289,16 @@ public:
// 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->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->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->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) {
@ -477,7 +308,7 @@ public:
// 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);
loadNode(input.nodes[inputNode.children[i]], input, node, inputNode.children[i], indexBuffer, vertexBuffer);
}
}
@ -605,12 +436,12 @@ public:
else {
nodes.push_back(node);
}
}
}
/*
/*
glTF vertex skinning functions
*/
glm::mat4 getNodeMatrix(VulkanglTFModel::Node* node) {
*/
glm::mat4 VulkanglTFModel::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;
@ -620,9 +451,9 @@ public:
currentParent = currentParent->parent;
}
return nodeMatrix;
}
}
glm::mat4 getNodeMatrix2(VulkanglTFModel::Node* node) {
glm::mat4 VulkanglTFModel::getNodeMatrix2(VulkanglTFModel::Node* node) {
glm::mat4 m = node->getLocalMatrix();
VulkanglTFModel::Node* p = node->parent;
while (p) {
@ -630,9 +461,9 @@ public:
p = p->parent;
}
return m;
}
}
void updateJoints(VulkanglTFModel::Node* node) {
void VulkanglTFModel::updateJoints(VulkanglTFModel::Node* node) {
if (node->skin > -1) {
glm::mat4 m = getNodeMatrix2(node);
// Update joint matrices
@ -653,15 +484,19 @@ public:
for (auto& child : node->children) {
updateJoints(child);
}
}
}
void updateAnimation(uint32_t index, float time)
{
if (index > static_cast<uint32_t>(animations.size()) - 1) {
std::cout << "No animation with index " << index << std::endl;
void VulkanglTFModel::updateAnimation(float deltaTime)
{
if (activeAnimation> static_cast<uint32_t>(animations.size()) - 1) {
std::cout << "No animation with index " << activeAnimation << std::endl;
return;
}
Animation& animation = animations[index];
Animation& animation = animations[activeAnimation];
animation.currentTime += deltaTime;
if (animation.currentTime > animation.end) {
animation.currentTime -= animation.end;
}
bool updated = false;
for (auto& channel : animation.channels) {
@ -671,8 +506,8 @@ public:
}
for (size_t i = 0; i < sampler.inputs.size() - 1; i++) {
if ((time >= sampler.inputs[i]) && (time <= sampler.inputs[i + 1])) {
float u = std::max(0.0f, time - sampler.inputs[i]) / (sampler.inputs[i + 1] - sampler.inputs[i]);
if ((animation.currentTime >= sampler.inputs[i]) && (animation.currentTime <= sampler.inputs[i + 1])) {
float u = std::max(0.0f, animation.currentTime - sampler.inputs[i]) / (sampler.inputs[i + 1] - sampler.inputs[i]);
if (u <= 1.0f) {
if (channel.path == "translation") {
glm::vec4 trans = glm::mix(sampler.outputsVec4[i], sampler.outputsVec4[i + 1], u);
@ -707,15 +542,15 @@ public:
updateJoints(node);
}
}
}
}
/*
/*
glTF rendering functions
*/
*/
// Draw a single node including child nodes (if present)
void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node node)
{
// Draw a single node including child nodes (if present)
void VulkanglTFModel::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
@ -727,18 +562,14 @@ public:
}
// 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...
}
// Bind SSBO with skin data for this node to set 1
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 1, 1, &skins[node.skin].descriptorSet, 0, nullptr);
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);
// Bind the descriptor for the current primitive's texture to set 2
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 2, 1, &images[texture.imageIndex].descriptorSet, 0, nullptr);
vkCmdDrawIndexed(commandBuffer, primitive.indexCount, 1, primitive.firstIndex, 0, 0);
}
}
@ -746,11 +577,11 @@ public:
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)
{
// Draw the glTF scene starting at the top-level-nodes
void VulkanglTFModel::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);
@ -759,43 +590,17 @@ public:
for (auto& node : nodes) {
drawNode(commandBuffer, pipelineLayout, *node);
}
}
}
};
class VulkanExample : public VulkanExampleBase
/*
Vulkan Example class
*/
VulkanExample::VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{
public:
bool wireframe = false;
float animationTimer = 0.0f;
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;
@ -803,10 +608,10 @@ public:
camera.setRotation(glm::vec3(0.0f, 0.0f, 0.0f));
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
settings.overlay = true;
}
}
~VulkanExample()
{
VulkanExample::~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.solid, nullptr);
@ -819,18 +624,18 @@ public:
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr);
shaderData.buffer.destroy();
}
}
virtual void getEnabledFeatures()
{
void VulkanExample::getEnabledFeatures()
{
// Fill mode non solid is required for wireframe display
if (deviceFeatures.fillModeNonSolid) {
enabledFeatures.fillModeNonSolid = VK_TRUE;
};
}
}
void buildCommandBuffers()
{
void VulkanExample::buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2];
@ -865,10 +670,10 @@ public:
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
}
}
}
void loadglTFFile(std::string filename)
{
void VulkanExample::loadglTFFile(std::string filename)
{
tinygltf::Model glTFInput;
tinygltf::TinyGLTF gltfContext;
std::string error, warning;
@ -901,8 +706,6 @@ public:
glTFModel.loadSkins(glTFInput);
glTFModel.loadAnimations(glTFInput);
// Calculate initial pose
// @todo: Ugly code
// @todo: Linear nodes?
for (auto node : glTFModel.nodes) {
glTFModel.updateJoints(node);
}
@ -913,9 +716,6 @@ public:
}
// 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<uint32_t>(indexBuffer.size());
@ -970,15 +770,10 @@ public:
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()
{
void VulkanExample::setupDescriptors()
{
/*
This sample uses separate descriptor sets (and layouts) for the matrices and materials (textures)
*/
@ -1012,15 +807,15 @@ public:
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
// Set 0 = Scene matrices (VS)
// Set 1 = Joint matrices (VS)
// Set 2 = Material texture (FS)
std::array<VkDescriptorSetLayout, 3> setLayouts = {
descriptorSetLayouts.matrices,
descriptorSetLayouts.textures,
descriptorSetLayouts.jointMatrices
descriptorSetLayouts.jointMatrices,
descriptorSetLayouts.textures
};
VkPipelineLayoutCreateInfo pipelineLayoutCI= vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast<uint32_t>(setLayouts.size()));
VkPipelineLayoutCreateInfo pipelineLayoutCI = vks::initializers::pipelineLayoutCreateInfo(setLayouts.data(), static_cast<uint32_t>(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);
@ -1035,14 +830,6 @@ public:
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);
@ -1050,10 +837,18 @@ public:
VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(skin.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, &skin.ssbo.descriptor);
vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr);
}
}
void preparePipelines()
{
// 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);
}
}
void VulkanExample::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);
@ -1107,24 +902,29 @@ public:
rasterizationStateCI.lineWidth = 1.0f;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.wireframe));
}
}
}
void prepareUniformBuffers()
{
void VulkanExample::prepareUniformBuffers()
{
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)));
VK_CHECK_RESULT(shaderData.buffer.map());
updateUniformBuffers();
}
}
void updateUniformBuffers()
{
void VulkanExample::updateUniformBuffers()
{
shaderData.values.projection = camera.matrices.perspective;
shaderData.values.model = camera.matrices.view;
memcpy(shaderData.buffer.mapped, &shaderData.values, sizeof(shaderData.values));
}
}
void prepare()
{
void VulkanExample::loadAssets()
{
loadglTFFile(getAssetPath() + "models/CesiumMan/glTF/CesiumMan.gltf");
}
void VulkanExample::prepare()
{
VulkanExampleBase::prepare();
loadAssets();
prepareUniformBuffers();
@ -1132,34 +932,27 @@ public:
preparePipelines();
buildCommandBuffers();
prepared = true;
}
}
virtual void render()
{
void VulkanExample::render()
{
renderFrame();
if (camera.updated) {
updateUniformBuffers();
}
// @todo: poi
if (!paused) {
if (glTFModel.animations.size() > 0) {
animationTimer += frameTimer * 0.75f;
if (animationTimer > glTFModel.animations[0].end) {
animationTimer -= glTFModel.animations[0].end;
}
glTFModel.updateAnimation(0, animationTimer);
}
}
glTFModel.updateAnimation(frameTimer);
}
}
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
{
void VulkanExample::OnUpdateUIOverlay(vks::UIOverlay* overlay)
{
if (overlay->header("Settings")) {
if (overlay->checkBox("Wireframe", &wireframe)) {
buildCommandBuffers();
}
}
}
};
}
VULKAN_EXAMPLE_MAIN()

View file

@ -0,0 +1,251 @@
/*
* 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)
*/
/*
* This is based on the glTF scene example and only the parts that show added functionality are commented
* @todo: Rework comments
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <vector>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#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 <vulkan/vulkan.h>
#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:
vks::VulkanDevice* vulkanDevice;
VkQueue copyQueue;
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;
};
struct {
VkBuffer buffer;
VkDeviceMemory memory;
} vertices;
struct {
int count;
VkBuffer buffer;
VkDeviceMemory memory;
} indices;
struct Node;
struct Material {
glm::vec4 baseColorFactor = glm::vec4(1.0f);
uint32_t baseColorTextureIndex;
};
struct Image {
vks::Texture2D texture;
VkDescriptorSet descriptorSet;
};
struct Texture {
int32_t imageIndex;
};
struct Primitive {
uint32_t firstIndex;
uint32_t indexCount;
int32_t materialIndex;
};
struct Mesh {
std::vector<Primitive> primitives;
};
struct Node {
Node* parent;
uint32_t index;
std::vector<Node*> children;
Mesh mesh;
// Matrix components are stored separately as they are affected by animations
glm::vec3 translation{};
glm::vec3 scale{ 1.0f };
glm::quat rotation{};
// Index of the skin for this node
int32_t skin = -1;
glm::mat4 matrix;
// Gets the current local matrix based on translation, rotation and scale, which can all be affected by animations
glm::mat4 getLocalMatrix();
};
// A skin contains the joints and matrices applied during vertex skinning
// @todo: Add link to spec
struct Skin {
std::string name;
Node* skeletonRoot = nullptr;
std::vector<glm::mat4> inverseBindMatrices;
std::vector<Node*> joints;
// POI: Store joint matrices in an SSBO
// @todo: proper comment
std::vector<glm::mat4> jointMatrices;
vks::Buffer ssbo;
VkDescriptorSet descriptorSet;
};
/*
glTF animation channel
// @todo: Comment
*/
struct AnimationChannel {
std::string path;
Node* node;
uint32_t samplerIndex;
};
/*
glTF animation sampler
// @todo: Comment
*/
struct AnimationSampler {
std::string interpolation;
std::vector<float> inputs;
std::vector<glm::vec4> outputsVec4;
};
/*
glTF animation
// @todo: Comment
*/
struct Animation {
std::string name;
std::vector<AnimationSampler> samplers;
std::vector<AnimationChannel> channels;
float start = std::numeric_limits<float>::max();
float end = std::numeric_limits<float>::min();
float currentTime = 0.0f;
};
std::vector<Image> images;
std::vector<Texture> textures;
std::vector<Material> materials;
std::vector<Node*> nodes;
// Store skins and animations
std::vector<Skin> skins;
std::vector<Animation> animations;
// POI: @todo: document
struct MeshData {
glm::mat4 jointMatrix[32]{};
};
struct ShaderData {
vks::Buffer buffer;
} shaderData;
VkDescriptorSet descriptorSet;
std::vector<MeshData> meshdata;
uint32_t activeAnimation = 0;
~VulkanglTFModel();
void loadImages(tinygltf::Model& input);
void loadTextures(tinygltf::Model& input);
void loadMaterials(tinygltf::Model& input);
Node* findNode(Node* parent, uint32_t index);
Node* nodeFromIndex(uint32_t index);
void loadSkins(tinygltf::Model& input);
void loadAnimations(tinygltf::Model& input);
void loadNode(const tinygltf::Node& inputNode, const tinygltf::Model& input, VulkanglTFModel::Node* parent, uint32_t nodeIndex, std::vector<uint32_t>& indexBuffer, std::vector<VulkanglTFModel::Vertex>& vertexBuffer);
glm::mat4 getNodeMatrix(VulkanglTFModel::Node* node);
glm::mat4 getNodeMatrix2(VulkanglTFModel::Node* node);
void updateJoints(VulkanglTFModel::Node* node);
void updateAnimation(float deltaTime);
void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node node);
void draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout);
};
class VulkanExample : public VulkanExampleBase
{
public:
bool wireframe = false;
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;
VkPipelineLayout pipelineLayout;
struct Pipelines {
VkPipeline solid;
VkPipeline wireframe = VK_NULL_HANDLE;
} pipelines;
struct DescriptorSetLayouts {
VkDescriptorSetLayout matrices;
VkDescriptorSetLayout textures;
VkDescriptorSetLayout jointMatrices;
} descriptorSetLayouts;
VkDescriptorSet descriptorSet;
VulkanglTFModel glTFModel;
VulkanExample();
~VulkanExample();
void loadglTFFile(std::string filename);
virtual void getEnabledFeatures();
void buildCommandBuffers();
void loadAssets();
void setupDescriptors();
void preparePipelines();
void prepareUniformBuffers();
void updateUniformBuffers();
void prepare();
virtual void render();
virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay);
};