wg_engine: apply tvg coding style.

This commit is contained in:
Hermet Park 2023-12-18 23:07:39 +09:00 committed by Hermet Park
parent e2458570f8
commit 9d06308207
14 changed files with 541 additions and 282 deletions

View file

@ -22,8 +22,18 @@
#include "tvgWgBindGroups.h" #include "tvgWgBindGroups.h"
WGPUBindGroupLayout WgBindGroupCanvas::layout = nullptr; 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; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntryBuffer(0) makeBindGroupLayoutEntryBuffer(0)
@ -33,11 +43,15 @@ WGPUBindGroupLayout WgBindGroupCanvas::getLayout(WGPUDevice device) {
return layout; 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(); release();
uBufferViewMat = createBuffer(device, queue, &uViewMat, sizeof(uViewMat)); uBufferViewMat = createBuffer(device, queue, &uViewMat, sizeof(uViewMat));
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
@ -47,13 +61,16 @@ void WgBindGroupCanvas::initialize(WGPUDevice device, WGPUQueue queue, WgShaderT
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupCanvas::release() {
void WgBindGroupCanvas::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
releaseBuffer(uBufferViewMat); releaseBuffer(uBufferViewMat);
} }
WGPUBindGroupLayout WgBindGroupPaint::layout = nullptr;
WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntryBuffer(0), makeBindGroupLayoutEntryBuffer(0),
@ -64,11 +81,15 @@ WGPUBindGroupLayout WgBindGroupPaint::getLayout(WGPUDevice device) {
return layout; 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(); release();
uBufferModelMat = createBuffer(device, queue, &uModelMat, sizeof(uModelMat)); uBufferModelMat = createBuffer(device, queue, &uModelMat, sizeof(uModelMat));
uBufferBlendSettings = createBuffer(device, queue, &uBlendSettings, sizeof(uBlendSettings)); uBufferBlendSettings = createBuffer(device, queue, &uBlendSettings, sizeof(uBlendSettings));
@ -80,14 +101,17 @@ void WgBindGroupPaint::initialize(WGPUDevice device, WGPUQueue queue, WgShaderTy
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupPaint::release() {
void WgBindGroupPaint::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
releaseBuffer(uBufferBlendSettings); releaseBuffer(uBufferBlendSettings);
releaseBuffer(uBufferModelMat); releaseBuffer(uBufferModelMat);
} }
WGPUBindGroupLayout WgBindGroupSolidColor::layout = nullptr;
WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntryBuffer(0) makeBindGroupLayoutEntryBuffer(0)
@ -97,11 +121,15 @@ WGPUBindGroupLayout WgBindGroupSolidColor::getLayout(WGPUDevice device) {
return layout; 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(); release();
uBufferSolidColor = createBuffer(device, queue, &uSolidColor, sizeof(uSolidColor)); uBufferSolidColor = createBuffer(device, queue, &uSolidColor, sizeof(uSolidColor));
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
@ -111,13 +139,16 @@ void WgBindGroupSolidColor::initialize(WGPUDevice device, WGPUQueue queue, WgSha
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupSolidColor::release() {
void WgBindGroupSolidColor::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
releaseBuffer(uBufferSolidColor); releaseBuffer(uBufferSolidColor);
} }
WGPUBindGroupLayout WgBindGroupLinearGradient::layout = nullptr;
WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntryBuffer(0) makeBindGroupLayoutEntryBuffer(0)
@ -127,11 +158,15 @@ WGPUBindGroupLayout WgBindGroupLinearGradient::getLayout(WGPUDevice device) {
return layout; 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(); release();
uBufferLinearGradient = createBuffer(device, queue, &uLinearGradient, sizeof(uLinearGradient)); uBufferLinearGradient = createBuffer(device, queue, &uLinearGradient, sizeof(uLinearGradient));
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
@ -141,13 +176,16 @@ void WgBindGroupLinearGradient::initialize(WGPUDevice device, WGPUQueue queue, W
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupLinearGradient::release() {
void WgBindGroupLinearGradient::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
releaseBuffer(uBufferLinearGradient); releaseBuffer(uBufferLinearGradient);
} }
WGPUBindGroupLayout WgBindGroupRadialGradient::layout = nullptr;
WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntryBuffer(0) makeBindGroupLayoutEntryBuffer(0)
@ -157,11 +195,15 @@ WGPUBindGroupLayout WgBindGroupRadialGradient::getLayout(WGPUDevice device) {
return layout; 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(); release();
uBufferRadialGradient = createBuffer(device, queue, &uRadialGradient, sizeof(uRadialGradient)); uBufferRadialGradient = createBuffer(device, queue, &uRadialGradient, sizeof(uRadialGradient));
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
@ -171,13 +213,16 @@ void WgBindGroupRadialGradient::initialize(WGPUDevice device, WGPUQueue queue, W
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupRadialGradient::release() {
void WgBindGroupRadialGradient::release()
{
releaseBuffer(uBufferRadialGradient); releaseBuffer(uBufferRadialGradient);
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
} }
WGPUBindGroupLayout WgBindGroupPicture::layout = nullptr;
WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntrySampler(0), makeBindGroupLayoutEntrySampler(0),
@ -188,11 +233,15 @@ WGPUBindGroupLayout WgBindGroupPicture::getLayout(WGPUDevice device) {
return layout; 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(); release();
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
makeBindGroupEntrySampler(0, uSampler), makeBindGroupEntrySampler(0, uSampler),
@ -202,12 +251,15 @@ void WgBindGroupPicture::initialize(WGPUDevice device, WGPUQueue queue, WGPUSamp
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupPicture::release() {
void WgBindGroupPicture::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
} }
WGPUBindGroupLayout WgBindGroupCompose::layout = nullptr;
WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device) { WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device)
{
if (layout) return layout; if (layout) return layout;
const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] { const WGPUBindGroupLayoutEntry bindGroupLayoutEntries[] {
makeBindGroupLayoutEntrySampler(0), makeBindGroupLayoutEntrySampler(0),
@ -219,11 +271,15 @@ WGPUBindGroupLayout WgBindGroupCompose::getLayout(WGPUDevice device) {
return layout; 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(); release();
const WGPUBindGroupEntry bindGroupEntries[] { const WGPUBindGroupEntry bindGroupEntries[] {
makeBindGroupEntrySampler(0, uSampler), makeBindGroupEntrySampler(0, uSampler),
@ -234,6 +290,8 @@ void WgBindGroupCompose::initialize(WGPUDevice device, WGPUQueue queue, WGPUSamp
assert(mBindGroup); assert(mBindGroup);
} }
void WgBindGroupCompose::release() {
void WgBindGroupCompose::release()
{
releaseBindGroup(mBindGroup); releaseBindGroup(mBindGroup);
} }

View file

@ -27,7 +27,8 @@
#include "tvgWgShaderTypes.h" #include "tvgWgShaderTypes.h"
// @group(0) // @group(0)
struct WgBindGroupCanvas: public WgBindGroup { struct WgBindGroupCanvas : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -38,8 +39,10 @@ struct WgBindGroupCanvas: public WgBindGroup {
void release(); void release();
}; };
// @group(1) // @group(1)
struct WgBindGroupPaint: public WgBindGroup { struct WgBindGroupPaint : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -53,7 +56,8 @@ struct WgBindGroupPaint: public WgBindGroup {
}; };
// @group(2) // @group(2)
struct WgBindGroupSolidColor: public WgBindGroup { struct WgBindGroupSolidColor : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -65,7 +69,8 @@ struct WgBindGroupSolidColor: public WgBindGroup {
}; };
// @group(2) // @group(2)
struct WgBindGroupLinearGradient: public WgBindGroup { struct WgBindGroupLinearGradient : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -77,7 +82,8 @@ struct WgBindGroupLinearGradient: public WgBindGroup {
}; };
// @group(2) // @group(2)
struct WgBindGroupRadialGradient: public WgBindGroup { struct WgBindGroupRadialGradient : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -89,7 +95,8 @@ struct WgBindGroupRadialGradient: public WgBindGroup {
}; };
// @group(2) // @group(2)
struct WgBindGroupPicture: public WgBindGroup { struct WgBindGroupPicture : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();
@ -101,7 +108,8 @@ struct WgBindGroupPicture: public WgBindGroup {
}; };
// @group(2) // @group(2)
struct WgBindGroupCompose: public WgBindGroup { struct WgBindGroupCompose : public WgBindGroup
{
static WGPUBindGroupLayout layout; static WGPUBindGroupLayout layout;
static WGPUBindGroupLayout getLayout(WGPUDevice device); static WGPUBindGroupLayout getLayout(WGPUDevice device);
static void releaseLayout(); static void releaseLayout();

View file

@ -26,11 +26,14 @@
// bind group // bind group
//***************************************************************************** //*****************************************************************************
void WgBindGroup::set(WGPURenderPassEncoder encoder, uint32_t groupIndex) { void WgBindGroup::set(WGPURenderPassEncoder encoder, uint32_t groupIndex)
{
wgpuRenderPassEncoderSetBindGroup(encoder, groupIndex, mBindGroup, 0, nullptr); wgpuRenderPassEncoderSetBindGroup(encoder, groupIndex, mBindGroup, 0, nullptr);
} }
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUBuffer buffer) {
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUBuffer buffer)
{
WGPUBindGroupEntry bindGroupEntry{}; WGPUBindGroupEntry bindGroupEntry{};
bindGroupEntry.nextInChain = nullptr; bindGroupEntry.nextInChain = nullptr;
bindGroupEntry.binding = binding; bindGroupEntry.binding = binding;
@ -42,7 +45,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryBuffer(uint32_t binding, WGPUB
return bindGroupEntry; return bindGroupEntry;
} }
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPUSampler sampler) {
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPUSampler sampler)
{
WGPUBindGroupEntry bindGroupEntry{}; WGPUBindGroupEntry bindGroupEntry{};
bindGroupEntry.nextInChain = nullptr; bindGroupEntry.nextInChain = nullptr;
bindGroupEntry.binding = binding; bindGroupEntry.binding = binding;
@ -54,7 +59,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntrySampler(uint32_t binding, WGPU
return bindGroupEntry; return bindGroupEntry;
} }
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding, WGPUTextureView textureView) {
WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding, WGPUTextureView textureView)
{
WGPUBindGroupEntry bindGroupEntry{}; WGPUBindGroupEntry bindGroupEntry{};
bindGroupEntry.nextInChain = nullptr; bindGroupEntry.nextInChain = nullptr;
bindGroupEntry.binding = binding; bindGroupEntry.binding = binding;
@ -66,7 +73,9 @@ WGPUBindGroupEntry WgBindGroup::makeBindGroupEntryTextureView(uint32_t binding,
return bindGroupEntry; return bindGroupEntry;
} }
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t binding) {
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t binding)
{
WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; WGPUBindGroupLayoutEntry bindGroupLayoutEntry{};
bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.nextInChain = nullptr;
bindGroupLayoutEntry.binding = binding; bindGroupLayoutEntry.binding = binding;
@ -78,7 +87,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryBuffer(uint32_t bi
return bindGroupLayoutEntry; return bindGroupLayoutEntry;
} }
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t binding) {
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t binding)
{
WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; WGPUBindGroupLayoutEntry bindGroupLayoutEntry{};
bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.nextInChain = nullptr;
bindGroupLayoutEntry.binding = binding; bindGroupLayoutEntry.binding = binding;
@ -88,7 +99,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntrySampler(uint32_t b
return bindGroupLayoutEntry; return bindGroupLayoutEntry;
} }
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32_t binding) {
WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32_t binding)
{
WGPUBindGroupLayoutEntry bindGroupLayoutEntry{}; WGPUBindGroupLayoutEntry bindGroupLayoutEntry{};
bindGroupLayoutEntry.nextInChain = nullptr; bindGroupLayoutEntry.nextInChain = nullptr;
bindGroupLayoutEntry.binding = binding; bindGroupLayoutEntry.binding = binding;
@ -100,7 +113,9 @@ WGPUBindGroupLayoutEntry WgBindGroup::makeBindGroupLayoutEntryTextureView(uint32
return bindGroupLayoutEntry; 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{}; WGPUBufferDescriptor bufferDescriptor{};
bufferDescriptor.nextInChain = nullptr; bufferDescriptor.nextInChain = nullptr;
bufferDescriptor.label = "The uniform buffer"; bufferDescriptor.label = "The uniform buffer";
@ -113,7 +128,9 @@ WGPUBuffer WgBindGroup::createBuffer(WGPUDevice device, WGPUQueue queue, const v
return buffer; 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{}; WGPUBindGroupDescriptor bindGroupDesc{};
bindGroupDesc.nextInChain = nullptr; bindGroupDesc.nextInChain = nullptr;
bindGroupDesc.label = "The binding group sampler"; bindGroupDesc.label = "The binding group sampler";
@ -123,7 +140,9 @@ WGPUBindGroup WgBindGroup::createBindGroup(WGPUDevice device, WGPUBindGroupLayou
return wgpuDeviceCreateBindGroup(device, &bindGroupDesc); 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{}; WGPUBindGroupLayoutDescriptor bindGroupLayoutDesc{};
bindGroupLayoutDesc.nextInChain = nullptr; bindGroupLayoutDesc.nextInChain = nullptr;
bindGroupLayoutDesc.label = "The bind group layout"; bindGroupLayoutDesc.label = "The bind group layout";
@ -132,7 +151,9 @@ WGPUBindGroupLayout WgBindGroup::createBindGroupLayout(WGPUDevice device, const
return wgpuDeviceCreateBindGroupLayout(device, &bindGroupLayoutDesc); return wgpuDeviceCreateBindGroupLayout(device, &bindGroupLayoutDesc);
} }
void WgBindGroup::releaseBuffer(WGPUBuffer& buffer) {
void WgBindGroup::releaseBuffer(WGPUBuffer& buffer)
{
if (buffer) { if (buffer) {
wgpuBufferDestroy(buffer); wgpuBufferDestroy(buffer);
wgpuBufferRelease(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); if (bindGroup) wgpuBindGroupRelease(bindGroup);
bindGroup = nullptr; bindGroup = nullptr;
} }
void WgBindGroup::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout) {
void WgBindGroup::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout)
{
if (bindGroupLayout) wgpuBindGroupLayoutRelease(bindGroupLayout); if (bindGroupLayout) wgpuBindGroupLayoutRelease(bindGroupLayout);
bindGroupLayout = nullptr; bindGroupLayout = nullptr;
} }
@ -167,23 +192,29 @@ void WgPipeline::allocate(WGPUDevice device,
assert(mPipelineLayout); assert(mPipelineLayout);
mRenderPipeline = createRenderPipeline(device, mRenderPipeline = createRenderPipeline(device,
vertexBufferLayouts, attribsCount, vertexBufferLayouts, attribsCount,
stencilCompareFunction, stencilOperation, stencilCompareFunction, stencilOperation,
mPipelineLayout, mShaderModule, pipelineLabel); mPipelineLayout, mShaderModule, pipelineLabel);
assert(mRenderPipeline); assert(mRenderPipeline);
} }
void WgPipeline::release() {
void WgPipeline::release()
{
destroyRenderPipeline(mRenderPipeline); destroyRenderPipeline(mRenderPipeline);
destroyShaderModule(mShaderModule); destroyShaderModule(mShaderModule);
destroyPipelineLayout(mPipelineLayout); destroyPipelineLayout(mPipelineLayout);
} }
void WgPipeline::set(WGPURenderPassEncoder renderPassEncoder) {
void WgPipeline::set(WGPURenderPassEncoder renderPassEncoder)
{
wgpuRenderPassEncoderSetPipeline(renderPassEncoder, mRenderPipeline); wgpuRenderPassEncoderSetPipeline(renderPassEncoder, mRenderPipeline);
}; };
WGPUBlendState WgPipeline::makeBlendState() {
WGPUBlendState WgPipeline::makeBlendState()
{
WGPUBlendState blendState{}; WGPUBlendState blendState{};
blendState.color.operation = WGPUBlendOperation_Add; blendState.color.operation = WGPUBlendOperation_Add;
blendState.color.srcFactor = WGPUBlendFactor_SrcAlpha; blendState.color.srcFactor = WGPUBlendFactor_SrcAlpha;
@ -194,7 +225,9 @@ WGPUBlendState WgPipeline::makeBlendState() {
return blendState; return blendState;
} }
WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blendState) {
WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blendState)
{
WGPUColorTargetState colorTargetState{}; WGPUColorTargetState colorTargetState{};
colorTargetState.nextInChain = nullptr; colorTargetState.nextInChain = nullptr;
colorTargetState.format = WGPUTextureFormat_BGRA8Unorm; // (WGPUTextureFormat_BGRA8UnormSrgb) colorTargetState.format = WGPUTextureFormat_BGRA8Unorm; // (WGPUTextureFormat_BGRA8UnormSrgb)
@ -203,7 +236,9 @@ WGPUColorTargetState WgPipeline::makeColorTargetState(const WGPUBlendState* blen
return colorTargetState; 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{}; WGPUVertexBufferLayout vertexBufferLayoutPos{};
vertexBufferLayoutPos.arrayStride = stride; vertexBufferLayoutPos.arrayStride = stride;
vertexBufferLayoutPos.stepMode = WGPUVertexStepMode_Vertex; vertexBufferLayoutPos.stepMode = WGPUVertexStepMode_Vertex;
@ -212,7 +247,9 @@ WGPUVertexBufferLayout WgPipeline::makeVertexBufferLayout(const WGPUVertexAttrib
return vertexBufferLayoutPos; 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{}; WGPUVertexState vertexState{};
vertexState.nextInChain = nullptr; vertexState.nextInChain = nullptr;
vertexState.module = shaderModule; vertexState.module = shaderModule;
@ -224,7 +261,9 @@ WGPUVertexState WgPipeline::makeVertexState(WGPUShaderModule shaderModule, const
return vertexState; return vertexState;
} }
WGPUPrimitiveState WgPipeline::makePrimitiveState() {
WGPUPrimitiveState WgPipeline::makePrimitiveState()
{
WGPUPrimitiveState primitiveState{}; WGPUPrimitiveState primitiveState{};
primitiveState.nextInChain = nullptr; primitiveState.nextInChain = nullptr;
primitiveState.topology = WGPUPrimitiveTopology_TriangleList; primitiveState.topology = WGPUPrimitiveTopology_TriangleList;
@ -234,7 +273,8 @@ WGPUPrimitiveState WgPipeline::makePrimitiveState() {
return primitiveState; return primitiveState;
} }
WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction compare, WGPUStencilOperation operation) { WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction compare, WGPUStencilOperation operation)
{
WGPUDepthStencilState depthStencilState{}; WGPUDepthStencilState depthStencilState{};
depthStencilState.nextInChain = nullptr; depthStencilState.nextInChain = nullptr;
depthStencilState.format = WGPUTextureFormat_Stencil8; depthStencilState.format = WGPUTextureFormat_Stencil8;
@ -256,7 +296,9 @@ WGPUDepthStencilState WgPipeline::makeDepthStencilState(WGPUCompareFunction comp
return depthStencilState; return depthStencilState;
} }
WGPUMultisampleState WgPipeline::makeMultisampleState() {
WGPUMultisampleState WgPipeline::makeMultisampleState()
{
WGPUMultisampleState multisampleState{}; WGPUMultisampleState multisampleState{};
multisampleState.nextInChain = nullptr; multisampleState.nextInChain = nullptr;
multisampleState.count = 1; multisampleState.count = 1;
@ -265,7 +307,9 @@ WGPUMultisampleState WgPipeline::makeMultisampleState() {
return multisampleState; return multisampleState;
} }
WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, WGPUColorTargetState* targets, uint32_t size) {
WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, WGPUColorTargetState* targets, uint32_t size)
{
WGPUFragmentState fragmentState{}; WGPUFragmentState fragmentState{};
fragmentState.nextInChain = nullptr; fragmentState.nextInChain = nullptr;
fragmentState.module = shaderModule; fragmentState.module = shaderModule;
@ -277,7 +321,9 @@ WGPUFragmentState WgPipeline::makeFragmentState(WGPUShaderModule shaderModule, W
return fragmentState; 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{}; WGPUPipelineLayoutDescriptor pipelineLayoutDesc{};
pipelineLayoutDesc.nextInChain = nullptr; pipelineLayoutDesc.nextInChain = nullptr;
pipelineLayoutDesc.label = "The Pipeline layout"; pipelineLayoutDesc.label = "The Pipeline layout";
@ -286,7 +332,9 @@ WGPUPipelineLayout WgPipeline::createPipelineLayout(WGPUDevice device, const WGP
return wgpuDeviceCreatePipelineLayout(device, &pipelineLayoutDesc); 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{}; WGPUShaderModuleWGSLDescriptor shaderModuleWGSLDesc{};
shaderModuleWGSLDesc.chain.next = nullptr; shaderModuleWGSLDesc.chain.next = nullptr;
shaderModuleWGSLDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; shaderModuleWGSLDesc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
@ -299,11 +347,13 @@ WGPUShaderModule WgPipeline::createShaderModule(WGPUDevice device, const char* c
return wgpuDeviceCreateShaderModule(device, &shaderModuleDesc); return wgpuDeviceCreateShaderModule(device, &shaderModuleDesc);
} }
WGPURenderPipeline WgPipeline::createRenderPipeline(WGPUDevice device, WGPURenderPipeline WgPipeline::createRenderPipeline(WGPUDevice device,
WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount,
WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation,
WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule,
const char* pipelineName) { const char* pipelineName)
{
WGPUBlendState blendState = makeBlendState(); WGPUBlendState blendState = makeBlendState();
WGPUColorTargetState colorTargetStates[] = { WGPUColorTargetState colorTargetStates[] = {
makeColorTargetState(&blendState) makeColorTargetState(&blendState)
@ -327,17 +377,23 @@ WGPURenderPipeline WgPipeline::createRenderPipeline(WGPUDevice device,
return wgpuDeviceCreateRenderPipeline(device, &renderPipelineDesc); return wgpuDeviceCreateRenderPipeline(device, &renderPipelineDesc);
} }
void WgPipeline::destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout) {
void WgPipeline::destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout)
{
if (pipelineLayout) wgpuPipelineLayoutRelease(pipelineLayout); if (pipelineLayout) wgpuPipelineLayoutRelease(pipelineLayout);
pipelineLayout = nullptr; pipelineLayout = nullptr;
} }
void WgPipeline::destroyShaderModule(WGPUShaderModule& shaderModule) {
void WgPipeline::destroyShaderModule(WGPUShaderModule& shaderModule)
{
if (shaderModule) wgpuShaderModuleRelease(shaderModule); if (shaderModule) wgpuShaderModuleRelease(shaderModule);
shaderModule = nullptr; shaderModule = nullptr;
} }
void WgPipeline::destroyRenderPipeline(WGPURenderPipeline& renderPipeline) {
void WgPipeline::destroyRenderPipeline(WGPURenderPipeline& renderPipeline)
{
if (renderPipeline) wgpuRenderPipelineRelease(renderPipeline); if (renderPipeline) wgpuRenderPipelineRelease(renderPipeline);
renderPipeline = nullptr; renderPipeline = nullptr;
} }

View file

@ -28,7 +28,8 @@
#include "tvgCommon.h" #include "tvgCommon.h"
#include "tvgRender.h" #include "tvgRender.h"
struct WgBindGroup { struct WgBindGroup
{
WGPUBindGroup mBindGroup{}; WGPUBindGroup mBindGroup{};
void set(WGPURenderPassEncoder encoder, uint32_t groupIndex); void set(WGPURenderPassEncoder encoder, uint32_t groupIndex);
@ -50,7 +51,8 @@ struct WgBindGroup {
static void releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout); static void releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout);
}; };
struct WgPipeline { struct WgPipeline
{
protected: protected:
void allocate(WGPUDevice device, void allocate(WGPUDevice device,
WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount,
@ -76,12 +78,11 @@ public:
static WGPUPipelineLayout createPipelineLayout(WGPUDevice device, const WGPUBindGroupLayout* bindGroupLayouts, uint32_t count); static WGPUPipelineLayout createPipelineLayout(WGPUDevice device, const WGPUBindGroupLayout* bindGroupLayouts, uint32_t count);
static WGPUShaderModule createShaderModule(WGPUDevice device, const char* code, const char* label); static WGPUShaderModule createShaderModule(WGPUDevice device, const char* code, const char* label);
static WGPURenderPipeline createRenderPipeline( static WGPURenderPipeline createRenderPipeline(WGPUDevice device,
WGPUDevice device, WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount,
WGPUVertexBufferLayout vertexBufferLayouts[], uint32_t attribsCount, WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation,
WGPUCompareFunction stencilCompareFunction, WGPUStencilOperation stencilOperation, WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule,
WGPUPipelineLayout pipelineLayout, WGPUShaderModule shaderModule, const char* pipelineLabel);
const char* pipelineLabel);
static void destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout); static void destroyPipelineLayout(WGPUPipelineLayout& pipelineLayout);
static void destroyShaderModule(WGPUShaderModule& shaderModule); static void destroyShaderModule(WGPUShaderModule& shaderModule);
static void destroyRenderPipeline(WGPURenderPipeline& renderPipeline); static void destroyRenderPipeline(WGPURenderPipeline& renderPipeline);

View file

@ -22,7 +22,8 @@
#include "tvgWgGeometry.h" #include "tvgWgGeometry.h"
void WgVertexList::computeTriFansIndexes() { void WgVertexList::computeTriFansIndexes()
{
assert(mVertexList.count > 2); assert(mVertexList.count > 2);
mIndexList.reserve((mVertexList.count - 2) * 3); mIndexList.reserve((mVertexList.count - 2) * 3);
for (size_t i = 0; i < mVertexList.count - 2; i++) { 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); WgPoint p0 = mVertexList.count > 0 ? mVertexList.last() : WgPoint(0.0f, 0.0f);
const size_t segs = 16; const size_t segs = 16;
for (size_t i = 1; i <= segs; i++) { 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; uint32_t index = mVertexList.count;
mVertexList.push(p0); // +0 mVertexList.push(p0); // +0
mVertexList.push(p1); // +1 mVertexList.push(p1); // +1
@ -60,8 +65,10 @@ void WgVertexList::appendRect(WgPoint p0, WgPoint p1, WgPoint p2, WgPoint p3) {
mIndexList.push(index + 2); mIndexList.push(index + 2);
} }
// TODO: optimize vertex and index count // 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 index = mVertexList.count;
uint32_t nSegments = 32; uint32_t nSegments = 32;
for (uint32_t i = 0; i < nSegments; i++) { 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]); mVertexList.push(mVertexList[0]);
}
} }

View file

@ -34,7 +34,6 @@ public:
float x; float x;
float y; float y;
public:
WgPoint() {} WgPoint() {}
WgPoint(float x, float y): x(x), y(y) {} WgPoint(float x, float y): x(x), y(y) {}
WgPoint(const Point& p): x(p.x), y(p.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 length() const { return sqrt(x*x + y*y); }
float dot(const WgPoint& p) const { return x * p.x + y * p.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( return sqrt(
(p.x - x)*(p.x - x) + (p.x - x)*(p.x - x) +
(p.y - y)*(p.y - y) (p.y - y)*(p.y - y)
); );
} }
void normalize() { void normalize()
{
float rlen = 1.0f / length(); float rlen = 1.0f / length();
x *= rlen; x *= rlen;
y *= rlen; y *= rlen;
} }
WgPoint normal() const { WgPoint normal() const
{
float rlen = 1.0f / length(); float rlen = 1.0f / length();
return { x * rlen, y * rlen }; return { x * rlen, y * rlen };
} }
}; };
class WgVertexList { class WgVertexList
{
public: public:
Array<WgPoint> mVertexList; Array<WgPoint> mVertexList;
Array<uint32_t> mIndexList; Array<uint32_t> mIndexList;

View file

@ -25,7 +25,8 @@
#define ARRAY_ELEMENTS_COUNT(arr) sizeof(arr)/sizeof(arr[0]) #define ARRAY_ELEMENTS_COUNT(arr) sizeof(arr)/sizeof(arr[0])
void WgPipelineFillShape::initialize(WGPUDevice device) { void WgPipelineFillShape::initialize(WGPUDevice device)
{
// vertex attributes settings // vertex attributes settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexBufferLayout vertexBufferLayouts[] = { WGPUVertexBufferLayout vertexBufferLayouts[] = {
@ -43,20 +44,21 @@ void WgPipelineFillShape::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Invert; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Invert;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineFill; auto shaderSource = cShaderSource_PipelineFill;
const char* shaderLabel = "The shader fill"; auto shaderLabel = "The shader fill";
const char* pipelineLabel = "The render pipeline fill shape"; auto pipelineLabel = "The render pipeline fill shape";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
void WgPipelineFillStroke::initialize(WGPUDevice device) {
void WgPipelineFillStroke::initialize(WGPUDevice device)
{
// vertex and buffers settings // vertex and buffers settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexBufferLayout vertexBufferLayouts[] = { WGPUVertexBufferLayout vertexBufferLayouts[] = {
@ -74,20 +76,21 @@ void WgPipelineFillStroke::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Replace; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Replace;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineFill; auto shaderSource = cShaderSource_PipelineFill;
const char* shaderLabel = "The shader fill"; auto shaderLabel = "The shader fill";
const char* pipelineLabel = "The render pipeline fill stroke"; auto pipelineLabel = "The render pipeline fill stroke";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
void WgPipelineSolid::initialize(WGPUDevice device) {
void WgPipelineSolid::initialize(WGPUDevice device)
{
// vertex and buffers settings // vertex and buffers settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexBufferLayout vertexBufferLayouts[] = { WGPUVertexBufferLayout vertexBufferLayouts[] = {
@ -106,20 +109,21 @@ void WgPipelineSolid::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineSolid; auto shaderSource = cShaderSource_PipelineSolid;
const char* shaderLabel = "The shader solid color"; auto shaderLabel = "The shader solid color";
const char* pipelineLabel = "The render pipeline solid color"; auto pipelineLabel = "The render pipeline solid color";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
void WgPipelineLinear::initialize(WGPUDevice device) {
void WgPipelineLinear::initialize(WGPUDevice device)
{
// vertex and buffers settings // vertex and buffers settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexBufferLayout vertexBufferLayouts[] = { WGPUVertexBufferLayout vertexBufferLayouts[] = {
@ -138,20 +142,21 @@ void WgPipelineLinear::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineLinear; auto shaderSource = cShaderSource_PipelineLinear;
const char* shaderLabel = "The shader linear gradient"; auto shaderLabel = "The shader linear gradient";
const char* pipelineLabel = "The render pipeline linear gradient"; auto pipelineLabel = "The render pipeline linear gradient";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
void WgPipelineRadial::initialize(WGPUDevice device) {
void WgPipelineRadial::initialize(WGPUDevice device)
{
// vertex and buffers settings // vertex and buffers settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexBufferLayout vertexBufferLayouts[] = { WGPUVertexBufferLayout vertexBufferLayouts[] = {
@ -170,20 +175,21 @@ void WgPipelineRadial::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineRadial; auto shaderSource = cShaderSource_PipelineRadial;
const char* shaderLabel = "The shader radial gradient"; auto shaderLabel = "The shader radial gradient";
const char* pipelineLabel = "The render pipeline radial gradient"; auto pipelineLabel = "The render pipeline radial gradient";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
void WgPipelineImage::initialize(WGPUDevice device) {
void WgPipelineImage::initialize(WGPUDevice device)
{
// vertex and buffers settings // vertex and buffers settings
WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 }; WGPUVertexAttribute vertexAttributesPos = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 0 };
WGPUVertexAttribute vertexAttributesTex = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 1 }; WGPUVertexAttribute vertexAttributesTex = { WGPUVertexFormat_Float32x2, sizeof(float) * 0, 1 };
@ -204,24 +210,25 @@ void WgPipelineImage::initialize(WGPUDevice device) {
WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero; WGPUStencilOperation stencilOperation = WGPUStencilOperation_Zero;
// sheder source and labels // sheder source and labels
const char* shaderSource = cShaderSource_PipelineImage; auto shaderSource = cShaderSource_PipelineImage;
const char* shaderLabel = "The shader image"; auto shaderLabel = "The shader image";
const char* pipelineLabel = "The render pipeline image"; auto pipelineLabel = "The render pipeline image";
// allocate all pipeline handles // allocate all pipeline handles
allocate( allocate(device,
device, vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts),
vertexBufferLayouts, ARRAY_ELEMENTS_COUNT(vertexBufferLayouts), bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts),
bindGroupLayouts, ARRAY_ELEMENTS_COUNT(bindGroupLayouts), stencilFuncion, stencilOperation,
stencilFuncion, stencilOperation, shaderSource, shaderLabel, pipelineLabel);
shaderSource, shaderLabel, pipelineLabel); }
};
//************************************************************************ //************************************************************************
// pipelines // pipelines
//************************************************************************ //************************************************************************
void WgPipelines::initialize(WGPUDevice device) { void WgPipelines::initialize(WGPUDevice device)
{
mPipelineFillShape.initialize(device); mPipelineFillShape.initialize(device);
mPipelineFillStroke.initialize(device); mPipelineFillStroke.initialize(device);
mPipelineSolid.initialize(device); mPipelineSolid.initialize(device);
@ -230,7 +237,9 @@ void WgPipelines::initialize(WGPUDevice device) {
mPipelineImage.initialize(device); mPipelineImage.initialize(device);
} }
void WgPipelines::release() {
void WgPipelines::release()
{
WgBindGroupCompose::releaseLayout(); WgBindGroupCompose::releaseLayout();
WgBindGroupPicture::releaseLayout(); WgBindGroupPicture::releaseLayout();
WgBindGroupRadialGradient::releaseLayout(); WgBindGroupRadialGradient::releaseLayout();

View file

@ -25,34 +25,33 @@
#include "tvgWgBindGroups.h" #include "tvgWgBindGroups.h"
struct WgPipelineFillShape: public WgPipeline { struct WgPipelineFillShape: public WgPipeline
{
void initialize(WGPUDevice device) override; void initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); groupPaint.set(encoder, 1);
} }
}; };
struct WgPipelineFillStroke: public WgPipeline { struct WgPipelineFillStroke: public WgPipeline
{
void initialize(WGPUDevice device) override; void initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); groupPaint.set(encoder, 1);
} }
}; };
struct WgPipelineSolid: public WgPipeline { struct WgPipelineSolid: public WgPipeline
{
void initialize(WGPUDevice device) override; void initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas,WgBindGroupPaint& groupPaint, WgBindGroupSolidColor& groupSolid)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint,
WgBindGroupSolidColor& groupSolid) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); 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 initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupLinearGradient& groupLinear)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint,
WgBindGroupLinearGradient& groupLinear) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); 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 initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupRadialGradient& groupRadial)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint,
WgBindGroupRadialGradient& groupRadial) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); 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 initialize(WGPUDevice device) override;
void use(WGPURenderPassEncoder encoder, void use(WGPURenderPassEncoder encoder, WgBindGroupCanvas& groupCanvas, WgBindGroupPaint& groupPaint, WgBindGroupPicture& groupPicture)
WgBindGroupCanvas& groupCanvas, {
WgBindGroupPaint& groupPaint,
WgBindGroupPicture& groupPicture) {
set(encoder); set(encoder);
groupCanvas.set(encoder, 0); groupCanvas.set(encoder, 0);
groupPaint.set(encoder, 1); groupPaint.set(encoder, 1);
@ -99,7 +95,8 @@ struct WgPipelineImage: public WgPipeline {
} }
}; };
struct WgPipelines { struct WgPipelines
{
WgPipelineFillShape mPipelineFillShape; WgPipelineFillShape mPipelineFillShape;
WgPipelineFillStroke mPipelineFillStroke; WgPipelineFillStroke mPipelineFillStroke;
WgPipelineSolid mPipelineSolid; WgPipelineSolid mPipelineSolid;

View file

@ -26,20 +26,25 @@
// WgGeometryData // WgGeometryData
//*********************************************************************** //***********************************************************************
void WgGeometryData::draw(WGPURenderPassEncoder renderPassEncoder) { void WgGeometryData::draw(WGPURenderPassEncoder renderPassEncoder)
{
wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 0, mBufferVertex, 0, mVertexCount * sizeof(float) * 2); wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 0, mBufferVertex, 0, mVertexCount * sizeof(float) * 2);
wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t)); wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t));
wgpuRenderPassEncoderDrawIndexed(renderPassEncoder, mIndexCount, 1, 0, 0, 0); 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, 0, mBufferVertex, 0, mVertexCount * sizeof(float) * 2);
wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 1, mBufferTexCoords, 0, mVertexCount * sizeof(float) * 2); wgpuRenderPassEncoderSetVertexBuffer(renderPassEncoder, 1, mBufferTexCoords, 0, mVertexCount * sizeof(float) * 2);
wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t)); wgpuRenderPassEncoderSetIndexBuffer(renderPassEncoder, mBufferIndex, WGPUIndexFormat_Uint32, 0, mIndexCount * sizeof(uint32_t));
wgpuRenderPassEncoderDrawIndexed(renderPassEncoder, mIndexCount, 1, 0, 0, 0); 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, update(device, queue,
(float *)vertexList->mVertexList.data, (float *)vertexList->mVertexList.data,
vertexList->mVertexList.count, vertexList->mVertexList.count,
@ -47,7 +52,9 @@ void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, WgVertexList* ve
vertexList->mIndexList.count); 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(); release();
// buffer vertex data create and write // buffer vertex data create and write
@ -74,6 +81,7 @@ void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexDat
mIndexCount = indexCount; mIndexCount = indexCount;
} }
void WgGeometryData::update(WGPUDevice device, WGPUQueue queue, float* vertexData, float* texCoordsData, size_t vertexCount, uint32_t* indexData, size_t 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); update(device, queue, vertexData, vertexCount, indexData, indexCount);
// buffer tex coords data create and write // 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); wgpuQueueWriteBuffer(queue, mBufferTexCoords, 0, texCoordsData, vertexCount * sizeof(float) * 2);
} }
void WgGeometryData::release() {
void WgGeometryData::release()
{
if (mBufferIndex) { if (mBufferIndex) {
wgpuBufferDestroy(mBufferIndex); wgpuBufferDestroy(mBufferIndex);
wgpuBufferRelease(mBufferIndex); wgpuBufferRelease(mBufferIndex);
@ -112,7 +122,8 @@ void WgGeometryData::release() {
// WgImageData // WgImageData
//*********************************************************************** //***********************************************************************
void WgImageData::update(WGPUDevice device, WGPUQueue queue, Surface* surface) { void WgImageData::update(WGPUDevice device, WGPUQueue queue, Surface* surface)
{
release(); release();
// sampler descriptor // sampler descriptor
WGPUSamplerDescriptor samplerDesc{}; 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); wgpuQueueWriteTexture(queue, &imageCopyTexture, surface->data, 4 * surface->w * surface->h, &textureDataLayout, &writeSize);
} }
void WgImageData::release() {
void WgImageData::release()
{
if (mTexture) { if (mTexture) {
wgpuTextureDestroy(mTexture); wgpuTextureDestroy(mTexture);
wgpuTextureRelease(mTexture); wgpuTextureRelease(mTexture);
@ -219,7 +232,8 @@ void WgRenderDataShapeSettings::update(WGPUDevice device, WGPUQueue queue,
} }
} }
void WgRenderDataShapeSettings::release() { void WgRenderDataShapeSettings::release()
{
mBindGroupSolid.release(); mBindGroupSolid.release();
mBindGroupLinear.release(); mBindGroupLinear.release();
mBindGroupRadial.release(); mBindGroupRadial.release();
@ -229,7 +243,8 @@ void WgRenderDataShapeSettings::release() {
// WgRenderDataShape // WgRenderDataShape
//*********************************************************************** //***********************************************************************
void WgRenderDataShape::release() { void WgRenderDataShape::release()
{
releaseRenderData(); releaseRenderData();
mImageData.release(); mImageData.release();
mBindGroupPaint.release(); mBindGroupPaint.release();
@ -238,7 +253,9 @@ void WgRenderDataShape::release() {
mBindGroupPicture.release(); mBindGroupPicture.release();
} }
void WgRenderDataShape::releaseRenderData() {
void WgRenderDataShape::releaseRenderData()
{
for (uint32_t i = 0; i < mGeometryDataImage.count; i++) { for (uint32_t i = 0; i < mGeometryDataImage.count; i++) {
mGeometryDataImage[i]->release(); mGeometryDataImage[i]->release();
delete mGeometryDataImage[i]; delete mGeometryDataImage[i];
@ -255,7 +272,9 @@ void WgRenderDataShape::releaseRenderData() {
mGeometryDataShape.clear(); 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 // create image geometry data
Array<WgPoint> vertexList; Array<WgPoint> vertexList;
Array<WgPoint> texCoordsList; Array<WgPoint> texCoordsList;
@ -304,7 +323,9 @@ void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, Surface* s
mImageData.update(device, queue, surface); 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<WgVertexList*> outlines{}; Array<WgVertexList*> outlines{};
decodePath(rshape, outlines); decodePath(rshape, outlines);
@ -326,8 +347,10 @@ void WgRenderDataShape::tesselate(WGPUDevice device, WGPUQueue queue, const Rend
delete outlines[i]; delete outlines[i];
} }
// TODO: separate to entity // 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; if (!rshape.stroke) return;
// TODO: chnage to shared_ptrs // TODO: chnage to shared_ptrs
@ -357,7 +380,9 @@ void WgRenderDataShape::stroke(WGPUDevice device, WGPUQueue queue, const RenderS
delete outlines[i]; delete outlines[i];
} }
void WgRenderDataShape::decodePath(const RenderShape& rshape, Array<WgVertexList*>& outlines) {
void WgRenderDataShape::decodePath(const RenderShape& rshape, Array<WgVertexList*>& outlines)
{
size_t pntIndex = 0; size_t pntIndex = 0;
for (uint32_t cmdIndex = 0; cmdIndex < rshape.path.cmds.count; cmdIndex++) { for (uint32_t cmdIndex = 0; cmdIndex < rshape.path.cmds.count; cmdIndex++) {
PathCommand cmd = rshape.path.cmds[cmdIndex]; PathCommand cmd = rshape.path.cmds[cmdIndex];
@ -388,7 +413,9 @@ void WgRenderDataShape::decodePath(const RenderShape& rshape, Array<WgVertexList
} }
} }
void WgRenderDataShape::strokeSegments(const RenderShape& rshape, Array<WgVertexList*>& outlines, Array<WgVertexList*>& segments) {
void WgRenderDataShape::strokeSegments(const RenderShape& rshape, Array<WgVertexList*>& outlines, Array<WgVertexList*>& segments)
{
for (uint32_t i = 0; i < outlines.count; i++) { for (uint32_t i = 0; i < outlines.count; i++) {
auto& vlist = outlines[i]->mVertexList; auto& vlist = outlines[i]->mVertexList;
@ -425,7 +452,9 @@ void WgRenderDataShape::strokeSegments(const RenderShape& rshape, Array<WgVertex
} }
} }
void WgRenderDataShape::strokeSublines(const RenderShape& rshape, Array<WgVertexList*>& outlines, WgVertexList& strokes) {
void WgRenderDataShape::strokeSublines(const RenderShape& rshape, Array<WgVertexList*>& outlines, WgVertexList& strokes)
{
float wdt = rshape.stroke->width / 2; float wdt = rshape.stroke->width / 2;
for (uint32_t i = 0; i < outlines.count; i++) { for (uint32_t i = 0; i < outlines.count; i++) {
auto outline = outlines[i]; auto outline = outlines[i];

View file

@ -26,7 +26,8 @@
#include "tvgWgPipelines.h" #include "tvgWgPipelines.h"
#include "tvgWgGeometry.h" #include "tvgWgGeometry.h"
struct WgGeometryData { struct WgGeometryData
{
WGPUBuffer mBufferVertex{}; WGPUBuffer mBufferVertex{};
WGPUBuffer mBufferTexCoords{}; WGPUBuffer mBufferTexCoords{};
WGPUBuffer mBufferIndex{}; WGPUBuffer mBufferIndex{};
@ -45,7 +46,8 @@ struct WgGeometryData {
void release(); void release();
}; };
struct WgImageData { struct WgImageData
{
WGPUSampler mSampler{}; WGPUSampler mSampler{};
WGPUTexture mTexture{}; WGPUTexture mTexture{};
WGPUTextureView mTextureView{}; WGPUTextureView mTextureView{};
@ -58,14 +60,17 @@ struct WgImageData {
void release(); void release();
}; };
class WgRenderData { class WgRenderData
{
public: public:
virtual void initialize(WGPUDevice device) {}; virtual void initialize(WGPUDevice device) {};
virtual void release() = 0; virtual void release() = 0;
}; };
enum class WgRenderDataShapeFillType { None = 0, Solid = 1, Linear = 2, Radial = 3 }; enum class WgRenderDataShapeFillType { None = 0, Solid = 1, Linear = 2, Radial = 3 };
struct WgRenderDataShapeSettings {
struct WgRenderDataShapeSettings
{
WgBindGroupSolidColor mBindGroupSolid{}; WgBindGroupSolidColor mBindGroupSolid{};
WgBindGroupLinearGradient mBindGroupLinear{}; WgBindGroupLinearGradient mBindGroupLinear{};
WgBindGroupRadialGradient mBindGroupRadial{}; WgBindGroupRadialGradient mBindGroupRadial{};
@ -77,7 +82,8 @@ struct WgRenderDataShapeSettings {
void release(); void release();
}; };
class WgRenderDataShape: public WgRenderData { class WgRenderDataShape: public WgRenderData
{
public: public:
// geometry data for shapes, strokes and image // geometry data for shapes, strokes and image
Array<WgGeometryData*> mGeometryDataShape; Array<WgGeometryData*> mGeometryDataShape;

View file

@ -30,15 +30,20 @@
#include "tvgWgRenderData.h" #include "tvgWgRenderData.h"
#include "tvgWgShaderSrc.h" #include "tvgWgShaderSrc.h"
WgRenderer::WgRenderer() { WgRenderer::WgRenderer()
{
initialize(); initialize();
} }
WgRenderer::~WgRenderer() {
WgRenderer::~WgRenderer()
{
release(); release();
} }
void WgRenderer::initialize() {
void WgRenderer::initialize()
{
// create instance // create instance
WGPUInstanceDescriptor instanceDesc{}; WGPUInstanceDescriptor instanceDesc{};
instanceDesc.nextInChain = nullptr; instanceDesc.nextInChain = nullptr;
@ -106,7 +111,9 @@ void WgRenderer::initialize() {
mPipelines.initialize(mDevice); mPipelines.initialize(mDevice);
} }
void WgRenderer::release() {
void WgRenderer::release()
{
if (mStencilTex) { if (mStencilTex) {
wgpuTextureDestroy(mStencilTex); wgpuTextureDestroy(mStencilTex);
wgpuTextureRelease(mStencilTex); wgpuTextureRelease(mStencilTex);
@ -126,7 +133,9 @@ void WgRenderer::release() {
if (mInstance) wgpuInstanceRelease(mInstance); if (mInstance) wgpuInstanceRelease(mInstance);
} }
RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper) {
RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const RenderTransform* transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags, bool clipper)
{
// get or create render data shape // get or create render data shape
auto renderDataShape = (WgRenderDataShape*)data; auto renderDataShape = (WgRenderDataShape*)data;
if (!renderDataShape) { if (!renderDataShape) {
@ -156,11 +165,15 @@ RenderData WgRenderer::prepare(const RenderShape& rshape, RenderData data, const
return renderDataShape; return renderDataShape;
} }
RenderData WgRenderer::prepare(const Array<RenderData>& scene, RenderData data, const RenderTransform* transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags) {
RenderData WgRenderer::prepare(TVG_UNUSED const Array<RenderData>& scene, TVG_UNUSED RenderData data, TVG_UNUSED const RenderTransform* transform, TVG_UNUSED Array<RenderData>& clips, TVG_UNUSED uint8_t opacity, TVG_UNUSED RenderUpdateFlag flags)
{
return nullptr; return nullptr;
} }
RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags) {
RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderData data, const RenderTransform* transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flags)
{
// get or create render data shape // get or create render data shape
auto renderDataShape = (WgRenderDataShape*)data; auto renderDataShape = (WgRenderDataShape*)data;
if (!renderDataShape) { if (!renderDataShape) {
@ -188,56 +201,79 @@ RenderData WgRenderer::prepare(Surface* surface, const RenderMesh* mesh, RenderD
return renderDataShape; return renderDataShape;
} }
bool WgRenderer::preRender() {
bool WgRenderer::preRender()
{
return true; return true;
} }
bool WgRenderer::renderShape(RenderData data) {
bool WgRenderer::renderShape(RenderData data)
{
mRenderDatas.push(data); mRenderDatas.push(data);
return true; return true;
} }
bool WgRenderer::renderImage(RenderData data) {
bool WgRenderer::renderImage(RenderData data)
{
mRenderDatas.push(data); mRenderDatas.push(data);
return true; return true;
} }
bool WgRenderer::postRender() {
bool WgRenderer::postRender()
{
return true; return true;
} }
bool WgRenderer::dispose(RenderData data) {
bool WgRenderer::dispose(RenderData data)
{
auto renderData = (WgRenderData*)data; auto renderData = (WgRenderData*)data;
if (renderData) renderData->release(); if (renderData) renderData->release();
return true; return true;
} }
RenderRegion WgRenderer::region(RenderData data) {
RenderRegion WgRenderer::region(TVG_UNUSED RenderData data)
{
return { 0, 0, INT32_MAX, INT32_MAX }; return { 0, 0, INT32_MAX, INT32_MAX };
} }
RenderRegion WgRenderer::viewport() { RenderRegion WgRenderer::viewport() {
return { 0, 0, INT32_MAX, INT32_MAX }; return { 0, 0, INT32_MAX, INT32_MAX };
} }
bool WgRenderer::viewport(const RenderRegion& vp) {
bool WgRenderer::viewport(TVG_UNUSED const RenderRegion& vp)
{
return true; return true;
} }
bool WgRenderer::blend(BlendMethod method) {
bool WgRenderer::blend(TVG_UNUSED BlendMethod method)
{
return false; return false;
} }
ColorSpace WgRenderer::colorSpace() {
ColorSpace WgRenderer::colorSpace()
{
return ColorSpace::Unsupported; return ColorSpace::Unsupported;
} }
bool WgRenderer::clear() {
bool WgRenderer::clear()
{
mClearBuffer = true; mClearBuffer = true;
return true; return true;
} }
bool WgRenderer::sync() {
bool WgRenderer::sync()
{
WGPUTextureView backBufferView = wgpuSwapChainGetCurrentTextureView(mSwapChain); WGPUTextureView backBufferView = wgpuSwapChainGetCurrentTextureView(mSwapChain);
// command buffer descriptor // command buffer descriptor
@ -364,7 +400,9 @@ bool WgRenderer::sync() {
return true; 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 // store target surface properties
mTargetSurface.stride = stride; mTargetSurface.stride = stride;
mTargetSurface.w = w; mTargetSurface.w = w;
@ -380,8 +418,10 @@ bool WgRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t
return true; return true;
} }
// target for native window handle // 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 // store target surface properties
mTargetSurface.stride = w; mTargetSurface.stride = w;
mTargetSurface.w = w > 0 ? w : 1; mTargetSurface.w = w > 0 ? w : 1;
@ -463,26 +503,37 @@ bool WgRenderer::target(void* window, uint32_t w, uint32_t h) {
return true; return true;
} }
Compositor* WgRenderer::target(const RenderRegion& region, ColorSpace cs) {
Compositor* WgRenderer::target(TVG_UNUSED const RenderRegion& region, TVG_UNUSED ColorSpace cs)
{
return nullptr; 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; return false;
} }
bool WgRenderer::endComposite(Compositor* cmp) {
bool WgRenderer::endComposite(TVG_UNUSED Compositor* cmp)
{
return false; return false;
} }
WgRenderer* WgRenderer::gen() {
WgRenderer* WgRenderer::gen()
{
return new WgRenderer(); return new WgRenderer();
} }
bool WgRenderer::init(uint32_t threads) {
bool WgRenderer::init(TVG_UNUSED uint32_t threads)
{
return true; return true;
} }
bool WgRenderer::term() {
bool WgRenderer::term()
{
return true; return true;
} }

View file

@ -30,7 +30,6 @@ class WgRenderer : public RenderMethod
private: private:
WgRenderer(); WgRenderer();
~WgRenderer(); ~WgRenderer();
private:
void initialize(); void initialize();
void release(); void release();
public: public:
@ -56,13 +55,14 @@ public:
Compositor* target(const RenderRegion& region, ColorSpace cs); Compositor* target(const RenderRegion& region, ColorSpace cs);
bool beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity); bool beginComposite(Compositor* cmp, CompositeMethod method, uint8_t opacity);
bool endComposite(Compositor* cmp); bool endComposite(Compositor* cmp);
public:
static WgRenderer* gen(); static WgRenderer* gen();
static bool init(uint32_t threads); static bool init(uint32_t threads);
static bool term(); static bool term();
private: private:
Array<RenderData> mRenderDatas{}; Array<RenderData> mRenderDatas{};
private:
Surface mTargetSurface = { nullptr, 0, 0, 0, ColorSpace::Unsupported, true }; Surface mTargetSurface = { nullptr, 0, 0, 0, ColorSpace::Unsupported, true };
float mViewMatrix[16]{}; float mViewMatrix[16]{};
// basic webgpu instances (TODO: create separated entity) // basic webgpu instances (TODO: create separated entity)
@ -78,7 +78,6 @@ private:
WgBindGroupCanvas mBindGroupCanvasWnd; WgBindGroupCanvas mBindGroupCanvasWnd;
WgBindGroupPaint mBindGroupPaintWnd; WgBindGroupPaint mBindGroupPaintWnd;
WgGeometryData mGeometryDataWnd; WgGeometryData mGeometryDataWnd;
private:
WgPipelines mPipelines; WgPipelines mPipelines;
bool mClearBuffer; bool mClearBuffer;

View file

@ -26,26 +26,35 @@
// shader types // shader types
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
WgShaderTypeMat4x4f::WgShaderTypeMat4x4f() { WgShaderTypeMat4x4f::WgShaderTypeMat4x4f()
{
identity(); identity();
} }
WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(const RenderTransform* transform) {
WgShaderTypeMat4x4f::WgShaderTypeMat4x4f(const RenderTransform* transform)
{
update(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[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[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[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; 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); update(w, h);
} }
void WgShaderTypeMat4x4f::update(const RenderTransform* transform) {
void WgShaderTypeMat4x4f::update(const RenderTransform* transform)
{
identity(); identity();
if (transform) { if (transform) {
mat[0] = transform->m.e11; mat[0] = transform->m.e11;
@ -65,42 +74,56 @@ void WgShaderTypeMat4x4f::update(const RenderTransform* transform) {
mat[14] = 0.0f; mat[14] = 0.0f;
mat[15] = transform->m.e33; 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[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[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[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; 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); update(colorSpace);
} }
void WgShaderTypeBlendSettings::update(const ColorSpace colorSpace) {
void WgShaderTypeBlendSettings::update(const ColorSpace colorSpace)
{
format = (uint32_t)colorSpace; format = (uint32_t)colorSpace;
dummy0 = 0.0f; dummy0 = 0.0f;
dummy1 = 0.0f; dummy1 = 0.0f;
dummy2 = 0.0f; dummy2 = 0.0f;
}; }
WgShaderTypeSolidColor::WgShaderTypeSolidColor(const uint8_t* c) {
WgShaderTypeSolidColor::WgShaderTypeSolidColor(const uint8_t* c)
{
update(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[0] = c[0] / 255.0f; // red
color[1] = c[1] / 255.0f; // green color[1] = c[1] / 255.0f; // green
color[2] = c[2] / 255.0f; // blue color[2] = c[2] / 255.0f; // blue
color[3] = c[3] / 255.0f; // alpha color[3] = c[3] / 255.0f; // alpha
}; }
WgShaderTypeLinearGradient::WgShaderTypeLinearGradient(const LinearGradient* linearGradient) {
WgShaderTypeLinearGradient::WgShaderTypeLinearGradient(const LinearGradient* linearGradient)
{
update(linearGradient); update(linearGradient);
} }
void WgShaderTypeLinearGradient::update(const LinearGradient* linearGradient) {
void WgShaderTypeLinearGradient::update(const LinearGradient* linearGradient)
{
const Fill::ColorStop* stops = nullptr; const Fill::ColorStop* stops = nullptr;
auto stopCnt = linearGradient->colorStops(&stops); 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]); linearGradient->linear(&startPos[0], &startPos[1], &endPos[0], &endPos[1]);
}; }
WgShaderTypeRadialGradient::WgShaderTypeRadialGradient(const RadialGradient* radialGradient) {
WgShaderTypeRadialGradient::WgShaderTypeRadialGradient(const RadialGradient* radialGradient)
{
update(radialGradient); update(radialGradient);
} }
void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient) {
void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient)
{
const Fill::ColorStop* stops = nullptr; const Fill::ColorStop* stops = nullptr;
auto stopCnt = radialGradient->colorStops(&stops); auto stopCnt = radialGradient->colorStops(&stops);
@ -138,4 +165,4 @@ void WgShaderTypeRadialGradient::update(const RadialGradient* radialGradient) {
} }
radialGradient->radial(&centerPos[0], &centerPos[1], &radius[0]); radialGradient->radial(&centerPos[0], &centerPos[1], &radius[0]);
}; }

View file

@ -30,7 +30,8 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// mat4x4f // mat4x4f
struct WgShaderTypeMat4x4f { struct WgShaderTypeMat4x4f
{
float mat[16]{}; float mat[16]{};
WgShaderTypeMat4x4f(); WgShaderTypeMat4x4f();
@ -47,7 +48,8 @@ struct WgShaderTypeMat4x4f {
// dummy1 : f32, // dummy1 : f32,
// opacity : f32 // opacity : f32
// }; // };
struct WgShaderTypeBlendSettings { struct WgShaderTypeBlendSettings
{
uint32_t format{}; // ColorSpace uint32_t format{}; // ColorSpace
float dummy0{}; float dummy0{};
float dummy1{}; float dummy1{};
@ -61,7 +63,8 @@ struct WgShaderTypeBlendSettings {
// struct SolidColor { // struct SolidColor {
// color: vec4f // color: vec4f
// }; // };
struct WgShaderTypeSolidColor { struct WgShaderTypeSolidColor
{
float color[4]{}; float color[4]{};
WgShaderTypeSolidColor(const uint8_t* c); WgShaderTypeSolidColor(const uint8_t* c);
@ -77,7 +80,8 @@ struct WgShaderTypeSolidColor {
// stopColors : array<vec4f, MAX_LINEAR_GRADIENT_STOPS> // stopColors : array<vec4f, MAX_LINEAR_GRADIENT_STOPS>
// }; // };
#define MAX_LINEAR_GRADIENT_STOPS 4 #define MAX_LINEAR_GRADIENT_STOPS 4
struct WgShaderTypeLinearGradient { struct WgShaderTypeLinearGradient
{
alignas(16) float nStops[4]{}; alignas(16) float nStops[4]{};
alignas(16) float startPos[2]{}; alignas(16) float startPos[2]{};
alignas(8) float endPos[2]{}; alignas(8) float endPos[2]{};
@ -97,7 +101,8 @@ struct WgShaderTypeLinearGradient {
// stopColors : array<vec4f, MAX_RADIAL_GRADIENT_STOPS> // stopColors : array<vec4f, MAX_RADIAL_GRADIENT_STOPS>
// }; // };
#define MAX_RADIAL_GRADIENT_STOPS 4 #define MAX_RADIAL_GRADIENT_STOPS 4
struct WgShaderTypeRadialGradient { struct WgShaderTypeRadialGradient
{
alignas(16) float nStops[4]{}; alignas(16) float nStops[4]{};
alignas(16) float centerPos[2]{}; alignas(16) float centerPos[2]{};
alignas(8) float radius[2]{}; alignas(8) float radius[2]{};