Fixed crashes in Metal renderer due to ARC releasing references on random memory in newly allocated structs

This commit is contained in:
Sam Lantinga 2024-09-05 19:48:33 -07:00
parent e7969553f8
commit 8edb901724
2 changed files with 33 additions and 33 deletions

0
build-scripts/setup-gdk-desktop.py Normal file → Executable file
View File

View File

@ -971,7 +971,7 @@ static SDL_GPUComputePipeline *METAL_CreateComputePipeline(
return NULL; return NULL;
} }
pipeline = SDL_malloc(sizeof(MetalComputePipeline)); pipeline = SDL_calloc(1, sizeof(MetalComputePipeline));
pipeline->handle = handle; pipeline->handle = handle;
pipeline->readOnlyStorageTextureCount = pipelineCreateInfo->readOnlyStorageTextureCount; pipeline->readOnlyStorageTextureCount = pipelineCreateInfo->readOnlyStorageTextureCount;
pipeline->writeOnlyStorageTextureCount = pipelineCreateInfo->writeOnlyStorageTextureCount; pipeline->writeOnlyStorageTextureCount = pipelineCreateInfo->writeOnlyStorageTextureCount;
@ -1099,7 +1099,7 @@ static SDL_GPUGraphicsPipeline *METAL_CreateGraphicsPipeline(
return NULL; return NULL;
} }
result = SDL_malloc(sizeof(MetalGraphicsPipeline)); result = SDL_calloc(1, sizeof(MetalGraphicsPipeline));
result->handle = pipelineState; result->handle = pipelineState;
result->sampleMask = pipelineCreateInfo->multisampleState.sampleMask; result->sampleMask = pipelineCreateInfo->multisampleState.sampleMask;
result->depthStencilState = depthStencilState; result->depthStencilState = depthStencilState;
@ -1263,7 +1263,7 @@ static SDL_GPUSampler *METAL_CreateSampler(
return NULL; return NULL;
} }
metalSampler = (MetalSampler *)SDL_malloc(sizeof(MetalSampler)); metalSampler = (MetalSampler *)SDL_calloc(1, sizeof(MetalSampler));
metalSampler->handle = sampler; metalSampler->handle = sampler;
return (SDL_GPUSampler *)metalSampler; return (SDL_GPUSampler *)metalSampler;
} }
@ -1288,7 +1288,7 @@ static SDL_GPUShader *METAL_CreateShader(
return NULL; return NULL;
} }
result = SDL_malloc(sizeof(MetalShader)); result = SDL_calloc(1, sizeof(MetalShader));
result->library = libraryFunction.library; result->library = libraryFunction.library;
result->function = libraryFunction.function; result->function = libraryFunction.function;
result->samplerCount = shaderCreateInfo->samplerCount; result->samplerCount = shaderCreateInfo->samplerCount;
@ -1361,7 +1361,7 @@ static MetalTexture *METAL_INTERNAL_CreateTexture(
} }
} }
metalTexture = (MetalTexture *)SDL_malloc(sizeof(MetalTexture)); metalTexture = (MetalTexture *)SDL_calloc(1, sizeof(MetalTexture));
metalTexture->handle = texture; metalTexture->handle = texture;
metalTexture->msaaHandle = msaaTexture; metalTexture->msaaHandle = msaaTexture;
SDL_AtomicSet(&metalTexture->referenceCount, 0); SDL_AtomicSet(&metalTexture->referenceCount, 0);
@ -1398,14 +1398,14 @@ static SDL_GPUTexture *METAL_CreateTexture(
return NULL; return NULL;
} }
container = SDL_malloc(sizeof(MetalTextureContainer)); container = SDL_calloc(1, sizeof(MetalTextureContainer));
container->canBeCycled = 1; container->canBeCycled = 1;
container->header.info = *textureCreateInfo; container->header.info = *textureCreateInfo;
container->activeTexture = texture; container->activeTexture = texture;
container->textureCapacity = 1; container->textureCapacity = 1;
container->textureCount = 1; container->textureCount = 1;
container->textures = SDL_malloc( container->textures = SDL_calloc(
container->textureCapacity * sizeof(MetalTexture *)); container->textureCapacity, sizeof(MetalTexture *));
container->textures[0] = texture; container->textures[0] = texture;
container->debugName = NULL; container->debugName = NULL;
@ -1470,7 +1470,7 @@ static MetalBuffer *METAL_INTERNAL_CreateBuffer(
return NULL; return NULL;
} }
metalBuffer = SDL_malloc(sizeof(MetalBuffer)); metalBuffer = SDL_calloc(1, sizeof(MetalBuffer));
metalBuffer->handle = bufferHandle; metalBuffer->handle = bufferHandle;
SDL_AtomicSet(&metalBuffer->referenceCount, 0); SDL_AtomicSet(&metalBuffer->referenceCount, 0);
@ -1484,14 +1484,14 @@ static MetalBufferContainer *METAL_INTERNAL_CreateBufferContainer(
bool isPrivate, bool isPrivate,
bool isWriteOnly) bool isWriteOnly)
{ {
MetalBufferContainer *container = SDL_malloc(sizeof(MetalBufferContainer)); MetalBufferContainer *container = SDL_calloc(1, sizeof(MetalBufferContainer));
MTLResourceOptions resourceOptions; MTLResourceOptions resourceOptions;
container->size = sizeInBytes; container->size = sizeInBytes;
container->bufferCapacity = 1; container->bufferCapacity = 1;
container->bufferCount = 1; container->bufferCount = 1;
container->buffers = SDL_malloc( container->buffers = SDL_calloc(
container->bufferCapacity * sizeof(MetalBuffer *)); container->bufferCapacity, sizeof(MetalBuffer *));
container->isPrivate = isPrivate; container->isPrivate = isPrivate;
container->isWriteOnly = isWriteOnly; container->isWriteOnly = isWriteOnly;
container->debugName = NULL; container->debugName = NULL;
@ -1557,7 +1557,7 @@ static MetalUniformBuffer *METAL_INTERNAL_CreateUniformBuffer(
return NULL; return NULL;
} }
uniformBuffer = SDL_malloc(sizeof(MetalUniformBuffer)); uniformBuffer = SDL_calloc(1, sizeof(MetalUniformBuffer));
uniformBuffer->handle = bufferHandle; uniformBuffer->handle = bufferHandle;
uniformBuffer->writeOffset = 0; uniformBuffer->writeOffset = 0;
uniformBuffer->drawOffset = 0; uniformBuffer->drawOffset = 0;
@ -1906,19 +1906,19 @@ static void METAL_INTERNAL_AllocateCommandBuffers(
commandBuffer->windowDataCapacity = 1; commandBuffer->windowDataCapacity = 1;
commandBuffer->windowDataCount = 0; commandBuffer->windowDataCount = 0;
commandBuffer->windowDatas = SDL_malloc( commandBuffer->windowDatas = SDL_calloc(
commandBuffer->windowDataCapacity * sizeof(MetalWindowData *)); commandBuffer->windowDataCapacity, sizeof(MetalWindowData *));
// Reference Counting // Reference Counting
commandBuffer->usedBufferCapacity = 4; commandBuffer->usedBufferCapacity = 4;
commandBuffer->usedBufferCount = 0; commandBuffer->usedBufferCount = 0;
commandBuffer->usedBuffers = SDL_malloc( commandBuffer->usedBuffers = SDL_calloc(
commandBuffer->usedBufferCapacity * sizeof(MetalBuffer *)); commandBuffer->usedBufferCapacity, sizeof(MetalBuffer *));
commandBuffer->usedTextureCapacity = 4; commandBuffer->usedTextureCapacity = 4;
commandBuffer->usedTextureCount = 0; commandBuffer->usedTextureCount = 0;
commandBuffer->usedTextures = SDL_malloc( commandBuffer->usedTextures = SDL_calloc(
commandBuffer->usedTextureCapacity * sizeof(MetalTexture *)); commandBuffer->usedTextureCapacity, sizeof(MetalTexture *));
renderer->availableCommandBuffers[renderer->availableCommandBufferCount] = commandBuffer; renderer->availableCommandBuffers[renderer->availableCommandBufferCount] = commandBuffer;
renderer->availableCommandBufferCount += 1; renderer->availableCommandBufferCount += 1;
@ -1947,7 +1947,7 @@ static Uint8 METAL_INTERNAL_CreateFence(
{ {
MetalFence *fence; MetalFence *fence;
fence = SDL_malloc(sizeof(MetalFence)); fence = SDL_calloc(1, sizeof(MetalFence));
SDL_AtomicSet(&fence->complete, 0); SDL_AtomicSet(&fence->complete, 0);
// Add it to the available pool // Add it to the available pool
@ -3794,8 +3794,8 @@ static void METAL_INTERNAL_InitBlitResources(
// Allocate the dynamic blit pipeline list // Allocate the dynamic blit pipeline list
renderer->blitPipelineCapacity = 2; renderer->blitPipelineCapacity = 2;
renderer->blitPipelineCount = 0; renderer->blitPipelineCount = 0;
renderer->blitPipelines = SDL_malloc( renderer->blitPipelines = SDL_calloc(
renderer->blitPipelineCapacity * sizeof(BlitPipelineCacheEntry)); renderer->blitPipelineCapacity, sizeof(BlitPipelineCacheEntry));
// Fullscreen vertex shader // Fullscreen vertex shader
SDL_zero(shaderModuleCreateInfo); SDL_zero(shaderModuleCreateInfo);
@ -3980,14 +3980,14 @@ static SDL_GPUDevice *METAL_CreateDevice(bool debugMode, bool preferLowPower, SD
// Create fence pool // Create fence pool
renderer->availableFenceCapacity = 2; renderer->availableFenceCapacity = 2;
renderer->availableFences = SDL_malloc( renderer->availableFences = SDL_calloc(
sizeof(MetalFence *) * renderer->availableFenceCapacity); renderer->availableFenceCapacity, sizeof(MetalFence *));
// Create uniform buffer pool // Create uniform buffer pool
renderer->uniformBufferPoolCapacity = 32; renderer->uniformBufferPoolCapacity = 32;
renderer->uniformBufferPoolCount = 32; renderer->uniformBufferPoolCount = 32;
renderer->uniformBufferPool = SDL_malloc( renderer->uniformBufferPool = SDL_calloc(
renderer->uniformBufferPoolCapacity * sizeof(MetalUniformBuffer *)); renderer->uniformBufferPoolCapacity, sizeof(MetalUniformBuffer *));
for (Uint32 i = 0; i < renderer->uniformBufferPoolCount; i += 1) { for (Uint32 i = 0; i < renderer->uniformBufferPoolCount; i += 1) {
renderer->uniformBufferPool[i] = METAL_INTERNAL_CreateUniformBuffer( renderer->uniformBufferPool[i] = METAL_INTERNAL_CreateUniformBuffer(
@ -3998,23 +3998,23 @@ static SDL_GPUDevice *METAL_CreateDevice(bool debugMode, bool preferLowPower, SD
// Create deferred destroy arrays // Create deferred destroy arrays
renderer->bufferContainersToDestroyCapacity = 2; renderer->bufferContainersToDestroyCapacity = 2;
renderer->bufferContainersToDestroyCount = 0; renderer->bufferContainersToDestroyCount = 0;
renderer->bufferContainersToDestroy = SDL_malloc( renderer->bufferContainersToDestroy = SDL_calloc(
renderer->bufferContainersToDestroyCapacity * sizeof(MetalBufferContainer *)); renderer->bufferContainersToDestroyCapacity, sizeof(MetalBufferContainer *));
renderer->textureContainersToDestroyCapacity = 2; renderer->textureContainersToDestroyCapacity = 2;
renderer->textureContainersToDestroyCount = 0; renderer->textureContainersToDestroyCount = 0;
renderer->textureContainersToDestroy = SDL_malloc( renderer->textureContainersToDestroy = SDL_calloc(
renderer->textureContainersToDestroyCapacity * sizeof(MetalTextureContainer *)); renderer->textureContainersToDestroyCapacity, sizeof(MetalTextureContainer *));
// Create claimed window list // Create claimed window list
renderer->claimedWindowCapacity = 1; renderer->claimedWindowCapacity = 1;
renderer->claimedWindows = SDL_malloc( renderer->claimedWindows = SDL_calloc(
sizeof(MetalWindowData *) * renderer->claimedWindowCapacity); renderer->claimedWindowCapacity, sizeof(MetalWindowData *));
// Initialize blit resources // Initialize blit resources
METAL_INTERNAL_InitBlitResources(renderer); METAL_INTERNAL_InitBlitResources(renderer);
SDL_GPUDevice *result = SDL_malloc(sizeof(SDL_GPUDevice)); SDL_GPUDevice *result = SDL_calloc(1, sizeof(SDL_GPUDevice));
ASSIGN_DRIVER(METAL) ASSIGN_DRIVER(METAL)
result->driverData = (SDL_GPURenderer *)renderer; result->driverData = (SDL_GPURenderer *)renderer;
renderer->sdlGPUDevice = result; renderer->sdlGPUDevice = result;