This commit is contained in:
2025-03-17 22:41:04 +01:00
parent cbcc1b7e25
commit b70317d85d
37 changed files with 698 additions and 229 deletions

72
Tests/Backend.c git.filemode.normal_file
View File

@@ -0,0 +1,72 @@
#include "Common.h"
#include <unity/unity.h>
#include <Pulse.h>
void DumbDebugCallBack(PulseDebugMessageSeverity severity, const char* message)
{
(void)severity;
(void)message;
}
void TestVulkanSupport()
{
#if defined(VULKAN_ENABLED)
if(!PulseSupportsBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT))
#elif defined(WEBGPU_ENABLED)
if(!PulseSupportsBackend(PULSE_BACKEND_WEBGPU, PULSE_SHADER_FORMAT_WGSL_BIT))
#endif
{
TEST_MESSAGE("Vulkan is not supported");
exit(0);
}
}
void TestBackendSetup()
{
#if defined(VULKAN_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_HIGH_DEBUG);
#elif defined(WEBGPU_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_WEBGPU, PULSE_SHADER_FORMAT_WGSL_BIT, PULSE_HIGH_DEBUG);
#endif
TEST_ASSERT_NOT_EQUAL_MESSAGE(backend, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestBackendAnySetup()
{
#if defined(VULKAN_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_ANY, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_HIGH_DEBUG);
#elif defined(WEBGPU_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_ANY, PULSE_SHADER_FORMAT_WGSL_BIT, PULSE_HIGH_DEBUG);
#endif
TEST_ASSERT_NOT_EQUAL_MESSAGE(backend, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
#if defined(VULKAN_ENABLED)
TEST_ASSERT_EQUAL(PulseGetBackendType(backend), PULSE_BACKEND_VULKAN);
#elif defined(WEBGPU_ENABLED)
TEST_ASSERT_EQUAL(PulseGetBackendType(backend), PULSE_BACKEND_WEBGPU);
#endif
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestWrongBackendSetup()
{
#if defined(VULKAN_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_MSL_BIT, PULSE_HIGH_DEBUG);
#elif defined(WEBGPU_ENABLED)
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_WEBGPU, PULSE_SHADER_FORMAT_MSL_BIT, PULSE_HIGH_DEBUG);
#endif
TEST_ASSERT_EQUAL(backend, PULSE_NULL_HANDLE);
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestBackend()
{
RUN_TEST(TestVulkanSupport);
RUN_TEST(TestBackendSetup);
RUN_TEST(TestBackendAnySetup);
RUN_TEST(TestWrongBackendSetup);
}

View File

@@ -54,15 +54,6 @@ void TestBufferCreation()
TEST_ASSERT_NOT_EQUAL_MESSAGE(buffer, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
PulseDestroyBuffer(device, buffer);
DISABLE_ERRORS;
buffer_create_info.size = -1;
buffer_create_info.usage = PULSE_BUFFER_USAGE_STORAGE_READ;
buffer = PulseCreateBuffer(device, &buffer_create_info);
TEST_ASSERT_EQUAL(buffer, PULSE_NULL_HANDLE);
PulseGetLastErrorType(); // Just to clear the error code
PulseDestroyBuffer(device, buffer);
ENABLE_ERRORS;
CleanupDevice(device);
CleanupPulse(backend);
}
@@ -278,9 +269,14 @@ void TestBufferComputeWrite()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/SimpleBufferWrite.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/SimpleBufferWrite.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/SimpleBufferWrite.wgsl.h"
#endif
PulseBufferCreateInfo buffer_create_info = { 0 };
buffer_create_info.size = 256 * sizeof(int32_t);
@@ -300,7 +296,7 @@ void TestBufferComputeWrite()
TEST_ASSERT_NOT_EQUAL_MESSAGE(pass, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
PulseBindStorageBuffers(pass, &buffer, 1);
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));
@@ -340,9 +336,14 @@ void TestBufferComputeCopy()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/BufferCopy.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/BufferCopy.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/BufferCopy.wgsl.h"
#endif
uint32_t data[256];
memset(data, 0xFF, 256 * sizeof(uint32_t));
@@ -386,7 +387,7 @@ void TestBufferComputeCopy()
PulseBindStorageBuffers(pass, &read_buffer, 1);
PulseBindStorageBuffers(pass, &write_buffer, 1);
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));

View File

@@ -1,5 +1,7 @@
#include "Common.h"
#include <unity/unity.h>
#include <stdio.h>
#include <string.h>
bool errors_enabled = true;
bool has_recieved_error = false;
@@ -7,7 +9,10 @@ bool has_recieved_error = false;
void DebugCallBack(PulseDebugMessageSeverity severity, const char* message)
{
if(errors_enabled && severity == PULSE_DEBUG_MESSAGE_SEVERITY_ERROR)
TEST_FAIL_MESSAGE(message);
{
fprintf(stderr, "%s", message);
TEST_FAIL();
}
has_recieved_error = true;
}
@@ -15,11 +20,15 @@ void DebugCallBack(PulseDebugMessageSeverity severity, const char* message)
void SetupPulse(PulseBackend* backend)
{
*backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_PARANOID_DEBUG);
#if defined(VULKAN_ENABLED)
*backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_PARANOID_DEBUG);
#elif defined(WEBGPU_ENABLED)
*backend = PulseLoadBackend(PULSE_BACKEND_WEBGPU, PULSE_SHADER_FORMAT_WGSL_BIT, PULSE_PARANOID_DEBUG);
#endif
if(*backend == PULSE_NULL_HANDLE)
{
char complete_message[LOG_MESSAGE_MAX_LENGTH] = { 0 };
snprintf(complete_message, LOG_MESSAGE_MAX_LENGTH, "Fatal Error: could not load Pulse using Vulkan due to %s", PulseVerbaliseErrorType(PulseGetLastErrorType()));
snprintf(complete_message, LOG_MESSAGE_MAX_LENGTH, "Fatal Error: could not load Pulse backend due to %s", PulseVerbaliseErrorType(PulseGetLastErrorType()));
TEST_FAIL_MESSAGE(complete_message);
TEST_ABORT();
}
@@ -50,10 +59,18 @@ void LoadComputePipeline(PulseDevice device, PulseComputePipeline* pipeline, con
uint32_t num_uniform_buffers)
{
PulseComputePipelineCreateInfo info = { 0 };
info.code_size = code_size;
#if defined(WEBGPU_ENABLED)
info.code_size = strlen(code);
#else
info.code_size = code_size;
#endif
info.code = code;
info.entrypoint = "main";
info.format = PULSE_SHADER_FORMAT_SPIRV_BIT;
#if defined(VULKAN_ENABLED)
info.format = PULSE_SHADER_FORMAT_SPIRV_BIT;
#elif defined(WEBGPU_ENABLED)
info.format = PULSE_SHADER_FORMAT_WGSL_BIT;
#endif
info.num_readonly_storage_images = num_readonly_storage_images;
info.num_readonly_storage_buffers = num_readonly_storage_buffers;
info.num_readwrite_storage_buffers = num_readwrite_storage_buffers;

View File

@@ -46,7 +46,11 @@ void TestBackendInUse()
PulseDevice device = PulseCreateDevice(backend, NULL, 0);
TEST_ASSERT_NOT_EQUAL_MESSAGE(device, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
TEST_ASSERT_EQUAL(PulseGetBackendInUseByDevice(device), PULSE_BACKEND_VULKAN);
#if defined(VULKAN_ENABLED)
TEST_ASSERT_EQUAL(PulseGetBackendInUseByDevice(device), PULSE_BACKEND_VULKAN);
#elif defined(WEBGPU_ENABLED)
TEST_ASSERT_EQUAL(PulseGetBackendInUseByDevice(device), PULSE_BACKEND_WEBGPU);
#endif
PulseDestroyDevice(device);
CleanupPulse(backend);
@@ -59,7 +63,11 @@ void TestShaderFormatSupport()
PulseDevice device = PulseCreateDevice(backend, NULL, 0);
TEST_ASSERT_NOT_EQUAL_MESSAGE(device, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
TEST_ASSERT_TRUE(PulseDeviceSupportsShaderFormats(device, PULSE_SHADER_FORMAT_SPIRV_BIT));
#if defined(VULKAN_ENABLED)
TEST_ASSERT_TRUE(PulseDeviceSupportsShaderFormats(device, PULSE_SHADER_FORMAT_SPIRV_BIT));
#elif defined(WEBGPU_ENABLED)
TEST_ASSERT_TRUE(PulseDeviceSupportsShaderFormats(device, PULSE_SHADER_FORMAT_WGSL_BIT));
#endif
PulseDestroyDevice(device);
CleanupPulse(backend);

View File

@@ -10,9 +10,14 @@ void TestPipelineSetup()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/Simple.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/Simple.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/Simple.wgsl.h"
#endif
PulseComputePipeline pipeline;
LoadComputePipeline(device, &pipeline, shader_bytecode, sizeof(shader_bytecode), 0, 0, 0, 0, 0);
@@ -25,7 +30,7 @@ void TestPipelineSetup()
PulseComputePass pass = PulseBeginComputePass(cmd);
TEST_ASSERT_NOT_EQUAL_MESSAGE(pass, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));
@@ -46,9 +51,14 @@ void TestPipelineReadOnlyBindings()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/ReadOnlyBindings.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/ReadOnlyBindings.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/ReadOnlyBindings.wgsl.h"
#endif
PulseBufferCreateInfo buffer_create_info = { 0 };
buffer_create_info.size = 256 * sizeof(int32_t);
@@ -79,7 +89,7 @@ void TestPipelineReadOnlyBindings()
PulseBindStorageImages(pass, &image, 1);
PulseBindStorageBuffers(pass, &buffer, 1);
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));
@@ -102,9 +112,14 @@ void TestPipelineWriteOnlyBindings()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/ReadOnlyBindings.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/WriteOnlyBindings.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/WriteOnlyBindings.wgsl.h"
#endif
PulseBufferCreateInfo buffer_create_info = { 0 };
buffer_create_info.size = 256 * sizeof(int32_t);
@@ -135,7 +150,7 @@ void TestPipelineWriteOnlyBindings()
PulseBindStorageImages(pass, &image, 1);
PulseBindStorageBuffers(pass, &buffer, 1);
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));
@@ -158,9 +173,14 @@ void TestPipelineReadWriteBindings()
PulseDevice device;
SetupDevice(backend, &device);
const uint8_t shader_bytecode[] = {
#include "Shaders/ReadOnlyBindings.spv.h"
};
#if defined(VULKAN_ENABLED)
const uint8_t shader_bytecode[] = {
#include "Shaders/Vulkan/ReadWriteBindings.spv.h"
};
#elif defined(WEBGPU_ENABLED)
#define SHADER_NAME shader_bytecode
#include "Shaders/WebGPU/ReadWriteBindings.wgsl.h"
#endif
PulseBufferCreateInfo buffer_create_info = { 0 };
buffer_create_info.size = 256 * sizeof(int32_t);
@@ -201,7 +221,7 @@ void TestPipelineReadWriteBindings()
PulseBindStorageBuffers(pass, &read_buffer, 1);
PulseBindStorageBuffers(pass, &write_buffer, 1);
PulseBindComputePipeline(pass, pipeline);
PulseDispatchComputations(pass, 32, 32, 1);
PulseDispatchComputations(pass, 16, 1, 1);
PulseEndComputePass(pass);
TEST_ASSERT_TRUE_MESSAGE(PulseSubmitCommandList(device, cmd, fence), PulseVerbaliseErrorType(PulseGetLastErrorType()));

