Skip to content

Commit e69a5ab

Browse files
committed
dev
1 parent bfe7737 commit e69a5ab

File tree

7 files changed

+191
-13
lines changed

7 files changed

+191
-13
lines changed

game/xmake.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ target("game", function()
22
set_kind("binary")
33
set_languages("cxxlatest", "clatest")
44

5-
add_rules("compile.shaders")
5+
add_rules("wgsl.compile.shaders")
66

77
add_rules(stormkit_rule_prefix .. "stormkit::application")
88
set_values("stormkit.components", { "stormkit", "log", "entities", "image", "wsi", "gpu", "lua" })
@@ -18,6 +18,8 @@ target("game", function()
1818

1919
add_deps("stormkit::engine")
2020

21+
add_packages("slang")
22+
2123
on_load(function(target)
2224
if get_config("devmode") then
2325
import("core.project.config")

shaders/sprite_renderer/quad_sprite.nzsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct VertOut {
1212
[location(0)] uv: vec2[f32]
1313
}
1414

15-
[layout(std140)]
15+
[layout(std140), spirv(RowMajor)]
1616
struct Camera {
1717
proj: mat4[f32],
1818
view: mat4[f32],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct VertexOut {
2+
float3 position: SV_Position;
3+
float2 uv;
4+
};
5+
6+
struct FragmentOut {
7+
float4 color: SV_Target<0>;
8+
}
9+
10+
[shader("vertex")]
11+
VertexOut vs_main() {
12+
VertexOutput out;
13+
out.color = float4(input.uv, 0., 1.);
14+
out.uv = input.uv;
15+
16+
return out;
17+
}
18+
19+
[shader("fragment")]
20+
FragmentOut frag_main(VertexOut in) {
21+
FragmentOut out;
22+
out.color = float4(input.uv, 0., 1.);
23+
24+
return out;
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct VertIn {
2+
@location(0) position: vec2f,
3+
@location(1) uv: vec2f
4+
}
5+
6+
struct VertOut {
7+
@builtin(position) position: vec4f,
8+
@location(0) uv: vec2f
9+
}
10+
11+
struct Camera {
12+
proj: mat4x4f,
13+
view: mat4x4f,
14+
}
15+
16+
@binding(0) @group(0)
17+
var<uniform> camera: Camera;
18+
19+
@vertex fn vert_main(input: VertIn) -> VertOut {
20+
var output: VertOut;
21+
22+
output.position = camera.proj * camera.view * vec4f(input.position, 1., 1.);
23+
output.uv = input.uv;
24+
25+
return output;
26+
}
27+
28+
struct FragIn {
29+
@location(0) uv: vec2f
30+
}
31+
32+
struct FragOut {
33+
@location(0) color: vec4f
34+
}
35+
36+
@fragment fn frag_main(input: FragIn) -> FragOut {
37+
var output: FragOut;
38+
39+
output.color = vec4f(input.uv, 1., 1.);
40+
41+
return output;
42+
}

src/sprite_renderer.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ namespace stormkit::engine {
131131
.input_assembly_state = { gpu::PrimitiveTopology::TRIANGLE_STRIP },
132132
.viewport_state = { .viewports = { window_viewport },
133133
.scissors = { scissor }, },
134+
.rasterization_state = { .cull_mode = gpu::CullModeFlag::NONE, },
134135
.color_blend_state = { .attachments = { { .blend_enable = true,
135136
.src_color_blend_factor = gpu::BlendFactor::SRC_ALPHA,
136137
.dst_color_blend_factor = gpu::BlendFactor::ONE_MINUS_SRC_ALPHA,
@@ -142,14 +143,15 @@ namespace stormkit::engine {
142143
.binding_descriptions = SPRITE_VERTEX_BINDING_DESCRIPTIONS | stdr::to<std::vector>(),
143144
.input_attribute_descriptions= SPRITE_VERTEX_ATTRIBUTE_DESCRIPTIONS | stdr::to<std::vector>(),
144145
},
145-
};
146+
}; // namespace stormkit::engine
146147

147148
m_render_data.descriptor_set_layout = Try(gpu::DescriptorSetLayout::create(
148149
device,
149150
into_dyn_array<
150151
gpu::DescriptorSetLayoutBinding>(Camera::layout_binding()
151152
// gpu::DescriptorSetLayoutBinding {
152-
// 1, gpu::DescriptorType::COMBINED_IMAGE_SAMPLER,
153+
// 1,
154+
// gpu::DescriptorType::COMBINED_IMAGE_SAMPLER,
153155
// gpu::ShaderStageFlag::FRAGMENT,
154156
// 1 }
155157
)));
@@ -171,10 +173,27 @@ namespace stormkit::engine {
171173
m_render_data
172174
.camera_descriptor_set = Try(m_render_data.descriptor_pool->create_descriptor_set(m_render_data.descriptor_set_layout));
173175

174-
m_camera.projection = math::orthographique(window_viewport.position.x,
175-
window_viewport.extent.width,
176-
window_viewport.position.y,
177-
window_viewport.extent.height);
176+
const auto ortho = [](auto& out, f32 left, f32 right, f32 bottom, f32 top) {
177+
out = math::fmat4::identity();
178+
179+
out[0, 0] = f32 { 2 } / (right - left);
180+
out[1, 1] = f32 { 2 } / (top - bottom);
181+
out[2, 2] = -f32 { 1 };
182+
183+
out[0, 3] = -(right + left) / (right - left);
184+
out[1, 3] = -(top + bottom) / (top - bottom);
185+
out[2, 3] = -f32 { 1 };
186+
};
187+
188+
ortho(m_camera.projection,
189+
window_viewport.position.x,
190+
window_viewport.extent.width,
191+
window_viewport.position.y,
192+
window_viewport.extent.height);
193+
// m_camera.projection = math::orthographique(window_viewport.position.x,
194+
// window_viewport.extent.width,
195+
// window_viewport.position.y,
196+
// window_viewport.extent.height);
178197
std::println("{}", m_camera.projection);
179198
Return {};
180199
}
@@ -276,8 +295,8 @@ namespace stormkit::engine {
276295

277296
std::array<SpriteVertex, 4> vertices = {
278297
SpriteVertex { { 0.f, 0.f }, { 0.f, 0.f } },
279-
SpriteVertex { { 0.f, 100.f }, { 0.f, 1.f } },
280298
SpriteVertex { { 100.f, 0.f }, { 1.f, 0.f } },
299+
SpriteVertex { { 0.f, 100.f }, { 0.f, 1.f } },
281300
SpriteVertex { { 100.f, 100.f }, { 1.f, 1.f } },
282301
};
283302
vertex_staging_buffer.upload(as_bytes(vertices));
@@ -287,7 +306,10 @@ namespace stormkit::engine {
287306
auto& camera_staging_buffer = frame_resources.get_buffer(data.camera_staging_buffer_id);
288307
const auto& camera_buffer = frame_resources.get_buffer(data.camera_buffer_id);
289308

290-
camera_staging_buffer.upload(as_bytes(m_camera));
309+
auto camera = auto(m_camera);
310+
camera.projection = math::transpose(camera.projection);
311+
camera.view = math::transpose(camera.view);
312+
camera_staging_buffer.upload(as_bytes(camera));
291313

292314
cmb.copy_buffer(camera_staging_buffer, camera_buffer, sizeof(Camera));
293315
});

xmake.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ add_requires(stormkit_dep_name, {
149149
if is_mode("debug") or is_mode("reldbg") then add_cxflags("clang::-ggdb3") end
150150

151151
namespace("stormkit", function()
152-
includes("xmake/rules/*.xmake.lua")
152+
includes("xmake/rules/slang.xmake.lua")
153153

154154
target("engine", function()
155155
set_kind("$(kind)")
156156
set_languages("cxxlatest", "clatest")
157157

158-
add_rules("compile.shaders")
158+
add_rules("wgsl.compile.shaders")
159159

160160
set_basename("stormkit-engine")
161161

@@ -166,12 +166,14 @@ namespace("stormkit", function()
166166

167167
add_files("modules/stormkit/**.cppm", { public = true })
168168
add_files("src/**.cpp")
169-
add_files("shaders/**.nzsl")
169+
add_files("shaders/**.wgsl")
170170

171171
add_defines("STORMKIT_ENGINE_BUILD", { public = false })
172172

173173
add_embeddirs("$(builddir)/shaders")
174174
add_cxflags("--embed-dir=$(builddir)/shaders")
175+
176+
add_packages("slang")
175177
end)
176178

177179
includes("game/xmake.lua")

xmake/rules/slang.xmake.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
add_requires("wgpu-native", {
2+
system = false,
3+
configs = {
4+
naga = true,
5+
},
6+
})
7+
8+
rule("wgsl.find_naga", function()
9+
on_config(function(target)
10+
import("core.project.project")
11+
import("core.tool.toolchain")
12+
import("lib.detect.find_tool")
13+
14+
-- on windows+asan we need run envs because of .dll dependencies which may be not part of the PATH
15+
local envs
16+
if is_plat("windows") then
17+
local msvc = target:toolchain("msvc")
18+
if msvc and msvc:check() then envs = msvc:runenvs() end
19+
end
20+
target:data_set("wgsl_envs", envs)
21+
22+
-- find wgsl binaries
23+
local wgsl = project.required_package("wgpu-native~host") or project.required_package("wgpu-native")
24+
local wgsldir
25+
if wgsl then
26+
wgsldir = path.join(wgsl:installdir(), "bin")
27+
local osenvs = os.getenvs()
28+
envs = envs or {}
29+
for env, values in pairs(wgsl:get("envs")) do
30+
local flatval = path.joinenv(values)
31+
local oldenv = envs[env] or osenvs[env]
32+
if not oldenv or oldenv == "" then
33+
envs[env] = flatval
34+
elseif not oldenv:startswith(flatval) then
35+
envs[env] = flatval .. path.envsep() .. oldenv
36+
end
37+
end
38+
end
39+
40+
local naga = find_tool("naga", { version = false, paths = wgsldir, envs = envs, check = "--help" })
41+
assert(naga, "naga not found! please install wgsl package with naga enabled")
42+
43+
target:data_set("naga", naga)
44+
target:data_set("naga_runenv", envs)
45+
end)
46+
end)
47+
48+
-- Compile shaders to includables headers
49+
rule("wgsl.compile.shaders", function()
50+
set_extensions(".wgsl")
51+
add_deps("wgsl.find_naga")
52+
before_buildcmd_file(function(target, batchcmds, shaderfile, opt)
53+
import("lib.detect.find_tool")
54+
import("utils.progress")
55+
56+
local outputdir = path.join(import("core.project.config").builddir(), "shaders")
57+
local naga = target:data("naga")
58+
local runenvs = target:data("naga_runenv")
59+
assert(naga, "naga not found! please install wgsl package with naga enabled")
60+
61+
local outputfile = path.join(outputdir or path.directory(shaderfile), path.basename(shaderfile) .. ".spv")
62+
63+
-- add commands
64+
if progress.apply_target then opt.progress = progress.apply_target(target, opt.progress) end
65+
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
66+
local argv = {
67+
"--spirv-version",
68+
"1.3",
69+
shaderfile,
70+
}
71+
if outputdir then
72+
batchcmds:mkdir(outputdir)
73+
local outputfile = path.join(outputdir, path.basename(shaderfile) .. ".spv")
74+
table.insert(argv, outputfile)
75+
end
76+
77+
batchcmds:vrunv(naga.program, argv, { curdir = ".", envs = runenvs })
78+
79+
-- add deps
80+
batchcmds:add_depfiles(shaderfile)
81+
batchcmds:add_depvalues(naga.version)
82+
batchcmds:set_depmtime(os.mtime(outputfile))
83+
batchcmds:set_depcache(target:dependfile(outputfile))
84+
end)
85+
end)

0 commit comments

Comments
 (0)