Skip to content

Commit 78c82dd

Browse files
committed
Add TextureCommonHeader to vulkan/d3d11/metal
1 parent 441d5e7 commit 78c82dd

File tree

4 files changed

+65
-55
lines changed

4 files changed

+65
-55
lines changed

src/gpu/SDL_sysgpu.h

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ typedef struct CommandBufferCommonHeader
4343
SDL_bool submitted;
4444
} CommandBufferCommonHeader;
4545

46+
typedef struct TextureCommonHeader
47+
{
48+
SDL_GpuTextureCreateInfo info;
49+
} TextureCommonHeader;
50+
4651
/* Internal Helper Utilities */
4752

4853
static inline Sint32 Texture_GetBlockSize(

src/gpu/d3d11/SDL_gpu_d3d11.c

+35-34
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ struct D3D11Texture
453453

454454
typedef struct D3D11TextureContainer
455455
{
456-
SDL_GpuTextureCreateInfo createInfo;
456+
TextureCommonHeader header;
457+
457458
D3D11Texture *activeTexture;
458459
SDL_bool canBeCycled;
459460

@@ -2292,8 +2293,8 @@ static SDL_GpuTexture *D3D11_CreateTexture(
22922293
}
22932294

22942295
container = SDL_malloc(sizeof(D3D11TextureContainer));
2296+
container->header.info = *textureCreateInfo;
22952297
container->canBeCycled = 1;
2296-
container->createInfo = *textureCreateInfo;
22972298
container->activeTexture = texture;
22982299
container->textureCapacity = 1;
22992300
container->textureCount = 1;
@@ -2330,7 +2331,7 @@ static void D3D11_INTERNAL_CycleActiveTexture(
23302331

23312332
container->textures[container->textureCount] = D3D11_INTERNAL_CreateTexture(
23322333
renderer,
2333-
&container->createInfo,
2334+
&container->header.info,
23342335
NULL);
23352336
container->textureCount += 1;
23362337

@@ -2349,7 +2350,7 @@ static D3D11TextureSubresource *D3D11_INTERNAL_FetchTextureSubresource(
23492350
Uint32 layer,
23502351
Uint32 level)
23512352
{
2352-
Uint32 index = D3D11_INTERNAL_CalcSubresource(level, layer, container->createInfo.levelCount);
2353+
Uint32 index = D3D11_INTERNAL_CalcSubresource(level, layer, container->header.info.levelCount);
23532354
return &container->activeTexture->subresources[index];
23542355
}
23552356

@@ -2761,7 +2762,7 @@ static void D3D11_UploadToTexture(
27612762
D3D11TransferBufferContainer *srcTransferContainer = (D3D11TransferBufferContainer *)source->transferBuffer;
27622763
D3D11TransferBuffer *srcTransferBuffer = srcTransferContainer->activeBuffer;
27632764
D3D11TextureContainer *dstTextureContainer = (D3D11TextureContainer *)destination->textureSlice.texture;
2764-
SDL_GpuTextureFormat dstFormat = dstTextureContainer->createInfo.format;
2765+
SDL_GpuTextureFormat dstFormat = dstTextureContainer->header.info.format;
27652766
Uint32 bufferStride = source->imagePitch;
27662767
Uint32 bufferImageHeight = source->imageHeight;
27672768
Sint32 w = destination->w;
@@ -2902,7 +2903,7 @@ static void D3D11_DownloadFromTexture(
29022903
D3D11TransferBufferContainer *dstTransferContainer = (D3D11TransferBufferContainer *)destination->transferBuffer;
29032904
D3D11TransferBuffer *d3d11TransferBuffer = dstTransferContainer->activeBuffer;
29042905
D3D11TextureContainer *srcTextureContainer = (D3D11TextureContainer *)source->textureSlice.texture;
2905-
SDL_GpuTextureFormat srcFormat = srcTextureContainer->createInfo.format;
2906+
SDL_GpuTextureFormat srcFormat = srcTextureContainer->header.info.format;
29062907
D3D11_TEXTURE2D_DESC stagingDesc2D;
29072908
D3D11_TEXTURE3D_DESC stagingDesc3D;
29082909
D3D11TextureSubresource *textureSubresource = D3D11_INTERNAL_FetchTextureSubresource(
@@ -3531,7 +3532,7 @@ static void D3D11_BeginRenderPass(
35313532
d3d11CommandBuffer->colorTargetResolveTexture[i] = subresource->parent;
35323533
d3d11CommandBuffer->colorTargetResolveSubresourceIndex[i] = subresource->index;
35333534
d3d11CommandBuffer->colorTargetMsaaHandle[i] = subresource->msaaHandle;
3534-
d3d11CommandBuffer->colorTargetMsaaFormat[i] = SDLToD3D11_TextureFormat[container->createInfo.format];
3535+
d3d11CommandBuffer->colorTargetMsaaFormat[i] = SDLToD3D11_TextureFormat[container->header.info.format];
35353536

35363537
rtvs[i] = subresource->msaaTargetView;
35373538
} else {
@@ -3605,8 +3606,8 @@ static void D3D11_BeginRenderPass(
36053606
/* The viewport cannot be larger than the smallest attachment. */
36063607
for (Uint32 i = 0; i < colorAttachmentCount; i += 1) {
36073608
D3D11TextureContainer *container = (D3D11TextureContainer *)colorAttachmentInfos[i].textureSlice.texture;
3608-
Uint32 w = container->createInfo.width >> colorAttachmentInfos[i].textureSlice.mipLevel;
3609-
Uint32 h = container->createInfo.height >> colorAttachmentInfos[i].textureSlice.mipLevel;
3609+
Uint32 w = container->header.info.width >> colorAttachmentInfos[i].textureSlice.mipLevel;
3610+
Uint32 h = container->header.info.height >> colorAttachmentInfos[i].textureSlice.mipLevel;
36103611

36113612
if (w < vpWidth) {
36123613
vpWidth = w;
@@ -3619,8 +3620,8 @@ static void D3D11_BeginRenderPass(
36193620

36203621
if (depthStencilAttachmentInfo != NULL) {
36213622
D3D11TextureContainer *container = (D3D11TextureContainer *)depthStencilAttachmentInfo->textureSlice.texture;
3622-
Uint32 w = container->createInfo.width >> depthStencilAttachmentInfo->textureSlice.mipLevel;
3623-
Uint32 h = container->createInfo.height >> depthStencilAttachmentInfo->textureSlice.mipLevel;
3623+
Uint32 w = container->header.info.width >> depthStencilAttachmentInfo->textureSlice.mipLevel;
3624+
Uint32 h = container->header.info.height >> depthStencilAttachmentInfo->textureSlice.mipLevel;
36243625

36253626
if (w < vpWidth) {
36263627
vpWidth = w;
@@ -4243,7 +4244,7 @@ static void D3D11_Blit(
42434244
SDL_GpuViewport viewport;
42444245
SDL_GpuTextureSamplerBinding textureSamplerBinding;
42454246

4246-
if (destinationTextureContainer->createInfo.depth > 1) {
4247+
if (destinationTextureContainer->header.info.depth > 1) {
42474248
SDL_LogError(SDL_LOG_CATEGORY_GPU, "3D blit destination not implemented!");
42484249
return;
42494250
}
@@ -4256,11 +4257,11 @@ static void D3D11_Blit(
42564257

42574258
/* If the entire destination is blitted, we don't have to load */
42584259
if (
4259-
destinationTextureContainer->createInfo.layerCount == 1 &&
4260-
destinationTextureContainer->createInfo.levelCount == 1 &&
4261-
destination->w == destinationTextureContainer->createInfo.width &&
4262-
destination->h == destinationTextureContainer->createInfo.height &&
4263-
destination->d == destinationTextureContainer->createInfo.depth) {
4260+
destinationTextureContainer->header.info.layerCount == 1 &&
4261+
destinationTextureContainer->header.info.levelCount == 1 &&
4262+
destination->w == destinationTextureContainer->header.info.width &&
4263+
destination->h == destinationTextureContainer->header.info.height &&
4264+
destination->d == destinationTextureContainer->header.info.depth) {
42644265
colorAttachmentInfo.loadOp = SDL_GPU_LOADOP_DONT_CARE;
42654266
} else {
42664267
colorAttachmentInfo.loadOp = SDL_GPU_LOADOP_LOAD;
@@ -4289,14 +4290,14 @@ static void D3D11_Blit(
42894290
&viewport);
42904291

42914292
if (
4292-
sourceTextureContainer->createInfo.layerCount == 1 &&
4293-
sourceTextureContainer->createInfo.depth == 1) {
4293+
sourceTextureContainer->header.info.layerCount == 1 &&
4294+
sourceTextureContainer->header.info.depth == 1) {
42944295
/* 2D source */
42954296
D3D11_BindGraphicsPipeline(
42964297
commandBuffer,
42974298
renderer->blitFrom2DPipeline);
42984299
} else if (
4299-
sourceTextureContainer->createInfo.layerCount > 1) {
4300+
sourceTextureContainer->header.info.layerCount > 1) {
43004301
/* 2D array source */
43014302
D3D11_BindGraphicsPipeline(
43024303
commandBuffer,
@@ -4352,7 +4353,7 @@ static void D3D11_BeginComputePass(
43524353

43534354
for (i = 0; i < storageTextureBindingCount; i += 1) {
43544355
textureContainer = (D3D11TextureContainer *)storageTextureBindings[i].textureSlice.texture;
4355-
if (!(textureContainer->createInfo.usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE_BIT)) {
4356+
if (!(textureContainer->header.info.usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE_BIT)) {
43564357
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempted to bind read-only texture as compute write texture");
43574358
}
43584359

@@ -5225,16 +5226,16 @@ static SDL_bool D3D11_INTERNAL_CreateSwapchain(
52255226
windowData->textureContainer.textureCount = 1;
52265227
windowData->textureContainer.textureCapacity = 1;
52275228

5228-
windowData->textureContainer.createInfo.depth = 1;
5229-
windowData->textureContainer.createInfo.format = SwapchainCompositionToSDLTextureFormat[windowData->swapchainComposition];
5230-
windowData->textureContainer.createInfo.isCube = SDL_FALSE;
5231-
windowData->textureContainer.createInfo.layerCount = 1;
5232-
windowData->textureContainer.createInfo.levelCount = 1;
5233-
windowData->textureContainer.createInfo.sampleCount = SDL_GPU_SAMPLECOUNT_1;
5234-
windowData->textureContainer.createInfo.usageFlags = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET_BIT |
5235-
SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ_BIT |
5236-
SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ_BIT |
5237-
SDL_GPU_TEXTUREUSAGE_SAMPLER_BIT;
5229+
windowData->textureContainer.header.info.depth = 1;
5230+
windowData->textureContainer.header.info.format = SwapchainCompositionToSDLTextureFormat[windowData->swapchainComposition];
5231+
windowData->textureContainer.header.info.isCube = SDL_FALSE;
5232+
windowData->textureContainer.header.info.layerCount = 1;
5233+
windowData->textureContainer.header.info.levelCount = 1;
5234+
windowData->textureContainer.header.info.sampleCount = SDL_GPU_SAMPLECOUNT_1;
5235+
windowData->textureContainer.header.info.usageFlags = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET_BIT |
5236+
SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ_BIT |
5237+
SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ_BIT |
5238+
SDL_GPU_TEXTUREUSAGE_SAMPLER_BIT;
52385239

52395240
return SDL_TRUE;
52405241
}
@@ -5488,8 +5489,8 @@ static SDL_GpuTexture *D3D11_AcquireSwapchainTexture(
54885489
*pHeight = h;
54895490

54905491
/* Update the texture container dimensions */
5491-
windowData->textureContainer.createInfo.width = w;
5492-
windowData->textureContainer.createInfo.height = h;
5492+
windowData->textureContainer.header.info.width = w;
5493+
windowData->textureContainer.header.info.height = h;
54935494

54945495
/* Set up presentation */
54955496
if (d3d11CommandBuffer->windowDataCount == d3d11CommandBuffer->windowDataCapacity) {
@@ -5516,7 +5517,7 @@ static SDL_GpuTextureFormat D3D11_GetSwapchainTextureFormat(
55165517
return 0;
55175518
}
55185519

5519-
return windowData->textureContainer.createInfo.format;
5520+
return windowData->textureContainer.header.info.format;
55205521
}
55215522

55225523
static SDL_bool D3D11_SetSwapchainParameters(

src/gpu/metal/SDL_gpu_metal.m

+22-21
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ static MTLColorWriteMask SDLToMetal_ColorWriteMask(
337337

338338
typedef struct MetalTextureContainer
339339
{
340-
SDL_GpuTextureCreateInfo createInfo;
340+
TextureCommonHeader header;
341+
341342
MetalTexture *activeTexture;
342343
Uint8 canBeCycled;
343344

@@ -1409,7 +1410,7 @@ static SDL_GpuSampleCount METAL_GetBestSampleCount(
14091410

14101411
container = SDL_malloc(sizeof(MetalTextureContainer));
14111412
container->canBeCycled = 1;
1412-
container->createInfo = *textureCreateInfo;
1413+
container->header.info = *textureCreateInfo;
14131414
container->activeTexture = texture;
14141415
container->textureCapacity = 1;
14151416
container->textureCount = 1;
@@ -1448,7 +1449,7 @@ static SDL_GpuSampleCount METAL_GetBestSampleCount(
14481449

14491450
container->textures[container->textureCount] = METAL_INTERNAL_CreateTexture(
14501451
renderer,
1451-
&container->createInfo);
1452+
&container->header.info);
14521453
container->textureCount += 1;
14531454

14541455
container->activeTexture = container->textures[container->textureCount - 1];
@@ -1725,8 +1726,8 @@ static void METAL_UploadToTexture(
17251726
[metalCommandBuffer->blitEncoder
17261727
copyFromBuffer:bufferContainer->activeBuffer->handle
17271728
sourceOffset:source->offset
1728-
sourceBytesPerRow:BytesPerRow(destination->w, textureContainer->createInfo.format)
1729-
sourceBytesPerImage:BytesPerImage(destination->w, destination->h, textureContainer->createInfo.format)
1729+
sourceBytesPerRow:BytesPerRow(destination->w, textureContainer->header.info.format)
1730+
sourceBytesPerImage:BytesPerImage(destination->w, destination->h, textureContainer->header.info.format)
17301731
sourceSize:MTLSizeMake(destination->w, destination->h, destination->d)
17311732
toTexture:metalTexture->handle
17321733
destinationSlice:destination->textureSlice.layer
@@ -1844,7 +1845,7 @@ static void METAL_GenerateMipmaps(
18441845
MetalTextureContainer *container = (MetalTextureContainer *)texture;
18451846
MetalTexture *metalTexture = container->activeTexture;
18461847

1847-
if (container->createInfo.levelCount <= 1) {
1848+
if (container->header.info.levelCount <= 1) {
18481849
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Cannot generate mipmaps for texture with levelCount <= 1!");
18491850
return;
18501851
}
@@ -1892,7 +1893,7 @@ static void METAL_DownloadFromTexture(
18921893
bufferImageHeight = source->h;
18931894
}
18941895

1895-
bytesPerRow = BytesPerRow(bufferStride, textureContainer->createInfo.format);
1896+
bytesPerRow = BytesPerRow(bufferStride, textureContainer->header.info.format);
18961897
bytesPerDepthSlice = bytesPerRow * bufferImageHeight;
18971898

18981899
[metalCommandBuffer->blitEncoder
@@ -2198,7 +2199,7 @@ static void METAL_BeginRenderPass(
21982199
texture->msaaHandle ? 1 : 0);
21992200
passDescriptor.depthAttachment.clearDepth = depthStencilAttachmentInfo->depthStencilClearValue.depth;
22002201

2201-
if (IsStencilFormat(container->createInfo.format)) {
2202+
if (IsStencilFormat(container->header.info.format)) {
22022203
if (texture->msaaHandle) {
22032204
passDescriptor.stencilAttachment.texture = texture->msaaHandle;
22042205
passDescriptor.stencilAttachment.resolveTexture = texture->handle;
@@ -2222,8 +2223,8 @@ static void METAL_BeginRenderPass(
22222223
/* The viewport cannot be larger than the smallest attachment. */
22232224
for (Uint32 i = 0; i < colorAttachmentCount; i += 1) {
22242225
MetalTextureContainer *container = (MetalTextureContainer *)colorAttachmentInfos[i].textureSlice.texture;
2225-
Uint32 w = container->createInfo.width >> colorAttachmentInfos[i].textureSlice.mipLevel;
2226-
Uint32 h = container->createInfo.height >> colorAttachmentInfos[i].textureSlice.mipLevel;
2226+
Uint32 w = container->header.info.width >> colorAttachmentInfos[i].textureSlice.mipLevel;
2227+
Uint32 h = container->header.info.height >> colorAttachmentInfos[i].textureSlice.mipLevel;
22272228

22282229
if (w < vpWidth) {
22292230
vpWidth = w;
@@ -2236,8 +2237,8 @@ static void METAL_BeginRenderPass(
22362237

22372238
if (depthStencilAttachmentInfo != NULL) {
22382239
MetalTextureContainer *container = (MetalTextureContainer *)depthStencilAttachmentInfo->textureSlice.texture;
2239-
Uint32 w = container->createInfo.width >> depthStencilAttachmentInfo->textureSlice.mipLevel;
2240-
Uint32 h = container->createInfo.height >> depthStencilAttachmentInfo->textureSlice.mipLevel;
2240+
Uint32 w = container->header.info.width >> depthStencilAttachmentInfo->textureSlice.mipLevel;
2241+
Uint32 h = container->header.info.height >> depthStencilAttachmentInfo->textureSlice.mipLevel;
22412242

22422243
if (w < vpWidth) {
22432244
vpWidth = w;
@@ -2992,24 +2993,24 @@ static void METAL_Blit(
29922993

29932994
/* FIXME: cube copies? texture arrays? */
29942995

2995-
if (sourceTextureContainer->createInfo.depth > 1) {
2996+
if (sourceTextureContainer->header.info.depth > 1) {
29962997
SDL_LogError(SDL_LOG_CATEGORY_GPU, "3D blit source not implemented!");
29972998
return;
29982999
}
29993000

3000-
if (destinationTextureContainer->createInfo.depth > 1) {
3001+
if (destinationTextureContainer->header.info.depth > 1) {
30013002
SDL_LogError(SDL_LOG_CATEGORY_GPU, "3D blit destination not implemented!");
30023003
return;
30033004
}
30043005

3005-
if ((sourceTextureContainer->createInfo.usageFlags & SDL_GPU_TEXTUREUSAGE_SAMPLER_BIT) == 0) {
3006+
if ((sourceTextureContainer->header.info.usageFlags & SDL_GPU_TEXTUREUSAGE_SAMPLER_BIT) == 0) {
30063007
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Blit source texture must be created with SAMPLER bit!");
30073008
return;
30083009
}
30093010

30103011
pipeline = METAL_INTERNAL_FetchBlitPipeline(
30113012
renderer,
3012-
destinationTextureContainer->createInfo.format);
3013+
destinationTextureContainer->header.info.format);
30133014
if (pipeline == NULL) {
30143015
/* Drop the blit if the pipeline fetch failed! */
30153016
return;
@@ -3023,10 +3024,10 @@ static void METAL_Blit(
30233024

30243025
/* If the entire destination is blitted, we don't have to load */
30253026
if (
3026-
destinationTextureContainer->createInfo.levelCount == 1 &&
3027-
destination->w == destinationTextureContainer->createInfo.width &&
3028-
destination->h == destinationTextureContainer->createInfo.height &&
3029-
destination->d == destinationTextureContainer->createInfo.depth) {
3027+
destinationTextureContainer->header.info.levelCount == 1 &&
3028+
destination->w == destinationTextureContainer->header.info.width &&
3029+
destination->h == destinationTextureContainer->header.info.height &&
3030+
destination->d == destinationTextureContainer->header.info.depth) {
30303031
colorAttachmentInfo.loadOp = SDL_GPU_LOADOP_DONT_CARE;
30313032
} else {
30323033
colorAttachmentInfo.loadOp = SDL_GPU_LOADOP_LOAD;
@@ -3087,7 +3088,7 @@ static void METAL_BeginComputePass(
30873088

30883089
for (Uint32 i = 0; i < storageTextureBindingCount; i += 1) {
30893090
textureContainer = (MetalTextureContainer *)storageTextureBindings[i].textureSlice.texture;
3090-
if (!(textureContainer->createInfo.usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE_BIT)) {
3091+
if (!(textureContainer->header.info.usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE_BIT)) {
30913092
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Attempted to bind read-only texture as compute write texture");
30923093
}
30933094

src/gpu/vulkan/SDL_gpu_vulkan.c

+3
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ struct VulkanTexture
621621
*/
622622
struct VulkanTextureContainer
623623
{
624+
TextureCommonHeader header; /* FIXME: Use this instead of passing so many args to CreateTexture */
625+
624626
VulkanTextureHandle *activeTextureHandle;
625627

626628
/* These are all the texture handles that have been used by this container.
@@ -6905,6 +6907,7 @@ static SDL_GpuTexture *VULKAN_CreateTexture(
69056907
}
69066908

69076909
container = SDL_malloc(sizeof(VulkanTextureContainer));
6910+
container->header.info = *textureCreateInfo;
69086911
container->canBeCycled = 1;
69096912
container->activeTextureHandle = textureHandle;
69106913
container->textureCapacity = 1;

0 commit comments

Comments
 (0)