Skip to content

processors/processing_processor_render.hpp

Namespaces

Name
sgns
sgns::sgprocessing
Artifact and manifest binary serialization.

Classes

Name
class sgns::sgprocessing::RenderProcessor

Source code

#pragma once
#include <vulkan/vulkan.h>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "processing_processor.hpp"
#include <RenderShaderUniform.hpp>
#include <Parameter.hpp>
#include <Stage.hpp>
#include <IndexType.hpp>
#include <RenderTarget.hpp>
#include <PipelineState.hpp>
#include <VertexLayoutEntry.hpp>
#include <TextureFilter.hpp>

namespace sgns::sgprocessing
{
    class RenderProcessor : public ProcessingProcessor
    {
    public:
        RenderProcessor() {}
        ~RenderProcessor() override = default;

        ProcessingResult StartProcessing( std::vector<std::vector<uint8_t>> &chunkhashes,
                           const sgns::IoDeclaration         &proc,
                           std::vector<char>                 &imageData,
                           std::vector<char>                 &modelFile,
                           const std::vector<sgns::Parameter> *parameters,
                           const ExecutionContext            &execCtx ) override;

        static bool IsAcceptable( VkPhysicalDeviceType type );

        bool InitializeContext();

        VkPhysicalDevice GetPhysicalDevice() const { return m_physicalDevice; }

    private:
        struct ParsedStage
        {
            sgns::Stage           stage;
            std::string           entry_point;
            std::vector<uint32_t> spirv;
        };

        struct ResolvedUniforms
        {
            std::vector<uint8_t> packedBytes;
            bool                 pushConstant = true;
        };

        static VkDeviceSize LargestDeviceLocalHeap( VkPhysicalDevice device );

        static bool ParseCompiledStages( const std::vector<char>  &modelFile,
                                          std::vector<ParsedStage> &outStages,
                                          ProcessingResult         &errorOut );

        static bool ParseRenderPassConfig(
            const std::vector<char>                                            &imageData,
            sgns::RenderTarget                                                 &outTarget,
            boost::optional<sgns::PipelineState>                               &outPipelineState,
            std::vector<sgns::VertexLayoutEntry>                               &outVertexLayout,
            boost::optional<std::map<std::string, sgns::RenderShaderUniform>>  &outUniforms,
            std::vector<uint8_t>                                               &outVertexBytes,
            bool                                                                &outHasIndex,
            sgns::IndexType                                                    &outIndexType,
            std::vector<uint8_t>                                               &outIndexBytes,
            uint32_t                                                           &outDataTransformCount,
            bool                                                                &outHasTextureBuffer,
            uint32_t                                                            &outTextureWidth,
            uint32_t                                                            &outTextureHeight,
            std::vector<uint8_t>                                               &outTextureBytes,
            ProcessingResult                                                   &errorOut );

        static bool ResolveUniforms(
            const boost::optional<std::map<std::string, sgns::RenderShaderUniform>> &uniforms,
            const std::vector<sgns::Parameter>                                      *parameters,
            ResolvedUniforms                                                        &outResolved,
            ProcessingResult                                                        &errorOut );

        static ProcessingResult MakeError( sgns::sgprocessing::ProcessingErrorStage stage, const std::string &message );

        bool CheckFormatSupport( VkFormat format, VkFormatFeatureFlagBits requiredFeature, ProcessingResult &errorOut );

        bool CreateBufferDedicated( VkDeviceSize          size,
                                     VkBufferUsageFlags    usage,
                                     VkMemoryPropertyFlags properties,
                                     VkBuffer              &outBuffer,
                                     VkDeviceMemory        &outMemory,
                                     ProcessingResult      &errorOut );

        bool CreateImageDedicated( const VkImageCreateInfo &imageInfo,
                                    VkMemoryPropertyFlags   properties,
                                    VkImage                 &outImage,
                                    VkDeviceMemory          &outMemory,
                                    ProcessingResult        &errorOut );

        static constexpr uint32_t kMaxRenderDimension = 8192;

        bool BuildRenderPass( const sgns::RenderTarget &target, ProcessingResult &errorOut );

        bool BuildFramebuffer( const sgns::RenderTarget &target, ProcessingResult &errorOut );

        bool BuildPipeline( const std::vector<ParsedStage>             &stages,
                             const std::vector<sgns::VertexLayoutEntry> &vertexLayout,
                             const boost::optional<sgns::PipelineState> &pipelineState,
                             const ResolvedUniforms                     &uniforms,
                             bool                                        hasTexture,
                             ProcessingResult                           &errorOut );

