Luaエンジン:sizeof()関数

守矢諏訪子 <suwako@076.moe>、2026-06-27 03:34
コミット d49c30886768bdc2a87f5d9358e3d2eab06d9193 ・親 c46af6b ・ファイルを見る

2個のファイル、+49 -4

@@ -94,7 +94,9 @@
9494 }
9595
9696 string NewShader::getDefaultLua() {
97- return R"(local mesh = {
97+ return R"(local stride = le.sizeof("vec2") + le.sizeof("vec3")
98+
99+local mesh = {
98100 vertices = {
99101 0, .5, 1, 0, 0,
100102 -.5, -.5, 0, 1, 0,
@@ -103,8 +105,18 @@
103105 indices = { 0, 1, 2 },
104106
105107 attributes = {
106- { location = 0, size = 2, stride = 20, offset = 0 }, -- position
107- { location = 1, size = 3, stride = 20, offset = 8 } -- uv
108+ {
109+ location = 0,
110+ size = 2,
111+ stride = stride,
112+ offset = 0
113+ }, -- position
114+ {
115+ location = 1,
116+ size = 3,
117+ stride = stride,
118+ offset = le.sizeof("vec2")
119+ } -- uv
108120 }
109121 }
110122
@@ -420,8 +420,41 @@
420420 gui::LogEntry entry = {};
421421 entry.type = gui::LogType::Normal;
422422 entry.text = out;
423- m_Info->cmd->Add(entry); (entry);
423+ m_Info->cmd->Add(entry);
424424 std::cerr << entry.text << std::endl;
425+ };
426+
427+ lua["le"]["sizeof"] = [&](const string &type) -> size_t {
428+ static const std::unordered_map<string, size_t> sizes = {
429+ { "float", sizeof(f32) },
430+ { "double", sizeof(f64) },
431+ { "int", sizeof(i32) },
432+ { "uint", sizeof(u32) },
433+
434+ { "vec2", sizeof(f32) * 2 },
435+ { "vec3", sizeof(f32) * 3 },
436+ { "vec4", sizeof(f32) * 4 },
437+
438+ { "ivec2", sizeof(i32) * 2 },
439+ { "ivec3", sizeof(i32) * 3 },
440+ { "ivec4", sizeof(i32) * 4 },
441+
442+ { "uvec2", sizeof(u32) * 2 },
443+ { "uvec3", sizeof(u32) * 3 },
444+ { "uvec4", sizeof(u32) * 4 },
445+ };
446+
447+ auto it = sizes.find(type);
448+ if (it == sizes.end()) {
449+ gui::LogEntry entry = {};
450+ entry.type = gui::LogType::Error;
451+ entry.text = "不正な値類: " + type;
452+ m_Info->cmd->Add(entry);
453+ std::cerr << entry.text << std::endl;
454+ return 0;
455+ }
456+
457+ return it->second;
425458 };
426459 }
427460