Skip to content

Commit

Permalink
rework CreateTransferBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcosmonaut committed Jun 18, 2024
1 parent 5d1d376 commit a218038
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 60 deletions.
20 changes: 2 additions & 18 deletions include/SDL3/SDL_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
*
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),)
Expand Down
6 changes: 2 additions & 4 deletions src/gpu/SDL_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/gpu/SDL_gpu_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
25 changes: 7 additions & 18 deletions src/gpu/d3d11/SDL_gpu_d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -2605,24 +2601,19 @@ 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(
container->bufferCapacity * sizeof(D3D11TransferBuffer *));

container->buffers[0] = D3D11_INTERNAL_CreateTransferBuffer(
renderer,
usage,
mapFlags,
sizeInBytes);

container->activeBuffer = container->buffers[0];
Expand Down Expand Up @@ -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;

Expand Down
34 changes: 21 additions & 13 deletions src/gpu/metal/SDL_gpu_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ static MTLColorWriteMask SDLToMetal_ColorWriteMask(
Uint32 bufferCount;
MetalBuffer **buffers;

SDL_GpuTransferBufferMapFlags transferMapFlags;
SDL_bool isPrivate;
SDL_bool isWriteOnly;
char *debugName;
} MetalBufferContainer;

Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/gpu/vulkan/SDL_gpu_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions test/testgpu_spinning_cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a218038

Please sign in to comment.