Repository: libsdl-org/SDL
File: src/gpu/d3d12/SDL_gpu_d3d12.c (C)

Code before:
    pool->freeDescriptors = (D3D12StagingDescriptor *)SDL_realloc(pool->freeDescriptors, pool->freeDescriptorCapacity * sizeof(D3D12StagingDescriptor));

    for (Uint32 i = 0; i < STAGING_HEAP_DESCRIPTOR_COUNT; i += 1) {
        pool->freeDescriptors[i].pool = pool;
        pool->freeDescriptors[i].heap = heap;
        pool->freeDescriptors[i].cpuHandleIndex = i;
        pool->freeDescriptors[i].cpuHandle.ptr = heap->descriptorHeapCPUStart.ptr + (i * heap->descriptorSize);
    }

    return true;
}

static void D3D12_INTERNAL_TrackGPUDescriptorHeap(
    D3D12CommandBuffer *commandBuffer,
    D3D12DescriptorHeap *descriptorHeap)
{
    Uint32 i;
    for (i = 0; i < commandBuffer->usedDescriptorHeapCount; i += 1) {
        if (commandBuffer->usedDescriptorHeaps[i] == descriptorHeap) {
            return;
        }
    }

    if (commandBuffer->usedDescriptorHeapCount == commandBuffer->usedDescriptorHeapCapacity) {
        commandBuffer->usedDescriptorHeapCapacity += 1;
        commandBuffer->usedDescriptorHeaps = (D3D12DescriptorHeap **)SDL_realloc(
            commandBuffer->usedDescriptorHeaps,
            commandBuffer->usedDescriptorHeapCapacity * sizeof(D3D12DescriptorHeap *));
    }

    commandBuffer->usedDescriptorHeaps[commandBuffer->usedDescriptorHeapCount] = descriptorHeap;
    commandBuffer->usedDescriptorHeaps += 1;
}

static D3D12DescriptorHeap *D3D12_INTERNAL_AcquireGPUDescriptorHeapFromPool(
    D3D12CommandBuffer *commandBuffer,
    D3D12_DESCRIPTOR_HEAP_TYPE descriptorHeapType)
{
    D3D12DescriptorHeap *result;
    D3D12Renderer *renderer = commandBuffer->renderer;
    D3D12GPUDescriptorHeapPool *pool = &renderer->gpuDescriptorHeapPools[descriptorHeapType];

    SDL_LockMutex(pool->lock);
    if (pool->count > 0) {
        result = pool->heaps[pool->count - 1];
        pool->count -= 1;
    } else {
        result = D3D12_INTERNAL_CreateDescriptorHeap(
            renderer,
            descriptorHeapType,
            descriptorHeapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? VIEW_GPU_DESCRIPTOR_COUNT : SAMPLER_GPU_DESCRIPTOR_COUNT,

Review comment (bug):
NULL check? Typically the realloc is attempted and assigned to a temporary pointer and if it succeeds, it's stored and the count incremented. If it fails, we should bubble that up to the caller.

Code after:
    pool->freeDescriptors = (D3D12StagingDescriptor *)SDL_realloc(pool->freeDescriptors, pool->freeDescriptorCapacity * sizeof(D3D12StagingDescriptor));

    for (Uint32 i = 0; i < STAGING_HEAP_DESCRIPTOR_COUNT; i += 1) {
        pool->freeDescriptors[i].pool = pool;
        pool->freeDescriptors[i].heap = heap;
        pool->freeDescriptors[i].cpuHandleIndex = i;
        pool->freeDescriptors[i].cpuHandle.ptr = heap->descriptorHeapCPUStart.ptr + (i * heap->descriptorSize);
    }

    return true;
}

static void D3D12_INTERNAL_TrackGPUDescriptorHeap(
    D3D12CommandBuffer *commandBuffer,
    D3D12DescriptorHeap *descriptorHeap)
{
    Uint32 i;
    for (i = 0; i < commandBuffer->usedDescriptorHeapCount; i += 1) {
        if (commandBuffer->usedDescriptorHeaps[i] == descriptorHeap) {
            return;
        }
    }

    if (commandBuffer->usedDescriptorHeapCount == commandBuffer->usedDescriptorHeapCapacity) {
        commandBuffer->usedDescriptorHeapCapacity += 1;
        commandBuffer->usedDescriptorHeaps = (D3D12DescriptorHeap **)SDL_realloc(
            commandBuffer->usedDescriptorHeaps,
            commandBuffer->usedDescriptorHeapCapacity * sizeof(D3D12DescriptorHeap *));
    }

    commandBuffer->usedDescriptorHeaps[commandBuffer->usedDescriptorHeapCount] = descriptorHeap;
    commandBuffer->usedDescriptorHeapCount += 1;
}

static D3D12DescriptorHeap *D3D12_INTERNAL_AcquireGPUDescriptorHeapFromPool(
    D3D12CommandBuffer *commandBuffer,
    D3D12_DESCRIPTOR_HEAP_TYPE descriptorHeapType)
{
    D3D12DescriptorHeap *result;
    D3D12Renderer *renderer = commandBuffer->renderer;
    D3D12GPUDescriptorHeapPool *pool = &renderer->gpuDescriptorHeapPools[descriptorHeapType];

    SDL_LockMutex(pool->lock);
    if (pool->count > 0) {
        result = pool->heaps[pool->count - 1];
        pool->count -= 1;
    } else {
        result = D3D12_INTERNAL_CreateDescriptorHeap(
            renderer,
            descriptorHeapType,
            descriptorHeapType == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV ? VIEW_GPU_DESCRIPTOR_COUNT : SAMPLER_GPU_DESCRIPTOR_COUNT,