diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2018-10-26 18:59:06 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2018-10-26 18:59:06 +0200 |
| commit | cf965d29b9092ea6b56847028dc885b67b1603e0 (patch) | |
| tree | dc2054210d903c5d629f0487e74a58e1b96958c3 | |
| parent | a5082d2962d621dc7145614d655016c59ddacc5b (diff) | |
| -rw-r--r-- | include/vke.hpp (renamed from include/vke.h) | 0 | ||||
| -rw-r--r-- | src/VkEngine.h | 0 | ||||
| -rw-r--r-- | src/VulkanDevice.cpp | 2 | ||||
| -rw-r--r-- | src/VulkanDevice.hpp | 4 | ||||
| -rw-r--r-- | src/VulkanSwapChain.cpp | 2 | ||||
| -rw-r--r-- | src/VulkanSwapChain.hpp | 2 | ||||
| -rw-r--r-- | src/VulkanTexture.hpp | 263 | ||||
| -rw-r--r-- | src/VulkanglTFModel.hpp | 7 | ||||
| -rw-r--r-- | src/buffer.cpp | 4 | ||||
| -rw-r--r-- | src/buffer.hpp (renamed from src/VulkanBuffer.hpp) | 2 | ||||
| -rw-r--r-- | src/macros.hpp (renamed from src/macros.h) | 0 | ||||
| -rw-r--r-- | src/pbrrenderer2.cpp | 2 | ||||
| -rw-r--r-- | src/pbrrenderer2.hpp (renamed from src/pbrrenderer2.h) | 6 | ||||
| -rw-r--r-- | src/phyInfos.h | 102 | ||||
| -rw-r--r-- | src/phyInfos.hpp | 102 | ||||
| -rw-r--r-- | src/rendertarget.cpp | 4 | ||||
| -rw-r--r-- | src/rendertarget.hpp | 2 | ||||
| -rw-r--r-- | src/resource.hpp | 2 | ||||
| -rw-r--r-- | src/shadingcontext.cpp | 2 | ||||
| -rw-r--r-- | src/shadingcontext.hpp | 2 | ||||
| -rw-r--r-- | src/texture.cpp | 4 | ||||
| -rw-r--r-- | src/texture.hpp | 245 | ||||
| -rw-r--r-- | src/vke.cpp (renamed from src/VkEngine.cpp) | 6 | ||||
| -rw-r--r-- | src/vkrenderer.cpp | 6 | ||||
| -rw-r--r-- | src/vkrenderer.hpp (renamed from src/vkrenderer.h) | 2 | ||||
| -rw-r--r-- | tests/test1.cpp | 19 |
26 files changed, 388 insertions, 404 deletions
diff --git a/include/vke.h b/include/vke.hpp index b6656f8..b6656f8 100644 --- a/include/vke.h +++ b/include/vke.hpp diff --git a/src/VkEngine.h b/src/VkEngine.h deleted file mode 100644 index e69de29..0000000 --- a/src/VkEngine.h +++ /dev/null diff --git a/src/VulkanDevice.cpp b/src/VulkanDevice.cpp index 16d5aae..27f0db5 100644 --- a/src/VulkanDevice.cpp +++ b/src/VulkanDevice.cpp @@ -1,5 +1,5 @@ #include "VulkanDevice.hpp" -#include "macros.h" +#include "macros.hpp" namespace vke { device_t::device_t(vkPhyInfo *phyInfos, const std::vector<const char*>& devLayers) diff --git a/src/VulkanDevice.hpp b/src/VulkanDevice.hpp index 7c3783b..d4aff6c 100644 --- a/src/VulkanDevice.hpp +++ b/src/VulkanDevice.hpp @@ -10,8 +10,8 @@ #pragma once -#include "vke.h" -#include "phyInfos.h" +#include "vke.hpp" +#include "phyInfos.hpp" namespace vke { diff --git a/src/VulkanSwapChain.cpp b/src/VulkanSwapChain.cpp index 59aac45..e4190fe 100644 --- a/src/VulkanSwapChain.cpp +++ b/src/VulkanSwapChain.cpp @@ -13,7 +13,7 @@ #include "VulkanSwapChain.hpp" #include "VulkanDevice.hpp" #include "rendertarget.hpp" -#include "macros.h" +#include "macros.hpp" vke::swap_chain_t::swap_chain_t(engine_t *_vke, bool vsync) { vke = _vke; diff --git a/src/VulkanSwapChain.hpp b/src/VulkanSwapChain.hpp index d4e4424..49b07c2 100644 --- a/src/VulkanSwapChain.hpp +++ b/src/VulkanSwapChain.hpp @@ -10,7 +10,7 @@ #pragma once -#include "vke.h" +#include "vke.hpp" namespace vke { diff --git a/src/VulkanTexture.hpp b/src/VulkanTexture.hpp deleted file mode 100644 index 44eea90..0000000 --- a/src/VulkanTexture.hpp +++ /dev/null @@ -1,263 +0,0 @@ -/* -* Vulkan texture loader -* -* Copyright(C) 2016-2017 by Sascha Willems - www.saschawillems.de -* -* This code is licensed under the MIT license(MIT) (http://opensource.org/licenses/MIT) -*/ - -#pragma once - -#include <stdlib.h> -#include <string> -#include <fstream> -#include <vector> - -#include "vulkan/vulkan.h" -#include "macros.h" -#include "VulkanDevice.hpp" -#include "texture.hpp" - -#include <gli/gli.hpp> - -#include "tiny_gltf.h" - -#if defined(__ANDROID__) -#include <android/asset_manager.h> -#endif - -#include "VulkanBuffer.hpp" - -namespace vke -{ - class Texture2D : public Texture { - public: - void loadFromFile( - std::string filename, - VkFormat format, - vke::device_t *_device, - VkQueue copyQueue, - VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, - VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) - { -#if defined(__ANDROID__) - // Textures are stored inside the apk on Android (compressed) - // So they need to be loaded via the asset manager - AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING); - if (!asset) { - LOGE("Could not load texture %s", filename.c_str()); - exit(-1); - } - size_t size = AAsset_getLength(asset); - assert(size > 0); - - void *textureData = malloc(size); - AAsset_read(asset, textureData, size); - AAsset_close(asset); - - gli::texture2d tex2D(gli::load((const char*)textureData, size)); - - free(textureData); -#else - gli::texture2d tex2D(gli::load(filename.c_str())); -#endif - assert(!tex2D.empty()); - - create(_device, VK_IMAGE_TYPE_2D, format, - static_cast<uint32_t>(tex2D[0].extent().x),static_cast<uint32_t>(tex2D[0].extent().y), - imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - VK_IMAGE_TILING_OPTIMAL,static_cast<uint32_t>(tex2D.levels())); - - copyTo(copyQueue, (unsigned char*)tex2D.data(), tex2D.size()); - - createView(VK_IMAGE_VIEW_TYPE_2D); - createSampler(VK_FILTER_LINEAR,VK_SAMPLER_ADDRESS_MODE_REPEAT,VK_SAMPLER_MIPMAP_MODE_LINEAR); - - updateDescriptor(); - } - }; - - class TextureCubeMap : public Texture { - public: - void buildFromImages(const std::vector<std::string>& mapDic, uint32_t textureSize, - VkFormat _format, - vke::device_t *_device, - VkQueue copyQueue, - VkImageUsageFlags _imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, - VkImageLayout _imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL){ - - create(_device, VK_IMAGE_TYPE_2D, _format, - textureSize, textureSize, - _imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT| VK_IMAGE_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_IMAGE_TILING_OPTIMAL, - (uint32_t)floor(log2(textureSize)) + 1, 6, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT); - - imageLayout = _imageLayout; - - for (int l = 0; l < mapDic.size(); l++) { - Texture inTex; - inTex.loadStbLinearNoSampling(mapDic[l].c_str(), device); - - VkCommandBuffer blitFirstMipCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - - VkImageBlit firstMipBlit{}; - firstMipBlit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, l, 1}; - firstMipBlit.srcOffsets[1] = {inTex.infos.extent.width,inTex.infos.extent.height,1}; - firstMipBlit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, l, 1}; - firstMipBlit.dstOffsets[1] = {infos.extent.width,infos.extent.height,1}; - - VkImageSubresourceRange firstMipSubRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, l, 1}; - - setImageLayout(blitFirstMipCmd, - VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - firstMipSubRange, - VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); - - // Blit from source texture - vkCmdBlitImage(blitFirstMipCmd, inTex.image, - VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, image, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &firstMipBlit, VK_FILTER_CUBIC_IMG); - - setImageLayout(blitFirstMipCmd, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - firstMipSubRange, - VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); - - device->flushCommandBuffer(blitFirstMipCmd, copyQueue, true); - - inTex.destroy(); - - VkCommandBuffer blitCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - - // mipmap generation - // Copy down mips from n-1 to n - for (int32_t i = 1; i < infos.mipLevels; i++) - { - VkImageBlit imageBlit{}; - imageBlit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, i-1, l, 1}; - imageBlit.srcOffsets[1] = {infos.extent.width >> (i - 1),infos.extent.height >> (i - 1),1}; - imageBlit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, i, l, 1}; - imageBlit.dstOffsets[1] = {infos.extent.width >> i,infos.extent.height >> i, 1}; - - VkImageSubresourceRange mipSubRange = {VK_IMAGE_ASPECT_COLOR_BIT, i, 1, l, 1}; - - setImageLayout(blitCmd, - VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - mipSubRange, - VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); - - // Blit from previous level - vkCmdBlitImage(blitCmd, - image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - 1, &imageBlit, VK_FILTER_LINEAR); - - setImageLayout(blitCmd, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - mipSubRange, - VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); - } - firstMipSubRange.levelCount = infos.mipLevels; - setImageLayout(blitCmd, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout, - firstMipSubRange, - VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); - device->flushCommandBuffer(blitCmd, copyQueue, true); - } - - createView(VK_IMAGE_VIEW_TYPE_CUBE, VK_IMAGE_ASPECT_COLOR_BIT,infos.mipLevels,6); - createSampler(VK_FILTER_CUBIC_IMG,VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,VK_SAMPLER_MIPMAP_MODE_LINEAR); - //samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER; - - updateDescriptor(); - - } - void loadFromFile( - std::string filename, - VkFormat format, - vke::device_t *_device, - VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, - VkImageLayout _imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) - { -#if defined(__ANDROID__) - // Textures are stored inside the apk on Android (compressed) - // So they need to be loaded via the asset manager - AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING); - if (!asset) { - LOGE("Could not load texture %s", filename.c_str()); - exit(-1); - } - size_t size = AAsset_getLength(asset); - assert(size > 0); - - void *textureData = malloc(size); - AAsset_read(asset, textureData, size); - AAsset_close(asset); - - gli::texture_cube texCube(gli::load((const char*)textureData, size)); - - free(textureData); -#else - gli::texture_cube texCube(gli::load(filename)); -#endif - assert(!texCube.empty()); - - create(_device, VK_IMAGE_TYPE_2D, format, - static_cast<uint32_t>(texCube.extent().x),static_cast<uint32_t>(texCube[0].extent().y), - imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - VK_IMAGE_TILING_OPTIMAL,static_cast<uint32_t>(texCube.levels()), 6, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT); - - imageLayout = _imageLayout; - - vke::buffer_t stagingBuffer (device, - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - texCube.size(), texCube.data()); - - descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - - VkCommandBuffer copyCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - - size_t offset = 0; - - std::vector<VkBufferImageCopy> bufferCopyRegions; - for (uint32_t face = 0; face < 6; face++) { - for (uint32_t level = 0; level < infos.mipLevels; level++) { - VkBufferImageCopy bufferCopyRegion = {}; - bufferCopyRegion.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, face, 1}; - bufferCopyRegion.imageExtent = {(uint32_t)texCube[face][level].extent().x,(uint32_t)texCube[face][level].extent().y,1}; - bufferCopyRegion.bufferOffset = offset; - - bufferCopyRegions.push_back(bufferCopyRegion); - offset += texCube[face][level].size(); - } - } - VkImageSubresourceRange subresourceRange = {}; - subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - subresourceRange.baseMipLevel = 0; - subresourceRange.levelCount = infos.mipLevels; - subresourceRange.layerCount = 6; - - setImageLayout (copyCmd, - VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - subresourceRange, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); - - vkCmdCopyBufferToImage(copyCmd, stagingBuffer.buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - static_cast<uint32_t>(bufferCopyRegions.size()), bufferCopyRegions.data()); - - setImageLayout(copyCmd, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout, - subresourceRange, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - - device->flushCommandBuffer(copyCmd, device->queue, true); - - createView(VK_IMAGE_VIEW_TYPE_CUBE, VK_IMAGE_ASPECT_COLOR_BIT,infos.mipLevels,6); - createSampler(VK_FILTER_LINEAR,VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,VK_SAMPLER_MIPMAP_MODE_LINEAR); - - updateDescriptor(); - } - }; - -} diff --git a/src/VulkanglTFModel.hpp b/src/VulkanglTFModel.hpp index dede000..052efe0 100644 --- a/src/VulkanglTFModel.hpp +++ b/src/VulkanglTFModel.hpp @@ -14,9 +14,10 @@ #include <vector> #include <map> -#include <vke.h> +#include "vke.hpp" +#include "macros.hpp" -#include "vkrenderer.h" +#include "vkrenderer.hpp" #include "shadingcontext.hpp" #include <glm/gtc/type_ptr.hpp> @@ -31,7 +32,7 @@ //when mipLevels is set to 0 => mipmaps with be generated #define AUTOGEN_MIPMAPS 0 -#include "VulkanBuffer.hpp" +#include "buffer.hpp" namespace vkglTF { diff --git a/src/buffer.cpp b/src/buffer.cpp index 1974cfa..d3e45b6 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -1,8 +1,8 @@ -#include "VulkanBuffer.hpp" +#include "buffer.hpp" #include "VulkanDevice.hpp" #include "resource.hpp" -#include "macros.h" +#include "macros.hpp" namespace vke { diff --git a/src/VulkanBuffer.hpp b/src/buffer.hpp index ed667bf..58ed5dd 100644 --- a/src/VulkanBuffer.hpp +++ b/src/buffer.hpp @@ -10,7 +10,7 @@ #pragma once -#include "vke.h" +#include "vke.hpp" #include "resource.hpp" namespace vke diff --git a/src/macros.h b/src/macros.hpp index 4c1ddf1..4c1ddf1 100644 --- a/src/macros.h +++ b/src/macros.hpp diff --git a/src/pbrrenderer2.cpp b/src/pbrrenderer2.cpp index 29df3ef..79b7110 100644 --- a/src/pbrrenderer2.cpp +++ b/src/pbrrenderer2.cpp @@ -1,4 +1,4 @@ -#include "pbrrenderer2.h" +#include "pbrrenderer2.hpp" #include "rendertarget.hpp" pbrRenderer::pbrRenderer () : vkRenderer() diff --git a/src/pbrrenderer2.h b/src/pbrrenderer2.hpp index 71342b4..1124737 100644 --- a/src/pbrrenderer2.h +++ b/src/pbrrenderer2.hpp @@ -1,8 +1,8 @@ #pragma once -#include "VkEngine.h" -#include "vkrenderer.h" -#include "VulkanTexture.hpp" +#include "vke.hpp" +#include "vkrenderer.hpp" +#include "texture.hpp" #include "VulkanglTFModel.hpp" diff --git a/src/phyInfos.h b/src/phyInfos.h deleted file mode 100644 index db89cbd..0000000 --- a/src/phyInfos.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once - -#include "vke.h" - -namespace vke -{ - struct vkPhyInfo{ - VkPhysicalDevice phy; - VkPhysicalDeviceMemoryProperties memProps; - VkPhysicalDeviceProperties properties; - VkPhysicalDeviceFeatures features; - VkPhysicalDeviceFeatures enabledFeatures; - VkQueueFamilyProperties* queues; - uint32_t queueCount; - std::vector<int> cQueues;//compute, dedicated first - std::vector<int> gQueues;//graphic, idem - std::vector<int> tQueues;//transfer, idem - int pQueue;//presentation - - std::vector<VkDeviceQueueCreateInfo> pQueueInfos; - std::vector<std::vector<float>> qPriorities; - - vkPhyInfo (){} - vkPhyInfo (VkPhysicalDevice _phy, VkSurfaceKHR surface = VK_NULL_HANDLE) { - phy = _phy; -// cQueues.clear(); -// gQueues.clear(); -// tQueues.clear(); - pQueue = -1; -// pQueueInfos.clear(); -// qPriorities.clear(); - - vkGetPhysicalDeviceProperties (phy, &properties); - vkGetPhysicalDeviceMemoryProperties (phy, &memProps); - vkGetPhysicalDeviceFeatures (phy, &features); - - vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, NULL); - queues = (VkQueueFamilyProperties*)malloc(queueCount * sizeof(VkQueueFamilyProperties)); - vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, queues); - - //identify dedicated queues - //try to find dedicated queues first - VkBool32 present = VK_FALSE; - for (uint j=0; j<queueCount; j++){ - switch (queues[j].queueFlags) { - case VK_QUEUE_GRAPHICS_BIT: - if (surface){ - vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present); - if (present && pQueue<0) - pQueue = j; - } - gQueues.push_back (j); - break; - case VK_QUEUE_COMPUTE_BIT: - cQueues.push_back (j); - break; - case VK_QUEUE_TRANSFER_BIT: - tQueues.push_back (j); - break; - } - } - //add queues that include flag - for (uint j=0; j<queueCount; j++){ - if (queues[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) { - if (surface) { - vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present); - if (present && pQueue<0) - pQueue = j; - } - if (std::find(gQueues.begin(), gQueues.end(), j) == gQueues.end()) - gQueues.push_back(j); - } - if (queues[j].queueFlags & VK_QUEUE_COMPUTE_BIT){ - if (std::find(cQueues.begin(), cQueues.end(), j) == cQueues.end()) - cQueues.push_back(j); - } - if (queues[j].queueFlags & VK_QUEUE_TRANSFER_BIT){ - if (std::find(tQueues.begin(), tQueues.end(), j) == tQueues.end()) - tQueues.push_back(j); - } - } - } - - void selectQueue (int qIndex, float priority = 0.f) { - auto it = find_if(pQueueInfos.begin(), pQueueInfos.end(), [&qIndex](const VkDeviceQueueCreateInfo& qci) - {return qci.queueFamilyIndex == qIndex;}); - if (it == pQueueInfos.end()) { - VkDeviceQueueCreateInfo dqci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO}; - dqci.queueCount = 1; - dqci.queueFamilyIndex = qIndex; - pQueueInfos.push_back (dqci); - qPriorities.push_back ({priority}); - return; - } - - int qciIdx = std::distance(pQueueInfos.begin(), it); - pQueueInfos[qciIdx].queueCount ++; - qPriorities[qciIdx].push_back(priority); - } - - }; -} diff --git a/src/phyInfos.hpp b/src/phyInfos.hpp new file mode 100644 index 0000000..12523cd --- /dev/null +++ b/src/phyInfos.hpp @@ -0,0 +1,102 @@ +#pragma once + +#include "vke.hpp" + +namespace vke +{ + struct vkPhyInfo{ + VkPhysicalDevice phy; + VkPhysicalDeviceMemoryProperties memProps; + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceFeatures features; + VkPhysicalDeviceFeatures enabledFeatures; + VkQueueFamilyProperties* queues; + uint32_t queueCount; + std::vector<int> cQueues;//compute, dedicated first + std::vector<int> gQueues;//graphic, idem + std::vector<int> tQueues;//transfer, idem + int pQueue;//presentation + + std::vector<VkDeviceQueueCreateInfo> pQueueInfos; + std::vector<std::vector<float>> qPriorities; + + vkPhyInfo (){} + vkPhyInfo (VkPhysicalDevice _phy, VkSurfaceKHR surface = VK_NULL_HANDLE) { + phy = _phy; +// cQueues.clear(); +// gQueues.clear(); +// tQueues.clear(); + pQueue = -1; +// pQueueInfos.clear(); +// qPriorities.clear(); + + vkGetPhysicalDeviceProperties (phy, &properties); + vkGetPhysicalDeviceMemoryProperties (phy, &memProps); + vkGetPhysicalDeviceFeatures (phy, &features); + + vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, NULL); + queues = (VkQueueFamilyProperties*)malloc(queueCount * sizeof(VkQueueFamilyProperties)); + vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, queues); + + //identify dedicated queues + //try to find dedicated queues first + VkBool32 present = VK_FALSE; + for (uint j=0; j<queueCount; j++){ + switch (queues[j].queueFlags) { + case VK_QUEUE_GRAPHICS_BIT: + if (surface){ + vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present); + if (present && pQueue<0) + pQueue = j; + } + gQueues.push_back (j); + break; + case VK_QUEUE_COMPUTE_BIT: + cQueues.push_back (j); + break; + case VK_QUEUE_TRANSFER_BIT: + tQueues.push_back (j); + break; + } + } + //add queues that include flag + for (uint j=0; j<queueCount; j++){ + if (queues[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) { + if (surface) { + vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present); + if (present && pQueue<0) + pQueue = j; + } + if (std::find(gQueues.begin(), gQueues.end(), j) == gQueues.end()) + gQueues.push_back(j); + } + if (queues[j].queueFlags & VK_QUEUE_COMPUTE_BIT){ + if (std::find(cQueues.begin(), cQueues.end(), j) == cQueues.end()) + cQueues.push_back(j); + } + if (queues[j].queueFlags & VK_QUEUE_TRANSFER_BIT){ + if (std::find(tQueues.begin(), tQueues.end(), j) == tQueues.end()) + tQueues.push_back(j); + } + } + } + + void selectQueue (int qIndex, float priority = 0.f) { + auto it = find_if(pQueueInfos.begin(), pQueueInfos.end(), [&qIndex](const VkDeviceQueueCreateInfo& qci) + {return qci.queueFamilyIndex == qIndex;}); + if (it == pQueueInfos.end()) { + VkDeviceQueueCreateInfo dqci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO}; + dqci.queueCount = 1; + dqci.queueFamilyIndex = qIndex; + pQueueInfos.push_back (dqci); + qPriorities.push_back ({priority}); + return; + } + + int qciIdx = std::distance(pQueueInfos.begin(), it); + pQueueInfos[qciIdx].queueCount ++; + qPriorities[qciIdx].push_back(priority); + } + + }; +} diff --git a/src/rendertarget.cpp b/src/rendertarget.cpp index e61785b..f54a2b8 100644 --- a/src/rendertarget.cpp +++ b/src/rendertarget.cpp @@ -2,8 +2,8 @@ #include "VulkanDevice.hpp" #include "VulkanSwapChain.hpp" -#include "VulkanBuffer.hpp" -#include "macros.h" +#include "buffer.hpp" +#include "macros.hpp" vke::render_target_t::render_target_t(device_t* _device, VkSampleCountFlagBits _samples) { device = _device; diff --git a/src/rendertarget.hpp b/src/rendertarget.hpp index ffc17e9..5259412 100644 --- a/src/rendertarget.hpp +++ b/src/rendertarget.hpp @@ -1,6 +1,6 @@ #pragma once -#include "vke.h" +#include "vke.hpp" #include "texture.hpp" namespace vke { diff --git a/src/resource.hpp b/src/resource.hpp index 9b15505..91bf02c 100644 --- a/src/resource.hpp +++ b/src/resource.hpp @@ -1,6 +1,6 @@ #pragma once -#include "vke.h" +#include "vke.hpp" namespace vke { struct Resource diff --git a/src/shadingcontext.cpp b/src/shadingcontext.cpp index 8e28a77..4135bce 100644 --- a/src/shadingcontext.cpp +++ b/src/shadingcontext.cpp @@ -1,7 +1,7 @@ #include "shadingcontext.hpp" #include "VulkanDevice.hpp" #include "resource.hpp" -#include "macros.h" +#include "macros.hpp" namespace vke { ShadingContext::ShadingContext(device_t* _device, uint32_t maxDescriptorSet) { diff --git a/src/shadingcontext.hpp b/src/shadingcontext.hpp index 7b8d5f0..5dced83 100644 --- a/src/shadingcontext.hpp +++ b/src/shadingcontext.hpp @@ -1,6 +1,6 @@ #pragma once -#include "vke.h" +#include "vke.hpp" namespace vke { struct BindingSlot { diff --git a/src/texture.cpp b/src/texture.cpp index ab7f643..0112ed8 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,8 +1,8 @@ #include "texture.hpp" #include "VulkanDevice.hpp" -#include "VulkanBuffer.hpp" +#include "buffer.hpp" #include "stb_image.h" -#include "macros.h" +#include "macros.hpp" namespace vke { diff --git a/src/texture.hpp b/src/texture.hpp index 5f664be..9d98d49 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -1,7 +1,20 @@ +/* +* Vulkan texture loader +* +* Copyright(C) 2016-2017 by Sascha Willems - www.saschawillems.de +* +* This code is licensed under the MIT license(MIT) (http://opensource.org/licenses/MIT) +*/ + #pragma once -#include "vke.h" +#include "vke.hpp" + +#include <gli/gli.hpp> + #include "resource.hpp" +#include "buffer.hpp" +#include "VulkanDevice.hpp" namespace vke { @@ -78,4 +91,234 @@ namespace vke VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT, bool flipY = true); }; + class Texture2D : public Texture { + public: + void loadFromFile( + std::string filename, + VkFormat format, + vke::device_t *_device, + VkQueue copyQueue, + VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, + VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) + { +#if defined(__ANDROID__) + // Textures are stored inside the apk on Android (compressed) + // So they need to be loaded via the asset manager + AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING); + if (!asset) { + LOGE("Could not load texture %s", filename.c_str()); + exit(-1); + } + size_t size = AAsset_getLength(asset); + assert(size > 0); + + void *textureData = malloc(size); + AAsset_read(asset, textureData, size); + AAsset_close(asset); + + gli::texture2d tex2D(gli::load((const char*)textureData, size)); + + free(textureData); +#else + gli::texture2d tex2D(gli::load(filename.c_str())); +#endif + assert(!tex2D.empty()); + + create(_device, VK_IMAGE_TYPE_2D, format, + static_cast<uint32_t>(tex2D[0].extent().x),static_cast<uint32_t>(tex2D[0].extent().y), + imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + VK_IMAGE_TILING_OPTIMAL,static_cast<uint32_t>(tex2D.levels())); + + copyTo(copyQueue, (unsigned char*)tex2D.data(), tex2D.size()); + + createView(VK_IMAGE_VIEW_TYPE_2D); + createSampler(VK_FILTER_LINEAR,VK_SAMPLER_ADDRESS_MODE_REPEAT,VK_SAMPLER_MIPMAP_MODE_LINEAR); + + updateDescriptor(); + } + }; + + class TextureCubeMap : public Texture { + public: + void buildFromImages(const std::vector<std::string>& mapDic, uint32_t textureSize, + VkFormat _format, + vke::device_t *_device, + VkQueue copyQueue, + VkImageUsageFlags _imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, + VkImageLayout _imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL){ + + create(_device, VK_IMAGE_TYPE_2D, _format, + textureSize, textureSize, + _imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT| VK_IMAGE_USAGE_TRANSFER_SRC_BIT, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_IMAGE_TILING_OPTIMAL, + (uint32_t)floor(log2(textureSize)) + 1, 6, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT); + + imageLayout = _imageLayout; + + for (int l = 0; l < mapDic.size(); l++) { + Texture inTex; + inTex.loadStbLinearNoSampling(mapDic[l].c_str(), device); + + VkCommandBuffer blitFirstMipCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); + + VkImageBlit firstMipBlit{}; + firstMipBlit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, l, 1}; + firstMipBlit.srcOffsets[1] = {inTex.infos.extent.width,inTex.infos.extent.height,1}; + firstMipBlit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, l, 1}; + firstMipBlit.dstOffsets[1] = {infos.extent.width,infos.extent.height,1}; + + VkImageSubresourceRange firstMipSubRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, l, 1}; + + setImageLayout(blitFirstMipCmd, + VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + firstMipSubRange, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + + // Blit from source texture + vkCmdBlitImage(blitFirstMipCmd, inTex.image, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, image, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &firstMipBlit, VK_FILTER_CUBIC_IMG); + + setImageLayout(blitFirstMipCmd, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + firstMipSubRange, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + + device->flushCommandBuffer(blitFirstMipCmd, copyQueue, true); + + inTex.destroy(); + + VkCommandBuffer blitCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); + + // mipmap generation + // Copy down mips from n-1 to n + for (int32_t i = 1; i < infos.mipLevels; i++) + { + VkImageBlit imageBlit{}; + imageBlit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, i-1, l, 1}; + imageBlit.srcOffsets[1] = {infos.extent.width >> (i - 1),infos.extent.height >> (i - 1),1}; + imageBlit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, i, l, 1}; + imageBlit.dstOffsets[1] = {infos.extent.width >> i,infos.extent.height >> i, 1}; + + VkImageSubresourceRange mipSubRange = {VK_IMAGE_ASPECT_COLOR_BIT, i, 1, l, 1}; + + setImageLayout(blitCmd, + VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + mipSubRange, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + + // Blit from previous level + vkCmdBlitImage(blitCmd, + image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + 1, &imageBlit, VK_FILTER_LINEAR); + + setImageLayout(blitCmd, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + mipSubRange, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + } + firstMipSubRange.levelCount = infos.mipLevels; + setImageLayout(blitCmd, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout, + firstMipSubRange, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + device->flushCommandBuffer(blitCmd, copyQueue, true); + } + + createView(VK_IMAGE_VIEW_TYPE_CUBE, VK_IMAGE_ASPECT_COLOR_BIT,infos.mipLevels,6); + createSampler(VK_FILTER_CUBIC_IMG,VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,VK_SAMPLER_MIPMAP_MODE_LINEAR); + //samplerCreateInfo.compareOp = VK_COMPARE_OP_NEVER; + + updateDescriptor(); + + } + void loadFromFile( + std::string filename, + VkFormat format, + vke::device_t *_device, + VkImageUsageFlags imageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT, + VkImageLayout _imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) + { +#if defined(__ANDROID__) + // Textures are stored inside the apk on Android (compressed) + // So they need to be loaded via the asset manager + AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING); + if (!asset) { + LOGE("Could not load texture %s", filename.c_str()); + exit(-1); + } + size_t size = AAsset_getLength(asset); + assert(size > 0); + + void *textureData = malloc(size); + AAsset_read(asset, textureData, size); + AAsset_close(asset); + + gli::texture_cube texCube(gli::load((const char*)textureData, size)); + + free(textureData); +#else + gli::texture_cube texCube(gli::load(filename)); +#endif + assert(!texCube.empty()); + + create(_device, VK_IMAGE_TYPE_2D, format, + static_cast<uint32_t>(texCube.extent().x),static_cast<uint32_t>(texCube[0].extent().y), + imageUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + VK_IMAGE_TILING_OPTIMAL,static_cast<uint32_t>(texCube.levels()), 6, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT); + + imageLayout = _imageLayout; + + vke::buffer_t stagingBuffer (device, + VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + texCube.size(), texCube.data()); + + descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkCommandBuffer copyCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); + + size_t offset = 0; + + std::vector<VkBufferImageCopy> bufferCopyRegions; + for (uint32_t face = 0; face < 6; face++) { + for (uint32_t level = 0; level < infos.mipLevels; level++) { + VkBufferImageCopy bufferCopyRegion = {}; + bufferCopyRegion.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, face, 1}; + bufferCopyRegion.imageExtent = {(uint32_t)texCube[face][level].extent().x,(uint32_t)texCube[face][level].extent().y,1}; + bufferCopyRegion.bufferOffset = offset; + + bufferCopyRegions.push_back(bufferCopyRegion); + offset += texCube[face][level].size(); + } + } + VkImageSubresourceRange subresourceRange = {}; + subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + subresourceRange.baseMipLevel = 0; + subresourceRange.levelCount = infos.mipLevels; + subresourceRange.layerCount = 6; + + setImageLayout (copyCmd, + VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + subresourceRange, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); + + vkCmdCopyBufferToImage(copyCmd, stagingBuffer.buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + static_cast<uint32_t>(bufferCopyRegions.size()), bufferCopyRegions.data()); + + setImageLayout(copyCmd, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, imageLayout, + subresourceRange, + VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + + device->flushCommandBuffer(copyCmd, device->queue, true); + + createView(VK_IMAGE_VIEW_TYPE_CUBE, VK_IMAGE_ASPECT_COLOR_BIT,infos.mipLevels,6); + createSampler(VK_FILTER_LINEAR,VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,VK_SAMPLER_MIPMAP_MODE_LINEAR); + + updateDescriptor(); + } + }; + } diff --git a/src/VkEngine.cpp b/src/vke.cpp index e6d12ab..6e3ed3a 100644 --- a/src/VkEngine.cpp +++ b/src/vke.cpp @@ -10,8 +10,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "tiny_gltf.h" -#include "vke.h" -#include "macros.h" +#include "vke.hpp" +#include "macros.hpp" #define ENGINE_NAME "vke" #define ENGINE_VERSION 1 @@ -19,7 +19,7 @@ #include "VulkanDevice.hpp" #include "resource.hpp" -#include "VulkanBuffer.hpp" +#include "buffer.hpp" #include "texture.hpp" #include "shadingcontext.hpp" diff --git a/src/vkrenderer.cpp b/src/vkrenderer.cpp index 7bde5b7..e9c9640 100644 --- a/src/vkrenderer.cpp +++ b/src/vkrenderer.cpp @@ -1,11 +1,11 @@ -#include "vkrenderer.h" +#include "vkrenderer.hpp" //#include "pbrrenderer2.h" #include "VulkanDevice.hpp" #include "rendertarget.hpp" #include "VulkanSwapChain.hpp" #include "shadingcontext.hpp" -#include "VulkanBuffer.hpp" -#include "macros.h" +#include "buffer.hpp" +#include "macros.hpp" const VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; diff --git a/src/vkrenderer.h b/src/vkrenderer.hpp index 4c7c7e7..ea85115 100644 --- a/src/vkrenderer.h +++ b/src/vkrenderer.hpp @@ -1,6 +1,6 @@ #pragma once -#include "vke.h" +#include "vke.hpp" #define DRAW_FENCE_TIMEOUT 99900000 diff --git a/tests/test1.cpp b/tests/test1.cpp index efd6090..2b8d17d 100644 --- a/tests/test1.cpp +++ b/tests/test1.cpp @@ -1,7 +1,7 @@ -#include "vke.h" +#include "vke.hpp" #include "camera.hpp" -#include "phyInfos.h" -#include "pbrrenderer2.h" +#include "phyInfos.hpp" +#include "pbrrenderer2.hpp" #include "VulkanSwapChain.hpp" class Test : public vke::engine_t @@ -16,9 +16,10 @@ public: camera->type = vke::Camera::CameraType::firstperson; camera->movementSpeed = 8.0f; camera->rotationSpeed = 0.25f; - camera->setPerspective(50.0f, (float)width / (float)height, 0.1f, 50.0f); - camera->setRotation({ 42.0f, 0.0f, 0.0f }); - camera->setPosition({ .0f, -12.f, -15.f }); + camera->setPerspective(45.0f, (float)width / (float)height, 0.1f, 256.0f); + camera->setRotation({ .0f, 0.0f, 2.5f }); + camera->setPosition({ .0f, .0f, 0.f }); + settings.validation = true; @@ -40,9 +41,11 @@ public: sceneRenderer->models.resize(1); mod = &sceneRenderer->models[0]; - mod->loadFromFile ("data/models/chess.gltf", device, true); + //mod->loadFromFile ("data/models/chess.gltf", device, true); + mod->loadFromFile ("data/models/DamagedHelmet/glTF-Embedded/DamagedHelmet.gltf", device, true); - mod->addInstance("frame", glm::translate(glm::mat4(1.0), glm::vec3( 0,0,0))); + //mod->addInstance("frame", glm::translate(glm::mat4(1.0), glm::vec3( 0,0,0))); + mod->addOneInstanceOfEach(); sceneRenderer->prepareModels(); sceneRenderer->buildCommandBuffer(); |