11#include <SDL3/SDL_gpu.h>
17 case SDL_GPU_SAMPLECOUNT_1:
19 case SDL_GPU_SAMPLECOUNT_2:
21 case SDL_GPU_SAMPLECOUNT_4:
23 case SDL_GPU_SAMPLECOUNT_8:
41 bool m_msaaEnabled =
false;
42 SDL_GPUTexture *swapchain{
nullptr};
44 void createMsaaTargets(SDL_GPUSampleCount samples);
45 void createRenderTargets(SDL_GPUTextureFormat suggested_depth_format);
56 SDL_GPUTextureFormat suggested_depth_format =
57 SDL_GPU_TEXTUREFORMAT_INVALID);
63 return (m_msaaEnabled && colorMsaa) ? colorMsaa : colorBuffer;
73 if (m_msaaEnabled && colorMsaa) {
74 return colorMsaa.sampleCount();
76 return SDL_GPU_SAMPLECOUNT_1;
80 if (samples > SDL_GPU_SAMPLECOUNT_1) {
82 createMsaaTargets(samples);
84 spdlog::info(
"MSAA enabled with {:d} samples", sample_size);
94 m_msaaEnabled =
false;
96 spdlog::info(
"MSAA disabled");
121 return SDL_GetGPUSwapchainTextureFormat(
device,
window);
128 SDL_GPUPresentMode present_mode) {
129 SDL_SetGPUSwapchainParameters(
device,
window, composition, present_mode);
132 SDL_GPUTextureFormat
colorFormat()
const {
return colorBuffer.format(); }
133 SDL_GPUTextureFormat
depthFormat()
const {
return depthBuffer.format(); }
161 void drawViews(SDL_GPURenderPass *pass, std::span<const MeshView> meshViews,
162 Uint32 numInstances = 1);
172 inline void draw(SDL_GPURenderPass *pass,
const Mesh &mesh,
173 Uint32 numInstances = 1) {
183 Uint32 numInstances = 1);
188 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
189 SDL_BindGPUVertexSamplers(pass, first_slot, bindings.data(),
190 Uint32(bindings.size()));
196 SDL_GPURenderPass *pass, Uint32 first_slot,
197 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
204 std::span<const SDL_GPUTextureSamplerBinding> bindings) {
205 SDL_BindGPUFragmentSamplers(pass, first_slot, bindings.data(),
206 Uint32(bindings.size()));
212 SDL_GPURenderPass *pass, Uint32 first_slot,
213 std::initializer_list<SDL_GPUTextureSamplerBinding> sampler_bindings) {
Definition CommandBuffer.h:16
A view into a Mesh object.
Definition Mesh.h:22
Handle class for meshes (vertex buffers and an optional index buffer) on the GPU.
Definition Mesh.h:57
std::span< const MeshView > views() const
Definition Mesh.h:92
Definition RenderContext.h:139
void drawView(SDL_GPURenderPass *pass, const MeshView &mesh, Uint32 numInstances=1)
Draw a MeshView.
void bindMeshView(SDL_GPURenderPass *pass, const MeshView &view)
Bind a MeshView object.
void draw(SDL_GPURenderPass *pass, const Mesh &mesh, Uint32 numInstances=1)
Definition RenderContext.h:172
void bindMesh(SDL_GPURenderPass *pass, const Mesh &mesh)
Bind a Mesh object.
void drawViews(SDL_GPURenderPass *pass, std::span< const MeshView > meshViews, Uint32 numInstances=1)
Render a collection of MeshView. This collection must satisfy the invariant that they all have the sa...
void bindFragmentSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:203
void bindVertexSamplers(SDL_GPURenderPass *pass, Uint32 first_slot, std::span< const SDL_GPUTextureSamplerBinding > bindings)
Bind multiple fragment shader samplers.
Definition RenderContext.h:187
constexpr int sdlSampleToValue(SDL_GPUSampleCount samples)
Definition RenderContext.h:15
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
constexpr NoInitT NoInit
Definition Tags.h:9
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
Tag type for non-initializing constructors (for e.g. RAII classes)
Definition Tags.h:6
Device device
Definition RenderContext.h:48
bool msaaEnabled() const
Definition RenderContext.h:70
CommandBuffer acquireCommandBuffer() const
Acquire the command buffer, starting a frame.
Definition RenderContext.h:102
const Texture & colorTarget() const
Definition RenderContext.h:62
RenderContext(RenderContext &&) noexcept=default
RenderContext(NoInitT)
Definition RenderContext.h:51
void enableMSAA(SDL_GPUSampleCount samples)
Definition RenderContext.h:79
bool waitAndAcquireSwapchain(CommandBuffer &command_buffer)
Wait until swapchain is available, then acquire it.
void presentToSwapchain(CommandBuffer &command_buffer)
Present the resolved texture to the swapchain. You must acquire the swapchain beforehand.
Window window
Definition RenderContext.h:49
bool initialized() const
Definition RenderContext.h:99
void disableMSAA()
Definition RenderContext.h:93
const Texture & resolvedColorTarget() const
Definition RenderContext.h:68
void setSwapchainParameters(SDL_GPUSwapchainComposition composition, SDL_GPUPresentMode present_mode)
Definition RenderContext.h:127
SDL_GPUTextureFormat depthFormat() const
Definition RenderContext.h:133
SDL_GPUTextureFormat getSwapchainTextureFormat() const
Definition RenderContext.h:120
bool waitForSwapchain()
Wait for the swapchain to be available.
Definition RenderContext.h:114
SDL_GPUTextureFormat colorFormat() const
Definition RenderContext.h:132
RenderContext(Device &&device, Window &&window, SDL_GPUTextureFormat suggested_depth_format=SDL_GPU_TEXTUREFORMAT_INVALID)
Constructor. The last argument (the depth texture format) is optional. By default,...
bool acquireSwapchain(CommandBuffer &command_buffer)
Acquire GPU swapchain.
SDL_GPUSampleCount getMsaaSampleCount() const
Definition RenderContext.h:72
const Texture & depthTarget() const
Definition RenderContext.h:66
bool hasDepthTexture() const
Check if a depth texture was created.
Definition RenderContext.h:125
RAII wrapper for the SDL_Window opaque type.
Definition Window.h:11