diff --git a/include/SDL3/SDL_gpu.h b/include/SDL3/SDL_gpu.h index 565680f34c5dca..89d3711d1671f5 100644 --- a/include/SDL3/SDL_gpu.h +++ b/include/SDL3/SDL_gpu.h @@ -177,14 +177,6 @@ typedef enum SDL_GpuBufferUsageFlagBits typedef Uint32 SDL_GpuBufferUsageFlags; -typedef enum SDL_GpuTransferBufferMapFlagBits -{ - SDL_GPU_TRANSFER_MAP_READ = 0x00000001, - SDL_GPU_TRANSFER_MAP_WRITE = 0x00000002 -} SDL_GpuTransferBufferMapFlagBits; - -typedef Uint32 SDL_GpuTransferBufferMapFlags; - typedef enum SDL_GpuShaderStage { SDL_GPU_SHADERSTAGE_VERTEX, @@ -336,12 +328,6 @@ typedef enum SDL_GpuBorderColor SDL_GPU_BORDERCOLOR_INT_OPAQUE_WHITE } SDL_GpuBorderColor; -typedef enum SDL_GpuTransferUsage -{ - SDL_GPU_TRANSFERUSAGE_BUFFER, - SDL_GPU_TRANSFERUSAGE_TEXTURE -} SDL_GpuTransferUsage; - /* * VSYNC: * Waits for vblank before presenting. @@ -950,8 +936,7 @@ extern SDL_DECLSPEC SDL_GpuBuffer *SDLCALL SDL_GpuCreateBuffer( * Creates a transfer buffer to be used when uploading to or downloading from graphics resources. * * \param device a GPU Context - * \param usage specifies whether the transfer buffer will transfer buffers or textures - * \param mapFlags specify read-write options for the transfer buffer + * \param uploadOnly specifies that the transfer buffer will only be used for uploads, allows optimizations on certain backends * \param sizeInBytes the size of the transfer buffer * \returns a transfer buffer on success, or NULL on failure * @@ -965,8 +950,7 @@ extern SDL_DECLSPEC SDL_GpuBuffer *SDLCALL SDL_GpuCreateBuffer( */ extern SDL_DECLSPEC SDL_GpuTransferBuffer *SDLCALL SDL_GpuCreateTransferBuffer( SDL_GpuDevice *device, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, + SDL_bool uploadOnly, Uint32 sizeInBytes); /* Debug Naming */ diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 8214fafb96aa3d..eec3d4694e8d6e 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -1060,7 +1060,7 @@ SDL_DYNAPI_PROC(SDL_GpuSampler*,SDL_GpuCreateSampler,(SDL_GpuDevice *a, SDL_GpuS SDL_DYNAPI_PROC(SDL_GpuShader*,SDL_GpuCreateShader,(SDL_GpuDevice *a, SDL_GpuShaderCreateInfo *b),(a,b),return) SDL_DYNAPI_PROC(SDL_GpuTexture*,SDL_GpuCreateTexture,(SDL_GpuDevice *a, SDL_GpuTextureCreateInfo *b),(a,b),return) SDL_DYNAPI_PROC(SDL_GpuBuffer*,SDL_GpuCreateBuffer,(SDL_GpuDevice *a, SDL_GpuBufferUsageFlags b, Uint32 c),(a,b,c),return) -SDL_DYNAPI_PROC(SDL_GpuTransferBuffer*,SDL_GpuCreateTransferBuffer,(SDL_GpuDevice *a, SDL_GpuTransferUsage b, SDL_GpuTransferBufferMapFlags c, Uint32 d),(a,b,c,d),return) +SDL_DYNAPI_PROC(SDL_GpuTransferBuffer*,SDL_GpuCreateTransferBuffer,(SDL_GpuDevice *a, SDL_bool b, Uint32 c),(a,b,c),return) SDL_DYNAPI_PROC(void,SDL_GpuSetBufferName,(SDL_GpuDevice *a, SDL_GpuBuffer *b, const char *c),(a,b,c),) SDL_DYNAPI_PROC(void,SDL_GpuSetTextureName,(SDL_GpuDevice *a, SDL_GpuTexture *b, const char *c),(a,b,c),) SDL_DYNAPI_PROC(void,SDL_GpuSetStringMarker,(SDL_GpuCommandBuffer *a, const char *b),(a,b),) diff --git a/src/gpu/SDL_gpu.c b/src/gpu/SDL_gpu.c index e07b9506f92773..73422869be1f6b 100644 --- a/src/gpu/SDL_gpu.c +++ b/src/gpu/SDL_gpu.c @@ -444,15 +444,13 @@ SDL_GpuBuffer *SDL_GpuCreateBuffer( SDL_GpuTransferBuffer *SDL_GpuCreateTransferBuffer( SDL_GpuDevice *device, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, + SDL_bool uploadOnly, Uint32 sizeInBytes) { NULL_ASSERT(device) return device->CreateTransferBuffer( device->driverData, - usage, - mapFlags, + uploadOnly, sizeInBytes); } diff --git a/src/gpu/SDL_gpu_driver.h b/src/gpu/SDL_gpu_driver.h index 6bf1a603261cf4..1149604f54630e 100644 --- a/src/gpu/SDL_gpu_driver.h +++ b/src/gpu/SDL_gpu_driver.h @@ -231,8 +231,7 @@ struct SDL_GpuDevice SDL_GpuTransferBuffer *(*CreateTransferBuffer)( SDL_GpuRenderer *driverData, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, + SDL_bool uploadOnly, Uint32 sizeInBytes); /* Debug Naming */ diff --git a/src/gpu/d3d11/SDL_gpu_d3d11.c b/src/gpu/d3d11/SDL_gpu_d3d11.c index e269e84d9d3c03..3961dace7b306b 100644 --- a/src/gpu/d3d11/SDL_gpu_d3d11.c +++ b/src/gpu/d3d11/SDL_gpu_d3d11.c @@ -614,9 +614,6 @@ typedef struct D3D11TransferBuffer typedef struct D3D11TransferBufferContainer { - SDL_GpuTransferUsage usage; - SDL_GpuTransferBufferMapFlags mapFlags; - D3D11TransferBuffer *activeBuffer; /* These are all the buffers that have been used by this container. @@ -1885,7 +1882,7 @@ static D3D11Texture *D3D11_INTERNAL_CreateTexture( SDL_GpuTextureCreateInfo *textureCreateInfo, D3D11_SUBRESOURCE_DATA *initialData) { - Uint8 isSampler, isColorTarget, isDepthStencil, isMultisample, needSubresourceSRV, needSubresourceUAV; + Uint8 isSampler, isColorTarget, isDepthStencil, isMultisample, isStaging, needSubresourceSRV, needSubresourceUAV; DXGI_FORMAT format; ID3D11Resource *textureHandle; ID3D11ShaderResourceView *srv = NULL; @@ -1901,6 +1898,7 @@ static D3D11Texture *D3D11_INTERNAL_CreateTexture( needSubresourceUAV = (textureCreateInfo->usageFlags & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE_BIT); isMultisample = textureCreateInfo->sampleCount > SDL_GPU_SAMPLECOUNT_1; + isStaging = textureCreateInfo->usageFlags == 0; format = SDLToD3D11_TextureFormat[textureCreateInfo->format]; if (isDepthStencil) { @@ -1927,13 +1925,13 @@ static D3D11Texture *D3D11_INTERNAL_CreateTexture( desc2D.Width = textureCreateInfo->width; desc2D.Height = textureCreateInfo->height; desc2D.ArraySize = textureCreateInfo->isCube ? 6 : textureCreateInfo->layerCount; - desc2D.CPUAccessFlags = 0; + desc2D.CPUAccessFlags = isStaging ? D3D11_CPU_ACCESS_WRITE : 0; desc2D.Format = format; desc2D.MipLevels = textureCreateInfo->levelCount; desc2D.MiscFlags = textureCreateInfo->isCube ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0; desc2D.SampleDesc.Count = 1; desc2D.SampleDesc.Quality = 0; - desc2D.Usage = D3D11_USAGE_DEFAULT; + desc2D.Usage = isStaging ? D3D11_USAGE_STAGING : D3D11_USAGE_DEFAULT; res = ID3D11Device_CreateTexture2D( renderer->device, @@ -1991,11 +1989,11 @@ static D3D11Texture *D3D11_INTERNAL_CreateTexture( desc3D.Width = textureCreateInfo->width; desc3D.Height = textureCreateInfo->height; desc3D.Depth = textureCreateInfo->depth; - desc3D.CPUAccessFlags = 0; + desc3D.CPUAccessFlags = isStaging ? D3D11_CPU_ACCESS_WRITE : 0; desc3D.Format = format; desc3D.MipLevels = textureCreateInfo->levelCount; desc3D.MiscFlags = 0; - desc3D.Usage = D3D11_USAGE_DEFAULT; + desc3D.Usage = isStaging ? D3D11_USAGE_STAGING : D3D11_USAGE_DEFAULT; res = ID3D11Device_CreateTexture3D( renderer->device, @@ -2588,8 +2586,6 @@ static D3D11Buffer *D3D11_INTERNAL_PrepareBufferForWrite( static D3D11TransferBuffer *D3D11_INTERNAL_CreateTransferBuffer( D3D11Renderer *renderer, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, Uint32 sizeInBytes) { D3D11TransferBuffer *transferBuffer = SDL_malloc(sizeof(D3D11TransferBuffer)); @@ -2605,15 +2601,12 @@ static D3D11TransferBuffer *D3D11_INTERNAL_CreateTransferBuffer( /* This actually returns a container handle so we can rotate buffers on Cycle. */ static SDL_GpuTransferBuffer *D3D11_CreateTransferBuffer( SDL_GpuRenderer *driverData, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, + SDL_bool uploadOnly, /* ignored on D3D11 */ Uint32 sizeInBytes) { D3D11Renderer *renderer = (D3D11Renderer *)driverData; D3D11TransferBufferContainer *container = (D3D11TransferBufferContainer *)SDL_malloc(sizeof(D3D11TransferBufferContainer)); - container->usage = usage; - container->mapFlags = mapFlags; container->bufferCapacity = 1; container->bufferCount = 1; container->buffers = SDL_malloc( @@ -2621,8 +2614,6 @@ static SDL_GpuTransferBuffer *D3D11_CreateTransferBuffer( container->buffers[0] = D3D11_INTERNAL_CreateTransferBuffer( renderer, - usage, - mapFlags, sizeInBytes); container->activeBuffer = container->buffers[0]; @@ -2654,8 +2645,6 @@ static void D3D11_INTERNAL_CycleActiveTransferBuffer( container->buffers[container->bufferCount] = D3D11_INTERNAL_CreateTransferBuffer( renderer, - container->usage, - container->mapFlags, size); container->bufferCount += 1; diff --git a/src/gpu/metal/SDL_gpu_metal.m b/src/gpu/metal/SDL_gpu_metal.m index 02aafd95a0326d..1f21f22c0317d6 100644 --- a/src/gpu/metal/SDL_gpu_metal.m +++ b/src/gpu/metal/SDL_gpu_metal.m @@ -430,7 +430,8 @@ static MTLColorWriteMask SDLToMetal_ColorWriteMask( Uint32 bufferCount; MetalBuffer **buffers; - SDL_GpuTransferBufferMapFlags transferMapFlags; + SDL_bool isPrivate; + SDL_bool isWriteOnly; char *debugName; } MetalBufferContainer; @@ -1356,7 +1357,8 @@ static void METAL_INTERNAL_CycleActiveTexture( static MetalBufferContainer *METAL_INTERNAL_CreateBufferContainer( MetalRenderer *renderer, Uint32 sizeInBytes, - SDL_GpuTransferBufferMapFlags transferMapFlags) + SDL_bool isPrivate, + SDL_bool isWriteOnly) { MetalBufferContainer *container = SDL_malloc(sizeof(MetalBufferContainer)); MTLResourceOptions resourceOptions; @@ -1366,15 +1368,19 @@ static void METAL_INTERNAL_CycleActiveTexture( container->bufferCount = 1; container->buffers = SDL_malloc( container->bufferCapacity * sizeof(MetalBuffer *)); - container->transferMapFlags = transferMapFlags; + container->isPrivate = isPrivate; + container->isWriteOnly = isWriteOnly; container->debugName = NULL; - if (transferMapFlags == 0) { + if (isPrivate) { resourceOptions = MTLResourceStorageModePrivate; - } else if ((transferMapFlags & SDL_GPU_TRANSFER_MAP_WRITE) && !(transferMapFlags & SDL_GPU_TRANSFER_MAP_READ)) { - resourceOptions = MTLResourceCPUCacheModeWriteCombined; - } else { - resourceOptions = MTLResourceCPUCacheModeDefaultCache; + } else + { + if (isWriteOnly) { + resourceOptions = MTLResourceCPUCacheModeWriteCombined; + } else { + resourceOptions = MTLResourceCPUCacheModeDefaultCache; + } } container->buffers[0] = METAL_INTERNAL_CreateBuffer( @@ -1395,20 +1401,21 @@ static void METAL_INTERNAL_CycleActiveTexture( return (SDL_GpuBuffer *)METAL_INTERNAL_CreateBufferContainer( (MetalRenderer *)driverData, sizeInBytes, - 0); + SDL_TRUE, + SDL_FALSE); } static SDL_GpuTransferBuffer *METAL_CreateTransferBuffer( SDL_GpuRenderer *driverData, - SDL_GpuTransferUsage usage, - SDL_GpuTransferBufferMapFlags mapFlags, + SDL_bool uploadOnly, Uint32 sizeInBytes) { (void)usage; return (SDL_GpuTransferBuffer *)METAL_INTERNAL_CreateBufferContainer( (MetalRenderer *)driverData, sizeInBytes, - mapFlags); + SDL_FALSE, + uploadOnly); } static MetalUniformBuffer *METAL_INTERNAL_CreateUniformBuffer( @@ -1419,7 +1426,8 @@ static void METAL_INTERNAL_CycleActiveTexture( uniformBuffer->container = METAL_INTERNAL_CreateBufferContainer( renderer, sizeInBytes, - SDL_GPU_TRANSFER_MAP_WRITE); + SDL_FALSE, + SDL_TRUE); uniformBuffer->offset = 0; return uniformBuffer; } diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c index ffc3dcfefddd31..1078eaf92e9456 100644 --- a/src/gpu/vulkan/SDL_gpu_vulkan.c +++ b/src/gpu/vulkan/SDL_gpu_vulkan.c @@ -6765,8 +6765,7 @@ static VulkanUniformBuffer *VULKAN_INTERNAL_CreateUniformBuffer( static SDL_GpuTransferBuffer *VULKAN_CreateTransferBuffer( SDL_GpuRenderer *driverData, - SDL_GpuTransferUsage usage, /* ignored on Vulkan */ - SDL_GpuTransferBufferMapFlags mapFlags, /* ignored on Vulkan */ + SDL_bool uploadOnly, /* ignored on Vulkan */ Uint32 sizeInBytes) { return (SDL_GpuTransferBuffer *)VULKAN_INTERNAL_CreateBufferContainer( diff --git a/test/testgpu_spinning_cube.c b/test/testgpu_spinning_cube.c index 50d5afdd4ac29c..dac2a214e4a426 100644 --- a/test/testgpu_spinning_cube.c +++ b/test/testgpu_spinning_cube.c @@ -455,8 +455,7 @@ init_render_state(void) buf_transfer = SDL_GpuCreateTransferBuffer( gpu_device, - SDL_GPU_TRANSFERUSAGE_BUFFER, - SDL_GPU_TRANSFER_MAP_WRITE, + SDL_TRUE, sizeof(vertex_data) ); CHECK_CREATE(buf_transfer, "Vertex transfer buffer")