Skip to content

Commit 942be82

Browse files
committed
mesh vertex layout
1 parent 2e17af1 commit 942be82

File tree

9 files changed

+148
-46
lines changed

9 files changed

+148
-46
lines changed

externals/dawn/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ message(STATUS "dawn")
33
message(STATUS "----------------------------------------------------------------")
44

55
set(CMAKE_DISABLE_FIND_PACKAGE_Python3 OFF)
6+
67
if(APPLE)
7-
set(metal ON)
8+
set(metal ON)
89
set(vulkan OFF)
910
else()
10-
set(metal OFF)
11+
set(metal OFF)
1112
set(vulkan ON)
1213
endif()
1314

sources/asset-processor/model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ void processModel()
374374
MemoryBuffer buffer;
375375
Serializer ser(buffer);
376376
ser << dsm;
377-
ser << mat;
378377
ser.write(serMesh);
378+
ser << mat;
379379
ser.write(serCol);
380380
h.originalSize = buffer.size();
381381
Holder<PointerRange<char>> compressed = memoryCompress(buffer);

sources/include/cage-core/assetContext.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,31 @@ namespace cage
99

1010
using AssetDelegate = Delegate<void(AssetContext *)>;
1111

12-
struct CAGE_CORE_API AssetContext : private Immovable
12+
struct CAGE_CORE_API AssetsScheme
13+
{
14+
AssetDelegate fetch;
15+
AssetDelegate decompress;
16+
AssetDelegate load;
17+
void *device = nullptr;
18+
uint32 threadIndex = m;
19+
uint32 typeHash = m;
20+
21+
AssetsScheme();
22+
};
23+
24+
struct CAGE_CORE_API AssetContext : public AssetsScheme, private Immovable
1325
{
1426
detail::StringBase<128> textId;
1527
Holder<void> customData;
1628
Holder<PointerRange<uint32>> dependencies;
1729
Holder<PointerRange<char>> compressedData;
1830
Holder<PointerRange<char>> originalData;
1931
Holder<void> assetHolder;
20-
void *device = nullptr;
2132
const uint32 assetId = 0;
2233
uint32 scheme = m;
2334

2435
AssetContext(uint32 assetId) : assetId(assetId) {}
2536
};
26-
27-
struct CAGE_CORE_API AssetsScheme
28-
{
29-
AssetDelegate fetch;
30-
AssetDelegate decompress;
31-
AssetDelegate load;
32-
void *device = nullptr;
33-
uint32 threadIndex = m;
34-
uint32 typeHash = m;
35-
36-
AssetsScheme();
37-
};
3837
}
3938

4039
#endif // guard_assetContext_h_4lkjh9sd654gtsdfg

sources/include/cage-engine/graphicsEncoder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace cage
2727
Vec4 clearValue = Vec4(0, 0, 0, 1);
2828
bool clear = true;
2929
};
30-
3130
std::vector<TargetConfig> targets;
31+
const char *label = nullptr;
3232
};
3333

3434
struct CAGE_ENGINE_API DrawConfig
@@ -46,6 +46,8 @@ namespace cage
4646
void draw(const DrawConfig &config);
4747
void submit();
4848

49+
PassConfig getCurrentPass() const;
50+
4951
wgpu::CommandEncoder nativeCommandEncoder();
5052
wgpu::RenderPassEncoder nativeDrawEncoder();
5153
};

sources/include/cage-engine/model.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include <cage-core/geometry.h>
77
#include <cage-engine/core.h>
88

9+
namespace wgpu
10+
{
11+
struct VertexBufferLayout;
12+
}
13+
914
namespace cage
1015
{
1116
class Mesh;
@@ -32,6 +37,9 @@ namespace cage
3237
public:
3338
void setLabel(const String &name);
3439

40+
void updateLayout();
41+
const wgpu::VertexBufferLayout &getLayout() const;
42+
3543
// general
3644
Mat4 importTransform;
3745
Aabb boundingBox = Aabb::Universe();
@@ -45,6 +53,7 @@ namespace cage
4553
uint32 indicesOffset = 0;
4654
uint32 primitivesCount = 0;
4755
uint32 primitiveType = 0;
56+
MeshComponentsFlags components = MeshComponentsFlags::None;
4857

4958
// material
5059
Holder<GpuBuffer> materialBuffer;

sources/libcore/assets/assetsManager.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace cage
5555
class AssetsManagerImpl;
5656

5757
// one particular version of an asset
58-
struct Asset : public AssetContext, AssetsScheme
58+
struct Asset : public AssetContext
5959
{
6060
Holder<void> ref; // the application receives shares of the ref, its destructor will call a method in the AssetsManager to schedule the asset for unloading
6161
AssetsManagerImpl *const impl = nullptr;
@@ -357,6 +357,7 @@ namespace cage
357357
textId = textId_.empty() ? (Stringizer() + "<" + assetId + "> with value") : textId_;
358358
scheme = scheme_;
359359
impl->existsCounter++;
360+
*(AssetsScheme *)this = impl->schemes[scheme];
360361
assetHolder = std::move(value_);
361362
}
362363

@@ -366,8 +367,8 @@ namespace cage
366367
scheme = scheme_;
367368
impl->existsCounter++;
368369
*(AssetsScheme *)this = customScheme_;
369-
threadIndex = impl->schemes[scheme].threadIndex;
370-
typeHash = impl->schemes[scheme].typeHash;
370+
threadIndex = impl->schemes[scheme].threadIndex; // prevent thread confusion
371+
typeHash = impl->schemes[scheme].typeHash; // prevent type confusion
371372
customData = std::move(customData_);
372373
}
373374

