Shaders SPIR-V compilation in SDK

This commit is contained in:
legatt 2026-04-27 17:24:51 +03:00
parent 7ec8c82569
commit eb79ff9879
13 changed files with 176 additions and 8 deletions

View File

@ -1,4 +1,4 @@
{ {
"vertexShaderAssetID": "assets/shaders/provinces.vert", "vertexShaderAssetID": "assets/shaders/provinces-flat/provinces.vert",
"fragmentShaderAssetID": "assets/shaders/provinces.frag" "fragmentShaderAssetID": "assets/shaders/provinces-flat/provinces.frag"
} }

View File

@ -0,0 +1,10 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(fragColor, 1.0);
}

View File

@ -0,0 +1,35 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec2 position;
layout(binding = 0) uniform Camera {
mat4 viewProjection;
} camera;
layout(push_constant) uniform Model {
mat4 model;
} model;
struct Province {
uint regionId;
uint color;
};
layout(std430, binding = 1) readonly buffer Data {
Province provinces[];
};
layout(location = 0) out vec3 fragColor;
vec3 unpackColor(uint c) {
return vec3(
c & 0xFFu,
(c >> 8) & 0xFFu,
(c >> 16) & 0xFFu
) / 255.0;
}
void main() {
gl_Position = camera.viewProjection * vec4(position, 0.0, 1.0);
fragColor = unpackColor(provinces[gl_InstanceIndex].color);
}

View File

@ -0,0 +1,10 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(fragColor, 1.0);
}

View File

