From 9d06308207f99709dc2d6ecdd64a31005cdac86f Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 18 Dec 2023 23:07:39 +0900 Subject: [PATCH] wg_engine: apply tvg coding style. --- src/renderer/wg_engine/tvgWgBindGroups.cpp | 154 ++++++++++++++------ src/renderer/wg_engine/tvgWgBindGroups.h | 22 ++- src/renderer/wg_engine/tvgWgCommon.cpp | 126 +++++++++++----- src/renderer/wg_engine/tvgWgCommon.h | 17 ++- src/renderer/wg_engine/tvgWgGeometry.cpp | 22 ++- src/renderer/wg_engine/tvgWgGeometry.h | 13 +- src/renderer/wg_engine/tvgWgPipelines.cpp | 145 +++++++++--------- src/renderer/wg_engine/tvgWgPipelines.h | 55 ++++--- src/renderer/wg_engine/tvgWgRenderData.cpp | 61 ++++++-- src/renderer/wg_engine/tvgWgRenderData.h | 16 +- src/renderer/wg_engine/tvgWgRenderer.cpp | 103 +++++++++---- src/renderer/wg_engine/tvgWgRenderer.h | 7 +- src/renderer/wg_engine/tvgWgShaderTypes.cpp | 67 ++++++--- src/renderer/wg_engine/tvgWgShaderTypes.h | 15 +- 14 files changed, 541 insertions(+), 282 deletions(-) diff --git a/src/renderer/wg_engine/tvgWgBindGroups.cpp b/src/renderer/wg_engine/tvgWgBindGroups.cpp index 49bf7739..220f38cd 100644 --- a/src/renderer/wg_engine/tvgWgBindGroups.cpp +++ b/src/renderer/wg_engine/tvgWgBindGroups.cpp @@ -22,8 +22,18 @@ #include "tvgWgBindGroups.h" + WGPUBindGroupLayout WgBindGroupCanvas::layout = nullptr; -WGPUBindGroupLayout WgBindGroupCanvas::getLayout(WGPUDevice device) { +WGPUBindGroupLayout WgBindGroupPaint::layout = nullptr; +WGPUBindGroupLayout WgBindGroupSolidColor::layout = nullptr; +WGPUBindGroupLayout WgBindGroupLinearGradient::layout = nullptr; +WGPUBindGroupLayout WgBindGroupRadialGradient::layout = nullptr; +WGPUBindGroupLayout WgBindGroupPicture::layout = nullptr; +WGPUBindGroupLayout WgBindGroupCompose::layout = nullptr; + + +WGPUBindGroupLayout WgBindGroupCanvas::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntryBuffer(0) @@ -33,11 +43,15 @@ WGPUBindGroupLayout WgBindGroupCanvas::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupCanvas::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupCanvas::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeMat4x4f& uViewMat) { +void WgBindGroupCanvas::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupCanvas::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeMat4x4f& uViewMat) +{ release(); uBufferViewMat = createBuffer(device, queue, &uViewMat, sizeof(uViewMat)); const WGPUBindGroupEntry bindGroupEntries[] { @@ -47,13 +61,16 @@ void WgBindGroupCanvas::initialize(WGPUDevice device, WGPUQueue queue, WgShaderT assert(mBindGroup); } -void WgBindGroupCanvas::release() { + +void WgBindGroupCanvas::release() +{ releaseBindGroup(mBindGroup); releaseBuffer(uBufferViewMat); } -WGPUBindGroupLayout WgBindGroupPaint::layout = nullptr; -WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntryBuffer(0), @@ -64,11 +81,15 @@ WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupPaint::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupPaint::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeMat4x4f& uModelMat, WgShaderTypeBlendSettings& uBlendSettings) { +void WgBindGroupPaint::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupPaint::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeMat4x4f& uModelMat, WgShaderTypeBlendSettings& uBlendSettings) +{ release(); uBufferModelMat = createBuffer(device, queue, &uModelMat, sizeof(uModelMat)); uBufferBlendSettings = createBuffer(device, queue, &uBlendSettings, sizeof(uBlendSettings)); @@ -80,14 +101,17 @@ void WgBindGroupPaint::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTy assert(mBindGroup); } -void WgBindGroupPaint::release() { + +void WgBindGroupPaint::release() +{ releaseBindGroup(mBindGroup); releaseBuffer(uBufferBlendSettings); releaseBuffer(uBufferModelMat); } -WGPUBindGroupLayout WgBindGroupSolidColor::layout = nullptr; -WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntryBuffer(0) @@ -97,11 +121,15 @@ WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupSolidColor::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupSolidColor::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeSolidColor &uSolidColor) { +void WgBindGroupSolidColor::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupSolidColor::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeSolidColor &uSolidColor) +{ release(); uBufferSolidColor = createBuffer(device, queue, &uSolidColor, sizeof(uSolidColor)); const WGPUBindGroupEntry bindGroupEntries[] { @@ -111,13 +139,16 @@ void WgBindGroupSolidColor::initialize(WGPUDevice device, WGPUQueue queue, WgSha assert(mBindGroup); } -void WgBindGroupSolidColor::release() { + +void WgBindGroupSolidColor::release() +{ releaseBindGroup(mBindGroup); releaseBuffer(uBufferSolidColor); } -WGPUBindGroupLayout WgBindGroupLinearGradient::layout = nullptr; -WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntryBuffer(0) @@ -127,11 +158,15 @@ WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupLinearGradient::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupLinearGradient::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeLinearGradient &uLinearGradient) { +void WgBindGroupLinearGradient::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupLinearGradient::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeLinearGradient &uLinearGradient) +{ release(); uBufferLinearGradient = createBuffer(device, queue, &uLinearGradient, sizeof(uLinearGradient)); const WGPUBindGroupEntry bindGroupEntries[] { @@ -141,13 +176,16 @@ void WgBindGroupLinearGradient::initialize(WGPUDevice device, WGPUQueue queue, W assert(mBindGroup); } -void WgBindGroupLinearGradient::release() { + +void WgBindGroupLinearGradient::release() +{ releaseBindGroup(mBindGroup); releaseBuffer(uBufferLinearGradient); } -WGPUBindGroupLayout WgBindGroupRadialGradient::layout = nullptr; -WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntryBuffer(0) @@ -157,11 +195,15 @@ WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupRadialGradient::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupRadialGradient::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeRadialGradient &uRadialGradient) { +void WgBindGroupRadialGradient::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupRadialGradient::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTypeRadialGradient &uRadialGradient) +{ release(); uBufferRadialGradient = createBuffer(device, queue, &uRadialGradient, sizeof(uRadialGradient)); const WGPUBindGroupEntry bindGroupEntries[] { @@ -171,13 +213,16 @@ void WgBindGroupRadialGradient::initialize(WGPUDevice device, WGPUQueue queue, W assert(mBindGroup); } -void WgBindGroupRadialGradient::release() { + +void WgBindGroupRadialGradient::release() +{ releaseBuffer(uBufferRadialGradient); releaseBindGroup(mBindGroup); } -WGPUBindGroupLayout WgBindGroupPicture::layout = nullptr; -WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntrySampler(0), @@ -188,11 +233,15 @@ WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupPicture::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupPicture::initialize(WGPUDevice device, WGPUQueue queue, WGPUSampler uSampler, WGPUTextureView uTextureView) { +void WgBindGroupPicture::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupPicture::initialize(WGPUDevice device, WGPUQueue queue, WGPUSampler uSampler, WGPUTextureView uTextureView) +{ release(); const WGPUBindGroupEntry bindGroupEntries[] { makeBindGroupEntrySampler(0, uSampler), @@ -202,12 +251,15 @@ void WgBindGroupPicture::initialize(WGPUDevice device, WGPUQueue queue, WGPUSamp assert(mBindGroup); } -void WgBindGroupPicture::release() { + +void WgBindGroupPicture::release() +{ releaseBindGroup(mBindGroup); } -WGPUBindGroupLayout WgBindGroupCompose::layout = nullptr; -WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device) { + +WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device) +{ if (layout) return layout; const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { makeBindGroupLayoutEntrySampler(0), @@ -219,11 +271,15 @@ WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device) { return layout; } -void WgBindGroupCompose::releaseLayout() { - releaseBindGroupLayout(layout); -}; -void WgBindGroupCompose::initialize(WGPUDevice device, WGPUQueue queue, WGPUSampler uSampler, WGPUTextureView uTextureSrc, WGPUTextureView uTextureDst) { +void WgBindGroupCompose::releaseLayout() +{ + releaseBindGroupLayout(layout); +} + + +void WgBindGroupCompose::initialize(WGPUDevice device, WGPUQueue queue, WGPUSampler uSampler, WGPUTextureView uTextureSrc, WGPUTextureView uTextureDst) +{ release(); const WGPUBindGroupEntry bindGroupEntries[] { makeBindGroupEntrySampler(0, uSampler), @@ -234,6 +290,8 @@ void WgBindGroupCompose::initialize(WGPUDevice device, WGPUQueue queue, WGPUSamp assert(mBindGroup); } -void WgBindGroupCompose::release() { + +void WgBindGroupCompose::release() +{ releaseBindGroup(mBindGroup); } diff --git a/src/renderer/wg_engine/tvgWgBindGroups.h b/src/renderer/wg_engine/tvgWgBindGroups.h index c6379377..d849e48c 100644 --- a/src/renderer/wg_engine/tvgWgBindGroups.h +++ b/src/renderer/wg_engine/tvgWgBindGroups.h @@ -27,7 +27,8 @@ #include "tvgWgShaderTypes.h" // @group(0) -struct WgBindGroupCanvas: public WgBindGroup { +struct WgBindGroupCanvas : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -38,8 +39,10 @@ struct WgBindGroupCanvas: public WgBindGroup { void release(); }; + // @group(1) -struct WgBindGroupPaint: public WgBindGroup { +struct WgBindGroupPaint : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -53,7 +56,8 @@ struct WgBindGroupPaint: public WgBindGroup { }; // @group(2) -struct WgBindGroupSolidColor: public WgBindGroup { +struct WgBindGroupSolidColor : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -65,7 +69,8 @@ struct WgBindGroupSolidColor: public WgBindGroup { }; // @group(2) -struct WgBindGroupLinearGradient: public WgBindGroup { +struct WgBindGroupLinearGradient : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -77,7 +82,8 @@ struct WgBindGroupLinearGradient: public WgBindGroup { }; // @group(2) -struct WgBindGroupRadialGradient: public WgBindGroup { +struct WgBindGroupRadialGradient : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -89,7 +95,8 @@ struct WgBindGroupRadialGradient: public WgBindGroup { }; // @group(2) -struct WgBindGroupPicture: public WgBindGroup { +struct WgBindGroupPicture : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); @@ -101,7 +108,8 @@ struct WgBindGroupPicture: public WgBindGroup { }; // @group(2) -struct WgBindGroupCompose: public WgBindGroup { +struct WgBindGroupCompose : public WgBindGroup +{ static WGPUBindGroupLayout layout; static WGPUBindGroupLayout getLayout(WGPUDevice device); static void releaseLayout(); diff --git a/src/renderer/wg_engine/tvgWgCommon.cpp b/src/renderer/wg_engine/tvgWgCommon.cpp index c9ee8803..54584e31 100644 --- a/src/renderer/wg_engine/tvgWgCommon.cpp +++ b/src/renderer/wg_engine/tvgWgCommon.cpp @@ -26,11 +26,14 @@ // bind group //***************************************************************************** -void WgBindGroup::set(WGPURenderPassEncoder encoder, uint32_t groupIndex) { +void WgBindGroup::set(WGPURenderPassEncoder encoder, uint32_t groupIndex) +{ wgpuRenderPassEncoderSetBindGroup(encoder, groupIndex, mBindGroup, 0, nullptr); } -WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUBuffer buffer) { + +WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUBuffer buffer) +{ WGPUBindGroupEntry bindGroupEntry{}; bindGroupEntry.nextInChain = nullptr; bindGroupEntry.binding = binding; @@ -42,7 +45,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUB return bindGroupEntry; } -WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPUSampler sampler) { + +WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPUSampler sampler) +{ WGPUBindGroupEntry bindGroupEntry{}; bindGroupEntry.nextInChain = nullptr; bindGroupEntry.binding = binding; @@ -54,7 +59,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPU return bindGroupEntry; } -WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding, WGPUTextureView textureView) { + +WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding, WGPUTextureView textureView) +{ WGPUBindGroupEntry bindGroupEntry{}; bindGroupEntry.nextInChain = nullptr; bindGroupEntry.binding = binding; @@ -66,7 +73,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding, return bindGroupEntry; } -WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t binding) { + +WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t binding) +{ WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.binding = binding; @@ -78,7 +87,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t bi return bindGroupLayoutEntry; } -WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t binding) { + +WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t binding) +{ WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.binding = binding; @@ -88,7 +99,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t b return bindGroupLayoutEntry; } -WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32_t binding) { + +WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32_t binding) +{ WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.binding = binding; @@ -100,7 +113,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32 return bindGroupLayoutEntry; } -WGPUBuffer WgBindGroup::createBuffer(WGPUDevice device, WGPUQueue queue, const void *data, size_t size) { + +WGPUBuffer WgBindGroup::createBuffer(WGPUDevice device, WGPUQueue queue, const void *data, size_t size) +{ WGPUBufferDescriptor bufferDescriptor{}; bufferDescriptor.nextInChain = nullptr; bufferDescriptor.label = "The uniform buffer"; @@ -113,7 +128,9 @@ WGPUBuffer WgBindGroup::createBuffer(WGPUDevice device, WGPUQueue queue, const v return buffer; } -WGPUBindGroup WgBindGroup::createBindGroup(WGPUDevice device, WGPUBindGroupLayout layout, const WGPUBindGroupEntry* bindGroupEntries, uint32_t count) { + +WGPUBindGroup WgBindGroup::createBindGroup(WGPUDevice device, WGPUBindGroupLayout layout, const WGPUBindGroupEntry* bindGroupEntries, uint32_t count) +{ WGPUBindGroupDescriptor bindGroupDesc{}; bindGroupDesc.nextInChain = nullptr; bindGroupDesc.label = "The binding group sampler"; @@ -123,7 +140,9 @@ WGPUBindGroup WgBindGroup::createBindGroup(WGPUDevice device, WGPUBindGroupLayou return wgpuDeviceCreateBindGroup(device, &bindGroupDesc); } -WGPUBindGroupLayout WgBindGroup::createBindGroupLayout(WGPUDevice device, const WGPUBindGroupLayoutEntry* bindGroupLayoutEntries, uint32_t count) { + +WGPUBindGroupLayout WgBindGroup::createBindGroupLayout(WGPUDevice device, const WGPUBindGroupLayoutEntry* bindGroupLayoutEntries, uint32_t count) +{ WGPUBindGroupLayoutDescriptor bindGroupLayoutDesc{}; bindGroupLayoutDesc.nextInChain = nullptr; bindGroupLayoutDesc.label = "The bind group layout"; @@ -132,7 +151,9 @@ WGPUBindGroupLayout WgBindGroup::createBindGroupLayout(WGPUDevice device, const return wgpuDeviceCreateBindGroupLayout(device, &bindGroupLayoutDesc); } -void WgBindGroup::releaseBuffer(WGPUBuffer& buffer) { + +void WgBindGroup::releaseBuffer(WGPUBuffer& buffer) +{ if (buffer) { wgpuBufferDestroy(buffer); wgpuBufferRelease(buffer); @@ -140,12 +161,16 @@ void WgBindGroup::releaseBuffer(WGPUBuffer& buffer) { } } -void WgBindGroup::releaseBindGroup(WGPUBindGroup& bindGroup) { + +void WgBindGroup::releaseBindGroup(WGPUBindGroup& bindGroup) +{ if (bindGroup) wgpuBindGroupRelease(bindGroup); bindGroup = nullptr; } -void WgBindGroup::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout) { + +void WgBindGroup::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout) +{ if (bindGroupLayout) wgpuBindGroupLayoutRelease(bindGroupLayout); bindGroupLayout = nullptr; } @@ -167,23 +192,29 @@ void WgPipeline::allocate(WGPUDevice device, assert(mPipelineLayout); mRenderPipeline = createRenderPipeline(device, - vertexBufferLayouts, attribsCount, - stencilCompareFunction, stencilOperation, - mPipelineLayout, mShaderModule, pipelineLabel); + vertexBufferLayouts, attribsCount, + stencilCompareFunction, stencilOperation, + mPipelineLayout, mShaderModule, pipelineLabel); assert(mRenderPipeline); } -void WgPipeline::release() { + +void WgPipeline::release() +{ destroyRenderPipeline(mRenderPipeline); destroyShaderModule(mShaderModule); destroyPipelineLayout(mPipelineLayout); } -void WgPipeline::set(WGPURenderPassEncoder renderPassEncoder) { + +void WgPipeline::set(WGPURenderPassEncoder renderPassEncoder) +{ wgpuRenderPassEncoderSetPipeline(renderPassEncoder, mRenderPipeline); }; -WGPUBlendState WgPipeline::makeBlendState() { + +WGPUBlendState WgPipeline::makeBlendState() +{ WGPUBlendState blendState{}; blendState.color.operation = WGPUBlendOperation_Add; blendState.color.srcFactor = WGPUBlendFactor_SrcAlpha; @@ -194,7 +225,9 @@ WGPUBlendState WgPipeline::makeBlendState() { return blendState; } -WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blendState) { + +WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blendState) +{ WGPUColorTargetState colorTargetState{}; colorTargetState.nextInChain = nullptr; colorTargetState.format = WGPUTextureFormat_BGRA8Unorm; // (WGPUTextureFormat_BGRA8UnormSrgb) @@ -203,7 +236,9 @@ WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blen return colorTargetState; } -WGPUVertexBufferLayout WgPipeline::makeVertexBufferLayout(const WGPUVertexAttribute* vertexAttributes, uint32_t count, uint64_t stride) { + +WGPUVertexBufferLayout WgPipeline::makeVertexBufferLayout(const WGPUVertexAttribute* vertexAttributes, uint32_t count, uint64_t stride) +{ WGPUVertexBufferLayout vertexBufferLayoutPos{}; vertexBufferLayoutPos.arrayStride = stride; vertexBufferLayoutPos.stepMode = WGPUVertexStepMode_Vertex; @@ -212,7 +247,9 @@ WGPUVertexBufferLayout WgPipeline::makeVertexBufferLayout(const WGPUVertexAttrib return vertexBufferLayoutPos; } -WGPUVertexState WgPipeline::makeVertexState(WGPUShaderModule shaderModule, const WGPUVertexBufferLayout* buffers, uint32_t count) { + +WGPUVertexState WgPipeline::makeVertexState(WGPUShaderModule shaderModule, const WGPUVertexBufferLayout* buffers, uint32_t count) +{ WGPUVertexState vertexState{}; vertexState.nextInChain = nullptr; vertexState.module = shaderModule; @@ -224,7 +261,9 @@ WGPUVertexState WgPipeline::makeVertexState(WGPUShaderModule shaderModule, const return vertexState; } -WGPUPrimitiveState WgPipeline::makePrimitiveState() { + +WGPUPrimitiveState WgPipeline::makePrimitiveState() +{ WGPUPrimitiveState primitiveState{}; primitiveState.nextInChain = nullptr; primitiveState.topology = WGPUPrimitiveTopology_TriangleList; @@ -234,7 +273,8 @@ WGPUPrimitiveState WgPipeline::makePrimitiveState() { return primitiveState; } -WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction compare, WGPUStencilOperation operation) { +WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction compare, WGPUStencilOperation operation) +{ WGPUDepthStencilState depthStencilState{}; depthStencilState.nextInChain = nullptr; depthStencilState.format = WGPUTextureFormat_Stencil8; @@ -256,7 +296,9 @@ WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction comp return depthStencilState; } -WGPUMultisampleState WgPipeline::makeMultisampleState() { + +WGPUMultisampleState WgPipeline::makeMultisampleState() +{ WGPUMultisampleState multisampleState{}; multisampleState.nextInChain = nullptr; multisampleState.count = 1; @@ -265,7 +307,9 @@ WGPUMultisampleState WgPipeline::makeMultisampleState() { return multisampleState; } -WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, WGPUColorTargetState* targets, uint32_t size) { + +WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, WGPUColorTargetState* targets, uint32_t size) +{ WGPUFragmentState fragmentState{}; fragmentState.nextInChain = nullptr; fragmentState.module = shaderModule; @@ -277,7 +321,9 @@ WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, W return fragmentState; } -WGPUPipelineLayout WgPipeline::createPipelineLayout(WGPUDevice device, const WGPUBindGroupLayout* bindGroupLayouts, uint32_t count) { + +WGPUPipelineLayout WgPipeline::createPipelineLayout(WGPUDevice device, const WGPUBindGroupLayout* bindGroupLayouts, uint32_t count) +{ WGPUPipelineLayoutDescriptor pipelineLayoutDesc{}; pipelineLayoutDesc.nextInChain = nullptr; pipelineLayoutDesc.label = "The Pipeline layout"; @@ -286,7 +332,9 @@ WGPUPipelineLayout WgPipeline::createPipelineLayout(WGPUDevice device, const WGP return wgpuDeviceCreatePipelineLayout(device, &pipelineLayoutDesc); } -WGPUShaderModule WgPipeline::createShaderModule(WGPUDevice device, const char* code, const char* label) { + +WGPUShaderModule WgPipeline::createShaderModule(WGPUDevice device, const char* code, const char* label) +{ WGPUShaderModuleWGSLDescriptor shaderModuleWGSLDesc{}; shaderModuleWGSLDesc.chain.next = nullptr; shaderModuleWGSLDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; @@ -299,11 +347,13 @@ WGPUShaderModule WgPipeline::createShaderModule(WGPUDevice device, const char* c return wgpuDeviceCreateShaderModule(device, &shaderModuleDesc); } + WGPURenderPipeline WgPipeline::createRenderPipeline(WGPUDevice device, - WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, - WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, - WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, - const char* pipelineName) { + WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, + WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, + WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, + const char* pipelineName) +{ WGPUBlendState blendState = makeBlendState(); WGPUColorTargetState colorTargetStates[] = { makeColorTargetState(&blendState) @@ -327,17 +377,23 @@ WGPURenderPipeline WgPipeline::createRenderPipeline(WGPUDevice device, return wgpuDeviceCreateRenderPipeline(device, &renderPipelineDesc); } -void WgPipeline::destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout) { + +void WgPipeline::destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout) +{ if (pipelineLayout) wgpuPipelineLayoutRelease(pipelineLayout); pipelineLayout = nullptr; } -void WgPipeline::destroyShaderModule(WGPUShaderModule& shaderModule) { + +void WgPipeline::destroyShaderModule(WGPUShaderModule& shaderModule) +{ if (shaderModule) wgpuShaderModuleRelease(shaderModule); shaderModule = nullptr; } -void WgPipeline::destroyRenderPipeline(WGPURenderPipeline& renderPipeline) { + +void WgPipeline::destroyRenderPipeline(WGPURenderPipeline& renderPipeline) +{ if (renderPipeline) wgpuRenderPipelineRelease(renderPipeline); renderPipeline = nullptr; } \ No newline at end of file diff --git a/src/renderer/wg_engine/tvgWgCommon.h b/src/renderer/wg_engine/tvgWgCommon.h index b428ff5f..b7808c5f 100644 --- a/src/renderer/wg_engine/tvgWgCommon.h +++ b/src/renderer/wg_engine/tvgWgCommon.h @@ -28,7 +28,8 @@ #include "tvgCommon.h" #include "tvgRender.h" -struct WgBindGroup { +struct WgBindGroup +{ WGPUBindGroup mBindGroup{}; void set(WGPURenderPassEncoder encoder, uint32_t groupIndex); @@ -50,7 +51,8 @@ struct WgBindGroup { static void releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout); }; -struct WgPipeline { +struct WgPipeline +{ protected: void allocate(WGPUDevice device, WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, @@ -76,12 +78,11 @@ public: static WGPUPipelineLayout createPipelineLayout(WGPUDevice device, const WGPUBindGroupLayout* bindGroupLayouts, uint32_t count); static WGPUShaderModule createShaderModule(WGPUDevice device, const char* code, const char* label); - static WGPURenderPipeline createRenderPipeline( - WGPUDevice device, - WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, - WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, - WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, - const char* pipelineLabel); + static WGPURenderPipeline createRenderPipeline(WGPUDevice device, + WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, + WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, + WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, + const char* pipelineLabel); static void destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout); static void destroyShaderModule(WGPUShaderModule& shaderModule); static void destroyRenderPipeline(WGPURenderPipeline& renderPipeline); diff --git a/src/renderer/wg_engine/tvgWgGeometry.cpp b/src/renderer/wg_engine/tvgWgGeometry.cpp index ecda95a6..b8791d7c 100644 --- a/src/renderer/wg_engine/tvgWgGeometry.cpp +++ b/src/renderer/wg_engine/tvgWgGeometry.cpp @@ -22,7 +22,8 @@ #include "tvgWgGeometry.h" -void WgVertexList::computeTriFansIndexes() { +void WgVertexList::computeTriFansIndexes() +{ assert(mVertexList.count > 2); mIndexList.reserve((mVertexList.count - 2) * 3); for (size_t i = 0; i < mVertexList.count - 2; i++) { @@ -32,7 +33,9 @@ void WgVertexList::computeTriFansIndexes() { } } -void WgVertexList::appendCubic(WgPoint p1, WgPoint p2, WgPoint p3) { + +void WgVertexList::appendCubic(WgPoint p1, WgPoint p2, WgPoint p3) +{ WgPoint p0 = mVertexList.count > 0 ? mVertexList.last() : WgPoint(0.0f, 0.0f); const size_t segs = 16; for (size_t i = 1; i <= segs; i++) { @@ -46,7 +49,9 @@ void WgVertexList::appendCubic(WgPoint p1, WgPoint p2, WgPoint p3) { } } -void WgVertexList::appendRect(WgPoint p0, WgPoint p1, WgPoint p2, WgPoint p3) { + +void WgVertexList::appendRect(WgPoint p0, WgPoint p1, WgPoint p2, WgPoint p3) +{ uint32_t index = mVertexList.count; mVertexList.push(p0); // +0 mVertexList.push(p1); // +1 @@ -60,8 +65,10 @@ void WgVertexList::appendRect(WgPoint p0, WgPoint p1, WgPoint p2, WgPoint p3) { mIndexList.push(index + 2); } + // TODO: optimize vertex and index count -void WgVertexList::appendCircle(WgPoint center, float radius) { +void WgVertexList::appendCircle(WgPoint center, float radius) +{ uint32_t index = mVertexList.count; uint32_t nSegments = 32; for (uint32_t i = 0; i < nSegments; i++) { @@ -79,7 +86,10 @@ void WgVertexList::appendCircle(WgPoint center, float radius) { } } -void WgVertexList::close() { - if (mVertexList.count > 1) + +void WgVertexList::close() +{ + if (mVertexList.count > 1) { mVertexList.push(mVertexList[0]); + } } \ No newline at end of file diff --git a/src/renderer/wg_engine/tvgWgGeometry.h b/src/renderer/wg_engine/tvgWgGeometry.h index 402a607f..4dc7e8ed 100644 --- a/src/renderer/wg_engine/tvgWgGeometry.h +++ b/src/renderer/wg_engine/tvgWgGeometry.h @@ -34,7 +34,6 @@ public: float x; float y; -public: WgPoint() {} WgPoint(float x, float y): x(x), y(y) {} WgPoint(const Point& p): x(p.x), y(p.y) {} @@ -54,26 +53,30 @@ public: float length() const { return sqrt(x*x + y*y); } float dot(const WgPoint& p) const { return x * p.x + y * p.y; } - float dist(const WgPoint& p) const { + float dist(const WgPoint& p) const + { return sqrt( (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) ); } - void normalize() { + void normalize() + { float rlen = 1.0f / length(); x *= rlen; y *= rlen; } - WgPoint normal() const { + WgPoint normal() const + { float rlen = 1.0f / length(); return { x * rlen, y * rlen }; } }; -class WgVertexList { +class WgVertexList +{ public: Array mVertexList; Array mIndexList; diff --git a/src/renderer/wg_engine/tvgWgPipelines.cpp b/src/renderer/wg_engine/tvgWgPipelines.cpp index 0903dc82..ef9d3a95 100644 --- a/src/renderer/wg_engine/tvgWgPipelines.cpp +++ b/src/renderer/wg_engine/tvgWgPipelines.cpp @@ -25,7 +25,8 @@ #define ARRAY_ELEMENTS_COUNT(arr) sizeof(arr)/sizeof(arr[0]) -void WgPipelineFillShape::initialize(WGPUDevice device) { +void WgPipelineFillShape::initialize(WGPUDevice device) +{ // vertex attributes settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexBufferLayout vertexBufferLayouts[] = { @@ -43,20 +44,21 @@ void WgPipelineFillShape::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Invert; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineFill; - const char* shaderLabel = "The shader fill"; - const char* pipelineLabel = "The render pipeline fill shape"; + auto shaderSource = cShaderSource_PipelineFill; + auto shaderLabel = "The shader fill"; + auto pipelineLabel = "The render pipeline fill shape"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} -void WgPipelineFillStroke::initialize(WGPUDevice device) { + +void WgPipelineFillStroke::initialize(WGPUDevice device) +{ // vertex and buffers settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexBufferLayout vertexBufferLayouts[] = { @@ -74,20 +76,21 @@ void WgPipelineFillStroke::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Replace; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineFill; - const char* shaderLabel = "The shader fill"; - const char* pipelineLabel = "The render pipeline fill stroke"; + auto shaderSource = cShaderSource_PipelineFill; + auto shaderLabel = "The shader fill"; + auto pipelineLabel = "The render pipeline fill stroke"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} -void WgPipelineSolid::initialize(WGPUDevice device) { + +void WgPipelineSolid::initialize(WGPUDevice device) +{ // vertex and buffers settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexBufferLayout vertexBufferLayouts[] = { @@ -106,20 +109,21 @@ void WgPipelineSolid::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineSolid; - const char* shaderLabel = "The shader solid color"; - const char* pipelineLabel = "The render pipeline solid color"; + auto shaderSource = cShaderSource_PipelineSolid; + auto shaderLabel = "The shader solid color"; + auto pipelineLabel = "The render pipeline solid color"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} -void WgPipelineLinear::initialize(WGPUDevice device) { + +void WgPipelineLinear::initialize(WGPUDevice device) +{ // vertex and buffers settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexBufferLayout vertexBufferLayouts[] = { @@ -138,20 +142,21 @@ void WgPipelineLinear::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineLinear; - const char* shaderLabel = "The shader linear gradient"; - const char* pipelineLabel = "The render pipeline linear gradient"; + auto shaderSource = cShaderSource_PipelineLinear; + auto shaderLabel = "The shader linear gradient"; + auto pipelineLabel = "The render pipeline linear gradient"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} -void WgPipelineRadial::initialize(WGPUDevice device) { + +void WgPipelineRadial::initialize(WGPUDevice device) +{ // vertex and buffers settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexBufferLayout vertexBufferLayouts[] = { @@ -170,20 +175,21 @@ void WgPipelineRadial::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineRadial; - const char* shaderLabel = "The shader radial gradient"; - const char* pipelineLabel = "The render pipeline radial gradient"; + auto shaderSource = cShaderSource_PipelineRadial; + auto shaderLabel = "The shader radial gradient"; + auto pipelineLabel = "The render pipeline radial gradient"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} -void WgPipelineImage::initialize(WGPUDevice device) { + +void WgPipelineImage::initialize(WGPUDevice device) +{ // vertex and buffers settings WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesTex = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 1 }; @@ -204,24 +210,25 @@ void WgPipelineImage::initialize(WGPUDevice device) { WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; // sheder source and labels - const char* shaderSource = cShaderSource_PipelineImage; - const char* shaderLabel = "The shader image"; - const char* pipelineLabel = "The render pipeline image"; + auto shaderSource = cShaderSource_PipelineImage; + auto shaderLabel = "The shader image"; + auto pipelineLabel = "The render pipeline image"; // allocate all pipeline handles - allocate( - device, - vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), - bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), - stencilFuncion, stencilOperation, - shaderSource, shaderLabel, pipelineLabel); -}; + allocate(device, + vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), + bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), + stencilFuncion, stencilOperation, + shaderSource, shaderLabel, pipelineLabel); +} + //************************************************************************ // pipelines //************************************************************************ -void WgPipelines::initialize(WGPUDevice device) { +void WgPipelines::initialize(WGPUDevice device) +{ mPipelineFillShape.initialize(device); mPipelineFillStroke.initialize(device); mPipelineSolid.initialize(device); @@ -230,7 +237,9 @@ void WgPipelines::initialize(WGPUDevice device) { mPipelineImage.initialize(device); } -void WgPipelines::release() { + +void WgPipelines::release() +{ WgBindGroupCompose::releaseLayout(); WgBindGroupPicture::releaseLayout(); WgBindGroupRadialGradient::releaseLayout(); diff --git a/src/renderer/wg_engine/tvgWgPipelines.h b/src/renderer/wg_engine/tvgWgPipelines.h index 0cd43efc..a90377d3 100644 --- a/src/renderer/wg_engine/tvgWgPipelines.h +++ b/src/renderer/wg_engine/tvgWgPipelines.h @@ -25,34 +25,33 @@ #include "tvgWgBindGroups.h" -struct WgPipelineFillShape: public WgPipeline { +struct WgPipelineFillShape: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); } }; -struct WgPipelineFillStroke: public WgPipeline { +struct WgPipelineFillStroke: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); } }; -struct WgPipelineSolid: public WgPipeline { +struct WgPipelineSolid: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint, - WgBindGroupSolidColor& groupSolid) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas,WgBindGroupPaint& groupPaint, WgBindGroupSolidColor& groupSolid) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); @@ -60,12 +59,11 @@ struct WgPipelineSolid: public WgPipeline { } }; -struct WgPipelineLinear: public WgPipeline { +struct WgPipelineLinear: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint, - WgBindGroupLinearGradient& groupLinear) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupLinearGradient& groupLinear) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); @@ -73,12 +71,11 @@ struct WgPipelineLinear: public WgPipeline { } }; -struct WgPipelineRadial: public WgPipeline { +struct WgPipelineRadial: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint, - WgBindGroupRadialGradient& groupRadial) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupRadialGradient& groupRadial) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); @@ -86,12 +83,11 @@ struct WgPipelineRadial: public WgPipeline { } }; -struct WgPipelineImage: public WgPipeline { +struct WgPipelineImage: public WgPipeline +{ void initialize(WGPUDevice device) override; - void use(WGPURenderPassEncoder encoder, - WgBindGroupCanvas& groupCanvas, - WgBindGroupPaint& groupPaint, - WgBindGroupPicture& groupPicture) { + void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupPicture& groupPicture) + { set(encoder); groupCanvas.set(encoder, 0); groupPaint.set(encoder, 1); @@ -99,7 +95,8 @@ struct WgPipelineImage: public WgPipeline { } }; -struct WgPipelines { +struct WgPipelines +{ WgPipelineFillShape mPipelineFillShape; WgPipelineFillStroke mPipelineFillStroke; WgPipelineSolid mPipelineSolid; diff --git a/src/renderer/wg_engine/tvgWgRenderData.cpp b/src/renderer/wg_engine/tvgWgRenderData.cpp index 6f919b96..1f79a1fa 100644 --- a/src/renderer/wg_engine/tvgWgRenderData.cpp +++ b/src/renderer/wg_engine/tvgWgRenderData.cpp @@ -26,20 +26,25 @@ // WgGeometryData //*********************************************************************** -void WgGeometryData::draw(WGPURenderPassEncoder renderPassEncoder) { +void WgGeometryData::draw(WGPURenderPassEncoder renderPassEncoder) +{ wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 0, mBufferVertex, 0, mVertexCount * sizeof(float) * 2); wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t)); wgpuRenderPassEncoderDrawIndexed(renderPassEncoder, mIndexCount, 1, 0, 0, 0); } -void WgGeometryData::drawImage(WGPURenderPassEncoder renderPassEncoder) { + +void WgGeometryData::drawImage(WGPURenderPassEncoder renderPassEncoder) +{ wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 0, mBufferVertex, 0, mVertexCount * sizeof(float) * 2); wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 1, mBufferTexCoords, 0, mVertexCount * sizeof(float) * 2); wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t)); wgpuRenderPassEncoderDrawIndexed(renderPassEncoder, mIndexCount, 1, 0, 0, 0); } -void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, WgVertexList* vertexList) { + +void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, WgVertexList* vertexList) +{ update(device, queue, (float *)vertexList->mVertexList.data, vertexList->mVertexList.count, @@ -47,7 +52,9 @@ void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, WgVertexList* ve vertexList->mIndexList.count); } -void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexData, size_t vertexCount, uint32_t* indexData, size_t indexCount) { + +void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexData, size_t vertexCount, uint32_t* indexData, size_t indexCount) +{ release(); // buffer vertex data create and write @@ -74,6 +81,7 @@ void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexDat mIndexCount = indexCount; } + void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexData, float* texCoordsData, size_t vertexCount, uint32_t* indexData, size_t indexCount) { update(device, queue, vertexData, vertexCount, indexData, indexCount); // buffer tex coords data create and write @@ -88,7 +96,9 @@ void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexDat wgpuQueueWriteBuffer(queue, mBufferTexCoords, 0, texCoordsData, vertexCount * sizeof(float) * 2); } -void WgGeometryData::release() { + +void WgGeometryData::release() +{ if (mBufferIndex) { wgpuBufferDestroy(mBufferIndex); wgpuBufferRelease(mBufferIndex); @@ -112,7 +122,8 @@ void WgGeometryData::release() { // WgImageData //*********************************************************************** -void WgImageData::update(WGPUDevice device, WGPUQueue queue, Surface* surface) { +void WgImageData::update(WGPUDevice device, WGPUQueue queue, Surface* surface) +{ release(); // sampler descriptor WGPUSamplerDescriptor samplerDesc{}; @@ -176,7 +187,9 @@ void WgImageData::update(WGPUDevice device, WGPUQueue queue, Surface* surface) { wgpuQueueWriteTexture(queue, &imageCopyTexture, surface->data, 4 * surface->w * surface->h, &textureDataLayout, &writeSize); } -void WgImageData::release() { + +void WgImageData::release() +{ if (mTexture) { wgpuTextureDestroy(mTexture); wgpuTextureRelease(mTexture); @@ -219,7 +232,8 @@ void WgRenderDataShapeSettings::update(WGPUDevice device, WGPUQueue queue, } } -void WgRenderDataShapeSettings::release() { +void WgRenderDataShapeSettings::release() +{ mBindGroupSolid.release(); mBindGroupLinear.release(); mBindGroupRadial.release(); @@ -229,7 +243,8 @@ void WgRenderDataShapeSettings::release() { // WgRenderDataShape //*********************************************************************** -void WgRenderDataShape::release() { +void WgRenderDataShape::release() +{ releaseRenderData(); mImageData.release(); mBindGroupPaint.release(); @@ -238,7 +253,9 @@ void WgRenderDataShape::release() { mBindGroupPicture.release(); } -void WgRenderDataShape::releaseRenderData() { + +void WgRenderDataShape::releaseRenderData() +{ for (uint32_t i = 0; i < mGeometryDataImage.count; i++) { mGeometryDataImage[i]->release(); delete mGeometryDataImage[i]; @@ -255,7 +272,9 @@ void WgRenderDataShape::releaseRenderData() { mGeometryDataShape.clear(); } -void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, Surface* surface, const RenderMesh* mesh) { + +void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, Surface* surface, const RenderMesh* mesh) +{ // create image geometry data Array vertexList; Array texCoordsList; @@ -304,7 +323,9 @@ void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, Surface* s mImageData.update(device, queue, surface); } -void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, const RenderShape& rshape) { + +void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, const RenderShape& rshape) +{ Array outlines{}; decodePath(rshape, outlines); @@ -326,8 +347,10 @@ void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, const Rend delete outlines[i]; } + // TODO: separate to entity -void WgRenderDataShape::stroke(WGPUDevice device, WGPUQueue queue, const RenderShape& rshape) { +void WgRenderDataShape::stroke(WGPUDevice device, WGPUQueue queue, const RenderShape& rshape) +{ if (!rshape.stroke) return; // TODO: chnage to shared_ptrs @@ -357,7 +380,9 @@ void WgRenderDataShape::stroke(WGPUDevice device, WGPUQueue queue, const RenderS delete outlines[i]; } -void WgRenderDataShape::decodePath(const RenderShape& rshape, Array& outlines) { + +void WgRenderDataShape::decodePath(const RenderShape& rshape, Array& outlines) +{ size_t pntIndex = 0; for (uint32_t cmdIndex = 0; cmdIndex < rshape.path.cmds.count; cmdIndex++) { PathCommand cmd = rshape.path.cmds[cmdIndex]; @@ -388,7 +413,9 @@ void WgRenderDataShape::decodePath(const RenderShape& rshape, Array& outlines, Array& segments) { + +void WgRenderDataShape::strokeSegments(const RenderShape& rshape, Array& outlines, Array& segments) +{ for (uint32_t i = 0; i < outlines.count; i++) { auto& vlist = outlines[i]->mVertexList; @@ -425,7 +452,9 @@ void WgRenderDataShape::strokeSegments(const RenderShape& rshape, Array& outlines, WgVertexList& strokes) { + +void WgRenderDataShape::strokeSublines(const RenderShape& rshape, Array& outlines, WgVertexList& strokes) +{ float wdt = rshape.stroke->width / 2; for (uint32_t i = 0; i < outlines.count; i++) { auto outline = outlines[i]; diff --git a/src/renderer/wg_engine/tvgWgRenderData.h b/src/renderer/wg_engine/tvgWgRenderData.h index 0f0930ea..e872e7a7 100644 --- a/src/renderer/wg_engine/tvgWgRenderData.h +++ b/src/renderer/wg_engine/tvgWgRenderData.h @@ -26,7 +26,8 @@ #include "tvgWgPipelines.h" #include "tvgWgGeometry.h" -struct WgGeometryData { +struct WgGeometryData +{ WGPUBuffer mBufferVertex{}; WGPUBuffer mBufferTexCoords{}; WGPUBuffer mBufferIndex{}; @@ -45,7 +46,8 @@ struct WgGeometryData { void release(); }; -struct WgImageData { +struct WgImageData +{ WGPUSampler mSampler{}; WGPUTexture mTexture{}; WGPUTextureView mTextureView{}; @@ -58,14 +60,17 @@ struct WgImageData { void release(); }; -class WgRenderData { +class WgRenderData +{ public: virtual void initialize(WGPUDevice device) {}; virtual void release() = 0; }; enum class WgRenderDataShapeFillType { None = 0, Solid = 1, Linear = 2, Radial = 3 }; -struct WgRenderDataShapeSettings { + +struct WgRenderDataShapeSettings +{ WgBindGroupSolidColor mBindGroupSolid{}; WgBindGroupLinearGradient mBindGroupLinear{}; WgBindGroupRadialGradient mBindGroupRadial{}; @@ -77,7 +82,8 @@ struct WgRenderDataShapeSettings { void release(); }; -class WgRenderDataShape: public WgRenderData { +class WgRenderDataShape: public WgRenderData +{ public: // geometry data for shapes, strokes and image Array mGeometryDataShape; diff --git a/src/renderer/wg_engine/tvgWgRenderer.cpp b/src/renderer/wg_engine/tvgWgRenderer.cpp index b9f16369..51d11e1e 100644 --- a/src/renderer/wg_engine/tvgWgRenderer.cpp +++ b/src/renderer/wg_engine/tvgWgRenderer.cpp @@ -30,15 +30,20 @@ #include "tvgWgRenderData.h" #include "tvgWgShaderSrc.h" -WgRenderer::WgRenderer() { +WgRenderer::WgRenderer() +{ initialize(); } -WgRenderer::~WgRenderer() { + +WgRenderer::~WgRenderer() +{ release(); } -void WgRenderer::initialize() { + +void WgRenderer::initialize() +{ // create instance WGPUInstanceDescriptor instanceDesc{}; instanceDesc.nextInChain = nullptr; @@ -106,7 +111,9 @@ void WgRenderer::initialize() { mPipelines.initialize(mDevice); } -void WgRenderer::release() { + +void WgRenderer::release() +{ if (mStencilTex) { wgpuTextureDestroy(mStencilTex); wgpuTextureRelease(mStencilTex); @@ -126,7 +133,9 @@ void WgRenderer::release() { if (mInstance) wgpuInstanceRelease(mInstance); } -RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) { + +RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) +{ // get or create render data shape auto renderDataShape = (WgRenderDataShape*)data; if (!renderDataShape) { @@ -156,11 +165,15 @@ RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const return renderDataShape; } -RenderData WgRenderer::prepare(const Array& scene, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) { + +RenderData WgRenderer::prepare(TVG_UNUSED const Array& scene, TVG_UNUSED RenderData data, TVG_UNUSED const RenderTransform* transform, TVG_UNUSED Array& clips, TVG_UNUSED uint8_t opacity, TVG_UNUSED RenderUpdateFlag flags) +{ return nullptr; } -RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) { + +RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array& clips, uint8_t opacity, RenderUpdateFlag flags) +{ // get or create render data shape auto renderDataShape = (WgRenderDataShape*)data; if (!renderDataShape) { @@ -188,56 +201,79 @@ RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderD return renderDataShape; } -bool WgRenderer::preRender() { + +bool WgRenderer::preRender() +{ return true; } -bool WgRenderer::renderShape(RenderData data) { + +bool WgRenderer::renderShape(RenderData data) +{ mRenderDatas.push(data); return true; } -bool WgRenderer::renderImage(RenderData data) { + +bool WgRenderer::renderImage(RenderData data) +{ mRenderDatas.push(data); return true; } -bool WgRenderer::postRender() { + +bool WgRenderer::postRender() +{ return true; } -bool WgRenderer::dispose(RenderData data) { + +bool WgRenderer::dispose(RenderData data) +{ auto renderData = (WgRenderData*)data; if (renderData) renderData->release(); return true; } -RenderRegion WgRenderer::region(RenderData data) { + +RenderRegion WgRenderer::region(TVG_UNUSED RenderData data) +{ return { 0, 0, INT32_MAX, INT32_MAX }; } + RenderRegion WgRenderer::viewport() { return { 0, 0, INT32_MAX, INT32_MAX }; } -bool WgRenderer::viewport(const RenderRegion& vp) { + +bool WgRenderer::viewport(TVG_UNUSED const RenderRegion& vp) +{ return true; } -bool WgRenderer::blend(BlendMethod method) { + +bool WgRenderer::blend(TVG_UNUSED BlendMethod method) +{ return false; } -ColorSpace WgRenderer::colorSpace() { + +ColorSpace WgRenderer::colorSpace() +{ return ColorSpace::Unsupported; } -bool WgRenderer::clear() { + +bool WgRenderer::clear() +{ mClearBuffer = true; return true; } -bool WgRenderer::sync() { + +bool WgRenderer::sync() +{ WGPUTextureView backBufferView = wgpuSwapChainGetCurrentTextureView(mSwapChain); // command buffer descriptor @@ -364,7 +400,9 @@ bool WgRenderer::sync() { return true; } -bool WgRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) { + +bool WgRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h) +{ // store target surface properties mTargetSurface.stride = stride; mTargetSurface.w = w; @@ -380,8 +418,10 @@ bool WgRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t return true; } + // target for native window handle -bool WgRenderer::target(void* window, uint32_t w, uint32_t h) { +bool WgRenderer::target(void* window, uint32_t w, uint32_t h) +{ // store target surface properties mTargetSurface.stride = w; mTargetSurface.w = w > 0 ? w : 1; @@ -463,26 +503,37 @@ bool WgRenderer::target(void* window, uint32_t w, uint32_t h) { return true; } -Compositor* WgRenderer::target(const RenderRegion& region, ColorSpace cs) { + +Compositor* WgRenderer::target(TVG_UNUSED const RenderRegion& region, TVG_UNUSED ColorSpace cs) +{ return nullptr; } -bool WgRenderer::beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity) { +bool WgRenderer::beginComposite(TVG_UNUSED Compositor* cmp, TVG_UNUSED CompositeMethod method, TVG_UNUSED uint8_t opacity) +{ return false; } -bool WgRenderer::endComposite(Compositor* cmp) { + +bool WgRenderer::endComposite(TVG_UNUSED Compositor* cmp) +{ return false; } -WgRenderer* WgRenderer::gen() { + +WgRenderer* WgRenderer::gen() +{ return new WgRenderer(); } -bool WgRenderer::init(uint32_t threads) { + +bool WgRenderer::init(TVG_UNUSED uint32_t threads) +{ return true; } -bool WgRenderer::term() { + +bool WgRenderer::term() +{ return true; } diff --git a/src/renderer/wg_engine/tvgWgRenderer.h b/src/renderer/wg_engine/tvgWgRenderer.h index a0a521e0..c2ba83c1 100644 --- a/src/renderer/wg_engine/tvgWgRenderer.h +++ b/src/renderer/wg_engine/tvgWgRenderer.h @@ -30,7 +30,6 @@ class WgRenderer : public RenderMethod private: WgRenderer(); ~WgRenderer(); -private: void initialize(); void release(); public: @@ -56,13 +55,14 @@ public: Compositor* target(const RenderRegion& region, ColorSpace cs); bool beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity); bool endComposite(Compositor* cmp); -public: + static WgRenderer* gen(); static bool init(uint32_t threads); static bool term(); + private: Array mRenderDatas{}; -private: + Surface mTargetSurface = { nullptr, 0, 0, 0, ColorSpace::Unsupported, true }; float mViewMatrix[16]{}; // basic webgpu instances (TODO: create separated entity) @@ -78,7 +78,6 @@ private: WgBindGroupCanvas mBindGroupCanvasWnd; WgBindGroupPaint mBindGroupPaintWnd; WgGeometryData mGeometryDataWnd; -private: WgPipelines mPipelines; bool mClearBuffer; diff --git a/src/renderer/wg_engine/tvgWgShaderTypes.cpp b/src/renderer/wg_engine/tvgWgShaderTypes.cpp index 16411694..f72c1210 100644 --- a/src/renderer/wg_engine/tvgWgShaderTypes.cpp +++ b/src/renderer/wg_engine/tvgWgShaderTypes.cpp @@ -26,26 +26,35 @@ // shader types /////////////////////////////////////////////////////////////////////////////// -WgShaderTypeMat4x4f::WgShaderTypeMat4x4f() { +WgShaderTypeMat4x4f::WgShaderTypeMat4x4f() +{ identity(); } -WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(const RenderTransform* transform) { + +WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(const RenderTransform* transform) +{ update(transform); } -void WgShaderTypeMat4x4f::identity() { + +void WgShaderTypeMat4x4f::identity() +{ mat[0] = 1.0f; mat[1] = 0.0f; mat[2] = 0.0f; mat[3] = 0.0f; mat[4] = 0.0f; mat[5] = 1.0f; mat[6] = 0.0f; mat[7] = 0.0f; mat[8] = 0.0f; mat[9] = 0.0f; mat[10] = 1.0f; mat[11] = 0.0f; mat[12] = 0.0f; mat[13] = 0.0f; mat[14] = 0.0f; mat[15] = 1.0f; } -WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(size_t w, size_t h) { + +WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(size_t w, size_t h) +{ update(w, h); } -void WgShaderTypeMat4x4f::update(const RenderTransform* transform) { + +void WgShaderTypeMat4x4f::update(const RenderTransform* transform) +{ identity(); if (transform) { mat[0] = transform->m.e11; @@ -65,42 +74,56 @@ void WgShaderTypeMat4x4f::update(const RenderTransform* transform) { mat[14] = 0.0f; mat[15] = transform->m.e33; }; -}; +} -void WgShaderTypeMat4x4f::update(size_t w, size_t h) { + +void WgShaderTypeMat4x4f::update(size_t w, size_t h) +{ mat[0] = +2.0f / w; mat[1] = +0.0f; mat[2] = +0.0f; mat[3] = +0.0f; mat[4] = +0.0f; mat[5] = -2.0f / h; mat[6] = +0.0f; mat[7] = +0.0f; mat[8] = +0.0f; mat[9] = +0.0f; mat[10] = -1.0f; mat[11] = +0.0f; mat[12] = -1.0f; mat[13] = +1.0f; mat[14] = +0.0f; mat[15] = +1.0f; -}; +} -WgShaderTypeBlendSettings::WgShaderTypeBlendSettings(const ColorSpace colorSpace) { + +WgShaderTypeBlendSettings::WgShaderTypeBlendSettings(const ColorSpace colorSpace) +{ update(colorSpace); } -void WgShaderTypeBlendSettings::update(const ColorSpace colorSpace) { + +void WgShaderTypeBlendSettings::update(const ColorSpace colorSpace) +{ format = (uint32_t)colorSpace; dummy0 = 0.0f; dummy1 = 0.0f; dummy2 = 0.0f; -}; +} -WgShaderTypeSolidColor::WgShaderTypeSolidColor(const uint8_t* c) { + +WgShaderTypeSolidColor::WgShaderTypeSolidColor(const uint8_t* c) +{ update(c); } -void WgShaderTypeSolidColor::update(const uint8_t* c) { + +void WgShaderTypeSolidColor::update(const uint8_t* c) +{ color[0] = c[0] / 255.0f; // red color[1] = c[1] / 255.0f; // green color[2] = c[2] / 255.0f; // blue color[3] = c[3] / 255.0f; // alpha -}; +} -WgShaderTypeLinearGradient::WgShaderTypeLinearGradient(const LinearGradient* linearGradient) { + +WgShaderTypeLinearGradient::WgShaderTypeLinearGradient(const LinearGradient* linearGradient) +{ update(linearGradient); } -void WgShaderTypeLinearGradient::update(const LinearGradient* linearGradient) { + +void WgShaderTypeLinearGradient::update(const LinearGradient* linearGradient) +{ const Fill::ColorStop* stops = nullptr; auto stopCnt = linearGradient->colorStops(&stops); @@ -116,13 +139,17 @@ void WgShaderTypeLinearGradient::update(const LinearGradient* linearGradient) { } linearGradient->linear(&startPos[0], &startPos[1], &endPos[0], &endPos[1]); -}; +} -WgShaderTypeRadialGradient::WgShaderTypeRadialGradient(const RadialGradient* radialGradient) { + +WgShaderTypeRadialGradient::WgShaderTypeRadialGradient(const RadialGradient* radialGradient) +{ update(radialGradient); } -void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient) { + +void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient) +{ const Fill::ColorStop* stops = nullptr; auto stopCnt = radialGradient->colorStops(&stops); @@ -138,4 +165,4 @@ void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient) { } radialGradient->radial(¢erPos[0], ¢erPos[1], &radius[0]); -}; +} diff --git a/src/renderer/wg_engine/tvgWgShaderTypes.h b/src/renderer/wg_engine/tvgWgShaderTypes.h index 3872aff9..dd1551e2 100644 --- a/src/renderer/wg_engine/tvgWgShaderTypes.h +++ b/src/renderer/wg_engine/tvgWgShaderTypes.h @@ -30,7 +30,8 @@ /////////////////////////////////////////////////////////////////////////////// // mat4x4f -struct WgShaderTypeMat4x4f { +struct WgShaderTypeMat4x4f +{ float mat[16]{}; WgShaderTypeMat4x4f(); @@ -47,7 +48,8 @@ struct WgShaderTypeMat4x4f { // dummy1 : f32, // opacity : f32 // }; -struct WgShaderTypeBlendSettings { +struct WgShaderTypeBlendSettings +{ uint32_t format{}; // ColorSpace float dummy0{}; float dummy1{}; @@ -61,7 +63,8 @@ struct WgShaderTypeBlendSettings { // struct SolidColor { // color: vec4f // }; -struct WgShaderTypeSolidColor { +struct WgShaderTypeSolidColor +{ float color[4]{}; WgShaderTypeSolidColor(const uint8_t* c); @@ -77,7 +80,8 @@ struct WgShaderTypeSolidColor { // stopColors : array // }; #define MAX_LINEAR_GRADIENT_STOPS 4 -struct WgShaderTypeLinearGradient { +struct WgShaderTypeLinearGradient +{ alignas(16) float nStops[4]{}; alignas(16) float startPos[2]{}; alignas(8) float endPos[2]{}; @@ -97,7 +101,8 @@ struct WgShaderTypeLinearGradient { // stopColors : array // }; #define MAX_RADIAL_GRADIENT_STOPS 4 -struct WgShaderTypeRadialGradient { +struct WgShaderTypeRadialGradient +{ alignas(16) float nStops[4]{}; alignas(16) float centerPos[2]{}; alignas(8) float radius[2]{};