Skip to content

Commit 3a24f33

Browse files
AssetLoader: move GLTF sampler conversions to utilities
Extract GLTF filter and wrap-mode conversion helpers from ModelBuilder into GLTFUtilities and update GLTFLoader to use the standalone functions.
1 parent f1a8d73 commit 3a24f33

6 files changed

Lines changed: 132 additions & 48 deletions

File tree

AssetLoader/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(INCLUDE
99
set(INTERFACE
1010
interface/GLTFLoader.hpp
1111
interface/GLTFBuilder.hpp
12+
interface/GLTFUtilities.hpp
1213
interface/GLTFVertexDataConverter.hpp
1314
interface/DXSDKMeshLoader.hpp
1415
interface/GLTFResourceManager.hpp
@@ -17,6 +18,7 @@ set(INTERFACE
1718
set(SOURCE
1819
src/GLTFLoader.cpp
1920
src/GLTFBuilder.cpp
21+
src/GLTFUtilities.cpp
2022
src/GLTFVertexDataConverter.cpp
2123
src/DXSDKMeshLoader.cpp
2224
src/GLTFResourceManager.cpp

AssetLoader/interface/GLTFBuilder.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ class ModelBuilder
102102
void InitIndexBuffer(IRenderDevice* pDevice);
103103
void InitVertexBuffers(IRenderDevice* pDevice);
104104

105-
static std::pair<FILTER_TYPE, FILTER_TYPE> GetFilterType(int32_t GltfFilterMode);
106-
107-
static TEXTURE_ADDRESS_MODE GetAddressMode(int32_t GltfWrapMode);
108-
109105
private:
110106
struct PrimitiveKey
111107
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019-2026 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+
#pragma once
28+
29+
#include <cstdint>
30+
#include <utility>
31+
32+
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/GraphicsTypes.h"
33+
34+
namespace Diligent
35+
{
36+
37+
namespace GLTF
38+
{
39+
40+
std::pair<FILTER_TYPE, FILTER_TYPE> GltfFilterModeToFilterType(int32_t GltfFilterMode);
41+
42+
TEXTURE_ADDRESS_MODE GltfWrapModeToAddressMode(int32_t GltfWrapMode);
43+
44+
} // namespace GLTF
45+
46+
} // namespace Diligent

AssetLoader/src/GLTFBuilder.cpp

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -339,44 +339,6 @@ void ModelBuilder::InitVertexBuffers(IRenderDevice* pDevice)
339339
}
340340
}
341341

342-
std::pair<FILTER_TYPE, FILTER_TYPE> ModelBuilder::GetFilterType(int32_t GltfFilterMode)
343-
{
344-
switch (GltfFilterMode)
345-
{
346-
case 9728: // NEAREST
347-
return {FILTER_TYPE_POINT, FILTER_TYPE_POINT};
348-
case 9729: // LINEAR
349-
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
350-
case 9984: // NEAREST_MIPMAP_NEAREST
351-
return {FILTER_TYPE_POINT, FILTER_TYPE_POINT};
352-
case 9985: // LINEAR_MIPMAP_NEAREST
353-
return {FILTER_TYPE_LINEAR, FILTER_TYPE_POINT};
354-
case 9986: // NEAREST_MIPMAP_LINEAR
355-
return {FILTER_TYPE_POINT, FILTER_TYPE_LINEAR};
356-
case 9987: // LINEAR_MIPMAP_LINEAR
357-
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
358-
default:
359-
LOG_WARNING_MESSAGE("Unknown gltf filter mode: ", GltfFilterMode, ". Defaulting to linear.");
360-
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
361-
}
362-
}
363-
364-
TEXTURE_ADDRESS_MODE ModelBuilder::GetAddressMode(int32_t GltfWrapMode)
365-
{
366-
switch (GltfWrapMode)
367-
{
368-
case 10497:
369-
return TEXTURE_ADDRESS_WRAP;
370-
case 33071:
371-
return TEXTURE_ADDRESS_CLAMP;
372-
case 33648:
373-
return TEXTURE_ADDRESS_MIRROR;
374-
default:
375-
LOG_WARNING_MESSAGE("Unknown gltf address wrap mode: ", GltfWrapMode, ". Defaulting to WRAP.");
376-
return TEXTURE_ADDRESS_WRAP;
377-
}
378-
}
379-
380342
} // namespace GLTF
381343

382344
} // namespace Diligent

AssetLoader/src/GLTFLoader.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "GraphicsUtilities.h"
4545
#include "Align.hpp"
4646
#include "GLTFBuilder.hpp"
47+
#include "GLTFUtilities.hpp"
4748
#include "FixedLinearAllocator.hpp"
4849
#include "DefaultRawMemoryAllocator.hpp"
4950

