Migrate GLSL to Slang, text shader

This commit is contained in:
2026-09-12 23:48:30 +02:00
parent 8d5a2db342
commit f7576a941d
30 changed files with 868 additions and 632 deletions

View File

@@ -55,6 +55,14 @@ const chunk_descriptor_pool = 1024;
const camera_near_plane = 0.1;
const point_lights_data = [_]shaders.PointLight{};
const directional_lights_data = [_]shaders.DirectionalLight{
.{
.directionWS = .unit_nz,
.color = .init(0.3, 0.3, 0.3),
},
};
pub fn init() !Game {
const allocator_general = ctx.allocator_general;
const allocator_frame = ctx.allocator_frame;
@@ -158,13 +166,9 @@ pub fn init() !Game {
errdefer engine.destroyPipelineLayout(pipeline_layout);
engine.setObjectName(pipeline_layout, "PL Main", .{});
const vertex_shader = try engine.createShaderModule(.{ .code = &shaders.main_vert_spv });
defer engine.destroyShaderModule(vertex_shader);
engine.setObjectName(vertex_shader, "SM main_vert", .{});
const fragment_shader = try engine.createShaderModule(.{ .code = &shaders.main_frag_spv });
defer engine.destroyShaderModule(fragment_shader);
engine.setObjectName(fragment_shader, "SM main_frag", .{});
const shader = try engine.createShaderModule(.{ .code = &shaders.main_spv });
defer engine.destroyShaderModule(shader);
engine.setObjectName(shader, "SM main", .{});
var vertex_buffer = try shaders.VertexBuffer.init(.{
.usage = .vertex,
@@ -267,13 +271,13 @@ pub fn init() !Game {
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = vertex_shader,
.name = "main",
.module = shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = fragment_shader,
.name = "main",
.module = shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{
@@ -609,22 +613,8 @@ pub fn init() !Game {
});
}
const point_lights_data: []const shaders.PointLight = &.{};
try point_lights.write(.{
.header = @intCast(point_lights_data.len),
.elements = point_lights_data,
});
const directional_lights_data: []const shaders.DirectionalLight = &.{
.{
.directionWS = .unit_nz,
.color = .init(0.3, 0.3, 0.3),
},
};
try directional_lights.write(.{
.header = @intCast(directional_lights_data.len),
.elements = directional_lights_data,
});
try point_lights.write(.{ .elements = &point_lights_data });
try directional_lights.write(.{ .elements = &directional_lights_data });
var skybox = try Skybox.load("skybox.hdr", 512, global_uniforms.buffer);
errdefer skybox.deinit();
@@ -857,6 +847,9 @@ fn render(self: *Game) !void {
.matrixWStoVS = matrix_ws_to_vs,
.matrixVStoCS = matrix_vs_to_cs,
.matrixSSPXtoCS = matrix_sspx_to_cs,
.pointLightCount = point_lights_data.len,
.directionalLightCount = directional_lights_data.len,
.ambientLight = ambient_light,
};

View File

@@ -156,13 +156,9 @@ pub fn init() !*Gui {
errdefer engine.destroyPipelineLayout(box_pipeline_layout);
engine.setObjectName(box_pipeline_layout, "PL GUI Box", .{});
const box_vertex_shader = try engine.createShaderModule(.{ .code = &shaders.gui_box_vert_spv });
defer engine.destroyShaderModule(box_vertex_shader);
engine.setObjectName(box_vertex_shader, "SM gui_box_vert", .{});
const box_fragment_shader = try engine.createShaderModule(.{ .code = &shaders.gui_box_frag_spv });
defer engine.destroyShaderModule(box_fragment_shader);
engine.setObjectName(box_fragment_shader, "SM gui_box_frag", .{});
const box_shader = try engine.createShaderModule(.{ .code = &shaders.gui_box_spv });
defer engine.destroyShaderModule(box_shader);
engine.setObjectName(box_shader, "SM gui_box", .{});
var index_buffer = try shaders.IndexBuffer.init(.{
.usage = .index,
@@ -179,13 +175,13 @@ pub fn init() !*Gui {
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = box_vertex_shader,
.name = "main",
.module = box_shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = box_fragment_shader,
.name = "main",
.module = box_shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{},

View File

@@ -261,6 +261,26 @@ pub fn load(
});
errdefer engine.destroyImageView(cubemap_image_view);
// NOTE Because slang cannot conceive of such a thing as RWTextureCube
// (equivalent to imageCube in GLSL, which docs claim exists, but it
// doesn't), we have to resort to RWTexture2DArray (WTexture2DArray to be
// specific) in the compute shader. We have to make another image view with
// appropriate view_type to pass validation.
const cubemap_image_view_2d_array = try engine.createImageView(.{
.image = cubemap_image,
.view_type = .@"2d_array",
.format = .r16g16b16a16_sfloat,
.subresource_range = .{
.aspect_mask = .{ .color_bit = true },
.base_mip_level = 0,
.level_count = 1,
.base_array_layer = 0,
.layer_count = 6,
},
});
defer engine.destroyImageView(cubemap_image_view_2d_array);
// --- PIPELINE AND DESCRIPTORS --------------------------------------------
const sampler = try engine.createSampler(.{
@@ -312,7 +332,7 @@ pub fn load(
});
defer engine.destroyPipelineLayout(compute_pipeline_layout);
const compute_shader = try engine.createShaderModule(.{ .code = &shaders.equirect_to_cube_comp_spv });
const compute_shader = try engine.createShaderModule(.{ .code = &shaders.equirect_to_cube_spv });
defer engine.destroyShaderModule(compute_shader);
var compute_pipeline: vk.Pipeline = undefined;
@@ -380,7 +400,7 @@ pub fn load(
.image = &.{
.{
.sampler = .null_handle,
.image_view = cubemap_image_view,
.image_view = cubemap_image_view_2d_array,
.image_layout = .general,
},
},
@@ -622,23 +642,20 @@ pub fn load(
},
});
const vertex_shader = try engine.createShaderModule(.{ .code = &shaders.skybox_vert_spv });
defer engine.destroyShaderModule(vertex_shader);
const fragment_shader = try engine.createShaderModule(.{ .code = &shaders.skybox_frag_spv });
defer engine.destroyShaderModule(fragment_shader);
const shader = try engine.createShaderModule(.{ .code = &shaders.skybox_spv });
defer engine.destroyShaderModule(shader);
const pipeline = try engine.createGraphicsPipeline(.{
.stages = &.{
.{
.stage = .{ .vertex_bit = true },
.module = vertex_shader,
.name = "main",
.module = shader,
.name = "vertexMain",
},
.{
.stage = .{ .fragment_bit = true },
.module = fragment_shader,
.name = "main",
.module = shader,
.name = "fragmentMain",
},
},
.vertex_input_state = .{

View File

@@ -8,8 +8,8 @@ const Textures = @import("engine/Textures.zig");
pub const VertexBuffer = GenericBuffer(void, Vertex);
pub const IndexBuffer = GenericBuffer(void, Index);
pub const GlobalUniformsBuffer = GenericBuffer(GlobalUniforms, void);
pub const PointLightBuffer = GenericBuffer(u32, PointLight);
pub const DirectionalLightBuffer = GenericBuffer(u32, DirectionalLight);
pub const PointLightBuffer = GenericBuffer(void, PointLight);
pub const DirectionalLightBuffer = GenericBuffer(void, DirectionalLight);
pub const MaterialBuffer = GenericBuffer(void, Material);
pub const ObjectUniformsBuffer = GenericBuffer(void, ObjectUniforms);
@@ -47,6 +47,9 @@ pub const GlobalUniforms = extern struct {
matrixWStoVS: vm.Matrix4x4,
matrixVStoCS: vm.Matrix4x4,
matrixSSPXtoCS: vm.Matrix3x2,
pointLightCount: u32,
directionalLightCount: u32,
ambientLight: vm.Vector3,
};
@@ -96,10 +99,7 @@ pub const ObjectUniforms = extern struct {
material: Materials.Id,
};
pub const equirect_to_cube_comp_spv align(4) = @embedFile("shaders/equirect_to_cube.comp").*;
pub const gui_box_vert_spv align(4) = @embedFile("shaders/gui_box.vert").*;
pub const gui_box_frag_spv align(4) = @embedFile("shaders/gui_box.frag").*;
pub const main_vert_spv align(4) = @embedFile("shaders/main.vert").*;
pub const main_frag_spv align(4) = @embedFile("shaders/main.frag").*;
pub const skybox_vert_spv align(4) = @embedFile("shaders/skybox.vert").*;
pub const skybox_frag_spv align(4) = @embedFile("shaders/skybox.frag").*;
pub const equirect_to_cube_spv align(4) = @embedFile("shaders/equirect_to_cube").*;
pub const gui_box_spv align(4) = @embedFile("shaders/gui_box").*;
pub const main_spv align(4) = @embedFile("shaders/main").*;
pub const skybox_spv align(4) = @embedFile("shaders/skybox").*;