2
Tests/Shaders/.gitignore vendored git.filemode.normal_file
View File

@@ -0,0 +1,2 @@
*.spv.h
*.wgsl.h

View File

@@ -19,7 +19,7 @@ external
}
[entry(compute)]
[workgroup(32, 32, 1)]
[workgroup(16, 16, 1)]
fn main(input: Input)
{
write_ssbo.data[input.indices.x * input.indices.y] = read_ssbo.data[input.indices.x * input.indices.y];

View File

@@ -19,7 +19,7 @@ external
}
[entry(compute)]
[workgroup(32, 32, 1)]
[workgroup(16, 16, 1)]
fn main(input: Input)
{
}

View File

@@ -21,7 +21,7 @@ external
}
[entry(compute)]
[workgroup(32, 32, 1)]
[workgroup(16, 16, 1)]
fn main(input: Input)
{
}

View File

@@ -18,7 +18,7 @@ external
}
[entry(compute)]
[workgroup(32, 32, 1)]
[workgroup(16, 16, 1)]
fn main(input: Input)
{
ssbo.data[input.indices.x * input.indices.y] = u32(0xFFFFFFFF);

View File

@@ -14,12 +14,12 @@ struct SSBO
external
{
[set(1), binding(0)] write_texture: texture2D[f32, readonly, rgba8],
[set(1), binding(0)] write_texture: texture2D[f32, readwrite, rgba8],
[set(1), binding(1)] write_ssbo: storage[SSBO],
}
[entry(compute)]
[workgroup(32, 32, 1)]
[workgroup(16, 16, 1)]
fn main(input: Input)
{
}

8
Tests/Shaders/WebGPU/BufferCopy.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,8 @@
@group(0) @binding(0) var<storage, read_only> read_ssbo: array<u32>;
@group(1) @binding(0) var<storage, read_write> write_ssbo: array<u32>;
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
write_ssbo[grid.x * grid.y] = read_ssbo[grid.x * grid.y];
}

