Skip to content

Commit 51dccbf

Browse files
Render state cache: fixed resource arrays in WebGPU
1 parent 12d7287 commit 51dccbf

File tree

13 files changed

+186
-43
lines changed

13 files changed

+186
-43
lines changed

Graphics/GraphicsEngineWebGPU/include/ShaderWebGPUImpl.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ class ShaderWebGPUImpl final : public ShaderBase<EngineWebGPUImplTraits>
7575
/// Implementation of IShader::GetBytecode() in WebGPU backend.
7676
void DILIGENT_CALL_TYPE GetBytecode(const void** ppBytecode, Uint64& Size) const override final;
7777

78-
/// Implementation of IShaderWebGPU::GetWGSL() in WebGPU backend.
78+
/// Implementation of IShaderWebGPU::GetWGSL().
7979
const std::string& DILIGENT_CALL_TYPE GetWGSL() const override final;
8080

81+
/// Implementation of IShaderWebGPU::GetEmulatedArrayIndexSuffix().
82+
const char* DILIGENT_CALL_TYPE GetEmulatedArrayIndexSuffix() const override final
83+
{
84+
return m_pShaderResources->GetEmulatedArrayIndexSuffix();
85+
}
86+
8187
const char* GetEntryPoint() const;
8288

8389
const std::shared_ptr<const WGSLShaderResources>& GetShaderResources() const
@@ -86,11 +92,6 @@ class ShaderWebGPUImpl final : public ShaderBase<EngineWebGPUImplTraits>
8692
return m_pShaderResources;
8793
}
8894

89-
const char* GetEmulatedArrayIndexSuffix() const
90-
{
91-
return m_pShaderResources->GetEmulatedArrayIndexSuffix();
92-
}
93-
9495
private:
9596
void Initialize(const ShaderCreateInfo& ShaderCI,
9697
const CreateInfo& WebGPUShaderCI) noexcept(false);

Graphics/GraphicsEngineWebGPU/interface/ShaderWebGPU.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class IShaderWebGPU : public IShader
5454
public:
5555
/// Returns WGSL source code
5656
virtual const std::string& DILIGENT_CALL_TYPE GetWGSL() const = 0;
57+
58+
/// Returns a suffix to append to the name of emulated array variables to get
59+
/// the indexed array element name.
60+
///
61+
/// \remarks This value is defined by ShaderCI.WebGPUEmulatedArrayIndexSuffix,
62+
/// see Diligent::ShaderCreateInfo::WebGPUEmulatedArrayIndexSuffix.
63+
virtual const char* DILIGENT_CALL_TYPE GetEmulatedArrayIndexSuffix() const = 0;
5764
};
5865

5966
#endif

Graphics/GraphicsTools/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ set(SOURCE
3737
src/DynamicTextureArray.cpp
3838
src/DynamicTextureAtlas.cpp
3939
src/GraphicsUtilities.cpp
40+
src/GraphicsUtilitiesD3D11.cpp
41+
src/GraphicsUtilitiesD3D12.cpp
42+
src/GraphicsUtilitiesGL.cpp
43+
src/GraphicsUtilitiesVk.cpp
44+
src/GraphicsUtilitiesWebGPU.cpp
4045
src/OffScreenSwapChain.cpp
4146
src/ScopedQueryHelper.cpp
4247
src/ScreenCapture.cpp
@@ -73,18 +78,16 @@ endif()
7378
set(DEPENDENCIES)
7479

7580
if(D3D11_SUPPORTED)
76-
list(APPEND SOURCE src/TextureUploaderD3D11.cpp src/GraphicsUtilitiesD3D11.cpp)
81+
list(APPEND SOURCE src/TextureUploaderD3D11.cpp)
7782
list(APPEND INTERFACE interface/TextureUploaderD3D11.hpp)
7883
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D11Interface)
7984
endif()
8085

8186
if(D3D12_SUPPORTED)
82-
list(APPEND SOURCE src/GraphicsUtilitiesD3D12.cpp)
8387
list(APPEND DEPENDENCIES Diligent-GraphicsEngineD3D12Interface)
8488
endif()
8589

8690
if(VULKAN_SUPPORTED)
87-
list(APPEND SOURCE src/GraphicsUtilitiesVk.cpp)
8891
list(APPEND DEPENDENCIES Diligent-GraphicsEngineVkInterface Vulkan::Headers)
8992
endif()
9093

