Program Listing for File PunctualShadowMap.ixx

Program Listing for File PunctualShadowMap.ixx#

↰ Return to documentation for file (Src/GraphicsEngineVulkan/scene/light/punctual_light/PunctualShadowMap.ixx)

module;
#include <array>
#include <cstdint>
#include <glm/glm.hpp>
#include <memory>
#include <span>
#include <vector>
#include <vulkan/vulkan.hpp>

export module kataglyphis.vulkan.punctual_shadow_map;

import kataglyphis.vulkan.device;
import kataglyphis.vulkan.texture;
import kataglyphis.vulkan.scene;
import kataglyphis.vulkan.frustum;
import kataglyphis.vulkan.buffer;
import kataglyphis.vulkan.descriptor_set_group;
// Re-exported: setSlots takes the slots assignPunctualShadowSlots returns.
export import kataglyphis.vulkan.punctual_light;

export namespace Kataglyphis {

inline constexpr uint32_t PUNCTUAL_SHADOW_RESOLUTION = 512;

inline constexpr vk::DeviceSize PUNCTUAL_SHADOW_SLOT_STRIDE = 512;

bool castsIntoPunctualShadow(const AABB &casterBounds, const glm::vec3 &lightPosition, float farPlane);

AABB sceneWorldBounds(Scene &scene);

class PunctualShadowMaps
{
  public:
    PunctualShadowMaps() = default;
    PunctualShadowMaps(const PunctualShadowMaps &) = delete;
    PunctualShadowMaps &operator=(const PunctualShadowMaps &) = delete;

    void init(const std::shared_ptr<VulkanDevice> &device,
      vk::DescriptorSetLayout sharedRenderDescriptorSetLayout,
      uint32_t swapChainImageCount);
    void createGraphicsPipeline();
    void shaderHotReload();

    void setSlots(std::span<const PunctualShadowSlot> slots);
    [[nodiscard]] uint32_t slotCount() const { return static_cast<uint32_t>(slots.size()); }
    [[nodiscard]] bool isSupported() const;

    void uploadFaceMatrices(uint32_t image_index);
    void recordCommands(vk::CommandBuffer commandBuffer,
      uint32_t image_index,
      Scene *scene,
      vk::DescriptorSet sharedRenderSet,
      bool cullingEnabled);
    void recordSkippedPass(vk::CommandBuffer commandBuffer);

    Texture *getCubeArray() const { return cubeArray.get(); }
    unsigned int getCastersDrawn() const { return castersDrawn; }
    unsigned int getCastersConsidered() const { return castersConsidered; }

    void cleanUp();
    ~PunctualShadowMaps() { cleanUp(); }

  private:
    std::shared_ptr<VulkanDevice> device{ nullptr };
    uint32_t swapChainImageCount{ 0 };
    vk::Format depthFormat{ vk::Format::eD32Sfloat };
    uint32_t resolution{ 1 };
    unsigned int castersDrawn{ 0 };
    unsigned int castersConsidered{ 0 };

    std::vector<PunctualShadowSlot> slots;
    std::array<glm::mat4, MAX_SHADOWED_PUNCTUAL_LIGHTS * 6> faceMatrices{};

    // Sampled through a cube-array view; each light also has a 2D-array view of its six layers to render into.
    std::unique_ptr<Texture> cubeArray;
    std::vector<vk::ImageView> slotViews;
    uint32_t cubeCount{ 0 };

    vk::Pipeline graphicsPipeline{};
    vk::PipelineLayout pipelineLayout{};
    DescriptorSetGroup faceMatrixDescriptors;
    // Owned by VulkanRenderer's sharedRenderDescriptors; bound at set 0 for the alpha test, never destroyed here.
    vk::DescriptorSetLayout sharedRenderDescriptorSetLayout{};
    std::vector<VulkanBuffer> faceMatrixBuffers;

    void createCubeArray(uint32_t cubes, uint32_t size);
    void releaseCubeArray();
    void buildGraphicsPipeline();
    void recordLight(vk::CommandBuffer commandBuffer,
      uint32_t slot,
      Scene *scene,
      std::span<const vk::DescriptorSet, 2> sets,
      bool cullingEnabled);
};

}// namespace Kataglyphis