Fix cross-platform rendering and optimize frame performance

Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
This commit is contained in:
shen
2026-10-11 17:55:53 -07:00
parent 08cc8865be
commit 281418f351
101 changed files with 6244 additions and 1877 deletions
+35 -25
View File
@@ -1,7 +1,9 @@
#pragma once
#include "RenderCommands3D.h"
#include "HardwareScene.h"
#include <gr2/gr2.h>
#include <gr2_render/gr2_render.h>
#include <SDL3/SDL.h>
#include <string>
#include <unordered_map>
@@ -12,7 +14,13 @@ namespace ava2 {
class Gr2ModelInstance {
public:
Gr2ModelInstance() = default;
static void SetHardwareSkinningEnabled(bool enabled);
explicit Gr2ModelInstance(std::shared_ptr<gr2_render::Gr2ModelInstance> impl)
: m_impl(std::move(impl)) {}
static std::unique_ptr<Gr2ModelInstance> Load(const std::string& filepath);
static std::unique_ptr<Gr2ModelInstance> LoadFromMemory(const uint8_t* data, size_t size, const std::string& name = "");
// Load external animation (.gr2) to animate this model
bool LoadExternalAnimation(const std::string& anim_filepath);
@@ -31,11 +39,11 @@ public:
void SetAttachmentScale(const std::string& bone_name, float sx, float sy, float sz);
// CGrannyModelInstance::SetMaterialImagePointer: textures whose StringPath matches src are drawn with dst.
void SetSkin(const std::string& src_file_name, const std::string& dst_file_name);
void ClearSkins() { m_skins.clear(); }
const std::string& GetFilePath() const { return m_filepath; }
void ClearSkins();
const std::string& GetFilePath() const;
// Build 3D draw commands for this model and all attached components (weapons, hair, armor parts)
std::vector<Render3DDraw> BuildDrawCommands(float animation_time = -1.0f, const float* world_matrix = nullptr);
std::vector<Render3DDraw> BuildDrawCommands(float animation_time = -1.0f, const float* world_matrix = nullptr, bool forceCPU = false);
// Build draw commands for a skinned part using an external bone palette
std::vector<Render3DDraw> BuildDrawCommandsWithPalette(const std::vector<gr2::Mat4>& skin_matrices,
@@ -44,31 +52,17 @@ public:
bool GetLocalBounds(float& minX, float& minY, float& minZ, float& maxX, float& maxY, float& maxZ) const;
const gr2::File& GetFile() const { return *m_file; }
const gr2::FileInfo& GetInfo() const { return m_file->file_info(); }
const gr2::File& GetFile() const { return m_impl->GetFile(); }
const gr2::FileInfo& GetInfo() const { return m_impl->GetInfo(); }
std::shared_ptr<gr2_render::Gr2ModelInstance> GetImpl() const { return m_impl; }
private:
struct AttachedModel {
std::string bone_name;
std::shared_ptr<Gr2ModelInstance> model;
float offset[16];
bool animated = false;
bool scaled = false;
float scale[3] = {1.0f, 1.0f, 1.0f};
};
void ApplySkins(std::vector<Render3DDraw>& draws, size_t first) const;
std::unique_ptr<gr2::File> m_file;
std::string m_filepath;
std::unique_ptr<gr2::File> m_anim_file;
std::string m_anim_filepath;
std::vector<AttachedModel> m_attached_models;
std::shared_ptr<gr2_render::Gr2ModelInstance> m_impl;
std::vector<std::shared_ptr<Gr2ModelInstance>> m_attached_models;
std::vector<std::shared_ptr<Gr2ModelInstance>> m_attached_parts;
std::unordered_map<std::string, std::string> m_skins;
};
// 3D Scene and Model Renderer (modeled after 40250 pipeline architecture)
class Gr2SceneRenderer {
public:
Gr2SceneRenderer(int width = 1024, int height = 768);
@@ -95,13 +89,19 @@ public:
// Rasterize a list of draw commands (CPU software rasterizer)
void RenderScene(SDL_Surface* targetSurface, const std::vector<Render3DDraw>& draws);
// GPU Hardware-accelerated 3D Scene Rendering via SDL_RenderGeometry / Metal / Vulkan
// Native SDL_GPU depth/perspective pipeline; SDL geometry fallback for software renderers.
void RenderSceneGPU(SDL_Renderer* renderer, const std::vector<Render3DDraw>& draws);
void SetGPUDevice(SDL_GPUDevice* device);
void DestroyGPUTextures();
SDL_Texture* GetGPUTexture(SDL_Renderer* renderer, const std::string& texturePath);
// Exponential & Linear Depth Fog (modeled after 40250 CMapOutdoor fog)
void SetFog(bool enable, uint32_t color = 0xFFCCDDEE, float density = 0.0003f, float fog_near = 300.0f, float fog_far = 3000.0f, bool exp_mode = true);
void SetTextureBudgets(size_t cpuBytes,size_t gpuBytes);
size_t GetHardwareTextureBytes() const {return m_hardware ? m_hardware->GetTextureBytes() : 0;}
size_t GetHardwareTextureUploadCount() const {return m_hardware ? m_hardware->GetTextureUploadCount() : 0;}
size_t GetDecodedTextureBytes() const { return m_decodedTextureBytes; }
size_t GetTextureDecodeCount() const { return m_textureDecodeCount; }
bool IsFogEnabled() const { return m_fog_enabled; }
uint32_t GetFogColor() const { return m_fog_color; }
@@ -128,10 +128,12 @@ public:
private:
struct TextureData {
uint64_t lastUsed = 0;
int width = 0;
int height = 0;
std::vector<uint8_t> rgba;
};
void TrimTextureCaches(bool protectDecodedWorkingSet);
const TextureData* GetTexture(const std::string& texturePath);
struct GpuTransformedVertex {
@@ -154,7 +156,10 @@ private:
int m_logicalHeight = 0;
std::vector<float> m_depthBuffer;
std::unordered_map<std::string, TextureData> m_textureCache;
std::unordered_map<std::string, SDL_Texture*> m_gpuTextureCache;
struct SDLTextureEntry {SDL_Texture* texture=nullptr;size_t bytes=0;uint64_t lastUsed=0;};
std::unordered_map<std::string, SDLTextureEntry> m_gpuTextureCache;
uint64_t m_textureFrame=0;
size_t m_decodedTextureBytes=0,m_textureDecodeCount=0,m_decodedTextureBudget=64u*1024u*1024u,m_gpuTextureBudget=256u*1024u*1024u;
// Persistent scratch buffers to eliminate per-draw heap allocation churn in RenderSceneGPU
std::vector<DrawOrder> m_scratchSortedDraws;
@@ -162,6 +167,11 @@ private:
std::vector<SDL_Vertex> m_scratchSdlVertices;
std::vector<int> m_scratchSdlIndices;
std::unique_ptr<HardwareScene> m_hardware;
std::vector<HardwareVertex> m_hardwareVertices;
std::vector<uint32_t> m_hardwareIndices;
std::vector<HardwareBatch> m_hardwareBatches;
bool m_fog_enabled = false;
uint32_t m_fog_color = 0xFFCCDDEE;
float m_fog_density = 0.0003f;