GPU: Rework MSAA (#10859)

This commit is contained in:
Caleb Cornett
2024-09-16 12:19:09 -05:00
committed by GitHub
parent e59344ad46
commit 9416917353
7 changed files with 775 additions and 1110 deletions

View File

@@ -42,7 +42,7 @@ typedef struct RenderState
typedef struct WindowState
{
int angle_x, angle_y, angle_z;
SDL_GPUTexture *tex_depth, *tex_msaa;
SDL_GPUTexture *tex_depth, *tex_msaa, *tex_resolve;
Uint32 prev_drawablew, prev_drawableh;
} WindowState;
@@ -59,6 +59,7 @@ static void shutdownGPU(void)
WindowState *winstate = &window_states[i];
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_depth);
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_msaa);
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_resolve);
SDL_ReleaseWindowFromGPUDevice(gpu_device, state->windows[i]);
}
SDL_free(window_states);
@@ -249,20 +250,20 @@ static const VertexData vertex_data[] = {
static SDL_GPUTexture*
CreateDepthTexture(Uint32 drawablew, Uint32 drawableh)
{
SDL_GPUTextureCreateInfo depthtex_createinfo;
SDL_GPUTextureCreateInfo createinfo;
SDL_GPUTexture *result;
depthtex_createinfo.type = SDL_GPU_TEXTURETYPE_2D;
depthtex_createinfo.format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
depthtex_createinfo.width = drawablew;
depthtex_createinfo.height = drawableh;
depthtex_createinfo.layer_count_or_depth = 1;
depthtex_createinfo.num_levels = 1;
depthtex_createinfo.sample_count = render_state.sample_count;
depthtex_createinfo.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET;
depthtex_createinfo.props = 0;
createinfo.type = SDL_GPU_TEXTURETYPE_2D;
createinfo.format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
createinfo.width = drawablew;
createinfo.height = drawableh;
createinfo.layer_count_or_depth = 1;
createinfo.num_levels = 1;
createinfo.sample_count = render_state.sample_count;
createinfo.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET;
createinfo.props = 0;
result = SDL_CreateGPUTexture(gpu_device, &depthtex_createinfo);
result = SDL_CreateGPUTexture(gpu_device, &createinfo);
CHECK_CREATE(result, "Depth Texture")
return result;
@@ -271,29 +272,55 @@ CreateDepthTexture(Uint32 drawablew, Uint32 drawableh)
static SDL_GPUTexture*
CreateMSAATexture(Uint32 drawablew, Uint32 drawableh)
{
SDL_GPUTextureCreateInfo msaatex_createinfo;
SDL_GPUTextureCreateInfo createinfo;
SDL_GPUTexture *result;
if (render_state.sample_count == SDL_GPU_SAMPLECOUNT_1) {
return NULL;
}
msaatex_createinfo.type = SDL_GPU_TEXTURETYPE_2D;
msaatex_createinfo.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
msaatex_createinfo.width = drawablew;
msaatex_createinfo.height = drawableh;
msaatex_createinfo.layer_count_or_depth = 1;
msaatex_createinfo.num_levels = 1;
msaatex_createinfo.sample_count = render_state.sample_count;
msaatex_createinfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
msaatex_createinfo.props = 0;
createinfo.type = SDL_GPU_TEXTURETYPE_2D;
createinfo.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
createinfo.width = drawablew;
createinfo.height = drawableh;
createinfo.layer_count_or_depth = 1;
createinfo.num_levels = 1;
createinfo.sample_count = render_state.sample_count;
createinfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
createinfo.props = 0;
result = SDL_CreateGPUTexture(gpu_device, &msaatex_createinfo);
result = SDL_CreateGPUTexture(gpu_device, &createinfo);
CHECK_CREATE(result, "MSAA Texture")
return result;
}
static SDL_GPUTexture *
CreateResolveTexture(Uint32 drawablew, Uint32 drawableh)
{
SDL_GPUTextureCreateInfo createinfo;
SDL_GPUTexture *result;
if (render_state.sample_count == SDL_GPU_SAMPLECOUNT_1) {
return NULL;
}
createinfo.type = SDL_GPU_TEXTURETYPE_2D;
createinfo.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
createinfo.width = drawablew;
createinfo.height = drawableh;
createinfo.layer_count_or_depth = 1;
createinfo.num_levels = 1;
createinfo.sample_count = SDL_GPU_SAMPLECOUNT_1;
createinfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
createinfo.props = 0;
result = SDL_CreateGPUTexture(gpu_device, &createinfo);
CHECK_CREATE(result, "Resolve Texture")
return result;
}
static void
Render(SDL_Window *window, const int windownum)
{
@@ -354,8 +381,10 @@ Render(SDL_Window *window, const int windownum)
if (winstate->prev_drawablew != drawablew || winstate->prev_drawableh != drawableh) {
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_depth);
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_msaa);
SDL_ReleaseGPUTexture(gpu_device, winstate->tex_resolve);
winstate->tex_depth = CreateDepthTexture(drawablew, drawableh);
winstate->tex_msaa = CreateMSAATexture(drawablew, drawableh);
winstate->tex_resolve = CreateResolveTexture(drawablew, drawableh);
}
winstate->prev_drawablew = drawablew;
winstate->prev_drawableh = drawableh;
@@ -364,14 +393,25 @@ Render(SDL_Window *window, const int windownum)
SDL_zero(color_target);
color_target.clear_color.a = 1.0f;
color_target.load_op = SDL_GPU_LOADOP_CLEAR;
color_target.store_op = SDL_GPU_STOREOP_STORE;
color_target.texture = winstate->tex_msaa ? winstate->tex_msaa : swapchain;
if (winstate->tex_msaa) {
color_target.load_op = SDL_GPU_LOADOP_CLEAR;
color_target.store_op = SDL_GPU_STOREOP_RESOLVE;
color_target.texture = winstate->tex_msaa;
color_target.resolve_texture = winstate->tex_resolve;
color_target.cycle = SDL_TRUE;
color_target.cycle_resolve_texture = SDL_TRUE;
} else {
color_target.load_op = SDL_GPU_LOADOP_CLEAR;
color_target.store_op = SDL_GPU_STOREOP_STORE;
color_target.texture = swapchain;
}
SDL_zero(depth_target);
depth_target.clear_depth = 1.0f;
depth_target.load_op = SDL_GPU_LOADOP_CLEAR;
depth_target.store_op = SDL_GPU_STOREOP_DONT_CARE;
depth_target.stencil_load_op = SDL_GPU_LOADOP_DONT_CARE;
depth_target.stencil_store_op = SDL_GPU_STOREOP_DONT_CARE;
depth_target.texture = winstate->tex_depth;
depth_target.cycle = SDL_TRUE;
@@ -390,10 +430,10 @@ Render(SDL_Window *window, const int windownum)
SDL_DrawGPUPrimitives(pass, 36, 1, 0, 0);
SDL_EndGPURenderPass(pass);
/* Blit MSAA to swapchain, if needed */
/* Blit MSAA resolve target to swapchain, if needed */
if (render_state.sample_count > SDL_GPU_SAMPLECOUNT_1) {
SDL_zero(blit_info);
blit_info.source.texture = winstate->tex_msaa;
blit_info.source.texture = winstate->tex_resolve;
blit_info.source.w = drawablew;
blit_info.source.h = drawableh;
@@ -609,6 +649,7 @@ init_render_state(int msaa)
SDL_GetWindowSizeInPixels(state->windows[i], (int*) &drawablew, (int*) &drawableh);
winstate->tex_depth = CreateDepthTexture(drawablew, drawableh);
winstate->tex_msaa = CreateMSAATexture(drawablew, drawableh);
winstate->tex_resolve = CreateResolveTexture(drawablew, drawableh);
/* make each window different */
winstate->angle_x = (i * 10) % 360;