Skip to content

Commit 16550d4

Browse files
committed
bind layouts and groups
1 parent 52d0159 commit 16550d4

File tree

14 files changed

+309
-51
lines changed

14 files changed

+309
-51
lines changed

sources/include/cage-core/assetsManager.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ namespace cage
4949
Holder<T> get(uint32 assetId) const
5050
{
5151
CAGE_ASSERT(detail::typeHash<T>() == schemeTypeHash_(Scheme))
52-
return get_(Scheme, assetId).template cast<T>();
52+
return get1_(Scheme, assetId).template cast<T>();
53+
}
54+
55+
// returns null if the asset is not yet loaded, failed to load, or has different type
56+
template<class T>
57+
Holder<T> get(uint32 assetId) const
58+
{
59+
return get2_(detail::typeHash<T>(), assetId).template cast<T>();
5360
}
5461

5562
// returns true if the asset exists and is successfully loaded
@@ -70,7 +77,8 @@ namespace cage
7077
void defineScheme_(uint32 typeHash, uint32 scheme, const AssetsScheme &value, bool allowOverride);
7178
void load_(uint32 scheme, uint32 assetId, const String &textId, Holder<void> &&value);
7279
void load_(uint32 scheme, uint32 assetId, const String &textId, const AssetsScheme &customScheme, Holder<void> &&customData);
73-
Holder<void> get_(uint32 scheme, uint32 assetId) const;
80+
Holder<void> get1_(uint32 scheme, uint32 assetId) const;
81+
Holder<void> get2_(uint32 typeHash, uint32 assetId) const;
7482
uint32 schemeTypeHash_(uint32 scheme) const;
7583
friend class AssetsOnDemand;
7684
};

sources/include/cage-core/assetsOnDemand.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ namespace cage
1212
public:
1313
// begin thread-safe methods
1414

15-
// returns null if the asset is not yet loaded or has different scheme
15+
// returns null if the asset is not yet loaded, failed to load, or has different scheme
1616
template<uint32 Scheme, class T>
1717
Holder<T> get(uint32 assetId, bool autoLoad = true)
1818
{
1919
CAGE_ASSERT(detail::typeHash<T>() == schemeTypeHash_(Scheme))
20-
return get_(Scheme, assetId, autoLoad).template cast<T>();
20+
return get1_(Scheme, assetId, autoLoad).template cast<T>();
21+
}
22+
23+
// returns null if the asset is not yet loaded, failed to load, or has different type
24+
template<class T>
25+
Holder<T> get(uint32 assetId, bool autoLoad = true)
26+
{
27+
return get2_(detail::typeHash<T>(), assetId, autoLoad).template cast<T>();
2128
}
2229

2330
void preload(uint32 assetId);
@@ -28,7 +35,8 @@ namespace cage
2835
// end thread-safe methods
2936

3037
private:
31-
Holder<void> get_(uint32 scheme, uint32 assetId, bool autoLoad);
38+
Holder<void> get1_(uint32 scheme, uint32 assetId, bool autoLoad);
39+
Holder<void> get2_(uint32 typeHash, uint32 assetId, bool autoLoad);
3240
uint32 schemeTypeHash_(uint32 scheme) const;
3341
};
3442

sources/include/cage-engine/gpuBuffer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ namespace cage
2525

2626
uint32 size() const;
2727

28-
wgpu::Buffer nativeBuffer();
28+
const wgpu::Buffer &nativeBuffer();
2929
};
3030

3131
CAGE_ENGINE_API Holder<GpuBuffer> newGpuBufferUniform(GraphicsDevice *device);
32+
CAGE_ENGINE_API Holder<GpuBuffer> newGpuBufferStorage(GraphicsDevice *device);
3233
CAGE_ENGINE_API Holder<GpuBuffer> newGpuBufferGeometry(GraphicsDevice *device);
3334
}
3435

sources/include/cage-engine/graphicsDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace cage
2121

