From 10a89e8ea1fbcffc26fbc0bca6dd561f5379d4bf Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Tue, 17 Sep 2024 17:44:03 +0200 Subject: [PATCH] Add some validation for copy_texture_to_texture --- Sources/kope/graphics5/commandlist.c | 5 ++ Sources/kope/graphics5/device.c | 110 +----------------------- Sources/kope/graphics5/device.h | 52 +----------- Sources/kope/graphics5/g5unit.c | 1 + Sources/kope/graphics5/texture.h | 4 + Sources/kope/graphics5/textureformat.c | 111 +++++++++++++++++++++++++ Sources/kope/graphics5/textureformat.h | 65 +++++++++++++++ 7 files changed, 189 insertions(+), 159 deletions(-) create mode 100644 Sources/kope/graphics5/textureformat.c create mode 100644 Sources/kope/graphics5/textureformat.h diff --git a/Sources/kope/graphics5/commandlist.c b/Sources/kope/graphics5/commandlist.c index 13360d194..0bd670ae4 100644 --- a/Sources/kope/graphics5/commandlist.c +++ b/Sources/kope/graphics5/commandlist.c @@ -8,6 +8,8 @@ #include #endif +#include + void kope_g5_command_list_copy_buffer_to_texture(kope_g5_command_list *list, const kope_g5_image_copy_buffer *source, const kope_g5_image_copy_texture *destination, uint32_t width, uint32_t height, uint32_t depth_or_array_layers) { @@ -17,6 +19,9 @@ void kope_g5_command_list_copy_buffer_to_texture(kope_g5_command_list *list, con void kope_g5_command_list_copy_texture_to_texture(kope_g5_command_list *list, const kope_g5_image_copy_texture *source, const kope_g5_image_copy_texture *destination, uint32_t width, uint32_t height, uint32_t depth_or_array_layers) { +#ifdef KOPE_G5_VALIDATION + assert(source->texture->validation_format == destination->texture->validation_format); +#endif KOPE_G5_CALL6(command_list_copy_texture_to_texture, list, source, destination, width, height, depth_or_array_layers); } diff --git a/Sources/kope/graphics5/device.c b/Sources/kope/graphics5/device.c index 67f4471f6..916e7be7c 100644 --- a/Sources/kope/graphics5/device.c +++ b/Sources/kope/graphics5/device.c @@ -35,6 +35,8 @@ void kope_g5_device_create_texture(kope_g5_device *device, const kope_g5_texture if (kope_g5_texture_format_is_depth(parameters->format)) { assert(parameters->dimension != KOPE_G5_TEXTURE_DIMENSION_3D); } + + texture->validation_format = parameters->format; #endif KOPE_G5_CALL3(device_create_texture, device, parameters, texture); } @@ -47,114 +49,6 @@ void kope_g5_device_execute_command_list(kope_g5_device *device, kope_g5_command KOPE_G5_CALL2(device_execute_command_list, device, list); } -uint32_t kope_g5_texture_format_byte_size(kope_g5_texture_format format) { - switch (format) { - case KOPE_G5_TEXTURE_FORMAT_R8_UNORM: - return 1; - case KOPE_G5_TEXTURE_FORMAT_R8_SNORM: - return 1; - case KOPE_G5_TEXTURE_FORMAT_R8_UINT: - return 1; - case KOPE_G5_TEXTURE_FORMAT_R8_SINT: - return 1; - case KOPE_G5_TEXTURE_FORMAT_R16_UINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_R16_SINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_R16_FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG8_UNORM: - return 2; - case KOPE_G5_TEXTURE_FORMAT_RG8_SNORM: - return 2; - case KOPE_G5_TEXTURE_FORMAT_RG8_UINT: - return 2; - case KOPE_G5_TEXTURE_FORMAT_RG8_SINT: - return 2; - case KOPE_G5_TEXTURE_FORMAT_R32_UINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_R32_SINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_R32_FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG16_UINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG16_SINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG16_FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM_SRGB: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGBA8_SNORM: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGBA8_UINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGBA8_SINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM: - return 4; - case KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM_SRGB: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGB9E5U_FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGB10A2_UINT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RGB10A2_UNORM: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG11B10U_FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_RG32_UINT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RG32_SINT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RG32_FLOAT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RGBA16_UINT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RGBA16_SINT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RGBA16_FLOAT: - return 8; - case KOPE_G5_TEXTURE_FORMAT_RGBA32_UINT: - return 16; - case KOPE_G5_TEXTURE_FORMAT_RGBA32_SINT: - return 16; - case KOPE_G5_TEXTURE_FORMAT_RGBA32_FLOAT: - return 16; - // case KOPE_G5_TEXTURE_FORMAT_STENCIL8: - // return 1; - case KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM: - return 2; - case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8: - return 4; - case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8: - return 4; - case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT: - return 4; - case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24: - return 8; - } - - assert(false); - return 4; -} - -bool kope_g5_texture_format_is_depth(kope_g5_texture_format format) { - switch (format) { - // case KOPE_G5_TEXTURE_FORMAT_STENCIL8: - // return 1; - case KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM: - case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8: - case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8: - case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT: - case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24: - return true; - } - return false; -} - void kope_g5_device_create_sampler(kope_g5_device *device, const kope_g5_sampler_parameters *parameters, kope_g5_sampler *sampler) { KOPE_G5_CALL3(device_create_sampler, device, parameters, sampler); } diff --git a/Sources/kope/graphics5/device.h b/Sources/kope/graphics5/device.h index 53198f9aa..aacddff27 100644 --- a/Sources/kope/graphics5/device.h +++ b/Sources/kope/graphics5/device.h @@ -9,6 +9,7 @@ #include "buffer.h" #include "commandlist.h" #include "sampler.h" +#include "textureformat.h" #ifdef KOPE_DIRECT3D12 #include @@ -57,57 +58,6 @@ KOPE_FUNC void kope_g5_device_create_buffer(kope_g5_device *device, const kope_g typedef enum kope_g5_texture_dimension { KOPE_G5_TEXTURE_DIMENSION_1D, KOPE_G5_TEXTURE_DIMENSION_2D, KOPE_G5_TEXTURE_DIMENSION_3D } kope_g5_texture_dimension; -typedef enum kope_g5_texture_format { - KOPE_G5_TEXTURE_FORMAT_R8_UNORM, - KOPE_G5_TEXTURE_FORMAT_R8_SNORM, - KOPE_G5_TEXTURE_FORMAT_R8_UINT, - KOPE_G5_TEXTURE_FORMAT_R8_SINT, - KOPE_G5_TEXTURE_FORMAT_R16_UINT, - KOPE_G5_TEXTURE_FORMAT_R16_SINT, - KOPE_G5_TEXTURE_FORMAT_R16_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RG8_UNORM, - KOPE_G5_TEXTURE_FORMAT_RG8_SNORM, - KOPE_G5_TEXTURE_FORMAT_RG8_UINT, - KOPE_G5_TEXTURE_FORMAT_RG8_SINT, - KOPE_G5_TEXTURE_FORMAT_R32_UINT, - KOPE_G5_TEXTURE_FORMAT_R32_SINT, - KOPE_G5_TEXTURE_FORMAT_R32_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RG16_UINT, - KOPE_G5_TEXTURE_FORMAT_RG16_SINT, - KOPE_G5_TEXTURE_FORMAT_RG16_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM, - KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM_SRGB, - KOPE_G5_TEXTURE_FORMAT_RGBA8_SNORM, - KOPE_G5_TEXTURE_FORMAT_RGBA8_UINT, - KOPE_G5_TEXTURE_FORMAT_RGBA8_SINT, - KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM, - KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM_SRGB, - KOPE_G5_TEXTURE_FORMAT_RGB9E5U_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RGB10A2_UINT, - KOPE_G5_TEXTURE_FORMAT_RGB10A2_UNORM, - KOPE_G5_TEXTURE_FORMAT_RG11B10U_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RG32_UINT, - KOPE_G5_TEXTURE_FORMAT_RG32_SINT, - KOPE_G5_TEXTURE_FORMAT_RG32_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RGBA16_UINT, - KOPE_G5_TEXTURE_FORMAT_RGBA16_SINT, - KOPE_G5_TEXTURE_FORMAT_RGBA16_FLOAT, - KOPE_G5_TEXTURE_FORMAT_RGBA32_UINT, - KOPE_G5_TEXTURE_FORMAT_RGBA32_SINT, - KOPE_G5_TEXTURE_FORMAT_RGBA32_FLOAT, - // KOPE_G5_TEXTURE_FORMAT_STENCIL8, // not available in d3d12 - KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM, - KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8, - KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8, - KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT, - KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24, - // TODO: compressed formats -} kope_g5_texture_format; - -uint32_t kope_g5_texture_format_byte_size(kope_g5_texture_format format); - -bool kope_g5_texture_format_is_depth(kope_g5_texture_format format); - typedef enum kope_g5_texture_usage { KONG_G5_TEXTURE_USAGE_COPY_SRC = 0x01, KONG_G5_TEXTURE_USAGE_COPY_DST = 0x02, diff --git a/Sources/kope/graphics5/g5unit.c b/Sources/kope/graphics5/g5unit.c index 742eacb83..15b5d2f8d 100644 --- a/Sources/kope/graphics5/g5unit.c +++ b/Sources/kope/graphics5/g5unit.c @@ -6,3 +6,4 @@ #include "graphics.c" #include "sampler.c" #include "texture.c" +#include "textureformat.c" diff --git a/Sources/kope/graphics5/texture.h b/Sources/kope/graphics5/texture.h index 7e778ace8..0235d8a0f 100644 --- a/Sources/kope/graphics5/texture.h +++ b/Sources/kope/graphics5/texture.h @@ -4,6 +4,7 @@ #include #include "api.h" +#include "textureformat.h" #ifdef KOPE_DIRECT3D12 #include @@ -18,6 +19,9 @@ extern "C" { #endif typedef struct kope_g5_texture { +#ifdef KOPE_G5_VALIDATION + kope_g5_texture_format validation_format; +#endif KOPE_G5_IMPL(texture); } kope_g5_texture; diff --git a/Sources/kope/graphics5/textureformat.c b/Sources/kope/graphics5/textureformat.c new file mode 100644 index 000000000..fc5b5038e --- /dev/null +++ b/Sources/kope/graphics5/textureformat.c @@ -0,0 +1,111 @@ +#include "textureformat.h" + +#include + +uint32_t kope_g5_texture_format_byte_size(kope_g5_texture_format format) { + switch (format) { + case KOPE_G5_TEXTURE_FORMAT_R8_UNORM: + return 1; + case KOPE_G5_TEXTURE_FORMAT_R8_SNORM: + return 1; + case KOPE_G5_TEXTURE_FORMAT_R8_UINT: + return 1; + case KOPE_G5_TEXTURE_FORMAT_R8_SINT: + return 1; + case KOPE_G5_TEXTURE_FORMAT_R16_UINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_R16_SINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_R16_FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG8_UNORM: + return 2; + case KOPE_G5_TEXTURE_FORMAT_RG8_SNORM: + return 2; + case KOPE_G5_TEXTURE_FORMAT_RG8_UINT: + return 2; + case KOPE_G5_TEXTURE_FORMAT_RG8_SINT: + return 2; + case KOPE_G5_TEXTURE_FORMAT_R32_UINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_R32_SINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_R32_FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG16_UINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG16_SINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG16_FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM_SRGB: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGBA8_SNORM: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGBA8_UINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGBA8_SINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM: + return 4; + case KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM_SRGB: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGB9E5U_FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGB10A2_UINT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RGB10A2_UNORM: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG11B10U_FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_RG32_UINT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RG32_SINT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RG32_FLOAT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RGBA16_UINT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RGBA16_SINT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RGBA16_FLOAT: + return 8; + case KOPE_G5_TEXTURE_FORMAT_RGBA32_UINT: + return 16; + case KOPE_G5_TEXTURE_FORMAT_RGBA32_SINT: + return 16; + case KOPE_G5_TEXTURE_FORMAT_RGBA32_FLOAT: + return 16; + // case KOPE_G5_TEXTURE_FORMAT_STENCIL8: + // return 1; + case KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM: + return 2; + case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8: + return 4; + case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8: + return 4; + case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT: + return 4; + case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24: + return 8; + } + + assert(false); + return 4; +} + +bool kope_g5_texture_format_is_depth(kope_g5_texture_format format) { + switch (format) { + // case KOPE_G5_TEXTURE_FORMAT_STENCIL8: + // return 1; + case KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM: + case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8: + case KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8: + case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT: + case KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24: + return true; + } + return false; +} diff --git a/Sources/kope/graphics5/textureformat.h b/Sources/kope/graphics5/textureformat.h new file mode 100644 index 000000000..3e168a757 --- /dev/null +++ b/Sources/kope/graphics5/textureformat.h @@ -0,0 +1,65 @@ +#ifndef KOPE_G5_TEXTUREFORMAT_HEADER +#define KOPE_G5_TEXTUREFORMAT_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum kope_g5_texture_format { + KOPE_G5_TEXTURE_FORMAT_R8_UNORM, + KOPE_G5_TEXTURE_FORMAT_R8_SNORM, + KOPE_G5_TEXTURE_FORMAT_R8_UINT, + KOPE_G5_TEXTURE_FORMAT_R8_SINT, + KOPE_G5_TEXTURE_FORMAT_R16_UINT, + KOPE_G5_TEXTURE_FORMAT_R16_SINT, + KOPE_G5_TEXTURE_FORMAT_R16_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RG8_UNORM, + KOPE_G5_TEXTURE_FORMAT_RG8_SNORM, + KOPE_G5_TEXTURE_FORMAT_RG8_UINT, + KOPE_G5_TEXTURE_FORMAT_RG8_SINT, + KOPE_G5_TEXTURE_FORMAT_R32_UINT, + KOPE_G5_TEXTURE_FORMAT_R32_SINT, + KOPE_G5_TEXTURE_FORMAT_R32_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RG16_UINT, + KOPE_G5_TEXTURE_FORMAT_RG16_SINT, + KOPE_G5_TEXTURE_FORMAT_RG16_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM, + KOPE_G5_TEXTURE_FORMAT_RGBA8_UNORM_SRGB, + KOPE_G5_TEXTURE_FORMAT_RGBA8_SNORM, + KOPE_G5_TEXTURE_FORMAT_RGBA8_UINT, + KOPE_G5_TEXTURE_FORMAT_RGBA8_SINT, + KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM, + KOPE_G5_TEXTURE_FORMAT_BGRA8_UNORM_SRGB, + KOPE_G5_TEXTURE_FORMAT_RGB9E5U_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RGB10A2_UINT, + KOPE_G5_TEXTURE_FORMAT_RGB10A2_UNORM, + KOPE_G5_TEXTURE_FORMAT_RG11B10U_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RG32_UINT, + KOPE_G5_TEXTURE_FORMAT_RG32_SINT, + KOPE_G5_TEXTURE_FORMAT_RG32_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RGBA16_UINT, + KOPE_G5_TEXTURE_FORMAT_RGBA16_SINT, + KOPE_G5_TEXTURE_FORMAT_RGBA16_FLOAT, + KOPE_G5_TEXTURE_FORMAT_RGBA32_UINT, + KOPE_G5_TEXTURE_FORMAT_RGBA32_SINT, + KOPE_G5_TEXTURE_FORMAT_RGBA32_FLOAT, + // KOPE_G5_TEXTURE_FORMAT_STENCIL8, // not available in d3d12 + KOPE_G5_TEXTURE_FORMAT_DEPTH16_UNORM, + KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_NOTHING8, + KOPE_G5_TEXTURE_FORMAT_DEPTH24PLUS_STENCIL8, + KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT, + KOPE_G5_TEXTURE_FORMAT_DEPTH32FLOAT_STENCIL8_NOTHING24, + // TODO: compressed formats +} kope_g5_texture_format; + +uint32_t kope_g5_texture_format_byte_size(kope_g5_texture_format format); + +bool kope_g5_texture_format_is_depth(kope_g5_texture_format format); + +#ifdef __cplusplus +} +#endif + +#endif