Skip to content

Commit e342d0b

Browse files
committed
most materials working
1 parent 4d9fa38 commit e342d0b

File tree

15 files changed

+182
-46
lines changed

15 files changed

+182
-46
lines changed

data/cage/shaders/engine/fragment.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ void updateNormal()
248248
{
249249
normal = normalize(varNormal);
250250

251+
#if defined(MaterialTexRegular) || defined(MaterialTexArray) || defined(MaterialTexCube)
251252
if (uniOptsLights[3] > 0.5)
252253
{
253254
vec3 normalMap = restoreNormalMap(loadNormalMap());
254255
mat3 tbn = makeTangentSpace(normal, varPosition - uniViewport.eyePos.xyz, varUv.xy);
255256
normal = tbn * normalMap;
256257
}
258+
#endif
257259

258260
if (!gl_FrontFacing)
259261
normal *= -1;

data/cage/shaders/engine/standard.glsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void main()
3333
discard;
3434
#endif // CutOut
3535
outColor = lighting(material);
36-
outColor = vec4(material.albedo, 1); // todo remove debug
3736

3837
#endif // DepthOnly
3938
}

schemes/texture.scheme

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,33 @@ default = bcn
7373
display = vertical flip
7474
type = bool
7575
default = false
76+
77+
[sampleFilter]
78+
display = sampling filter
79+
type = enum
80+
values = nearest, linear
81+
default = linear
82+
83+
[anisoFilter]
84+
display = anisotropic filter
85+
type = enum
86+
values = 0, 2, 4, 8, 16, 32, 64
87+
default = 16
88+
89+
[wrapX]
90+
display = wrap x
91+
type = enum
92+
values = clamp, repeat, mirror
93+
default = repeat
94+
95+
[wrapY]
96+
display = wrap y
97+
type = enum
98+
values = clamp, repeat, mirror
99+
default = repeat
100+
101+
[wrapZ]
102+
display = wrap z
103+
type = enum
104+
values = clamp, repeat, mirror
105+
default = repeat

sources/asset-processor/texture.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ void meshImportNotifyUsedFiles(const MeshImportResult &result);
1414

1515
namespace
1616
{
17+
wgpu::FilterMode convertFilter(const String &f)
18+
{
19+
if (f == "nearest")
20+
return wgpu::FilterMode::Nearest;
21+
if (f == "linear")
22+
return wgpu::FilterMode::Linear;
23+
return wgpu::FilterMode::Undefined;
24+
}
25+
26+
wgpu::AddressMode convertWrap(const String &f)
27+
{
28+
if (f == "clamp")
29+
return wgpu::AddressMode::ClampToEdge;
30+
if (f == "repeat")
31+
return wgpu::AddressMode::Repeat;
32+
if (f == "mirror")
33+
return wgpu::AddressMode::MirrorRepeat;
34+
return wgpu::AddressMode::Undefined;
35+
}
36+
1737
TextureFlags convertTarget()
1838
{
1939
TextureFlags result = TextureFlags::None;
@@ -269,6 +289,7 @@ namespace
269289
}
270290
}
271291
images.parts = std::move(parts);
292+
images.sort();
272293
}
273294

274295
void checkConsistency(const TextureFlags target)
@@ -311,6 +332,9 @@ namespace
311332
{
312333
CAGE_LOG(SeverityEnum::Info, "assetProcessor", "using bcn encoding");
313334

335+
if ((data.resolution[0] % 4) != 0 || (data.resolution[1] % 4) != 0)
336+
CAGE_THROW_ERROR(Exception, "base image resolution for bcn encoding must be divisible by 4");
337+
314338
data.usage = (uint64)wgpu::TextureUsage::CopyDst | (uint64)wgpu::TextureUsage::TextureBinding;
315339
data.format = (uint32)findInternalFormatForBcn(data);
316340

@@ -378,6 +402,14 @@ namespace
378402
header.resolution = Vec3i(images.parts[0].image->width(), images.parts[0].image->height(), numeric_cast<uint32>(images.parts.size()));
379403
header.channels = images.parts[0].image->channels();
380404
header.mipLevels = toBool(processor->property("mipmaps")) ? min(findContainedMipmapLevels(header.resolution, any(target & TextureFlags::Volume3D), any(target & TextureFlags::Compressed)), 8u) : 1;
405+
header.sampleFilter = (uint32)convertFilter(processor->property("sampleFilter"));
406+
header.mipmapFilter = (uint32)(header.mipLevels > 1 ? wgpu::MipmapFilterMode::Linear : wgpu::MipmapFilterMode::Nearest);
407+
header.anisoFilter = toUint32(processor->property("anisoFilter"));
408+
if (header.sampleFilter != (uint32)wgpu::FilterMode::Linear || header.mipmapFilter != (uint32)wgpu::MipmapFilterMode::Linear)
409+
header.anisoFilter = 1;
410+
header.wrapX = (uint32)convertWrap(processor->property("wrapX"));
411+
header.wrapY = (uint32)convertWrap(processor->property("wrapY"));
412+
header.wrapZ = (uint32)convertWrap(processor->property("wrapZ"));
381413

382414
// todo
383415
if (images.parts[0].image->format() != ImageFormatEnum::U8)

sources/include/cage-core/imageImport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace cage
2828
struct CAGE_CORE_API ImageImportResult
2929
{
3030
Holder<PointerRange<ImageImportPart>> parts;
31+
32+
void sort();
3133
};
3234

3335
CAGE_CORE_API ImageImportResult imageImportBuffer(PointerRange<const char> buffer);

sources/include/cage-engine/assetsStructs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ namespace cage
3131
uint32 mipLevels = 0;
3232
uint64 usage = 0; // wgpu::TextureUsage
3333
uint32 format = 0; // wgpu::TextureFormat
34+
uint32 sampleFilter = 0; // wgpu::FilterMode
35+
uint32 mipmapFilter = 0; // wgpu::MipmapFilterMode
36+
uint32 anisoFilter = 1;
37+
uint32 wrapX = 0; // wgpu::AddressMode
38+
uint32 wrapY = 0; // wgpu::AddressMode
39+
uint32 wrapZ = 0; // wgpu::AddressMode
3440

3541
// follows:
3642
// for each mipmap level:

sources/include/cage-engine/graphicsEncoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace cage
5353
uint32 instances = 1;
5454
wgpu::CompareFunction depthTest = wgpu::CompareFunction::Less;
5555
bool depthWrite = true;
56+
bool backFaceCulling = true;
5657
BlendingEnum blending = BlendingEnum::None;
5758
};
5859

