22 if (!builder._layers.size()) {
23 LUG_LOG.error(
"Vulkan::Texture::build: Not layers added");
33 transferQueue = device.
getQueue(
"queue_transfer");
35 LUG_LOG.error(
"Vulkan::Texture::build: Can't find transfer queue");
43 VkResult result{VK_SUCCESS};
45 if (!commandPoolBuilder.build(transferQueueCommandPool, &result)) {
46 LUG_LOG.error(
"Vulkan::Texture::build: Can't create a command pool: {}", result);
58 return VK_FORMAT_R8G8B8A8_UNORM;
61 return VK_FORMAT_R16G16_SFLOAT;
64 return VK_FORMAT_R16G16B16_SFLOAT;
67 return VK_FORMAT_R32G32B32A32_SFLOAT;
70 return VK_FORMAT_UNDEFINED;
75 imageBuilder.
setUsage(VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
79 imageBuilder.
setTiling(VK_IMAGE_TILING_OPTIMAL);
81 imageBuilder.
setArrayLayers(static_cast<uint32_t>(builder._layers.size()));
84 deviceMemoryBuilder.
setMemoryFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
87 static_cast<uint32_t
>(builder._width),
88 static_cast<uint32_t>(builder._height),
99 VkResult result{VK_SUCCESS};
100 if (!imageBuilder.
build(texture->
_image, &result)) {
101 LUG_LOG.error(
"Vulkan::Texture::build: Can't create the image: {}", result);
106 LUG_LOG.error(
"Vulkan::Texture::build: Can't add image to device memory");
112 LUG_LOG.error(
"Vulkan::Texture::build: Can't create buffer device memory: {}", result);
124 imageViewBuilder.
setLayerCount(static_cast<uint32_t>(builder._layers.size()));
128 imageViewBuilder.
setViewType(VK_IMAGE_VIEW_TYPE_CUBE);
131 VkResult result{VK_SUCCESS};
133 LUG_LOG.error(
"Vulkan::Texture::build: Can't create image view: {}", result);
138 uint32_t nbLayersWithData = 0;
139 for (
const auto& layer : builder._layers) {
147 if (nbLayersWithData)
150 const VkDeviceSize bufferSize = layerSize * nbLayersWithData;
160 bufferBuilder.
setSize(bufferSize);
161 bufferBuilder.
setUsage(VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
164 VkResult result{VK_SUCCESS};
165 if (!bufferBuilder.
build(stagingBuffer, &result)) {
166 LUG_LOG.error(
"Vulkan::Texture::build: Can't create staging buffer: {}", result);
174 deviceMemoryBuilder.
setMemoryFlags(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
175 deviceMemoryBuilder.
addBuffer(stagingBuffer);
177 VkResult result{VK_SUCCESS};
178 if (!deviceMemoryBuilder.
build(stagingBufferMemory, &result)) {
179 LUG_LOG.error(
"Vulkan::Texture::build: Can't create staging buffer device memory: {}", result);
186 VkDeviceSize pixelsOffset{0};
187 for (
const auto& layer : builder._layers) {
192 stagingBuffer.
updateData(layer.data, layerSize, pixelsOffset);
193 pixelsOffset += layerSize;
200 VkResult result{VK_SUCCESS};
204 commandBufferBuilder.
setLevel(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
206 if (!commandBufferBuilder.
build(commandBuffer, &result)) {
207 LUG_LOG.error(
"Gui::init: Can't create the command buffer: {}", result);
216 if (!fenceBuilder.
build(fence, &result)) {
217 LUG_LOG.error(
"Vulkan::Texture::build: Can't create swapchain fence: {}", result);
222 commandBuffer.
begin();
227 pipelineBarrier.imageMemoryBarriers.resize(1);
228 pipelineBarrier.imageMemoryBarriers[0].srcAccessMask = 0;
229 pipelineBarrier.imageMemoryBarriers[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
230 pipelineBarrier.imageMemoryBarriers[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
231 pipelineBarrier.imageMemoryBarriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
232 pipelineBarrier.imageMemoryBarriers[0].image = &texture->
_image;
233 pipelineBarrier.imageMemoryBarriers[0].subresourceRange.layerCount =
static_cast<uint32_t
>(builder._layers.size());
235 commandBuffer.pipelineBarrier(pipelineBarrier);
238 std::vector<VkBufferImageCopy> bufferCopyRegions(nbLayersWithData);
239 VkDeviceSize pixelsOffset{0};
241 for (uint32_t layerNb = 0; layerNb < builder._layers.size(); ++layerNb) {
242 if (!builder._layers[layerNb].data) {
247 bufferCopyRegions[i++] = {
252 VK_IMAGE_ASPECT_COLOR_BIT,
263 static_cast<uint32_t
>(builder._width),
264 static_cast<uint32_t>(builder._height),
269 pixelsOffset += layerSize;
275 vkCmdCopyBufferToImage(
276 static_cast<VkCommandBuffer>(commandBuffer),
277 static_cast<VkBuffer>(stagingBuffer),
278 static_cast<VkImage>(texture->
_image),
279 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
280 static_cast<uint32_t>(bufferCopyRegions.size()),
281 bufferCopyRegions.data()
287 pipelineBarrier.imageMemoryBarriers.resize(1);
288 pipelineBarrier.imageMemoryBarriers[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
289 pipelineBarrier.imageMemoryBarriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
290 pipelineBarrier.imageMemoryBarriers[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
291 pipelineBarrier.imageMemoryBarriers[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
292 pipelineBarrier.imageMemoryBarriers[0].image = &texture->
_image;
293 pipelineBarrier.imageMemoryBarriers[0].subresourceRange.layerCount =
static_cast<uint32_t
>(builder._layers.size());
295 commandBuffer.pipelineBarrier(pipelineBarrier);
298 if (commandBuffer.
end() ==
false) {
299 LUG_LOG.error(
"Vulkan::Texture::build: Failed to end commandBuffer");
303 if (transferQueue->
submit(commandBuffer, {}, {}, {}, static_cast<VkFence>(fence)) ==
false) {
304 LUG_LOG.error(
"Vulkan::Texture::build: Can't submit commandBuffer");
310 LUG_LOG.error(
"Vulkan::Texture::build: Can't vkWaitForFences");
327 switch(wrappingMode) {
329 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
331 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
333 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
336 return VkSamplerAddressMode{};
346 return VK_FILTER_NEAREST;
348 return VK_FILTER_LINEAR;
354 samplerBuilder.
setMinFilter(filterToVulkan(builder._minFilter));
355 samplerBuilder.
setMagFilter(filterToVulkan(builder._magFilter));
360 return VK_SAMPLER_MIPMAP_MODE_NEAREST;
362 return VK_SAMPLER_MIPMAP_MODE_LINEAR;
365 return VkSamplerMipmapMode{};
366 }(builder._mipMapFilter));
368 samplerBuilder.
setMaxLod(static_cast<float>(builder._mipLevels));
370 VkResult result{VK_SUCCESS};
372 LUG_LOG.error(
"Gui::initFontsTexture: Can't create image view: {}", result);
void setAspectFlags(VkImageAspectFlags aspectFlags)
bool build(API::DeviceMemory &instance, VkResult *returnResult=nullptr)
void setMipmapMode(VkSamplerMipmapMode mipmapMode)
bool build(API::Sampler &instance, VkResult *returnResult=nullptr)
bool build(API::ImageView &instance, VkResult *returnResult=nullptr)
bool build(API::CommandBuffer &instance, VkResult *returnResult=nullptr)
void setMipLevels(uint32_t mipLevels)
void setAddressModeU(VkSamplerAddressMode addressModeU)
API::ImageView _imageView
void setFeatureFlags(VkFormatFeatureFlags featureFlags)
void pipelineBarrier(const CmdPipelineBarrier ¶meters, VkDependencyFlags dependencyFlags=VK_DEPENDENCY_BY_REGION_BIT, VkPipelineStageFlags srcStageMask=VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VkPipelineStageFlags dstStageMask=VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) const
void setQueueFamilyIndices(std::set< uint32_t > queueFamilyIndices)
const API::Queue * getQueue(const std::string &queueName) const
void setExtent(VkExtent3D extent)
Resource::SharedPtr< lug::Graphics::Render::Texture > build(const ::lug::Graphics::Builder::Texture &builder)
bool submit(const CommandBuffer &commandBuffer, const std::vector< VkSemaphore > &signalSemaphores={}, const std::vector< VkSemaphore > &waitSemaphores={}, const std::vector< VkPipelineStageFlags > &waitDstStageMasks={}, VkFence fence=VK_NULL_HANDLE) const
void setViewType(VkImageViewType viewType)
void setMagFilter(VkFilter magFilter)
bool build(API::Fence &instance, VkResult *returnResult=nullptr)
void setLevel(VkCommandBufferLevel level)
void setMaxLod(float maxLod)
static size_t formatToSize(Render::Texture::Format format)
bool build(API::Buffer &instance, VkResult *returnResult=nullptr)
Dummy class for a shared pointer.
void setSize(VkDeviceSize size)
void setAddressModeW(VkSamplerAddressMode addressModeW)
void setAddressModeV(VkSamplerAddressMode addressModeV)
void setTiling(VkImageTiling tiling)
const QueueFamily * getQueueFamily() const
void setUsage(VkImageUsageFlags usage)
void setFormat(VkFormat format)
API::DeviceMemory _deviceMemory
void setQueueFamilyIndices(const std::set< uint32_t > &queueFamilyIndices)
bool addBuffer(API::Buffer &buffer)
VkFormat getFormat() const
void setMinFilter(VkFilter minFilter)
void setCreateFlags(VkImageCreateFlags createFlags)
void setExclusive(bool exclusive)
void setMemoryFlags(VkMemoryPropertyFlags flags)
bool addImage(API::Image &image)
API::Device & getDevice()
void setUsage(VkBufferUsageFlags usage)
void setLayerCount(uint32_t layerCount)
void setPreferedFormats(const std::set< VkFormat > &preferedFormats)
bool build(API::Image &instance, VkResult *returnResult=nullptr)
bool begin(VkCommandBufferUsageFlags flags=VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) const
void setArrayLayers(uint32_t arrayLayers)
void setLevelCount(uint32_t levelCount)
bool updateData(const void *data, VkDeviceSize size, VkDeviceSize offset=0) const