Skip to content

Commit a700ce2

Browse files
committed
- Dev: bindless material textures for deferred
1 parent 6cf818f commit a700ce2

File tree

31 files changed

+514
-570
lines changed

31 files changed

+514
-570
lines changed

examples/raytracing/application.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void Application::setup() {
5555

5656
Mesh* lightDummy = new Mesh();
5757
Tools::Loaders::load_3D_file(lightDummy, ENGINE_MESH_PATH + "sphere.obj");
58-
lightDummy->push_material(new UnlitMaterial());
58+
lightDummy->add_material(new UnlitMaterial());
5959
lightDummy->set_scale(0.5f);
6060
lightDummy->ray_hittable(false);
6161
lightDummy->cast_shadows(false);
@@ -89,7 +89,7 @@ void Application::setup() {
8989
// toriiMat->set_mask_texture(toriiM, UNREAL_ENGINE);
9090
toriiMat->set_metalness(0.05);
9191
toriiMat->set_roughness(0.5);
92-
toriiMesh->push_material(toriiMat);
92+
toriiMesh->add_material(toriiMat);
9393

9494
Tools::Loaders::load_3D_file(toriiMesh, MESH_PATH + "torii.obj");
9595
toriiMesh->set_name("Torii");
@@ -100,7 +100,7 @@ void Application::setup() {
100100

101101
Mesh* plane = new Mesh();
102102
// Tools::Loaders::load_3D_file(plane, MESH_PATH + "torii.obj", false);
103-
plane->push_geometry(Geometry::create_quad());
103+
plane->set_geometry(Geometry::create_quad());
104104
auto terrainMat = new PhysicalMaterial();
105105
TextureLDR* floorText = new TextureLDR();
106106
Tools::Loaders::load_texture(floorText, TEXTURE_PATH + "floor_diffuse.jpg");
@@ -115,7 +115,7 @@ void Application::setup() {
115115
terrainMat->set_roughness(0.2f);
116116
terrainMat->set_tile({10.0f, 10.0f});
117117
terrainMat->reflective(true);
118-
plane->push_material(terrainMat);
118+
plane->add_material(terrainMat);
119119
plane->set_name("Floor");
120120
plane->set_position({0.0, -2.3, 0.0});
121121
plane->set_rotation({-90.0f, 0.0f, 0.0f});
@@ -130,7 +130,7 @@ void Application::setup() {
130130
TextureLDR* stonelanternN = new TextureLDR();
131131
Tools::Loaders::load_texture(stonelanternN, TEXTURE_PATH + "moisturizer_normal.png", TEXTURE_FORMAT_UNORM);
132132
stoneMat->set_normal_texture(stonelanternN);
133-
stoneMesh->push_material(stoneMat);
133+
stoneMesh->add_material(stoneMat);
134134
stoneMesh->set_name("Tower");
135135
stoneMesh->set_position({2.0f, -2.3f, -2.3f});
136136
stoneMesh->set_rotation({0.0, 126.0f, 0.0f});
@@ -151,7 +151,7 @@ void Application::setup() {
151151
TextureLDR* droidText2 = new TextureLDR();
152152
Tools::Loaders::load_texture(droidText2, TEXTURE_PATH + "DROID_Body_Normal.jpg", TEXTURE_FORMAT_UNORM);
153153
droidMat->set_normal_texture(droidText2);
154-
droidMesh->push_material(droidMat);
154+
droidMesh->add_material(droidMat);
155155
droidMesh->set_name("Droid");
156156
droidMesh->set_position({-0.7f, -2.3f, -1.6f});
157157
droidMesh->set_rotation({0.0, -136.0f, 0.0f});
@@ -161,7 +161,7 @@ void Application::setup() {
161161
auto droidMat1 = new PhysicalMaterial();
162162
droidMat1->set_emissive_color(Vec3(1.0));
163163
droidMat1->set_emission_intensity(10.0);
164-
eyesMesh->push_material(droidMat1);
164+
eyesMesh->add_material(droidMat1);
165165
eyesMesh->set_name("Eyes");
166166
droidMesh->add_child(eyesMesh);
167167
m_scene->add(droidMesh);
@@ -178,7 +178,7 @@ void Application::setup() {
178178
TextureLDR* stormtrooperText2 = new TextureLDR();
179179
Tools::Loaders::load_texture(stormtrooperText2, TEXTURE_PATH + "stormtrooper_mask.png", TEXTURE_FORMAT_UNORM);
180180
stormtrooperMat->set_mask_texture(stormtrooperText2, MaskType::UNREAL_ENGINE);
181-
stormtrooper->push_material(stormtrooperMat);
181+
stormtrooper->add_material(stormtrooperMat);
182182
stormtrooper->set_name("Trooper");
183183
stormtrooper->set_position({-1.8f, -2.3f, 0.4f});
184184
stormtrooper->set_rotation({0.0, -136.0f, 0.0f});
@@ -195,15 +195,15 @@ void Application::setup() {
195195
TextureLDR* stormtrooperText13 = new TextureLDR();
196196
Tools::Loaders::load_texture(stormtrooperText13, TEXTURE_PATH + "stormtrooper_head_mask.png", TEXTURE_FORMAT_UNORM);
197197
stormtrooperMat1->set_mask_texture(stormtrooperText13, MaskType::UNREAL_ENGINE);
198-
stormtrooperHead->push_material(stormtrooperMat1);
198+
stormtrooperHead->add_material(stormtrooperMat1);
199199
stormtrooperHead->set_name("Head");
200200
stormtrooper->add_child(stormtrooperHead);
201201
m_scene->add(stormtrooper);
202202

203203
Mesh* roninMesh = new Mesh();
204204
Tools::Loaders::load_3D_file(roninMesh, MESH_PATH + "ronin.obj");
205205
auto roninMat = new PhysicalMaterial();
206-
roninMesh->push_material(roninMat);
206+
roninMesh->add_material(roninMat);
207207
roninMesh->set_name("Ronin");
208208
roninMesh->set_position({-2.1f, -2.065f, -3.4f});
209209
roninMesh->set_rotation({0.0, 14.0f, 0.0f});
@@ -214,7 +214,7 @@ void Application::setup() {
214214
Mesh* sphereMesh = new Mesh();
215215
Tools::Loaders::load_3D_file(sphereMesh, ENGINE_MESH_PATH + "sphere.obj");
216216
auto spheremat = new PhysicalMaterial();
217-
sphereMesh->push_material(spheremat);
217+
sphereMesh->add_material(spheremat);
218218
sphereMesh->set_name("Energy ball");
219219
spheremat->set_albedo(Vec3(0.0));
220220
spheremat->set_metalness(0.0);

examples/renderer-app/application.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void Application::setup() {
171171
toriiMat->set_metalness(0.05);
172172
toriiMat->set_roughness(0.5);
173173
// toriiMat->set_mask_texture(toriiM, UNREAL_ENGINE);
174-
toriiMesh->push_material(toriiMat);
174+
toriiMesh->add_material(toriiMat);
175175
Tools::Loaders::load_3D_file(toriiMesh, MESH_PATH + "torii.obj");
176176
toriiMesh->set_name("Torii");
177177
toriiMesh->set_scale(0.2f);
@@ -196,15 +196,15 @@ void Application::setup() {
196196
terrainMat->set_normal_texture(floorNormalText);
197197
terrainMat->set_roughness_texture(floorRoughText);
198198
terrainMat->set_tile({25.0f, 25.0f});
199-
terrainMesh->push_material(terrainMat);
199+
terrainMesh->add_material(terrainMat);
200200
terrainMesh->set_name("Terrain");
201201
m_scene->add(terrainMesh);
202202

203203
auto lightMat = new UnlitMaterial();
204204
lightMat->set_color(glm::vec4(m_scene->get_lights()[0]->get_color(), 1.0f));
205205
m_lightDummy = new Mesh();
206206
Tools::Loaders::load_3D_file(m_lightDummy, ENGINE_MESH_PATH + "sphere.obj");
207-
m_lightDummy->push_material(lightMat);
207+
m_lightDummy->add_material(lightMat);
208208
m_lightDummy->set_scale(0.5f);
209209
m_lightDummy->set_name("Light Gizmo");
210210
// m_scene->add(m_lightDummy);
@@ -219,7 +219,7 @@ void Application::setup() {
219219
kabutoMat->set_albedo({0.0, 1.0, 0.0});
220220
kabutoMat->set_metalness(0.8f);
221221
kabutoMat->set_roughness(0.4f);
222-
kabutoMesh->push_material(kabutoMat);
222+
kabutoMesh->add_material(kabutoMat);
223223
kabutoMesh->set_name("Kabuto");
224224
m_scene->add(kabutoMesh);
225225

@@ -240,7 +240,7 @@ void Application::setup() {
240240
templeMat->set_albedo({0.0, 1.0, 0.0});
241241
templeMat->set_metalness(0.8f);
242242
templeMat->set_roughness(0.4f);
243-
templeMesh->push_material(templeMat);
243+
templeMesh->add_material(templeMat);
244244
templeMesh->set_name("Temple");
245245
templeMesh->set_position({7.2, -2.87, 14.1});
246246
templeMesh->set_rotation({0.0, 230.0f, 0.0f});
@@ -260,7 +260,7 @@ void Application::setup() {
260260
templeMat2->set_albedo_texture(templeText2);
261261
templeMat2->set_metallic_texture(templeMText2);
262262
templeMat2->set_roughness_texture(templeRText2);
263-
templeMesh2->push_material(templeMat2);
263+
templeMesh2->add_material(templeMat2);
264264
templeMesh2->set_name("Shrine");
265265
templeMesh2->set_position({0, -2.77, 14.1});
266266
templeMesh2->set_rotation({0.0, 160.0f, 0.0f});
@@ -273,7 +273,7 @@ void Application::setup() {
273273
TextureLDR* lanternT = new TextureLDR();
274274
Tools::Loaders::load_texture(lanternT, TEXTURE_PATH + "lantern_diffuse.png");
275275
lanternMat->set_albedo_texture(lanternT);
276-
lanternMesh->push_material(lanternMat);
276+
lanternMesh->add_material(lanternMat);
277277
lanternMesh->set_name("Lantern");
278278
lanternMesh->set_position({2.5, -1.67, 2.8});
279279
lanternMesh->set_rotation({0.0, 23.0f, 0.0f});
@@ -302,7 +302,7 @@ void Application::setup() {
302302
TextureLDR* stonelanternT = new TextureLDR();
303303
Tools::Loaders::load_texture(stonelanternT, TEXTURE_PATH + "stone_diffuse.png");
304304
stoneMat->set_albedo_texture(stonelanternT);
305-
stoneMesh->push_material(stoneMat);
305+
stoneMesh->add_material(stoneMat);
306306
stoneMesh->set_name("Stone Lantern");
307307
stoneMesh->set_position({2.5, -2.67, 9.5});
308308
stoneMesh->set_rotation({0.0, 107.0f, 0.0f});

examples/rotating-kabuto/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int main() {
5656

5757
Core::PhysicalMaterial* material = new Core::PhysicalMaterial();
5858
material->set_albedo(Vec4{1.0});
59-
kabuto->push_material(material);
59+
kabuto->add_material(material);
6060

6161
scene->add(kabuto);
6262

include/engine/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
// Max object ocurrence
9898
#define ENGINE_MAX_OBJECTS 100
9999
#define ENGINE_MAX_LIGHTS 50
100+
#define MAX_TEXTURES_PER_MATERIAL 6
101+
#define MAX_TEXTURES ENGINE_MAX_OBJECTS * MAX_TEXTURES_PER_MATERIAL
100102

101103
// File terminations
102104
#define PLY "ply"

include/engine/core/materials/material.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class IMaterial
3434
std::string m_shaderPassID = {};
3535
Graphics::DescriptorSet m_textureDescriptor = {};
3636

37-
bool m_isDirty = true;
37+
bool m_isDirty = true;
3838

3939
friend class Renderer;
4040

@@ -52,13 +52,10 @@ class IMaterial
5252
~IMaterial() {
5353
}
5454

55-
virtual Graphics::MaterialUniforms get_uniforms() const = 0;
56-
57-
virtual std::unordered_map<int, ITexture*> get_textures() const = 0;
58-
59-
virtual std::unordered_map<int, bool> get_texture_binding_state() const = 0;
60-
61-
virtual void set_texture_binding_state(int id, bool state) = 0;
55+
virtual Graphics::MaterialUniforms get_uniforms() const = 0;
56+
virtual std::unordered_map<int, ITexture*> get_textures() const = 0;
57+
virtual std::unordered_map<int, bool> get_texture_binding_state() const = 0;
58+
virtual void set_texture_binding_state(int id, bool state) = 0;
6259

6360
virtual std::string get_shaderpass_ID() const {
6461
return m_shaderPassID;

include/engine/core/passes/forward_pass.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ForwardPass final : public BaseGraphicPass
3030
struct FrameDescriptors {
3131
Graphics::DescriptorSet globalDescritor;
3232
Graphics::DescriptorSet objectDescritor;
33+
Graphics::DescriptorSet textureDescriptor;
3334
};
3435
std::vector<FrameDescriptors> m_descriptors;
3536

@@ -50,21 +51,20 @@ class ForwardPass final : public BaseGraphicPass
5051
- Depth
5152
*/
5253
ForwardPass(const ptr<Graphics::Device>& device,
53-
const PassLinkage<3, 3>& config,
54-
Extent2D extent,
55-
ColorFormatType colorFormat,
56-
ColorFormatType depthFormat,
57-
MSAASamples samples,
58-
bool isDefault = false)
54+
const PassLinkage<3, 3>& config,
55+
Extent2D extent,
56+
ColorFormatType colorFormat,
57+
ColorFormatType depthFormat,
58+
MSAASamples samples,
59+
bool isDefault = false)
5960
: BaseGraphicPass(device, extent, 1, 1, true, isDefault, "FORWARD")
6061
, m_colorFormat(colorFormat)
6162
, m_depthFormat(depthFormat)
6263
, m_aa(samples) {
6364
BasePass::store_attachments<3, 3>(config);
6465
}
6566

66-
void setup_out_attachments(std::vector<Graphics::AttachmentConfig>& attachments,
67-
std::vector<Graphics::SubPassDependency>& dependencies) override;
67+
void setup_out_attachments(std::vector<Graphics::AttachmentConfig>& attachments, std::vector<Graphics::SubPassDependency>& dependencies) override;
6868

6969
void create_framebuffer() override;
7070

include/engine/core/passes/geometry_pass.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class GeometryPass final : public BaseGraphicPass
2828
struct FrameDescriptors {
2929
Graphics::DescriptorSet globalDescritor;
3030
Graphics::DescriptorSet objectDescritor;
31+
Graphics::DescriptorSet textureDescritor;
3132
};
3233
std::vector<FrameDescriptors> m_descriptors;
3334

34-
void setup_material_descriptor(IMaterial* mat);
35+
void setup_material_descriptor(IMaterial* mat, uint32_t meshIdx);
3536

3637
public:
3738
/*
@@ -49,7 +50,11 @@ class GeometryPass final : public BaseGraphicPass
4950
- Velocity + Emissive buffer
5051
- Depth buffer
5152
*/
52-
GeometryPass(const ptr<Graphics::Device>& device, const PassLinkage<3, 5>& config, Extent2D extent, ColorFormatType floatingPointFormat, ColorFormatType depthFormat)
53+
GeometryPass(const ptr<Graphics::Device>& device,
54+
const PassLinkage<3, 5>& config,
55+
Extent2D extent,
56+
ColorFormatType floatingPointFormat,
57+
ColorFormatType depthFormat)
5358
: BaseGraphicPass(device, extent, 1, 1, true, false, "GEOMETRY")
5459
, m_depthFormat(depthFormat)
5560
, m_floatFormat(floatingPointFormat) {

include/engine/core/passes/voxelization_pass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ class VoxelizationPass final : public BaseGraphicPass
3333
struct FrameDescriptors {
3434
Graphics::DescriptorSet globalDescritor;
3535
Graphics::DescriptorSet objectDescritor;
36+
Graphics::DescriptorSet textureDescritor;
3637
};
3738
std::vector<FrameDescriptors> m_descriptors;
3839

3940
void create_voxelization_image();
40-
void setup_material_descriptor(IMaterial* mat);
41+
void setup_material_descriptor(IMaterial* mat, uint32_t meshIdx);
4142

4243
public:
4344
/*

include/engine/core/scene/mesh.h

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ struct BoundingVolume {
3535
: TYPE(t)
3636
, obj(o) {
3737
}
38+
~BoundingVolume() {
39+
}
3840

3941
virtual void setup(Mesh* const mesh) = 0;
4042

@@ -84,7 +86,7 @@ Class used to represent a 3D model instance.
8486
class Mesh : public Object3D
8587
{
8688
protected:
87-
std::vector<Geometry*> m_geometry;
89+
Geometry* m_geometry;
8890
std::vector<IMaterial*> m_material;
8991

9092
BV* m_volume = nullptr;
@@ -111,25 +113,26 @@ class Mesh : public Object3D
111113
: Object3D("Mesh #" + std::to_string(Mesh::m_instanceCount), ObjectType::MESH)
112114
, m_volume(nullptr) {
113115
Mesh::m_instanceCount++;
114-
m_geometry.push_back(geom);
116+
m_geometry = geom;
115117
m_material.push_back(mat);
116118
}
117119
~Mesh() {
118120
// delete m_geometry;
119121
}
120-
/**
121-
* Returns the geometry in the slot.
122-
*/
123-
inline Geometry* const get_geometry(size_t id = 0) const {
124-
return m_geometry.size() >= id + 1 ? m_geometry[id] : nullptr;
125-
}
126-
inline std::vector<Geometry*> get_geometries() const {
122+
123+
inline Geometry* const get_geometry() const {
127124
return m_geometry;
128-
};
129-
/**
130-
* Change the geometry in the given slot and returns the old one ref.
125+
}
126+
inline void set_geometry(Geometry* g) {
127+
m_geometry = g;
128+
setup_volume();
129+
}
130+
/*
131+
* Adds this material in the next free slot
131132
*/
132-
Geometry* change_geometry(Geometry* g, size_t id = 0);
133+
inline void add_material(IMaterial* m) {
134+
m_material.push_back(m);
135+
}
133136
/**
134137
* Returns the material in the slot.
135138
*/
@@ -142,29 +145,10 @@ class Mesh : public Object3D
142145
/**
143146
* Change the material in the given slot and returns the old one ref.
144147
*/
145-
IMaterial* change_material(IMaterial* m, size_t id = 0);
146-
/*
147-
* Adds this geometry in the next free slot. It is important to set correctly the id of the material slot this
148-
* geometry is pointing.
149-
*/
150-
inline void push_geometry(Geometry* g) {
151-
m_geometry.push_back(g);
152-
setup_volume();
153-
}
154-
/*
155-
* Adds this material in the next free slot
156-
*/
157-
inline void push_material(IMaterial* m) {
158-
m_material.push_back(m);
159-
}
160-
161-
inline size_t get_num_geometries() const {
162-
return m_geometry.size();
163-
}
148+
IMaterial* change_material(IMaterial* m, size_t id = 0);
164149
inline size_t get_num_materials() const {
165150
return m_material.size();
166151
}
167-
168152
inline void cast_shadows(bool op) {
169153
m_castShadows = op;
170154
}
@@ -213,13 +197,6 @@ class Mesh : public Object3D
213197
m_fileRoute = r;
214198
}
215199

216-
inline size_t get_material_ID(size_t geometrySlot) const {
217-
return m_geometry[geometrySlot]->get_material_ID();
218-
}
219-
inline void set_material_ID(size_t geometrySlot, size_t materialSlot) {
220-
m_geometry[geometrySlot]->set_material_ID(materialSlot);
221-
}
222-
223200
void setup_volume(VolumeType type = VolumeType::SPHERE_VOLUME);
224201
Mesh* clone() const;
225202
};

0 commit comments

Comments
 (0)