Skip to content

Commit 583b72b

Browse files
committed
add sampler type in DescriptorSetLayout
[**New Material Version**]
1 parent 36e7759 commit 583b72b

21 files changed

+193
-80
lines changed

NEW_RELEASE_NOTES.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ for next branch cut* header.
77
appropriate header in [RELEASE_NOTES.md](./RELEASE_NOTES.md).
88

99
## Release notes for next branch cut
10+
11+
- materials: sampler now export their type in the material binary [⚠️ **New Material Version**]

filament/backend/include/backend/DriverEnums.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,14 @@ static inline constexpr bool hasShaderType(ShaderStageFlags flags, ShaderStage t
234234
}
235235

236236
enum class DescriptorType : uint8_t {
237+
SAMPLER_FLOAT,
238+
SAMPLER_INT,
239+
SAMPLER_UINT,
240+
SAMPLER_DEPTH,
241+
SAMPLER_EXTERNAL,
237242
UNIFORM_BUFFER,
238243
SHADER_STORAGE_BUFFER,
239-
SAMPLER,
240244
INPUT_ATTACHMENT,
241-
SAMPLER_EXTERNAL
242245
};
243246

244247
enum class DescriptorFlags : uint8_t {
@@ -251,6 +254,9 @@ using descriptor_set_t = uint8_t;
251254
using descriptor_binding_t = uint8_t;
252255

253256
struct DescriptorSetLayoutBinding {
257+
static bool isSampler(DescriptorType type) noexcept {
258+
return int(type) <= int(DescriptorType::SAMPLER_EXTERNAL);
259+
}
254260
DescriptorType type;
255261
ShaderStageFlags stageFlags;
256262
descriptor_binding_t binding;

filament/backend/src/metal/MetalDriver.mm

+14-4
Original file line numberDiff line numberDiff line change
@@ -672,22 +672,32 @@
672672
// nothing to do, timer query was constructed in createTimerQueryS
673673
}
674674

675-
const char* toString(DescriptorType type) {
675+
UTILS_UNUSED
676+
static const char* toString(DescriptorType const type) noexcept {
676677
switch (type) {
677678
case DescriptorType::UNIFORM_BUFFER:
678679
return "UNIFORM_BUFFER";
679680
case DescriptorType::SHADER_STORAGE_BUFFER:
680681
return "SHADER_STORAGE_BUFFER";
681-
case DescriptorType::SAMPLER:
682-
return "SAMPLER";
682+
case DescriptorType::SAMPLER_INT:
683+
return "SAMPLER_INT";
684+
case DescriptorType::SAMPLER_UINT:
685+
return "SAMPLER_UINT";
686+
case DescriptorType::SAMPLER_FLOAT:
687+
return "SAMPLER_FLOAT";
688+
case DescriptorType::SAMPLER_DEPTH:
689+
return "SAMPLER_DEPTH";
683690
case DescriptorType::INPUT_ATTACHMENT:
684691
return "INPUT_ATTACHMENT";
685692
case DescriptorType::SAMPLER_EXTERNAL:
686693
return "SAMPLER_EXTERNAL";
687694
}
695+
// should never get here
696+
return "UNKNOWN";
688697
}
689698

690-
const char* toString(ShaderStageFlags flags) {
699+
UTILS_UNUSED
700+
static const char* toString(ShaderStageFlags flags) {
691701
std::vector<const char*> stages;
692702
if (any(flags & ShaderStageFlags::VERTEX)) {
693703
stages.push_back("VERTEX");

filament/backend/src/metal/MetalHandles.mm

+8-2
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,10 @@ static void func(void* user) {
13561356
[arguments addObject:bufferArgument];
13571357
break;
13581358
}
1359-
case DescriptorType::SAMPLER:
1359+
case DescriptorType::SAMPLER_FLOAT:
1360+
case DescriptorType::SAMPLER_INT:
1361+
case DescriptorType::SAMPLER_UINT:
1362+
case DescriptorType::SAMPLER_DEPTH:
13601363
case DescriptorType::SAMPLER_EXTERNAL: {
13611364
MTLArgumentDescriptor* textureArgument = [MTLArgumentDescriptor argumentDescriptor];
13621365
textureArgument.index = binding.binding * 2;
@@ -1472,7 +1475,10 @@ static void func(void* user) {
14721475
atIndex:binding.binding * 2];
14731476
break;
14741477
}
1475-
case DescriptorType::SAMPLER:
1478+
case DescriptorType::SAMPLER_FLOAT:
1479+
case DescriptorType::SAMPLER_INT:
1480+
case DescriptorType::SAMPLER_UINT:
1481+
case DescriptorType::SAMPLER_DEPTH:
14761482
case DescriptorType::SAMPLER_EXTERNAL: {
14771483
auto found = textures.find(binding.binding);
14781484
if (found == textures.end()) {

filament/backend/src/opengl/BindingMap.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ class BindingMap {
6969
assert_invariant(set < MAX_DESCRIPTOR_SET_COUNT);
7070
assert_invariant(binding < MAX_DESCRIPTOR_COUNT);
7171
assert_invariant(entry.binding < 128); // we reserve 1 bit for the type right now
72-
mStorage[set][binding] = { (uint8_t)entry.binding,
73-
entry.type == DescriptorType::SAMPLER ||
74-
entry.type == DescriptorType::SAMPLER_EXTERNAL };
72+
mStorage[set][binding] = { uint8_t(entry.binding),
73+
DescriptorSetLayoutBinding::isSampler(entry.type) };
7574
mActiveDescriptors[set].set(binding);
7675
}
7776

filament/backend/src/opengl/GLDescriptorSet.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ GLDescriptorSet::GLDescriptorSet(OpenGLContext& gl, DescriptorSetLayoutHandle ds
9595
}
9696
break;
9797
}
98-
case DescriptorType::SAMPLER:
98+
99+
case DescriptorType::SAMPLER_FLOAT:
100+
case DescriptorType::SAMPLER_INT:
101+
case DescriptorType::SAMPLER_UINT:
102+
case DescriptorType::SAMPLER_DEPTH:
99103
case DescriptorType::SAMPLER_EXTERNAL:
100104
if (UTILS_UNLIKELY(gl.isES2())) {
101105
desc.emplace<SamplerGLES2>();

filament/backend/src/opengl/OpenGLProgram.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ void OpenGLProgram::initializeProgramState(OpenGLContext& context, GLuint progra
175175
}
176176
break;
177177
}
178-
case DescriptorType::SAMPLER:
178+
case DescriptorType::SAMPLER_FLOAT:
179+
case DescriptorType::SAMPLER_INT:
180+
case DescriptorType::SAMPLER_UINT:
181+
case DescriptorType::SAMPLER_DEPTH:
179182
case DescriptorType::SAMPLER_EXTERNAL: {
180183
if (!entry.name.empty()) {
181184
GLint const loc = glGetUniformLocation(program, entry.name.c_str());

filament/backend/src/vulkan/VulkanHandles.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ BitmaskGroup fromBackendLayout(DescriptorSetLayout const& layout) {
9393
case DescriptorType::SAMPLER_EXTERNAL:
9494
fromStageFlags(binding.stageFlags, binding.binding, mask.externalSampler);
9595
UTILS_FALLTHROUGH;
96-
case DescriptorType::SAMPLER: {
96+
97+
case DescriptorType::SAMPLER_FLOAT:
98+
case DescriptorType::SAMPLER_INT:
99+
case DescriptorType::SAMPLER_UINT:
100+
case DescriptorType::SAMPLER_DEPTH: {
97101
fromStageFlags(binding.stageFlags, binding.binding, mask.sampler);
98102
break;
99103
}

filament/backend/src/webgpu/WebGPUHandles.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ WebGPUDescriptorSetLayout::WebGPUDescriptorSetLayout(DescriptorSetLayout const&
254254

255255
unsigned int samplerCount =
256256
std::count_if(layout.bindings.begin(), layout.bindings.end(), [](auto& fEntry) {
257-
return fEntry.type == DescriptorType::SAMPLER ||
258-
fEntry.type == DescriptorType::SAMPLER_EXTERNAL;
257+
return DescriptorSetLayoutBinding::isSampler(fEntry.type);
259258
});
260259

261260

@@ -271,8 +270,11 @@ WebGPUDescriptorSetLayout::WebGPUDescriptorSetLayout(DescriptorSetLayout const&
271270
entryInfo.binding = wEntry.binding;
272271

273272
switch (fEntry.type) {
274-
case DescriptorType::SAMPLER_EXTERNAL:
275-
case DescriptorType::SAMPLER: {
273+
case DescriptorType::SAMPLER_FLOAT:
274+
case DescriptorType::SAMPLER_INT:
275+
case DescriptorType::SAMPLER_UINT:
276+
case DescriptorType::SAMPLER_DEPTH:
277+
case DescriptorType::SAMPLER_EXTERNAL: {
276278
auto& samplerEntry = wEntries.emplace_back();
277279
auto& samplerEntryInfo = mBindGroupEntries.emplace_back();
278280
samplerEntry.binding = fEntry.binding * 2 + 1;

filament/backend/test/SharedShaders.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ std::vector<UniformConfig> GetUniformConfig(ShaderUniformType type) {
184184
"backend_test", "test_tex", 0,
185185
SamplerType::SAMPLER_2D, SamplerFormat::FLOAT, Precision::HIGH, false };
186186
return {{
187-
"test_tex", DescriptorType::SAMPLER, samplerInfo
187+
"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo
188188
}};
189189
}
190190
default:

filament/backend/test/test_FeedbackLoops.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ TEST_F(BackendTest, FeedbackLoops) {
118118
Shader shader = Shader(api, cleanup, ShaderConfig {
119119
.vertexShader = fullscreenVs,
120120
.fragmentShader = fullscreenFs,
121-
.uniforms = {{"test_tex", DescriptorType::SAMPLER, samplerInfo}, {"Params"}}
121+
.uniforms = {{"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo}, {"Params"}}
122122
});
123123

124124
TrianglePrimitive const triangle(getDriverApi());

filament/backend/test/test_LoadImage.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ TEST_F(LoadImageTest, UpdateImage2D) {
308308
Shader shader(api, cleanup, ShaderConfig{
309309
.vertexShader = mVertexShader,
310310
.fragmentShader= fragment,
311-
.uniforms = {{"test_tex", DescriptorType::SAMPLER, samplerInfo}}
311+
.uniforms = {{"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo}}
312312
});
313313

314314
// Create a Texture.
@@ -372,7 +372,7 @@ TEST_F(LoadImageTest, UpdateImageSRGB) {
372372
getSamplerTypeName(textureFormat), fragmentTemplate);
373373
Shader shader(api, cleanup, ShaderConfig{
374374
.vertexShader = mVertexShader, .fragmentShader = fragment, .uniforms = {{
375-
"test_tex", DescriptorType::SAMPLER, samplerInfo
375+
"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo
376376
}}});
377377

378378
// Create a texture.
@@ -447,7 +447,7 @@ TEST_F(LoadImageTest, UpdateImageMipLevel) {
447447
Shader shader(api, cleanup, ShaderConfig {
448448
.vertexShader = mVertexShader,
449449
.fragmentShader = fragment,
450-
.uniforms = {{"test_tex", DescriptorType::SAMPLER, samplerInfo}}
450+
.uniforms = {{"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo}}
451451
});
452452

453453
// Create a texture with 3 mip levels.
@@ -508,7 +508,7 @@ TEST_F(LoadImageTest, UpdateImage3D) {
508508
Shader shader(api, cleanup, ShaderConfig {
509509
.vertexShader = mVertexShader,
510510
.fragmentShader = fragment,
511-
.uniforms = {{"test_tex", DescriptorType::SAMPLER, samplerInfo}}
511+
.uniforms = {{"test_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo}}
512512
});
513513

514514
// Create a texture.

filament/backend/test/test_MipLevels.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TEST_F(BackendTest, TextureViewLod) {
8282
.vertexShader = vertexShader,
8383
.fragmentShader = fragmentTexturedLod,
8484
.uniforms = {{
85-
"backend_test_sib_tex", DescriptorType::SAMPLER, samplerInfo
85+
"backend_test_sib_tex", DescriptorType::SAMPLER_FLOAT, samplerInfo
8686
}}
8787
});
8888

filament/src/PostProcessManager.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ PostProcessManager::StructurePassOutput PostProcessManager::structure(FrameGraph
504504
FrameGraphTexture::Usage::DEPTH_ATTACHMENT);
505505

506506
if (config.picking) {
507+
// FIXME: the DescriptorSetLayout must specify SAMPLER_FLOAT
507508
data.picking = builder.createTexture("Picking Buffer", {
508509
.width = width, .height = height,
509510
.format = isES2 ? TextureFormat::RGBA8 : TextureFormat::RG32F });

filament/src/RendererUtils.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ RendererUtils::ColorPassOutput RendererUtils::colorPass(
209209

210210
// set shadow sampler
211211
view.prepareShadow(data.shadows ?
212-
resources.getTexture(data.shadows) : engine.getOneTextureArray());
212+
resources.getTexture(data.shadows) :
213+
(view.getShadowType() != ShadowType::PCF ?
214+
engine.getOneTextureArray() : engine.getOneTextureArrayDepth()));
213215

214216
// set structure sampler
215217
view.prepareStructure(data.structure ?

filament/src/details/Engine.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ void FEngine::init() {
361361
}
362362

363363
// initialize the dummy textures so that their contents are not undefined
364-
static constexpr uint32_t zeroes[6] = {};
365-
static constexpr uint32_t ones = 0xffffffff;
366364

367365
mDefaultIblTexture = downcast(Texture::Builder()
368366
.width(1).height(1).levels(1)
@@ -371,7 +369,8 @@ void FEngine::init() {
371369
.build(*this));
372370

373371
driverApi.update3DImage(mDefaultIblTexture->getHwHandle(), 0, 0, 0, 0, 1, 1, 6,
374-
{ zeroes, sizeof(zeroes), Texture::Format::RGBA, Texture::Type::UBYTE });
372+
{ std::array<uint8_t, 6>{}.data(), 6,
373+
Texture::Format::RGBA, Texture::Type::UBYTE });
375374

376375
// 3 bands = 9 float3
377376
constexpr float sh[9 * 3] = { 0.0f };
@@ -393,10 +392,12 @@ void FEngine::init() {
393392
TextureFormat::RGBA8, 1, 1, 1, 1, TextureUsage::DEFAULT);
394393

395394
driverApi.update3DImage(mDummyOneTexture, 0, 0, 0, 0, 1, 1, 1,
396-
{ &ones, 4, Texture::Format::RGBA, Texture::Type::UBYTE });
395+
{ std::array<uint8_t, 4>{0xff, 0xff, 0xff, 0xff}.data(), 4,
396+
Texture::Format::RGBA, Texture::Type::UBYTE });
397397

398398
driverApi.update3DImage(mDummyZeroTexture, 0, 0, 0, 0, 1, 1, 1,
399-
{ zeroes, 4, Texture::Format::RGBA, Texture::Type::UBYTE });
399+
{ std::array<uint8_t, 4>{}.data(), 4,
400+
Texture::Format::RGBA, Texture::Type::UBYTE });
400401

401402

402403
mPerViewDescriptorSetLayoutSsrVariant = {
@@ -451,14 +452,23 @@ void FEngine::init() {
451452
mDummyOneTextureArray = driverApi.createTexture(SamplerType::SAMPLER_2D_ARRAY, 1,
452453
TextureFormat::RGBA8, 1, 1, 1, 1, TextureUsage::DEFAULT);
453454

455+
mDummyOneTextureArrayDepth = driverApi.createTexture(SamplerType::SAMPLER_2D_ARRAY, 1,
456+
TextureFormat::DEPTH32F, 1, 1, 1, 1, TextureUsage::DEFAULT);
457+
454458
mDummyZeroTextureArray = driverApi.createTexture(SamplerType::SAMPLER_2D_ARRAY, 1,
455459
TextureFormat::RGBA8, 1, 1, 1, 1, TextureUsage::DEFAULT);
456460

457461
driverApi.update3DImage(mDummyOneTextureArray, 0, 0, 0, 0, 1, 1, 1,
458-
{ &ones, 4, Texture::Format::RGBA, Texture::Type::UBYTE });
462+
{ std::array<uint8_t, 4>{0xff, 0xff, 0xff, 0xff}.data(), 4,
463+
Texture::Format::RGBA, Texture::Type::UBYTE });
464+
465+
driverApi.update3DImage(mDummyOneTextureArrayDepth, 0, 0, 0, 0, 1, 1, 1,
466+
{ std::array<float, 1>{1.0f}.data(), 4,
467+
Texture::Format::DEPTH_COMPONENT, Texture::Type::FLOAT });
459468

460469
driverApi.update3DImage(mDummyZeroTextureArray, 0, 0, 0, 0, 1, 1, 1,
461-
{ zeroes, 4, Texture::Format::RGBA, Texture::Type::UBYTE });
470+
{ std::array<uint8_t, 4>{}.data(), 4,
471+
Texture::Format::RGBA, Texture::Type::UBYTE });
462472

463473
mLightManager.init(*this);
464474
mDFG.init(*this);

filament/src/details/Engine.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,10 @@ class FEngine : public Engine {
487487
}
488488

489489
backend::Handle<backend::HwTexture> getOneTexture() const { return mDummyOneTexture; }
490-
backend::Handle<backend::HwTexture> getZeroTexture() const { return mDummyZeroTexture; }
491490
backend::Handle<backend::HwTexture> getOneTextureArray() const { return mDummyOneTextureArray; }
491+
backend::Handle<backend::HwTexture> getOneTextureArrayDepth() const { return mDummyOneTextureArrayDepth; }
492+
493+
backend::Handle<backend::HwTexture> getZeroTexture() const { return mDummyZeroTexture; }
492494
backend::Handle<backend::HwTexture> getZeroTextureArray() const { return mDummyZeroTextureArray; }
493495

494496
static constexpr size_t MiB = 1024u * 1024u;
@@ -628,6 +630,7 @@ class FEngine : public Engine {
628630

629631
backend::Handle<backend::HwTexture> mDummyOneTexture;
630632
backend::Handle<backend::HwTexture> mDummyOneTextureArray;
633+
backend::Handle<backend::HwTexture> mDummyOneTextureArrayDepth;
631634
backend::Handle<backend::HwTexture> mDummyZeroTextureArray;
632635
backend::Handle<backend::HwTexture> mDummyZeroTexture;
633636

filament/src/ds/DescriptorSetLayout.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ DescriptorSetLayout::DescriptorSetLayout(
3535
backend::DescriptorSetLayout descriptorSetLayout) noexcept {
3636
for (auto&& desc : descriptorSetLayout.bindings) {
3737
mMaxDescriptorBinding = std::max(mMaxDescriptorBinding, desc.binding);
38-
mSamplers.set(desc.binding,
39-
desc.type == backend::DescriptorType::SAMPLER ||
40-
desc.type == backend::DescriptorType::SAMPLER_EXTERNAL);
38+
mSamplers.set(desc.binding, backend::DescriptorSetLayoutBinding::isSampler(desc.type));
4139
mUniformBuffers.set(desc.binding,
4240
desc.type == backend::DescriptorType::UNIFORM_BUFFER);
4341
}

0 commit comments

Comments
 (0)