@ -0,0 +1,67 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec2 position;
layout(binding = 0) uniform Camera {
mat4 viewProjection;
} camera;
layout(push_constant) uniform Model {
mat4 model;
} model;
struct Province {
uint regionId;
uint color;
};
layout(std430, binding = 1) readonly buffer Data {
Province provinces[];
};
layout(location = 0) out vec3 fragColor;
const float PI = 3.141592653589793;
vec3 unpackColor(uint c) {
return vec3(
c & 0xFFu,
(c >> 8) & 0xFFu,
(c >> 16) & 0xFFu
) / 255.0;
}
vec2 toUV(vec2 pos)
{
vec2 uv = pos / vec2(5632.0, 2048.0);
uv.y = 1.0 - uv.y;
return uv; //TODO: Provide map size
}
vec3 mercatorToSphere(vec2 uv)
{
float lon = uv.x * 2.0 * PI - PI;
lon += PI;
float lat = PI * 0.5 - uv.y * PI;
float cosLat = cos(lat);
return vec3(
cosLat * cos(lon),
sin(lat),
cosLat * sin(lon)
);
}
void main() {
vec2 uv = toUV(position);
vec3 spherePos = mercatorToSphere(uv);
gl_Position = camera.viewProjection
* vec4(spherePos, 1.0);
//gl_Position = camera.viewProjection * vec4(position, 0.0, 1.0);
//fragColor = unpackColor(provinces[gl_InstanceIndex].color);
fragColor = unpackColor(provinces[gl_InstanceIndex].regionId);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -98,8 +98,8 @@ export
Deallocate(diskSystem); Deallocate(diskSystem);
} }
void LoadDir(ee_assets::AssetType type, const std::filesystem::path& path) { void LoadDir(const ee_assets::AssetType type, const std::filesystem::path& path) {
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(path)) { for (const std::filesystem::directory_entry& entry : std::filesystem::recursive_directory_iterator(path)) {
if (entry.is_directory()) continue; if (entry.is_directory()) continue;
LoadAsync(type, entry.path()); LoadAsync(type, entry.path());
} }

View File

@ -43,7 +43,6 @@ export namespace ee_assets
} }
export namespace ee_sdk { export namespace ee_sdk {
std::optional<json> ReadJson(const std::filesystem::path &assetPath) { std::optional<json> ReadJson(const std::filesystem::path &assetPath) {
std::ifstream fin(assetPath); std::ifstream fin(assetPath);
if (!fin.is_open()) { if (!fin.is_open()) {

View File

@ -13,6 +13,12 @@ import std;
using json = nlohmann::json; using json = nlohmann::json;
using path = std::filesystem::path; using path = std::filesystem::path;
void IfNotExluded(const std::vector<path> &exluded, const path &a, const std::function<void()> &callback) {
if (std::ranges::find(exluded, a) == exluded.end())
callback();
else LOG(INFO, "SKIP EXCLUDED [%s]", a.c_str());
}
template <ee_assets::Asset TAsset> template <ee_assets::Asset TAsset>
void CompileJson(const ee_sdk::JsonCompiler &compiler, const path &relative, const path &src, const path &out) { void CompileJson(const ee_sdk::JsonCompiler &compiler, const path &relative, const path &src, const path &out) {
LOG(INFO, "Start compiling %s...", relative.c_str()); LOG(INFO, "Start compiling %s...", relative.c_str());
@ -46,6 +52,32 @@ void CompileMaps(const ee_sdk::MapMeshCompiler &compiler, const path &relative,
} }
} }
void CompileSPIRV(const path &compiler, const path &relative, const path &src, const path &out) {
LOG(INFO, "Start compiling %s...", relative.c_str());
for (const std::filesystem::directory_entry &asset : std::filesystem::recursive_directory_iterator(src/relative)) {
if (!asset.is_regular_file()) continue;
const path shader = std::filesystem::relative(asset.path(), src/relative);
std::filesystem::create_directories((out/relative/shader).parent_path());
const std::string cmd = std::format("{} \"{}\" -O -o \"{}\"",
compiler.string(),
asset.path().string(),
(out/relative/shader).string()
);
LOG(INFO, "Run [%hs]", cmd.c_str());
if (
const std::int32_t result = std::system(cmd.c_str());
result == 0
) {
LOG(INFO, "COMPILED SPIR-V SHADER: [%s]", (relative/shader).c_str());
}
else LOG(CRITICAL, "COMPILATION FAILED: [%s]. Result code: %d", (relative/shader).c_str(), result);
}
}
int main(const int argc, char **argv) { int main(const int argc, char **argv) {
std::uint32_t status = 0; std::uint32_t status = 0;
@ -61,6 +93,8 @@ int main(const int argc, char **argv) {
options.add_options() options.add_options()
("src", "Path to your mod source assets", cxxopts::value<path>()) ("src", "Path to your mod source assets", cxxopts::value<path>())
("o,out", "Path to compiled assets", cxxopts::value<path>()->default_value("out")) ("o,out", "Path to compiled assets", cxxopts::value<path>()->default_value("out"))
("e,exclude", "List of excluded asset types", cxxopts::value<std::vector<path>>()->default_value(""))
("E,exclude-path", "List of exluded asset paths", cxxopts::value<std::vector<path>>()->default_value(""))
("h,help", "Print usage"); ("h,help", "Print usage");
if ( if (
@ -73,12 +107,25 @@ int main(const int argc, char **argv) {
const path &src = cmd["src"].as<path>(); const path &src = cmd["src"].as<path>();
const path &out = cmd["out"].as<path>(); const path &out = cmd["out"].as<path>();
const std::vector<path> &excludedTypes = cmd["exclude"].as<std::vector<path>>();
const std::vector<path> &excludedPaths = cmd["exclude-path"].as<std::vector<path>>();
const ee_sdk::JsonCompiler jsonCompiler(src/"regions.json"); const ee_sdk::JsonCompiler jsonCompiler(src/"regions.json");
IfNotExluded(excludedTypes, "materials", [jsonCompiler, src, out] {
CompileJson<ee_assets::AssetMaterial>(jsonCompiler, "materials", src, out); CompileJson<ee_assets::AssetMaterial>(jsonCompiler, "materials", src, out);
});
IfNotExluded(excludedTypes, "scenarios", [jsonCompiler, src, out] {
CompileJson<ee_assets::AssetSave>(jsonCompiler, "scenarios", src, out); CompileJson<ee_assets::AssetSave>(jsonCompiler, "scenarios", src, out);
});
constexpr ee_sdk::MapMeshCompiler mapMeshCompiler; constexpr ee_sdk::MapMeshCompiler mapMeshCompiler;
IfNotExluded(excludedTypes, "map", [mapMeshCompiler, src, out] {
CompileMaps(mapMeshCompiler, "map", src, out); CompileMaps(mapMeshCompiler, "map", src, out);
});
IfNotExluded(excludedTypes, "shaders", [src, out] {
CompileSPIRV("glslc", "shaders", src, out);
});
} }
} }