diff --git a/src/eter_render/HardwareScene.cpp b/src/eter_render/HardwareScene.cpp index 4922f190..2e166501 100644 --- a/src/eter_render/HardwareScene.cpp +++ b/src/eter_render/HardwareScene.cpp @@ -1,5 +1,7 @@ #include "HardwareScene.h" #include "shaders/SceneSpirv.h" +#include +#include #include #include #include @@ -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>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+11e-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& geometry) { diff --git a/src/eter_render/HardwareScene.h b/src/eter_render/HardwareScene.h index 82782ea4..302bb554 100644 --- a/src/eter_render/HardwareScene.h +++ b/src/eter_render/HardwareScene.h @@ -7,12 +7,16 @@ #include #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]{}; diff --git a/tests/hardware_scene_test.cpp b/tests/hardware_scene_test.cpp index ca29db3f..00b9bd04 100644 --- a/tests/hardware_scene_test.cpp +++ b/tests/hardware_scene_test.cpp @@ -29,7 +29,7 @@ int main() { std::vector 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 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 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;