ITextureSP VKTS_APIENTRY textureCreate(const IInitialResourcesSP& initialResources, const std::string& name, const VkBool32 mipmap, const VkBool32 cubemap, const IMemoryImageSP& memoryImage, const VkSamplerCreateInfo& samplerCreateInfo, const VkImageViewCreateInfo& imageViewCreateInfo) { if (!memoryImage.get()) { return ITextureSP(); } // VkSamplerCreateInfo finalSamplerCreateInfo; memcpy(&finalSamplerCreateInfo, &samplerCreateInfo, sizeof(VkImageViewCreateInfo)); finalSamplerCreateInfo.maxLod = mipmap ? (float) memoryImage->getImageData()->getMipLevels() : 0.0f; auto sampler = samplerCreate(initialResources->getDevice()->getDevice(), finalSamplerCreateInfo.flags, finalSamplerCreateInfo.magFilter, finalSamplerCreateInfo.minFilter, finalSamplerCreateInfo.mipmapMode, samplerCreateInfo.addressModeU, samplerCreateInfo.addressModeV, finalSamplerCreateInfo.addressModeW, finalSamplerCreateInfo.mipLodBias, finalSamplerCreateInfo.anisotropyEnable, finalSamplerCreateInfo.maxAnisotropy, finalSamplerCreateInfo.compareEnable, finalSamplerCreateInfo.compareOp, finalSamplerCreateInfo.minLod, finalSamplerCreateInfo.maxLod, finalSamplerCreateInfo.borderColor, finalSamplerCreateInfo.unnormalizedCoordinates); if (!sampler.get()) { logPrint(VKTS_LOG_ERROR, "Texture: Could not create sampler."); return ITextureSP(); } // VkImageViewCreateInfo finalImageViewCreateInfo; memcpy(&finalImageViewCreateInfo, &imageViewCreateInfo, sizeof(VkImageViewCreateInfo)); finalImageViewCreateInfo.image = memoryImage->getImage()->getImage(); finalImageViewCreateInfo.viewType = cubemap ? VK_IMAGE_VIEW_TYPE_CUBE : VK_IMAGE_VIEW_TYPE_2D; finalImageViewCreateInfo.format = memoryImage->getImage()->getFormat(); finalImageViewCreateInfo.subresourceRange.levelCount = mipmap ? memoryImage->getImageData()->getMipLevels() : 1; finalImageViewCreateInfo.subresourceRange.layerCount = cubemap ? memoryImage->getImageData()->getArrayLayers() : 1; auto imageView = imageViewCreate(initialResources->getDevice()->getDevice(), finalImageViewCreateInfo.flags, finalImageViewCreateInfo.image, finalImageViewCreateInfo.viewType, finalImageViewCreateInfo.format, finalImageViewCreateInfo.components, finalImageViewCreateInfo.subresourceRange); if (!imageView.get()) { logPrint(VKTS_LOG_ERROR, "Texture: Could not create image view."); return ITextureSP(); } // auto newInstance = new Texture(initialResources, name, memoryImage, sampler, imageView); if (!newInstance) { newInstance->destroy(); return ITextureSP(); } return ITextureSP(newInstance); }
void vkeGameRendererDynamic::initFramebuffer(uint32_t inWidth, uint32_t inHeight){ /*---------------------------------------------------------- Create the framebuffer ----------------------------------------------------------*/ VulkanDC *dc = VulkanDC::Get(); VulkanDC::Device *device = dc->getDefaultDevice(); VulkanDC::Device::Queue *queue = dc->getDefaultQueue(); const VkFormat depthFmt = VK_FORMAT_D24_UNORM_S8_UINT; const VkFormat colorFmt = VK_FORMAT_R8G8B8A8_UNORM; /* If framebuffers already exist, release them. */ if (m_framebuffers[0] != VK_NULL_HANDLE){ releaseFramebuffer(); } /* Update the frame dimensions. */ m_width = inWidth; m_height = inHeight; /* Specify usage for the frame buffer attachments. */ VkImageUsageFlagBits gBufferFlags = (VkImageUsageFlagBits)(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); VkImageUsageFlagBits sBufferFlags = (VkImageUsageFlagBits)(VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT); /* Create the depth attachment image and image view. */ m_depth_attachment.format = depthFmt; imageCreateAndBind(&m_depth_attachment.image, &m_depth_attachment.memory, depthFmt, VK_IMAGE_TYPE_2D, m_width, m_height, 1, 1, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_IMAGE_TILING_OPTIMAL, getSamples()); imageViewCreate(&m_depth_attachment.view, m_depth_attachment.image, VK_IMAGE_VIEW_TYPE_2D, depthFmt); /* Create the color attachment image and image view. */ m_color_attachment.format = colorFmt; imageCreateAndBind(&m_color_attachment.image, &m_color_attachment.memory, colorFmt, VK_IMAGE_TYPE_2D, m_width, m_height, 1, 1, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, gBufferFlags, VK_IMAGE_TILING_OPTIMAL, getSamples()); imageViewCreate(&m_color_attachment.view, m_color_attachment.image, VK_IMAGE_VIEW_TYPE_2D, colorFmt); /* Create the resolve attachment image and image view. */ for (uint32_t i = 0; i < 2; ++i){ m_resolve_attachment[i].format = colorFmt; imageCreateAndBind(&m_resolve_attachment[i].image, &m_resolve_attachment[i].memory, colorFmt, VK_IMAGE_TYPE_2D, m_width, m_height, 1, 1, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, gBufferFlags, VK_IMAGE_TILING_OPTIMAL, VK_SAMPLE_COUNT_1_BIT); imageViewCreate(&m_resolve_attachment[i].view, m_resolve_attachment[i].image, VK_IMAGE_VIEW_TYPE_2D, colorFmt); } /* Put the image views into a temporary array to pass to the framebuffer create info struct. */ /* Setup the framebuffer create info struct. */ for (uint32_t i = 0; i < 2; ++i){ VkImageView views[] = { m_color_attachment.view, m_depth_attachment.view, m_resolve_attachment[i].view }; VkFramebufferCreateInfo fbInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO }; fbInfo.renderPass = m_render_pass; fbInfo.attachmentCount = 3; fbInfo.pAttachments = views; fbInfo.width = m_width; fbInfo.height = m_height; fbInfo.layers = 1; /* Create 2 framebuffers for ping pong. */ VKA_CHECK_ERROR(vkCreateFramebuffer(device->getVKDevice(), &fbInfo, NULL, &m_framebuffers[i]), "Could not create framebuffer.\n"); } }