Skip to content

Commit 52d0159

Browse files
committed
depth
1 parent 942be82 commit 52d0159

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

sources/include/cage-engine/graphicsEncoder.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define guard_graphicsPipelines_sdrzhlkdz
33

44
#include <array>
5+
#include <optional>
56
#include <vector>
67

78
#include <cage-engine/core.h>
@@ -21,13 +22,22 @@ namespace cage
2122

2223
struct CAGE_ENGINE_API PassConfig
2324
{
24-
struct TargetConfig
25+
struct ColorTargetConfig
2526
{
2627
Texture *texture = nullptr;
2728
Vec4 clearValue = Vec4(0, 0, 0, 1);
2829
bool clear = true;
2930
};
30-
std::vector<TargetConfig> targets;
31+
std::vector<ColorTargetConfig> colorTargets;
32+
33+
struct DepthTargetConfig
34+
{
35+
Texture *texture = nullptr;
36+
bool clear = true;
37+
bool write = true;
38+
};
39+
std::optional<DepthTargetConfig> depthTarget;
40+
3141
const char *label = nullptr;
3242
};
3343

sources/include/cage-engine/texture.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,25 @@ namespace cage
3434
wgpu::Sampler nativeSampler();
3535
};
3636

37-
struct CAGE_ENGINE_API TextureCreateConfig
37+
struct CAGE_ENGINE_API ColorTextureCreateConfig
3838
{
3939
Vec3i resolution = Vec3i(0, 0, 1);
4040
uint32 channels = 3;
4141
uint32 mipLevels = 1;
4242
bool srgb = false;
43+
bool sampling = true;
44+
bool renderable = false;
4345
};
4446

45-
CAGE_ENGINE_API Holder<Texture> newTexture(GraphicsDevice *device, const TextureCreateConfig &config);
47+
struct CAGE_ENGINE_API DepthTextureCreateConfig
48+
{
49+
Vec2i resolution = Vec2i(0, 0);
50+
bool sampling = false;
51+
bool renderable = true;
52+
};
53+
54+
CAGE_ENGINE_API Holder<Texture> newTexture(GraphicsDevice *device, const ColorTextureCreateConfig &config);
55+
CAGE_ENGINE_API Holder<Texture> newTexture(GraphicsDevice *device, const DepthTextureCreateConfig &config);
4656
CAGE_ENGINE_API Holder<Texture> newTexture(GraphicsDevice *device, const Image *image);
4757
CAGE_ENGINE_API Holder<Texture> newTexture(wgpu::Texture texture, wgpu::Sampler sampler);
4858
}

sources/libengine/graphics/encoder.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ namespace cage
4141
}
4242
{
4343
passData = PassData{ config };
44-
passData.colors.reserve(config.targets.size());
44+
passData.colors.reserve(config.colorTargets.size());
4545

4646
std::vector<wgpu::RenderPassColorAttachment> atts;
47-
atts.reserve(config.targets.size());
48-
for (const auto &it : config.targets)
47+
atts.reserve(config.colorTargets.size());
48+
for (const auto &it : config.colorTargets)
4949
{
5050
wgpu::RenderPassColorAttachment rpca = {};
5151
rpca.clearValue = { it.clearValue[0].value, it.clearValue[1].value, it.clearValue[2].value, it.clearValue[3].value };
@@ -59,9 +59,20 @@ namespace cage
5959
passData.colors.push_back(std::move(cts));
6060
}
6161

62+
wgpu::RenderPassDepthStencilAttachment rpdsa = {};
63+
if (config.depthTarget)
64+
{
65+
rpdsa.view = config.depthTarget->texture->nativeView();
66+
rpdsa.depthLoadOp = config.depthTarget->clear ? wgpu::LoadOp::Clear : wgpu::LoadOp::Load;
67+
rpdsa.depthStoreOp = wgpu::StoreOp::Store;
68+
rpdsa.depthClearValue = 1;
69+
}
70+
6271
wgpu::RenderPassDescriptor rpd = {};
6372
rpd.colorAttachmentCount = atts.size();
6473
rpd.colorAttachments = atts.data();
74+
if (config.depthTarget)
75+
rpd.depthStencilAttachment = &rpdsa;
6576
passEnc = cmdEnc.BeginRenderPass(&rpd);
6677
if (config.label)
6778
passEnc.SetLabel(config.label);
@@ -74,6 +85,14 @@ namespace cage
7485

7586
wgpu::RenderPipeline pip;
7687
{
88+
wgpu::DepthStencilState dss = {};
89+
if (passData.depthTarget)
90+
{
91+
dss.format = passData.depthTarget->texture->nativeTexture().GetFormat();
92+
dss.depthCompare = wgpu::CompareFunction::Less;
93+
dss.depthWriteEnabled = passData.depthTarget->write;
94+
}
95+
7796
wgpu::FragmentState fs = {};
7897
fs.module = config.shader->nativeFragment();
7998
fs.targetCount = passData.colors.size();
@@ -83,13 +102,16 @@ namespace cage
83102
rpd.vertex.module = config.shader->nativeVertex();
84103
rpd.vertex.bufferCount = 1;
85104
rpd.vertex.buffers = &config.model->getLayout();
105+
if (passData.depthTarget)
106+
rpd.depthStencil = &dss;
86107
rpd.fragment = &fs;
87108
pip = device->nativeDevice().CreateRenderPipeline(&rpd);
88109
}
89110

90111
passEnc.SetPipeline(pip);
91112
passEnc.SetVertexBuffer(0, config.model->geometryBuffer->nativeBuffer());
92-
passEnc.SetIndexBuffer(config.model->geometryBuffer->nativeBuffer(), wgpu::IndexFormat::Uint32, config.model->indicesOffset);
113+
if (config.model->indicesCount)
114+
passEnc.SetIndexBuffer(config.model->geometryBuffer->nativeBuffer(), wgpu::IndexFormat::Uint32, config.model->indicesOffset);
93115
/*
94116
{
95117
std::array<wgpu::BindGroupEntry, 2> entries = {}; // todo adapt to the mesh
@@ -105,7 +127,10 @@ namespace cage
105127
passEnc.SetBindGroup(0, group);
106128
}
107129
*/
108-
passEnc.DrawIndexed(config.model->indicesCount);
130+
if (config.model->indicesCount)
131+
passEnc.DrawIndexed(config.model->indicesCount);
132+
else
133+
passEnc.Draw(config.model->verticesCount);
109134
}
110135

