Skip to content

Commit 880b2ba

Browse files
authored
Merge pull request #52 from Interrupt/features/simple-materials-api
Updating the Materials API so that materials own pointers to their material params
2 parents 972b438 + ce89293 commit 880b2ba

15 files changed

Lines changed: 134 additions & 130 deletions

File tree

src/examples/frustums.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ pub fn on_init() !void {
4444
const shader = delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);
4545

4646
// Create some materials
47-
material_frustum = delve.platform.graphics.Material.init(.{
47+
material_frustum = try delve.platform.graphics.Material.init(.{
4848
.shader = shader,
4949
.texture_0 = delve.platform.graphics.createSolidTexture(0x66FFFFFF),
5050
.cull_mode = .NONE,
5151
.depth_write_enabled = false,
5252
.blend_mode = .BLEND,
5353
});
5454

55-
material_cube = delve.platform.graphics.Material.init(.{
55+
material_cube = try delve.platform.graphics.Material.init(.{
5656
.shader = shader,
5757
.texture_0 = delve.platform.graphics.tex_white,
5858
});
5959

60-
material_highlight = delve.platform.graphics.Material.init(.{
60+
material_highlight = try delve.platform.graphics.Material.init(.{
6161
.shader = shader,
6262
.texture_0 = delve.platform.graphics.createSolidTexture(0xFF0000CC),
6363
});
@@ -76,7 +76,7 @@ pub fn on_init() !void {
7676
return;
7777
};
7878

79-
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, &material_cube) catch {
79+
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, material_cube) catch {
8080
delve.debug.fatal("Could not create cube mesh!", .{});
8181
return;
8282
};
@@ -111,7 +111,7 @@ pub fn on_draw() void {
111111
const bounds = cube_mesh.bounds.translate(cube_pos);
112112

113113
if (frustum.containsBoundingBox(bounds)) {
114-
cube_mesh.drawWithMaterial(&material_highlight, view_mats, cube_model_matrix);
114+
cube_mesh.drawWithMaterial(material_highlight, view_mats, cube_model_matrix);
115115
} else {
116116
cube_mesh.draw(view_mats, cube_model_matrix);
117117
}
@@ -127,5 +127,5 @@ pub fn createFrustumMesh() !delve.graphics.mesh.Mesh {
127127

128128
try builder.addFrustum(secondary_camera.getViewFrustum(), delve.math.Mat4.identity, delve.colors.cyan);
129129

130-
return builder.buildMesh(&material_frustum);
130+
return builder.buildMesh(material_frustum);
131131
}

src/examples/lighting.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn on_init() !void {
9090
const tex_base = graphics.Texture.init(&base_img);
9191

9292
// Create a material out of our shader and textures
93-
skinned_mesh_material = delve.platform.graphics.Material.init(.{
93+
skinned_mesh_material = try delve.platform.graphics.Material.init(.{
9494
.shader = skinned_shader.?,
9595
.texture_0 = tex_base,
9696
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -103,7 +103,7 @@ fn on_init() !void {
103103
});
104104

105105
// Create a material out of the texture
106-
static_mesh_material = graphics.Material.init(.{
106+
static_mesh_material = try graphics.Material.init(.{
107107
.shader = static_shader.?,
108108
.texture_0 = delve.platform.graphics.createSolidTexture(0xFFFFFFFF),
109109
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -113,16 +113,16 @@ fn on_init() !void {
113113
});
114114

115115
// Load an animated mesh
116-
const loaded_mesh = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = &skinned_mesh_material });
116+
const loaded_mesh = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = skinned_mesh_material });
117117

118118
if (loaded_mesh == null) {
119119
debug.fatal("Could not load skinned mesh!", .{});
120120
return;
121121
}
122122

123123
// make some cubes
124-
cube1 = try delve.graphics.mesh.createCube(math.Vec3.new(0, -1.0, 0), math.Vec3.new(10.0, 0.25, 10.0), delve.colors.white, &static_mesh_material);
125-
cube2 = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2.0, 1.25, 1.0), delve.colors.white, &static_mesh_material);
124+
cube1 = try delve.graphics.mesh.createCube(math.Vec3.new(0, -1.0, 0), math.Vec3.new(10.0, 0.25, 10.0), delve.colors.white, static_mesh_material);
125+
cube2 = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2.0, 1.25, 1.0), delve.colors.white, static_mesh_material);
126126

127127
animated_mesh = loaded_mesh.?;
128128
animation = try animated_mesh.createAnimation(0, 1.0, true);
@@ -163,10 +163,10 @@ fn on_draw() void {
163163
const point_lights = &[_]delve.platform.graphics.PointLight{ point_light_1, point_light_2, point_light_3 };
164164

165165
// add the lights and camera to the materials
166-
static_mesh_material.params.point_lights = @constCast(point_lights);
167-
static_mesh_material.params.directional_light = directional_light;
168-
static_mesh_material.params.ambient_light = colors.Color.new(0.02, 0.02, 0.05, 1.0);
169-
skinned_mesh_material.params = static_mesh_material.params;
166+
static_mesh_material.state.params.point_lights = @constCast(point_lights);
167+
static_mesh_material.state.params.directional_light = directional_light;
168+
static_mesh_material.state.params.ambient_light = colors.Color.new(0.02, 0.02, 0.05, 1.0);
169+
skinned_mesh_material.state.params = static_mesh_material.state.params;
170170

171171
animated_mesh.draw(view_mats, model);
172172
cube1.draw(view_mats, Mat4.identity);

src/examples/meshbuilder.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn on_init() !void {
5252
const shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);
5353

5454
// Create a material out of the texture
55-
material = graphics.Material.init(.{
55+
material = try graphics.Material.init(.{
5656
.shader = shader,
5757
.texture_0 = tex,
5858
.samplers = &[_]graphics.FilterMode{.NEAREST},
@@ -62,19 +62,19 @@ pub fn on_init() !void {
6262
camera = delve.graphics.camera.Camera.initThirdPerson(90.0, 0.01, 20.0, 5.0, math.Vec3.up);
6363

6464
// make a cube
65-
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, &material) catch {
65+
cube1 = delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), math.Vec3.new(2, 3, 1), delve.colors.white, material) catch {
6666
delve.debug.log("Could not create cube!", .{});
6767
return;
6868
};
6969

7070
// and another
71-
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.green, &material) catch {
71+
cube2 = delve.graphics.mesh.createCube(math.Vec3.new(3, 0, -1), math.Vec3.new(1, 1, 2), delve.colors.green, material) catch {
7272
delve.debug.log("Could not create cube!", .{});
7373
return;
7474
};
7575

7676
// and then a floor
77-
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, -2, 0), math.Vec3.new(12, 0.25, 12), delve.colors.red, &material) catch {
77+
cube3 = delve.graphics.mesh.createCube(math.Vec3.new(0, -2, 0), math.Vec3.new(12, 0.25, 12), delve.colors.red, material) catch {
7878
delve.debug.log("Could not create cube!", .{});
7979
return;
8080
};

src/examples/meshes.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ fn on_init() !void {
9595
}
9696

9797
// Create a material out of our shader and textures
98-
material = graphics.Material.init(.{
98+
material = try graphics.Material.init(.{
9999
.shader = shader.?,
100100
.texture_0 = tex_base,
101101
.texture_1 = tex_emissive,
102102
});
103103

104104
// Load our mesh!
105-
mesh_test = mesh.Mesh.initFromFile(delve.mem.getAllocator(), "assets/meshes/SciFiHelmet.gltf", .{ .material = &material });
105+
mesh_test = mesh.Mesh.initFromFile(delve.mem.getAllocator(), "assets/meshes/SciFiHelmet.gltf", .{ .material = material });
106106
}
107107

108108
fn on_tick(delta: f32) void {
@@ -126,7 +126,7 @@ fn on_draw() void {
126126
model = model.mul(Mat4.rotate(time * 0.6, Vec3.new(0.0, 1.0, 0.0)));
127127

128128
const sin_val = std.math.sin(time * 0.006) + 0.5;
129-
mesh_test.?.material.params.draw_color = Color.new(sin_val, sin_val, sin_val, 1.0);
129+
mesh_test.?.material.state.params.draw_color = Color.new(sin_val, sin_val, sin_val, 1.0);
130130
mesh_test.?.draw(view_mats, model);
131131

132132
model = Mat4.translate(Vec3.new(-2.0, 0.0, 0.0));

src/examples/quakemap.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn on_init() !void {
147147
quake_map = try delve.utils.quakemap.QuakeMap.read(allocator, test_map_file, map_transform, &err);
148148

149149
// Create a material out of the texture
150-
fallback_material = graphics.Material.init(.{
150+
fallback_material = try graphics.Material.init(.{
151151
.shader = graphics.Shader.initDefault(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }),
152152
.texture_0 = fallback_tex,
153153
.samplers = &[_]graphics.FilterMode{.NEAREST},
@@ -190,7 +190,7 @@ pub fn on_init() !void {
190190
defer tex_img.deinit();
191191
const tex = graphics.Texture.init(&tex_img);
192192

193-
const mat = graphics.Material.init(.{
193+
const mat = try graphics.Material.init(.{
194194
.shader = shader,
195195
.samplers = &[_]graphics.FilterMode{.NEAREST},
196196
.texture_0 = tex,
@@ -207,7 +207,7 @@ pub fn on_init() !void {
207207
entity_meshes = try quake_map.buildEntityMeshes(allocator, math.Mat4.identity, &materials, &fallback_quake_material);
208208

209209
// make a bounding box cube
210-
cube_mesh = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), bounding_box_size, delve.colors.red, &fallback_material);
210+
cube_mesh = try delve.graphics.mesh.createCube(math.Vec3.new(0, 0, 0), bounding_box_size, delve.colors.red, fallback_material);
211211

212212
// set a bg color
213213
delve.platform.graphics.setClearColor(delve.colors.examples_bg_light);

src/examples/rays.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ pub fn on_init() !void {
4646
const shader = delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);
4747

4848
// Create some materials
49-
material_frustum = delve.platform.graphics.Material.init(.{
49+
material_frustum = try delve.platform.graphics.Material.init(.{
5050
.shader = shader,
5151
.texture_0 = delve.platform.graphics.createSolidTexture(0x66FFFFFF),
5252
.cull_mode = .NONE,
5353
.depth_write_enabled = false,
5454
.blend_mode = .BLEND,
5555
});
5656

57-
material_cube = delve.platform.graphics.Material.init(.{
57+
material_cube = try delve.platform.graphics.Material.init(.{
5858
.shader = shader,
5959
.texture_0 = delve.platform.graphics.tex_white,
6060
});
6161

62-
material_highlight = delve.platform.graphics.Material.init(.{
62+
material_highlight = try delve.platform.graphics.Material.init(.{
6363
.shader = shader,
6464
.texture_0 = delve.platform.graphics.createSolidTexture(0xFF0000CC),
6565
});
6666

67-
material_hitpoint = delve.platform.graphics.Material.init(.{
67+
material_hitpoint = try delve.platform.graphics.Material.init(.{
6868
.shader = shader,
6969
.texture_0 = delve.platform.graphics.createSolidTexture(0xFFFF0000),
7070
});
@@ -76,17 +76,17 @@ pub fn on_init() !void {
7676
camera.position = delve.math.Vec3.new(0, 30, 32);
7777
camera.pitch_angle = -50.0;
7878

79-
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, &material_cube) catch {
79+
cube_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(1, 1, 1), delve.colors.white, material_cube) catch {
8080
delve.debug.fatal("Could not create cube mesh!", .{});
8181
return;
8282
};
8383

84-
hit_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(0.5, 0.5, 0.5), delve.colors.white, &material_cube) catch {
84+
hit_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(0, 0, 0), delve.math.Vec3.new(0.5, 0.5, 0.5), delve.colors.white, material_cube) catch {
8585
delve.debug.fatal("Could not create cube mesh!", .{});
8686
return;
8787
};
8888

89-
ray_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(50, 0, 0), delve.math.Vec3.new(100, 0.1, 0.1), delve.colors.red, &material_cube) catch {
89+
ray_mesh = delve.graphics.mesh.createCube(delve.math.Vec3.new(50, 0, 0), delve.math.Vec3.new(100, 0.1, 0.1), delve.colors.red, material_cube) catch {
9090
delve.debug.fatal("Could not create cube mesh!", .{});
9191
return;
9292
};
@@ -129,10 +129,10 @@ pub fn on_draw() void {
129129
const rayhit = ray.intersectOrientedBoundingBox(bounds);
130130

131131
if (rayhit != null) {
132-
cube_mesh.drawWithMaterial(&material_highlight, view_mats, cube_model_matrix);
132+
cube_mesh.drawWithMaterial(material_highlight, view_mats, cube_model_matrix);
133133

134134
const hit_model_matrix = delve.math.Mat4.translate(rayhit.?.hit_pos);
135-
hit_mesh.drawWithMaterial(&material_hitpoint, view_mats, hit_model_matrix);
135+
hit_mesh.drawWithMaterial(material_hitpoint, view_mats, hit_model_matrix);
136136
} else {
137137
cube_mesh.draw(view_mats, cube_model_matrix);
138138
}

src/examples/skinned-meshes.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn on_init() !void {
9292
const tex_base = graphics.Texture.init(&base_img);
9393

9494
// Create a material out of our shader and textures
95-
material = delve.platform.graphics.Material.init(.{
95+
material = try delve.platform.graphics.Material.init(.{
9696
.shader = shader.?,
9797
.texture_0 = tex_base,
9898
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
@@ -102,7 +102,7 @@ fn on_init() !void {
102102
});
103103

104104
// Load our mesh!
105-
const loaded = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = &material });
105+
const loaded = skinned_mesh.SkinnedMesh.initFromFile(delve.mem.getAllocator(), mesh_file, .{ .material = material });
106106

107107
if (loaded == null) {
108108
debug.fatal("Could not load skinned mesh!", .{});

src/examples/sprite-animation.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn on_init() !void {
7171
shader_default = loaded_shader.?;
7272

7373
// make a material to draw with
74-
test_material = graphics.Material.init(.{
74+
test_material = try graphics.Material.init(.{
7575
.shader = shader_default,
7676
.texture_0 = sprite_texture,
7777
.cull_mode = .BACK,
@@ -115,7 +115,7 @@ fn on_draw() void {
115115
sprite_batch.reset();
116116

117117
// make sure we are using our material
118-
sprite_batch.useMaterial(&test_material);
118+
sprite_batch.useMaterial(test_material);
119119

120120
// add our sprite rectangle
121121
const rect = delve.spatial.Rect.new(cur_frame.offset, cur_frame.size);

src/examples/sprites.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ fn on_init() !void {
9494
shader_blend = graphics.Shader.initDefault(.{ .blend_mode = graphics.BlendMode.BLEND });
9595

9696
// Create some test materials out of our shader and textures
97-
test_material_1 = graphics.Material.init(.{
97+
test_material_1 = try graphics.Material.init(.{
9898
.shader = shader_opaque,
9999
.texture_0 = graphics.tex_white,
100100
.cull_mode = .BACK,
101101
.blend_mode = .BLEND,
102102
});
103103

104-
test_material_2 = graphics.Material.init(.{
104+
test_material_2 = try graphics.Material.init(.{
105105
.shader = shader_opaque,
106106
.texture_0 = texture_1,
107107
.cull_mode = .BACK,
@@ -177,12 +177,12 @@ fn pre_draw() void {
177177

178178
// test using materials as well!
179179
// test a filled rectangle
180-
test_batch.useMaterial(&test_material_1);
180+
test_batch.useMaterial(test_material_1);
181181
test_batch.setTransformMatrix(math.Mat4.translate(math.vec3(0, 0, -0.001)));
182182

183183
test_batch.addRectangle(rect1, sprites.TextureRegion.default(), colors.cyan.mul(Color{ .a = 0.75 }));
184184

185-
test_batch.useMaterial(&test_material_2);
185+
test_batch.useMaterial(test_material_2);
186186
test_batch.setTransformMatrix(math.Mat4.translate(math.vec3(1, -1, -0.001)));
187187

188188
const rect3 = Rect.new(math.vec2(-1.0, 0), math.vec2(1, 1));

src/framework/graphics/batcher.zig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const BatcherConfig = struct {
3333
min_indices: usize = 128,
3434
texture: ?graphics.Texture = null,
3535
shader: ?graphics.Shader = null,
36-
material: ?*graphics.Material = null,
36+
material: ?graphics.Material = null,
3737
flip_tex_y: bool = false,
3838
};
3939

@@ -47,7 +47,7 @@ pub const SpriteBatcher = struct {
4747
current_batch_key: u64 = 0,
4848
current_tex: graphics.Texture = undefined,
4949
current_shader: graphics.Shader = undefined,
50-
current_material: ?*graphics.Material = null,
50+
current_material: ?graphics.Material = null,
5151

5252
// If we needed to make resources, we need to clean them up later too
5353
owned_texture: ?graphics.Texture = null,
@@ -94,7 +94,7 @@ pub const SpriteBatcher = struct {
9494
}
9595

9696
/// Switch the current batch to one for the given material
97-
pub fn useMaterial(self: *SpriteBatcher, material: *graphics.Material) void {
97+
pub fn useMaterial(self: *SpriteBatcher, material: graphics.Material) void {
9898
self.current_batch_key = makeSpriteBatchKeyFromMaterial(material);
9999
self.current_material = material;
100100
}
@@ -225,11 +225,8 @@ fn makeSpriteBatchKey(tex: graphics.Texture, shader: graphics.Shader) u64 {
225225
return tex.handle + (shader.handle * 1000000);
226226
}
227227

228-
fn makeSpriteBatchKeyFromMaterial(material: *const graphics.Material) u64 {
229-
// Hash the material, just like an auto map would.
230-
var hasher = Wyhash.init(0);
231-
autoHash(&hasher, material);
232-
return hasher.final();
228+
fn makeSpriteBatchKeyFromMaterial(material: graphics.Material) u64 {
229+
return @intCast(@intFromPtr(material.state));
233230
}
234231

235232
/// Handles drawing a batch of primitive shapes all with the same texture / shader
@@ -241,7 +238,7 @@ pub const Batcher = struct {
241238
index_pos: usize,
242239
bindings: graphics.Bindings,
243240
shader: graphics.Shader,
244-
material: ?*graphics.Material = null,
241+
material: ?graphics.Material = null,
245242
transform: Mat4 = Mat4.identity,
246243
flip_tex_y: bool = false,
247244

@@ -503,7 +500,7 @@ pub const Batcher = struct {
503500
if (self.material == null)
504501
return;
505502

506-
graphics.drawWithMaterial(&self.bindings, self.material.?, cam_matrices, model_matrix);
503+
graphics.drawWithMaterial(&self.bindings, &self.material.?, cam_matrices, model_matrix);
507504
}
508505

509506
/// Expand the buffers for this batch if needed to fit the new size

0 commit comments

Comments
 (0)