Skip to content

Commit

Permalink
Merge pull request #56 from Interrupt/features/shader-error-handling
Browse files Browse the repository at this point in the history
Better shader error handling: Shader creation returns an error union now instead of null
  • Loading branch information
Interrupt authored Sep 24, 2024
2 parents fdbdcc2 + c594177 commit d5cd402
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 110 deletions.
5 changes: 3 additions & 2 deletions src/examples/collision.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const Vec2 = math.Vec2;
const Rect = delve.spatial.Rect;
const TextureRegion = delve.graphics.sprites.TextureRegion;

const def_shader = delve.shaders.default;

var shader_default: graphics.Shader = undefined;
var sprite_batch: batcher.SpriteBatcher = undefined;

Expand Down Expand Up @@ -58,14 +60,13 @@ pub fn registerModule() !void {

fn on_init() !void {
debug.log("Collision example module initializing", .{});
shader_default = try graphics.Shader.initDefault(.{});

sprite_batch = batcher.SpriteBatcher.init(.{}) catch {
debug.showErrorScreen("Fatal error during batch init!");
return;
};

shader_default = graphics.Shader.initDefault(.{});

graphics.setClearColor(colors.examples_bg_light);
}

Expand Down
2 changes: 1 addition & 1 deletion src/examples/fonts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn on_init() !void {
_ = try delve.fonts.loadFont("IBMPlexSerif", "assets/fonts/IBMPlexSerif-Regular.ttf", 1024, 200);

// make a shader with alpha blending
shader_blend = graphics.Shader.initDefault(.{ .blend_mode = graphics.BlendMode.BLEND });
shader_blend = try graphics.Shader.initDefault(.{ .blend_mode = graphics.BlendMode.BLEND });
}

fn on_tick(delta: f32) void {
Expand Down
2 changes: 1 addition & 1 deletion src/examples/forest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn on_init() !void {
tex_treesheet = graphics.Texture.init(&treesheet_img);

// make our default shader
shader_blend = graphics.Shader.initDefault(.{ .blend_mode = .NONE, .cull_mode = .NONE });
shader_blend = try graphics.Shader.initDefault(.{ .blend_mode = .NONE, .cull_mode = .NONE });

// set the sky color
graphics.setClearColor(sky_color);
Expand Down
2 changes: 1 addition & 1 deletion src/examples/frustums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn main() !void {
}

pub fn on_init() !void {
shader = delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh).?;
shader = try delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);

// Create some materials
material_frustum = try delve.platform.graphics.Material.init(.{
Expand Down
4 changes: 2 additions & 2 deletions src/examples/lighting.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ fn on_init() !void {
camera.direction = Vec3.new(0.0, 0.0, 1.0);

// make shaders for skinned and unskinned meshes
skinned_shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = skinned_mesh.getSkinnedShaderAttributes() }, skinned_lit_shader).?;
static_shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, lit_shader).?;
skinned_shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = skinned_mesh.getSkinnedShaderAttributes() }, skinned_lit_shader);
static_shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, lit_shader);

var base_img: images.Image = try images.loadFile(mesh_texture_file);
defer base_img.deinit();
Expand Down
4 changes: 2 additions & 2 deletions src/examples/meshbuilder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ pub fn on_init() !void {
defer img.deinit();
const tex = graphics.Texture.init(&img);

const shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);
const shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);

// Create a material out of the texture
material = try graphics.Material.init(.{
.shader = shader.?,
.shader = shader,
.own_shader = true,
.texture_0 = tex,
.samplers = &[_]graphics.FilterMode{.NEAREST},
Expand Down
8 changes: 1 addition & 7 deletions src/examples/meshes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ fn on_init() !void {
const tex_emissive = graphics.Texture.init(&emissive_img);

// Make our emissive shader from one that is pre-compiled
const loaded_shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = mesh.getShaderAttributes() }, emissive_shader_builtin);

if (loaded_shader == null) {
debug.log("Could not get emissive shader", .{});
return;
}
shader = loaded_shader.?;
shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = mesh.getShaderAttributes() }, emissive_shader_builtin);

// Create a material out of our shader and textures
material = try graphics.Material.init(.{
Expand Down
2 changes: 1 addition & 1 deletion src/examples/quakemap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn on_init() !void {
var err: delve.utils.quakemap.ErrorInfo = undefined;
quake_map = try delve.utils.quakemap.QuakeMap.read(allocator, test_map_file, map_transform, &err);

shader = graphics.Shader.initDefault(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() });
shader = try graphics.Shader.initDefault(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() });

// Create a material out of the texture
fallback_material = try graphics.Material.init(.{
Expand Down
2 changes: 1 addition & 1 deletion src/examples/rays.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn main() !void {
}

pub fn on_init() !void {
const shader = delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);
const shader = try delve.platform.graphics.Shader.initFromBuiltin(.{ .vertex_attributes = delve.graphics.mesh.getShaderAttributes() }, delve.shaders.default_mesh);

// Create some materials
material_frustum = try delve.platform.graphics.Material.init(.{
Expand Down
10 changes: 3 additions & 7 deletions src/examples/skinned-meshes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,19 @@ fn on_init() !void {
camera.direction = Vec3.new(0.0, 0.0, 1.0);

// Make our emissive shader from one that is pre-compiled
const shader = graphics.Shader.initFromBuiltin(.{ .vertex_attributes = skinned_mesh.getSkinnedShaderAttributes() }, shader_builtin);

if (shader == null) {
debug.log("Could not get shader", .{});
return;
}
const shader = try graphics.Shader.initFromBuiltin(.{ .vertex_attributes = skinned_mesh.getSkinnedShaderAttributes() }, shader_builtin);

var base_img: images.Image = images.loadFile(mesh_texture_file) catch {
debug.log("Assets: Error loading image asset: {s}", .{mesh_texture_file});
return;
};
defer base_img.deinit();

const tex_base = graphics.Texture.init(&base_img);

// Create a material out of our shader and textures
material = try delve.platform.graphics.Material.init(.{
.shader = shader.?,
.shader = shader,
.own_shader = true,
.texture_0 = tex_base,
.texture_1 = delve.platform.graphics.createSolidTexture(0x00000000),
Expand Down
4 changes: 2 additions & 2 deletions src/examples/sprites.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ fn on_init() !void {
texture_2 = graphics.Texture.init(&test_image_2);

// make some shaders for testing
shader_opaque = graphics.Shader.initDefault(.{});
shader_blend = graphics.Shader.initDefault(.{ .blend_mode = graphics.BlendMode.BLEND });
shader_opaque = try graphics.Shader.initDefault(.{});
shader_blend = try graphics.Shader.initDefault(.{ .blend_mode = graphics.BlendMode.BLEND });

// Create some test materials out of our shader and textures
test_material_1 = try graphics.Material.init(.{
Expand Down
2 changes: 1 addition & 1 deletion src/framework/graphics/shaders.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn loadFromYaml(cfg: graphics.ShaderConfig, file_path: []const u8) !?graphic
break;
}

return graphics.Shader.initFromShaderInfo(cfg, shader_info);
return try graphics.Shader.initFromShaderInfo(cfg, shader_info);
}

fn loadShaderSource(allocator: std.mem.Allocator, shader_path: []const u8) ![:0]const u8 {
Expand Down
Loading

0 comments on commit d5cd402

Please sign in to comment.