7
Tests/Shaders/WebGPU/ReadOnlyBindings.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,7 @@
@group(0) @binding(0) var<storage, read_only> read_ssbo: array<u32>;
@group(0) @binding(1) var read_texture: texture_storage_2d<rgba8unorm, read>;
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
}

9
Tests/Shaders/WebGPU/ReadWriteBindings.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,9 @@
@group(0) @binding(0) var<storage, read_only> read_ssbo: array<u32>;
@group(0) @binding(1) var read_texture: texture_storage_2d<rgba8unorm, read>;
@group(1) @binding(0) var<storage, read_write> write_ssbo: array<u32>;
@group(1) @binding(1) var write_texture: texture_storage_2d<rgba8unorm, read_write>;
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
}

4
Tests/Shaders/WebGPU/Simple.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,4 @@
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
}

7
Tests/Shaders/WebGPU/SimpleBufferWrite.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,7 @@
@group(1) @binding(0) var<storage, read_write> write_ssbo: array<u32>;
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
write_ssbo[grid.x * grid.y] = u32(0xFFFFFFFF);
}

7
Tests/Shaders/WebGPU/WriteOnlyBindings.wgsl git.filemode.normal_file
View File

@@ -0,0 +1,7 @@
@group(1) @binding(0) var<storage, read_write> write_ssbo: array<u32>;
@group(1) @binding(1) var write_texture: texture_storage_2d<rgba8unorm, write>;
@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) grid: vec3<u32>)
{
}

