Skip to content

Commit 4d9fa38

Browse files
committed
better debuggable shaders; fix projection matrices
1 parent 054bb73 commit 4d9fa38

File tree

15 files changed

+126
-31
lines changed

15 files changed

+126
-31
lines changed

data/cage/shaders/engine/standard.glsl

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

3738
#endif // DepthOnly
3839
}

sources/asset-processor/shader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ namespace
464464
std::advance(it, i);
465465
const detail::StringBase<20> k = *it;
466466
preamble += std::string("#define ") + k.c_str() + " 1\n";
467-
id += hashRawString(k.c_str());
467+
id += HashString(k);
468468
}
469469
}
470470
try

sources/asset-processor/texture.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ namespace
171171
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "loading done");
172172
}
173173

174+
void fillAlphaChannel(Image *img)
175+
{
176+
CAGE_ASSERT(img->channels() == 4);
177+
const uint32 w = img->width();
178+
const uint32 h = img->height();
179+
for (uint32 y = 0; y < h; y++)
180+
for (uint32 x = 0; x < w; x++)
181+
img->value(x, y, 3, 1);
182+
}
183+
174184
void overrideColorConfig(AlphaModeEnum alpha, GammaSpaceEnum gamma)
175185
{
176186
ImageColorConfig cfg;
@@ -442,7 +452,11 @@ void processTexture()
442452
if (ch != 0 && images.parts[0].image->channels() != ch)
443453
{
444454
for (auto &p : images.parts)
455+
{
445456
imageConvert(+p.image, ch);
457+
if (ch == 4)
458+
fillAlphaChannel(+p.image);
459+
}
446460
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "converted to " + ch + " channels");
447461
}
448462
}
@@ -551,6 +565,19 @@ void processTexture()
551565
}
552566
}
553567

568+
{ // convert 3 channels to 4
569+
if (images.parts[0].image->channels() == 3)
570+
{
571+
CAGE_LOG(SeverityEnum::Warning, "assetProcessor", Stringizer() + "3-channel images are not supported, converting to 4 channels");
572+
for (auto &p : images.parts)
573+
{
574+
imageConvert(+p.image, 4);
575+
fillAlphaChannel(+p.image);
576+
}
577+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "converted to 4 channels");
578+
}
579+
}
580+
554581
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "output resolution: " + images.parts[0].image->width() + "*" + images.parts[0].image->height() + "*" + numeric_cast<uint32>(images.parts.size()));
555582
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "output channels: " + images.parts[0].image->channels());
556583

sources/include/cage-core/core.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ namespace cage
341341
static_assert(std::is_trivially_copyable_v<T> && std::is_trivially_copyable_v<M>);
342342
return PointerRange<M>((M *)begin_, (M *)end_);
343343
}
344+
345+
CAGE_FORCE_INLINE PointerRange<T> subRange(size_type offset, size_type size) const
346+
{
347+
CAGE_ASSERT(offset + size <= this->size());
348+
return PointerRange<T>(begin_ + offset, begin_ + offset + size);
349+
}
344350
};
345351

346352
// string

sources/include/cage-engine/shader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace cage
3737

3838
void addVariant(GraphicsDevice *device, uint32 variant, const Spirv *spirv);
3939

40+
bool checkKeyword(uint32 keyword) const;
41+
4042
Holder<Shader> get(uint32 variant); // sum of hashes of keywords
4143

4244
uint32 customDataCount = 0; // number of floats passed from the game to the shader, per instance

sources/libcore/geometry/shapes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ namespace cage
355355
{
356356
const Mat4 invVP = inverse(viewProj);
357357
static constexpr const Vec3 clipCorners[8] = {
358-
Vec3(-1, -1, -1),
358+
Vec3(-1, -1, 0),
359359
Vec3(-1, -1, +1),
360-
Vec3(-1, +1, -1),
360+
Vec3(-1, +1, 0),
361361
Vec3(-1, +1, +1),
362-
Vec3(+1, -1, -1),
362+
Vec3(+1, -1, 0),
363363
Vec3(+1, -1, +1),
364-
Vec3(+1, +1, -1),
364+
Vec3(+1, +1, 0),
365365
Vec3(+1, +1, +1),
366366
};
367367
Frustum::Corners res;

sources/libcore/math/camera.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace cage
3434
CAGE_ASSERT(aspectRatio > 0);
3535
CAGE_ASSERT(sign(near) == sign(far) && near != far);
3636
Real f = 1 / tan(fov / 2);
37-
return Mat4(f / aspectRatio, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) / (near - far), -1, 0, 0, far * near * 2.0 / (near - far), 0);
37+
return Mat4(f / aspectRatio, 0, 0, 0, 0, f, 0, 0, 0, 0, -far / (far - near), -1, 0, 0, (-far * near) / (far - near), 0);
3838
}
3939

