2022-03-06 20:58:06 +00:00
|
|
|
#include "shader.hpp"
|
|
|
|
|
|
|
|
#include "../../gpu.hpp"
|
|
|
|
#include "../common.hpp"
|
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
#include <absl/container/flat_hash_map.h>
|
2022-03-06 20:58:06 +00:00
|
|
|
#include <aurora/model.hpp>
|
|
|
|
|
|
|
|
namespace aurora::gfx::model {
|
|
|
|
static logvisor::Module Log("aurora::gfx::model");
|
|
|
|
|
|
|
|
static const std::vector<zeus::CVector3f>* vtxData;
|
|
|
|
static const std::vector<zeus::CVector3f>* nrmData;
|
2022-03-08 07:44:46 +00:00
|
|
|
static const std::vector<Vec2<float>>* tex0TcData;
|
|
|
|
static const std::vector<Vec2<float>>* tcData;
|
2022-03-27 00:30:29 +00:00
|
|
|
static std::optional<Range> cachedVtxRange;
|
|
|
|
static std::optional<Range> cachedNrmRange;
|
|
|
|
static std::optional<Range> cachedPackedTcRange;
|
|
|
|
static std::optional<Range> cachedTcRange;
|
2022-03-06 20:58:06 +00:00
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
static inline void read_vert(ByteBuffer& out, const u8* data) noexcept {
|
2022-03-06 20:58:06 +00:00
|
|
|
size_t offset = 0;
|
2022-03-27 00:30:29 +00:00
|
|
|
for (const auto& type : gx::g_gxState.vtxDesc) {
|
|
|
|
if (type == GX::INDEX8) {
|
|
|
|
const auto v = static_cast<s16>(data[offset]); // expand to s16
|
|
|
|
out.append(&v, 2);
|
|
|
|
++offset;
|
|
|
|
} else if (type == GX::INDEX16) {
|
|
|
|
const s16 v = metaforce::SBig(*reinterpret_cast<const s16*>(data + offset));
|
|
|
|
out.append(&v, 2);
|
2022-03-06 20:58:06 +00:00
|
|
|
offset += 2;
|
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
}
|
|
|
|
constexpr size_t align = 4; // Sint16x2
|
|
|
|
if (offset % align != 0) {
|
|
|
|
out.append_zeroes(align - (offset % align));
|
|
|
|
}
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
static absl::flat_hash_map<XXH64_hash_t, std::pair<ByteBuffer, ByteBuffer>> sCachedDisplayLists;
|
2022-03-15 06:18:45 +00:00
|
|
|
|
2022-03-06 20:58:06 +00:00
|
|
|
void queue_surface(const u8* dlStart, u32 dlSize) noexcept {
|
2022-05-02 23:42:59 +00:00
|
|
|
const auto hash = xxh3_hash_s(dlStart, dlSize, 0);
|
2022-03-15 06:18:45 +00:00
|
|
|
Range vertRange, idxRange;
|
2022-03-27 00:30:29 +00:00
|
|
|
u32 numIndices = 0;
|
2022-03-15 06:18:45 +00:00
|
|
|
auto it = sCachedDisplayLists.find(hash);
|
|
|
|
if (it != sCachedDisplayLists.end()) {
|
|
|
|
const auto& [verts, indices] = it->second;
|
2022-03-27 00:30:29 +00:00
|
|
|
numIndices = indices.size() / 2;
|
|
|
|
vertRange = push_verts(verts.data(), verts.size());
|
|
|
|
idxRange = push_indices(indices.data(), indices.size());
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
2022-03-27 00:30:29 +00:00
|
|
|
ByteBuffer vtxBuf;
|
|
|
|
ByteBuffer idxBuf;
|
|
|
|
u8 inVtxSize = 0;
|
|
|
|
u8 outVtxSize = 0;
|
|
|
|
for (const auto& type : gx::g_gxState.vtxDesc) {
|
|
|
|
if (type == GX::NONE || type == GX::DIRECT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type == GX::INDEX8) {
|
|
|
|
++inVtxSize;
|
|
|
|
outVtxSize += 2;
|
|
|
|
} else if (type == GX::INDEX16) {
|
|
|
|
inVtxSize += 2;
|
|
|
|
outVtxSize += 2;
|
|
|
|
} else {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("unexpected vtx type {}"), type);
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outVtxSize = ALIGN(outVtxSize, 4);
|
2022-03-06 20:58:06 +00:00
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
u16 vtxStart = 0;
|
2022-03-15 06:18:45 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < dlSize - 6) {
|
|
|
|
const auto header = dlStart[offset];
|
|
|
|
const auto primitive = static_cast<GX::Primitive>(header & 0xF8);
|
2022-03-27 00:30:29 +00:00
|
|
|
const auto dlVtxCount = metaforce::SBig(*reinterpret_cast<const u16*>(dlStart + offset + 1));
|
2022-03-15 06:18:45 +00:00
|
|
|
offset += 3;
|
2022-03-06 20:58:06 +00:00
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
if (primitive == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (primitive != GX::TRIANGLES && primitive != GX::TRIANGLESTRIP && primitive != GX::TRIANGLEFAN) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("queue_surface: unsupported primitive type {}"), primitive);
|
|
|
|
unreachable();
|
|
|
|
}
|
2022-03-06 20:58:06 +00:00
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
vtxBuf.reserve_extra(dlVtxCount * outVtxSize);
|
|
|
|
if (dlVtxCount > 3 && (primitive == GX::TRIANGLEFAN || primitive == GX::TRIANGLESTRIP)) {
|
|
|
|
idxBuf.reserve_extra(((u32(dlVtxCount) - 3) * 3 + 3) * 2);
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
2022-03-27 00:30:29 +00:00
|
|
|
idxBuf.reserve_extra(dlVtxCount * 2);
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
u16 curVert = vtxStart;
|
|
|
|
for (u16 v = 0; v < dlVtxCount; ++v) {
|
|
|
|
read_vert(vtxBuf, dlStart + offset);
|
|
|
|
offset += inVtxSize;
|
2022-03-15 06:18:45 +00:00
|
|
|
if (primitive == GX::TRIANGLES || v < 3) {
|
2022-03-27 00:30:29 +00:00
|
|
|
idxBuf.append(&curVert, 2);
|
|
|
|
++numIndices;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else if (primitive == GX::TRIANGLEFAN) {
|
2022-03-27 00:30:29 +00:00
|
|
|
const std::array<u16, 3> idxs{
|
|
|
|
vtxStart,
|
|
|
|
u16(curVert - 1),
|
|
|
|
curVert,
|
|
|
|
};
|
|
|
|
idxBuf.append(idxs.data(), 6);
|
|
|
|
numIndices += 3;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else if (primitive == GX::TRIANGLESTRIP) {
|
|
|
|
if ((v & 1) == 0) {
|
2022-03-27 00:30:29 +00:00
|
|
|
const std::array<u16, 3> idxs{
|
|
|
|
u16(curVert - 2),
|
|
|
|
u16(curVert - 1),
|
|
|
|
curVert,
|
|
|
|
};
|
|
|
|
idxBuf.append(idxs.data(), 6);
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
2022-03-27 00:30:29 +00:00
|
|
|
const std::array<u16, 3> idxs{
|
|
|
|
u16(curVert - 1),
|
|
|
|
u16(curVert - 2),
|
|
|
|
curVert,
|
|
|
|
};
|
|
|
|
idxBuf.append(idxs.data(), 6);
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
numIndices += 3;
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
2022-03-15 06:18:45 +00:00
|
|
|
++curVert;
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
vtxStart += dlVtxCount;
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
2022-03-15 06:18:45 +00:00
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
vertRange = push_verts(vtxBuf.data(), vtxBuf.size());
|
|
|
|
idxRange = push_indices(idxBuf.data(), idxBuf.size());
|
|
|
|
sCachedDisplayLists.try_emplace(hash, std::move(vtxBuf), std::move(idxBuf));
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
Range sVtxRange, sNrmRange, sTcRange, sPackedTcRange;
|
2022-03-27 00:30:29 +00:00
|
|
|
if (cachedVtxRange) {
|
|
|
|
sVtxRange = *cachedVtxRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
|
|
|
sVtxRange = push_storage(reinterpret_cast<const uint8_t*>(vtxData->data()), vtxData->size() * 16);
|
2022-03-27 00:30:29 +00:00
|
|
|
cachedVtxRange = sVtxRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
if (cachedNrmRange) {
|
|
|
|
sNrmRange = *cachedNrmRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
|
|
|
sNrmRange = push_storage(reinterpret_cast<const uint8_t*>(nrmData->data()), nrmData->size() * 16);
|
2022-03-27 00:30:29 +00:00
|
|
|
cachedNrmRange = sNrmRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
if (cachedTcRange) {
|
|
|
|
sTcRange = *cachedTcRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else {
|
|
|
|
sTcRange = push_storage(reinterpret_cast<const uint8_t*>(tcData->data()), tcData->size() * 8);
|
2022-03-27 00:30:29 +00:00
|
|
|
cachedTcRange = sTcRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
2022-03-27 00:30:29 +00:00
|
|
|
if (cachedPackedTcRange) {
|
|
|
|
sPackedTcRange = *cachedPackedTcRange;
|
2022-03-15 06:18:45 +00:00
|
|
|
} else if (tcData == tex0TcData) {
|
2022-03-08 05:28:31 +00:00
|
|
|
sPackedTcRange = sTcRange;
|
|
|
|
} else {
|
2022-03-08 07:44:46 +00:00
|
|
|
sPackedTcRange = push_storage(reinterpret_cast<const uint8_t*>(tex0TcData->data()), tex0TcData->size() * 8);
|
2022-03-27 00:30:29 +00:00
|
|
|
cachedPackedTcRange = sPackedTcRange;
|
2022-03-08 05:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
model::PipelineConfig config{};
|
2022-05-02 23:42:59 +00:00
|
|
|
populate_pipeline_config(config, GX::TRIANGLES);
|
|
|
|
const auto info = gx::build_shader_info(config.shaderConfig);
|
2022-03-08 05:28:31 +00:00
|
|
|
const gx::BindGroupRanges ranges{
|
|
|
|
.vtxDataRange = sVtxRange,
|
|
|
|
.nrmDataRange = sNrmRange,
|
|
|
|
.tcDataRange = sTcRange,
|
|
|
|
.packedTcDataRange = sPackedTcRange,
|
|
|
|
};
|
2022-05-02 23:42:59 +00:00
|
|
|
const auto bindGroups = gx::build_bind_groups(info, config.shaderConfig, ranges);
|
2022-03-08 05:28:31 +00:00
|
|
|
const auto pipeline = pipeline_ref(config);
|
|
|
|
|
|
|
|
push_draw_command(model::DrawData{
|
|
|
|
.pipeline = pipeline,
|
|
|
|
.vertRange = vertRange,
|
|
|
|
.idxRange = idxRange,
|
2022-03-15 06:18:45 +00:00
|
|
|
.dataRanges = ranges,
|
2022-03-08 05:28:31 +00:00
|
|
|
.uniformRange = build_uniform(info),
|
2022-03-15 06:18:45 +00:00
|
|
|
.indexCount = numIndices,
|
2022-05-02 23:42:59 +00:00
|
|
|
.bindGroups = bindGroups,
|
2022-03-20 20:24:02 +00:00
|
|
|
.dstAlpha = gx::g_gxState.dstAlpha,
|
2022-03-08 05:28:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
State construct_state() { return {}; }
|
|
|
|
|
|
|
|
wgpu::RenderPipeline create_pipeline(const State& state, [[maybe_unused]] PipelineConfig config) {
|
2022-05-02 23:42:59 +00:00
|
|
|
const auto info = build_shader_info(config.shaderConfig); // TODO remove
|
|
|
|
const auto shader = build_shader(config.shaderConfig, info);
|
2022-03-08 05:28:31 +00:00
|
|
|
|
2022-05-02 23:42:59 +00:00
|
|
|
std::array<wgpu::VertexAttribute, gx::MaxVtxAttr> vtxAttrs{};
|
2022-03-27 00:30:29 +00:00
|
|
|
auto [num4xAttr, rem] = std::div(config.shaderConfig.indexedAttributeCount, 4);
|
|
|
|
u32 num2xAttr = 0;
|
|
|
|
if (rem > 2) {
|
|
|
|
++num4xAttr;
|
|
|
|
} else if (rem > 0) {
|
|
|
|
++num2xAttr;
|
|
|
|
}
|
|
|
|
u32 offset = 0;
|
|
|
|
for (u32 i = 0; i < num4xAttr; ++i) {
|
|
|
|
vtxAttrs[i] = {
|
|
|
|
.format = wgpu::VertexFormat::Sint16x4,
|
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = i,
|
|
|
|
};
|
|
|
|
offset += 8;
|
|
|
|
}
|
|
|
|
for (u32 i = 0; i < num2xAttr; ++i) {
|
|
|
|
const u32 idx = num4xAttr + i;
|
|
|
|
vtxAttrs[idx] = {
|
|
|
|
.format = wgpu::VertexFormat::Sint16x2,
|
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = idx,
|
|
|
|
};
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
const std::array vtxBuffers{wgpu::VertexBufferLayout{
|
|
|
|
.arrayStride = offset,
|
|
|
|
.stepMode = wgpu::VertexStepMode::Vertex,
|
|
|
|
.attributeCount = num4xAttr + num2xAttr,
|
|
|
|
.attributes = vtxAttrs.data(),
|
|
|
|
}};
|
2022-03-08 05:28:31 +00:00
|
|
|
|
2022-03-27 00:30:29 +00:00
|
|
|
return build_pipeline(config, info, vtxBuffers, shader, "Model Pipeline");
|
2022-03-08 05:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void render(const State& state, const DrawData& data, const wgpu::RenderPassEncoder& pass) {
|
|
|
|
if (!bind_pipeline(data.pipeline, pass)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::array offsets{
|
2022-03-15 06:18:45 +00:00
|
|
|
data.uniformRange.offset,
|
|
|
|
storage_offset(data.dataRanges.vtxDataRange),
|
|
|
|
storage_offset(data.dataRanges.nrmDataRange),
|
|
|
|
storage_offset(data.dataRanges.packedTcDataRange),
|
2022-03-27 00:30:29 +00:00
|
|
|
storage_offset(data.dataRanges.tcDataRange),
|
2022-03-08 05:28:31 +00:00
|
|
|
};
|
|
|
|
pass.SetBindGroup(0, find_bind_group(data.bindGroups.uniformBindGroup), offsets.size(), offsets.data());
|
|
|
|
if (data.bindGroups.samplerBindGroup && data.bindGroups.textureBindGroup) {
|
|
|
|
pass.SetBindGroup(1, find_bind_group(data.bindGroups.samplerBindGroup));
|
|
|
|
pass.SetBindGroup(2, find_bind_group(data.bindGroups.textureBindGroup));
|
|
|
|
}
|
2022-03-15 06:18:45 +00:00
|
|
|
pass.SetVertexBuffer(0, g_vertexBuffer, data.vertRange.offset, data.vertRange.size);
|
2022-03-27 00:30:29 +00:00
|
|
|
pass.SetIndexBuffer(g_indexBuffer, wgpu::IndexFormat::Uint16, data.idxRange.offset, data.idxRange.size);
|
2022-05-02 23:42:59 +00:00
|
|
|
if (data.dstAlpha != UINT32_MAX) {
|
|
|
|
const wgpu::Color color{0.f, 0.f, 0.f, data.dstAlpha / 255.f};
|
2022-03-20 20:24:02 +00:00
|
|
|
pass.SetBlendConstant(&color);
|
|
|
|
}
|
2022-03-08 05:28:31 +00:00
|
|
|
pass.DrawIndexed(data.indexCount);
|
2022-03-06 20:58:06 +00:00
|
|
|
}
|
|
|
|
} // namespace aurora::gfx::model
|
2022-03-12 15:47:20 +00:00
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
static absl::flat_hash_map<XXH64_hash_t, aurora::gfx::Range> sCachedRanges;
|
|
|
|
template <typename Vec>
|
|
|
|
static inline void cache_array(const void* data, Vec*& outPtr, std::optional<aurora::gfx::Range>& outRange, u8 stride) {
|
|
|
|
Vec* vecPtr = static_cast<Vec*>(data);
|
|
|
|
outPtr = vecPtr;
|
2022-03-27 00:30:29 +00:00
|
|
|
outRange.reset();
|
2022-03-15 06:18:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 15:47:20 +00:00
|
|
|
void GXSetArray(GX::Attr attr, const void* data, u8 stride) noexcept {
|
2022-03-15 06:18:45 +00:00
|
|
|
using namespace aurora::gfx::model;
|
2022-03-12 15:47:20 +00:00
|
|
|
switch (attr) {
|
|
|
|
case GX::VA_POS:
|
2022-03-27 00:30:29 +00:00
|
|
|
cache_array(data, vtxData, cachedVtxRange, stride);
|
2022-03-12 15:47:20 +00:00
|
|
|
break;
|
|
|
|
case GX::VA_NRM:
|
2022-03-27 00:30:29 +00:00
|
|
|
cache_array(data, nrmData, cachedNrmRange, stride);
|
2022-03-12 15:47:20 +00:00
|
|
|
break;
|
|
|
|
case GX::VA_TEX0:
|
2022-03-27 00:30:29 +00:00
|
|
|
cache_array(data, tex0TcData, cachedPackedTcRange, stride);
|
2022-03-12 15:47:20 +00:00
|
|
|
break;
|
|
|
|
case GX::VA_TEX1:
|
2022-03-27 00:30:29 +00:00
|
|
|
cache_array(data, tcData, cachedTcRange, stride);
|
2022-03-12 15:47:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-03-15 06:18:45 +00:00
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("GXSetArray: invalid attr {}"), attr);
|
2022-03-12 15:47:20 +00:00
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GXCallDisplayList(const void* data, u32 nbytes) noexcept {
|
|
|
|
aurora::gfx::model::queue_surface(static_cast<const u8*>(data), nbytes);
|
|
|
|
}
|