@@ -94,7 +97,7 @@ if(D3D12_SUPPORTED OR VULKAN_SUPPORTED)
9497
endif()
9598

9699
if(GL_SUPPORTED OR GLES_SUPPORTED)
97-
list(APPEND SOURCE src/TextureUploaderGL.cpp src/GraphicsUtilitiesGL.cpp)
100+
list(APPEND SOURCE src/TextureUploaderGL.cpp)
98101
list(APPEND INTERFACE interface/TextureUploaderGL.hpp)
99102
list(APPEND DEPENDENCIES Diligent-GraphicsEngineOpenGLInterface)
100103
endif()

Graphics/GraphicsTools/interface/GraphicsUtilities.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultSRV)(IObject* pBuffer);
206206
/// If the buffer is null, returns null.
207207
IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultUAV)(IObject* pBuffer);
208208

209+
210+
/// For WebGPU shaders, returns the suffix to append to the name of emulated array variables to get
211+
/// the indexed array element name.
212+
/// For other shader types, returns null.
213+
const char* DILIGENT_GLOBAL_FUNCTION(GetWebGPUEmulatedArrayIndexSuffix)(IShader* pShader);
214+
215+
209216
#include "../../../Primitives/interface/UndefRefMacro.h"
210217

211218
DILIGENT_END_NAMESPACE // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilities.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,9 @@ extern "C"
611611
{
612612
return Diligent::GetBufferDefaultUAV(pBuffer);
613613
}
614+
615+
const char* Diligent_GetWebGPUEmulatedArrayIndexSuffix(Diligent::IShader* pShader)
616+
{
617+
return Diligent::GetWebGPUEmulatedArrayIndexSuffix(pShader);
618+
}
614619
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "GraphicsUtilities.h"
28+
29+
#if WEBGPU_SUPPORTED
30+
# include "ShaderWebGPU.h"
31+
#endif
32+
33+
#include "RefCntAutoPtr.hpp"
34+
35+
namespace Diligent
36+
{
37+
38+
const char* GetWebGPUEmulatedArrayIndexSuffix(IShader* pShader)
39+
{
40+
#if WEBGPU_SUPPORTED
41+
if (RefCntAutoPtr<IShaderWebGPU> pShaderWGPU{pShader, IID_ShaderWebGPU})
42+
{
43+
return pShaderWGPU->GetEmulatedArrayIndexSuffix();
44+
}
45+
#endif
46+
return nullptr;
47+
}
48+
49+
} // namespace Diligent

Graphics/GraphicsTools/src/RenderStateCacheImpl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "SerializedShader.h"
4848
#include "CallbackWrapper.hpp"
4949
#include "GraphicsAccessories.hpp"
50+
#include "GraphicsUtilities.h"
5051
#include "ShaderSourceFactoryUtils.hpp"
5152

5253
namespace Diligent
@@ -560,9 +561,10 @@ struct RenderStateCacheImpl::SerializedPsoCIWrapperBase
560561
}
561562
else if (DeviceType == RENDER_DEVICE_TYPE_WEBGPU)
562563
{
563-
ShaderCI.Source = static_cast<const char*>(ShaderCI.ByteCode);
564-
ShaderCI.ByteCode = nullptr;
565-
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_WGSL;
564+
ShaderCI.Source = static_cast<const char*>(ShaderCI.ByteCode);
565+
ShaderCI.ByteCode = nullptr;
566+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_WGSL;
567+
ShaderCI.WebGPUEmulatedArrayIndexSuffix = GetWebGPUEmulatedArrayIndexSuffix(pShader);
566568
}
567569
ShaderArchiveInfo ArchiveInfo;
568570
ArchiveInfo.DeviceFlags = RenderDeviceTypeToArchiveDataFlag(DeviceType);
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
#include "GraphicsCommon.h"
22

3-
Texture2D g_Tex2D;
3+
#ifdef WEBGPU
4+
Texture2D g_Tex2D_0;
5+
Texture2D g_Tex2D_1;
6+
#else
7+
Texture2D g_Tex2D[2];
8+
#endif
49
SamplerState g_Tex2D_sampler;
510