@@ -1539,12 +1540,12 @@ void Model::LoadTextureSamplers(IRenderDevice* pDevice, const tinygltf::Model& g
15391540
for (const tinygltf::Sampler& smpl : gltf_model.samplers)
15401541
{
15411542
SamplerDesc SamDesc;
1542-
SamDesc.MagFilter = ModelBuilder::GetFilterType(smpl.magFilter).first;
1543-
auto MinMipFilter = ModelBuilder::GetFilterType(smpl.minFilter);
1543+
SamDesc.MagFilter = GltfFilterModeToFilterType(smpl.magFilter).first;
1544+
auto MinMipFilter = GltfFilterModeToFilterType(smpl.minFilter);
15441545
SamDesc.MinFilter = MinMipFilter.first;
15451546
SamDesc.MipFilter = MinMipFilter.second;
1546-
SamDesc.AddressU = ModelBuilder::GetAddressMode(smpl.wrapS);
1547-
SamDesc.AddressV = ModelBuilder::GetAddressMode(smpl.wrapT);
1547+
SamDesc.AddressU = GltfWrapModeToAddressMode(smpl.wrapS);
1548+
SamDesc.AddressV = GltfWrapModeToAddressMode(smpl.wrapT);
15481549
SamDesc.AddressW = SamDesc.AddressV;
15491550
RefCntAutoPtr<ISampler> pSampler;
15501551
pDevice->CreateSampler(SamDesc, &pSampler);
@@ -1562,8 +1563,8 @@ static void SetMaterialTextureSamplerProps(const tinygltf::Model& gltf_model, in
15621563
return;
15631564

15641565
const tinygltf::Sampler& gltf_sampler = gltf_model.samplers[gltf_tex.sampler];
1565-
Attribs.SetWrapUMode(ModelBuilder::GetAddressMode(gltf_sampler.wrapS));
1566-
Attribs.SetWrapVMode(ModelBuilder::GetAddressMode(gltf_sampler.wrapT));
1566+
Attribs.SetWrapUMode(GltfWrapModeToAddressMode(gltf_sampler.wrapS));
1567+
Attribs.SetWrapVMode(GltfWrapModeToAddressMode(gltf_sampler.wrapT));
15671568
}
15681569

15691570
struct MaterialLoadContext

AssetLoader/src/GLTFUtilities.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2019-2026 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 "GLTFUtilities.hpp"
28+
29+
#include "DebugUtilities.hpp"
30+
31+
namespace Diligent
32+
{
33+
34+
namespace GLTF
35+
{
36+
37+
std::pair<FILTER_TYPE, FILTER_TYPE> GltfFilterModeToFilterType(int32_t GltfFilterMode)
38+
{
39+
switch (GltfFilterMode)
40+
{
41+
case 9728: // NEAREST
42+
return {FILTER_TYPE_POINT, FILTER_TYPE_POINT};
43+
case 9729: // LINEAR
44+
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
45+
case 9984: // NEAREST_MIPMAP_NEAREST
46+
return {FILTER_TYPE_POINT, FILTER_TYPE_POINT};
47+
case 9985: // LINEAR_MIPMAP_NEAREST
48+
return {FILTER_TYPE_LINEAR, FILTER_TYPE_POINT};
49+
case 9986: // NEAREST_MIPMAP_LINEAR
50+
return {FILTER_TYPE_POINT, FILTER_TYPE_LINEAR};
51+
case 9987: // LINEAR_MIPMAP_LINEAR
52+
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
53+
default:
54+
LOG_WARNING_MESSAGE("Unknown gltf filter mode: ", GltfFilterMode, ". Defaulting to linear.");
55+
return {FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR};
56+
}
57+
}
58+
59+
TEXTURE_ADDRESS_MODE GltfWrapModeToAddressMode(int32_t GltfWrapMode)
60+
{
61+
switch (GltfWrapMode)
62+
{
63+
case 10497:
64+
return TEXTURE_ADDRESS_WRAP;
65+
case 33071:
66+
return TEXTURE_ADDRESS_CLAMP;
67+
case 33648:
68+
return TEXTURE_ADDRESS_MIRROR;
69+
default:
70+
LOG_WARNING_MESSAGE("Unknown gltf address wrap mode: ", GltfWrapMode, ". Defaulting to WRAP.");
71+
return TEXTURE_ADDRESS_WRAP;
72+
}
73+
}
74+
75+
} // namespace GLTF
76+
77+
} // namespace Diligent

0 commit comments

Comments
 (0)