2222
Holder<Texture> nextFrame(Window *window);
2323

24-
wgpu::Device nativeDevice();
24+
const wgpu::Device &nativeDevice();
2525
Holder<wgpu::Queue> nativeQueue(); // locks the queue for thread-safe access
2626
};
2727

sources/include/cage-engine/graphicsEncoder.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,32 @@ namespace cage
1717
{
1818
class GraphicsDevice;
1919
class Texture;
20+
class GpuBuffer;
2021
class Shader;
2122
class Model;
2223

23-
struct CAGE_ENGINE_API PassConfig
24+
struct CAGE_ENGINE_API BindingsConfig
25+
{
26+
struct TextureConfig
27+
{
28+
Texture *texture = nullptr;
29+
uint32 shaderSet = m;
30+
uint32 shaderBinding = m;
31+
};
32+
std::vector<TextureConfig> textures;
33+
34+
struct BufferConfig
35+
{
36+
GpuBuffer *buffer = nullptr;
37+
uint32 shaderSet = m;
38+
uint32 shaderBinding = m;
39+
uint32 offset = 0;
40+
uint32 size = m;
41+
};
42+
std::vector<BufferConfig> buffers;
43+
};
44+
45+
struct CAGE_ENGINE_API PassConfig : public BindingsConfig
2446
{
2547
struct ColorTargetConfig
2648
{
@@ -41,25 +63,28 @@ namespace cage
4163
const char *label = nullptr;
4264
};
4365

44-
struct CAGE_ENGINE_API DrawConfig
66+
struct CAGE_ENGINE_API DrawConfig : public BindingsConfig
4567
{
46-
std::array<Texture *, MaxTexturesCountPerMaterial> materialTextures = {};
47-
std::vector<Texture *> extraTextures;
4868
Shader *shader = nullptr;
4969
Model *model = nullptr;
70+
71+
uint32 instances = 1;
5072
};
5173

74+
// fills in all textures and material buffer from the model, using shader set = 1, and also fills in the shader
75+
CAGE_ENGINE_API DrawConfig prepareDrawConfig(const AssetsManager *assets, Model *model, uint32 shaderKeywords = 0);
76+
5277
class CAGE_ENGINE_API GraphicsEncoder : private Immovable
5378
{
5479
public:
5580
void nextPass(const PassConfig &config);
5681
void draw(const DrawConfig &config);
5782
void submit();
5883

59-
PassConfig getCurrentPass() const;
84+
const PassConfig &getCurrentPass() const;
6085

61-
wgpu::CommandEncoder nativeCommandEncoder();
62-
wgpu::RenderPassEncoder nativeDrawEncoder();
86+
const wgpu::CommandEncoder &nativeCommandEncoder();
87+
const wgpu::RenderPassEncoder &nativeDrawEncoder();
6388
};
6489

6590
CAGE_ENGINE_API Holder<GraphicsEncoder> newGraphicsEncoder(GraphicsDevice *device);

sources/include/cage-engine/shader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace cage
2222
public:
2323
void setLabel(const String &name, uint32 variant = 0);
2424

25-
wgpu::ShaderModule nativeVertex();
26-
wgpu::ShaderModule nativeFragment();
25+
const wgpu::ShaderModule &nativeVertex();
26+
const wgpu::ShaderModule &nativeFragment();
2727
};
2828

2929
CAGE_ENGINE_API Holder<Shader> newShader(GraphicsDevice *device, const Spirv *spirv);

sources/include/cage-engine/texture.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ namespace cage
2929
Vec2i mipResolution(uint32 mipmapLevel) const;
3030
Vec3i mipResolution3(uint32 mipmapLevel) const;
3131

32-
wgpu::Texture nativeTexture();
33-
wgpu::TextureView nativeView();
34-
wgpu::Sampler nativeSampler();
32+
const wgpu::Texture &nativeTexture();
33+
const wgpu::TextureView &nativeView();
34+
const wgpu::Sampler &nativeSampler();
3535
};
3636

