diff --git a/impls/Windows_Libs/Render/inc/Renderer.h b/impls/Windows_Libs/Render/inc/Renderer.h index 66378bd..7f6ade9 100644 --- a/impls/Windows_Libs/Render/inc/Renderer.h +++ b/impls/Windows_Libs/Render/inc/Renderer.h @@ -37,7 +37,19 @@ SOFTWARE. #define STACK_TYPES 4 #define STACK_SIZE 16 -#define MAX_TEXTURES 512 + +#define MAX_TEXTURES 512 // initial pool size (grows in MAX_TEXTURES chunks) +#define MAX_COMMAND_BUFFERS 16000 // initial slot capacity, doubles on demand + +#define COMMAND_HANDLE_PAGE_BITS 12 +#define COMMAND_HANDLE_PAGE_SIZE (1 << COMMAND_HANDLE_PAGE_BITS) // 4096 handles per page +#define COMMAND_HANDLE_PAGE_MASK (COMMAND_HANDLE_PAGE_SIZE - 1) +#define COMMAND_HANDLES_INITIAL_PAGES 16 // 64K handles of directory headroom +#define COMMAND_HANDLE_HARD_LIMIT 0x40000000 // sanity ceiling for the handle space + +// handle map sentinels (same meaning as the old int16_t table) +#define COMMAND_HANDLE_FREE (-1) // never allocated, or released by CBuffDelete +#define COMMAND_HANDLE_RESERVED (-2) // reserved by CBuffCreate / parked by CBuffClear #define NUM_THUMBNAIL_DOWNSAMPLES 4 // the amount of downsamples we do to the thumbnail texture #define THUMBNAIL_MAX_QUALITY 0 // 1920x1080 @@ -46,9 +58,6 @@ SOFTWARE. #define THUMBNAIL_DOWNSAMPLE_QUALITY_3 3 // 128x128 #define THUMBNAIL_DOWNSAMPLE_TARGET 4 // 64x64 -#define NUM_COMMAND_HANDLES 0x800000 -#define MAX_COMMAND_BUFFERS 16000 - class Renderer { public: @@ -168,6 +177,15 @@ private: ID3D11RasterizerState *GetManagedRasterizerState(); ID3D11SamplerState *GetManagedSamplerState(); void DeleteInternalBuffer(int index); + + void TracePoolGrowth(const char *pool, size_t oldCapacity, size_t newCapacity); + void EnsureHandleDirectory(size_t pageCount); + std::int32_t *AllocateHandlePage(size_t pageIndex); + std::int32_t GetCommandHandle(std::uint32_t handle) const; + void SetCommandHandle(std::uint32_t handle, std::int32_t slot); + void EnsureCommandSlots(size_t slotCount); + void GrowTexturePool(size_t extraSlots); + Renderer::Context &getContext(); public: @@ -340,6 +358,11 @@ public: } set_face_cull; }; }; + + // the 80-byte command layout is load-bearing: keep the type tag, padding + // and union payload exactly as-is so recorded streams stay byte-compatible + static_assert(sizeof(Command) == 80, "Command layout must stay 80 bytes"); + ID3D11Buffer *m_vertexBuffer; void *m_vertexData; std::uint64_t m_vertexDataLength; @@ -465,18 +488,20 @@ public: C4JRender::eViewportType m_ViewportType; BYTE reservedRendererByte0; BYTE paddingAfterViewportType[3]; - Renderer::Texture m_textures[512]; + std::vector m_textures; + std::uint32_t m_textureScanHint; DWORD backBufferWidth; DWORD backBufferHeight; BYTE reservedRendererByte1; BYTE paddingAfterRendererByte1[3]; DWORD reservedRendererDword1; - int16_t *m_vertexIdxToBufferIdx; - CommandBuffer **m_commandBuffers; - DirectX::XMMATRIX *m_commandMatrices; - int *m_bufferIdxToVertexIdx; - uint8_t *m_commandPrimitiveTypes; - uint8_t *m_commandVertexTypes; + std::vector m_handlePages; + std::vector m_commandBuffers; + std::vector m_commandMatrices; + std::vector m_bufferIdxToVertexIdx; + std::vector m_commandPrimitiveTypes; + std::vector m_commandVertexTypes; + std::vector m_retiredBuffers; DWORD m_currentCommandBuffer; DWORD m_numBuffersToDeallocate; std::unordered_map managedBlendStates; diff --git a/impls/Windows_Libs/Render/src/RendererCBuff.cpp b/impls/Windows_Libs/Render/src/RendererCBuff.cpp index 6a1ab18..b12b30a 100644 --- a/impls/Windows_Libs/Render/src/RendererCBuff.cpp +++ b/impls/Windows_Libs/Render/src/RendererCBuff.cpp @@ -26,9 +26,83 @@ SOFTWARE. #include "Renderer.h" #include +#include #include #include +void Renderer::TracePoolGrowth(const char *pool, size_t oldCapacity, size_t newCapacity) +{ + char trace[160]; + sprintf(trace, "[Renderer] %s grown: %zu -> %zu\n", pool, oldCapacity, newCapacity); + OutputDebugStringA(trace); +} + +void Renderer::EnsureHandleDirectory(size_t pageCount) +{ + if (pageCount <= m_handlePages.size()) + return; + + const size_t oldCapacity = m_handlePages.size(); + size_t newCapacity = oldCapacity ? oldCapacity * 2 : static_cast(COMMAND_HANDLES_INITIAL_PAGES); + while (newCapacity < pageCount) + newCapacity *= 2; + + m_handlePages.resize(newCapacity, nullptr); + TracePoolGrowth("handle page directory", oldCapacity, newCapacity); +} + +std::int32_t *Renderer::AllocateHandlePage(size_t pageIndex) +{ + // 0xFF fill == -1 for every int32 lane -- same trick as the old memset + std::int32_t *page = new std::int32_t[COMMAND_HANDLE_PAGE_SIZE]; + std::memset(page, 0xFF, COMMAND_HANDLE_PAGE_SIZE * sizeof(std::int32_t)); + m_handlePages[pageIndex] = page; + TracePoolGrowth("handle page", 0, COMMAND_HANDLE_PAGE_SIZE); + return page; +} + +std::int32_t Renderer::GetCommandHandle(std::uint32_t handle) const +{ + const size_t pageIndex = handle >> COMMAND_HANDLE_PAGE_BITS; + if (pageIndex >= m_handlePages.size()) + return COMMAND_HANDLE_FREE; + + const std::int32_t *page = m_handlePages[pageIndex]; + return page ? page[handle & COMMAND_HANDLE_PAGE_MASK] : COMMAND_HANDLE_FREE; +} + +void Renderer::SetCommandHandle(std::uint32_t handle, std::int32_t slot) +{ + const size_t pageIndex = handle >> COMMAND_HANDLE_PAGE_BITS; + if (pageIndex >= m_handlePages.size()) + EnsureHandleDirectory(pageIndex + 1); + + std::int32_t *page = m_handlePages[pageIndex]; + if (!page) + page = AllocateHandlePage(pageIndex); + + page[handle & COMMAND_HANDLE_PAGE_MASK] = slot; +} + +void Renderer::EnsureCommandSlots(size_t slotCount) +{ + if (slotCount <= m_commandBuffers.size()) + return; + + const size_t oldCapacity = m_commandBuffers.size(); + size_t newCapacity = oldCapacity ? oldCapacity * 2 : static_cast(MAX_COMMAND_BUFFERS); + while (newCapacity < slotCount) + newCapacity *= 2; + + m_commandBuffers.resize(newCapacity, nullptr); + m_commandMatrices.resize(newCapacity); + m_bufferIdxToVertexIdx.resize(newCapacity, 0); + m_commandVertexTypes.resize(newCapacity, 0); + m_commandPrimitiveTypes.resize(newCapacity, 0); + + TracePoolGrowth("command buffer slots", oldCapacity, newCapacity); +} + Renderer::CommandBuffer::CommandBuffer(bool full) : m_vertexBuffer(NULL) , m_vertexData(NULL) @@ -37,6 +111,7 @@ Renderer::CommandBuffer::CommandBuffer(bool full) , m_allocated(0x1000) , isActive(full ? 1 : 0) { + m_commands.reserve(64); m_vertexData = std::malloc(m_allocated); EnterCriticalSection(&Renderer::totalAllocCS); Renderer::totalAlloc += static_cast(m_allocated); @@ -114,8 +189,8 @@ bool Renderer::CBuffCall(int index, bool full) EnterCriticalSection(&m_commandBufferCS); bool result = false; - const int commandIndex = m_vertexIdxToBufferIdx[index]; - if (commandIndex >= 0) + const int commandIndex = GetCommandHandle(static_cast(index)); + if (commandIndex >= 0 && static_cast(commandIndex) < m_currentCommandBuffer) { Renderer::Context &c = getContext(); const std::uint8_t vertexType = m_commandVertexTypes[commandIndex]; @@ -210,11 +285,11 @@ void Renderer::CBuffClear(int index) { EnterCriticalSection(&m_commandBufferCS); - const int internalIndex = m_vertexIdxToBufferIdx[index]; + const int internalIndex = GetCommandHandle(static_cast(index)); if (internalIndex >= 0) { DeleteInternalBuffer(internalIndex); - m_vertexIdxToBufferIdx[index] = static_cast(-2); + SetCommandHandle(static_cast(index), COMMAND_HANDLE_RESERVED); } LeaveCriticalSection(&m_commandBufferCS); @@ -222,49 +297,43 @@ void Renderer::CBuffClear(int index) int Renderer::CBuffCreate(int count) { + if (count <= 0 || static_cast(count) > static_cast(COMMAND_HANDLE_HARD_LIMIT)) + return -1; + EnterCriticalSection(&m_commandBufferCS); - int first = reservedRendererDword1; - if (first < NUM_COMMAND_HANDLES) + int first = -1; + std::uint32_t searchBase = reservedRendererDword1; + + while (true) { - int probe = first; - int end = first + count; - while (true) + EnsureHandleDirectory((static_cast(searchBase) + static_cast(count)) / COMMAND_HANDLE_PAGE_SIZE + 1); + + std::uint32_t cursor = searchBase; + const std::uint32_t end = searchBase + static_cast(count); + while (cursor < end && GetCommandHandle(cursor) == COMMAND_HANDLE_FREE) + ++cursor; + + if (cursor >= end) { - assert(first < NUM_COMMAND_HANDLES); - - int cursor = probe; - while (cursor < end && cursor < NUM_COMMAND_HANDLES && m_vertexIdxToBufferIdx[cursor] == static_cast(-1)) - { - ++cursor; - } - - if (cursor >= end) - break; - - ++first; - ++probe; - ++end; - if (first >= NUM_COMMAND_HANDLES || end > NUM_COMMAND_HANDLES) - { - first = -1; - break; - } + first = static_cast(searchBase); + break; } - if (first >= 0) - { - const int allocationEnd = first + count; - for (int i = first; i < allocationEnd; ++i) - m_vertexIdxToBufferIdx[i] = static_cast(-2); + ++searchBase; - if (reservedRendererByte1) - reservedRendererDword1 = allocationEnd; - } + if (static_cast(searchBase) + static_cast(count) > static_cast(COMMAND_HANDLE_HARD_LIMIT)) + break; // genuinely out of addressable handles, should never hit in practice } - else + + if (first >= 0) { - first = -1; + const std::uint32_t allocationEnd = static_cast(first) + static_cast(count); + for (std::uint32_t i = static_cast(first); i < allocationEnd; ++i) + SetCommandHandle(i, COMMAND_HANDLE_RESERVED); + + if (reservedRendererByte1) + reservedRendererDword1 = allocationEnd; } LeaveCriticalSection(&m_commandBufferCS); @@ -283,17 +352,16 @@ void Renderer::CBuffDeferredModeEnd() for (std::vector::const_iterator it = c.deferredBuffers.begin(); it != c.deferredBuffers.end(); ++it) { const Renderer::DeferredCBuff &deferred = *it; - const int existingIndex = m_vertexIdxToBufferIdx[deferred.m_vertex_index]; + const int existingIndex = GetCommandHandle(static_cast(deferred.m_vertex_index)); if (existingIndex >= 0) DeleteInternalBuffer(existingIndex); - if (static_cast(m_currentCommandBuffer + m_numBuffersToDeallocate + 10) > MAX_COMMAND_BUFFERS) - DebugBreak(); + EnsureCommandSlots(static_cast(m_currentCommandBuffer) + 1); - const int internalSlot = m_currentCommandBuffer; - ++m_currentCommandBuffer; + const std::uint32_t internalSlot = m_currentCommandBuffer++; - m_vertexIdxToBufferIdx[deferred.m_vertex_index] = static_cast(internalSlot); + if (deferred.m_vertex_index >= 0) + SetCommandHandle(static_cast(deferred.m_vertex_index), static_cast(internalSlot)); m_bufferIdxToVertexIdx[internalSlot] = deferred.m_vertex_index; m_commandVertexTypes[internalSlot] = static_cast(deferred.m_vertex_type); m_commandPrimitiveTypes[internalSlot] = static_cast(deferred.m_primitive_type); @@ -312,16 +380,19 @@ void Renderer::CBuffDeferredModeStart() void Renderer::CBuffDelete(int first, int count) { + if (first < 0 || count <= 0) + return; + EnterCriticalSection(&m_commandBufferCS); const int end = first + count; for (int i = first; i < end; ++i) { - const int internalIndex = m_vertexIdxToBufferIdx[i]; + const int internalIndex = GetCommandHandle(static_cast(i)); if (internalIndex >= 0) DeleteInternalBuffer(internalIndex); - m_vertexIdxToBufferIdx[i] = static_cast(-1); + SetCommandHandle(static_cast(i), COMMAND_HANDLE_FREE); } LeaveCriticalSection(&m_commandBufferCS); @@ -348,17 +419,15 @@ void Renderer::CBuffEnd() } else { - const int existingIndex = m_vertexIdxToBufferIdx[c.recordingBufferIndex]; + const int existingIndex = GetCommandHandle(static_cast(c.recordingBufferIndex)); if (existingIndex >= 0) DeleteInternalBuffer(existingIndex); - if (static_cast(m_currentCommandBuffer + m_numBuffersToDeallocate + 10) > MAX_COMMAND_BUFFERS) - DebugBreak(); + EnsureCommandSlots(static_cast(m_currentCommandBuffer) + 1); - const int internalSlot = m_currentCommandBuffer; - ++m_currentCommandBuffer; + const std::uint32_t internalSlot = m_currentCommandBuffer++; - m_vertexIdxToBufferIdx[c.recordingBufferIndex] = static_cast(internalSlot); + SetCommandHandle(static_cast(c.recordingBufferIndex), static_cast(internalSlot)); m_bufferIdxToVertexIdx[internalSlot] = c.recordingBufferIndex; m_commandVertexTypes[internalSlot] = static_cast(c.recordingVertexType); m_commandPrimitiveTypes[internalSlot] = static_cast(c.recordingPrimitiveType); @@ -385,8 +454,8 @@ int Renderer::CBuffSize(int index) unsigned int size = 0; EnterCriticalSection(&m_commandBufferCS); - const int commandIndex = m_vertexIdxToBufferIdx[index]; - if (commandIndex >= 0) + const int commandIndex = GetCommandHandle(static_cast(index)); + if (commandIndex >= 0 && static_cast(commandIndex) < m_currentCommandBuffer) size = static_cast(m_commandBuffers[commandIndex]->GetAllocated()); LeaveCriticalSection(&m_commandBufferCS); return size; @@ -408,59 +477,46 @@ void Renderer::CBuffStart(int index, bool full) void Renderer::CBuffTick() { + if (m_retiredBuffers.empty()) + return; + EnterCriticalSection(&m_commandBufferCS); - int completedDeletes = 0; - if (m_numBuffersToDeallocate > 0) + for (size_t i = 0; i < m_retiredBuffers.size(); ++i) { - int deleteIdx = MAX_COMMAND_BUFFERS - 1; - do - { - Renderer::CommandBuffer *buffer = m_commandBuffers[deleteIdx]; - if (buffer) - delete buffer; - m_commandBuffers[deleteIdx] = NULL; - - if (--m_numBuffersToDeallocate == 0) - { - ++completedDeletes; - --deleteIdx; - } - else - { - m_commandBuffers[deleteIdx] = m_commandBuffers[(MAX_COMMAND_BUFFERS - 1) - static_cast(m_numBuffersToDeallocate)]; - } - } while (completedDeletes < static_cast(m_numBuffersToDeallocate)); + Renderer::CommandBuffer *buffer = m_retiredBuffers[i]; + if (buffer) + delete buffer; } + m_retiredBuffers.clear(); + m_numBuffersToDeallocate = 0; + LeaveCriticalSection(&m_commandBufferCS); } void Renderer::DeleteInternalBuffer(int index) { - EnterCriticalSection(&m_commandBufferCS); + if (index < 0 || static_cast(index) >= m_currentCommandBuffer) + return; + m_retiredBuffers.push_back(m_commandBuffers[index]); ++m_numBuffersToDeallocate; - const int index_delete = MAX_COMMAND_BUFFERS - static_cast(m_numBuffersToDeallocate); - m_commandBuffers[index_delete] = m_commandBuffers[index]; - m_commandMatrices[index_delete] = m_commandMatrices[index]; - - if (m_currentCommandBuffer-- != 1) + const std::uint32_t last = m_currentCommandBuffer - 1; + if (static_cast(index) != last) { - const int mappedFrom = m_currentCommandBuffer; + m_commandBuffers[index] = m_commandBuffers[last]; + m_commandMatrices[index] = m_commandMatrices[last]; + m_commandVertexTypes[index] = m_commandVertexTypes[last]; + m_commandPrimitiveTypes[index] = m_commandPrimitiveTypes[last]; - m_commandBuffers[index] = m_commandBuffers[mappedFrom]; - m_commandMatrices[index] = m_commandMatrices[mappedFrom]; - m_commandVertexTypes[index] = m_commandVertexTypes[mappedFrom]; - m_commandPrimitiveTypes[index] = m_commandPrimitiveTypes[mappedFrom]; - - const int commandIndex = m_bufferIdxToVertexIdx[mappedFrom]; - m_vertexIdxToBufferIdx[commandIndex] = static_cast(index); - m_bufferIdxToVertexIdx[index] = commandIndex; + const std::int32_t movedHandle = m_bufferIdxToVertexIdx[last]; + m_bufferIdxToVertexIdx[index] = movedHandle; + SetCommandHandle(static_cast(movedHandle), static_cast(index)); } - LeaveCriticalSection(&m_commandBufferCS); + --m_currentCommandBuffer; } void Renderer::CommandBuffer::EndRecording(ID3D11Device *device) diff --git a/impls/Windows_Libs/Render/src/RendererCore.cpp b/impls/Windows_Libs/Render/src/RendererCore.cpp index 0cbf944..2eed815 100644 --- a/impls/Windows_Libs/Render/src/RendererCore.cpp +++ b/impls/Windows_Libs/Render/src/RendererCore.cpp @@ -680,19 +680,10 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) PROFILER_INIT(); - m_vertexIdxToBufferIdx = new int16_t[NUM_COMMAND_HANDLES]; - m_commandBuffers = new CommandBuffer *[MAX_COMMAND_BUFFERS]; - m_commandMatrices = new DirectX::XMMATRIX[MAX_COMMAND_BUFFERS]; - m_bufferIdxToVertexIdx = new int[MAX_COMMAND_BUFFERS]; - m_commandPrimitiveTypes = new uint8_t[MAX_COMMAND_BUFFERS]; - m_commandVertexTypes = new uint8_t[MAX_COMMAND_BUFFERS]; - - std::memset(m_vertexIdxToBufferIdx, 0xFF, NUM_COMMAND_HANDLES * sizeof(int16_t)); - std::memset(m_commandBuffers, 0, MAX_COMMAND_BUFFERS * sizeof(CommandBuffer *)); - std::memset(m_bufferIdxToVertexIdx, 0, MAX_COMMAND_BUFFERS * sizeof(int)); - std::memset(m_commandPrimitiveTypes, 0, MAX_COMMAND_BUFFERS * sizeof(uint8_t)); - std::memset(m_commandVertexTypes, 0, MAX_COMMAND_BUFFERS * sizeof(uint8_t)); + EnsureHandleDirectory(COMMAND_HANDLES_INITIAL_PAGES); + EnsureCommandSlots(MAX_COMMAND_BUFFERS); + m_currentCommandBuffer = 0; m_numBuffersToDeallocate = 0; m_bShouldScreenGrabNextFrame = false; @@ -767,7 +758,9 @@ void Renderer::Initialise(ID3D11Device *pDevice, IDXGISwapChain *pSwapChain) m_pDevice->CreateShaderResourceView(downSampleTextures[i], NULL, &downSampleShaderResourceViews[i]); } - std::memset(m_textures, 0, sizeof(m_textures)); + // TextureCreate() appends another chunk whenever the scan runs off the end + m_textureScanHint = 0; + m_textures.assign(MAX_TEXTURES, Texture()); defaultTextureIndex = TextureCreate(); TextureBind(defaultTextureIndex); diff --git a/impls/Windows_Libs/Render/src/RendererTexture.cpp b/impls/Windows_Libs/Render/src/RendererTexture.cpp index f666f1f..d5aed00 100644 --- a/impls/Windows_Libs/Render/src/RendererTexture.cpp +++ b/impls/Windows_Libs/Render/src/RendererTexture.cpp @@ -40,6 +40,14 @@ DXGI_FORMAT Renderer::textureFormats[] = { void user_flush_data(png_struct_def *png_ptr); void user_write_data(png_struct_def *png_ptr, unsigned char *src, size_t length); +void Renderer::GrowTexturePool(size_t extraSlots) +{ + const size_t oldCapacity = m_textures.size(); + m_textures.resize(oldCapacity + extraSlots); + TracePoolGrowth("texture pool", oldCapacity, oldCapacity + extraSlots); +} + + HRESULT Renderer::LoadTextureData(BYTE* pbData, DWORD dwBytes, D3DXIMAGE_INFO* pSrcInfo, int** ppDataOut) { PROFILER_SCOPE("Renderer::LoadTextureData_Memory", "LoadTextureData_Memory", MP_PURPLE4); @@ -155,18 +163,29 @@ void Renderer::TextureBindVertex(int idx) int Renderer::TextureCreate() { PROFILER_SCOPE("Renderer::TextureCreate", "TextureCreate", MP_PURPLE4); - for (int i = 0; i < MAX_TEXTURES; i++) + const std::uint32_t poolSize = static_cast(m_textures.size()); + std::uint32_t slot = m_textureScanHint; + + while (slot < poolSize && m_textures[slot].allocated) + ++slot; + + if (slot >= poolSize) { - if (!m_textures[i].allocated) - { - m_textures[i].texture = NULL; - m_textures[i].allocated = true; - m_textures[i].mipLevels = 1; - m_textures[i].samplerParams = 0; - return i; - } + GrowTexturePool(MAX_TEXTURES); + slot = poolSize; } - return -1; + + Texture& texture = m_textures[slot]; + texture.allocated = true; + texture.texture = NULL; + texture.view = NULL; + texture.textureFlags = 0; + texture.mipLevels = 0; + texture.textureFormat = 0; + texture.samplerParams = 0; + + m_textureScanHint = slot + 1; + return static_cast(slot); } void Renderer::TextureData(int width, int height, void* data, int level, C4JRender::eTextureFormat format) @@ -239,11 +258,18 @@ void Renderer::TextureDynamicUpdateStart() {} void Renderer::TextureFree(int idx) { PROFILER_SCOPE("Renderer::TextureFree", "TextureFree", MP_PURPLE4); - m_textures[idx].texture->Release(); - m_textures[idx].view->Release(); - m_textures[idx].view = NULL; - m_textures[idx].allocated = false; - m_textures[idx].texture = NULL; + if (idx < 0 || static_cast(idx) >= m_textures.size()) + return; + + Texture& texture = m_textures[idx]; + if (texture.view) + texture.view->Release(); + if (texture.texture) + texture.texture->Release(); + texture = Texture(); + + if (static_cast(idx) < m_textureScanHint) + m_textureScanHint = static_cast(idx); } void Renderer::TextureGetStats() {}