GPU: Add SDL_CalculateGPUTextureFormatSize (#11146)

---------

Co-authored-by: Sam Lantinga <slouken@libsdl.org>
This commit is contained in:
Evan Hemsley 2024-10-10 16:34:38 -07:00 committed by GitHub
parent 6d85127560
commit 6ea4a66451
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 34 additions and 13 deletions

View file

@ -3714,6 +3714,23 @@ extern SDL_DECLSPEC bool SDLCALL SDL_GPUTextureSupportsSampleCount(
SDL_GPUTextureFormat format, SDL_GPUTextureFormat format,
SDL_GPUSampleCount sample_count); SDL_GPUSampleCount sample_count);
/**
* Calculate the size in bytes of a texture format with dimensions.
*
* \param format a texture format.
* \param width width in pixels.
* \param height height in pixels.
* \param depth_or_layer_count depth for 3D textures or layer count otherwise.
* \returns the size of a texture with this format and dimensions.
*
* \since This function is available since SDL 3.1.5.
*/
extern SDL_DECLSPEC Uint32 SDLCALL SDL_CalculateGPUTextureFormatSize(
SDL_GPUTextureFormat format,
Uint32 width,
Uint32 height,
Uint32 depth_or_layer_count);
#ifdef SDL_PLATFORM_GDK #ifdef SDL_PLATFORM_GDK
/** /**

View file

@ -1178,6 +1178,7 @@ SDL3_0.0.0 {
SDL_wcstol; SDL_wcstol;
SDL_StepBackUTF8; SDL_StepBackUTF8;
SDL_DelayPrecise; SDL_DelayPrecise;
SDL_CalculateGPUTextureFormatSize;
# extra symbols go here (don't modify this line) # extra symbols go here (don't modify this line)
local: *; local: *;
}; };

View file

@ -1203,3 +1203,4 @@
#define SDL_wcstol SDL_wcstol_REAL #define SDL_wcstol SDL_wcstol_REAL
#define SDL_StepBackUTF8 SDL_StepBackUTF8_REAL #define SDL_StepBackUTF8 SDL_StepBackUTF8_REAL
#define SDL_DelayPrecise SDL_DelayPrecise_REAL #define SDL_DelayPrecise SDL_DelayPrecise_REAL
#define SDL_CalculateGPUTextureFormatSize SDL_CalculateGPUTextureFormatSize_REAL

View file

@ -1209,3 +1209,4 @@ SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),r
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return) SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(Uint32,SDL_StepBackUTF8,(const char *a, const char **b),(a,b),return) SDL_DYNAPI_PROC(Uint32,SDL_StepBackUTF8,(const char *a, const char **b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_DelayPrecise,(Uint64 a),(a),) SDL_DYNAPI_PROC(void,SDL_DelayPrecise,(Uint64 a),(a),)
SDL_DYNAPI_PROC(Uint32,SDL_CalculateGPUTextureFormatSize,(SDL_GPUTextureFormat a, Uint32 b, Uint32 c, Uint32 d),(a,b,c,d),return)

View file

@ -2796,3 +2796,16 @@ void SDL_ReleaseGPUFence(
device->driverData, device->driverData,
fence); fence);
} }
Uint32 SDL_CalculateGPUTextureFormatSize(
SDL_GPUTextureFormat format,
Uint32 width,
Uint32 height,
Uint32 depth_or_layer_count)
{
Uint32 blockWidth = Texture_GetBlockWidth(format);
Uint32 blockHeight = Texture_GetBlockHeight(format);
Uint32 blocksPerRow = (width + blockWidth - 1) / blockWidth;
Uint32 blocksPerColumn = (height + blockHeight - 1) / blockHeight;
return depth_or_layer_count * blocksPerRow * blocksPerColumn * SDL_GPUTextureFormatTexelBlockSize(format);
}

View file

@ -362,18 +362,6 @@ static inline Uint32 BytesPerRow(
return blocksPerRow * SDL_GPUTextureFormatTexelBlockSize(format); return blocksPerRow * SDL_GPUTextureFormatTexelBlockSize(format);
} }
static inline Sint32 BytesPerImage(
Uint32 width,
Uint32 height,
SDL_GPUTextureFormat format)
{
Uint32 blockWidth = Texture_GetBlockWidth(format);
Uint32 blockHeight = Texture_GetBlockHeight(format);
Uint32 blocksPerRow = (width + blockWidth - 1) / blockWidth;
Uint32 blocksPerColumn = (height + blockHeight - 1) / blockHeight;
return blocksPerRow * blocksPerColumn * SDL_GPUTextureFormatTexelBlockSize(format);
}
// GraphicsDevice Limits // GraphicsDevice Limits
#define MAX_TEXTURE_SAMPLERS_PER_STAGE 16 #define MAX_TEXTURE_SAMPLERS_PER_STAGE 16

View file

@ -1752,7 +1752,7 @@ static void METAL_UploadToTexture(
copyFromBuffer:bufferContainer->activeBuffer->handle copyFromBuffer:bufferContainer->activeBuffer->handle
sourceOffset:source->offset sourceOffset:source->offset
sourceBytesPerRow:BytesPerRow(destination->w, textureContainer->header.info.format) sourceBytesPerRow:BytesPerRow(destination->w, textureContainer->header.info.format)
sourceBytesPerImage:BytesPerImage(destination->w, destination->h, textureContainer->header.info.format) sourceBytesPerImage:SDL_CalculateGPUTextureFormatSize(textureContainer->header.info.format, destination->w, destination->h, destination->d)
sourceSize:MTLSizeMake(destination->w, destination->h, destination->d) sourceSize:MTLSizeMake(destination->w, destination->h, destination->d)
toTexture:metalTexture->handle toTexture:metalTexture->handle
destinationSlice:destination->layer destinationSlice:destination->layer