611
float4 main(in PSInput PSIn) : SV_Target
712
{
813
float2 UV = float2(0.5, 0.5);
9-
return float4(PSIn.Color.rgb, 1.0) * g_Tex2D.Sample(g_Tex2D_sampler, UV.xy);
14+
float4 Color = float4(PSIn.Color.rgb, 1.0);
15+
#ifdef WEBGPU
16+
Color *= g_Tex2D_0.Sample(g_Tex2D_sampler, UV.xy);
17+
Color *= g_Tex2D_1.Sample(g_Tex2D_sampler, UV.xy);
18+
#else
19+
Color *= g_Tex2D[0].Sample(g_Tex2D_sampler, UV.xy);
20+
Color *= g_Tex2D[1].Sample(g_Tex2D_sampler, UV.xy);
21+
#endif
22+
return Color;
1023
// NB: no new line at the end of file!
1124
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
#include "GraphicsCommon.h"
22

3-
Texture2D g_Tex2D;
3+
#ifdef WEBGPU
4+
Texture2D g_Tex2D_0;
5+
Texture2D g_Tex2D_1;
6+
#else
7+
Texture2D g_Tex2D[2];
8+
#endif
49
SamplerState g_Tex2D_sampler;
510

611
float4 main(in PSInput PSIn) : SV_Target
712
{
813
float2 UV = float2(0.5, 0.5);
9-
return float4(PSIn.Color.bgr, 1.0) * g_Tex2D.Sample(g_Tex2D_sampler, UV.xy);
14+
float4 Color = float4(PSIn.Color.bgr, 1.0);
15+
#ifdef WEBGPU
16+
Color *= g_Tex2D_0.Sample(g_Tex2D_sampler, UV.xy);
17+
Color *= g_Tex2D_1.Sample(g_Tex2D_sampler, UV.xy);
18+
#else
19+
Color *= g_Tex2D[0].Sample(g_Tex2D_sampler, UV.xy);
20+
Color *= g_Tex2D[1].Sample(g_Tex2D_sampler, UV.xy);
21+
#endif
22+
return Color;
1023
}

Tests/DiligentCoreAPITest/assets/shaders/RenderStateCache/PixelShaderRld.psh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ cbuffer Colors
77

88
Texture2D g_Tex2D_Static0;
99
Texture2D g_Tex2D_Static1;
10-
Texture2D g_Tex2D_Mut;
10+
#ifdef WEBGPU
11+
Texture2D g_Tex2D_Mut_0;
12+
Texture2D g_Tex2D_Mut_1;
13+
#else
14+
Texture2D g_Tex2D_Mut[2];
15+
#endif
1116
Texture2D g_Tex2D_Dyn;
1217

1318
SamplerState g_Tex2D_Static0_sampler;
@@ -18,10 +23,17 @@ SamplerState g_Tex2D_Dyn_sampler;
1823
float4 main(in PSInput PSIn) : SV_Target
1924
{
2025
float2 UV = float2(0.5, 0.5);
21-
return float4(0.0, 0.0, 0.0, PSIn.Color.r) *
22-
g_Tex2D_Static0.Sample(g_Tex2D_Static0_sampler, UV.xy) *
23-
g_Tex2D_Static1.Sample(g_Tex2D_Static1_sampler, UV.xy) *
24-
g_Tex2D_Mut.Sample(g_Tex2D_Mut_sampler, UV.xy) *
25-
g_Tex2D_Dyn.Sample(g_Tex2D_Dyn_sampler, UV.xy) *
26-
g_Data.RefTexColors[0];
26+
float4 Color = float4(0.0, 0.0, 0.0, PSIn.Color.r);
27+
Color *= g_Tex2D_Static0.Sample(g_Tex2D_Static0_sampler, UV.xy);
28+
Color *= g_Tex2D_Static1.Sample(g_Tex2D_Static1_sampler, UV.xy);
29+
#ifdef WEBGPU
30+
Color *= g_Tex2D_Mut_0.Sample(g_Tex2D_Mut_sampler, UV.xy);
31+
Color *= g_Tex2D_Mut_1.Sample(g_Tex2D_Mut_sampler, UV.xy);
32+
#else
33+
Color *= g_Tex2D_Mut[0].Sample(g_Tex2D_Mut_sampler, UV.xy);
34+
Color *= g_Tex2D_Mut[1].Sample(g_Tex2D_Mut_sampler, UV.xy);
35+
#endif
36+
Color *= g_Tex2D_Dyn.Sample(g_Tex2D_Dyn_sampler, UV.xy);
37+
Color *= g_Data.RefTexColors[0];
38+
return Color;
2739
}

0 commit comments

Comments
 (0)