@@ -849,7 +850,7 @@ namespace cage
849850
CAGE_THROW_ERROR(Exception, "cage asset scheme out of range");
850851
asset->scheme = h.scheme;
851852

852-
*((AssetsScheme *)(Asset *)asset) = impl->schemes[asset->scheme];
853+
*((AssetsScheme *)asset) = impl->schemes[asset->scheme];
853854
if (!impl->schemes[asset->scheme].load)
854855
return;
855856

sources/libengine/graphics/device.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cage-core/concurrent.h>
1111
#include <cage-core/debug.h>
12+
#include <cage-core/profiling.h>
1213
#include <cage-engine/graphicsDevice.h>
1314
#include <cage-engine/texture.h>
1415
#include <cage-engine/window.h>
@@ -59,13 +60,13 @@ namespace cage
5960

6061
void lostFromDevice(const wgpu::Device &device, wgpu::DeviceLostReason reason, wgpu::StringView message)
6162
{
62-
CAGE_LOG(SeverityEnum::Info, "graphics", "device lost");
63+
CAGE_LOG(SeverityEnum::Info, "graphics", "wgpu device lost:");
6364
CAGE_LOG(SeverityEnum::Note, "wgpu", conv(message));
6465
}
6566

6667
void errorFromDevice(const wgpu::Device &device, wgpu::ErrorType error, wgpu::StringView message)
6768
{
68-
CAGE_LOG(SeverityEnum::Error, "graphics", "device error");
69+
CAGE_LOG(SeverityEnum::Error, "graphics", "wgpu device error:");
6970
CAGE_LOG(SeverityEnum::Error, "wgpu", conv(message));
7071
detail::debugBreakpoint();
7172
}
@@ -81,6 +82,7 @@ namespace cage
8182
GraphicsDeviceImpl(const GraphicsDeviceCreateConfig &config)
8283
{
8384
{
85+
CAGE_LOG(SeverityEnum::Info, "graphics", "initializing wgpu instance");
8486
dawn::native::DawnInstanceDescriptor ex = {};
8587
ex.SetLoggingCallback(logFromInstance);
8688
wgpu::InstanceDescriptor desc = {};
@@ -96,8 +98,8 @@ namespace cage
9698

9799
wgpu::Adapter adapter;
98100
{
101+
CAGE_LOG(SeverityEnum::Info, "graphics", "initializing wgpu adapter");
99102
wgpu::RequestAdapterOptions opts = {};
100-
//opts.backendType = wgpu::BackendType::Vulkan;
101103
opts.powerPreference = wgpu::PowerPreference::HighPerformance;
102104
if (config.compatibility)
103105
opts.compatibleSurface = getContext(config.compatibility)->surface;
@@ -120,6 +122,7 @@ namespace cage
120122
}
121123

122124
{
125+
CAGE_LOG(SeverityEnum::Info, "graphics", "initializing wgpu device");
123126
wgpu::DeviceDescriptor desc = {};
124127
desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowProcessEvents, lostFromDevice);
125128
desc.SetUncapturedErrorCallback(errorFromDevice);
@@ -130,10 +133,13 @@ namespace cage
130133
}
131134

132135
{
136+
CAGE_LOG(SeverityEnum::Info, "graphics", "initializing wgpu queue");
133137
queue = device.GetQueue();
134138
if (!queue)
135139
CAGE_THROW_ERROR(Exception, "failed to create wgpu queue");
136140
}
141+
142+
CAGE_LOG(SeverityEnum::Info, "graphics", "wgpu initialized");
137143
}
138144