111136
void submit()

sources/libengine/graphics/texture.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ namespace cage
8383
wgpu::TextureView view;
8484
wgpu::Sampler sampler;
8585

86-
TextureImpl(GraphicsDevice *device, const TextureCreateConfig &config)
86+
TextureImpl(GraphicsDevice *device, const ColorTextureCreateConfig &config)
8787
{
8888
wgpu::TextureDescriptor desc = {};
89-
desc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst;
89+
desc.usage = wgpu::TextureUsage::CopyDst;
90+
if (config.sampling)
91+
desc.usage |= wgpu::TextureUsage::TextureBinding;
92+
if (config.renderable)
93+
desc.usage |= wgpu::TextureUsage::RenderAttachment;
9094
desc.dimension = wgpu::TextureDimension::e2D;
9195
desc.size.width = config.resolution[0];
9296
desc.size.height = config.resolution[1];
@@ -98,6 +102,24 @@ namespace cage
98102
sampler = device->nativeDevice().CreateSampler();
99103
}
100104

105+
TextureImpl(GraphicsDevice *device, const DepthTextureCreateConfig &config)
106+
{
107+
wgpu::TextureDescriptor desc = {};
108+
desc.usage = wgpu::TextureUsage::None;
109+
if (config.sampling)
110+
desc.usage |= wgpu::TextureUsage::TextureBinding;
111+
if (config.renderable)
112+
desc.usage |= wgpu::TextureUsage::RenderAttachment;
113+
desc.dimension = wgpu::TextureDimension::e2D;
114+
desc.size.width = config.resolution[0];
115+
desc.size.height = config.resolution[1];
116+
desc.size.depthOrArrayLayers = 1;
117+
desc.format = wgpu::TextureFormat::Depth32Float;
118+
texture = device->nativeDevice().CreateTexture(&desc);
119+
view = texture.CreateView();
120+
sampler = device->nativeDevice().CreateSampler();
121+
}
122+
101123
TextureImpl(wgpu::Texture texture, wgpu::Sampler sampler) : texture(texture), sampler(sampler) { view = texture.CreateView(); }
102124

103125
Vec3i mipRes(uint32 mip) const
@@ -164,7 +186,12 @@ namespace cage
164186
return impl->sampler;
165187
}
166188

167-
Holder<Texture> newTexture(GraphicsDevice *device, const TextureCreateConfig &config)
189+
Holder<Texture> newTexture(GraphicsDevice *device, const ColorTextureCreateConfig &config)
190+
{
191+
return systemMemory().createImpl<Texture, TextureImpl>(device, config);
192+
}
193+
194+
Holder<Texture> newTexture(GraphicsDevice *device, const DepthTextureCreateConfig &config)
168195
{
169196
return systemMemory().createImpl<Texture, TextureImpl>(device, config);
170197
}
@@ -173,7 +200,7 @@ namespace cage
173200
{
174201
if (image->format() != ImageFormatEnum::U8)
175202
CAGE_THROW_ERROR(Exception, "unsupported image format for texture (not U8)");
176-
TextureCreateConfig conf;
203+
ColorTextureCreateConfig conf;
177204
conf.resolution = Vec3i(image->resolution(), 1);
178205
conf.channels = image->channels();
179206
conf.srgb = image->colorConfig.gammaSpace == GammaSpaceEnum::Gamma;

0 commit comments

Comments
 (0)