feat(graphics): Update Draw function to include index and vertex offsets

This commit is contained in:
2026-01-06 10:48:50 +00:00
parent b05a63db9e
commit f619001157
6 changed files with 31 additions and 13 deletions

View File

@@ -178,7 +178,8 @@ void GraphicsService::RenderScene(const std::vector<RenderCommand>& 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);
}
}
}

View File

@@ -1,4 +1,5 @@
#include "gxm_graphics_backend.hpp"
#include "../../core/vertex.hpp"
#include <psp2/gxm.h>
#include <psp2/display.h>
#include <psp2/kernel/sysmem.h>
@@ -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<float, 16>& modelMatrix) {
std::cout << "GXM: Drawing " << indexCount << " indices" << std::endl;
uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset,
const std::array<float, 16>& 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<uint32_t>(sizeof(core::Vertex));
uint8_t* vertexBase = static_cast<uint8_t*>(vbHandle->data);
vertexBase += static_cast<size_t>(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<uint8_t*>(ibHandle->data);
indexBase += static_cast<size_t>(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;
}

View File

@@ -41,7 +41,8 @@ public:
void Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline,
GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer,
uint32_t indexCount, const std::array<float, 16>& modelMatrix) override;
uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset,
const std::array<float, 16>& modelMatrix) override;
GraphicsDeviceHandle GetPhysicalDevice() const override;
std::pair<uint32_t, uint32_t> GetSwapchainExtent() const override;

View File

@@ -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<float, 16>& modelMatrix) {
logger_->Trace("VulkanGraphicsBackend", "Draw", "indexCount=" + std::to_string(indexCount));
uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset,
const std::array<float, 16>& 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;

View File

@@ -52,7 +52,8 @@ public:
void Draw(GraphicsDeviceHandle device, GraphicsPipelineHandle pipeline,
GraphicsBufferHandle vertexBuffer, GraphicsBufferHandle indexBuffer,
uint32_t indexCount, const std::array<float, 16>& modelMatrix) override;
uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset,
const std::array<float, 16>& modelMatrix) override;
GraphicsDeviceHandle GetPhysicalDevice() const override;
std::pair<uint32_t, uint32_t> GetSwapchainExtent() const override;

View File

@@ -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<float, 16>& modelMatrix) = 0;
uint32_t indexOffset, uint32_t indexCount, int32_t vertexOffset,
const std::array<float, 16>& modelMatrix) = 0;
/**
* @brief Get the physical device handle.