From 7fed971b2df7b077d13bea49cc48e10255da5df9 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sun, 6 Nov 2022 19:20:17 +0000 Subject: [PATCH] Take firstIndex into account when calculating index (quad) buffer size Without this we would miss any elements beyond indexCount in the index buffer and they would be filled with random garbage causing vertex bombs --- .../interconnect/maxwell_3d/active_state.cpp | 22 +++++++++++-------- .../interconnect/maxwell_3d/active_state.h | 7 +++--- .../interconnect/maxwell_3d/maxwell_3d.cpp | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.cpp b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.cpp index 820f648a..4e11c34f 100644 --- a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.cpp +++ b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.cpp @@ -93,7 +93,7 @@ namespace skyline::gpu::interconnect::maxwell3d { } } - static BufferBinding GenerateQuadConversionIndexBuffer(InterconnectContext &ctx, engine::IndexBuffer::IndexSize indexType, BufferView &view, u32 elementCount) { + static BufferBinding GenerateQuadConversionIndexBuffer(InterconnectContext &ctx, engine::IndexBuffer::IndexSize indexType, BufferView &view, u32 firstIndex, u32 elementCount) { auto viewSpan{view.GetReadOnlyBackingSpan(false /* We attach above so always false */, []() { // TODO: see Read() Logger::Error("Dirty index buffer reads for attached buffers are unimplemented"); @@ -103,7 +103,7 @@ namespace skyline::gpu::interconnect::maxwell3d { vk::DeviceSize indexBufferSize{conversion::quads::GetRequiredBufferSize(elementCount, indexSize)}; auto quadConversionAllocation{ctx.gpu.megaBufferAllocator.Allocate(ctx.executor.cycle, indexBufferSize)}; - conversion::quads::GenerateIndexedQuadConversionBuffer(quadConversionAllocation.region.data(), viewSpan.data(), elementCount, ConvertIndexType(indexType)); + conversion::quads::GenerateIndexedQuadConversionBuffer(quadConversionAllocation.region.data(), viewSpan.subspan(GetIndexBufferSize(indexType, firstIndex)).data(), elementCount, ConvertIndexType(indexType)); return {quadConversionAllocation.buffer, quadConversionAllocation.offset, indexBufferSize}; } @@ -115,11 +115,12 @@ namespace skyline::gpu::interconnect::maxwell3d { IndexBufferState::IndexBufferState(dirty::Handle dirtyHandle, DirtyManager &manager, const EngineRegisters &engine) : engine{manager, dirtyHandle, engine} {} - void IndexBufferState::Flush(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 elementCount) { + void IndexBufferState::Flush(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 firstIndex, u32 elementCount) { usedElementCount = elementCount; + usedFirstIndex = firstIndex; usedQuadConversion = quadConversion; - size_t size{GetIndexBufferSize(engine->indexBuffer.indexSize, elementCount)}; + size_t size{GetIndexBufferSize(engine->indexBuffer.indexSize, firstIndex + elementCount)}; view.Update(ctx, engine->indexBuffer.address, size); if (!*view) { Logger::Warn("Unmapped index buffer: 0x{:X}", engine->indexBuffer.address); @@ -131,7 +132,7 @@ namespace skyline::gpu::interconnect::maxwell3d { indexType = ConvertIndexType(engine->indexBuffer.indexSize); if (quadConversion) - megaBufferBinding = GenerateQuadConversionIndexBuffer(ctx, engine->indexBuffer.indexSize, *view, elementCount); + megaBufferBinding = GenerateQuadConversionIndexBuffer(ctx, engine->indexBuffer.indexSize, *view, firstIndex, elementCount); else megaBufferBinding = view->TryMegaBuffer(ctx.executor.cycle, ctx.gpu.megaBufferAllocator, ctx.executor.executionNumber); @@ -141,16 +142,19 @@ namespace skyline::gpu::interconnect::maxwell3d { builder.SetIndexBuffer(*view, indexType); } - bool IndexBufferState::Refresh(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 elementCount) { + bool IndexBufferState::Refresh(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 firstIndex, u32 elementCount) { if (elementCount > usedElementCount) return true; if (quadConversion != usedQuadConversion) return true; + if (quadConversion != usedQuadConversion) + return true; + // TODO: optimise this to use buffer sequencing to avoid needing to regenerate the quad buffer every time. We can't use as it is rn though because sequences aren't globally unique and may conflict after buffer recreation if (usedQuadConversion) { - megaBufferBinding = GenerateQuadConversionIndexBuffer(ctx, engine->indexBuffer.indexSize, *view, elementCount); + megaBufferBinding = GenerateQuadConversionIndexBuffer(ctx, engine->indexBuffer.indexSize, *view, firstIndex, elementCount); builder.SetIndexBuffer(megaBufferBinding, indexType); } else if (megaBufferBinding) { if (auto newMegaBufferBinding{view->TryMegaBuffer(ctx.executor.cycle, ctx.gpu.megaBufferAllocator, ctx.executor.executionNumber)}; @@ -390,7 +394,7 @@ namespace skyline::gpu::interconnect::maxwell3d { dirtyFunc(stencilValues); } - void ActiveState::Update(InterconnectContext &ctx, Textures &textures, ConstantBufferSet &constantBuffers, StateUpdateBuilder &builder, bool indexed, engine::DrawTopology topology, u32 drawElementCount) { + void ActiveState::Update(InterconnectContext &ctx, Textures &textures, ConstantBufferSet &constantBuffers, StateUpdateBuilder &builder, bool indexed, engine::DrawTopology topology, u32 drawFirstIndex, u32 drawElementCount) { if (topology != directState.inputAssembly.GetPrimitiveTopology()) { directState.inputAssembly.SetPrimitiveTopology(topology); pipeline.MarkDirty(false); @@ -400,7 +404,7 @@ namespace skyline::gpu::interconnect::maxwell3d { pipeline.Update(ctx, textures, constantBuffers, builder); ranges::for_each(vertexBuffers, updateFunc); if (indexed) - updateFunc(indexBuffer, directState.inputAssembly.NeedsQuadConversion(), drawElementCount); + updateFunc(indexBuffer, directState.inputAssembly.NeedsQuadConversion(), drawFirstIndex, drawElementCount); ranges::for_each(transformFeedbackBuffers, updateFunc); ranges::for_each(viewports, updateFunc); ranges::for_each(scissors, updateFunc); diff --git a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.h b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.h index f622e725..99de6442 100644 --- a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.h +++ b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/active_state.h @@ -48,14 +48,15 @@ namespace skyline::gpu::interconnect::maxwell3d { BufferBinding megaBufferBinding{}; vk::IndexType indexType{}; u32 usedElementCount{}; + u32 usedFirstIndex{}; bool usedQuadConversion{}; public: IndexBufferState(dirty::Handle dirtyHandle, DirtyManager &manager, const EngineRegisters &engine); - void Flush(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 elementCount); + void Flush(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 firstIndex, u32 elementCount); - bool Refresh(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 elementCount); + bool Refresh(InterconnectContext &ctx, StateUpdateBuilder &builder, bool quadConversion, u32 firstIndex, u32 elementCount); void PurgeCaches(); }; @@ -256,7 +257,7 @@ namespace skyline::gpu::interconnect::maxwell3d { /** * @brief Updates the active state for a given draw operation, removing the dirtiness of all member states */ - void Update(InterconnectContext &ctx, Textures &textures, ConstantBufferSet &constantBuffers, StateUpdateBuilder &builder, bool indexed, engine::DrawTopology topology, u32 drawElementCount); + void Update(InterconnectContext &ctx, Textures &textures, ConstantBufferSet &constantBuffers, StateUpdateBuilder &builder, bool indexed, engine::DrawTopology topology, u32 drawFirstIndex, u32 drawElementCount); Pipeline *GetPipeline(); diff --git a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/maxwell_3d.cpp b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/maxwell_3d.cpp index 24446e0f..5fe6c0a5 100644 --- a/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/maxwell_3d.cpp +++ b/app/src/main/cpp/skyline/gpu/interconnect/maxwell_3d/maxwell_3d.cpp @@ -204,7 +204,7 @@ namespace skyline::gpu::interconnect::maxwell3d { StateUpdateBuilder builder{*ctx.executor.allocator}; Pipeline *oldPipeline{activeState.GetPipeline()}; - activeState.Update(ctx, textures, constantBuffers.boundConstantBuffers, builder, indexed, topology, count); + activeState.Update(ctx, textures, constantBuffers.boundConstantBuffers, builder, indexed, topology, first, count); if (directState.inputAssembly.NeedsQuadConversion()) { count = conversion::quads::GetIndexCount(count); first = 0;