2022-03-05 03:36:54 +00:00
|
|
|
#include "shader.hpp"
|
|
|
|
|
|
|
|
#include "../../gpu.hpp"
|
|
|
|
#include "../common.hpp"
|
|
|
|
|
|
|
|
#include <magic_enum.hpp>
|
2022-03-06 20:58:06 +00:00
|
|
|
#include <utility>
|
2022-03-05 03:36:54 +00:00
|
|
|
|
|
|
|
namespace aurora::gfx::stream {
|
|
|
|
static logvisor::Module Log("aurora::gfx::stream");
|
|
|
|
|
|
|
|
using gpu::g_device;
|
|
|
|
|
|
|
|
wgpu::RenderPipeline create_pipeline(const State& state, [[maybe_unused]] PipelineConfig config) {
|
2022-03-07 23:53:42 +00:00
|
|
|
const auto [shader, info] = build_shader(config.shaderConfig);
|
|
|
|
|
2022-03-05 03:36:54 +00:00
|
|
|
std::array<wgpu::VertexAttribute, 4> attributes{};
|
|
|
|
attributes[0] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x3,
|
|
|
|
.offset = 0,
|
|
|
|
.shaderLocation = 0,
|
|
|
|
};
|
|
|
|
uint64_t offset = 12;
|
|
|
|
uint32_t shaderLocation = 1;
|
2022-03-19 17:30:25 +00:00
|
|
|
if (config.shaderConfig.vtxAttrs[GX::VA_NRM] == GX::DIRECT) {
|
2022-03-05 03:36:54 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x3,
|
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 12;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
2022-03-19 17:30:25 +00:00
|
|
|
if (config.shaderConfig.vtxAttrs[GX::VA_CLR0] == GX::DIRECT) {
|
2022-03-05 03:36:54 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x4,
|
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 16;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
2022-03-19 17:30:25 +00:00
|
|
|
for (int i = GX::VA_TEX0; i < GX::VA_TEX7; ++i) {
|
|
|
|
if (config.shaderConfig.vtxAttrs[i] != GX::DIRECT) {
|
2022-03-07 23:53:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-05 03:36:54 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x2,
|
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 8;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
|
|
|
const std::array vertexBuffers{wgpu::VertexBufferLayout{
|
|
|
|
.arrayStride = offset,
|
|
|
|
.attributeCount = shaderLocation,
|
|
|
|
.attributes = attributes.data(),
|
|
|
|
}};
|
|
|
|
|
2022-03-07 23:53:42 +00:00
|
|
|
return build_pipeline(config, info, vertexBuffers, shader, "Stream Pipeline");
|
2022-03-05 03:36:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
State construct_state() { return {}; }
|
2022-03-05 03:36:54 +00:00
|
|
|
|
|
|
|
void render(const State& state, const DrawData& data, const wgpu::RenderPassEncoder& pass) {
|
|
|
|
if (!bind_pipeline(data.pipeline, pass)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-15 06:18:45 +00:00
|
|
|
const std::array offsets{data.uniformRange.offset};
|
2022-03-07 23:53:42 +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-05 03:36:54 +00:00
|
|
|
}
|
2022-03-15 06:18:45 +00:00
|
|
|
pass.SetVertexBuffer(0, g_vertexBuffer, data.vertRange.offset, data.vertRange.size);
|
2022-03-19 17:30:25 +00:00
|
|
|
pass.SetIndexBuffer(g_indexBuffer, wgpu::IndexFormat::Uint16, data.indexRange.offset, data.indexRange.size);
|
|
|
|
pass.DrawIndexed(data.indexCount);
|
2022-03-05 03:36:54 +00:00
|
|
|
}
|
|
|
|
} // namespace aurora::gfx::stream
|