Skip to content

Commit f6b736d

Browse files
committed
dawn static samplers (with a crash)
1 parent 47ceade commit f6b736d

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

externals/dawn/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ set(constants_path "${CMAKE_CURRENT_LIST_DIR}/dawn/src/dawn/common/Constants.h")
5858
include_directories("${CMAKE_CURRENT_BINARY_DIR}/patches")
5959
file(READ "${constants_path}" constants_content)
6060
string(REPLACE "kMaxSampledTexturesPerShaderStage = 16;" "kMaxSampledTexturesPerShaderStage = 32;" constants_content "${constants_content}")
61-
string(REPLACE "kMaxSamplersPerShaderStage = 16;" "kMaxSamplersPerShaderStage = 32;" constants_content "${constants_content}")
6261
set(CMAKE_DISABLE_SOURCE_CHANGES OFF)
6362
file(CONFIGURE OUTPUT "${constants_path}" CONTENT "${constants_content}" @ONLY)
6463
set(CMAKE_DISABLE_SOURCE_CHANGES ON)

sources/include/cage-engine/graphicsBindings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace cage
4242
{
4343
Texture *texture = nullptr;
4444
uint32 binding = m;
45+
bool staticSampler = false;
4546
};
4647
ankerl::svector<TextureConfig, 4> textures;
4748
};

sources/libengine/assets/texture.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ namespace cage
6060
wgpu::Extent3D extents = { copyWidth, copyHeight, numeric_cast<uint32>(resolution[2]) };
6161

6262
queue.WriteTexture(&dest, data.data(), data.size(), &layout, &extents);
63-
64-
/*
65-
for (uint32 i = 0; i < resolution[2]; i++)
66-
{
67-
dest.origin.z = i;
68-
extents.depthOrArrayLayers = 1;
69-
PointerRange sub = data.subRange(layout.bytesPerRow * layout.rowsPerImage * i, layout.bytesPerRow * layout.rowsPerImage);
70-
queue.WriteTexture(&dest, sub.data(), sub.size(), &layout, &extents);
71-
}
72-
*/
7363
}
7464

7565
void processLoad(AssetContext *context)

sources/libengine/graphics/bindings.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ namespace cage
8585
entries.push_back(e);
8686
}
8787