sources/libcore/image/dds.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ namespace cage
161161
parts.push_back(std::move(part));
162162
ImageImportResult result;
163163
result.parts = std::move(parts);
164+
result.sort();
164165
return result;
165166
}
166167

sources/libcore/image/imageIo.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <algorithm>
2+
13
#include "image.h"
24

35
#include <cage-core/files.h>
@@ -150,4 +152,14 @@ namespace cage
150152
return ddsDecode(buffer);
151153
return decodeSingle(buffer);
152154
}
155+
156+
void ImageImportResult::sort()
157+
{
158+
std::sort(parts.begin(), parts.end(),
159+
[](const ImageImportPart &a, const ImageImportPart &b)
160+
{
161+
const auto &cmp = [](const ImageImportPart &p) { return std::tuple(p.mipmapLevel, p.cubeFace, p.layer, p.fileName); };
162+
return cmp(a) < cmp(b);
163+
});
164+
}
153165
}

sources/libengine/assets/texture.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ namespace cage
7878
TextureHeader header;
7979
des >> header;
8080

81+
Holder<wgpu::Device> dev = ((GraphicsDevice *)context->device)->nativeDevice();
82+
8183
wgpu::TextureDescriptor desc = {};
8284
desc.dimension = any(header.flags & TextureFlags::Volume3D) ? wgpu::TextureDimension::e3D : wgpu::TextureDimension::e2D;
8385
static_assert(sizeof(desc.usage) == sizeof(header.usage));
@@ -87,13 +89,29 @@ namespace cage
8789
desc.mipLevelCount = header.mipLevels;
8890
desc.size = { numeric_cast<uint32>(header.resolution[0]), numeric_cast<uint32>(header.resolution[1]), numeric_cast<uint32>(header.resolution[2]) };
8991
desc.label = context->textId.c_str();
90-
Holder<wgpu::Device> dev = ((GraphicsDevice *)context->device)->nativeDevice();
9192
wgpu::Texture wtex = dev->CreateTexture(&desc);
93+
9294
wgpu::TextureViewDescriptor twd = {};
9395
twd.dimension = textureViewDimension(header.flags);
94-
Holder<Texture> tex = newTexture(wtex, wtex.CreateView(&twd), dev->CreateSampler(), context->textId);
95-
dev.clear();
96+
twd.label = context->textId.c_str();
97+
wgpu::TextureView view = wtex.CreateView(&twd);
98+
99+
wgpu::SamplerDescriptor sd = {};
100+
static_assert(sizeof(sd.magFilter) == sizeof(header.sampleFilter));
101+
sd.magFilter = (wgpu::FilterMode)header.sampleFilter;
102+
sd.minFilter = (wgpu::FilterMode)header.sampleFilter;
103+
static_assert(sizeof(sd.mipmapFilter) == sizeof(header.mipmapFilter));
104+
sd.mipmapFilter = (wgpu::MipmapFilterMode)header.mipmapFilter;
105+
sd.maxAnisotropy = header.anisoFilter;
106+
static_assert(sizeof(sd.addressModeU) == sizeof(header.wrapX));
107+
sd.addressModeU = (wgpu::AddressMode)header.wrapX;
108+
sd.addressModeV = (wgpu::AddressMode)header.wrapY;
109+
sd.addressModeW = (wgpu::AddressMode)header.wrapZ;
110+
sd.label = context->textId.c_str();
111+
wgpu::Sampler samp = dev->CreateSampler(&sd);
96112

113+
dev.clear();
114+
Holder<Texture> tex = newTexture(wtex, view, samp, context->textId);
97115
tex->flags = header.flags;
98116

99117
Holder<wgpu::Queue> queue = ((GraphicsDevice *)context->device)->nativeQueue();

0 commit comments

Comments
 (0)