139145
GraphicsContext *getContext(Window *window)
@@ -143,17 +149,24 @@ namespace cage
143149
context = systemMemory().createHolder<GraphicsContext>();
144150
if (!context->surface)
145151
{
152+
CAGE_LOG(SeverityEnum::Info, "graphics", "initializing wgpu surface");
146153
context->surface = wgpu::glfw::CreateSurfaceForWindow(instance, getGlfwWindow(window));
147154
if (!context->surface)
148155
CAGE_THROW_ERROR(Exception, "failed to create wgpu surface from window");
149156
}
150157
return +context;
151158
}
152159

153-
void processEvents() { device.Tick(); }
160+
void processEvents()
161+
{
162+
const ProfilingScope profiling("graphics process events");
163+
device.Tick();
164+
}
154165

155166
Holder<Texture> nextFrame(Window *window)
156167
{
168+
const ProfilingScope profiling("graphics next frame");
169+
157170
GraphicsContext *context = getContext(window);
158171

159172
const Vec2i res = window->resolution();
@@ -169,9 +182,7 @@ namespace cage
169182
cfg.width = res[0];
170183
cfg.height = res[1];
171184
cfg.presentMode = wgpu::PresentMode::Mailbox;
172-
//cfg.format = wgpu::TextureFormat::RGBA8Unorm;
173-
cfg.format = wgpu::TextureFormat::BGRA8Unorm;
174-
//cfg.usage = wgpu::TextureUsage::RenderAttachment;
185+
cfg.format = wgpu::TextureFormat::BGRA8Unorm; // this should be guaranteed to work everywhere
175186
context->surface.Configure(&cfg);
176187
context->resolution = res;
177188
}

sources/libengine/graphics/encoder.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ namespace cage
6363
rpd.colorAttachmentCount = atts.size();
6464
rpd.colorAttachments = atts.data();
6565
passEnc = cmdEnc.BeginRenderPass(&rpd);
66+
if (config.label)
67+
passEnc.SetLabel(config.label);
6668
}
6769
}
6870

@@ -77,28 +79,18 @@ namespace cage
7779
fs.targetCount = passData.colors.size();
7880
fs.targets = passData.colors.data();
7981

80-
std::array<wgpu::VertexAttribute, 2> attrs = {}; // todo adapt to the mesh
81-
attrs[0].format = wgpu::VertexFormat::Float32x3;
82-
attrs[0].shaderLocation = 0;
83-
attrs[1].format = wgpu::VertexFormat::Float32x2;
84-
attrs[1].shaderLocation = 1;
85-
86-
wgpu::VertexBufferLayout layt = {}; // todo adapt to the mesh
87-
layt.attributeCount = attrs.size();
88-
layt.attributes = attrs.data();
89-
layt.arrayStride = sizeof(Vec3) + sizeof(Vec2);
90-
layt.stepMode = wgpu::VertexStepMode::Vertex;
91-
92-
wgpu::RenderPipelineDescriptor rpd = {}; // todo adapt to the mesh
82+
wgpu::RenderPipelineDescriptor rpd = {};
9383
rpd.vertex.module = config.shader->nativeVertex();
9484
rpd.vertex.bufferCount = 1;
95-
rpd.vertex.buffers = &layt;
85+
rpd.vertex.buffers = &config.model->getLayout();
9686
rpd.fragment = &fs;
9787
pip = device->nativeDevice().CreateRenderPipeline(&rpd);
9888
}
9989

10090
passEnc.SetPipeline(pip);
10191
passEnc.SetVertexBuffer(0, config.model->geometryBuffer->nativeBuffer());
92+
passEnc.SetIndexBuffer(config.model->geometryBuffer->nativeBuffer(), wgpu::IndexFormat::Uint32, config.model->indicesOffset);
93+
/*
10294
{
10395
std::array<wgpu::BindGroupEntry, 2> entries = {}; // todo adapt to the mesh
10496
entries[0].binding = 0;
@@ -112,7 +104,8 @@ namespace cage
112104
wgpu::BindGroup group = device->nativeDevice().CreateBindGroup(&groupDesc);
113105
passEnc.SetBindGroup(0, group);
114106
}
115-
passEnc.Draw(config.model->verticesCount);
107+
*/
108+
passEnc.DrawIndexed(config.model->indicesCount);
116109
}
117110

118111
void submit()
@@ -150,6 +143,12 @@ namespace cage
150143
impl->submit();
151144
}
152145

146+
PassConfig GraphicsEncoder::getCurrentPass() const
147+
{
148+
const GraphicsEncoderImpl *impl = (const GraphicsEncoderImpl *)this;
149+
return (PassConfig)impl->passData;
150+
}
151+
153152
wgpu::CommandEncoder GraphicsEncoder::nativeCommandEncoder()
154153
{
155154
GraphicsEncoderImpl *impl = (GraphicsEncoderImpl *)this;

0 commit comments

Comments
 (0)