recreate descriptors

This commit is contained in:
tylermurphy534 2022-09-20 23:04:33 -04:00
parent 249f6c9fa3
commit b6438c6779
6 changed files with 103 additions and 95 deletions

View file

@ -12,11 +12,13 @@ namespace xe {
XeImage::XeImage(XeDevice &xeDevice, const std::string &filename) : xeDevice{xeDevice} {
createTextureImage(filename);
createTextureImageView();
}
XeImage::~XeImage() {
vkDestroyImage(xeDevice.device(), textureImage, nullptr);
vkFreeMemory(xeDevice.device(), textureImageMemory, nullptr);
vkDestroyImageView(xeDevice.device(), textureImageView, nullptr);
}
void XeImage::createTextureImage(const std::string &filename) {
@ -138,9 +140,9 @@ void XeImage::transitionImageLayout(VkImage image, VkFormat format, VkImageLayou
);
xeDevice.endSingleTimeCommands(commandBuffer);
}
}
void XeImage::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
void XeImage::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
VkCommandBuffer commandBuffer = xeDevice.beginSingleTimeCommands();
VkBufferImageCopy region{};
@ -161,6 +163,23 @@ void XeImage::transitionImageLayout(VkImage image, VkFormat format, VkImageLayou
vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
xeDevice.endSingleTimeCommands(commandBuffer);
}
void XeImage::createTextureImageView() {
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = textureImage;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_SRGB;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(xeDevice.device(), &viewInfo, nullptr, &textureImageView) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture image view!");
}
}
}

View file

@ -22,10 +22,12 @@ class XeImage {
void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
void createTextureImageView();
XeDevice &xeDevice;
VkImage textureImage;
VkImageView textureImageView;
VkDeviceMemory textureImageMemory;
friend class XeRenderSystem;

View file

@ -25,13 +25,13 @@ XeRenderSystem::XeRenderSystem(
XeImage *image
) : xeDevice{xeEngine.xeDevice},
xeRenderer{xeEngine.xeRenderer},
xeDescriptorPool{xeEngine.xeDescriptorPool},
pushCunstantDataSize{pushCunstantDataSize},
uniformBufferDataSize{uniformBufferDataSize},
textureSamplerBinding{image != nullptr} {
createDescriptorSetLayout();
createUniformBuffers();
createTextureImageView(image);
createDescriptorSets(*xeEngine.xeDescriptorPool);
createDescriptorSets(image);
createPipelineLayout();
createPipeline(xeRenderer.getSwapChainRenderPass(), vert, frag);
}
@ -41,7 +41,6 @@ XeRenderSystem::~XeRenderSystem() {
vkDestroyPipelineLayout(xeDevice.device(), pipelineLayout, nullptr);
if ( textureSamplerBinding ) {
vkDestroySampler(xeDevice.device(), textureSampler, nullptr);
vkDestroyImageView(xeDevice.device(), textureImageView, nullptr);
}
};
@ -100,34 +99,18 @@ void XeRenderSystem::createUniformBuffers() {
}
void XeRenderSystem::createTextureImageView(XeImage *image) {
if (!textureSamplerBinding) {
return;
}
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image->textureImage;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_SRGB;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(xeDevice.device(), &viewInfo, nullptr, &textureImageView) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture image view!");
}
}
void XeRenderSystem::createDescriptorSets(XeDescriptorPool &xeDescriptorPool) {
void XeRenderSystem::createDescriptorSets(XeImage *image) {
descriptorSets = std::vector<VkDescriptorSet>(XeSwapChain::MAX_FRAMES_IN_FLIGHT);
for (int i = 0; i < descriptorSets.size(); i++) {
auto bufferInfo = uboBuffers[i]->descriptorInfo();
XeDescriptorWriter writer{*xeDescriptorSetLayout, xeDescriptorPool};
updateDescriptorSet(image, i, true);
}
}
void XeRenderSystem::updateDescriptorSet(XeImage *image, int frameIndex, bool allocate) {
auto bufferInfo = uboBuffers[frameIndex]->descriptorInfo();
XeDescriptorWriter writer{*xeDescriptorSetLayout, *xeDescriptorPool};
int binding = 0;
@ -139,12 +122,15 @@ void XeRenderSystem::createDescriptorSets(XeDescriptorPool &xeDescriptorPool) {
if (textureSamplerBinding) {
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = textureImageView;
imageInfo.imageView = image->textureImageView;
imageInfo.sampler = textureSampler;
writer.writeImage(binding, &imageInfo);
binding += 1;
}
writer.build(descriptorSets[i]);
if (allocate) {
writer.build(descriptorSets[frameIndex]);
} else {
writer.overwrite(descriptorSets[frameIndex]);
}
}
@ -234,7 +220,7 @@ void XeRenderSystem::loadUniformObject(void *uniformBufferData) {
}
void XeRenderSystem::loadTexture(XeImage *image) {
// createTextureImageView(image);
updateDescriptorSet(image, xeRenderer.getFrameIndex(), false);
}
void XeRenderSystem::render(XeGameObject &gameObject) {

View file

@ -41,8 +41,8 @@ class XeRenderSystem {
void createDescriptorSetLayout();
void createUniformBuffers();
void createTextureImageView(XeImage *image);
void createDescriptorSets(XeDescriptorPool &xeDescriptorPool);
void createDescriptorSets(XeImage *image);
void updateDescriptorSet(XeImage *image, int frameIndex, bool allocate);
void createPipelineLayout();
void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag);
@ -61,9 +61,9 @@ class XeRenderSystem {
std::vector<VkDescriptorSet> descriptorSets;
VkSampler textureSampler;
VkImageView textureImageView;
VkPipelineLayout pipelineLayout;
std::unique_ptr<XeDescriptorPool> &xeDescriptorPool;
std::unique_ptr<XeDescriptorSetLayout> xeDescriptorSetLayout;
};

View file

@ -13,7 +13,7 @@ namespace xe {
class XeSwapChain {
public:
static constexpr int MAX_FRAMES_IN_FLIGHT = 3;
static constexpr int MAX_FRAMES_IN_FLIGHT = 2;
XeSwapChain(XeDevice &deviceRef, VkExtent2D windowExtent);
XeSwapChain(XeDevice &deviceRef, VkExtent2D windowExtent, std::shared_ptr<XeSwapChain> previous);

View file

@ -7,12 +7,13 @@ SimpleRenderer::SimpleRenderer(xe::XeEngine &xeEngine, xe::XeImage *xeImage)
void SimpleRenderer::render(std::vector<xe::XeGameObject> &gameObjects, xe::XeCamera &xeCamera, xe::XeImage *xeImage) {
xeRenderSystem.loadTexture(xeImage);
xeRenderSystem.start();
UniformBuffer ubo{};
ubo.projectionView = xeCamera.getProjection() * xeCamera.getView();
xeRenderSystem.loadUniformObject(&ubo);
xeRenderSystem.loadTexture(xeImage);
for(auto &obj : gameObjects) {
PushConstant pc{};