Share GPU skinning palette per instance; push it once per change
Render3DDraw/HardwareBatch hold the SDK's shared palette instead of copying 16 floats per bone per draw, and HardwareScene re-pushes the uniform only when the palette pointer changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1eccdc87d7
commit
837ccc2f31
+1
-1
Submodule sdk updated: de8fa40098...a1b3938159
@@ -67,7 +67,7 @@ Render3DDraw ToRender3DDraw(gr2_render::Gr2MeshDraw&& mesh) {
|
||||
draw.indices = std::move(mesh.indices);
|
||||
draw.bone_indices = std::move(mesh.bone_indices);
|
||||
draw.bone_weights = std::move(mesh.bone_weights);
|
||||
draw.bone_matrices = std::move(mesh.bone_matrices);
|
||||
draw.palette = std::move(mesh.palette);
|
||||
draw.alpha_test = mesh.alpha_test;
|
||||
draw.alpha_ref = mesh.alpha_ref;
|
||||
draw.cull_mode = mesh.double_sided ? 1 : 2;
|
||||
@@ -914,7 +914,7 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
|
||||
t.fogColor[0]=fog_r;t.fogColor[1]=fog_g;t.fogColor[2]=fog_b;
|
||||
t.fogParams[0]=m_fog_density;t.fogParams[1]=m_fog_near;t.fogParams[2]=m_fog_far;
|
||||
t.modes[0]=1;t.modes[1]=draw.lighting ? 1.f : 0.f;t.modes[2]=m_fog_enabled&&draw.fog ? (m_fog_exp ? 2.f : 1.f) : 0.f;t.modes[3]=draw.z_enable ? 0.f : 1.f;
|
||||
if(draw.gpu_skinning){batch.palette=draw.bone_matrices;t.skin[0]=batch.palette.size()/16;}
|
||||
if(draw.gpu_skinning && draw.palette){batch.palette=draw.palette;t.skin[0]=batch.palette->size()/16;}
|
||||
batch.indexCount=mesh.indices.size();
|
||||
if(!batch.geometry) {
|
||||
const uint32_t base=m_hardwareVertices.size();batch.firstIndex=m_hardwareIndices.size();
|
||||
|
||||
@@ -268,6 +268,7 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
auto* pass=SDL_BeginGPURenderPass(cmd,&color,1,&depth);if(!pass){SDL_CancelGPUCommandBuffer(cmd);return false;}
|
||||
SDL_BindGPUVertexStorageBuffers(pass,0,&instances_,1);
|
||||
float palette[128*16]{};SDL_PushGPUVertexUniformData(cmd,1,palette,sizeof(palette));
|
||||
const std::vector<float>* pushedPalette=nullptr; // draws of one instance share a palette; push once
|
||||
for(size_t i=0;i<batches.size();) {
|
||||
const auto& batch=batches[i];auto* mesh=meshes[i];
|
||||
size_t count=1;
|
||||
@@ -285,7 +286,7 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
float params[4]={batch.alphaRef,(!batch.blend&&!batch.additive)?1.f:0.f,0,0};SDL_PushGPUFragmentUniformData(cmd,0,params,sizeof(params));
|
||||
auto transform=batch.transform;if(count>1)transform.modes[0]=2;
|
||||
SDL_PushGPUVertexUniformData(cmd,0,&transform,sizeof(transform));
|
||||
if(transform.skin[0]>0){std::fill(std::begin(palette),std::end(palette),0);std::copy_n(batch.palette.data(),std::min(batch.palette.size(),size_t(128*16)),palette);SDL_PushGPUVertexUniformData(cmd,1,palette,sizeof(palette));}
|
||||
if(transform.skin[0]>0 && batch.palette && batch.palette.get()!=pushedPalette){pushedPalette=batch.palette.get();std::fill(std::begin(palette),std::end(palette),0);std::copy_n(batch.palette->data(),std::min(batch.palette->size(),size_t(128*16)),palette);SDL_PushGPUVertexUniformData(cmd,1,palette,sizeof(palette));}
|
||||
SDL_DrawGPUIndexedPrimitives(pass,batch.indexCount,count,mesh ? 0 : batch.firstIndex,0,count>1 ? i : 0);++lastDrawCalls_;i+=count;
|
||||
}
|
||||
SDL_EndGPURenderPass(pass);const auto beforeSubmit=SDL_GetPerformanceCounter();
|
||||
|
||||
@@ -31,7 +31,7 @@ struct HardwareBatch {
|
||||
float alphaRef;
|
||||
HardwareTransform transform{};
|
||||
std::shared_ptr<const Render3DDraw> geometry;
|
||||
std::vector<float> palette;
|
||||
std::shared_ptr<const std::vector<float>> palette;
|
||||
|
||||
};
|
||||
class HardwareScene {
|
||||
|
||||
@@ -52,7 +52,6 @@ struct Render3DDraw {
|
||||
command.indices.clear(); command.indices.shrink_to_fit();
|
||||
command.bone_indices.clear(); command.bone_indices.shrink_to_fit();
|
||||
command.bone_weights.clear(); command.bone_weights.shrink_to_fit();
|
||||
command.bone_matrices.clear(); command.bone_matrices.shrink_to_fit();
|
||||
command.geometry = std::make_shared<const Render3DDraw>(std::move(source));
|
||||
return command;
|
||||
}
|
||||
@@ -83,7 +82,7 @@ struct Render3DDraw {
|
||||
// Skeletal skinning streams
|
||||
std::vector<std::uint8_t> bone_indices; // 4 uint8 per vertex
|
||||
std::vector<float> bone_weights; // 4 floats per vertex
|
||||
std::vector<float> bone_matrices; // 16 floats per bone (palette)
|
||||
std::shared_ptr<const std::vector<float>> palette; // GPU skinning, 16 floats per bone; shared per instance
|
||||
|
||||
// Render states
|
||||
bool z_enable = true;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include "test_rundir.h"
|
||||
@@ -716,8 +717,13 @@ int main(int argc, char** argv) {
|
||||
assert(cpu.size()==gpu.size());
|
||||
auto again=instance.GetImpl()->BuildDrawCommands(time+.1f,nullptr,true);
|
||||
assert(again.size()==gpu.size());
|
||||
// One palette per instance per frame: draws of a mesh/part share it instead of copying.
|
||||
std::set<const void*> palettes;size_t skinned=0;
|
||||
for(const auto& g:gpu)if(g.gpu_skinning){palettes.insert(g.palette.get());++skinned;}
|
||||
assert(skinned<2 || palettes.size()<skinned);
|
||||
for(size_t d=0;d<cpu.size();++d) {
|
||||
const auto& g=gpu[d];assert(g.geometry && again[d].geometry==g.geometry);
|
||||
assert(!g.gpu_skinning || g.palette);
|
||||
const auto& mesh=*g.geometry;
|
||||
assert(cpu[d].positions.size()==mesh.positions.size());
|
||||
assert(cpu[d].indices==mesh.indices);
|
||||
@@ -725,8 +731,8 @@ int main(int argc, char** argv) {
|
||||
float p[3]{},n[3]{},total=0;
|
||||
if(g.gpu_skinning)for(int b=0;b<4;++b) {
|
||||
const auto bone=mesh.bone_indices[v*4+b];const float w=mesh.bone_weights[v*4+b];
|
||||
if(w<=0 || size_t(bone)*16+15>=g.bone_matrices.size())continue;
|
||||
const auto* m=g.bone_matrices.data()+bone*16;
|
||||
if(w<=0 || size_t(bone)*16+15>=g.palette->size())continue;
|
||||
const auto* m=g.palette->data()+bone*16;
|
||||
for(int k=0;k<3;++k){p[k]+=(mesh.positions[v*3]*m[k]+mesh.positions[v*3+1]*m[k+4]+mesh.positions[v*3+2]*m[k+8]+m[k+12])*w;
|
||||
n[k]+=(mesh.normals[v*3]*m[k]+mesh.normals[v*3+1]*m[k+4]+mesh.normals[v*3+2]*m[k+8])*w;}
|
||||
total+=w;
|
||||
|
||||
@@ -139,20 +139,20 @@ int main() {
|
||||
assert(resident.GetLastDrawCallCount()==2 && redAt(16)>245 && redAt(48)>120 && redAt(48)<135);
|
||||
SDL_DestroySurface(pixels);
|
||||
// Bone translation moves the raw triangle to the center; no CPU-deformed stream is uploaded.
|
||||
left.transform.skin[0]=1;left.palette={1,0,0,0,0,1,0,0,0,0,1,0,.5f,0,0,1};
|
||||
left.transform.skin[0]=1;left.palette=std::make_shared<std::vector<float>>(std::vector<float>{1,0,0,0,0,1,0,0,0,0,1,0,.5f,0,0,1});
|
||||
auto skinned=std::make_shared<ava2::Render3DDraw>(*mesh);skinned->bone_indices=std::vector<uint8_t>(12,0);skinned->bone_weights={1,0,0,0,1,0,0,0,1,0,0,0};left.geometry=skinned;
|
||||
pixels=frame({left});assert(redAt(32)>245 && redAt(16)<5);SDL_DestroySurface(pixels);
|
||||
// Two weighted, rotated bones: compare every rendered pixel to CPU-transformed vertices.
|
||||
left.transform.skin[0]=2;
|
||||
left.palette={1,0,0,0,0,1,0,0,0,0,1,0,.2f,0,0,1,
|
||||
0,1,0,0,-1,0,0,0,0,0,1,0,.7f,0,0,1};
|
||||
left.palette=std::make_shared<std::vector<float>>(std::vector<float>{1,0,0,0,0,1,0,0,0,0,1,0,.2f,0,0,1,
|
||||
0,1,0,0,-1,0,0,0,0,0,1,0,.7f,0,0,1});
|
||||
skinned=std::make_shared<ava2::Render3DDraw>(*mesh);
|
||||
skinned->bone_indices={0,1,0,0,0,1,0,0,0,1,0,0};
|
||||
skinned->bone_weights={.4f,.6f,0,0,.4f,.6f,0,0,.4f,.6f,0,0};
|
||||
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];
|
||||
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