diff --git a/src/services/impl/graphics_service.cpp b/src/services/impl/graphics_service.cpp index 63a39ee..7bb9557 100644 --- a/src/services/impl/graphics_service.cpp +++ b/src/services/impl/graphics_service.cpp @@ -178,7 +178,8 @@ void GraphicsService::RenderScene(const std::vector& commands, auto it = pipelines_.find(command.shaderKey); if (it != pipelines_.end()) { backend_->Draw(device_, it->second, vertexBuffer_, indexBuffer_, - command.indexCount, command.modelMatrix); + command.indexOffset, command.indexCount, command.vertexOffset, + command.modelMatrix); } } } diff --git a/src/services/impl/gxm_graphics_backend.cpp b/src/services/impl/gxm_graphics_backend.cpp index c44ea0c..fc5dd36 100644 --- a/src/services/impl/gxm_graphics_backend.cpp +++ b/src/services/impl/gxm_graphics_backend.cpp @@ -1,4 +1,5 @@ #include "gxm_graphics_backend.hpp" +#include "../../core/vertex.hpp" #include #include #include @@ -469,8 +470,10 @@ void GxmGraphicsBackend::SetRenderGraphDefinition(const RenderGraphDefinition& d void GxmGraphicsBackend::Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline, GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer, - uint32_t indexCount, const std::array& modelMatrix) { - std::cout << "GXM: Drawing " << indexCount << " indices" << std::endl; + uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset, + const std::array& modelMatrix) { + std::cout << "GXM: Drawing " << indexCount << " indices (indexOffset=" << indexOffset + << ", vertexOffset=" << vertexOffset << ")" << std::endl; if (!pipeline || !vertexBuffer || !indexBuffer) { std::cerr << "Invalid pipeline or buffer handles" << std::endl; @@ -502,11 +505,16 @@ void GxmGraphicsBackend::Draw(GraphicsDeviceHandle device, GraphicsPipelineHandl sceGxmSetUniformDataF(uniformBuffer, nullptr, 0, 16, modelMatrix.data()); } - // Set vertex stream (simplified - assumes single stream) - sceGxmSetVertexStream(context_, 0, vbHandle->data); + // Set vertex stream with base vertex offset + const auto vertexStride = static_cast(sizeof(core::Vertex)); + uint8_t* vertexBase = static_cast(vbHandle->data); + vertexBase += static_cast(vertexOffset) * vertexStride; + sceGxmSetVertexStream(context_, 0, vertexBase); // Draw - err = sceGxmDraw(context_, SCE_GXM_PRIMITIVE_TRIANGLES, SCE_GXM_INDEX_FORMAT_U16, ibHandle->data, indexCount); + uint8_t* indexBase = static_cast(ibHandle->data); + indexBase += static_cast(indexOffset) * sizeof(uint16_t); + err = sceGxmDraw(context_, SCE_GXM_PRIMITIVE_TRIANGLES, SCE_GXM_INDEX_FORMAT_U16, indexBase, indexCount); if (err != SCE_OK) { std::cerr << "Draw failed: " << err << std::endl; } diff --git a/src/services/impl/gxm_graphics_backend.hpp b/src/services/impl/gxm_graphics_backend.hpp index 1a5b4b5..6d79d47 100644 --- a/src/services/impl/gxm_graphics_backend.hpp +++ b/src/services/impl/gxm_graphics_backend.hpp @@ -41,7 +41,8 @@ public: void Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline, GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer, - uint32_t indexCount, const std::array& modelMatrix) override; + uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset, + const std::array& modelMatrix) override; GraphicsDeviceHandle GetPhysicalDevice() const override; std::pair GetSwapchainExtent() const override; diff --git a/src/services/impl/vulkan_graphics_backend.cpp b/src/services/impl/vulkan_graphics_backend.cpp index 92a3e93..39e9e60 100644 --- a/src/services/impl/vulkan_graphics_backend.cpp +++ b/src/services/impl/vulkan_graphics_backend.cpp @@ -246,8 +246,12 @@ void VulkanGraphicsBackend::SetRenderGraphDefinition(const RenderGraphDefinition void VulkanGraphicsBackend::Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline, GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer, - uint32_t indexCount, const std::array& modelMatrix) { - logger_->Trace("VulkanGraphicsBackend", "Draw", "indexCount=" + std::to_string(indexCount)); + uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset, + const std::array& modelMatrix) { + logger_->Trace("VulkanGraphicsBackend", "Draw", + "indexOffset=" + std::to_string(indexOffset) + + ", indexCount=" + std::to_string(indexCount) + + ", vertexOffset=" + std::to_string(vertexOffset)); // Find the shader key for this pipeline auto it = pipelineToShaderKey_.find(pipeline); @@ -258,9 +262,9 @@ void VulkanGraphicsBackend::Draw(GraphicsDeviceHandle device, GraphicsPipelineHa // Create a render command RenderCommand command; - command.indexOffset = 0; // TODO: Calculate proper offset + command.indexOffset = indexOffset; command.indexCount = indexCount; - command.vertexOffset = 0; // TODO: Calculate proper offset + command.vertexOffset = vertexOffset; command.shaderKey = it->second; command.modelMatrix = modelMatrix; diff --git a/src/services/impl/vulkan_graphics_backend.hpp b/src/services/impl/vulkan_graphics_backend.hpp index f98dae4..f2a095f 100644 --- a/src/services/impl/vulkan_graphics_backend.hpp +++ b/src/services/impl/vulkan_graphics_backend.hpp @@ -52,7 +52,8 @@ public: void Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline, GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer, - uint32_t indexCount, const std::array& modelMatrix) override; + uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset, + const std::array& modelMatrix) override; GraphicsDeviceHandle GetPhysicalDevice() const override; std::pair GetSwapchainExtent() const override; diff --git a/src/services/interfaces/i_graphics_backend.hpp b/src/services/interfaces/i_graphics_backend.hpp index b0359f3..f36c318 100644 --- a/src/services/interfaces/i_graphics_backend.hpp +++ b/src/services/interfaces/i_graphics_backend.hpp @@ -160,12 +160,15 @@ public: * @param pipeline Pipeline handle * @param vertexBuffer Vertex buffer handle * @param indexBuffer Index buffer handle + * @param indexOffset Starting index offset * @param indexCount Number of indices + * @param vertexOffset Base vertex offset * @param modelMatrix Model transformation matrix */ virtual void Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline, GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer, - uint32_t indexCount, const std::array& modelMatrix) = 0; + uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset, + const std::array& modelMatrix) = 0; /** * @brief Get the physical device handle.