88+
std::vector<wgpu::StaticSamplerBindingLayout> staticSamplers;
89+
{
90+
uint32 cnt = 0;
91+
for (const auto &t : config.textures)
92+
cnt += t.staticSampler;
93+
staticSamplers.reserve(cnt);
94+
}
95+
8896
for (const auto &t : config.textures)
8997
{
9098
CAGE_ASSERT(t.texture && t.texture->nativeTexture() && t.texture->nativeView() && t.texture->nativeSampler());
@@ -101,7 +109,18 @@ namespace cage
101109
wgpu::BindGroupLayoutEntry e = {};
102110
e.binding = t.binding + 1;
103111
e.visibility = wgpu::ShaderStage::Fragment;
104-
e.sampler.type = filterable ? wgpu::SamplerBindingType::Filtering : wgpu::SamplerBindingType::NonFiltering;
112+
if (t.staticSampler)
113+
{
114+
wgpu::StaticSamplerBindingLayout s = {};
115+
s.sampler = t.texture->nativeSampler();
116+
s.sampledTextureBinding = t.binding;
117+
staticSamplers.push_back(std::move(s));
118+
e.nextInChain = &staticSamplers.back();
119+
}
120+
else
121+
{
122+
e.sampler.type = filterable ? wgpu::SamplerBindingType::Filtering : wgpu::SamplerBindingType::NonFiltering;
123+
}
105124
entries.push_back(e);
106125
}
107126
}
@@ -140,6 +159,7 @@ namespace cage
140159
CAGE_ASSERT(e.textureView);
141160
entries.push_back(e);
142161
}
162+
if (!t.staticSampler)
143163
{
144164
wgpu::BindGroupEntry e = {};
145165
e.binding = t.binding + 1;
@@ -182,9 +202,16 @@ namespace cage
182202
keys.push_back(75431564); // separator
183203
for (const auto &t : config.textures)
184204
{
185-
const uint32 filterable = (uint32)isFormatFilterable(t.texture->nativeTexture().GetFormat()) << 10;
186-
const uint32 flags = (uint32)t.texture->flags << 11;
187-
keys.push_back(t.binding + filterable + flags);
205+
const uint32 staticSamp = (uint32)t.staticSampler << 10;
206+
const uint32 filterable = (uint32)isFormatFilterable(t.texture->nativeTexture().GetFormat()) << 11;
207+
const uint32 flags = (uint32)t.texture->flags << 12;
208+
keys.push_back(t.binding + staticSamp + filterable + flags);
209+
if (t.staticSampler)
210+
{
211+
const uint64 s = (uint64)t.texture->nativeSampler().Get();
212+
keys.push_back((uint32)(s >> 32));
213+
keys.push_back((uint32)s);
214+
}
188215
}
189216

190217
auto hashCombine = [&](std::unsigned_integral auto v) { hash ^= std::hash<std::decay_t<decltype(v)>>{}(v) + 0x9e3779b9 + (hash << 6) + (hash >> 2); };

sources/libengine/graphics/device.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ namespace cage
8989
Holder<DevicePipelinesCache> pipelinesCache;
9090
Holder<Texture> texDummy2d, texDummyArray, texDummyCube;
9191
Holder<GpuBuffer> bufDummyUniform, bufDummyStorage;
92+
wgpu::Sampler sampStaticShadowmaps;
93+
94+
void generateStaticSamplers()
95+
{
96+
wgpu::SamplerDescriptor desc = {};
97+
desc.addressModeU = desc.addressModeV = desc.addressModeW = wgpu::AddressMode::ClampToEdge;
98+
desc.magFilter = desc.minFilter = wgpu::FilterMode::Nearest;
99+
desc.mipmapFilter = wgpu::MipmapFilterMode::Nearest;
100+
desc.label = "static sampler shadowmaps";
101+
sampStaticShadowmaps = device.CreateSampler(&desc);
102+
}
92103

93104
void generateDummyTextures()
94105
{
@@ -97,11 +108,18 @@ namespace cage
97108
cfg.channels = 4;
98109
cfg.mipLevels = 1;
99110
texDummy2d = newTexture(this, cfg, "dummy2d");
111+
texDummy2d = newTexture(texDummy2d->nativeTexture(), texDummy2d->nativeView(), sampStaticShadowmaps, "dummy2d");
112+
100113
cfg.flags = TextureFlags::Array;
101114
texDummyArray = newTexture(this, cfg, "dummyArray");
115+
texDummyArray = newTexture(texDummyArray->nativeTexture(), texDummyArray->nativeView(), sampStaticShadowmaps, "dummyArray");
116+
texDummyArray->flags = cfg.flags;
117+
102118
cfg.resolution[2] = 6;
103119
cfg.flags = TextureFlags::Cubemap;
104120
texDummyCube = newTexture(this, cfg, "dummyCube");
121+
texDummyCube = newTexture(texDummyCube->nativeTexture(), texDummyCube->nativeView(), sampStaticShadowmaps, "dummyCube");
122+
texDummyCube->flags = cfg.flags;
105123

106124
wgpu::TexelCopyTextureInfo dest = {};
107125
dest.texture = texDummy2d->nativeTexture();
@@ -201,20 +219,21 @@ namespace cage
201219
desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowProcessEvents, lostFromDevice);
202220
desc.SetUncapturedErrorCallback(errorFromDevice);
203221

204-
static constexpr std::array<wgpu::FeatureName, 1> requiredFeatures = {
222+
static constexpr std::array<wgpu::FeatureName, 2> requiredFeatures = {
223+
wgpu::FeatureName::StaticSamplers,
205224
wgpu::FeatureName::TextureCompressionBC,
206225
};
207226
desc.requiredFeatures = requiredFeatures.data();
208227
desc.requiredFeatureCount = requiredFeatures.size();
209228

210229
wgpu::Limits requiredLimits = {};
211230
requiredLimits.maxSampledTexturesPerShaderStage = 32;
212-
requiredLimits.maxSamplersPerShaderStage = 32;
213231
desc.requiredLimits = &requiredLimits;
214232

215-
static constexpr std::array<const char *, 2> toggles = {
216-
"use_user_defined_labels_in_backend", // show my names for textures etc in nsight
233+
static constexpr std::array<const char *, 3> toggles = {
234+
"allow_unsafe_apis", // allow use of StaticSamplers
217235
"disable_symbol_renaming", // preserve variable names in shaders
236+
"use_user_defined_labels_in_backend", // show my names for textures etc in nsight
218237
};
219238
wgpu::DawnTogglesDescriptor togglesDesc;
220239
togglesDesc.enabledToggles = toggles.data();
@@ -235,6 +254,7 @@ namespace cage
235254

236255
bindingsCache = newDeviceBindingsCache(this);
237256
pipelinesCache = newDevicePipelinesCache(this);
257+
generateStaticSamplers();
238258
generateDummyTextures();
239259
generateDummyBuffers();
240260

@@ -407,4 +427,10 @@ namespace cage
407427
GraphicsDeviceImpl *impl = (GraphicsDeviceImpl *)device;
408428
return +impl->bufDummyStorage;
409429
}
430+
431+
wgpu::Sampler getStaticSamplerShadowmap(GraphicsDevice *device)
432+
{
433+
GraphicsDeviceImpl *impl = (GraphicsDeviceImpl *)device;
434+
return impl->sampStaticShadowmaps;
435+
}
410436
}

sources/libengine/graphics/sceneRender.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace cage
4343
Texture *getTextureDummyCube(GraphicsDevice *device);
4444
GpuBuffer *getBufferDummyUniform(GraphicsDevice *device);
4545
GpuBuffer *getBufferDummyStorage(GraphicsDevice *device);
46+
wgpu::Sampler getStaticSamplerShadowmap(GraphicsDevice *device);
4647

4748
namespace detail
4849
{
@@ -1108,8 +1109,8 @@ namespace cage
11081109
sh2d[i] = getTextureDummyArray(device);
11091110
if (!shCube[i])
11101111
shCube[i] = getTextureDummyCube(device);
1111-
bind.textures.push_back({ sh2d[i], 8 + i * 2 });
1112-
bind.textures.push_back({ shCube[i], 24 + i * 2 });
1112+
bind.textures.push_back({ sh2d[i], 8 + i * 2, true });
1113+
bind.textures.push_back({ shCube[i], 24 + i * 2, true });
11131114
}
11141115

11151116
globalBindings = newGpuBindings(device, bind);
@@ -1358,8 +1359,8 @@ namespace cage
13581359
bind.textures.push_back({ dummyRegular, 6 });
13591360
for (uint32 i = 0; i < 8; i++)
13601361
{
1361-
bind.textures.push_back({ dummyArray, 8 + i * 2 });
1362-
bind.textures.push_back({ dummyCube, 24 + i * 2 });
1362+
bind.textures.push_back({ dummyArray, 8 + i * 2, true });
1363+
bind.textures.push_back({ dummyCube, 24 + i * 2, true });
13631364
}
13641365
globalBindings = newGpuBindings(device, bind);
13651366
}
@@ -1569,8 +1570,7 @@ namespace cage
15691570
wgpu::TextureViewDescriptor vd = {};
15701571
vd.dimension = wgpu::TextureViewDimension::e2DArray;
15711572
wgpu::TextureView view = tex.CreateView(&vd);
1572-
wgpu::Sampler samp = device->nativeDevice()->CreateSampler();
1573-
Holder<Texture> t = newTexture(tex, view, samp, "shadowmap target");
1573+
Holder<Texture> t = newTexture(tex, view, getStaticSamplerShadowmap(device), "shadowmap target");
15741574
t->flags = TextureFlags::Array;
15751575
return t;
15761576
}
@@ -1588,8 +1588,7 @@ namespace cage
15881588
wgpu::TextureViewDescriptor vd = {};
15891589
vd.dimension = wgpu::TextureViewDimension::Cube;
15901590
wgpu::TextureView view = tex.CreateView(&vd);
1591-
wgpu::Sampler samp = device->nativeDevice()->CreateSampler();
1592-
Holder<Texture> t = newTexture(tex, view, samp, "shadowmap target");
1591+
Holder<Texture> t = newTexture(tex, view, getStaticSamplerShadowmap(device), "shadowmap target");
15931592
t->flags = TextureFlags::Cubemap;
15941593
return t;
15951594
}

0 commit comments

Comments
 (0)