Files
AVA2/src/eter_render/Gr2ModelRenderer.h
T
shenleiandClaude Opus 5.5 df87bc64eb Drop the extra full-screen copy at render scale < 1
The SDL_GPU pipeline renders into its own view-sized colour target and
already composites it over the whole render target, so routing it through
gpu_scene_texture cost one extra same-size copy per frame. The scaled
intermediate is now used only by the SDL geometry fallback; the hardware
composite upscales with linear filtering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:19:51 +09:00

192 lines
9.2 KiB
C++

#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>
#include <vector>
#include <memory>
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);
float GetAnimationDuration() const;
// Granny loop window: looping motions pass (true, true) so the seam samples across loops.
void SetAnimationLoop(bool underflow_loop, bool overflow_loop);
// Bone queries & attachments
bool GetBoneWorldTransform(const std::string& bone_name, float* out_matrix, float animation_time = -1.0f) const;
void AttachModel(const std::string& bone_name, std::shared_ptr<Gr2ModelInstance> attachment, const float* offset_matrix = nullptr);
void AttachPart(std::shared_ptr<Gr2ModelInstance> part);
void DetachAll();
void DetachModel(const std::string& bone_name);
void DetachPart(const std::shared_ptr<Gr2ModelInstance>& part);
// CActorInstance::AttachSash: the attached instance plays its own motion 0 on the parent's clock.
void SetAttachmentAnimated(const std::string& bone_name, bool animated);
// ENABLE_OBJ_SCALLING: the attachment is scaled about the parent origin in Granny (Z-up) space.
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();
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, 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,
const float* world_matrix = nullptr,
const gr2::Skeleton* parent_skeleton = nullptr);
bool GetLocalBounds(float& minX, float& minY, float& minZ, float& maxX, float& maxY, float& maxZ) const;
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:
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;
};
class Gr2SceneRenderer {
public:
Gr2SceneRenderer(int width = 1024, int height = 768);
~Gr2SceneRenderer();
void SetCameraLookAt(float eyeX, float eyeY, float eyeZ,
float targetX, float targetY, float targetZ,
float upX, float upY, float upZ);
// D3DXMatrixLookAtRH as used by the original CCamera::SetViewMatrix, expressed for this
// renderer's LH projection: LookAtLH with the x axis negated (RH view x RH proj == this view x LH proj).
void SetCameraLookAtRH(float eyeX, float eyeY, float eyeZ,
float targetX, float targetY, float targetZ,
float upX, float upY, float upZ);
void SetPerspective(float fovYDegrees, float aspect, float zNear, float zFar);
void SetViewport(int x, int y, int width, int height);
void Clear(uint32_t color = 0xFF101520, float depth = 1.0f);
// Rasterize a 3D draw command into the surface using depth buffering and lighting
void RenderDraw(SDL_Surface* targetSurface, const Render3DDraw& draw);
// Rasterize a list of draw commands (CPU software rasterizer)
void RenderScene(SDL_Surface* targetSurface, const std::vector<Render3DDraw>& draws);
void RenderScene(SDL_Surface* targetSurface, const std::vector<const Render3DDraw*>& draws);
// Native SDL_GPU depth/perspective pipeline; SDL geometry fallback for software renderers.
void RenderSceneGPU(SDL_Renderer* renderer, const std::vector<Render3DDraw>& draws);
void RenderSceneGPU(SDL_Renderer* renderer, const std::vector<const Render3DDraw*>& draws);
void SetGPUDevice(SDL_GPUDevice* device);
// The SDL_GPU pipeline composites its own (smaller) colour target to the full render target.
bool HasHardwarePipeline() const { return m_hardware != nullptr; }
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; }
void SetLogicalSize(int width, int height) { m_logicalWidth = width; m_logicalHeight = height; }
int GetLogicalWidth() const { return m_logicalWidth > 0 ? m_logicalWidth : m_viewport[2]; }
int GetLogicalHeight() const { return m_logicalHeight > 0 ? m_logicalHeight : m_viewport[3]; }
const float* GetViewMatrix() const { return m_view; }
const float* GetProjMatrix() const { return m_proj; }
const int* GetViewport() const { return m_viewport; }
void GetEyePosition(float& x, float& y, float& z) const { x = m_eye[0]; y = m_eye[1]; z = m_eye[2]; }
// Project world coordinates (X, Y, Z) to 2D screen coordinates
bool ProjectToScreen(float worldX, float worldY, float worldZ, float& screenX, float& screenY) const;
// Unproject 2D screen coordinates (screenX, screenY) to a 3D ray in world space
bool ScreenToWorldRay(float screenX, float screenY, float rayOrig[3], float rayDir[3]) const;
// Pick 3D point on terrain heightmap via ray marching and binary search refinement
bool PickTerrain(float screenX, float screenY, const class TerrainHeightmap& terrain,
float& pickX, float& pickY, float& pickZ, float maxRange = 5000.0f) const;
const std::vector<float>& GetDepthBuffer() const { return m_depthBuffer; }
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 {
float x_clip, y_clip, z_clip, w_clip;
float x_screen, y_screen;
float u, v;
float r, g, b, a;
};
struct DrawOrder {
int priority = 1;
float depth = 0.0f;
const Render3DDraw* draw = nullptr;
SDL_GPUTexture* texture = nullptr; // resolved once per draw; also the opaque sort key
};
float m_eye[3] = {0.0f, 0.0f, 0.0f};
float m_view[16];
float m_proj[16];
int m_viewport[4];
int m_logicalWidth = 0;
int m_logicalHeight = 0;
std::vector<float> m_depthBuffer;
std::unordered_map<std::string, TextureData> m_textureCache;
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;
std::vector<const Render3DDraw*> m_scratchDrawPtrs;
std::vector<GpuTransformedVertex> m_scratchGpuXverts;
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;
float m_fog_near = 300.0f;
float m_fog_far = 3000.0f;
bool m_fog_exp = true;
};
} // namespace ava2