#include "PipelineBuilder.h" #include "../GPU.h" PipelineBuilder& PipelineBuilder::Shaders(Shader* vertShader, Shader* fragShader) { this->vertShader = vertShader; this->fragShader = fragShader; return *this; } PipelineBuilder& PipelineBuilder::Format(SDL_GPUTextureFormat format) { this->format = format; return *this; } PipelineBuilder& PipelineBuilder::FormatSwapchain() { this->format = GPU::GetSwapchainFormat(); return *this; } PipelineBuilder& PipelineBuilder::Blend(SDL_GPUColorTargetBlendState blendState) { this->blendState = blendState; return *this; } PipelineBuilder& PipelineBuilder::BlendPremultiplied() { blendState = {}; blendState.alpha_blend_op = SDL_GPU_BLENDOP_ADD; blendState.color_blend_op = SDL_GPU_BLENDOP_ADD; blendState.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE; blendState.src_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE; blendState.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.enable_blend = true; return *this; } PipelineBuilder& PipelineBuilder::BlendAlpha() { blendState = {}; blendState.alpha_blend_op = SDL_GPU_BLENDOP_ADD; blendState.color_blend_op = SDL_GPU_BLENDOP_ADD; blendState.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE; blendState.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; blendState.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.enable_blend = true; return *this; } PipelineBuilder& PipelineBuilder::BlendErase() { blendState = {}; blendState.alpha_blend_op = SDL_GPU_BLENDOP_ADD; blendState.color_blend_op = SDL_GPU_BLENDOP_ADD; blendState.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ZERO; blendState.src_color_blendfactor = SDL_GPU_BLENDFACTOR_ZERO; blendState.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.enable_blend = true; return *this; } PipelineBuilder& PipelineBuilder::BlendAlphaKeepColor() { blendState = {}; blendState.alpha_blend_op = SDL_GPU_BLENDOP_ADD; blendState.color_blend_op = SDL_GPU_BLENDOP_ADD; blendState.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE; blendState.src_color_blendfactor = SDL_GPU_BLENDFACTOR_ZERO; blendState.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blendState.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE; blendState.enable_blend = true; return *this; } PipelineBuilder& PipelineBuilder::Vertex(SDL_GPUVertexInputState vertexInputState) { this->vertexInputState = vertexInputState; return *this; } PipelineBuilder& PipelineBuilder::Primitive(SDL_GPUPrimitiveType primitiveType) { this->primitiveType = primitiveType; return *this; } GraphicsPipeline* PipelineBuilder::Build() { SDL_GPUColorTargetDescription colorTargetDesciption = {}; colorTargetDesciption.format = format; colorTargetDesciption.blend_state = blendState; SDL_GPUGraphicsPipelineTargetInfo targetInfo = {}; targetInfo.num_color_targets = 1; targetInfo.color_target_descriptions = &colorTargetDesciption; SDL_GPUGraphicsPipelineCreateInfo createInfo = {}; createInfo.vertex_shader = vertShader->Get(); createInfo.fragment_shader = fragShader->Get(); createInfo.target_info = targetInfo; createInfo.vertex_input_state = vertexInputState; createInfo.primitive_type = primitiveType; SDL_GPUGraphicsPipeline* pipeline = SDL_CreateGPUGraphicsPipeline(GPU::GetDevice(), &createInfo); return new GraphicsPipeline(pipeline); }