View File

@@ -1,52 +0,0 @@
#include "Common.h"
#include <unity/unity.h>
#include <Pulse.h>
void DumbDebugCallBack(PulseDebugMessageSeverity severity, const char* message)
{
(void)severity;
(void)message;
}
void TestVulkanSupport()
{
if(!PulseSupportsBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT))
{
TEST_MESSAGE("Vulkan is not supported");
exit(0);
}
}
void TestBackendSetup()
{
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_HIGH_DEBUG);
TEST_ASSERT_NOT_EQUAL_MESSAGE(backend, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestBackendAnySetup()
{
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_ANY, PULSE_SHADER_FORMAT_SPIRV_BIT, PULSE_HIGH_DEBUG);
TEST_ASSERT_NOT_EQUAL_MESSAGE(backend, PULSE_NULL_HANDLE, PulseVerbaliseErrorType(PulseGetLastErrorType()));
TEST_ASSERT_EQUAL(PulseGetBackendType(backend), PULSE_BACKEND_VULKAN);
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestWrongBackendSetup()
{
PulseBackend backend = PulseLoadBackend(PULSE_BACKEND_VULKAN, PULSE_SHADER_FORMAT_MSL_BIT, PULSE_HIGH_DEBUG);
TEST_ASSERT_EQUAL(backend, PULSE_NULL_HANDLE);
PulseSetDebugCallback(backend, DumbDebugCallBack);
PulseUnloadBackend(backend);
}
void TestBackend()
{
RUN_TEST(TestVulkanSupport);
RUN_TEST(TestBackendSetup);
RUN_TEST(TestBackendAnySetup);
RUN_TEST(TestWrongBackendSetup);
}

View File

@@ -1 +0,0 @@
*.spv.h

View File

@@ -1,114 +0,0 @@
option("vulkan-tests", { description = "Build Vulkan tests", default = false })
add_repositories("nazara-engine-repo https://github.com/NazaraEngine/xmake-repo")
add_requires("nzsl >=2023.12.31", { configs = { shared = false, nzslc = true } })
if is_cross() then
add_requires("nzsl~host", { kind = "binary", host = true })
end
-- Yoinked from NZSL xmake repo
rule("find_nzsl")
on_config(function(target)
import("core.project.project")
import("core.tool.toolchain")
import("lib.detect.find_tool")
local envs
if is_plat("windows") then
local msvc = target:toolchain("msvc")
if msvc and msvc:check() then
envs = msvc:runenvs()
end
elseif is_plat("mingw") then
local mingw = target:toolchain("mingw")
if mingw and mingw:check() then
envs = mingw:runenvs()
end
end
target:data_set("nzsl_envs", envs)
local nzsl = project.required_package("nzsl~host") or project.required_package("nzsl")
local nzsldir
if nzsl then
nzsldir = path.join(nzsl:installdir(), "bin")
local osenvs = os.getenvs()
envs = envs or {}
for env, values in pairs(nzsl:get("envs")) do
local flatval = path.joinenv(values)
local oldenv = envs[env] or osenvs[env]
if not oldenv or oldenv == "" then
envs[env] = flatval
elseif not oldenv:startswith(flatval) then
envs[env] = flatval .. path.envsep() .. oldenv
end
end
end
local nzsla = find_tool("nzsla", { version = true, paths = nzsldir, envs = envs })
local nzslc = find_tool("nzslc", { version = true, paths = nzsldir, envs = envs })
target:data_set("nzsla", nzsla)
target:data_set("nzslc", nzslc)
target:data_set("nzsl_runenv", envs)
end)
rule_end()
rule("compile_shaders")
set_extensions(".nzsl")
add_deps("find_nzsl")
before_buildcmd_file(function(target, batchcmds, shaderfile, opt)
local outputdir = target:data("nzsl_includedirs")
local nzslc = target:data("nzslc")
local runenvs = target:data("nzsl_runenv")
assert(nzslc, "nzslc not found! please install nzsl package with nzslc enabled")
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
local argv = { "--compile=spv-header", "--optimize" }
if outputdir then
batchcmds:mkdir(outputdir)
table.insert(argv, "--output=" .. outputdir)
end
local kind = target:data("plugin.project.kind") or ""
if kind:match("vs") then
table.insert(argv, "--log-format=vs")
end
table.insert(argv, shaderfile)
batchcmds:vrunv(nzslc.program, argv, { curdir = ".", envs = runenvs })
local outputfile = path.join(outputdir or path.directory(shaderfile), path.basename(shaderfile) .. ".spv.h")
batchcmds:add_depfiles(shaderfile)
batchcmds:add_depvalues(nzslc.version)
batchcmds:set_depmtime(os.mtime(outputfile))
batchcmds:set_depcache(target:dependfile(outputfile))
end)
rule_end()
if has_config("vulkan-tests") then
set_group("VulkanTests")
add_requires("unity_test")
if is_plat("linux") then
add_requires("libbacktrace")
end
target("VulkanUnitTests")
set_kind("binary")
add_deps("pulse_gpu")
add_rules("compile_shaders")
add_files("**.c")
add_files("**.nzsl")
add_packages("unity_test")
if is_plat("linux") then
add_packages("libbacktrace")
set_extension(".x86_64")
add_defines("BACKTRACE")
end
target_end()
end

148
Tests/xmake.lua git.filemode.normal_file
View File

@@ -0,0 +1,148 @@
local tests = {
Vulkan = {
option = "vulkan",
packages = { "nzsl" },
global_custom = function()
add_repositories("nazara-engine-repo https://github.com/NazaraEngine/xmake-repo")
add_requires("nzsl >=2023.12.31", { configs = { shared = false, nzslc = true } })
if is_cross() then
add_requires("nzsl~host", { kind = "binary", host = true })
end
-- Yoinked from NZSL xmake repo
rule("find_nzsl")
on_config(function(target)
import("core.project.project")
import("core.tool.toolchain")
import("lib.detect.find_tool")
local envs
if is_plat("windows") then
local msvc = target:toolchain("msvc")
if msvc and msvc:check() then
envs = msvc:runenvs()
end
elseif is_plat("mingw") then
local mingw = target:toolchain("mingw")
if mingw and mingw:check() then
envs = mingw:runenvs()
end
end
target:data_set("nzsl_envs", envs)
local nzsl = project.required_package("nzsl~host") or project.required_package("nzsl")
local nzsldir
if nzsl then
nzsldir = path.join(nzsl:installdir(), "bin")
local osenvs = os.getenvs()
envs = envs or {}
for env, values in pairs(nzsl:get("envs")) do
local flatval = path.joinenv(values)
local oldenv = envs[env] or osenvs[env]
if not oldenv or oldenv == "" then
envs[env] = flatval
elseif not oldenv:startswith(flatval) then
envs[env] = flatval .. path.envsep() .. oldenv
end
end
end
local nzsla = find_tool("nzsla", { version = true, paths = nzsldir, envs = envs })
local nzslc = find_tool("nzslc", { version = true, paths = nzsldir, envs = envs })
target:data_set("nzsla", nzsla)
target:data_set("nzslc", nzslc)
target:data_set("nzsl_runenv", envs)
end)
rule_end()
rule("nzsl_compile_shaders")
set_extensions(".nzsl")
add_deps("find_nzsl")
before_buildcmd_file(function(target, batchcmds, shaderfile, opt)
local outputdir = target:data("nzsl_includedirs")
local nzslc = target:data("nzslc")
local runenvs = target:data("nzsl_runenv")
assert(nzslc, "nzslc not found! please install nzsl package with nzslc enabled")
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
local argv = { "--compile=spv-header", "--optimize" }
if outputdir then
batchcmds:mkdir(outputdir)
table.insert(argv, "--output=" .. outputdir)
end
local kind = target:data("plugin.project.kind") or ""
if kind:match("vs") then
table.insert(argv, "--log-format=vs")
end
table.insert(argv, shaderfile)
batchcmds:vrunv(nzslc.program, argv, { curdir = ".", envs = runenvs })
local outputfile = path.join(outputdir or path.directory(shaderfile), path.basename(shaderfile) .. ".spv.h")
batchcmds:add_depfiles(shaderfile)
batchcmds:add_depvalues(nzslc.version)
batchcmds:set_depmtime(os.mtime(outputfile))
batchcmds:set_depcache(target:dependfile(outputfile))
end)
rule_end()
end,
custom = function()
add_rules("nzsl_compile_shaders")
add_files("**.nzsl")
end
},
WebGPU = {
option = "webgpu",
global_custom = function()
rule("wgsl_compile_shaders")
set_extensions(".wgsl")
before_buildcmd_file(function(target, batchcmds, shaderfile, opt)
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
io.writefile(shaderfile .. ".h", [[
// Generated File
#undef WGSL_SOURCE
#define WGSL_SOURCE(...) #__VA_ARGS__
const uint8_t SHADER_NAME[] = WGSL_SOURCE(
]] .. io.readfile(shaderfile) .. [[
);]])
end)
rule_end()
end,
custom = function()
add_rules("wgsl_compile_shaders")
add_files("**.wgsl")
end
}
}
if is_plat("linux") then
add_requires("libbacktrace")
end
add_requires("unity_test")
for name, module in table.orderpairs(tests) do
if module.option then
option(module.option .. "-tests", { description = "Enables " .. name .. " tests", default = false })
end
end
for name, module in pairs(tests) do
if has_config(module.option) then
if module.global_custom then
module.global_custom()
end
if module.packages then
add_requires(table.unpack(module.packages))
end
target(name .. "UnitTests")
set_kind("binary")
add_deps("pulse_gpu")
add_packages("unity_test")
add_files("**.c")
add_defines(string.upper(name) .. "_ENABLED")
if module.custom then
module.custom()
end
if is_plat("linux") then
add_packages("libbacktrace")
set_extension(".x86_64")
add_defines("BACKTRACE")
end
target_end()
end
end