2019-04-09 13:19:18 +00:00
|
|
|
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// VulkanHpp Samples : InitTexture
|
|
|
|
// Initialize texture
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
#if defined( _MSC_VER )
|
|
|
|
// no need to ignore any warnings with MSVC
|
|
|
|
#elif defined( __clang__ )
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-variable"
|
|
|
|
#elif defined( __GNUC__ )
|
|
|
|
# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
|
|
|
|
#else
|
|
|
|
// unknow compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
2019-04-09 13:19:18 +00:00
|
|
|
#include "../utils/geometries.hpp"
|
|
|
|
#include "../utils/math.hpp"
|
|
|
|
#include "../utils/shaders.hpp"
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
#include "SPIRV/GlslangToSpv.h"
|
2020-04-12 19:49:12 +00:00
|
|
|
#include "vulkan/vulkan.hpp"
|
|
|
|
|
2019-04-09 13:19:18 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "InitTexture";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
2019-04-09 13:19:18 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 50, 50 ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
|
|
|
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.surface );
|
|
|
|
vk::UniqueDevice device =
|
|
|
|
vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueCommandPool commandPool = vk::su::createCommandPool( device, graphicsAndPresentQueueFamilyIndex.first );
|
|
|
|
vk::UniqueCommandBuffer commandBuffer = std::move( device
|
|
|
|
->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo(
|
|
|
|
commandPool.get(), vk::CommandBufferLevel::ePrimary, 1 ) )
|
|
|
|
.front() );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Queue graphicsQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
|
|
|
|
vk::Queue presentQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Format format = vk::Format::eR8G8B8A8Unorm;
|
|
|
|
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// See if we can use a linear tiled image for a texture, if not, we will need a staging buffer for the texture data
|
2020-04-12 19:49:12 +00:00
|
|
|
bool needsStaging = !( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage );
|
|
|
|
|
|
|
|
vk::UniqueImage image = device->createImageUnique(
|
|
|
|
vk::ImageCreateInfo( vk::ImageCreateFlags(),
|
|
|
|
vk::ImageType::e2D,
|
|
|
|
format,
|
|
|
|
vk::Extent3D( surfaceData.extent, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
needsStaging ? vk::ImageTiling::eOptimal : vk::ImageTiling::eLinear,
|
|
|
|
vk::ImageUsageFlagBits::eSampled |
|
|
|
|
( needsStaging ? vk::ImageUsageFlagBits::eTransferDst : vk::ImageUsageFlagBits() ),
|
|
|
|
vk::SharingMode::eExclusive,
|
2020-07-08 08:58:37 +00:00
|
|
|
{},
|
2020-04-12 19:49:12 +00:00
|
|
|
needsStaging ? vk::ImageLayout::eUndefined : vk::ImageLayout::ePreinitialized ) );
|
|
|
|
|
|
|
|
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements( image.get() );
|
|
|
|
uint32_t memoryTypeIndex = vk::su::findMemoryType(
|
|
|
|
physicalDevice.getMemoryProperties(),
|
|
|
|
memoryRequirements.memoryTypeBits,
|
|
|
|
needsStaging ? vk::MemoryPropertyFlags()
|
|
|
|
: ( vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// allocate memory
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueDeviceMemory imageMemory =
|
|
|
|
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// bind memory
|
2020-04-12 19:49:12 +00:00
|
|
|
device->bindImageMemory( image.get(), imageMemory.get(), 0 );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueBuffer textureBuffer;
|
2019-04-09 13:19:18 +00:00
|
|
|
vk::UniqueDeviceMemory textureBufferMemory;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( needsStaging )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
|
|
|
// Need a staging buffer to map and copy texture into
|
2020-04-12 19:49:12 +00:00
|
|
|
textureBuffer =
|
|
|
|
device->createBufferUnique( vk::BufferCreateInfo( vk::BufferCreateFlags(),
|
|
|
|
surfaceData.extent.width * surfaceData.extent.height * 4,
|
|
|
|
vk::BufferUsageFlagBits::eTransferSrc ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
memoryRequirements = device->getBufferMemoryRequirements( textureBuffer.get() );
|
|
|
|
memoryTypeIndex =
|
|
|
|
vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
|
|
|
|
memoryRequirements.memoryTypeBits,
|
|
|
|
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// allocate memory
|
2020-04-12 19:49:12 +00:00
|
|
|
textureBufferMemory =
|
|
|
|
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// bind memory
|
2020-04-12 19:49:12 +00:00
|
|
|
device->bindBufferMemory( textureBuffer.get(), textureBufferMemory.get(), 0 );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SubresourceLayout subresourceLayout =
|
|
|
|
device->getImageSubresourceLayout( image.get(), vk::ImageSubresource( vk::ImageAspectFlagBits::eColor ) );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
void * data = device->mapMemory(
|
|
|
|
needsStaging ? textureBufferMemory.get() : imageMemory.get(), 0, memoryRequirements.size, vk::MemoryMapFlags() );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
// Checkerboard of 16x16 pixel squares
|
2020-04-12 19:49:12 +00:00
|
|
|
unsigned char * pImageMemory = static_cast<unsigned char *>( data );
|
|
|
|
for ( uint32_t row = 0; row < surfaceData.extent.height; row++ )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( uint32_t col = 0; col < surfaceData.extent.width; col++ )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
unsigned char rgb = ( ( ( row & 0x10 ) == 0 ) ^ ( ( col & 0x10 ) == 0 ) ) * 255;
|
|
|
|
pImageMemory[0] = rgb;
|
|
|
|
pImageMemory[1] = rgb;
|
|
|
|
pImageMemory[2] = rgb;
|
|
|
|
pImageMemory[3] = 255;
|
2019-04-09 13:19:18 +00:00
|
|
|
pImageMemory += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
device->unmapMemory( needsStaging ? textureBufferMemory.get() : imageMemory.get() );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
commandBuffer->begin( vk::CommandBufferBeginInfo() );
|
|
|
|
if ( needsStaging )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
|
|
|
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::setImageLayout(
|
|
|
|
commandBuffer, image.get(), format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal );
|
|
|
|
vk::BufferImageCopy copyRegion( 0,
|
|
|
|
surfaceData.extent.width,
|
|
|
|
surfaceData.extent.height,
|
|
|
|
vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ),
|
|
|
|
vk::Offset3D( 0, 0, 0 ),
|
|
|
|
vk::Extent3D( surfaceData.extent, 1 ) );
|
|
|
|
commandBuffer->copyBufferToImage(
|
|
|
|
textureBuffer.get(), image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion );
|
2019-04-09 13:19:18 +00:00
|
|
|
// Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::setImageLayout( commandBuffer,
|
|
|
|
image.get(),
|
|
|
|
format,
|
|
|
|
vk::ImageLayout::eTransferDstOptimal,
|
|
|
|
vk::ImageLayout::eShaderReadOnlyOptimal );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we can use the linear tiled image as a texture, just do it
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::setImageLayout(
|
|
|
|
commandBuffer, image.get(), format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
commandBuffer->end();
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::submitAndWait( device, graphicsQueue, commandBuffer );
|
|
|
|
|
|
|
|
vk::UniqueSampler sampler =
|
|
|
|
device->createSamplerUnique( vk::SamplerCreateInfo( vk::SamplerCreateFlags(),
|
|
|
|
vk::Filter::eNearest,
|
|
|
|
vk::Filter::eNearest,
|
|
|
|
vk::SamplerMipmapMode::eNearest,
|
|
|
|
vk::SamplerAddressMode::eClampToEdge,
|
|
|
|
vk::SamplerAddressMode::eClampToEdge,
|
|
|
|
vk::SamplerAddressMode::eClampToEdge,
|
|
|
|
0.0f,
|
|
|
|
false,
|
|
|
|
1.0f,
|
|
|
|
false,
|
|
|
|
vk::CompareOp::eNever,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
vk::BorderColor::eFloatOpaqueWhite ) );
|
|
|
|
|
|
|
|
vk::ComponentMapping componentMapping(
|
|
|
|
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
|
|
|
|
vk::ImageViewCreateInfo imageViewCreateInfo(
|
|
|
|
vk::ImageViewCreateFlags(),
|
|
|
|
image.get(),
|
|
|
|
vk::ImageViewType::e2D,
|
|
|
|
format,
|
|
|
|
componentMapping,
|
|
|
|
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
|
|
|
|
vk::UniqueImageView imageView = device->createImageViewUnique( imageViewCreateInfo );
|
2019-04-09 13:19:18 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|