render: draw tri-groups as index sub-ranges of one resident mesh
Render3DDraw/HardwareBatch carry first_index/index_count; resident meshes are uploaded once per model and groups draw with firstIndex. Bumps sdk to 9ec7fa1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d5333745c5
commit
f03f63e700
+1
-1
Submodule sdk updated: e354b92e08...9ec7fa10c0
@@ -65,6 +65,8 @@ Render3DDraw ToRender3DDraw(gr2_render::Gr2MeshDraw&& mesh) {
|
||||
draw.uv0 = std::move(mesh.uv0);
|
||||
draw.diffuse = std::move(mesh.diffuse);
|
||||
draw.indices = std::move(mesh.indices);
|
||||
draw.first_index = mesh.first_index;
|
||||
draw.index_count = mesh.index_count;
|
||||
draw.bone_indices = std::move(mesh.bone_indices);
|
||||
draw.bone_weights = std::move(mesh.bone_weights);
|
||||
draw.palette = std::move(mesh.palette);
|
||||
@@ -364,7 +366,8 @@ void Gr2SceneRenderer::SetFog(bool enable, uint32_t color, float density, float
|
||||
|
||||
void Gr2SceneRenderer::RenderDraw(SDL_Surface* targetSurface, const Render3DDraw& draw) {
|
||||
const auto& mesh = draw.Mesh();
|
||||
if (!targetSurface || mesh.indices.size() < 3 || mesh.positions.size() < 9) return;
|
||||
const auto drawIndices = draw.Indices();
|
||||
if (!targetSurface || drawIndices.size < 3 || mesh.positions.size() < 9) return;
|
||||
|
||||
int vpWidth = m_viewport[2];
|
||||
int vpHeight = m_viewport[3];
|
||||
@@ -457,7 +460,7 @@ void Gr2SceneRenderer::RenderDraw(SDL_Surface* targetSurface, const Render3DDraw
|
||||
diffuse_tex = GetTexture(draw.texture0);
|
||||
}
|
||||
|
||||
std::size_t num_tris = mesh.indices.size() / 3;
|
||||
std::size_t num_tris = drawIndices.size / 3;
|
||||
|
||||
auto make_screen_vertex = [&](TransformedVertex& v) {
|
||||
if (v.w_clip > 1e-5f) {
|
||||
@@ -677,9 +680,9 @@ void Gr2SceneRenderer::RenderDraw(SDL_Surface* targetSurface, const Render3DDraw
|
||||
|
||||
constexpr float W_NEAR = 0.1f;
|
||||
for (std::size_t t = 0; t < num_tris; ++t) {
|
||||
uint32_t i0 = mesh.indices[t * 3 + 0];
|
||||
uint32_t i1 = mesh.indices[t * 3 + 1];
|
||||
uint32_t i2 = mesh.indices[t * 3 + 2];
|
||||
uint32_t i0 = drawIndices.data[t * 3 + 0];
|
||||
uint32_t i1 = drawIndices.data[t * 3 + 1];
|
||||
uint32_t i2 = drawIndices.data[t * 3 + 2];
|
||||
if (i0 >= num_verts || i1 >= num_verts || i2 >= num_verts) continue;
|
||||
|
||||
const auto& v0 = xverts[i0];
|
||||
@@ -873,7 +876,8 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
|
||||
for (const auto& ord : m_scratchSortedDraws) {
|
||||
const auto& draw = *ord.draw;
|
||||
const auto& mesh = draw.Mesh();
|
||||
if (mesh.indices.size() < 3 || mesh.positions.size() < 9) continue;
|
||||
const auto drawIndices = draw.Indices();
|
||||
if (drawIndices.size < 3 || mesh.positions.size() < 9) continue;
|
||||
if (draw.texture0.empty() && mesh.diffuse.empty()) continue;
|
||||
|
||||
float mv[16];
|
||||
@@ -915,12 +919,14 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
|
||||
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 && draw.palette){batch.palette=draw.palette;t.skin[0]=batch.palette->size()/16;}
|
||||
batch.indexCount=mesh.indices.size();
|
||||
batch.indexCount=drawIndices.size;
|
||||
batch.firstIndex=drawIndices.data-mesh.indices.data(); // resident buffers hold the whole mesh
|
||||
if(!batch.geometry) {
|
||||
const uint32_t base=m_hardwareVertices.size();batch.firstIndex=m_hardwareIndices.size();
|
||||
bool valid=true;for(auto index:mesh.indices)if(index>=num_verts){valid=false;break;}if(!valid)continue;
|
||||
const auto* first=drawIndices.data;const auto* last=first+drawIndices.size;
|
||||
if(std::any_of(first,last,[&](uint32_t index){return index>=num_verts;}))continue;
|
||||
for(size_t i=0;i<num_verts;++i)m_hardwareVertices.push_back(MakeHardwareVertex(mesh,i));
|
||||
for(auto index:mesh.indices)m_hardwareIndices.push_back(base+index);
|
||||
for(const auto* index=first;index!=last;++index)m_hardwareIndices.push_back(base+*index);
|
||||
}
|
||||
m_hardwareBatches.push_back(std::move(batch));continue;
|
||||
}
|
||||
@@ -1004,8 +1010,8 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
|
||||
|
||||
m_scratchSdlVertices.clear();
|
||||
m_scratchSdlIndices.clear();
|
||||
m_scratchSdlVertices.reserve(mesh.indices.size());
|
||||
m_scratchSdlIndices.reserve(mesh.indices.size());
|
||||
m_scratchSdlVertices.reserve(drawIndices.size);
|
||||
m_scratchSdlIndices.reserve(drawIndices.size);
|
||||
auto& vertices = m_scratchSdlVertices;
|
||||
auto& indices = m_scratchSdlIndices;
|
||||
|
||||
@@ -1049,11 +1055,11 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
|
||||
return out;
|
||||
};
|
||||
|
||||
size_t num_tris = mesh.indices.size() / 3;
|
||||
size_t num_tris = drawIndices.size / 3;
|
||||
for (size_t t = 0; t < num_tris; ++t) {
|
||||
uint32_t i0 = mesh.indices[t * 3 + 0];
|
||||
uint32_t i1 = mesh.indices[t * 3 + 1];
|
||||
uint32_t i2 = mesh.indices[t * 3 + 2];
|
||||
uint32_t i0 = drawIndices.data[t * 3 + 0];
|
||||
uint32_t i1 = drawIndices.data[t * 3 + 1];
|
||||
uint32_t i2 = drawIndices.data[t * 3 + 2];
|
||||
if (i0 >= num_verts || i1 >= num_verts || i2 >= num_verts) continue;
|
||||
|
||||
const auto& v0 = xverts[i0];
|
||||
|
||||
@@ -281,7 +281,7 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
if(mesh && !batch.blend && !batch.additive && batch.depthWrite && batch.transform.skin[0]==0 && batch.transform.modes[0]>0.5f)
|
||||
while(i+count<batches.size()) {
|
||||
const auto& next=batches[i+count];
|
||||
if(meshes[i+count]!=mesh || next.texture!=batch.texture || next.cull!=batch.cull || next.depth!=batch.depth || next.depthWrite!=batch.depthWrite || next.blend || next.additive || next.alphaRef!=batch.alphaRef || next.transform.skin[0]!=0 || next.transform.modes[0]<.5f)break;
|
||||
if(meshes[i+count]!=mesh || next.firstIndex!=batch.firstIndex || next.indexCount!=batch.indexCount || next.texture!=batch.texture || next.cull!=batch.cull || next.depth!=batch.depth || next.depthWrite!=batch.depthWrite || next.blend || next.additive || next.alphaRef!=batch.alphaRef || next.transform.skin[0]!=0 || next.transform.modes[0]<.5f)break;
|
||||
++count;
|
||||
}
|
||||
SDL_GPUBufferBinding binding{mesh ? mesh->vertices : vertices_,0};SDL_BindGPUVertexBuffers(pass,0,&binding,1);
|
||||
@@ -292,7 +292,7 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
auto transform=batch.transform;if(count>1)transform.modes[0]=2;
|
||||
SDL_PushGPUVertexUniformData(cmd,0,&transform,sizeof(transform));
|
||||
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_DrawGPUIndexedPrimitives(pass,batch.indexCount,count,batch.firstIndex,0,count>1 ? i : 0);++lastDrawCalls_;i+=count;
|
||||
}
|
||||
SDL_EndGPURenderPass(pass);const auto beforeSubmit=SDL_GetPerformanceCounter();
|
||||
if(!SDL_SubmitGPUCommandBuffer(cmd))return false;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -78,6 +79,15 @@ struct Render3DDraw {
|
||||
std::vector<float> uv1; // 2 floats (u, v) per vertex
|
||||
std::vector<std::uint32_t> diffuse; // 0xAARRGGBB per vertex
|
||||
std::vector<std::uint32_t> indices; // Triangle list indices (3 per triangle)
|
||||
// Sub-range of Mesh().indices this draw covers; tri-groups of one mesh share geometry.
|
||||
std::uint32_t first_index = 0;
|
||||
std::uint32_t index_count = UINT32_MAX; // UINT32_MAX = through the end
|
||||
struct IndexSpan { const std::uint32_t* data; std::size_t size; };
|
||||
IndexSpan Indices() const {
|
||||
const auto& all = Mesh().indices;
|
||||
const std::size_t first = std::min<std::size_t>(first_index, all.size());
|
||||
return {all.data() + first, std::min<std::size_t>(index_count, all.size() - first)};
|
||||
}
|
||||
|
||||
// Skeletal skinning streams
|
||||
std::vector<std::uint8_t> bone_indices; // 4 uint8 per vertex
|
||||
|
||||
@@ -726,7 +726,10 @@ int main(int argc, char** argv) {
|
||||
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);
|
||||
// Tri-groups share one geometry per mesh; each GPU draw covers its own index range.
|
||||
assert(g.first_index+size_t(cpu[d].indices.size())<=mesh.indices.size() || g.index_count==0);
|
||||
assert(std::equal(cpu[d].indices.begin(),cpu[d].indices.end(),mesh.indices.begin()+std::min<size_t>(g.first_index,mesh.indices.size())));
|
||||
assert(g.index_count==UINT32_MAX ? cpu[d].indices.size()==mesh.indices.size() : cpu[d].indices.size()==g.index_count);
|
||||
for(size_t v=0;v<mesh.positions.size()/3;++v) {
|
||||
float p[3]{},n[3]{},total=0;
|
||||
if(g.gpu_skinning)for(int b=0;b<4;++b) {
|
||||
|
||||
@@ -138,6 +138,21 @@ int main() {
|
||||
pixels=frame({offscreen,left,right});
|
||||
assert(resident.GetLastDrawCallCount()==2 && redAt(16)>245 && redAt(48)>120 && redAt(48)<135);
|
||||
SDL_DestroySurface(pixels);
|
||||
// Tri-group sub-ranges share one resident buffer; firstIndex selects the right-hand triangle only.
|
||||
{
|
||||
auto groups=std::make_shared<ava2::Render3DDraw>(*mesh);
|
||||
groups->positions={-.65f,-.2f,.5f,-.35f,-.2f,.5f,-.5f,.2f,.5f, .35f,-.2f,.5f,.65f,-.2f,.5f,.5f,.2f,.5f};
|
||||
groups->indices={0,1,2,3,4,5};groups->diffuse=std::vector<uint32_t>(6,0xffff0000);
|
||||
auto second=left;second.geometry=groups;second.transform.mvp[12]=0;second.firstIndex=3;second.indexCount=3;
|
||||
const auto uploads=resident.GetResidentUploadCount();
|
||||
pixels=frame({second});
|
||||
assert(resident.GetResidentUploadCount()==uploads+1 && resident.GetLastDynamicUploadBytes()==0);
|
||||
assert(redAt(48)>245 && redAt(16)<5);SDL_DestroySurface(pixels);
|
||||
auto first=second;first.firstIndex=0;
|
||||
pixels=frame({first,second});
|
||||
assert(resident.GetResidentUploadCount()==uploads+1 && resident.GetLastDrawCallCount()==2);
|
||||
assert(redAt(16)>245 && redAt(48)>245);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=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;
|
||||
|
||||
Reference in New Issue
Block a user