4040
Mat4 perspectiveProjection(Rads fov, Real aspectRatio, Real near, Real far, Real zeroParallaxDistance, Real eyeSeparation)
@@ -56,15 +56,15 @@ namespace cage
5656
CAGE_ASSERT(left != right);
5757
CAGE_ASSERT(bottom != top);
5858
CAGE_ASSERT(sign(near) == sign(far) && near != far);
59-
return Mat4(near * 2.0 / (right - left), 0, 0, 0, 0, near * 2.0 / (top - bottom), 0, 0, (right + left) / (right - left), (top + bottom) / (top - bottom), (far + near) / (near - far), -1, 0, 0, 2 * far * near / (near - far), 0);
59+
return Mat4((2 * near) / (right - left), 0, 0, 0, 0, -(2 * near) / (bottom - top), 0, 0, (right + left) / (right - left), -(bottom + top) / (bottom - top), -far / (far - near), -1, 0, 0, -(far * near) / (far - near), 0);
6060
}
6161

6262
Mat4 orthographicProjection(Real left, Real right, Real bottom, Real top, Real near, Real far)
6363
{
6464
CAGE_ASSERT(left != right);
6565
CAGE_ASSERT(bottom != top);
6666
CAGE_ASSERT(near != far);
67-
return transpose(Mat4(2 / (right - left), 0, 0, -(right + left) / (right - left), 0, 2 / (top - bottom), 0, -(top + bottom) / (top - bottom), 0, 0, -2 / (far - near), -(far + near) / (far - near), 0, 0, 0, 1));
67+
return Mat4(2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, -1 / (far - near), 0, -(right + left) / (right - left), -(top + bottom) / (top - bottom), -near / (far - near), 1);
6868
}
6969

7070
StringPointer stereoModeToString(StereoModeEnum mode)

sources/libengine/assets/texture.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ 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+
*/
6373
}
6474

6575
void processLoad(AssetContext *context)

sources/libengine/graphics/device.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,21 @@ namespace cage
136136
desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowProcessEvents, lostFromDevice);
137137
desc.SetUncapturedErrorCallback(errorFromDevice);
138138

139-
std::array<wgpu::FeatureName, 1> requiredFeatures = {};
140-
requiredFeatures[0] = wgpu::FeatureName::TextureCompressionBC;
139+
static constexpr std::array<wgpu::FeatureName, 1> requiredFeatures = {
140+
wgpu::FeatureName::TextureCompressionBC,
141+
};
141142
desc.requiredFeatures = requiredFeatures.data();
142143
desc.requiredFeatureCount = requiredFeatures.size();
143144

145+
static constexpr std::array<const char *, 2> toggles = {
146+
"use_user_defined_labels_in_backend", // show my names for textures etc in nsight
147+
"disable_symbol_renaming", // preserve variable names in shaders
148+
};
149+
wgpu::DawnTogglesDescriptor togglesDesc;
150+
togglesDesc.enabledToggles = toggles.data();
151+
togglesDesc.enabledToggleCount = toggles.size();
152+
desc.nextInChain = &togglesDesc;
153+
144154
device = adapter.CreateDevice(&desc);
145155
if (!device)
146156
CAGE_THROW_ERROR(Exception, "failed to create wgpu device");

sources/libengine/graphics/encoder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ namespace cage
139139
rpd.vertex.module = config.shader->nativeVertex();
140140
rpd.vertex.bufferCount = 1;
141141
rpd.vertex.buffers = &config.model->getLayout();
142+
rpd.primitive.cullMode = wgpu::CullMode::None;
142143
if (passData.depthTarget)
143144
rpd.depthStencil = &dss;
144145
rpd.fragment = &fs;

0 commit comments

Comments
 (0)