2022-03-05 03:36:54 +00:00
|
|
|
#include "stream/shader.hpp"
|
|
|
|
|
|
|
|
#include "../gpu.hpp"
|
2022-03-04 07:47:21 +00:00
|
|
|
#include "common.hpp"
|
2022-03-06 07:46:42 +00:00
|
|
|
#include "gx.hpp"
|
2022-03-04 07:47:21 +00:00
|
|
|
|
|
|
|
namespace aurora::gfx {
|
2022-03-05 03:36:54 +00:00
|
|
|
static logvisor::Module Log("aurora::gfx::stream");
|
|
|
|
|
|
|
|
struct SStreamState {
|
|
|
|
GX::Primitive primitive;
|
|
|
|
metaforce::EStreamFlags flags;
|
|
|
|
uint32_t vertexCount = 0;
|
|
|
|
ByteBuffer vertexBuffer;
|
|
|
|
|
|
|
|
explicit SStreamState(GX::Primitive primitive) noexcept : primitive(primitive) {}
|
|
|
|
};
|
|
|
|
static std::optional<SStreamState> sStreamState;
|
|
|
|
|
|
|
|
void stream_begin(GX::Primitive primitive) noexcept {
|
|
|
|
if (sStreamState) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Stream began twice!"));
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
sStreamState.emplace(primitive);
|
|
|
|
}
|
|
|
|
|
2022-03-04 07:47:21 +00:00
|
|
|
void stream_vertex(metaforce::EStreamFlags flags, const zeus::CVector3f& pos, const zeus::CVector3f& nrm,
|
2022-03-05 03:36:54 +00:00
|
|
|
const zeus::CColor& color, const zeus::CVector2f& uv) noexcept {
|
|
|
|
if (!sStreamState) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Stream not started!"));
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
if (sStreamState->flags) {
|
|
|
|
if (sStreamState->flags != flags) {
|
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Stream changed flags?"));
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sStreamState->flags = flags;
|
|
|
|
}
|
|
|
|
sStreamState->vertexBuffer.append(&pos, 12);
|
|
|
|
if (flags & metaforce::EStreamFlagBits::fHasNormal) {
|
|
|
|
sStreamState->vertexBuffer.append(&nrm, 12);
|
|
|
|
}
|
|
|
|
if (flags & metaforce::EStreamFlagBits::fHasColor) {
|
|
|
|
sStreamState->vertexBuffer.append(&color, 16);
|
|
|
|
}
|
|
|
|
if (flags & metaforce::EStreamFlagBits::fHasTexture) {
|
|
|
|
sStreamState->vertexBuffer.append(&uv, 8);
|
|
|
|
}
|
|
|
|
sStreamState->vertexCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stream_end() noexcept {
|
2022-03-06 20:58:06 +00:00
|
|
|
const auto& tex = get_texture(GX::TEXMAP0);
|
|
|
|
if (sStreamState->flags & metaforce::EStreamFlagBits::fHasTexture && !tex) {
|
2022-03-05 03:36:54 +00:00
|
|
|
Log.report(logvisor::Fatal, FMT_STRING("Stream has texture but no texture bound!"));
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
const auto vertRange = push_verts(sStreamState->vertexBuffer.data(), sStreamState->vertexBuffer.size());
|
|
|
|
|
|
|
|
ByteBuffer uniBuf;
|
|
|
|
std::bitset<g_colorRegs.size()> usedColors;
|
|
|
|
{
|
|
|
|
const auto xf = get_combined_matrix();
|
|
|
|
uniBuf.append(&xf, 64);
|
|
|
|
}
|
|
|
|
if (sStreamState->flags & metaforce::EStreamFlagBits::fHasTexture) {
|
2022-03-06 20:58:06 +00:00
|
|
|
uniBuf.append(&tex.lod, 4);
|
2022-03-05 03:36:54 +00:00
|
|
|
}
|
|
|
|
const auto uniRange = push_uniform(uniBuf.data(), uniBuf.size());
|
|
|
|
|
|
|
|
const auto uniform_size = align_uniform(uniBuf.size());
|
2022-03-06 20:58:06 +00:00
|
|
|
|
|
|
|
stream::PipelineConfig config{
|
2022-03-05 03:36:54 +00:00
|
|
|
.uniformSize = uniform_size,
|
|
|
|
.flags = sStreamState->flags,
|
2022-03-06 20:58:06 +00:00
|
|
|
};
|
|
|
|
config.shaderConfig.denormalizedVertexAttributes = true;
|
|
|
|
config.shaderConfig.denormalizedNorm = sStreamState->flags.IsSet(metaforce::EStreamFlagBits::fHasNormal);
|
|
|
|
config.shaderConfig.denormalizedColor = sStreamState->flags.IsSet(metaforce::EStreamFlagBits::fHasColor);
|
|
|
|
populate_gx_pipeline_config(config, sStreamState->primitive, {1});
|
|
|
|
const auto pipeline = pipeline_ref(config);
|
2022-03-05 03:36:54 +00:00
|
|
|
|
|
|
|
BindGroupRef samplerBindGroup{};
|
|
|
|
BindGroupRef textureBindGroup{};
|
|
|
|
if (sStreamState->flags & metaforce::EStreamFlagBits::fHasTexture) {
|
|
|
|
const auto& state = get_state<stream::State>();
|
|
|
|
{
|
|
|
|
const std::array samplerEntries{wgpu::BindGroupEntry{
|
|
|
|
.binding = 0,
|
2022-03-06 20:58:06 +00:00
|
|
|
.sampler = sampler_ref(tex.get_descriptor()),
|
2022-03-05 03:36:54 +00:00
|
|
|
}};
|
|
|
|
samplerBindGroup = bind_group_ref(wgpu::BindGroupDescriptor{
|
|
|
|
.label = "Stream Sampler Bind Group",
|
|
|
|
.layout = state.samplerLayout,
|
|
|
|
.entryCount = samplerEntries.size(),
|
|
|
|
.entries = samplerEntries.data(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::array textureEntries{wgpu::BindGroupEntry{
|
|
|
|
.binding = 0,
|
2022-03-06 20:58:06 +00:00
|
|
|
.textureView = tex.handle.ref->view,
|
2022-03-05 03:36:54 +00:00
|
|
|
}};
|
|
|
|
textureBindGroup = bind_group_ref(wgpu::BindGroupDescriptor{
|
|
|
|
.label = "Stream Texture Bind Group",
|
|
|
|
.layout = state.textureLayout,
|
|
|
|
.entryCount = textureEntries.size(),
|
|
|
|
.entries = textureEntries.data(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
push_draw_command(stream::DrawData{
|
|
|
|
.pipeline = pipeline,
|
|
|
|
.vertRange = vertRange,
|
|
|
|
.uniformRange = uniRange,
|
|
|
|
.vertexCount = sStreamState->vertexCount,
|
|
|
|
.uniformSize = uniform_size,
|
|
|
|
.samplerBindGroup = samplerBindGroup,
|
|
|
|
.textureBindGroup = textureBindGroup,
|
|
|
|
});
|
|
|
|
|
|
|
|
sStreamState.reset();
|
|
|
|
}
|
2022-03-04 07:47:21 +00:00
|
|
|
} // namespace aurora::gfx
|