- Create proper sprite shader (vs_sprite.sc, fs_sprite.sc) with GPU instancing - SpriteInstance struct now 80 bytes (5 x vec4) for GPU layout compatibility - Add BufferDesc::Layout enum (Raw, PosColor, InstanceData) for proper vertex layouts - BgfxDevice creates correct VertexLayout based on buffer type - SpritePass uses PosColor layout for quad vertices (Position + Color0) - Instance buffer uses 5 x vec4 layout (TexCoord7-3) for i_data0-4 - Add texture support with v_texcoord0 interpolation from instance UVs - Create default white 1x1 texture fallback - Compile shaders for SPIRV (Vulkan), GLSL (OpenGL), and Metal Visual test confirms sprites render correctly at ~545 FPS. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
120 lines
4.1 KiB
C++
120 lines
4.1 KiB
C++
#include "SpritePass.h"
|
|
#include "../RHI/RHIDevice.h"
|
|
#include "../Frame/FramePacket.h"
|
|
|
|
namespace grove {
|
|
|
|
SpritePass::SpritePass(rhi::ShaderHandle shader)
|
|
: m_shader(shader)
|
|
{
|
|
}
|
|
|
|
void SpritePass::setup(rhi::IRHIDevice& device) {
|
|
// Create quad vertex buffer (unit quad, instanced)
|
|
// Layout must match shader: a_position (vec3) + a_color0 (vec4)
|
|
// Note: Color is white (1,1,1,1) - actual color comes from instance data
|
|
float quadVertices[] = {
|
|
// pos.x, pos.y, pos.z, r, g, b, a
|
|
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // bottom-left
|
|
1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // bottom-right
|
|
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // top-right
|
|
0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, // top-left
|
|
};
|
|
|
|
rhi::BufferDesc vbDesc;
|
|
vbDesc.type = rhi::BufferDesc::Vertex;
|
|
vbDesc.size = sizeof(quadVertices);
|
|
vbDesc.data = quadVertices;
|
|
vbDesc.dynamic = false;
|
|
vbDesc.layout = rhi::BufferDesc::PosColor; // Match shader: a_position + a_color0
|
|
m_quadVB = device.createBuffer(vbDesc);
|
|
|
|
// Create index buffer
|
|
uint16_t quadIndices[] = {
|
|
0, 1, 2, // first triangle
|
|
0, 2, 3 // second triangle
|
|
};
|
|
|
|
rhi::BufferDesc ibDesc;
|
|
ibDesc.type = rhi::BufferDesc::Index;
|
|
ibDesc.size = sizeof(quadIndices);
|
|
ibDesc.data = quadIndices;
|
|
ibDesc.dynamic = false;
|
|
m_quadIB = device.createBuffer(ibDesc);
|
|
|
|
// Create dynamic instance buffer
|
|
rhi::BufferDesc instDesc;
|
|
instDesc.type = rhi::BufferDesc::Instance;
|
|
instDesc.size = MAX_SPRITES_PER_BATCH * sizeof(SpriteInstance);
|
|
instDesc.data = nullptr;
|
|
instDesc.dynamic = true;
|
|
m_instanceBuffer = device.createBuffer(instDesc);
|
|
|
|
// Create texture sampler uniform (must match shader: s_texColor)
|
|
m_textureSampler = device.createUniform("s_texColor", 1);
|
|
|
|
// Create default white 1x1 texture (used when no texture is bound)
|
|
uint32_t whitePixel = 0xFFFFFFFF; // RGBA white
|
|
rhi::TextureDesc texDesc;
|
|
texDesc.width = 1;
|
|
texDesc.height = 1;
|
|
texDesc.format = rhi::TextureDesc::RGBA8;
|
|
texDesc.data = &whitePixel;
|
|
texDesc.dataSize = sizeof(whitePixel);
|
|
m_defaultTexture = device.createTexture(texDesc);
|
|
}
|
|
|
|
void SpritePass::shutdown(rhi::IRHIDevice& device) {
|
|
device.destroy(m_quadVB);
|
|
device.destroy(m_quadIB);
|
|
device.destroy(m_instanceBuffer);
|
|
device.destroy(m_textureSampler);
|
|
device.destroy(m_defaultTexture);
|
|
// Note: m_shader is owned by ShaderManager, not destroyed here
|
|
}
|
|
|
|
void SpritePass::execute(const FramePacket& frame, rhi::IRHIDevice& device, rhi::RHICommandBuffer& cmd) {
|
|
if (frame.spriteCount == 0) {
|
|
return;
|
|
}
|
|
|
|
// Set render state for sprites (alpha blending, no depth)
|
|
rhi::RenderState state;
|
|
state.blend = rhi::BlendMode::Alpha;
|
|
state.cull = rhi::CullMode::None;
|
|
state.depthTest = false;
|
|
state.depthWrite = false;
|
|
cmd.setState(state);
|
|
|
|
// Process sprites in batches
|
|
size_t remaining = frame.spriteCount;
|
|
size_t offset = 0;
|
|
|
|
while (remaining > 0) {
|
|
size_t batchSize = (remaining > MAX_SPRITES_PER_BATCH)
|
|
? MAX_SPRITES_PER_BATCH : remaining;
|
|
|
|
// Update instance buffer with sprite data
|
|
// The SpriteInstance struct matches what we send to GPU
|
|
const SpriteInstance* batchData = frame.sprites + offset;
|
|
device.updateBuffer(m_instanceBuffer, batchData,
|
|
static_cast<uint32_t>(batchSize * sizeof(SpriteInstance)));
|
|
|
|
cmd.setVertexBuffer(m_quadVB);
|
|
cmd.setIndexBuffer(m_quadIB);
|
|
cmd.setInstanceBuffer(m_instanceBuffer, 0, static_cast<uint32_t>(batchSize));
|
|
|
|
// Bind default texture (TODO: support per-sprite textures via texture array)
|
|
cmd.setTexture(0, m_defaultTexture, m_textureSampler);
|
|
|
|
// Submit draw call
|
|
cmd.drawInstanced(6, static_cast<uint32_t>(batchSize)); // 6 indices per quad
|
|
cmd.submit(0, m_shader, 0);
|
|
|
|
offset += batchSize;
|
|
remaining -= batchSize;
|
|
}
|
|
}
|
|
|
|
} // namespace grove
|