Skip to content

Commit

Permalink
Prefer sRGB formats by default, as per spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Dec 11, 2024
1 parent f05c2d5 commit c051384
Showing 1 changed file with 61 additions and 15 deletions.
76 changes: 61 additions & 15 deletions src/gpu/vulkan/SDL_gpu_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ static VkPresentModeKHR SDLToVK_PresentMode[] = {
VK_PRESENT_MODE_MAILBOX_KHR
};

typedef struct TextureFormatPair {
VkFormat vk;
SDL_GPUTextureFormat sdl;
} TextureFormatPair;

static TextureFormatPair SDLToVK_TextureFormat_SrgbOnly[] = {
{VK_FORMAT_R8G8B8A8_SRGB, SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB},
{VK_FORMAT_B8G8R8A8_SRGB, SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB},
{VK_FORMAT_BC1_RGBA_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM_SRGB},
{VK_FORMAT_BC2_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM_SRGB},
{VK_FORMAT_BC3_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM_SRGB},
{VK_FORMAT_BC7_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM_SRGB},
{VK_FORMAT_ASTC_4x4_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM_SRGB},
{VK_FORMAT_ASTC_5x4_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM_SRGB},
{VK_FORMAT_ASTC_5x5_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM_SRGB},
{VK_FORMAT_ASTC_6x5_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM_SRGB},
{VK_FORMAT_ASTC_6x6_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM_SRGB},
{VK_FORMAT_ASTC_8x5_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM_SRGB},
{VK_FORMAT_ASTC_8x6_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM_SRGB},
{VK_FORMAT_ASTC_8x8_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM_SRGB},
{VK_FORMAT_ASTC_10x5_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM_SRGB},
{VK_FORMAT_ASTC_10x6_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM_SRGB},
{VK_FORMAT_ASTC_10x8_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM_SRGB},
{VK_FORMAT_ASTC_10x10_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM_SRGB},
{VK_FORMAT_ASTC_12x10_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM_SRGB},
{VK_FORMAT_ASTC_12x12_SRGB_BLOCK, SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM_SRGB},
};

static VkFormat SDLToVK_TextureFormat[] = {
VK_FORMAT_UNDEFINED, // INVALID
VK_FORMAT_R8_UNORM, // A8_UNORM
Expand Down Expand Up @@ -6817,6 +6845,21 @@ static XrResult VULKAN_DestroyXRSwapchain(
return renderer->xr->xrDestroySwapchain(swapchain);
}

static bool VULKAN_INTERNAL_FindXRSrgbSwapchain(int64_t *supportedFormats, Uint32 numFormats, SDL_GPUTextureFormat *sdlFormat, int64_t *vkFormat)
{
for(Uint32 i = 0; i < SDL_arraysize(SDLToVK_TextureFormat_SrgbOnly); i++) {
for(Uint32 j = 0; j < numFormats; j++) {
if(SDLToVK_TextureFormat_SrgbOnly[i].vk == supportedFormats[j]) {
*sdlFormat = SDLToVK_TextureFormat_SrgbOnly[i].sdl;
*vkFormat = SDLToVK_TextureFormat_SrgbOnly[i].vk;
return true;
}
}
}

return false;
}

static XrResult VULKAN_CreateXRSwapchain(
SDL_GPURenderer *driverData,
XrSession session,
Expand All @@ -6842,34 +6885,37 @@ static XrResult VULKAN_CreateXRSwapchain(
return result;
}

int64_t format = VK_FORMAT_UNDEFINED;
/* Iterate over all formats the runtime support */
for(i = 0; i < num_supported_formats && format == VK_FORMAT_UNDEFINED; i++)
{
/* Iterate over all formats we support */
for(j = 0; j < SDL_arraysize(SDLToVK_TextureFormat); j++)
int64_t vkFormat = VK_FORMAT_UNDEFINED;
/* The OpenXR spec reccomends applications not submit linear data, so let's try to explicitly find an sRGB swapchain before we search the whole list */
if (!VULKAN_INTERNAL_FindXRSrgbSwapchain(supported_formats, num_supported_formats, textureFormat, &vkFormat)) {
/* Iterate over all formats the runtime support */
for(i = 0; i < num_supported_formats && vkFormat == VK_FORMAT_UNDEFINED; i++)
{
/* Pick the first format the runtime wants that we also support, the runtime should return these in order of preference */
if(SDLToVK_TextureFormat[j] == supported_formats[j])
/* Iterate over all formats we support */
for(j = 0; j < SDL_arraysize(SDLToVK_TextureFormat); j++)
{
format = supported_formats[j];
*textureFormat = j;
break;
/* Pick the first format the runtime wants that we also support, the runtime should return these in order of preference */
if(SDLToVK_TextureFormat[j] == supported_formats[j])
{
vkFormat = supported_formats[j];
*textureFormat = j;
break;
}
}
}
}

SDL_stack_free(num_supported_formats);

if(format == VK_FORMAT_UNDEFINED)
if(vkFormat == VK_FORMAT_UNDEFINED)
{
/* TODO: there needs to be a better way of returning SDL errors from here... */
SDL_SetError("Failed to find swapchain format supported by both the OpenXR runtime and SDL_gpu");
return XR_ERROR_RUNTIME_FAILURE;
}

XrSwapchainCreateInfo createInfo = *oldCreateInfo;
createInfo.format = format;
createInfo.format = vkFormat;

/* TODO: validate createInfo.usageFlags */

Expand Down Expand Up @@ -6935,11 +6981,11 @@ static XrResult VULKAN_CreateXRSwapchain(
texture,
i,
j,
format,
vkFormat,
texture->swizzle,
&texture->subresources[subresourceIndex].renderTargetViews[0])) {
VULKAN_INTERNAL_DestroyTexture(renderer, texture);
SDL_Log("piss and shit\n");
SDL_SetError("Failed to create render target view\n");
return XR_ERROR_RUNTIME_FAILURE; /* TODO: there's probably a better error for this.. */
}
}
Expand Down

0 comments on commit c051384

Please sign in to comment.