render: pack HardwareVertex from 88 to 48 bytes

Colour as UBYTE4_NORM, normals as SHORT4_NORM (unit direction, the
shader renormalises), bones as UBYTE4 and weights as USHORT4_NORM.
Shader inputs keep their float/uint types, so the shaders are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 16:33:19 +09:00
co-authored by Claude Opus 5.5
parent 545cbb1e5d
commit aaee4c8f70
3 changed files with 28 additions and 15 deletions
+17 -8
View File
@@ -1,5 +1,7 @@
#include "HardwareScene.h"
#include "shaders/SceneSpirv.h"
#include <cmath>
#include <cstddef>
#include <cstring>
#include <algorithm>
#include <unordered_set>
@@ -152,11 +154,11 @@ SDL_GPUGraphicsPipeline* HardwareScene::Pipeline(const HardwareBatch& b) {
SDL_GPUVertexBufferDescription buffer{};buffer.slot=0;buffer.pitch=sizeof(HardwareVertex);buffer.input_rate=SDL_GPU_VERTEXINPUTRATE_VERTEX;
SDL_GPUVertexAttribute attributes[6]{};
attributes[0]={0,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,0};
attributes[1]={1,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,16};
attributes[2]={2,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,32};
attributes[3]={3,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,40};
attributes[4]={4,0,SDL_GPU_VERTEXELEMENTFORMAT_UINT4,56};
attributes[5]={5,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,72};
attributes[1]={1,0,SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM,offsetof(HardwareVertex,color)};
attributes[2]={2,0,SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,offsetof(HardwareVertex,uv)};
attributes[3]={3,0,SDL_GPU_VERTEXELEMENTFORMAT_SHORT4_NORM,offsetof(HardwareVertex,normal)};
attributes[4]={4,0,SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4,offsetof(HardwareVertex,bones)};
attributes[5]={5,0,SDL_GPU_VERTEXELEMENTFORMAT_USHORT4_NORM,offsetof(HardwareVertex,weights)};
info.vertex_input_state={&buffer,1,attributes,6};
info.rasterizer_state.fill_mode=SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.front_face=SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
@@ -178,10 +180,17 @@ HardwareVertex MakeHardwareVertex(const Render3DDraw& mesh,size_t i) {
HardwareVertex v{};
v.position[0]=mesh.positions[i*3];v.position[1]=mesh.positions[i*3+1];v.position[2]=mesh.positions[i*3+2];v.position[3]=1;
uint32_t color=i<mesh.diffuse.size() ? mesh.diffuse[i] : 0xffffffff;
v.color[0]=((color>>16)&255)/255.f;v.color[1]=((color>>8)&255)/255.f;v.color[2]=(color&255)/255.f;v.color[3]=(color>>24)/255.f;
v.color[0]=uint8_t(color>>16);v.color[1]=uint8_t(color>>8);v.color[2]=uint8_t(color);v.color[3]=uint8_t(color>>24);
if(i*2+1<mesh.uv0.size()){v.uv[0]=mesh.uv0[i*2];v.uv[1]=mesh.uv0[i*2+1];}
if(i*3+2<mesh.normals.size()){for(int k=0;k<3;++k)v.normal[k]=mesh.normals[i*3+k];v.normal[3]=1;}
if(i*4+3<mesh.bone_indices.size() && i*4+3<mesh.bone_weights.size())for(int k=0;k<4;++k){v.bones[k]=mesh.bone_indices[i*4+k];v.weights[k]=mesh.bone_weights[i*4+k];}
if(i*3+2<mesh.normals.size()){
// The shader renormalises after the (linear) skin/world transform, so storing the unit
// direction keeps lighting identical while fitting SNORM.
const float* n=&mesh.normals[i*3];const float len=std::sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);
if(len>1e-12f)for(int k=0;k<3;++k)v.normal[k]=int16_t(std::lround(std::clamp(n[k]/len,-1.f,1.f)*32767.f));
v.normal[3]=32767;
}
if(i*4+3<mesh.bone_indices.size() && i*4+3<mesh.bone_weights.size())for(int k=0;k<4;++k){
v.bones[k]=mesh.bone_indices[i*4+k];v.weights[k]=uint16_t(std::lround(std::clamp(mesh.bone_weights[i*4+k],0.f,1.f)*65535.f));}
return v;
}
HardwareScene::ResidentMesh* HardwareScene::Resident(const std::shared_ptr<const Render3DDraw>& geometry) {