3737
struct CAGE_ENGINE_API ColorTextureCreateConfig

sources/libcore/assets/assetsManager.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ namespace cage
778778
}
779779
}
780780

781-
Holder<void> AssetsManager::get_(uint32 scheme, uint32 assetId) const
781+
Holder<void> AssetsManager::get1_(uint32 scheme, uint32 assetId) const
782782
{
783783
CAGE_ASSERT(assetId != 0 && assetId != m);
784784
const AssetsManagerImpl *impl = (const AssetsManagerImpl *)this;
@@ -798,6 +798,23 @@ namespace cage
798798
return a->ref.share();
799799
}
800800

801+
Holder<void> AssetsManager::get2_(uint32 typeHash, uint32 assetId) const
802+
{
803+
CAGE_ASSERT(assetId != 0 && assetId != m);
804+
const AssetsManagerImpl *impl = (const AssetsManagerImpl *)this;
805+
const auto &publicIndex = impl->publicIndex;
806+
ScopeLock lock(impl->publicMutex, ReadLockTag());
807+
auto it = publicIndex.find(assetId);
808+
if (it == publicIndex.end())
809+
return {}; // not found
810+
const auto &a = it->second;
811+
if (a->typeHash != typeHash || a->failed)
812+
return {}; // different type
813+
CAGE_ASSERT(a->ref);
814+
CAGE_ASSERT(+a->ref != (void *)1);
815+
return a->ref.share();
816+
}
817+
801818
uint32 AssetsManager::schemeTypeHash_(uint32 scheme) const
802819
{
803820
const AssetsManagerImpl *impl = (const AssetsManagerImpl *)this;

sources/libcore/assets/assetsOnDemand.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ namespace cage
9595
impl->clear();
9696
}
9797

98-
Holder<void> AssetsOnDemand::get_(uint32 scheme, uint32 assetId, bool autoLoad)
98+
Holder<void> AssetsOnDemand::get1_(uint32 scheme, uint32 assetId, bool autoLoad)
9999
{
100-
CAGE_ASSERT(assetId != 0 && assetId != m);
101100
AssetOnDemandImpl *impl = (AssetOnDemandImpl *)this;
102-
auto r = impl->assets->get_(scheme, assetId);
101+
auto r = impl->assets->get1_(scheme, assetId);
102+
impl->update(assetId, autoLoad && !r);
103+
return r;
104+
}
105+
106+
Holder<void> AssetsOnDemand::get2_(uint32 typeHash, uint32 assetId, bool autoLoad)
107+
{
108+
AssetOnDemandImpl *impl = (AssetOnDemandImpl *)this;
109+
auto r = impl->assets->get2_(typeHash, assetId);
103110
impl->update(assetId, autoLoad && !r);
104111
return r;
105112
}

sources/libengine/graphics/buffer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace cage
5353
return numeric_cast<uint32>(impl->buffer.GetSize());
5454
}
5555

56-
wgpu::Buffer GpuBuffer::nativeBuffer()
56+
const wgpu::Buffer &GpuBuffer::nativeBuffer()
5757
{
5858
GpuBufferImpl *impl = (GpuBufferImpl *)this;
5959
return impl->buffer;
@@ -64,6 +64,11 @@ namespace cage
6464
return systemMemory().createImpl<GpuBuffer, GpuBufferImpl>(device, wgpu::BufferUsage::Uniform);
6565
}
6666

67+
Holder<GpuBuffer> newGpuBufferStorage(GraphicsDevice *device)
68+
{
69+
return systemMemory().createImpl<GpuBuffer, GpuBufferImpl>(device, wgpu::BufferUsage::Storage);
70+
}
71+
6772
Holder<GpuBuffer> newGpuBufferGeometry(GraphicsDevice *device)
6873
{
6974
return systemMemory().createImpl<GpuBuffer, GpuBufferImpl>(device, wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Index);

0 commit comments

Comments
 (0)