        bool UploadBuffers( const std::vector<uint8_t> &vertexBytes,
                             bool                        hasIndex,
                             sgns::IndexType             indexType,
                             const std::vector<uint8_t> &indexBytes,
                             uint32_t                    stride,
                             const ResolvedUniforms      &uniforms,
                             ProcessingResult            &errorOut );

        bool UploadTexture( const std::vector<uint8_t> &textureBytes,
                             uint32_t                    width,
                             uint32_t                    height,
                             sgns::TextureFilter         filter,
                             ProcessingResult           &errorOut );

        bool RecordAndSubmit( const sgns::RenderTarget &target, ProcessingResult &errorOut );

        bool Readback( const sgns::RenderTarget &target, std::vector<uint8_t> &outBytes, ProcessingResult &errorOut );

        static uint32_t ColorFormatByteSize( sgns::ColorFormat fmt );

        static VkFormat ToVkFormat( sgns::ColorFormat fmt );
        static VkFormat ToVkFormat( sgns::DepthFormat fmt );
        static VkFormat ToVkFormat( sgns::VertexLayoutFormat fmt );
        static VkPrimitiveTopology ToVkTopology( sgns::Topology t );
        static VkCullModeFlags ToVkCullMode( sgns::CullMode c );
        static VkFrontFace ToVkFrontFace( sgns::FrontFace f );
        static VkBool32 ToVkBool( sgns::DepthTest d );
        static VkBlendFactor ToVkBlendFactor( sgns::BlendFactor f );

        static uint32_t VertexFormatByteSize( sgns::VertexLayoutFormat f );

        VkInstance m_instance{VK_NULL_HANDLE};
        VkPhysicalDevice m_physicalDevice{VK_NULL_HANDLE};
        VkDevice m_device{VK_NULL_HANDLE};
        VkQueue m_queue{VK_NULL_HANDLE};
        uint32_t m_queueFamilyIndex{0};
        bool m_contextInitialized{false};

        uint32_t m_renderWidth{0};
        uint32_t m_renderHeight{0};

        VkRenderPass   m_renderPass{VK_NULL_HANDLE};
        VkFramebuffer  m_framebuffer{VK_NULL_HANDLE};
        VkImage        m_colorImage{VK_NULL_HANDLE}, m_depthImage{VK_NULL_HANDLE};
        VkImageView    m_colorView{VK_NULL_HANDLE}, m_depthView{VK_NULL_HANDLE};
        VkDeviceMemory m_colorMemory{VK_NULL_HANDLE}, m_depthMemory{VK_NULL_HANDLE};

        VkPipelineLayout      m_pipelineLayout{VK_NULL_HANDLE};
        VkPipeline            m_pipeline{VK_NULL_HANDLE};
        VkDescriptorSetLayout m_descriptorSetLayout{VK_NULL_HANDLE};
        VkDescriptorPool      m_descriptorPool{VK_NULL_HANDLE};
        VkDescriptorSet       m_descriptorSet{VK_NULL_HANDLE};

        VkBuffer       m_vertexBuffer{VK_NULL_HANDLE}, m_indexBuffer{VK_NULL_HANDLE};
        VkBuffer       m_uniformBuffer{VK_NULL_HANDLE}, m_stagingBuffer{VK_NULL_HANDLE};
        VkDeviceMemory m_vertexMemory{VK_NULL_HANDLE}, m_indexMemory{VK_NULL_HANDLE};
        VkDeviceMemory m_uniformMemory{VK_NULL_HANDLE}, m_stagingMemory{VK_NULL_HANDLE};

        VkBuffer       m_textureStagingBuffer{VK_NULL_HANDLE};
        VkDeviceMemory m_textureStagingMemory{VK_NULL_HANDLE};
        VkImage        m_textureImage{VK_NULL_HANDLE};
        VkDeviceMemory m_textureMemory{VK_NULL_HANDLE};
        VkImageView    m_textureView{VK_NULL_HANDLE};
        VkSampler      m_textureSampler{VK_NULL_HANDLE};
        bool           m_hasTexture{false};
        uint32_t       m_textureWidth{0}, m_textureHeight{0};

        VkCommandPool   m_commandPool{VK_NULL_HANDLE};
        VkCommandBuffer m_commandBuffer{VK_NULL_HANDLE};

        bool            m_hasIndexBuffer{false};
        sgns::IndexType m_indexType{sgns::IndexType::UINT32};
        uint32_t        m_vertexCount{0};
        uint32_t        m_indexCount{0};

        bool                 m_usePushConstant{false};
        std::vector<uint8_t> m_pushConstantBytes;
    };
}

Updated on 2026-09-17 at 06:29:15 +0000