summaryrefslogtreecommitdiff
path: root/cpp-timeplot/src/renderer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp-timeplot/src/renderer.cpp')
-rw-r--r--cpp-timeplot/src/renderer.cpp185
1 files changed, 127 insertions, 58 deletions
diff --git a/cpp-timeplot/src/renderer.cpp b/cpp-timeplot/src/renderer.cpp
index 1c61698..b65ed46 100644
--- a/cpp-timeplot/src/renderer.cpp
+++ b/cpp-timeplot/src/renderer.cpp
@@ -4,11 +4,14 @@
#include <sstream>
#include <iostream>
-Renderer::Renderer(wgpu::Device device, wgpu::Surface surface, int width, int height)
+Renderer::Renderer(WGPUDevice device, WGPUSurface surface, int width, int height)
: device_(device), surface_(surface), width_(width), height_(height), time_(0.0f),
- surfaceFormat_(wgpu::TextureFormat::BGRA8Unorm) {}
+ surfaceFormat_(WGPUTextureFormat_BGRA8Unorm), linePipeline_(nullptr), lineListPipeline_(nullptr) {}
-Renderer::~Renderer() = default;
+Renderer::~Renderer() {
+ if (linePipeline_) wgpuRenderPipelineRelease(linePipeline_);
+ if (lineListPipeline_) wgpuRenderPipelineRelease(lineListPipeline_);
+}
bool Renderer::initialize() {
configureSurface();
@@ -30,16 +33,19 @@ bool Renderer::initialize() {
}
void Renderer::configureSurface() {
- wgpu::SurfaceConfiguration config{};
+ WGPUSurfaceConfiguration config = {};
+ config.nextInChain = nullptr;
config.device = device_;
config.format = surfaceFormat_;
- config.usage = wgpu::TextureUsage::RenderAttachment;
+ config.usage = WGPUTextureUsage_RenderAttachment;
config.width = width_;
config.height = height_;
- config.presentMode = wgpu::PresentMode::Fifo;
- config.alphaMode = wgpu::CompositeAlphaMode::Auto;
+ config.presentMode = WGPUPresentMode_Fifo;
+ config.alphaMode = WGPUCompositeAlphaMode_Auto;
+ config.viewFormatCount = 0;
+ config.viewFormats = nullptr;
- surface_.configure(config);
+ wgpuSurfaceConfigure(surface_, &config);
}
void Renderer::createPipelines() {
@@ -49,74 +55,103 @@ void Renderer::createPipelines() {
buffer << shaderFile.rdbuf();
std::string shaderCode = buffer.str();
- wgpu::ShaderSourceWGSL wgslSource{};
- wgslSource.chain.sType = wgpu::SType::ShaderSourceWGSL;
- wgslSource.code.data = shaderCode.c_str();
- wgslSource.code.length = shaderCode.length();
+ WGPUShaderSourceWGSL wgslSource = {};
+ wgslSource.chain.sType = WGPUSType_ShaderSourceWGSL;
+ wgslSource.chain.next = nullptr;
+ wgslSource.code = {shaderCode.c_str(), shaderCode.length()};
- wgpu::ShaderModuleDescriptor shaderDesc{};
+ WGPUShaderModuleDescriptor shaderDesc = {};
shaderDesc.nextInChain = &wgslSource.chain;
- wgpu::ShaderModule shader = device_.createShaderModule(shaderDesc);
+ shaderDesc.label = {nullptr, WGPU_STRLEN};
+
+ WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device_, &shaderDesc);
// Vertex buffer layout
- wgpu::VertexAttribute attributes[2];
- attributes[0].format = wgpu::VertexFormat::Float32x2;
+ WGPUVertexAttribute attributes[2] = {};
+ attributes[0].format = WGPUVertexFormat_Float32x2;
attributes[0].offset = 0;
attributes[0].shaderLocation = 0;
- attributes[1].format = wgpu::VertexFormat::Float32x3;
+ attributes[1].format = WGPUVertexFormat_Float32x3;
attributes[1].offset = 2 * sizeof(float);
attributes[1].shaderLocation = 1;
- wgpu::VertexBufferLayout vertexBufferLayout{};
+ WGPUVertexBufferLayout vertexBufferLayout = {};
vertexBufferLayout.arrayStride = 5 * sizeof(float);
- vertexBufferLayout.stepMode = wgpu::VertexStepMode::Vertex;
+ vertexBufferLayout.stepMode = WGPUVertexStepMode_Vertex;
vertexBufferLayout.attributeCount = 2;
vertexBufferLayout.attributes = attributes;
// Pipeline layout
- wgpu::PipelineLayoutDescriptor layoutDesc{};
- wgpu::PipelineLayout pipelineLayout = device_.createPipelineLayout(layoutDesc);
+ WGPUPipelineLayoutDescriptor layoutDesc = {};
+ layoutDesc.nextInChain = nullptr;
+ layoutDesc.label = {nullptr, WGPU_STRLEN};
+ layoutDesc.bindGroupLayoutCount = 0;
+ layoutDesc.bindGroupLayouts = nullptr;
+
+ WGPUPipelineLayout pipelineLayout = wgpuDeviceCreatePipelineLayout(device_, &layoutDesc);
// Color target
- wgpu::BlendState blend{};
- blend.color.operation = wgpu::BlendOperation::Add;
- blend.color.srcFactor = wgpu::BlendFactor::One;
- blend.color.dstFactor = wgpu::BlendFactor::Zero;
- blend.alpha.operation = wgpu::BlendOperation::Add;
- blend.alpha.srcFactor = wgpu::BlendFactor::One;
- blend.alpha.dstFactor = wgpu::BlendFactor::Zero;
-
- wgpu::ColorTargetState colorTarget{};
+ WGPUBlendState blend = {};
+ blend.color.operation = WGPUBlendOperation_Add;
+ blend.color.srcFactor = WGPUBlendFactor_One;
+ blend.color.dstFactor = WGPUBlendFactor_Zero;
+ blend.alpha.operation = WGPUBlendOperation_Add;
+ blend.alpha.srcFactor = WGPUBlendFactor_One;
+ blend.alpha.dstFactor = WGPUBlendFactor_Zero;
+
+ WGPUColorTargetState colorTarget = {};
+ colorTarget.nextInChain = nullptr;
colorTarget.format = surfaceFormat_;
colorTarget.blend = &blend;
- colorTarget.writeMask = wgpu::ColorWriteMask::All;
+ colorTarget.writeMask = WGPUColorWriteMask_All;
- wgpu::FragmentState fragmentState{};
+ WGPUFragmentState fragmentState = {};
+ fragmentState.nextInChain = nullptr;
fragmentState.module = shader;
- fragmentState.entryPoint.data = "fs_main";
- fragmentState.entryPoint.length = 7;
+ fragmentState.entryPoint = {"fs_main", 7};
+ fragmentState.constantCount = 0;
+ fragmentState.constants = nullptr;
fragmentState.targetCount = 1;
fragmentState.targets = &colorTarget;
// Line strip pipeline
- wgpu::RenderPipelineDescriptor pipelineDesc{};
+ WGPURenderPipelineDescriptor pipelineDesc = {};
+ pipelineDesc.nextInChain = nullptr;
+ pipelineDesc.label = {nullptr, WGPU_STRLEN};
pipelineDesc.layout = pipelineLayout;
+
+ pipelineDesc.vertex.nextInChain = nullptr;
pipelineDesc.vertex.module = shader;
- pipelineDesc.vertex.entryPoint.data = "vs_main";
- pipelineDesc.vertex.entryPoint.length = 7;
+ pipelineDesc.vertex.entryPoint = {"vs_main", 7};
+ pipelineDesc.vertex.constantCount = 0;
+ pipelineDesc.vertex.constants = nullptr;
pipelineDesc.vertex.bufferCount = 1;
pipelineDesc.vertex.buffers = &vertexBufferLayout;
- pipelineDesc.primitive.topology = wgpu::PrimitiveTopology::LineStrip;
- pipelineDesc.fragment = &fragmentState;
+
+ pipelineDesc.primitive.nextInChain = nullptr;
+ pipelineDesc.primitive.topology = WGPUPrimitiveTopology_LineStrip;
+ pipelineDesc.primitive.stripIndexFormat = WGPUIndexFormat_Undefined;
+ pipelineDesc.primitive.frontFace = WGPUFrontFace_CCW;
+ pipelineDesc.primitive.cullMode = WGPUCullMode_None;
+
+ pipelineDesc.depthStencil = nullptr;
+ pipelineDesc.multisample.nextInChain = nullptr;
pipelineDesc.multisample.count = 1;
pipelineDesc.multisample.mask = ~0u;
+ pipelineDesc.multisample.alphaToCoverageEnabled = false;
- linePipeline_ = device_.createRenderPipeline(pipelineDesc);
+ pipelineDesc.fragment = &fragmentState;
+
+ linePipeline_ = wgpuDeviceCreateRenderPipeline(device_, &pipelineDesc);
// Line list pipeline for grid
- pipelineDesc.primitive.topology = wgpu::PrimitiveTopology::LineList;
- lineListPipeline_ = device_.createRenderPipeline(pipelineDesc);
+ pipelineDesc.primitive.topology = WGPUPrimitiveTopology_LineList;
+ lineListPipeline_ = wgpuDeviceCreateRenderPipeline(device_, &pipelineDesc);
+
+ // Cleanup
+ wgpuPipelineLayoutRelease(pipelineLayout);
+ wgpuShaderModuleRelease(shader);
}
void Renderer::update() {
@@ -128,41 +163,75 @@ void Renderer::update() {
}
void Renderer::render() {
- wgpu::SurfaceTexture surfaceTexture;
- surface_.getCurrentTexture(&surfaceTexture);
+ WGPUSurfaceTexture surfaceTexture;
+ wgpuSurfaceGetCurrentTexture(surface_, &surfaceTexture);
- if (surfaceTexture.status != wgpu::SurfaceGetCurrentTextureStatus::SuccessOptimal &&
- surfaceTexture.status != wgpu::SurfaceGetCurrentTextureStatus::SuccessSuboptimal) {
+ if (surfaceTexture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal &&
+ surfaceTexture.status != WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal) {
std::cerr << "Failed to get surface texture" << std::endl;
return;
}
- wgpu::Texture texture(surfaceTexture.texture);
- wgpu::TextureView textureView = texture.createView();
+ WGPUTextureViewDescriptor viewDesc = {};
+ viewDesc.nextInChain = nullptr;
+ viewDesc.label = {nullptr, WGPU_STRLEN};
+ viewDesc.format = surfaceFormat_;
+ viewDesc.dimension = WGPUTextureViewDimension_2D;
+ viewDesc.baseMipLevel = 0;
+ viewDesc.mipLevelCount = 1;
+ viewDesc.baseArrayLayer = 0;
+ viewDesc.arrayLayerCount = 1;
+ viewDesc.aspect = WGPUTextureAspect_All;
+
+ WGPUTextureView textureView = wgpuTextureCreateView(surfaceTexture.texture, &viewDesc);
+
+ WGPUCommandEncoderDescriptor encoderDesc = {};
+ encoderDesc.nextInChain = nullptr;
+ encoderDesc.label = {nullptr, WGPU_STRLEN};
- wgpu::CommandEncoder encoder = device_.createCommandEncoder();
+ WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, &encoderDesc);
- wgpu::RenderPassColorAttachment colorAttachment{};
+ WGPURenderPassColorAttachment colorAttachment = {};
+ colorAttachment.nextInChain = nullptr;
colorAttachment.view = textureView;
- colorAttachment.loadOp = wgpu::LoadOp::Clear;
- colorAttachment.storeOp = wgpu::StoreOp::Store;
+ colorAttachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
+ colorAttachment.resolveTarget = nullptr;
+ colorAttachment.loadOp = WGPULoadOp_Clear;
+ colorAttachment.storeOp = WGPUStoreOp_Store;
colorAttachment.clearValue = {0.1, 0.1, 0.15, 1.0};
- wgpu::RenderPassDescriptor renderPassDesc{};
+ WGPURenderPassDescriptor renderPassDesc = {};
+ renderPassDesc.nextInChain = nullptr;
+ renderPassDesc.label = {nullptr, WGPU_STRLEN};
renderPassDesc.colorAttachmentCount = 1;
renderPassDesc.colorAttachments = &colorAttachment;
+ renderPassDesc.depthStencilAttachment = nullptr;
+ renderPassDesc.occlusionQuerySet = nullptr;
+ renderPassDesc.timestampWrites = nullptr;
- wgpu::RenderPassEncoder pass = encoder.beginRenderPass(renderPassDesc);
+ WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderPassDesc);
for (auto& waterfall : waterfalls_) {
waterfall->render(pass, linePipeline_, lineListPipeline_, width_, height_);
}
- pass.end();
+ wgpuRenderPassEncoderEnd(pass);
+
+ WGPUCommandBufferDescriptor cmdBufferDesc = {};
+ cmdBufferDesc.nextInChain = nullptr;
+ cmdBufferDesc.label = {nullptr, WGPU_STRLEN};
+
+ WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, &cmdBufferDesc);
+
+ WGPUQueue queue = wgpuDeviceGetQueue(device_);
+ wgpuQueueSubmit(queue, 1, &commands);
+ wgpuSurfacePresent(surface_);
- wgpu::CommandBuffer commands = encoder.finish();
- device_.getQueue().submit(1, &commands);
- surface_.present();
+ // Cleanup
+ wgpuCommandBufferRelease(commands);
+ wgpuCommandEncoderRelease(encoder);
+ wgpuRenderPassEncoderRelease(pass);
+ wgpuTextureViewRelease(textureView);
}
void Renderer::resize(int width, int height) {