candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
GraphicsPipeline.h
Go to the documentation of this file.
1#pragma once
2
3#include "Core.h"
4#include "Tags.h"
5#include "errors.h"
6#include <SDL3/SDL_gpu.h>
7#include <vector>
8
9namespace candlewick {
10
16 SDL_GPUDevice *m_device{nullptr};
17 SDL_GPUGraphicsPipeline *m_pipeline{nullptr};
18 struct PipelineMetadata {
19 SDL_GPUPrimitiveType primitiveType;
20 std::vector<SDL_GPUColorTargetDescription> colorTargets;
21 SDL_GPUTextureFormat depthStencilFormat;
22 bool hasDepthStencilTarget;
23 SDL_GPUMultisampleState multisampleState;
24
25 PipelineMetadata(SDL_GPUGraphicsPipelineCreateInfo desc)
26 : primitiveType(desc.primitive_type)
27 , colorTargets(desc.target_info.color_target_descriptions,
28 desc.target_info.color_target_descriptions +
29 desc.target_info.num_color_targets)
30 , depthStencilFormat(desc.target_info.depth_stencil_format)
31 , hasDepthStencilTarget(desc.target_info.depth_stencil_format)
32 , multisampleState(desc.multisample_state) {}
33 PipelineMetadata() noexcept {}
34 } m_meta;
35
36public:
38 GraphicsPipeline(SDL_GPUDevice *device,
39 SDL_GPUGraphicsPipelineCreateInfo pipeline_desc,
40 const char *name)
41 : m_device(device), m_pipeline(nullptr), m_meta(pipeline_desc) {
42 if (name && !pipeline_desc.props) {
43 pipeline_desc.props = SDL_CreateProperties();
44 SDL_SetStringProperty(pipeline_desc.props,
45 SDL_PROP_GPU_GRAPHICSPIPELINE_CREATE_NAME_STRING,
46 name);
47 }
48 m_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &pipeline_desc);
49 if (!m_pipeline) {
50 terminate_with_message("Failed to create graphics pipeline: {:s}",
51 SDL_GetError());
52 }
53 }
54
55 bool initialized() const noexcept { return m_pipeline; }
56 SDL_GPUGraphicsPipeline *handle() const noexcept { return m_pipeline; }
57
60 : m_device(other.m_device)
61 , m_pipeline(other.m_pipeline)
62 , m_meta(std::move(other.m_meta)) {
63 other.m_device = nullptr;
64 other.m_pipeline = nullptr;
65 }
66
69 if (this != &other) {
70 this->release(); // release if we've got managed resources
71 m_device = other.m_device;
72 m_pipeline = other.m_pipeline;
73 m_meta = std::move(other.m_meta);
74
75 other.m_device = nullptr;
76 other.m_pipeline = nullptr;
77 }
78 return *this;
79 }
80
81 auto primitiveType() const noexcept { return m_meta.primitiveType; }
82 auto numColorTargets() const noexcept {
83 return Uint32(m_meta.colorTargets.size());
84 }
85
86 void bind(SDL_GPURenderPass *render_pass) const noexcept {
87 SDL_BindGPUGraphicsPipeline(render_pass, m_pipeline);
88 }
89
90 void release() noexcept {
91 if (m_device && m_pipeline)
92 SDL_ReleaseGPUGraphicsPipeline(m_device, m_pipeline);
93 m_pipeline = nullptr;
94 }
95
96 ~GraphicsPipeline() noexcept { this->release(); }
97};
98
99} // namespace candlewick
void release() noexcept
Definition GraphicsPipeline.h:90
GraphicsPipeline(SDL_GPUDevice *device, SDL_GPUGraphicsPipelineCreateInfo pipeline_desc, const char *name)
Definition GraphicsPipeline.h:38
auto primitiveType() const noexcept
Definition GraphicsPipeline.h:81
auto numColorTargets() const noexcept
Definition GraphicsPipeline.h:82
~GraphicsPipeline() noexcept
Definition GraphicsPipeline.h:96
GraphicsPipeline(GraphicsPipeline &&other) noexcept
Definition GraphicsPipeline.h:59
GraphicsPipeline & operator=(GraphicsPipeline &&other) noexcept
Definition GraphicsPipeline.h:68
GraphicsPipeline & operator=(const GraphicsPipeline &)=delete
GraphicsPipeline(NoInitT)
Definition GraphicsPipeline.h:37
bool initialized() const noexcept
Definition GraphicsPipeline.h:55
SDL_GPUGraphicsPipeline * handle() const noexcept
Definition GraphicsPipeline.h:56
GraphicsPipeline(const GraphicsPipeline &)=delete
void bind(SDL_GPURenderPass *render_pass) const noexcept
Definition GraphicsPipeline.h:86
Definition Camera.h:8
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6