group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
Boilerplate.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <SDL3/SDL.h>
7#include <SDL3/SDL_gpu.h>
8
9#include <backends/imgui_impl_sdlgpu3.h>
10#include <cstddef>
11#include <vector>
12
13#define MAX_POINT_LIGHTS 8
14// macOS (Metal) caps the usable bindless/sampler+UBO budget lower than other
15// backends, so the moving-light array must stay at 56 there. Everywhere else
16// uses 64. Keep this in lockstep with the shader-side value, which is injected
17// at build time via -DMAX_MOVING_POINT_LIGHTS in CMakeLists.txt.
18#if defined(__APPLE__)
19#define MAX_MOVING_POINT_LIGHTS 56
20#else
21#define MAX_MOVING_POINT_LIGHTS 64
22#endif
23#define MAX_SHADOW_COUNT MAX_POINT_LIGHTS
24
25namespace Boilerplate
26{
27constexpr SDL_GPUColorTargetBlendState OVER_BLEND_MODE = {
28 .src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA,
29 .dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
30 .color_blend_op = SDL_GPU_BLENDOP_ADD,
31
32 .src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE,
33 .dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
34 .alpha_blend_op = SDL_GPU_BLENDOP_ADD,
35 .enable_blend = true,
36};
37
39{
40 const char* path = nullptr;
41 SDL_GPUShaderStage stage = SDL_GPU_SHADERSTAGE_VERTEX;
42 Uint32 samplerCount = 0;
46};
47
50{
51 // Uint32 vertexPitch = 0;
52 std::vector<SDL_GPUVertexBufferDescription> bufferDescriptions;
53 std::vector<SDL_GPUVertexAttribute> attributes;
54};
55
57{
58 const ShaderInfo* vertexShaderInfo = nullptr;
60
61 SDL_GPUShaderFormat shaderFormat;
62
64 const SDL_GPUColorTargetDescription* colorTarget;
65
66 bool reverseZ = false;
67 bool depthTest = false;
68 bool depthWrite = true;
69
70 // SDL_GPUCullMode cullMode = SDL_GPU_CULLMODE_BACK;
71 SDL_GPURasterizerState rasterizer_state{};
72};
73
76{
77 SDL_GPUBuffer* buffer = nullptr;
78 const void* data = nullptr;
79 size_t size = 0;
80};
81
86ImGui_ImplSDLGPU3_InitInfo createImGuiInfo(SDL_GPUDevice* device, SDL_Window* window);
87
94SDL_GPUVertexAttribute
95makeAttribute(Uint32 location, SDL_GPUVertexElementFormat format, Uint32 offset, Uint32 bufferSlot = 0);
96
101SDL_GPUColorTargetInfo makeColorTargetClear(SDL_GPUTexture* texture, SDL_FColor clearColor);
102
106SDL_GPUColorTargetInfo makeColorTargetLoad(SDL_GPUTexture* texture);
107
111SDL_GPUDepthStencilTargetInfo makeDepthTarget(SDL_GPUTexture* texture, Uint8 layer, bool store,bool reverseZ);
112
117SDL_GPUTextureSamplerBinding makeTextureSamplerBinding(SDL_GPUTexture* texture, SDL_GPUSampler* sampler);
118
122SDL_GPUShaderFormat selectShaderFormat(SDL_GPUDevice* device);
123
134SDL_GPUShader* loadShader(SDL_GPUDevice* device,
135 const char* path,
136 SDL_GPUShaderFormat format,
137 SDL_GPUShaderStage stage,
138 Uint32 samplerCount,
139 Uint32 uniformBufferCount,
140 Uint32 storageBufferCount,
141 Uint32 storageTextureCount);
142
148SDL_GPUShader* loadShader(SDL_GPUDevice* device, const ShaderInfo& shaderInfo, SDL_GPUShaderFormat format);
149
160SDL_GPUGraphicsPipeline* createGraphicsPipeline(SDL_GPUDevice* device,
161 SDL_GPUTextureFormat& colorFormat,
162 SDL_GPUShaderFormat shaderFormat,
163 const ShaderInfo& vertexShaderInfo,
164 const ShaderInfo& fragmentShaderInfo,
165 const VertexInputLayout& vertexInputLayout,
166 bool enableDepth = true,
167 bool overBlending = false);
168
170SDL_GPUGraphicsPipeline* createGraphicsPipeline(SDL_GPUDevice* device,
171 const std::vector<SDL_GPUTextureFormat>& colorFormats,
172 SDL_GPUShaderFormat shaderFormat,
173 const ShaderInfo& vertexShaderInfo,
174 const ShaderInfo& fragmentShaderInfo,
175 const VertexInputLayout& vertexInputLayout,
176 bool enableDepth = true,
177 bool overBlending = false);
178
179SDL_GPUGraphicsPipeline* createGraphicsDepthPipeline(SDL_GPUDevice* device, PipelineDescription pipelineDesc);
185SDL_GPUBuffer* createBuffer(SDL_GPUDevice* device, size_t bufferSize, SDL_GPUBufferUsageFlags usage);
186
192SDL_GPUTransferBuffer* createTransferBuffer(SDL_GPUDevice* device, size_t transferBufferSize, bool upload);
193
198SDL_GPUTransferBuffer* createUploadBuffer(SDL_GPUDevice* device, size_t transferBufferSize);
199
204void uploadBuffers(SDL_GPUDevice* device, SDL_GPUCommandBuffer* cmd, const std::vector<BufferUpload>& uploads);
205
213SDL_GPUTexture* createEmptyTextureD32F(SDL_GPUDevice* device, Uint32 width, Uint32 height, bool cube, Uint32 arraySize);
214
222SDL_GPUTexture* createTextureRGBA8(SDL_GPUDevice* device,
223 Uint32 width,
224 Uint32 height,
225 const void* data,
226 SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM);
227
228SDL_GPUTexture* createTextureRGBA32(SDL_GPUDevice* device, Uint32 width, Uint32 height, const void* data, SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT);
229
234SDL_GPUTexture* loadTexture(SDL_GPUDevice* device, const char* path);
235
241SDL_GPUTexture* createDepthTexture(SDL_GPUDevice* device, Uint32 width, Uint32 height);
242
244SDL_GPUTexture*
245createSampledColorTarget(SDL_GPUDevice* device, Uint32 width, Uint32 height, SDL_GPUTextureFormat format);
246
250SDL_GPUSampler* createLinearRepeatSampler(SDL_GPUDevice* device,bool nearest);
251
255SDL_GPUSampler* createLinearComparisonSampler(SDL_GPUDevice* device, SDL_GPUFilter filterMode,bool reverseZ);
256
260SDL_GPUSampler* createLinearClampSampler(SDL_GPUDevice* device);
261
262} // namespace Boilerplate
Definition Boilerplate.cpp:12
SDL_GPUColorTargetInfo makeColorTargetLoad(SDL_GPUTexture *texture)
Create a color render-target info with a clear color.
Definition Boilerplate.cpp:49
SDL_GPUShader * loadShader(SDL_GPUDevice *device, const char *path, SDL_GPUShaderFormat format, SDL_GPUShaderStage stage, Uint32 samplerCount, Uint32 uniformBufferCount, Uint32 storageBufferCount, Uint32 storageTextureCount)
Load and compile a shader from disk (explicit parameters).
Definition Boilerplate.cpp:101
SDL_GPUSampler * createLinearClampSampler(SDL_GPUDevice *device)
Create a linear-filtering, clamp-to-edge sampler.
Definition Boilerplate.cpp:632
void uploadBuffers(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, const std::vector< BufferUpload > &uploads)
Batch-upload multiple CPU buffers to their corresponding GPU buffers.
Definition Boilerplate.cpp:321
SDL_GPUDepthStencilTargetInfo makeDepthTarget(SDL_GPUTexture *texture, Uint8 layer, bool store, bool reverseZ)
Create a depth/stencil render-target info that clears to depth 1.0.
Definition Boilerplate.cpp:58
SDL_GPUColorTargetInfo makeColorTargetClear(SDL_GPUTexture *texture, SDL_FColor clearColor)
Create a color render-target info with a clear color.
Definition Boilerplate.cpp:39
SDL_GPUTransferBuffer * createUploadBuffer(SDL_GPUDevice *device, size_t transferBufferSize)
Allocate a GPU transfer buffer for uploading data.
Definition Boilerplate.cpp:316
SDL_GPUGraphicsPipeline * createGraphicsDepthPipeline(SDL_GPUDevice *device, PipelineDescription pipelineDesc)
Definition Boilerplate.cpp:159
constexpr SDL_GPUColorTargetBlendState OVER_BLEND_MODE
Definition Boilerplate.hpp:27
SDL_GPUBuffer * createBuffer(SDL_GPUDevice *device, size_t bufferSize, SDL_GPUBufferUsageFlags usage)
Allocate a GPU buffer of the given size and usage.
Definition Boilerplate.cpp:298
SDL_GPUTexture * createSampledColorTarget(SDL_GPUDevice *device, Uint32 width, Uint32 height, SDL_GPUTextureFormat format)
Create a sampleable 2D color render target of the given dimensions.
Definition Boilerplate.cpp:584
SDL_GPUSampler * createLinearRepeatSampler(SDL_GPUDevice *device, bool nearest)
Create a linear-filtering, repeat-addressing sampler.
Definition Boilerplate.cpp:603
SDL_GPUTexture * loadTexture(SDL_GPUDevice *device, const char *path)
Load an image file from disk and create a GPU texture from it.
Definition Boilerplate.cpp:539
SDL_GPUTransferBuffer * createTransferBuffer(SDL_GPUDevice *device, size_t transferBufferSize, bool upload)
Allocate a GPU transfer buffer for upload or download.
Definition Boilerplate.cpp:307
SDL_GPUTexture * createTextureRGBA32(SDL_GPUDevice *device, Uint32 width, Uint32 height, const void *data, SDL_GPUTextureFormat format)
Definition Boilerplate.cpp:418
SDL_GPUVertexAttribute makeAttribute(Uint32 location, SDL_GPUVertexElementFormat format, Uint32 offset, Uint32 bufferSlot)
Build an SDL_GPUVertexAttribute descriptor.
Definition Boilerplate.cpp:29
SDL_GPUTextureSamplerBinding makeTextureSamplerBinding(SDL_GPUTexture *texture, SDL_GPUSampler *sampler)
Create a texture-sampler binding pair for fragment shader use.
Definition Boilerplate.cpp:73
SDL_GPUTexture * createEmptyTextureD32F(SDL_GPUDevice *device, Uint32 width, Uint32 height, bool cube, Uint32 arraySize)
Create an empty depth texture, optionally cube/cube-array shaped.
Definition Boilerplate.cpp:372
SDL_GPUTexture * createTextureRGBA8(SDL_GPUDevice *device, Uint32 width, Uint32 height, const void *data, SDL_GPUTextureFormat format)
Create a 2D RGBA8 texture and upload pixel data to it.
Definition Boilerplate.cpp:479
ImGui_ImplSDLGPU3_InitInfo createImGuiInfo(SDL_GPUDevice *device, SDL_Window *window)
Create an ImGui initialization info struct for SDL3 GPU rendering.
Definition Boilerplate.cpp:19
SDL_GPUSampler * createLinearComparisonSampler(SDL_GPUDevice *device, SDL_GPUFilter filterMode, bool reverseZ)
Create a linear-filtering, comparison sampler (for shadow map depth comparison).
Definition Boilerplate.cpp:616
SDL_GPUShaderFormat selectShaderFormat(SDL_GPUDevice *device)
Pick the best available shader format for the given device.
Definition Boilerplate.cpp:81
SDL_GPUGraphicsPipeline * createGraphicsPipeline(SDL_GPUDevice *device, SDL_GPUTextureFormat &colorFormat, SDL_GPUShaderFormat shaderFormat, const ShaderInfo &vertexShaderInfo, const ShaderInfo &fragmentShaderInfo, const VertexInputLayout &vertexInputLayout, bool enableDepth, bool overBlending)
Create a full graphics pipeline from vertex/fragment shaders and vertex layout.
Definition Boilerplate.cpp:209
SDL_GPUTexture * createDepthTexture(SDL_GPUDevice *device, Uint32 width, Uint32 height)
Create a D32_FLOAT depth texture of the given dimensions.
Definition Boilerplate.cpp:564
Describes a pending CPU-to-GPU buffer upload: target buffer, source data, and byte size.
Definition Boilerplate.hpp:76
SDL_GPUBuffer * buffer
Definition Boilerplate.hpp:77
const void * data
Definition Boilerplate.hpp:78
size_t size
Definition Boilerplate.hpp:79
Definition Boilerplate.hpp:57
SDL_GPURasterizerState rasterizer_state
Definition Boilerplate.hpp:71
const ShaderInfo * fragmentShaderInfo
Definition Boilerplate.hpp:59
const VertexInputLayout * vertexInputLayout
Definition Boilerplate.hpp:63
bool depthTest
Definition Boilerplate.hpp:67
bool reverseZ
Definition Boilerplate.hpp:66
const SDL_GPUColorTargetDescription * colorTarget
Definition Boilerplate.hpp:64
SDL_GPUShaderFormat shaderFormat
Definition Boilerplate.hpp:61
const ShaderInfo * vertexShaderInfo
Definition Boilerplate.hpp:58
bool depthWrite
Definition Boilerplate.hpp:68
Descriptor for a single shader stage: file path, stage, and resource counts.
Definition Boilerplate.hpp:39
SDL_GPUShaderStage stage
Definition Boilerplate.hpp:41
Uint32 uniformBufferCount
Definition Boilerplate.hpp:43
Uint32 storageBufferCount
Definition Boilerplate.hpp:44
const char * path
Definition Boilerplate.hpp:40
Uint32 storageTextureCount
Definition Boilerplate.hpp:45
Uint32 samplerCount
Definition Boilerplate.hpp:42
Vertex buffer layout: stride (pitch) and per-attribute descriptions.
Definition Boilerplate.hpp:50
std::vector< SDL_GPUVertexAttribute > attributes
Definition Boilerplate.hpp:53
std::vector< SDL_GPUVertexBufferDescription > bufferDescriptions
Definition Boilerplate.hpp:52