2022-07-27 15:25:25 +00:00
|
|
|
#include "shader.hpp"
|
|
|
|
|
|
|
|
#include "../../webgpu/gpu.hpp"
|
|
|
|
|
|
|
|
namespace aurora::gfx::stream {
|
|
|
|
static Module Log("aurora::gfx::stream");
|
|
|
|
|
|
|
|
using webgpu::g_device;
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
wgpu::RenderPipeline create_pipeline(const State& state, [[maybe_unused]] const PipelineConfig& config) {
|
2022-07-27 15:25:25 +00:00
|
|
|
const auto info = build_shader_info(config.shaderConfig); // TODO remove
|
|
|
|
const auto shader = build_shader(config.shaderConfig, info);
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
std::array<wgpu::VertexAttribute, 4> attributes{};
|
|
|
|
attributes[0] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x3,
|
2022-07-27 15:25:25 +00:00
|
|
|
.offset = 0,
|
|
|
|
.shaderLocation = 0,
|
|
|
|
};
|
|
|
|
uint64_t offset = 12;
|
|
|
|
uint32_t shaderLocation = 1;
|
|
|
|
if (config.shaderConfig.vtxAttrs[GX_VA_NRM] == GX_DIRECT) {
|
2022-08-02 20:37:56 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x3,
|
2022-07-27 15:25:25 +00:00
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 12;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
|
|
|
if (config.shaderConfig.vtxAttrs[GX_VA_CLR0] == GX_DIRECT) {
|
2022-08-02 20:37:56 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x4,
|
2022-07-27 15:25:25 +00:00
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 16;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
|
|
|
for (int i = GX_VA_TEX0; i < GX_VA_TEX7; ++i) {
|
|
|
|
if (config.shaderConfig.vtxAttrs[i] != GX_DIRECT) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
attributes[shaderLocation] = wgpu::VertexAttribute{
|
|
|
|
.format = wgpu::VertexFormat::Float32x2,
|
2022-07-27 15:25:25 +00:00
|
|
|
.offset = offset,
|
|
|
|
.shaderLocation = shaderLocation,
|
|
|
|
};
|
|
|
|
offset += 8;
|
|
|
|
shaderLocation++;
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
const std::array vertexBuffers{wgpu::VertexBufferLayout{
|
2022-07-27 15:25:25 +00:00
|
|
|
.arrayStride = offset,
|
|
|
|
.attributeCount = shaderLocation,
|
|
|
|
.attributes = attributes.data(),
|
|
|
|
}};
|
|
|
|
|
|
|
|
return build_pipeline(config, info, vertexBuffers, shader, "Stream Pipeline");
|
|
|
|
}
|
|
|
|
|
|
|
|
State construct_state() { return {}; }
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
void render(const State& state, const DrawData& data, const wgpu::RenderPassEncoder& pass) {
|
2022-07-27 15:25:25 +00:00
|
|
|
if (!bind_pipeline(data.pipeline, pass)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::array offsets{data.uniformRange.offset};
|
2022-08-02 20:37:56 +00:00
|
|
|
pass.SetBindGroup(0, find_bind_group(data.bindGroups.uniformBindGroup), offsets.size(), offsets.data());
|
2022-07-27 15:25:25 +00:00
|
|
|
if (data.bindGroups.samplerBindGroup && data.bindGroups.textureBindGroup) {
|
2022-08-02 20:37:56 +00:00
|
|
|
pass.SetBindGroup(1, find_bind_group(data.bindGroups.samplerBindGroup));
|
|
|
|
pass.SetBindGroup(2, find_bind_group(data.bindGroups.textureBindGroup));
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
pass.SetVertexBuffer(0, g_vertexBuffer, data.vertRange.offset, data.vertRange.size);
|
|
|
|
pass.SetIndexBuffer(g_indexBuffer, wgpu::IndexFormat::Uint16, data.indexRange.offset, data.indexRange.size);
|
2022-07-27 15:25:25 +00:00
|
|
|
if (data.dstAlpha != UINT32_MAX) {
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Color color{0.f, 0.f, 0.f, data.dstAlpha / 255.f};
|
|
|
|
pass.SetBlendConstant(&color);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
pass.DrawIndexed(data.indexCount);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
} // namespace aurora::gfx::stream
|