webgpu: replace readonly strorages to texture2d (v24 migration)

https://github.com/thorvg/thorvg/issues/2909
This commit is contained in:
Sergii Liebodkin 2025-04-15 19:06:24 +00:00 committed by Hermet Park
parent a2f9b6da21
commit 858205a132
3 changed files with 19 additions and 18 deletions

View file

@ -141,6 +141,7 @@ void WgBindGroupLayouts::releaseBindGroup(WGPUBindGroup& bindGroup)
bindGroup = nullptr; bindGroup = nullptr;
} }
void WgBindGroupLayouts::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout) void WgBindGroupLayouts::releaseBindGroupLayout(WGPUBindGroupLayout& bindGroupLayout)
{ {
if (bindGroupLayout) wgpuBindGroupLayoutRelease(bindGroupLayout); if (bindGroupLayout) wgpuBindGroupLayoutRelease(bindGroupLayout);
@ -160,7 +161,6 @@ void WgBindGroupLayouts::initialize(WGPUDevice device)
const WGPUSamplerBindingLayout sampler = { .type = WGPUSamplerBindingType_Filtering }; const WGPUSamplerBindingLayout sampler = { .type = WGPUSamplerBindingType_Filtering };
const WGPUTextureBindingLayout texture = { .sampleType = WGPUTextureSampleType_Float, .viewDimension = WGPUTextureViewDimension_2D }; const WGPUTextureBindingLayout texture = { .sampleType = WGPUTextureSampleType_Float, .viewDimension = WGPUTextureViewDimension_2D };
const WGPUStorageTextureBindingLayout storageTextureWO { .access = WGPUStorageTextureAccess_WriteOnly, .format = WGPUTextureFormat_RGBA8Unorm, .viewDimension = WGPUTextureViewDimension_2D }; const WGPUStorageTextureBindingLayout storageTextureWO { .access = WGPUStorageTextureAccess_WriteOnly, .format = WGPUTextureFormat_RGBA8Unorm, .viewDimension = WGPUTextureViewDimension_2D };
const WGPUStorageTextureBindingLayout storageTextureRO { .access = WGPUStorageTextureAccess_ReadOnly, .format = WGPUTextureFormat_RGBA8Unorm, .viewDimension = WGPUTextureViewDimension_2D };
const WGPUBufferBindingLayout bufferUniform { .type = WGPUBufferBindingType_Uniform }; const WGPUBufferBindingLayout bufferUniform { .type = WGPUBufferBindingType_Uniform };
// bind group layout tex sampled with buffer uniforms // bind group layout tex sampled with buffer uniforms
@ -190,9 +190,9 @@ void WgBindGroupLayouts::initialize(WGPUDevice device)
// bind group layout tex storages RO // bind group layout tex storages RO
const WGPUBindGroupLayoutEntry entriesTexStoragesRO[] { const WGPUBindGroupLayoutEntry entriesTexStoragesRO[] {
{ .binding = 0, .visibility = visibility_frag, .storageTexture = storageTextureRO }, { .binding = 0, .visibility = visibility_frag, .texture = texture },
{ .binding = 1, .visibility = visibility_frag, .storageTexture = storageTextureRO }, { .binding = 1, .visibility = visibility_frag, .texture = texture },
{ .binding = 2, .visibility = visibility_frag, .storageTexture = storageTextureRO } { .binding = 2, .visibility = visibility_frag, .texture = texture }
}; };
const WGPUBindGroupLayoutDescriptor layoutDescTexStorages1RO { .entryCount = 1, .entries = entriesTexStoragesRO }; const WGPUBindGroupLayoutDescriptor layoutDescTexStorages1RO { .entryCount = 1, .entries = entriesTexStoragesRO };
const WGPUBindGroupLayoutDescriptor layoutDescTexStorages2RO { .entryCount = 2, .entries = entriesTexStoragesRO }; const WGPUBindGroupLayoutDescriptor layoutDescTexStorages2RO { .entryCount = 2, .entries = entriesTexStoragesRO };

View file

@ -44,6 +44,7 @@ public:
WGPUBindGroup createBindGroupTexSampledBuff1Un(WGPUSampler sampler, WGPUTextureView texView, WGPUBuffer buff); WGPUBindGroup createBindGroupTexSampledBuff1Un(WGPUSampler sampler, WGPUTextureView texView, WGPUBuffer buff);
WGPUBindGroup createBindGroupTexSampledBuff2Un(WGPUSampler sampler, WGPUTextureView texView, WGPUBuffer buff0, WGPUBuffer buff1); WGPUBindGroup createBindGroupTexSampledBuff2Un(WGPUSampler sampler, WGPUTextureView texView, WGPUBuffer buff0, WGPUBuffer buff1);
WGPUBindGroup createBindGroupStrorage1WO(WGPUTextureView texView); WGPUBindGroup createBindGroupStrorage1WO(WGPUTextureView texView);
// for read-only access in compute shaders, use texture_2d<f32> instead of texture_storage_2d<rgba8unorm, read>
WGPUBindGroup createBindGroupStrorage1RO(WGPUTextureView texView); WGPUBindGroup createBindGroupStrorage1RO(WGPUTextureView texView);
WGPUBindGroup createBindGroupStrorage2RO(WGPUTextureView texView0, WGPUTextureView texView1); WGPUBindGroup createBindGroupStrorage2RO(WGPUTextureView texView0, WGPUTextureView texView1);
WGPUBindGroup createBindGroupStrorage3RO(WGPUTextureView texView0, WGPUTextureView texView1, WGPUTextureView texView2); WGPUBindGroup createBindGroupStrorage3RO(WGPUTextureView texView0, WGPUTextureView texView1, WGPUTextureView texView2);

View file

@ -709,7 +709,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4f {
//************************************************************************ //************************************************************************
const char* cShaderSrc_GaussianBlur = R"( const char* cShaderSrc_GaussianBlur = R"(
@group(0) @binding(0) var imageSrc : texture_storage_2d<rgba8unorm, read>; @group(0) @binding(0) var imageSrc : texture_2d<f32>;
@group(1) @binding(0) var imageDst : texture_storage_2d<rgba8unorm, write>; @group(1) @binding(0) var imageDst : texture_storage_2d<rgba8unorm, write>;
@group(2) @binding(0) var<uniform> settings: array<vec4f, 3>; @group(2) @binding(0) var<uniform> settings: array<vec4f, 3>;
@group(3) @binding(0) var<uniform> viewport: vec4f; @group(3) @binding(0) var<uniform> viewport: vec4f;
@ -743,9 +743,9 @@ fn cs_main_horz(@builtin(global_invocation_id) gid: vec3u,
let iid = vec2i(uid); let iid = vec2i(uid);
// load source to local workgroup memory // load source to local workgroup memory
buff[lid.x + N*0] = textureLoad(imageSrc, uid - vec2u(N, 0)); buff[lid.x + N*0] = textureLoad(imageSrc, uid - vec2u(N, 0), 0);
buff[lid.x + N*1] = textureLoad(imageSrc, uid + vec2u(0, 0)); buff[lid.x + N*1] = textureLoad(imageSrc, uid + vec2u(0, 0), 0);
buff[lid.x + N*2] = textureLoad(imageSrc, uid + vec2u(N, 0)); buff[lid.x + N*2] = textureLoad(imageSrc, uid + vec2u(N, 0), 0);
workgroupBarrier(); workgroupBarrier();
// apply filter // apply filter
@ -786,9 +786,9 @@ fn cs_main_vert(@builtin(global_invocation_id) gid: vec3u,
let iid = vec2i(uid); let iid = vec2i(uid);
// load source to local workgroup memory // load source to local workgroup memory
buff[lid.y + N*0] = textureLoad(imageSrc, uid - vec2u(0, N)); buff[lid.y + N*0] = textureLoad(imageSrc, uid - vec2u(0, N), 0);
buff[lid.y + N*1] = textureLoad(imageSrc, uid + vec2u(0, 0)); buff[lid.y + N*1] = textureLoad(imageSrc, uid + vec2u(0, 0), 0);
buff[lid.y + N*2] = textureLoad(imageSrc, uid + vec2u(0, N)); buff[lid.y + N*2] = textureLoad(imageSrc, uid + vec2u(0, N), 0);
workgroupBarrier(); workgroupBarrier();
// apply filter // apply filter
@ -812,8 +812,8 @@ fn cs_main_vert(@builtin(global_invocation_id) gid: vec3u,
)"; )";
const char* cShaderSrc_Effects = R"( const char* cShaderSrc_Effects = R"(
@group(0) @binding(0) var imageSrc : texture_storage_2d<rgba8unorm, read>; @group(0) @binding(0) var imageSrc : texture_2d<f32>;
@group(0) @binding(1) var imageSdw : texture_storage_2d<rgba8unorm, read>; @group(0) @binding(1) var imageSdw : texture_2d<f32>;
@group(1) @binding(0) var imageTrg : texture_storage_2d<rgba8unorm, write>; @group(1) @binding(0) var imageTrg : texture_storage_2d<rgba8unorm, write>;
@group(2) @binding(0) var<uniform> settings: array<vec4f, 3>; @group(2) @binding(0) var<uniform> settings: array<vec4f, 3>;
@group(3) @binding(0) var<uniform> viewport: vec4f; @group(3) @binding(0) var<uniform> viewport: vec4f;
@ -829,8 +829,8 @@ fn cs_main_drop_shadow(@builtin(global_invocation_id) gid: vec3u) {
let uid = gid.xy + vmin; let uid = gid.xy + vmin;
let oid = clamp(vec2u(vec2i(uid) - voff), vmin, vmax); let oid = clamp(vec2u(vec2i(uid) - voff), vmin, vmax);
let orig = textureLoad(imageSrc, uid); let orig = textureLoad(imageSrc, uid, 0);
let blur = textureLoad(imageSdw, oid); let blur = textureLoad(imageSdw, oid, 0);
let shad = settings[1] * blur.a; let shad = settings[1] * blur.a;
let color = orig + shad * (1.0 - orig.a); let color = orig + shad * (1.0 - orig.a);
textureStore(imageTrg, uid.xy, color); textureStore(imageTrg, uid.xy, color);
@ -845,7 +845,7 @@ fn cs_main_fill(@builtin(global_invocation_id) gid: vec3u) {
// tex coord // tex coord
let uid = min(gid.xy + vmin, vmax); let uid = min(gid.xy + vmin, vmax);
let orig = textureLoad(imageSrc, uid); let orig = textureLoad(imageSrc, uid, 0);
let fill = settings[0]; let fill = settings[0];
let color = fill * orig.a * fill.a; let color = fill * orig.a * fill.a;
textureStore(imageTrg, uid.xy, color); textureStore(imageTrg, uid.xy, color);
@ -860,7 +860,7 @@ fn cs_main_tint(@builtin(global_invocation_id) gid: vec3u) {
// tex coord // tex coord
let uid = min(gid.xy + vmin, vmax); let uid = min(gid.xy + vmin, vmax);
let orig = textureLoad(imageSrc, uid); let orig = textureLoad(imageSrc, uid, 0);
let luma: f32 = dot(orig.rgb, vec3f(0.2125, 0.7154, 0.0721)); let luma: f32 = dot(orig.rgb, vec3f(0.2125, 0.7154, 0.0721));
let black = settings[0]; let black = settings[0];
let white = settings[1]; let white = settings[1];
@ -878,7 +878,7 @@ fn cs_main_tritone(@builtin(global_invocation_id) gid: vec3u) {
// tex coord // tex coord
let uid = min(gid.xy + vmin, vmax); let uid = min(gid.xy + vmin, vmax);
let orig = textureLoad(imageSrc, uid); let orig = textureLoad(imageSrc, uid, 0);
let luma: f32 = dot(orig.rgb, vec3f(0.2125, 0.7154, 0.0721)); let luma: f32 = dot(orig.rgb, vec3f(0.2125, 0.7154, 0.0721));
let shadow = settings[0]; let shadow = settings[0];
let midtone = settings[1]; let midtone = settings[1];