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:
co-authored by
Claude Opus 5.5
parent
545cbb1e5d
commit
aaee4c8f70
@@ -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) {
|
||||
|
||||
@@ -7,12 +7,16 @@
|
||||
#include <memory>
|
||||
#include "RenderCommands3D.h"
|
||||
namespace ava2 {
|
||||
// 48-byte vertex; the GPU expands the packed fields to the same float/uint shader inputs.
|
||||
struct HardwareVertex {
|
||||
float position[4], color[4], uv[2];
|
||||
float normal[4]{};
|
||||
uint32_t bones[4]{};
|
||||
float weights[4]{};
|
||||
float position[4]; // FLOAT4 (raw-vertex batches may carry w != 1)
|
||||
uint8_t color[4]; // UBYTE4_NORM, RGBA
|
||||
float uv[2]; // FLOAT2
|
||||
int16_t normal[4]{}; // SHORT4_NORM, unit xyz; w=1 when the vertex has a normal
|
||||
uint8_t bones[4]{}; // UBYTE4 (palette holds at most 128 bones)
|
||||
uint16_t weights[4]{}; // USHORT4_NORM
|
||||
};
|
||||
static_assert(sizeof(HardwareVertex)==48);
|
||||
struct HardwareTransform {
|
||||
float world[16]{}, mvp[16]{};
|
||||
float diffuse[4]{}, ambient[4]{}, emissive[4]{};
|
||||
|
||||
@@ -29,7 +29,7 @@ int main() {
|
||||
std::vector<ava2::HardwareVertex> vertices;
|
||||
auto triangle=[&](float z,float r,float g,float b,float alpha) {
|
||||
for(auto p: {std::pair{-0.9f,-0.9f},std::pair{0.9f,-0.9f},std::pair{0.f,0.9f}})
|
||||
vertices.push_back({{p.first,p.second,z,1},{r,g,b,alpha},{0,0}});
|
||||
vertices.push_back({{p.first,p.second,z,1},{uint8_t(r*255),uint8_t(g*255),uint8_t(b*255),uint8_t(alpha*255)},{0,0}});
|
||||
};
|
||||
triangle(.2,1,0,0,1);triangle(.8,0,1,0,1);triangle(.1,0,0,1,.2);
|
||||
std::vector<uint32_t> indices{0,1,2,3,4,5,6,7,8};
|
||||
@@ -59,7 +59,7 @@ int main() {
|
||||
// Perspective-correct color interpolation: top W=2 doubles its reciprocal denominator.
|
||||
auto saved=vertices;
|
||||
vertices[2].position[1]*=2;vertices[2].position[2]*=2;vertices[2].position[3]=2;
|
||||
vertices[0].color[0]=vertices[1].color[0]=0;vertices[2].color[0]=1;
|
||||
vertices[0].color[0]=vertices[1].color[0]=0;vertices[2].color[0]=255;
|
||||
near.cull=1;color=center({near});assert(color[0]>65&&color[0]<100);
|
||||
vertices=saved;
|
||||
// Newly uploaded textures must be usable on their first frame, even
|
||||
@@ -167,7 +167,7 @@ int main() {
|
||||
left.geometry=skinned;pixels=frame({left});
|
||||
std::vector<ava2::HardwareVertex> reference;
|
||||
for(size_t v=0;v<3;++v){auto vertex=ava2::MakeHardwareVertex(*skinned,v);float pos[4]{};
|
||||
for(size_t b=0;b<2;++b){const auto* matrix=left.palette->data()+b*16;float w=vertex.weights[b];
|
||||
for(size_t b=0;b<2;++b){const auto* matrix=left.palette->data()+b*16;float w=vertex.weights[b]/65535.f;
|
||||
for(int k=0;k<4;++k)pos[k]+=(vertex.position[0]*matrix[k]+vertex.position[1]*matrix[k+4]+vertex.position[2]*matrix[k+8]+matrix[k+12])*w;}
|
||||
std::copy_n(pos,4,vertex.position);vertex.position[0]-=.5f;reference.push_back(vertex);}
|
||||
ava2::HardwareBatch cpu{};cpu.indexCount=3;cpu.cull=1;cpu.depth=true;cpu.depthWrite=true;cpu.alphaRef=-1;
|
||||
|
||||
Reference in New Issue
Block a user