From 6e8a394d5f4bb0f8a364388b5c2960b8cbd8dea7 Mon Sep 17 00:00:00 2001 From: tanmaysachan Date: Mon, 12 Aug 2024 00:31:13 +0530 Subject: [PATCH] Add vertex and index buffer persistence --- src/metal/rt64_metal.h | 11 ++++++++++- src/metal/rt64_metal.mm | 30 ++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/metal/rt64_metal.h b/src/metal/rt64_metal.h index c65f1db..e0f9a72 100755 --- a/src/metal/rt64_metal.h +++ b/src/metal/rt64_metal.h @@ -42,7 +42,7 @@ namespace RT64 { MTLDepthClipMode depthClipMode = MTLDepthClipModeClip; MTLWinding winding = MTLWindingClockwise; #endif - } + }; struct MetalDescriptorSet : RenderDescriptorSet { MetalDevice *device = nullptr; @@ -107,6 +107,11 @@ namespace RT64 { MTLIndexType currentIndexType = MTLIndexTypeUInt32; id indexBuffer = nil; + uint32_t viewCount = 0; + std::vector> vertexBuffers; + std::vector vertexBufferOffsets; + std::vector vertexBufferIndices; + std::vector viewportVector; std::vector scissorVector; #endif @@ -309,6 +314,10 @@ namespace RT64 { }; struct MetalGraphicsPipeline : MetalPipeline { +#ifdef __OBJC__ + id state = nil; +#endif + MetalRenderState *renderState; MetalGraphicsPipeline(MetalDevice *device, const RenderGraphicsPipelineDesc &desc); ~MetalGraphicsPipeline() override; diff --git a/src/metal/rt64_metal.mm b/src/metal/rt64_metal.mm index 0301ffd..3f2fae9 100755 --- a/src/metal/rt64_metal.mm +++ b/src/metal/rt64_metal.mm @@ -681,6 +681,7 @@ namespace RT64 { NSError *error = nullptr; this->state = [device->device newRenderPipelineStateWithDescriptor: descriptor error: &error]; + renderState->renderPipelineState = state; if (error != nullptr) { fprintf(stderr, "MTLDevice newRenderPipelineStateWithDescriptor: failed with error %s.\n", [error.localizedDescription cStringUsingEncoding: NSUTF8StringEncoding]); @@ -872,6 +873,12 @@ namespace RT64 { [renderEncoder setViewports: viewportVector.data() count: viewportVector.size()]; [renderEncoder setScissorRects: scissorVector.data() count: scissorVector.size()]; + + for (uint32_t i = 0; i < viewCount; i++) { + [renderEncoder setVertexBuffer: vertexBuffers[i] + offset: vertexBufferOffsets[i] + atIndex: vertexBufferIndices[i]]; + } } } @@ -958,7 +965,7 @@ namespace RT64 { guaranteeRenderEncoder(); const auto *graphicsPipeline = static_cast(interfacePipeline); assert(renderEncoder != nil && "Cannot set pipeline state on nil MTLRenderCommandEncoder!"); - rebindRenderState(graphicsPipeline->state); + rebindRenderState(graphicsPipeline->renderState); break; } default: @@ -1012,17 +1019,32 @@ namespace RT64 { } void MetalCommandList::setIndexBuffer(const RenderIndexBufferView *view) { - // TODO: Argument Buffer Creation & Binding if (view != nullptr) { const auto *interfaceBuffer = static_cast(view->buffer.ref); - + indexBuffer = interfaceBuffer->buffer; } } void MetalCommandList::setVertexBuffers(uint32_t startSlot, const RenderVertexBufferView *views, uint32_t viewCount, const RenderInputSlot *inputSlots) { - // TODO: Argument Buffer Creation & Binding if ((views != nullptr) && (viewCount > 0)) { + guaranteeRenderEncoder(); assert(inputSlots != nullptr); + + this->viewCount = viewCount; + vertexBuffers.clear(); + vertexBufferOffsets.clear(); + vertexBufferIndices.clear(); + + for (uint32_t i = 0; i < viewCount; i++) { + const MetalBuffer *interfaceBuffer = static_cast(views[i].buffer.ref); + vertexBuffers.emplace_back(interfaceBuffer->buffer); + vertexBufferOffsets.emplace_back(views[i].buffer.offset); + vertexBufferIndices.emplace_back(startSlot + i); + + [renderEncoder setVertexBuffer: interfaceBuffer->buffer + offset: views[i].buffer.offset + atIndex: startSlot + i]; + } } }