Files
AVA2/src/eter_render/HardwareScene.h
T
shenleiandClaude Opus 5.5 aaee4c8f70 render: pack HardwareVertex from 88 to 48 bytes
Colour as UBYTE4_NORM, normals as SHORT4_NORM (unit direction, the
shader renormalises), bones as UBYTE4 and weights as USHORT4_NORM.
Shader inputs keep their float/uint types, so the shaders are unchanged.

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

89 lines
3.8 KiB
C++

#pragma once
#include <SDL3/SDL.h>
#include <vector>
#include <unordered_map>
#include <cstdint>
#include <string>
#include <memory>
#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]; // 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]{};
float lightDirection[4]{}, lightAmbient[4]{}, lightDiffuse[4]{};
float fogColor[4]{}, fogParams[4]{};
float modes[4]{}; // raw vertices, lighting, fog type, disable depth
float skin[4]{}; // palette count
};
static_assert(sizeof(HardwareTransform)==288);
HardwareVertex MakeHardwareVertex(const Render3DDraw& mesh, size_t index);
struct HardwareBatch {
uint32_t firstIndex, indexCount;
SDL_GPUTexture* texture;
uint32_t cull;
bool depth, depthWrite, blend, additive;
float alphaRef;
HardwareTransform transform{};
std::shared_ptr<const Render3DDraw> geometry;
std::shared_ptr<const std::vector<float>> palette;
};
class HardwareScene {
public:
explicit HardwareScene(SDL_GPUDevice* device): device_(device) {}
~HardwareScene();
SDL_GPUTexture* FindTexture(const std::string& key);
void SetTextureBudget(size_t bytes) { textureBudget_=bytes; }
size_t GetTextureBytes() const { return textureBytes_; }
size_t GetTextureUploadCount() const { return textureUploads_; }
SDL_GPUTexture* Texture(const std::string& key, int width, int height, const void* rgba);
bool Render(SDL_Renderer* renderer, int width, int height,
const std::vector<HardwareVertex>& vertices,
const std::vector<uint32_t>& indices,
const std::vector<HardwareBatch>& batches);
size_t GetResidentUploadCount() const { return residentUploads_; }
size_t GetLastDrawCallCount() const { return lastDrawCalls_; }
size_t GetLastDynamicUploadBytes() const { return lastDynamicBytes_; }
private:
struct ResidentMesh {
std::shared_ptr<const Render3DDraw> source;
SDL_GPUBuffer *vertices=nullptr, *indices=nullptr;
uint32_t count=0, bytes=0;
uint64_t lastUsed=0;
};
ResidentMesh* Resident(const std::shared_ptr<const Render3DDraw>& geometry);
std::unordered_map<const Render3DDraw*,ResidentMesh> resident_;
uint64_t frame_=0;
size_t residentBytes_=0, residentUploads_=0, lastDrawCalls_=0, lastDynamicBytes_=0;
SDL_GPUBuffer* instances_=nullptr;
SDL_GPUTransferBuffer* instanceTransfer_=nullptr;
uint32_t instanceBytes_=0;
bool Initialize(SDL_Renderer* renderer, int width, int height);
SDL_GPUGraphicsPipeline* Pipeline(const HardwareBatch& batch);
SDL_GPUDevice* device_;
SDL_GPUShader *vertex_=nullptr, *fragment_=nullptr;
SDL_GPUSampler* sampler_=nullptr;
SDL_GPUTexture *color_=nullptr, *depth_=nullptr;
SDL_Texture* output_=nullptr;
SDL_GPUTexture* white_=nullptr;
struct TextureEntry {SDL_GPUTexture* texture;size_t bytes;uint64_t lastUsed;};
std::unordered_map<std::string,TextureEntry> textures_;
size_t textureBytes_=0,textureUploads_=0,textureBudget_=256u*1024u*1024u;
SDL_GPUBuffer *vertices_=nullptr, *indices_=nullptr;
SDL_GPUTransferBuffer* transfer_=nullptr;
uint32_t vertexBytes_=0, indexBytes_=0;
int width_=0,height_=0;
std::unordered_map<unsigned,SDL_GPUGraphicsPipeline*> pipelines_;
};
}